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