1 // SPDX-License-Identifier: GPL-2.0-or-later
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  *-----------------------------------------------------------------------------
26  */
27 #include <linux/ctype.h>
28 #include <linux/init.h>
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 
43 struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
44 unsigned int sym_debug_flags = 0;
45 
46 static char *excl_string;
47 static char *safe_string;
48 module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
49 module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
50 module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
51 module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
52 module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
53 module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
54 module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
55 module_param_named(verb, sym_driver_setup.verbose, byte, 0);
56 module_param_named(debug, sym_debug_flags, uint, 0);
57 module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
58 module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
59 module_param_named(excl, excl_string, charp, 0);
60 module_param_named(safe, safe_string, charp, 0);
61 
62 MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
63 MODULE_PARM_DESC(burst, "Maximum burst.  0 to disable, 255 to read from registers");
64 MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
65 MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
66 MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
67 MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
68 MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
69 MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
70 MODULE_PARM_DESC(debug, "Set bits to enable debugging");
71 MODULE_PARM_DESC(settle, "Settle delay in seconds.  Default 3");
72 MODULE_PARM_DESC(nvram, "Option currently not used");
73 MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
74 MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
75 
76 MODULE_LICENSE("GPL");
77 MODULE_VERSION(SYM_VERSION);
78 MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
79 MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
80 
sym2_setup_params(void)81 static 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 
112 static struct scsi_transport_template *sym2_transport_template = NULL;
113 
114 /*
115  *  Driver private area in the SCSI command structure.
116  */
117 struct sym_ucmd {		/* Override the SCSI pointer structure */
118 	struct completion *eh_done;		/* SCSI error handling */
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 
124 /*
125  *  Complete a pending CAM CCB.
126  */
sym_xpt_done(struct sym_hcb * np,struct scsi_cmnd * cmd)127 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
128 {
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 
135 	scsi_dma_unmap(cmd);
136 	cmd->scsi_done(cmd);
137 }
138 
139 /*
140  *  Tell the SCSI layer about a BUS RESET.
141  */
sym_xpt_async_bus_reset(struct sym_hcb * np)142 void 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 
152 /*
153  *  Choose the more appropriate CAM status if
154  *  the IO encountered an extended error.
155  */
sym_xerr_cam_status(int cam_status,int x_status)156 static 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  */
sym_set_cam_result_error(struct sym_hcb * np,struct sym_ccb * cp,int resid)174 void 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 			 */
197 			memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
198 			memcpy(cmd->sense_buffer, cp->sns_bbuf,
199 			       min(SCSI_SENSE_BUFFERSIZE, SYM_SNS_BBUF_LEN));
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 	}
241 	scsi_set_resid(cmd, resid);
242 	cmd->result = (drv_status << 24) | (cam_status << 16) | scsi_status;
243 }
244 
sym_scatter(struct sym_hcb * np,struct sym_ccb * cp,struct scsi_cmnd * cmd)245 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
246 {
247 	int segment;
248 	int use_sg;
249 
250 	cp->data_len = 0;
251 
252 	use_sg = scsi_dma_map(cmd);
253 	if (use_sg > 0) {
254 		struct scatterlist *sg;
255 		struct sym_tcb *tp = &np->target[cp->target];
256 		struct sym_tblmove *data;
257 
258 		if (use_sg > SYM_CONF_MAX_SG) {
259 			scsi_dma_unmap(cmd);
260 			return -1;
261 		}
262 
263 		data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
264 
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);
268 
269 			if ((len & 1) && (tp->head.wval & EWS)) {
270 				len++;
271 				cp->odd_byte_adjustment++;
272 			}
273 
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  */
sym_queue_command(struct sym_hcb * np,struct scsi_cmnd * cmd)287 static 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 
295 	/*
296 	 *  Retrieve the target descriptor.
297 	 */
298 	tp = &np->target[sdev->id];
299 
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  */
sym_setup_cdb(struct sym_hcb * np,struct scsi_cmnd * cmd,struct sym_ccb * cp)319 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
320 {
321 	memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
322 
323 	cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
324 	cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
325 
326 	return 0;
327 }
328 
329 /*
330  *  Setup pointers that address the data and start the I/O.
331  */
sym_setup_data_and_start(struct sym_hcb * np,struct scsi_cmnd * cmd,struct sym_ccb * cp)332 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
333 {
334 	u32 lastp, goalp;
335 	int dir;
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) {
350 			sym_set_cam_status(cmd, DID_ERROR);
351 			goto out_abort;
352 		}
353 
354 		/*
355 		 *  No segments means no data.
356 		 */
357 		if (!cp->segments)
358 			dir = DMA_NONE;
359 	} else {
360 		cp->data_len = 0;
361 		cp->segments = 0;
362 	}
363 
364 	/*
365 	 *  Set the data pointer.
366 	 */
367 	switch (dir) {
368 	case DMA_BIDIRECTIONAL:
369 		scmd_printk(KERN_INFO, cmd, "got DMA_BIDIRECTIONAL command");
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.
389 	 */
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);
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 	 */
414 	sym_put_start_queue(np, cp);
415 	return 0;
416 
417 out_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  */
sym_timer(struct sym_hcb * np)430 static 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  */
sym_log_bus_error(struct Scsi_Host * shost)478 void sym_log_bus_error(struct Scsi_Host *shost)
479 {
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);
484 	if (pci_sts & 0xf900) {
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);
488 	}
489 }
490 
491 /*
492  * queuecommand method.  Entered with the host adapter lock held and
493  * interrupts disabled.
494  */
sym53c8xx_queue_command_lck(struct scsi_cmnd * cmd,void (* done)(struct scsi_cmnd *))495 static int sym53c8xx_queue_command_lck(struct scsi_cmnd *cmd,
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 
502 	cmd->scsi_done = done;
503 	memset(ucp, 0, sizeof(*ucp));
504 
505 	/*
506 	 *  Shorten our settle_time if needed for
507 	 *  this command not to time out.
508 	 */
509 	if (np->s.settle_time_valid && cmd->request->timeout) {
510 		unsigned long tlimit = jiffies + cmd->request->timeout;
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 
DEF_SCSI_QCMD(sym53c8xx_queue_command)526 static DEF_SCSI_QCMD(sym53c8xx_queue_command)
527 
528 /*
529  *  Linux entry point of the interrupt handler.
530  */
531 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
532 {
533 	struct Scsi_Host *shost = dev_id;
534 	struct sym_data *sym_data = shost_priv(shost);
535 	irqreturn_t result;
536 
537 	/* Avoid spinloop trying to handle interrupts on frozen device */
538 	if (pci_channel_offline(sym_data->pdev))
539 		return IRQ_NONE;
540 
541 	if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
542 
543 	spin_lock(shost->host_lock);
544 	result = sym_interrupt(shost);
545 	spin_unlock(shost->host_lock);
546 
547 	if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
548 
549 	return result;
550 }
551 
552 /*
553  *  Linux entry point of the timer handler
554  */
sym53c8xx_timer(struct timer_list * t)555 static void sym53c8xx_timer(struct timer_list *t)
556 {
557 	struct sym_hcb *np = from_timer(np, t, s.timer);
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 
574 /*
575  *  Generic method for our eh processing.
576  *  The 'op' argument tells what we have to do.
577  */
sym_eh_handler(int op,char * opname,struct scsi_cmnd * cmd)578 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
579 {
580 	struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
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;
585 	SYM_QUEHEAD *qp;
586 	int cmd_queued = 0;
587 	int sts = -1;
588 	struct completion eh_done;
589 
590 	scmd_printk(KERN_WARNING, cmd, "%s operation started\n", opname);
591 
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)) {
600 		int finished_reset = 0;
601 		init_completion(&eh_done);
602 		spin_lock_irq(shost->host_lock);
603 		/* Make sure we didn't race */
604 		if (pci_channel_offline(pdev)) {
605 			BUG_ON(sym_data->io_reset);
606 			sym_data->io_reset = &eh_done;
607 		} else {
608 			finished_reset = 1;
609 		}
610 		spin_unlock_irq(shost->host_lock);
611 		if (!finished_reset)
612 			finished_reset = wait_for_completion_timeout
613 						(sym_data->io_reset,
614 						WAIT_FOR_PCI_RECOVERY*HZ);
615 		spin_lock_irq(shost->host_lock);
616 		sym_data->io_reset = NULL;
617 		spin_unlock_irq(shost->host_lock);
618 		if (!finished_reset)
619 			return SCSI_FAILED;
620 	}
621 
622 	spin_lock_irq(shost->host_lock);
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) {
627 			cmd_queued = 1;
628 			break;
629 		}
630 	}
631 
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);
647 		sym_start_up(shost, 1);
648 		sts = 0;
649 		break;
650 	default:
651 		break;
652 	}
653 
654 	/* On error, restore everything and cross fingers :) */
655 	if (sts)
656 		cmd_queued = 0;
657 
658 	if (cmd_queued) {
659 		init_completion(&eh_done);
660 		ucmd->eh_done = &eh_done;
661 		spin_unlock_irq(shost->host_lock);
662 		if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
663 			ucmd->eh_done = NULL;
664 			sts = -2;
665 		}
666 	} else {
667 		spin_unlock_irq(shost->host_lock);
668 	}
669 
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  */
sym53c8xx_eh_abort_handler(struct scsi_cmnd * cmd)679 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
680 {
681 	return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
682 }
683 
sym53c8xx_eh_device_reset_handler(struct scsi_cmnd * cmd)684 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
685 {
686 	return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
687 }
688 
sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd * cmd)689 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
690 {
691 	return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
692 }
693 
sym53c8xx_eh_host_reset_handler(struct scsi_cmnd * cmd)694 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
695 {
696 	return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
697 }
698 
699 /*
700  *  Tune device queuing depth, according to various limits.
701  */
sym_tune_dev_queuing(struct sym_tcb * tp,int lun,u_short reqtags)702 static 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 
715 	lp->s.reqtags     = reqtags;
716 
717 	if (reqtags != oldtags) {
718 		dev_info(&tp->starget->dev,
719 		         "tagged command queuing %s, command queue depth %d.\n",
720 		          lp->s.reqtags ? "enabled" : "disabled", reqtags);
721 	}
722 }
723 
sym53c8xx_slave_alloc(struct scsi_device * sdev)724 static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
725 {
726 	struct sym_hcb *np = sym_get_hcb(sdev->host);
727 	struct sym_tcb *tp = &np->target[sdev->id];
728 	struct sym_lcb *lp;
729 	unsigned long flags;
730 	int error;
731 
732 	if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
733 		return -ENXIO;
734 
735 	spin_lock_irqsave(np->s.host->host_lock, flags);
736 
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 
746 	if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
747 		tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
748 		starget_printk(KERN_INFO, sdev->sdev_target,
749 				"Scan at boot disabled in NVRAM\n");
750 		error = -ENXIO;
751 		goto out;
752 	}
753 
754 	if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
755 		if (sdev->lun != 0) {
756 			error = -ENXIO;
757 			goto out;
758 		}
759 		starget_printk(KERN_INFO, sdev->sdev_target,
760 				"Multiple LUNs disabled in NVRAM\n");
761 	}
762 
763 	lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
764 	if (!lp) {
765 		error = -ENOMEM;
766 		goto out;
767 	}
768 	if (tp->nlcb == 1)
769 		tp->starget = sdev->sdev_target;
770 
771 	spi_min_period(tp->starget) = tp->usr_period;
772 	spi_max_width(tp->starget) = tp->usr_width;
773 
774 	error = 0;
775 out:
776 	spin_unlock_irqrestore(np->s.host->host_lock, flags);
777 
778 	return error;
779 }
780 
781 /*
782  * Linux entry point for device queue sizing.
783  */
sym53c8xx_slave_configure(struct scsi_device * sdev)784 static int sym53c8xx_slave_configure(struct scsi_device *sdev)
785 {
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);
789 	int reqtags, depth_to_use;
790 
791 	/*
792 	 *  Get user flags.
793 	 */
794 	lp->curr_flags = lp->user_flags;
795 
796 	/*
797 	 *  Select queue depth from driver setup.
798 	 *  Do not use more than configured by user.
799 	 *  Use at least 1.
800 	 *  Do not use more than our maximum.
801 	 */
802 	reqtags = sym_driver_setup.max_tag;
803 	if (reqtags > tp->usrtags)
804 		reqtags = tp->usrtags;
805 	if (!sdev->tagged_supported)
806 		reqtags = 0;
807 	if (reqtags > SYM_CONF_MAX_TAG)
808 		reqtags = SYM_CONF_MAX_TAG;
809 	depth_to_use = reqtags ? reqtags : 1;
810 	scsi_change_queue_depth(sdev, depth_to_use);
811 	lp->s.scdev_depth = depth_to_use;
812 	sym_tune_dev_queuing(tp, sdev->lun, reqtags);
813 
814 	if (!spi_initial_dv(sdev->sdev_target))
815 		spi_dv_device(sdev);
816 
817 	return 0;
818 }
819 
sym53c8xx_slave_destroy(struct scsi_device * sdev)820 static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
821 {
822 	struct sym_hcb *np = sym_get_hcb(sdev->host);
823 	struct sym_tcb *tp = &np->target[sdev->id];
824 	struct sym_lcb *lp = sym_lp(tp, sdev->lun);
825 	unsigned long flags;
826 
827 	/* if slave_alloc returned before allocating a sym_lcb, return */
828 	if (!lp)
829 		return;
830 
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,
839 			       "Removing busy LCB (%d)\n", (u8)sdev->lun);
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);
855 }
856 
857 /*
858  *  Linux entry point for info() function
859  */
sym53c8xx_info(struct Scsi_Host * host)860 static 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 
878 struct	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 
sym_exec_user_command(struct sym_hcb * np,struct sym_usrcmd * uc)894 static 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];
920 			if (!tp->nlcb)
921 				continue;
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 
sym_skip_spaces(char * ptr,int len)978 static int sym_skip_spaces(char *ptr, int len)
979 {
980 	int cnt, c;
981 
982 	for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
983 
984 	return (len - cnt);
985 }
986 
get_int_arg(char * ptr,int len,u_long * pv)987 static 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 
is_keyword(char * ptr,int len,char * verb)995 static 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)						\
1006 	if ((arg_len = sym_skip_spaces(ptr, len)) < 1)			\
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 
sym_user_command(struct Scsi_Host * shost,char * buffer,int length)1020 static int sym_user_command(struct Scsi_Host *shost, char *buffer, int length)
1021 {
1022 	struct sym_hcb *np = sym_get_hcb(shost);
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
1056 printk("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
1078 printk("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
1092 printk("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
1126 printk("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 
1149 		spin_lock_irqsave(shost->host_lock, flags);
1150 		sym_exec_user_command(np, uc);
1151 		spin_unlock_irqrestore(shost->host_lock, flags);
1152 	}
1153 	return length;
1154 }
1155 
1156 #endif	/* SYM_LINUX_USER_COMMAND_SUPPORT */
1157 
1158 
1159 /*
1160  *  Copy formatted information into the input buffer.
1161  */
sym_show_info(struct seq_file * m,struct Scsi_Host * shost)1162 static int sym_show_info(struct seq_file *m, struct Scsi_Host *shost)
1163 {
1164 #ifdef SYM_LINUX_USER_INFO_SUPPORT
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;
1168 
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",
1173 			 pci_name(pdev), pdev->irq);
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" : "");
1178 
1179 	seq_printf(m, "Max. started commands %d, "
1180 		 "max. commands per LUN %d\n",
1181 		 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1182 
1183 	return 0;
1184 #else
1185 	return -EINVAL;
1186 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1187 }
1188 
1189 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1190 
1191 /*
1192  * Free resources claimed by sym_iomap_device().  Note that
1193  * sym_free_resources() should be used instead of this function after calling
1194  * sym_attach().
1195  */
sym_iounmap_device(struct sym_device * device)1196 static void sym_iounmap_device(struct sym_device *device)
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 
1204 /*
1205  *	Free controller resources.
1206  */
sym_free_resources(struct sym_hcb * np,struct pci_dev * pdev,int do_free_irq)1207 static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev,
1208 		int do_free_irq)
1209 {
1210 	/*
1211 	 *  Free O/S specific resources.
1212 	 */
1213 	if (do_free_irq)
1214 		free_irq(pdev->irq, np->s.host);
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 
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  */
sym_attach(struct scsi_host_template * tpnt,int unit,struct sym_device * dev)1236 static struct Scsi_Host *sym_attach(struct scsi_host_template *tpnt, int unit,
1237 				    struct sym_device *dev)
1238 {
1239 	struct sym_data *sym_data;
1240 	struct sym_hcb *np = NULL;
1241 	struct Scsi_Host *shost = NULL;
1242 	struct pci_dev *pdev = dev->pdev;
1243 	unsigned long flags;
1244 	struct sym_fw *fw;
1245 	int do_free_irq = 0;
1246 
1247 	printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq %u\n",
1248 		unit, dev->chip.name, pdev->revision, pci_name(pdev),
1249 		pdev->irq);
1250 
1251 	/*
1252 	 *  Get the firmware for this chip.
1253 	 */
1254 	fw = sym_find_firmware(&dev->chip);
1255 	if (!fw)
1256 		goto attach_failed;
1257 
1258 	shost = scsi_host_alloc(tpnt, sizeof(*sym_data));
1259 	if (!shost)
1260 		goto attach_failed;
1261 	sym_data = shost_priv(shost);
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;
1272 	np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
1273 	sym_data->ncb = np;
1274 	sym_data->pdev = pdev;
1275 	np->s.host = shost;
1276 
1277 	pci_set_drvdata(pdev, shost);
1278 
1279 	/*
1280 	 *  Copy some useful infos to the HCB.
1281 	 */
1282 	np->hcb_ba	= vtobus(np);
1283 	np->verbose	= sym_driver_setup.verbose;
1284 	np->s.unit	= unit;
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;
1290 	np->mmio_ba	= (u32)dev->mmio_base;
1291 	np->ram_ba	= (u32)dev->ram_base;
1292 	np->s.ioaddr	= dev->s.ioaddr;
1293 	np->s.ramaddr	= dev->s.ramaddr;
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 
1301 	if ((SYM_CONF_DMA_ADDRESSING_MODE > 0) && (np->features & FE_DAC) &&
1302 			!dma_set_mask(&pdev->dev, DMA_DAC_MASK)) {
1303 		set_dac(np);
1304 	} else if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
1305 		printf_warning("%s: No suitable DMA available\n", sym_name(np));
1306 		goto attach_failed;
1307 	}
1308 
1309 	if (sym_hcb_attach(shost, fw, dev->nvram))
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 	 */
1317 	if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX,
1318 			shost)) {
1319 		printf_err("%s: request irq %u failure\n",
1320 			sym_name(np), pdev->irq);
1321 		goto attach_failed;
1322 	}
1323 	do_free_irq = 1;
1324 
1325 	/*
1326 	 *  After SCSI devices have been opened, we cannot
1327 	 *  reset the bus safely, so we do it here.
1328 	 */
1329 	spin_lock_irqsave(shost->host_lock, flags);
1330 	if (sym_reset_scsi_bus(np, 0))
1331 		goto reset_failed;
1332 
1333 	/*
1334 	 *  Start the SCRIPTS.
1335 	 */
1336 	sym_start_up(shost, 1);
1337 
1338 	/*
1339 	 *  Start the timer daemon
1340 	 */
1341 	timer_setup(&np->s.timer, sym53c8xx_timer, 0);
1342 	np->s.lasttime=0;
1343 	sym_timer (np);
1344 
1345 	/*
1346 	 *  Fill Linux host instance structure
1347 	 *  and return success.
1348 	 */
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;
1358 	BUG_ON(sym2_transport_template == NULL);
1359 	shost->transportt	= sym2_transport_template;
1360 
1361 	/* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
1362 	if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
1363 		shost->dma_boundary = 0xFFFFFF;
1364 
1365 	spin_unlock_irqrestore(shost->host_lock, flags);
1366 
1367 	return shost;
1368 
1369  reset_failed:
1370 	printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1371 		   "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1372 	spin_unlock_irqrestore(shost->host_lock, flags);
1373  attach_failed:
1374 	printf_info("sym%d: giving up ...\n", unit);
1375 	if (np)
1376 		sym_free_resources(np, pdev, do_free_irq);
1377 	else
1378 		sym_iounmap_device(dev);
1379 	if (shost)
1380 		scsi_host_put(shost);
1381 
1382 	return NULL;
1383 }
1384 
1385 
1386 /*
1387  *    Detect and try to read SYMBIOS and TEKRAM NVRAM.
1388  */
1389 #if SYM_CONF_NVRAM_SUPPORT
sym_get_nvram(struct sym_device * devp,struct sym_nvram * nvp)1390 static void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1391 {
1392 	devp->nvram = nvp;
1393 	nvp->type = 0;
1394 
1395 	sym_read_nvram(devp, nvp);
1396 }
1397 #else
sym_get_nvram(struct sym_device * devp,struct sym_nvram * nvp)1398 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1399 {
1400 }
1401 #endif	/* SYM_CONF_NVRAM_SUPPORT */
1402 
sym_check_supported(struct sym_device * device)1403 static int sym_check_supported(struct sym_device *device)
1404 {
1405 	struct sym_chip *chip;
1406 	struct pci_dev *pdev = device->pdev;
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 	 */
1426 	chip = sym_lookup_chip_table(pdev->device, pdev->revision);
1427 	if (!chip) {
1428 		dev_info(&pdev->dev, "device not supported\n");
1429 		return -ENODEV;
1430 	}
1431 	memcpy(&device->chip, chip, sizeof(device->chip));
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  */
sym_check_raid(struct sym_device * device)1440 static int sym_check_raid(struct sym_device *device)
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 
sym_set_workarounds(struct sym_device * device)1461 static int sym_set_workarounds(struct sym_device *device)
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 	 */
1472 	if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 0x4) {
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 /*
1509  * Map HBA registers and on-chip SRAM (if present).
1510  */
sym_iomap_device(struct sym_device * device)1511 static int sym_iomap_device(struct sym_device *device)
1512 {
1513 	struct pci_dev *pdev = device->pdev;
1514 	struct pci_bus_region bus_addr;
1515 	int i = 2;
1516 
1517 	pcibios_resource_to_bus(pdev->bus, &bus_addr, &pdev->resource[1]);
1518 	device->mmio_base = bus_addr.start;
1519 
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++;
1527 		pcibios_resource_to_bus(pdev->bus, &bus_addr,
1528 					&pdev->resource[i]);
1529 		device->ram_base = bus_addr.start;
1530 	}
1531 
1532 #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
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));
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) {
1545 		device->s.ramaddr = pci_iomap(pdev, i,
1546 						pci_resource_len(pdev, i));
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;
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  */
sym_config_pqs(struct pci_dev * pdev,struct sym_device * sym_dev)1570 static 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  */
sym_detach(struct Scsi_Host * shost,struct pci_dev * pdev)1610 static int sym_detach(struct Scsi_Host *shost, struct pci_dev *pdev)
1611 {
1612 	struct sym_hcb *np = sym_get_hcb(shost);
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);
1624 	INB(np, nc_mbox1);
1625 	udelay(10);
1626 	OUTB(np, nc_istat, 0);
1627 
1628 	sym_free_resources(np, pdev, 1);
1629 	scsi_host_put(shost);
1630 
1631 	return 1;
1632 }
1633 
1634 /*
1635  * Driver host template.
1636  */
1637 static 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,
1644 	.slave_destroy		= sym53c8xx_slave_destroy,
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,
1650 	.max_sectors		= 0xFFFF,
1651 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1652 	.show_info		= sym_show_info,
1653 #ifdef	SYM_LINUX_USER_COMMAND_SUPPORT
1654 	.write_info		= sym_user_command,
1655 #endif
1656 	.proc_name		= NAME53C8XX,
1657 #endif
1658 };
1659 
1660 static int attach_count;
1661 
sym2_probe(struct pci_dev * pdev,const struct pci_device_id * ent)1662 static int sym2_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1663 {
1664 	struct sym_device sym_dev;
1665 	struct sym_nvram nvram;
1666 	struct Scsi_Host *shost;
1667 	int do_iounmap = 0;
1668 	int do_disable_device = 1;
1669 
1670 	memset(&sym_dev, 0, sizeof(sym_dev));
1671 	memset(&nvram, 0, sizeof(nvram));
1672 	sym_dev.pdev = pdev;
1673 	sym_dev.host_id = SYM_SETUP_HOST_ID;
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 
1683 	if (sym_check_supported(&sym_dev))
1684 		goto free;
1685 
1686 	if (sym_iomap_device(&sym_dev))
1687 		goto free;
1688 	do_iounmap = 1;
1689 
1690 	if (sym_check_raid(&sym_dev)) {
1691 		do_disable_device = 0;	/* Don't disable the device */
1692 		goto free;
1693 	}
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 
1702 	do_iounmap = 0; /* Don't sym_iounmap_device() after sym_attach(). */
1703 	shost = sym_attach(&sym2_template, attach_count, &sym_dev);
1704 	if (!shost)
1705 		goto free;
1706 
1707 	if (scsi_add_host(shost, &pdev->dev))
1708 		goto detach;
1709 	scsi_scan_host(shost);
1710 
1711 	attach_count++;
1712 
1713 	return 0;
1714 
1715  detach:
1716 	sym_detach(pci_get_drvdata(pdev), pdev);
1717  free:
1718 	if (do_iounmap)
1719 		sym_iounmap_device(&sym_dev);
1720 	pci_release_regions(pdev);
1721  disable:
1722 	if (do_disable_device)
1723 		pci_disable_device(pdev);
1724  leave:
1725 	return -ENODEV;
1726 }
1727 
sym2_remove(struct pci_dev * pdev)1728 static void sym2_remove(struct pci_dev *pdev)
1729 {
1730 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
1731 
1732 	scsi_remove_host(shost);
1733 	sym_detach(shost, pdev);
1734 	pci_release_regions(pdev);
1735 	pci_disable_device(pdev);
1736 
1737 	attach_count--;
1738 }
1739 
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  */
sym2_io_error_detected(struct pci_dev * pdev,enum pci_channel_state state)1745 static 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  */
sym2_io_slot_dump(struct pci_dev * pdev)1765 static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
1766 {
1767 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
1768 
1769 	sym_dump_registers(shost);
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
1780  * successfully initialized at least once before, and so most
1781  * of the steps taken there are un-needed here.
1782  */
sym2_reset_workarounds(struct pci_dev * pdev)1783 static void sym2_reset_workarounds(struct pci_dev *pdev)
1784 {
1785 	u_short status_reg;
1786 	struct sym_chip *chip;
1787 
1788 	chip = sym_lookup_chip_table(pdev->device, pdev->revision);
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  */
sym2_io_slot_reset(struct pci_dev * pdev)1807 static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
1808 {
1809 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
1810 	struct sym_hcb *np = sym_get_hcb(shost);
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 		}
1840 		sym_start_up(shost, 1);
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  */
sym2_io_resume(struct pci_dev * pdev)1854 static void sym2_io_resume(struct pci_dev *pdev)
1855 {
1856 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
1857 	struct sym_data *sym_data = shost_priv(shost);
1858 
1859 	spin_lock_irq(shost->host_lock);
1860 	if (sym_data->io_reset)
1861 		complete(sym_data->io_reset);
1862 	spin_unlock_irq(shost->host_lock);
1863 }
1864 
sym2_get_signalling(struct Scsi_Host * shost)1865 static 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 
sym2_set_offset(struct scsi_target * starget,int offset)1887 static 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 
sym2_set_period(struct scsi_target * starget,int period)1897 static 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 
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))
1906 		tp->tgoal.dt = 1;
1907 
1908 	tp->tgoal.period = period;
1909 	tp->tgoal.check_nego = 1;
1910 }
1911 
sym2_set_width(struct scsi_target * starget,int width)1912 static 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 
sym2_set_dt(struct scsi_target * starget,int dt)1927 static 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 
1941 #if 0
1942 static 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 
1955 static 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 }
1967 #endif
1968 
1969 static 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,
1978 #if 0
1979 	.set_iu		= sym2_set_iu,
1980 	.show_iu	= 1,
1981 	.set_qas	= sym2_set_qas,
1982 	.show_qas	= 1,
1983 #endif
1984 	.get_signalling	= sym2_get_signalling,
1985 };
1986 
1987 static struct pci_device_id sym2_id_table[] = {
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,
2001 	  PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8,  0xffff00, 0UL },
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,
2011 	  PCI_ANY_ID, PCI_ANY_ID,  PCI_CLASS_STORAGE_SCSI<<8,  0xffff00, 0UL }, /* new */
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 
2025 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2026 
2027 static const struct pci_error_handlers sym2_err_handler = {
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 
2034 static struct pci_driver sym2_driver = {
2035 	.name		= NAME53C8XX,
2036 	.id_table	= sym2_id_table,
2037 	.probe		= sym2_probe,
2038 	.remove		= sym2_remove,
2039 	.err_handler 	= &sym2_err_handler,
2040 };
2041 
sym2_init(void)2042 static 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 
sym2_exit(void)2057 static void __exit sym2_exit(void)
2058 {
2059 	pci_unregister_driver(&sym2_driver);
2060 	spi_release_transport(sym2_transport_template);
2061 }
2062 
2063 module_init(sym2_init);
2064 module_exit(sym2_exit);
2065