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