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 dma_addr_t data_mapping;
138 unsigned char data_mapped;
139 unsigned char to_do; /* For error handling */
140 void (*old_done)(struct scsi_cmnd *); /* For error handling */
141 struct completion *eh_done; /* For error handling */
144 #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
145 #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
147 static void __unmap_scsi_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
149 if (SYM_UCMD_PTR(cmd)->data_mapped)
152 SYM_UCMD_PTR(cmd)->data_mapped = 0;
155 static int __map_scsi_sg_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
159 use_sg = scsi_dma_map(cmd);
161 SYM_UCMD_PTR(cmd)->data_mapped = 2;
162 SYM_UCMD_PTR(cmd)->data_mapping = use_sg;
168 #define unmap_scsi_data(np, cmd) \
169 __unmap_scsi_data(np->s.device, cmd)
170 #define map_scsi_sg_data(np, cmd) \
171 __map_scsi_sg_data(np->s.device, cmd)
173 * Complete a pending CAM CCB.
175 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
177 unmap_scsi_data(np, cmd);
181 static void sym_xpt_done2(struct sym_hcb *np, struct scsi_cmnd *cmd, int cam_status)
183 sym_set_cam_status(cmd, cam_status);
184 sym_xpt_done(np, cmd);
189 * Tell the SCSI layer about a BUS RESET.
191 void sym_xpt_async_bus_reset(struct sym_hcb *np)
193 printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
194 np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
195 np->s.settle_time_valid = 1;
196 if (sym_verbose >= 2)
197 printf_info("%s: command processing suspended for %d seconds\n",
198 sym_name(np), sym_driver_setup.settle_delay);
202 * Tell the SCSI layer about a BUS DEVICE RESET message sent.
204 void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
206 printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
210 * Choose the more appropriate CAM status if
211 * the IO encountered an extended error.
213 static int sym_xerr_cam_status(int cam_status, int x_status)
216 if (x_status & XE_PARITY_ERR)
217 cam_status = DID_PARITY;
218 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
219 cam_status = DID_ERROR;
220 else if (x_status & XE_BAD_PHASE)
221 cam_status = DID_ERROR;
223 cam_status = DID_ERROR;
229 * Build CAM result for a failed or auto-sensed IO.
231 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
233 struct scsi_cmnd *cmd = cp->cmd;
234 u_int cam_status, scsi_status, drv_status;
238 scsi_status = cp->ssss_status;
240 if (cp->host_flags & HF_SENSE) {
241 scsi_status = cp->sv_scsi_status;
242 resid = cp->sv_resid;
243 if (sym_verbose && cp->sv_xerr_status)
244 sym_print_xerr(cmd, cp->sv_xerr_status);
245 if (cp->host_status == HS_COMPLETE &&
246 cp->ssss_status == S_GOOD &&
247 cp->xerr_status == 0) {
248 cam_status = sym_xerr_cam_status(DID_OK,
250 drv_status = DRIVER_SENSE;
252 * Bounce back the sense data to user.
254 memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
255 memcpy(cmd->sense_buffer, cp->sns_bbuf,
256 min(sizeof(cmd->sense_buffer),
257 (size_t)SYM_SNS_BBUF_LEN));
260 * If the device reports a UNIT ATTENTION condition
261 * due to a RESET condition, we should consider all
262 * disconnect CCBs for this unit as aborted.
266 p = (u_char *) cmd->sense_data;
267 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
268 sym_clear_tasks(np, DID_ABORT,
269 cp->target,cp->lun, -1);
274 * Error return from our internal request sense. This
275 * is bad: we must clear the contingent allegiance
276 * condition otherwise the device will always return
277 * BUSY. Use a big stick.
279 sym_reset_scsi_target(np, cmd->device->id);
280 cam_status = DID_ERROR;
282 } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
284 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
285 cam_status = DID_NO_CONNECT;
286 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
287 cam_status = DID_ERROR;
288 else { /* Extended error */
290 sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
291 cp->host_status, cp->ssss_status,
295 * Set the most appropriate value for CAM status.
297 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
299 scsi_set_resid(cmd, resid);
300 cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
303 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
310 use_sg = map_scsi_sg_data(np, cmd);
312 struct scatterlist *sg;
313 struct sym_tcb *tp = &np->target[cp->target];
314 struct sym_tblmove *data;
316 if (use_sg > SYM_CONF_MAX_SG) {
317 unmap_scsi_data(np, cmd);
321 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
323 scsi_for_each_sg(cmd, sg, use_sg, segment) {
324 dma_addr_t baddr = sg_dma_address(sg);
325 unsigned int len = sg_dma_len(sg);
327 if ((len & 1) && (tp->head.wval & EWS)) {
329 cp->odd_byte_adjustment++;
332 sym_build_sge(np, &data[segment], baddr, len);
343 * Queue a SCSI command.
345 static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
347 struct scsi_device *sdev = cmd->device;
354 * Minimal checkings, so that we will not
355 * go outside our tables.
357 if (sdev->id == np->myaddr) {
358 sym_xpt_done2(np, cmd, DID_NO_CONNECT);
363 * Retrieve the target descriptor.
365 tp = &np->target[sdev->id];
368 * Select tagged/untagged.
370 lp = sym_lp(tp, sdev->lun);
371 order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
376 cp = sym_get_ccb(np, cmd, order);
378 return 1; /* Means resource shortage */
379 sym_queue_scsiio(np, cmd, cp);
384 * Setup buffers and pointers that address the CDB.
386 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
388 memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
390 cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
391 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
397 * Setup pointers that address the data and start the I/O.
399 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
407 if (sym_setup_cdb(np, cmd, cp))
411 * No direction means no data.
413 dir = cmd->sc_data_direction;
414 if (dir != DMA_NONE) {
415 cp->segments = sym_scatter(np, cp, cmd);
416 if (cp->segments < 0) {
417 sym_set_cam_status(cmd, DID_ERROR);
422 * No segments means no data.
432 * Set the data pointer.
435 case DMA_BIDIRECTIONAL:
436 printk("%s: got DMA_BIDIRECTIONAL command", sym_name(np));
437 sym_set_cam_status(cmd, DID_ERROR);
440 goalp = SCRIPTA_BA(np, data_out2) + 8;
441 lastp = goalp - 8 - (cp->segments * (2*4));
443 case DMA_FROM_DEVICE:
444 cp->host_flags |= HF_DATA_IN;
445 goalp = SCRIPTA_BA(np, data_in2) + 8;
446 lastp = goalp - 8 - (cp->segments * (2*4));
450 lastp = goalp = SCRIPTB_BA(np, no_data);
455 * Set all pointers values needed by SCRIPTS.
457 cp->phys.head.lastp = cpu_to_scr(lastp);
458 cp->phys.head.savep = cpu_to_scr(lastp);
459 cp->startp = cp->phys.head.savep;
460 cp->goalp = cpu_to_scr(goalp);
463 * When `#ifed 1', the code below makes the driver
464 * panic on the first attempt to write to a SCSI device.
465 * It is the first test we want to do after a driver
466 * change that does not seem obviously safe. :)
469 switch (cp->cdb_buf[0]) {
470 case 0x0A: case 0x2A: case 0xAA:
471 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
481 sym_put_start_queue(np, cp);
485 sym_free_ccb(np, cp);
486 sym_xpt_done(np, cmd);
494 * Misused to keep the driver running when
495 * interrupts are not configured correctly.
497 static void sym_timer(struct sym_hcb *np)
499 unsigned long thistime = jiffies;
504 np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
505 add_timer(&np->s.timer);
508 * If we are resetting the ncr, wait for settle_time before
509 * clearing it. Then command processing will be resumed.
511 if (np->s.settle_time_valid) {
512 if (time_before_eq(np->s.settle_time, thistime)) {
513 if (sym_verbose >= 2 )
514 printk("%s: command processing resumed\n",
516 np->s.settle_time_valid = 0;
522 * Nothing to do for now, but that may come.
524 if (np->s.lasttime + 4*HZ < thistime) {
525 np->s.lasttime = thistime;
528 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
530 * Some way-broken PCI bridges may lead to
531 * completions being lost when the clearing
532 * of the INTFLY flag by the CPU occurs
533 * concurrently with the chip raising this flag.
534 * If this ever happen, lost completions will
543 * PCI BUS error handler.
545 void sym_log_bus_error(struct sym_hcb *np)
548 pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
549 if (pci_sts & 0xf900) {
550 pci_write_config_word(np->s.device, PCI_STATUS, pci_sts);
551 printf("%s: PCI STATUS = 0x%04x\n",
552 sym_name(np), pci_sts & 0xf900);
557 * queuecommand method. Entered with the host adapter lock held and
558 * interrupts disabled.
560 static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
561 void (*done)(struct scsi_cmnd *))
563 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
564 struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
567 cmd->scsi_done = done;
568 memset(ucp, 0, sizeof(*ucp));
571 * Shorten our settle_time if needed for
572 * this command not to time out.
574 if (np->s.settle_time_valid && cmd->timeout_per_command) {
575 unsigned long tlimit = jiffies + cmd->timeout_per_command;
576 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
577 if (time_after(np->s.settle_time, tlimit)) {
578 np->s.settle_time = tlimit;
582 if (np->s.settle_time_valid)
583 return SCSI_MLQUEUE_HOST_BUSY;
585 sts = sym_queue_command(np, cmd);
587 return SCSI_MLQUEUE_HOST_BUSY;
592 * Linux entry point of the interrupt handler.
594 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
597 struct sym_hcb *np = (struct sym_hcb *)dev_id;
599 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
601 spin_lock_irqsave(np->s.host->host_lock, flags);
603 spin_unlock_irqrestore(np->s.host->host_lock, flags);
605 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
611 * Linux entry point of the timer handler
613 static void sym53c8xx_timer(unsigned long npref)
615 struct sym_hcb *np = (struct sym_hcb *)npref;
618 spin_lock_irqsave(np->s.host->host_lock, flags);
620 spin_unlock_irqrestore(np->s.host->host_lock, flags);
625 * What the eh thread wants us to perform.
627 #define SYM_EH_ABORT 0
628 #define SYM_EH_DEVICE_RESET 1
629 #define SYM_EH_BUS_RESET 2
630 #define SYM_EH_HOST_RESET 3
633 * What we will do regarding the involved SCSI command.
635 #define SYM_EH_DO_IGNORE 0
636 #define SYM_EH_DO_WAIT 2
639 * scsi_done() alias when error recovery is in progress.
641 static void sym_eh_done(struct scsi_cmnd *cmd)
643 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
644 BUILD_BUG_ON(sizeof(struct scsi_pointer) < sizeof(struct sym_ucmd));
646 cmd->scsi_done = ucmd->old_done;
648 if (ucmd->to_do == SYM_EH_DO_WAIT)
649 complete(ucmd->eh_done);
653 * Generic method for our eh processing.
654 * The 'op' argument tells what we have to do.
656 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
658 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
659 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
660 struct Scsi_Host *host = cmd->device->host;
662 int to_do = SYM_EH_DO_IGNORE;
664 struct completion eh_done;
666 dev_warn(&cmd->device->sdev_gendev, "%s operation started.\n", opname);
668 spin_lock_irq(host->host_lock);
669 /* This one is queued in some place -> to wait for completion */
670 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
671 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
672 if (cp->cmd == cmd) {
673 to_do = SYM_EH_DO_WAIT;
678 if (to_do == SYM_EH_DO_WAIT) {
679 init_completion(&eh_done);
680 ucmd->old_done = cmd->scsi_done;
681 ucmd->eh_done = &eh_done;
683 cmd->scsi_done = sym_eh_done;
686 /* Try to proceed the operation we have been asked for */
690 sts = sym_abort_scsiio(np, cmd, 1);
692 case SYM_EH_DEVICE_RESET:
693 sts = sym_reset_scsi_target(np, cmd->device->id);
695 case SYM_EH_BUS_RESET:
696 sym_reset_scsi_bus(np, 1);
699 case SYM_EH_HOST_RESET:
700 sym_reset_scsi_bus(np, 0);
701 sym_start_up (np, 1);
708 /* On error, restore everything and cross fingers :) */
710 cmd->scsi_done = ucmd->old_done;
711 to_do = SYM_EH_DO_IGNORE;
715 spin_unlock_irq(host->host_lock);
717 if (to_do == SYM_EH_DO_WAIT) {
718 if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
719 ucmd->to_do = SYM_EH_DO_IGNORE;
724 dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
725 sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
726 return sts ? SCSI_FAILED : SCSI_SUCCESS;
731 * Error handlers called from the eh thread (one thread per HBA).
733 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
735 return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
738 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
740 return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
743 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
745 return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
748 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
750 return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
754 * Tune device queuing depth, according to various limits.
756 static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
758 struct sym_lcb *lp = sym_lp(tp, lun);
764 oldtags = lp->s.reqtags;
766 if (reqtags > lp->s.scdev_depth)
767 reqtags = lp->s.scdev_depth;
769 lp->s.reqtags = reqtags;
771 if (reqtags != oldtags) {
772 dev_info(&tp->starget->dev,
773 "tagged command queuing %s, command queue depth %d.\n",
774 lp->s.reqtags ? "enabled" : "disabled", reqtags);
779 * Linux select queue depths function
781 #define DEF_DEPTH (sym_driver_setup.max_tag)
782 #define ALL_TARGETS -2
787 static int device_queue_depth(struct sym_hcb *np, int target, int lun)
790 char *p = sym_driver_setup.tag_ctrl;
796 while ((c = *p++) != 0) {
797 v = simple_strtoul(p, &ep, 0);
806 t = (target == v) ? v : NO_TARGET;
811 u = (lun == v) ? v : NO_LUN;
814 if (h == np->s.unit &&
815 (t == ALL_TARGETS || t == target) &&
816 (u == ALL_LUNS || u == lun))
831 static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
833 struct sym_hcb *np = sym_get_hcb(sdev->host);
834 struct sym_tcb *tp = &np->target[sdev->id];
837 if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
840 tp->starget = sdev->sdev_target;
842 * Fail the device init if the device is flagged NOSCAN at BOOT in
843 * the NVRAM. This may speed up boot and maintain coherency with
844 * BIOS device numbering. Clearing the flag allows the user to
845 * rescan skipped devices later. We also return an error for
846 * devices not flagged for SCAN LUNS in the NVRAM since some single
847 * lun devices behave badly when asked for a non zero LUN.
850 if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
851 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
852 starget_printk(KERN_INFO, tp->starget,
853 "Scan at boot disabled in NVRAM\n");
857 if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
860 starget_printk(KERN_INFO, tp->starget,
861 "Multiple LUNs disabled in NVRAM\n");
864 lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
868 spi_min_period(tp->starget) = tp->usr_period;
869 spi_max_width(tp->starget) = tp->usr_width;
875 * Linux entry point for device queue sizing.
877 static int sym53c8xx_slave_configure(struct scsi_device *sdev)
879 struct sym_hcb *np = sym_get_hcb(sdev->host);
880 struct sym_tcb *tp = &np->target[sdev->id];
881 struct sym_lcb *lp = sym_lp(tp, sdev->lun);
882 int reqtags, depth_to_use;
887 lp->curr_flags = lp->user_flags;
890 * Select queue depth from driver setup.
891 * Donnot use more than configured by user.
893 * Donnot use more than our maximum.
895 reqtags = device_queue_depth(np, sdev->id, sdev->lun);
896 if (reqtags > tp->usrtags)
897 reqtags = tp->usrtags;
898 if (!sdev->tagged_supported)
900 #if 1 /* Avoid to locally queue commands for no good reasons */
901 if (reqtags > SYM_CONF_MAX_TAG)
902 reqtags = SYM_CONF_MAX_TAG;
903 depth_to_use = (reqtags ? reqtags : 2);
905 depth_to_use = (reqtags ? SYM_CONF_MAX_TAG : 2);
907 scsi_adjust_queue_depth(sdev,
908 (sdev->tagged_supported ?
911 lp->s.scdev_depth = depth_to_use;
912 sym_tune_dev_queuing(tp, sdev->lun, reqtags);
914 if (!spi_initial_dv(sdev->sdev_target))
920 static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
922 struct sym_hcb *np = sym_get_hcb(sdev->host);
923 struct sym_lcb *lp = sym_lp(&np->target[sdev->id], sdev->lun);
926 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK * 4, "ITLQ_TBL");
928 sym_mfree_dma(lp, sizeof(*lp), "LCB");
932 * Linux entry point for info() function
934 static const char *sym53c8xx_info (struct Scsi_Host *host)
936 return SYM_DRIVER_NAME;
940 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
942 * Proc file system stuff
944 * A read operation returns adapter information.
945 * A write operation is a control command.
946 * The string is parsed in the driver code and the command is passed
947 * to the sym_usercmd() function.
950 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
959 #define UC_SETSYNC 10
960 #define UC_SETTAGS 11
961 #define UC_SETDEBUG 12
962 #define UC_SETWIDE 14
963 #define UC_SETFLAG 15
964 #define UC_SETVERBOSE 17
965 #define UC_RESETDEV 18
966 #define UC_CLEARDEV 19
968 static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
976 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
978 sym_debug_flags = uc->data;
982 np->verbose = uc->data;
986 * We assume that other commands apply to targets.
987 * This should always be the case and avoid the below
988 * 4 lines to be repeated 6 times.
990 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
991 if (!((uc->target >> t) & 1))
998 if (!uc->data || uc->data >= 255) {
999 tp->tgoal.iu = tp->tgoal.dt =
1001 tp->tgoal.offset = 0;
1002 } else if (uc->data <= 9 && np->minsync_dt) {
1003 if (uc->data < np->minsync_dt)
1004 uc->data = np->minsync_dt;
1005 tp->tgoal.iu = tp->tgoal.dt =
1007 tp->tgoal.width = 1;
1008 tp->tgoal.period = uc->data;
1009 tp->tgoal.offset = np->maxoffs_dt;
1011 if (uc->data < np->minsync)
1012 uc->data = np->minsync;
1013 tp->tgoal.iu = tp->tgoal.dt =
1015 tp->tgoal.period = uc->data;
1016 tp->tgoal.offset = np->maxoffs;
1018 tp->tgoal.check_nego = 1;
1021 tp->tgoal.width = uc->data ? 1 : 0;
1022 tp->tgoal.check_nego = 1;
1025 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
1026 sym_tune_dev_queuing(tp, l, uc->data);
1030 np->istat_sem = SEM;
1031 OUTB(np, nc_istat, SIGP|SEM);
1034 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
1035 struct sym_lcb *lp = sym_lp(tp, l);
1036 if (lp) lp->to_clear = 1;
1038 np->istat_sem = SEM;
1039 OUTB(np, nc_istat, SIGP|SEM);
1042 tp->usrflags = uc->data;
1050 static int skip_spaces(char *ptr, int len)
1054 for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
1059 static int get_int_arg(char *ptr, int len, u_long *pv)
1063 *pv = simple_strtoul(ptr, &end, 10);
1067 static int is_keyword(char *ptr, int len, char *verb)
1069 int verb_len = strlen(verb);
1071 if (len >= verb_len && !memcmp(verb, ptr, verb_len))
1077 #define SKIP_SPACES(ptr, len) \
1078 if ((arg_len = skip_spaces(ptr, len)) < 1) \
1080 ptr += arg_len; len -= arg_len;
1082 #define GET_INT_ARG(ptr, len, v) \
1083 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
1085 ptr += arg_len; len -= arg_len;
1089 * Parse a control command
1092 static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
1096 struct sym_usrcmd cmd, *uc = &cmd;
1100 memset(uc, 0, sizeof(*uc));
1102 if (len > 0 && ptr[len-1] == '\n')
1105 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1106 uc->cmd = UC_SETSYNC;
1107 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1108 uc->cmd = UC_SETTAGS;
1109 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1110 uc->cmd = UC_SETVERBOSE;
1111 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1112 uc->cmd = UC_SETWIDE;
1113 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1114 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1115 uc->cmd = UC_SETDEBUG;
1117 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1118 uc->cmd = UC_SETFLAG;
1119 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1120 uc->cmd = UC_RESETDEV;
1121 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1122 uc->cmd = UC_CLEARDEV;
1126 #ifdef DEBUG_PROC_INFO
1127 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1132 ptr += arg_len; len -= arg_len;
1141 SKIP_SPACES(ptr, len);
1142 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1143 ptr += arg_len; len -= arg_len;
1146 GET_INT_ARG(ptr, len, target);
1147 uc->target = (1<<target);
1148 #ifdef DEBUG_PROC_INFO
1149 printk("sym_user_command: target=%ld\n", target);
1160 SKIP_SPACES(ptr, len);
1161 GET_INT_ARG(ptr, len, uc->data);
1162 #ifdef DEBUG_PROC_INFO
1163 printk("sym_user_command: data=%ld\n", uc->data);
1166 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1169 SKIP_SPACES(ptr, len);
1170 if ((arg_len = is_keyword(ptr, len, "alloc")))
1171 uc->data |= DEBUG_ALLOC;
1172 else if ((arg_len = is_keyword(ptr, len, "phase")))
1173 uc->data |= DEBUG_PHASE;
1174 else if ((arg_len = is_keyword(ptr, len, "queue")))
1175 uc->data |= DEBUG_QUEUE;
1176 else if ((arg_len = is_keyword(ptr, len, "result")))
1177 uc->data |= DEBUG_RESULT;
1178 else if ((arg_len = is_keyword(ptr, len, "scatter")))
1179 uc->data |= DEBUG_SCATTER;
1180 else if ((arg_len = is_keyword(ptr, len, "script")))
1181 uc->data |= DEBUG_SCRIPT;
1182 else if ((arg_len = is_keyword(ptr, len, "tiny")))
1183 uc->data |= DEBUG_TINY;
1184 else if ((arg_len = is_keyword(ptr, len, "timing")))
1185 uc->data |= DEBUG_TIMING;
1186 else if ((arg_len = is_keyword(ptr, len, "nego")))
1187 uc->data |= DEBUG_NEGO;
1188 else if ((arg_len = is_keyword(ptr, len, "tags")))
1189 uc->data |= DEBUG_TAGS;
1190 else if ((arg_len = is_keyword(ptr, len, "pointer")))
1191 uc->data |= DEBUG_POINTER;
1194 ptr += arg_len; len -= arg_len;
1196 #ifdef DEBUG_PROC_INFO
1197 printk("sym_user_command: data=%ld\n", uc->data);
1200 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1203 SKIP_SPACES(ptr, len);
1204 if ((arg_len = is_keyword(ptr, len, "no_disc")))
1205 uc->data &= ~SYM_DISC_ENABLED;
1208 ptr += arg_len; len -= arg_len;
1218 unsigned long flags;
1220 spin_lock_irqsave(np->s.host->host_lock, flags);
1221 sym_exec_user_command (np, uc);
1222 spin_unlock_irqrestore(np->s.host->host_lock, flags);
1227 #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1230 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1232 * Informations through the proc file system.
1241 static void copy_mem_info(struct info_str *info, char *data, int len)
1243 if (info->pos + len > info->length)
1244 len = info->length - info->pos;
1246 if (info->pos + len < info->offset) {
1250 if (info->pos < info->offset) {
1251 data += (info->offset - info->pos);
1252 len -= (info->offset - info->pos);
1256 memcpy(info->buffer + info->pos, data, len);
1261 static int copy_info(struct info_str *info, char *fmt, ...)
1267 va_start(args, fmt);
1268 len = vsprintf(buf, fmt, args);
1271 copy_mem_info(info, buf, len);
1276 * Copy formatted information into the input buffer.
1278 static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
1280 struct info_str info;
1284 info.offset = offset;
1287 copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1288 "revision id 0x%x\n",
1289 np->s.chip_name, np->device_id, np->revision_id);
1290 copy_info(&info, "At PCI address %s, IRQ " IRQ_FMT "\n",
1291 pci_name(np->s.device), IRQ_PRM(np->s.irq));
1292 copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1293 (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1294 np->maxwide ? "Wide" : "Narrow",
1295 np->minsync_dt ? ", DT capable" : "");
1297 copy_info(&info, "Max. started commands %d, "
1298 "max. commands per LUN %d\n",
1299 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1301 return info.pos > info.offset? info.pos - info.offset : 0;
1303 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1306 * Entry point of the scsi proc fs of the driver.
1307 * - func = 0 means read (returns adapter infos)
1308 * - func = 1 means write (not yet merget from sym53c8xx)
1310 static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
1311 char **start, off_t offset, int length, int func)
1313 struct sym_hcb *np = sym_get_hcb(host);
1317 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1318 retv = sym_user_command(np, buffer, length);
1325 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1326 retv = sym_host_info(np, buffer, offset, length);
1334 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1337 * Free controller resources.
1339 static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
1342 * Free O/S specific resources.
1345 free_irq(np->s.irq, np);
1347 pci_iounmap(pdev, np->s.ioaddr);
1349 pci_iounmap(pdev, np->s.ramaddr);
1351 * Free O/S independent resources.
1355 sym_mfree_dma(np, sizeof(*np), "HCB");
1359 * Ask/tell the system about DMA addressing.
1361 static int sym_setup_bus_dma_mask(struct sym_hcb *np)
1363 #if SYM_CONF_DMA_ADDRESSING_MODE > 0
1364 #if SYM_CONF_DMA_ADDRESSING_MODE == 1
1365 #define DMA_DAC_MASK DMA_40BIT_MASK
1366 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
1367 #define DMA_DAC_MASK DMA_64BIT_MASK
1369 if ((np->features & FE_DAC) &&
1370 !pci_set_dma_mask(np->s.device, DMA_DAC_MASK)) {
1376 if (!pci_set_dma_mask(np->s.device, DMA_32BIT_MASK))
1379 printf_warning("%s: No suitable DMA available\n", sym_name(np));
1384 * Host attach and initialisations.
1386 * Allocate host data and ncb structure.
1387 * Remap MMIO region.
1388 * Do chip initialization.
1389 * If all is OK, install interrupt handling and
1390 * start the timer daemon.
1392 static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
1393 int unit, struct sym_device *dev)
1395 struct host_data *host_data;
1396 struct sym_hcb *np = NULL;
1397 struct Scsi_Host *instance = NULL;
1398 struct pci_dev *pdev = dev->pdev;
1399 unsigned long flags;
1403 "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT "\n",
1404 unit, dev->chip.name, dev->chip.revision_id,
1405 pci_name(pdev), IRQ_PRM(pdev->irq));
1408 * Get the firmware for this chip.
1410 fw = sym_find_firmware(&dev->chip);
1415 * Allocate host_data structure
1417 instance = scsi_host_alloc(tpnt, sizeof(*host_data));
1420 host_data = (struct host_data *) instance->hostdata;
1423 * Allocate immediately the host control block,
1424 * since we are only expecting to succeed. :)
1425 * We keep track in the HCB of all the resources that
1426 * are to be released on error.
1428 np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
1431 np->s.device = pdev;
1432 np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
1433 host_data->ncb = np;
1434 np->s.host = instance;
1436 pci_set_drvdata(pdev, np);
1439 * Copy some useful infos to the HCB.
1441 np->hcb_ba = vtobus(np);
1442 np->verbose = sym_driver_setup.verbose;
1443 np->s.device = pdev;
1445 np->device_id = dev->chip.device_id;
1446 np->revision_id = dev->chip.revision_id;
1447 np->features = dev->chip.features;
1448 np->clock_divn = dev->chip.nr_divisor;
1449 np->maxoffs = dev->chip.offset_max;
1450 np->maxburst = dev->chip.burst_max;
1451 np->myaddr = dev->host_id;
1456 strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1457 sprintf(np->s.inst_name, "sym%d", np->s.unit);
1459 if (sym_setup_bus_dma_mask(np))
1463 * Try to map the controller chip to
1464 * virtual and physical memory.
1466 np->mmio_ba = (u32)dev->mmio_base;
1467 np->s.ioaddr = dev->s.ioaddr;
1468 np->s.ramaddr = dev->s.ramaddr;
1469 np->s.io_ws = (np->features & FE_IO256) ? 256 : 128;
1472 * Map on-chip RAM if present and supported.
1474 if (!(np->features & FE_RAM))
1476 if (dev->ram_base) {
1477 np->ram_ba = (u32)dev->ram_base;
1478 np->ram_ws = (np->features & FE_RAM8K) ? 8192 : 4096;
1481 if (sym_hcb_attach(instance, fw, dev->nvram))
1485 * Install the interrupt handler.
1486 * If we synchonize the C code with SCRIPTS on interrupt,
1487 * we do not want to share the INTR line at all.
1489 if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX, np)) {
1490 printf_err("%s: request irq %d failure\n",
1491 sym_name(np), pdev->irq);
1494 np->s.irq = pdev->irq;
1497 * After SCSI devices have been opened, we cannot
1498 * reset the bus safely, so we do it here.
1500 spin_lock_irqsave(instance->host_lock, flags);
1501 if (sym_reset_scsi_bus(np, 0))
1505 * Start the SCRIPTS.
1507 sym_start_up (np, 1);
1510 * Start the timer daemon
1512 init_timer(&np->s.timer);
1513 np->s.timer.data = (unsigned long) np;
1514 np->s.timer.function = sym53c8xx_timer;
1519 * Fill Linux host instance structure
1520 * and return success.
1522 instance->max_channel = 0;
1523 instance->this_id = np->myaddr;
1524 instance->max_id = np->maxwide ? 16 : 8;
1525 instance->max_lun = SYM_CONF_MAX_LUN;
1526 instance->unique_id = pci_resource_start(pdev, 0);
1527 instance->cmd_per_lun = SYM_CONF_MAX_TAG;
1528 instance->can_queue = (SYM_CONF_MAX_START-2);
1529 instance->sg_tablesize = SYM_CONF_MAX_SG;
1530 instance->max_cmd_len = 16;
1531 BUG_ON(sym2_transport_template == NULL);
1532 instance->transportt = sym2_transport_template;
1534 spin_unlock_irqrestore(instance->host_lock, flags);
1539 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1540 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1541 spin_unlock_irqrestore(instance->host_lock, flags);
1545 printf_info("%s: giving up ...\n", sym_name(np));
1547 sym_free_resources(np, pdev);
1548 scsi_host_put(instance);
1555 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1557 #if SYM_CONF_NVRAM_SUPPORT
1558 static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1561 devp->device_id = devp->chip.device_id;
1564 sym_read_nvram(devp, nvp);
1567 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1570 #endif /* SYM_CONF_NVRAM_SUPPORT */
1572 static int __devinit sym_check_supported(struct sym_device *device)
1574 struct sym_chip *chip;
1575 struct pci_dev *pdev = device->pdev;
1577 unsigned long io_port = pci_resource_start(pdev, 0);
1581 * If user excluded this chip, do not initialize it.
1582 * I hate this code so much. Must kill it.
1585 for (i = 0 ; i < 8 ; i++) {
1586 if (sym_driver_setup.excludes[i] == io_port)
1592 * Check if the chip is supported. Then copy the chip description
1593 * to our device structure so we can make it match the actual device
1596 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1597 chip = sym_lookup_chip_table(pdev->device, revision);
1599 dev_info(&pdev->dev, "device not supported\n");
1602 memcpy(&device->chip, chip, sizeof(device->chip));
1603 device->chip.revision_id = revision;
1609 * Ignore Symbios chips controlled by various RAID controllers.
1610 * These controllers set value 0x52414944 at RAM end - 16.
1612 static int __devinit sym_check_raid(struct sym_device *device)
1614 unsigned int ram_size, ram_val;
1616 if (!device->s.ramaddr)
1619 if (device->chip.features & FE_RAM8K)
1624 ram_val = readl(device->s.ramaddr + ram_size - 16);
1625 if (ram_val != 0x52414944)
1628 dev_info(&device->pdev->dev,
1629 "not initializing, driven by RAID controller.\n");
1633 static int __devinit sym_set_workarounds(struct sym_device *device)
1635 struct sym_chip *chip = &device->chip;
1636 struct pci_dev *pdev = device->pdev;
1640 * (ITEM 12 of a DEL about the 896 I haven't yet).
1641 * We must ensure the chip will use WRITE AND INVALIDATE.
1642 * The revision number limit is for now arbitrary.
1644 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && chip->revision_id < 0x4) {
1645 chip->features |= (FE_WRIE | FE_CLSE);
1648 /* If the chip can do Memory Write Invalidate, enable it */
1649 if (chip->features & FE_WRIE) {
1650 if (pci_set_mwi(pdev))
1655 * Work around for errant bit in 895A. The 66Mhz
1656 * capable bit is set erroneously. Clear this bit.
1659 * Make sure Config space and Features agree.
1661 * Recall: writes are not normal to status register -
1662 * write a 1 to clear and a 0 to leave unchanged.
1663 * Can only reset bits.
1665 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1666 if (chip->features & FE_66MHZ) {
1667 if (!(status_reg & PCI_STATUS_66MHZ))
1668 chip->features &= ~FE_66MHZ;
1670 if (status_reg & PCI_STATUS_66MHZ) {
1671 status_reg = PCI_STATUS_66MHZ;
1672 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1673 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1681 * Read and check the PCI configuration for any detected NCR
1682 * boards and save data for attaching after all boards have
1685 static void __devinit
1686 sym_init_device(struct pci_dev *pdev, struct sym_device *device)
1689 struct pci_bus_region bus_addr;
1691 device->host_id = SYM_SETUP_HOST_ID;
1692 device->pdev = pdev;
1694 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
1695 device->mmio_base = bus_addr.start;
1698 * If the BAR is 64-bit, resource 2 will be occupied by the
1701 if (!pdev->resource[i].flags)
1703 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
1704 device->ram_base = bus_addr.start;
1706 #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
1707 if (device->mmio_base)
1708 device->s.ioaddr = pci_iomap(pdev, 1,
1709 pci_resource_len(pdev, 1));
1711 if (!device->s.ioaddr)
1712 device->s.ioaddr = pci_iomap(pdev, 0,
1713 pci_resource_len(pdev, 0));
1714 if (device->ram_base)
1715 device->s.ramaddr = pci_iomap(pdev, i,
1716 pci_resource_len(pdev, i));
1720 * The NCR PQS and PDS cards are constructed as a DEC bridge
1721 * behind which sits a proprietary NCR memory controller and
1722 * either four or two 53c875s as separate devices. We can tell
1723 * if an 875 is part of a PQS/PDS or not since if it is, it will
1724 * be on the same bus as the memory controller. In its usual
1725 * mode of operation, the 875s are slaved to the memory
1726 * controller for all transfers. To operate with the Linux
1727 * driver, the memory controller is disabled and the 875s
1728 * freed to function independently. The only wrinkle is that
1729 * the preset SCSI ID (which may be zero) must be read in from
1730 * a special configuration space register of the 875.
1732 static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
1737 for (slot = 0; slot < 256; slot++) {
1738 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
1740 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
1745 /* bit 1: allow individual 875 configuration */
1746 pci_read_config_byte(memc, 0x44, &tmp);
1747 if ((tmp & 0x2) == 0) {
1749 pci_write_config_byte(memc, 0x44, tmp);
1752 /* bit 2: drive individual 875 interrupts to the bus */
1753 pci_read_config_byte(memc, 0x45, &tmp);
1754 if ((tmp & 0x4) == 0) {
1756 pci_write_config_byte(memc, 0x45, tmp);
1763 pci_read_config_byte(pdev, 0x84, &tmp);
1764 sym_dev->host_id = tmp;
1768 * Called before unloading the module.
1770 * We have to free resources and halt the NCR chip.
1772 static int sym_detach(struct sym_hcb *np, struct pci_dev *pdev)
1774 printk("%s: detaching ...\n", sym_name(np));
1776 del_timer_sync(&np->s.timer);
1780 * We should use sym_soft_reset(), but we don't want to do
1781 * so, since we may not be safe if interrupts occur.
1783 printk("%s: resetting chip\n", sym_name(np));
1784 OUTB(np, nc_istat, SRST);
1787 OUTB(np, nc_istat, 0);
1789 sym_free_resources(np, pdev);
1795 * Driver host template.
1797 static struct scsi_host_template sym2_template = {
1798 .module = THIS_MODULE,
1799 .name = "sym53c8xx",
1800 .info = sym53c8xx_info,
1801 .queuecommand = sym53c8xx_queue_command,
1802 .slave_alloc = sym53c8xx_slave_alloc,
1803 .slave_configure = sym53c8xx_slave_configure,
1804 .slave_destroy = sym53c8xx_slave_destroy,
1805 .eh_abort_handler = sym53c8xx_eh_abort_handler,
1806 .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
1807 .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
1808 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
1810 .use_clustering = ENABLE_CLUSTERING,
1811 .max_sectors = 0xFFFF,
1812 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1813 .proc_info = sym53c8xx_proc_info,
1814 .proc_name = NAME53C8XX,
1818 static int attach_count;
1820 static int __devinit sym2_probe(struct pci_dev *pdev,
1821 const struct pci_device_id *ent)
1823 struct sym_device sym_dev;
1824 struct sym_nvram nvram;
1825 struct Scsi_Host *instance;
1827 memset(&sym_dev, 0, sizeof(sym_dev));
1828 memset(&nvram, 0, sizeof(nvram));
1830 if (pci_enable_device(pdev))
1833 pci_set_master(pdev);
1835 if (pci_request_regions(pdev, NAME53C8XX))
1838 sym_init_device(pdev, &sym_dev);
1839 if (sym_check_supported(&sym_dev))
1842 if (sym_check_raid(&sym_dev))
1843 goto leave; /* Don't disable the device */
1845 if (sym_set_workarounds(&sym_dev))
1848 sym_config_pqs(pdev, &sym_dev);
1850 sym_get_nvram(&sym_dev, &nvram);
1852 instance = sym_attach(&sym2_template, attach_count, &sym_dev);
1856 if (scsi_add_host(instance, &pdev->dev))
1858 scsi_scan_host(instance);
1865 sym_detach(pci_get_drvdata(pdev), pdev);
1867 pci_release_regions(pdev);
1869 pci_disable_device(pdev);
1874 static void __devexit sym2_remove(struct pci_dev *pdev)
1876 struct sym_hcb *np = pci_get_drvdata(pdev);
1877 struct Scsi_Host *host = np->s.host;
1879 scsi_remove_host(host);
1880 scsi_host_put(host);
1882 sym_detach(np, pdev);
1884 pci_release_regions(pdev);
1885 pci_disable_device(pdev);
1890 static void sym2_get_signalling(struct Scsi_Host *shost)
1892 struct sym_hcb *np = sym_get_hcb(shost);
1893 enum spi_signal_type type;
1895 switch (np->scsi_mode) {
1897 type = SPI_SIGNAL_SE;
1900 type = SPI_SIGNAL_LVD;
1903 type = SPI_SIGNAL_HVD;
1906 type = SPI_SIGNAL_UNKNOWN;
1909 spi_signalling(shost) = type;
1912 static void sym2_set_offset(struct scsi_target *starget, int offset)
1914 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1915 struct sym_hcb *np = sym_get_hcb(shost);
1916 struct sym_tcb *tp = &np->target[starget->id];
1918 tp->tgoal.offset = offset;
1919 tp->tgoal.check_nego = 1;
1922 static void sym2_set_period(struct scsi_target *starget, int period)
1924 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1925 struct sym_hcb *np = sym_get_hcb(shost);
1926 struct sym_tcb *tp = &np->target[starget->id];
1928 /* have to have DT for these transfers, but DT will also
1929 * set width, so check that this is allowed */
1930 if (period <= np->minsync && spi_width(starget))
1933 tp->tgoal.period = period;
1934 tp->tgoal.check_nego = 1;
1937 static void sym2_set_width(struct scsi_target *starget, int width)
1939 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1940 struct sym_hcb *np = sym_get_hcb(shost);
1941 struct sym_tcb *tp = &np->target[starget->id];
1943 /* It is illegal to have DT set on narrow transfers. If DT is
1944 * clear, we must also clear IU and QAS. */
1946 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1948 tp->tgoal.width = width;
1949 tp->tgoal.check_nego = 1;
1952 static void sym2_set_dt(struct scsi_target *starget, int dt)
1954 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1955 struct sym_hcb *np = sym_get_hcb(shost);
1956 struct sym_tcb *tp = &np->target[starget->id];
1958 /* We must clear QAS and IU if DT is clear */
1962 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1963 tp->tgoal.check_nego = 1;
1967 static void sym2_set_iu(struct scsi_target *starget, int iu)
1969 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1970 struct sym_hcb *np = sym_get_hcb(shost);
1971 struct sym_tcb *tp = &np->target[starget->id];
1974 tp->tgoal.iu = tp->tgoal.dt = 1;
1977 tp->tgoal.check_nego = 1;
1980 static void sym2_set_qas(struct scsi_target *starget, int qas)
1982 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1983 struct sym_hcb *np = sym_get_hcb(shost);
1984 struct sym_tcb *tp = &np->target[starget->id];
1987 tp->tgoal.dt = tp->tgoal.qas = 1;
1990 tp->tgoal.check_nego = 1;
1994 static struct spi_function_template sym2_transport_functions = {
1995 .set_offset = sym2_set_offset,
1997 .set_period = sym2_set_period,
1999 .set_width = sym2_set_width,
2001 .set_dt = sym2_set_dt,
2004 .set_iu = sym2_set_iu,
2006 .set_qas = sym2_set_qas,
2009 .get_signalling = sym2_get_signalling,
2012 static struct pci_device_id sym2_id_table[] __devinitdata = {
2013 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
2014 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2015 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
2016 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2017 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
2018 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2019 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
2020 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2021 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
2022 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2023 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
2024 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2025 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
2026 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
2027 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
2028 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2029 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
2030 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2031 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2032 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2033 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2034 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2035 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
2036 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
2037 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2038 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2039 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2040 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2041 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2042 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2043 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2044 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2045 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2046 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2050 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2052 static struct pci_driver sym2_driver = {
2054 .id_table = sym2_id_table,
2055 .probe = sym2_probe,
2056 .remove = __devexit_p(sym2_remove),
2059 static int __init sym2_init(void)
2063 sym2_setup_params();
2064 sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2065 if (!sym2_transport_template)
2068 error = pci_register_driver(&sym2_driver);
2070 spi_release_transport(sym2_transport_template);
2074 static void __exit sym2_exit(void)
2076 pci_unregister_driver(&sym2_driver);
2077 spi_release_transport(sym2_transport_template);
2080 module_init(sym2_init);
2081 module_exit(sym2_exit);