blob: 1c18b3b9aa1504bd300459abeae056c8f6346126 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
3 * of PCI-SCSI IO processors.
4 *
5 * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
6 * Copyright (c) 2003-2005 Matthew Wilcox <matthew@wil.cx>
7 *
8 * This driver is derived from the Linux sym53c8xx driver.
9 * Copyright (C) 1998-2000 Gerard Roudier
10 *
11 * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
12 * a port of the FreeBSD ncr driver to Linux-1.2.13.
13 *
14 * The original ncr driver has been written for 386bsd and FreeBSD by
15 * Wolfgang Stanglmeier <wolf@cologne.de>
16 * Stefan Esser <se@mi.Uni-Koeln.de>
17 * Copyright (C) 1994 Wolfgang Stanglmeier
18 *
19 * Other major contributions:
20 *
21 * NVRAM detection and reading.
22 * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
23 *
24 *-----------------------------------------------------------------------------
25 *
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
30 *
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 */
40#include <linux/ctype.h>
41#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/module.h>
43#include <linux/moduleparam.h>
44#include <linux/spinlock.h>
45#include <scsi/scsi.h>
46#include <scsi/scsi_tcq.h>
47#include <scsi/scsi_device.h>
48#include <scsi/scsi_transport.h>
49
50#include "sym_glue.h"
51#include "sym_nvram.h"
52
53#define NAME53C "sym53c"
54#define NAME53C8XX "sym53c8xx"
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
57unsigned int sym_debug_flags = 0;
58
59static char *excl_string;
60static char *safe_string;
61module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
63module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
64module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
65module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
66module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
67module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
68module_param_named(verb, sym_driver_setup.verbose, byte, 0);
69module_param_named(debug, sym_debug_flags, uint, 0);
70module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
71module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
72module_param_named(excl, excl_string, charp, 0);
73module_param_named(safe, safe_string, charp, 0);
74
75MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
77MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
78MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
79MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
80MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
81MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
82MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
83MODULE_PARM_DESC(debug, "Set bits to enable debugging");
84MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
85MODULE_PARM_DESC(nvram, "Option currently not used");
86MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
87MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
88
89MODULE_LICENSE("GPL");
90MODULE_VERSION(SYM_VERSION);
91MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
92MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
93
94static void sym2_setup_params(void)
95{
96 char *p = excl_string;
97 int xi = 0;
98
99 while (p && (xi < 8)) {
100 char *next_p;
101 int val = (int) simple_strtoul(p, &next_p, 0);
102 sym_driver_setup.excludes[xi++] = val;
103 p = next_p;
104 }
105
106 if (safe_string) {
107 if (*safe_string == 'y') {
108 sym_driver_setup.max_tag = 0;
109 sym_driver_setup.burst_order = 0;
110 sym_driver_setup.scsi_led = 0;
111 sym_driver_setup.scsi_diff = 1;
112 sym_driver_setup.irq_mode = 0;
113 sym_driver_setup.scsi_bus_check = 2;
114 sym_driver_setup.host_id = 7;
115 sym_driver_setup.verbose = 2;
116 sym_driver_setup.settle_delay = 10;
117 sym_driver_setup.use_nvram = 1;
118 } else if (*safe_string != 'n') {
119 printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
120 " passed to safe option", safe_string);
121 }
122 }
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static struct scsi_transport_template *sym2_transport_template = NULL;
126
127/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * Driver private area in the SCSI command structure.
129 */
130struct sym_ucmd { /* Override the SCSI pointer structure */
Linas Vepstasd68cd752007-10-05 15:55:04 -0400131 struct completion *eh_done; /* SCSI error handling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
134#define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
135#define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
138 * Complete a pending CAM CCB.
139 */
140void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
141{
Matthew Wilcox2ba65362007-10-05 15:55:03 -0400142 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
143 BUILD_BUG_ON(sizeof(struct scsi_pointer) < sizeof(struct sym_ucmd));
144
145 if (ucmd->eh_done)
146 complete(ucmd->eh_done);
147
Matthew Wilcox39c05d12007-10-05 15:55:00 -0400148 scsi_dma_unmap(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 cmd->scsi_done(cmd);
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
153 * Tell the SCSI layer about a BUS RESET.
154 */
155void sym_xpt_async_bus_reset(struct sym_hcb *np)
156{
157 printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
158 np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
159 np->s.settle_time_valid = 1;
160 if (sym_verbose >= 2)
161 printf_info("%s: command processing suspended for %d seconds\n",
162 sym_name(np), sym_driver_setup.settle_delay);
163}
164
165/*
166 * Tell the SCSI layer about a BUS DEVICE RESET message sent.
167 */
168void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
169{
170 printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
171}
172
173/*
174 * Choose the more appropriate CAM status if
175 * the IO encountered an extended error.
176 */
177static int sym_xerr_cam_status(int cam_status, int x_status)
178{
179 if (x_status) {
180 if (x_status & XE_PARITY_ERR)
181 cam_status = DID_PARITY;
182 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
183 cam_status = DID_ERROR;
184 else if (x_status & XE_BAD_PHASE)
185 cam_status = DID_ERROR;
186 else
187 cam_status = DID_ERROR;
188 }
189 return cam_status;
190}
191
192/*
193 * Build CAM result for a failed or auto-sensed IO.
194 */
195void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
196{
197 struct scsi_cmnd *cmd = cp->cmd;
198 u_int cam_status, scsi_status, drv_status;
199
200 drv_status = 0;
201 cam_status = DID_OK;
202 scsi_status = cp->ssss_status;
203
204 if (cp->host_flags & HF_SENSE) {
205 scsi_status = cp->sv_scsi_status;
206 resid = cp->sv_resid;
207 if (sym_verbose && cp->sv_xerr_status)
208 sym_print_xerr(cmd, cp->sv_xerr_status);
209 if (cp->host_status == HS_COMPLETE &&
210 cp->ssss_status == S_GOOD &&
211 cp->xerr_status == 0) {
212 cam_status = sym_xerr_cam_status(DID_OK,
213 cp->sv_xerr_status);
214 drv_status = DRIVER_SENSE;
215 /*
216 * Bounce back the sense data to user.
217 */
218 memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
219 memcpy(cmd->sense_buffer, cp->sns_bbuf,
220 min(sizeof(cmd->sense_buffer),
221 (size_t)SYM_SNS_BBUF_LEN));
222#if 0
223 /*
224 * If the device reports a UNIT ATTENTION condition
225 * due to a RESET condition, we should consider all
226 * disconnect CCBs for this unit as aborted.
227 */
228 if (1) {
229 u_char *p;
230 p = (u_char *) cmd->sense_data;
231 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
232 sym_clear_tasks(np, DID_ABORT,
233 cp->target,cp->lun, -1);
234 }
235#endif
236 } else {
237 /*
238 * Error return from our internal request sense. This
239 * is bad: we must clear the contingent allegiance
240 * condition otherwise the device will always return
241 * BUSY. Use a big stick.
242 */
243 sym_reset_scsi_target(np, cmd->device->id);
244 cam_status = DID_ERROR;
245 }
246 } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
247 cam_status = DID_OK;
248 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
249 cam_status = DID_NO_CONNECT;
250 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
251 cam_status = DID_ERROR;
252 else { /* Extended error */
253 if (sym_verbose) {
254 sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
255 cp->host_status, cp->ssss_status,
256 cp->xerr_status);
257 }
258 /*
259 * Set the most appropriate value for CAM status.
260 */
261 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
262 }
FUJITA Tomonori938febd2007-05-26 02:31:17 +0900263 scsi_set_resid(cmd, resid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
268{
269 int segment;
FUJITA Tomonori938febd2007-05-26 02:31:17 +0900270 int use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 cp->data_len = 0;
273
Matthew Wilcox39c05d12007-10-05 15:55:00 -0400274 use_sg = scsi_dma_map(cmd);
FUJITA Tomonori938febd2007-05-26 02:31:17 +0900275 if (use_sg > 0) {
276 struct scatterlist *sg;
Matthew Wilcox 53222b92005-05-20 19:15:43 +0100277 struct sym_tcb *tp = &np->target[cp->target];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct sym_tblmove *data;
279
280 if (use_sg > SYM_CONF_MAX_SG) {
Matthew Wilcox39c05d12007-10-05 15:55:00 -0400281 scsi_dma_unmap(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -1;
283 }
284
285 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
286
FUJITA Tomonori938febd2007-05-26 02:31:17 +0900287 scsi_for_each_sg(cmd, sg, use_sg, segment) {
288 dma_addr_t baddr = sg_dma_address(sg);
289 unsigned int len = sg_dma_len(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Matthew Wilcox 53222b92005-05-20 19:15:43 +0100291 if ((len & 1) && (tp->head.wval & EWS)) {
292 len++;
293 cp->odd_byte_adjustment++;
294 }
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 sym_build_sge(np, &data[segment], baddr, len);
297 cp->data_len += len;
298 }
299 } else {
300 segment = -2;
301 }
302
303 return segment;
304}
305
306/*
307 * Queue a SCSI command.
308 */
309static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
310{
311 struct scsi_device *sdev = cmd->device;
312 struct sym_tcb *tp;
313 struct sym_lcb *lp;
314 struct sym_ccb *cp;
315 int order;
316
317 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * Retrieve the target descriptor.
319 */
320 tp = &np->target[sdev->id];
321
322 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * Select tagged/untagged.
324 */
325 lp = sym_lp(tp, sdev->lun);
326 order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
327
328 /*
329 * Queue the SCSI IO.
330 */
331 cp = sym_get_ccb(np, cmd, order);
332 if (!cp)
333 return 1; /* Means resource shortage */
334 sym_queue_scsiio(np, cmd, cp);
335 return 0;
336}
337
338/*
339 * Setup buffers and pointers that address the CDB.
340 */
341static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
342{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Matthew Wilcox 53222b92005-05-20 19:15:43 +0100345 cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
346 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 return 0;
349}
350
351/*
352 * Setup pointers that address the data and start the I/O.
353 */
354int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
355{
Matthew Wilcox44f30b0f2005-11-29 23:08:33 -0500356 u32 lastp, goalp;
357 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /*
360 * Build the CDB.
361 */
362 if (sym_setup_cdb(np, cmd, cp))
363 goto out_abort;
364
365 /*
366 * No direction means no data.
367 */
368 dir = cmd->sc_data_direction;
369 if (dir != DMA_NONE) {
370 cp->segments = sym_scatter(np, cp, cmd);
371 if (cp->segments < 0) {
Matthew Wilcox 53222b92005-05-20 19:15:43 +0100372 sym_set_cam_status(cmd, DID_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 goto out_abort;
374 }
Matthew Wilcox44f30b0f2005-11-29 23:08:33 -0500375
376 /*
377 * No segments means no data.
378 */
379 if (!cp->segments)
380 dir = DMA_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 } else {
382 cp->data_len = 0;
383 cp->segments = 0;
384 }
385
386 /*
Matthew Wilcox44f30b0f2005-11-29 23:08:33 -0500387 * Set the data pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 */
Matthew Wilcox44f30b0f2005-11-29 23:08:33 -0500389 switch (dir) {
390 case DMA_BIDIRECTIONAL:
Matthew Wilcox3fb364e2007-10-05 15:55:10 -0400391 scmd_printk(KERN_INFO, cmd, "got DMA_BIDIRECTIONAL command");
Matthew Wilcox44f30b0f2005-11-29 23:08:33 -0500392 sym_set_cam_status(cmd, DID_ERROR);
393 goto out_abort;
394 case DMA_TO_DEVICE:
395 goalp = SCRIPTA_BA(np, data_out2) + 8;
396 lastp = goalp - 8 - (cp->segments * (2*4));
397 break;
398 case DMA_FROM_DEVICE:
399 cp->host_flags |= HF_DATA_IN;
400 goalp = SCRIPTA_BA(np, data_in2) + 8;
401 lastp = goalp - 8 - (cp->segments * (2*4));
402 break;
403 case DMA_NONE:
404 default:
405 lastp = goalp = SCRIPTB_BA(np, no_data);
406 break;
407 }
408
409 /*
410 * Set all pointers values needed by SCRIPTS.
411 */
412 cp->phys.head.lastp = cpu_to_scr(lastp);
413 cp->phys.head.savep = cpu_to_scr(lastp);
414 cp->startp = cp->phys.head.savep;
415 cp->goalp = cpu_to_scr(goalp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /*
418 * When `#ifed 1', the code below makes the driver
419 * panic on the first attempt to write to a SCSI device.
420 * It is the first test we want to do after a driver
421 * change that does not seem obviously safe. :)
422 */
423#if 0
424 switch (cp->cdb_buf[0]) {
425 case 0x0A: case 0x2A: case 0xAA:
426 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
427 break;
428 default:
429 break;
430 }
431#endif
432
433 /*
434 * activate this job.
435 */
Matthew Wilcox3bea15a2006-03-28 11:03:44 -0500436 sym_put_start_queue(np, cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return 0;
438
439out_abort:
440 sym_free_ccb(np, cp);
441 sym_xpt_done(np, cmd);
442 return 0;
443}
444
445
446/*
447 * timer daemon.
448 *
449 * Misused to keep the driver running when
450 * interrupts are not configured correctly.
451 */
452static void sym_timer(struct sym_hcb *np)
453{
454 unsigned long thistime = jiffies;
455
456 /*
457 * Restart the timer.
458 */
459 np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
460 add_timer(&np->s.timer);
461
462 /*
463 * If we are resetting the ncr, wait for settle_time before
464 * clearing it. Then command processing will be resumed.
465 */
466 if (np->s.settle_time_valid) {
467 if (time_before_eq(np->s.settle_time, thistime)) {
468 if (sym_verbose >= 2 )
469 printk("%s: command processing resumed\n",
470 sym_name(np));
471 np->s.settle_time_valid = 0;
472 }
473 return;
474 }
475
476 /*
477 * Nothing to do for now, but that may come.
478 */
479 if (np->s.lasttime + 4*HZ < thistime) {
480 np->s.lasttime = thistime;
481 }
482
483#ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
484 /*
485 * Some way-broken PCI bridges may lead to
486 * completions being lost when the clearing
487 * of the INTFLY flag by the CPU occurs
488 * concurrently with the chip raising this flag.
489 * If this ever happen, lost completions will
490 * be reaped here.
491 */
492 sym_wakeup_done(np);
493#endif
494}
495
496
497/*
498 * PCI BUS error handler.
499 */
500void sym_log_bus_error(struct sym_hcb *np)
501{
502 u_short pci_sts;
503 pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
504 if (pci_sts & 0xf900) {
505 pci_write_config_word(np->s.device, PCI_STATUS, pci_sts);
506 printf("%s: PCI STATUS = 0x%04x\n",
507 sym_name(np), pci_sts & 0xf900);
508 }
509}
510
511/*
512 * queuecommand method. Entered with the host adapter lock held and
513 * interrupts disabled.
514 */
515static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
516 void (*done)(struct scsi_cmnd *))
517{
518 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
519 struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
520 int sts = 0;
521
Matthew Wilcox71c222d2007-10-05 15:55:01 -0400522 cmd->scsi_done = done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 memset(ucp, 0, sizeof(*ucp));
524
525 /*
526 * Shorten our settle_time if needed for
527 * this command not to time out.
528 */
529 if (np->s.settle_time_valid && cmd->timeout_per_command) {
530 unsigned long tlimit = jiffies + cmd->timeout_per_command;
531 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
532 if (time_after(np->s.settle_time, tlimit)) {
533 np->s.settle_time = tlimit;
534 }
535 }
536
537 if (np->s.settle_time_valid)
538 return SCSI_MLQUEUE_HOST_BUSY;
539
540 sts = sym_queue_command(np, cmd);
541 if (sts)
542 return SCSI_MLQUEUE_HOST_BUSY;
543 return 0;
544}
545
546/*
547 * Linux entry point of the interrupt handler.
548 */
David Howells7d12e782006-10-05 14:55:46 +0100549static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -0400551 struct Scsi_Host *shost = dev_id;
552 struct sym_data *sym_data = shost_priv(shost);
553 irqreturn_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linas Vepstasd68cd752007-10-05 15:55:04 -0400555 /* Avoid spinloop trying to handle interrupts on frozen device */
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -0400556 if (pci_channel_offline(sym_data->pdev))
Linas Vepstasd68cd752007-10-05 15:55:04 -0400557 return IRQ_NONE;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
560
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -0400561 spin_lock(shost->host_lock);
562 result = sym_interrupt(shost);
563 spin_unlock(shost->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
566
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -0400567 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570/*
571 * Linux entry point of the timer handler
572 */
573static void sym53c8xx_timer(unsigned long npref)
574{
575 struct sym_hcb *np = (struct sym_hcb *)npref;
576 unsigned long flags;
577
578 spin_lock_irqsave(np->s.host->host_lock, flags);
579 sym_timer(np);
580 spin_unlock_irqrestore(np->s.host->host_lock, flags);
581}
582
583
584/*
585 * What the eh thread wants us to perform.
586 */
587#define SYM_EH_ABORT 0
588#define SYM_EH_DEVICE_RESET 1
589#define SYM_EH_BUS_RESET 2
590#define SYM_EH_HOST_RESET 3
591
592/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * Generic method for our eh processing.
594 * The 'op' argument tells what we have to do.
595 */
596static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
597{
598 struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
Matthew Wilcoxb4e93a72006-03-28 11:03:44 -0500599 struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
600 struct Scsi_Host *host = cmd->device->host;
Linas Vepstasd68cd752007-10-05 15:55:04 -0400601 struct pci_dev *pdev = np->s.device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 SYM_QUEHEAD *qp;
Matthew Wilcox2ba65362007-10-05 15:55:03 -0400603 int cmd_queued = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int sts = -1;
Matthew Wilcoxd637c452006-03-29 14:45:18 -0700605 struct completion eh_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Matthew Wilcox3fb364e2007-10-05 15:55:10 -0400607 scmd_printk(KERN_WARNING, cmd, "%s operation started.\n", opname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Linas Vepstasd68cd752007-10-05 15:55:04 -0400609 /* We may be in an error condition because the PCI bus
610 * went down. In this case, we need to wait until the
611 * PCI bus is reset, the card is reset, and only then
612 * proceed with the scsi error recovery. There's no
613 * point in hurrying; take a leisurely wait.
614 */
615#define WAIT_FOR_PCI_RECOVERY 35
616 if (pci_channel_offline(pdev)) {
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -0400617 struct sym_data *sym_data = shost_priv(host);
Linas Vepstasd68cd752007-10-05 15:55:04 -0400618 struct completion *io_reset;
619 int finished_reset = 0;
620 init_completion(&eh_done);
621 spin_lock_irq(host->host_lock);
622 /* Make sure we didn't race */
623 if (pci_channel_offline(pdev)) {
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -0400624 if (!sym_data->io_reset)
625 sym_data->io_reset = &eh_done;
626 io_reset = sym_data->io_reset;
Linas Vepstasd68cd752007-10-05 15:55:04 -0400627 } else {
Linas Vepstasd68cd752007-10-05 15:55:04 -0400628 finished_reset = 1;
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -0400629 }
Linas Vepstasd68cd752007-10-05 15:55:04 -0400630 spin_unlock_irq(host->host_lock);
631 if (!finished_reset)
632 finished_reset = wait_for_completion_timeout(io_reset,
633 WAIT_FOR_PCI_RECOVERY*HZ);
634 if (!finished_reset)
635 return SCSI_FAILED;
636 }
637
Matthew Wilcoxb4e93a72006-03-28 11:03:44 -0500638 spin_lock_irq(host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* This one is queued in some place -> to wait for completion */
640 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
641 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
642 if (cp->cmd == cmd) {
Matthew Wilcox2ba65362007-10-05 15:55:03 -0400643 cmd_queued = 1;
Matthew Wilcoxab19d522006-03-28 11:03:44 -0500644 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 }
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* Try to proceed the operation we have been asked for */
649 sts = -1;
650 switch(op) {
651 case SYM_EH_ABORT:
652 sts = sym_abort_scsiio(np, cmd, 1);
653 break;
654 case SYM_EH_DEVICE_RESET:
655 sts = sym_reset_scsi_target(np, cmd->device->id);
656 break;
657 case SYM_EH_BUS_RESET:
658 sym_reset_scsi_bus(np, 1);
659 sts = 0;
660 break;
661 case SYM_EH_HOST_RESET:
662 sym_reset_scsi_bus(np, 0);
Linas Vepstasd68cd752007-10-05 15:55:04 -0400663 sym_start_up(np, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 sts = 0;
665 break;
666 default:
667 break;
668 }
669
670 /* On error, restore everything and cross fingers :) */
Matthew Wilcox2ba65362007-10-05 15:55:03 -0400671 if (sts)
672 cmd_queued = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Matthew Wilcox2ba65362007-10-05 15:55:03 -0400674 if (cmd_queued) {
675 init_completion(&eh_done);
676 ucmd->eh_done = &eh_done;
677 spin_unlock_irq(host->host_lock);
Matthew Wilcoxd637c452006-03-29 14:45:18 -0700678 if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
Matthew Wilcox2ba65362007-10-05 15:55:03 -0400679 ucmd->eh_done = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 sts = -2;
Matthew Wilcoxb4e93a72006-03-28 11:03:44 -0500681 }
Matthew Wilcox2ba65362007-10-05 15:55:03 -0400682 } else {
683 spin_unlock_irq(host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Matthew Wilcox2ba65362007-10-05 15:55:03 -0400685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
687 sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
688 return sts ? SCSI_FAILED : SCSI_SUCCESS;
689}
690
691
692/*
693 * Error handlers called from the eh thread (one thread per HBA).
694 */
695static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
696{
Matthew Wilcoxab19d522006-03-28 11:03:44 -0500697 return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
701{
Matthew Wilcoxab19d522006-03-28 11:03:44 -0500702 return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
706{
Matthew Wilcoxab19d522006-03-28 11:03:44 -0500707 return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
711{
Matthew Wilcoxab19d522006-03-28 11:03:44 -0500712 return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715/*
716 * Tune device queuing depth, according to various limits.
717 */
718static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
719{
720 struct sym_lcb *lp = sym_lp(tp, lun);
721 u_short oldtags;
722
723 if (!lp)
724 return;
725
726 oldtags = lp->s.reqtags;
727
728 if (reqtags > lp->s.scdev_depth)
729 reqtags = lp->s.scdev_depth;
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 lp->s.reqtags = reqtags;
732
733 if (reqtags != oldtags) {
Matthew Wilcox 53222b92005-05-20 19:15:43 +0100734 dev_info(&tp->starget->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 "tagged command queuing %s, command queue depth %d.\n",
Matthew Wilcox3bea15a2006-03-28 11:03:44 -0500736 lp->s.reqtags ? "enabled" : "disabled", reqtags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738}
739
Matthew Wilcox 53222b92005-05-20 19:15:43 +0100740static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Matthew Wilcox84e203a2005-11-29 23:08:31 -0500742 struct sym_hcb *np = sym_get_hcb(sdev->host);
743 struct sym_tcb *tp = &np->target[sdev->id];
744 struct sym_lcb *lp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Matthew Wilcox 53222b92005-05-20 19:15:43 +0100746 if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
747 return -ENXIO;
748
Matthew Wilcox66e8d1c2005-11-29 23:08:46 -0500749 tp->starget = sdev->sdev_target;
Matthew Wilcox 53222b92005-05-20 19:15:43 +0100750 /*
751 * Fail the device init if the device is flagged NOSCAN at BOOT in
752 * the NVRAM. This may speed up boot and maintain coherency with
753 * BIOS device numbering. Clearing the flag allows the user to
754 * rescan skipped devices later. We also return an error for
755 * devices not flagged for SCAN LUNS in the NVRAM since some single
756 * lun devices behave badly when asked for a non zero LUN.
757 */
758
Matthew Wilcox66e8d1c2005-11-29 23:08:46 -0500759 if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
Matthew Wilcox 53222b92005-05-20 19:15:43 +0100760 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
Matthew Wilcox66e8d1c2005-11-29 23:08:46 -0500761 starget_printk(KERN_INFO, tp->starget,
762 "Scan at boot disabled in NVRAM\n");
Matthew Wilcox 53222b92005-05-20 19:15:43 +0100763 return -ENXIO;
764 }
765
Matthew Wilcox66e8d1c2005-11-29 23:08:46 -0500766 if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
767 if (sdev->lun != 0)
768 return -ENXIO;
769 starget_printk(KERN_INFO, tp->starget,
770 "Multiple LUNs disabled in NVRAM\n");
771 }
772
Matthew Wilcox84e203a2005-11-29 23:08:31 -0500773 lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
774 if (!lp)
775 return -ENOMEM;
776
Matthew Wilcoxb37df482005-11-29 23:08:44 -0500777 spi_min_period(tp->starget) = tp->usr_period;
778 spi_max_width(tp->starget) = tp->usr_width;
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return 0;
781}
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783/*
784 * Linux entry point for device queue sizing.
785 */
Matthew Wilcox84e203a2005-11-29 23:08:31 -0500786static int sym53c8xx_slave_configure(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Matthew Wilcox84e203a2005-11-29 23:08:31 -0500788 struct sym_hcb *np = sym_get_hcb(sdev->host);
789 struct sym_tcb *tp = &np->target[sdev->id];
790 struct sym_lcb *lp = sym_lp(tp, sdev->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 int reqtags, depth_to_use;
792
793 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 * Get user flags.
795 */
796 lp->curr_flags = lp->user_flags;
797
798 /*
799 * Select queue depth from driver setup.
800 * Donnot use more than configured by user.
801 * Use at least 2.
802 * Donnot use more than our maximum.
803 */
Matthew Wilcoxa44131b2007-10-05 15:55:08 -0400804 reqtags = sym_driver_setup.max_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (reqtags > tp->usrtags)
806 reqtags = tp->usrtags;
Matthew Wilcox84e203a2005-11-29 23:08:31 -0500807 if (!sdev->tagged_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 reqtags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (reqtags > SYM_CONF_MAX_TAG)
810 reqtags = SYM_CONF_MAX_TAG;
Matthew Wilcoxa44131b2007-10-05 15:55:08 -0400811 depth_to_use = reqtags ? reqtags : 2;
Matthew Wilcox84e203a2005-11-29 23:08:31 -0500812 scsi_adjust_queue_depth(sdev,
Matthew Wilcoxa44131b2007-10-05 15:55:08 -0400813 sdev->tagged_supported ? MSG_SIMPLE_TAG : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 depth_to_use);
815 lp->s.scdev_depth = depth_to_use;
Matthew Wilcox84e203a2005-11-29 23:08:31 -0500816 sym_tune_dev_queuing(tp, sdev->lun, reqtags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Matthew Wilcox84e203a2005-11-29 23:08:31 -0500818 if (!spi_initial_dv(sdev->sdev_target))
819 spi_dv_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 return 0;
822}
823
Matthew Wilcox84e203a2005-11-29 23:08:31 -0500824static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
825{
826 struct sym_hcb *np = sym_get_hcb(sdev->host);
827 struct sym_lcb *lp = sym_lp(&np->target[sdev->id], sdev->lun);
828
829 if (lp->itlq_tbl)
830 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK * 4, "ITLQ_TBL");
831 kfree(lp->cb_tags);
832 sym_mfree_dma(lp, sizeof(*lp), "LCB");
833}
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835/*
836 * Linux entry point for info() function
837 */
838static const char *sym53c8xx_info (struct Scsi_Host *host)
839{
840 return SYM_DRIVER_NAME;
841}
842
843
844#ifdef SYM_LINUX_PROC_INFO_SUPPORT
845/*
846 * Proc file system stuff
847 *
848 * A read operation returns adapter information.
849 * A write operation is a control command.
850 * The string is parsed in the driver code and the command is passed
851 * to the sym_usercmd() function.
852 */
853
854#ifdef SYM_LINUX_USER_COMMAND_SUPPORT
855
856struct sym_usrcmd {
857 u_long target;
858 u_long lun;
859 u_long data;
860 u_long cmd;
861};
862
863#define UC_SETSYNC 10
864#define UC_SETTAGS 11
865#define UC_SETDEBUG 12
866#define UC_SETWIDE 14
867#define UC_SETFLAG 15
868#define UC_SETVERBOSE 17
869#define UC_RESETDEV 18
870#define UC_CLEARDEV 19
871
872static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
873{
874 struct sym_tcb *tp;
875 int t, l;
876
877 switch (uc->cmd) {
878 case 0: return;
879
880#ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
881 case UC_SETDEBUG:
882 sym_debug_flags = uc->data;
883 break;
884#endif
885 case UC_SETVERBOSE:
886 np->verbose = uc->data;
887 break;
888 default:
889 /*
890 * We assume that other commands apply to targets.
891 * This should always be the case and avoid the below
892 * 4 lines to be repeated 6 times.
893 */
894 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
895 if (!((uc->target >> t) & 1))
896 continue;
897 tp = &np->target[t];
898
899 switch (uc->cmd) {
900
901 case UC_SETSYNC:
902 if (!uc->data || uc->data >= 255) {
903 tp->tgoal.iu = tp->tgoal.dt =
904 tp->tgoal.qas = 0;
905 tp->tgoal.offset = 0;
906 } else if (uc->data <= 9 && np->minsync_dt) {
907 if (uc->data < np->minsync_dt)
908 uc->data = np->minsync_dt;
909 tp->tgoal.iu = tp->tgoal.dt =
910 tp->tgoal.qas = 1;
911 tp->tgoal.width = 1;
912 tp->tgoal.period = uc->data;
913 tp->tgoal.offset = np->maxoffs_dt;
914 } else {
915 if (uc->data < np->minsync)
916 uc->data = np->minsync;
917 tp->tgoal.iu = tp->tgoal.dt =
918 tp->tgoal.qas = 0;
919 tp->tgoal.period = uc->data;
920 tp->tgoal.offset = np->maxoffs;
921 }
922 tp->tgoal.check_nego = 1;
923 break;
924 case UC_SETWIDE:
925 tp->tgoal.width = uc->data ? 1 : 0;
926 tp->tgoal.check_nego = 1;
927 break;
928 case UC_SETTAGS:
929 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
930 sym_tune_dev_queuing(tp, l, uc->data);
931 break;
932 case UC_RESETDEV:
933 tp->to_reset = 1;
934 np->istat_sem = SEM;
935 OUTB(np, nc_istat, SIGP|SEM);
936 break;
937 case UC_CLEARDEV:
938 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
939 struct sym_lcb *lp = sym_lp(tp, l);
940 if (lp) lp->to_clear = 1;
941 }
942 np->istat_sem = SEM;
943 OUTB(np, nc_istat, SIGP|SEM);
944 break;
945 case UC_SETFLAG:
946 tp->usrflags = uc->data;
947 break;
948 }
949 }
950 break;
951 }
952}
953
954static int skip_spaces(char *ptr, int len)
955{
956 int cnt, c;
957
958 for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
959
960 return (len - cnt);
961}
962
963static int get_int_arg(char *ptr, int len, u_long *pv)
964{
965 char *end;
966
967 *pv = simple_strtoul(ptr, &end, 10);
968 return (end - ptr);
969}
970
971static int is_keyword(char *ptr, int len, char *verb)
972{
973 int verb_len = strlen(verb);
974
975 if (len >= verb_len && !memcmp(verb, ptr, verb_len))
976 return verb_len;
977 else
978 return 0;
979}
980
981#define SKIP_SPACES(ptr, len) \
982 if ((arg_len = skip_spaces(ptr, len)) < 1) \
983 return -EINVAL; \
984 ptr += arg_len; len -= arg_len;
985
986#define GET_INT_ARG(ptr, len, v) \
987 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
988 return -EINVAL; \
989 ptr += arg_len; len -= arg_len;
990
991
992/*
993 * Parse a control command
994 */
995
996static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
997{
998 char *ptr = buffer;
999 int len = length;
1000 struct sym_usrcmd cmd, *uc = &cmd;
1001 int arg_len;
1002 u_long target;
1003
1004 memset(uc, 0, sizeof(*uc));
1005
1006 if (len > 0 && ptr[len-1] == '\n')
1007 --len;
1008
1009 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1010 uc->cmd = UC_SETSYNC;
1011 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1012 uc->cmd = UC_SETTAGS;
1013 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1014 uc->cmd = UC_SETVERBOSE;
1015 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1016 uc->cmd = UC_SETWIDE;
1017#ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1018 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1019 uc->cmd = UC_SETDEBUG;
1020#endif
1021 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1022 uc->cmd = UC_SETFLAG;
1023 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1024 uc->cmd = UC_RESETDEV;
1025 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1026 uc->cmd = UC_CLEARDEV;
1027 else
1028 arg_len = 0;
1029
1030#ifdef DEBUG_PROC_INFO
1031printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1032#endif
1033
1034 if (!arg_len)
1035 return -EINVAL;
1036 ptr += arg_len; len -= arg_len;
1037
1038 switch(uc->cmd) {
1039 case UC_SETSYNC:
1040 case UC_SETTAGS:
1041 case UC_SETWIDE:
1042 case UC_SETFLAG:
1043 case UC_RESETDEV:
1044 case UC_CLEARDEV:
1045 SKIP_SPACES(ptr, len);
1046 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1047 ptr += arg_len; len -= arg_len;
1048 uc->target = ~0;
1049 } else {
1050 GET_INT_ARG(ptr, len, target);
1051 uc->target = (1<<target);
1052#ifdef DEBUG_PROC_INFO
1053printk("sym_user_command: target=%ld\n", target);
1054#endif
1055 }
1056 break;
1057 }
1058
1059 switch(uc->cmd) {
1060 case UC_SETVERBOSE:
1061 case UC_SETSYNC:
1062 case UC_SETTAGS:
1063 case UC_SETWIDE:
1064 SKIP_SPACES(ptr, len);
1065 GET_INT_ARG(ptr, len, uc->data);
1066#ifdef DEBUG_PROC_INFO
1067printk("sym_user_command: data=%ld\n", uc->data);
1068#endif
1069 break;
1070#ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1071 case UC_SETDEBUG:
1072 while (len > 0) {
1073 SKIP_SPACES(ptr, len);
1074 if ((arg_len = is_keyword(ptr, len, "alloc")))
1075 uc->data |= DEBUG_ALLOC;
1076 else if ((arg_len = is_keyword(ptr, len, "phase")))
1077 uc->data |= DEBUG_PHASE;
1078 else if ((arg_len = is_keyword(ptr, len, "queue")))
1079 uc->data |= DEBUG_QUEUE;
1080 else if ((arg_len = is_keyword(ptr, len, "result")))
1081 uc->data |= DEBUG_RESULT;
1082 else if ((arg_len = is_keyword(ptr, len, "scatter")))
1083 uc->data |= DEBUG_SCATTER;
1084 else if ((arg_len = is_keyword(ptr, len, "script")))
1085 uc->data |= DEBUG_SCRIPT;
1086 else if ((arg_len = is_keyword(ptr, len, "tiny")))
1087 uc->data |= DEBUG_TINY;
1088 else if ((arg_len = is_keyword(ptr, len, "timing")))
1089 uc->data |= DEBUG_TIMING;
1090 else if ((arg_len = is_keyword(ptr, len, "nego")))
1091 uc->data |= DEBUG_NEGO;
1092 else if ((arg_len = is_keyword(ptr, len, "tags")))
1093 uc->data |= DEBUG_TAGS;
1094 else if ((arg_len = is_keyword(ptr, len, "pointer")))
1095 uc->data |= DEBUG_POINTER;
1096 else
1097 return -EINVAL;
1098 ptr += arg_len; len -= arg_len;
1099 }
1100#ifdef DEBUG_PROC_INFO
1101printk("sym_user_command: data=%ld\n", uc->data);
1102#endif
1103 break;
1104#endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1105 case UC_SETFLAG:
1106 while (len > 0) {
1107 SKIP_SPACES(ptr, len);
1108 if ((arg_len = is_keyword(ptr, len, "no_disc")))
1109 uc->data &= ~SYM_DISC_ENABLED;
1110 else
1111 return -EINVAL;
1112 ptr += arg_len; len -= arg_len;
1113 }
1114 break;
1115 default:
1116 break;
1117 }
1118
1119 if (len)
1120 return -EINVAL;
1121 else {
1122 unsigned long flags;
1123
1124 spin_lock_irqsave(np->s.host->host_lock, flags);
1125 sym_exec_user_command (np, uc);
1126 spin_unlock_irqrestore(np->s.host->host_lock, flags);
1127 }
1128 return length;
1129}
1130
1131#endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
1132
1133
1134#ifdef SYM_LINUX_USER_INFO_SUPPORT
1135/*
1136 * Informations through the proc file system.
1137 */
1138struct info_str {
1139 char *buffer;
1140 int length;
1141 int offset;
1142 int pos;
1143};
1144
1145static void copy_mem_info(struct info_str *info, char *data, int len)
1146{
1147 if (info->pos + len > info->length)
1148 len = info->length - info->pos;
1149
1150 if (info->pos + len < info->offset) {
1151 info->pos += len;
1152 return;
1153 }
1154 if (info->pos < info->offset) {
1155 data += (info->offset - info->pos);
1156 len -= (info->offset - info->pos);
1157 }
1158
1159 if (len > 0) {
1160 memcpy(info->buffer + info->pos, data, len);
1161 info->pos += len;
1162 }
1163}
1164
1165static int copy_info(struct info_str *info, char *fmt, ...)
1166{
1167 va_list args;
1168 char buf[81];
1169 int len;
1170
1171 va_start(args, fmt);
1172 len = vsprintf(buf, fmt, args);
1173 va_end(args);
1174
1175 copy_mem_info(info, buf, len);
1176 return len;
1177}
1178
1179/*
1180 * Copy formatted information into the input buffer.
1181 */
1182static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
1183{
1184 struct info_str info;
1185
1186 info.buffer = ptr;
1187 info.length = len;
1188 info.offset = offset;
1189 info.pos = 0;
1190
1191 copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
Matthew Wilcoxbd678452007-10-05 15:55:05 -04001192 "revision id 0x%x\n", np->s.chip_name,
Matthew Wilcoxe58bc062007-10-05 15:55:06 -04001193 np->s.device->device, np->s.device->revision);
Matthew Wilcox8022fbd2007-10-05 15:55:11 -04001194 copy_info(&info, "At PCI address %s, IRQ %u\n",
1195 pci_name(np->s.device), np->s.device->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1197 (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1198 np->maxwide ? "Wide" : "Narrow",
1199 np->minsync_dt ? ", DT capable" : "");
1200
1201 copy_info(&info, "Max. started commands %d, "
1202 "max. commands per LUN %d\n",
1203 SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1204
1205 return info.pos > info.offset? info.pos - info.offset : 0;
1206}
1207#endif /* SYM_LINUX_USER_INFO_SUPPORT */
1208
1209/*
1210 * Entry point of the scsi proc fs of the driver.
1211 * - func = 0 means read (returns adapter infos)
1212 * - func = 1 means write (not yet merget from sym53c8xx)
1213 */
1214static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
1215 char **start, off_t offset, int length, int func)
1216{
1217 struct sym_hcb *np = sym_get_hcb(host);
1218 int retv;
1219
1220 if (func) {
1221#ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1222 retv = sym_user_command(np, buffer, length);
1223#else
1224 retv = -EINVAL;
1225#endif
1226 } else {
1227 if (start)
1228 *start = buffer;
1229#ifdef SYM_LINUX_USER_INFO_SUPPORT
1230 retv = sym_host_info(np, buffer, offset, length);
1231#else
1232 retv = -EINVAL;
1233#endif
1234 }
1235
1236 return retv;
1237}
1238#endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1239
1240/*
1241 * Free controller resources.
1242 */
1243static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
1244{
1245 /*
1246 * Free O/S specific resources.
1247 */
Matthew Wilcoxf363abf2007-10-05 15:54:59 -04001248 if (pdev->irq)
1249 free_irq(pdev->irq, np);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (np->s.ioaddr)
1251 pci_iounmap(pdev, np->s.ioaddr);
1252 if (np->s.ramaddr)
1253 pci_iounmap(pdev, np->s.ramaddr);
1254 /*
1255 * Free O/S independent resources.
1256 */
1257 sym_hcb_free(np);
1258
1259 sym_mfree_dma(np, sizeof(*np), "HCB");
1260}
1261
1262/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 * Host attach and initialisations.
1264 *
1265 * Allocate host data and ncb structure.
1266 * Remap MMIO region.
1267 * Do chip initialization.
1268 * If all is OK, install interrupt handling and
1269 * start the timer daemon.
1270 */
1271static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
1272 int unit, struct sym_device *dev)
1273{
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001274 struct sym_data *sym_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 struct sym_hcb *np = NULL;
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001276 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 struct pci_dev *pdev = dev->pdev;
1278 unsigned long flags;
1279 struct sym_fw *fw;
1280
Matthew Wilcox8022fbd2007-10-05 15:55:11 -04001281 printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq %u\n",
Matthew Wilcoxbd678452007-10-05 15:55:05 -04001282 unit, dev->chip.name, pdev->revision, pci_name(pdev),
Matthew Wilcox8022fbd2007-10-05 15:55:11 -04001283 pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /*
1286 * Get the firmware for this chip.
1287 */
1288 fw = sym_find_firmware(&dev->chip);
1289 if (!fw)
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001290 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001292 shost = scsi_host_alloc(tpnt, sizeof(*sym_data));
1293 if (!shost)
1294 return NULL;
1295 sym_data = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /*
1298 * Allocate immediately the host control block,
1299 * since we are only expecting to succeed. :)
1300 * We keep track in the HCB of all the resources that
1301 * are to be released on error.
1302 */
1303 np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
1304 if (!np)
1305 goto attach_failed;
1306 np->s.device = pdev;
1307 np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001308 sym_data->ncb = np;
1309 sym_data->pdev = pdev;
1310 np->s.host = shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 pci_set_drvdata(pdev, np);
1313
1314 /*
1315 * Copy some useful infos to the HCB.
1316 */
1317 np->hcb_ba = vtobus(np);
1318 np->verbose = sym_driver_setup.verbose;
1319 np->s.device = pdev;
1320 np->s.unit = unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 np->features = dev->chip.features;
1322 np->clock_divn = dev->chip.nr_divisor;
1323 np->maxoffs = dev->chip.offset_max;
1324 np->maxburst = dev->chip.burst_max;
1325 np->myaddr = dev->host_id;
1326
1327 /*
1328 * Edit its name.
1329 */
1330 strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1331 sprintf(np->s.inst_name, "sym%d", np->s.unit);
1332
Matthew Wilcox4d85b472007-10-05 15:55:09 -04001333 if ((SYM_CONF_DMA_ADDRESSING_MODE > 0) && (np->features & FE_DAC) &&
1334 !pci_set_dma_mask(np->s.device, DMA_DAC_MASK)) {
1335 set_dac(np);
1336 } else if (pci_set_dma_mask(np->s.device, DMA_32BIT_MASK)) {
1337 printf_warning("%s: No suitable DMA available\n", sym_name(np));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 goto attach_failed;
Matthew Wilcox4d85b472007-10-05 15:55:09 -04001339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 /*
1342 * Try to map the controller chip to
1343 * virtual and physical memory.
1344 */
1345 np->mmio_ba = (u32)dev->mmio_base;
1346 np->s.ioaddr = dev->s.ioaddr;
1347 np->s.ramaddr = dev->s.ramaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 /*
1350 * Map on-chip RAM if present and supported.
1351 */
1352 if (!(np->features & FE_RAM))
1353 dev->ram_base = 0;
Matthew Wilcox8637baa2007-10-05 15:55:07 -04001354 if (dev->ram_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 np->ram_ba = (u32)dev->ram_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001357 if (sym_hcb_attach(shost, fw, dev->nvram))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 goto attach_failed;
1359
1360 /*
1361 * Install the interrupt handler.
1362 * If we synchonize the C code with SCRIPTS on interrupt,
1363 * we do not want to share the INTR line at all.
1364 */
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001365 if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX,
1366 shost)) {
Matthew Wilcox8022fbd2007-10-05 15:55:11 -04001367 printf_err("%s: request irq %u failure\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 sym_name(np), pdev->irq);
1369 goto attach_failed;
1370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /*
1373 * After SCSI devices have been opened, we cannot
1374 * reset the bus safely, so we do it here.
1375 */
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001376 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (sym_reset_scsi_bus(np, 0))
1378 goto reset_failed;
1379
1380 /*
1381 * Start the SCRIPTS.
1382 */
Linas Vepstasd68cd752007-10-05 15:55:04 -04001383 sym_start_up(np, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /*
1386 * Start the timer daemon
1387 */
1388 init_timer(&np->s.timer);
1389 np->s.timer.data = (unsigned long) np;
1390 np->s.timer.function = sym53c8xx_timer;
1391 np->s.lasttime=0;
1392 sym_timer (np);
1393
1394 /*
1395 * Fill Linux host instance structure
1396 * and return success.
1397 */
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001398 shost->max_channel = 0;
1399 shost->this_id = np->myaddr;
1400 shost->max_id = np->maxwide ? 16 : 8;
1401 shost->max_lun = SYM_CONF_MAX_LUN;
1402 shost->unique_id = pci_resource_start(pdev, 0);
1403 shost->cmd_per_lun = SYM_CONF_MAX_TAG;
1404 shost->can_queue = (SYM_CONF_MAX_START-2);
1405 shost->sg_tablesize = SYM_CONF_MAX_SG;
1406 shost->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 BUG_ON(sym2_transport_template == NULL);
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001408 shost->transportt = sym2_transport_template;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Kai Makisara34996ac2007-10-05 15:54:58 -04001410 /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
Matthew Wilcoxbd678452007-10-05 15:55:05 -04001411 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001412 shost->dma_boundary = 0xFFFFFF;
Kai Makisara34996ac2007-10-05 15:54:58 -04001413
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001414 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001416 return shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 reset_failed:
1419 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1420 "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001421 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 attach_failed:
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001423 if (!shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return NULL;
1425 printf_info("%s: giving up ...\n", sym_name(np));
1426 if (np)
1427 sym_free_resources(np, pdev);
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001428 scsi_host_put(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 return NULL;
1431 }
1432
1433
1434/*
1435 * Detect and try to read SYMBIOS and TEKRAM NVRAM.
1436 */
1437#if SYM_CONF_NVRAM_SUPPORT
1438static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1439{
1440 devp->nvram = nvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 nvp->type = 0;
1442
1443 sym_read_nvram(devp, nvp);
1444}
1445#else
1446static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1447{
1448}
1449#endif /* SYM_CONF_NVRAM_SUPPORT */
1450
1451static int __devinit sym_check_supported(struct sym_device *device)
1452{
1453 struct sym_chip *chip;
1454 struct pci_dev *pdev = device->pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 unsigned long io_port = pci_resource_start(pdev, 0);
1456 int i;
1457
1458 /*
1459 * If user excluded this chip, do not initialize it.
1460 * I hate this code so much. Must kill it.
1461 */
1462 if (io_port) {
1463 for (i = 0 ; i < 8 ; i++) {
1464 if (sym_driver_setup.excludes[i] == io_port)
1465 return -ENODEV;
1466 }
1467 }
1468
1469 /*
1470 * Check if the chip is supported. Then copy the chip description
1471 * to our device structure so we can make it match the actual device
1472 * and options.
1473 */
Matthew Wilcoxbd678452007-10-05 15:55:05 -04001474 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (!chip) {
1476 dev_info(&pdev->dev, "device not supported\n");
1477 return -ENODEV;
1478 }
1479 memcpy(&device->chip, chip, sizeof(device->chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 return 0;
1482}
1483
1484/*
1485 * Ignore Symbios chips controlled by various RAID controllers.
1486 * These controllers set value 0x52414944 at RAM end - 16.
1487 */
1488static int __devinit sym_check_raid(struct sym_device *device)
1489{
1490 unsigned int ram_size, ram_val;
1491
1492 if (!device->s.ramaddr)
1493 return 0;
1494
1495 if (device->chip.features & FE_RAM8K)
1496 ram_size = 8192;
1497 else
1498 ram_size = 4096;
1499
1500 ram_val = readl(device->s.ramaddr + ram_size - 16);
1501 if (ram_val != 0x52414944)
1502 return 0;
1503
1504 dev_info(&device->pdev->dev,
1505 "not initializing, driven by RAID controller.\n");
1506 return -ENODEV;
1507}
1508
1509static int __devinit sym_set_workarounds(struct sym_device *device)
1510{
1511 struct sym_chip *chip = &device->chip;
1512 struct pci_dev *pdev = device->pdev;
1513 u_short status_reg;
1514
1515 /*
1516 * (ITEM 12 of a DEL about the 896 I haven't yet).
1517 * We must ensure the chip will use WRITE AND INVALIDATE.
1518 * The revision number limit is for now arbitrary.
1519 */
Matthew Wilcoxbd678452007-10-05 15:55:05 -04001520 if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 0x4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 chip->features |= (FE_WRIE | FE_CLSE);
1522 }
1523
1524 /* If the chip can do Memory Write Invalidate, enable it */
1525 if (chip->features & FE_WRIE) {
1526 if (pci_set_mwi(pdev))
1527 return -ENODEV;
1528 }
1529
1530 /*
1531 * Work around for errant bit in 895A. The 66Mhz
1532 * capable bit is set erroneously. Clear this bit.
1533 * (Item 1 DEL 533)
1534 *
1535 * Make sure Config space and Features agree.
1536 *
1537 * Recall: writes are not normal to status register -
1538 * write a 1 to clear and a 0 to leave unchanged.
1539 * Can only reset bits.
1540 */
1541 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1542 if (chip->features & FE_66MHZ) {
1543 if (!(status_reg & PCI_STATUS_66MHZ))
1544 chip->features &= ~FE_66MHZ;
1545 } else {
1546 if (status_reg & PCI_STATUS_66MHZ) {
1547 status_reg = PCI_STATUS_66MHZ;
1548 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1549 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1550 }
1551 }
1552
1553 return 0;
1554}
1555
1556/*
1557 * Read and check the PCI configuration for any detected NCR
1558 * boards and save data for attaching after all boards have
1559 * been detected.
1560 */
1561static void __devinit
1562sym_init_device(struct pci_dev *pdev, struct sym_device *device)
1563{
Matthew Wilcoxb6d105d2006-03-28 11:03:44 -05001564 int i = 2;
1565 struct pci_bus_region bus_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 device->host_id = SYM_SETUP_HOST_ID;
1568 device->pdev = pdev;
1569
Matthew Wilcoxb6d105d2006-03-28 11:03:44 -05001570 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
1571 device->mmio_base = bus_addr.start;
1572
1573 /*
1574 * If the BAR is 64-bit, resource 2 will be occupied by the
1575 * upper 32 bits
1576 */
1577 if (!pdev->resource[i].flags)
1578 i++;
1579 pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
1580 device->ram_base = bus_addr.start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Matthew Wilcox1f61d822006-03-28 11:03:43 -05001582#ifdef CONFIG_SCSI_SYM53C8XX_MMIO
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (device->mmio_base)
1584 device->s.ioaddr = pci_iomap(pdev, 1,
1585 pci_resource_len(pdev, 1));
1586#endif
1587 if (!device->s.ioaddr)
1588 device->s.ioaddr = pci_iomap(pdev, 0,
1589 pci_resource_len(pdev, 0));
1590 if (device->ram_base)
1591 device->s.ramaddr = pci_iomap(pdev, i,
1592 pci_resource_len(pdev, i));
1593}
1594
1595/*
1596 * The NCR PQS and PDS cards are constructed as a DEC bridge
1597 * behind which sits a proprietary NCR memory controller and
1598 * either four or two 53c875s as separate devices. We can tell
1599 * if an 875 is part of a PQS/PDS or not since if it is, it will
1600 * be on the same bus as the memory controller. In its usual
1601 * mode of operation, the 875s are slaved to the memory
1602 * controller for all transfers. To operate with the Linux
1603 * driver, the memory controller is disabled and the 875s
1604 * freed to function independently. The only wrinkle is that
1605 * the preset SCSI ID (which may be zero) must be read in from
1606 * a special configuration space register of the 875.
1607 */
1608static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
1609{
1610 int slot;
1611 u8 tmp;
1612
1613 for (slot = 0; slot < 256; slot++) {
1614 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
1615
1616 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
1617 pci_dev_put(memc);
1618 continue;
1619 }
1620
1621 /* bit 1: allow individual 875 configuration */
1622 pci_read_config_byte(memc, 0x44, &tmp);
1623 if ((tmp & 0x2) == 0) {
1624 tmp |= 0x2;
1625 pci_write_config_byte(memc, 0x44, tmp);
1626 }
1627
1628 /* bit 2: drive individual 875 interrupts to the bus */
1629 pci_read_config_byte(memc, 0x45, &tmp);
1630 if ((tmp & 0x4) == 0) {
1631 tmp |= 0x4;
1632 pci_write_config_byte(memc, 0x45, tmp);
1633 }
1634
1635 pci_dev_put(memc);
1636 break;
1637 }
1638
1639 pci_read_config_byte(pdev, 0x84, &tmp);
1640 sym_dev->host_id = tmp;
1641}
1642
1643/*
1644 * Called before unloading the module.
1645 * Detach the host.
1646 * We have to free resources and halt the NCR chip.
1647 */
1648static int sym_detach(struct sym_hcb *np, struct pci_dev *pdev)
1649{
1650 printk("%s: detaching ...\n", sym_name(np));
1651
1652 del_timer_sync(&np->s.timer);
1653
1654 /*
1655 * Reset NCR chip.
1656 * We should use sym_soft_reset(), but we don't want to do
1657 * so, since we may not be safe if interrupts occur.
1658 */
1659 printk("%s: resetting chip\n", sym_name(np));
1660 OUTB(np, nc_istat, SRST);
Matthew Wilcox 53222b92005-05-20 19:15:43 +01001661 INB(np, nc_mbox1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 udelay(10);
1663 OUTB(np, nc_istat, 0);
1664
1665 sym_free_resources(np, pdev);
1666
1667 return 1;
1668}
1669
1670/*
1671 * Driver host template.
1672 */
1673static struct scsi_host_template sym2_template = {
1674 .module = THIS_MODULE,
1675 .name = "sym53c8xx",
1676 .info = sym53c8xx_info,
1677 .queuecommand = sym53c8xx_queue_command,
1678 .slave_alloc = sym53c8xx_slave_alloc,
1679 .slave_configure = sym53c8xx_slave_configure,
Matthew Wilcox84e203a2005-11-29 23:08:31 -05001680 .slave_destroy = sym53c8xx_slave_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 .eh_abort_handler = sym53c8xx_eh_abort_handler,
1682 .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
1683 .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
1684 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
1685 .this_id = 7,
Matthew Wilcox14ac8bf2006-03-28 11:03:44 -05001686 .use_clustering = ENABLE_CLUSTERING,
FUJITA Tomonori9cb83c72007-10-16 11:24:32 +02001687 .use_sg_chaining = ENABLE_SG_CHAINING,
Matthew Wilcox14ac8bf2006-03-28 11:03:44 -05001688 .max_sectors = 0xFFFF,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689#ifdef SYM_LINUX_PROC_INFO_SUPPORT
1690 .proc_info = sym53c8xx_proc_info,
1691 .proc_name = NAME53C8XX,
1692#endif
1693};
1694
1695static int attach_count;
1696
1697static int __devinit sym2_probe(struct pci_dev *pdev,
1698 const struct pci_device_id *ent)
1699{
1700 struct sym_device sym_dev;
1701 struct sym_nvram nvram;
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001702 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 memset(&sym_dev, 0, sizeof(sym_dev));
1705 memset(&nvram, 0, sizeof(nvram));
1706
1707 if (pci_enable_device(pdev))
1708 goto leave;
1709
1710 pci_set_master(pdev);
1711
1712 if (pci_request_regions(pdev, NAME53C8XX))
1713 goto disable;
1714
1715 sym_init_device(pdev, &sym_dev);
1716 if (sym_check_supported(&sym_dev))
1717 goto free;
1718
1719 if (sym_check_raid(&sym_dev))
1720 goto leave; /* Don't disable the device */
1721
1722 if (sym_set_workarounds(&sym_dev))
1723 goto free;
1724
1725 sym_config_pqs(pdev, &sym_dev);
1726
1727 sym_get_nvram(&sym_dev, &nvram);
1728
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001729 shost = sym_attach(&sym2_template, attach_count, &sym_dev);
1730 if (!shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 goto free;
1732
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001733 if (scsi_add_host(shost, &pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 goto detach;
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001735 scsi_scan_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 attach_count++;
1738
1739 return 0;
1740
1741 detach:
1742 sym_detach(pci_get_drvdata(pdev), pdev);
1743 free:
1744 pci_release_regions(pdev);
1745 disable:
1746 pci_disable_device(pdev);
1747 leave:
1748 return -ENODEV;
1749}
1750
1751static void __devexit sym2_remove(struct pci_dev *pdev)
1752{
1753 struct sym_hcb *np = pci_get_drvdata(pdev);
1754 struct Scsi_Host *host = np->s.host;
1755
1756 scsi_remove_host(host);
1757 scsi_host_put(host);
1758
1759 sym_detach(np, pdev);
1760
1761 pci_release_regions(pdev);
1762 pci_disable_device(pdev);
1763
1764 attach_count--;
1765}
1766
Linas Vepstasd68cd752007-10-05 15:55:04 -04001767/**
1768 * sym2_io_error_detected() - called when PCI error is detected
1769 * @pdev: pointer to PCI device
1770 * @state: current state of the PCI slot
1771 */
1772static pci_ers_result_t sym2_io_error_detected(struct pci_dev *pdev,
1773 enum pci_channel_state state)
1774{
1775 /* If slot is permanently frozen, turn everything off */
1776 if (state == pci_channel_io_perm_failure) {
1777 sym2_remove(pdev);
1778 return PCI_ERS_RESULT_DISCONNECT;
1779 }
1780
1781 disable_irq(pdev->irq);
1782 pci_disable_device(pdev);
1783
1784 /* Request that MMIO be enabled, so register dump can be taken. */
1785 return PCI_ERS_RESULT_CAN_RECOVER;
1786}
1787
1788/**
1789 * sym2_io_slot_dump - Enable MMIO and dump debug registers
1790 * @pdev: pointer to PCI device
1791 */
1792static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
1793{
1794 struct sym_hcb *np = pci_get_drvdata(pdev);
1795
1796 sym_dump_registers(np);
1797
1798 /* Request a slot reset. */
1799 return PCI_ERS_RESULT_NEED_RESET;
1800}
1801
1802/**
1803 * sym2_reset_workarounds - hardware-specific work-arounds
1804 *
1805 * This routine is similar to sym_set_workarounds(), except
1806 * that, at this point, we already know that the device was
1807 * succesfully intialized at least once before, and so most
1808 * of the steps taken there are un-needed here.
1809 */
1810static void sym2_reset_workarounds(struct pci_dev *pdev)
1811{
Linas Vepstasd68cd752007-10-05 15:55:04 -04001812 u_short status_reg;
1813 struct sym_chip *chip;
1814
Matthew Wilcoxbd678452007-10-05 15:55:05 -04001815 chip = sym_lookup_chip_table(pdev->device, pdev->revision);
Linas Vepstasd68cd752007-10-05 15:55:04 -04001816
1817 /* Work around for errant bit in 895A, in a fashion
1818 * similar to what is done in sym_set_workarounds().
1819 */
1820 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1821 if (!(chip->features & FE_66MHZ) && (status_reg & PCI_STATUS_66MHZ)) {
1822 status_reg = PCI_STATUS_66MHZ;
1823 pci_write_config_word(pdev, PCI_STATUS, status_reg);
1824 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1825 }
1826}
1827
1828/**
1829 * sym2_io_slot_reset() - called when the pci bus has been reset.
1830 * @pdev: pointer to PCI device
1831 *
1832 * Restart the card from scratch.
1833 */
1834static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
1835{
1836 struct sym_hcb *np = pci_get_drvdata(pdev);
1837
1838 printk(KERN_INFO "%s: recovering from a PCI slot reset\n",
1839 sym_name(np));
1840
1841 if (pci_enable_device(pdev)) {
1842 printk(KERN_ERR "%s: Unable to enable after PCI reset\n",
1843 sym_name(np));
1844 return PCI_ERS_RESULT_DISCONNECT;
1845 }
1846
1847 pci_set_master(pdev);
1848 enable_irq(pdev->irq);
1849
1850 /* If the chip can do Memory Write Invalidate, enable it */
1851 if (np->features & FE_WRIE) {
1852 if (pci_set_mwi(pdev))
1853 return PCI_ERS_RESULT_DISCONNECT;
1854 }
1855
1856 /* Perform work-arounds, analogous to sym_set_workarounds() */
1857 sym2_reset_workarounds(pdev);
1858
1859 /* Perform host reset only on one instance of the card */
1860 if (PCI_FUNC(pdev->devfn) == 0) {
1861 if (sym_reset_scsi_bus(np, 0)) {
1862 printk(KERN_ERR "%s: Unable to reset scsi host\n",
1863 sym_name(np));
1864 return PCI_ERS_RESULT_DISCONNECT;
1865 }
1866 sym_start_up(np, 1);
1867 }
1868
1869 return PCI_ERS_RESULT_RECOVERED;
1870}
1871
1872/**
1873 * sym2_io_resume() - resume normal ops after PCI reset
1874 * @pdev: pointer to PCI device
1875 *
1876 * Called when the error recovery driver tells us that its
1877 * OK to resume normal operation. Use completion to allow
1878 * halted scsi ops to resume.
1879 */
1880static void sym2_io_resume(struct pci_dev *pdev)
1881{
1882 struct sym_hcb *np = pci_get_drvdata(pdev);
1883 struct Scsi_Host *shost = np->s.host;
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001884 struct sym_data *sym_data = shost_priv(shost);
Linas Vepstasd68cd752007-10-05 15:55:04 -04001885
1886 spin_lock_irq(shost->host_lock);
Matthew Wilcox99c9e0a2007-10-05 15:55:12 -04001887 if (sym_data->io_reset)
1888 complete_all(sym_data->io_reset);
1889 sym_data->io_reset = NULL;
Linas Vepstasd68cd752007-10-05 15:55:04 -04001890 spin_unlock_irq(shost->host_lock);
1891}
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893static void sym2_get_signalling(struct Scsi_Host *shost)
1894{
1895 struct sym_hcb *np = sym_get_hcb(shost);
1896 enum spi_signal_type type;
1897
1898 switch (np->scsi_mode) {
1899 case SMODE_SE:
1900 type = SPI_SIGNAL_SE;
1901 break;
1902 case SMODE_LVD:
1903 type = SPI_SIGNAL_LVD;
1904 break;
1905 case SMODE_HVD:
1906 type = SPI_SIGNAL_HVD;
1907 break;
1908 default:
1909 type = SPI_SIGNAL_UNKNOWN;
1910 break;
1911 }
1912 spi_signalling(shost) = type;
1913}
1914
1915static void sym2_set_offset(struct scsi_target *starget, int offset)
1916{
1917 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1918 struct sym_hcb *np = sym_get_hcb(shost);
1919 struct sym_tcb *tp = &np->target[starget->id];
1920
1921 tp->tgoal.offset = offset;
1922 tp->tgoal.check_nego = 1;
1923}
1924
1925static void sym2_set_period(struct scsi_target *starget, int period)
1926{
1927 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1928 struct sym_hcb *np = sym_get_hcb(shost);
1929 struct sym_tcb *tp = &np->target[starget->id];
1930
James Bottomleye4862fe2005-05-06 13:14:48 -05001931 /* have to have DT for these transfers, but DT will also
1932 * set width, so check that this is allowed */
1933 if (period <= np->minsync && spi_width(starget))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 tp->tgoal.dt = 1;
1935
1936 tp->tgoal.period = period;
1937 tp->tgoal.check_nego = 1;
1938}
1939
1940static void sym2_set_width(struct scsi_target *starget, int width)
1941{
1942 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1943 struct sym_hcb *np = sym_get_hcb(shost);
1944 struct sym_tcb *tp = &np->target[starget->id];
1945
1946 /* It is illegal to have DT set on narrow transfers. If DT is
1947 * clear, we must also clear IU and QAS. */
1948 if (width == 0)
1949 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1950
1951 tp->tgoal.width = width;
1952 tp->tgoal.check_nego = 1;
1953}
1954
1955static void sym2_set_dt(struct scsi_target *starget, int dt)
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 /* We must clear QAS and IU if DT is clear */
1962 if (dt)
1963 tp->tgoal.dt = 1;
1964 else
1965 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
1966 tp->tgoal.check_nego = 1;
1967}
1968
Matthew Wilcox8b2f8132005-11-29 23:08:38 -05001969#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970static void sym2_set_iu(struct scsi_target *starget, int iu)
1971{
1972 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1973 struct sym_hcb *np = sym_get_hcb(shost);
1974 struct sym_tcb *tp = &np->target[starget->id];
1975
1976 if (iu)
1977 tp->tgoal.iu = tp->tgoal.dt = 1;
1978 else
1979 tp->tgoal.iu = 0;
1980 tp->tgoal.check_nego = 1;
1981}
1982
1983static void sym2_set_qas(struct scsi_target *starget, int qas)
1984{
1985 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1986 struct sym_hcb *np = sym_get_hcb(shost);
1987 struct sym_tcb *tp = &np->target[starget->id];
1988
1989 if (qas)
1990 tp->tgoal.dt = tp->tgoal.qas = 1;
1991 else
1992 tp->tgoal.qas = 0;
1993 tp->tgoal.check_nego = 1;
1994}
Matthew Wilcox8b2f8132005-11-29 23:08:38 -05001995#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997static struct spi_function_template sym2_transport_functions = {
1998 .set_offset = sym2_set_offset,
1999 .show_offset = 1,
2000 .set_period = sym2_set_period,
2001 .show_period = 1,
2002 .set_width = sym2_set_width,
2003 .show_width = 1,
2004 .set_dt = sym2_set_dt,
2005 .show_dt = 1,
Matthew Wilcox8b2f8132005-11-29 23:08:38 -05002006#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 .set_iu = sym2_set_iu,
2008 .show_iu = 1,
2009 .set_qas = sym2_set_qas,
2010 .show_qas = 1,
Matthew Wilcox8b2f8132005-11-29 23:08:38 -05002011#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 .get_signalling = sym2_get_signalling,
2013};
2014
2015static struct pci_device_id sym2_id_table[] __devinitdata = {
2016 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
2017 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2018 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
2019 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2020 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
2021 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2022 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
2023 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2024 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
2025 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2026 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
2027 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2028 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
Grant Grundlerb2b3c122006-07-17 07:22:45 -06002029 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
2031 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2032 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
2033 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2034 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2035 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2036 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2037 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2038 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
Chip Coldwell147e5052007-05-23 14:41:38 -07002039 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2041 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2042 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2043 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2044 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2045 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2046 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2047 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2048 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2049 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2050 { 0, }
2051};
2052
2053MODULE_DEVICE_TABLE(pci, sym2_id_table);
2054
Linas Vepstasd68cd752007-10-05 15:55:04 -04002055static struct pci_error_handlers sym2_err_handler = {
2056 .error_detected = sym2_io_error_detected,
2057 .mmio_enabled = sym2_io_slot_dump,
2058 .slot_reset = sym2_io_slot_reset,
2059 .resume = sym2_io_resume,
2060};
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062static struct pci_driver sym2_driver = {
2063 .name = NAME53C8XX,
2064 .id_table = sym2_id_table,
2065 .probe = sym2_probe,
2066 .remove = __devexit_p(sym2_remove),
Linas Vepstasd68cd752007-10-05 15:55:04 -04002067 .err_handler = &sym2_err_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068};
2069
2070static int __init sym2_init(void)
2071{
2072 int error;
2073
2074 sym2_setup_params();
2075 sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2076 if (!sym2_transport_template)
2077 return -ENODEV;
2078
2079 error = pci_register_driver(&sym2_driver);
2080 if (error)
2081 spi_release_transport(sym2_transport_template);
2082 return error;
2083}
2084
2085static void __exit sym2_exit(void)
2086{
2087 pci_unregister_driver(&sym2_driver);
2088 spi_release_transport(sym2_transport_template);
2089}
2090
2091module_init(sym2_init);
2092module_exit(sym2_exit);