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 struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
59 unsigned int sym_debug_flags = 0;
61 static char *excl_string;
62 static char *safe_string;
63 module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
64 module_param_string(tag_ctrl, sym_driver_setup.tag_ctrl, 100, 0);
65 module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
66 module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
67 module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
68 module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
69 module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
70 module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
71 module_param_named(verb, sym_driver_setup.verbose, byte, 0);
72 module_param_named(debug, sym_debug_flags, uint, 0);
73 module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
74 module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
75 module_param_named(excl, excl_string, charp, 0);
76 module_param_named(safe, safe_string, charp, 0);
78 MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
79 MODULE_PARM_DESC(tag_ctrl, "More detailed control over tags per LUN");
80 MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
81 MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
82 MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
83 MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
84 MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
85 MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
86 MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
87 MODULE_PARM_DESC(debug, "Set bits to enable debugging");
88 MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
89 MODULE_PARM_DESC(nvram, "Option currently not used");
90 MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
91 MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
93 MODULE_LICENSE("GPL");
94 MODULE_VERSION(SYM_VERSION);
95 MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
96 MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
98 static void sym2_setup_params(void)
100 char *p = excl_string;
103 while (p && (xi < 8)) {
105 int val = (int) simple_strtoul(p, &next_p, 0);
106 sym_driver_setup.excludes[xi++] = val;
111 if (*safe_string == 'y') {
112 sym_driver_setup.max_tag = 0;
113 sym_driver_setup.burst_order = 0;
114 sym_driver_setup.scsi_led = 0;
115 sym_driver_setup.scsi_diff = 1;
116 sym_driver_setup.irq_mode = 0;
117 sym_driver_setup.scsi_bus_check = 2;
118 sym_driver_setup.host_id = 7;
119 sym_driver_setup.verbose = 2;
120 sym_driver_setup.settle_delay = 10;
121 sym_driver_setup.use_nvram = 1;
122 } else if (*safe_string != 'n') {
123 printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
124 " passed to safe option", safe_string);
130 pci_get_base_address(struct pci_dev *pdev, int index, u_long *base)
133 #define PCI_BAR_OFFSET(index) (PCI_BASE_ADDRESS_0 + (index<<2))
135 pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
138 if ((tmp & 0x7) == PCI_BASE_ADDRESS_MEM_TYPE_64) {
139 #if BITS_PER_LONG > 32
140 pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
141 *base |= (((u_long)tmp) << 32);
146 #undef PCI_BAR_OFFSET
149 /* This lock protects only the memory allocation/free. */
150 spinlock_t sym53c8xx_lock = SPIN_LOCK_UNLOCKED;
152 static struct scsi_transport_template *sym2_transport_template = NULL;
155 * Wrappers to the generic memory allocator.
157 void *sym_calloc(int size, char *name)
161 spin_lock_irqsave(&sym53c8xx_lock, flags);
162 m = sym_calloc_unlocked(size, name);
163 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
167 void sym_mfree(void *m, int size, char *name)
170 spin_lock_irqsave(&sym53c8xx_lock, flags);
171 sym_mfree_unlocked(m, size, name);
172 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
175 void *__sym_calloc_dma(m_pool_ident_t dev_dmat, int size, char *name)
179 spin_lock_irqsave(&sym53c8xx_lock, flags);
180 m = __sym_calloc_dma_unlocked(dev_dmat, size, name);
181 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
185 void __sym_mfree_dma(m_pool_ident_t dev_dmat, void *m, int size, char *name)
188 spin_lock_irqsave(&sym53c8xx_lock, flags);
189 __sym_mfree_dma_unlocked(dev_dmat, m, size, name);
190 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
193 m_addr_t __vtobus(m_pool_ident_t dev_dmat, void *m)
197 spin_lock_irqsave(&sym53c8xx_lock, flags);
198 b = __vtobus_unlocked(dev_dmat, m);
199 spin_unlock_irqrestore(&sym53c8xx_lock, flags);
204 * Used by the eh thread to wait for command completion.
205 * It is allocated on the eh thread stack.
208 struct completion done;
209 struct timer_list timer;
210 void (*old_done)(struct scsi_cmnd *);
216 * Driver private area in the SCSI command structure.
218 struct sym_ucmd { /* Override the SCSI pointer structure */
219 dma_addr_t data_mapping;
221 struct sym_eh_wait *eh_wait;
224 #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
225 #define SYM_SCMD_PTR(ucmd) sym_que_entry(ucmd, struct scsi_cmnd, SCp)
226 #define SYM_SOFTC_PTR(cmd) (((struct host_data *)cmd->device->host->hostdata)->ncb)
228 static void __unmap_scsi_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
230 int dma_dir = cmd->sc_data_direction;
232 switch(SYM_UCMD_PTR(cmd)->data_mapped) {
234 pci_unmap_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
237 pci_unmap_single(pdev, SYM_UCMD_PTR(cmd)->data_mapping,
238 cmd->request_bufflen, dma_dir);
241 SYM_UCMD_PTR(cmd)->data_mapped = 0;
244 static dma_addr_t __map_scsi_single_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
247 int dma_dir = cmd->sc_data_direction;
249 mapping = pci_map_single(pdev, cmd->request_buffer,
250 cmd->request_bufflen, dma_dir);
252 SYM_UCMD_PTR(cmd)->data_mapped = 1;
253 SYM_UCMD_PTR(cmd)->data_mapping = mapping;
259 static int __map_scsi_sg_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
262 int dma_dir = cmd->sc_data_direction;
264 use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
266 SYM_UCMD_PTR(cmd)->data_mapped = 2;
267 SYM_UCMD_PTR(cmd)->data_mapping = use_sg;
273 #define unmap_scsi_data(np, cmd) \
274 __unmap_scsi_data(np->s.device, cmd)
275 #define map_scsi_single_data(np, cmd) \
276 __map_scsi_single_data(np->s.device, cmd)
277 #define map_scsi_sg_data(np, cmd) \
278 __map_scsi_sg_data(np->s.device, cmd)
280 * Complete a pending CAM CCB.
282 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *ccb)
284 unmap_scsi_data(np, ccb);
288 void sym_xpt_done2(struct sym_hcb *np, struct scsi_cmnd *ccb, int cam_status)
290 sym_set_cam_status(ccb, cam_status);
291 sym_xpt_done(np, ccb);
296 * Print something that identifies the IO.
298 void sym_print_addr(struct sym_ccb *cp)
300 struct scsi_cmnd *cmd = cp->cam_ccb;
302 printf("%s:%d:%d:", sym_name(SYM_SOFTC_PTR(cmd)),
303 cmd->device->id, cmd->device->lun);
307 * Tell the SCSI layer about a BUS RESET.
309 void sym_xpt_async_bus_reset(struct sym_hcb *np)
311 printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
312 np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
313 np->s.settle_time_valid = 1;
314 if (sym_verbose >= 2)
315 printf_info("%s: command processing suspended for %d seconds\n",
316 sym_name(np), sym_driver_setup.settle_delay);
320 * Tell the SCSI layer about a BUS DEVICE RESET message sent.
322 void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
324 printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
328 * Tell the SCSI layer about the new transfer parameters.
330 void sym_xpt_async_nego_wide(struct sym_hcb *np, int target)
334 sym_announce_transfer_rate(np, target);
338 * Choose the more appropriate CAM status if
339 * the IO encountered an extended error.
341 static int sym_xerr_cam_status(int cam_status, int x_status)
344 if (x_status & XE_PARITY_ERR)
345 cam_status = DID_PARITY;
346 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
347 cam_status = DID_ERROR;
348 else if (x_status & XE_BAD_PHASE)
349 cam_status = DID_ERROR;
351 cam_status = DID_ERROR;
357 * Build CAM result for a failed or auto-sensed IO.
359 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
361 struct scsi_cmnd *csio = cp->cam_ccb;
362 u_int cam_status, scsi_status, drv_status;
366 scsi_status = cp->ssss_status;
368 if (cp->host_flags & HF_SENSE) {
369 scsi_status = cp->sv_scsi_status;
370 resid = cp->sv_resid;
371 if (sym_verbose && cp->sv_xerr_status)
372 sym_print_xerr(cp, cp->sv_xerr_status);
373 if (cp->host_status == HS_COMPLETE &&
374 cp->ssss_status == S_GOOD &&
375 cp->xerr_status == 0) {
376 cam_status = sym_xerr_cam_status(DID_OK,
378 drv_status = DRIVER_SENSE;
380 * Bounce back the sense data to user.
382 bzero(&csio->sense_buffer, sizeof(csio->sense_buffer));
383 memcpy(csio->sense_buffer, cp->sns_bbuf,
384 min(sizeof(csio->sense_buffer),
385 (size_t)SYM_SNS_BBUF_LEN));
388 * If the device reports a UNIT ATTENTION condition
389 * due to a RESET condition, we should consider all
390 * disconnect CCBs for this unit as aborted.
394 p = (u_char *) csio->sense_data;
395 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
396 sym_clear_tasks(np, DID_ABORT,
397 cp->target,cp->lun, -1);
402 * Error return from our internal request sense. This
403 * is bad: we must clear the contingent allegiance
404 * condition otherwise the device will always return
405 * BUSY. Use a big stick.
407 sym_reset_scsi_target(np, csio->device->id);
408 cam_status = DID_ERROR;
410 } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
412 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
413 cam_status = DID_NO_CONNECT;
414 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
415 cam_status = DID_ERROR;
416 else { /* Extended error */
419 printf ("COMMAND FAILED (%x %x %x).\n",
420 cp->host_status, cp->ssss_status,
424 * Set the most appropriate value for CAM status.
426 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
429 csio->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
434 * Build the scatter/gather array for an I/O.
437 static int sym_scatter_no_sglist(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
439 struct sym_tblmove *data = &cp->phys.data[SYM_CONF_MAX_SG-1];
442 cp->data_len = cmd->request_bufflen;
444 if (cmd->request_bufflen) {
445 dma_addr_t baddr = map_scsi_single_data(np, cmd);
447 sym_build_sge(np, data, baddr, cmd->request_bufflen);
459 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
462 int use_sg = (int) cmd->use_sg;
467 segment = sym_scatter_no_sglist(np, cp, cmd);
468 else if ((use_sg = map_scsi_sg_data(np, cmd)) > 0) {
469 struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
470 struct sym_tblmove *data;
472 if (use_sg > SYM_CONF_MAX_SG) {
473 unmap_scsi_data(np, cmd);
477 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
479 for (segment = 0; segment < use_sg; segment++) {
480 dma_addr_t baddr = sg_dma_address(&scatter[segment]);
481 unsigned int len = sg_dma_len(&scatter[segment]);
483 sym_build_sge(np, &data[segment], baddr, len);
494 * Queue a SCSI command.
496 static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *ccb)
498 /* struct scsi_device *device = ccb->device; */
505 * Minimal checkings, so that we will not
506 * go outside our tables.
508 if (ccb->device->id == np->myaddr ||
509 ccb->device->id >= SYM_CONF_MAX_TARGET ||
510 ccb->device->lun >= SYM_CONF_MAX_LUN) {
511 sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
516 * Retreive the target descriptor.
518 tp = &np->target[ccb->device->id];
521 * Complete the 1st INQUIRY command with error
522 * condition if the device is flagged NOSCAN
523 * at BOOT in the NVRAM. This may speed up
524 * the boot and maintain coherency with BIOS
525 * device numbering. Clearing the flag allows
526 * user to rescan skipped devices later.
527 * We also return error for devices not flagged
528 * for SCAN LUNS in the NVRAM since some mono-lun
529 * devices behave badly when asked for some non
530 * zero LUN. Btw, this is an absolute hack.:-)
532 if (ccb->cmnd[0] == 0x12 || ccb->cmnd[0] == 0x0) {
533 if ((tp->usrflags & SYM_SCAN_BOOT_DISABLED) ||
534 ((tp->usrflags & SYM_SCAN_LUNS_DISABLED) &&
535 ccb->device->lun != 0)) {
536 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
537 sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
543 * Select tagged/untagged.
545 lp = sym_lp(np, tp, ccb->device->lun);
546 order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
551 cp = sym_get_ccb(np, ccb->device->id, ccb->device->lun, order);
553 return 1; /* Means resource shortage */
554 sym_queue_scsiio(np, ccb, cp);
559 * Setup buffers and pointers that address the CDB.
561 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *ccb, struct sym_ccb *cp)
567 * CDB is 16 bytes max.
569 if (ccb->cmd_len > sizeof(cp->cdb_buf)) {
570 sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID);
574 memcpy(cp->cdb_buf, ccb->cmnd, ccb->cmd_len);
575 cmd_ba = CCB_BA (cp, cdb_buf[0]);
576 cmd_len = ccb->cmd_len;
578 cp->phys.cmd.addr = cpu_to_scr(cmd_ba);
579 cp->phys.cmd.size = cpu_to_scr(cmd_len);
585 * Setup pointers that address the data and start the I/O.
587 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *csio, struct sym_ccb *cp)
590 struct sym_tcb *tp = &np->target[cp->target];
591 struct sym_lcb *lp = sym_lp(np, tp, cp->lun);
596 if (sym_setup_cdb(np, csio, cp))
600 * No direction means no data.
602 dir = csio->sc_data_direction;
603 if (dir != DMA_NONE) {
604 cp->segments = sym_scatter(np, cp, csio);
605 if (cp->segments < 0) {
606 if (cp->segments == -2)
607 sym_set_cam_status(csio, CAM_RESRC_UNAVAIL);
609 sym_set_cam_status(csio, CAM_REQ_TOO_BIG);
620 sym_setup_data_pointers(np, cp, dir);
623 * When `#ifed 1', the code below makes the driver
624 * panic on the first attempt to write to a SCSI device.
625 * It is the first test we want to do after a driver
626 * change that does not seem obviously safe. :)
629 switch (cp->cdb_buf[0]) {
630 case 0x0A: case 0x2A: case 0xAA:
631 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
642 sym_start_next_ccbs(np, lp, 2);
644 sym_put_start_queue(np, cp);
648 sym_free_ccb(np, cp);
649 sym_xpt_done(np, csio);
657 * Misused to keep the driver running when
658 * interrupts are not configured correctly.
660 static void sym_timer(struct sym_hcb *np)
662 unsigned long thistime = jiffies;
667 np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
668 add_timer(&np->s.timer);
671 * If we are resetting the ncr, wait for settle_time before
672 * clearing it. Then command processing will be resumed.
674 if (np->s.settle_time_valid) {
675 if (time_before_eq(np->s.settle_time, thistime)) {
676 if (sym_verbose >= 2 )
677 printk("%s: command processing resumed\n",
679 np->s.settle_time_valid = 0;
685 * Nothing to do for now, but that may come.
687 if (np->s.lasttime + 4*HZ < thistime) {
688 np->s.lasttime = thistime;
691 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
693 * Some way-broken PCI bridges may lead to
694 * completions being lost when the clearing
695 * of the INTFLY flag by the CPU occurs
696 * concurrently with the chip raising this flag.
697 * If this ever happen, lost completions will
706 * PCI BUS error handler.
708 void sym_log_bus_error(struct sym_hcb *np)
711 pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
712 if (pci_sts & 0xf900) {
713 pci_write_config_word(np->s.device, PCI_STATUS, pci_sts);
714 printf("%s: PCI STATUS = 0x%04x\n",
715 sym_name(np), pci_sts & 0xf900);
720 * queuecommand method. Entered with the host adapter lock held and
721 * interrupts disabled.
723 static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
724 void (*done)(struct scsi_cmnd *))
726 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
727 struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
730 cmd->scsi_done = done;
731 cmd->host_scribble = NULL;
732 memset(ucp, 0, sizeof(*ucp));
735 * Shorten our settle_time if needed for
736 * this command not to time out.
738 if (np->s.settle_time_valid && cmd->timeout_per_command) {
739 unsigned long tlimit = jiffies + cmd->timeout_per_command;
740 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
741 if (time_after(np->s.settle_time, tlimit)) {
742 np->s.settle_time = tlimit;
746 if (np->s.settle_time_valid)
747 return SCSI_MLQUEUE_HOST_BUSY;
749 sts = sym_queue_command(np, cmd);
751 return SCSI_MLQUEUE_HOST_BUSY;
756 * Linux entry point of the interrupt handler.
758 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
761 struct sym_hcb *np = (struct sym_hcb *)dev_id;
763 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
765 spin_lock_irqsave(np->s.host->host_lock, flags);
767 spin_unlock_irqrestore(np->s.host->host_lock, flags);
769 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
775 * Linux entry point of the timer handler
777 static void sym53c8xx_timer(unsigned long npref)
779 struct sym_hcb *np = (struct sym_hcb *)npref;
782 spin_lock_irqsave(np->s.host->host_lock, flags);
784 spin_unlock_irqrestore(np->s.host->host_lock, flags);
789 * What the eh thread wants us to perform.
791 #define SYM_EH_ABORT 0
792 #define SYM_EH_DEVICE_RESET 1
793 #define SYM_EH_BUS_RESET 2
794 #define SYM_EH_HOST_RESET 3
797 * What we will do regarding the involved SCSI command.
799 #define SYM_EH_DO_IGNORE 0
800 #define SYM_EH_DO_COMPLETE 1
801 #define SYM_EH_DO_WAIT 2
804 * Our general completion handler.
806 static void __sym_eh_done(struct scsi_cmnd *cmd, int timed_out)
808 struct sym_eh_wait *ep = SYM_UCMD_PTR(cmd)->eh_wait;
812 /* Try to avoid a race here (not 100% safe) */
815 if (ep->to_do == SYM_EH_DO_WAIT && !del_timer(&ep->timer))
819 /* Revert everything */
820 SYM_UCMD_PTR(cmd)->eh_wait = NULL;
821 cmd->scsi_done = ep->old_done;
823 /* Wake up the eh thread if it wants to sleep */
824 if (ep->to_do == SYM_EH_DO_WAIT)
829 * scsi_done() alias when error recovery is in progress.
831 static void sym_eh_done(struct scsi_cmnd *cmd) { __sym_eh_done(cmd, 0); }
834 * Some timeout handler to avoid waiting too long.
836 static void sym_eh_timeout(u_long p) { __sym_eh_done((struct scsi_cmnd *)p, 1); }
839 * Generic method for our eh processing.
840 * The 'op' argument tells what we have to do.
842 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
844 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
846 int to_do = SYM_EH_DO_IGNORE;
848 struct sym_eh_wait eh, *ep = &eh;
851 sprintf(devname, "%s:%d:%d", sym_name(np), cmd->device->id, cmd->device->lun);
853 printf_warning("%s: %s operation started.\n", devname, opname);
856 /* This one should be the result of some race, thus to ignore */
857 if (cmd->serial_number != cmd->serial_number_at_timeout)
861 /* This one is queued in some place -> to wait for completion */
862 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
863 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
864 if (cp->cam_ccb == cmd) {
865 to_do = SYM_EH_DO_WAIT;
871 /* Prepare stuff to either ignore, complete or wait for completion */
874 case SYM_EH_DO_IGNORE:
877 init_completion(&ep->done);
879 case SYM_EH_DO_COMPLETE:
880 ep->old_done = cmd->scsi_done;
881 cmd->scsi_done = sym_eh_done;
882 SYM_UCMD_PTR(cmd)->eh_wait = ep;
885 /* Try to proceed the operation we have been asked for */
889 sts = sym_abort_scsiio(np, cmd, 1);
891 case SYM_EH_DEVICE_RESET:
892 sts = sym_reset_scsi_target(np, cmd->device->id);
894 case SYM_EH_BUS_RESET:
895 sym_reset_scsi_bus(np, 1);
898 case SYM_EH_HOST_RESET:
899 sym_reset_scsi_bus(np, 0);
900 sym_start_up (np, 1);
907 /* On error, restore everything and cross fingers :) */
909 SYM_UCMD_PTR(cmd)->eh_wait = NULL;
910 cmd->scsi_done = ep->old_done;
911 to_do = SYM_EH_DO_IGNORE;
915 /* Complete the command with locks held as required by the driver */
916 if (to_do == SYM_EH_DO_COMPLETE)
917 sym_xpt_done2(np, cmd, CAM_REQ_ABORTED);
919 /* Wait for completion with locks released, as required by kernel */
920 if (to_do == SYM_EH_DO_WAIT) {
921 init_timer(&ep->timer);
922 ep->timer.expires = jiffies + (5*HZ);
923 ep->timer.function = sym_eh_timeout;
924 ep->timer.data = (u_long)cmd;
925 ep->timed_out = 1; /* Be pessimistic for once :) */
926 add_timer(&ep->timer);
927 spin_unlock_irq(np->s.host->host_lock);
928 wait_for_completion(&ep->done);
929 spin_lock_irq(np->s.host->host_lock);
933 printf_warning("%s: %s operation %s.\n", devname, opname,
934 sts==0?"complete":sts==-2?"timed-out":"failed");
935 return sts? SCSI_FAILED : SCSI_SUCCESS;
940 * Error handlers called from the eh thread (one thread per HBA).
942 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
944 return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
947 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
949 return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
952 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
954 return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
957 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
959 return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
963 * Tune device queuing depth, according to various limits.
965 static void sym_tune_dev_queuing(struct sym_hcb *np, int target, int lun, u_short reqtags)
967 struct sym_tcb *tp = &np->target[target];
968 struct sym_lcb *lp = sym_lp(np, tp, lun);
974 oldtags = lp->s.reqtags;
976 if (reqtags > lp->s.scdev_depth)
977 reqtags = lp->s.scdev_depth;
979 lp->started_limit = reqtags ? reqtags : 2;
981 lp->s.reqtags = reqtags;
983 if (reqtags != oldtags) {
984 printf_info("%s:%d:%d: "
985 "tagged command queuing %s, command queue depth %d.\n",
986 sym_name(np), target, lun,
987 lp->s.reqtags ? "enabled" : "disabled",
993 * Linux select queue depths function
995 #define DEF_DEPTH (sym_driver_setup.max_tag)
996 #define ALL_TARGETS -2
1001 static int device_queue_depth(struct sym_hcb *np, int target, int lun)
1004 char *p = sym_driver_setup.tag_ctrl;
1010 while ((c = *p++) != 0) {
1011 v = simple_strtoul(p, &ep, 0);
1020 t = (target == v) ? v : NO_TARGET;
1025 u = (lun == v) ? v : NO_LUN;
1028 if (h == np->s.unit &&
1029 (t == ALL_TARGETS || t == target) &&
1030 (u == ALL_LUNS || u == lun))
1046 * Linux entry point for device queue sizing.
1048 static int sym53c8xx_slave_configure(struct scsi_device *device)
1050 struct Scsi_Host *host = device->host;
1054 int reqtags, depth_to_use;
1056 np = ((struct host_data *) host->hostdata)->ncb;
1057 tp = &np->target[device->id];
1061 * Allocate the LCB if not yet.
1062 * If it fail, we may well be in the sh*t. :)
1064 lp = sym_alloc_lcb(np, device->id, device->lun);
1071 lp->curr_flags = lp->user_flags;
1074 * Select queue depth from driver setup.
1075 * Donnot use more than configured by user.
1077 * Donnot use more than our maximum.
1079 reqtags = device_queue_depth(np, device->id, device->lun);
1080 if (reqtags > tp->usrtags)
1081 reqtags = tp->usrtags;
1082 if (!device->tagged_supported)
1084 #if 1 /* Avoid to locally queue commands for no good reasons */
1085 if (reqtags > SYM_CONF_MAX_TAG)
1086 reqtags = SYM_CONF_MAX_TAG;
1087 depth_to_use = (reqtags ? reqtags : 2);
1089 depth_to_use = (reqtags ? SYM_CONF_MAX_TAG : 2);
1091 scsi_adjust_queue_depth(device,
1092 (device->tagged_supported ?
1093 MSG_SIMPLE_TAG : 0),
1095 lp->s.scdev_depth = depth_to_use;
1096 sym_tune_dev_queuing(np, device->id, device->lun, reqtags);
1098 if (!spi_initial_dv(device->sdev_target))
1099 spi_dv_device(device);
1105 * Linux entry point for info() function
1107 static const char *sym53c8xx_info (struct Scsi_Host *host)
1109 return sym_driver_name();
1113 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1115 * Proc file system stuff
1117 * A read operation returns adapter information.
1118 * A write operation is a control command.
1119 * The string is parsed in the driver code and the command is passed
1120 * to the sym_usercmd() function.
1123 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1132 #define UC_SETSYNC 10
1133 #define UC_SETTAGS 11
1134 #define UC_SETDEBUG 12
1135 #define UC_SETWIDE 14
1136 #define UC_SETFLAG 15
1137 #define UC_SETVERBOSE 17
1138 #define UC_RESETDEV 18
1139 #define UC_CLEARDEV 19
1141 static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
1149 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1151 sym_debug_flags = uc->data;
1155 np->verbose = uc->data;
1159 * We assume that other commands apply to targets.
1160 * This should always be the case and avoid the below
1161 * 4 lines to be repeated 6 times.
1163 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
1164 if (!((uc->target >> t) & 1))
1166 tp = &np->target[t];
1171 if (!uc->data || uc->data >= 255) {
1172 tp->tinfo.goal.options = 0;
1173 tp->tinfo.goal.offset = 0;
1176 if (uc->data <= 9 && np->minsync_dt) {
1177 if (uc->data < np->minsync_dt)
1178 uc->data = np->minsync_dt;
1179 tp->tinfo.goal.options = PPR_OPT_MASK;
1180 tp->tinfo.goal.width = 1;
1181 tp->tinfo.goal.period = uc->data;
1182 tp->tinfo.goal.offset = np->maxoffs_dt;
1184 if (uc->data < np->minsync)
1185 uc->data = np->minsync;
1186 tp->tinfo.goal.options = 0;
1187 tp->tinfo.goal.period = uc->data;
1188 tp->tinfo.goal.offset = np->maxoffs;
1192 tp->tinfo.goal.width = uc->data ? 1 : 0;
1195 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
1196 sym_tune_dev_queuing(np, t,l, uc->data);
1200 np->istat_sem = SEM;
1201 OUTB (nc_istat, SIGP|SEM);
1204 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
1205 struct sym_lcb *lp = sym_lp(np, tp, l);
1206 if (lp) lp->to_clear = 1;
1208 np->istat_sem = SEM;
1209 OUTB (nc_istat, SIGP|SEM);
1212 tp->usrflags = uc->data;
1220 #define digit_to_bin(c) ((c) - '0')
1222 static int skip_spaces(char *ptr, int len)
1226 for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
1231 static int get_int_arg(char *ptr, int len, u_long *pv)
1236 for (v = 0, cnt = len; cnt > 0 && (c = *ptr++) && isdigit(c); cnt--) {
1237 v = (v * 10) + digit_to_bin(c);
1246 static int is_keyword(char *ptr, int len, char *verb)
1248 int verb_len = strlen(verb);
1250 if (len >= verb_len && !memcmp(verb, ptr, verb_len))
1257 #define SKIP_SPACES(min_spaces) \
1258 if ((arg_len = skip_spaces(ptr, len)) < (min_spaces)) \
1260 ptr += arg_len; len -= arg_len;
1262 #define GET_INT_ARG(v) \
1263 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
1265 ptr += arg_len; len -= arg_len;
1269 * Parse a control command
1272 static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
1276 struct sym_usrcmd cmd, *uc = &cmd;
1280 bzero(uc, sizeof(*uc));
1282 if (len > 0 && ptr[len-1] == '\n')
1285 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1286 uc->cmd = UC_SETSYNC;
1287 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1288 uc->cmd = UC_SETTAGS;
1289 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1290 uc->cmd = UC_SETVERBOSE;
1291 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1292 uc->cmd = UC_SETWIDE;
1293 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1294 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1295 uc->cmd = UC_SETDEBUG;
1297 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1298 uc->cmd = UC_SETFLAG;
1299 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1300 uc->cmd = UC_RESETDEV;
1301 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1302 uc->cmd = UC_CLEARDEV;
1306 #ifdef DEBUG_PROC_INFO
1307 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1312 ptr += arg_len; len -= arg_len;
1322 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1323 ptr += arg_len; len -= arg_len;
1326 GET_INT_ARG(target);
1327 uc->target = (1<<target);
1328 #ifdef DEBUG_PROC_INFO
1329 printk("sym_user_command: target=%ld\n", target);
1341 GET_INT_ARG(uc->data);
1342 #ifdef DEBUG_PROC_INFO
1343 printk("sym_user_command: data=%ld\n", uc->data);
1346 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1350 if ((arg_len = is_keyword(ptr, len, "alloc")))
1351 uc->data |= DEBUG_ALLOC;
1352 else if ((arg_len = is_keyword(ptr, len, "phase")))
1353 uc->data |= DEBUG_PHASE;
1354 else if ((arg_len = is_keyword(ptr, len, "queue")))
1355 uc->data |= DEBUG_QUEUE;
1356 else if ((arg_len = is_keyword(ptr, len, "result")))
1357 uc->data |= DEBUG_RESULT;
1358 else if ((arg_len = is_keyword(ptr, len, "scatter")))
1359 uc->data |= DEBUG_SCATTER;
1360 else if ((arg_len = is_keyword(ptr, len, "script")))
1361 uc->data |= DEBUG_SCRIPT;
1362 else if ((arg_len = is_keyword(ptr, len, "tiny")))
1363 uc->data |= DEBUG_TINY;
1364 else if ((arg_len = is_keyword(ptr, len, "timing")))
1365 uc->data |= DEBUG_TIMING;
1366 else if ((arg_len = is_keyword(ptr, len, "nego")))
1367 uc->data |= DEBUG_NEGO;
1368 else if ((arg_len = is_keyword(ptr, len, "tags")))
1369 uc->data |= DEBUG_TAGS;
1370 else if ((arg_len = is_keyword(ptr, len, "pointer")))
1371 uc->data |= DEBUG_POINTER;
1374 ptr += arg_len; len -= arg_len;
1376 #ifdef DEBUG_PROC_INFO
1377 printk("sym_user_command: data=%ld\n", uc->data);
1380 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1384 if ((arg_len = is_keyword(ptr, len, "no_disc")))
1385 uc->data &= ~SYM_DISC_ENABLED;
1388 ptr += arg_len; len -= arg_len;
1398 unsigned long flags;
1400 spin_lock_irqsave(np->s.host->host_lock, flags);
1401 sym_exec_user_command (np, uc);
1402 spin_unlock_irqrestore(np->s.host->host_lock, flags);
1407 #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1410 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1412 * Informations through the proc file system.
1421 static void copy_mem_info(struct info_str *info, char *data, int len)
1423 if (info->pos + len > info->length)
1424 len = info->length - info->pos;
1426 if (info->pos + len < info->offset) {
1430 if (info->pos < info->offset) {
1431 data += (info->offset - info->pos);
1432 len -= (info->offset - info->pos);
1436 memcpy(info->buffer + info->pos, data, len);
1441 static int copy_info(struct info_str *info, char *fmt, ...)
1447 va_start(args, fmt);
1448 len = vsprintf(buf, fmt, args);
1451 copy_mem_info(info, buf, len);
1456 * Copy formatted information into the input buffer.
1458 static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
1460 struct info_str info;
1464 info.offset = offset;
1467 copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1468 "revision id 0x%x\n",
1469 np->s.chip_name, np->device_id, np->revision_id);
1470 copy_info(&info, "At PCI address %s, "
1476 pci_name(np->s.device),
1478 __irq_itoa(np->s.irq));
1482 copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1483 (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1484 np->maxwide ? "Wide" : "Narrow",
1485 np->minsync_dt ? ", DT capable" : "");
1487 copy_info(&info, "Max. started commands %d, "
1488 "max. commands per LUN %d\n",
1489 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1491 return info.pos > info.offset? info.pos - info.offset : 0;
1493 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1496 * Entry point of the scsi proc fs of the driver.
1497 * - func = 0 means read (returns adapter infos)
1498 * - func = 1 means write (not yet merget from sym53c8xx)
1500 static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
1501 char **start, off_t offset, int length, int func)
1503 struct host_data *host_data;
1504 struct sym_hcb *np = NULL;
1507 host_data = (struct host_data *) host->hostdata;
1508 np = host_data->ncb;
1513 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1514 retv = sym_user_command(np, buffer, length);
1521 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1522 retv = sym_host_info(np, buffer, offset, length);
1530 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1533 * Free controller resources.
1535 static void sym_free_resources(struct sym_hcb *np)
1538 * Free O/S specific resources.
1541 free_irq(np->s.irq, np);
1542 #ifndef SYM_CONF_IOMAPPED
1544 iounmap(np->s.mmio_va);
1547 iounmap(np->s.ram_va);
1549 * Free O/S independent resources.
1553 sym_mfree_dma(np, sizeof(*np), "HCB");
1557 * Ask/tell the system about DMA addressing.
1559 static int sym_setup_bus_dma_mask(struct sym_hcb *np)
1561 #if SYM_CONF_DMA_ADDRESSING_MODE == 0
1562 if (pci_set_dma_mask(np->s.device, 0xffffffffUL))
1565 #if SYM_CONF_DMA_ADDRESSING_MODE == 1
1566 #define PciDmaMask 0xffffffffffULL
1567 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
1568 #define PciDmaMask 0xffffffffffffffffULL
1570 if (np->features & FE_DAC) {
1571 if (!pci_set_dma_mask(np->s.device, PciDmaMask)) {
1573 printf_info("%s: using 64 bit DMA addressing\n",
1576 if (pci_set_dma_mask(np->s.device, 0xffffffffUL))
1585 printf_warning("%s: 32 BIT DMA ADDRESSING NOT SUPPORTED\n",
1591 * Host attach and initialisations.
1593 * Allocate host data and ncb structure.
1594 * Remap MMIO region.
1595 * Do chip initialization.
1596 * If all is OK, install interrupt handling and
1597 * start the timer daemon.
1599 static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
1600 int unit, struct sym_device *dev)
1602 struct host_data *host_data;
1603 struct sym_hcb *np = NULL;
1604 struct Scsi_Host *instance = NULL;
1605 unsigned long flags;
1609 "sym%d: <%s> rev 0x%x at pci %s "
1615 unit, dev->chip.name, dev->chip.revision_id,
1616 pci_name(dev->pdev),
1618 __irq_itoa(dev->s.irq));
1624 * Get the firmware for this chip.
1626 fw = sym_find_firmware(&dev->chip);
1631 * Allocate host_data structure
1633 instance = scsi_host_alloc(tpnt, sizeof(*host_data));
1636 host_data = (struct host_data *) instance->hostdata;
1639 * Allocate immediately the host control block,
1640 * since we are only expecting to succeed. :)
1641 * We keep track in the HCB of all the resources that
1642 * are to be released on error.
1644 np = __sym_calloc_dma(dev->pdev, sizeof(*np), "HCB");
1647 np->s.device = dev->pdev;
1648 np->bus_dmat = dev->pdev; /* Result in 1 DMA pool per HBA */
1649 host_data->ncb = np;
1650 np->s.host = instance;
1652 pci_set_drvdata(dev->pdev, np);
1655 * Copy some useful infos to the HCB.
1657 np->hcb_ba = vtobus(np);
1658 np->verbose = sym_driver_setup.verbose;
1659 np->s.device = dev->pdev;
1661 np->device_id = dev->chip.device_id;
1662 np->revision_id = dev->chip.revision_id;
1663 np->features = dev->chip.features;
1664 np->clock_divn = dev->chip.nr_divisor;
1665 np->maxoffs = dev->chip.offset_max;
1666 np->maxburst = dev->chip.burst_max;
1667 np->myaddr = dev->host_id;
1672 strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1673 sprintf(np->s.inst_name, "sym%d", np->s.unit);
1676 * Ask/tell the system about DMA addressing.
1678 if (sym_setup_bus_dma_mask(np))
1682 * Try to map the controller chip to
1683 * virtual and physical memory.
1685 np->mmio_ba = (u32)dev->s.base;
1686 np->s.io_ws = (np->features & FE_IO256)? 256 : 128;
1688 #ifndef SYM_CONF_IOMAPPED
1689 np->s.mmio_va = ioremap(dev->s.base_c, np->s.io_ws);
1690 if (!np->s.mmio_va) {
1691 printf_err("%s: can't map PCI MMIO region\n", sym_name(np));
1693 } else if (sym_verbose > 1)
1694 printf_info("%s: using memory mapped IO\n", sym_name(np));
1695 #endif /* !defined SYM_CONF_IOMAPPED */
1697 np->s.io_port = dev->s.io_port;
1700 * Map on-chip RAM if present and supported.
1702 if (!(np->features & FE_RAM))
1704 if (dev->s.base_2) {
1705 np->ram_ba = (u32)dev->s.base_2;
1706 if (np->features & FE_RAM8K)
1710 np->s.ram_va = ioremap(dev->s.base_2_c, np->ram_ws);
1711 if (!np->s.ram_va) {
1712 printf_err("%s: can't map PCI MEMORY region\n",
1719 * Perform O/S independent stuff.
1721 if (sym_hcb_attach(np, fw, dev->nvram))
1726 * Install the interrupt handler.
1727 * If we synchonize the C code with SCRIPTS on interrupt,
1728 * we donnot want to share the INTR line at all.
1730 if (request_irq(dev->s.irq, sym53c8xx_intr, SA_SHIRQ,
1732 printf_err("%s: request irq %d failure\n",
1733 sym_name(np), dev->s.irq);
1736 np->s.irq = dev->s.irq;
1739 * After SCSI devices have been opened, we cannot
1740 * reset the bus safely, so we do it here.
1742 spin_lock_irqsave(instance->host_lock, flags);
1743 if (sym_reset_scsi_bus(np, 0))
1747 * Start the SCRIPTS.
1749 sym_start_up (np, 1);
1752 * Start the timer daemon
1754 init_timer(&np->s.timer);
1755 np->s.timer.data = (unsigned long) np;
1756 np->s.timer.function = sym53c8xx_timer;
1761 * Fill Linux host instance structure
1762 * and return success.
1764 instance->max_channel = 0;
1765 instance->this_id = np->myaddr;
1766 instance->max_id = np->maxwide ? 16 : 8;
1767 instance->max_lun = SYM_CONF_MAX_LUN;
1768 #ifndef SYM_CONF_IOMAPPED
1769 instance->base = (unsigned long) np->s.mmio_va;
1771 instance->irq = np->s.irq;
1772 instance->unique_id = np->s.io_port;
1773 instance->io_port = np->s.io_port;
1774 instance->n_io_port = np->s.io_ws;
1775 instance->dma_channel = 0;
1776 instance->cmd_per_lun = SYM_CONF_MAX_TAG;
1777 instance->can_queue = (SYM_CONF_MAX_START-2);
1778 instance->sg_tablesize = SYM_CONF_MAX_SG;
1779 instance->max_cmd_len = 16;
1780 BUG_ON(sym2_transport_template == NULL);
1781 instance->transportt = sym2_transport_template;
1783 spin_unlock_irqrestore(instance->host_lock, flags);
1788 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1789 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1790 spin_unlock_irqrestore(instance->host_lock, flags);
1794 printf_info("%s: giving up ...\n", sym_name(np));
1796 sym_free_resources(np);
1797 scsi_host_put(instance);
1804 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1806 #if SYM_CONF_NVRAM_SUPPORT
1807 static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1810 devp->device_id = devp->chip.device_id;
1814 * Get access to chip IO registers
1816 #ifndef SYM_CONF_IOMAPPED
1817 devp->s.mmio_va = ioremap(devp->s.base_c, 128);
1818 if (!devp->s.mmio_va)
1822 sym_read_nvram(devp, nvp);
1825 * Release access to chip IO registers
1827 #ifndef SYM_CONF_IOMAPPED
1828 iounmap(devp->s.mmio_va);
1832 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1835 #endif /* SYM_CONF_NVRAM_SUPPORT */
1837 static int __devinit sym_check_supported(struct sym_device *device)
1839 struct sym_pci_chip *chip;
1840 struct pci_dev *pdev = device->pdev;
1842 unsigned long io_port = device->s.io_port;
1843 unsigned long base = device->s.base;
1847 * If user excluded this chip, do not initialize it.
1850 for (i = 0 ; i < 8 ; i++) {
1851 if (sym_driver_setup.excludes[i] == io_port)
1857 * Check if the chip has been assigned resources we need.
1858 * XXX: can this still happen with Linux 2.6's PCI layer?
1860 #ifdef SYM_CONF_IOMAPPED
1862 printf_info("%s: IO base address disabled.\n",
1868 printf_info("%s: MMIO base address disabled.\n",
1875 * Check if the chip is supported. Then copy the chip description
1876 * to our device structure so we can make it match the actual device
1879 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1880 chip = sym_lookup_pci_chip_table(pdev->device, revision);
1882 printf_info("%s: device not supported\n", sym_name(device));
1885 memcpy(&device->chip, chip, sizeof(device->chip));
1886 device->chip.revision_id = revision;
1892 * Ignore Symbios chips controlled by various RAID controllers.
1893 * These controllers set value 0x52414944 at RAM end - 16.
1895 static int __devinit sym_check_raid(struct sym_device *device)
1897 unsigned long base_2_c = device->s.base_2_c;
1898 unsigned int ram_size, ram_val;
1899 void __iomem *ram_ptr;
1904 if (device->chip.features & FE_RAM8K)
1909 ram_ptr = ioremap(base_2_c, ram_size);
1913 ram_val = readl(ram_ptr + ram_size - 16);
1915 if (ram_val != 0x52414944)
1918 printf_info("%s: not initializing, driven by RAID controller.\n",
1923 static int __devinit sym_set_workarounds(struct sym_device *device)
1925 struct sym_pci_chip *chip = &device->chip;
1926 struct pci_dev *pdev = device->pdev;
1930 * (ITEM 12 of a DEL about the 896 I haven't yet).
1931 * We must ensure the chip will use WRITE AND INVALIDATE.
1932 * The revision number limit is for now arbitrary.
1934 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && chip->revision_id < 0x4) {
1935 chip->features |= (FE_WRIE | FE_CLSE);
1938 /* If the chip can do Memory Write Invalidate, enable it */
1939 if (chip->features & FE_WRIE) {
1940 if (pci_set_mwi(pdev))
1945 * Work around for errant bit in 895A. The 66Mhz
1946 * capable bit is set erroneously. Clear this bit.
1949 * Make sure Config space and Features agree.
1951 * Recall: writes are not normal to status register -
1952 * write a 1 to clear and a 0 to leave unchanged.
1953 * Can only reset bits.
1955 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1956 if (chip->features & FE_66MHZ) {
1957 if (!(status_reg & PCI_STATUS_66MHZ))
1958 chip->features &= ~FE_66MHZ;
1960 if (status_reg & PCI_STATUS_66MHZ) {
1961 status_reg = PCI_STATUS_66MHZ;
1962 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1963 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1971 * Read and check the PCI configuration for any detected NCR
1972 * boards and save data for attaching after all boards have
1975 static void __devinit
1976 sym_init_device(struct pci_dev *pdev, struct sym_device *device)
1978 unsigned long base, base_2;
1981 device->host_id = SYM_SETUP_HOST_ID;
1982 device->pdev = pdev;
1983 device->s.irq = pdev->irq;
1985 /* Choose some short name for this device */
1986 sprintf(device->s.inst_name, "sym.%d.%d.%d", pdev->bus->number,
1987 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1989 device->s.io_port = pdev->resource[0].start;
1991 device->s.base_c = pdev->resource[1].start;
1992 i = pci_get_base_address(pdev, 1, &base);
1993 device->s.base = base & PCI_BASE_ADDRESS_MEM_MASK;
1995 device->s.base_2_c = pdev->resource[i].start;
1996 pci_get_base_address(pdev, i, &base_2);
1997 device->s.base_2 = base_2 & PCI_BASE_ADDRESS_MEM_MASK;
2001 * The NCR PQS and PDS cards are constructed as a DEC bridge
2002 * behind which sits a proprietary NCR memory controller and
2003 * either four or two 53c875s as separate devices. We can tell
2004 * if an 875 is part of a PQS/PDS or not since if it is, it will
2005 * be on the same bus as the memory controller. In its usual
2006 * mode of operation, the 875s are slaved to the memory
2007 * controller for all transfers. To operate with the Linux
2008 * driver, the memory controller is disabled and the 875s
2009 * freed to function independently. The only wrinkle is that
2010 * the preset SCSI ID (which may be zero) must be read in from
2011 * a special configuration space register of the 875.
2013 void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
2018 for (slot = 0; slot < 256; slot++) {
2019 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
2021 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
2026 /* bit 1: allow individual 875 configuration */
2027 pci_read_config_byte(memc, 0x44, &tmp);
2028 if ((tmp & 0x2) == 0) {
2030 pci_write_config_byte(memc, 0x44, tmp);
2033 /* bit 2: drive individual 875 interrupts to the bus */
2034 pci_read_config_byte(memc, 0x45, &tmp);
2035 if ((tmp & 0x4) == 0) {
2037 pci_write_config_byte(memc, 0x45, tmp);
2044 pci_read_config_byte(pdev, 0x84, &tmp);
2045 sym_dev->host_id = tmp;
2049 * Called before unloading the module.
2051 * We have to free resources and halt the NCR chip.
2053 static int sym_detach(struct sym_hcb *np)
2055 printk("%s: detaching ...\n", sym_name(np));
2057 del_timer_sync(&np->s.timer);
2061 * We should use sym_soft_reset(), but we don't want to do
2062 * so, since we may not be safe if interrupts occur.
2064 printk("%s: resetting chip\n", sym_name(np));
2065 OUTB (nc_istat, SRST);
2069 sym_free_resources(np);
2075 * Driver host template.
2077 static struct scsi_host_template sym2_template = {
2078 .module = THIS_MODULE,
2079 .name = "sym53c8xx",
2080 .info = sym53c8xx_info,
2081 .queuecommand = sym53c8xx_queue_command,
2082 .slave_configure = sym53c8xx_slave_configure,
2083 .eh_abort_handler = sym53c8xx_eh_abort_handler,
2084 .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
2085 .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
2086 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
2088 .use_clustering = DISABLE_CLUSTERING,
2089 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
2090 .proc_info = sym53c8xx_proc_info,
2091 .proc_name = NAME53C8XX,
2095 static int attach_count;
2097 static int __devinit sym2_probe(struct pci_dev *pdev,
2098 const struct pci_device_id *ent)
2100 struct sym_device sym_dev;
2101 struct sym_nvram nvram;
2102 struct Scsi_Host *instance;
2104 memset(&sym_dev, 0, sizeof(sym_dev));
2105 memset(&nvram, 0, sizeof(nvram));
2107 if (pci_enable_device(pdev))
2110 pci_set_master(pdev);
2112 if (pci_request_regions(pdev, NAME53C8XX))
2115 sym_init_device(pdev, &sym_dev);
2116 if (sym_check_supported(&sym_dev))
2119 if (sym_check_raid(&sym_dev))
2120 goto leave; /* Don't disable the device */
2122 if (sym_set_workarounds(&sym_dev))
2125 sym_config_pqs(pdev, &sym_dev);
2127 sym_get_nvram(&sym_dev, &nvram);
2129 instance = sym_attach(&sym2_template, attach_count, &sym_dev);
2133 if (scsi_add_host(instance, &pdev->dev))
2135 scsi_scan_host(instance);
2142 sym_detach(pci_get_drvdata(pdev));
2144 pci_release_regions(pdev);
2146 pci_disable_device(pdev);
2151 static void __devexit sym2_remove(struct pci_dev *pdev)
2153 struct sym_hcb *np = pci_get_drvdata(pdev);
2154 struct Scsi_Host *host = np->s.host;
2156 scsi_remove_host(host);
2157 scsi_host_put(host);
2161 pci_release_regions(pdev);
2162 pci_disable_device(pdev);
2167 static void sym2_get_signalling(struct Scsi_Host *shost)
2169 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2170 enum spi_signal_type type;
2172 switch (np->scsi_mode) {
2174 type = SPI_SIGNAL_SE;
2177 type = SPI_SIGNAL_LVD;
2180 type = SPI_SIGNAL_HVD;
2183 type = SPI_SIGNAL_UNKNOWN;
2186 spi_signalling(shost) = type;
2189 static void sym2_get_offset(struct scsi_target *starget)
2191 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2192 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2193 struct sym_tcb *tp = &np->target[starget->id];
2195 spi_offset(starget) = tp->tinfo.curr.offset;
2198 static void sym2_set_offset(struct scsi_target *starget, int offset)
2200 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2201 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2202 struct sym_tcb *tp = &np->target[starget->id];
2204 tp->tinfo.goal.offset = offset;
2208 static void sym2_get_period(struct scsi_target *starget)
2210 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2211 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2212 struct sym_tcb *tp = &np->target[starget->id];
2214 spi_period(starget) = tp->tinfo.curr.period;
2217 static void sym2_set_period(struct scsi_target *starget, int period)
2219 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2220 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2221 struct sym_tcb *tp = &np->target[starget->id];
2223 /* have to have DT for these transfers */
2224 if (period <= np->minsync)
2225 tp->tinfo.goal.options |= PPR_OPT_DT;
2227 tp->tinfo.goal.period = period;
2230 static void sym2_get_width(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_width(starget) = tp->tinfo.curr.width ? 1 : 0;
2239 static void sym2_set_width(struct scsi_target *starget, int width)
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 /* It is illegal to have DT set on narrow transfers. If DT is
2246 * clear, we must also clear IU and QAS. */
2248 tp->tinfo.goal.options &= ~PPR_OPT_MASK;
2250 tp->tinfo.goal.width = width;
2253 static void sym2_get_dt(struct scsi_target *starget)
2255 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2256 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2257 struct sym_tcb *tp = &np->target[starget->id];
2259 spi_dt(starget) = (tp->tinfo.curr.options & PPR_OPT_DT) ? 1 : 0;
2262 static void sym2_set_dt(struct scsi_target *starget, int dt)
2264 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2265 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2266 struct sym_tcb *tp = &np->target[starget->id];
2268 /* We must clear QAS and IU if DT is clear */
2270 tp->tinfo.goal.options |= PPR_OPT_DT;
2272 tp->tinfo.goal.options &= ~PPR_OPT_MASK;
2275 static void sym2_get_iu(struct scsi_target *starget)
2277 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2278 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2279 struct sym_tcb *tp = &np->target[starget->id];
2281 spi_iu(starget) = (tp->tinfo.curr.options & PPR_OPT_IU) ? 1 : 0;
2284 static void sym2_set_iu(struct scsi_target *starget, int iu)
2286 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2287 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2288 struct sym_tcb *tp = &np->target[starget->id];
2291 tp->tinfo.goal.options |= PPR_OPT_IU | PPR_OPT_DT;
2293 tp->tinfo.goal.options &= ~PPR_OPT_IU;
2296 static void sym2_get_qas(struct scsi_target *starget)
2298 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2299 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2300 struct sym_tcb *tp = &np->target[starget->id];
2302 spi_qas(starget) = (tp->tinfo.curr.options & PPR_OPT_QAS) ? 1 : 0;
2305 static void sym2_set_qas(struct scsi_target *starget, int qas)
2307 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2308 struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
2309 struct sym_tcb *tp = &np->target[starget->id];
2312 tp->tinfo.goal.options |= PPR_OPT_QAS | PPR_OPT_DT;
2314 tp->tinfo.goal.options &= ~PPR_OPT_QAS;
2318 static struct spi_function_template sym2_transport_functions = {
2319 .set_offset = sym2_set_offset,
2320 .get_offset = sym2_get_offset,
2322 .set_period = sym2_set_period,
2323 .get_period = sym2_get_period,
2325 .set_width = sym2_set_width,
2326 .get_width = sym2_get_width,
2328 .get_dt = sym2_get_dt,
2329 .set_dt = sym2_set_dt,
2331 .get_iu = sym2_get_iu,
2332 .set_iu = sym2_set_iu,
2334 .get_qas = sym2_get_qas,
2335 .set_qas = sym2_set_qas,
2337 .get_signalling = sym2_get_signalling,
2340 static struct pci_device_id sym2_id_table[] __devinitdata = {
2341 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
2342 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2343 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
2344 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2345 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
2346 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2347 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
2348 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2349 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
2350 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2351 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
2352 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2353 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
2354 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2355 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
2356 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2357 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
2358 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2359 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2360 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2361 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2362 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2363 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
2364 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2365 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2366 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2367 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2368 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2369 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2370 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2371 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2372 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2373 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2374 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2378 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2380 static struct pci_driver sym2_driver = {
2382 .id_table = sym2_id_table,
2383 .probe = sym2_probe,
2384 .remove = __devexit_p(sym2_remove),
2387 static int __init sym2_init(void)
2391 sym2_setup_params();
2392 sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2393 if (!sym2_transport_template)
2396 error = pci_module_init(&sym2_driver);
2398 spi_release_transport(sym2_transport_template);
2402 static void __exit sym2_exit(void)
2404 pci_unregister_driver(&sym2_driver);
2405 spi_release_transport(sym2_transport_template);
2408 module_init(sym2_init);
2409 module_exit(sym2_exit);