[SCSI] sym53c8xx: Use scmd_printk where appropriate
[linux-2.6/kvm.git] / drivers / scsi / sym53c8xx_2 / sym_glue.c
blob84a0fc571ceb736df90fbd50591b1c662a3f0293
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_named(burst, sym_driver_setup.burst_order, byte, 0);
67 module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
68 module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
69 module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
70 module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
71 module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
72 module_param_named(verb, sym_driver_setup.verbose, byte, 0);
73 module_param_named(debug, sym_debug_flags, uint, 0);
74 module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
75 module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
76 module_param_named(excl, excl_string, charp, 0);
77 module_param_named(safe, safe_string, charp, 0);
79 MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
80 MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
81 MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
82 MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
83 MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
84 MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
85 MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
86 MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
87 MODULE_PARM_DESC(debug, "Set bits to enable debugging");
88 MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
89 MODULE_PARM_DESC(nvram, "Option currently not used");
90 MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
91 MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
93 MODULE_LICENSE("GPL");
94 MODULE_VERSION(SYM_VERSION);
95 MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
96 MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
98 static void sym2_setup_params(void)
100 char *p = excl_string;
101 int xi = 0;
103 while (p && (xi < 8)) {
104 char *next_p;
105 int val = (int) simple_strtoul(p, &next_p, 0);
106 sym_driver_setup.excludes[xi++] = val;
107 p = next_p;
110 if (safe_string) {
111 if (*safe_string == 'y') {
112 sym_driver_setup.max_tag = 0;
113 sym_driver_setup.burst_order = 0;
114 sym_driver_setup.scsi_led = 0;
115 sym_driver_setup.scsi_diff = 1;
116 sym_driver_setup.irq_mode = 0;
117 sym_driver_setup.scsi_bus_check = 2;
118 sym_driver_setup.host_id = 7;
119 sym_driver_setup.verbose = 2;
120 sym_driver_setup.settle_delay = 10;
121 sym_driver_setup.use_nvram = 1;
122 } else if (*safe_string != 'n') {
123 printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
124 " passed to safe option", safe_string);
129 static struct scsi_transport_template *sym2_transport_template = NULL;
132 * Driver private area in the SCSI command structure.
134 struct sym_ucmd { /* Override the SCSI pointer structure */
135 struct completion *eh_done; /* SCSI error handling */
138 #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
139 #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
142 * Complete a pending CAM CCB.
144 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
146 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
147 BUILD_BUG_ON(sizeof(struct scsi_pointer) < sizeof(struct sym_ucmd));
149 if (ucmd->eh_done)
150 complete(ucmd->eh_done);
152 scsi_dma_unmap(cmd);
153 cmd->scsi_done(cmd);
157 * Tell the SCSI layer about a BUS RESET.
159 void sym_xpt_async_bus_reset(struct sym_hcb *np)
161 printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
162 np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
163 np->s.settle_time_valid = 1;
164 if (sym_verbose >= 2)
165 printf_info("%s: command processing suspended for %d seconds\n",
166 sym_name(np), sym_driver_setup.settle_delay);
170 * Tell the SCSI layer about a BUS DEVICE RESET message sent.
172 void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
174 printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
178 * Choose the more appropriate CAM status if
179 * the IO encountered an extended error.
181 static int sym_xerr_cam_status(int cam_status, int x_status)
183 if (x_status) {
184 if (x_status & XE_PARITY_ERR)
185 cam_status = DID_PARITY;
186 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
187 cam_status = DID_ERROR;
188 else if (x_status & XE_BAD_PHASE)
189 cam_status = DID_ERROR;
190 else
191 cam_status = DID_ERROR;
193 return cam_status;
197 * Build CAM result for a failed or auto-sensed IO.
199 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
201 struct scsi_cmnd *cmd = cp->cmd;
202 u_int cam_status, scsi_status, drv_status;
204 drv_status = 0;
205 cam_status = DID_OK;
206 scsi_status = cp->ssss_status;
208 if (cp->host_flags & HF_SENSE) {
209 scsi_status = cp->sv_scsi_status;
210 resid = cp->sv_resid;
211 if (sym_verbose && cp->sv_xerr_status)
212 sym_print_xerr(cmd, cp->sv_xerr_status);
213 if (cp->host_status == HS_COMPLETE &&
214 cp->ssss_status == S_GOOD &&
215 cp->xerr_status == 0) {
216 cam_status = sym_xerr_cam_status(DID_OK,
217 cp->sv_xerr_status);
218 drv_status = DRIVER_SENSE;
220 * Bounce back the sense data to user.
222 memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
223 memcpy(cmd->sense_buffer, cp->sns_bbuf,
224 min(sizeof(cmd->sense_buffer),
225 (size_t)SYM_SNS_BBUF_LEN));
226 #if 0
228 * If the device reports a UNIT ATTENTION condition
229 * due to a RESET condition, we should consider all
230 * disconnect CCBs for this unit as aborted.
232 if (1) {
233 u_char *p;
234 p = (u_char *) cmd->sense_data;
235 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
236 sym_clear_tasks(np, DID_ABORT,
237 cp->target,cp->lun, -1);
239 #endif
240 } else {
242 * Error return from our internal request sense. This
243 * is bad: we must clear the contingent allegiance
244 * condition otherwise the device will always return
245 * BUSY. Use a big stick.
247 sym_reset_scsi_target(np, cmd->device->id);
248 cam_status = DID_ERROR;
250 } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
251 cam_status = DID_OK;
252 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
253 cam_status = DID_NO_CONNECT;
254 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
255 cam_status = DID_ERROR;
256 else { /* Extended error */
257 if (sym_verbose) {
258 sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
259 cp->host_status, cp->ssss_status,
260 cp->xerr_status);
263 * Set the most appropriate value for CAM status.
265 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
267 scsi_set_resid(cmd, resid);
268 cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
271 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
273 int segment;
274 int use_sg;
276 cp->data_len = 0;
278 use_sg = scsi_dma_map(cmd);
279 if (use_sg > 0) {
280 struct scatterlist *sg;
281 struct sym_tcb *tp = &np->target[cp->target];
282 struct sym_tblmove *data;
284 if (use_sg > SYM_CONF_MAX_SG) {
285 scsi_dma_unmap(cmd);
286 return -1;
289 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
291 scsi_for_each_sg(cmd, sg, use_sg, segment) {
292 dma_addr_t baddr = sg_dma_address(sg);
293 unsigned int len = sg_dma_len(sg);
295 if ((len & 1) && (tp->head.wval & EWS)) {
296 len++;
297 cp->odd_byte_adjustment++;
300 sym_build_sge(np, &data[segment], baddr, len);
301 cp->data_len += len;
303 } else {
304 segment = -2;
307 return segment;
311 * Queue a SCSI command.
313 static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
315 struct scsi_device *sdev = cmd->device;
316 struct sym_tcb *tp;
317 struct sym_lcb *lp;
318 struct sym_ccb *cp;
319 int order;
322 * Retrieve the target descriptor.
324 tp = &np->target[sdev->id];
327 * Select tagged/untagged.
329 lp = sym_lp(tp, sdev->lun);
330 order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
333 * Queue the SCSI IO.
335 cp = sym_get_ccb(np, cmd, order);
336 if (!cp)
337 return 1; /* Means resource shortage */
338 sym_queue_scsiio(np, cmd, cp);
339 return 0;
343 * Setup buffers and pointers that address the CDB.
345 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
347 memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
349 cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
350 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
352 return 0;
356 * Setup pointers that address the data and start the I/O.
358 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
360 u32 lastp, goalp;
361 int dir;
364 * Build the CDB.
366 if (sym_setup_cdb(np, cmd, cp))
367 goto out_abort;
370 * No direction means no data.
372 dir = cmd->sc_data_direction;
373 if (dir != DMA_NONE) {
374 cp->segments = sym_scatter(np, cp, cmd);
375 if (cp->segments < 0) {
376 sym_set_cam_status(cmd, DID_ERROR);
377 goto out_abort;
381 * No segments means no data.
383 if (!cp->segments)
384 dir = DMA_NONE;
385 } else {
386 cp->data_len = 0;
387 cp->segments = 0;
391 * Set the data pointer.
393 switch (dir) {
394 case DMA_BIDIRECTIONAL:
395 scmd_printk(KERN_INFO, cmd, "got DMA_BIDIRECTIONAL command");
396 sym_set_cam_status(cmd, DID_ERROR);
397 goto out_abort;
398 case DMA_TO_DEVICE:
399 goalp = SCRIPTA_BA(np, data_out2) + 8;
400 lastp = goalp - 8 - (cp->segments * (2*4));
401 break;
402 case DMA_FROM_DEVICE:
403 cp->host_flags |= HF_DATA_IN;
404 goalp = SCRIPTA_BA(np, data_in2) + 8;
405 lastp = goalp - 8 - (cp->segments * (2*4));
406 break;
407 case DMA_NONE:
408 default:
409 lastp = goalp = SCRIPTB_BA(np, no_data);
410 break;
414 * Set all pointers values needed by SCRIPTS.
416 cp->phys.head.lastp = cpu_to_scr(lastp);
417 cp->phys.head.savep = cpu_to_scr(lastp);
418 cp->startp = cp->phys.head.savep;
419 cp->goalp = cpu_to_scr(goalp);
422 * When `#ifed 1', the code below makes the driver
423 * panic on the first attempt to write to a SCSI device.
424 * It is the first test we want to do after a driver
425 * change that does not seem obviously safe. :)
427 #if 0
428 switch (cp->cdb_buf[0]) {
429 case 0x0A: case 0x2A: case 0xAA:
430 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
431 break;
432 default:
433 break;
435 #endif
438 * activate this job.
440 sym_put_start_queue(np, cp);
441 return 0;
443 out_abort:
444 sym_free_ccb(np, cp);
445 sym_xpt_done(np, cmd);
446 return 0;
451 * timer daemon.
453 * Misused to keep the driver running when
454 * interrupts are not configured correctly.
456 static void sym_timer(struct sym_hcb *np)
458 unsigned long thistime = jiffies;
461 * Restart the timer.
463 np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
464 add_timer(&np->s.timer);
467 * If we are resetting the ncr, wait for settle_time before
468 * clearing it. Then command processing will be resumed.
470 if (np->s.settle_time_valid) {
471 if (time_before_eq(np->s.settle_time, thistime)) {
472 if (sym_verbose >= 2 )
473 printk("%s: command processing resumed\n",
474 sym_name(np));
475 np->s.settle_time_valid = 0;
477 return;
481 * Nothing to do for now, but that may come.
483 if (np->s.lasttime + 4*HZ < thistime) {
484 np->s.lasttime = thistime;
487 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
489 * Some way-broken PCI bridges may lead to
490 * completions being lost when the clearing
491 * of the INTFLY flag by the CPU occurs
492 * concurrently with the chip raising this flag.
493 * If this ever happen, lost completions will
494 * be reaped here.
496 sym_wakeup_done(np);
497 #endif
502 * PCI BUS error handler.
504 void sym_log_bus_error(struct sym_hcb *np)
506 u_short pci_sts;
507 pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
508 if (pci_sts & 0xf900) {
509 pci_write_config_word(np->s.device, PCI_STATUS, pci_sts);
510 printf("%s: PCI STATUS = 0x%04x\n",
511 sym_name(np), pci_sts & 0xf900);
516 * queuecommand method. Entered with the host adapter lock held and
517 * interrupts disabled.
519 static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
520 void (*done)(struct scsi_cmnd *))
522 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
523 struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
524 int sts = 0;
526 cmd->scsi_done = done;
527 memset(ucp, 0, sizeof(*ucp));
530 * Shorten our settle_time if needed for
531 * this command not to time out.
533 if (np->s.settle_time_valid && cmd->timeout_per_command) {
534 unsigned long tlimit = jiffies + cmd->timeout_per_command;
535 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
536 if (time_after(np->s.settle_time, tlimit)) {
537 np->s.settle_time = tlimit;
541 if (np->s.settle_time_valid)
542 return SCSI_MLQUEUE_HOST_BUSY;
544 sts = sym_queue_command(np, cmd);
545 if (sts)
546 return SCSI_MLQUEUE_HOST_BUSY;
547 return 0;
551 * Linux entry point of the interrupt handler.
553 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
555 struct sym_hcb *np = dev_id;
557 /* Avoid spinloop trying to handle interrupts on frozen device */
558 if (pci_channel_offline(np->s.device))
559 return IRQ_NONE;
561 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
563 spin_lock(np->s.host->host_lock);
564 sym_interrupt(np);
565 spin_unlock(np->s.host->host_lock);
567 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
569 return IRQ_HANDLED;
573 * Linux entry point of the timer handler
575 static void sym53c8xx_timer(unsigned long npref)
577 struct sym_hcb *np = (struct sym_hcb *)npref;
578 unsigned long flags;
580 spin_lock_irqsave(np->s.host->host_lock, flags);
581 sym_timer(np);
582 spin_unlock_irqrestore(np->s.host->host_lock, flags);
587 * What the eh thread wants us to perform.
589 #define SYM_EH_ABORT 0
590 #define SYM_EH_DEVICE_RESET 1
591 #define SYM_EH_BUS_RESET 2
592 #define SYM_EH_HOST_RESET 3
595 * Generic method for our eh processing.
596 * The 'op' argument tells what we have to do.
598 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
600 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
601 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
602 struct Scsi_Host *host = cmd->device->host;
603 struct pci_dev *pdev = np->s.device;
604 SYM_QUEHEAD *qp;
605 int cmd_queued = 0;
606 int sts = -1;
607 struct completion eh_done;
609 scmd_printk(KERN_WARNING, cmd, "%s operation started.\n", opname);
611 /* We may be in an error condition because the PCI bus
612 * went down. In this case, we need to wait until the
613 * PCI bus is reset, the card is reset, and only then
614 * proceed with the scsi error recovery. There's no
615 * point in hurrying; take a leisurely wait.
617 #define WAIT_FOR_PCI_RECOVERY 35
618 if (pci_channel_offline(pdev)) {
619 struct host_data *hostdata = shost_priv(host);
620 struct completion *io_reset;
621 int finished_reset = 0;
622 init_completion(&eh_done);
623 spin_lock_irq(host->host_lock);
624 /* Make sure we didn't race */
625 if (pci_channel_offline(pdev)) {
626 if (!hostdata->io_reset)
627 hostdata->io_reset = &eh_done;
628 io_reset = hostdata->io_reset;
629 } else {
630 io_reset = NULL;
633 if (!pci_channel_offline(pdev))
634 finished_reset = 1;
635 spin_unlock_irq(host->host_lock);
636 if (!finished_reset)
637 finished_reset = wait_for_completion_timeout(io_reset,
638 WAIT_FOR_PCI_RECOVERY*HZ);
639 if (!finished_reset)
640 return SCSI_FAILED;
643 spin_lock_irq(host->host_lock);
644 /* This one is queued in some place -> to wait for completion */
645 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
646 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
647 if (cp->cmd == cmd) {
648 cmd_queued = 1;
649 break;
653 /* Try to proceed the operation we have been asked for */
654 sts = -1;
655 switch(op) {
656 case SYM_EH_ABORT:
657 sts = sym_abort_scsiio(np, cmd, 1);
658 break;
659 case SYM_EH_DEVICE_RESET:
660 sts = sym_reset_scsi_target(np, cmd->device->id);
661 break;
662 case SYM_EH_BUS_RESET:
663 sym_reset_scsi_bus(np, 1);
664 sts = 0;
665 break;
666 case SYM_EH_HOST_RESET:
667 sym_reset_scsi_bus(np, 0);
668 sym_start_up(np, 1);
669 sts = 0;
670 break;
671 default:
672 break;
675 /* On error, restore everything and cross fingers :) */
676 if (sts)
677 cmd_queued = 0;
679 if (cmd_queued) {
680 init_completion(&eh_done);
681 ucmd->eh_done = &eh_done;
682 spin_unlock_irq(host->host_lock);
683 if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
684 ucmd->eh_done = NULL;
685 sts = -2;
687 } else {
688 spin_unlock_irq(host->host_lock);
691 dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
692 sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
693 return sts ? SCSI_FAILED : SCSI_SUCCESS;
698 * Error handlers called from the eh thread (one thread per HBA).
700 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
702 return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
705 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
707 return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
710 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
712 return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
715 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
717 return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
721 * Tune device queuing depth, according to various limits.
723 static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
725 struct sym_lcb *lp = sym_lp(tp, lun);
726 u_short oldtags;
728 if (!lp)
729 return;
731 oldtags = lp->s.reqtags;
733 if (reqtags > lp->s.scdev_depth)
734 reqtags = lp->s.scdev_depth;
736 lp->s.reqtags = reqtags;
738 if (reqtags != oldtags) {
739 dev_info(&tp->starget->dev,
740 "tagged command queuing %s, command queue depth %d.\n",
741 lp->s.reqtags ? "enabled" : "disabled", reqtags);
745 static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
747 struct sym_hcb *np = sym_get_hcb(sdev->host);
748 struct sym_tcb *tp = &np->target[sdev->id];
749 struct sym_lcb *lp;
751 if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
752 return -ENXIO;
754 tp->starget = sdev->sdev_target;
756 * Fail the device init if the device is flagged NOSCAN at BOOT in
757 * the NVRAM. This may speed up boot and maintain coherency with
758 * BIOS device numbering. Clearing the flag allows the user to
759 * rescan skipped devices later. We also return an error for
760 * devices not flagged for SCAN LUNS in the NVRAM since some single
761 * lun devices behave badly when asked for a non zero LUN.
764 if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
765 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
766 starget_printk(KERN_INFO, tp->starget,
767 "Scan at boot disabled in NVRAM\n");
768 return -ENXIO;
771 if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
772 if (sdev->lun != 0)
773 return -ENXIO;
774 starget_printk(KERN_INFO, tp->starget,
775 "Multiple LUNs disabled in NVRAM\n");
778 lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
779 if (!lp)
780 return -ENOMEM;
782 spi_min_period(tp->starget) = tp->usr_period;
783 spi_max_width(tp->starget) = tp->usr_width;
785 return 0;
789 * Linux entry point for device queue sizing.
791 static int sym53c8xx_slave_configure(struct scsi_device *sdev)
793 struct sym_hcb *np = sym_get_hcb(sdev->host);
794 struct sym_tcb *tp = &np->target[sdev->id];
795 struct sym_lcb *lp = sym_lp(tp, sdev->lun);
796 int reqtags, depth_to_use;
799 * Get user flags.
801 lp->curr_flags = lp->user_flags;
804 * Select queue depth from driver setup.
805 * Donnot use more than configured by user.
806 * Use at least 2.
807 * Donnot use more than our maximum.
809 reqtags = sym_driver_setup.max_tag;
810 if (reqtags > tp->usrtags)
811 reqtags = tp->usrtags;
812 if (!sdev->tagged_supported)
813 reqtags = 0;
814 if (reqtags > SYM_CONF_MAX_TAG)
815 reqtags = SYM_CONF_MAX_TAG;
816 depth_to_use = reqtags ? reqtags : 2;
817 scsi_adjust_queue_depth(sdev,
818 sdev->tagged_supported ? MSG_SIMPLE_TAG : 0,
819 depth_to_use);
820 lp->s.scdev_depth = depth_to_use;
821 sym_tune_dev_queuing(tp, sdev->lun, reqtags);
823 if (!spi_initial_dv(sdev->sdev_target))
824 spi_dv_device(sdev);
826 return 0;
829 static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
831 struct sym_hcb *np = sym_get_hcb(sdev->host);
832 struct sym_lcb *lp = sym_lp(&np->target[sdev->id], sdev->lun);
834 if (lp->itlq_tbl)
835 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK * 4, "ITLQ_TBL");
836 kfree(lp->cb_tags);
837 sym_mfree_dma(lp, sizeof(*lp), "LCB");
841 * Linux entry point for info() function
843 static const char *sym53c8xx_info (struct Scsi_Host *host)
845 return SYM_DRIVER_NAME;
849 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
851 * Proc file system stuff
853 * A read operation returns adapter information.
854 * A write operation is a control command.
855 * The string is parsed in the driver code and the command is passed
856 * to the sym_usercmd() function.
859 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
861 struct sym_usrcmd {
862 u_long target;
863 u_long lun;
864 u_long data;
865 u_long cmd;
868 #define UC_SETSYNC 10
869 #define UC_SETTAGS 11
870 #define UC_SETDEBUG 12
871 #define UC_SETWIDE 14
872 #define UC_SETFLAG 15
873 #define UC_SETVERBOSE 17
874 #define UC_RESETDEV 18
875 #define UC_CLEARDEV 19
877 static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
879 struct sym_tcb *tp;
880 int t, l;
882 switch (uc->cmd) {
883 case 0: return;
885 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
886 case UC_SETDEBUG:
887 sym_debug_flags = uc->data;
888 break;
889 #endif
890 case UC_SETVERBOSE:
891 np->verbose = uc->data;
892 break;
893 default:
895 * We assume that other commands apply to targets.
896 * This should always be the case and avoid the below
897 * 4 lines to be repeated 6 times.
899 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
900 if (!((uc->target >> t) & 1))
901 continue;
902 tp = &np->target[t];
904 switch (uc->cmd) {
906 case UC_SETSYNC:
907 if (!uc->data || uc->data >= 255) {
908 tp->tgoal.iu = tp->tgoal.dt =
909 tp->tgoal.qas = 0;
910 tp->tgoal.offset = 0;
911 } else if (uc->data <= 9 && np->minsync_dt) {
912 if (uc->data < np->minsync_dt)
913 uc->data = np->minsync_dt;
914 tp->tgoal.iu = tp->tgoal.dt =
915 tp->tgoal.qas = 1;
916 tp->tgoal.width = 1;
917 tp->tgoal.period = uc->data;
918 tp->tgoal.offset = np->maxoffs_dt;
919 } else {
920 if (uc->data < np->minsync)
921 uc->data = np->minsync;
922 tp->tgoal.iu = tp->tgoal.dt =
923 tp->tgoal.qas = 0;
924 tp->tgoal.period = uc->data;
925 tp->tgoal.offset = np->maxoffs;
927 tp->tgoal.check_nego = 1;
928 break;
929 case UC_SETWIDE:
930 tp->tgoal.width = uc->data ? 1 : 0;
931 tp->tgoal.check_nego = 1;
932 break;
933 case UC_SETTAGS:
934 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
935 sym_tune_dev_queuing(tp, l, uc->data);
936 break;
937 case UC_RESETDEV:
938 tp->to_reset = 1;
939 np->istat_sem = SEM;
940 OUTB(np, nc_istat, SIGP|SEM);
941 break;
942 case UC_CLEARDEV:
943 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
944 struct sym_lcb *lp = sym_lp(tp, l);
945 if (lp) lp->to_clear = 1;
947 np->istat_sem = SEM;
948 OUTB(np, nc_istat, SIGP|SEM);
949 break;
950 case UC_SETFLAG:
951 tp->usrflags = uc->data;
952 break;
955 break;
959 static int skip_spaces(char *ptr, int len)
961 int cnt, c;
963 for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
965 return (len - cnt);
968 static int get_int_arg(char *ptr, int len, u_long *pv)
970 char *end;
972 *pv = simple_strtoul(ptr, &end, 10);
973 return (end - ptr);
976 static int is_keyword(char *ptr, int len, char *verb)
978 int verb_len = strlen(verb);
980 if (len >= verb_len && !memcmp(verb, ptr, verb_len))
981 return verb_len;
982 else
983 return 0;
986 #define SKIP_SPACES(ptr, len) \
987 if ((arg_len = skip_spaces(ptr, len)) < 1) \
988 return -EINVAL; \
989 ptr += arg_len; len -= arg_len;
991 #define GET_INT_ARG(ptr, len, v) \
992 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
993 return -EINVAL; \
994 ptr += arg_len; len -= arg_len;
998 * Parse a control command
1001 static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
1003 char *ptr = buffer;
1004 int len = length;
1005 struct sym_usrcmd cmd, *uc = &cmd;
1006 int arg_len;
1007 u_long target;
1009 memset(uc, 0, sizeof(*uc));
1011 if (len > 0 && ptr[len-1] == '\n')
1012 --len;
1014 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1015 uc->cmd = UC_SETSYNC;
1016 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1017 uc->cmd = UC_SETTAGS;
1018 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1019 uc->cmd = UC_SETVERBOSE;
1020 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1021 uc->cmd = UC_SETWIDE;
1022 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1023 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1024 uc->cmd = UC_SETDEBUG;
1025 #endif
1026 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1027 uc->cmd = UC_SETFLAG;
1028 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1029 uc->cmd = UC_RESETDEV;
1030 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1031 uc->cmd = UC_CLEARDEV;
1032 else
1033 arg_len = 0;
1035 #ifdef DEBUG_PROC_INFO
1036 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1037 #endif
1039 if (!arg_len)
1040 return -EINVAL;
1041 ptr += arg_len; len -= arg_len;
1043 switch(uc->cmd) {
1044 case UC_SETSYNC:
1045 case UC_SETTAGS:
1046 case UC_SETWIDE:
1047 case UC_SETFLAG:
1048 case UC_RESETDEV:
1049 case UC_CLEARDEV:
1050 SKIP_SPACES(ptr, len);
1051 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1052 ptr += arg_len; len -= arg_len;
1053 uc->target = ~0;
1054 } else {
1055 GET_INT_ARG(ptr, len, target);
1056 uc->target = (1<<target);
1057 #ifdef DEBUG_PROC_INFO
1058 printk("sym_user_command: target=%ld\n", target);
1059 #endif
1061 break;
1064 switch(uc->cmd) {
1065 case UC_SETVERBOSE:
1066 case UC_SETSYNC:
1067 case UC_SETTAGS:
1068 case UC_SETWIDE:
1069 SKIP_SPACES(ptr, len);
1070 GET_INT_ARG(ptr, len, uc->data);
1071 #ifdef DEBUG_PROC_INFO
1072 printk("sym_user_command: data=%ld\n", uc->data);
1073 #endif
1074 break;
1075 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1076 case UC_SETDEBUG:
1077 while (len > 0) {
1078 SKIP_SPACES(ptr, len);
1079 if ((arg_len = is_keyword(ptr, len, "alloc")))
1080 uc->data |= DEBUG_ALLOC;
1081 else if ((arg_len = is_keyword(ptr, len, "phase")))
1082 uc->data |= DEBUG_PHASE;
1083 else if ((arg_len = is_keyword(ptr, len, "queue")))
1084 uc->data |= DEBUG_QUEUE;
1085 else if ((arg_len = is_keyword(ptr, len, "result")))
1086 uc->data |= DEBUG_RESULT;
1087 else if ((arg_len = is_keyword(ptr, len, "scatter")))
1088 uc->data |= DEBUG_SCATTER;
1089 else if ((arg_len = is_keyword(ptr, len, "script")))
1090 uc->data |= DEBUG_SCRIPT;
1091 else if ((arg_len = is_keyword(ptr, len, "tiny")))
1092 uc->data |= DEBUG_TINY;
1093 else if ((arg_len = is_keyword(ptr, len, "timing")))
1094 uc->data |= DEBUG_TIMING;
1095 else if ((arg_len = is_keyword(ptr, len, "nego")))
1096 uc->data |= DEBUG_NEGO;
1097 else if ((arg_len = is_keyword(ptr, len, "tags")))
1098 uc->data |= DEBUG_TAGS;
1099 else if ((arg_len = is_keyword(ptr, len, "pointer")))
1100 uc->data |= DEBUG_POINTER;
1101 else
1102 return -EINVAL;
1103 ptr += arg_len; len -= arg_len;
1105 #ifdef DEBUG_PROC_INFO
1106 printk("sym_user_command: data=%ld\n", uc->data);
1107 #endif
1108 break;
1109 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1110 case UC_SETFLAG:
1111 while (len > 0) {
1112 SKIP_SPACES(ptr, len);
1113 if ((arg_len = is_keyword(ptr, len, "no_disc")))
1114 uc->data &= ~SYM_DISC_ENABLED;
1115 else
1116 return -EINVAL;
1117 ptr += arg_len; len -= arg_len;
1119 break;
1120 default:
1121 break;
1124 if (len)
1125 return -EINVAL;
1126 else {
1127 unsigned long flags;
1129 spin_lock_irqsave(np->s.host->host_lock, flags);
1130 sym_exec_user_command (np, uc);
1131 spin_unlock_irqrestore(np->s.host->host_lock, flags);
1133 return length;
1136 #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1139 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1141 * Informations through the proc file system.
1143 struct info_str {
1144 char *buffer;
1145 int length;
1146 int offset;
1147 int pos;
1150 static void copy_mem_info(struct info_str *info, char *data, int len)
1152 if (info->pos + len > info->length)
1153 len = info->length - info->pos;
1155 if (info->pos + len < info->offset) {
1156 info->pos += len;
1157 return;
1159 if (info->pos < info->offset) {
1160 data += (info->offset - info->pos);
1161 len -= (info->offset - info->pos);
1164 if (len > 0) {
1165 memcpy(info->buffer + info->pos, data, len);
1166 info->pos += len;
1170 static int copy_info(struct info_str *info, char *fmt, ...)
1172 va_list args;
1173 char buf[81];
1174 int len;
1176 va_start(args, fmt);
1177 len = vsprintf(buf, fmt, args);
1178 va_end(args);
1180 copy_mem_info(info, buf, len);
1181 return len;
1185 * Copy formatted information into the input buffer.
1187 static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
1189 struct info_str info;
1191 info.buffer = ptr;
1192 info.length = len;
1193 info.offset = offset;
1194 info.pos = 0;
1196 copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1197 "revision id 0x%x\n", np->s.chip_name,
1198 np->s.device->device, np->s.device->revision);
1199 copy_info(&info, "At PCI address %s, IRQ " IRQ_FMT "\n",
1200 pci_name(np->s.device), IRQ_PRM(np->s.device->irq));
1201 copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1202 (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1203 np->maxwide ? "Wide" : "Narrow",
1204 np->minsync_dt ? ", DT capable" : "");
1206 copy_info(&info, "Max. started commands %d, "
1207 "max. commands per LUN %d\n",
1208 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1210 return info.pos > info.offset? info.pos - info.offset : 0;
1212 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1215 * Entry point of the scsi proc fs of the driver.
1216 * - func = 0 means read (returns adapter infos)
1217 * - func = 1 means write (not yet merget from sym53c8xx)
1219 static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
1220 char **start, off_t offset, int length, int func)
1222 struct sym_hcb *np = sym_get_hcb(host);
1223 int retv;
1225 if (func) {
1226 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1227 retv = sym_user_command(np, buffer, length);
1228 #else
1229 retv = -EINVAL;
1230 #endif
1231 } else {
1232 if (start)
1233 *start = buffer;
1234 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1235 retv = sym_host_info(np, buffer, offset, length);
1236 #else
1237 retv = -EINVAL;
1238 #endif
1241 return retv;
1243 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1246 * Free controller resources.
1248 static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
1251 * Free O/S specific resources.
1253 if (pdev->irq)
1254 free_irq(pdev->irq, np);
1255 if (np->s.ioaddr)
1256 pci_iounmap(pdev, np->s.ioaddr);
1257 if (np->s.ramaddr)
1258 pci_iounmap(pdev, np->s.ramaddr);
1260 * Free O/S independent resources.
1262 sym_hcb_free(np);
1264 sym_mfree_dma(np, sizeof(*np), "HCB");
1268 * Host attach and initialisations.
1270 * Allocate host data and ncb structure.
1271 * Remap MMIO region.
1272 * Do chip initialization.
1273 * If all is OK, install interrupt handling and
1274 * start the timer daemon.
1276 static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
1277 int unit, struct sym_device *dev)
1279 struct host_data *host_data;
1280 struct sym_hcb *np = NULL;
1281 struct Scsi_Host *instance = NULL;
1282 struct pci_dev *pdev = dev->pdev;
1283 unsigned long flags;
1284 struct sym_fw *fw;
1286 printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT "\n",
1287 unit, dev->chip.name, pdev->revision, pci_name(pdev),
1288 IRQ_PRM(pdev->irq));
1291 * Get the firmware for this chip.
1293 fw = sym_find_firmware(&dev->chip);
1294 if (!fw)
1295 goto attach_failed;
1298 * Allocate host_data structure
1300 instance = scsi_host_alloc(tpnt, sizeof(*host_data));
1301 if (!instance)
1302 goto attach_failed;
1303 host_data = (struct host_data *) instance->hostdata;
1306 * Allocate immediately the host control block,
1307 * since we are only expecting to succeed. :)
1308 * We keep track in the HCB of all the resources that
1309 * are to be released on error.
1311 np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
1312 if (!np)
1313 goto attach_failed;
1314 np->s.device = pdev;
1315 np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
1316 host_data->ncb = np;
1317 np->s.host = instance;
1319 pci_set_drvdata(pdev, np);
1322 * Copy some useful infos to the HCB.
1324 np->hcb_ba = vtobus(np);
1325 np->verbose = sym_driver_setup.verbose;
1326 np->s.device = pdev;
1327 np->s.unit = unit;
1328 np->features = dev->chip.features;
1329 np->clock_divn = dev->chip.nr_divisor;
1330 np->maxoffs = dev->chip.offset_max;
1331 np->maxburst = dev->chip.burst_max;
1332 np->myaddr = dev->host_id;
1335 * Edit its name.
1337 strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1338 sprintf(np->s.inst_name, "sym%d", np->s.unit);
1340 if ((SYM_CONF_DMA_ADDRESSING_MODE > 0) && (np->features & FE_DAC) &&
1341 !pci_set_dma_mask(np->s.device, DMA_DAC_MASK)) {
1342 set_dac(np);
1343 } else if (pci_set_dma_mask(np->s.device, DMA_32BIT_MASK)) {
1344 printf_warning("%s: No suitable DMA available\n", sym_name(np));
1345 goto attach_failed;
1349 * Try to map the controller chip to
1350 * virtual and physical memory.
1352 np->mmio_ba = (u32)dev->mmio_base;
1353 np->s.ioaddr = dev->s.ioaddr;
1354 np->s.ramaddr = dev->s.ramaddr;
1357 * Map on-chip RAM if present and supported.
1359 if (!(np->features & FE_RAM))
1360 dev->ram_base = 0;
1361 if (dev->ram_base)
1362 np->ram_ba = (u32)dev->ram_base;
1364 if (sym_hcb_attach(instance, fw, dev->nvram))
1365 goto attach_failed;
1368 * Install the interrupt handler.
1369 * If we synchonize the C code with SCRIPTS on interrupt,
1370 * we do not want to share the INTR line at all.
1372 if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX, np)) {
1373 printf_err("%s: request irq %d failure\n",
1374 sym_name(np), pdev->irq);
1375 goto attach_failed;
1379 * After SCSI devices have been opened, we cannot
1380 * reset the bus safely, so we do it here.
1382 spin_lock_irqsave(instance->host_lock, flags);
1383 if (sym_reset_scsi_bus(np, 0))
1384 goto reset_failed;
1387 * Start the SCRIPTS.
1389 sym_start_up(np, 1);
1392 * Start the timer daemon
1394 init_timer(&np->s.timer);
1395 np->s.timer.data = (unsigned long) np;
1396 np->s.timer.function = sym53c8xx_timer;
1397 np->s.lasttime=0;
1398 sym_timer (np);
1401 * Fill Linux host instance structure
1402 * and return success.
1404 instance->max_channel = 0;
1405 instance->this_id = np->myaddr;
1406 instance->max_id = np->maxwide ? 16 : 8;
1407 instance->max_lun = SYM_CONF_MAX_LUN;
1408 instance->unique_id = pci_resource_start(pdev, 0);
1409 instance->cmd_per_lun = SYM_CONF_MAX_TAG;
1410 instance->can_queue = (SYM_CONF_MAX_START-2);
1411 instance->sg_tablesize = SYM_CONF_MAX_SG;
1412 instance->max_cmd_len = 16;
1413 BUG_ON(sym2_transport_template == NULL);
1414 instance->transportt = sym2_transport_template;
1416 /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
1417 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
1418 instance->dma_boundary = 0xFFFFFF;
1420 spin_unlock_irqrestore(instance->host_lock, flags);
1422 return instance;
1424 reset_failed:
1425 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1426 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1427 spin_unlock_irqrestore(instance->host_lock, flags);
1428 attach_failed:
1429 if (!instance)
1430 return NULL;
1431 printf_info("%s: giving up ...\n", sym_name(np));
1432 if (np)
1433 sym_free_resources(np, pdev);
1434 scsi_host_put(instance);
1436 return NULL;
1441 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1443 #if SYM_CONF_NVRAM_SUPPORT
1444 static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1446 devp->nvram = nvp;
1447 nvp->type = 0;
1449 sym_read_nvram(devp, nvp);
1451 #else
1452 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1455 #endif /* SYM_CONF_NVRAM_SUPPORT */
1457 static int __devinit sym_check_supported(struct sym_device *device)
1459 struct sym_chip *chip;
1460 struct pci_dev *pdev = device->pdev;
1461 unsigned long io_port = pci_resource_start(pdev, 0);
1462 int i;
1465 * If user excluded this chip, do not initialize it.
1466 * I hate this code so much. Must kill it.
1468 if (io_port) {
1469 for (i = 0 ; i < 8 ; i++) {
1470 if (sym_driver_setup.excludes[i] == io_port)
1471 return -ENODEV;
1476 * Check if the chip is supported. Then copy the chip description
1477 * to our device structure so we can make it match the actual device
1478 * and options.
1480 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
1481 if (!chip) {
1482 dev_info(&pdev->dev, "device not supported\n");
1483 return -ENODEV;
1485 memcpy(&device->chip, chip, sizeof(device->chip));
1487 return 0;
1491 * Ignore Symbios chips controlled by various RAID controllers.
1492 * These controllers set value 0x52414944 at RAM end - 16.
1494 static int __devinit sym_check_raid(struct sym_device *device)
1496 unsigned int ram_size, ram_val;
1498 if (!device->s.ramaddr)
1499 return 0;
1501 if (device->chip.features & FE_RAM8K)
1502 ram_size = 8192;
1503 else
1504 ram_size = 4096;
1506 ram_val = readl(device->s.ramaddr + ram_size - 16);
1507 if (ram_val != 0x52414944)
1508 return 0;
1510 dev_info(&device->pdev->dev,
1511 "not initializing, driven by RAID controller.\n");
1512 return -ENODEV;
1515 static int __devinit sym_set_workarounds(struct sym_device *device)
1517 struct sym_chip *chip = &device->chip;
1518 struct pci_dev *pdev = device->pdev;
1519 u_short status_reg;
1522 * (ITEM 12 of a DEL about the 896 I haven't yet).
1523 * We must ensure the chip will use WRITE AND INVALIDATE.
1524 * The revision number limit is for now arbitrary.
1526 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 0x4) {
1527 chip->features |= (FE_WRIE | FE_CLSE);
1530 /* If the chip can do Memory Write Invalidate, enable it */
1531 if (chip->features & FE_WRIE) {
1532 if (pci_set_mwi(pdev))
1533 return -ENODEV;
1537 * Work around for errant bit in 895A. The 66Mhz
1538 * capable bit is set erroneously. Clear this bit.
1539 * (Item 1 DEL 533)
1541 * Make sure Config space and Features agree.
1543 * Recall: writes are not normal to status register -
1544 * write a 1 to clear and a 0 to leave unchanged.
1545 * Can only reset bits.
1547 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1548 if (chip->features & FE_66MHZ) {
1549 if (!(status_reg & PCI_STATUS_66MHZ))
1550 chip->features &= ~FE_66MHZ;
1551 } else {
1552 if (status_reg & PCI_STATUS_66MHZ) {
1553 status_reg = PCI_STATUS_66MHZ;
1554 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1555 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1559 return 0;
1563 * Read and check the PCI configuration for any detected NCR
1564 * boards and save data for attaching after all boards have
1565 * been detected.
1567 static void __devinit
1568 sym_init_device(struct pci_dev *pdev, struct sym_device *device)
1570 int i = 2;
1571 struct pci_bus_region bus_addr;
1573 device->host_id = SYM_SETUP_HOST_ID;
1574 device->pdev = pdev;
1576 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
1577 device->mmio_base = bus_addr.start;
1580 * If the BAR is 64-bit, resource 2 will be occupied by the
1581 * upper 32 bits
1583 if (!pdev->resource[i].flags)
1584 i++;
1585 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
1586 device->ram_base = bus_addr.start;
1588 #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
1589 if (device->mmio_base)
1590 device->s.ioaddr = pci_iomap(pdev, 1,
1591 pci_resource_len(pdev, 1));
1592 #endif
1593 if (!device->s.ioaddr)
1594 device->s.ioaddr = pci_iomap(pdev, 0,
1595 pci_resource_len(pdev, 0));
1596 if (device->ram_base)
1597 device->s.ramaddr = pci_iomap(pdev, i,
1598 pci_resource_len(pdev, i));
1602 * The NCR PQS and PDS cards are constructed as a DEC bridge
1603 * behind which sits a proprietary NCR memory controller and
1604 * either four or two 53c875s as separate devices. We can tell
1605 * if an 875 is part of a PQS/PDS or not since if it is, it will
1606 * be on the same bus as the memory controller. In its usual
1607 * mode of operation, the 875s are slaved to the memory
1608 * controller for all transfers. To operate with the Linux
1609 * driver, the memory controller is disabled and the 875s
1610 * freed to function independently. The only wrinkle is that
1611 * the preset SCSI ID (which may be zero) must be read in from
1612 * a special configuration space register of the 875.
1614 static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
1616 int slot;
1617 u8 tmp;
1619 for (slot = 0; slot < 256; slot++) {
1620 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
1622 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
1623 pci_dev_put(memc);
1624 continue;
1627 /* bit 1: allow individual 875 configuration */
1628 pci_read_config_byte(memc, 0x44, &tmp);
1629 if ((tmp & 0x2) == 0) {
1630 tmp |= 0x2;
1631 pci_write_config_byte(memc, 0x44, tmp);
1634 /* bit 2: drive individual 875 interrupts to the bus */
1635 pci_read_config_byte(memc, 0x45, &tmp);
1636 if ((tmp & 0x4) == 0) {
1637 tmp |= 0x4;
1638 pci_write_config_byte(memc, 0x45, tmp);
1641 pci_dev_put(memc);
1642 break;
1645 pci_read_config_byte(pdev, 0x84, &tmp);
1646 sym_dev->host_id = tmp;
1650 * Called before unloading the module.
1651 * Detach the host.
1652 * We have to free resources and halt the NCR chip.
1654 static int sym_detach(struct sym_hcb *np, struct pci_dev *pdev)
1656 printk("%s: detaching ...\n", sym_name(np));
1658 del_timer_sync(&np->s.timer);
1661 * Reset NCR chip.
1662 * We should use sym_soft_reset(), but we don't want to do
1663 * so, since we may not be safe if interrupts occur.
1665 printk("%s: resetting chip\n", sym_name(np));
1666 OUTB(np, nc_istat, SRST);
1667 INB(np, nc_mbox1);
1668 udelay(10);
1669 OUTB(np, nc_istat, 0);
1671 sym_free_resources(np, pdev);
1673 return 1;
1677 * Driver host template.
1679 static struct scsi_host_template sym2_template = {
1680 .module = THIS_MODULE,
1681 .name = "sym53c8xx",
1682 .info = sym53c8xx_info,
1683 .queuecommand = sym53c8xx_queue_command,
1684 .slave_alloc = sym53c8xx_slave_alloc,
1685 .slave_configure = sym53c8xx_slave_configure,
1686 .slave_destroy = sym53c8xx_slave_destroy,
1687 .eh_abort_handler = sym53c8xx_eh_abort_handler,
1688 .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
1689 .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
1690 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
1691 .this_id = 7,
1692 .use_clustering = ENABLE_CLUSTERING,
1693 .use_sg_chaining = ENABLE_SG_CHAINING,
1694 .max_sectors = 0xFFFF,
1695 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1696 .proc_info = sym53c8xx_proc_info,
1697 .proc_name = NAME53C8XX,
1698 #endif
1701 static int attach_count;
1703 static int __devinit sym2_probe(struct pci_dev *pdev,
1704 const struct pci_device_id *ent)
1706 struct sym_device sym_dev;
1707 struct sym_nvram nvram;
1708 struct Scsi_Host *instance;
1710 memset(&sym_dev, 0, sizeof(sym_dev));
1711 memset(&nvram, 0, sizeof(nvram));
1713 if (pci_enable_device(pdev))
1714 goto leave;
1716 pci_set_master(pdev);
1718 if (pci_request_regions(pdev, NAME53C8XX))
1719 goto disable;
1721 sym_init_device(pdev, &sym_dev);
1722 if (sym_check_supported(&sym_dev))
1723 goto free;
1725 if (sym_check_raid(&sym_dev))
1726 goto leave; /* Don't disable the device */
1728 if (sym_set_workarounds(&sym_dev))
1729 goto free;
1731 sym_config_pqs(pdev, &sym_dev);
1733 sym_get_nvram(&sym_dev, &nvram);
1735 instance = sym_attach(&sym2_template, attach_count, &sym_dev);
1736 if (!instance)
1737 goto free;
1739 if (scsi_add_host(instance, &pdev->dev))
1740 goto detach;
1741 scsi_scan_host(instance);
1743 attach_count++;
1745 return 0;
1747 detach:
1748 sym_detach(pci_get_drvdata(pdev), pdev);
1749 free:
1750 pci_release_regions(pdev);
1751 disable:
1752 pci_disable_device(pdev);
1753 leave:
1754 return -ENODEV;
1757 static void __devexit sym2_remove(struct pci_dev *pdev)
1759 struct sym_hcb *np = pci_get_drvdata(pdev);
1760 struct Scsi_Host *host = np->s.host;
1762 scsi_remove_host(host);
1763 scsi_host_put(host);
1765 sym_detach(np, pdev);
1767 pci_release_regions(pdev);
1768 pci_disable_device(pdev);
1770 attach_count--;
1774 * sym2_io_error_detected() - called when PCI error is detected
1775 * @pdev: pointer to PCI device
1776 * @state: current state of the PCI slot
1778 static pci_ers_result_t sym2_io_error_detected(struct pci_dev *pdev,
1779 enum pci_channel_state state)
1781 /* If slot is permanently frozen, turn everything off */
1782 if (state == pci_channel_io_perm_failure) {
1783 sym2_remove(pdev);
1784 return PCI_ERS_RESULT_DISCONNECT;
1787 disable_irq(pdev->irq);
1788 pci_disable_device(pdev);
1790 /* Request that MMIO be enabled, so register dump can be taken. */
1791 return PCI_ERS_RESULT_CAN_RECOVER;
1795 * sym2_io_slot_dump - Enable MMIO and dump debug registers
1796 * @pdev: pointer to PCI device
1798 static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
1800 struct sym_hcb *np = pci_get_drvdata(pdev);
1802 sym_dump_registers(np);
1804 /* Request a slot reset. */
1805 return PCI_ERS_RESULT_NEED_RESET;
1809 * sym2_reset_workarounds - hardware-specific work-arounds
1811 * This routine is similar to sym_set_workarounds(), except
1812 * that, at this point, we already know that the device was
1813 * succesfully intialized at least once before, and so most
1814 * of the steps taken there are un-needed here.
1816 static void sym2_reset_workarounds(struct pci_dev *pdev)
1818 u_short status_reg;
1819 struct sym_chip *chip;
1821 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
1823 /* Work around for errant bit in 895A, in a fashion
1824 * similar to what is done in sym_set_workarounds().
1826 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1827 if (!(chip->features & FE_66MHZ) && (status_reg & PCI_STATUS_66MHZ)) {
1828 status_reg = PCI_STATUS_66MHZ;
1829 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1830 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1835 * sym2_io_slot_reset() - called when the pci bus has been reset.
1836 * @pdev: pointer to PCI device
1838 * Restart the card from scratch.
1840 static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
1842 struct sym_hcb *np = pci_get_drvdata(pdev);
1844 printk(KERN_INFO "%s: recovering from a PCI slot reset\n",
1845 sym_name(np));
1847 if (pci_enable_device(pdev)) {
1848 printk(KERN_ERR "%s: Unable to enable after PCI reset\n",
1849 sym_name(np));
1850 return PCI_ERS_RESULT_DISCONNECT;
1853 pci_set_master(pdev);
1854 enable_irq(pdev->irq);
1856 /* If the chip can do Memory Write Invalidate, enable it */
1857 if (np->features & FE_WRIE) {
1858 if (pci_set_mwi(pdev))
1859 return PCI_ERS_RESULT_DISCONNECT;
1862 /* Perform work-arounds, analogous to sym_set_workarounds() */
1863 sym2_reset_workarounds(pdev);
1865 /* Perform host reset only on one instance of the card */
1866 if (PCI_FUNC(pdev->devfn) == 0) {
1867 if (sym_reset_scsi_bus(np, 0)) {
1868 printk(KERN_ERR "%s: Unable to reset scsi host\n",
1869 sym_name(np));
1870 return PCI_ERS_RESULT_DISCONNECT;
1872 sym_start_up(np, 1);
1875 return PCI_ERS_RESULT_RECOVERED;
1879 * sym2_io_resume() - resume normal ops after PCI reset
1880 * @pdev: pointer to PCI device
1882 * Called when the error recovery driver tells us that its
1883 * OK to resume normal operation. Use completion to allow
1884 * halted scsi ops to resume.
1886 static void sym2_io_resume(struct pci_dev *pdev)
1888 struct sym_hcb *np = pci_get_drvdata(pdev);
1889 struct Scsi_Host *shost = np->s.host;
1890 struct host_data *hostdata = shost_priv(shost);
1892 spin_lock_irq(shost->host_lock);
1893 if (hostdata->io_reset)
1894 complete_all(hostdata->io_reset);
1895 hostdata->io_reset = NULL;
1896 spin_unlock_irq(shost->host_lock);
1899 static void sym2_get_signalling(struct Scsi_Host *shost)
1901 struct sym_hcb *np = sym_get_hcb(shost);
1902 enum spi_signal_type type;
1904 switch (np->scsi_mode) {
1905 case SMODE_SE:
1906 type = SPI_SIGNAL_SE;
1907 break;
1908 case SMODE_LVD:
1909 type = SPI_SIGNAL_LVD;
1910 break;
1911 case SMODE_HVD:
1912 type = SPI_SIGNAL_HVD;
1913 break;
1914 default:
1915 type = SPI_SIGNAL_UNKNOWN;
1916 break;
1918 spi_signalling(shost) = type;
1921 static void sym2_set_offset(struct scsi_target *starget, int offset)
1923 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1924 struct sym_hcb *np = sym_get_hcb(shost);
1925 struct sym_tcb *tp = &np->target[starget->id];
1927 tp->tgoal.offset = offset;
1928 tp->tgoal.check_nego = 1;
1931 static void sym2_set_period(struct scsi_target *starget, int period)
1933 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1934 struct sym_hcb *np = sym_get_hcb(shost);
1935 struct sym_tcb *tp = &np->target[starget->id];
1937 /* have to have DT for these transfers, but DT will also
1938 * set width, so check that this is allowed */
1939 if (period <= np->minsync && spi_width(starget))
1940 tp->tgoal.dt = 1;
1942 tp->tgoal.period = period;
1943 tp->tgoal.check_nego = 1;
1946 static void sym2_set_width(struct scsi_target *starget, int width)
1948 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1949 struct sym_hcb *np = sym_get_hcb(shost);
1950 struct sym_tcb *tp = &np->target[starget->id];
1952 /* It is illegal to have DT set on narrow transfers. If DT is
1953 * clear, we must also clear IU and QAS. */
1954 if (width == 0)
1955 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1957 tp->tgoal.width = width;
1958 tp->tgoal.check_nego = 1;
1961 static void sym2_set_dt(struct scsi_target *starget, int dt)
1963 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1964 struct sym_hcb *np = sym_get_hcb(shost);
1965 struct sym_tcb *tp = &np->target[starget->id];
1967 /* We must clear QAS and IU if DT is clear */
1968 if (dt)
1969 tp->tgoal.dt = 1;
1970 else
1971 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1972 tp->tgoal.check_nego = 1;
1975 #if 0
1976 static void sym2_set_iu(struct scsi_target *starget, int iu)
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 (iu)
1983 tp->tgoal.iu = tp->tgoal.dt = 1;
1984 else
1985 tp->tgoal.iu = 0;
1986 tp->tgoal.check_nego = 1;
1989 static void sym2_set_qas(struct scsi_target *starget, int qas)
1991 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1992 struct sym_hcb *np = sym_get_hcb(shost);
1993 struct sym_tcb *tp = &np->target[starget->id];
1995 if (qas)
1996 tp->tgoal.dt = tp->tgoal.qas = 1;
1997 else
1998 tp->tgoal.qas = 0;
1999 tp->tgoal.check_nego = 1;
2001 #endif
2003 static struct spi_function_template sym2_transport_functions = {
2004 .set_offset = sym2_set_offset,
2005 .show_offset = 1,
2006 .set_period = sym2_set_period,
2007 .show_period = 1,
2008 .set_width = sym2_set_width,
2009 .show_width = 1,
2010 .set_dt = sym2_set_dt,
2011 .show_dt = 1,
2012 #if 0
2013 .set_iu = sym2_set_iu,
2014 .show_iu = 1,
2015 .set_qas = sym2_set_qas,
2016 .show_qas = 1,
2017 #endif
2018 .get_signalling = sym2_get_signalling,
2021 static struct pci_device_id sym2_id_table[] __devinitdata = {
2022 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
2023 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2024 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
2025 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2026 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
2027 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2028 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
2029 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2030 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
2031 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2032 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
2033 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2034 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
2035 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
2036 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
2037 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2038 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
2039 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2040 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2041 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2042 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2043 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2044 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
2045 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
2046 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2047 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2048 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2049 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2050 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2051 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2052 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2053 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2054 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2055 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2056 { 0, }
2059 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2061 static struct pci_error_handlers sym2_err_handler = {
2062 .error_detected = sym2_io_error_detected,
2063 .mmio_enabled = sym2_io_slot_dump,
2064 .slot_reset = sym2_io_slot_reset,
2065 .resume = sym2_io_resume,
2068 static struct pci_driver sym2_driver = {
2069 .name = NAME53C8XX,
2070 .id_table = sym2_id_table,
2071 .probe = sym2_probe,
2072 .remove = __devexit_p(sym2_remove),
2073 .err_handler = &sym2_err_handler,
2076 static int __init sym2_init(void)
2078 int error;
2080 sym2_setup_params();
2081 sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2082 if (!sym2_transport_template)
2083 return -ENODEV;
2085 error = pci_register_driver(&sym2_driver);
2086 if (error)
2087 spi_release_transport(sym2_transport_template);
2088 return error;
2091 static void __exit sym2_exit(void)
2093 pci_unregister_driver(&sym2_driver);
2094 spi_release_transport(sym2_transport_template);
2097 module_init(sym2_init);
2098 module_exit(sym2_exit);