[SCSI] sym53c8xx: Don't disable interrupts in the interrupt handler
[linux-2.6/mini2440.git] / drivers / scsi / sym53c8xx_2 / sym_glue.c
blobb5e7c6478653c87ef5681d32f3fba5d9ac4846ef
1 /*
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>
51 #include "sym_glue.h"
52 #include "sym_nvram.h"
54 #define NAME53C "sym53c"
55 #define NAME53C8XX "sym53c8xx"
57 #define IRQ_FMT "%d"
58 #define IRQ_PRM(x) (x)
60 struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
61 unsigned int sym_debug_flags = 0;
63 static char *excl_string;
64 static char *safe_string;
65 module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
66 module_param_string(tag_ctrl, sym_driver_setup.tag_ctrl, 100, 0);
67 module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
68 module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
69 module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
70 module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
71 module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
72 module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
73 module_param_named(verb, sym_driver_setup.verbose, byte, 0);
74 module_param_named(debug, sym_debug_flags, uint, 0);
75 module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
76 module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
77 module_param_named(excl, excl_string, charp, 0);
78 module_param_named(safe, safe_string, charp, 0);
80 MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
81 MODULE_PARM_DESC(tag_ctrl, "More detailed control over tags per LUN");
82 MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
83 MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
84 MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
85 MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
86 MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
87 MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
88 MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
89 MODULE_PARM_DESC(debug, "Set bits to enable debugging");
90 MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
91 MODULE_PARM_DESC(nvram, "Option currently not used");
92 MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
93 MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(SYM_VERSION);
97 MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
98 MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
100 static void sym2_setup_params(void)
102 char *p = excl_string;
103 int xi = 0;
105 while (p && (xi < 8)) {
106 char *next_p;
107 int val = (int) simple_strtoul(p, &next_p, 0);
108 sym_driver_setup.excludes[xi++] = val;
109 p = next_p;
112 if (safe_string) {
113 if (*safe_string == 'y') {
114 sym_driver_setup.max_tag = 0;
115 sym_driver_setup.burst_order = 0;
116 sym_driver_setup.scsi_led = 0;
117 sym_driver_setup.scsi_diff = 1;
118 sym_driver_setup.irq_mode = 0;
119 sym_driver_setup.scsi_bus_check = 2;
120 sym_driver_setup.host_id = 7;
121 sym_driver_setup.verbose = 2;
122 sym_driver_setup.settle_delay = 10;
123 sym_driver_setup.use_nvram = 1;
124 } else if (*safe_string != 'n') {
125 printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
126 " passed to safe option", safe_string);
131 static struct scsi_transport_template *sym2_transport_template = NULL;
134 * Driver private area in the SCSI command structure.
136 struct sym_ucmd { /* Override the SCSI pointer structure */
137 unsigned char to_do; /* For error handling */
138 void (*old_done)(struct scsi_cmnd *); /* For error handling */
139 struct completion *eh_done; /* For error handling */
142 #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
143 #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
146 * Complete a pending CAM CCB.
148 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
150 scsi_dma_unmap(cmd);
151 cmd->scsi_done(cmd);
155 * Tell the SCSI layer about a BUS RESET.
157 void sym_xpt_async_bus_reset(struct sym_hcb *np)
159 printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
160 np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
161 np->s.settle_time_valid = 1;
162 if (sym_verbose >= 2)
163 printf_info("%s: command processing suspended for %d seconds\n",
164 sym_name(np), sym_driver_setup.settle_delay);
168 * Tell the SCSI layer about a BUS DEVICE RESET message sent.
170 void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
172 printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
176 * Choose the more appropriate CAM status if
177 * the IO encountered an extended error.
179 static int sym_xerr_cam_status(int cam_status, int x_status)
181 if (x_status) {
182 if (x_status & XE_PARITY_ERR)
183 cam_status = DID_PARITY;
184 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
185 cam_status = DID_ERROR;
186 else if (x_status & XE_BAD_PHASE)
187 cam_status = DID_ERROR;
188 else
189 cam_status = DID_ERROR;
191 return cam_status;
195 * Build CAM result for a failed or auto-sensed IO.
197 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
199 struct scsi_cmnd *cmd = cp->cmd;
200 u_int cam_status, scsi_status, drv_status;
202 drv_status = 0;
203 cam_status = DID_OK;
204 scsi_status = cp->ssss_status;
206 if (cp->host_flags & HF_SENSE) {
207 scsi_status = cp->sv_scsi_status;
208 resid = cp->sv_resid;
209 if (sym_verbose && cp->sv_xerr_status)
210 sym_print_xerr(cmd, cp->sv_xerr_status);
211 if (cp->host_status == HS_COMPLETE &&
212 cp->ssss_status == S_GOOD &&
213 cp->xerr_status == 0) {
214 cam_status = sym_xerr_cam_status(DID_OK,
215 cp->sv_xerr_status);
216 drv_status = DRIVER_SENSE;
218 * Bounce back the sense data to user.
220 memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
221 memcpy(cmd->sense_buffer, cp->sns_bbuf,
222 min(sizeof(cmd->sense_buffer),
223 (size_t)SYM_SNS_BBUF_LEN));
224 #if 0
226 * If the device reports a UNIT ATTENTION condition
227 * due to a RESET condition, we should consider all
228 * disconnect CCBs for this unit as aborted.
230 if (1) {
231 u_char *p;
232 p = (u_char *) cmd->sense_data;
233 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
234 sym_clear_tasks(np, DID_ABORT,
235 cp->target,cp->lun, -1);
237 #endif
238 } else {
240 * Error return from our internal request sense. This
241 * is bad: we must clear the contingent allegiance
242 * condition otherwise the device will always return
243 * BUSY. Use a big stick.
245 sym_reset_scsi_target(np, cmd->device->id);
246 cam_status = DID_ERROR;
248 } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
249 cam_status = DID_OK;
250 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
251 cam_status = DID_NO_CONNECT;
252 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
253 cam_status = DID_ERROR;
254 else { /* Extended error */
255 if (sym_verbose) {
256 sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
257 cp->host_status, cp->ssss_status,
258 cp->xerr_status);
261 * Set the most appropriate value for CAM status.
263 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
265 scsi_set_resid(cmd, resid);
266 cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
269 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
271 int segment;
272 int use_sg;
274 cp->data_len = 0;
276 use_sg = scsi_dma_map(cmd);
277 if (use_sg > 0) {
278 struct scatterlist *sg;
279 struct sym_tcb *tp = &np->target[cp->target];
280 struct sym_tblmove *data;
282 if (use_sg > SYM_CONF_MAX_SG) {
283 scsi_dma_unmap(cmd);
284 return -1;
287 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
289 scsi_for_each_sg(cmd, sg, use_sg, segment) {
290 dma_addr_t baddr = sg_dma_address(sg);
291 unsigned int len = sg_dma_len(sg);
293 if ((len & 1) && (tp->head.wval & EWS)) {
294 len++;
295 cp->odd_byte_adjustment++;
298 sym_build_sge(np, &data[segment], baddr, len);
299 cp->data_len += len;
301 } else {
302 segment = -2;
305 return segment;
309 * Queue a SCSI command.
311 static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
313 struct scsi_device *sdev = cmd->device;
314 struct sym_tcb *tp;
315 struct sym_lcb *lp;
316 struct sym_ccb *cp;
317 int order;
320 * Retrieve the target descriptor.
322 tp = &np->target[sdev->id];
325 * Select tagged/untagged.
327 lp = sym_lp(tp, sdev->lun);
328 order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
331 * Queue the SCSI IO.
333 cp = sym_get_ccb(np, cmd, order);
334 if (!cp)
335 return 1; /* Means resource shortage */
336 sym_queue_scsiio(np, cmd, cp);
337 return 0;
341 * Setup buffers and pointers that address the CDB.
343 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
345 memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
347 cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
348 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
350 return 0;
354 * Setup pointers that address the data and start the I/O.
356 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
358 u32 lastp, goalp;
359 int dir;
362 * Build the CDB.
364 if (sym_setup_cdb(np, cmd, cp))
365 goto out_abort;
368 * No direction means no data.
370 dir = cmd->sc_data_direction;
371 if (dir != DMA_NONE) {
372 cp->segments = sym_scatter(np, cp, cmd);
373 if (cp->segments < 0) {
374 sym_set_cam_status(cmd, DID_ERROR);
375 goto out_abort;
379 * No segments means no data.
381 if (!cp->segments)
382 dir = DMA_NONE;
383 } else {
384 cp->data_len = 0;
385 cp->segments = 0;
389 * Set the data pointer.
391 switch (dir) {
392 case DMA_BIDIRECTIONAL:
393 printk("%s: got DMA_BIDIRECTIONAL command", sym_name(np));
394 sym_set_cam_status(cmd, DID_ERROR);
395 goto out_abort;
396 case DMA_TO_DEVICE:
397 goalp = SCRIPTA_BA(np, data_out2) + 8;
398 lastp = goalp - 8 - (cp->segments * (2*4));
399 break;
400 case DMA_FROM_DEVICE:
401 cp->host_flags |= HF_DATA_IN;
402 goalp = SCRIPTA_BA(np, data_in2) + 8;
403 lastp = goalp - 8 - (cp->segments * (2*4));
404 break;
405 case DMA_NONE:
406 default:
407 lastp = goalp = SCRIPTB_BA(np, no_data);
408 break;
412 * Set all pointers values needed by SCRIPTS.
414 cp->phys.head.lastp = cpu_to_scr(lastp);
415 cp->phys.head.savep = cpu_to_scr(lastp);
416 cp->startp = cp->phys.head.savep;
417 cp->goalp = cpu_to_scr(goalp);
420 * When `#ifed 1', the code below makes the driver
421 * panic on the first attempt to write to a SCSI device.
422 * It is the first test we want to do after a driver
423 * change that does not seem obviously safe. :)
425 #if 0
426 switch (cp->cdb_buf[0]) {
427 case 0x0A: case 0x2A: case 0xAA:
428 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
429 break;
430 default:
431 break;
433 #endif
436 * activate this job.
438 sym_put_start_queue(np, cp);
439 return 0;
441 out_abort:
442 sym_free_ccb(np, cp);
443 sym_xpt_done(np, cmd);
444 return 0;
449 * timer daemon.
451 * Misused to keep the driver running when
452 * interrupts are not configured correctly.
454 static void sym_timer(struct sym_hcb *np)
456 unsigned long thistime = jiffies;
459 * Restart the timer.
461 np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
462 add_timer(&np->s.timer);
465 * If we are resetting the ncr, wait for settle_time before
466 * clearing it. Then command processing will be resumed.
468 if (np->s.settle_time_valid) {
469 if (time_before_eq(np->s.settle_time, thistime)) {
470 if (sym_verbose >= 2 )
471 printk("%s: command processing resumed\n",
472 sym_name(np));
473 np->s.settle_time_valid = 0;
475 return;
479 * Nothing to do for now, but that may come.
481 if (np->s.lasttime + 4*HZ < thistime) {
482 np->s.lasttime = thistime;
485 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
487 * Some way-broken PCI bridges may lead to
488 * completions being lost when the clearing
489 * of the INTFLY flag by the CPU occurs
490 * concurrently with the chip raising this flag.
491 * If this ever happen, lost completions will
492 * be reaped here.
494 sym_wakeup_done(np);
495 #endif
500 * PCI BUS error handler.
502 void sym_log_bus_error(struct sym_hcb *np)
504 u_short pci_sts;
505 pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
506 if (pci_sts & 0xf900) {
507 pci_write_config_word(np->s.device, PCI_STATUS, pci_sts);
508 printf("%s: PCI STATUS = 0x%04x\n",
509 sym_name(np), pci_sts & 0xf900);
514 * queuecommand method. Entered with the host adapter lock held and
515 * interrupts disabled.
517 static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
518 void (*done)(struct scsi_cmnd *))
520 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
521 struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
522 int sts = 0;
524 cmd->scsi_done = done;
525 memset(ucp, 0, sizeof(*ucp));
528 * Shorten our settle_time if needed for
529 * this command not to time out.
531 if (np->s.settle_time_valid && cmd->timeout_per_command) {
532 unsigned long tlimit = jiffies + cmd->timeout_per_command;
533 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
534 if (time_after(np->s.settle_time, tlimit)) {
535 np->s.settle_time = tlimit;
539 if (np->s.settle_time_valid)
540 return SCSI_MLQUEUE_HOST_BUSY;
542 sts = sym_queue_command(np, cmd);
543 if (sts)
544 return SCSI_MLQUEUE_HOST_BUSY;
545 return 0;
549 * Linux entry point of the interrupt handler.
551 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
553 struct sym_hcb *np = dev_id;
555 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
557 spin_lock(np->s.host->host_lock);
558 sym_interrupt(np);
559 spin_unlock(np->s.host->host_lock);
561 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
563 return IRQ_HANDLED;
567 * Linux entry point of the timer handler
569 static void sym53c8xx_timer(unsigned long npref)
571 struct sym_hcb *np = (struct sym_hcb *)npref;
572 unsigned long flags;
574 spin_lock_irqsave(np->s.host->host_lock, flags);
575 sym_timer(np);
576 spin_unlock_irqrestore(np->s.host->host_lock, flags);
581 * What the eh thread wants us to perform.
583 #define SYM_EH_ABORT 0
584 #define SYM_EH_DEVICE_RESET 1
585 #define SYM_EH_BUS_RESET 2
586 #define SYM_EH_HOST_RESET 3
589 * What we will do regarding the involved SCSI command.
591 #define SYM_EH_DO_IGNORE 0
592 #define SYM_EH_DO_WAIT 2
595 * scsi_done() alias when error recovery is in progress.
597 static void sym_eh_done(struct scsi_cmnd *cmd)
599 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
600 BUILD_BUG_ON(sizeof(struct scsi_pointer) < sizeof(struct sym_ucmd));
602 cmd->scsi_done = ucmd->old_done;
604 if (ucmd->to_do == SYM_EH_DO_WAIT)
605 complete(ucmd->eh_done);
609 * Generic method for our eh processing.
610 * The 'op' argument tells what we have to do.
612 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
614 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
615 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
616 struct Scsi_Host *host = cmd->device->host;
617 SYM_QUEHEAD *qp;
618 int to_do = SYM_EH_DO_IGNORE;
619 int sts = -1;
620 struct completion eh_done;
622 dev_warn(&cmd->device->sdev_gendev, "%s operation started.\n", opname);
624 spin_lock_irq(host->host_lock);
625 /* This one is queued in some place -> to wait for completion */
626 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
627 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
628 if (cp->cmd == cmd) {
629 to_do = SYM_EH_DO_WAIT;
630 break;
634 if (to_do == SYM_EH_DO_WAIT) {
635 init_completion(&eh_done);
636 ucmd->old_done = cmd->scsi_done;
637 ucmd->eh_done = &eh_done;
638 wmb();
639 cmd->scsi_done = sym_eh_done;
642 /* Try to proceed the operation we have been asked for */
643 sts = -1;
644 switch(op) {
645 case SYM_EH_ABORT:
646 sts = sym_abort_scsiio(np, cmd, 1);
647 break;
648 case SYM_EH_DEVICE_RESET:
649 sts = sym_reset_scsi_target(np, cmd->device->id);
650 break;
651 case SYM_EH_BUS_RESET:
652 sym_reset_scsi_bus(np, 1);
653 sts = 0;
654 break;
655 case SYM_EH_HOST_RESET:
656 sym_reset_scsi_bus(np, 0);
657 sym_start_up (np, 1);
658 sts = 0;
659 break;
660 default:
661 break;
664 /* On error, restore everything and cross fingers :) */
665 if (sts) {
666 cmd->scsi_done = ucmd->old_done;
667 to_do = SYM_EH_DO_IGNORE;
670 ucmd->to_do = to_do;
671 spin_unlock_irq(host->host_lock);
673 if (to_do == SYM_EH_DO_WAIT) {
674 if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
675 ucmd->to_do = SYM_EH_DO_IGNORE;
676 wmb();
677 sts = -2;
680 dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
681 sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
682 return sts ? SCSI_FAILED : SCSI_SUCCESS;
687 * Error handlers called from the eh thread (one thread per HBA).
689 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
691 return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
694 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
696 return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
699 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
701 return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
704 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
706 return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
710 * Tune device queuing depth, according to various limits.
712 static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
714 struct sym_lcb *lp = sym_lp(tp, lun);
715 u_short oldtags;
717 if (!lp)
718 return;
720 oldtags = lp->s.reqtags;
722 if (reqtags > lp->s.scdev_depth)
723 reqtags = lp->s.scdev_depth;
725 lp->s.reqtags = reqtags;
727 if (reqtags != oldtags) {
728 dev_info(&tp->starget->dev,
729 "tagged command queuing %s, command queue depth %d.\n",
730 lp->s.reqtags ? "enabled" : "disabled", reqtags);
735 * Linux select queue depths function
737 #define DEF_DEPTH (sym_driver_setup.max_tag)
738 #define ALL_TARGETS -2
739 #define NO_TARGET -1
740 #define ALL_LUNS -2
741 #define NO_LUN -1
743 static int device_queue_depth(struct sym_hcb *np, int target, int lun)
745 int c, h, t, u, v;
746 char *p = sym_driver_setup.tag_ctrl;
747 char *ep;
749 h = -1;
750 t = NO_TARGET;
751 u = NO_LUN;
752 while ((c = *p++) != 0) {
753 v = simple_strtoul(p, &ep, 0);
754 switch(c) {
755 case '/':
756 ++h;
757 t = ALL_TARGETS;
758 u = ALL_LUNS;
759 break;
760 case 't':
761 if (t != target)
762 t = (target == v) ? v : NO_TARGET;
763 u = ALL_LUNS;
764 break;
765 case 'u':
766 if (u != lun)
767 u = (lun == v) ? v : NO_LUN;
768 break;
769 case 'q':
770 if (h == np->s.unit &&
771 (t == ALL_TARGETS || t == target) &&
772 (u == ALL_LUNS || u == lun))
773 return v;
774 break;
775 case '-':
776 t = ALL_TARGETS;
777 u = ALL_LUNS;
778 break;
779 default:
780 break;
782 p = ep;
784 return DEF_DEPTH;
787 static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
789 struct sym_hcb *np = sym_get_hcb(sdev->host);
790 struct sym_tcb *tp = &np->target[sdev->id];
791 struct sym_lcb *lp;
793 if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
794 return -ENXIO;
796 tp->starget = sdev->sdev_target;
798 * Fail the device init if the device is flagged NOSCAN at BOOT in
799 * the NVRAM. This may speed up boot and maintain coherency with
800 * BIOS device numbering. Clearing the flag allows the user to
801 * rescan skipped devices later. We also return an error for
802 * devices not flagged for SCAN LUNS in the NVRAM since some single
803 * lun devices behave badly when asked for a non zero LUN.
806 if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
807 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
808 starget_printk(KERN_INFO, tp->starget,
809 "Scan at boot disabled in NVRAM\n");
810 return -ENXIO;
813 if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
814 if (sdev->lun != 0)
815 return -ENXIO;
816 starget_printk(KERN_INFO, tp->starget,
817 "Multiple LUNs disabled in NVRAM\n");
820 lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
821 if (!lp)
822 return -ENOMEM;
824 spi_min_period(tp->starget) = tp->usr_period;
825 spi_max_width(tp->starget) = tp->usr_width;
827 return 0;
831 * Linux entry point for device queue sizing.
833 static int sym53c8xx_slave_configure(struct scsi_device *sdev)
835 struct sym_hcb *np = sym_get_hcb(sdev->host);
836 struct sym_tcb *tp = &np->target[sdev->id];
837 struct sym_lcb *lp = sym_lp(tp, sdev->lun);
838 int reqtags, depth_to_use;
841 * Get user flags.
843 lp->curr_flags = lp->user_flags;
846 * Select queue depth from driver setup.
847 * Donnot use more than configured by user.
848 * Use at least 2.
849 * Donnot use more than our maximum.
851 reqtags = device_queue_depth(np, sdev->id, sdev->lun);
852 if (reqtags > tp->usrtags)
853 reqtags = tp->usrtags;
854 if (!sdev->tagged_supported)
855 reqtags = 0;
856 #if 1 /* Avoid to locally queue commands for no good reasons */
857 if (reqtags > SYM_CONF_MAX_TAG)
858 reqtags = SYM_CONF_MAX_TAG;
859 depth_to_use = (reqtags ? reqtags : 2);
860 #else
861 depth_to_use = (reqtags ? SYM_CONF_MAX_TAG : 2);
862 #endif
863 scsi_adjust_queue_depth(sdev,
864 (sdev->tagged_supported ?
865 MSG_SIMPLE_TAG : 0),
866 depth_to_use);
867 lp->s.scdev_depth = depth_to_use;
868 sym_tune_dev_queuing(tp, sdev->lun, reqtags);
870 if (!spi_initial_dv(sdev->sdev_target))
871 spi_dv_device(sdev);
873 return 0;
876 static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
878 struct sym_hcb *np = sym_get_hcb(sdev->host);
879 struct sym_lcb *lp = sym_lp(&np->target[sdev->id], sdev->lun);
881 if (lp->itlq_tbl)
882 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK * 4, "ITLQ_TBL");
883 kfree(lp->cb_tags);
884 sym_mfree_dma(lp, sizeof(*lp), "LCB");
888 * Linux entry point for info() function
890 static const char *sym53c8xx_info (struct Scsi_Host *host)
892 return SYM_DRIVER_NAME;
896 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
898 * Proc file system stuff
900 * A read operation returns adapter information.
901 * A write operation is a control command.
902 * The string is parsed in the driver code and the command is passed
903 * to the sym_usercmd() function.
906 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
908 struct sym_usrcmd {
909 u_long target;
910 u_long lun;
911 u_long data;
912 u_long cmd;
915 #define UC_SETSYNC 10
916 #define UC_SETTAGS 11
917 #define UC_SETDEBUG 12
918 #define UC_SETWIDE 14
919 #define UC_SETFLAG 15
920 #define UC_SETVERBOSE 17
921 #define UC_RESETDEV 18
922 #define UC_CLEARDEV 19
924 static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
926 struct sym_tcb *tp;
927 int t, l;
929 switch (uc->cmd) {
930 case 0: return;
932 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
933 case UC_SETDEBUG:
934 sym_debug_flags = uc->data;
935 break;
936 #endif
937 case UC_SETVERBOSE:
938 np->verbose = uc->data;
939 break;
940 default:
942 * We assume that other commands apply to targets.
943 * This should always be the case and avoid the below
944 * 4 lines to be repeated 6 times.
946 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
947 if (!((uc->target >> t) & 1))
948 continue;
949 tp = &np->target[t];
951 switch (uc->cmd) {
953 case UC_SETSYNC:
954 if (!uc->data || uc->data >= 255) {
955 tp->tgoal.iu = tp->tgoal.dt =
956 tp->tgoal.qas = 0;
957 tp->tgoal.offset = 0;
958 } else if (uc->data <= 9 && np->minsync_dt) {
959 if (uc->data < np->minsync_dt)
960 uc->data = np->minsync_dt;
961 tp->tgoal.iu = tp->tgoal.dt =
962 tp->tgoal.qas = 1;
963 tp->tgoal.width = 1;
964 tp->tgoal.period = uc->data;
965 tp->tgoal.offset = np->maxoffs_dt;
966 } else {
967 if (uc->data < np->minsync)
968 uc->data = np->minsync;
969 tp->tgoal.iu = tp->tgoal.dt =
970 tp->tgoal.qas = 0;
971 tp->tgoal.period = uc->data;
972 tp->tgoal.offset = np->maxoffs;
974 tp->tgoal.check_nego = 1;
975 break;
976 case UC_SETWIDE:
977 tp->tgoal.width = uc->data ? 1 : 0;
978 tp->tgoal.check_nego = 1;
979 break;
980 case UC_SETTAGS:
981 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
982 sym_tune_dev_queuing(tp, l, uc->data);
983 break;
984 case UC_RESETDEV:
985 tp->to_reset = 1;
986 np->istat_sem = SEM;
987 OUTB(np, nc_istat, SIGP|SEM);
988 break;
989 case UC_CLEARDEV:
990 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
991 struct sym_lcb *lp = sym_lp(tp, l);
992 if (lp) lp->to_clear = 1;
994 np->istat_sem = SEM;
995 OUTB(np, nc_istat, SIGP|SEM);
996 break;
997 case UC_SETFLAG:
998 tp->usrflags = uc->data;
999 break;
1002 break;
1006 static int skip_spaces(char *ptr, int len)
1008 int cnt, c;
1010 for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
1012 return (len - cnt);
1015 static int get_int_arg(char *ptr, int len, u_long *pv)
1017 char *end;
1019 *pv = simple_strtoul(ptr, &end, 10);
1020 return (end - ptr);
1023 static int is_keyword(char *ptr, int len, char *verb)
1025 int verb_len = strlen(verb);
1027 if (len >= verb_len && !memcmp(verb, ptr, verb_len))
1028 return verb_len;
1029 else
1030 return 0;
1033 #define SKIP_SPACES(ptr, len) \
1034 if ((arg_len = skip_spaces(ptr, len)) < 1) \
1035 return -EINVAL; \
1036 ptr += arg_len; len -= arg_len;
1038 #define GET_INT_ARG(ptr, len, v) \
1039 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
1040 return -EINVAL; \
1041 ptr += arg_len; len -= arg_len;
1045 * Parse a control command
1048 static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
1050 char *ptr = buffer;
1051 int len = length;
1052 struct sym_usrcmd cmd, *uc = &cmd;
1053 int arg_len;
1054 u_long target;
1056 memset(uc, 0, sizeof(*uc));
1058 if (len > 0 && ptr[len-1] == '\n')
1059 --len;
1061 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1062 uc->cmd = UC_SETSYNC;
1063 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1064 uc->cmd = UC_SETTAGS;
1065 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1066 uc->cmd = UC_SETVERBOSE;
1067 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1068 uc->cmd = UC_SETWIDE;
1069 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1070 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1071 uc->cmd = UC_SETDEBUG;
1072 #endif
1073 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1074 uc->cmd = UC_SETFLAG;
1075 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1076 uc->cmd = UC_RESETDEV;
1077 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1078 uc->cmd = UC_CLEARDEV;
1079 else
1080 arg_len = 0;
1082 #ifdef DEBUG_PROC_INFO
1083 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1084 #endif
1086 if (!arg_len)
1087 return -EINVAL;
1088 ptr += arg_len; len -= arg_len;
1090 switch(uc->cmd) {
1091 case UC_SETSYNC:
1092 case UC_SETTAGS:
1093 case UC_SETWIDE:
1094 case UC_SETFLAG:
1095 case UC_RESETDEV:
1096 case UC_CLEARDEV:
1097 SKIP_SPACES(ptr, len);
1098 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1099 ptr += arg_len; len -= arg_len;
1100 uc->target = ~0;
1101 } else {
1102 GET_INT_ARG(ptr, len, target);
1103 uc->target = (1<<target);
1104 #ifdef DEBUG_PROC_INFO
1105 printk("sym_user_command: target=%ld\n", target);
1106 #endif
1108 break;
1111 switch(uc->cmd) {
1112 case UC_SETVERBOSE:
1113 case UC_SETSYNC:
1114 case UC_SETTAGS:
1115 case UC_SETWIDE:
1116 SKIP_SPACES(ptr, len);
1117 GET_INT_ARG(ptr, len, uc->data);
1118 #ifdef DEBUG_PROC_INFO
1119 printk("sym_user_command: data=%ld\n", uc->data);
1120 #endif
1121 break;
1122 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1123 case UC_SETDEBUG:
1124 while (len > 0) {
1125 SKIP_SPACES(ptr, len);
1126 if ((arg_len = is_keyword(ptr, len, "alloc")))
1127 uc->data |= DEBUG_ALLOC;
1128 else if ((arg_len = is_keyword(ptr, len, "phase")))
1129 uc->data |= DEBUG_PHASE;
1130 else if ((arg_len = is_keyword(ptr, len, "queue")))
1131 uc->data |= DEBUG_QUEUE;
1132 else if ((arg_len = is_keyword(ptr, len, "result")))
1133 uc->data |= DEBUG_RESULT;
1134 else if ((arg_len = is_keyword(ptr, len, "scatter")))
1135 uc->data |= DEBUG_SCATTER;
1136 else if ((arg_len = is_keyword(ptr, len, "script")))
1137 uc->data |= DEBUG_SCRIPT;
1138 else if ((arg_len = is_keyword(ptr, len, "tiny")))
1139 uc->data |= DEBUG_TINY;
1140 else if ((arg_len = is_keyword(ptr, len, "timing")))
1141 uc->data |= DEBUG_TIMING;
1142 else if ((arg_len = is_keyword(ptr, len, "nego")))
1143 uc->data |= DEBUG_NEGO;
1144 else if ((arg_len = is_keyword(ptr, len, "tags")))
1145 uc->data |= DEBUG_TAGS;
1146 else if ((arg_len = is_keyword(ptr, len, "pointer")))
1147 uc->data |= DEBUG_POINTER;
1148 else
1149 return -EINVAL;
1150 ptr += arg_len; len -= arg_len;
1152 #ifdef DEBUG_PROC_INFO
1153 printk("sym_user_command: data=%ld\n", uc->data);
1154 #endif
1155 break;
1156 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1157 case UC_SETFLAG:
1158 while (len > 0) {
1159 SKIP_SPACES(ptr, len);
1160 if ((arg_len = is_keyword(ptr, len, "no_disc")))
1161 uc->data &= ~SYM_DISC_ENABLED;
1162 else
1163 return -EINVAL;
1164 ptr += arg_len; len -= arg_len;
1166 break;
1167 default:
1168 break;
1171 if (len)
1172 return -EINVAL;
1173 else {
1174 unsigned long flags;
1176 spin_lock_irqsave(np->s.host->host_lock, flags);
1177 sym_exec_user_command (np, uc);
1178 spin_unlock_irqrestore(np->s.host->host_lock, flags);
1180 return length;
1183 #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1186 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1188 * Informations through the proc file system.
1190 struct info_str {
1191 char *buffer;
1192 int length;
1193 int offset;
1194 int pos;
1197 static void copy_mem_info(struct info_str *info, char *data, int len)
1199 if (info->pos + len > info->length)
1200 len = info->length - info->pos;
1202 if (info->pos + len < info->offset) {
1203 info->pos += len;
1204 return;
1206 if (info->pos < info->offset) {
1207 data += (info->offset - info->pos);
1208 len -= (info->offset - info->pos);
1211 if (len > 0) {
1212 memcpy(info->buffer + info->pos, data, len);
1213 info->pos += len;
1217 static int copy_info(struct info_str *info, char *fmt, ...)
1219 va_list args;
1220 char buf[81];
1221 int len;
1223 va_start(args, fmt);
1224 len = vsprintf(buf, fmt, args);
1225 va_end(args);
1227 copy_mem_info(info, buf, len);
1228 return len;
1232 * Copy formatted information into the input buffer.
1234 static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
1236 struct info_str info;
1238 info.buffer = ptr;
1239 info.length = len;
1240 info.offset = offset;
1241 info.pos = 0;
1243 copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1244 "revision id 0x%x\n",
1245 np->s.chip_name, np->device_id, np->revision_id);
1246 copy_info(&info, "At PCI address %s, IRQ " IRQ_FMT "\n",
1247 pci_name(np->s.device), IRQ_PRM(np->s.device->irq));
1248 copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1249 (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1250 np->maxwide ? "Wide" : "Narrow",
1251 np->minsync_dt ? ", DT capable" : "");
1253 copy_info(&info, "Max. started commands %d, "
1254 "max. commands per LUN %d\n",
1255 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1257 return info.pos > info.offset? info.pos - info.offset : 0;
1259 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1262 * Entry point of the scsi proc fs of the driver.
1263 * - func = 0 means read (returns adapter infos)
1264 * - func = 1 means write (not yet merget from sym53c8xx)
1266 static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
1267 char **start, off_t offset, int length, int func)
1269 struct sym_hcb *np = sym_get_hcb(host);
1270 int retv;
1272 if (func) {
1273 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1274 retv = sym_user_command(np, buffer, length);
1275 #else
1276 retv = -EINVAL;
1277 #endif
1278 } else {
1279 if (start)
1280 *start = buffer;
1281 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1282 retv = sym_host_info(np, buffer, offset, length);
1283 #else
1284 retv = -EINVAL;
1285 #endif
1288 return retv;
1290 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1293 * Free controller resources.
1295 static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
1298 * Free O/S specific resources.
1300 if (pdev->irq)
1301 free_irq(pdev->irq, np);
1302 if (np->s.ioaddr)
1303 pci_iounmap(pdev, np->s.ioaddr);
1304 if (np->s.ramaddr)
1305 pci_iounmap(pdev, np->s.ramaddr);
1307 * Free O/S independent resources.
1309 sym_hcb_free(np);
1311 sym_mfree_dma(np, sizeof(*np), "HCB");
1315 * Ask/tell the system about DMA addressing.
1317 static int sym_setup_bus_dma_mask(struct sym_hcb *np)
1319 #if SYM_CONF_DMA_ADDRESSING_MODE > 0
1320 #if SYM_CONF_DMA_ADDRESSING_MODE == 1
1321 #define DMA_DAC_MASK DMA_40BIT_MASK
1322 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
1323 #define DMA_DAC_MASK DMA_64BIT_MASK
1324 #endif
1325 if ((np->features & FE_DAC) &&
1326 !pci_set_dma_mask(np->s.device, DMA_DAC_MASK)) {
1327 np->use_dac = 1;
1328 return 0;
1330 #endif
1332 if (!pci_set_dma_mask(np->s.device, DMA_32BIT_MASK))
1333 return 0;
1335 printf_warning("%s: No suitable DMA available\n", sym_name(np));
1336 return -1;
1340 * Host attach and initialisations.
1342 * Allocate host data and ncb structure.
1343 * Remap MMIO region.
1344 * Do chip initialization.
1345 * If all is OK, install interrupt handling and
1346 * start the timer daemon.
1348 static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
1349 int unit, struct sym_device *dev)
1351 struct host_data *host_data;
1352 struct sym_hcb *np = NULL;
1353 struct Scsi_Host *instance = NULL;
1354 struct pci_dev *pdev = dev->pdev;
1355 unsigned long flags;
1356 struct sym_fw *fw;
1358 printk(KERN_INFO
1359 "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT "\n",
1360 unit, dev->chip.name, dev->chip.revision_id,
1361 pci_name(pdev), IRQ_PRM(pdev->irq));
1364 * Get the firmware for this chip.
1366 fw = sym_find_firmware(&dev->chip);
1367 if (!fw)
1368 goto attach_failed;
1371 * Allocate host_data structure
1373 instance = scsi_host_alloc(tpnt, sizeof(*host_data));
1374 if (!instance)
1375 goto attach_failed;
1376 host_data = (struct host_data *) instance->hostdata;
1379 * Allocate immediately the host control block,
1380 * since we are only expecting to succeed. :)
1381 * We keep track in the HCB of all the resources that
1382 * are to be released on error.
1384 np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
1385 if (!np)
1386 goto attach_failed;
1387 np->s.device = pdev;
1388 np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
1389 host_data->ncb = np;
1390 np->s.host = instance;
1392 pci_set_drvdata(pdev, np);
1395 * Copy some useful infos to the HCB.
1397 np->hcb_ba = vtobus(np);
1398 np->verbose = sym_driver_setup.verbose;
1399 np->s.device = pdev;
1400 np->s.unit = unit;
1401 np->device_id = dev->chip.device_id;
1402 np->revision_id = dev->chip.revision_id;
1403 np->features = dev->chip.features;
1404 np->clock_divn = dev->chip.nr_divisor;
1405 np->maxoffs = dev->chip.offset_max;
1406 np->maxburst = dev->chip.burst_max;
1407 np->myaddr = dev->host_id;
1410 * Edit its name.
1412 strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1413 sprintf(np->s.inst_name, "sym%d", np->s.unit);
1415 if (sym_setup_bus_dma_mask(np))
1416 goto attach_failed;
1419 * Try to map the controller chip to
1420 * virtual and physical memory.
1422 np->mmio_ba = (u32)dev->mmio_base;
1423 np->s.ioaddr = dev->s.ioaddr;
1424 np->s.ramaddr = dev->s.ramaddr;
1425 np->s.io_ws = (np->features & FE_IO256) ? 256 : 128;
1428 * Map on-chip RAM if present and supported.
1430 if (!(np->features & FE_RAM))
1431 dev->ram_base = 0;
1432 if (dev->ram_base) {
1433 np->ram_ba = (u32)dev->ram_base;
1434 np->ram_ws = (np->features & FE_RAM8K) ? 8192 : 4096;
1437 if (sym_hcb_attach(instance, fw, dev->nvram))
1438 goto attach_failed;
1441 * Install the interrupt handler.
1442 * If we synchonize the C code with SCRIPTS on interrupt,
1443 * we do not want to share the INTR line at all.
1445 if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX, np)) {
1446 printf_err("%s: request irq %d failure\n",
1447 sym_name(np), pdev->irq);
1448 goto attach_failed;
1452 * After SCSI devices have been opened, we cannot
1453 * reset the bus safely, so we do it here.
1455 spin_lock_irqsave(instance->host_lock, flags);
1456 if (sym_reset_scsi_bus(np, 0))
1457 goto reset_failed;
1460 * Start the SCRIPTS.
1462 sym_start_up (np, 1);
1465 * Start the timer daemon
1467 init_timer(&np->s.timer);
1468 np->s.timer.data = (unsigned long) np;
1469 np->s.timer.function = sym53c8xx_timer;
1470 np->s.lasttime=0;
1471 sym_timer (np);
1474 * Fill Linux host instance structure
1475 * and return success.
1477 instance->max_channel = 0;
1478 instance->this_id = np->myaddr;
1479 instance->max_id = np->maxwide ? 16 : 8;
1480 instance->max_lun = SYM_CONF_MAX_LUN;
1481 instance->unique_id = pci_resource_start(pdev, 0);
1482 instance->cmd_per_lun = SYM_CONF_MAX_TAG;
1483 instance->can_queue = (SYM_CONF_MAX_START-2);
1484 instance->sg_tablesize = SYM_CONF_MAX_SG;
1485 instance->max_cmd_len = 16;
1486 BUG_ON(sym2_transport_template == NULL);
1487 instance->transportt = sym2_transport_template;
1489 /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
1490 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && np->revision_id < 2)
1491 instance->dma_boundary = 0xFFFFFF;
1493 spin_unlock_irqrestore(instance->host_lock, flags);
1495 return instance;
1497 reset_failed:
1498 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1499 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1500 spin_unlock_irqrestore(instance->host_lock, flags);
1501 attach_failed:
1502 if (!instance)
1503 return NULL;
1504 printf_info("%s: giving up ...\n", sym_name(np));
1505 if (np)
1506 sym_free_resources(np, pdev);
1507 scsi_host_put(instance);
1509 return NULL;
1514 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1516 #if SYM_CONF_NVRAM_SUPPORT
1517 static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1519 devp->nvram = nvp;
1520 devp->device_id = devp->chip.device_id;
1521 nvp->type = 0;
1523 sym_read_nvram(devp, nvp);
1525 #else
1526 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1529 #endif /* SYM_CONF_NVRAM_SUPPORT */
1531 static int __devinit sym_check_supported(struct sym_device *device)
1533 struct sym_chip *chip;
1534 struct pci_dev *pdev = device->pdev;
1535 u_char revision;
1536 unsigned long io_port = pci_resource_start(pdev, 0);
1537 int i;
1540 * If user excluded this chip, do not initialize it.
1541 * I hate this code so much. Must kill it.
1543 if (io_port) {
1544 for (i = 0 ; i < 8 ; i++) {
1545 if (sym_driver_setup.excludes[i] == io_port)
1546 return -ENODEV;
1551 * Check if the chip is supported. Then copy the chip description
1552 * to our device structure so we can make it match the actual device
1553 * and options.
1555 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1556 chip = sym_lookup_chip_table(pdev->device, revision);
1557 if (!chip) {
1558 dev_info(&pdev->dev, "device not supported\n");
1559 return -ENODEV;
1561 memcpy(&device->chip, chip, sizeof(device->chip));
1562 device->chip.revision_id = revision;
1564 return 0;
1568 * Ignore Symbios chips controlled by various RAID controllers.
1569 * These controllers set value 0x52414944 at RAM end - 16.
1571 static int __devinit sym_check_raid(struct sym_device *device)
1573 unsigned int ram_size, ram_val;
1575 if (!device->s.ramaddr)
1576 return 0;
1578 if (device->chip.features & FE_RAM8K)
1579 ram_size = 8192;
1580 else
1581 ram_size = 4096;
1583 ram_val = readl(device->s.ramaddr + ram_size - 16);
1584 if (ram_val != 0x52414944)
1585 return 0;
1587 dev_info(&device->pdev->dev,
1588 "not initializing, driven by RAID controller.\n");
1589 return -ENODEV;
1592 static int __devinit sym_set_workarounds(struct sym_device *device)
1594 struct sym_chip *chip = &device->chip;
1595 struct pci_dev *pdev = device->pdev;
1596 u_short status_reg;
1599 * (ITEM 12 of a DEL about the 896 I haven't yet).
1600 * We must ensure the chip will use WRITE AND INVALIDATE.
1601 * The revision number limit is for now arbitrary.
1603 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && chip->revision_id < 0x4) {
1604 chip->features |= (FE_WRIE | FE_CLSE);
1607 /* If the chip can do Memory Write Invalidate, enable it */
1608 if (chip->features & FE_WRIE) {
1609 if (pci_set_mwi(pdev))
1610 return -ENODEV;
1614 * Work around for errant bit in 895A. The 66Mhz
1615 * capable bit is set erroneously. Clear this bit.
1616 * (Item 1 DEL 533)
1618 * Make sure Config space and Features agree.
1620 * Recall: writes are not normal to status register -
1621 * write a 1 to clear and a 0 to leave unchanged.
1622 * Can only reset bits.
1624 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1625 if (chip->features & FE_66MHZ) {
1626 if (!(status_reg & PCI_STATUS_66MHZ))
1627 chip->features &= ~FE_66MHZ;
1628 } else {
1629 if (status_reg & PCI_STATUS_66MHZ) {
1630 status_reg = PCI_STATUS_66MHZ;
1631 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1632 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1636 return 0;
1640 * Read and check the PCI configuration for any detected NCR
1641 * boards and save data for attaching after all boards have
1642 * been detected.
1644 static void __devinit
1645 sym_init_device(struct pci_dev *pdev, struct sym_device *device)
1647 int i = 2;
1648 struct pci_bus_region bus_addr;
1650 device->host_id = SYM_SETUP_HOST_ID;
1651 device->pdev = pdev;
1653 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
1654 device->mmio_base = bus_addr.start;
1657 * If the BAR is 64-bit, resource 2 will be occupied by the
1658 * upper 32 bits
1660 if (!pdev->resource[i].flags)
1661 i++;
1662 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
1663 device->ram_base = bus_addr.start;
1665 #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
1666 if (device->mmio_base)
1667 device->s.ioaddr = pci_iomap(pdev, 1,
1668 pci_resource_len(pdev, 1));
1669 #endif
1670 if (!device->s.ioaddr)
1671 device->s.ioaddr = pci_iomap(pdev, 0,
1672 pci_resource_len(pdev, 0));
1673 if (device->ram_base)
1674 device->s.ramaddr = pci_iomap(pdev, i,
1675 pci_resource_len(pdev, i));
1679 * The NCR PQS and PDS cards are constructed as a DEC bridge
1680 * behind which sits a proprietary NCR memory controller and
1681 * either four or two 53c875s as separate devices. We can tell
1682 * if an 875 is part of a PQS/PDS or not since if it is, it will
1683 * be on the same bus as the memory controller. In its usual
1684 * mode of operation, the 875s are slaved to the memory
1685 * controller for all transfers. To operate with the Linux
1686 * driver, the memory controller is disabled and the 875s
1687 * freed to function independently. The only wrinkle is that
1688 * the preset SCSI ID (which may be zero) must be read in from
1689 * a special configuration space register of the 875.
1691 static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
1693 int slot;
1694 u8 tmp;
1696 for (slot = 0; slot < 256; slot++) {
1697 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
1699 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
1700 pci_dev_put(memc);
1701 continue;
1704 /* bit 1: allow individual 875 configuration */
1705 pci_read_config_byte(memc, 0x44, &tmp);
1706 if ((tmp & 0x2) == 0) {
1707 tmp |= 0x2;
1708 pci_write_config_byte(memc, 0x44, tmp);
1711 /* bit 2: drive individual 875 interrupts to the bus */
1712 pci_read_config_byte(memc, 0x45, &tmp);
1713 if ((tmp & 0x4) == 0) {
1714 tmp |= 0x4;
1715 pci_write_config_byte(memc, 0x45, tmp);
1718 pci_dev_put(memc);
1719 break;
1722 pci_read_config_byte(pdev, 0x84, &tmp);
1723 sym_dev->host_id = tmp;
1727 * Called before unloading the module.
1728 * Detach the host.
1729 * We have to free resources and halt the NCR chip.
1731 static int sym_detach(struct sym_hcb *np, struct pci_dev *pdev)
1733 printk("%s: detaching ...\n", sym_name(np));
1735 del_timer_sync(&np->s.timer);
1738 * Reset NCR chip.
1739 * We should use sym_soft_reset(), but we don't want to do
1740 * so, since we may not be safe if interrupts occur.
1742 printk("%s: resetting chip\n", sym_name(np));
1743 OUTB(np, nc_istat, SRST);
1744 INB(np, nc_mbox1);
1745 udelay(10);
1746 OUTB(np, nc_istat, 0);
1748 sym_free_resources(np, pdev);
1750 return 1;
1754 * Driver host template.
1756 static struct scsi_host_template sym2_template = {
1757 .module = THIS_MODULE,
1758 .name = "sym53c8xx",
1759 .info = sym53c8xx_info,
1760 .queuecommand = sym53c8xx_queue_command,
1761 .slave_alloc = sym53c8xx_slave_alloc,
1762 .slave_configure = sym53c8xx_slave_configure,
1763 .slave_destroy = sym53c8xx_slave_destroy,
1764 .eh_abort_handler = sym53c8xx_eh_abort_handler,
1765 .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
1766 .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
1767 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
1768 .this_id = 7,
1769 .use_clustering = ENABLE_CLUSTERING,
1770 .use_sg_chaining = ENABLE_SG_CHAINING,
1771 .max_sectors = 0xFFFF,
1772 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1773 .proc_info = sym53c8xx_proc_info,
1774 .proc_name = NAME53C8XX,
1775 #endif
1778 static int attach_count;
1780 static int __devinit sym2_probe(struct pci_dev *pdev,
1781 const struct pci_device_id *ent)
1783 struct sym_device sym_dev;
1784 struct sym_nvram nvram;
1785 struct Scsi_Host *instance;
1787 memset(&sym_dev, 0, sizeof(sym_dev));
1788 memset(&nvram, 0, sizeof(nvram));
1790 if (pci_enable_device(pdev))
1791 goto leave;
1793 pci_set_master(pdev);
1795 if (pci_request_regions(pdev, NAME53C8XX))
1796 goto disable;
1798 sym_init_device(pdev, &sym_dev);
1799 if (sym_check_supported(&sym_dev))
1800 goto free;
1802 if (sym_check_raid(&sym_dev))
1803 goto leave; /* Don't disable the device */
1805 if (sym_set_workarounds(&sym_dev))
1806 goto free;
1808 sym_config_pqs(pdev, &sym_dev);
1810 sym_get_nvram(&sym_dev, &nvram);
1812 instance = sym_attach(&sym2_template, attach_count, &sym_dev);
1813 if (!instance)
1814 goto free;
1816 if (scsi_add_host(instance, &pdev->dev))
1817 goto detach;
1818 scsi_scan_host(instance);
1820 attach_count++;
1822 return 0;
1824 detach:
1825 sym_detach(pci_get_drvdata(pdev), pdev);
1826 free:
1827 pci_release_regions(pdev);
1828 disable:
1829 pci_disable_device(pdev);
1830 leave:
1831 return -ENODEV;
1834 static void __devexit sym2_remove(struct pci_dev *pdev)
1836 struct sym_hcb *np = pci_get_drvdata(pdev);
1837 struct Scsi_Host *host = np->s.host;
1839 scsi_remove_host(host);
1840 scsi_host_put(host);
1842 sym_detach(np, pdev);
1844 pci_release_regions(pdev);
1845 pci_disable_device(pdev);
1847 attach_count--;
1850 static void sym2_get_signalling(struct Scsi_Host *shost)
1852 struct sym_hcb *np = sym_get_hcb(shost);
1853 enum spi_signal_type type;
1855 switch (np->scsi_mode) {
1856 case SMODE_SE:
1857 type = SPI_SIGNAL_SE;
1858 break;
1859 case SMODE_LVD:
1860 type = SPI_SIGNAL_LVD;
1861 break;
1862 case SMODE_HVD:
1863 type = SPI_SIGNAL_HVD;
1864 break;
1865 default:
1866 type = SPI_SIGNAL_UNKNOWN;
1867 break;
1869 spi_signalling(shost) = type;
1872 static void sym2_set_offset(struct scsi_target *starget, int offset)
1874 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1875 struct sym_hcb *np = sym_get_hcb(shost);
1876 struct sym_tcb *tp = &np->target[starget->id];
1878 tp->tgoal.offset = offset;
1879 tp->tgoal.check_nego = 1;
1882 static void sym2_set_period(struct scsi_target *starget, int period)
1884 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1885 struct sym_hcb *np = sym_get_hcb(shost);
1886 struct sym_tcb *tp = &np->target[starget->id];
1888 /* have to have DT for these transfers, but DT will also
1889 * set width, so check that this is allowed */
1890 if (period <= np->minsync && spi_width(starget))
1891 tp->tgoal.dt = 1;
1893 tp->tgoal.period = period;
1894 tp->tgoal.check_nego = 1;
1897 static void sym2_set_width(struct scsi_target *starget, int width)
1899 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1900 struct sym_hcb *np = sym_get_hcb(shost);
1901 struct sym_tcb *tp = &np->target[starget->id];
1903 /* It is illegal to have DT set on narrow transfers. If DT is
1904 * clear, we must also clear IU and QAS. */
1905 if (width == 0)
1906 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1908 tp->tgoal.width = width;
1909 tp->tgoal.check_nego = 1;
1912 static void sym2_set_dt(struct scsi_target *starget, int dt)
1914 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1915 struct sym_hcb *np = sym_get_hcb(shost);
1916 struct sym_tcb *tp = &np->target[starget->id];
1918 /* We must clear QAS and IU if DT is clear */
1919 if (dt)
1920 tp->tgoal.dt = 1;
1921 else
1922 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1923 tp->tgoal.check_nego = 1;
1926 #if 0
1927 static void sym2_set_iu(struct scsi_target *starget, int iu)
1929 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1930 struct sym_hcb *np = sym_get_hcb(shost);
1931 struct sym_tcb *tp = &np->target[starget->id];
1933 if (iu)
1934 tp->tgoal.iu = tp->tgoal.dt = 1;
1935 else
1936 tp->tgoal.iu = 0;
1937 tp->tgoal.check_nego = 1;
1940 static void sym2_set_qas(struct scsi_target *starget, int qas)
1942 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1943 struct sym_hcb *np = sym_get_hcb(shost);
1944 struct sym_tcb *tp = &np->target[starget->id];
1946 if (qas)
1947 tp->tgoal.dt = tp->tgoal.qas = 1;
1948 else
1949 tp->tgoal.qas = 0;
1950 tp->tgoal.check_nego = 1;
1952 #endif
1954 static struct spi_function_template sym2_transport_functions = {
1955 .set_offset = sym2_set_offset,
1956 .show_offset = 1,
1957 .set_period = sym2_set_period,
1958 .show_period = 1,
1959 .set_width = sym2_set_width,
1960 .show_width = 1,
1961 .set_dt = sym2_set_dt,
1962 .show_dt = 1,
1963 #if 0
1964 .set_iu = sym2_set_iu,
1965 .show_iu = 1,
1966 .set_qas = sym2_set_qas,
1967 .show_qas = 1,
1968 #endif
1969 .get_signalling = sym2_get_signalling,
1972 static struct pci_device_id sym2_id_table[] __devinitdata = {
1973 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
1974 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1975 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
1976 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
1977 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
1978 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1979 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
1980 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1981 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
1982 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
1983 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
1984 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1985 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
1986 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
1987 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
1988 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1989 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
1990 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1991 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
1992 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1993 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
1994 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1995 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
1996 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
1997 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
1998 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
1999 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2000 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2001 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2002 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2003 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2004 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2005 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2006 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2007 { 0, }
2010 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2012 static struct pci_driver sym2_driver = {
2013 .name = NAME53C8XX,
2014 .id_table = sym2_id_table,
2015 .probe = sym2_probe,
2016 .remove = __devexit_p(sym2_remove),
2019 static int __init sym2_init(void)
2021 int error;
2023 sym2_setup_params();
2024 sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2025 if (!sym2_transport_template)
2026 return -ENODEV;
2028 error = pci_register_driver(&sym2_driver);
2029 if (error)
2030 spi_release_transport(sym2_transport_template);
2031 return error;
2034 static void __exit sym2_exit(void)
2036 pci_unregister_driver(&sym2_driver);
2037 spi_release_transport(sym2_transport_template);
2040 module_init(sym2_init);
2041 module_exit(sym2_exit);