2 * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
3 * of PCI-SCSI IO processors.
5 * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
6 * Copyright (c) 2003-2005 Matthew Wilcox <matthew@wil.cx>
8 * This driver is derived from the Linux sym53c8xx driver.
9 * Copyright (C) 1998-2000 Gerard Roudier
11 * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
12 * a port of the FreeBSD ncr driver to Linux-1.2.13.
14 * The original ncr driver has been written for 386bsd and FreeBSD by
15 * Wolfgang Stanglmeier <wolf@cologne.de>
16 * Stefan Esser <se@mi.Uni-Koeln.de>
17 * Copyright (C) 1994 Wolfgang Stanglmeier
19 * Other major contributions:
21 * NVRAM detection and reading.
22 * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
24 *-----------------------------------------------------------------------------
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #include <linux/ctype.h>
41 #include <linux/init.h>
42 #include <linux/interrupt.h>
43 #include <linux/module.h>
44 #include <linux/moduleparam.h>
45 #include <linux/spinlock.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_tcq.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_transport.h>
52 #include "sym_nvram.h"
54 #define NAME53C "sym53c"
55 #define NAME53C8XX "sym53c8xx"
57 /* SPARC just has to be different ... */
60 #define IRQ_PRM(x) __irq_itoa(x)
63 #define IRQ_PRM(x) (x)
66 struct sym_driver_setup sym_driver_setup
= SYM_LINUX_DRIVER_SETUP
;
67 unsigned int sym_debug_flags
= 0;
69 static char *excl_string
;
70 static char *safe_string
;
71 module_param_named(cmd_per_lun
, sym_driver_setup
.max_tag
, ushort
, 0);
72 module_param_string(tag_ctrl
, sym_driver_setup
.tag_ctrl
, 100, 0);
73 module_param_named(burst
, sym_driver_setup
.burst_order
, byte
, 0);
74 module_param_named(led
, sym_driver_setup
.scsi_led
, byte
, 0);
75 module_param_named(diff
, sym_driver_setup
.scsi_diff
, byte
, 0);
76 module_param_named(irqm
, sym_driver_setup
.irq_mode
, byte
, 0);
77 module_param_named(buschk
, sym_driver_setup
.scsi_bus_check
, byte
, 0);
78 module_param_named(hostid
, sym_driver_setup
.host_id
, byte
, 0);
79 module_param_named(verb
, sym_driver_setup
.verbose
, byte
, 0);
80 module_param_named(debug
, sym_debug_flags
, uint
, 0);
81 module_param_named(settle
, sym_driver_setup
.settle_delay
, byte
, 0);
82 module_param_named(nvram
, sym_driver_setup
.use_nvram
, byte
, 0);
83 module_param_named(excl
, excl_string
, charp
, 0);
84 module_param_named(safe
, safe_string
, charp
, 0);
86 MODULE_PARM_DESC(cmd_per_lun
, "The maximum number of tags to use by default");
87 MODULE_PARM_DESC(tag_ctrl
, "More detailed control over tags per LUN");
88 MODULE_PARM_DESC(burst
, "Maximum burst. 0 to disable, 255 to read from registers");
89 MODULE_PARM_DESC(led
, "Set to 1 to enable LED support");
90 MODULE_PARM_DESC(diff
, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
91 MODULE_PARM_DESC(irqm
, "0 for open drain, 1 to leave alone, 2 for totem pole");
92 MODULE_PARM_DESC(buschk
, "0 to not check, 1 for detach on error, 2 for warn on error");
93 MODULE_PARM_DESC(hostid
, "The SCSI ID to use for the host adapters");
94 MODULE_PARM_DESC(verb
, "0 for minimal verbosity, 1 for normal, 2 for excessive");
95 MODULE_PARM_DESC(debug
, "Set bits to enable debugging");
96 MODULE_PARM_DESC(settle
, "Settle delay in seconds. Default 3");
97 MODULE_PARM_DESC(nvram
, "Option currently not used");
98 MODULE_PARM_DESC(excl
, "List ioport addresses here to prevent controllers from being attached");
99 MODULE_PARM_DESC(safe
, "Set other settings to a \"safe mode\"");
101 MODULE_LICENSE("GPL");
102 MODULE_VERSION(SYM_VERSION
);
103 MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
104 MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
106 static void sym2_setup_params(void)
108 char *p
= excl_string
;
111 while (p
&& (xi
< 8)) {
113 int val
= (int) simple_strtoul(p
, &next_p
, 0);
114 sym_driver_setup
.excludes
[xi
++] = val
;
119 if (*safe_string
== 'y') {
120 sym_driver_setup
.max_tag
= 0;
121 sym_driver_setup
.burst_order
= 0;
122 sym_driver_setup
.scsi_led
= 0;
123 sym_driver_setup
.scsi_diff
= 1;
124 sym_driver_setup
.irq_mode
= 0;
125 sym_driver_setup
.scsi_bus_check
= 2;
126 sym_driver_setup
.host_id
= 7;
127 sym_driver_setup
.verbose
= 2;
128 sym_driver_setup
.settle_delay
= 10;
129 sym_driver_setup
.use_nvram
= 1;
130 } else if (*safe_string
!= 'n') {
131 printk(KERN_WARNING NAME53C8XX
"Ignoring parameter %s"
132 " passed to safe option", safe_string
);
138 * We used to try to deal with 64-bit BARs here, but don't any more.
139 * There are many parts of this driver which would need to be modified
140 * to handle a 64-bit base address, including scripts. I'm uncomfortable
141 * with making those changes when I have no way of testing it, so I'm
142 * just going to disable it.
144 * Note that some machines (eg HP rx8620 and Superdome) have bus addresses
145 * below 4GB and physical addresses above 4GB. These will continue to work.
148 pci_get_base_address(struct pci_dev
*pdev
, int index
, unsigned long *basep
)
152 #define PCI_BAR_OFFSET(index) (PCI_BASE_ADDRESS_0 + (index<<2))
154 pci_read_config_dword(pdev
, PCI_BAR_OFFSET(index
++), &tmp
);
156 if ((tmp
& 0x7) == PCI_BASE_ADDRESS_MEM_TYPE_64
) {
157 pci_read_config_dword(pdev
, PCI_BAR_OFFSET(index
++), &tmp
);
160 "BAR %d is 64-bit, disabling\n", index
- 1);
164 if ((base
& PCI_BASE_ADDRESS_SPACE
) == PCI_BASE_ADDRESS_SPACE_IO
) {
165 base
&= PCI_BASE_ADDRESS_IO_MASK
;
167 base
&= PCI_BASE_ADDRESS_MEM_MASK
;
172 #undef PCI_BAR_OFFSET
175 static struct scsi_transport_template
*sym2_transport_template
= NULL
;
178 * Used by the eh thread to wait for command completion.
179 * It is allocated on the eh thread stack.
182 struct completion done
;
183 struct timer_list timer
;
184 void (*old_done
)(struct scsi_cmnd
*);
190 * Driver private area in the SCSI command structure.
192 struct sym_ucmd
{ /* Override the SCSI pointer structure */
193 dma_addr_t data_mapping
;
195 struct sym_eh_wait
*eh_wait
;
198 #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
199 #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
201 static void __unmap_scsi_data(struct pci_dev
*pdev
, struct scsi_cmnd
*cmd
)
203 int dma_dir
= cmd
->sc_data_direction
;
205 switch(SYM_UCMD_PTR(cmd
)->data_mapped
) {
207 pci_unmap_sg(pdev
, cmd
->buffer
, cmd
->use_sg
, dma_dir
);
210 pci_unmap_single(pdev
, SYM_UCMD_PTR(cmd
)->data_mapping
,
211 cmd
->request_bufflen
, dma_dir
);
214 SYM_UCMD_PTR(cmd
)->data_mapped
= 0;
217 static dma_addr_t
__map_scsi_single_data(struct pci_dev
*pdev
, struct scsi_cmnd
*cmd
)
220 int dma_dir
= cmd
->sc_data_direction
;
222 mapping
= pci_map_single(pdev
, cmd
->request_buffer
,
223 cmd
->request_bufflen
, dma_dir
);
225 SYM_UCMD_PTR(cmd
)->data_mapped
= 1;
226 SYM_UCMD_PTR(cmd
)->data_mapping
= mapping
;
232 static int __map_scsi_sg_data(struct pci_dev
*pdev
, struct scsi_cmnd
*cmd
)
235 int dma_dir
= cmd
->sc_data_direction
;
237 use_sg
= pci_map_sg(pdev
, cmd
->buffer
, cmd
->use_sg
, dma_dir
);
239 SYM_UCMD_PTR(cmd
)->data_mapped
= 2;
240 SYM_UCMD_PTR(cmd
)->data_mapping
= use_sg
;
246 #define unmap_scsi_data(np, cmd) \
247 __unmap_scsi_data(np->s.device, cmd)
248 #define map_scsi_single_data(np, cmd) \
249 __map_scsi_single_data(np->s.device, cmd)
250 #define map_scsi_sg_data(np, cmd) \
251 __map_scsi_sg_data(np->s.device, cmd)
253 * Complete a pending CAM CCB.
255 void sym_xpt_done(struct sym_hcb
*np
, struct scsi_cmnd
*cmd
)
257 unmap_scsi_data(np
, cmd
);
261 static void sym_xpt_done2(struct sym_hcb
*np
, struct scsi_cmnd
*cmd
, int cam_status
)
263 sym_set_cam_status(cmd
, cam_status
);
264 sym_xpt_done(np
, cmd
);
269 * Tell the SCSI layer about a BUS RESET.
271 void sym_xpt_async_bus_reset(struct sym_hcb
*np
)
273 printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np
));
274 np
->s
.settle_time
= jiffies
+ sym_driver_setup
.settle_delay
* HZ
;
275 np
->s
.settle_time_valid
= 1;
276 if (sym_verbose
>= 2)
277 printf_info("%s: command processing suspended for %d seconds\n",
278 sym_name(np
), sym_driver_setup
.settle_delay
);
282 * Tell the SCSI layer about a BUS DEVICE RESET message sent.
284 void sym_xpt_async_sent_bdr(struct sym_hcb
*np
, int target
)
286 printf_notice("%s: TARGET %d has been reset.\n", sym_name(np
), target
);
290 * Choose the more appropriate CAM status if
291 * the IO encountered an extended error.
293 static int sym_xerr_cam_status(int cam_status
, int x_status
)
296 if (x_status
& XE_PARITY_ERR
)
297 cam_status
= DID_PARITY
;
298 else if (x_status
&(XE_EXTRA_DATA
|XE_SODL_UNRUN
|XE_SWIDE_OVRUN
))
299 cam_status
= DID_ERROR
;
300 else if (x_status
& XE_BAD_PHASE
)
301 cam_status
= DID_ERROR
;
303 cam_status
= DID_ERROR
;
309 * Build CAM result for a failed or auto-sensed IO.
311 void sym_set_cam_result_error(struct sym_hcb
*np
, struct sym_ccb
*cp
, int resid
)
313 struct scsi_cmnd
*cmd
= cp
->cmd
;
314 u_int cam_status
, scsi_status
, drv_status
;
318 scsi_status
= cp
->ssss_status
;
320 if (cp
->host_flags
& HF_SENSE
) {
321 scsi_status
= cp
->sv_scsi_status
;
322 resid
= cp
->sv_resid
;
323 if (sym_verbose
&& cp
->sv_xerr_status
)
324 sym_print_xerr(cmd
, cp
->sv_xerr_status
);
325 if (cp
->host_status
== HS_COMPLETE
&&
326 cp
->ssss_status
== S_GOOD
&&
327 cp
->xerr_status
== 0) {
328 cam_status
= sym_xerr_cam_status(DID_OK
,
330 drv_status
= DRIVER_SENSE
;
332 * Bounce back the sense data to user.
334 memset(&cmd
->sense_buffer
, 0, sizeof(cmd
->sense_buffer
));
335 memcpy(cmd
->sense_buffer
, cp
->sns_bbuf
,
336 min(sizeof(cmd
->sense_buffer
),
337 (size_t)SYM_SNS_BBUF_LEN
));
340 * If the device reports a UNIT ATTENTION condition
341 * due to a RESET condition, we should consider all
342 * disconnect CCBs for this unit as aborted.
346 p
= (u_char
*) cmd
->sense_data
;
347 if (p
[0]==0x70 && p
[2]==0x6 && p
[12]==0x29)
348 sym_clear_tasks(np
, DID_ABORT
,
349 cp
->target
,cp
->lun
, -1);
354 * Error return from our internal request sense. This
355 * is bad: we must clear the contingent allegiance
356 * condition otherwise the device will always return
357 * BUSY. Use a big stick.
359 sym_reset_scsi_target(np
, cmd
->device
->id
);
360 cam_status
= DID_ERROR
;
362 } else if (cp
->host_status
== HS_COMPLETE
) /* Bad SCSI status */
364 else if (cp
->host_status
== HS_SEL_TIMEOUT
) /* Selection timeout */
365 cam_status
= DID_NO_CONNECT
;
366 else if (cp
->host_status
== HS_UNEXPECTED
) /* Unexpected BUS FREE*/
367 cam_status
= DID_ERROR
;
368 else { /* Extended error */
370 sym_print_addr(cmd
, "COMMAND FAILED (%x %x %x).\n",
371 cp
->host_status
, cp
->ssss_status
,
375 * Set the most appropriate value for CAM status.
377 cam_status
= sym_xerr_cam_status(DID_ERROR
, cp
->xerr_status
);
380 cmd
->result
= (drv_status
<< 24) + (cam_status
<< 16) + scsi_status
;
385 * Build the scatter/gather array for an I/O.
388 static int sym_scatter_no_sglist(struct sym_hcb
*np
, struct sym_ccb
*cp
, struct scsi_cmnd
*cmd
)
390 struct sym_tblmove
*data
= &cp
->phys
.data
[SYM_CONF_MAX_SG
-1];
393 cp
->data_len
= cmd
->request_bufflen
;
395 if (cmd
->request_bufflen
) {
396 dma_addr_t baddr
= map_scsi_single_data(np
, cmd
);
398 sym_build_sge(np
, data
, baddr
, cmd
->request_bufflen
);
410 static int sym_scatter(struct sym_hcb
*np
, struct sym_ccb
*cp
, struct scsi_cmnd
*cmd
)
413 int use_sg
= (int) cmd
->use_sg
;
418 segment
= sym_scatter_no_sglist(np
, cp
, cmd
);
419 else if ((use_sg
= map_scsi_sg_data(np
, cmd
)) > 0) {
420 struct scatterlist
*scatter
= (struct scatterlist
*)cmd
->buffer
;
421 struct sym_tblmove
*data
;
423 if (use_sg
> SYM_CONF_MAX_SG
) {
424 unmap_scsi_data(np
, cmd
);
428 data
= &cp
->phys
.data
[SYM_CONF_MAX_SG
- use_sg
];
430 for (segment
= 0; segment
< use_sg
; segment
++) {
431 dma_addr_t baddr
= sg_dma_address(&scatter
[segment
]);
432 unsigned int len
= sg_dma_len(&scatter
[segment
]);
434 sym_build_sge(np
, &data
[segment
], baddr
, len
);
445 * Queue a SCSI command.
447 static int sym_queue_command(struct sym_hcb
*np
, struct scsi_cmnd
*cmd
)
449 struct scsi_device
*sdev
= cmd
->device
;
456 * Minimal checkings, so that we will not
457 * go outside our tables.
459 if (sdev
->id
== np
->myaddr
||
460 sdev
->id
>= SYM_CONF_MAX_TARGET
||
461 sdev
->lun
>= SYM_CONF_MAX_LUN
) {
462 sym_xpt_done2(np
, cmd
, CAM_DEV_NOT_THERE
);
467 * Retrieve the target descriptor.
469 tp
= &np
->target
[sdev
->id
];
472 * Complete the 1st INQUIRY command with error
473 * condition if the device is flagged NOSCAN
474 * at BOOT in the NVRAM. This may speed up
475 * the boot and maintain coherency with BIOS
476 * device numbering. Clearing the flag allows
477 * user to rescan skipped devices later.
478 * We also return error for devices not flagged
479 * for SCAN LUNS in the NVRAM since some mono-lun
480 * devices behave badly when asked for some non
481 * zero LUN. Btw, this is an absolute hack.:-)
483 if (cmd
->cmnd
[0] == 0x12 || cmd
->cmnd
[0] == 0x0) {
484 if ((tp
->usrflags
& SYM_SCAN_BOOT_DISABLED
) ||
485 ((tp
->usrflags
& SYM_SCAN_LUNS_DISABLED
) &&
487 tp
->usrflags
&= ~SYM_SCAN_BOOT_DISABLED
;
488 sym_xpt_done2(np
, cmd
, CAM_DEV_NOT_THERE
);
494 * Select tagged/untagged.
496 lp
= sym_lp(tp
, sdev
->lun
);
497 order
= (lp
&& lp
->s
.reqtags
) ? M_SIMPLE_TAG
: 0;
502 cp
= sym_get_ccb(np
, cmd
, order
);
504 return 1; /* Means resource shortage */
505 sym_queue_scsiio(np
, cmd
, cp
);
510 * Setup buffers and pointers that address the CDB.
512 static inline int sym_setup_cdb(struct sym_hcb
*np
, struct scsi_cmnd
*cmd
, struct sym_ccb
*cp
)
518 * CDB is 16 bytes max.
520 if (cmd
->cmd_len
> sizeof(cp
->cdb_buf
)) {
521 sym_set_cam_status(cp
->cmd
, CAM_REQ_INVALID
);
525 memcpy(cp
->cdb_buf
, cmd
->cmnd
, cmd
->cmd_len
);
526 cmd_ba
= CCB_BA (cp
, cdb_buf
[0]);
527 cmd_len
= cmd
->cmd_len
;
529 cp
->phys
.cmd
.addr
= cpu_to_scr(cmd_ba
);
530 cp
->phys
.cmd
.size
= cpu_to_scr(cmd_len
);
536 * Setup pointers that address the data and start the I/O.
538 int sym_setup_data_and_start(struct sym_hcb
*np
, struct scsi_cmnd
*cmd
, struct sym_ccb
*cp
)
541 struct sym_tcb
*tp
= &np
->target
[cp
->target
];
542 struct sym_lcb
*lp
= sym_lp(tp
, cp
->lun
);
547 if (sym_setup_cdb(np
, cmd
, cp
))
551 * No direction means no data.
553 dir
= cmd
->sc_data_direction
;
554 if (dir
!= DMA_NONE
) {
555 cp
->segments
= sym_scatter(np
, cp
, cmd
);
556 if (cp
->segments
< 0) {
557 if (cp
->segments
== -2)
558 sym_set_cam_status(cmd
, CAM_RESRC_UNAVAIL
);
560 sym_set_cam_status(cmd
, CAM_REQ_TOO_BIG
);
571 sym_setup_data_pointers(np
, cp
, dir
);
574 * When `#ifed 1', the code below makes the driver
575 * panic on the first attempt to write to a SCSI device.
576 * It is the first test we want to do after a driver
577 * change that does not seem obviously safe. :)
580 switch (cp
->cdb_buf
[0]) {
581 case 0x0A: case 0x2A: case 0xAA:
582 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
593 sym_start_next_ccbs(np
, lp
, 2);
595 sym_put_start_queue(np
, cp
);
599 sym_free_ccb(np
, cp
);
600 sym_xpt_done(np
, cmd
);
608 * Misused to keep the driver running when
609 * interrupts are not configured correctly.
611 static void sym_timer(struct sym_hcb
*np
)
613 unsigned long thistime
= jiffies
;
618 np
->s
.timer
.expires
= thistime
+ SYM_CONF_TIMER_INTERVAL
;
619 add_timer(&np
->s
.timer
);
622 * If we are resetting the ncr, wait for settle_time before
623 * clearing it. Then command processing will be resumed.
625 if (np
->s
.settle_time_valid
) {
626 if (time_before_eq(np
->s
.settle_time
, thistime
)) {
627 if (sym_verbose
>= 2 )
628 printk("%s: command processing resumed\n",
630 np
->s
.settle_time_valid
= 0;
636 * Nothing to do for now, but that may come.
638 if (np
->s
.lasttime
+ 4*HZ
< thistime
) {
639 np
->s
.lasttime
= thistime
;
642 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
644 * Some way-broken PCI bridges may lead to
645 * completions being lost when the clearing
646 * of the INTFLY flag by the CPU occurs
647 * concurrently with the chip raising this flag.
648 * If this ever happen, lost completions will
657 * PCI BUS error handler.
659 void sym_log_bus_error(struct sym_hcb
*np
)
662 pci_read_config_word(np
->s
.device
, PCI_STATUS
, &pci_sts
);
663 if (pci_sts
& 0xf900) {
664 pci_write_config_word(np
->s
.device
, PCI_STATUS
, pci_sts
);
665 printf("%s: PCI STATUS = 0x%04x\n",
666 sym_name(np
), pci_sts
& 0xf900);
671 * queuecommand method. Entered with the host adapter lock held and
672 * interrupts disabled.
674 static int sym53c8xx_queue_command(struct scsi_cmnd
*cmd
,
675 void (*done
)(struct scsi_cmnd
*))
677 struct sym_hcb
*np
= SYM_SOFTC_PTR(cmd
);
678 struct sym_ucmd
*ucp
= SYM_UCMD_PTR(cmd
);
681 cmd
->scsi_done
= done
;
682 memset(ucp
, 0, sizeof(*ucp
));
685 * Shorten our settle_time if needed for
686 * this command not to time out.
688 if (np
->s
.settle_time_valid
&& cmd
->timeout_per_command
) {
689 unsigned long tlimit
= jiffies
+ cmd
->timeout_per_command
;
690 tlimit
-= SYM_CONF_TIMER_INTERVAL
*2;
691 if (time_after(np
->s
.settle_time
, tlimit
)) {
692 np
->s
.settle_time
= tlimit
;
696 if (np
->s
.settle_time_valid
)
697 return SCSI_MLQUEUE_HOST_BUSY
;
699 sts
= sym_queue_command(np
, cmd
);
701 return SCSI_MLQUEUE_HOST_BUSY
;
706 * Linux entry point of the interrupt handler.
708 static irqreturn_t
sym53c8xx_intr(int irq
, void *dev_id
, struct pt_regs
* regs
)
711 struct sym_hcb
*np
= (struct sym_hcb
*)dev_id
;
713 if (DEBUG_FLAGS
& DEBUG_TINY
) printf_debug ("[");
715 spin_lock_irqsave(np
->s
.host
->host_lock
, flags
);
717 spin_unlock_irqrestore(np
->s
.host
->host_lock
, flags
);
719 if (DEBUG_FLAGS
& DEBUG_TINY
) printf_debug ("]\n");
725 * Linux entry point of the timer handler
727 static void sym53c8xx_timer(unsigned long npref
)
729 struct sym_hcb
*np
= (struct sym_hcb
*)npref
;
732 spin_lock_irqsave(np
->s
.host
->host_lock
, flags
);
734 spin_unlock_irqrestore(np
->s
.host
->host_lock
, flags
);
739 * What the eh thread wants us to perform.
741 #define SYM_EH_ABORT 0
742 #define SYM_EH_DEVICE_RESET 1
743 #define SYM_EH_BUS_RESET 2
744 #define SYM_EH_HOST_RESET 3
747 * What we will do regarding the involved SCSI command.
749 #define SYM_EH_DO_IGNORE 0
750 #define SYM_EH_DO_COMPLETE 1
751 #define SYM_EH_DO_WAIT 2
754 * Our general completion handler.
756 static void __sym_eh_done(struct scsi_cmnd
*cmd
, int timed_out
)
758 struct sym_eh_wait
*ep
= SYM_UCMD_PTR(cmd
)->eh_wait
;
762 /* Try to avoid a race here (not 100% safe) */
765 if (ep
->to_do
== SYM_EH_DO_WAIT
&& !del_timer(&ep
->timer
))
769 /* Revert everything */
770 SYM_UCMD_PTR(cmd
)->eh_wait
= NULL
;
771 cmd
->scsi_done
= ep
->old_done
;
773 /* Wake up the eh thread if it wants to sleep */
774 if (ep
->to_do
== SYM_EH_DO_WAIT
)
779 * scsi_done() alias when error recovery is in progress.
781 static void sym_eh_done(struct scsi_cmnd
*cmd
) { __sym_eh_done(cmd
, 0); }
784 * Some timeout handler to avoid waiting too long.
786 static void sym_eh_timeout(u_long p
) { __sym_eh_done((struct scsi_cmnd
*)p
, 1); }
789 * Generic method for our eh processing.
790 * The 'op' argument tells what we have to do.
792 static int sym_eh_handler(int op
, char *opname
, struct scsi_cmnd
*cmd
)
794 struct sym_hcb
*np
= SYM_SOFTC_PTR(cmd
);
796 int to_do
= SYM_EH_DO_IGNORE
;
798 struct sym_eh_wait eh
, *ep
= &eh
;
800 dev_warn(&cmd
->device
->sdev_gendev
, "%s operation started.\n", opname
);
802 /* This one is queued in some place -> to wait for completion */
803 FOR_EACH_QUEUED_ELEMENT(&np
->busy_ccbq
, qp
) {
804 struct sym_ccb
*cp
= sym_que_entry(qp
, struct sym_ccb
, link_ccbq
);
805 if (cp
->cmd
== cmd
) {
806 to_do
= SYM_EH_DO_WAIT
;
812 /* Prepare stuff to either ignore, complete or wait for completion */
815 case SYM_EH_DO_IGNORE
:
818 init_completion(&ep
->done
);
820 case SYM_EH_DO_COMPLETE
:
821 ep
->old_done
= cmd
->scsi_done
;
822 cmd
->scsi_done
= sym_eh_done
;
823 SYM_UCMD_PTR(cmd
)->eh_wait
= ep
;
826 /* Try to proceed the operation we have been asked for */
830 sts
= sym_abort_scsiio(np
, cmd
, 1);
832 case SYM_EH_DEVICE_RESET
:
833 sts
= sym_reset_scsi_target(np
, cmd
->device
->id
);
835 case SYM_EH_BUS_RESET
:
836 sym_reset_scsi_bus(np
, 1);
839 case SYM_EH_HOST_RESET
:
840 sym_reset_scsi_bus(np
, 0);
841 sym_start_up (np
, 1);
848 /* On error, restore everything and cross fingers :) */
850 SYM_UCMD_PTR(cmd
)->eh_wait
= NULL
;
851 cmd
->scsi_done
= ep
->old_done
;
852 to_do
= SYM_EH_DO_IGNORE
;
856 /* Complete the command with locks held as required by the driver */
857 if (to_do
== SYM_EH_DO_COMPLETE
)
858 sym_xpt_done2(np
, cmd
, CAM_REQ_ABORTED
);
860 /* Wait for completion with locks released, as required by kernel */
861 if (to_do
== SYM_EH_DO_WAIT
) {
862 init_timer(&ep
->timer
);
863 ep
->timer
.expires
= jiffies
+ (5*HZ
);
864 ep
->timer
.function
= sym_eh_timeout
;
865 ep
->timer
.data
= (u_long
)cmd
;
866 ep
->timed_out
= 1; /* Be pessimistic for once :) */
867 add_timer(&ep
->timer
);
868 spin_unlock_irq(np
->s
.host
->host_lock
);
869 wait_for_completion(&ep
->done
);
870 spin_lock_irq(np
->s
.host
->host_lock
);
874 dev_warn(&cmd
->device
->sdev_gendev
, "%s operation %s.\n", opname
,
875 sts
==0 ? "complete" :sts
==-2 ? "timed-out" : "failed");
876 return sts
? SCSI_FAILED
: SCSI_SUCCESS
;
881 * Error handlers called from the eh thread (one thread per HBA).
883 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd
*cmd
)
885 return sym_eh_handler(SYM_EH_ABORT
, "ABORT", cmd
);
888 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd
*cmd
)
890 return sym_eh_handler(SYM_EH_DEVICE_RESET
, "DEVICE RESET", cmd
);
893 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd
*cmd
)
895 return sym_eh_handler(SYM_EH_BUS_RESET
, "BUS RESET", cmd
);
898 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd
*cmd
)
900 return sym_eh_handler(SYM_EH_HOST_RESET
, "HOST RESET", cmd
);
904 * Tune device queuing depth, according to various limits.
906 static void sym_tune_dev_queuing(struct sym_tcb
*tp
, int lun
, u_short reqtags
)
908 struct sym_lcb
*lp
= sym_lp(tp
, lun
);
914 oldtags
= lp
->s
.reqtags
;
916 if (reqtags
> lp
->s
.scdev_depth
)
917 reqtags
= lp
->s
.scdev_depth
;
919 lp
->started_limit
= reqtags
? reqtags
: 2;
921 lp
->s
.reqtags
= reqtags
;
923 if (reqtags
!= oldtags
) {
924 dev_info(&tp
->sdev
->sdev_target
->dev
,
925 "tagged command queuing %s, command queue depth %d.\n",
926 lp
->s
.reqtags
? "enabled" : "disabled",
932 * Linux select queue depths function
934 #define DEF_DEPTH (sym_driver_setup.max_tag)
935 #define ALL_TARGETS -2
940 static int device_queue_depth(struct sym_hcb
*np
, int target
, int lun
)
943 char *p
= sym_driver_setup
.tag_ctrl
;
949 while ((c
= *p
++) != 0) {
950 v
= simple_strtoul(p
, &ep
, 0);
959 t
= (target
== v
) ? v
: NO_TARGET
;
964 u
= (lun
== v
) ? v
: NO_LUN
;
967 if (h
== np
->s
.unit
&&
968 (t
== ALL_TARGETS
|| t
== target
) &&
969 (u
== ALL_LUNS
|| u
== lun
))
984 static int sym53c8xx_slave_alloc(struct scsi_device
*device
)
986 struct sym_hcb
*np
= sym_get_hcb(device
->host
);
987 struct sym_tcb
*tp
= &np
->target
[device
->id
];
994 static void sym53c8xx_slave_destroy(struct scsi_device
*device
)
996 struct sym_hcb
*np
= sym_get_hcb(device
->host
);
997 struct sym_tcb
*tp
= &np
->target
[device
->id
];
998 if (tp
->sdev
== device
)
1003 * Linux entry point for device queue sizing.
1005 static int sym53c8xx_slave_configure(struct scsi_device
*device
)
1007 struct sym_hcb
*np
= sym_get_hcb(device
->host
);
1008 struct sym_tcb
*tp
= &np
->target
[device
->id
];
1010 int reqtags
, depth_to_use
;
1013 * Allocate the LCB if not yet.
1014 * If it fail, we may well be in the sh*t. :)
1016 lp
= sym_alloc_lcb(np
, device
->id
, device
->lun
);
1023 lp
->curr_flags
= lp
->user_flags
;
1026 * Select queue depth from driver setup.
1027 * Donnot use more than configured by user.
1029 * Donnot use more than our maximum.
1031 reqtags
= device_queue_depth(np
, device
->id
, device
->lun
);
1032 if (reqtags
> tp
->usrtags
)
1033 reqtags
= tp
->usrtags
;
1034 if (!device
->tagged_supported
)
1036 #if 1 /* Avoid to locally queue commands for no good reasons */
1037 if (reqtags
> SYM_CONF_MAX_TAG
)
1038 reqtags
= SYM_CONF_MAX_TAG
;
1039 depth_to_use
= (reqtags
? reqtags
: 2);
1041 depth_to_use
= (reqtags
? SYM_CONF_MAX_TAG
: 2);
1043 scsi_adjust_queue_depth(device
,
1044 (device
->tagged_supported
?
1045 MSG_SIMPLE_TAG
: 0),
1047 lp
->s
.scdev_depth
= depth_to_use
;
1048 sym_tune_dev_queuing(tp
, device
->lun
, reqtags
);
1050 if (!spi_initial_dv(device
->sdev_target
))
1051 spi_dv_device(device
);
1057 * Linux entry point for info() function
1059 static const char *sym53c8xx_info (struct Scsi_Host
*host
)
1061 return SYM_DRIVER_NAME
;
1065 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1067 * Proc file system stuff
1069 * A read operation returns adapter information.
1070 * A write operation is a control command.
1071 * The string is parsed in the driver code and the command is passed
1072 * to the sym_usercmd() function.
1075 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1084 #define UC_SETSYNC 10
1085 #define UC_SETTAGS 11
1086 #define UC_SETDEBUG 12
1087 #define UC_SETWIDE 14
1088 #define UC_SETFLAG 15
1089 #define UC_SETVERBOSE 17
1090 #define UC_RESETDEV 18
1091 #define UC_CLEARDEV 19
1093 static void sym_exec_user_command (struct sym_hcb
*np
, struct sym_usrcmd
*uc
)
1101 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1103 sym_debug_flags
= uc
->data
;
1107 np
->verbose
= uc
->data
;
1111 * We assume that other commands apply to targets.
1112 * This should always be the case and avoid the below
1113 * 4 lines to be repeated 6 times.
1115 for (t
= 0; t
< SYM_CONF_MAX_TARGET
; t
++) {
1116 if (!((uc
->target
>> t
) & 1))
1118 tp
= &np
->target
[t
];
1123 if (!uc
->data
|| uc
->data
>= 255) {
1124 tp
->tgoal
.iu
= tp
->tgoal
.dt
=
1126 tp
->tgoal
.offset
= 0;
1127 } else if (uc
->data
<= 9 && np
->minsync_dt
) {
1128 if (uc
->data
< np
->minsync_dt
)
1129 uc
->data
= np
->minsync_dt
;
1130 tp
->tgoal
.iu
= tp
->tgoal
.dt
=
1132 tp
->tgoal
.width
= 1;
1133 tp
->tgoal
.period
= uc
->data
;
1134 tp
->tgoal
.offset
= np
->maxoffs_dt
;
1136 if (uc
->data
< np
->minsync
)
1137 uc
->data
= np
->minsync
;
1138 tp
->tgoal
.iu
= tp
->tgoal
.dt
=
1140 tp
->tgoal
.period
= uc
->data
;
1141 tp
->tgoal
.offset
= np
->maxoffs
;
1143 tp
->tgoal
.check_nego
= 1;
1146 tp
->tgoal
.width
= uc
->data
? 1 : 0;
1147 tp
->tgoal
.check_nego
= 1;
1150 for (l
= 0; l
< SYM_CONF_MAX_LUN
; l
++)
1151 sym_tune_dev_queuing(tp
, l
, uc
->data
);
1155 np
->istat_sem
= SEM
;
1156 OUTB(np
, nc_istat
, SIGP
|SEM
);
1159 for (l
= 0; l
< SYM_CONF_MAX_LUN
; l
++) {
1160 struct sym_lcb
*lp
= sym_lp(tp
, l
);
1161 if (lp
) lp
->to_clear
= 1;
1163 np
->istat_sem
= SEM
;
1164 OUTB(np
, nc_istat
, SIGP
|SEM
);
1167 tp
->usrflags
= uc
->data
;
1175 static int skip_spaces(char *ptr
, int len
)
1179 for (cnt
= len
; cnt
> 0 && (c
= *ptr
++) && isspace(c
); cnt
--);
1184 static int get_int_arg(char *ptr
, int len
, u_long
*pv
)
1188 *pv
= simple_strtoul(ptr
, &end
, 10);
1192 static int is_keyword(char *ptr
, int len
, char *verb
)
1194 int verb_len
= strlen(verb
);
1196 if (len
>= verb_len
&& !memcmp(verb
, ptr
, verb_len
))
1202 #define SKIP_SPACES(ptr, len) \
1203 if ((arg_len = skip_spaces(ptr, len)) < 1) \
1205 ptr += arg_len; len -= arg_len;
1207 #define GET_INT_ARG(ptr, len, v) \
1208 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
1210 ptr += arg_len; len -= arg_len;
1214 * Parse a control command
1217 static int sym_user_command(struct sym_hcb
*np
, char *buffer
, int length
)
1221 struct sym_usrcmd cmd
, *uc
= &cmd
;
1225 memset(uc
, 0, sizeof(*uc
));
1227 if (len
> 0 && ptr
[len
-1] == '\n')
1230 if ((arg_len
= is_keyword(ptr
, len
, "setsync")) != 0)
1231 uc
->cmd
= UC_SETSYNC
;
1232 else if ((arg_len
= is_keyword(ptr
, len
, "settags")) != 0)
1233 uc
->cmd
= UC_SETTAGS
;
1234 else if ((arg_len
= is_keyword(ptr
, len
, "setverbose")) != 0)
1235 uc
->cmd
= UC_SETVERBOSE
;
1236 else if ((arg_len
= is_keyword(ptr
, len
, "setwide")) != 0)
1237 uc
->cmd
= UC_SETWIDE
;
1238 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1239 else if ((arg_len
= is_keyword(ptr
, len
, "setdebug")) != 0)
1240 uc
->cmd
= UC_SETDEBUG
;
1242 else if ((arg_len
= is_keyword(ptr
, len
, "setflag")) != 0)
1243 uc
->cmd
= UC_SETFLAG
;
1244 else if ((arg_len
= is_keyword(ptr
, len
, "resetdev")) != 0)
1245 uc
->cmd
= UC_RESETDEV
;
1246 else if ((arg_len
= is_keyword(ptr
, len
, "cleardev")) != 0)
1247 uc
->cmd
= UC_CLEARDEV
;
1251 #ifdef DEBUG_PROC_INFO
1252 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len
, uc
->cmd
);
1257 ptr
+= arg_len
; len
-= arg_len
;
1266 SKIP_SPACES(ptr
, len
);
1267 if ((arg_len
= is_keyword(ptr
, len
, "all")) != 0) {
1268 ptr
+= arg_len
; len
-= arg_len
;
1271 GET_INT_ARG(ptr
, len
, target
);
1272 uc
->target
= (1<<target
);
1273 #ifdef DEBUG_PROC_INFO
1274 printk("sym_user_command: target=%ld\n", target
);
1285 SKIP_SPACES(ptr
, len
);
1286 GET_INT_ARG(ptr
, len
, uc
->data
);
1287 #ifdef DEBUG_PROC_INFO
1288 printk("sym_user_command: data=%ld\n", uc
->data
);
1291 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1294 SKIP_SPACES(ptr
, len
);
1295 if ((arg_len
= is_keyword(ptr
, len
, "alloc")))
1296 uc
->data
|= DEBUG_ALLOC
;
1297 else if ((arg_len
= is_keyword(ptr
, len
, "phase")))
1298 uc
->data
|= DEBUG_PHASE
;
1299 else if ((arg_len
= is_keyword(ptr
, len
, "queue")))
1300 uc
->data
|= DEBUG_QUEUE
;
1301 else if ((arg_len
= is_keyword(ptr
, len
, "result")))
1302 uc
->data
|= DEBUG_RESULT
;
1303 else if ((arg_len
= is_keyword(ptr
, len
, "scatter")))
1304 uc
->data
|= DEBUG_SCATTER
;
1305 else if ((arg_len
= is_keyword(ptr
, len
, "script")))
1306 uc
->data
|= DEBUG_SCRIPT
;
1307 else if ((arg_len
= is_keyword(ptr
, len
, "tiny")))
1308 uc
->data
|= DEBUG_TINY
;
1309 else if ((arg_len
= is_keyword(ptr
, len
, "timing")))
1310 uc
->data
|= DEBUG_TIMING
;
1311 else if ((arg_len
= is_keyword(ptr
, len
, "nego")))
1312 uc
->data
|= DEBUG_NEGO
;
1313 else if ((arg_len
= is_keyword(ptr
, len
, "tags")))
1314 uc
->data
|= DEBUG_TAGS
;
1315 else if ((arg_len
= is_keyword(ptr
, len
, "pointer")))
1316 uc
->data
|= DEBUG_POINTER
;
1319 ptr
+= arg_len
; len
-= arg_len
;
1321 #ifdef DEBUG_PROC_INFO
1322 printk("sym_user_command: data=%ld\n", uc
->data
);
1325 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1328 SKIP_SPACES(ptr
, len
);
1329 if ((arg_len
= is_keyword(ptr
, len
, "no_disc")))
1330 uc
->data
&= ~SYM_DISC_ENABLED
;
1333 ptr
+= arg_len
; len
-= arg_len
;
1343 unsigned long flags
;
1345 spin_lock_irqsave(np
->s
.host
->host_lock
, flags
);
1346 sym_exec_user_command (np
, uc
);
1347 spin_unlock_irqrestore(np
->s
.host
->host_lock
, flags
);
1352 #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1355 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1357 * Informations through the proc file system.
1366 static void copy_mem_info(struct info_str
*info
, char *data
, int len
)
1368 if (info
->pos
+ len
> info
->length
)
1369 len
= info
->length
- info
->pos
;
1371 if (info
->pos
+ len
< info
->offset
) {
1375 if (info
->pos
< info
->offset
) {
1376 data
+= (info
->offset
- info
->pos
);
1377 len
-= (info
->offset
- info
->pos
);
1381 memcpy(info
->buffer
+ info
->pos
, data
, len
);
1386 static int copy_info(struct info_str
*info
, char *fmt
, ...)
1392 va_start(args
, fmt
);
1393 len
= vsprintf(buf
, fmt
, args
);
1396 copy_mem_info(info
, buf
, len
);
1401 * Copy formatted information into the input buffer.
1403 static int sym_host_info(struct sym_hcb
*np
, char *ptr
, off_t offset
, int len
)
1405 struct info_str info
;
1409 info
.offset
= offset
;
1412 copy_info(&info
, "Chip " NAME53C
"%s, device id 0x%x, "
1413 "revision id 0x%x\n",
1414 np
->s
.chip_name
, np
->device_id
, np
->revision_id
);
1415 copy_info(&info
, "At PCI address %s, IRQ " IRQ_FMT
"\n",
1416 pci_name(np
->s
.device
), IRQ_PRM(np
->s
.irq
));
1417 copy_info(&info
, "Min. period factor %d, %s SCSI BUS%s\n",
1418 (int) (np
->minsync_dt
? np
->minsync_dt
: np
->minsync
),
1419 np
->maxwide
? "Wide" : "Narrow",
1420 np
->minsync_dt
? ", DT capable" : "");
1422 copy_info(&info
, "Max. started commands %d, "
1423 "max. commands per LUN %d\n",
1424 SYM_CONF_MAX_START
, SYM_CONF_MAX_TAG
);
1426 return info
.pos
> info
.offset
? info
.pos
- info
.offset
: 0;
1428 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1431 * Entry point of the scsi proc fs of the driver.
1432 * - func = 0 means read (returns adapter infos)
1433 * - func = 1 means write (not yet merget from sym53c8xx)
1435 static int sym53c8xx_proc_info(struct Scsi_Host
*host
, char *buffer
,
1436 char **start
, off_t offset
, int length
, int func
)
1438 struct sym_hcb
*np
= sym_get_hcb(host
);
1442 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1443 retv
= sym_user_command(np
, buffer
, length
);
1450 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1451 retv
= sym_host_info(np
, buffer
, offset
, length
);
1459 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1462 * Free controller resources.
1464 static void sym_free_resources(struct sym_hcb
*np
, struct pci_dev
*pdev
)
1467 * Free O/S specific resources.
1470 free_irq(np
->s
.irq
, np
);
1472 pci_iounmap(pdev
, np
->s
.ioaddr
);
1474 pci_iounmap(pdev
, np
->s
.ramaddr
);
1476 * Free O/S independent resources.
1480 sym_mfree_dma(np
, sizeof(*np
), "HCB");
1484 * Ask/tell the system about DMA addressing.
1486 static int sym_setup_bus_dma_mask(struct sym_hcb
*np
)
1488 #if SYM_CONF_DMA_ADDRESSING_MODE > 0
1489 #if SYM_CONF_DMA_ADDRESSING_MODE == 1
1490 #define DMA_DAC_MASK 0x000000ffffffffffULL /* 40-bit */
1491 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
1492 #define DMA_DAC_MASK DMA_64BIT_MASK
1494 if ((np
->features
& FE_DAC
) &&
1495 !pci_set_dma_mask(np
->s
.device
, DMA_DAC_MASK
)) {
1501 if (!pci_set_dma_mask(np
->s
.device
, DMA_32BIT_MASK
))
1504 printf_warning("%s: No suitable DMA available\n", sym_name(np
));
1509 * Host attach and initialisations.
1511 * Allocate host data and ncb structure.
1512 * Remap MMIO region.
1513 * Do chip initialization.
1514 * If all is OK, install interrupt handling and
1515 * start the timer daemon.
1517 static struct Scsi_Host
* __devinit
sym_attach(struct scsi_host_template
*tpnt
,
1518 int unit
, struct sym_device
*dev
)
1520 struct host_data
*host_data
;
1521 struct sym_hcb
*np
= NULL
;
1522 struct Scsi_Host
*instance
= NULL
;
1523 struct pci_dev
*pdev
= dev
->pdev
;
1524 unsigned long flags
;
1528 "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT
"\n",
1529 unit
, dev
->chip
.name
, dev
->chip
.revision_id
,
1530 pci_name(pdev
), IRQ_PRM(pdev
->irq
));
1533 * Get the firmware for this chip.
1535 fw
= sym_find_firmware(&dev
->chip
);
1540 * Allocate host_data structure
1542 instance
= scsi_host_alloc(tpnt
, sizeof(*host_data
));
1545 host_data
= (struct host_data
*) instance
->hostdata
;
1548 * Allocate immediately the host control block,
1549 * since we are only expecting to succeed. :)
1550 * We keep track in the HCB of all the resources that
1551 * are to be released on error.
1553 np
= __sym_calloc_dma(&pdev
->dev
, sizeof(*np
), "HCB");
1556 np
->s
.device
= pdev
;
1557 np
->bus_dmat
= &pdev
->dev
; /* Result in 1 DMA pool per HBA */
1558 host_data
->ncb
= np
;
1559 np
->s
.host
= instance
;
1561 pci_set_drvdata(pdev
, np
);
1564 * Copy some useful infos to the HCB.
1566 np
->hcb_ba
= vtobus(np
);
1567 np
->verbose
= sym_driver_setup
.verbose
;
1568 np
->s
.device
= pdev
;
1570 np
->device_id
= dev
->chip
.device_id
;
1571 np
->revision_id
= dev
->chip
.revision_id
;
1572 np
->features
= dev
->chip
.features
;
1573 np
->clock_divn
= dev
->chip
.nr_divisor
;
1574 np
->maxoffs
= dev
->chip
.offset_max
;
1575 np
->maxburst
= dev
->chip
.burst_max
;
1576 np
->myaddr
= dev
->host_id
;
1581 strlcpy(np
->s
.chip_name
, dev
->chip
.name
, sizeof(np
->s
.chip_name
));
1582 sprintf(np
->s
.inst_name
, "sym%d", np
->s
.unit
);
1584 if (sym_setup_bus_dma_mask(np
))
1588 * Try to map the controller chip to
1589 * virtual and physical memory.
1591 np
->mmio_ba
= (u32
)dev
->mmio_base
;
1592 np
->s
.ioaddr
= dev
->s
.ioaddr
;
1593 np
->s
.ramaddr
= dev
->s
.ramaddr
;
1594 np
->s
.io_ws
= (np
->features
& FE_IO256
) ? 256 : 128;
1597 * Map on-chip RAM if present and supported.
1599 if (!(np
->features
& FE_RAM
))
1601 if (dev
->ram_base
) {
1602 np
->ram_ba
= (u32
)dev
->ram_base
;
1603 np
->ram_ws
= (np
->features
& FE_RAM8K
) ? 8192 : 4096;
1606 if (sym_hcb_attach(instance
, fw
, dev
->nvram
))
1610 * Install the interrupt handler.
1611 * If we synchonize the C code with SCRIPTS on interrupt,
1612 * we do not want to share the INTR line at all.
1614 if (request_irq(pdev
->irq
, sym53c8xx_intr
, SA_SHIRQ
, NAME53C8XX
, np
)) {
1615 printf_err("%s: request irq %d failure\n",
1616 sym_name(np
), pdev
->irq
);
1619 np
->s
.irq
= pdev
->irq
;
1622 * After SCSI devices have been opened, we cannot
1623 * reset the bus safely, so we do it here.
1625 spin_lock_irqsave(instance
->host_lock
, flags
);
1626 if (sym_reset_scsi_bus(np
, 0))
1630 * Start the SCRIPTS.
1632 sym_start_up (np
, 1);
1635 * Start the timer daemon
1637 init_timer(&np
->s
.timer
);
1638 np
->s
.timer
.data
= (unsigned long) np
;
1639 np
->s
.timer
.function
= sym53c8xx_timer
;
1644 * Fill Linux host instance structure
1645 * and return success.
1647 instance
->max_channel
= 0;
1648 instance
->this_id
= np
->myaddr
;
1649 instance
->max_id
= np
->maxwide
? 16 : 8;
1650 instance
->max_lun
= SYM_CONF_MAX_LUN
;
1651 instance
->unique_id
= pci_resource_start(pdev
, 0);
1652 instance
->cmd_per_lun
= SYM_CONF_MAX_TAG
;
1653 instance
->can_queue
= (SYM_CONF_MAX_START
-2);
1654 instance
->sg_tablesize
= SYM_CONF_MAX_SG
;
1655 instance
->max_cmd_len
= 16;
1656 BUG_ON(sym2_transport_template
== NULL
);
1657 instance
->transportt
= sym2_transport_template
;
1659 spin_unlock_irqrestore(instance
->host_lock
, flags
);
1664 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1665 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np
));
1666 spin_unlock_irqrestore(instance
->host_lock
, flags
);
1670 printf_info("%s: giving up ...\n", sym_name(np
));
1672 sym_free_resources(np
, pdev
);
1673 scsi_host_put(instance
);
1680 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1682 #if SYM_CONF_NVRAM_SUPPORT
1683 static void __devinit
sym_get_nvram(struct sym_device
*devp
, struct sym_nvram
*nvp
)
1686 devp
->device_id
= devp
->chip
.device_id
;
1689 sym_read_nvram(devp
, nvp
);
1692 static inline void sym_get_nvram(struct sym_device
*devp
, struct sym_nvram
*nvp
)
1695 #endif /* SYM_CONF_NVRAM_SUPPORT */
1697 static int __devinit
sym_check_supported(struct sym_device
*device
)
1699 struct sym_chip
*chip
;
1700 struct pci_dev
*pdev
= device
->pdev
;
1702 unsigned long io_port
= pci_resource_start(pdev
, 0);
1706 * If user excluded this chip, do not initialize it.
1707 * I hate this code so much. Must kill it.
1710 for (i
= 0 ; i
< 8 ; i
++) {
1711 if (sym_driver_setup
.excludes
[i
] == io_port
)
1717 * Check if the chip is supported. Then copy the chip description
1718 * to our device structure so we can make it match the actual device
1721 pci_read_config_byte(pdev
, PCI_CLASS_REVISION
, &revision
);
1722 chip
= sym_lookup_chip_table(pdev
->device
, revision
);
1724 dev_info(&pdev
->dev
, "device not supported\n");
1727 memcpy(&device
->chip
, chip
, sizeof(device
->chip
));
1728 device
->chip
.revision_id
= revision
;
1734 * Ignore Symbios chips controlled by various RAID controllers.
1735 * These controllers set value 0x52414944 at RAM end - 16.
1737 static int __devinit
sym_check_raid(struct sym_device
*device
)
1739 unsigned int ram_size
, ram_val
;
1741 if (!device
->s
.ramaddr
)
1744 if (device
->chip
.features
& FE_RAM8K
)
1749 ram_val
= readl(device
->s
.ramaddr
+ ram_size
- 16);
1750 if (ram_val
!= 0x52414944)
1753 dev_info(&device
->pdev
->dev
,
1754 "not initializing, driven by RAID controller.\n");
1758 static int __devinit
sym_set_workarounds(struct sym_device
*device
)
1760 struct sym_chip
*chip
= &device
->chip
;
1761 struct pci_dev
*pdev
= device
->pdev
;
1765 * (ITEM 12 of a DEL about the 896 I haven't yet).
1766 * We must ensure the chip will use WRITE AND INVALIDATE.
1767 * The revision number limit is for now arbitrary.
1769 if (pdev
->device
== PCI_DEVICE_ID_NCR_53C896
&& chip
->revision_id
< 0x4) {
1770 chip
->features
|= (FE_WRIE
| FE_CLSE
);
1773 /* If the chip can do Memory Write Invalidate, enable it */
1774 if (chip
->features
& FE_WRIE
) {
1775 if (pci_set_mwi(pdev
))
1780 * Work around for errant bit in 895A. The 66Mhz
1781 * capable bit is set erroneously. Clear this bit.
1784 * Make sure Config space and Features agree.
1786 * Recall: writes are not normal to status register -
1787 * write a 1 to clear and a 0 to leave unchanged.
1788 * Can only reset bits.
1790 pci_read_config_word(pdev
, PCI_STATUS
, &status_reg
);
1791 if (chip
->features
& FE_66MHZ
) {
1792 if (!(status_reg
& PCI_STATUS_66MHZ
))
1793 chip
->features
&= ~FE_66MHZ
;
1795 if (status_reg
& PCI_STATUS_66MHZ
) {
1796 status_reg
= PCI_STATUS_66MHZ
;
1797 pci_write_config_word(pdev
, PCI_STATUS
, status_reg
);
1798 pci_read_config_word(pdev
, PCI_STATUS
, &status_reg
);
1806 * Read and check the PCI configuration for any detected NCR
1807 * boards and save data for attaching after all boards have
1810 static void __devinit
1811 sym_init_device(struct pci_dev
*pdev
, struct sym_device
*device
)
1815 device
->host_id
= SYM_SETUP_HOST_ID
;
1816 device
->pdev
= pdev
;
1818 i
= pci_get_base_address(pdev
, 1, &device
->mmio_base
);
1819 pci_get_base_address(pdev
, i
, &device
->ram_base
);
1821 #ifndef CONFIG_SCSI_SYM53C8XX_IOMAPPED
1822 if (device
->mmio_base
)
1823 device
->s
.ioaddr
= pci_iomap(pdev
, 1,
1824 pci_resource_len(pdev
, 1));
1826 if (!device
->s
.ioaddr
)
1827 device
->s
.ioaddr
= pci_iomap(pdev
, 0,
1828 pci_resource_len(pdev
, 0));
1829 if (device
->ram_base
)
1830 device
->s
.ramaddr
= pci_iomap(pdev
, i
,
1831 pci_resource_len(pdev
, i
));
1835 * The NCR PQS and PDS cards are constructed as a DEC bridge
1836 * behind which sits a proprietary NCR memory controller and
1837 * either four or two 53c875s as separate devices. We can tell
1838 * if an 875 is part of a PQS/PDS or not since if it is, it will
1839 * be on the same bus as the memory controller. In its usual
1840 * mode of operation, the 875s are slaved to the memory
1841 * controller for all transfers. To operate with the Linux
1842 * driver, the memory controller is disabled and the 875s
1843 * freed to function independently. The only wrinkle is that
1844 * the preset SCSI ID (which may be zero) must be read in from
1845 * a special configuration space register of the 875.
1847 static void sym_config_pqs(struct pci_dev
*pdev
, struct sym_device
*sym_dev
)
1852 for (slot
= 0; slot
< 256; slot
++) {
1853 struct pci_dev
*memc
= pci_get_slot(pdev
->bus
, slot
);
1855 if (!memc
|| memc
->vendor
!= 0x101a || memc
->device
== 0x0009) {
1860 /* bit 1: allow individual 875 configuration */
1861 pci_read_config_byte(memc
, 0x44, &tmp
);
1862 if ((tmp
& 0x2) == 0) {
1864 pci_write_config_byte(memc
, 0x44, tmp
);
1867 /* bit 2: drive individual 875 interrupts to the bus */
1868 pci_read_config_byte(memc
, 0x45, &tmp
);
1869 if ((tmp
& 0x4) == 0) {
1871 pci_write_config_byte(memc
, 0x45, tmp
);
1878 pci_read_config_byte(pdev
, 0x84, &tmp
);
1879 sym_dev
->host_id
= tmp
;
1883 * Called before unloading the module.
1885 * We have to free resources and halt the NCR chip.
1887 static int sym_detach(struct sym_hcb
*np
, struct pci_dev
*pdev
)
1889 printk("%s: detaching ...\n", sym_name(np
));
1891 del_timer_sync(&np
->s
.timer
);
1895 * We should use sym_soft_reset(), but we don't want to do
1896 * so, since we may not be safe if interrupts occur.
1898 printk("%s: resetting chip\n", sym_name(np
));
1899 OUTB(np
, nc_istat
, SRST
);
1901 OUTB(np
, nc_istat
, 0);
1903 sym_free_resources(np
, pdev
);
1909 * Driver host template.
1911 static struct scsi_host_template sym2_template
= {
1912 .module
= THIS_MODULE
,
1913 .name
= "sym53c8xx",
1914 .info
= sym53c8xx_info
,
1915 .queuecommand
= sym53c8xx_queue_command
,
1916 .slave_alloc
= sym53c8xx_slave_alloc
,
1917 .slave_configure
= sym53c8xx_slave_configure
,
1918 .slave_destroy
= sym53c8xx_slave_destroy
,
1919 .eh_abort_handler
= sym53c8xx_eh_abort_handler
,
1920 .eh_device_reset_handler
= sym53c8xx_eh_device_reset_handler
,
1921 .eh_bus_reset_handler
= sym53c8xx_eh_bus_reset_handler
,
1922 .eh_host_reset_handler
= sym53c8xx_eh_host_reset_handler
,
1924 .use_clustering
= DISABLE_CLUSTERING
,
1925 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1926 .proc_info
= sym53c8xx_proc_info
,
1927 .proc_name
= NAME53C8XX
,
1931 static int attach_count
;
1933 static int __devinit
sym2_probe(struct pci_dev
*pdev
,
1934 const struct pci_device_id
*ent
)
1936 struct sym_device sym_dev
;
1937 struct sym_nvram nvram
;
1938 struct Scsi_Host
*instance
;
1940 memset(&sym_dev
, 0, sizeof(sym_dev
));
1941 memset(&nvram
, 0, sizeof(nvram
));
1943 if (pci_enable_device(pdev
))
1946 pci_set_master(pdev
);
1948 if (pci_request_regions(pdev
, NAME53C8XX
))
1951 sym_init_device(pdev
, &sym_dev
);
1952 if (sym_check_supported(&sym_dev
))
1955 if (sym_check_raid(&sym_dev
))
1956 goto leave
; /* Don't disable the device */
1958 if (sym_set_workarounds(&sym_dev
))
1961 sym_config_pqs(pdev
, &sym_dev
);
1963 sym_get_nvram(&sym_dev
, &nvram
);
1965 instance
= sym_attach(&sym2_template
, attach_count
, &sym_dev
);
1969 if (scsi_add_host(instance
, &pdev
->dev
))
1971 scsi_scan_host(instance
);
1978 sym_detach(pci_get_drvdata(pdev
), pdev
);
1980 pci_release_regions(pdev
);
1982 pci_disable_device(pdev
);
1987 static void __devexit
sym2_remove(struct pci_dev
*pdev
)
1989 struct sym_hcb
*np
= pci_get_drvdata(pdev
);
1990 struct Scsi_Host
*host
= np
->s
.host
;
1992 scsi_remove_host(host
);
1993 scsi_host_put(host
);
1995 sym_detach(np
, pdev
);
1997 pci_release_regions(pdev
);
1998 pci_disable_device(pdev
);
2003 static void sym2_get_signalling(struct Scsi_Host
*shost
)
2005 struct sym_hcb
*np
= sym_get_hcb(shost
);
2006 enum spi_signal_type type
;
2008 switch (np
->scsi_mode
) {
2010 type
= SPI_SIGNAL_SE
;
2013 type
= SPI_SIGNAL_LVD
;
2016 type
= SPI_SIGNAL_HVD
;
2019 type
= SPI_SIGNAL_UNKNOWN
;
2022 spi_signalling(shost
) = type
;
2025 static void sym2_set_offset(struct scsi_target
*starget
, int offset
)
2027 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
2028 struct sym_hcb
*np
= sym_get_hcb(shost
);
2029 struct sym_tcb
*tp
= &np
->target
[starget
->id
];
2031 tp
->tgoal
.offset
= offset
;
2032 tp
->tgoal
.check_nego
= 1;
2035 static void sym2_set_period(struct scsi_target
*starget
, int period
)
2037 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
2038 struct sym_hcb
*np
= sym_get_hcb(shost
);
2039 struct sym_tcb
*tp
= &np
->target
[starget
->id
];
2041 /* have to have DT for these transfers, but DT will also
2042 * set width, so check that this is allowed */
2043 if (period
<= np
->minsync
&& spi_width(starget
))
2046 tp
->tgoal
.period
= period
;
2047 tp
->tgoal
.check_nego
= 1;
2050 static void sym2_set_width(struct scsi_target
*starget
, int width
)
2052 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
2053 struct sym_hcb
*np
= sym_get_hcb(shost
);
2054 struct sym_tcb
*tp
= &np
->target
[starget
->id
];
2056 /* It is illegal to have DT set on narrow transfers. If DT is
2057 * clear, we must also clear IU and QAS. */
2059 tp
->tgoal
.iu
= tp
->tgoal
.dt
= tp
->tgoal
.qas
= 0;
2061 tp
->tgoal
.width
= width
;
2062 tp
->tgoal
.check_nego
= 1;
2065 static void sym2_set_dt(struct scsi_target
*starget
, int dt
)
2067 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
2068 struct sym_hcb
*np
= sym_get_hcb(shost
);
2069 struct sym_tcb
*tp
= &np
->target
[starget
->id
];
2071 /* We must clear QAS and IU if DT is clear */
2075 tp
->tgoal
.iu
= tp
->tgoal
.dt
= tp
->tgoal
.qas
= 0;
2076 tp
->tgoal
.check_nego
= 1;
2079 static void sym2_set_iu(struct scsi_target
*starget
, int iu
)
2081 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
2082 struct sym_hcb
*np
= sym_get_hcb(shost
);
2083 struct sym_tcb
*tp
= &np
->target
[starget
->id
];
2086 tp
->tgoal
.iu
= tp
->tgoal
.dt
= 1;
2089 tp
->tgoal
.check_nego
= 1;
2092 static void sym2_set_qas(struct scsi_target
*starget
, int qas
)
2094 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
2095 struct sym_hcb
*np
= sym_get_hcb(shost
);
2096 struct sym_tcb
*tp
= &np
->target
[starget
->id
];
2099 tp
->tgoal
.dt
= tp
->tgoal
.qas
= 1;
2102 tp
->tgoal
.check_nego
= 1;
2106 static struct spi_function_template sym2_transport_functions
= {
2107 .set_offset
= sym2_set_offset
,
2109 .set_period
= sym2_set_period
,
2111 .set_width
= sym2_set_width
,
2113 .set_dt
= sym2_set_dt
,
2115 .set_iu
= sym2_set_iu
,
2117 .set_qas
= sym2_set_qas
,
2119 .get_signalling
= sym2_get_signalling
,
2122 static struct pci_device_id sym2_id_table
[] __devinitdata
= {
2123 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_NCR_53C810
,
2124 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2125 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_NCR_53C820
,
2126 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL }, /* new */
2127 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_NCR_53C825
,
2128 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2129 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_NCR_53C815
,
2130 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2131 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_53C810AP
,
2132 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL }, /* new */
2133 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_NCR_53C860
,
2134 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2135 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_53C1510
,
2136 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2137 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_NCR_53C896
,
2138 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2139 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_NCR_53C895
,
2140 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2141 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_NCR_53C885
,
2142 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2143 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_NCR_53C875
,
2144 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2145 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_NCR_53C1510
,
2146 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL }, /* new */
2147 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_53C895A
,
2148 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2149 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_53C875A
,
2150 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2151 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_53C1010_33
,
2152 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2153 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_53C1010_66
,
2154 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2155 { PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_NCR_53C875J
,
2156 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
2160 MODULE_DEVICE_TABLE(pci
, sym2_id_table
);
2162 static struct pci_driver sym2_driver
= {
2164 .id_table
= sym2_id_table
,
2165 .probe
= sym2_probe
,
2166 .remove
= __devexit_p(sym2_remove
),
2169 static int __init
sym2_init(void)
2173 sym2_setup_params();
2174 sym2_transport_template
= spi_attach_transport(&sym2_transport_functions
);
2175 if (!sym2_transport_template
)
2178 error
= pci_register_driver(&sym2_driver
);
2180 spi_release_transport(sym2_transport_template
);
2184 static void __exit
sym2_exit(void)
2186 pci_unregister_driver(&sym2_driver
);
2187 spi_release_transport(sym2_transport_template
);
2190 module_init(sym2_init
);
2191 module_exit(sym2_exit
);