1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
4 * of PCI-SCSI IO processors.
6 * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
7 * Copyright (c) 2003-2005 Matthew Wilcox <matthew@wil.cx>
9 * This driver is derived from the Linux sym53c8xx driver.
10 * Copyright (C) 1998-2000 Gerard Roudier
12 * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
13 * a port of the FreeBSD ncr driver to Linux-1.2.13.
15 * The original ncr driver has been written for 386bsd and FreeBSD by
16 * Wolfgang Stanglmeier <wolf@cologne.de>
17 * Stefan Esser <se@mi.Uni-Koeln.de>
18 * Copyright (C) 1994 Wolfgang Stanglmeier
20 * Other major contributions:
22 * NVRAM detection and reading.
23 * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
25 *-----------------------------------------------------------------------------
27 #include <linux/ctype.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/spinlock.h>
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_transport.h>
38 #include "sym_nvram.h"
40 #define NAME53C "sym53c"
41 #define NAME53C8XX "sym53c8xx"
43 struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
44 unsigned int sym_debug_flags = 0;
46 static char *excl_string;
47 static char *safe_string;
48 module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
49 module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
50 module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
51 module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
52 module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
53 module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
54 module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
55 module_param_named(verb, sym_driver_setup.verbose, byte, 0);
56 module_param_named(debug, sym_debug_flags, uint, 0);
57 module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
58 module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
59 module_param_named(excl, excl_string, charp, 0);
60 module_param_named(safe, safe_string, charp, 0);
62 MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
63 MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
64 MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
65 MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
66 MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
67 MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
68 MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
69 MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
70 MODULE_PARM_DESC(debug, "Set bits to enable debugging");
71 MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
72 MODULE_PARM_DESC(nvram, "Option currently not used");
73 MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
74 MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
76 MODULE_LICENSE("GPL");
77 MODULE_VERSION(SYM_VERSION);
78 MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
79 MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
81 static void sym2_setup_params(void)
83 char *p = excl_string;
86 while (p && (xi < 8)) {
88 int val = (int) simple_strtoul(p, &next_p, 0);
89 sym_driver_setup.excludes[xi++] = val;
94 if (*safe_string == 'y') {
95 sym_driver_setup.max_tag = 0;
96 sym_driver_setup.burst_order = 0;
97 sym_driver_setup.scsi_led = 0;
98 sym_driver_setup.scsi_diff = 1;
99 sym_driver_setup.irq_mode = 0;
100 sym_driver_setup.scsi_bus_check = 2;
101 sym_driver_setup.host_id = 7;
102 sym_driver_setup.verbose = 2;
103 sym_driver_setup.settle_delay = 10;
104 sym_driver_setup.use_nvram = 1;
105 } else if (*safe_string != 'n') {
106 printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
107 " passed to safe option", safe_string);
112 static struct scsi_transport_template *sym2_transport_template = NULL;
115 * Driver private area in the SCSI command structure.
117 struct sym_ucmd { /* Override the SCSI pointer structure */
118 struct completion *eh_done; /* SCSI error handling */
121 #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)scsi_cmd_priv(cmd))
122 #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
125 * Complete a pending CAM CCB.
127 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
129 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
132 complete(ucmd->eh_done);
139 * Tell the SCSI layer about a BUS RESET.
141 void sym_xpt_async_bus_reset(struct sym_hcb *np)
143 printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
144 np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
145 np->s.settle_time_valid = 1;
146 if (sym_verbose >= 2)
147 printf_info("%s: command processing suspended for %d seconds\n",
148 sym_name(np), sym_driver_setup.settle_delay);
152 * Choose the more appropriate CAM status if
153 * the IO encountered an extended error.
155 static int sym_xerr_cam_status(int cam_status, int x_status)
158 if (x_status & XE_PARITY_ERR)
159 cam_status = DID_PARITY;
161 cam_status = DID_ERROR;
167 * Build CAM result for a failed or auto-sensed IO.
169 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
171 struct scsi_cmnd *cmd = cp->cmd;
172 u_int cam_status, scsi_status;
175 scsi_status = cp->ssss_status;
177 if (cp->host_flags & HF_SENSE) {
178 scsi_status = cp->sv_scsi_status;
179 resid = cp->sv_resid;
180 if (sym_verbose && cp->sv_xerr_status)
181 sym_print_xerr(cmd, cp->sv_xerr_status);
182 if (cp->host_status == HS_COMPLETE &&
183 cp->ssss_status == S_GOOD &&
184 cp->xerr_status == 0) {
185 cam_status = sym_xerr_cam_status(DID_OK,
188 * Bounce back the sense data to user.
190 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
191 memcpy(cmd->sense_buffer, cp->sns_bbuf,
192 min(SCSI_SENSE_BUFFERSIZE, SYM_SNS_BBUF_LEN));
195 * If the device reports a UNIT ATTENTION condition
196 * due to a RESET condition, we should consider all
197 * disconnect CCBs for this unit as aborted.
201 p = (u_char *) cmd->sense_data;
202 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
203 sym_clear_tasks(np, DID_ABORT,
204 cp->target,cp->lun, -1);
209 * Error return from our internal request sense. This
210 * is bad: we must clear the contingent allegiance
211 * condition otherwise the device will always return
212 * BUSY. Use a big stick.
214 sym_reset_scsi_target(np, cmd->device->id);
215 cam_status = DID_ERROR;
217 } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
219 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
220 cam_status = DID_NO_CONNECT;
221 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
222 cam_status = DID_ERROR;
223 else { /* Extended error */
225 sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
226 cp->host_status, cp->ssss_status,
230 * Set the most appropriate value for CAM status.
232 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
234 scsi_set_resid(cmd, resid);
235 cmd->result = (cam_status << 16) | scsi_status;
238 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
245 use_sg = scsi_dma_map(cmd);
247 struct scatterlist *sg;
248 struct sym_tcb *tp = &np->target[cp->target];
249 struct sym_tblmove *data;
251 if (use_sg > SYM_CONF_MAX_SG) {
256 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
258 scsi_for_each_sg(cmd, sg, use_sg, segment) {
259 dma_addr_t baddr = sg_dma_address(sg);
260 unsigned int len = sg_dma_len(sg);
262 if ((len & 1) && (tp->head.wval & EWS)) {
264 cp->odd_byte_adjustment++;
267 sym_build_sge(np, &data[segment], baddr, len);
278 * Queue a SCSI command.
280 static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
282 struct scsi_device *sdev = cmd->device;
289 * Retrieve the target descriptor.
291 tp = &np->target[sdev->id];
294 * Select tagged/untagged.
296 lp = sym_lp(tp, sdev->lun);
297 order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
302 cp = sym_get_ccb(np, cmd, order);
304 return 1; /* Means resource shortage */
305 sym_queue_scsiio(np, cmd, cp);
310 * Setup buffers and pointers that address the CDB.
312 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
314 memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
316 cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
317 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
323 * Setup pointers that address the data and start the I/O.
325 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
333 if (sym_setup_cdb(np, cmd, cp))
337 * No direction means no data.
339 dir = cmd->sc_data_direction;
340 if (dir != DMA_NONE) {
341 cp->segments = sym_scatter(np, cp, cmd);
342 if (cp->segments < 0) {
343 sym_set_cam_status(cmd, DID_ERROR);
348 * No segments means no data.
358 * Set the data pointer.
361 case DMA_BIDIRECTIONAL:
362 scmd_printk(KERN_INFO, cmd, "got DMA_BIDIRECTIONAL command");
363 sym_set_cam_status(cmd, DID_ERROR);
366 goalp = SCRIPTA_BA(np, data_out2) + 8;
367 lastp = goalp - 8 - (cp->segments * (2*4));
369 case DMA_FROM_DEVICE:
370 cp->host_flags |= HF_DATA_IN;
371 goalp = SCRIPTA_BA(np, data_in2) + 8;
372 lastp = goalp - 8 - (cp->segments * (2*4));
376 lastp = goalp = SCRIPTB_BA(np, no_data);
381 * Set all pointers values needed by SCRIPTS.
383 cp->phys.head.lastp = cpu_to_scr(lastp);
384 cp->phys.head.savep = cpu_to_scr(lastp);
385 cp->startp = cp->phys.head.savep;
386 cp->goalp = cpu_to_scr(goalp);
389 * When `#ifed 1', the code below makes the driver
390 * panic on the first attempt to write to a SCSI device.
391 * It is the first test we want to do after a driver
392 * change that does not seem obviously safe. :)
395 switch (cp->cdb_buf[0]) {
396 case 0x0A: case 0x2A: case 0xAA:
397 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
407 sym_put_start_queue(np, cp);
411 sym_free_ccb(np, cp);
412 sym_xpt_done(np, cmd);
420 * Misused to keep the driver running when
421 * interrupts are not configured correctly.
423 static void sym_timer(struct sym_hcb *np)
425 unsigned long thistime = jiffies;
430 np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
431 add_timer(&np->s.timer);
434 * If we are resetting the ncr, wait for settle_time before
435 * clearing it. Then command processing will be resumed.
437 if (np->s.settle_time_valid) {
438 if (time_before_eq(np->s.settle_time, thistime)) {
439 if (sym_verbose >= 2 )
440 printk("%s: command processing resumed\n",
442 np->s.settle_time_valid = 0;
448 * Nothing to do for now, but that may come.
450 if (np->s.lasttime + 4*HZ < thistime) {
451 np->s.lasttime = thistime;
454 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
456 * Some way-broken PCI bridges may lead to
457 * completions being lost when the clearing
458 * of the INTFLY flag by the CPU occurs
459 * concurrently with the chip raising this flag.
460 * If this ever happen, lost completions will
469 * PCI BUS error handler.
471 void sym_log_bus_error(struct Scsi_Host *shost)
473 struct sym_data *sym_data = shost_priv(shost);
474 struct pci_dev *pdev = sym_data->pdev;
475 unsigned short pci_sts;
476 pci_read_config_word(pdev, PCI_STATUS, &pci_sts);
477 if (pci_sts & 0xf900) {
478 pci_write_config_word(pdev, PCI_STATUS, pci_sts);
479 shost_printk(KERN_WARNING, shost,
480 "PCI bus error: status = 0x%04x\n", pci_sts & 0xf900);
485 * queuecommand method. Entered with the host adapter lock held and
486 * interrupts disabled.
488 static int sym53c8xx_queue_command_lck(struct scsi_cmnd *cmd)
490 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
491 struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
494 memset(ucp, 0, sizeof(*ucp));
497 * Shorten our settle_time if needed for
498 * this command not to time out.
500 if (np->s.settle_time_valid && scsi_cmd_to_rq(cmd)->timeout) {
501 unsigned long tlimit = jiffies + scsi_cmd_to_rq(cmd)->timeout;
502 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
503 if (time_after(np->s.settle_time, tlimit)) {
504 np->s.settle_time = tlimit;
508 if (np->s.settle_time_valid)
509 return SCSI_MLQUEUE_HOST_BUSY;
511 sts = sym_queue_command(np, cmd);
513 return SCSI_MLQUEUE_HOST_BUSY;
517 static DEF_SCSI_QCMD(sym53c8xx_queue_command)
520 * Linux entry point of the interrupt handler.
522 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
524 struct Scsi_Host *shost = dev_id;
525 struct sym_data *sym_data = shost_priv(shost);
528 /* Avoid spinloop trying to handle interrupts on frozen device */
529 if (pci_channel_offline(sym_data->pdev))
532 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
534 spin_lock(shost->host_lock);
535 result = sym_interrupt(shost);
536 spin_unlock(shost->host_lock);
538 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
544 * Linux entry point of the timer handler
546 static void sym53c8xx_timer(struct timer_list *t)
548 struct sym_hcb *np = from_timer(np, t, s.timer);
551 spin_lock_irqsave(np->s.host->host_lock, flags);
553 spin_unlock_irqrestore(np->s.host->host_lock, flags);
558 * What the eh thread wants us to perform.
560 #define SYM_EH_ABORT 0
561 #define SYM_EH_DEVICE_RESET 1
562 #define SYM_EH_BUS_RESET 2
563 #define SYM_EH_HOST_RESET 3
566 * Generic method for our eh processing.
567 * The 'op' argument tells what we have to do.
569 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
571 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
572 struct Scsi_Host *shost = cmd->device->host;
573 struct sym_data *sym_data = shost_priv(shost);
574 struct pci_dev *pdev = sym_data->pdev;
575 struct sym_hcb *np = sym_data->ncb;
579 struct completion eh_done;
581 scmd_printk(KERN_WARNING, cmd, "%s operation started\n", opname);
583 /* We may be in an error condition because the PCI bus
584 * went down. In this case, we need to wait until the
585 * PCI bus is reset, the card is reset, and only then
586 * proceed with the scsi error recovery. There's no
587 * point in hurrying; take a leisurely wait.
589 #define WAIT_FOR_PCI_RECOVERY 35
590 if (pci_channel_offline(pdev)) {
591 int finished_reset = 0;
592 init_completion(&eh_done);
593 spin_lock_irq(shost->host_lock);
594 /* Make sure we didn't race */
595 if (pci_channel_offline(pdev)) {
596 BUG_ON(sym_data->io_reset);
597 sym_data->io_reset = &eh_done;
601 spin_unlock_irq(shost->host_lock);
603 finished_reset = wait_for_completion_timeout
605 WAIT_FOR_PCI_RECOVERY*HZ);
606 spin_lock_irq(shost->host_lock);
607 sym_data->io_reset = NULL;
608 spin_unlock_irq(shost->host_lock);
613 spin_lock_irq(shost->host_lock);
614 /* This one is queued in some place -> to wait for completion */
615 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
616 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
617 if (cp->cmd == cmd) {
623 /* Try to proceed the operation we have been asked for */
627 sts = sym_abort_scsiio(np, cmd, 1);
629 case SYM_EH_DEVICE_RESET:
630 sts = sym_reset_scsi_target(np, cmd->device->id);
632 case SYM_EH_BUS_RESET:
633 sym_reset_scsi_bus(np, 1);
636 case SYM_EH_HOST_RESET:
637 sym_reset_scsi_bus(np, 0);
638 sym_start_up(shost, 1);
645 /* On error, restore everything and cross fingers :) */
650 init_completion(&eh_done);
651 ucmd->eh_done = &eh_done;
652 spin_unlock_irq(shost->host_lock);
653 if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
654 ucmd->eh_done = NULL;
658 spin_unlock_irq(shost->host_lock);
661 dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
662 sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
663 return sts ? SCSI_FAILED : SCSI_SUCCESS;
668 * Error handlers called from the eh thread (one thread per HBA).
670 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
672 return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
675 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
677 return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
680 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
682 return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
685 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
687 return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
691 * Tune device queuing depth, according to various limits.
693 static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
695 struct sym_lcb *lp = sym_lp(tp, lun);
701 oldtags = lp->s.reqtags;
703 if (reqtags > lp->s.scdev_depth)
704 reqtags = lp->s.scdev_depth;
706 lp->s.reqtags = reqtags;
708 if (reqtags != oldtags) {
709 dev_info(&tp->starget->dev,
710 "tagged command queuing %s, command queue depth %d.\n",
711 lp->s.reqtags ? "enabled" : "disabled", reqtags);
715 static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
717 struct sym_hcb *np = sym_get_hcb(sdev->host);
718 struct sym_tcb *tp = &np->target[sdev->id];
723 if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
726 spin_lock_irqsave(np->s.host->host_lock, flags);
729 * Fail the device init if the device is flagged NOSCAN at BOOT in
730 * the NVRAM. This may speed up boot and maintain coherency with
731 * BIOS device numbering. Clearing the flag allows the user to
732 * rescan skipped devices later. We also return an error for
733 * devices not flagged for SCAN LUNS in the NVRAM since some single
734 * lun devices behave badly when asked for a non zero LUN.
737 if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
738 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
739 starget_printk(KERN_INFO, sdev->sdev_target,
740 "Scan at boot disabled in NVRAM\n");
745 if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
746 if (sdev->lun != 0) {
750 starget_printk(KERN_INFO, sdev->sdev_target,
751 "Multiple LUNs disabled in NVRAM\n");
754 lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
760 tp->starget = sdev->sdev_target;
762 spi_min_period(tp->starget) = tp->usr_period;
763 spi_max_width(tp->starget) = tp->usr_width;
767 spin_unlock_irqrestore(np->s.host->host_lock, flags);
773 * Linux entry point for device queue sizing.
775 static int sym53c8xx_slave_configure(struct scsi_device *sdev)
777 struct sym_hcb *np = sym_get_hcb(sdev->host);
778 struct sym_tcb *tp = &np->target[sdev->id];
779 struct sym_lcb *lp = sym_lp(tp, sdev->lun);
780 int reqtags, depth_to_use;
785 lp->curr_flags = lp->user_flags;
788 * Select queue depth from driver setup.
789 * Do not use more than configured by user.
791 * Do not use more than our maximum.
793 reqtags = sym_driver_setup.max_tag;
794 if (reqtags > tp->usrtags)
795 reqtags = tp->usrtags;
796 if (!sdev->tagged_supported)
798 if (reqtags > SYM_CONF_MAX_TAG)
799 reqtags = SYM_CONF_MAX_TAG;
800 depth_to_use = reqtags ? reqtags : 1;
801 scsi_change_queue_depth(sdev, depth_to_use);
802 lp->s.scdev_depth = depth_to_use;
803 sym_tune_dev_queuing(tp, sdev->lun, reqtags);
805 if (!spi_initial_dv(sdev->sdev_target))
811 static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
813 struct sym_hcb *np = sym_get_hcb(sdev->host);
814 struct sym_tcb *tp = &np->target[sdev->id];
815 struct sym_lcb *lp = sym_lp(tp, sdev->lun);
818 /* if slave_alloc returned before allocating a sym_lcb, return */
822 spin_lock_irqsave(np->s.host->host_lock, flags);
824 if (lp->busy_itlq || lp->busy_itl) {
826 * This really shouldn't happen, but we can't return an error
827 * so let's try to stop all on-going I/O.
829 starget_printk(KERN_WARNING, tp->starget,
830 "Removing busy LCB (%d)\n", (u8)sdev->lun);
831 sym_reset_scsi_bus(np, 1);
834 if (sym_free_lcb(np, sdev->id, sdev->lun) == 0) {
836 * It was the last unit for this target.
839 tp->head.wval = np->rv_scntl3;
841 tp->tgoal.check_nego = 1;
845 spin_unlock_irqrestore(np->s.host->host_lock, flags);
849 * Linux entry point for info() function
851 static const char *sym53c8xx_info (struct Scsi_Host *host)
853 return SYM_DRIVER_NAME;
857 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
859 * Proc file system stuff
861 * A read operation returns adapter information.
862 * A write operation is a control command.
863 * The string is parsed in the driver code and the command is passed
864 * to the sym_usercmd() function.
867 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
876 #define UC_SETSYNC 10
877 #define UC_SETTAGS 11
878 #define UC_SETDEBUG 12
879 #define UC_SETWIDE 14
880 #define UC_SETFLAG 15
881 #define UC_SETVERBOSE 17
882 #define UC_RESETDEV 18
883 #define UC_CLEARDEV 19
885 static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
893 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
895 sym_debug_flags = uc->data;
899 np->verbose = uc->data;
903 * We assume that other commands apply to targets.
904 * This should always be the case and avoid the below
905 * 4 lines to be repeated 6 times.
907 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
908 if (!((uc->target >> t) & 1))
917 if (!uc->data || uc->data >= 255) {
918 tp->tgoal.iu = tp->tgoal.dt =
920 tp->tgoal.offset = 0;
921 } else if (uc->data <= 9 && np->minsync_dt) {
922 if (uc->data < np->minsync_dt)
923 uc->data = np->minsync_dt;
924 tp->tgoal.iu = tp->tgoal.dt =
927 tp->tgoal.period = uc->data;
928 tp->tgoal.offset = np->maxoffs_dt;
930 if (uc->data < np->minsync)
931 uc->data = np->minsync;
932 tp->tgoal.iu = tp->tgoal.dt =
934 tp->tgoal.period = uc->data;
935 tp->tgoal.offset = np->maxoffs;
937 tp->tgoal.check_nego = 1;
940 tp->tgoal.width = uc->data ? 1 : 0;
941 tp->tgoal.check_nego = 1;
944 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
945 sym_tune_dev_queuing(tp, l, uc->data);
950 OUTB(np, nc_istat, SIGP|SEM);
953 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
954 struct sym_lcb *lp = sym_lp(tp, l);
955 if (lp) lp->to_clear = 1;
958 OUTB(np, nc_istat, SIGP|SEM);
961 tp->usrflags = uc->data;
969 static int sym_skip_spaces(char *ptr, int len)
973 for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
978 static int get_int_arg(char *ptr, int len, u_long *pv)
982 *pv = simple_strtoul(ptr, &end, 10);
986 static int is_keyword(char *ptr, int len, char *verb)
988 int verb_len = strlen(verb);
990 if (len >= verb_len && !memcmp(verb, ptr, verb_len))
996 #define SKIP_SPACES(ptr, len) \
997 if ((arg_len = sym_skip_spaces(ptr, len)) < 1) \
999 ptr += arg_len; len -= arg_len;
1001 #define GET_INT_ARG(ptr, len, v) \
1002 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
1004 ptr += arg_len; len -= arg_len;
1008 * Parse a control command
1011 static int sym_user_command(struct Scsi_Host *shost, char *buffer, int length)
1013 struct sym_hcb *np = sym_get_hcb(shost);
1016 struct sym_usrcmd cmd, *uc = &cmd;
1020 memset(uc, 0, sizeof(*uc));
1022 if (len > 0 && ptr[len-1] == '\n')
1025 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1026 uc->cmd = UC_SETSYNC;
1027 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1028 uc->cmd = UC_SETTAGS;
1029 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1030 uc->cmd = UC_SETVERBOSE;
1031 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1032 uc->cmd = UC_SETWIDE;
1033 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1034 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1035 uc->cmd = UC_SETDEBUG;
1037 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1038 uc->cmd = UC_SETFLAG;
1039 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1040 uc->cmd = UC_RESETDEV;
1041 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1042 uc->cmd = UC_CLEARDEV;
1046 #ifdef DEBUG_PROC_INFO
1047 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1052 ptr += arg_len; len -= arg_len;
1061 SKIP_SPACES(ptr, len);
1062 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1063 ptr += arg_len; len -= arg_len;
1066 GET_INT_ARG(ptr, len, target);
1067 uc->target = (1<<target);
1068 #ifdef DEBUG_PROC_INFO
1069 printk("sym_user_command: target=%ld\n", target);
1080 SKIP_SPACES(ptr, len);
1081 GET_INT_ARG(ptr, len, uc->data);
1082 #ifdef DEBUG_PROC_INFO
1083 printk("sym_user_command: data=%ld\n", uc->data);
1086 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1089 SKIP_SPACES(ptr, len);
1090 if ((arg_len = is_keyword(ptr, len, "alloc")))
1091 uc->data |= DEBUG_ALLOC;
1092 else if ((arg_len = is_keyword(ptr, len, "phase")))
1093 uc->data |= DEBUG_PHASE;
1094 else if ((arg_len = is_keyword(ptr, len, "queue")))
1095 uc->data |= DEBUG_QUEUE;
1096 else if ((arg_len = is_keyword(ptr, len, "result")))
1097 uc->data |= DEBUG_RESULT;
1098 else if ((arg_len = is_keyword(ptr, len, "scatter")))
1099 uc->data |= DEBUG_SCATTER;
1100 else if ((arg_len = is_keyword(ptr, len, "script")))
1101 uc->data |= DEBUG_SCRIPT;
1102 else if ((arg_len = is_keyword(ptr, len, "tiny")))
1103 uc->data |= DEBUG_TINY;
1104 else if ((arg_len = is_keyword(ptr, len, "timing")))
1105 uc->data |= DEBUG_TIMING;
1106 else if ((arg_len = is_keyword(ptr, len, "nego")))
1107 uc->data |= DEBUG_NEGO;
1108 else if ((arg_len = is_keyword(ptr, len, "tags")))
1109 uc->data |= DEBUG_TAGS;
1110 else if ((arg_len = is_keyword(ptr, len, "pointer")))
1111 uc->data |= DEBUG_POINTER;
1114 ptr += arg_len; len -= arg_len;
1116 #ifdef DEBUG_PROC_INFO
1117 printk("sym_user_command: data=%ld\n", uc->data);
1120 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1123 SKIP_SPACES(ptr, len);
1124 if ((arg_len = is_keyword(ptr, len, "no_disc")))
1125 uc->data &= ~SYM_DISC_ENABLED;
1128 ptr += arg_len; len -= arg_len;
1138 unsigned long flags;
1140 spin_lock_irqsave(shost->host_lock, flags);
1141 sym_exec_user_command(np, uc);
1142 spin_unlock_irqrestore(shost->host_lock, flags);
1147 #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1151 * Copy formatted information into the input buffer.
1153 static int sym_show_info(struct seq_file *m, struct Scsi_Host *shost)
1155 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1156 struct sym_data *sym_data = shost_priv(shost);
1157 struct pci_dev *pdev = sym_data->pdev;
1158 struct sym_hcb *np = sym_data->ncb;
1160 seq_printf(m, "Chip " NAME53C "%s, device id 0x%x, "
1161 "revision id 0x%x\n", np->s.chip_name,
1162 pdev->device, pdev->revision);
1163 seq_printf(m, "At PCI address %s, IRQ %u\n",
1164 pci_name(pdev), pdev->irq);
1165 seq_printf(m, "Min. period factor %d, %s SCSI BUS%s\n",
1166 (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1167 np->maxwide ? "Wide" : "Narrow",
1168 np->minsync_dt ? ", DT capable" : "");
1170 seq_printf(m, "Max. started commands %d, "
1171 "max. commands per LUN %d\n",
1172 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1177 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1180 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1183 * Free resources claimed by sym_iomap_device(). Note that
1184 * sym_free_resources() should be used instead of this function after calling
1187 static void sym_iounmap_device(struct sym_device *device)
1189 if (device->s.ioaddr)
1190 pci_iounmap(device->pdev, device->s.ioaddr);
1191 if (device->s.ramaddr)
1192 pci_iounmap(device->pdev, device->s.ramaddr);
1196 * Free controller resources.
1198 static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev,
1202 * Free O/S specific resources.
1205 free_irq(pdev->irq, np->s.host);
1207 pci_iounmap(pdev, np->s.ioaddr);
1209 pci_iounmap(pdev, np->s.ramaddr);
1211 * Free O/S independent resources.
1215 sym_mfree_dma(np, sizeof(*np), "HCB");
1219 * Host attach and initialisations.
1221 * Allocate host data and ncb structure.
1222 * Remap MMIO region.
1223 * Do chip initialization.
1224 * If all is OK, install interrupt handling and
1225 * start the timer daemon.
1227 static struct Scsi_Host *sym_attach(struct scsi_host_template *tpnt, int unit,
1228 struct sym_device *dev)
1230 struct sym_data *sym_data;
1231 struct sym_hcb *np = NULL;
1232 struct Scsi_Host *shost = NULL;
1233 struct pci_dev *pdev = dev->pdev;
1234 unsigned long flags;
1236 int do_free_irq = 0;
1238 printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq %u\n",
1239 unit, dev->chip.name, pdev->revision, pci_name(pdev),
1243 * Get the firmware for this chip.
1245 fw = sym_find_firmware(&dev->chip);
1249 shost = scsi_host_alloc(tpnt, sizeof(*sym_data));
1252 sym_data = shost_priv(shost);
1255 * Allocate immediately the host control block,
1256 * since we are only expecting to succeed. :)
1257 * We keep track in the HCB of all the resources that
1258 * are to be released on error.
1260 np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
1263 np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
1265 sym_data->pdev = pdev;
1268 pci_set_drvdata(pdev, shost);
1271 * Copy some useful infos to the HCB.
1273 np->hcb_ba = vtobus(np);
1274 np->verbose = sym_driver_setup.verbose;
1276 np->features = dev->chip.features;
1277 np->clock_divn = dev->chip.nr_divisor;
1278 np->maxoffs = dev->chip.offset_max;
1279 np->maxburst = dev->chip.burst_max;
1280 np->myaddr = dev->host_id;
1281 np->mmio_ba = (u32)dev->mmio_base;
1282 np->ram_ba = (u32)dev->ram_base;
1283 np->s.ioaddr = dev->s.ioaddr;
1284 np->s.ramaddr = dev->s.ramaddr;
1289 strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1290 sprintf(np->s.inst_name, "sym%d", np->s.unit);
1292 if ((SYM_CONF_DMA_ADDRESSING_MODE > 0) && (np->features & FE_DAC) &&
1293 !dma_set_mask(&pdev->dev, DMA_DAC_MASK)) {
1295 } else if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
1296 printf_warning("%s: No suitable DMA available\n", sym_name(np));
1300 if (sym_hcb_attach(shost, fw, dev->nvram))
1304 * Install the interrupt handler.
1305 * If we synchonize the C code with SCRIPTS on interrupt,
1306 * we do not want to share the INTR line at all.
1308 if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX,
1310 printf_err("%s: request irq %u failure\n",
1311 sym_name(np), pdev->irq);
1317 * After SCSI devices have been opened, we cannot
1318 * reset the bus safely, so we do it here.
1320 spin_lock_irqsave(shost->host_lock, flags);
1321 if (sym_reset_scsi_bus(np, 0))
1325 * Start the SCRIPTS.
1327 sym_start_up(shost, 1);
1330 * Start the timer daemon
1332 timer_setup(&np->s.timer, sym53c8xx_timer, 0);
1337 * Fill Linux host instance structure
1338 * and return success.
1340 shost->max_channel = 0;
1341 shost->this_id = np->myaddr;
1342 shost->max_id = np->maxwide ? 16 : 8;
1343 shost->max_lun = SYM_CONF_MAX_LUN;
1344 shost->unique_id = pci_resource_start(pdev, 0);
1345 shost->cmd_per_lun = SYM_CONF_MAX_TAG;
1346 shost->can_queue = (SYM_CONF_MAX_START-2);
1347 shost->sg_tablesize = SYM_CONF_MAX_SG;
1348 shost->max_cmd_len = 16;
1349 BUG_ON(sym2_transport_template == NULL);
1350 shost->transportt = sym2_transport_template;
1352 /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
1353 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
1354 shost->dma_boundary = 0xFFFFFF;
1356 spin_unlock_irqrestore(shost->host_lock, flags);
1361 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1362 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1363 spin_unlock_irqrestore(shost->host_lock, flags);
1365 printf_info("sym%d: giving up ...\n", unit);
1367 sym_free_resources(np, pdev, do_free_irq);
1369 sym_iounmap_device(dev);
1371 scsi_host_put(shost);
1378 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1380 #if SYM_CONF_NVRAM_SUPPORT
1381 static void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1386 sym_read_nvram(devp, nvp);
1389 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1392 #endif /* SYM_CONF_NVRAM_SUPPORT */
1394 static int sym_check_supported(struct sym_device *device)
1396 struct sym_chip *chip;
1397 struct pci_dev *pdev = device->pdev;
1398 unsigned long io_port = pci_resource_start(pdev, 0);
1402 * If user excluded this chip, do not initialize it.
1403 * I hate this code so much. Must kill it.
1406 for (i = 0 ; i < 8 ; i++) {
1407 if (sym_driver_setup.excludes[i] == io_port)
1413 * Check if the chip is supported. Then copy the chip description
1414 * to our device structure so we can make it match the actual device
1417 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
1419 dev_info(&pdev->dev, "device not supported\n");
1422 memcpy(&device->chip, chip, sizeof(device->chip));
1428 * Ignore Symbios chips controlled by various RAID controllers.
1429 * These controllers set value 0x52414944 at RAM end - 16.
1431 static int sym_check_raid(struct sym_device *device)
1433 unsigned int ram_size, ram_val;
1435 if (!device->s.ramaddr)
1438 if (device->chip.features & FE_RAM8K)
1443 ram_val = readl(device->s.ramaddr + ram_size - 16);
1444 if (ram_val != 0x52414944)
1447 dev_info(&device->pdev->dev,
1448 "not initializing, driven by RAID controller.\n");
1452 static int sym_set_workarounds(struct sym_device *device)
1454 struct sym_chip *chip = &device->chip;
1455 struct pci_dev *pdev = device->pdev;
1459 * (ITEM 12 of a DEL about the 896 I haven't yet).
1460 * We must ensure the chip will use WRITE AND INVALIDATE.
1461 * The revision number limit is for now arbitrary.
1463 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 0x4) {
1464 chip->features |= (FE_WRIE | FE_CLSE);
1467 /* If the chip can do Memory Write Invalidate, enable it */
1468 if (chip->features & FE_WRIE) {
1469 if (pci_set_mwi(pdev))
1474 * Work around for errant bit in 895A. The 66Mhz
1475 * capable bit is set erroneously. Clear this bit.
1478 * Make sure Config space and Features agree.
1480 * Recall: writes are not normal to status register -
1481 * write a 1 to clear and a 0 to leave unchanged.
1482 * Can only reset bits.
1484 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1485 if (chip->features & FE_66MHZ) {
1486 if (!(status_reg & PCI_STATUS_66MHZ))
1487 chip->features &= ~FE_66MHZ;
1489 if (status_reg & PCI_STATUS_66MHZ) {
1490 status_reg = PCI_STATUS_66MHZ;
1491 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1492 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1500 * Map HBA registers and on-chip SRAM (if present).
1502 static int sym_iomap_device(struct sym_device *device)
1504 struct pci_dev *pdev = device->pdev;
1505 struct pci_bus_region bus_addr;
1508 pcibios_resource_to_bus(pdev->bus, &bus_addr, &pdev->resource[1]);
1509 device->mmio_base = bus_addr.start;
1511 if (device->chip.features & FE_RAM) {
1513 * If the BAR is 64-bit, resource 2 will be occupied by the
1516 if (!pdev->resource[i].flags)
1518 pcibios_resource_to_bus(pdev->bus, &bus_addr,
1519 &pdev->resource[i]);
1520 device->ram_base = bus_addr.start;
1523 #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
1524 if (device->mmio_base)
1525 device->s.ioaddr = pci_iomap(pdev, 1,
1526 pci_resource_len(pdev, 1));
1528 if (!device->s.ioaddr)
1529 device->s.ioaddr = pci_iomap(pdev, 0,
1530 pci_resource_len(pdev, 0));
1531 if (!device->s.ioaddr) {
1532 dev_err(&pdev->dev, "could not map registers; giving up.\n");
1535 if (device->ram_base) {
1536 device->s.ramaddr = pci_iomap(pdev, i,
1537 pci_resource_len(pdev, i));
1538 if (!device->s.ramaddr) {
1539 dev_warn(&pdev->dev,
1540 "could not map SRAM; continuing anyway.\n");
1541 device->ram_base = 0;
1549 * The NCR PQS and PDS cards are constructed as a DEC bridge
1550 * behind which sits a proprietary NCR memory controller and
1551 * either four or two 53c875s as separate devices. We can tell
1552 * if an 875 is part of a PQS/PDS or not since if it is, it will
1553 * be on the same bus as the memory controller. In its usual
1554 * mode of operation, the 875s are slaved to the memory
1555 * controller for all transfers. To operate with the Linux
1556 * driver, the memory controller is disabled and the 875s
1557 * freed to function independently. The only wrinkle is that
1558 * the preset SCSI ID (which may be zero) must be read in from
1559 * a special configuration space register of the 875.
1561 static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
1566 for (slot = 0; slot < 256; slot++) {
1567 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
1569 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
1574 /* bit 1: allow individual 875 configuration */
1575 pci_read_config_byte(memc, 0x44, &tmp);
1576 if ((tmp & 0x2) == 0) {
1578 pci_write_config_byte(memc, 0x44, tmp);
1581 /* bit 2: drive individual 875 interrupts to the bus */
1582 pci_read_config_byte(memc, 0x45, &tmp);
1583 if ((tmp & 0x4) == 0) {
1585 pci_write_config_byte(memc, 0x45, tmp);
1592 pci_read_config_byte(pdev, 0x84, &tmp);
1593 sym_dev->host_id = tmp;
1597 * Called before unloading the module.
1599 * We have to free resources and halt the NCR chip.
1601 static int sym_detach(struct Scsi_Host *shost, struct pci_dev *pdev)
1603 struct sym_hcb *np = sym_get_hcb(shost);
1604 printk("%s: detaching ...\n", sym_name(np));
1606 del_timer_sync(&np->s.timer);
1610 * We should use sym_soft_reset(), but we don't want to do
1611 * so, since we may not be safe if interrupts occur.
1613 printk("%s: resetting chip\n", sym_name(np));
1614 OUTB(np, nc_istat, SRST);
1617 OUTB(np, nc_istat, 0);
1619 sym_free_resources(np, pdev, 1);
1620 scsi_host_put(shost);
1626 * Driver host template.
1628 static struct scsi_host_template sym2_template = {
1629 .module = THIS_MODULE,
1630 .name = "sym53c8xx",
1631 .info = sym53c8xx_info,
1632 .cmd_size = sizeof(struct sym_ucmd),
1633 .queuecommand = sym53c8xx_queue_command,
1634 .slave_alloc = sym53c8xx_slave_alloc,
1635 .slave_configure = sym53c8xx_slave_configure,
1636 .slave_destroy = sym53c8xx_slave_destroy,
1637 .eh_abort_handler = sym53c8xx_eh_abort_handler,
1638 .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
1639 .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
1640 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
1642 .max_sectors = 0xFFFF,
1643 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1644 .show_info = sym_show_info,
1645 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1646 .write_info = sym_user_command,
1648 .proc_name = NAME53C8XX,
1652 static int attach_count;
1654 static int sym2_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1656 struct sym_device sym_dev;
1657 struct sym_nvram nvram;
1658 struct Scsi_Host *shost;
1660 int do_disable_device = 1;
1662 memset(&sym_dev, 0, sizeof(sym_dev));
1663 memset(&nvram, 0, sizeof(nvram));
1664 sym_dev.pdev = pdev;
1665 sym_dev.host_id = SYM_SETUP_HOST_ID;
1667 if (pci_enable_device(pdev))
1670 pci_set_master(pdev);
1672 if (pci_request_regions(pdev, NAME53C8XX))
1675 if (sym_check_supported(&sym_dev))
1678 if (sym_iomap_device(&sym_dev))
1682 if (sym_check_raid(&sym_dev)) {
1683 do_disable_device = 0; /* Don't disable the device */
1687 if (sym_set_workarounds(&sym_dev))
1690 sym_config_pqs(pdev, &sym_dev);
1692 sym_get_nvram(&sym_dev, &nvram);
1694 do_iounmap = 0; /* Don't sym_iounmap_device() after sym_attach(). */
1695 shost = sym_attach(&sym2_template, attach_count, &sym_dev);
1699 if (scsi_add_host(shost, &pdev->dev))
1701 scsi_scan_host(shost);
1708 sym_detach(pci_get_drvdata(pdev), pdev);
1711 sym_iounmap_device(&sym_dev);
1712 pci_release_regions(pdev);
1714 if (do_disable_device)
1715 pci_disable_device(pdev);
1720 static void sym2_remove(struct pci_dev *pdev)
1722 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1724 scsi_remove_host(shost);
1725 sym_detach(shost, pdev);
1726 pci_release_regions(pdev);
1727 pci_disable_device(pdev);
1733 * sym2_io_error_detected() - called when PCI error is detected
1734 * @pdev: pointer to PCI device
1735 * @state: current state of the PCI slot
1737 static pci_ers_result_t sym2_io_error_detected(struct pci_dev *pdev,
1738 pci_channel_state_t state)
1740 /* If slot is permanently frozen, turn everything off */
1741 if (state == pci_channel_io_perm_failure) {
1743 return PCI_ERS_RESULT_DISCONNECT;
1746 disable_irq(pdev->irq);
1747 pci_disable_device(pdev);
1749 /* Request that MMIO be enabled, so register dump can be taken. */
1750 return PCI_ERS_RESULT_CAN_RECOVER;
1754 * sym2_io_slot_dump - Enable MMIO and dump debug registers
1755 * @pdev: pointer to PCI device
1757 static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
1759 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1761 sym_dump_registers(shost);
1763 /* Request a slot reset. */
1764 return PCI_ERS_RESULT_NEED_RESET;
1768 * sym2_reset_workarounds - hardware-specific work-arounds
1769 * @pdev: pointer to PCI device
1771 * This routine is similar to sym_set_workarounds(), except
1772 * that, at this point, we already know that the device was
1773 * successfully initialized at least once before, and so most
1774 * of the steps taken there are un-needed here.
1776 static void sym2_reset_workarounds(struct pci_dev *pdev)
1779 struct sym_chip *chip;
1781 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
1783 /* Work around for errant bit in 895A, in a fashion
1784 * similar to what is done in sym_set_workarounds().
1786 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1787 if (!(chip->features & FE_66MHZ) && (status_reg & PCI_STATUS_66MHZ)) {
1788 status_reg = PCI_STATUS_66MHZ;
1789 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1790 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1795 * sym2_io_slot_reset() - called when the pci bus has been reset.
1796 * @pdev: pointer to PCI device
1798 * Restart the card from scratch.
1800 static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
1802 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1803 struct sym_hcb *np = sym_get_hcb(shost);
1805 printk(KERN_INFO "%s: recovering from a PCI slot reset\n",
1808 if (pci_enable_device(pdev)) {
1809 printk(KERN_ERR "%s: Unable to enable after PCI reset\n",
1811 return PCI_ERS_RESULT_DISCONNECT;
1814 pci_set_master(pdev);
1815 enable_irq(pdev->irq);
1817 /* If the chip can do Memory Write Invalidate, enable it */
1818 if (np->features & FE_WRIE) {
1819 if (pci_set_mwi(pdev))
1820 return PCI_ERS_RESULT_DISCONNECT;
1823 /* Perform work-arounds, analogous to sym_set_workarounds() */
1824 sym2_reset_workarounds(pdev);
1826 /* Perform host reset only on one instance of the card */
1827 if (PCI_FUNC(pdev->devfn) == 0) {
1828 if (sym_reset_scsi_bus(np, 0)) {
1829 printk(KERN_ERR "%s: Unable to reset scsi host\n",
1831 return PCI_ERS_RESULT_DISCONNECT;
1833 sym_start_up(shost, 1);
1836 return PCI_ERS_RESULT_RECOVERED;
1840 * sym2_io_resume() - resume normal ops after PCI reset
1841 * @pdev: pointer to PCI device
1843 * Called when the error recovery driver tells us that its
1844 * OK to resume normal operation. Use completion to allow
1845 * halted scsi ops to resume.
1847 static void sym2_io_resume(struct pci_dev *pdev)
1849 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1850 struct sym_data *sym_data = shost_priv(shost);
1852 spin_lock_irq(shost->host_lock);
1853 if (sym_data->io_reset)
1854 complete(sym_data->io_reset);
1855 spin_unlock_irq(shost->host_lock);
1858 static void sym2_get_signalling(struct Scsi_Host *shost)
1860 struct sym_hcb *np = sym_get_hcb(shost);
1861 enum spi_signal_type type;
1863 switch (np->scsi_mode) {
1865 type = SPI_SIGNAL_SE;
1868 type = SPI_SIGNAL_LVD;
1871 type = SPI_SIGNAL_HVD;
1874 type = SPI_SIGNAL_UNKNOWN;
1877 spi_signalling(shost) = type;
1880 static void sym2_set_offset(struct scsi_target *starget, int offset)
1882 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1883 struct sym_hcb *np = sym_get_hcb(shost);
1884 struct sym_tcb *tp = &np->target[starget->id];
1886 tp->tgoal.offset = offset;
1887 tp->tgoal.check_nego = 1;
1890 static void sym2_set_period(struct scsi_target *starget, int period)
1892 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1893 struct sym_hcb *np = sym_get_hcb(shost);
1894 struct sym_tcb *tp = &np->target[starget->id];
1896 /* have to have DT for these transfers, but DT will also
1897 * set width, so check that this is allowed */
1898 if (period <= np->minsync && spi_width(starget))
1901 tp->tgoal.period = period;
1902 tp->tgoal.check_nego = 1;
1905 static void sym2_set_width(struct scsi_target *starget, int width)
1907 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1908 struct sym_hcb *np = sym_get_hcb(shost);
1909 struct sym_tcb *tp = &np->target[starget->id];
1911 /* It is illegal to have DT set on narrow transfers. If DT is
1912 * clear, we must also clear IU and QAS. */
1914 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1916 tp->tgoal.width = width;
1917 tp->tgoal.check_nego = 1;
1920 static void sym2_set_dt(struct scsi_target *starget, int dt)
1922 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1923 struct sym_hcb *np = sym_get_hcb(shost);
1924 struct sym_tcb *tp = &np->target[starget->id];
1926 /* We must clear QAS and IU if DT is clear */
1930 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1931 tp->tgoal.check_nego = 1;
1935 static void sym2_set_iu(struct scsi_target *starget, int iu)
1937 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1938 struct sym_hcb *np = sym_get_hcb(shost);
1939 struct sym_tcb *tp = &np->target[starget->id];
1942 tp->tgoal.iu = tp->tgoal.dt = 1;
1945 tp->tgoal.check_nego = 1;
1948 static void sym2_set_qas(struct scsi_target *starget, int qas)
1950 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1951 struct sym_hcb *np = sym_get_hcb(shost);
1952 struct sym_tcb *tp = &np->target[starget->id];
1955 tp->tgoal.dt = tp->tgoal.qas = 1;
1958 tp->tgoal.check_nego = 1;
1962 static struct spi_function_template sym2_transport_functions = {
1963 .set_offset = sym2_set_offset,
1965 .set_period = sym2_set_period,
1967 .set_width = sym2_set_width,
1969 .set_dt = sym2_set_dt,
1972 .set_iu = sym2_set_iu,
1974 .set_qas = sym2_set_qas,
1977 .get_signalling = sym2_get_signalling,
1980 static struct pci_device_id sym2_id_table[] = {
1981 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
1982 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1983 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
1984 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
1985 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
1986 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1987 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
1988 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1989 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
1990 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
1991 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
1992 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1993 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
1994 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
1995 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
1996 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1997 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
1998 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1999 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2000 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2001 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2002 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2003 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
2004 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
2005 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2006 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2007 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2008 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2009 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2010 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2011 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2012 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2013 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2014 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2018 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2020 static const struct pci_error_handlers sym2_err_handler = {
2021 .error_detected = sym2_io_error_detected,
2022 .mmio_enabled = sym2_io_slot_dump,
2023 .slot_reset = sym2_io_slot_reset,
2024 .resume = sym2_io_resume,
2027 static struct pci_driver sym2_driver = {
2029 .id_table = sym2_id_table,
2030 .probe = sym2_probe,
2031 .remove = sym2_remove,
2032 .err_handler = &sym2_err_handler,
2035 static int __init sym2_init(void)
2039 sym2_setup_params();
2040 sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2041 if (!sym2_transport_template)
2044 error = pci_register_driver(&sym2_driver);
2046 spi_release_transport(sym2_transport_template);
2050 static void __exit sym2_exit(void)
2052 pci_unregister_driver(&sym2_driver);
2053 spi_release_transport(sym2_transport_template);
2056 module_init(sym2_init);
2057 module_exit(sym2_exit);