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-2004 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>
50 #include <scsi/scsi_transport_spi.h>
53 #include "sym_nvram.h"
55 #define NAME53C "sym53c"
56 #define NAME53C8XX "sym53c8xx"
58 /* SPARC just has to be different ... */
61 #define IRQ_PRM(x) __irq_itoa(x)
64 #define IRQ_PRM(x) (x)
67 struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
68 unsigned int sym_debug_flags = 0;
70 static char *excl_string;
71 static char *safe_string;
72 module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
73 module_param_string(tag_ctrl, sym_driver_setup.tag_ctrl, 100, 0);
74 module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
75 module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
76 module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
77 module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
78 module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
79 module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
80 module_param_named(verb, sym_driver_setup.verbose, byte, 0);
81 module_param_named(debug, sym_debug_flags, uint, 0);
82 module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
83 module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
84 module_param_named(excl, excl_string, charp, 0);
85 module_param_named(safe, safe_string, charp, 0);
87 MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
88 MODULE_PARM_DESC(tag_ctrl, "More detailed control over tags per LUN");
89 MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
90 MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
91 MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
92 MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
93 MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
94 MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
95 MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
96 MODULE_PARM_DESC(debug, "Set bits to enable debugging");
97 MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
98 MODULE_PARM_DESC(nvram, "Option currently not used");
99 MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
100 MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
102 MODULE_LICENSE("GPL");
103 MODULE_VERSION(SYM_VERSION);
104 MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
105 MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
107 static void sym2_setup_params(void)
109 char *p = excl_string;
112 while (p && (xi < 8)) {
114 int val = (int) simple_strtoul(p, &next_p, 0);
115 sym_driver_setup.excludes[xi++] = val;
120 if (*safe_string == 'y') {
121 sym_driver_setup.max_tag = 0;
122 sym_driver_setup.burst_order = 0;
123 sym_driver_setup.scsi_led = 0;
124 sym_driver_setup.scsi_diff = 1;
125 sym_driver_setup.irq_mode = 0;
126 sym_driver_setup.scsi_bus_check = 2;
127 sym_driver_setup.host_id = 7;
128 sym_driver_setup.verbose = 2;
129 sym_driver_setup.settle_delay = 10;
130 sym_driver_setup.use_nvram = 1;
131 } else if (*safe_string != 'n') {
132 printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
133 " passed to safe option", safe_string);
139 pci_get_base_address(struct pci_dev *pdev, int index, u_long *base)
142 #define PCI_BAR_OFFSET(index) (PCI_BASE_ADDRESS_0 + (index<<2))
144 pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
147 if ((tmp & 0x7) == PCI_BASE_ADDRESS_MEM_TYPE_64) {
148 #if BITS_PER_LONG > 32
149 pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
150 *base |= (((u_long)tmp) << 32);
155 #undef PCI_BAR_OFFSET
158 /* This lock protects only the memory allocation/free. */
159 static DEFINE_SPINLOCK(sym53c8xx_lock);
161 static struct scsi_transport_template *sym2_transport_template = NULL;
164 * Wrappers to the generic memory allocator.
166 void *sym_calloc(int size, char *name)
170 spin_lock_irqsave(&sym53c8xx_lock, flags);
171 m = sym_calloc_unlocked(size, name);
172 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
176 void sym_mfree(void *m, int size, char *name)
179 spin_lock_irqsave(&sym53c8xx_lock, flags);
180 sym_mfree_unlocked(m, size, name);
181 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
184 void *__sym_calloc_dma(m_pool_ident_t dev_dmat, int size, char *name)
188 spin_lock_irqsave(&sym53c8xx_lock, flags);
189 m = __sym_calloc_dma_unlocked(dev_dmat, size, name);
190 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
194 void __sym_mfree_dma(m_pool_ident_t dev_dmat, void *m, int size, char *name)
197 spin_lock_irqsave(&sym53c8xx_lock, flags);
198 __sym_mfree_dma_unlocked(dev_dmat, m, size, name);
199 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
202 m_addr_t __vtobus(m_pool_ident_t dev_dmat, void *m)
206 spin_lock_irqsave(&sym53c8xx_lock, flags);
207 b = __vtobus_unlocked(dev_dmat, m);
208 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
213 * Used by the eh thread to wait for command completion.
214 * It is allocated on the eh thread stack.
217 struct completion done;
218 struct timer_list timer;
219 void (*old_done)(struct scsi_cmnd *);
225 * Driver private area in the SCSI command structure.
227 struct sym_ucmd { /* Override the SCSI pointer structure */
228 dma_addr_t data_mapping;
230 struct sym_eh_wait *eh_wait;
233 #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
234 #define SYM_SCMD_PTR(ucmd) sym_que_entry(ucmd, struct scsi_cmnd, SCp)
235 #define SYM_SOFTC_PTR(cmd) (((struct host_data *)cmd->device->host->hostdata)->ncb)
237 static void __unmap_scsi_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
239 int dma_dir = cmd->sc_data_direction;
241 switch(SYM_UCMD_PTR(cmd)->data_mapped) {
243 pci_unmap_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
246 pci_unmap_single(pdev, SYM_UCMD_PTR(cmd)->data_mapping,
247 cmd->request_bufflen, dma_dir);
250 SYM_UCMD_PTR(cmd)->data_mapped = 0;
253 static dma_addr_t __map_scsi_single_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
256 int dma_dir = cmd->sc_data_direction;
258 mapping = pci_map_single(pdev, cmd->request_buffer,
259 cmd->request_bufflen, dma_dir);
261 SYM_UCMD_PTR(cmd)->data_mapped = 1;
262 SYM_UCMD_PTR(cmd)->data_mapping = mapping;
268 static int __map_scsi_sg_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
271 int dma_dir = cmd->sc_data_direction;
273 use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
275 SYM_UCMD_PTR(cmd)->data_mapped = 2;
276 SYM_UCMD_PTR(cmd)->data_mapping = use_sg;
282 #define unmap_scsi_data(np, cmd) \
283 __unmap_scsi_data(np->s.device, cmd)
284 #define map_scsi_single_data(np, cmd) \
285 __map_scsi_single_data(np->s.device, cmd)
286 #define map_scsi_sg_data(np, cmd) \
287 __map_scsi_sg_data(np->s.device, cmd)
289 * Complete a pending CAM CCB.
291 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *ccb)
293 unmap_scsi_data(np, ccb);
297 static void sym_xpt_done2(struct sym_hcb *np, struct scsi_cmnd *ccb, int cam_status)
299 sym_set_cam_status(ccb, cam_status);
300 sym_xpt_done(np, ccb);
305 * Print something that identifies the IO.
307 void sym_print_addr(struct sym_ccb *cp)
309 struct scsi_cmnd *cmd = cp->cam_ccb;
311 printf("%s:%d:%d:", sym_name(SYM_SOFTC_PTR(cmd)),
312 cmd->device->id, cmd->device->lun);
316 * Tell the SCSI layer about a BUS RESET.
318 void sym_xpt_async_bus_reset(struct sym_hcb *np)
320 printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
321 np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
322 np->s.settle_time_valid = 1;
323 if (sym_verbose >= 2)
324 printf_info("%s: command processing suspended for %d seconds\n",
325 sym_name(np), sym_driver_setup.settle_delay);
329 * Tell the SCSI layer about a BUS DEVICE RESET message sent.
331 void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
333 printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
337 * Tell the SCSI layer about the new transfer parameters.
339 void sym_xpt_async_nego_wide(struct sym_hcb *np, int target)
343 sym_announce_transfer_rate(np, target);
347 * Choose the more appropriate CAM status if
348 * the IO encountered an extended error.
350 static int sym_xerr_cam_status(int cam_status, int x_status)
353 if (x_status & XE_PARITY_ERR)
354 cam_status = DID_PARITY;
355 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
356 cam_status = DID_ERROR;
357 else if (x_status & XE_BAD_PHASE)
358 cam_status = DID_ERROR;
360 cam_status = DID_ERROR;
366 * Build CAM result for a failed or auto-sensed IO.
368 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
370 struct scsi_cmnd *csio = cp->cam_ccb;
371 u_int cam_status, scsi_status, drv_status;
375 scsi_status = cp->ssss_status;
377 if (cp->host_flags & HF_SENSE) {
378 scsi_status = cp->sv_scsi_status;
379 resid = cp->sv_resid;
380 if (sym_verbose && cp->sv_xerr_status)
381 sym_print_xerr(cp, cp->sv_xerr_status);
382 if (cp->host_status == HS_COMPLETE &&
383 cp->ssss_status == S_GOOD &&
384 cp->xerr_status == 0) {
385 cam_status = sym_xerr_cam_status(DID_OK,
387 drv_status = DRIVER_SENSE;
389 * Bounce back the sense data to user.
391 memset(&csio->sense_buffer, 0, sizeof(csio->sense_buffer));
392 memcpy(csio->sense_buffer, cp->sns_bbuf,
393 min(sizeof(csio->sense_buffer),
394 (size_t)SYM_SNS_BBUF_LEN));
397 * If the device reports a UNIT ATTENTION condition
398 * due to a RESET condition, we should consider all
399 * disconnect CCBs for this unit as aborted.
403 p = (u_char *) csio->sense_data;
404 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
405 sym_clear_tasks(np, DID_ABORT,
406 cp->target,cp->lun, -1);
411 * Error return from our internal request sense. This
412 * is bad: we must clear the contingent allegiance
413 * condition otherwise the device will always return
414 * BUSY. Use a big stick.
416 sym_reset_scsi_target(np, csio->device->id);
417 cam_status = DID_ERROR;
419 } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
421 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
422 cam_status = DID_NO_CONNECT;
423 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
424 cam_status = DID_ERROR;
425 else { /* Extended error */
428 printf ("COMMAND FAILED (%x %x %x).\n",
429 cp->host_status, cp->ssss_status,
433 * Set the most appropriate value for CAM status.
435 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
438 csio->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
443 * Build the scatter/gather array for an I/O.
446 static int sym_scatter_no_sglist(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
448 struct sym_tblmove *data = &cp->phys.data[SYM_CONF_MAX_SG-1];
451 cp->data_len = cmd->request_bufflen;
453 if (cmd->request_bufflen) {
454 dma_addr_t baddr = map_scsi_single_data(np, cmd);
456 sym_build_sge(np, data, baddr, cmd->request_bufflen);
468 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
471 int use_sg = (int) cmd->use_sg;
476 segment = sym_scatter_no_sglist(np, cp, cmd);
477 else if ((use_sg = map_scsi_sg_data(np, cmd)) > 0) {
478 struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
479 struct sym_tblmove *data;
481 if (use_sg > SYM_CONF_MAX_SG) {
482 unmap_scsi_data(np, cmd);
486 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
488 for (segment = 0; segment < use_sg; segment++) {
489 dma_addr_t baddr = sg_dma_address(&scatter[segment]);
490 unsigned int len = sg_dma_len(&scatter[segment]);
492 sym_build_sge(np, &data[segment], baddr, len);
503 * Queue a SCSI command.
505 static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *ccb)
507 /* struct scsi_device *device = ccb->device; */
514 * Minimal checkings, so that we will not
515 * go outside our tables.
517 if (ccb->device->id == np->myaddr ||
518 ccb->device->id >= SYM_CONF_MAX_TARGET ||
519 ccb->device->lun >= SYM_CONF_MAX_LUN) {
520 sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
525 * Retrieve the target descriptor.
527 tp = &np->target[ccb->device->id];
530 * Complete the 1st INQUIRY command with error
531 * condition if the device is flagged NOSCAN
532 * at BOOT in the NVRAM. This may speed up
533 * the boot and maintain coherency with BIOS
534 * device numbering. Clearing the flag allows
535 * user to rescan skipped devices later.
536 * We also return error for devices not flagged
537 * for SCAN LUNS in the NVRAM since some mono-lun
538 * devices behave badly when asked for some non
539 * zero LUN. Btw, this is an absolute hack.:-)
541 if (ccb->cmnd[0] == 0x12 || ccb->cmnd[0] == 0x0) {
542 if ((tp->usrflags & SYM_SCAN_BOOT_DISABLED) ||
543 ((tp->usrflags & SYM_SCAN_LUNS_DISABLED) &&
544 ccb->device->lun != 0)) {
545 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
546 sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
552 * Select tagged/untagged.
554 lp = sym_lp(np, tp, ccb->device->lun);
555 order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
560 cp = sym_get_ccb(np, ccb->device->id, ccb->device->lun, order);
562 return 1; /* Means resource shortage */
563 sym_queue_scsiio(np, ccb, cp);
568 * Setup buffers and pointers that address the CDB.
570 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *ccb, struct sym_ccb *cp)
576 * CDB is 16 bytes max.
578 if (ccb->cmd_len > sizeof(cp->cdb_buf)) {
579 sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID);
583 memcpy(cp->cdb_buf, ccb->cmnd, ccb->cmd_len);
584 cmd_ba = CCB_BA (cp, cdb_buf[0]);
585 cmd_len = ccb->cmd_len;
587 cp->phys.cmd.addr = cpu_to_scr(cmd_ba);
588 cp->phys.cmd.size = cpu_to_scr(cmd_len);
594 * Setup pointers that address the data and start the I/O.
596 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *csio, struct sym_ccb *cp)
599 struct sym_tcb *tp = &np->target[cp->target];
600 struct sym_lcb *lp = sym_lp(np, tp, cp->lun);
605 if (sym_setup_cdb(np, csio, cp))
609 * No direction means no data.
611 dir = csio->sc_data_direction;
612 if (dir != DMA_NONE) {
613 cp->segments = sym_scatter(np, cp, csio);
614 if (cp->segments < 0) {
615 if (cp->segments == -2)
616 sym_set_cam_status(csio, CAM_RESRC_UNAVAIL);
618 sym_set_cam_status(csio, CAM_REQ_TOO_BIG);
629 sym_setup_data_pointers(np, cp, dir);
632 * When `#ifed 1', the code below makes the driver
633 * panic on the first attempt to write to a SCSI device.
634 * It is the first test we want to do after a driver
635 * change that does not seem obviously safe. :)
638 switch (cp->cdb_buf[0]) {
639 case 0x0A: case 0x2A: case 0xAA:
640 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
651 sym_start_next_ccbs(np, lp, 2);
653 sym_put_start_queue(np, cp);
657 sym_free_ccb(np, cp);
658 sym_xpt_done(np, csio);
666 * Misused to keep the driver running when
667 * interrupts are not configured correctly.
669 static void sym_timer(struct sym_hcb *np)
671 unsigned long thistime = jiffies;
676 np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
677 add_timer(&np->s.timer);
680 * If we are resetting the ncr, wait for settle_time before
681 * clearing it. Then command processing will be resumed.
683 if (np->s.settle_time_valid) {
684 if (time_before_eq(np->s.settle_time, thistime)) {
685 if (sym_verbose >= 2 )
686 printk("%s: command processing resumed\n",
688 np->s.settle_time_valid = 0;
694 * Nothing to do for now, but that may come.
696 if (np->s.lasttime + 4*HZ < thistime) {
697 np->s.lasttime = thistime;
700 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
702 * Some way-broken PCI bridges may lead to
703 * completions being lost when the clearing
704 * of the INTFLY flag by the CPU occurs
705 * concurrently with the chip raising this flag.
706 * If this ever happen, lost completions will
715 * PCI BUS error handler.
717 void sym_log_bus_error(struct sym_hcb *np)
720 pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
721 if (pci_sts & 0xf900) {
722 pci_write_config_word(np->s.device, PCI_STATUS, pci_sts);
723 printf("%s: PCI STATUS = 0x%04x\n",
724 sym_name(np), pci_sts & 0xf900);
729 * queuecommand method. Entered with the host adapter lock held and
730 * interrupts disabled.
732 static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
733 void (*done)(struct scsi_cmnd *))
735 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
736 struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
739 cmd->scsi_done = done;
740 cmd->host_scribble = NULL;
741 memset(ucp, 0, sizeof(*ucp));
744 * Shorten our settle_time if needed for
745 * this command not to time out.
747 if (np->s.settle_time_valid && cmd->timeout_per_command) {
748 unsigned long tlimit = jiffies + cmd->timeout_per_command;
749 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
750 if (time_after(np->s.settle_time, tlimit)) {
751 np->s.settle_time = tlimit;
755 if (np->s.settle_time_valid)
756 return SCSI_MLQUEUE_HOST_BUSY;
758 sts = sym_queue_command(np, cmd);
760 return SCSI_MLQUEUE_HOST_BUSY;
765 * Linux entry point of the interrupt handler.
767 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
770 struct sym_hcb *np = (struct sym_hcb *)dev_id;
772 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
774 spin_lock_irqsave(np->s.host->host_lock, flags);
776 spin_unlock_irqrestore(np->s.host->host_lock, flags);
778 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
784 * Linux entry point of the timer handler
786 static void sym53c8xx_timer(unsigned long npref)
788 struct sym_hcb *np = (struct sym_hcb *)npref;
791 spin_lock_irqsave(np->s.host->host_lock, flags);
793 spin_unlock_irqrestore(np->s.host->host_lock, flags);
798 * What the eh thread wants us to perform.
800 #define SYM_EH_ABORT 0
801 #define SYM_EH_DEVICE_RESET 1
802 #define SYM_EH_BUS_RESET 2
803 #define SYM_EH_HOST_RESET 3
806 * What we will do regarding the involved SCSI command.
808 #define SYM_EH_DO_IGNORE 0
809 #define SYM_EH_DO_COMPLETE 1
810 #define SYM_EH_DO_WAIT 2
813 * Our general completion handler.
815 static void __sym_eh_done(struct scsi_cmnd *cmd, int timed_out)
817 struct sym_eh_wait *ep = SYM_UCMD_PTR(cmd)->eh_wait;
821 /* Try to avoid a race here (not 100% safe) */
824 if (ep->to_do == SYM_EH_DO_WAIT && !del_timer(&ep->timer))
828 /* Revert everything */
829 SYM_UCMD_PTR(cmd)->eh_wait = NULL;
830 cmd->scsi_done = ep->old_done;
832 /* Wake up the eh thread if it wants to sleep */
833 if (ep->to_do == SYM_EH_DO_WAIT)
838 * scsi_done() alias when error recovery is in progress.
840 static void sym_eh_done(struct scsi_cmnd *cmd) { __sym_eh_done(cmd, 0); }
843 * Some timeout handler to avoid waiting too long.
845 static void sym_eh_timeout(u_long p) { __sym_eh_done((struct scsi_cmnd *)p, 1); }
848 * Generic method for our eh processing.
849 * The 'op' argument tells what we have to do.
851 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
853 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
855 int to_do = SYM_EH_DO_IGNORE;
857 struct sym_eh_wait eh, *ep = &eh;
860 sprintf(devname, "%s:%d:%d", sym_name(np), cmd->device->id, cmd->device->lun);
862 printf_warning("%s: %s operation started.\n", devname, opname);
865 /* This one should be the result of some race, thus to ignore */
866 if (cmd->serial_number != cmd->serial_number_at_timeout)
870 /* This one is queued in some place -> to wait for completion */
871 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
872 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
873 if (cp->cam_ccb == cmd) {
874 to_do = SYM_EH_DO_WAIT;
880 /* Prepare stuff to either ignore, complete or wait for completion */
883 case SYM_EH_DO_IGNORE:
886 init_completion(&ep->done);
888 case SYM_EH_DO_COMPLETE:
889 ep->old_done = cmd->scsi_done;
890 cmd->scsi_done = sym_eh_done;
891 SYM_UCMD_PTR(cmd)->eh_wait = ep;
894 /* Try to proceed the operation we have been asked for */
898 sts = sym_abort_scsiio(np, cmd, 1);
900 case SYM_EH_DEVICE_RESET:
901 sts = sym_reset_scsi_target(np, cmd->device->id);
903 case SYM_EH_BUS_RESET:
904 sym_reset_scsi_bus(np, 1);
907 case SYM_EH_HOST_RESET:
908 sym_reset_scsi_bus(np, 0);
909 sym_start_up (np, 1);
916 /* On error, restore everything and cross fingers :) */
918 SYM_UCMD_PTR(cmd)->eh_wait = NULL;
919 cmd->scsi_done = ep->old_done;
920 to_do = SYM_EH_DO_IGNORE;
924 /* Complete the command with locks held as required by the driver */
925 if (to_do == SYM_EH_DO_COMPLETE)
926 sym_xpt_done2(np, cmd, CAM_REQ_ABORTED);
928 /* Wait for completion with locks released, as required by kernel */
929 if (to_do == SYM_EH_DO_WAIT) {
930 init_timer(&ep->timer);
931 ep->timer.expires = jiffies + (5*HZ);
932 ep->timer.function = sym_eh_timeout;
933 ep->timer.data = (u_long)cmd;
934 ep->timed_out = 1; /* Be pessimistic for once :) */
935 add_timer(&ep->timer);
936 spin_unlock_irq(np->s.host->host_lock);
937 wait_for_completion(&ep->done);
938 spin_lock_irq(np->s.host->host_lock);
942 printf_warning("%s: %s operation %s.\n", devname, opname,
943 sts==0?"complete":sts==-2?"timed-out":"failed");
944 return sts? SCSI_FAILED : SCSI_SUCCESS;
949 * Error handlers called from the eh thread (one thread per HBA).
951 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
953 return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
956 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
958 return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
961 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
963 return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
966 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
968 return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
972 * Tune device queuing depth, according to various limits.
974 static void sym_tune_dev_queuing(struct sym_hcb *np, int target, int lun, u_short reqtags)
976 struct sym_tcb *tp = &np->target[target];
977 struct sym_lcb *lp = sym_lp(np, tp, lun);
983 oldtags = lp->s.reqtags;
985 if (reqtags > lp->s.scdev_depth)
986 reqtags = lp->s.scdev_depth;
988 lp->started_limit = reqtags ? reqtags : 2;
990 lp->s.reqtags = reqtags;
992 if (reqtags != oldtags) {
993 printf_info("%s:%d:%d: "
994 "tagged command queuing %s, command queue depth %d.\n",
995 sym_name(np), target, lun,
996 lp->s.reqtags ? "enabled" : "disabled",
1002 * Linux select queue depths function
1004 #define DEF_DEPTH (sym_driver_setup.max_tag)
1005 #define ALL_TARGETS -2
1006 #define NO_TARGET -1
1010 static int device_queue_depth(struct sym_hcb *np, int target, int lun)
1013 char *p = sym_driver_setup.tag_ctrl;
1019 while ((c = *p++) != 0) {
1020 v = simple_strtoul(p, &ep, 0);
1029 t = (target == v) ? v : NO_TARGET;
1034 u = (lun == v) ? v : NO_LUN;
1037 if (h == np->s.unit &&
1038 (t == ALL_TARGETS || t == target) &&
1039 (u == ALL_LUNS || u == lun))
1055 * Linux entry point for device queue sizing.
1057 static int sym53c8xx_slave_configure(struct scsi_device *device)
1059 struct Scsi_Host *host = device->host;
1063 int reqtags, depth_to_use;
1065 np = ((struct host_data *) host->hostdata)->ncb;
1066 tp = &np->target[device->id];
1070 * Allocate the LCB if not yet.
1071 * If it fail, we may well be in the sh*t. :)
1073 lp = sym_alloc_lcb(np, device->id, device->lun);
1080 lp->curr_flags = lp->user_flags;
1083 * Select queue depth from driver setup.
1084 * Donnot use more than configured by user.
1086 * Donnot use more than our maximum.
1088 reqtags = device_queue_depth(np, device->id, device->lun);
1089 if (reqtags > tp->usrtags)
1090 reqtags = tp->usrtags;
1091 if (!device->tagged_supported)
1093 #if 1 /* Avoid to locally queue commands for no good reasons */
1094 if (reqtags > SYM_CONF_MAX_TAG)
1095 reqtags = SYM_CONF_MAX_TAG;
1096 depth_to_use = (reqtags ? reqtags : 2);
1098 depth_to_use = (reqtags ? SYM_CONF_MAX_TAG : 2);
1100 scsi_adjust_queue_depth(device,
1101 (device->tagged_supported ?
1102 MSG_SIMPLE_TAG : 0),
1104 lp->s.scdev_depth = depth_to_use;
1105 sym_tune_dev_queuing(np, device->id, device->lun, reqtags);
1107 if (!spi_initial_dv(device->sdev_target))
1108 spi_dv_device(device);
1114 * Linux entry point for info() function
1116 static const char *sym53c8xx_info (struct Scsi_Host *host)
1118 return sym_driver_name();
1122 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1124 * Proc file system stuff
1126 * A read operation returns adapter information.
1127 * A write operation is a control command.
1128 * The string is parsed in the driver code and the command is passed
1129 * to the sym_usercmd() function.
1132 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1141 #define UC_SETSYNC 10
1142 #define UC_SETTAGS 11
1143 #define UC_SETDEBUG 12
1144 #define UC_SETWIDE 14
1145 #define UC_SETFLAG 15
1146 #define UC_SETVERBOSE 17
1147 #define UC_RESETDEV 18
1148 #define UC_CLEARDEV 19
1150 static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
1158 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1160 sym_debug_flags = uc->data;
1164 np->verbose = uc->data;
1168 * We assume that other commands apply to targets.
1169 * This should always be the case and avoid the below
1170 * 4 lines to be repeated 6 times.
1172 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
1173 if (!((uc->target >> t) & 1))
1175 tp = &np->target[t];
1180 if (!uc->data || uc->data >= 255) {
1181 tp->tinfo.goal.options = 0;
1182 tp->tinfo.goal.offset = 0;
1185 if (uc->data <= 9 && np->minsync_dt) {
1186 if (uc->data < np->minsync_dt)
1187 uc->data = np->minsync_dt;
1188 tp->tinfo.goal.options = PPR_OPT_MASK;
1189 tp->tinfo.goal.width = 1;
1190 tp->tinfo.goal.period = uc->data;
1191 tp->tinfo.goal.offset = np->maxoffs_dt;
1193 if (uc->data < np->minsync)
1194 uc->data = np->minsync;
1195 tp->tinfo.goal.options = 0;
1196 tp->tinfo.goal.period = uc->data;
1197 tp->tinfo.goal.offset = np->maxoffs;
1201 tp->tinfo.goal.width = uc->data ? 1 : 0;
1204 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
1205 sym_tune_dev_queuing(np, t,l, uc->data);
1209 np->istat_sem = SEM;
1210 OUTB (nc_istat, SIGP|SEM);
1213 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
1214 struct sym_lcb *lp = sym_lp(np, tp, l);
1215 if (lp) lp->to_clear = 1;
1217 np->istat_sem = SEM;
1218 OUTB (nc_istat, SIGP|SEM);
1221 tp->usrflags = uc->data;
1229 #define digit_to_bin(c) ((c) - '0')
1231 static int skip_spaces(char *ptr, int len)
1235 for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
1240 static int get_int_arg(char *ptr, int len, u_long *pv)
1245 for (v = 0, cnt = len; cnt > 0 && (c = *ptr++) && isdigit(c); cnt--) {
1246 v = (v * 10) + digit_to_bin(c);
1255 static int is_keyword(char *ptr, int len, char *verb)
1257 int verb_len = strlen(verb);
1259 if (len >= verb_len && !memcmp(verb, ptr, verb_len))
1266 #define SKIP_SPACES(min_spaces) \
1267 if ((arg_len = skip_spaces(ptr, len)) < (min_spaces)) \
1269 ptr += arg_len; len -= arg_len;
1271 #define GET_INT_ARG(v) \
1272 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
1274 ptr += arg_len; len -= arg_len;
1278 * Parse a control command
1281 static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
1285 struct sym_usrcmd cmd, *uc = &cmd;
1289 memset(uc, 0, sizeof(*uc));
1291 if (len > 0 && ptr[len-1] == '\n')
1294 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1295 uc->cmd = UC_SETSYNC;
1296 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1297 uc->cmd = UC_SETTAGS;
1298 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1299 uc->cmd = UC_SETVERBOSE;
1300 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1301 uc->cmd = UC_SETWIDE;
1302 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1303 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1304 uc->cmd = UC_SETDEBUG;
1306 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1307 uc->cmd = UC_SETFLAG;
1308 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1309 uc->cmd = UC_RESETDEV;
1310 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1311 uc->cmd = UC_CLEARDEV;
1315 #ifdef DEBUG_PROC_INFO
1316 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1321 ptr += arg_len; len -= arg_len;
1331 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1332 ptr += arg_len; len -= arg_len;
1335 GET_INT_ARG(target);
1336 uc->target = (1<<target);
1337 #ifdef DEBUG_PROC_INFO
1338 printk("sym_user_command: target=%ld\n", target);
1350 GET_INT_ARG(uc->data);
1351 #ifdef DEBUG_PROC_INFO
1352 printk("sym_user_command: data=%ld\n", uc->data);
1355 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1359 if ((arg_len = is_keyword(ptr, len, "alloc")))
1360 uc->data |= DEBUG_ALLOC;
1361 else if ((arg_len = is_keyword(ptr, len, "phase")))
1362 uc->data |= DEBUG_PHASE;
1363 else if ((arg_len = is_keyword(ptr, len, "queue")))
1364 uc->data |= DEBUG_QUEUE;
1365 else if ((arg_len = is_keyword(ptr, len, "result")))
1366 uc->data |= DEBUG_RESULT;
1367 else if ((arg_len = is_keyword(ptr, len, "scatter")))
1368 uc->data |= DEBUG_SCATTER;
1369 else if ((arg_len = is_keyword(ptr, len, "script")))
1370 uc->data |= DEBUG_SCRIPT;
1371 else if ((arg_len = is_keyword(ptr, len, "tiny")))
1372 uc->data |= DEBUG_TINY;
1373 else if ((arg_len = is_keyword(ptr, len, "timing")))
1374 uc->data |= DEBUG_TIMING;
1375 else if ((arg_len = is_keyword(ptr, len, "nego")))
1376 uc->data |= DEBUG_NEGO;
1377 else if ((arg_len = is_keyword(ptr, len, "tags")))
1378 uc->data |= DEBUG_TAGS;
1379 else if ((arg_len = is_keyword(ptr, len, "pointer")))
1380 uc->data |= DEBUG_POINTER;
1383 ptr += arg_len; len -= arg_len;
1385 #ifdef DEBUG_PROC_INFO
1386 printk("sym_user_command: data=%ld\n", uc->data);
1389 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1393 if ((arg_len = is_keyword(ptr, len, "no_disc")))
1394 uc->data &= ~SYM_DISC_ENABLED;
1397 ptr += arg_len; len -= arg_len;
1407 unsigned long flags;
1409 spin_lock_irqsave(np->s.host->host_lock, flags);
1410 sym_exec_user_command (np, uc);
1411 spin_unlock_irqrestore(np->s.host->host_lock, flags);
1416 #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1419 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1421 * Informations through the proc file system.
1430 static void copy_mem_info(struct info_str *info, char *data, int len)
1432 if (info->pos + len > info->length)
1433 len = info->length - info->pos;
1435 if (info->pos + len < info->offset) {
1439 if (info->pos < info->offset) {
1440 data += (info->offset - info->pos);
1441 len -= (info->offset - info->pos);
1445 memcpy(info->buffer + info->pos, data, len);
1450 static int copy_info(struct info_str *info, char *fmt, ...)
1456 va_start(args, fmt);
1457 len = vsprintf(buf, fmt, args);
1460 copy_mem_info(info, buf, len);
1465 * Copy formatted information into the input buffer.
1467 static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
1469 struct info_str info;
1473 info.offset = offset;
1476 copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1477 "revision id 0x%x\n",
1478 np->s.chip_name, np->device_id, np->revision_id);
1479 copy_info(&info, "At PCI address %s, IRQ " IRQ_FMT "\n",
1480 pci_name(np->s.device), IRQ_PRM(np->s.irq));
1481 copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1482 (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1483 np->maxwide ? "Wide" : "Narrow",
1484 np->minsync_dt ? ", DT capable" : "");
1486 copy_info(&info, "Max. started commands %d, "
1487 "max. commands per LUN %d\n",
1488 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1490 return info.pos > info.offset? info.pos - info.offset : 0;
1492 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1495 * Entry point of the scsi proc fs of the driver.
1496 * - func = 0 means read (returns adapter infos)
1497 * - func = 1 means write (not yet merget from sym53c8xx)
1499 static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
1500 char **start, off_t offset, int length, int func)
1502 struct host_data *host_data;
1503 struct sym_hcb *np = NULL;
1506 host_data = (struct host_data *) host->hostdata;
1507 np = host_data->ncb;
1512 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1513 retv = sym_user_command(np, buffer, length);
1520 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1521 retv = sym_host_info(np, buffer, offset, length);
1529 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1532 * Free controller resources.
1534 static void sym_free_resources(struct sym_hcb *np)
1537 * Free O/S specific resources.
1540 free_irq(np->s.irq, np);
1541 #ifndef SYM_CONF_IOMAPPED
1543 iounmap(np->s.mmio_va);
1546 iounmap(np->s.ram_va);
1548 * Free O/S independent resources.
1552 sym_mfree_dma(np, sizeof(*np), "HCB");
1556 * Ask/tell the system about DMA addressing.
1558 static int sym_setup_bus_dma_mask(struct sym_hcb *np)
1560 #if SYM_CONF_DMA_ADDRESSING_MODE > 0
1561 #if SYM_CONF_DMA_ADDRESSING_MODE == 1
1562 #define DMA_DAC_MASK 0x000000ffffffffffULL /* 40-bit */
1563 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
1564 #define DMA_DAC_MASK DMA_64BIT_MASK
1566 if ((np->features & FE_DAC) &&
1567 !pci_set_dma_mask(np->s.device, DMA_DAC_MASK)) {
1573 if (!pci_set_dma_mask(np->s.device, DMA_32BIT_MASK))
1576 printf_warning("%s: No suitable DMA available\n", sym_name(np));
1581 * Host attach and initialisations.
1583 * Allocate host data and ncb structure.
1584 * Remap MMIO region.
1585 * Do chip initialization.
1586 * If all is OK, install interrupt handling and
1587 * start the timer daemon.
1589 static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
1590 int unit, struct sym_device *dev)
1592 struct host_data *host_data;
1593 struct sym_hcb *np = NULL;
1594 struct Scsi_Host *instance = NULL;
1595 unsigned long flags;
1599 "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT "\n",
1600 unit, dev->chip.name, dev->chip.revision_id,
1601 pci_name(dev->pdev), IRQ_PRM(dev->s.irq));
1604 * Get the firmware for this chip.
1606 fw = sym_find_firmware(&dev->chip);
1611 * Allocate host_data structure
1613 instance = scsi_host_alloc(tpnt, sizeof(*host_data));
1616 host_data = (struct host_data *) instance->hostdata;
1619 * Allocate immediately the host control block,
1620 * since we are only expecting to succeed. :)
1621 * We keep track in the HCB of all the resources that
1622 * are to be released on error.
1624 np = __sym_calloc_dma(dev->pdev, sizeof(*np), "HCB");
1627 np->s.device = dev->pdev;
1628 np->bus_dmat = dev->pdev; /* Result in 1 DMA pool per HBA */
1629 host_data->ncb = np;
1630 np->s.host = instance;
1632 pci_set_drvdata(dev->pdev, np);
1635 * Copy some useful infos to the HCB.
1637 np->hcb_ba = vtobus(np);
1638 np->verbose = sym_driver_setup.verbose;
1639 np->s.device = dev->pdev;
1641 np->device_id = dev->chip.device_id;
1642 np->revision_id = dev->chip.revision_id;
1643 np->features = dev->chip.features;
1644 np->clock_divn = dev->chip.nr_divisor;
1645 np->maxoffs = dev->chip.offset_max;
1646 np->maxburst = dev->chip.burst_max;
1647 np->myaddr = dev->host_id;
1652 strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1653 sprintf(np->s.inst_name, "sym%d", np->s.unit);
1655 if (sym_setup_bus_dma_mask(np))
1659 * Try to map the controller chip to
1660 * virtual and physical memory.
1662 np->mmio_ba = (u32)dev->s.base;
1663 np->s.io_ws = (np->features & FE_IO256)? 256 : 128;
1665 #ifndef SYM_CONF_IOMAPPED
1666 np->s.mmio_va = ioremap(dev->s.base_c, np->s.io_ws);
1667 if (!np->s.mmio_va) {
1668 printf_err("%s: can't map PCI MMIO region\n", sym_name(np));
1670 } else if (sym_verbose > 1)
1671 printf_info("%s: using memory mapped IO\n", sym_name(np));
1672 #endif /* !defined SYM_CONF_IOMAPPED */
1674 np->s.io_port = dev->s.io_port;
1677 * Map on-chip RAM if present and supported.
1679 if (!(np->features & FE_RAM))
1681 if (dev->s.base_2) {
1682 np->ram_ba = (u32)dev->s.base_2;
1683 if (np->features & FE_RAM8K)
1687 np->s.ram_va = ioremap(dev->s.base_2_c, np->ram_ws);
1688 if (!np->s.ram_va) {
1689 printf_err("%s: can't map PCI MEMORY region\n",
1696 * Perform O/S independent stuff.
1698 if (sym_hcb_attach(np, fw, dev->nvram))
1703 * Install the interrupt handler.
1704 * If we synchonize the C code with SCRIPTS on interrupt,
1705 * we donnot want to share the INTR line at all.
1707 if (request_irq(dev->s.irq, sym53c8xx_intr, SA_SHIRQ,
1709 printf_err("%s: request irq %d failure\n",
1710 sym_name(np), dev->s.irq);
1713 np->s.irq = dev->s.irq;
1716 * After SCSI devices have been opened, we cannot
1717 * reset the bus safely, so we do it here.
1719 spin_lock_irqsave(instance->host_lock, flags);
1720 if (sym_reset_scsi_bus(np, 0))
1724 * Start the SCRIPTS.
1726 sym_start_up (np, 1);
1729 * Start the timer daemon
1731 init_timer(&np->s.timer);
1732 np->s.timer.data = (unsigned long) np;
1733 np->s.timer.function = sym53c8xx_timer;
1738 * Fill Linux host instance structure
1739 * and return success.
1741 instance->max_channel = 0;
1742 instance->this_id = np->myaddr;
1743 instance->max_id = np->maxwide ? 16 : 8;
1744 instance->max_lun = SYM_CONF_MAX_LUN;
1745 #ifndef SYM_CONF_IOMAPPED
1746 instance->base = (unsigned long) np->s.mmio_va;
1748 instance->irq = np->s.irq;
1749 instance->unique_id = np->s.io_port;
1750 instance->io_port = np->s.io_port;
1751 instance->n_io_port = np->s.io_ws;
1752 instance->dma_channel = 0;
1753 instance->cmd_per_lun = SYM_CONF_MAX_TAG;
1754 instance->can_queue = (SYM_CONF_MAX_START-2);
1755 instance->sg_tablesize = SYM_CONF_MAX_SG;
1756 instance->max_cmd_len = 16;
1757 BUG_ON(sym2_transport_template == NULL);
1758 instance->transportt = sym2_transport_template;
1760 spin_unlock_irqrestore(instance->host_lock, flags);
1765 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1766 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1767 spin_unlock_irqrestore(instance->host_lock, flags);
1771 printf_info("%s: giving up ...\n", sym_name(np));
1773 sym_free_resources(np);
1774 scsi_host_put(instance);
1781 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1783 #if SYM_CONF_NVRAM_SUPPORT
1784 static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1787 devp->device_id = devp->chip.device_id;
1791 * Get access to chip IO registers
1793 #ifndef SYM_CONF_IOMAPPED
1794 devp->s.mmio_va = ioremap(devp->s.base_c, 128);
1795 if (!devp->s.mmio_va)
1799 sym_read_nvram(devp, nvp);
1802 * Release access to chip IO registers
1804 #ifndef SYM_CONF_IOMAPPED
1805 iounmap(devp->s.mmio_va);
1809 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1812 #endif /* SYM_CONF_NVRAM_SUPPORT */
1814 static int __devinit sym_check_supported(struct sym_device *device)
1816 struct sym_pci_chip *chip;
1817 struct pci_dev *pdev = device->pdev;
1819 unsigned long io_port = device->s.io_port;
1820 unsigned long base = device->s.base;
1824 * If user excluded this chip, do not initialize it.
1827 for (i = 0 ; i < 8 ; i++) {
1828 if (sym_driver_setup.excludes[i] == io_port)
1834 * Check if the chip has been assigned resources we need.
1835 * XXX: can this still happen with Linux 2.6's PCI layer?
1837 #ifdef SYM_CONF_IOMAPPED
1839 printf_info("%s: IO base address disabled.\n",
1845 printf_info("%s: MMIO base address disabled.\n",
1852 * Check if the chip is supported. Then copy the chip description
1853 * to our device structure so we can make it match the actual device
1856 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1857 chip = sym_lookup_pci_chip_table(pdev->device, revision);
1859 printf_info("%s: device not supported\n", sym_name(device));
1862 memcpy(&device->chip, chip, sizeof(device->chip));
1863 device->chip.revision_id = revision;
1869 * Ignore Symbios chips controlled by various RAID controllers.
1870 * These controllers set value 0x52414944 at RAM end - 16.
1872 static int __devinit sym_check_raid(struct sym_device *device)
1874 unsigned long base_2_c = device->s.base_2_c;
1875 unsigned int ram_size, ram_val;
1876 void __iomem *ram_ptr;
1881 if (device->chip.features & FE_RAM8K)
1886 ram_ptr = ioremap(base_2_c, ram_size);
1890 ram_val = readl(ram_ptr + ram_size - 16);
1892 if (ram_val != 0x52414944)
1895 printf_info("%s: not initializing, driven by RAID controller.\n",
1900 static int __devinit sym_set_workarounds(struct sym_device *device)
1902 struct sym_pci_chip *chip = &device->chip;
1903 struct pci_dev *pdev = device->pdev;
1907 * (ITEM 12 of a DEL about the 896 I haven't yet).
1908 * We must ensure the chip will use WRITE AND INVALIDATE.
1909 * The revision number limit is for now arbitrary.
1911 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && chip->revision_id < 0x4) {
1912 chip->features |= (FE_WRIE | FE_CLSE);
1915 /* If the chip can do Memory Write Invalidate, enable it */
1916 if (chip->features & FE_WRIE) {
1917 if (pci_set_mwi(pdev))
1922 * Work around for errant bit in 895A. The 66Mhz
1923 * capable bit is set erroneously. Clear this bit.
1926 * Make sure Config space and Features agree.
1928 * Recall: writes are not normal to status register -
1929 * write a 1 to clear and a 0 to leave unchanged.
1930 * Can only reset bits.
1932 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1933 if (chip->features & FE_66MHZ) {
1934 if (!(status_reg & PCI_STATUS_66MHZ))
1935 chip->features &= ~FE_66MHZ;
1937 if (status_reg & PCI_STATUS_66MHZ) {
1938 status_reg = PCI_STATUS_66MHZ;
1939 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1940 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1948 * Read and check the PCI configuration for any detected NCR
1949 * boards and save data for attaching after all boards have
1952 static void __devinit
1953 sym_init_device(struct pci_dev *pdev, struct sym_device *device)
1955 unsigned long base, base_2;
1958 device->host_id = SYM_SETUP_HOST_ID;
1959 device->pdev = pdev;
1960 device->s.irq = pdev->irq;
1962 /* Choose some short name for this device */
1963 sprintf(device->s.inst_name, "sym.%d.%d.%d", pdev->bus->number,
1964 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1966 device->s.io_port = pdev->resource[0].start;
1968 device->s.base_c = pdev->resource[1].start;
1969 i = pci_get_base_address(pdev, 1, &base);
1970 device->s.base = base & PCI_BASE_ADDRESS_MEM_MASK;
1972 device->s.base_2_c = pdev->resource[i].start;
1973 pci_get_base_address(pdev, i, &base_2);
1974 device->s.base_2 = base_2 & PCI_BASE_ADDRESS_MEM_MASK;
1978 * The NCR PQS and PDS cards are constructed as a DEC bridge
1979 * behind which sits a proprietary NCR memory controller and
1980 * either four or two 53c875s as separate devices. We can tell
1981 * if an 875 is part of a PQS/PDS or not since if it is, it will
1982 * be on the same bus as the memory controller. In its usual
1983 * mode of operation, the 875s are slaved to the memory
1984 * controller for all transfers. To operate with the Linux
1985 * driver, the memory controller is disabled and the 875s
1986 * freed to function independently. The only wrinkle is that
1987 * the preset SCSI ID (which may be zero) must be read in from
1988 * a special configuration space register of the 875.
1990 static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
1995 for (slot = 0; slot < 256; slot++) {
1996 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
1998 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
2003 /* bit 1: allow individual 875 configuration */
2004 pci_read_config_byte(memc, 0x44, &tmp);
2005 if ((tmp & 0x2) == 0) {
2007 pci_write_config_byte(memc, 0x44, tmp);
2010 /* bit 2: drive individual 875 interrupts to the bus */
2011 pci_read_config_byte(memc, 0x45, &tmp);
2012 if ((tmp & 0x4) == 0) {
2014 pci_write_config_byte(memc, 0x45, tmp);
2021 pci_read_config_byte(pdev, 0x84, &tmp);
2022 sym_dev->host_id = tmp;
2026 * Called before unloading the module.
2028 * We have to free resources and halt the NCR chip.
2030 static int sym_detach(struct sym_hcb *np)
2032 printk("%s: detaching ...\n", sym_name(np));
2034 del_timer_sync(&np->s.timer);
2038 * We should use sym_soft_reset(), but we don't want to do
2039 * so, since we may not be safe if interrupts occur.
2041 printk("%s: resetting chip\n", sym_name(np));
2042 OUTB (nc_istat, SRST);
2046 sym_free_resources(np);
2052 * Driver host template.
2054 static struct scsi_host_template sym2_template = {
2055 .module = THIS_MODULE,
2056 .name = "sym53c8xx",
2057 .info = sym53c8xx_info,
2058 .queuecommand = sym53c8xx_queue_command,
2059 .slave_configure = sym53c8xx_slave_configure,
2060 .eh_abort_handler = sym53c8xx_eh_abort_handler,
2061 .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
2062 .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
2063 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
2065 .use_clustering = DISABLE_CLUSTERING,
2066 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
2067 .proc_info = sym53c8xx_proc_info,
2068 .proc_name = NAME53C8XX,
2072 static int attach_count;
2074 static int __devinit sym2_probe(struct pci_dev *pdev,
2075 const struct pci_device_id *ent)
2077 struct sym_device sym_dev;
2078 struct sym_nvram nvram;
2079 struct Scsi_Host *instance;
2081 memset(&sym_dev, 0, sizeof(sym_dev));
2082 memset(&nvram, 0, sizeof(nvram));
2084 if (pci_enable_device(pdev))
2087 pci_set_master(pdev);
2089 if (pci_request_regions(pdev, NAME53C8XX))
2092 sym_init_device(pdev, &sym_dev);
2093 if (sym_check_supported(&sym_dev))
2096 if (sym_check_raid(&sym_dev))
2097 goto leave; /* Don't disable the device */
2099 if (sym_set_workarounds(&sym_dev))
2102 sym_config_pqs(pdev, &sym_dev);
2104 sym_get_nvram(&sym_dev, &nvram);
2106 instance = sym_attach(&sym2_template, attach_count, &sym_dev);
2110 if (scsi_add_host(instance, &pdev->dev))
2112 scsi_scan_host(instance);
2119 sym_detach(pci_get_drvdata(pdev));
2121 pci_release_regions(pdev);
2123 pci_disable_device(pdev);
2128 static void __devexit sym2_remove(struct pci_dev *pdev)
2130 struct sym_hcb *np = pci_get_drvdata(pdev);
2131 struct Scsi_Host *host = np->s.host;
2133 scsi_remove_host(host);
2134 scsi_host_put(host);
2138 pci_release_regions(pdev);
2139 pci_disable_device(pdev);
2144 static void sym2_get_signalling(struct Scsi_Host *shost)
2146 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2147 enum spi_signal_type type;
2149 switch (np->scsi_mode) {
2151 type = SPI_SIGNAL_SE;
2154 type = SPI_SIGNAL_LVD;
2157 type = SPI_SIGNAL_HVD;
2160 type = SPI_SIGNAL_UNKNOWN;
2163 spi_signalling(shost) = type;
2166 static void sym2_get_offset(struct scsi_target *starget)
2168 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2169 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2170 struct sym_tcb *tp = &np->target[starget->id];
2172 spi_offset(starget) = tp->tinfo.curr.offset;
2175 static void sym2_set_offset(struct scsi_target *starget, int offset)
2177 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2178 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2179 struct sym_tcb *tp = &np->target[starget->id];
2181 tp->tinfo.goal.offset = offset;
2185 static void sym2_get_period(struct scsi_target *starget)
2187 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2188 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2189 struct sym_tcb *tp = &np->target[starget->id];
2191 spi_period(starget) = tp->tinfo.curr.period;
2194 static void sym2_set_period(struct scsi_target *starget, int period)
2196 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2197 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2198 struct sym_tcb *tp = &np->target[starget->id];
2200 /* have to have DT for these transfers */
2201 if (period <= np->minsync)
2202 tp->tinfo.goal.options |= PPR_OPT_DT;
2204 tp->tinfo.goal.period = period;
2207 static void sym2_get_width(struct scsi_target *starget)
2209 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2210 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2211 struct sym_tcb *tp = &np->target[starget->id];
2213 spi_width(starget) = tp->tinfo.curr.width ? 1 : 0;
2216 static void sym2_set_width(struct scsi_target *starget, int width)
2218 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2219 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2220 struct sym_tcb *tp = &np->target[starget->id];
2222 /* It is illegal to have DT set on narrow transfers. If DT is
2223 * clear, we must also clear IU and QAS. */
2225 tp->tinfo.goal.options &= ~PPR_OPT_MASK;
2227 tp->tinfo.goal.width = width;
2230 static void sym2_get_dt(struct scsi_target *starget)
2232 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2233 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2234 struct sym_tcb *tp = &np->target[starget->id];
2236 spi_dt(starget) = (tp->tinfo.curr.options & PPR_OPT_DT) ? 1 : 0;
2239 static void sym2_set_dt(struct scsi_target *starget, int dt)
2241 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2242 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2243 struct sym_tcb *tp = &np->target[starget->id];
2245 /* We must clear QAS and IU if DT is clear */
2247 tp->tinfo.goal.options |= PPR_OPT_DT;
2249 tp->tinfo.goal.options &= ~PPR_OPT_MASK;
2252 static void sym2_get_iu(struct scsi_target *starget)
2254 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2255 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2256 struct sym_tcb *tp = &np->target[starget->id];
2258 spi_iu(starget) = (tp->tinfo.curr.options & PPR_OPT_IU) ? 1 : 0;
2261 static void sym2_set_iu(struct scsi_target *starget, int iu)
2263 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2264 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2265 struct sym_tcb *tp = &np->target[starget->id];
2268 tp->tinfo.goal.options |= PPR_OPT_IU | PPR_OPT_DT;
2270 tp->tinfo.goal.options &= ~PPR_OPT_IU;
2273 static void sym2_get_qas(struct scsi_target *starget)
2275 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2276 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2277 struct sym_tcb *tp = &np->target[starget->id];
2279 spi_qas(starget) = (tp->tinfo.curr.options & PPR_OPT_QAS) ? 1 : 0;
2282 static void sym2_set_qas(struct scsi_target *starget, int qas)
2284 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2285 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2286 struct sym_tcb *tp = &np->target[starget->id];
2289 tp->tinfo.goal.options |= PPR_OPT_QAS | PPR_OPT_DT;
2291 tp->tinfo.goal.options &= ~PPR_OPT_QAS;
2295 static struct spi_function_template sym2_transport_functions = {
2296 .set_offset = sym2_set_offset,
2297 .get_offset = sym2_get_offset,
2299 .set_period = sym2_set_period,
2300 .get_period = sym2_get_period,
2302 .set_width = sym2_set_width,
2303 .get_width = sym2_get_width,
2305 .get_dt = sym2_get_dt,
2306 .set_dt = sym2_set_dt,
2308 .get_iu = sym2_get_iu,
2309 .set_iu = sym2_set_iu,
2311 .get_qas = sym2_get_qas,
2312 .set_qas = sym2_set_qas,
2314 .get_signalling = sym2_get_signalling,
2317 static struct pci_device_id sym2_id_table[] __devinitdata = {
2318 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
2319 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2320 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
2321 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2322 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
2323 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2324 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
2325 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2326 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
2327 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2328 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
2329 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2330 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
2331 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2332 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
2333 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2334 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
2335 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2336 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2337 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2338 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2339 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2340 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
2341 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2342 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2343 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2344 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2345 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2346 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2347 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2348 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2349 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2350 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2351 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2355 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2357 static struct pci_driver sym2_driver = {
2359 .id_table = sym2_id_table,
2360 .probe = sym2_probe,
2361 .remove = __devexit_p(sym2_remove),
2364 static int __init sym2_init(void)
2368 sym2_setup_params();
2369 sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2370 if (!sym2_transport_template)
2373 error = pci_module_init(&sym2_driver);
2375 spi_release_transport(sym2_transport_template);
2379 static void __exit sym2_exit(void)
2381 pci_unregister_driver(&sym2_driver);
2382 spi_release_transport(sym2_transport_template);
2385 module_init(sym2_init);
2386 module_exit(sym2_exit);