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"
57 /* SPARC just has to be different ... */
60 #define IRQ_PRM(x) __irq_itoa(x)
63 #define IRQ_PRM(x) (x)
66 struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
67 unsigned int sym_debug_flags = 0;
69 static char *excl_string;
70 static char *safe_string;
71 module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
72 module_param_string(tag_ctrl, sym_driver_setup.tag_ctrl, 100, 0);
73 module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
74 module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
75 module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
76 module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
77 module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
78 module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
79 module_param_named(verb, sym_driver_setup.verbose, byte, 0);
80 module_param_named(debug, sym_debug_flags, uint, 0);
81 module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
82 module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
83 module_param_named(excl, excl_string, charp, 0);
84 module_param_named(safe, safe_string, charp, 0);
86 MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
87 MODULE_PARM_DESC(tag_ctrl, "More detailed control over tags per LUN");
88 MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
89 MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
90 MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
91 MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
92 MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
93 MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
94 MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
95 MODULE_PARM_DESC(debug, "Set bits to enable debugging");
96 MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
97 MODULE_PARM_DESC(nvram, "Option currently not used");
98 MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
99 MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
101 MODULE_LICENSE("GPL");
102 MODULE_VERSION(SYM_VERSION);
103 MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
104 MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
106 static void sym2_setup_params(void)
108 char *p = excl_string;
111 while (p && (xi < 8)) {
113 int val = (int) simple_strtoul(p, &next_p, 0);
114 sym_driver_setup.excludes[xi++] = val;
119 if (*safe_string == 'y') {
120 sym_driver_setup.max_tag = 0;
121 sym_driver_setup.burst_order = 0;
122 sym_driver_setup.scsi_led = 0;
123 sym_driver_setup.scsi_diff = 1;
124 sym_driver_setup.irq_mode = 0;
125 sym_driver_setup.scsi_bus_check = 2;
126 sym_driver_setup.host_id = 7;
127 sym_driver_setup.verbose = 2;
128 sym_driver_setup.settle_delay = 10;
129 sym_driver_setup.use_nvram = 1;
130 } else if (*safe_string != 'n') {
131 printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
132 " passed to safe option", safe_string);
138 * We used to try to deal with 64-bit BARs here, but don't any more.
139 * There are many parts of this driver which would need to be modified
140 * to handle a 64-bit base address, including scripts. I'm uncomfortable
141 * with making those changes when I have no way of testing it, so I'm
142 * just going to disable it.
144 * Note that some machines (eg HP rx8620 and Superdome) have bus addresses
145 * below 4GB and physical addresses above 4GB. These will continue to work.
148 pci_get_base_address(struct pci_dev *pdev, int index, unsigned long *basep)
152 #define PCI_BAR_OFFSET(index) (PCI_BASE_ADDRESS_0 + (index<<2))
154 pci_read_config_dword(pdev, PCI_BAR_OFFSET(index++), &tmp);
156 if ((tmp & 0x7) == PCI_BASE_ADDRESS_MEM_TYPE_64) {
157 pci_read_config_dword(pdev, PCI_BAR_OFFSET(index++), &tmp);
160 "BAR %d is 64-bit, disabling\n", index - 1);
165 if ((base & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
166 base &= PCI_BASE_ADDRESS_IO_MASK;
168 base &= PCI_BASE_ADDRESS_MEM_MASK;
173 #undef PCI_BAR_OFFSET
176 static struct scsi_transport_template *sym2_transport_template = NULL;
179 * Used by the eh thread to wait for command completion.
180 * It is allocated on the eh thread stack.
183 struct completion done;
184 struct timer_list timer;
185 void (*old_done)(struct scsi_cmnd *);
191 * Driver private area in the SCSI command structure.
193 struct sym_ucmd { /* Override the SCSI pointer structure */
194 dma_addr_t data_mapping;
196 struct sym_eh_wait *eh_wait;
199 #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
200 #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
202 static void __unmap_scsi_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
204 int dma_dir = cmd->sc_data_direction;
206 switch(SYM_UCMD_PTR(cmd)->data_mapped) {
208 pci_unmap_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
211 pci_unmap_single(pdev, SYM_UCMD_PTR(cmd)->data_mapping,
212 cmd->request_bufflen, dma_dir);
215 SYM_UCMD_PTR(cmd)->data_mapped = 0;
218 static dma_addr_t __map_scsi_single_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
221 int dma_dir = cmd->sc_data_direction;
223 mapping = pci_map_single(pdev, cmd->request_buffer,
224 cmd->request_bufflen, dma_dir);
226 SYM_UCMD_PTR(cmd)->data_mapped = 1;
227 SYM_UCMD_PTR(cmd)->data_mapping = mapping;
233 static int __map_scsi_sg_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
236 int dma_dir = cmd->sc_data_direction;
238 use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
240 SYM_UCMD_PTR(cmd)->data_mapped = 2;
241 SYM_UCMD_PTR(cmd)->data_mapping = use_sg;
247 #define unmap_scsi_data(np, cmd) \
248 __unmap_scsi_data(np->s.device, cmd)
249 #define map_scsi_single_data(np, cmd) \
250 __map_scsi_single_data(np->s.device, cmd)
251 #define map_scsi_sg_data(np, cmd) \
252 __map_scsi_sg_data(np->s.device, cmd)
254 * Complete a pending CAM CCB.
256 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
258 unmap_scsi_data(np, cmd);
262 static void sym_xpt_done2(struct sym_hcb *np, struct scsi_cmnd *cmd, int cam_status)
264 sym_set_cam_status(cmd, cam_status);
265 sym_xpt_done(np, cmd);
270 * Tell the SCSI layer about a BUS RESET.
272 void sym_xpt_async_bus_reset(struct sym_hcb *np)
274 printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
275 np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
276 np->s.settle_time_valid = 1;
277 if (sym_verbose >= 2)
278 printf_info("%s: command processing suspended for %d seconds\n",
279 sym_name(np), sym_driver_setup.settle_delay);
283 * Tell the SCSI layer about a BUS DEVICE RESET message sent.
285 void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
287 printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
291 * Choose the more appropriate CAM status if
292 * the IO encountered an extended error.
294 static int sym_xerr_cam_status(int cam_status, int x_status)
297 if (x_status & XE_PARITY_ERR)
298 cam_status = DID_PARITY;
299 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
300 cam_status = DID_ERROR;
301 else if (x_status & XE_BAD_PHASE)
302 cam_status = DID_ERROR;
304 cam_status = DID_ERROR;
310 * Build CAM result for a failed or auto-sensed IO.
312 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
314 struct scsi_cmnd *cmd = cp->cmd;
315 u_int cam_status, scsi_status, drv_status;
319 scsi_status = cp->ssss_status;
321 if (cp->host_flags & HF_SENSE) {
322 scsi_status = cp->sv_scsi_status;
323 resid = cp->sv_resid;
324 if (sym_verbose && cp->sv_xerr_status)
325 sym_print_xerr(cmd, cp->sv_xerr_status);
326 if (cp->host_status == HS_COMPLETE &&
327 cp->ssss_status == S_GOOD &&
328 cp->xerr_status == 0) {
329 cam_status = sym_xerr_cam_status(DID_OK,
331 drv_status = DRIVER_SENSE;
333 * Bounce back the sense data to user.
335 memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
336 memcpy(cmd->sense_buffer, cp->sns_bbuf,
337 min(sizeof(cmd->sense_buffer),
338 (size_t)SYM_SNS_BBUF_LEN));
341 * If the device reports a UNIT ATTENTION condition
342 * due to a RESET condition, we should consider all
343 * disconnect CCBs for this unit as aborted.
347 p = (u_char *) cmd->sense_data;
348 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
349 sym_clear_tasks(np, DID_ABORT,
350 cp->target,cp->lun, -1);
355 * Error return from our internal request sense. This
356 * is bad: we must clear the contingent allegiance
357 * condition otherwise the device will always return
358 * BUSY. Use a big stick.
360 sym_reset_scsi_target(np, cmd->device->id);
361 cam_status = DID_ERROR;
363 } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
365 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
366 cam_status = DID_NO_CONNECT;
367 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
368 cam_status = DID_ERROR;
369 else { /* Extended error */
371 sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
372 cp->host_status, cp->ssss_status,
376 * Set the most appropriate value for CAM status.
378 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
381 cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
386 * Build the scatter/gather array for an I/O.
389 static int sym_scatter_no_sglist(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
391 struct sym_tblmove *data = &cp->phys.data[SYM_CONF_MAX_SG-1];
393 unsigned int len = cmd->request_bufflen;
396 dma_addr_t baddr = map_scsi_single_data(np, cmd);
399 struct sym_tcb *tp = &np->target[cp->target];
400 if (tp->head.wval & EWS) {
402 cp->odd_byte_adjustment++;
406 sym_build_sge(np, data, baddr, len);
418 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
421 int use_sg = (int) cmd->use_sg;
426 segment = sym_scatter_no_sglist(np, cp, cmd);
427 else if ((use_sg = map_scsi_sg_data(np, cmd)) > 0) {
428 struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
429 struct sym_tcb *tp = &np->target[cp->target];
430 struct sym_tblmove *data;
432 if (use_sg > SYM_CONF_MAX_SG) {
433 unmap_scsi_data(np, cmd);
437 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
439 for (segment = 0; segment < use_sg; segment++) {
440 dma_addr_t baddr = sg_dma_address(&scatter[segment]);
441 unsigned int len = sg_dma_len(&scatter[segment]);
443 if ((len & 1) && (tp->head.wval & EWS)) {
445 cp->odd_byte_adjustment++;
448 sym_build_sge(np, &data[segment], baddr, len);
459 * Queue a SCSI command.
461 static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
463 struct scsi_device *sdev = cmd->device;
470 * Minimal checkings, so that we will not
471 * go outside our tables.
473 if (sdev->id == np->myaddr) {
474 sym_xpt_done2(np, cmd, DID_NO_CONNECT);
479 * Retrieve the target descriptor.
481 tp = &np->target[sdev->id];
484 * Select tagged/untagged.
486 lp = sym_lp(tp, sdev->lun);
487 order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
492 cp = sym_get_ccb(np, cmd, order);
494 return 1; /* Means resource shortage */
495 sym_queue_scsiio(np, cmd, cp);
500 * Setup buffers and pointers that address the CDB.
502 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
504 memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
506 cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
507 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
513 * Setup pointers that address the data and start the I/O.
515 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
518 struct sym_tcb *tp = &np->target[cp->target];
519 struct sym_lcb *lp = sym_lp(tp, cp->lun);
524 if (sym_setup_cdb(np, cmd, cp))
528 * No direction means no data.
530 dir = cmd->sc_data_direction;
531 if (dir != DMA_NONE) {
532 cp->segments = sym_scatter(np, cp, cmd);
533 if (cp->segments < 0) {
534 sym_set_cam_status(cmd, DID_ERROR);
545 sym_setup_data_pointers(np, cp, dir);
548 * When `#ifed 1', the code below makes the driver
549 * panic on the first attempt to write to a SCSI device.
550 * It is the first test we want to do after a driver
551 * change that does not seem obviously safe. :)
554 switch (cp->cdb_buf[0]) {
555 case 0x0A: case 0x2A: case 0xAA:
556 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
567 sym_start_next_ccbs(np, lp, 2);
569 sym_put_start_queue(np, cp);
573 sym_free_ccb(np, cp);
574 sym_xpt_done(np, cmd);
582 * Misused to keep the driver running when
583 * interrupts are not configured correctly.
585 static void sym_timer(struct sym_hcb *np)
587 unsigned long thistime = jiffies;
592 np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
593 add_timer(&np->s.timer);
596 * If we are resetting the ncr, wait for settle_time before
597 * clearing it. Then command processing will be resumed.
599 if (np->s.settle_time_valid) {
600 if (time_before_eq(np->s.settle_time, thistime)) {
601 if (sym_verbose >= 2 )
602 printk("%s: command processing resumed\n",
604 np->s.settle_time_valid = 0;
610 * Nothing to do for now, but that may come.
612 if (np->s.lasttime + 4*HZ < thistime) {
613 np->s.lasttime = thistime;
616 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
618 * Some way-broken PCI bridges may lead to
619 * completions being lost when the clearing
620 * of the INTFLY flag by the CPU occurs
621 * concurrently with the chip raising this flag.
622 * If this ever happen, lost completions will
631 * PCI BUS error handler.
633 void sym_log_bus_error(struct sym_hcb *np)
636 pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
637 if (pci_sts & 0xf900) {
638 pci_write_config_word(np->s.device, PCI_STATUS, pci_sts);
639 printf("%s: PCI STATUS = 0x%04x\n",
640 sym_name(np), pci_sts & 0xf900);
645 * queuecommand method. Entered with the host adapter lock held and
646 * interrupts disabled.
648 static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
649 void (*done)(struct scsi_cmnd *))
651 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
652 struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
655 cmd->scsi_done = done;
656 memset(ucp, 0, sizeof(*ucp));
659 * Shorten our settle_time if needed for
660 * this command not to time out.
662 if (np->s.settle_time_valid && cmd->timeout_per_command) {
663 unsigned long tlimit = jiffies + cmd->timeout_per_command;
664 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
665 if (time_after(np->s.settle_time, tlimit)) {
666 np->s.settle_time = tlimit;
670 if (np->s.settle_time_valid)
671 return SCSI_MLQUEUE_HOST_BUSY;
673 sts = sym_queue_command(np, cmd);
675 return SCSI_MLQUEUE_HOST_BUSY;
680 * Linux entry point of the interrupt handler.
682 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
685 struct sym_hcb *np = (struct sym_hcb *)dev_id;
687 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
689 spin_lock_irqsave(np->s.host->host_lock, flags);
691 spin_unlock_irqrestore(np->s.host->host_lock, flags);
693 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
699 * Linux entry point of the timer handler
701 static void sym53c8xx_timer(unsigned long npref)
703 struct sym_hcb *np = (struct sym_hcb *)npref;
706 spin_lock_irqsave(np->s.host->host_lock, flags);
708 spin_unlock_irqrestore(np->s.host->host_lock, flags);
713 * What the eh thread wants us to perform.
715 #define SYM_EH_ABORT 0
716 #define SYM_EH_DEVICE_RESET 1
717 #define SYM_EH_BUS_RESET 2
718 #define SYM_EH_HOST_RESET 3
721 * What we will do regarding the involved SCSI command.
723 #define SYM_EH_DO_IGNORE 0
724 #define SYM_EH_DO_COMPLETE 1
725 #define SYM_EH_DO_WAIT 2
728 * Our general completion handler.
730 static void __sym_eh_done(struct scsi_cmnd *cmd, int timed_out)
732 struct sym_eh_wait *ep = SYM_UCMD_PTR(cmd)->eh_wait;
736 /* Try to avoid a race here (not 100% safe) */
739 if (ep->to_do == SYM_EH_DO_WAIT && !del_timer(&ep->timer))
743 /* Revert everything */
744 SYM_UCMD_PTR(cmd)->eh_wait = NULL;
745 cmd->scsi_done = ep->old_done;
747 /* Wake up the eh thread if it wants to sleep */
748 if (ep->to_do == SYM_EH_DO_WAIT)
753 * scsi_done() alias when error recovery is in progress.
755 static void sym_eh_done(struct scsi_cmnd *cmd) { __sym_eh_done(cmd, 0); }
758 * Some timeout handler to avoid waiting too long.
760 static void sym_eh_timeout(u_long p) { __sym_eh_done((struct scsi_cmnd *)p, 1); }
763 * Generic method for our eh processing.
764 * The 'op' argument tells what we have to do.
766 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
768 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
770 int to_do = SYM_EH_DO_IGNORE;
772 struct sym_eh_wait eh, *ep = &eh;
774 dev_warn(&cmd->device->sdev_gendev, "%s operation started.\n", opname);
776 /* This one is queued in some place -> to wait for completion */
777 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
778 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
779 if (cp->cmd == cmd) {
780 to_do = SYM_EH_DO_WAIT;
786 /* Prepare stuff to either ignore, complete or wait for completion */
789 case SYM_EH_DO_IGNORE:
792 init_completion(&ep->done);
794 case SYM_EH_DO_COMPLETE:
795 ep->old_done = cmd->scsi_done;
796 cmd->scsi_done = sym_eh_done;
797 SYM_UCMD_PTR(cmd)->eh_wait = ep;
800 /* Try to proceed the operation we have been asked for */
804 sts = sym_abort_scsiio(np, cmd, 1);
806 case SYM_EH_DEVICE_RESET:
807 sts = sym_reset_scsi_target(np, cmd->device->id);
809 case SYM_EH_BUS_RESET:
810 sym_reset_scsi_bus(np, 1);
813 case SYM_EH_HOST_RESET:
814 sym_reset_scsi_bus(np, 0);
815 sym_start_up (np, 1);
822 /* On error, restore everything and cross fingers :) */
824 SYM_UCMD_PTR(cmd)->eh_wait = NULL;
825 cmd->scsi_done = ep->old_done;
826 to_do = SYM_EH_DO_IGNORE;
830 /* Complete the command with locks held as required by the driver */
831 if (to_do == SYM_EH_DO_COMPLETE)
832 sym_xpt_done2(np, cmd, DID_ABORT);
834 /* Wait for completion with locks released, as required by kernel */
835 if (to_do == SYM_EH_DO_WAIT) {
836 init_timer(&ep->timer);
837 ep->timer.expires = jiffies + (5*HZ);
838 ep->timer.function = sym_eh_timeout;
839 ep->timer.data = (u_long)cmd;
840 ep->timed_out = 1; /* Be pessimistic for once :) */
841 add_timer(&ep->timer);
842 spin_unlock_irq(np->s.host->host_lock);
843 wait_for_completion(&ep->done);
844 spin_lock_irq(np->s.host->host_lock);
848 dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
849 sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
850 return sts ? SCSI_FAILED : SCSI_SUCCESS;
855 * Error handlers called from the eh thread (one thread per HBA).
857 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
861 spin_lock_irq(cmd->device->host->host_lock);
862 rc = sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
863 spin_unlock_irq(cmd->device->host->host_lock);
868 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
870 return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
873 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
875 return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
878 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
880 return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
884 * Tune device queuing depth, according to various limits.
886 static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
888 struct sym_lcb *lp = sym_lp(tp, lun);
894 oldtags = lp->s.reqtags;
896 if (reqtags > lp->s.scdev_depth)
897 reqtags = lp->s.scdev_depth;
899 lp->started_limit = reqtags ? reqtags : 2;
901 lp->s.reqtags = reqtags;
903 if (reqtags != oldtags) {
904 dev_info(&tp->starget->dev,
905 "tagged command queuing %s, command queue depth %d.\n",
906 lp->s.reqtags ? "enabled" : "disabled",
912 * Linux select queue depths function
914 #define DEF_DEPTH (sym_driver_setup.max_tag)
915 #define ALL_TARGETS -2
920 static int device_queue_depth(struct sym_hcb *np, int target, int lun)
923 char *p = sym_driver_setup.tag_ctrl;
929 while ((c = *p++) != 0) {
930 v = simple_strtoul(p, &ep, 0);
939 t = (target == v) ? v : NO_TARGET;
944 u = (lun == v) ? v : NO_LUN;
947 if (h == np->s.unit &&
948 (t == ALL_TARGETS || t == target) &&
949 (u == ALL_LUNS || u == lun))
964 static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
969 if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
972 np = sym_get_hcb(sdev->host);
973 tp = &np->target[sdev->id];
976 * Fail the device init if the device is flagged NOSCAN at BOOT in
977 * the NVRAM. This may speed up boot and maintain coherency with
978 * BIOS device numbering. Clearing the flag allows the user to
979 * rescan skipped devices later. We also return an error for
980 * devices not flagged for SCAN LUNS in the NVRAM since some single
981 * lun devices behave badly when asked for a non zero LUN.
984 if ((tp->usrflags & SYM_SCAN_BOOT_DISABLED) ||
985 ((tp->usrflags & SYM_SCAN_LUNS_DISABLED) && sdev->lun != 0)) {
986 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
990 tp->starget = sdev->sdev_target;
995 * Linux entry point for device queue sizing.
997 static int sym53c8xx_slave_configure(struct scsi_device *device)
999 struct sym_hcb *np = sym_get_hcb(device->host);
1000 struct sym_tcb *tp = &np->target[device->id];
1002 int reqtags, depth_to_use;
1005 * Allocate the LCB if not yet.
1006 * If it fail, we may well be in the sh*t. :)
1008 lp = sym_alloc_lcb(np, device->id, device->lun);
1015 lp->curr_flags = lp->user_flags;
1018 * Select queue depth from driver setup.
1019 * Donnot use more than configured by user.
1021 * Donnot use more than our maximum.
1023 reqtags = device_queue_depth(np, device->id, device->lun);
1024 if (reqtags > tp->usrtags)
1025 reqtags = tp->usrtags;
1026 if (!device->tagged_supported)
1028 #if 1 /* Avoid to locally queue commands for no good reasons */
1029 if (reqtags > SYM_CONF_MAX_TAG)
1030 reqtags = SYM_CONF_MAX_TAG;
1031 depth_to_use = (reqtags ? reqtags : 2);
1033 depth_to_use = (reqtags ? SYM_CONF_MAX_TAG : 2);
1035 scsi_adjust_queue_depth(device,
1036 (device->tagged_supported ?
1037 MSG_SIMPLE_TAG : 0),
1039 lp->s.scdev_depth = depth_to_use;
1040 sym_tune_dev_queuing(tp, device->lun, reqtags);
1042 if (!spi_initial_dv(device->sdev_target))
1043 spi_dv_device(device);
1049 * Linux entry point for info() function
1051 static const char *sym53c8xx_info (struct Scsi_Host *host)
1053 return SYM_DRIVER_NAME;
1057 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1059 * Proc file system stuff
1061 * A read operation returns adapter information.
1062 * A write operation is a control command.
1063 * The string is parsed in the driver code and the command is passed
1064 * to the sym_usercmd() function.
1067 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1076 #define UC_SETSYNC 10
1077 #define UC_SETTAGS 11
1078 #define UC_SETDEBUG 12
1079 #define UC_SETWIDE 14
1080 #define UC_SETFLAG 15
1081 #define UC_SETVERBOSE 17
1082 #define UC_RESETDEV 18
1083 #define UC_CLEARDEV 19
1085 static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
1093 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1095 sym_debug_flags = uc->data;
1099 np->verbose = uc->data;
1103 * We assume that other commands apply to targets.
1104 * This should always be the case and avoid the below
1105 * 4 lines to be repeated 6 times.
1107 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
1108 if (!((uc->target >> t) & 1))
1110 tp = &np->target[t];
1115 if (!uc->data || uc->data >= 255) {
1116 tp->tgoal.iu = tp->tgoal.dt =
1118 tp->tgoal.offset = 0;
1119 } else if (uc->data <= 9 && np->minsync_dt) {
1120 if (uc->data < np->minsync_dt)
1121 uc->data = np->minsync_dt;
1122 tp->tgoal.iu = tp->tgoal.dt =
1124 tp->tgoal.width = 1;
1125 tp->tgoal.period = uc->data;
1126 tp->tgoal.offset = np->maxoffs_dt;
1128 if (uc->data < np->minsync)
1129 uc->data = np->minsync;
1130 tp->tgoal.iu = tp->tgoal.dt =
1132 tp->tgoal.period = uc->data;
1133 tp->tgoal.offset = np->maxoffs;
1135 tp->tgoal.check_nego = 1;
1138 tp->tgoal.width = uc->data ? 1 : 0;
1139 tp->tgoal.check_nego = 1;
1142 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
1143 sym_tune_dev_queuing(tp, l, uc->data);
1147 np->istat_sem = SEM;
1148 OUTB(np, nc_istat, SIGP|SEM);
1151 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
1152 struct sym_lcb *lp = sym_lp(tp, l);
1153 if (lp) lp->to_clear = 1;
1155 np->istat_sem = SEM;
1156 OUTB(np, nc_istat, SIGP|SEM);
1159 tp->usrflags = uc->data;
1167 static int skip_spaces(char *ptr, int len)
1171 for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
1176 static int get_int_arg(char *ptr, int len, u_long *pv)
1180 *pv = simple_strtoul(ptr, &end, 10);
1184 static int is_keyword(char *ptr, int len, char *verb)
1186 int verb_len = strlen(verb);
1188 if (len >= verb_len && !memcmp(verb, ptr, verb_len))
1194 #define SKIP_SPACES(ptr, len) \
1195 if ((arg_len = skip_spaces(ptr, len)) < 1) \
1197 ptr += arg_len; len -= arg_len;
1199 #define GET_INT_ARG(ptr, len, v) \
1200 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
1202 ptr += arg_len; len -= arg_len;
1206 * Parse a control command
1209 static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
1213 struct sym_usrcmd cmd, *uc = &cmd;
1217 memset(uc, 0, sizeof(*uc));
1219 if (len > 0 && ptr[len-1] == '\n')
1222 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1223 uc->cmd = UC_SETSYNC;
1224 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1225 uc->cmd = UC_SETTAGS;
1226 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1227 uc->cmd = UC_SETVERBOSE;
1228 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1229 uc->cmd = UC_SETWIDE;
1230 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1231 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1232 uc->cmd = UC_SETDEBUG;
1234 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1235 uc->cmd = UC_SETFLAG;
1236 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1237 uc->cmd = UC_RESETDEV;
1238 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1239 uc->cmd = UC_CLEARDEV;
1243 #ifdef DEBUG_PROC_INFO
1244 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1249 ptr += arg_len; len -= arg_len;
1258 SKIP_SPACES(ptr, len);
1259 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1260 ptr += arg_len; len -= arg_len;
1263 GET_INT_ARG(ptr, len, target);
1264 uc->target = (1<<target);
1265 #ifdef DEBUG_PROC_INFO
1266 printk("sym_user_command: target=%ld\n", target);
1277 SKIP_SPACES(ptr, len);
1278 GET_INT_ARG(ptr, len, uc->data);
1279 #ifdef DEBUG_PROC_INFO
1280 printk("sym_user_command: data=%ld\n", uc->data);
1283 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1286 SKIP_SPACES(ptr, len);
1287 if ((arg_len = is_keyword(ptr, len, "alloc")))
1288 uc->data |= DEBUG_ALLOC;
1289 else if ((arg_len = is_keyword(ptr, len, "phase")))
1290 uc->data |= DEBUG_PHASE;
1291 else if ((arg_len = is_keyword(ptr, len, "queue")))
1292 uc->data |= DEBUG_QUEUE;
1293 else if ((arg_len = is_keyword(ptr, len, "result")))
1294 uc->data |= DEBUG_RESULT;
1295 else if ((arg_len = is_keyword(ptr, len, "scatter")))
1296 uc->data |= DEBUG_SCATTER;
1297 else if ((arg_len = is_keyword(ptr, len, "script")))
1298 uc->data |= DEBUG_SCRIPT;
1299 else if ((arg_len = is_keyword(ptr, len, "tiny")))
1300 uc->data |= DEBUG_TINY;
1301 else if ((arg_len = is_keyword(ptr, len, "timing")))
1302 uc->data |= DEBUG_TIMING;
1303 else if ((arg_len = is_keyword(ptr, len, "nego")))
1304 uc->data |= DEBUG_NEGO;
1305 else if ((arg_len = is_keyword(ptr, len, "tags")))
1306 uc->data |= DEBUG_TAGS;
1307 else if ((arg_len = is_keyword(ptr, len, "pointer")))
1308 uc->data |= DEBUG_POINTER;
1311 ptr += arg_len; len -= arg_len;
1313 #ifdef DEBUG_PROC_INFO
1314 printk("sym_user_command: data=%ld\n", uc->data);
1317 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1320 SKIP_SPACES(ptr, len);
1321 if ((arg_len = is_keyword(ptr, len, "no_disc")))
1322 uc->data &= ~SYM_DISC_ENABLED;
1325 ptr += arg_len; len -= arg_len;
1335 unsigned long flags;
1337 spin_lock_irqsave(np->s.host->host_lock, flags);
1338 sym_exec_user_command (np, uc);
1339 spin_unlock_irqrestore(np->s.host->host_lock, flags);
1344 #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1347 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1349 * Informations through the proc file system.
1358 static void copy_mem_info(struct info_str *info, char *data, int len)
1360 if (info->pos + len > info->length)
1361 len = info->length - info->pos;
1363 if (info->pos + len < info->offset) {
1367 if (info->pos < info->offset) {
1368 data += (info->offset - info->pos);
1369 len -= (info->offset - info->pos);
1373 memcpy(info->buffer + info->pos, data, len);
1378 static int copy_info(struct info_str *info, char *fmt, ...)
1384 va_start(args, fmt);
1385 len = vsprintf(buf, fmt, args);
1388 copy_mem_info(info, buf, len);
1393 * Copy formatted information into the input buffer.
1395 static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
1397 struct info_str info;
1401 info.offset = offset;
1404 copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1405 "revision id 0x%x\n",
1406 np->s.chip_name, np->device_id, np->revision_id);
1407 copy_info(&info, "At PCI address %s, IRQ " IRQ_FMT "\n",
1408 pci_name(np->s.device), IRQ_PRM(np->s.irq));
1409 copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1410 (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1411 np->maxwide ? "Wide" : "Narrow",
1412 np->minsync_dt ? ", DT capable" : "");
1414 copy_info(&info, "Max. started commands %d, "
1415 "max. commands per LUN %d\n",
1416 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1418 return info.pos > info.offset? info.pos - info.offset : 0;
1420 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1423 * Entry point of the scsi proc fs of the driver.
1424 * - func = 0 means read (returns adapter infos)
1425 * - func = 1 means write (not yet merget from sym53c8xx)
1427 static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
1428 char **start, off_t offset, int length, int func)
1430 struct sym_hcb *np = sym_get_hcb(host);
1434 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1435 retv = sym_user_command(np, buffer, length);
1442 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1443 retv = sym_host_info(np, buffer, offset, length);
1451 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1454 * Free controller resources.
1456 static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
1459 * Free O/S specific resources.
1462 free_irq(np->s.irq, np);
1464 pci_iounmap(pdev, np->s.ioaddr);
1466 pci_iounmap(pdev, np->s.ramaddr);
1468 * Free O/S independent resources.
1472 sym_mfree_dma(np, sizeof(*np), "HCB");
1476 * Ask/tell the system about DMA addressing.
1478 static int sym_setup_bus_dma_mask(struct sym_hcb *np)
1480 #if SYM_CONF_DMA_ADDRESSING_MODE > 0
1481 #if SYM_CONF_DMA_ADDRESSING_MODE == 1
1482 #define DMA_DAC_MASK 0x000000ffffffffffULL /* 40-bit */
1483 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
1484 #define DMA_DAC_MASK DMA_64BIT_MASK
1486 if ((np->features & FE_DAC) &&
1487 !pci_set_dma_mask(np->s.device, DMA_DAC_MASK)) {
1493 if (!pci_set_dma_mask(np->s.device, DMA_32BIT_MASK))
1496 printf_warning("%s: No suitable DMA available\n", sym_name(np));
1501 * Host attach and initialisations.
1503 * Allocate host data and ncb structure.
1504 * Remap MMIO region.
1505 * Do chip initialization.
1506 * If all is OK, install interrupt handling and
1507 * start the timer daemon.
1509 static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
1510 int unit, struct sym_device *dev)
1512 struct host_data *host_data;
1513 struct sym_hcb *np = NULL;
1514 struct Scsi_Host *instance = NULL;
1515 struct pci_dev *pdev = dev->pdev;
1516 unsigned long flags;
1520 "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT "\n",
1521 unit, dev->chip.name, dev->chip.revision_id,
1522 pci_name(pdev), IRQ_PRM(pdev->irq));
1525 * Get the firmware for this chip.
1527 fw = sym_find_firmware(&dev->chip);
1532 * Allocate host_data structure
1534 instance = scsi_host_alloc(tpnt, sizeof(*host_data));
1537 host_data = (struct host_data *) instance->hostdata;
1540 * Allocate immediately the host control block,
1541 * since we are only expecting to succeed. :)
1542 * We keep track in the HCB of all the resources that
1543 * are to be released on error.
1545 np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
1548 np->s.device = pdev;
1549 np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
1550 host_data->ncb = np;
1551 np->s.host = instance;
1553 pci_set_drvdata(pdev, np);
1556 * Copy some useful infos to the HCB.
1558 np->hcb_ba = vtobus(np);
1559 np->verbose = sym_driver_setup.verbose;
1560 np->s.device = pdev;
1562 np->device_id = dev->chip.device_id;
1563 np->revision_id = dev->chip.revision_id;
1564 np->features = dev->chip.features;
1565 np->clock_divn = dev->chip.nr_divisor;
1566 np->maxoffs = dev->chip.offset_max;
1567 np->maxburst = dev->chip.burst_max;
1568 np->myaddr = dev->host_id;
1573 strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1574 sprintf(np->s.inst_name, "sym%d", np->s.unit);
1576 if (sym_setup_bus_dma_mask(np))
1580 * Try to map the controller chip to
1581 * virtual and physical memory.
1583 np->mmio_ba = (u32)dev->mmio_base;
1584 np->s.ioaddr = dev->s.ioaddr;
1585 np->s.ramaddr = dev->s.ramaddr;
1586 np->s.io_ws = (np->features & FE_IO256) ? 256 : 128;
1589 * Map on-chip RAM if present and supported.
1591 if (!(np->features & FE_RAM))
1593 if (dev->ram_base) {
1594 np->ram_ba = (u32)dev->ram_base;
1595 np->ram_ws = (np->features & FE_RAM8K) ? 8192 : 4096;
1598 if (sym_hcb_attach(instance, fw, dev->nvram))
1602 * Install the interrupt handler.
1603 * If we synchonize the C code with SCRIPTS on interrupt,
1604 * we do not want to share the INTR line at all.
1606 if (request_irq(pdev->irq, sym53c8xx_intr, SA_SHIRQ, NAME53C8XX, np)) {
1607 printf_err("%s: request irq %d failure\n",
1608 sym_name(np), pdev->irq);
1611 np->s.irq = pdev->irq;
1614 * After SCSI devices have been opened, we cannot
1615 * reset the bus safely, so we do it here.
1617 spin_lock_irqsave(instance->host_lock, flags);
1618 if (sym_reset_scsi_bus(np, 0))
1622 * Start the SCRIPTS.
1624 sym_start_up (np, 1);
1627 * Start the timer daemon
1629 init_timer(&np->s.timer);
1630 np->s.timer.data = (unsigned long) np;
1631 np->s.timer.function = sym53c8xx_timer;
1636 * Fill Linux host instance structure
1637 * and return success.
1639 instance->max_channel = 0;
1640 instance->this_id = np->myaddr;
1641 instance->max_id = np->maxwide ? 16 : 8;
1642 instance->max_lun = SYM_CONF_MAX_LUN;
1643 instance->unique_id = pci_resource_start(pdev, 0);
1644 instance->cmd_per_lun = SYM_CONF_MAX_TAG;
1645 instance->can_queue = (SYM_CONF_MAX_START-2);
1646 instance->sg_tablesize = SYM_CONF_MAX_SG;
1647 instance->max_cmd_len = 16;
1648 BUG_ON(sym2_transport_template == NULL);
1649 instance->transportt = sym2_transport_template;
1651 spin_unlock_irqrestore(instance->host_lock, flags);
1656 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1657 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1658 spin_unlock_irqrestore(instance->host_lock, flags);
1662 printf_info("%s: giving up ...\n", sym_name(np));
1664 sym_free_resources(np, pdev);
1665 scsi_host_put(instance);
1672 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1674 #if SYM_CONF_NVRAM_SUPPORT
1675 static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1678 devp->device_id = devp->chip.device_id;
1681 sym_read_nvram(devp, nvp);
1684 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1687 #endif /* SYM_CONF_NVRAM_SUPPORT */
1689 static int __devinit sym_check_supported(struct sym_device *device)
1691 struct sym_chip *chip;
1692 struct pci_dev *pdev = device->pdev;
1694 unsigned long io_port = pci_resource_start(pdev, 0);
1698 * If user excluded this chip, do not initialize it.
1699 * I hate this code so much. Must kill it.
1702 for (i = 0 ; i < 8 ; i++) {
1703 if (sym_driver_setup.excludes[i] == io_port)
1709 * Check if the chip is supported. Then copy the chip description
1710 * to our device structure so we can make it match the actual device
1713 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1714 chip = sym_lookup_chip_table(pdev->device, revision);
1716 dev_info(&pdev->dev, "device not supported\n");
1719 memcpy(&device->chip, chip, sizeof(device->chip));
1720 device->chip.revision_id = revision;
1726 * Ignore Symbios chips controlled by various RAID controllers.
1727 * These controllers set value 0x52414944 at RAM end - 16.
1729 static int __devinit sym_check_raid(struct sym_device *device)
1731 unsigned int ram_size, ram_val;
1733 if (!device->s.ramaddr)
1736 if (device->chip.features & FE_RAM8K)
1741 ram_val = readl(device->s.ramaddr + ram_size - 16);
1742 if (ram_val != 0x52414944)
1745 dev_info(&device->pdev->dev,
1746 "not initializing, driven by RAID controller.\n");
1750 static int __devinit sym_set_workarounds(struct sym_device *device)
1752 struct sym_chip *chip = &device->chip;
1753 struct pci_dev *pdev = device->pdev;
1757 * (ITEM 12 of a DEL about the 896 I haven't yet).
1758 * We must ensure the chip will use WRITE AND INVALIDATE.
1759 * The revision number limit is for now arbitrary.
1761 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && chip->revision_id < 0x4) {
1762 chip->features |= (FE_WRIE | FE_CLSE);
1765 /* If the chip can do Memory Write Invalidate, enable it */
1766 if (chip->features & FE_WRIE) {
1767 if (pci_set_mwi(pdev))
1772 * Work around for errant bit in 895A. The 66Mhz
1773 * capable bit is set erroneously. Clear this bit.
1776 * Make sure Config space and Features agree.
1778 * Recall: writes are not normal to status register -
1779 * write a 1 to clear and a 0 to leave unchanged.
1780 * Can only reset bits.
1782 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1783 if (chip->features & FE_66MHZ) {
1784 if (!(status_reg & PCI_STATUS_66MHZ))
1785 chip->features &= ~FE_66MHZ;
1787 if (status_reg & PCI_STATUS_66MHZ) {
1788 status_reg = PCI_STATUS_66MHZ;
1789 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1790 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1798 * Read and check the PCI configuration for any detected NCR
1799 * boards and save data for attaching after all boards have
1802 static void __devinit
1803 sym_init_device(struct pci_dev *pdev, struct sym_device *device)
1807 device->host_id = SYM_SETUP_HOST_ID;
1808 device->pdev = pdev;
1810 i = pci_get_base_address(pdev, 1, &device->mmio_base);
1811 pci_get_base_address(pdev, i, &device->ram_base);
1813 #ifndef CONFIG_SCSI_SYM53C8XX_IOMAPPED
1814 if (device->mmio_base)
1815 device->s.ioaddr = pci_iomap(pdev, 1,
1816 pci_resource_len(pdev, 1));
1818 if (!device->s.ioaddr)
1819 device->s.ioaddr = pci_iomap(pdev, 0,
1820 pci_resource_len(pdev, 0));
1821 if (device->ram_base)
1822 device->s.ramaddr = pci_iomap(pdev, i,
1823 pci_resource_len(pdev, i));
1827 * The NCR PQS and PDS cards are constructed as a DEC bridge
1828 * behind which sits a proprietary NCR memory controller and
1829 * either four or two 53c875s as separate devices. We can tell
1830 * if an 875 is part of a PQS/PDS or not since if it is, it will
1831 * be on the same bus as the memory controller. In its usual
1832 * mode of operation, the 875s are slaved to the memory
1833 * controller for all transfers. To operate with the Linux
1834 * driver, the memory controller is disabled and the 875s
1835 * freed to function independently. The only wrinkle is that
1836 * the preset SCSI ID (which may be zero) must be read in from
1837 * a special configuration space register of the 875.
1839 static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
1844 for (slot = 0; slot < 256; slot++) {
1845 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
1847 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
1852 /* bit 1: allow individual 875 configuration */
1853 pci_read_config_byte(memc, 0x44, &tmp);
1854 if ((tmp & 0x2) == 0) {
1856 pci_write_config_byte(memc, 0x44, tmp);
1859 /* bit 2: drive individual 875 interrupts to the bus */
1860 pci_read_config_byte(memc, 0x45, &tmp);
1861 if ((tmp & 0x4) == 0) {
1863 pci_write_config_byte(memc, 0x45, tmp);
1870 pci_read_config_byte(pdev, 0x84, &tmp);
1871 sym_dev->host_id = tmp;
1875 * Called before unloading the module.
1877 * We have to free resources and halt the NCR chip.
1879 static int sym_detach(struct sym_hcb *np, struct pci_dev *pdev)
1881 printk("%s: detaching ...\n", sym_name(np));
1883 del_timer_sync(&np->s.timer);
1887 * We should use sym_soft_reset(), but we don't want to do
1888 * so, since we may not be safe if interrupts occur.
1890 printk("%s: resetting chip\n", sym_name(np));
1891 OUTB(np, nc_istat, SRST);
1894 OUTB(np, nc_istat, 0);
1896 sym_free_resources(np, pdev);
1902 * Driver host template.
1904 static struct scsi_host_template sym2_template = {
1905 .module = THIS_MODULE,
1906 .name = "sym53c8xx",
1907 .info = sym53c8xx_info,
1908 .queuecommand = sym53c8xx_queue_command,
1909 .slave_alloc = sym53c8xx_slave_alloc,
1910 .slave_configure = sym53c8xx_slave_configure,
1911 .eh_abort_handler = sym53c8xx_eh_abort_handler,
1912 .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
1913 .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
1914 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
1916 .use_clustering = DISABLE_CLUSTERING,
1917 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1918 .proc_info = sym53c8xx_proc_info,
1919 .proc_name = NAME53C8XX,
1923 static int attach_count;
1925 static int __devinit sym2_probe(struct pci_dev *pdev,
1926 const struct pci_device_id *ent)
1928 struct sym_device sym_dev;
1929 struct sym_nvram nvram;
1930 struct Scsi_Host *instance;
1932 memset(&sym_dev, 0, sizeof(sym_dev));
1933 memset(&nvram, 0, sizeof(nvram));
1935 if (pci_enable_device(pdev))
1938 pci_set_master(pdev);
1940 if (pci_request_regions(pdev, NAME53C8XX))
1943 sym_init_device(pdev, &sym_dev);
1944 if (sym_check_supported(&sym_dev))
1947 if (sym_check_raid(&sym_dev))
1948 goto leave; /* Don't disable the device */
1950 if (sym_set_workarounds(&sym_dev))
1953 sym_config_pqs(pdev, &sym_dev);
1955 sym_get_nvram(&sym_dev, &nvram);
1957 instance = sym_attach(&sym2_template, attach_count, &sym_dev);
1961 if (scsi_add_host(instance, &pdev->dev))
1963 scsi_scan_host(instance);
1970 sym_detach(pci_get_drvdata(pdev), pdev);
1972 pci_release_regions(pdev);
1974 pci_disable_device(pdev);
1979 static void __devexit sym2_remove(struct pci_dev *pdev)
1981 struct sym_hcb *np = pci_get_drvdata(pdev);
1982 struct Scsi_Host *host = np->s.host;
1984 scsi_remove_host(host);
1985 scsi_host_put(host);
1987 sym_detach(np, pdev);
1989 pci_release_regions(pdev);
1990 pci_disable_device(pdev);
1995 static void sym2_get_signalling(struct Scsi_Host *shost)
1997 struct sym_hcb *np = sym_get_hcb(shost);
1998 enum spi_signal_type type;
2000 switch (np->scsi_mode) {
2002 type = SPI_SIGNAL_SE;
2005 type = SPI_SIGNAL_LVD;
2008 type = SPI_SIGNAL_HVD;
2011 type = SPI_SIGNAL_UNKNOWN;
2014 spi_signalling(shost) = type;
2017 static void sym2_set_offset(struct scsi_target *starget, int offset)
2019 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2020 struct sym_hcb *np = sym_get_hcb(shost);
2021 struct sym_tcb *tp = &np->target[starget->id];
2023 tp->tgoal.offset = offset;
2024 tp->tgoal.check_nego = 1;
2027 static void sym2_set_period(struct scsi_target *starget, int period)
2029 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2030 struct sym_hcb *np = sym_get_hcb(shost);
2031 struct sym_tcb *tp = &np->target[starget->id];
2033 /* have to have DT for these transfers, but DT will also
2034 * set width, so check that this is allowed */
2035 if (period <= np->minsync && spi_width(starget))
2038 tp->tgoal.period = period;
2039 tp->tgoal.check_nego = 1;
2042 static void sym2_set_width(struct scsi_target *starget, int width)
2044 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2045 struct sym_hcb *np = sym_get_hcb(shost);
2046 struct sym_tcb *tp = &np->target[starget->id];
2048 /* It is illegal to have DT set on narrow transfers. If DT is
2049 * clear, we must also clear IU and QAS. */
2051 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
2053 tp->tgoal.width = width;
2054 tp->tgoal.check_nego = 1;
2057 static void sym2_set_dt(struct scsi_target *starget, int dt)
2059 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2060 struct sym_hcb *np = sym_get_hcb(shost);
2061 struct sym_tcb *tp = &np->target[starget->id];
2063 /* We must clear QAS and IU if DT is clear */
2067 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
2068 tp->tgoal.check_nego = 1;
2071 static void sym2_set_iu(struct scsi_target *starget, int iu)
2073 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2074 struct sym_hcb *np = sym_get_hcb(shost);
2075 struct sym_tcb *tp = &np->target[starget->id];
2078 tp->tgoal.iu = tp->tgoal.dt = 1;
2081 tp->tgoal.check_nego = 1;
2084 static void sym2_set_qas(struct scsi_target *starget, int qas)
2086 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2087 struct sym_hcb *np = sym_get_hcb(shost);
2088 struct sym_tcb *tp = &np->target[starget->id];
2091 tp->tgoal.dt = tp->tgoal.qas = 1;
2094 tp->tgoal.check_nego = 1;
2098 static struct spi_function_template sym2_transport_functions = {
2099 .set_offset = sym2_set_offset,
2101 .set_period = sym2_set_period,
2103 .set_width = sym2_set_width,
2105 .set_dt = sym2_set_dt,
2107 .set_iu = sym2_set_iu,
2109 .set_qas = sym2_set_qas,
2111 .get_signalling = sym2_get_signalling,
2114 static struct pci_device_id sym2_id_table[] __devinitdata = {
2115 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
2116 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2117 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
2118 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2119 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
2120 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2121 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
2122 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2123 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
2124 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2125 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
2126 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2127 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
2128 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2129 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
2130 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2131 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
2132 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2133 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2134 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2135 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2136 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2137 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
2138 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2139 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2140 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2141 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2142 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2143 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2144 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2145 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2146 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2147 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2148 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2152 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2154 static struct pci_driver sym2_driver = {
2156 .id_table = sym2_id_table,
2157 .probe = sym2_probe,
2158 .remove = __devexit_p(sym2_remove),
2161 static int __init sym2_init(void)
2165 sym2_setup_params();
2166 sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2167 if (!sym2_transport_template)
2170 error = pci_register_driver(&sym2_driver);
2172 spi_release_transport(sym2_transport_template);
2176 static void __exit sym2_exit(void)
2178 pci_unregister_driver(&sym2_driver);
2179 spi_release_transport(sym2_transport_template);
2182 module_init(sym2_init);
2183 module_exit(sym2_exit);