2 * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
3 * of PCI-SCSI IO processors.
5 * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
6 * Copyright (c) 2003-2005 Matthew Wilcox <matthew@wil.cx>
8 * This driver is derived from the Linux sym53c8xx driver.
9 * Copyright (C) 1998-2000 Gerard Roudier
11 * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
12 * a port of the FreeBSD ncr driver to Linux-1.2.13.
14 * The original ncr driver has been written for 386bsd and FreeBSD by
15 * Wolfgang Stanglmeier <wolf@cologne.de>
16 * Stefan Esser <se@mi.Uni-Koeln.de>
17 * Copyright (C) 1994 Wolfgang Stanglmeier
19 * Other major contributions:
21 * NVRAM detection and reading.
22 * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
24 *-----------------------------------------------------------------------------
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #include <linux/ctype.h>
41 #include <linux/init.h>
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44 #include <linux/spinlock.h>
45 #include <scsi/scsi.h>
46 #include <scsi/scsi_tcq.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_transport.h>
51 #include "sym_nvram.h"
53 #define NAME53C "sym53c"
54 #define NAME53C8XX "sym53c8xx"
56 struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
57 unsigned int sym_debug_flags = 0;
59 static char *excl_string;
60 static char *safe_string;
61 module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
62 module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
63 module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
64 module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
65 module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
66 module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
67 module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
68 module_param_named(verb, sym_driver_setup.verbose, byte, 0);
69 module_param_named(debug, sym_debug_flags, uint, 0);
70 module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
71 module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
72 module_param_named(excl, excl_string, charp, 0);
73 module_param_named(safe, safe_string, charp, 0);
75 MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
76 MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
77 MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
78 MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
79 MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
80 MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
81 MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
82 MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
83 MODULE_PARM_DESC(debug, "Set bits to enable debugging");
84 MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
85 MODULE_PARM_DESC(nvram, "Option currently not used");
86 MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
87 MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
89 MODULE_LICENSE("GPL");
90 MODULE_VERSION(SYM_VERSION);
91 MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
92 MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
94 static void sym2_setup_params(void)
96 char *p = excl_string;
99 while (p && (xi < 8)) {
101 int val = (int) simple_strtoul(p, &next_p, 0);
102 sym_driver_setup.excludes[xi++] = val;
107 if (*safe_string == 'y') {
108 sym_driver_setup.max_tag = 0;
109 sym_driver_setup.burst_order = 0;
110 sym_driver_setup.scsi_led = 0;
111 sym_driver_setup.scsi_diff = 1;
112 sym_driver_setup.irq_mode = 0;
113 sym_driver_setup.scsi_bus_check = 2;
114 sym_driver_setup.host_id = 7;
115 sym_driver_setup.verbose = 2;
116 sym_driver_setup.settle_delay = 10;
117 sym_driver_setup.use_nvram = 1;
118 } else if (*safe_string != 'n') {
119 printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
120 " passed to safe option", safe_string);
125 static struct scsi_transport_template *sym2_transport_template = NULL;
128 * Driver private area in the SCSI command structure.
130 struct sym_ucmd { /* Override the SCSI pointer structure */
131 struct completion *eh_done; /* SCSI error handling */
134 #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
135 #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
138 * Complete a pending CAM CCB.
140 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
142 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
143 BUILD_BUG_ON(sizeof(struct scsi_pointer) < sizeof(struct sym_ucmd));
146 complete(ucmd->eh_done);
153 * Tell the SCSI layer about a BUS RESET.
155 void sym_xpt_async_bus_reset(struct sym_hcb *np)
157 printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
158 np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
159 np->s.settle_time_valid = 1;
160 if (sym_verbose >= 2)
161 printf_info("%s: command processing suspended for %d seconds\n",
162 sym_name(np), sym_driver_setup.settle_delay);
166 * Choose the more appropriate CAM status if
167 * the IO encountered an extended error.
169 static int sym_xerr_cam_status(int cam_status, int x_status)
172 if (x_status & XE_PARITY_ERR)
173 cam_status = DID_PARITY;
174 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
175 cam_status = DID_ERROR;
176 else if (x_status & XE_BAD_PHASE)
177 cam_status = DID_ERROR;
179 cam_status = DID_ERROR;
185 * Build CAM result for a failed or auto-sensed IO.
187 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
189 struct scsi_cmnd *cmd = cp->cmd;
190 u_int cam_status, scsi_status, drv_status;
194 scsi_status = cp->ssss_status;
196 if (cp->host_flags & HF_SENSE) {
197 scsi_status = cp->sv_scsi_status;
198 resid = cp->sv_resid;
199 if (sym_verbose && cp->sv_xerr_status)
200 sym_print_xerr(cmd, cp->sv_xerr_status);
201 if (cp->host_status == HS_COMPLETE &&
202 cp->ssss_status == S_GOOD &&
203 cp->xerr_status == 0) {
204 cam_status = sym_xerr_cam_status(DID_OK,
206 drv_status = DRIVER_SENSE;
208 * Bounce back the sense data to user.
210 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
211 memcpy(cmd->sense_buffer, cp->sns_bbuf,
212 min(SCSI_SENSE_BUFFERSIZE, SYM_SNS_BBUF_LEN));
215 * If the device reports a UNIT ATTENTION condition
216 * due to a RESET condition, we should consider all
217 * disconnect CCBs for this unit as aborted.
221 p = (u_char *) cmd->sense_data;
222 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
223 sym_clear_tasks(np, DID_ABORT,
224 cp->target,cp->lun, -1);
229 * Error return from our internal request sense. This
230 * is bad: we must clear the contingent allegiance
231 * condition otherwise the device will always return
232 * BUSY. Use a big stick.
234 sym_reset_scsi_target(np, cmd->device->id);
235 cam_status = DID_ERROR;
237 } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
239 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
240 cam_status = DID_NO_CONNECT;
241 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
242 cam_status = DID_ERROR;
243 else { /* Extended error */
245 sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
246 cp->host_status, cp->ssss_status,
250 * Set the most appropriate value for CAM status.
252 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
254 scsi_set_resid(cmd, resid);
255 cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
258 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
265 use_sg = scsi_dma_map(cmd);
267 struct scatterlist *sg;
268 struct sym_tcb *tp = &np->target[cp->target];
269 struct sym_tblmove *data;
271 if (use_sg > SYM_CONF_MAX_SG) {
276 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
278 scsi_for_each_sg(cmd, sg, use_sg, segment) {
279 dma_addr_t baddr = sg_dma_address(sg);
280 unsigned int len = sg_dma_len(sg);
282 if ((len & 1) && (tp->head.wval & EWS)) {
284 cp->odd_byte_adjustment++;
287 sym_build_sge(np, &data[segment], baddr, len);
298 * Queue a SCSI command.
300 static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
302 struct scsi_device *sdev = cmd->device;
309 * Retrieve the target descriptor.
311 tp = &np->target[sdev->id];
314 * Select tagged/untagged.
316 lp = sym_lp(tp, sdev->lun);
317 order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
322 cp = sym_get_ccb(np, cmd, order);
324 return 1; /* Means resource shortage */
325 sym_queue_scsiio(np, cmd, cp);
330 * Setup buffers and pointers that address the CDB.
332 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
334 memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
336 cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
337 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
343 * Setup pointers that address the data and start the I/O.
345 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
353 if (sym_setup_cdb(np, cmd, cp))
357 * No direction means no data.
359 dir = cmd->sc_data_direction;
360 if (dir != DMA_NONE) {
361 cp->segments = sym_scatter(np, cp, cmd);
362 if (cp->segments < 0) {
363 sym_set_cam_status(cmd, DID_ERROR);
368 * No segments means no data.
378 * Set the data pointer.
381 case DMA_BIDIRECTIONAL:
382 scmd_printk(KERN_INFO, cmd, "got DMA_BIDIRECTIONAL command");
383 sym_set_cam_status(cmd, DID_ERROR);
386 goalp = SCRIPTA_BA(np, data_out2) + 8;
387 lastp = goalp - 8 - (cp->segments * (2*4));
389 case DMA_FROM_DEVICE:
390 cp->host_flags |= HF_DATA_IN;
391 goalp = SCRIPTA_BA(np, data_in2) + 8;
392 lastp = goalp - 8 - (cp->segments * (2*4));
396 lastp = goalp = SCRIPTB_BA(np, no_data);
401 * Set all pointers values needed by SCRIPTS.
403 cp->phys.head.lastp = cpu_to_scr(lastp);
404 cp->phys.head.savep = cpu_to_scr(lastp);
405 cp->startp = cp->phys.head.savep;
406 cp->goalp = cpu_to_scr(goalp);
409 * When `#ifed 1', the code below makes the driver
410 * panic on the first attempt to write to a SCSI device.
411 * It is the first test we want to do after a driver
412 * change that does not seem obviously safe. :)
415 switch (cp->cdb_buf[0]) {
416 case 0x0A: case 0x2A: case 0xAA:
417 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
427 sym_put_start_queue(np, cp);
431 sym_free_ccb(np, cp);
432 sym_xpt_done(np, cmd);
440 * Misused to keep the driver running when
441 * interrupts are not configured correctly.
443 static void sym_timer(struct sym_hcb *np)
445 unsigned long thistime = jiffies;
450 np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
451 add_timer(&np->s.timer);
454 * If we are resetting the ncr, wait for settle_time before
455 * clearing it. Then command processing will be resumed.
457 if (np->s.settle_time_valid) {
458 if (time_before_eq(np->s.settle_time, thistime)) {
459 if (sym_verbose >= 2 )
460 printk("%s: command processing resumed\n",
462 np->s.settle_time_valid = 0;
468 * Nothing to do for now, but that may come.
470 if (np->s.lasttime + 4*HZ < thistime) {
471 np->s.lasttime = thistime;
474 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
476 * Some way-broken PCI bridges may lead to
477 * completions being lost when the clearing
478 * of the INTFLY flag by the CPU occurs
479 * concurrently with the chip raising this flag.
480 * If this ever happen, lost completions will
489 * PCI BUS error handler.
491 void sym_log_bus_error(struct Scsi_Host *shost)
493 struct sym_data *sym_data = shost_priv(shost);
494 struct pci_dev *pdev = sym_data->pdev;
495 unsigned short pci_sts;
496 pci_read_config_word(pdev, PCI_STATUS, &pci_sts);
497 if (pci_sts & 0xf900) {
498 pci_write_config_word(pdev, PCI_STATUS, pci_sts);
499 shost_printk(KERN_WARNING, shost,
500 "PCI bus error: status = 0x%04x\n", pci_sts & 0xf900);
505 * queuecommand method. Entered with the host adapter lock held and
506 * interrupts disabled.
508 static int sym53c8xx_queue_command_lck(struct scsi_cmnd *cmd,
509 void (*done)(struct scsi_cmnd *))
511 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
512 struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
515 cmd->scsi_done = done;
516 memset(ucp, 0, sizeof(*ucp));
519 * Shorten our settle_time if needed for
520 * this command not to time out.
522 if (np->s.settle_time_valid && cmd->request->timeout) {
523 unsigned long tlimit = jiffies + cmd->request->timeout;
524 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
525 if (time_after(np->s.settle_time, tlimit)) {
526 np->s.settle_time = tlimit;
530 if (np->s.settle_time_valid)
531 return SCSI_MLQUEUE_HOST_BUSY;
533 sts = sym_queue_command(np, cmd);
535 return SCSI_MLQUEUE_HOST_BUSY;
539 static DEF_SCSI_QCMD(sym53c8xx_queue_command)
542 * Linux entry point of the interrupt handler.
544 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
546 struct Scsi_Host *shost = dev_id;
547 struct sym_data *sym_data = shost_priv(shost);
550 /* Avoid spinloop trying to handle interrupts on frozen device */
551 if (pci_channel_offline(sym_data->pdev))
554 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
556 spin_lock(shost->host_lock);
557 result = sym_interrupt(shost);
558 spin_unlock(shost->host_lock);
560 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
566 * Linux entry point of the timer handler
568 static void sym53c8xx_timer(unsigned long npref)
570 struct sym_hcb *np = (struct sym_hcb *)npref;
573 spin_lock_irqsave(np->s.host->host_lock, flags);
575 spin_unlock_irqrestore(np->s.host->host_lock, flags);
580 * What the eh thread wants us to perform.
582 #define SYM_EH_ABORT 0
583 #define SYM_EH_DEVICE_RESET 1
584 #define SYM_EH_BUS_RESET 2
585 #define SYM_EH_HOST_RESET 3
588 * Generic method for our eh processing.
589 * The 'op' argument tells what we have to do.
591 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
593 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
594 struct Scsi_Host *shost = cmd->device->host;
595 struct sym_data *sym_data = shost_priv(shost);
596 struct pci_dev *pdev = sym_data->pdev;
597 struct sym_hcb *np = sym_data->ncb;
601 struct completion eh_done;
603 scmd_printk(KERN_WARNING, cmd, "%s operation started\n", opname);
605 /* We may be in an error condition because the PCI bus
606 * went down. In this case, we need to wait until the
607 * PCI bus is reset, the card is reset, and only then
608 * proceed with the scsi error recovery. There's no
609 * point in hurrying; take a leisurely wait.
611 #define WAIT_FOR_PCI_RECOVERY 35
612 if (pci_channel_offline(pdev)) {
613 int finished_reset = 0;
614 init_completion(&eh_done);
615 spin_lock_irq(shost->host_lock);
616 /* Make sure we didn't race */
617 if (pci_channel_offline(pdev)) {
618 BUG_ON(sym_data->io_reset);
619 sym_data->io_reset = &eh_done;
623 spin_unlock_irq(shost->host_lock);
625 finished_reset = wait_for_completion_timeout
627 WAIT_FOR_PCI_RECOVERY*HZ);
628 spin_lock_irq(shost->host_lock);
629 sym_data->io_reset = NULL;
630 spin_unlock_irq(shost->host_lock);
635 spin_lock_irq(shost->host_lock);
636 /* This one is queued in some place -> to wait for completion */
637 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
638 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
639 if (cp->cmd == cmd) {
645 /* Try to proceed the operation we have been asked for */
649 sts = sym_abort_scsiio(np, cmd, 1);
651 case SYM_EH_DEVICE_RESET:
652 sts = sym_reset_scsi_target(np, cmd->device->id);
654 case SYM_EH_BUS_RESET:
655 sym_reset_scsi_bus(np, 1);
658 case SYM_EH_HOST_RESET:
659 sym_reset_scsi_bus(np, 0);
660 sym_start_up(shost, 1);
667 /* On error, restore everything and cross fingers :) */
672 init_completion(&eh_done);
673 ucmd->eh_done = &eh_done;
674 spin_unlock_irq(shost->host_lock);
675 if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
676 ucmd->eh_done = NULL;
680 spin_unlock_irq(shost->host_lock);
683 dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
684 sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
685 return sts ? SCSI_FAILED : SCSI_SUCCESS;
690 * Error handlers called from the eh thread (one thread per HBA).
692 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
694 return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
697 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
699 return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
702 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
704 return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
707 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
709 return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
713 * Tune device queuing depth, according to various limits.
715 static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
717 struct sym_lcb *lp = sym_lp(tp, lun);
723 oldtags = lp->s.reqtags;
725 if (reqtags > lp->s.scdev_depth)
726 reqtags = lp->s.scdev_depth;
728 lp->s.reqtags = reqtags;
730 if (reqtags != oldtags) {
731 dev_info(&tp->starget->dev,
732 "tagged command queuing %s, command queue depth %d.\n",
733 lp->s.reqtags ? "enabled" : "disabled", reqtags);
737 static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
739 struct sym_hcb *np = sym_get_hcb(sdev->host);
740 struct sym_tcb *tp = &np->target[sdev->id];
745 if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
748 spin_lock_irqsave(np->s.host->host_lock, flags);
751 * Fail the device init if the device is flagged NOSCAN at BOOT in
752 * the NVRAM. This may speed up boot and maintain coherency with
753 * BIOS device numbering. Clearing the flag allows the user to
754 * rescan skipped devices later. We also return an error for
755 * devices not flagged for SCAN LUNS in the NVRAM since some single
756 * lun devices behave badly when asked for a non zero LUN.
759 if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
760 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
761 starget_printk(KERN_INFO, sdev->sdev_target,
762 "Scan at boot disabled in NVRAM\n");
767 if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
768 if (sdev->lun != 0) {
772 starget_printk(KERN_INFO, sdev->sdev_target,
773 "Multiple LUNs disabled in NVRAM\n");
776 lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
782 tp->starget = sdev->sdev_target;
784 spi_min_period(tp->starget) = tp->usr_period;
785 spi_max_width(tp->starget) = tp->usr_width;
789 spin_unlock_irqrestore(np->s.host->host_lock, flags);
795 * Linux entry point for device queue sizing.
797 static int sym53c8xx_slave_configure(struct scsi_device *sdev)
799 struct sym_hcb *np = sym_get_hcb(sdev->host);
800 struct sym_tcb *tp = &np->target[sdev->id];
801 struct sym_lcb *lp = sym_lp(tp, sdev->lun);
802 int reqtags, depth_to_use;
807 lp->curr_flags = lp->user_flags;
810 * Select queue depth from driver setup.
811 * Do not use more than configured by user.
813 * Do not use more than our maximum.
815 reqtags = sym_driver_setup.max_tag;
816 if (reqtags > tp->usrtags)
817 reqtags = tp->usrtags;
818 if (!sdev->tagged_supported)
820 if (reqtags > SYM_CONF_MAX_TAG)
821 reqtags = SYM_CONF_MAX_TAG;
822 depth_to_use = reqtags ? reqtags : 1;
823 scsi_adjust_queue_depth(sdev,
824 sdev->tagged_supported ? MSG_SIMPLE_TAG : 0,
826 lp->s.scdev_depth = depth_to_use;
827 sym_tune_dev_queuing(tp, sdev->lun, reqtags);
829 if (!spi_initial_dv(sdev->sdev_target))
835 static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
837 struct sym_hcb *np = sym_get_hcb(sdev->host);
838 struct sym_tcb *tp = &np->target[sdev->id];
839 struct sym_lcb *lp = sym_lp(tp, sdev->lun);
842 /* if slave_alloc returned before allocating a sym_lcb, return */
846 spin_lock_irqsave(np->s.host->host_lock, flags);
848 if (lp->busy_itlq || lp->busy_itl) {
850 * This really shouldn't happen, but we can't return an error
851 * so let's try to stop all on-going I/O.
853 starget_printk(KERN_WARNING, tp->starget,
854 "Removing busy LCB (%d)\n", sdev->lun);
855 sym_reset_scsi_bus(np, 1);
858 if (sym_free_lcb(np, sdev->id, sdev->lun) == 0) {
860 * It was the last unit for this target.
863 tp->head.wval = np->rv_scntl3;
865 tp->tgoal.check_nego = 1;
869 spin_unlock_irqrestore(np->s.host->host_lock, flags);
873 * Linux entry point for info() function
875 static const char *sym53c8xx_info (struct Scsi_Host *host)
877 return SYM_DRIVER_NAME;
881 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
883 * Proc file system stuff
885 * A read operation returns adapter information.
886 * A write operation is a control command.
887 * The string is parsed in the driver code and the command is passed
888 * to the sym_usercmd() function.
891 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
900 #define UC_SETSYNC 10
901 #define UC_SETTAGS 11
902 #define UC_SETDEBUG 12
903 #define UC_SETWIDE 14
904 #define UC_SETFLAG 15
905 #define UC_SETVERBOSE 17
906 #define UC_RESETDEV 18
907 #define UC_CLEARDEV 19
909 static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
917 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
919 sym_debug_flags = uc->data;
923 np->verbose = uc->data;
927 * We assume that other commands apply to targets.
928 * This should always be the case and avoid the below
929 * 4 lines to be repeated 6 times.
931 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
932 if (!((uc->target >> t) & 1))
941 if (!uc->data || uc->data >= 255) {
942 tp->tgoal.iu = tp->tgoal.dt =
944 tp->tgoal.offset = 0;
945 } else if (uc->data <= 9 && np->minsync_dt) {
946 if (uc->data < np->minsync_dt)
947 uc->data = np->minsync_dt;
948 tp->tgoal.iu = tp->tgoal.dt =
951 tp->tgoal.period = uc->data;
952 tp->tgoal.offset = np->maxoffs_dt;
954 if (uc->data < np->minsync)
955 uc->data = np->minsync;
956 tp->tgoal.iu = tp->tgoal.dt =
958 tp->tgoal.period = uc->data;
959 tp->tgoal.offset = np->maxoffs;
961 tp->tgoal.check_nego = 1;
964 tp->tgoal.width = uc->data ? 1 : 0;
965 tp->tgoal.check_nego = 1;
968 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
969 sym_tune_dev_queuing(tp, l, uc->data);
974 OUTB(np, nc_istat, SIGP|SEM);
977 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
978 struct sym_lcb *lp = sym_lp(tp, l);
979 if (lp) lp->to_clear = 1;
982 OUTB(np, nc_istat, SIGP|SEM);
985 tp->usrflags = uc->data;
993 static int sym_skip_spaces(char *ptr, int len)
997 for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
1002 static int get_int_arg(char *ptr, int len, u_long *pv)
1006 *pv = simple_strtoul(ptr, &end, 10);
1010 static int is_keyword(char *ptr, int len, char *verb)
1012 int verb_len = strlen(verb);
1014 if (len >= verb_len && !memcmp(verb, ptr, verb_len))
1020 #define SKIP_SPACES(ptr, len) \
1021 if ((arg_len = sym_skip_spaces(ptr, len)) < 1) \
1023 ptr += arg_len; len -= arg_len;
1025 #define GET_INT_ARG(ptr, len, v) \
1026 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
1028 ptr += arg_len; len -= arg_len;
1032 * Parse a control command
1035 static int sym_user_command(struct Scsi_Host *shost, char *buffer, int length)
1037 struct sym_hcb *np = sym_get_hcb(shost);
1040 struct sym_usrcmd cmd, *uc = &cmd;
1044 memset(uc, 0, sizeof(*uc));
1046 if (len > 0 && ptr[len-1] == '\n')
1049 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1050 uc->cmd = UC_SETSYNC;
1051 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1052 uc->cmd = UC_SETTAGS;
1053 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1054 uc->cmd = UC_SETVERBOSE;
1055 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1056 uc->cmd = UC_SETWIDE;
1057 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1058 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1059 uc->cmd = UC_SETDEBUG;
1061 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1062 uc->cmd = UC_SETFLAG;
1063 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1064 uc->cmd = UC_RESETDEV;
1065 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1066 uc->cmd = UC_CLEARDEV;
1070 #ifdef DEBUG_PROC_INFO
1071 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1076 ptr += arg_len; len -= arg_len;
1085 SKIP_SPACES(ptr, len);
1086 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1087 ptr += arg_len; len -= arg_len;
1090 GET_INT_ARG(ptr, len, target);
1091 uc->target = (1<<target);
1092 #ifdef DEBUG_PROC_INFO
1093 printk("sym_user_command: target=%ld\n", target);
1104 SKIP_SPACES(ptr, len);
1105 GET_INT_ARG(ptr, len, uc->data);
1106 #ifdef DEBUG_PROC_INFO
1107 printk("sym_user_command: data=%ld\n", uc->data);
1110 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1113 SKIP_SPACES(ptr, len);
1114 if ((arg_len = is_keyword(ptr, len, "alloc")))
1115 uc->data |= DEBUG_ALLOC;
1116 else if ((arg_len = is_keyword(ptr, len, "phase")))
1117 uc->data |= DEBUG_PHASE;
1118 else if ((arg_len = is_keyword(ptr, len, "queue")))
1119 uc->data |= DEBUG_QUEUE;
1120 else if ((arg_len = is_keyword(ptr, len, "result")))
1121 uc->data |= DEBUG_RESULT;
1122 else if ((arg_len = is_keyword(ptr, len, "scatter")))
1123 uc->data |= DEBUG_SCATTER;
1124 else if ((arg_len = is_keyword(ptr, len, "script")))
1125 uc->data |= DEBUG_SCRIPT;
1126 else if ((arg_len = is_keyword(ptr, len, "tiny")))
1127 uc->data |= DEBUG_TINY;
1128 else if ((arg_len = is_keyword(ptr, len, "timing")))
1129 uc->data |= DEBUG_TIMING;
1130 else if ((arg_len = is_keyword(ptr, len, "nego")))
1131 uc->data |= DEBUG_NEGO;
1132 else if ((arg_len = is_keyword(ptr, len, "tags")))
1133 uc->data |= DEBUG_TAGS;
1134 else if ((arg_len = is_keyword(ptr, len, "pointer")))
1135 uc->data |= DEBUG_POINTER;
1138 ptr += arg_len; len -= arg_len;
1140 #ifdef DEBUG_PROC_INFO
1141 printk("sym_user_command: data=%ld\n", uc->data);
1144 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1147 SKIP_SPACES(ptr, len);
1148 if ((arg_len = is_keyword(ptr, len, "no_disc")))
1149 uc->data &= ~SYM_DISC_ENABLED;
1152 ptr += arg_len; len -= arg_len;
1162 unsigned long flags;
1164 spin_lock_irqsave(shost->host_lock, flags);
1165 sym_exec_user_command(np, uc);
1166 spin_unlock_irqrestore(shost->host_lock, flags);
1171 #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1174 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1176 * Informations through the proc file system.
1185 static void copy_mem_info(struct info_str *info, char *data, int len)
1187 if (info->pos + len > info->length)
1188 len = info->length - info->pos;
1190 if (info->pos + len < info->offset) {
1194 if (info->pos < info->offset) {
1195 data += (info->offset - info->pos);
1196 len -= (info->offset - info->pos);
1200 memcpy(info->buffer + info->pos, data, len);
1205 static int copy_info(struct info_str *info, char *fmt, ...)
1211 va_start(args, fmt);
1212 len = vsprintf(buf, fmt, args);
1215 copy_mem_info(info, buf, len);
1220 * Copy formatted information into the input buffer.
1222 static int sym_host_info(struct Scsi_Host *shost, char *ptr, off_t offset, int len)
1224 struct sym_data *sym_data = shost_priv(shost);
1225 struct pci_dev *pdev = sym_data->pdev;
1226 struct sym_hcb *np = sym_data->ncb;
1227 struct info_str info;
1231 info.offset = offset;
1234 copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1235 "revision id 0x%x\n", np->s.chip_name,
1236 pdev->device, pdev->revision);
1237 copy_info(&info, "At PCI address %s, IRQ %u\n",
1238 pci_name(pdev), pdev->irq);
1239 copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1240 (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1241 np->maxwide ? "Wide" : "Narrow",
1242 np->minsync_dt ? ", DT capable" : "");
1244 copy_info(&info, "Max. started commands %d, "
1245 "max. commands per LUN %d\n",
1246 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1248 return info.pos > info.offset? info.pos - info.offset : 0;
1250 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1253 * Entry point of the scsi proc fs of the driver.
1254 * - func = 0 means read (returns adapter infos)
1255 * - func = 1 means write (not yet merget from sym53c8xx)
1257 static int sym53c8xx_proc_info(struct Scsi_Host *shost, char *buffer,
1258 char **start, off_t offset, int length, int func)
1263 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1264 retv = sym_user_command(shost, buffer, length);
1271 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1272 retv = sym_host_info(shost, buffer, offset, length);
1280 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1283 * Free resources claimed by sym_iomap_device(). Note that
1284 * sym_free_resources() should be used instead of this function after calling
1287 static void sym_iounmap_device(struct sym_device *device)
1289 if (device->s.ioaddr)
1290 pci_iounmap(device->pdev, device->s.ioaddr);
1291 if (device->s.ramaddr)
1292 pci_iounmap(device->pdev, device->s.ramaddr);
1296 * Free controller resources.
1298 static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev,
1302 * Free O/S specific resources.
1305 free_irq(pdev->irq, np->s.host);
1307 pci_iounmap(pdev, np->s.ioaddr);
1309 pci_iounmap(pdev, np->s.ramaddr);
1311 * Free O/S independent resources.
1315 sym_mfree_dma(np, sizeof(*np), "HCB");
1319 * Host attach and initialisations.
1321 * Allocate host data and ncb structure.
1322 * Remap MMIO region.
1323 * Do chip initialization.
1324 * If all is OK, install interrupt handling and
1325 * start the timer daemon.
1327 static struct Scsi_Host *sym_attach(struct scsi_host_template *tpnt, int unit,
1328 struct sym_device *dev)
1330 struct sym_data *sym_data;
1331 struct sym_hcb *np = NULL;
1332 struct Scsi_Host *shost = NULL;
1333 struct pci_dev *pdev = dev->pdev;
1334 unsigned long flags;
1336 int do_free_irq = 0;
1338 printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq %u\n",
1339 unit, dev->chip.name, pdev->revision, pci_name(pdev),
1343 * Get the firmware for this chip.
1345 fw = sym_find_firmware(&dev->chip);
1349 shost = scsi_host_alloc(tpnt, sizeof(*sym_data));
1352 sym_data = shost_priv(shost);
1355 * Allocate immediately the host control block,
1356 * since we are only expecting to succeed. :)
1357 * We keep track in the HCB of all the resources that
1358 * are to be released on error.
1360 np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
1363 np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
1365 sym_data->pdev = pdev;
1368 pci_set_drvdata(pdev, shost);
1371 * Copy some useful infos to the HCB.
1373 np->hcb_ba = vtobus(np);
1374 np->verbose = sym_driver_setup.verbose;
1376 np->features = dev->chip.features;
1377 np->clock_divn = dev->chip.nr_divisor;
1378 np->maxoffs = dev->chip.offset_max;
1379 np->maxburst = dev->chip.burst_max;
1380 np->myaddr = dev->host_id;
1381 np->mmio_ba = (u32)dev->mmio_base;
1382 np->ram_ba = (u32)dev->ram_base;
1383 np->s.ioaddr = dev->s.ioaddr;
1384 np->s.ramaddr = dev->s.ramaddr;
1389 strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1390 sprintf(np->s.inst_name, "sym%d", np->s.unit);
1392 if ((SYM_CONF_DMA_ADDRESSING_MODE > 0) && (np->features & FE_DAC) &&
1393 !pci_set_dma_mask(pdev, DMA_DAC_MASK)) {
1395 } else if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
1396 printf_warning("%s: No suitable DMA available\n", sym_name(np));
1400 if (sym_hcb_attach(shost, fw, dev->nvram))
1404 * Install the interrupt handler.
1405 * If we synchonize the C code with SCRIPTS on interrupt,
1406 * we do not want to share the INTR line at all.
1408 if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX,
1410 printf_err("%s: request irq %u failure\n",
1411 sym_name(np), pdev->irq);
1417 * After SCSI devices have been opened, we cannot
1418 * reset the bus safely, so we do it here.
1420 spin_lock_irqsave(shost->host_lock, flags);
1421 if (sym_reset_scsi_bus(np, 0))
1425 * Start the SCRIPTS.
1427 sym_start_up(shost, 1);
1430 * Start the timer daemon
1432 init_timer(&np->s.timer);
1433 np->s.timer.data = (unsigned long) np;
1434 np->s.timer.function = sym53c8xx_timer;
1439 * Fill Linux host instance structure
1440 * and return success.
1442 shost->max_channel = 0;
1443 shost->this_id = np->myaddr;
1444 shost->max_id = np->maxwide ? 16 : 8;
1445 shost->max_lun = SYM_CONF_MAX_LUN;
1446 shost->unique_id = pci_resource_start(pdev, 0);
1447 shost->cmd_per_lun = SYM_CONF_MAX_TAG;
1448 shost->can_queue = (SYM_CONF_MAX_START-2);
1449 shost->sg_tablesize = SYM_CONF_MAX_SG;
1450 shost->max_cmd_len = 16;
1451 BUG_ON(sym2_transport_template == NULL);
1452 shost->transportt = sym2_transport_template;
1454 /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
1455 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
1456 shost->dma_boundary = 0xFFFFFF;
1458 spin_unlock_irqrestore(shost->host_lock, flags);
1463 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1464 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1465 spin_unlock_irqrestore(shost->host_lock, flags);
1467 printf_info("sym%d: giving up ...\n", unit);
1469 sym_free_resources(np, pdev, do_free_irq);
1471 sym_iounmap_device(dev);
1473 scsi_host_put(shost);
1480 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1482 #if SYM_CONF_NVRAM_SUPPORT
1483 static void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1488 sym_read_nvram(devp, nvp);
1491 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1494 #endif /* SYM_CONF_NVRAM_SUPPORT */
1496 static int sym_check_supported(struct sym_device *device)
1498 struct sym_chip *chip;
1499 struct pci_dev *pdev = device->pdev;
1500 unsigned long io_port = pci_resource_start(pdev, 0);
1504 * If user excluded this chip, do not initialize it.
1505 * I hate this code so much. Must kill it.
1508 for (i = 0 ; i < 8 ; i++) {
1509 if (sym_driver_setup.excludes[i] == io_port)
1515 * Check if the chip is supported. Then copy the chip description
1516 * to our device structure so we can make it match the actual device
1519 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
1521 dev_info(&pdev->dev, "device not supported\n");
1524 memcpy(&device->chip, chip, sizeof(device->chip));
1530 * Ignore Symbios chips controlled by various RAID controllers.
1531 * These controllers set value 0x52414944 at RAM end - 16.
1533 static int sym_check_raid(struct sym_device *device)
1535 unsigned int ram_size, ram_val;
1537 if (!device->s.ramaddr)
1540 if (device->chip.features & FE_RAM8K)
1545 ram_val = readl(device->s.ramaddr + ram_size - 16);
1546 if (ram_val != 0x52414944)
1549 dev_info(&device->pdev->dev,
1550 "not initializing, driven by RAID controller.\n");
1554 static int sym_set_workarounds(struct sym_device *device)
1556 struct sym_chip *chip = &device->chip;
1557 struct pci_dev *pdev = device->pdev;
1561 * (ITEM 12 of a DEL about the 896 I haven't yet).
1562 * We must ensure the chip will use WRITE AND INVALIDATE.
1563 * The revision number limit is for now arbitrary.
1565 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 0x4) {
1566 chip->features |= (FE_WRIE | FE_CLSE);
1569 /* If the chip can do Memory Write Invalidate, enable it */
1570 if (chip->features & FE_WRIE) {
1571 if (pci_set_mwi(pdev))
1576 * Work around for errant bit in 895A. The 66Mhz
1577 * capable bit is set erroneously. Clear this bit.
1580 * Make sure Config space and Features agree.
1582 * Recall: writes are not normal to status register -
1583 * write a 1 to clear and a 0 to leave unchanged.
1584 * Can only reset bits.
1586 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1587 if (chip->features & FE_66MHZ) {
1588 if (!(status_reg & PCI_STATUS_66MHZ))
1589 chip->features &= ~FE_66MHZ;
1591 if (status_reg & PCI_STATUS_66MHZ) {
1592 status_reg = PCI_STATUS_66MHZ;
1593 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1594 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1602 * Map HBA registers and on-chip SRAM (if present).
1604 static int sym_iomap_device(struct sym_device *device)
1606 struct pci_dev *pdev = device->pdev;
1607 struct pci_bus_region bus_addr;
1610 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
1611 device->mmio_base = bus_addr.start;
1613 if (device->chip.features & FE_RAM) {
1615 * If the BAR is 64-bit, resource 2 will be occupied by the
1618 if (!pdev->resource[i].flags)
1620 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
1621 device->ram_base = bus_addr.start;
1624 #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
1625 if (device->mmio_base)
1626 device->s.ioaddr = pci_iomap(pdev, 1,
1627 pci_resource_len(pdev, 1));
1629 if (!device->s.ioaddr)
1630 device->s.ioaddr = pci_iomap(pdev, 0,
1631 pci_resource_len(pdev, 0));
1632 if (!device->s.ioaddr) {
1633 dev_err(&pdev->dev, "could not map registers; giving up.\n");
1636 if (device->ram_base) {
1637 device->s.ramaddr = pci_iomap(pdev, i,
1638 pci_resource_len(pdev, i));
1639 if (!device->s.ramaddr) {
1640 dev_warn(&pdev->dev,
1641 "could not map SRAM; continuing anyway.\n");
1642 device->ram_base = 0;
1650 * The NCR PQS and PDS cards are constructed as a DEC bridge
1651 * behind which sits a proprietary NCR memory controller and
1652 * either four or two 53c875s as separate devices. We can tell
1653 * if an 875 is part of a PQS/PDS or not since if it is, it will
1654 * be on the same bus as the memory controller. In its usual
1655 * mode of operation, the 875s are slaved to the memory
1656 * controller for all transfers. To operate with the Linux
1657 * driver, the memory controller is disabled and the 875s
1658 * freed to function independently. The only wrinkle is that
1659 * the preset SCSI ID (which may be zero) must be read in from
1660 * a special configuration space register of the 875.
1662 static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
1667 for (slot = 0; slot < 256; slot++) {
1668 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
1670 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
1675 /* bit 1: allow individual 875 configuration */
1676 pci_read_config_byte(memc, 0x44, &tmp);
1677 if ((tmp & 0x2) == 0) {
1679 pci_write_config_byte(memc, 0x44, tmp);
1682 /* bit 2: drive individual 875 interrupts to the bus */
1683 pci_read_config_byte(memc, 0x45, &tmp);
1684 if ((tmp & 0x4) == 0) {
1686 pci_write_config_byte(memc, 0x45, tmp);
1693 pci_read_config_byte(pdev, 0x84, &tmp);
1694 sym_dev->host_id = tmp;
1698 * Called before unloading the module.
1700 * We have to free resources and halt the NCR chip.
1702 static int sym_detach(struct Scsi_Host *shost, struct pci_dev *pdev)
1704 struct sym_hcb *np = sym_get_hcb(shost);
1705 printk("%s: detaching ...\n", sym_name(np));
1707 del_timer_sync(&np->s.timer);
1711 * We should use sym_soft_reset(), but we don't want to do
1712 * so, since we may not be safe if interrupts occur.
1714 printk("%s: resetting chip\n", sym_name(np));
1715 OUTB(np, nc_istat, SRST);
1718 OUTB(np, nc_istat, 0);
1720 sym_free_resources(np, pdev, 1);
1721 scsi_host_put(shost);
1727 * Driver host template.
1729 static struct scsi_host_template sym2_template = {
1730 .module = THIS_MODULE,
1731 .name = "sym53c8xx",
1732 .info = sym53c8xx_info,
1733 .queuecommand = sym53c8xx_queue_command,
1734 .slave_alloc = sym53c8xx_slave_alloc,
1735 .slave_configure = sym53c8xx_slave_configure,
1736 .slave_destroy = sym53c8xx_slave_destroy,
1737 .eh_abort_handler = sym53c8xx_eh_abort_handler,
1738 .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
1739 .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
1740 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
1742 .use_clustering = ENABLE_CLUSTERING,
1743 .max_sectors = 0xFFFF,
1744 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1745 .proc_info = sym53c8xx_proc_info,
1746 .proc_name = NAME53C8XX,
1750 static int attach_count;
1752 static int sym2_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1754 struct sym_device sym_dev;
1755 struct sym_nvram nvram;
1756 struct Scsi_Host *shost;
1758 int do_disable_device = 1;
1760 memset(&sym_dev, 0, sizeof(sym_dev));
1761 memset(&nvram, 0, sizeof(nvram));
1762 sym_dev.pdev = pdev;
1763 sym_dev.host_id = SYM_SETUP_HOST_ID;
1765 if (pci_enable_device(pdev))
1768 pci_set_master(pdev);
1770 if (pci_request_regions(pdev, NAME53C8XX))
1773 if (sym_check_supported(&sym_dev))
1776 if (sym_iomap_device(&sym_dev))
1780 if (sym_check_raid(&sym_dev)) {
1781 do_disable_device = 0; /* Don't disable the device */
1785 if (sym_set_workarounds(&sym_dev))
1788 sym_config_pqs(pdev, &sym_dev);
1790 sym_get_nvram(&sym_dev, &nvram);
1792 do_iounmap = 0; /* Don't sym_iounmap_device() after sym_attach(). */
1793 shost = sym_attach(&sym2_template, attach_count, &sym_dev);
1797 if (scsi_add_host(shost, &pdev->dev))
1799 scsi_scan_host(shost);
1806 sym_detach(pci_get_drvdata(pdev), pdev);
1809 sym_iounmap_device(&sym_dev);
1810 pci_release_regions(pdev);
1812 if (do_disable_device)
1813 pci_disable_device(pdev);
1818 static void sym2_remove(struct pci_dev *pdev)
1820 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1822 scsi_remove_host(shost);
1823 sym_detach(shost, pdev);
1824 pci_release_regions(pdev);
1825 pci_disable_device(pdev);
1831 * sym2_io_error_detected() - called when PCI error is detected
1832 * @pdev: pointer to PCI device
1833 * @state: current state of the PCI slot
1835 static pci_ers_result_t sym2_io_error_detected(struct pci_dev *pdev,
1836 enum pci_channel_state state)
1838 /* If slot is permanently frozen, turn everything off */
1839 if (state == pci_channel_io_perm_failure) {
1841 return PCI_ERS_RESULT_DISCONNECT;
1844 disable_irq(pdev->irq);
1845 pci_disable_device(pdev);
1847 /* Request that MMIO be enabled, so register dump can be taken. */
1848 return PCI_ERS_RESULT_CAN_RECOVER;
1852 * sym2_io_slot_dump - Enable MMIO and dump debug registers
1853 * @pdev: pointer to PCI device
1855 static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
1857 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1859 sym_dump_registers(shost);
1861 /* Request a slot reset. */
1862 return PCI_ERS_RESULT_NEED_RESET;
1866 * sym2_reset_workarounds - hardware-specific work-arounds
1868 * This routine is similar to sym_set_workarounds(), except
1869 * that, at this point, we already know that the device was
1870 * successfully initialized at least once before, and so most
1871 * of the steps taken there are un-needed here.
1873 static void sym2_reset_workarounds(struct pci_dev *pdev)
1876 struct sym_chip *chip;
1878 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
1880 /* Work around for errant bit in 895A, in a fashion
1881 * similar to what is done in sym_set_workarounds().
1883 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1884 if (!(chip->features & FE_66MHZ) && (status_reg & PCI_STATUS_66MHZ)) {
1885 status_reg = PCI_STATUS_66MHZ;
1886 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1887 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1892 * sym2_io_slot_reset() - called when the pci bus has been reset.
1893 * @pdev: pointer to PCI device
1895 * Restart the card from scratch.
1897 static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
1899 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1900 struct sym_hcb *np = sym_get_hcb(shost);
1902 printk(KERN_INFO "%s: recovering from a PCI slot reset\n",
1905 if (pci_enable_device(pdev)) {
1906 printk(KERN_ERR "%s: Unable to enable after PCI reset\n",
1908 return PCI_ERS_RESULT_DISCONNECT;
1911 pci_set_master(pdev);
1912 enable_irq(pdev->irq);
1914 /* If the chip can do Memory Write Invalidate, enable it */
1915 if (np->features & FE_WRIE) {
1916 if (pci_set_mwi(pdev))
1917 return PCI_ERS_RESULT_DISCONNECT;
1920 /* Perform work-arounds, analogous to sym_set_workarounds() */
1921 sym2_reset_workarounds(pdev);
1923 /* Perform host reset only on one instance of the card */
1924 if (PCI_FUNC(pdev->devfn) == 0) {
1925 if (sym_reset_scsi_bus(np, 0)) {
1926 printk(KERN_ERR "%s: Unable to reset scsi host\n",
1928 return PCI_ERS_RESULT_DISCONNECT;
1930 sym_start_up(shost, 1);
1933 return PCI_ERS_RESULT_RECOVERED;
1937 * sym2_io_resume() - resume normal ops after PCI reset
1938 * @pdev: pointer to PCI device
1940 * Called when the error recovery driver tells us that its
1941 * OK to resume normal operation. Use completion to allow
1942 * halted scsi ops to resume.
1944 static void sym2_io_resume(struct pci_dev *pdev)
1946 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1947 struct sym_data *sym_data = shost_priv(shost);
1949 spin_lock_irq(shost->host_lock);
1950 if (sym_data->io_reset)
1951 complete_all(sym_data->io_reset);
1952 spin_unlock_irq(shost->host_lock);
1955 static void sym2_get_signalling(struct Scsi_Host *shost)
1957 struct sym_hcb *np = sym_get_hcb(shost);
1958 enum spi_signal_type type;
1960 switch (np->scsi_mode) {
1962 type = SPI_SIGNAL_SE;
1965 type = SPI_SIGNAL_LVD;
1968 type = SPI_SIGNAL_HVD;
1971 type = SPI_SIGNAL_UNKNOWN;
1974 spi_signalling(shost) = type;
1977 static void sym2_set_offset(struct scsi_target *starget, int offset)
1979 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1980 struct sym_hcb *np = sym_get_hcb(shost);
1981 struct sym_tcb *tp = &np->target[starget->id];
1983 tp->tgoal.offset = offset;
1984 tp->tgoal.check_nego = 1;
1987 static void sym2_set_period(struct scsi_target *starget, int period)
1989 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1990 struct sym_hcb *np = sym_get_hcb(shost);
1991 struct sym_tcb *tp = &np->target[starget->id];
1993 /* have to have DT for these transfers, but DT will also
1994 * set width, so check that this is allowed */
1995 if (period <= np->minsync && spi_width(starget))
1998 tp->tgoal.period = period;
1999 tp->tgoal.check_nego = 1;
2002 static void sym2_set_width(struct scsi_target *starget, int width)
2004 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2005 struct sym_hcb *np = sym_get_hcb(shost);
2006 struct sym_tcb *tp = &np->target[starget->id];
2008 /* It is illegal to have DT set on narrow transfers. If DT is
2009 * clear, we must also clear IU and QAS. */
2011 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
2013 tp->tgoal.width = width;
2014 tp->tgoal.check_nego = 1;
2017 static void sym2_set_dt(struct scsi_target *starget, int dt)
2019 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2020 struct sym_hcb *np = sym_get_hcb(shost);
2021 struct sym_tcb *tp = &np->target[starget->id];
2023 /* We must clear QAS and IU if DT is clear */
2027 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
2028 tp->tgoal.check_nego = 1;
2032 static void sym2_set_iu(struct scsi_target *starget, int iu)
2034 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2035 struct sym_hcb *np = sym_get_hcb(shost);
2036 struct sym_tcb *tp = &np->target[starget->id];
2039 tp->tgoal.iu = tp->tgoal.dt = 1;
2042 tp->tgoal.check_nego = 1;
2045 static void sym2_set_qas(struct scsi_target *starget, int qas)
2047 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2048 struct sym_hcb *np = sym_get_hcb(shost);
2049 struct sym_tcb *tp = &np->target[starget->id];
2052 tp->tgoal.dt = tp->tgoal.qas = 1;
2055 tp->tgoal.check_nego = 1;
2059 static struct spi_function_template sym2_transport_functions = {
2060 .set_offset = sym2_set_offset,
2062 .set_period = sym2_set_period,
2064 .set_width = sym2_set_width,
2066 .set_dt = sym2_set_dt,
2069 .set_iu = sym2_set_iu,
2071 .set_qas = sym2_set_qas,
2074 .get_signalling = sym2_get_signalling,
2077 static struct pci_device_id sym2_id_table[] = {
2078 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
2079 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2080 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
2081 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2082 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
2083 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2084 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
2085 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2086 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
2087 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2088 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
2089 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2090 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
2091 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
2092 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
2093 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2094 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
2095 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2096 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2097 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2098 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2099 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2100 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
2101 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
2102 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2103 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2104 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2105 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2106 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2107 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2108 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2109 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2110 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2111 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2115 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2117 static const struct pci_error_handlers sym2_err_handler = {
2118 .error_detected = sym2_io_error_detected,
2119 .mmio_enabled = sym2_io_slot_dump,
2120 .slot_reset = sym2_io_slot_reset,
2121 .resume = sym2_io_resume,
2124 static struct pci_driver sym2_driver = {
2126 .id_table = sym2_id_table,
2127 .probe = sym2_probe,
2128 .remove = sym2_remove,
2129 .err_handler = &sym2_err_handler,
2132 static int __init sym2_init(void)
2136 sym2_setup_params();
2137 sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2138 if (!sym2_transport_template)
2141 error = pci_register_driver(&sym2_driver);
2143 spi_release_transport(sym2_transport_template);
2147 static void __exit sym2_exit(void)
2149 pci_unregister_driver(&sym2_driver);
2150 spi_release_transport(sym2_transport_template);
2153 module_init(sym2_init);
2154 module_exit(sym2_exit);