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/interrupt.h>
43 #include <linux/module.h>
44 #include <linux/moduleparam.h>
45 #include <linux/spinlock.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_tcq.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_transport.h>
52 #include "sym_nvram.h"
54 #define NAME53C "sym53c"
55 #define NAME53C8XX "sym53c8xx"
58 #define IRQ_PRM(x) (x)
60 struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
61 unsigned int sym_debug_flags = 0;
63 static char *excl_string;
64 static char *safe_string;
65 module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
66 module_param_string(tag_ctrl, sym_driver_setup.tag_ctrl, 100, 0);
67 module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
68 module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
69 module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
70 module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
71 module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
72 module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
73 module_param_named(verb, sym_driver_setup.verbose, byte, 0);
74 module_param_named(debug, sym_debug_flags, uint, 0);
75 module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
76 module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
77 module_param_named(excl, excl_string, charp, 0);
78 module_param_named(safe, safe_string, charp, 0);
80 MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
81 MODULE_PARM_DESC(tag_ctrl, "More detailed control over tags per LUN");
82 MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
83 MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
84 MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
85 MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
86 MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
87 MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
88 MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
89 MODULE_PARM_DESC(debug, "Set bits to enable debugging");
90 MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
91 MODULE_PARM_DESC(nvram, "Option currently not used");
92 MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
93 MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(SYM_VERSION);
97 MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
98 MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
100 static void sym2_setup_params(void)
102 char *p = excl_string;
105 while (p && (xi < 8)) {
107 int val = (int) simple_strtoul(p, &next_p, 0);
108 sym_driver_setup.excludes[xi++] = val;
113 if (*safe_string == 'y') {
114 sym_driver_setup.max_tag = 0;
115 sym_driver_setup.burst_order = 0;
116 sym_driver_setup.scsi_led = 0;
117 sym_driver_setup.scsi_diff = 1;
118 sym_driver_setup.irq_mode = 0;
119 sym_driver_setup.scsi_bus_check = 2;
120 sym_driver_setup.host_id = 7;
121 sym_driver_setup.verbose = 2;
122 sym_driver_setup.settle_delay = 10;
123 sym_driver_setup.use_nvram = 1;
124 } else if (*safe_string != 'n') {
125 printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
126 " passed to safe option", safe_string);
131 static struct scsi_transport_template *sym2_transport_template = NULL;
134 * Driver private area in the SCSI command structure.
136 struct sym_ucmd { /* Override the SCSI pointer structure */
137 struct completion *eh_done; /* SCSI error handling */
140 #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
141 #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
144 * Complete a pending CAM CCB.
146 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
148 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
149 BUILD_BUG_ON(sizeof(struct scsi_pointer) < sizeof(struct sym_ucmd));
152 complete(ucmd->eh_done);
159 * Tell the SCSI layer about a BUS RESET.
161 void sym_xpt_async_bus_reset(struct sym_hcb *np)
163 printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
164 np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
165 np->s.settle_time_valid = 1;
166 if (sym_verbose >= 2)
167 printf_info("%s: command processing suspended for %d seconds\n",
168 sym_name(np), sym_driver_setup.settle_delay);
172 * Tell the SCSI layer about a BUS DEVICE RESET message sent.
174 void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
176 printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
180 * Choose the more appropriate CAM status if
181 * the IO encountered an extended error.
183 static int sym_xerr_cam_status(int cam_status, int x_status)
186 if (x_status & XE_PARITY_ERR)
187 cam_status = DID_PARITY;
188 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
189 cam_status = DID_ERROR;
190 else if (x_status & XE_BAD_PHASE)
191 cam_status = DID_ERROR;
193 cam_status = DID_ERROR;
199 * Build CAM result for a failed or auto-sensed IO.
201 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
203 struct scsi_cmnd *cmd = cp->cmd;
204 u_int cam_status, scsi_status, drv_status;
208 scsi_status = cp->ssss_status;
210 if (cp->host_flags & HF_SENSE) {
211 scsi_status = cp->sv_scsi_status;
212 resid = cp->sv_resid;
213 if (sym_verbose && cp->sv_xerr_status)
214 sym_print_xerr(cmd, cp->sv_xerr_status);
215 if (cp->host_status == HS_COMPLETE &&
216 cp->ssss_status == S_GOOD &&
217 cp->xerr_status == 0) {
218 cam_status = sym_xerr_cam_status(DID_OK,
220 drv_status = DRIVER_SENSE;
222 * Bounce back the sense data to user.
224 memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
225 memcpy(cmd->sense_buffer, cp->sns_bbuf,
226 min(sizeof(cmd->sense_buffer),
227 (size_t)SYM_SNS_BBUF_LEN));
230 * If the device reports a UNIT ATTENTION condition
231 * due to a RESET condition, we should consider all
232 * disconnect CCBs for this unit as aborted.
236 p = (u_char *) cmd->sense_data;
237 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
238 sym_clear_tasks(np, DID_ABORT,
239 cp->target,cp->lun, -1);
244 * Error return from our internal request sense. This
245 * is bad: we must clear the contingent allegiance
246 * condition otherwise the device will always return
247 * BUSY. Use a big stick.
249 sym_reset_scsi_target(np, cmd->device->id);
250 cam_status = DID_ERROR;
252 } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
254 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
255 cam_status = DID_NO_CONNECT;
256 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
257 cam_status = DID_ERROR;
258 else { /* Extended error */
260 sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
261 cp->host_status, cp->ssss_status,
265 * Set the most appropriate value for CAM status.
267 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
269 scsi_set_resid(cmd, resid);
270 cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
273 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
280 use_sg = scsi_dma_map(cmd);
282 struct scatterlist *sg;
283 struct sym_tcb *tp = &np->target[cp->target];
284 struct sym_tblmove *data;
286 if (use_sg > SYM_CONF_MAX_SG) {
291 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
293 scsi_for_each_sg(cmd, sg, use_sg, segment) {
294 dma_addr_t baddr = sg_dma_address(sg);
295 unsigned int len = sg_dma_len(sg);
297 if ((len & 1) && (tp->head.wval & EWS)) {
299 cp->odd_byte_adjustment++;
302 sym_build_sge(np, &data[segment], baddr, len);
313 * Queue a SCSI command.
315 static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
317 struct scsi_device *sdev = cmd->device;
324 * Retrieve the target descriptor.
326 tp = &np->target[sdev->id];
329 * Select tagged/untagged.
331 lp = sym_lp(tp, sdev->lun);
332 order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
337 cp = sym_get_ccb(np, cmd, order);
339 return 1; /* Means resource shortage */
340 sym_queue_scsiio(np, cmd, cp);
345 * Setup buffers and pointers that address the CDB.
347 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
349 memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
351 cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
352 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
358 * Setup pointers that address the data and start the I/O.
360 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
368 if (sym_setup_cdb(np, cmd, cp))
372 * No direction means no data.
374 dir = cmd->sc_data_direction;
375 if (dir != DMA_NONE) {
376 cp->segments = sym_scatter(np, cp, cmd);
377 if (cp->segments < 0) {
378 sym_set_cam_status(cmd, DID_ERROR);
383 * No segments means no data.
393 * Set the data pointer.
396 case DMA_BIDIRECTIONAL:
397 printk("%s: got DMA_BIDIRECTIONAL command", sym_name(np));
398 sym_set_cam_status(cmd, DID_ERROR);
401 goalp = SCRIPTA_BA(np, data_out2) + 8;
402 lastp = goalp - 8 - (cp->segments * (2*4));
404 case DMA_FROM_DEVICE:
405 cp->host_flags |= HF_DATA_IN;
406 goalp = SCRIPTA_BA(np, data_in2) + 8;
407 lastp = goalp - 8 - (cp->segments * (2*4));
411 lastp = goalp = SCRIPTB_BA(np, no_data);
416 * Set all pointers values needed by SCRIPTS.
418 cp->phys.head.lastp = cpu_to_scr(lastp);
419 cp->phys.head.savep = cpu_to_scr(lastp);
420 cp->startp = cp->phys.head.savep;
421 cp->goalp = cpu_to_scr(goalp);
424 * When `#ifed 1', the code below makes the driver
425 * panic on the first attempt to write to a SCSI device.
426 * It is the first test we want to do after a driver
427 * change that does not seem obviously safe. :)
430 switch (cp->cdb_buf[0]) {
431 case 0x0A: case 0x2A: case 0xAA:
432 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
442 sym_put_start_queue(np, cp);
446 sym_free_ccb(np, cp);
447 sym_xpt_done(np, cmd);
455 * Misused to keep the driver running when
456 * interrupts are not configured correctly.
458 static void sym_timer(struct sym_hcb *np)
460 unsigned long thistime = jiffies;
465 np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
466 add_timer(&np->s.timer);
469 * If we are resetting the ncr, wait for settle_time before
470 * clearing it. Then command processing will be resumed.
472 if (np->s.settle_time_valid) {
473 if (time_before_eq(np->s.settle_time, thistime)) {
474 if (sym_verbose >= 2 )
475 printk("%s: command processing resumed\n",
477 np->s.settle_time_valid = 0;
483 * Nothing to do for now, but that may come.
485 if (np->s.lasttime + 4*HZ < thistime) {
486 np->s.lasttime = thistime;
489 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
491 * Some way-broken PCI bridges may lead to
492 * completions being lost when the clearing
493 * of the INTFLY flag by the CPU occurs
494 * concurrently with the chip raising this flag.
495 * If this ever happen, lost completions will
504 * PCI BUS error handler.
506 void sym_log_bus_error(struct sym_hcb *np)
509 pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
510 if (pci_sts & 0xf900) {
511 pci_write_config_word(np->s.device, PCI_STATUS, pci_sts);
512 printf("%s: PCI STATUS = 0x%04x\n",
513 sym_name(np), pci_sts & 0xf900);
518 * queuecommand method. Entered with the host adapter lock held and
519 * interrupts disabled.
521 static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
522 void (*done)(struct scsi_cmnd *))
524 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
525 struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
528 cmd->scsi_done = done;
529 memset(ucp, 0, sizeof(*ucp));
532 * Shorten our settle_time if needed for
533 * this command not to time out.
535 if (np->s.settle_time_valid && cmd->timeout_per_command) {
536 unsigned long tlimit = jiffies + cmd->timeout_per_command;
537 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
538 if (time_after(np->s.settle_time, tlimit)) {
539 np->s.settle_time = tlimit;
543 if (np->s.settle_time_valid)
544 return SCSI_MLQUEUE_HOST_BUSY;
546 sts = sym_queue_command(np, cmd);
548 return SCSI_MLQUEUE_HOST_BUSY;
553 * Linux entry point of the interrupt handler.
555 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
557 struct sym_hcb *np = dev_id;
559 /* Avoid spinloop trying to handle interrupts on frozen device */
560 if (pci_channel_offline(np->s.device))
563 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
565 spin_lock(np->s.host->host_lock);
567 spin_unlock(np->s.host->host_lock);
569 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
575 * Linux entry point of the timer handler
577 static void sym53c8xx_timer(unsigned long npref)
579 struct sym_hcb *np = (struct sym_hcb *)npref;
582 spin_lock_irqsave(np->s.host->host_lock, flags);
584 spin_unlock_irqrestore(np->s.host->host_lock, flags);
589 * What the eh thread wants us to perform.
591 #define SYM_EH_ABORT 0
592 #define SYM_EH_DEVICE_RESET 1
593 #define SYM_EH_BUS_RESET 2
594 #define SYM_EH_HOST_RESET 3
597 * Generic method for our eh processing.
598 * The 'op' argument tells what we have to do.
600 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
602 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
603 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
604 struct Scsi_Host *host = cmd->device->host;
605 struct pci_dev *pdev = np->s.device;
609 struct completion eh_done;
611 dev_warn(&cmd->device->sdev_gendev, "%s operation started.\n", opname);
613 /* We may be in an error condition because the PCI bus
614 * went down. In this case, we need to wait until the
615 * PCI bus is reset, the card is reset, and only then
616 * proceed with the scsi error recovery. There's no
617 * point in hurrying; take a leisurely wait.
619 #define WAIT_FOR_PCI_RECOVERY 35
620 if (pci_channel_offline(pdev)) {
621 struct host_data *hostdata = shost_priv(host);
622 struct completion *io_reset;
623 int finished_reset = 0;
624 init_completion(&eh_done);
625 spin_lock_irq(host->host_lock);
626 /* Make sure we didn't race */
627 if (pci_channel_offline(pdev)) {
628 if (!hostdata->io_reset)
629 hostdata->io_reset = &eh_done;
630 io_reset = hostdata->io_reset;
635 if (!pci_channel_offline(pdev))
637 spin_unlock_irq(host->host_lock);
639 finished_reset = wait_for_completion_timeout(io_reset,
640 WAIT_FOR_PCI_RECOVERY*HZ);
645 spin_lock_irq(host->host_lock);
646 /* This one is queued in some place -> to wait for completion */
647 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
648 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
649 if (cp->cmd == cmd) {
655 /* Try to proceed the operation we have been asked for */
659 sts = sym_abort_scsiio(np, cmd, 1);
661 case SYM_EH_DEVICE_RESET:
662 sts = sym_reset_scsi_target(np, cmd->device->id);
664 case SYM_EH_BUS_RESET:
665 sym_reset_scsi_bus(np, 1);
668 case SYM_EH_HOST_RESET:
669 sym_reset_scsi_bus(np, 0);
677 /* On error, restore everything and cross fingers :) */
682 init_completion(&eh_done);
683 ucmd->eh_done = &eh_done;
684 spin_unlock_irq(host->host_lock);
685 if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
686 ucmd->eh_done = NULL;
690 spin_unlock_irq(host->host_lock);
693 dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
694 sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
695 return sts ? SCSI_FAILED : SCSI_SUCCESS;
700 * Error handlers called from the eh thread (one thread per HBA).
702 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
704 return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
707 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
709 return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
712 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
714 return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
717 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
719 return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
723 * Tune device queuing depth, according to various limits.
725 static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
727 struct sym_lcb *lp = sym_lp(tp, lun);
733 oldtags = lp->s.reqtags;
735 if (reqtags > lp->s.scdev_depth)
736 reqtags = lp->s.scdev_depth;
738 lp->s.reqtags = reqtags;
740 if (reqtags != oldtags) {
741 dev_info(&tp->starget->dev,
742 "tagged command queuing %s, command queue depth %d.\n",
743 lp->s.reqtags ? "enabled" : "disabled", reqtags);
748 * Linux select queue depths function
750 #define DEF_DEPTH (sym_driver_setup.max_tag)
751 #define ALL_TARGETS -2
756 static int device_queue_depth(struct sym_hcb *np, int target, int lun)
759 char *p = sym_driver_setup.tag_ctrl;
765 while ((c = *p++) != 0) {
766 v = simple_strtoul(p, &ep, 0);
775 t = (target == v) ? v : NO_TARGET;
780 u = (lun == v) ? v : NO_LUN;
783 if (h == np->s.unit &&
784 (t == ALL_TARGETS || t == target) &&
785 (u == ALL_LUNS || u == lun))
800 static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
802 struct sym_hcb *np = sym_get_hcb(sdev->host);
803 struct sym_tcb *tp = &np->target[sdev->id];
806 if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
809 tp->starget = sdev->sdev_target;
811 * Fail the device init if the device is flagged NOSCAN at BOOT in
812 * the NVRAM. This may speed up boot and maintain coherency with
813 * BIOS device numbering. Clearing the flag allows the user to
814 * rescan skipped devices later. We also return an error for
815 * devices not flagged for SCAN LUNS in the NVRAM since some single
816 * lun devices behave badly when asked for a non zero LUN.
819 if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
820 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
821 starget_printk(KERN_INFO, tp->starget,
822 "Scan at boot disabled in NVRAM\n");
826 if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
829 starget_printk(KERN_INFO, tp->starget,
830 "Multiple LUNs disabled in NVRAM\n");
833 lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
837 spi_min_period(tp->starget) = tp->usr_period;
838 spi_max_width(tp->starget) = tp->usr_width;
844 * Linux entry point for device queue sizing.
846 static int sym53c8xx_slave_configure(struct scsi_device *sdev)
848 struct sym_hcb *np = sym_get_hcb(sdev->host);
849 struct sym_tcb *tp = &np->target[sdev->id];
850 struct sym_lcb *lp = sym_lp(tp, sdev->lun);
851 int reqtags, depth_to_use;
856 lp->curr_flags = lp->user_flags;
859 * Select queue depth from driver setup.
860 * Donnot use more than configured by user.
862 * Donnot use more than our maximum.
864 reqtags = device_queue_depth(np, sdev->id, sdev->lun);
865 if (reqtags > tp->usrtags)
866 reqtags = tp->usrtags;
867 if (!sdev->tagged_supported)
869 #if 1 /* Avoid to locally queue commands for no good reasons */
870 if (reqtags > SYM_CONF_MAX_TAG)
871 reqtags = SYM_CONF_MAX_TAG;
872 depth_to_use = (reqtags ? reqtags : 2);
874 depth_to_use = (reqtags ? SYM_CONF_MAX_TAG : 2);
876 scsi_adjust_queue_depth(sdev,
877 (sdev->tagged_supported ?
880 lp->s.scdev_depth = depth_to_use;
881 sym_tune_dev_queuing(tp, sdev->lun, reqtags);
883 if (!spi_initial_dv(sdev->sdev_target))
889 static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
891 struct sym_hcb *np = sym_get_hcb(sdev->host);
892 struct sym_lcb *lp = sym_lp(&np->target[sdev->id], sdev->lun);
895 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK * 4, "ITLQ_TBL");
897 sym_mfree_dma(lp, sizeof(*lp), "LCB");
901 * Linux entry point for info() function
903 static const char *sym53c8xx_info (struct Scsi_Host *host)
905 return SYM_DRIVER_NAME;
909 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
911 * Proc file system stuff
913 * A read operation returns adapter information.
914 * A write operation is a control command.
915 * The string is parsed in the driver code and the command is passed
916 * to the sym_usercmd() function.
919 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
928 #define UC_SETSYNC 10
929 #define UC_SETTAGS 11
930 #define UC_SETDEBUG 12
931 #define UC_SETWIDE 14
932 #define UC_SETFLAG 15
933 #define UC_SETVERBOSE 17
934 #define UC_RESETDEV 18
935 #define UC_CLEARDEV 19
937 static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
945 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
947 sym_debug_flags = uc->data;
951 np->verbose = uc->data;
955 * We assume that other commands apply to targets.
956 * This should always be the case and avoid the below
957 * 4 lines to be repeated 6 times.
959 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
960 if (!((uc->target >> t) & 1))
967 if (!uc->data || uc->data >= 255) {
968 tp->tgoal.iu = tp->tgoal.dt =
970 tp->tgoal.offset = 0;
971 } else if (uc->data <= 9 && np->minsync_dt) {
972 if (uc->data < np->minsync_dt)
973 uc->data = np->minsync_dt;
974 tp->tgoal.iu = tp->tgoal.dt =
977 tp->tgoal.period = uc->data;
978 tp->tgoal.offset = np->maxoffs_dt;
980 if (uc->data < np->minsync)
981 uc->data = np->minsync;
982 tp->tgoal.iu = tp->tgoal.dt =
984 tp->tgoal.period = uc->data;
985 tp->tgoal.offset = np->maxoffs;
987 tp->tgoal.check_nego = 1;
990 tp->tgoal.width = uc->data ? 1 : 0;
991 tp->tgoal.check_nego = 1;
994 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
995 sym_tune_dev_queuing(tp, l, uc->data);
1000 OUTB(np, nc_istat, SIGP|SEM);
1003 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
1004 struct sym_lcb *lp = sym_lp(tp, l);
1005 if (lp) lp->to_clear = 1;
1007 np->istat_sem = SEM;
1008 OUTB(np, nc_istat, SIGP|SEM);
1011 tp->usrflags = uc->data;
1019 static int skip_spaces(char *ptr, int len)
1023 for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
1028 static int get_int_arg(char *ptr, int len, u_long *pv)
1032 *pv = simple_strtoul(ptr, &end, 10);
1036 static int is_keyword(char *ptr, int len, char *verb)
1038 int verb_len = strlen(verb);
1040 if (len >= verb_len && !memcmp(verb, ptr, verb_len))
1046 #define SKIP_SPACES(ptr, len) \
1047 if ((arg_len = skip_spaces(ptr, len)) < 1) \
1049 ptr += arg_len; len -= arg_len;
1051 #define GET_INT_ARG(ptr, len, v) \
1052 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
1054 ptr += arg_len; len -= arg_len;
1058 * Parse a control command
1061 static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
1065 struct sym_usrcmd cmd, *uc = &cmd;
1069 memset(uc, 0, sizeof(*uc));
1071 if (len > 0 && ptr[len-1] == '\n')
1074 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1075 uc->cmd = UC_SETSYNC;
1076 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1077 uc->cmd = UC_SETTAGS;
1078 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1079 uc->cmd = UC_SETVERBOSE;
1080 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1081 uc->cmd = UC_SETWIDE;
1082 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1083 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1084 uc->cmd = UC_SETDEBUG;
1086 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1087 uc->cmd = UC_SETFLAG;
1088 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1089 uc->cmd = UC_RESETDEV;
1090 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1091 uc->cmd = UC_CLEARDEV;
1095 #ifdef DEBUG_PROC_INFO
1096 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1101 ptr += arg_len; len -= arg_len;
1110 SKIP_SPACES(ptr, len);
1111 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1112 ptr += arg_len; len -= arg_len;
1115 GET_INT_ARG(ptr, len, target);
1116 uc->target = (1<<target);
1117 #ifdef DEBUG_PROC_INFO
1118 printk("sym_user_command: target=%ld\n", target);
1129 SKIP_SPACES(ptr, len);
1130 GET_INT_ARG(ptr, len, uc->data);
1131 #ifdef DEBUG_PROC_INFO
1132 printk("sym_user_command: data=%ld\n", uc->data);
1135 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1138 SKIP_SPACES(ptr, len);
1139 if ((arg_len = is_keyword(ptr, len, "alloc")))
1140 uc->data |= DEBUG_ALLOC;
1141 else if ((arg_len = is_keyword(ptr, len, "phase")))
1142 uc->data |= DEBUG_PHASE;
1143 else if ((arg_len = is_keyword(ptr, len, "queue")))
1144 uc->data |= DEBUG_QUEUE;
1145 else if ((arg_len = is_keyword(ptr, len, "result")))
1146 uc->data |= DEBUG_RESULT;
1147 else if ((arg_len = is_keyword(ptr, len, "scatter")))
1148 uc->data |= DEBUG_SCATTER;
1149 else if ((arg_len = is_keyword(ptr, len, "script")))
1150 uc->data |= DEBUG_SCRIPT;
1151 else if ((arg_len = is_keyword(ptr, len, "tiny")))
1152 uc->data |= DEBUG_TINY;
1153 else if ((arg_len = is_keyword(ptr, len, "timing")))
1154 uc->data |= DEBUG_TIMING;
1155 else if ((arg_len = is_keyword(ptr, len, "nego")))
1156 uc->data |= DEBUG_NEGO;
1157 else if ((arg_len = is_keyword(ptr, len, "tags")))
1158 uc->data |= DEBUG_TAGS;
1159 else if ((arg_len = is_keyword(ptr, len, "pointer")))
1160 uc->data |= DEBUG_POINTER;
1163 ptr += arg_len; len -= arg_len;
1165 #ifdef DEBUG_PROC_INFO
1166 printk("sym_user_command: data=%ld\n", uc->data);
1169 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1172 SKIP_SPACES(ptr, len);
1173 if ((arg_len = is_keyword(ptr, len, "no_disc")))
1174 uc->data &= ~SYM_DISC_ENABLED;
1177 ptr += arg_len; len -= arg_len;
1187 unsigned long flags;
1189 spin_lock_irqsave(np->s.host->host_lock, flags);
1190 sym_exec_user_command (np, uc);
1191 spin_unlock_irqrestore(np->s.host->host_lock, flags);
1196 #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1199 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1201 * Informations through the proc file system.
1210 static void copy_mem_info(struct info_str *info, char *data, int len)
1212 if (info->pos + len > info->length)
1213 len = info->length - info->pos;
1215 if (info->pos + len < info->offset) {
1219 if (info->pos < info->offset) {
1220 data += (info->offset - info->pos);
1221 len -= (info->offset - info->pos);
1225 memcpy(info->buffer + info->pos, data, len);
1230 static int copy_info(struct info_str *info, char *fmt, ...)
1236 va_start(args, fmt);
1237 len = vsprintf(buf, fmt, args);
1240 copy_mem_info(info, buf, len);
1245 * Copy formatted information into the input buffer.
1247 static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
1249 struct info_str info;
1253 info.offset = offset;
1256 copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1257 "revision id 0x%x\n", np->s.chip_name,
1258 np->s.device->device, np->s.device->revision);
1259 copy_info(&info, "At PCI address %s, IRQ " IRQ_FMT "\n",
1260 pci_name(np->s.device), IRQ_PRM(np->s.device->irq));
1261 copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1262 (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1263 np->maxwide ? "Wide" : "Narrow",
1264 np->minsync_dt ? ", DT capable" : "");
1266 copy_info(&info, "Max. started commands %d, "
1267 "max. commands per LUN %d\n",
1268 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1270 return info.pos > info.offset? info.pos - info.offset : 0;
1272 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1275 * Entry point of the scsi proc fs of the driver.
1276 * - func = 0 means read (returns adapter infos)
1277 * - func = 1 means write (not yet merget from sym53c8xx)
1279 static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
1280 char **start, off_t offset, int length, int func)
1282 struct sym_hcb *np = sym_get_hcb(host);
1286 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1287 retv = sym_user_command(np, buffer, length);
1294 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1295 retv = sym_host_info(np, buffer, offset, length);
1303 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1306 * Free controller resources.
1308 static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
1311 * Free O/S specific resources.
1314 free_irq(pdev->irq, np);
1316 pci_iounmap(pdev, np->s.ioaddr);
1318 pci_iounmap(pdev, np->s.ramaddr);
1320 * Free O/S independent resources.
1324 sym_mfree_dma(np, sizeof(*np), "HCB");
1328 * Ask/tell the system about DMA addressing.
1330 static int sym_setup_bus_dma_mask(struct sym_hcb *np)
1332 #if SYM_CONF_DMA_ADDRESSING_MODE > 0
1333 #if SYM_CONF_DMA_ADDRESSING_MODE == 1
1334 #define DMA_DAC_MASK DMA_40BIT_MASK
1335 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
1336 #define DMA_DAC_MASK DMA_64BIT_MASK
1338 if ((np->features & FE_DAC) &&
1339 !pci_set_dma_mask(np->s.device, DMA_DAC_MASK)) {
1345 if (!pci_set_dma_mask(np->s.device, DMA_32BIT_MASK))
1348 printf_warning("%s: No suitable DMA available\n", sym_name(np));
1353 * Host attach and initialisations.
1355 * Allocate host data and ncb structure.
1356 * Remap MMIO region.
1357 * Do chip initialization.
1358 * If all is OK, install interrupt handling and
1359 * start the timer daemon.
1361 static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
1362 int unit, struct sym_device *dev)
1364 struct host_data *host_data;
1365 struct sym_hcb *np = NULL;
1366 struct Scsi_Host *instance = NULL;
1367 struct pci_dev *pdev = dev->pdev;
1368 unsigned long flags;
1371 printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT "\n",
1372 unit, dev->chip.name, pdev->revision, pci_name(pdev),
1373 IRQ_PRM(pdev->irq));
1376 * Get the firmware for this chip.
1378 fw = sym_find_firmware(&dev->chip);
1383 * Allocate host_data structure
1385 instance = scsi_host_alloc(tpnt, sizeof(*host_data));
1388 host_data = (struct host_data *) instance->hostdata;
1391 * Allocate immediately the host control block,
1392 * since we are only expecting to succeed. :)
1393 * We keep track in the HCB of all the resources that
1394 * are to be released on error.
1396 np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
1399 np->s.device = pdev;
1400 np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
1401 host_data->ncb = np;
1402 np->s.host = instance;
1404 pci_set_drvdata(pdev, np);
1407 * Copy some useful infos to the HCB.
1409 np->hcb_ba = vtobus(np);
1410 np->verbose = sym_driver_setup.verbose;
1411 np->s.device = pdev;
1413 np->features = dev->chip.features;
1414 np->clock_divn = dev->chip.nr_divisor;
1415 np->maxoffs = dev->chip.offset_max;
1416 np->maxburst = dev->chip.burst_max;
1417 np->myaddr = dev->host_id;
1422 strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1423 sprintf(np->s.inst_name, "sym%d", np->s.unit);
1425 if (sym_setup_bus_dma_mask(np))
1429 * Try to map the controller chip to
1430 * virtual and physical memory.
1432 np->mmio_ba = (u32)dev->mmio_base;
1433 np->s.ioaddr = dev->s.ioaddr;
1434 np->s.ramaddr = dev->s.ramaddr;
1435 np->s.io_ws = (np->features & FE_IO256) ? 256 : 128;
1438 * Map on-chip RAM if present and supported.
1440 if (!(np->features & FE_RAM))
1442 if (dev->ram_base) {
1443 np->ram_ba = (u32)dev->ram_base;
1444 np->ram_ws = (np->features & FE_RAM8K) ? 8192 : 4096;
1447 if (sym_hcb_attach(instance, fw, dev->nvram))
1451 * Install the interrupt handler.
1452 * If we synchonize the C code with SCRIPTS on interrupt,
1453 * we do not want to share the INTR line at all.
1455 if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX, np)) {
1456 printf_err("%s: request irq %d failure\n",
1457 sym_name(np), pdev->irq);
1462 * After SCSI devices have been opened, we cannot
1463 * reset the bus safely, so we do it here.
1465 spin_lock_irqsave(instance->host_lock, flags);
1466 if (sym_reset_scsi_bus(np, 0))
1470 * Start the SCRIPTS.
1472 sym_start_up(np, 1);
1475 * Start the timer daemon
1477 init_timer(&np->s.timer);
1478 np->s.timer.data = (unsigned long) np;
1479 np->s.timer.function = sym53c8xx_timer;
1484 * Fill Linux host instance structure
1485 * and return success.
1487 instance->max_channel = 0;
1488 instance->this_id = np->myaddr;
1489 instance->max_id = np->maxwide ? 16 : 8;
1490 instance->max_lun = SYM_CONF_MAX_LUN;
1491 instance->unique_id = pci_resource_start(pdev, 0);
1492 instance->cmd_per_lun = SYM_CONF_MAX_TAG;
1493 instance->can_queue = (SYM_CONF_MAX_START-2);
1494 instance->sg_tablesize = SYM_CONF_MAX_SG;
1495 instance->max_cmd_len = 16;
1496 BUG_ON(sym2_transport_template == NULL);
1497 instance->transportt = sym2_transport_template;
1499 /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
1500 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
1501 instance->dma_boundary = 0xFFFFFF;
1503 spin_unlock_irqrestore(instance->host_lock, flags);
1508 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1509 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1510 spin_unlock_irqrestore(instance->host_lock, flags);
1514 printf_info("%s: giving up ...\n", sym_name(np));
1516 sym_free_resources(np, pdev);
1517 scsi_host_put(instance);
1524 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1526 #if SYM_CONF_NVRAM_SUPPORT
1527 static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1532 sym_read_nvram(devp, nvp);
1535 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1538 #endif /* SYM_CONF_NVRAM_SUPPORT */
1540 static int __devinit sym_check_supported(struct sym_device *device)
1542 struct sym_chip *chip;
1543 struct pci_dev *pdev = device->pdev;
1544 unsigned long io_port = pci_resource_start(pdev, 0);
1548 * If user excluded this chip, do not initialize it.
1549 * I hate this code so much. Must kill it.
1552 for (i = 0 ; i < 8 ; i++) {
1553 if (sym_driver_setup.excludes[i] == io_port)
1559 * Check if the chip is supported. Then copy the chip description
1560 * to our device structure so we can make it match the actual device
1563 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
1565 dev_info(&pdev->dev, "device not supported\n");
1568 memcpy(&device->chip, chip, sizeof(device->chip));
1574 * Ignore Symbios chips controlled by various RAID controllers.
1575 * These controllers set value 0x52414944 at RAM end - 16.
1577 static int __devinit sym_check_raid(struct sym_device *device)
1579 unsigned int ram_size, ram_val;
1581 if (!device->s.ramaddr)
1584 if (device->chip.features & FE_RAM8K)
1589 ram_val = readl(device->s.ramaddr + ram_size - 16);
1590 if (ram_val != 0x52414944)
1593 dev_info(&device->pdev->dev,
1594 "not initializing, driven by RAID controller.\n");
1598 static int __devinit sym_set_workarounds(struct sym_device *device)
1600 struct sym_chip *chip = &device->chip;
1601 struct pci_dev *pdev = device->pdev;
1605 * (ITEM 12 of a DEL about the 896 I haven't yet).
1606 * We must ensure the chip will use WRITE AND INVALIDATE.
1607 * The revision number limit is for now arbitrary.
1609 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 0x4) {
1610 chip->features |= (FE_WRIE | FE_CLSE);
1613 /* If the chip can do Memory Write Invalidate, enable it */
1614 if (chip->features & FE_WRIE) {
1615 if (pci_set_mwi(pdev))
1620 * Work around for errant bit in 895A. The 66Mhz
1621 * capable bit is set erroneously. Clear this bit.
1624 * Make sure Config space and Features agree.
1626 * Recall: writes are not normal to status register -
1627 * write a 1 to clear and a 0 to leave unchanged.
1628 * Can only reset bits.
1630 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1631 if (chip->features & FE_66MHZ) {
1632 if (!(status_reg & PCI_STATUS_66MHZ))
1633 chip->features &= ~FE_66MHZ;
1635 if (status_reg & PCI_STATUS_66MHZ) {
1636 status_reg = PCI_STATUS_66MHZ;
1637 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1638 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1646 * Read and check the PCI configuration for any detected NCR
1647 * boards and save data for attaching after all boards have
1650 static void __devinit
1651 sym_init_device(struct pci_dev *pdev, struct sym_device *device)
1654 struct pci_bus_region bus_addr;
1656 device->host_id = SYM_SETUP_HOST_ID;
1657 device->pdev = pdev;
1659 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
1660 device->mmio_base = bus_addr.start;
1663 * If the BAR is 64-bit, resource 2 will be occupied by the
1666 if (!pdev->resource[i].flags)
1668 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
1669 device->ram_base = bus_addr.start;
1671 #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
1672 if (device->mmio_base)
1673 device->s.ioaddr = pci_iomap(pdev, 1,
1674 pci_resource_len(pdev, 1));
1676 if (!device->s.ioaddr)
1677 device->s.ioaddr = pci_iomap(pdev, 0,
1678 pci_resource_len(pdev, 0));
1679 if (device->ram_base)
1680 device->s.ramaddr = pci_iomap(pdev, i,
1681 pci_resource_len(pdev, i));
1685 * The NCR PQS and PDS cards are constructed as a DEC bridge
1686 * behind which sits a proprietary NCR memory controller and
1687 * either four or two 53c875s as separate devices. We can tell
1688 * if an 875 is part of a PQS/PDS or not since if it is, it will
1689 * be on the same bus as the memory controller. In its usual
1690 * mode of operation, the 875s are slaved to the memory
1691 * controller for all transfers. To operate with the Linux
1692 * driver, the memory controller is disabled and the 875s
1693 * freed to function independently. The only wrinkle is that
1694 * the preset SCSI ID (which may be zero) must be read in from
1695 * a special configuration space register of the 875.
1697 static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
1702 for (slot = 0; slot < 256; slot++) {
1703 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
1705 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
1710 /* bit 1: allow individual 875 configuration */
1711 pci_read_config_byte(memc, 0x44, &tmp);
1712 if ((tmp & 0x2) == 0) {
1714 pci_write_config_byte(memc, 0x44, tmp);
1717 /* bit 2: drive individual 875 interrupts to the bus */
1718 pci_read_config_byte(memc, 0x45, &tmp);
1719 if ((tmp & 0x4) == 0) {
1721 pci_write_config_byte(memc, 0x45, tmp);
1728 pci_read_config_byte(pdev, 0x84, &tmp);
1729 sym_dev->host_id = tmp;
1733 * Called before unloading the module.
1735 * We have to free resources and halt the NCR chip.
1737 static int sym_detach(struct sym_hcb *np, struct pci_dev *pdev)
1739 printk("%s: detaching ...\n", sym_name(np));
1741 del_timer_sync(&np->s.timer);
1745 * We should use sym_soft_reset(), but we don't want to do
1746 * so, since we may not be safe if interrupts occur.
1748 printk("%s: resetting chip\n", sym_name(np));
1749 OUTB(np, nc_istat, SRST);
1752 OUTB(np, nc_istat, 0);
1754 sym_free_resources(np, pdev);
1760 * Driver host template.
1762 static struct scsi_host_template sym2_template = {
1763 .module = THIS_MODULE,
1764 .name = "sym53c8xx",
1765 .info = sym53c8xx_info,
1766 .queuecommand = sym53c8xx_queue_command,
1767 .slave_alloc = sym53c8xx_slave_alloc,
1768 .slave_configure = sym53c8xx_slave_configure,
1769 .slave_destroy = sym53c8xx_slave_destroy,
1770 .eh_abort_handler = sym53c8xx_eh_abort_handler,
1771 .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
1772 .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
1773 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
1775 .use_clustering = ENABLE_CLUSTERING,
1776 .use_sg_chaining = ENABLE_SG_CHAINING,
1777 .max_sectors = 0xFFFF,
1778 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1779 .proc_info = sym53c8xx_proc_info,
1780 .proc_name = NAME53C8XX,
1784 static int attach_count;
1786 static int __devinit sym2_probe(struct pci_dev *pdev,
1787 const struct pci_device_id *ent)
1789 struct sym_device sym_dev;
1790 struct sym_nvram nvram;
1791 struct Scsi_Host *instance;
1793 memset(&sym_dev, 0, sizeof(sym_dev));
1794 memset(&nvram, 0, sizeof(nvram));
1796 if (pci_enable_device(pdev))
1799 pci_set_master(pdev);
1801 if (pci_request_regions(pdev, NAME53C8XX))
1804 sym_init_device(pdev, &sym_dev);
1805 if (sym_check_supported(&sym_dev))
1808 if (sym_check_raid(&sym_dev))
1809 goto leave; /* Don't disable the device */
1811 if (sym_set_workarounds(&sym_dev))
1814 sym_config_pqs(pdev, &sym_dev);
1816 sym_get_nvram(&sym_dev, &nvram);
1818 instance = sym_attach(&sym2_template, attach_count, &sym_dev);
1822 if (scsi_add_host(instance, &pdev->dev))
1824 scsi_scan_host(instance);
1831 sym_detach(pci_get_drvdata(pdev), pdev);
1833 pci_release_regions(pdev);
1835 pci_disable_device(pdev);
1840 static void __devexit sym2_remove(struct pci_dev *pdev)
1842 struct sym_hcb *np = pci_get_drvdata(pdev);
1843 struct Scsi_Host *host = np->s.host;
1845 scsi_remove_host(host);
1846 scsi_host_put(host);
1848 sym_detach(np, pdev);
1850 pci_release_regions(pdev);
1851 pci_disable_device(pdev);
1857 * sym2_io_error_detected() - called when PCI error is detected
1858 * @pdev: pointer to PCI device
1859 * @state: current state of the PCI slot
1861 static pci_ers_result_t sym2_io_error_detected(struct pci_dev *pdev,
1862 enum pci_channel_state state)
1864 /* If slot is permanently frozen, turn everything off */
1865 if (state == pci_channel_io_perm_failure) {
1867 return PCI_ERS_RESULT_DISCONNECT;
1870 disable_irq(pdev->irq);
1871 pci_disable_device(pdev);
1873 /* Request that MMIO be enabled, so register dump can be taken. */
1874 return PCI_ERS_RESULT_CAN_RECOVER;
1878 * sym2_io_slot_dump - Enable MMIO and dump debug registers
1879 * @pdev: pointer to PCI device
1881 static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
1883 struct sym_hcb *np = pci_get_drvdata(pdev);
1885 sym_dump_registers(np);
1887 /* Request a slot reset. */
1888 return PCI_ERS_RESULT_NEED_RESET;
1892 * sym2_reset_workarounds - hardware-specific work-arounds
1894 * This routine is similar to sym_set_workarounds(), except
1895 * that, at this point, we already know that the device was
1896 * succesfully intialized at least once before, and so most
1897 * of the steps taken there are un-needed here.
1899 static void sym2_reset_workarounds(struct pci_dev *pdev)
1902 struct sym_chip *chip;
1904 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
1906 /* Work around for errant bit in 895A, in a fashion
1907 * similar to what is done in sym_set_workarounds().
1909 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1910 if (!(chip->features & FE_66MHZ) && (status_reg & PCI_STATUS_66MHZ)) {
1911 status_reg = PCI_STATUS_66MHZ;
1912 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1913 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1918 * sym2_io_slot_reset() - called when the pci bus has been reset.
1919 * @pdev: pointer to PCI device
1921 * Restart the card from scratch.
1923 static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
1925 struct sym_hcb *np = pci_get_drvdata(pdev);
1927 printk(KERN_INFO "%s: recovering from a PCI slot reset\n",
1930 if (pci_enable_device(pdev)) {
1931 printk(KERN_ERR "%s: Unable to enable after PCI reset\n",
1933 return PCI_ERS_RESULT_DISCONNECT;
1936 pci_set_master(pdev);
1937 enable_irq(pdev->irq);
1939 /* If the chip can do Memory Write Invalidate, enable it */
1940 if (np->features & FE_WRIE) {
1941 if (pci_set_mwi(pdev))
1942 return PCI_ERS_RESULT_DISCONNECT;
1945 /* Perform work-arounds, analogous to sym_set_workarounds() */
1946 sym2_reset_workarounds(pdev);
1948 /* Perform host reset only on one instance of the card */
1949 if (PCI_FUNC(pdev->devfn) == 0) {
1950 if (sym_reset_scsi_bus(np, 0)) {
1951 printk(KERN_ERR "%s: Unable to reset scsi host\n",
1953 return PCI_ERS_RESULT_DISCONNECT;
1955 sym_start_up(np, 1);
1958 return PCI_ERS_RESULT_RECOVERED;
1962 * sym2_io_resume() - resume normal ops after PCI reset
1963 * @pdev: pointer to PCI device
1965 * Called when the error recovery driver tells us that its
1966 * OK to resume normal operation. Use completion to allow
1967 * halted scsi ops to resume.
1969 static void sym2_io_resume(struct pci_dev *pdev)
1971 struct sym_hcb *np = pci_get_drvdata(pdev);
1972 struct Scsi_Host *shost = np->s.host;
1973 struct host_data *hostdata = shost_priv(shost);
1975 spin_lock_irq(shost->host_lock);
1976 if (hostdata->io_reset)
1977 complete_all(hostdata->io_reset);
1978 hostdata->io_reset = NULL;
1979 spin_unlock_irq(shost->host_lock);
1982 static void sym2_get_signalling(struct Scsi_Host *shost)
1984 struct sym_hcb *np = sym_get_hcb(shost);
1985 enum spi_signal_type type;
1987 switch (np->scsi_mode) {
1989 type = SPI_SIGNAL_SE;
1992 type = SPI_SIGNAL_LVD;
1995 type = SPI_SIGNAL_HVD;
1998 type = SPI_SIGNAL_UNKNOWN;
2001 spi_signalling(shost) = type;
2004 static void sym2_set_offset(struct scsi_target *starget, int offset)
2006 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2007 struct sym_hcb *np = sym_get_hcb(shost);
2008 struct sym_tcb *tp = &np->target[starget->id];
2010 tp->tgoal.offset = offset;
2011 tp->tgoal.check_nego = 1;
2014 static void sym2_set_period(struct scsi_target *starget, int period)
2016 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2017 struct sym_hcb *np = sym_get_hcb(shost);
2018 struct sym_tcb *tp = &np->target[starget->id];
2020 /* have to have DT for these transfers, but DT will also
2021 * set width, so check that this is allowed */
2022 if (period <= np->minsync && spi_width(starget))
2025 tp->tgoal.period = period;
2026 tp->tgoal.check_nego = 1;
2029 static void sym2_set_width(struct scsi_target *starget, int width)
2031 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2032 struct sym_hcb *np = sym_get_hcb(shost);
2033 struct sym_tcb *tp = &np->target[starget->id];
2035 /* It is illegal to have DT set on narrow transfers. If DT is
2036 * clear, we must also clear IU and QAS. */
2038 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
2040 tp->tgoal.width = width;
2041 tp->tgoal.check_nego = 1;
2044 static void sym2_set_dt(struct scsi_target *starget, int dt)
2046 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2047 struct sym_hcb *np = sym_get_hcb(shost);
2048 struct sym_tcb *tp = &np->target[starget->id];
2050 /* We must clear QAS and IU if DT is clear */
2054 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
2055 tp->tgoal.check_nego = 1;
2059 static void sym2_set_iu(struct scsi_target *starget, int iu)
2061 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2062 struct sym_hcb *np = sym_get_hcb(shost);
2063 struct sym_tcb *tp = &np->target[starget->id];
2066 tp->tgoal.iu = tp->tgoal.dt = 1;
2069 tp->tgoal.check_nego = 1;
2072 static void sym2_set_qas(struct scsi_target *starget, int qas)
2074 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2075 struct sym_hcb *np = sym_get_hcb(shost);
2076 struct sym_tcb *tp = &np->target[starget->id];
2079 tp->tgoal.dt = tp->tgoal.qas = 1;
2082 tp->tgoal.check_nego = 1;
2086 static struct spi_function_template sym2_transport_functions = {
2087 .set_offset = sym2_set_offset,
2089 .set_period = sym2_set_period,
2091 .set_width = sym2_set_width,
2093 .set_dt = sym2_set_dt,
2096 .set_iu = sym2_set_iu,
2098 .set_qas = sym2_set_qas,
2101 .get_signalling = sym2_get_signalling,
2104 static struct pci_device_id sym2_id_table[] __devinitdata = {
2105 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
2106 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2107 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
2108 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2109 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
2110 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2111 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
2112 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2113 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
2114 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2115 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
2116 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2117 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
2118 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
2119 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
2120 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2121 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
2122 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2123 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2124 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2125 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2126 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2127 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
2128 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
2129 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2130 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2131 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2132 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2133 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2134 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2135 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2136 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2137 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2138 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2142 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2144 static struct pci_error_handlers sym2_err_handler = {
2145 .error_detected = sym2_io_error_detected,
2146 .mmio_enabled = sym2_io_slot_dump,
2147 .slot_reset = sym2_io_slot_reset,
2148 .resume = sym2_io_resume,
2151 static struct pci_driver sym2_driver = {
2153 .id_table = sym2_id_table,
2154 .probe = sym2_probe,
2155 .remove = __devexit_p(sym2_remove),
2156 .err_handler = &sym2_err_handler,
2159 static int __init sym2_init(void)
2163 sym2_setup_params();
2164 sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2165 if (!sym2_transport_template)
2168 error = pci_register_driver(&sym2_driver);
2170 spi_release_transport(sym2_transport_template);
2174 static void __exit sym2_exit(void)
2176 pci_unregister_driver(&sym2_driver);
2177 spi_release_transport(sym2_transport_template);
2180 module_init(sym2_init);
2181 module_exit(sym2_exit);