[SCSI] sym53c8xx: fix free_irq() regression
[linux-2.6/kmemtrace.git] / drivers / scsi / sym53c8xx_2 / sym_glue.c
blob9e0908d1981a99f1fef86f3b6f4bf531e83366fa
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/module.h>
43 #include <linux/moduleparam.h>
44 #include <linux/spinlock.h>
45 #include <scsi/scsi.h>
46 #include <scsi/scsi_tcq.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_transport.h>
50 #include "sym_glue.h"
51 #include "sym_nvram.h"
53 #define NAME53C "sym53c"
54 #define NAME53C8XX "sym53c8xx"
56 struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
57 unsigned int sym_debug_flags = 0;
59 static char *excl_string;
60 static char *safe_string;
61 module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
62 module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
63 module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
64 module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
65 module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
66 module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
67 module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
68 module_param_named(verb, sym_driver_setup.verbose, byte, 0);
69 module_param_named(debug, sym_debug_flags, uint, 0);
70 module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
71 module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
72 module_param_named(excl, excl_string, charp, 0);
73 module_param_named(safe, safe_string, charp, 0);
75 MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
76 MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
77 MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
78 MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
79 MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
80 MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
81 MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
82 MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
83 MODULE_PARM_DESC(debug, "Set bits to enable debugging");
84 MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
85 MODULE_PARM_DESC(nvram, "Option currently not used");
86 MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
87 MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
89 MODULE_LICENSE("GPL");
90 MODULE_VERSION(SYM_VERSION);
91 MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
92 MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
94 static void sym2_setup_params(void)
96 char *p = excl_string;
97 int xi = 0;
99 while (p && (xi < 8)) {
100 char *next_p;
101 int val = (int) simple_strtoul(p, &next_p, 0);
102 sym_driver_setup.excludes[xi++] = val;
103 p = next_p;
106 if (safe_string) {
107 if (*safe_string == 'y') {
108 sym_driver_setup.max_tag = 0;
109 sym_driver_setup.burst_order = 0;
110 sym_driver_setup.scsi_led = 0;
111 sym_driver_setup.scsi_diff = 1;
112 sym_driver_setup.irq_mode = 0;
113 sym_driver_setup.scsi_bus_check = 2;
114 sym_driver_setup.host_id = 7;
115 sym_driver_setup.verbose = 2;
116 sym_driver_setup.settle_delay = 10;
117 sym_driver_setup.use_nvram = 1;
118 } else if (*safe_string != 'n') {
119 printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
120 " passed to safe option", safe_string);
125 static struct scsi_transport_template *sym2_transport_template = NULL;
128 * Driver private area in the SCSI command structure.
130 struct sym_ucmd { /* Override the SCSI pointer structure */
131 struct completion *eh_done; /* SCSI error handling */
134 #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
135 #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
138 * Complete a pending CAM CCB.
140 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
142 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
143 BUILD_BUG_ON(sizeof(struct scsi_pointer) < sizeof(struct sym_ucmd));
145 if (ucmd->eh_done)
146 complete(ucmd->eh_done);
148 scsi_dma_unmap(cmd);
149 cmd->scsi_done(cmd);
153 * Tell the SCSI layer about a BUS RESET.
155 void sym_xpt_async_bus_reset(struct sym_hcb *np)
157 printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
158 np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
159 np->s.settle_time_valid = 1;
160 if (sym_verbose >= 2)
161 printf_info("%s: command processing suspended for %d seconds\n",
162 sym_name(np), sym_driver_setup.settle_delay);
166 * Choose the more appropriate CAM status if
167 * the IO encountered an extended error.
169 static int sym_xerr_cam_status(int cam_status, int x_status)
171 if (x_status) {
172 if (x_status & XE_PARITY_ERR)
173 cam_status = DID_PARITY;
174 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
175 cam_status = DID_ERROR;
176 else if (x_status & XE_BAD_PHASE)
177 cam_status = DID_ERROR;
178 else
179 cam_status = DID_ERROR;
181 return cam_status;
185 * Build CAM result for a failed or auto-sensed IO.
187 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
189 struct scsi_cmnd *cmd = cp->cmd;
190 u_int cam_status, scsi_status, drv_status;
192 drv_status = 0;
193 cam_status = DID_OK;
194 scsi_status = cp->ssss_status;
196 if (cp->host_flags & HF_SENSE) {
197 scsi_status = cp->sv_scsi_status;
198 resid = cp->sv_resid;
199 if (sym_verbose && cp->sv_xerr_status)
200 sym_print_xerr(cmd, cp->sv_xerr_status);
201 if (cp->host_status == HS_COMPLETE &&
202 cp->ssss_status == S_GOOD &&
203 cp->xerr_status == 0) {
204 cam_status = sym_xerr_cam_status(DID_OK,
205 cp->sv_xerr_status);
206 drv_status = DRIVER_SENSE;
208 * Bounce back the sense data to user.
210 memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
211 memcpy(cmd->sense_buffer, cp->sns_bbuf,
212 min(sizeof(cmd->sense_buffer),
213 (size_t)SYM_SNS_BBUF_LEN));
214 #if 0
216 * If the device reports a UNIT ATTENTION condition
217 * due to a RESET condition, we should consider all
218 * disconnect CCBs for this unit as aborted.
220 if (1) {
221 u_char *p;
222 p = (u_char *) cmd->sense_data;
223 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
224 sym_clear_tasks(np, DID_ABORT,
225 cp->target,cp->lun, -1);
227 #endif
228 } else {
230 * Error return from our internal request sense. This
231 * is bad: we must clear the contingent allegiance
232 * condition otherwise the device will always return
233 * BUSY. Use a big stick.
235 sym_reset_scsi_target(np, cmd->device->id);
236 cam_status = DID_ERROR;
238 } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
239 cam_status = DID_OK;
240 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
241 cam_status = DID_NO_CONNECT;
242 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
243 cam_status = DID_ERROR;
244 else { /* Extended error */
245 if (sym_verbose) {
246 sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
247 cp->host_status, cp->ssss_status,
248 cp->xerr_status);
251 * Set the most appropriate value for CAM status.
253 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
255 scsi_set_resid(cmd, resid);
256 cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
259 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
261 int segment;
262 int use_sg;
264 cp->data_len = 0;
266 use_sg = scsi_dma_map(cmd);
267 if (use_sg > 0) {
268 struct scatterlist *sg;
269 struct sym_tcb *tp = &np->target[cp->target];
270 struct sym_tblmove *data;
272 if (use_sg > SYM_CONF_MAX_SG) {
273 scsi_dma_unmap(cmd);
274 return -1;
277 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
279 scsi_for_each_sg(cmd, sg, use_sg, segment) {
280 dma_addr_t baddr = sg_dma_address(sg);
281 unsigned int len = sg_dma_len(sg);
283 if ((len & 1) && (tp->head.wval & EWS)) {
284 len++;
285 cp->odd_byte_adjustment++;
288 sym_build_sge(np, &data[segment], baddr, len);
289 cp->data_len += len;
291 } else {
292 segment = -2;
295 return segment;
299 * Queue a SCSI command.
301 static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
303 struct scsi_device *sdev = cmd->device;
304 struct sym_tcb *tp;
305 struct sym_lcb *lp;
306 struct sym_ccb *cp;
307 int order;
310 * Retrieve the target descriptor.
312 tp = &np->target[sdev->id];
315 * Select tagged/untagged.
317 lp = sym_lp(tp, sdev->lun);
318 order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
321 * Queue the SCSI IO.
323 cp = sym_get_ccb(np, cmd, order);
324 if (!cp)
325 return 1; /* Means resource shortage */
326 sym_queue_scsiio(np, cmd, cp);
327 return 0;
331 * Setup buffers and pointers that address the CDB.
333 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
335 memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
337 cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
338 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
340 return 0;
344 * Setup pointers that address the data and start the I/O.
346 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
348 u32 lastp, goalp;
349 int dir;
352 * Build the CDB.
354 if (sym_setup_cdb(np, cmd, cp))
355 goto out_abort;
358 * No direction means no data.
360 dir = cmd->sc_data_direction;
361 if (dir != DMA_NONE) {
362 cp->segments = sym_scatter(np, cp, cmd);
363 if (cp->segments < 0) {
364 sym_set_cam_status(cmd, DID_ERROR);
365 goto out_abort;
369 * No segments means no data.
371 if (!cp->segments)
372 dir = DMA_NONE;
373 } else {
374 cp->data_len = 0;
375 cp->segments = 0;
379 * Set the data pointer.
381 switch (dir) {
382 case DMA_BIDIRECTIONAL:
383 scmd_printk(KERN_INFO, cmd, "got DMA_BIDIRECTIONAL command");
384 sym_set_cam_status(cmd, DID_ERROR);
385 goto out_abort;
386 case DMA_TO_DEVICE:
387 goalp = SCRIPTA_BA(np, data_out2) + 8;
388 lastp = goalp - 8 - (cp->segments * (2*4));
389 break;
390 case DMA_FROM_DEVICE:
391 cp->host_flags |= HF_DATA_IN;
392 goalp = SCRIPTA_BA(np, data_in2) + 8;
393 lastp = goalp - 8 - (cp->segments * (2*4));
394 break;
395 case DMA_NONE:
396 default:
397 lastp = goalp = SCRIPTB_BA(np, no_data);
398 break;
402 * Set all pointers values needed by SCRIPTS.
404 cp->phys.head.lastp = cpu_to_scr(lastp);
405 cp->phys.head.savep = cpu_to_scr(lastp);
406 cp->startp = cp->phys.head.savep;
407 cp->goalp = cpu_to_scr(goalp);
410 * When `#ifed 1', the code below makes the driver
411 * panic on the first attempt to write to a SCSI device.
412 * It is the first test we want to do after a driver
413 * change that does not seem obviously safe. :)
415 #if 0
416 switch (cp->cdb_buf[0]) {
417 case 0x0A: case 0x2A: case 0xAA:
418 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
419 break;
420 default:
421 break;
423 #endif
426 * activate this job.
428 sym_put_start_queue(np, cp);
429 return 0;
431 out_abort:
432 sym_free_ccb(np, cp);
433 sym_xpt_done(np, cmd);
434 return 0;
439 * timer daemon.
441 * Misused to keep the driver running when
442 * interrupts are not configured correctly.
444 static void sym_timer(struct sym_hcb *np)
446 unsigned long thistime = jiffies;
449 * Restart the timer.
451 np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
452 add_timer(&np->s.timer);
455 * If we are resetting the ncr, wait for settle_time before
456 * clearing it. Then command processing will be resumed.
458 if (np->s.settle_time_valid) {
459 if (time_before_eq(np->s.settle_time, thistime)) {
460 if (sym_verbose >= 2 )
461 printk("%s: command processing resumed\n",
462 sym_name(np));
463 np->s.settle_time_valid = 0;
465 return;
469 * Nothing to do for now, but that may come.
471 if (np->s.lasttime + 4*HZ < thistime) {
472 np->s.lasttime = thistime;
475 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
477 * Some way-broken PCI bridges may lead to
478 * completions being lost when the clearing
479 * of the INTFLY flag by the CPU occurs
480 * concurrently with the chip raising this flag.
481 * If this ever happen, lost completions will
482 * be reaped here.
484 sym_wakeup_done(np);
485 #endif
490 * PCI BUS error handler.
492 void sym_log_bus_error(struct Scsi_Host *shost)
494 struct sym_data *sym_data = shost_priv(shost);
495 struct pci_dev *pdev = sym_data->pdev;
496 unsigned short pci_sts;
497 pci_read_config_word(pdev, PCI_STATUS, &pci_sts);
498 if (pci_sts & 0xf900) {
499 pci_write_config_word(pdev, PCI_STATUS, pci_sts);
500 shost_printk(KERN_WARNING, shost,
501 "PCI bus error: status = 0x%04x\n", pci_sts & 0xf900);
506 * queuecommand method. Entered with the host adapter lock held and
507 * interrupts disabled.
509 static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
510 void (*done)(struct scsi_cmnd *))
512 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
513 struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
514 int sts = 0;
516 cmd->scsi_done = done;
517 memset(ucp, 0, sizeof(*ucp));
520 * Shorten our settle_time if needed for
521 * this command not to time out.
523 if (np->s.settle_time_valid && cmd->timeout_per_command) {
524 unsigned long tlimit = jiffies + cmd->timeout_per_command;
525 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
526 if (time_after(np->s.settle_time, tlimit)) {
527 np->s.settle_time = tlimit;
531 if (np->s.settle_time_valid)
532 return SCSI_MLQUEUE_HOST_BUSY;
534 sts = sym_queue_command(np, cmd);
535 if (sts)
536 return SCSI_MLQUEUE_HOST_BUSY;
537 return 0;
541 * Linux entry point of the interrupt handler.
543 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
545 struct Scsi_Host *shost = dev_id;
546 struct sym_data *sym_data = shost_priv(shost);
547 irqreturn_t result;
549 /* Avoid spinloop trying to handle interrupts on frozen device */
550 if (pci_channel_offline(sym_data->pdev))
551 return IRQ_NONE;
553 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
555 spin_lock(shost->host_lock);
556 result = sym_interrupt(shost);
557 spin_unlock(shost->host_lock);
559 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
561 return result;
565 * Linux entry point of the timer handler
567 static void sym53c8xx_timer(unsigned long npref)
569 struct sym_hcb *np = (struct sym_hcb *)npref;
570 unsigned long flags;
572 spin_lock_irqsave(np->s.host->host_lock, flags);
573 sym_timer(np);
574 spin_unlock_irqrestore(np->s.host->host_lock, flags);
579 * What the eh thread wants us to perform.
581 #define SYM_EH_ABORT 0
582 #define SYM_EH_DEVICE_RESET 1
583 #define SYM_EH_BUS_RESET 2
584 #define SYM_EH_HOST_RESET 3
587 * Generic method for our eh processing.
588 * The 'op' argument tells what we have to do.
590 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
592 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
593 struct Scsi_Host *shost = cmd->device->host;
594 struct sym_data *sym_data = shost_priv(shost);
595 struct pci_dev *pdev = sym_data->pdev;
596 struct sym_hcb *np = sym_data->ncb;
597 SYM_QUEHEAD *qp;
598 int cmd_queued = 0;
599 int sts = -1;
600 struct completion eh_done;
602 scmd_printk(KERN_WARNING, cmd, "%s operation started\n", opname);
604 /* We may be in an error condition because the PCI bus
605 * went down. In this case, we need to wait until the
606 * PCI bus is reset, the card is reset, and only then
607 * proceed with the scsi error recovery. There's no
608 * point in hurrying; take a leisurely wait.
610 #define WAIT_FOR_PCI_RECOVERY 35
611 if (pci_channel_offline(pdev)) {
612 struct completion *io_reset;
613 int finished_reset = 0;
614 init_completion(&eh_done);
615 spin_lock_irq(shost->host_lock);
616 /* Make sure we didn't race */
617 if (pci_channel_offline(pdev)) {
618 if (!sym_data->io_reset)
619 sym_data->io_reset = &eh_done;
620 io_reset = sym_data->io_reset;
621 } else {
622 finished_reset = 1;
624 spin_unlock_irq(shost->host_lock);
625 if (!finished_reset)
626 finished_reset = wait_for_completion_timeout(io_reset,
627 WAIT_FOR_PCI_RECOVERY*HZ);
628 if (!finished_reset)
629 return SCSI_FAILED;
632 spin_lock_irq(shost->host_lock);
633 /* This one is queued in some place -> to wait for completion */
634 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
635 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
636 if (cp->cmd == cmd) {
637 cmd_queued = 1;
638 break;
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(shost, 1);
658 sts = 0;
659 break;
660 default:
661 break;
664 /* On error, restore everything and cross fingers :) */
665 if (sts)
666 cmd_queued = 0;
668 if (cmd_queued) {
669 init_completion(&eh_done);
670 ucmd->eh_done = &eh_done;
671 spin_unlock_irq(shost->host_lock);
672 if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
673 ucmd->eh_done = NULL;
674 sts = -2;
676 } else {
677 spin_unlock_irq(shost->host_lock);
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);
734 static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
736 struct sym_hcb *np = sym_get_hcb(sdev->host);
737 struct sym_tcb *tp = &np->target[sdev->id];
738 struct sym_lcb *lp;
740 if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
741 return -ENXIO;
743 tp->starget = sdev->sdev_target;
745 * Fail the device init if the device is flagged NOSCAN at BOOT in
746 * the NVRAM. This may speed up boot and maintain coherency with
747 * BIOS device numbering. Clearing the flag allows the user to
748 * rescan skipped devices later. We also return an error for
749 * devices not flagged for SCAN LUNS in the NVRAM since some single
750 * lun devices behave badly when asked for a non zero LUN.
753 if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
754 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
755 starget_printk(KERN_INFO, tp->starget,
756 "Scan at boot disabled in NVRAM\n");
757 return -ENXIO;
760 if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
761 if (sdev->lun != 0)
762 return -ENXIO;
763 starget_printk(KERN_INFO, tp->starget,
764 "Multiple LUNs disabled in NVRAM\n");
767 lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
768 if (!lp)
769 return -ENOMEM;
771 spi_min_period(tp->starget) = tp->usr_period;
772 spi_max_width(tp->starget) = tp->usr_width;
774 return 0;
778 * Linux entry point for device queue sizing.
780 static int sym53c8xx_slave_configure(struct scsi_device *sdev)
782 struct sym_hcb *np = sym_get_hcb(sdev->host);
783 struct sym_tcb *tp = &np->target[sdev->id];
784 struct sym_lcb *lp = sym_lp(tp, sdev->lun);
785 int reqtags, depth_to_use;
788 * Get user flags.
790 lp->curr_flags = lp->user_flags;
793 * Select queue depth from driver setup.
794 * Donnot use more than configured by user.
795 * Use at least 2.
796 * Donnot use more than our maximum.
798 reqtags = sym_driver_setup.max_tag;
799 if (reqtags > tp->usrtags)
800 reqtags = tp->usrtags;
801 if (!sdev->tagged_supported)
802 reqtags = 0;
803 if (reqtags > SYM_CONF_MAX_TAG)
804 reqtags = SYM_CONF_MAX_TAG;
805 depth_to_use = reqtags ? reqtags : 2;
806 scsi_adjust_queue_depth(sdev,
807 sdev->tagged_supported ? MSG_SIMPLE_TAG : 0,
808 depth_to_use);
809 lp->s.scdev_depth = depth_to_use;
810 sym_tune_dev_queuing(tp, sdev->lun, reqtags);
812 if (!spi_initial_dv(sdev->sdev_target))
813 spi_dv_device(sdev);
815 return 0;
818 static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
820 struct sym_hcb *np = sym_get_hcb(sdev->host);
821 struct sym_lcb *lp = sym_lp(&np->target[sdev->id], sdev->lun);
823 if (lp->itlq_tbl)
824 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK * 4, "ITLQ_TBL");
825 kfree(lp->cb_tags);
826 sym_mfree_dma(lp, sizeof(*lp), "LCB");
830 * Linux entry point for info() function
832 static const char *sym53c8xx_info (struct Scsi_Host *host)
834 return SYM_DRIVER_NAME;
838 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
840 * Proc file system stuff
842 * A read operation returns adapter information.
843 * A write operation is a control command.
844 * The string is parsed in the driver code and the command is passed
845 * to the sym_usercmd() function.
848 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
850 struct sym_usrcmd {
851 u_long target;
852 u_long lun;
853 u_long data;
854 u_long cmd;
857 #define UC_SETSYNC 10
858 #define UC_SETTAGS 11
859 #define UC_SETDEBUG 12
860 #define UC_SETWIDE 14
861 #define UC_SETFLAG 15
862 #define UC_SETVERBOSE 17
863 #define UC_RESETDEV 18
864 #define UC_CLEARDEV 19
866 static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
868 struct sym_tcb *tp;
869 int t, l;
871 switch (uc->cmd) {
872 case 0: return;
874 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
875 case UC_SETDEBUG:
876 sym_debug_flags = uc->data;
877 break;
878 #endif
879 case UC_SETVERBOSE:
880 np->verbose = uc->data;
881 break;
882 default:
884 * We assume that other commands apply to targets.
885 * This should always be the case and avoid the below
886 * 4 lines to be repeated 6 times.
888 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
889 if (!((uc->target >> t) & 1))
890 continue;
891 tp = &np->target[t];
893 switch (uc->cmd) {
895 case UC_SETSYNC:
896 if (!uc->data || uc->data >= 255) {
897 tp->tgoal.iu = tp->tgoal.dt =
898 tp->tgoal.qas = 0;
899 tp->tgoal.offset = 0;
900 } else if (uc->data <= 9 && np->minsync_dt) {
901 if (uc->data < np->minsync_dt)
902 uc->data = np->minsync_dt;
903 tp->tgoal.iu = tp->tgoal.dt =
904 tp->tgoal.qas = 1;
905 tp->tgoal.width = 1;
906 tp->tgoal.period = uc->data;
907 tp->tgoal.offset = np->maxoffs_dt;
908 } else {
909 if (uc->data < np->minsync)
910 uc->data = np->minsync;
911 tp->tgoal.iu = tp->tgoal.dt =
912 tp->tgoal.qas = 0;
913 tp->tgoal.period = uc->data;
914 tp->tgoal.offset = np->maxoffs;
916 tp->tgoal.check_nego = 1;
917 break;
918 case UC_SETWIDE:
919 tp->tgoal.width = uc->data ? 1 : 0;
920 tp->tgoal.check_nego = 1;
921 break;
922 case UC_SETTAGS:
923 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
924 sym_tune_dev_queuing(tp, l, uc->data);
925 break;
926 case UC_RESETDEV:
927 tp->to_reset = 1;
928 np->istat_sem = SEM;
929 OUTB(np, nc_istat, SIGP|SEM);
930 break;
931 case UC_CLEARDEV:
932 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
933 struct sym_lcb *lp = sym_lp(tp, l);
934 if (lp) lp->to_clear = 1;
936 np->istat_sem = SEM;
937 OUTB(np, nc_istat, SIGP|SEM);
938 break;
939 case UC_SETFLAG:
940 tp->usrflags = uc->data;
941 break;
944 break;
948 static int skip_spaces(char *ptr, int len)
950 int cnt, c;
952 for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
954 return (len - cnt);
957 static int get_int_arg(char *ptr, int len, u_long *pv)
959 char *end;
961 *pv = simple_strtoul(ptr, &end, 10);
962 return (end - ptr);
965 static int is_keyword(char *ptr, int len, char *verb)
967 int verb_len = strlen(verb);
969 if (len >= verb_len && !memcmp(verb, ptr, verb_len))
970 return verb_len;
971 else
972 return 0;
975 #define SKIP_SPACES(ptr, len) \
976 if ((arg_len = skip_spaces(ptr, len)) < 1) \
977 return -EINVAL; \
978 ptr += arg_len; len -= arg_len;
980 #define GET_INT_ARG(ptr, len, v) \
981 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
982 return -EINVAL; \
983 ptr += arg_len; len -= arg_len;
987 * Parse a control command
990 static int sym_user_command(struct Scsi_Host *shost, char *buffer, int length)
992 struct sym_hcb *np = sym_get_hcb(shost);
993 char *ptr = buffer;
994 int len = length;
995 struct sym_usrcmd cmd, *uc = &cmd;
996 int arg_len;
997 u_long target;
999 memset(uc, 0, sizeof(*uc));
1001 if (len > 0 && ptr[len-1] == '\n')
1002 --len;
1004 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1005 uc->cmd = UC_SETSYNC;
1006 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1007 uc->cmd = UC_SETTAGS;
1008 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1009 uc->cmd = UC_SETVERBOSE;
1010 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1011 uc->cmd = UC_SETWIDE;
1012 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1013 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1014 uc->cmd = UC_SETDEBUG;
1015 #endif
1016 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1017 uc->cmd = UC_SETFLAG;
1018 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1019 uc->cmd = UC_RESETDEV;
1020 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1021 uc->cmd = UC_CLEARDEV;
1022 else
1023 arg_len = 0;
1025 #ifdef DEBUG_PROC_INFO
1026 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1027 #endif
1029 if (!arg_len)
1030 return -EINVAL;
1031 ptr += arg_len; len -= arg_len;
1033 switch(uc->cmd) {
1034 case UC_SETSYNC:
1035 case UC_SETTAGS:
1036 case UC_SETWIDE:
1037 case UC_SETFLAG:
1038 case UC_RESETDEV:
1039 case UC_CLEARDEV:
1040 SKIP_SPACES(ptr, len);
1041 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1042 ptr += arg_len; len -= arg_len;
1043 uc->target = ~0;
1044 } else {
1045 GET_INT_ARG(ptr, len, target);
1046 uc->target = (1<<target);
1047 #ifdef DEBUG_PROC_INFO
1048 printk("sym_user_command: target=%ld\n", target);
1049 #endif
1051 break;
1054 switch(uc->cmd) {
1055 case UC_SETVERBOSE:
1056 case UC_SETSYNC:
1057 case UC_SETTAGS:
1058 case UC_SETWIDE:
1059 SKIP_SPACES(ptr, len);
1060 GET_INT_ARG(ptr, len, uc->data);
1061 #ifdef DEBUG_PROC_INFO
1062 printk("sym_user_command: data=%ld\n", uc->data);
1063 #endif
1064 break;
1065 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1066 case UC_SETDEBUG:
1067 while (len > 0) {
1068 SKIP_SPACES(ptr, len);
1069 if ((arg_len = is_keyword(ptr, len, "alloc")))
1070 uc->data |= DEBUG_ALLOC;
1071 else if ((arg_len = is_keyword(ptr, len, "phase")))
1072 uc->data |= DEBUG_PHASE;
1073 else if ((arg_len = is_keyword(ptr, len, "queue")))
1074 uc->data |= DEBUG_QUEUE;
1075 else if ((arg_len = is_keyword(ptr, len, "result")))
1076 uc->data |= DEBUG_RESULT;
1077 else if ((arg_len = is_keyword(ptr, len, "scatter")))
1078 uc->data |= DEBUG_SCATTER;
1079 else if ((arg_len = is_keyword(ptr, len, "script")))
1080 uc->data |= DEBUG_SCRIPT;
1081 else if ((arg_len = is_keyword(ptr, len, "tiny")))
1082 uc->data |= DEBUG_TINY;
1083 else if ((arg_len = is_keyword(ptr, len, "timing")))
1084 uc->data |= DEBUG_TIMING;
1085 else if ((arg_len = is_keyword(ptr, len, "nego")))
1086 uc->data |= DEBUG_NEGO;
1087 else if ((arg_len = is_keyword(ptr, len, "tags")))
1088 uc->data |= DEBUG_TAGS;
1089 else if ((arg_len = is_keyword(ptr, len, "pointer")))
1090 uc->data |= DEBUG_POINTER;
1091 else
1092 return -EINVAL;
1093 ptr += arg_len; len -= arg_len;
1095 #ifdef DEBUG_PROC_INFO
1096 printk("sym_user_command: data=%ld\n", uc->data);
1097 #endif
1098 break;
1099 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1100 case UC_SETFLAG:
1101 while (len > 0) {
1102 SKIP_SPACES(ptr, len);
1103 if ((arg_len = is_keyword(ptr, len, "no_disc")))
1104 uc->data &= ~SYM_DISC_ENABLED;
1105 else
1106 return -EINVAL;
1107 ptr += arg_len; len -= arg_len;
1109 break;
1110 default:
1111 break;
1114 if (len)
1115 return -EINVAL;
1116 else {
1117 unsigned long flags;
1119 spin_lock_irqsave(shost->host_lock, flags);
1120 sym_exec_user_command(np, uc);
1121 spin_unlock_irqrestore(shost->host_lock, flags);
1123 return length;
1126 #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1129 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1131 * Informations through the proc file system.
1133 struct info_str {
1134 char *buffer;
1135 int length;
1136 int offset;
1137 int pos;
1140 static void copy_mem_info(struct info_str *info, char *data, int len)
1142 if (info->pos + len > info->length)
1143 len = info->length - info->pos;
1145 if (info->pos + len < info->offset) {
1146 info->pos += len;
1147 return;
1149 if (info->pos < info->offset) {
1150 data += (info->offset - info->pos);
1151 len -= (info->offset - info->pos);
1154 if (len > 0) {
1155 memcpy(info->buffer + info->pos, data, len);
1156 info->pos += len;
1160 static int copy_info(struct info_str *info, char *fmt, ...)
1162 va_list args;
1163 char buf[81];
1164 int len;
1166 va_start(args, fmt);
1167 len = vsprintf(buf, fmt, args);
1168 va_end(args);
1170 copy_mem_info(info, buf, len);
1171 return len;
1175 * Copy formatted information into the input buffer.
1177 static int sym_host_info(struct Scsi_Host *shost, char *ptr, off_t offset, int len)
1179 struct sym_data *sym_data = shost_priv(shost);
1180 struct pci_dev *pdev = sym_data->pdev;
1181 struct sym_hcb *np = sym_data->ncb;
1182 struct info_str info;
1184 info.buffer = ptr;
1185 info.length = len;
1186 info.offset = offset;
1187 info.pos = 0;
1189 copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1190 "revision id 0x%x\n", np->s.chip_name,
1191 pdev->device, pdev->revision);
1192 copy_info(&info, "At PCI address %s, IRQ %u\n",
1193 pci_name(pdev), pdev->irq);
1194 copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1195 (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1196 np->maxwide ? "Wide" : "Narrow",
1197 np->minsync_dt ? ", DT capable" : "");
1199 copy_info(&info, "Max. started commands %d, "
1200 "max. commands per LUN %d\n",
1201 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1203 return info.pos > info.offset? info.pos - info.offset : 0;
1205 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1208 * Entry point of the scsi proc fs of the driver.
1209 * - func = 0 means read (returns adapter infos)
1210 * - func = 1 means write (not yet merget from sym53c8xx)
1212 static int sym53c8xx_proc_info(struct Scsi_Host *shost, char *buffer,
1213 char **start, off_t offset, int length, int func)
1215 int retv;
1217 if (func) {
1218 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1219 retv = sym_user_command(shost, buffer, length);
1220 #else
1221 retv = -EINVAL;
1222 #endif
1223 } else {
1224 if (start)
1225 *start = buffer;
1226 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1227 retv = sym_host_info(shost, buffer, offset, length);
1228 #else
1229 retv = -EINVAL;
1230 #endif
1233 return retv;
1235 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1238 * Free controller resources.
1240 static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
1243 * Free O/S specific resources.
1245 if (pdev->irq)
1246 free_irq(pdev->irq, np->s.host);
1247 if (np->s.ioaddr)
1248 pci_iounmap(pdev, np->s.ioaddr);
1249 if (np->s.ramaddr)
1250 pci_iounmap(pdev, np->s.ramaddr);
1252 * Free O/S independent resources.
1254 sym_hcb_free(np);
1256 sym_mfree_dma(np, sizeof(*np), "HCB");
1260 * Host attach and initialisations.
1262 * Allocate host data and ncb structure.
1263 * Remap MMIO region.
1264 * Do chip initialization.
1265 * If all is OK, install interrupt handling and
1266 * start the timer daemon.
1268 static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
1269 int unit, struct sym_device *dev)
1271 struct sym_data *sym_data;
1272 struct sym_hcb *np = NULL;
1273 struct Scsi_Host *shost;
1274 struct pci_dev *pdev = dev->pdev;
1275 unsigned long flags;
1276 struct sym_fw *fw;
1278 printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq %u\n",
1279 unit, dev->chip.name, pdev->revision, pci_name(pdev),
1280 pdev->irq);
1283 * Get the firmware for this chip.
1285 fw = sym_find_firmware(&dev->chip);
1286 if (!fw)
1287 return NULL;
1289 shost = scsi_host_alloc(tpnt, sizeof(*sym_data));
1290 if (!shost)
1291 return NULL;
1292 sym_data = shost_priv(shost);
1295 * Allocate immediately the host control block,
1296 * since we are only expecting to succeed. :)
1297 * We keep track in the HCB of all the resources that
1298 * are to be released on error.
1300 np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
1301 if (!np)
1302 goto attach_failed;
1303 np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
1304 sym_data->ncb = np;
1305 sym_data->pdev = pdev;
1306 np->s.host = shost;
1308 pci_set_drvdata(pdev, shost);
1311 * Copy some useful infos to the HCB.
1313 np->hcb_ba = vtobus(np);
1314 np->verbose = sym_driver_setup.verbose;
1315 np->s.unit = unit;
1316 np->features = dev->chip.features;
1317 np->clock_divn = dev->chip.nr_divisor;
1318 np->maxoffs = dev->chip.offset_max;
1319 np->maxburst = dev->chip.burst_max;
1320 np->myaddr = dev->host_id;
1323 * Edit its name.
1325 strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1326 sprintf(np->s.inst_name, "sym%d", np->s.unit);
1328 if ((SYM_CONF_DMA_ADDRESSING_MODE > 0) && (np->features & FE_DAC) &&
1329 !pci_set_dma_mask(pdev, DMA_DAC_MASK)) {
1330 set_dac(np);
1331 } else if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1332 printf_warning("%s: No suitable DMA available\n", sym_name(np));
1333 goto attach_failed;
1337 * Try to map the controller chip to
1338 * virtual and physical memory.
1340 np->mmio_ba = (u32)dev->mmio_base;
1341 np->s.ioaddr = dev->s.ioaddr;
1342 np->s.ramaddr = dev->s.ramaddr;
1345 * Map on-chip RAM if present and supported.
1347 if (!(np->features & FE_RAM))
1348 dev->ram_base = 0;
1349 if (dev->ram_base)
1350 np->ram_ba = (u32)dev->ram_base;
1352 if (sym_hcb_attach(shost, fw, dev->nvram))
1353 goto attach_failed;
1356 * Install the interrupt handler.
1357 * If we synchonize the C code with SCRIPTS on interrupt,
1358 * we do not want to share the INTR line at all.
1360 if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX,
1361 shost)) {
1362 printf_err("%s: request irq %u failure\n",
1363 sym_name(np), pdev->irq);
1364 goto attach_failed;
1368 * After SCSI devices have been opened, we cannot
1369 * reset the bus safely, so we do it here.
1371 spin_lock_irqsave(shost->host_lock, flags);
1372 if (sym_reset_scsi_bus(np, 0))
1373 goto reset_failed;
1376 * Start the SCRIPTS.
1378 sym_start_up(shost, 1);
1381 * Start the timer daemon
1383 init_timer(&np->s.timer);
1384 np->s.timer.data = (unsigned long) np;
1385 np->s.timer.function = sym53c8xx_timer;
1386 np->s.lasttime=0;
1387 sym_timer (np);
1390 * Fill Linux host instance structure
1391 * and return success.
1393 shost->max_channel = 0;
1394 shost->this_id = np->myaddr;
1395 shost->max_id = np->maxwide ? 16 : 8;
1396 shost->max_lun = SYM_CONF_MAX_LUN;
1397 shost->unique_id = pci_resource_start(pdev, 0);
1398 shost->cmd_per_lun = SYM_CONF_MAX_TAG;
1399 shost->can_queue = (SYM_CONF_MAX_START-2);
1400 shost->sg_tablesize = SYM_CONF_MAX_SG;
1401 shost->max_cmd_len = 16;
1402 BUG_ON(sym2_transport_template == NULL);
1403 shost->transportt = sym2_transport_template;
1405 /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
1406 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
1407 shost->dma_boundary = 0xFFFFFF;
1409 spin_unlock_irqrestore(shost->host_lock, flags);
1411 return shost;
1413 reset_failed:
1414 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1415 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1416 spin_unlock_irqrestore(shost->host_lock, flags);
1417 attach_failed:
1418 if (!shost)
1419 return NULL;
1420 printf_info("%s: giving up ...\n", sym_name(np));
1421 if (np)
1422 sym_free_resources(np, pdev);
1423 scsi_host_put(shost);
1425 return NULL;
1430 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1432 #if SYM_CONF_NVRAM_SUPPORT
1433 static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1435 devp->nvram = nvp;
1436 nvp->type = 0;
1438 sym_read_nvram(devp, nvp);
1440 #else
1441 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1444 #endif /* SYM_CONF_NVRAM_SUPPORT */
1446 static int __devinit sym_check_supported(struct sym_device *device)
1448 struct sym_chip *chip;
1449 struct pci_dev *pdev = device->pdev;
1450 unsigned long io_port = pci_resource_start(pdev, 0);
1451 int i;
1454 * If user excluded this chip, do not initialize it.
1455 * I hate this code so much. Must kill it.
1457 if (io_port) {
1458 for (i = 0 ; i < 8 ; i++) {
1459 if (sym_driver_setup.excludes[i] == io_port)
1460 return -ENODEV;
1465 * Check if the chip is supported. Then copy the chip description
1466 * to our device structure so we can make it match the actual device
1467 * and options.
1469 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
1470 if (!chip) {
1471 dev_info(&pdev->dev, "device not supported\n");
1472 return -ENODEV;
1474 memcpy(&device->chip, chip, sizeof(device->chip));
1476 return 0;
1480 * Ignore Symbios chips controlled by various RAID controllers.
1481 * These controllers set value 0x52414944 at RAM end - 16.
1483 static int __devinit sym_check_raid(struct sym_device *device)
1485 unsigned int ram_size, ram_val;
1487 if (!device->s.ramaddr)
1488 return 0;
1490 if (device->chip.features & FE_RAM8K)
1491 ram_size = 8192;
1492 else
1493 ram_size = 4096;
1495 ram_val = readl(device->s.ramaddr + ram_size - 16);
1496 if (ram_val != 0x52414944)
1497 return 0;
1499 dev_info(&device->pdev->dev,
1500 "not initializing, driven by RAID controller.\n");
1501 return -ENODEV;
1504 static int __devinit sym_set_workarounds(struct sym_device *device)
1506 struct sym_chip *chip = &device->chip;
1507 struct pci_dev *pdev = device->pdev;
1508 u_short status_reg;
1511 * (ITEM 12 of a DEL about the 896 I haven't yet).
1512 * We must ensure the chip will use WRITE AND INVALIDATE.
1513 * The revision number limit is for now arbitrary.
1515 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 0x4) {
1516 chip->features |= (FE_WRIE | FE_CLSE);
1519 /* If the chip can do Memory Write Invalidate, enable it */
1520 if (chip->features & FE_WRIE) {
1521 if (pci_set_mwi(pdev))
1522 return -ENODEV;
1526 * Work around for errant bit in 895A. The 66Mhz
1527 * capable bit is set erroneously. Clear this bit.
1528 * (Item 1 DEL 533)
1530 * Make sure Config space and Features agree.
1532 * Recall: writes are not normal to status register -
1533 * write a 1 to clear and a 0 to leave unchanged.
1534 * Can only reset bits.
1536 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1537 if (chip->features & FE_66MHZ) {
1538 if (!(status_reg & PCI_STATUS_66MHZ))
1539 chip->features &= ~FE_66MHZ;
1540 } else {
1541 if (status_reg & PCI_STATUS_66MHZ) {
1542 status_reg = PCI_STATUS_66MHZ;
1543 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1544 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1548 return 0;
1552 * Read and check the PCI configuration for any detected NCR
1553 * boards and save data for attaching after all boards have
1554 * been detected.
1556 static void __devinit
1557 sym_init_device(struct pci_dev *pdev, struct sym_device *device)
1559 int i = 2;
1560 struct pci_bus_region bus_addr;
1562 device->host_id = SYM_SETUP_HOST_ID;
1563 device->pdev = pdev;
1565 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
1566 device->mmio_base = bus_addr.start;
1569 * If the BAR is 64-bit, resource 2 will be occupied by the
1570 * upper 32 bits
1572 if (!pdev->resource[i].flags)
1573 i++;
1574 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
1575 device->ram_base = bus_addr.start;
1577 #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
1578 if (device->mmio_base)
1579 device->s.ioaddr = pci_iomap(pdev, 1,
1580 pci_resource_len(pdev, 1));
1581 #endif
1582 if (!device->s.ioaddr)
1583 device->s.ioaddr = pci_iomap(pdev, 0,
1584 pci_resource_len(pdev, 0));
1585 if (device->ram_base)
1586 device->s.ramaddr = pci_iomap(pdev, i,
1587 pci_resource_len(pdev, i));
1591 * The NCR PQS and PDS cards are constructed as a DEC bridge
1592 * behind which sits a proprietary NCR memory controller and
1593 * either four or two 53c875s as separate devices. We can tell
1594 * if an 875 is part of a PQS/PDS or not since if it is, it will
1595 * be on the same bus as the memory controller. In its usual
1596 * mode of operation, the 875s are slaved to the memory
1597 * controller for all transfers. To operate with the Linux
1598 * driver, the memory controller is disabled and the 875s
1599 * freed to function independently. The only wrinkle is that
1600 * the preset SCSI ID (which may be zero) must be read in from
1601 * a special configuration space register of the 875.
1603 static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
1605 int slot;
1606 u8 tmp;
1608 for (slot = 0; slot < 256; slot++) {
1609 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
1611 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
1612 pci_dev_put(memc);
1613 continue;
1616 /* bit 1: allow individual 875 configuration */
1617 pci_read_config_byte(memc, 0x44, &tmp);
1618 if ((tmp & 0x2) == 0) {
1619 tmp |= 0x2;
1620 pci_write_config_byte(memc, 0x44, tmp);
1623 /* bit 2: drive individual 875 interrupts to the bus */
1624 pci_read_config_byte(memc, 0x45, &tmp);
1625 if ((tmp & 0x4) == 0) {
1626 tmp |= 0x4;
1627 pci_write_config_byte(memc, 0x45, tmp);
1630 pci_dev_put(memc);
1631 break;
1634 pci_read_config_byte(pdev, 0x84, &tmp);
1635 sym_dev->host_id = tmp;
1639 * Called before unloading the module.
1640 * Detach the host.
1641 * We have to free resources and halt the NCR chip.
1643 static int sym_detach(struct Scsi_Host *shost, struct pci_dev *pdev)
1645 struct sym_hcb *np = sym_get_hcb(shost);
1646 printk("%s: detaching ...\n", sym_name(np));
1648 del_timer_sync(&np->s.timer);
1651 * Reset NCR chip.
1652 * We should use sym_soft_reset(), but we don't want to do
1653 * so, since we may not be safe if interrupts occur.
1655 printk("%s: resetting chip\n", sym_name(np));
1656 OUTB(np, nc_istat, SRST);
1657 INB(np, nc_mbox1);
1658 udelay(10);
1659 OUTB(np, nc_istat, 0);
1661 sym_free_resources(np, pdev);
1663 return 1;
1667 * Driver host template.
1669 static struct scsi_host_template sym2_template = {
1670 .module = THIS_MODULE,
1671 .name = "sym53c8xx",
1672 .info = sym53c8xx_info,
1673 .queuecommand = sym53c8xx_queue_command,
1674 .slave_alloc = sym53c8xx_slave_alloc,
1675 .slave_configure = sym53c8xx_slave_configure,
1676 .slave_destroy = sym53c8xx_slave_destroy,
1677 .eh_abort_handler = sym53c8xx_eh_abort_handler,
1678 .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
1679 .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
1680 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
1681 .this_id = 7,
1682 .use_clustering = ENABLE_CLUSTERING,
1683 .use_sg_chaining = ENABLE_SG_CHAINING,
1684 .max_sectors = 0xFFFF,
1685 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1686 .proc_info = sym53c8xx_proc_info,
1687 .proc_name = NAME53C8XX,
1688 #endif
1691 static int attach_count;
1693 static int __devinit sym2_probe(struct pci_dev *pdev,
1694 const struct pci_device_id *ent)
1696 struct sym_device sym_dev;
1697 struct sym_nvram nvram;
1698 struct Scsi_Host *shost;
1700 memset(&sym_dev, 0, sizeof(sym_dev));
1701 memset(&nvram, 0, sizeof(nvram));
1703 if (pci_enable_device(pdev))
1704 goto leave;
1706 pci_set_master(pdev);
1708 if (pci_request_regions(pdev, NAME53C8XX))
1709 goto disable;
1711 sym_init_device(pdev, &sym_dev);
1712 if (sym_check_supported(&sym_dev))
1713 goto free;
1715 if (sym_check_raid(&sym_dev))
1716 goto leave; /* Don't disable the device */
1718 if (sym_set_workarounds(&sym_dev))
1719 goto free;
1721 sym_config_pqs(pdev, &sym_dev);
1723 sym_get_nvram(&sym_dev, &nvram);
1725 shost = sym_attach(&sym2_template, attach_count, &sym_dev);
1726 if (!shost)
1727 goto free;
1729 if (scsi_add_host(shost, &pdev->dev))
1730 goto detach;
1731 scsi_scan_host(shost);
1733 attach_count++;
1735 return 0;
1737 detach:
1738 sym_detach(pci_get_drvdata(pdev), pdev);
1739 free:
1740 pci_release_regions(pdev);
1741 disable:
1742 pci_disable_device(pdev);
1743 leave:
1744 return -ENODEV;
1747 static void __devexit sym2_remove(struct pci_dev *pdev)
1749 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1751 scsi_remove_host(shost);
1752 scsi_host_put(shost);
1753 sym_detach(shost, pdev);
1754 pci_release_regions(pdev);
1755 pci_disable_device(pdev);
1757 attach_count--;
1761 * sym2_io_error_detected() - called when PCI error is detected
1762 * @pdev: pointer to PCI device
1763 * @state: current state of the PCI slot
1765 static pci_ers_result_t sym2_io_error_detected(struct pci_dev *pdev,
1766 enum pci_channel_state state)
1768 /* If slot is permanently frozen, turn everything off */
1769 if (state == pci_channel_io_perm_failure) {
1770 sym2_remove(pdev);
1771 return PCI_ERS_RESULT_DISCONNECT;
1774 disable_irq(pdev->irq);
1775 pci_disable_device(pdev);
1777 /* Request that MMIO be enabled, so register dump can be taken. */
1778 return PCI_ERS_RESULT_CAN_RECOVER;
1782 * sym2_io_slot_dump - Enable MMIO and dump debug registers
1783 * @pdev: pointer to PCI device
1785 static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
1787 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1789 sym_dump_registers(shost);
1791 /* Request a slot reset. */
1792 return PCI_ERS_RESULT_NEED_RESET;
1796 * sym2_reset_workarounds - hardware-specific work-arounds
1798 * This routine is similar to sym_set_workarounds(), except
1799 * that, at this point, we already know that the device was
1800 * succesfully intialized at least once before, and so most
1801 * of the steps taken there are un-needed here.
1803 static void sym2_reset_workarounds(struct pci_dev *pdev)
1805 u_short status_reg;
1806 struct sym_chip *chip;
1808 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
1810 /* Work around for errant bit in 895A, in a fashion
1811 * similar to what is done in sym_set_workarounds().
1813 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1814 if (!(chip->features & FE_66MHZ) && (status_reg & PCI_STATUS_66MHZ)) {
1815 status_reg = PCI_STATUS_66MHZ;
1816 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1817 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1822 * sym2_io_slot_reset() - called when the pci bus has been reset.
1823 * @pdev: pointer to PCI device
1825 * Restart the card from scratch.
1827 static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
1829 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1830 struct sym_hcb *np = sym_get_hcb(shost);
1832 printk(KERN_INFO "%s: recovering from a PCI slot reset\n",
1833 sym_name(np));
1835 if (pci_enable_device(pdev)) {
1836 printk(KERN_ERR "%s: Unable to enable after PCI reset\n",
1837 sym_name(np));
1838 return PCI_ERS_RESULT_DISCONNECT;
1841 pci_set_master(pdev);
1842 enable_irq(pdev->irq);
1844 /* If the chip can do Memory Write Invalidate, enable it */
1845 if (np->features & FE_WRIE) {
1846 if (pci_set_mwi(pdev))
1847 return PCI_ERS_RESULT_DISCONNECT;
1850 /* Perform work-arounds, analogous to sym_set_workarounds() */
1851 sym2_reset_workarounds(pdev);
1853 /* Perform host reset only on one instance of the card */
1854 if (PCI_FUNC(pdev->devfn) == 0) {
1855 if (sym_reset_scsi_bus(np, 0)) {
1856 printk(KERN_ERR "%s: Unable to reset scsi host\n",
1857 sym_name(np));
1858 return PCI_ERS_RESULT_DISCONNECT;
1860 sym_start_up(shost, 1);
1863 return PCI_ERS_RESULT_RECOVERED;
1867 * sym2_io_resume() - resume normal ops after PCI reset
1868 * @pdev: pointer to PCI device
1870 * Called when the error recovery driver tells us that its
1871 * OK to resume normal operation. Use completion to allow
1872 * halted scsi ops to resume.
1874 static void sym2_io_resume(struct pci_dev *pdev)
1876 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1877 struct sym_data *sym_data = shost_priv(shost);
1879 spin_lock_irq(shost->host_lock);
1880 if (sym_data->io_reset)
1881 complete_all(sym_data->io_reset);
1882 sym_data->io_reset = NULL;
1883 spin_unlock_irq(shost->host_lock);
1886 static void sym2_get_signalling(struct Scsi_Host *shost)
1888 struct sym_hcb *np = sym_get_hcb(shost);
1889 enum spi_signal_type type;
1891 switch (np->scsi_mode) {
1892 case SMODE_SE:
1893 type = SPI_SIGNAL_SE;
1894 break;
1895 case SMODE_LVD:
1896 type = SPI_SIGNAL_LVD;
1897 break;
1898 case SMODE_HVD:
1899 type = SPI_SIGNAL_HVD;
1900 break;
1901 default:
1902 type = SPI_SIGNAL_UNKNOWN;
1903 break;
1905 spi_signalling(shost) = type;
1908 static void sym2_set_offset(struct scsi_target *starget, int offset)
1910 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1911 struct sym_hcb *np = sym_get_hcb(shost);
1912 struct sym_tcb *tp = &np->target[starget->id];
1914 tp->tgoal.offset = offset;
1915 tp->tgoal.check_nego = 1;
1918 static void sym2_set_period(struct scsi_target *starget, int period)
1920 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1921 struct sym_hcb *np = sym_get_hcb(shost);
1922 struct sym_tcb *tp = &np->target[starget->id];
1924 /* have to have DT for these transfers, but DT will also
1925 * set width, so check that this is allowed */
1926 if (period <= np->minsync && spi_width(starget))
1927 tp->tgoal.dt = 1;
1929 tp->tgoal.period = period;
1930 tp->tgoal.check_nego = 1;
1933 static void sym2_set_width(struct scsi_target *starget, int width)
1935 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1936 struct sym_hcb *np = sym_get_hcb(shost);
1937 struct sym_tcb *tp = &np->target[starget->id];
1939 /* It is illegal to have DT set on narrow transfers. If DT is
1940 * clear, we must also clear IU and QAS. */
1941 if (width == 0)
1942 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1944 tp->tgoal.width = width;
1945 tp->tgoal.check_nego = 1;
1948 static void sym2_set_dt(struct scsi_target *starget, int dt)
1950 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1951 struct sym_hcb *np = sym_get_hcb(shost);
1952 struct sym_tcb *tp = &np->target[starget->id];
1954 /* We must clear QAS and IU if DT is clear */
1955 if (dt)
1956 tp->tgoal.dt = 1;
1957 else
1958 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1959 tp->tgoal.check_nego = 1;
1962 #if 0
1963 static void sym2_set_iu(struct scsi_target *starget, int iu)
1965 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1966 struct sym_hcb *np = sym_get_hcb(shost);
1967 struct sym_tcb *tp = &np->target[starget->id];
1969 if (iu)
1970 tp->tgoal.iu = tp->tgoal.dt = 1;
1971 else
1972 tp->tgoal.iu = 0;
1973 tp->tgoal.check_nego = 1;
1976 static void sym2_set_qas(struct scsi_target *starget, int qas)
1978 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1979 struct sym_hcb *np = sym_get_hcb(shost);
1980 struct sym_tcb *tp = &np->target[starget->id];
1982 if (qas)
1983 tp->tgoal.dt = tp->tgoal.qas = 1;
1984 else
1985 tp->tgoal.qas = 0;
1986 tp->tgoal.check_nego = 1;
1988 #endif
1990 static struct spi_function_template sym2_transport_functions = {
1991 .set_offset = sym2_set_offset,
1992 .show_offset = 1,
1993 .set_period = sym2_set_period,
1994 .show_period = 1,
1995 .set_width = sym2_set_width,
1996 .show_width = 1,
1997 .set_dt = sym2_set_dt,
1998 .show_dt = 1,
1999 #if 0
2000 .set_iu = sym2_set_iu,
2001 .show_iu = 1,
2002 .set_qas = sym2_set_qas,
2003 .show_qas = 1,
2004 #endif
2005 .get_signalling = sym2_get_signalling,
2008 static struct pci_device_id sym2_id_table[] __devinitdata = {
2009 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
2010 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2011 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
2012 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2013 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
2014 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2015 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
2016 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2017 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
2018 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2019 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
2020 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2021 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
2022 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
2023 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
2024 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2025 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
2026 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2027 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2028 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2029 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2030 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2031 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
2032 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
2033 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2034 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2035 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2036 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2037 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2038 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2039 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2040 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2041 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2042 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2043 { 0, }
2046 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2048 static struct pci_error_handlers sym2_err_handler = {
2049 .error_detected = sym2_io_error_detected,
2050 .mmio_enabled = sym2_io_slot_dump,
2051 .slot_reset = sym2_io_slot_reset,
2052 .resume = sym2_io_resume,
2055 static struct pci_driver sym2_driver = {
2056 .name = NAME53C8XX,
2057 .id_table = sym2_id_table,
2058 .probe = sym2_probe,
2059 .remove = __devexit_p(sym2_remove),
2060 .err_handler = &sym2_err_handler,
2063 static int __init sym2_init(void)
2065 int error;
2067 sym2_setup_params();
2068 sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2069 if (!sym2_transport_template)
2070 return -ENODEV;
2072 error = pci_register_driver(&sym2_driver);
2073 if (error)
2074 spi_release_transport(sym2_transport_template);
2075 return error;
2078 static void __exit sym2_exit(void)
2080 pci_unregister_driver(&sym2_driver);
2081 spi_release_transport(sym2_transport_template);
2084 module_init(sym2_init);
2085 module_exit(sym2_exit);