PCI: Change pci_bus_region addresses to dma_addr_t
[linux-block.git] / drivers / scsi / sym53c8xx_2 / sym_glue.c
CommitLineData
1da177e4
LT
1/*
2 * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
3 * of PCI-SCSI IO processors.
4 *
5 * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
6 * Copyright (c) 2003-2005 Matthew Wilcox <matthew@wil.cx>
7 *
8 * This driver is derived from the Linux sym53c8xx driver.
9 * Copyright (C) 1998-2000 Gerard Roudier
10 *
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.
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
18 *
19 * Other major contributions:
20 *
21 * NVRAM detection and reading.
22 * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
23 *
24 *-----------------------------------------------------------------------------
25 *
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.
30 *
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.
35 *
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
39 */
40#include <linux/ctype.h>
41#include <linux/init.h>
1da177e4
LT
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>
49
50#include "sym_glue.h"
51#include "sym_nvram.h"
52
53#define NAME53C "sym53c"
54#define NAME53C8XX "sym53c8xx"
55
1da177e4
LT
56struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
57unsigned int sym_debug_flags = 0;
58
59static char *excl_string;
60static char *safe_string;
61module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
1da177e4
LT
62module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
63module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
64module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
65module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
66module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
67module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
68module_param_named(verb, sym_driver_setup.verbose, byte, 0);
69module_param_named(debug, sym_debug_flags, uint, 0);
70module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
71module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
72module_param_named(excl, excl_string, charp, 0);
73module_param_named(safe, safe_string, charp, 0);
74
75MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
1da177e4
LT
76MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
77MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
78MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
79MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
80MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
81MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
82MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
83MODULE_PARM_DESC(debug, "Set bits to enable debugging");
84MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
85MODULE_PARM_DESC(nvram, "Option currently not used");
86MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
87MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
88
89MODULE_LICENSE("GPL");
90MODULE_VERSION(SYM_VERSION);
91MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
92MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
93
94static void sym2_setup_params(void)
95{
96 char *p = excl_string;
97 int xi = 0;
98
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;
104 }
105
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);
121 }
122 }
123}
124
1da177e4
LT
125static struct scsi_transport_template *sym2_transport_template = NULL;
126
1da177e4
LT
127/*
128 * Driver private area in the SCSI command structure.
129 */
130struct sym_ucmd { /* Override the SCSI pointer structure */
d68cd759 131 struct completion *eh_done; /* SCSI error handling */
1da177e4
LT
132};
133
134#define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
135#define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
136
1da177e4
LT
137/*
138 * Complete a pending CAM CCB.
139 */
140void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
141{
2ba65367
MW
142 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
143 BUILD_BUG_ON(sizeof(struct scsi_pointer) < sizeof(struct sym_ucmd));
144
145 if (ucmd->eh_done)
146 complete(ucmd->eh_done);
147
39c05d1e 148 scsi_dma_unmap(cmd);
1da177e4
LT
149 cmd->scsi_done(cmd);
150}
151
1da177e4
LT
152/*
153 * Tell the SCSI layer about a BUS RESET.
154 */
155void sym_xpt_async_bus_reset(struct sym_hcb *np)
156{
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);
163}
164
1da177e4
LT
165/*
166 * Choose the more appropriate CAM status if
167 * the IO encountered an extended error.
168 */
169static int sym_xerr_cam_status(int cam_status, int x_status)
170{
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;
180 }
181 return cam_status;
182}
183
184/*
185 * Build CAM result for a failed or auto-sensed IO.
186 */
187void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
188{
189 struct scsi_cmnd *cmd = cp->cmd;
190 u_int cam_status, scsi_status, drv_status;
191
192 drv_status = 0;
193 cam_status = DID_OK;
194 scsi_status = cp->ssss_status;
195
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;
207 /*
208 * Bounce back the sense data to user.
209 */
de15c201 210 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1da177e4 211 memcpy(cmd->sense_buffer, cp->sns_bbuf,
b80ca4f7 212 min(SCSI_SENSE_BUFFERSIZE, SYM_SNS_BBUF_LEN));
1da177e4
LT
213#if 0
214 /*
215 * If the device reports a UNIT ATTENTION condition
216 * due to a RESET condition, we should consider all
217 * disconnect CCBs for this unit as aborted.
218 */
219 if (1) {
220 u_char *p;
221 p = (u_char *) cmd->sense_data;
222 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
223 sym_clear_tasks(np, DID_ABORT,
224 cp->target,cp->lun, -1);
225 }
226#endif
227 } else {
228 /*
229 * Error return from our internal request sense. This
230 * is bad: we must clear the contingent allegiance
231 * condition otherwise the device will always return
232 * BUSY. Use a big stick.
233 */
234 sym_reset_scsi_target(np, cmd->device->id);
235 cam_status = DID_ERROR;
236 }
237 } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
238 cam_status = DID_OK;
239 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
240 cam_status = DID_NO_CONNECT;
241 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
242 cam_status = DID_ERROR;
243 else { /* Extended error */
244 if (sym_verbose) {
245 sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
246 cp->host_status, cp->ssss_status,
247 cp->xerr_status);
248 }
249 /*
250 * Set the most appropriate value for CAM status.
251 */
252 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
253 }
938febd6 254 scsi_set_resid(cmd, resid);
1da177e4
LT
255 cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
256}
257
1da177e4
LT
258static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
259{
260 int segment;
938febd6 261 int use_sg;
1da177e4
LT
262
263 cp->data_len = 0;
264
39c05d1e 265 use_sg = scsi_dma_map(cmd);
938febd6
FT
266 if (use_sg > 0) {
267 struct scatterlist *sg;
53222b90 268 struct sym_tcb *tp = &np->target[cp->target];
1da177e4
LT
269 struct sym_tblmove *data;
270
271 if (use_sg > SYM_CONF_MAX_SG) {
39c05d1e 272 scsi_dma_unmap(cmd);
1da177e4
LT
273 return -1;
274 }
275
276 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
277
938febd6
FT
278 scsi_for_each_sg(cmd, sg, use_sg, segment) {
279 dma_addr_t baddr = sg_dma_address(sg);
280 unsigned int len = sg_dma_len(sg);
1da177e4 281
53222b90
MW
282 if ((len & 1) && (tp->head.wval & EWS)) {
283 len++;
284 cp->odd_byte_adjustment++;
285 }
286
1da177e4
LT
287 sym_build_sge(np, &data[segment], baddr, len);
288 cp->data_len += len;
289 }
290 } else {
291 segment = -2;
292 }
293
294 return segment;
295}
296
297/*
298 * Queue a SCSI command.
299 */
300static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
301{
302 struct scsi_device *sdev = cmd->device;
303 struct sym_tcb *tp;
304 struct sym_lcb *lp;
305 struct sym_ccb *cp;
306 int order;
307
1da177e4
LT
308 /*
309 * Retrieve the target descriptor.
310 */
311 tp = &np->target[sdev->id];
312
1da177e4
LT
313 /*
314 * Select tagged/untagged.
315 */
316 lp = sym_lp(tp, sdev->lun);
317 order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
318
319 /*
320 * Queue the SCSI IO.
321 */
322 cp = sym_get_ccb(np, cmd, order);
323 if (!cp)
324 return 1; /* Means resource shortage */
325 sym_queue_scsiio(np, cmd, cp);
326 return 0;
327}
328
329/*
330 * Setup buffers and pointers that address the CDB.
331 */
332static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
333{
1da177e4 334 memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
1da177e4 335
53222b90
MW
336 cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
337 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
1da177e4
LT
338
339 return 0;
340}
341
342/*
343 * Setup pointers that address the data and start the I/O.
344 */
345int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
346{
44f30b0f
MW
347 u32 lastp, goalp;
348 int dir;
1da177e4
LT
349
350 /*
351 * Build the CDB.
352 */
353 if (sym_setup_cdb(np, cmd, cp))
354 goto out_abort;
355
356 /*
357 * No direction means no data.
358 */
359 dir = cmd->sc_data_direction;
360 if (dir != DMA_NONE) {
361 cp->segments = sym_scatter(np, cp, cmd);
362 if (cp->segments < 0) {
53222b90 363 sym_set_cam_status(cmd, DID_ERROR);
1da177e4
LT
364 goto out_abort;
365 }
44f30b0f
MW
366
367 /*
368 * No segments means no data.
369 */
370 if (!cp->segments)
371 dir = DMA_NONE;
1da177e4
LT
372 } else {
373 cp->data_len = 0;
374 cp->segments = 0;
375 }
376
377 /*
44f30b0f
MW
378 * Set the data pointer.
379 */
380 switch (dir) {
381 case DMA_BIDIRECTIONAL:
3fb364e0 382 scmd_printk(KERN_INFO, cmd, "got DMA_BIDIRECTIONAL command");
44f30b0f
MW
383 sym_set_cam_status(cmd, DID_ERROR);
384 goto out_abort;
385 case DMA_TO_DEVICE:
386 goalp = SCRIPTA_BA(np, data_out2) + 8;
387 lastp = goalp - 8 - (cp->segments * (2*4));
388 break;
389 case DMA_FROM_DEVICE:
390 cp->host_flags |= HF_DATA_IN;
391 goalp = SCRIPTA_BA(np, data_in2) + 8;
392 lastp = goalp - 8 - (cp->segments * (2*4));
393 break;
394 case DMA_NONE:
395 default:
396 lastp = goalp = SCRIPTB_BA(np, no_data);
397 break;
398 }
399
400 /*
401 * Set all pointers values needed by SCRIPTS.
1da177e4 402 */
44f30b0f
MW
403 cp->phys.head.lastp = cpu_to_scr(lastp);
404 cp->phys.head.savep = cpu_to_scr(lastp);
405 cp->startp = cp->phys.head.savep;
406 cp->goalp = cpu_to_scr(goalp);
1da177e4
LT
407
408 /*
409 * When `#ifed 1', the code below makes the driver
410 * panic on the first attempt to write to a SCSI device.
411 * It is the first test we want to do after a driver
412 * change that does not seem obviously safe. :)
413 */
414#if 0
415 switch (cp->cdb_buf[0]) {
416 case 0x0A: case 0x2A: case 0xAA:
417 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
418 break;
419 default:
420 break;
421 }
422#endif
423
424 /*
425 * activate this job.
426 */
3bea15a7 427 sym_put_start_queue(np, cp);
1da177e4
LT
428 return 0;
429
430out_abort:
431 sym_free_ccb(np, cp);
432 sym_xpt_done(np, cmd);
433 return 0;
434}
435
436
437/*
438 * timer daemon.
439 *
440 * Misused to keep the driver running when
441 * interrupts are not configured correctly.
442 */
443static void sym_timer(struct sym_hcb *np)
444{
445 unsigned long thistime = jiffies;
446
447 /*
448 * Restart the timer.
449 */
450 np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
451 add_timer(&np->s.timer);
452
453 /*
454 * If we are resetting the ncr, wait for settle_time before
455 * clearing it. Then command processing will be resumed.
456 */
457 if (np->s.settle_time_valid) {
458 if (time_before_eq(np->s.settle_time, thistime)) {
459 if (sym_verbose >= 2 )
460 printk("%s: command processing resumed\n",
461 sym_name(np));
462 np->s.settle_time_valid = 0;
463 }
464 return;
465 }
466
467 /*
468 * Nothing to do for now, but that may come.
469 */
470 if (np->s.lasttime + 4*HZ < thistime) {
471 np->s.lasttime = thistime;
472 }
473
474#ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
475 /*
476 * Some way-broken PCI bridges may lead to
477 * completions being lost when the clearing
478 * of the INTFLY flag by the CPU occurs
479 * concurrently with the chip raising this flag.
480 * If this ever happen, lost completions will
481 * be reaped here.
482 */
483 sym_wakeup_done(np);
484#endif
485}
486
487
488/*
489 * PCI BUS error handler.
490 */
5111eefa 491void sym_log_bus_error(struct Scsi_Host *shost)
1da177e4 492{
5111eefa
MW
493 struct sym_data *sym_data = shost_priv(shost);
494 struct pci_dev *pdev = sym_data->pdev;
495 unsigned short pci_sts;
496 pci_read_config_word(pdev, PCI_STATUS, &pci_sts);
1da177e4 497 if (pci_sts & 0xf900) {
5111eefa
MW
498 pci_write_config_word(pdev, PCI_STATUS, pci_sts);
499 shost_printk(KERN_WARNING, shost,
500 "PCI bus error: status = 0x%04x\n", pci_sts & 0xf900);
1da177e4
LT
501 }
502}
503
504/*
505 * queuecommand method. Entered with the host adapter lock held and
506 * interrupts disabled.
507 */
f281233d 508static int sym53c8xx_queue_command_lck(struct scsi_cmnd *cmd,
1da177e4
LT
509 void (*done)(struct scsi_cmnd *))
510{
511 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
512 struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
513 int sts = 0;
514
71c222dc 515 cmd->scsi_done = done;
1da177e4
LT
516 memset(ucp, 0, sizeof(*ucp));
517
518 /*
519 * Shorten our settle_time if needed for
520 * this command not to time out.
521 */
242f9dcb
JA
522 if (np->s.settle_time_valid && cmd->request->timeout) {
523 unsigned long tlimit = jiffies + cmd->request->timeout;
1da177e4
LT
524 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
525 if (time_after(np->s.settle_time, tlimit)) {
526 np->s.settle_time = tlimit;
527 }
528 }
529
530 if (np->s.settle_time_valid)
531 return SCSI_MLQUEUE_HOST_BUSY;
532
533 sts = sym_queue_command(np, cmd);
534 if (sts)
535 return SCSI_MLQUEUE_HOST_BUSY;
536 return 0;
537}
538
f281233d
JG
539static DEF_SCSI_QCMD(sym53c8xx_queue_command)
540
1da177e4
LT
541/*
542 * Linux entry point of the interrupt handler.
543 */
7d12e780 544static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
1da177e4 545{
99c9e0a1
MW
546 struct Scsi_Host *shost = dev_id;
547 struct sym_data *sym_data = shost_priv(shost);
548 irqreturn_t result;
1da177e4 549
d68cd759 550 /* Avoid spinloop trying to handle interrupts on frozen device */
99c9e0a1 551 if (pci_channel_offline(sym_data->pdev))
d68cd759
LV
552 return IRQ_NONE;
553
1da177e4
LT
554 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
555
99c9e0a1
MW
556 spin_lock(shost->host_lock);
557 result = sym_interrupt(shost);
558 spin_unlock(shost->host_lock);
1da177e4
LT
559
560 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
561
99c9e0a1 562 return result;
1da177e4
LT
563}
564
565/*
566 * Linux entry point of the timer handler
567 */
568static void sym53c8xx_timer(unsigned long npref)
569{
570 struct sym_hcb *np = (struct sym_hcb *)npref;
571 unsigned long flags;
572
573 spin_lock_irqsave(np->s.host->host_lock, flags);
574 sym_timer(np);
575 spin_unlock_irqrestore(np->s.host->host_lock, flags);
576}
577
578
579/*
580 * What the eh thread wants us to perform.
581 */
582#define SYM_EH_ABORT 0
583#define SYM_EH_DEVICE_RESET 1
584#define SYM_EH_BUS_RESET 2
585#define SYM_EH_HOST_RESET 3
586
1da177e4
LT
587/*
588 * Generic method for our eh processing.
589 * The 'op' argument tells what we have to do.
590 */
591static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
592{
b4e93a73 593 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
5111eefa
MW
594 struct Scsi_Host *shost = cmd->device->host;
595 struct sym_data *sym_data = shost_priv(shost);
596 struct pci_dev *pdev = sym_data->pdev;
597 struct sym_hcb *np = sym_data->ncb;
1da177e4 598 SYM_QUEHEAD *qp;
2ba65367 599 int cmd_queued = 0;
1da177e4 600 int sts = -1;
d637c454 601 struct completion eh_done;
1da177e4 602
5111eefa 603 scmd_printk(KERN_WARNING, cmd, "%s operation started\n", opname);
1da177e4 604
d68cd759
LV
605 /* We may be in an error condition because the PCI bus
606 * went down. In this case, we need to wait until the
607 * PCI bus is reset, the card is reset, and only then
608 * proceed with the scsi error recovery. There's no
609 * point in hurrying; take a leisurely wait.
610 */
611#define WAIT_FOR_PCI_RECOVERY 35
612 if (pci_channel_offline(pdev)) {
d68cd759
LV
613 int finished_reset = 0;
614 init_completion(&eh_done);
5111eefa 615 spin_lock_irq(shost->host_lock);
d68cd759
LV
616 /* Make sure we didn't race */
617 if (pci_channel_offline(pdev)) {
d9aa3af0
KH
618 BUG_ON(sym_data->io_reset);
619 sym_data->io_reset = &eh_done;
d68cd759 620 } else {
d68cd759 621 finished_reset = 1;
99c9e0a1 622 }
5111eefa 623 spin_unlock_irq(shost->host_lock);
d68cd759 624 if (!finished_reset)
d9aa3af0
KH
625 finished_reset = wait_for_completion_timeout
626 (sym_data->io_reset,
d68cd759 627 WAIT_FOR_PCI_RECOVERY*HZ);
d9aa3af0
KH
628 spin_lock_irq(shost->host_lock);
629 sym_data->io_reset = NULL;
630 spin_unlock_irq(shost->host_lock);
d68cd759
LV
631 if (!finished_reset)
632 return SCSI_FAILED;
633 }
634
5111eefa 635 spin_lock_irq(shost->host_lock);
1da177e4
LT
636 /* This one is queued in some place -> to wait for completion */
637 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
638 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
639 if (cp->cmd == cmd) {
2ba65367 640 cmd_queued = 1;
ab19d52b 641 break;
1da177e4
LT
642 }
643 }
644
1da177e4
LT
645 /* Try to proceed the operation we have been asked for */
646 sts = -1;
647 switch(op) {
648 case SYM_EH_ABORT:
649 sts = sym_abort_scsiio(np, cmd, 1);
650 break;
651 case SYM_EH_DEVICE_RESET:
652 sts = sym_reset_scsi_target(np, cmd->device->id);
653 break;
654 case SYM_EH_BUS_RESET:
655 sym_reset_scsi_bus(np, 1);
656 sts = 0;
657 break;
658 case SYM_EH_HOST_RESET:
659 sym_reset_scsi_bus(np, 0);
5111eefa 660 sym_start_up(shost, 1);
1da177e4
LT
661 sts = 0;
662 break;
663 default:
664 break;
665 }
666
667 /* On error, restore everything and cross fingers :) */
2ba65367
MW
668 if (sts)
669 cmd_queued = 0;
1da177e4 670
2ba65367
MW
671 if (cmd_queued) {
672 init_completion(&eh_done);
673 ucmd->eh_done = &eh_done;
5111eefa 674 spin_unlock_irq(shost->host_lock);
d637c454 675 if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
2ba65367 676 ucmd->eh_done = NULL;
1da177e4 677 sts = -2;
b4e93a73 678 }
2ba65367 679 } else {
5111eefa 680 spin_unlock_irq(shost->host_lock);
1da177e4 681 }
2ba65367 682
1da177e4
LT
683 dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
684 sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
685 return sts ? SCSI_FAILED : SCSI_SUCCESS;
686}
687
688
689/*
690 * Error handlers called from the eh thread (one thread per HBA).
691 */
692static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
693{
ab19d52b 694 return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
1da177e4
LT
695}
696
697static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
698{
ab19d52b 699 return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
1da177e4
LT
700}
701
702static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
703{
ab19d52b 704 return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
1da177e4
LT
705}
706
707static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
708{
ab19d52b 709 return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
1da177e4
LT
710}
711
712/*
713 * Tune device queuing depth, according to various limits.
714 */
715static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
716{
717 struct sym_lcb *lp = sym_lp(tp, lun);
718 u_short oldtags;
719
720 if (!lp)
721 return;
722
723 oldtags = lp->s.reqtags;
724
725 if (reqtags > lp->s.scdev_depth)
726 reqtags = lp->s.scdev_depth;
727
1da177e4
LT
728 lp->s.reqtags = reqtags;
729
730 if (reqtags != oldtags) {
53222b90 731 dev_info(&tp->starget->dev,
1da177e4 732 "tagged command queuing %s, command queue depth %d.\n",
3bea15a7 733 lp->s.reqtags ? "enabled" : "disabled", reqtags);
1da177e4
LT
734 }
735}
736
53222b90 737static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
1da177e4 738{
84e203a2
MW
739 struct sym_hcb *np = sym_get_hcb(sdev->host);
740 struct sym_tcb *tp = &np->target[sdev->id];
741 struct sym_lcb *lp;
fa858456
AK
742 unsigned long flags;
743 int error;
1da177e4 744
53222b90
MW
745 if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
746 return -ENXIO;
1da177e4 747
fa858456
AK
748 spin_lock_irqsave(np->s.host->host_lock, flags);
749
53222b90
MW
750 /*
751 * Fail the device init if the device is flagged NOSCAN at BOOT in
752 * the NVRAM. This may speed up boot and maintain coherency with
753 * BIOS device numbering. Clearing the flag allows the user to
754 * rescan skipped devices later. We also return an error for
755 * devices not flagged for SCAN LUNS in the NVRAM since some single
756 * lun devices behave badly when asked for a non zero LUN.
757 */
758
66e8d1cc 759 if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
53222b90 760 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
fa858456 761 starget_printk(KERN_INFO, sdev->sdev_target,
66e8d1cc 762 "Scan at boot disabled in NVRAM\n");
fa858456
AK
763 error = -ENXIO;
764 goto out;
53222b90
MW
765 }
766
66e8d1cc 767 if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
fa858456
AK
768 if (sdev->lun != 0) {
769 error = -ENXIO;
770 goto out;
771 }
772 starget_printk(KERN_INFO, sdev->sdev_target,
66e8d1cc
MW
773 "Multiple LUNs disabled in NVRAM\n");
774 }
775
84e203a2 776 lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
fa858456
AK
777 if (!lp) {
778 error = -ENOMEM;
779 goto out;
780 }
781 if (tp->nlcb == 1)
782 tp->starget = sdev->sdev_target;
84e203a2 783
b37df489
MW
784 spi_min_period(tp->starget) = tp->usr_period;
785 spi_max_width(tp->starget) = tp->usr_width;
786
fa858456
AK
787 error = 0;
788out:
789 spin_unlock_irqrestore(np->s.host->host_lock, flags);
790
791 return error;
1da177e4
LT
792}
793
794/*
795 * Linux entry point for device queue sizing.
796 */
84e203a2 797static int sym53c8xx_slave_configure(struct scsi_device *sdev)
1da177e4 798{
84e203a2
MW
799 struct sym_hcb *np = sym_get_hcb(sdev->host);
800 struct sym_tcb *tp = &np->target[sdev->id];
801 struct sym_lcb *lp = sym_lp(tp, sdev->lun);
1da177e4
LT
802 int reqtags, depth_to_use;
803
1da177e4
LT
804 /*
805 * Get user flags.
806 */
807 lp->curr_flags = lp->user_flags;
808
809 /*
810 * Select queue depth from driver setup.
c2fd206e
TB
811 * Do not use more than configured by user.
812 * Use at least 1.
813 * Do not use more than our maximum.
1da177e4 814 */
a44131b3 815 reqtags = sym_driver_setup.max_tag;
1da177e4
LT
816 if (reqtags > tp->usrtags)
817 reqtags = tp->usrtags;
84e203a2 818 if (!sdev->tagged_supported)
1da177e4 819 reqtags = 0;
1da177e4
LT
820 if (reqtags > SYM_CONF_MAX_TAG)
821 reqtags = SYM_CONF_MAX_TAG;
c2fd206e 822 depth_to_use = reqtags ? reqtags : 1;
84e203a2 823 scsi_adjust_queue_depth(sdev,
a44131b3 824 sdev->tagged_supported ? MSG_SIMPLE_TAG : 0,
1da177e4
LT
825 depth_to_use);
826 lp->s.scdev_depth = depth_to_use;
84e203a2 827 sym_tune_dev_queuing(tp, sdev->lun, reqtags);
1da177e4 828
84e203a2
MW
829 if (!spi_initial_dv(sdev->sdev_target))
830 spi_dv_device(sdev);
1da177e4
LT
831
832 return 0;
833}
834
84e203a2
MW
835static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
836{
837 struct sym_hcb *np = sym_get_hcb(sdev->host);
fa858456
AK
838 struct sym_tcb *tp = &np->target[sdev->id];
839 struct sym_lcb *lp = sym_lp(tp, sdev->lun);
840 unsigned long flags;
84e203a2 841
cced5041
SP
842 /* if slave_alloc returned before allocating a sym_lcb, return */
843 if (!lp)
844 return;
845
fa858456
AK
846 spin_lock_irqsave(np->s.host->host_lock, flags);
847
848 if (lp->busy_itlq || lp->busy_itl) {
849 /*
850 * This really shouldn't happen, but we can't return an error
851 * so let's try to stop all on-going I/O.
852 */
853 starget_printk(KERN_WARNING, tp->starget,
854 "Removing busy LCB (%d)\n", sdev->lun);
855 sym_reset_scsi_bus(np, 1);
856 }
857
858 if (sym_free_lcb(np, sdev->id, sdev->lun) == 0) {
859 /*
860 * It was the last unit for this target.
861 */
862 tp->head.sval = 0;
863 tp->head.wval = np->rv_scntl3;
864 tp->head.uval = 0;
865 tp->tgoal.check_nego = 1;
866 tp->starget = NULL;
867 }
868
869 spin_unlock_irqrestore(np->s.host->host_lock, flags);
84e203a2
MW
870}
871
1da177e4
LT
872/*
873 * Linux entry point for info() function
874 */
875static const char *sym53c8xx_info (struct Scsi_Host *host)
876{
877 return SYM_DRIVER_NAME;
878}
879
880
881#ifdef SYM_LINUX_PROC_INFO_SUPPORT
882/*
883 * Proc file system stuff
884 *
885 * A read operation returns adapter information.
886 * A write operation is a control command.
887 * The string is parsed in the driver code and the command is passed
888 * to the sym_usercmd() function.
889 */
890
891#ifdef SYM_LINUX_USER_COMMAND_SUPPORT
892
893struct sym_usrcmd {
894 u_long target;
895 u_long lun;
896 u_long data;
897 u_long cmd;
898};
899
900#define UC_SETSYNC 10
901#define UC_SETTAGS 11
902#define UC_SETDEBUG 12
903#define UC_SETWIDE 14
904#define UC_SETFLAG 15
905#define UC_SETVERBOSE 17
906#define UC_RESETDEV 18
907#define UC_CLEARDEV 19
908
909static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
910{
911 struct sym_tcb *tp;
912 int t, l;
913
914 switch (uc->cmd) {
915 case 0: return;
916
917#ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
918 case UC_SETDEBUG:
919 sym_debug_flags = uc->data;
920 break;
921#endif
922 case UC_SETVERBOSE:
923 np->verbose = uc->data;
924 break;
925 default:
926 /*
927 * We assume that other commands apply to targets.
928 * This should always be the case and avoid the below
929 * 4 lines to be repeated 6 times.
930 */
931 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
932 if (!((uc->target >> t) & 1))
933 continue;
934 tp = &np->target[t];
fa858456
AK
935 if (!tp->nlcb)
936 continue;
1da177e4
LT
937
938 switch (uc->cmd) {
939
940 case UC_SETSYNC:
941 if (!uc->data || uc->data >= 255) {
942 tp->tgoal.iu = tp->tgoal.dt =
943 tp->tgoal.qas = 0;
944 tp->tgoal.offset = 0;
945 } else if (uc->data <= 9 && np->minsync_dt) {
946 if (uc->data < np->minsync_dt)
947 uc->data = np->minsync_dt;
948 tp->tgoal.iu = tp->tgoal.dt =
949 tp->tgoal.qas = 1;
950 tp->tgoal.width = 1;
951 tp->tgoal.period = uc->data;
952 tp->tgoal.offset = np->maxoffs_dt;
953 } else {
954 if (uc->data < np->minsync)
955 uc->data = np->minsync;
956 tp->tgoal.iu = tp->tgoal.dt =
957 tp->tgoal.qas = 0;
958 tp->tgoal.period = uc->data;
959 tp->tgoal.offset = np->maxoffs;
960 }
961 tp->tgoal.check_nego = 1;
962 break;
963 case UC_SETWIDE:
964 tp->tgoal.width = uc->data ? 1 : 0;
965 tp->tgoal.check_nego = 1;
966 break;
967 case UC_SETTAGS:
968 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
969 sym_tune_dev_queuing(tp, l, uc->data);
970 break;
971 case UC_RESETDEV:
972 tp->to_reset = 1;
973 np->istat_sem = SEM;
974 OUTB(np, nc_istat, SIGP|SEM);
975 break;
976 case UC_CLEARDEV:
977 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
978 struct sym_lcb *lp = sym_lp(tp, l);
979 if (lp) lp->to_clear = 1;
980 }
981 np->istat_sem = SEM;
982 OUTB(np, nc_istat, SIGP|SEM);
983 break;
984 case UC_SETFLAG:
985 tp->usrflags = uc->data;
986 break;
987 }
988 }
989 break;
990 }
991}
992
4e62b093 993static int sym_skip_spaces(char *ptr, int len)
1da177e4
LT
994{
995 int cnt, c;
996
997 for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
998
999 return (len - cnt);
1000}
1001
1002static int get_int_arg(char *ptr, int len, u_long *pv)
1003{
1004 char *end;
1005
1006 *pv = simple_strtoul(ptr, &end, 10);
1007 return (end - ptr);
1008}
1009
1010static int is_keyword(char *ptr, int len, char *verb)
1011{
1012 int verb_len = strlen(verb);
1013
1014 if (len >= verb_len && !memcmp(verb, ptr, verb_len))
1015 return verb_len;
1016 else
1017 return 0;
1018}
1019
1020#define SKIP_SPACES(ptr, len) \
4e62b093 1021 if ((arg_len = sym_skip_spaces(ptr, len)) < 1) \
1da177e4
LT
1022 return -EINVAL; \
1023 ptr += arg_len; len -= arg_len;
1024
1025#define GET_INT_ARG(ptr, len, v) \
1026 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
1027 return -EINVAL; \
1028 ptr += arg_len; len -= arg_len;
1029
1030
1031/*
1032 * Parse a control command
1033 */
1034
5111eefa 1035static int sym_user_command(struct Scsi_Host *shost, char *buffer, int length)
1da177e4 1036{
5111eefa 1037 struct sym_hcb *np = sym_get_hcb(shost);
1da177e4
LT
1038 char *ptr = buffer;
1039 int len = length;
1040 struct sym_usrcmd cmd, *uc = &cmd;
1041 int arg_len;
1042 u_long target;
1043
1044 memset(uc, 0, sizeof(*uc));
1045
1046 if (len > 0 && ptr[len-1] == '\n')
1047 --len;
1048
1049 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1050 uc->cmd = UC_SETSYNC;
1051 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1052 uc->cmd = UC_SETTAGS;
1053 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1054 uc->cmd = UC_SETVERBOSE;
1055 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1056 uc->cmd = UC_SETWIDE;
1057#ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1058 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1059 uc->cmd = UC_SETDEBUG;
1060#endif
1061 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1062 uc->cmd = UC_SETFLAG;
1063 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1064 uc->cmd = UC_RESETDEV;
1065 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1066 uc->cmd = UC_CLEARDEV;
1067 else
1068 arg_len = 0;
1069
1070#ifdef DEBUG_PROC_INFO
1071printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1072#endif
1073
1074 if (!arg_len)
1075 return -EINVAL;
1076 ptr += arg_len; len -= arg_len;
1077
1078 switch(uc->cmd) {
1079 case UC_SETSYNC:
1080 case UC_SETTAGS:
1081 case UC_SETWIDE:
1082 case UC_SETFLAG:
1083 case UC_RESETDEV:
1084 case UC_CLEARDEV:
1085 SKIP_SPACES(ptr, len);
1086 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1087 ptr += arg_len; len -= arg_len;
1088 uc->target = ~0;
1089 } else {
1090 GET_INT_ARG(ptr, len, target);
1091 uc->target = (1<<target);
1092#ifdef DEBUG_PROC_INFO
1093printk("sym_user_command: target=%ld\n", target);
1094#endif
1095 }
1096 break;
1097 }
1098
1099 switch(uc->cmd) {
1100 case UC_SETVERBOSE:
1101 case UC_SETSYNC:
1102 case UC_SETTAGS:
1103 case UC_SETWIDE:
1104 SKIP_SPACES(ptr, len);
1105 GET_INT_ARG(ptr, len, uc->data);
1106#ifdef DEBUG_PROC_INFO
1107printk("sym_user_command: data=%ld\n", uc->data);
1108#endif
1109 break;
1110#ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1111 case UC_SETDEBUG:
1112 while (len > 0) {
1113 SKIP_SPACES(ptr, len);
1114 if ((arg_len = is_keyword(ptr, len, "alloc")))
1115 uc->data |= DEBUG_ALLOC;
1116 else if ((arg_len = is_keyword(ptr, len, "phase")))
1117 uc->data |= DEBUG_PHASE;
1118 else if ((arg_len = is_keyword(ptr, len, "queue")))
1119 uc->data |= DEBUG_QUEUE;
1120 else if ((arg_len = is_keyword(ptr, len, "result")))
1121 uc->data |= DEBUG_RESULT;
1122 else if ((arg_len = is_keyword(ptr, len, "scatter")))
1123 uc->data |= DEBUG_SCATTER;
1124 else if ((arg_len = is_keyword(ptr, len, "script")))
1125 uc->data |= DEBUG_SCRIPT;
1126 else if ((arg_len = is_keyword(ptr, len, "tiny")))
1127 uc->data |= DEBUG_TINY;
1128 else if ((arg_len = is_keyword(ptr, len, "timing")))
1129 uc->data |= DEBUG_TIMING;
1130 else if ((arg_len = is_keyword(ptr, len, "nego")))
1131 uc->data |= DEBUG_NEGO;
1132 else if ((arg_len = is_keyword(ptr, len, "tags")))
1133 uc->data |= DEBUG_TAGS;
1134 else if ((arg_len = is_keyword(ptr, len, "pointer")))
1135 uc->data |= DEBUG_POINTER;
1136 else
1137 return -EINVAL;
1138 ptr += arg_len; len -= arg_len;
1139 }
1140#ifdef DEBUG_PROC_INFO
1141printk("sym_user_command: data=%ld\n", uc->data);
1142#endif
1143 break;
1144#endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1145 case UC_SETFLAG:
1146 while (len > 0) {
1147 SKIP_SPACES(ptr, len);
1148 if ((arg_len = is_keyword(ptr, len, "no_disc")))
1149 uc->data &= ~SYM_DISC_ENABLED;
1150 else
1151 return -EINVAL;
1152 ptr += arg_len; len -= arg_len;
1153 }
1154 break;
1155 default:
1156 break;
1157 }
1158
1159 if (len)
1160 return -EINVAL;
1161 else {
1162 unsigned long flags;
1163
5111eefa
MW
1164 spin_lock_irqsave(shost->host_lock, flags);
1165 sym_exec_user_command(np, uc);
1166 spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4
LT
1167 }
1168 return length;
1169}
1170
1171#endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1172
1173
1da177e4
LT
1174/*
1175 * Copy formatted information into the input buffer.
1176 */
ee127fec 1177static int sym_show_info(struct seq_file *m, struct Scsi_Host *shost)
1da177e4 1178{
ee127fec 1179#ifdef SYM_LINUX_USER_INFO_SUPPORT
5111eefa
MW
1180 struct sym_data *sym_data = shost_priv(shost);
1181 struct pci_dev *pdev = sym_data->pdev;
1182 struct sym_hcb *np = sym_data->ncb;
1da177e4 1183
ee127fec
AV
1184 seq_printf(m, "Chip " NAME53C "%s, device id 0x%x, "
1185 "revision id 0x%x\n", np->s.chip_name,
1186 pdev->device, pdev->revision);
1187 seq_printf(m, "At PCI address %s, IRQ %u\n",
5111eefa 1188 pci_name(pdev), pdev->irq);
ee127fec
AV
1189 seq_printf(m, "Min. period factor %d, %s SCSI BUS%s\n",
1190 (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1191 np->maxwide ? "Wide" : "Narrow",
1192 np->minsync_dt ? ", DT capable" : "");
1da177e4 1193
ee127fec
AV
1194 seq_printf(m, "Max. started commands %d, "
1195 "max. commands per LUN %d\n",
1196 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1da177e4 1197
ee127fec 1198 return 0;
1da177e4 1199#else
ee127fec
AV
1200 return -EINVAL;
1201#endif /* SYM_LINUX_USER_INFO_SUPPORT */
1da177e4 1202}
ee127fec 1203
1da177e4
LT
1204#endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1205
a71d035d 1206/*
783fa731 1207 * Free resources claimed by sym_iomap_device(). Note that
a71d035d
TB
1208 * sym_free_resources() should be used instead of this function after calling
1209 * sym_attach().
1210 */
6f039790 1211static void sym_iounmap_device(struct sym_device *device)
a71d035d
TB
1212{
1213 if (device->s.ioaddr)
1214 pci_iounmap(device->pdev, device->s.ioaddr);
1215 if (device->s.ramaddr)
1216 pci_iounmap(device->pdev, device->s.ramaddr);
1217}
1218
1da177e4
LT
1219/*
1220 * Free controller resources.
1221 */
b409063a
TB
1222static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev,
1223 int do_free_irq)
1da177e4
LT
1224{
1225 /*
1226 * Free O/S specific resources.
1227 */
b409063a 1228 if (do_free_irq)
7ee2413c 1229 free_irq(pdev->irq, np->s.host);
1da177e4
LT
1230 if (np->s.ioaddr)
1231 pci_iounmap(pdev, np->s.ioaddr);
1232 if (np->s.ramaddr)
1233 pci_iounmap(pdev, np->s.ramaddr);
1234 /*
1235 * Free O/S independent resources.
1236 */
1237 sym_hcb_free(np);
1238
1239 sym_mfree_dma(np, sizeof(*np), "HCB");
1240}
1241
1da177e4
LT
1242/*
1243 * Host attach and initialisations.
1244 *
1245 * Allocate host data and ncb structure.
1246 * Remap MMIO region.
1247 * Do chip initialization.
1248 * If all is OK, install interrupt handling and
1249 * start the timer daemon.
1250 */
6f039790
GKH
1251static struct Scsi_Host *sym_attach(struct scsi_host_template *tpnt, int unit,
1252 struct sym_device *dev)
1da177e4 1253{
99c9e0a1 1254 struct sym_data *sym_data;
1da177e4 1255 struct sym_hcb *np = NULL;
a71d035d 1256 struct Scsi_Host *shost = NULL;
1da177e4
LT
1257 struct pci_dev *pdev = dev->pdev;
1258 unsigned long flags;
1259 struct sym_fw *fw;
b409063a 1260 int do_free_irq = 0;
1da177e4 1261
8022fbda 1262 printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq %u\n",
bd678450 1263 unit, dev->chip.name, pdev->revision, pci_name(pdev),
8022fbda 1264 pdev->irq);
1da177e4
LT
1265
1266 /*
1267 * Get the firmware for this chip.
1268 */
1269 fw = sym_find_firmware(&dev->chip);
1270 if (!fw)
a71d035d 1271 goto attach_failed;
1da177e4 1272
99c9e0a1
MW
1273 shost = scsi_host_alloc(tpnt, sizeof(*sym_data));
1274 if (!shost)
a71d035d 1275 goto attach_failed;
99c9e0a1 1276 sym_data = shost_priv(shost);
1da177e4
LT
1277
1278 /*
1279 * Allocate immediately the host control block,
1280 * since we are only expecting to succeed. :)
1281 * We keep track in the HCB of all the resources that
1282 * are to be released on error.
1283 */
1284 np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
1285 if (!np)
1286 goto attach_failed;
1da177e4 1287 np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
99c9e0a1
MW
1288 sym_data->ncb = np;
1289 sym_data->pdev = pdev;
1290 np->s.host = shost;
1da177e4 1291
5111eefa 1292 pci_set_drvdata(pdev, shost);
1da177e4
LT
1293
1294 /*
1295 * Copy some useful infos to the HCB.
1296 */
1297 np->hcb_ba = vtobus(np);
1298 np->verbose = sym_driver_setup.verbose;
1da177e4 1299 np->s.unit = unit;
1da177e4
LT
1300 np->features = dev->chip.features;
1301 np->clock_divn = dev->chip.nr_divisor;
1302 np->maxoffs = dev->chip.offset_max;
1303 np->maxburst = dev->chip.burst_max;
1304 np->myaddr = dev->host_id;
a71d035d 1305 np->mmio_ba = (u32)dev->mmio_base;
783fa731 1306 np->ram_ba = (u32)dev->ram_base;
a71d035d
TB
1307 np->s.ioaddr = dev->s.ioaddr;
1308 np->s.ramaddr = dev->s.ramaddr;
1da177e4
LT
1309
1310 /*
1311 * Edit its name.
1312 */
1313 strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1314 sprintf(np->s.inst_name, "sym%d", np->s.unit);
1315
4d85b471 1316 if ((SYM_CONF_DMA_ADDRESSING_MODE > 0) && (np->features & FE_DAC) &&
5111eefa 1317 !pci_set_dma_mask(pdev, DMA_DAC_MASK)) {
4d85b471 1318 set_dac(np);
284901a9 1319 } else if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
4d85b471 1320 printf_warning("%s: No suitable DMA available\n", sym_name(np));
1da177e4 1321 goto attach_failed;
4d85b471 1322 }
1da177e4 1323
99c9e0a1 1324 if (sym_hcb_attach(shost, fw, dev->nvram))
1da177e4
LT
1325 goto attach_failed;
1326
1327 /*
1328 * Install the interrupt handler.
1329 * If we synchonize the C code with SCRIPTS on interrupt,
1330 * we do not want to share the INTR line at all.
1331 */
99c9e0a1
MW
1332 if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX,
1333 shost)) {
8022fbda 1334 printf_err("%s: request irq %u failure\n",
1da177e4
LT
1335 sym_name(np), pdev->irq);
1336 goto attach_failed;
1337 }
b409063a 1338 do_free_irq = 1;
1da177e4
LT
1339
1340 /*
1341 * After SCSI devices have been opened, we cannot
1342 * reset the bus safely, so we do it here.
1343 */
99c9e0a1 1344 spin_lock_irqsave(shost->host_lock, flags);
1da177e4
LT
1345 if (sym_reset_scsi_bus(np, 0))
1346 goto reset_failed;
1347
1348 /*
1349 * Start the SCRIPTS.
1350 */
5111eefa 1351 sym_start_up(shost, 1);
1da177e4
LT
1352
1353 /*
1354 * Start the timer daemon
1355 */
1356 init_timer(&np->s.timer);
1357 np->s.timer.data = (unsigned long) np;
1358 np->s.timer.function = sym53c8xx_timer;
1359 np->s.lasttime=0;
1360 sym_timer (np);
1361
1362 /*
1363 * Fill Linux host instance structure
1364 * and return success.
1365 */
99c9e0a1
MW
1366 shost->max_channel = 0;
1367 shost->this_id = np->myaddr;
1368 shost->max_id = np->maxwide ? 16 : 8;
1369 shost->max_lun = SYM_CONF_MAX_LUN;
1370 shost->unique_id = pci_resource_start(pdev, 0);
1371 shost->cmd_per_lun = SYM_CONF_MAX_TAG;
1372 shost->can_queue = (SYM_CONF_MAX_START-2);
1373 shost->sg_tablesize = SYM_CONF_MAX_SG;
1374 shost->max_cmd_len = 16;
1da177e4 1375 BUG_ON(sym2_transport_template == NULL);
99c9e0a1 1376 shost->transportt = sym2_transport_template;
1da177e4 1377
34996acc 1378 /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
bd678450 1379 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
99c9e0a1 1380 shost->dma_boundary = 0xFFFFFF;
34996acc 1381
99c9e0a1 1382 spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4 1383
99c9e0a1 1384 return shost;
1da177e4
LT
1385
1386 reset_failed:
1387 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1388 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
99c9e0a1 1389 spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4 1390 attach_failed:
07b9d81e 1391 printf_info("sym%d: giving up ...\n", unit);
1da177e4 1392 if (np)
b409063a 1393 sym_free_resources(np, pdev, do_free_irq);
a71d035d
TB
1394 else
1395 sym_iounmap_device(dev);
1396 if (shost)
1397 scsi_host_put(shost);
1da177e4
LT
1398
1399 return NULL;
1400 }
1401
1402
1403/*
1404 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1405 */
1406#if SYM_CONF_NVRAM_SUPPORT
6f039790 1407static void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1da177e4
LT
1408{
1409 devp->nvram = nvp;
1da177e4
LT
1410 nvp->type = 0;
1411
1412 sym_read_nvram(devp, nvp);
1413}
1414#else
1415static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1416{
1417}
1418#endif /* SYM_CONF_NVRAM_SUPPORT */
1419
6f039790 1420static int sym_check_supported(struct sym_device *device)
1da177e4
LT
1421{
1422 struct sym_chip *chip;
1423 struct pci_dev *pdev = device->pdev;
1da177e4
LT
1424 unsigned long io_port = pci_resource_start(pdev, 0);
1425 int i;
1426
1427 /*
1428 * If user excluded this chip, do not initialize it.
1429 * I hate this code so much. Must kill it.
1430 */
1431 if (io_port) {
1432 for (i = 0 ; i < 8 ; i++) {
1433 if (sym_driver_setup.excludes[i] == io_port)
1434 return -ENODEV;
1435 }
1436 }
1437
1438 /*
1439 * Check if the chip is supported. Then copy the chip description
1440 * to our device structure so we can make it match the actual device
1441 * and options.
1442 */
bd678450 1443 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
1da177e4
LT
1444 if (!chip) {
1445 dev_info(&pdev->dev, "device not supported\n");
1446 return -ENODEV;
1447 }
1448 memcpy(&device->chip, chip, sizeof(device->chip));
1da177e4
LT
1449
1450 return 0;
1451}
1452
1453/*
1454 * Ignore Symbios chips controlled by various RAID controllers.
1455 * These controllers set value 0x52414944 at RAM end - 16.
1456 */
6f039790 1457static int sym_check_raid(struct sym_device *device)
1da177e4
LT
1458{
1459 unsigned int ram_size, ram_val;
1460
1461 if (!device->s.ramaddr)
1462 return 0;
1463
1464 if (device->chip.features & FE_RAM8K)
1465 ram_size = 8192;
1466 else
1467 ram_size = 4096;
1468
1469 ram_val = readl(device->s.ramaddr + ram_size - 16);
1470 if (ram_val != 0x52414944)
1471 return 0;
1472
1473 dev_info(&device->pdev->dev,
1474 "not initializing, driven by RAID controller.\n");
1475 return -ENODEV;
1476}
1477
6f039790 1478static int sym_set_workarounds(struct sym_device *device)
1da177e4
LT
1479{
1480 struct sym_chip *chip = &device->chip;
1481 struct pci_dev *pdev = device->pdev;
1482 u_short status_reg;
1483
1484 /*
1485 * (ITEM 12 of a DEL about the 896 I haven't yet).
1486 * We must ensure the chip will use WRITE AND INVALIDATE.
1487 * The revision number limit is for now arbitrary.
1488 */
bd678450 1489 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 0x4) {
1da177e4
LT
1490 chip->features |= (FE_WRIE | FE_CLSE);
1491 }
1492
1493 /* If the chip can do Memory Write Invalidate, enable it */
1494 if (chip->features & FE_WRIE) {
1495 if (pci_set_mwi(pdev))
1496 return -ENODEV;
1497 }
1498
1499 /*
1500 * Work around for errant bit in 895A. The 66Mhz
1501 * capable bit is set erroneously. Clear this bit.
1502 * (Item 1 DEL 533)
1503 *
1504 * Make sure Config space and Features agree.
1505 *
1506 * Recall: writes are not normal to status register -
1507 * write a 1 to clear and a 0 to leave unchanged.
1508 * Can only reset bits.
1509 */
1510 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1511 if (chip->features & FE_66MHZ) {
1512 if (!(status_reg & PCI_STATUS_66MHZ))
1513 chip->features &= ~FE_66MHZ;
1514 } else {
1515 if (status_reg & PCI_STATUS_66MHZ) {
1516 status_reg = PCI_STATUS_66MHZ;
1517 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1518 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1519 }
1520 }
1521
1522 return 0;
1523}
1524
1525/*
783fa731 1526 * Map HBA registers and on-chip SRAM (if present).
1da177e4 1527 */
6f039790 1528static int sym_iomap_device(struct sym_device *device)
1da177e4 1529{
783fa731 1530 struct pci_dev *pdev = device->pdev;
b6d105d7 1531 struct pci_bus_region bus_addr;
783fa731 1532 int i = 2;
1da177e4 1533
b6d105d7
MW
1534 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
1535 device->mmio_base = bus_addr.start;
1536
783fa731
TB
1537 if (device->chip.features & FE_RAM) {
1538 /*
1539 * If the BAR is 64-bit, resource 2 will be occupied by the
1540 * upper 32 bits
1541 */
1542 if (!pdev->resource[i].flags)
1543 i++;
1544 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
1545 device->ram_base = bus_addr.start;
1546 }
1da177e4 1547
1f61d824 1548#ifdef CONFIG_SCSI_SYM53C8XX_MMIO
1da177e4
LT
1549 if (device->mmio_base)
1550 device->s.ioaddr = pci_iomap(pdev, 1,
1551 pci_resource_len(pdev, 1));
1552#endif
1553 if (!device->s.ioaddr)
1554 device->s.ioaddr = pci_iomap(pdev, 0,
1555 pci_resource_len(pdev, 0));
783fa731
TB
1556 if (!device->s.ioaddr) {
1557 dev_err(&pdev->dev, "could not map registers; giving up.\n");
1558 return -EIO;
1559 }
1560 if (device->ram_base) {
1da177e4
LT
1561 device->s.ramaddr = pci_iomap(pdev, i,
1562 pci_resource_len(pdev, i));
783fa731
TB
1563 if (!device->s.ramaddr) {
1564 dev_warn(&pdev->dev,
1565 "could not map SRAM; continuing anyway.\n");
1566 device->ram_base = 0;
1567 }
1568 }
1569
1570 return 0;
1da177e4
LT
1571}
1572
1573/*
1574 * The NCR PQS and PDS cards are constructed as a DEC bridge
1575 * behind which sits a proprietary NCR memory controller and
1576 * either four or two 53c875s as separate devices. We can tell
1577 * if an 875 is part of a PQS/PDS or not since if it is, it will
1578 * be on the same bus as the memory controller. In its usual
1579 * mode of operation, the 875s are slaved to the memory
1580 * controller for all transfers. To operate with the Linux
1581 * driver, the memory controller is disabled and the 875s
1582 * freed to function independently. The only wrinkle is that
1583 * the preset SCSI ID (which may be zero) must be read in from
1584 * a special configuration space register of the 875.
1585 */
1586static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
1587{
1588 int slot;
1589 u8 tmp;
1590
1591 for (slot = 0; slot < 256; slot++) {
1592 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
1593
1594 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
1595 pci_dev_put(memc);
1596 continue;
1597 }
1598
1599 /* bit 1: allow individual 875 configuration */
1600 pci_read_config_byte(memc, 0x44, &tmp);
1601 if ((tmp & 0x2) == 0) {
1602 tmp |= 0x2;
1603 pci_write_config_byte(memc, 0x44, tmp);
1604 }
1605
1606 /* bit 2: drive individual 875 interrupts to the bus */
1607 pci_read_config_byte(memc, 0x45, &tmp);
1608 if ((tmp & 0x4) == 0) {
1609 tmp |= 0x4;
1610 pci_write_config_byte(memc, 0x45, tmp);
1611 }
1612
1613 pci_dev_put(memc);
1614 break;
1615 }
1616
1617 pci_read_config_byte(pdev, 0x84, &tmp);
1618 sym_dev->host_id = tmp;
1619}
1620
1621/*
1622 * Called before unloading the module.
1623 * Detach the host.
1624 * We have to free resources and halt the NCR chip.
1625 */
5111eefa 1626static int sym_detach(struct Scsi_Host *shost, struct pci_dev *pdev)
1da177e4 1627{
5111eefa 1628 struct sym_hcb *np = sym_get_hcb(shost);
1da177e4
LT
1629 printk("%s: detaching ...\n", sym_name(np));
1630
1631 del_timer_sync(&np->s.timer);
1632
1633 /*
1634 * Reset NCR chip.
1635 * We should use sym_soft_reset(), but we don't want to do
1636 * so, since we may not be safe if interrupts occur.
1637 */
1638 printk("%s: resetting chip\n", sym_name(np));
1639 OUTB(np, nc_istat, SRST);
53222b90 1640 INB(np, nc_mbox1);
1da177e4
LT
1641 udelay(10);
1642 OUTB(np, nc_istat, 0);
1643
b409063a 1644 sym_free_resources(np, pdev, 1);
d3ce65d1 1645 scsi_host_put(shost);
1da177e4
LT
1646
1647 return 1;
1648}
1649
1650/*
1651 * Driver host template.
1652 */
1653static struct scsi_host_template sym2_template = {
1654 .module = THIS_MODULE,
1655 .name = "sym53c8xx",
1656 .info = sym53c8xx_info,
1657 .queuecommand = sym53c8xx_queue_command,
1658 .slave_alloc = sym53c8xx_slave_alloc,
1659 .slave_configure = sym53c8xx_slave_configure,
84e203a2 1660 .slave_destroy = sym53c8xx_slave_destroy,
1da177e4
LT
1661 .eh_abort_handler = sym53c8xx_eh_abort_handler,
1662 .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
1663 .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
1664 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
1665 .this_id = 7,
14ac8bf5
MW
1666 .use_clustering = ENABLE_CLUSTERING,
1667 .max_sectors = 0xFFFF,
1da177e4 1668#ifdef SYM_LINUX_PROC_INFO_SUPPORT
ee127fec
AV
1669 .show_info = sym_show_info,
1670#ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1671 .write_info = sym_user_command,
1672#endif
1da177e4
LT
1673 .proc_name = NAME53C8XX,
1674#endif
1675};
1676
1677static int attach_count;
1678
6f039790 1679static int sym2_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1da177e4
LT
1680{
1681 struct sym_device sym_dev;
1682 struct sym_nvram nvram;
99c9e0a1 1683 struct Scsi_Host *shost;
a71d035d
TB
1684 int do_iounmap = 0;
1685 int do_disable_device = 1;
1da177e4
LT
1686
1687 memset(&sym_dev, 0, sizeof(sym_dev));
1688 memset(&nvram, 0, sizeof(nvram));
783fa731
TB
1689 sym_dev.pdev = pdev;
1690 sym_dev.host_id = SYM_SETUP_HOST_ID;
1da177e4
LT
1691
1692 if (pci_enable_device(pdev))
1693 goto leave;
1694
1695 pci_set_master(pdev);
1696
1697 if (pci_request_regions(pdev, NAME53C8XX))
1698 goto disable;
1699
1da177e4
LT
1700 if (sym_check_supported(&sym_dev))
1701 goto free;
1702
783fa731
TB
1703 if (sym_iomap_device(&sym_dev))
1704 goto free;
1705 do_iounmap = 1;
1706
a71d035d
TB
1707 if (sym_check_raid(&sym_dev)) {
1708 do_disable_device = 0; /* Don't disable the device */
1709 goto free;
1710 }
1da177e4
LT
1711
1712 if (sym_set_workarounds(&sym_dev))
1713 goto free;
1714
1715 sym_config_pqs(pdev, &sym_dev);
1716
1717 sym_get_nvram(&sym_dev, &nvram);
1718
a71d035d 1719 do_iounmap = 0; /* Don't sym_iounmap_device() after sym_attach(). */
99c9e0a1
MW
1720 shost = sym_attach(&sym2_template, attach_count, &sym_dev);
1721 if (!shost)
1da177e4
LT
1722 goto free;
1723
99c9e0a1 1724 if (scsi_add_host(shost, &pdev->dev))
1da177e4 1725 goto detach;
99c9e0a1 1726 scsi_scan_host(shost);
1da177e4
LT
1727
1728 attach_count++;
1729
1730 return 0;
1731
1732 detach:
1733 sym_detach(pci_get_drvdata(pdev), pdev);
1734 free:
a71d035d
TB
1735 if (do_iounmap)
1736 sym_iounmap_device(&sym_dev);
1da177e4
LT
1737 pci_release_regions(pdev);
1738 disable:
a71d035d
TB
1739 if (do_disable_device)
1740 pci_disable_device(pdev);
1da177e4
LT
1741 leave:
1742 return -ENODEV;
1743}
1744
864473bb 1745static void sym2_remove(struct pci_dev *pdev)
1da177e4 1746{
5111eefa 1747 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1da177e4 1748
5111eefa 1749 scsi_remove_host(shost);
5111eefa 1750 sym_detach(shost, pdev);
1da177e4
LT
1751 pci_release_regions(pdev);
1752 pci_disable_device(pdev);
1753
1754 attach_count--;
1755}
1756
d68cd759
LV
1757/**
1758 * sym2_io_error_detected() - called when PCI error is detected
1759 * @pdev: pointer to PCI device
1760 * @state: current state of the PCI slot
1761 */
1762static pci_ers_result_t sym2_io_error_detected(struct pci_dev *pdev,
1763 enum pci_channel_state state)
1764{
1765 /* If slot is permanently frozen, turn everything off */
1766 if (state == pci_channel_io_perm_failure) {
1767 sym2_remove(pdev);
1768 return PCI_ERS_RESULT_DISCONNECT;
1769 }
1770
1771 disable_irq(pdev->irq);
1772 pci_disable_device(pdev);
1773
1774 /* Request that MMIO be enabled, so register dump can be taken. */
1775 return PCI_ERS_RESULT_CAN_RECOVER;
1776}
1777
1778/**
1779 * sym2_io_slot_dump - Enable MMIO and dump debug registers
1780 * @pdev: pointer to PCI device
1781 */
1782static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
1783{
5111eefa 1784 struct Scsi_Host *shost = pci_get_drvdata(pdev);
d68cd759 1785
5111eefa 1786 sym_dump_registers(shost);
d68cd759
LV
1787
1788 /* Request a slot reset. */
1789 return PCI_ERS_RESULT_NEED_RESET;
1790}
1791
1792/**
1793 * sym2_reset_workarounds - hardware-specific work-arounds
1794 *
1795 * This routine is similar to sym_set_workarounds(), except
1796 * that, at this point, we already know that the device was
b595076a 1797 * successfully initialized at least once before, and so most
d68cd759
LV
1798 * of the steps taken there are un-needed here.
1799 */
1800static void sym2_reset_workarounds(struct pci_dev *pdev)
1801{
d68cd759
LV
1802 u_short status_reg;
1803 struct sym_chip *chip;
1804
bd678450 1805 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
d68cd759
LV
1806
1807 /* Work around for errant bit in 895A, in a fashion
1808 * similar to what is done in sym_set_workarounds().
1809 */
1810 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1811 if (!(chip->features & FE_66MHZ) && (status_reg & PCI_STATUS_66MHZ)) {
1812 status_reg = PCI_STATUS_66MHZ;
1813 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1814 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1815 }
1816}
1817
1818/**
1819 * sym2_io_slot_reset() - called when the pci bus has been reset.
1820 * @pdev: pointer to PCI device
1821 *
1822 * Restart the card from scratch.
1823 */
1824static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
1825{
5111eefa
MW
1826 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1827 struct sym_hcb *np = sym_get_hcb(shost);
d68cd759
LV
1828
1829 printk(KERN_INFO "%s: recovering from a PCI slot reset\n",
1830 sym_name(np));
1831
1832 if (pci_enable_device(pdev)) {
1833 printk(KERN_ERR "%s: Unable to enable after PCI reset\n",
1834 sym_name(np));
1835 return PCI_ERS_RESULT_DISCONNECT;
1836 }
1837
1838 pci_set_master(pdev);
1839 enable_irq(pdev->irq);
1840
1841 /* If the chip can do Memory Write Invalidate, enable it */
1842 if (np->features & FE_WRIE) {
1843 if (pci_set_mwi(pdev))
1844 return PCI_ERS_RESULT_DISCONNECT;
1845 }
1846
1847 /* Perform work-arounds, analogous to sym_set_workarounds() */
1848 sym2_reset_workarounds(pdev);
1849
1850 /* Perform host reset only on one instance of the card */
1851 if (PCI_FUNC(pdev->devfn) == 0) {
1852 if (sym_reset_scsi_bus(np, 0)) {
1853 printk(KERN_ERR "%s: Unable to reset scsi host\n",
1854 sym_name(np));
1855 return PCI_ERS_RESULT_DISCONNECT;
1856 }
5111eefa 1857 sym_start_up(shost, 1);
d68cd759
LV
1858 }
1859
1860 return PCI_ERS_RESULT_RECOVERED;
1861}
1862
1863/**
1864 * sym2_io_resume() - resume normal ops after PCI reset
1865 * @pdev: pointer to PCI device
1866 *
1867 * Called when the error recovery driver tells us that its
1868 * OK to resume normal operation. Use completion to allow
1869 * halted scsi ops to resume.
1870 */
1871static void sym2_io_resume(struct pci_dev *pdev)
1872{
5111eefa 1873 struct Scsi_Host *shost = pci_get_drvdata(pdev);
99c9e0a1 1874 struct sym_data *sym_data = shost_priv(shost);
d68cd759
LV
1875
1876 spin_lock_irq(shost->host_lock);
99c9e0a1
MW
1877 if (sym_data->io_reset)
1878 complete_all(sym_data->io_reset);
d68cd759
LV
1879 spin_unlock_irq(shost->host_lock);
1880}
1881
1da177e4
LT
1882static void sym2_get_signalling(struct Scsi_Host *shost)
1883{
1884 struct sym_hcb *np = sym_get_hcb(shost);
1885 enum spi_signal_type type;
1886
1887 switch (np->scsi_mode) {
1888 case SMODE_SE:
1889 type = SPI_SIGNAL_SE;
1890 break;
1891 case SMODE_LVD:
1892 type = SPI_SIGNAL_LVD;
1893 break;
1894 case SMODE_HVD:
1895 type = SPI_SIGNAL_HVD;
1896 break;
1897 default:
1898 type = SPI_SIGNAL_UNKNOWN;
1899 break;
1900 }
1901 spi_signalling(shost) = type;
1902}
1903
1904static void sym2_set_offset(struct scsi_target *starget, int offset)
1905{
1906 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1907 struct sym_hcb *np = sym_get_hcb(shost);
1908 struct sym_tcb *tp = &np->target[starget->id];
1909
1910 tp->tgoal.offset = offset;
1911 tp->tgoal.check_nego = 1;
1912}
1913
1914static void sym2_set_period(struct scsi_target *starget, int period)
1915{
1916 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1917 struct sym_hcb *np = sym_get_hcb(shost);
1918 struct sym_tcb *tp = &np->target[starget->id];
1919
e4862fed
JB
1920 /* have to have DT for these transfers, but DT will also
1921 * set width, so check that this is allowed */
1922 if (period <= np->minsync && spi_width(starget))
1da177e4
LT
1923 tp->tgoal.dt = 1;
1924
1925 tp->tgoal.period = period;
1926 tp->tgoal.check_nego = 1;
1927}
1928
1929static void sym2_set_width(struct scsi_target *starget, int width)
1930{
1931 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1932 struct sym_hcb *np = sym_get_hcb(shost);
1933 struct sym_tcb *tp = &np->target[starget->id];
1934
1935 /* It is illegal to have DT set on narrow transfers. If DT is
1936 * clear, we must also clear IU and QAS. */
1937 if (width == 0)
1938 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1939
1940 tp->tgoal.width = width;
1941 tp->tgoal.check_nego = 1;
1942}
1943
1944static void sym2_set_dt(struct scsi_target *starget, int dt)
1945{
1946 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1947 struct sym_hcb *np = sym_get_hcb(shost);
1948 struct sym_tcb *tp = &np->target[starget->id];
1949
1950 /* We must clear QAS and IU if DT is clear */
1951 if (dt)
1952 tp->tgoal.dt = 1;
1953 else
1954 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1955 tp->tgoal.check_nego = 1;
1956}
1957
8b2f8138 1958#if 0
1da177e4
LT
1959static void sym2_set_iu(struct scsi_target *starget, int iu)
1960{
1961 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1962 struct sym_hcb *np = sym_get_hcb(shost);
1963 struct sym_tcb *tp = &np->target[starget->id];
1964
1965 if (iu)
1966 tp->tgoal.iu = tp->tgoal.dt = 1;
1967 else
1968 tp->tgoal.iu = 0;
1969 tp->tgoal.check_nego = 1;
1970}
1971
1972static void sym2_set_qas(struct scsi_target *starget, int qas)
1973{
1974 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1975 struct sym_hcb *np = sym_get_hcb(shost);
1976 struct sym_tcb *tp = &np->target[starget->id];
1977
1978 if (qas)
1979 tp->tgoal.dt = tp->tgoal.qas = 1;
1980 else
1981 tp->tgoal.qas = 0;
1982 tp->tgoal.check_nego = 1;
1983}
8b2f8138 1984#endif
1da177e4
LT
1985
1986static struct spi_function_template sym2_transport_functions = {
1987 .set_offset = sym2_set_offset,
1988 .show_offset = 1,
1989 .set_period = sym2_set_period,
1990 .show_period = 1,
1991 .set_width = sym2_set_width,
1992 .show_width = 1,
1993 .set_dt = sym2_set_dt,
1994 .show_dt = 1,
8b2f8138 1995#if 0
1da177e4
LT
1996 .set_iu = sym2_set_iu,
1997 .show_iu = 1,
1998 .set_qas = sym2_set_qas,
1999 .show_qas = 1,
8b2f8138 2000#endif
1da177e4
LT
2001 .get_signalling = sym2_get_signalling,
2002};
2003
6f039790 2004static struct pci_device_id sym2_id_table[] = {
1da177e4
LT
2005 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
2006 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2007 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
2008 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2009 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
2010 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2011 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
2012 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2013 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
2014 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2015 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
2016 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2017 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
b2b3c121 2018 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
1da177e4
LT
2019 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
2020 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2021 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
2022 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2023 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2024 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2025 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2026 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2027 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
147e505e 2028 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
1da177e4
LT
2029 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2030 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2031 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2032 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2033 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2034 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2035 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2036 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2037 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2038 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2039 { 0, }
2040};
2041
2042MODULE_DEVICE_TABLE(pci, sym2_id_table);
2043
a55b2d21 2044static const struct pci_error_handlers sym2_err_handler = {
d68cd759
LV
2045 .error_detected = sym2_io_error_detected,
2046 .mmio_enabled = sym2_io_slot_dump,
2047 .slot_reset = sym2_io_slot_reset,
2048 .resume = sym2_io_resume,
2049};
2050
1da177e4
LT
2051static struct pci_driver sym2_driver = {
2052 .name = NAME53C8XX,
2053 .id_table = sym2_id_table,
2054 .probe = sym2_probe,
864473bb 2055 .remove = sym2_remove,
d68cd759 2056 .err_handler = &sym2_err_handler,
1da177e4
LT
2057};
2058
2059static int __init sym2_init(void)
2060{
2061 int error;
2062
2063 sym2_setup_params();
2064 sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2065 if (!sym2_transport_template)
2066 return -ENODEV;
2067
2068 error = pci_register_driver(&sym2_driver);
2069 if (error)
2070 spi_release_transport(sym2_transport_template);
2071 return error;
2072}
2073
2074static void __exit sym2_exit(void)
2075{
2076 pci_unregister_driver(&sym2_driver);
2077 spi_release_transport(sym2_transport_template);
2078}
2079
2080module_init(sym2_init);
2081module_exit(sym2_exit);