Linux Audio

Check our new training course

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