aic79xx_osm.c 77.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*
 * Adaptec AIC79xx device driver for Linux.
 *
 * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic79xx_osm.c#171 $
 *
 * --------------------------------------------------------------------------
 * Copyright (c) 1994-2000 Justin T. Gibbs.
 * Copyright (c) 1997-1999 Doug Ledford
 * Copyright (c) 2000-2003 Adaptec Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */

#include "aic79xx_osm.h"
#include "aic79xx_inline.h"
#include <scsi/scsicam.h>

49 50
static struct scsi_transport_template *ahd_linux_transport_template = NULL;

L
Linus Torvalds 已提交
51 52
#include <linux/init.h>		/* __setup */
#include <linux/mm.h>		/* For fetching system memory size */
53
#include <linux/blkdev.h>		/* For block_size() */
L
Linus Torvalds 已提交
54
#include <linux/delay.h>	/* For ssleep/msleep */
J
Jeff Garzik 已提交
55
#include <linux/device.h>
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

/*
 * Bucket size for counting good commands in between bad ones.
 */
#define AHD_LINUX_ERR_THRESH	1000

/*
 * Set this to the delay in seconds after SCSI bus reset.
 * Note, we honor this only for the initial bus reset.
 * The scsi error recovery code performs its own bus settle
 * delay handling for error recovery actions.
 */
#ifdef CONFIG_AIC79XX_RESET_DELAY_MS
#define AIC79XX_RESET_DELAY CONFIG_AIC79XX_RESET_DELAY_MS
#else
#define AIC79XX_RESET_DELAY 5000
#endif

/*
 * To change the default number of tagged transactions allowed per-device,
 * add a line to the lilo.conf file like:
 * append="aic79xx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
 * which will result in the first four devices on the first two
 * controllers being set to a tagged queue depth of 32.
 *
 * The tag_commands is an array of 16 to allow for wide and twin adapters.
 * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
 * for channel 1.
 */
typedef struct {
	uint16_t tag_commands[16];	/* Allow for wide/twin adapters. */
} adapter_tag_info_t;

/*
 * Modify this as you see fit for your system.
 *
 * 0			tagged queuing disabled
 * 1 <= n <= 253	n == max tags ever dispatched.
 *
 * The driver will throttle the number of commands dispatched to a
 * device if it returns queue full.  For devices with a fixed maximum
 * queue depth, the driver will eventually determine this depth and
 * lock it in (a console message is printed to indicate that a lock
 * has occurred).  On some devices, queue full is returned for a temporary
 * resource shortage.  These devices will return queue full at varying
 * depths.  The driver will throttle back when the queue fulls occur and
 * attempt to slowly increase the depth over time as the device recovers
 * from the resource shortage.
 *
 * In this example, the first line will disable tagged queueing for all
 * the devices on the first probed aic79xx adapter.
 *
 * The second line enables tagged queueing with 4 commands/LUN for IDs
 * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
 * driver to attempt to use up to 64 tags for ID 1.
 *
 * The third line is the same as the first line.
 *
 * The fourth line disables tagged queueing for devices 0 and 3.  It
 * enables tagged queueing for the other IDs, with 16 commands/LUN
 * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
 * IDs 2, 5-7, and 9-15.
 */

/*
 * NOTE: The below structure is for reference only, the actual structure
 *       to modify in order to change things is just below this comment block.
adapter_tag_info_t aic79xx_tag_info[] =
{
	{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
	{{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
	{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
	{{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
};
*/

#ifdef CONFIG_AIC79XX_CMDS_PER_DEVICE
#define AIC79XX_CMDS_PER_DEVICE CONFIG_AIC79XX_CMDS_PER_DEVICE
#else
#define AIC79XX_CMDS_PER_DEVICE AHD_MAX_QUEUE
#endif

#define AIC79XX_CONFIGED_TAG_COMMANDS {					\
	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE		\
}

/*
 * By default, use the number of commands specified by
 * the users kernel configuration.
 */
static adapter_tag_info_t aic79xx_tag_info[] =
{
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS},
	{AIC79XX_CONFIGED_TAG_COMMANDS}
};

/*
 * The I/O cell on the chip is very configurable in respect to its analog
 * characteristics.  Set the defaults here; they can be overriden with
 * the proper insmod parameters.
 */
struct ahd_linux_iocell_opts
{
	uint8_t	precomp;
	uint8_t	slewrate;
	uint8_t amplitude;
};
#define AIC79XX_DEFAULT_PRECOMP		0xFF
#define AIC79XX_DEFAULT_SLEWRATE	0xFF
#define AIC79XX_DEFAULT_AMPLITUDE	0xFF
#define AIC79XX_DEFAULT_IOOPTS			\
{						\
	AIC79XX_DEFAULT_PRECOMP,		\
	AIC79XX_DEFAULT_SLEWRATE,		\
	AIC79XX_DEFAULT_AMPLITUDE		\
}
#define AIC79XX_PRECOMP_INDEX	0
#define AIC79XX_SLEWRATE_INDEX	1
#define AIC79XX_AMPLITUDE_INDEX	2
static struct ahd_linux_iocell_opts aic79xx_iocell_info[] =
{
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS,
	AIC79XX_DEFAULT_IOOPTS
};

/*
 * There should be a specific return value for this in scsi.h, but
 * it seems that most drivers ignore it.
 */
#define DID_UNDERFLOW   DID_ERROR

void
ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
{
	printk("(scsi%d:%c:%d:%d): ",
	       ahd->platform_data->host->host_no,
	       scb != NULL ? SCB_GET_CHANNEL(ahd, scb) : 'X',
	       scb != NULL ? SCB_GET_TARGET(ahd, scb) : -1,
	       scb != NULL ? SCB_GET_LUN(scb) : -1);
}

/*
 * XXX - these options apply unilaterally to _all_ adapters
 *       cards in the system.  This should be fixed.  Exceptions to this
 *       rule are noted in the comments.
 */

/*
 * Skip the scsi bus reset.  Non 0 make us skip the reset at startup.  This
 * has no effect on any later resets that might occur due to things like
 * SCSI bus timeouts.
 */
static uint32_t aic79xx_no_reset;

/*
 * Certain PCI motherboards will scan PCI devices from highest to lowest,
 * others scan from lowest to highest, and they tend to do all kinds of
 * strange things when they come into contact with PCI bridge chips.  The
 * net result of all this is that the PCI card that is actually used to boot
 * the machine is very hard to detect.  Most motherboards go from lowest
 * PCI slot number to highest, and the first SCSI controller found is the
 * one you boot from.  The only exceptions to this are when a controller
 * has its BIOS disabled.  So, we by default sort all of our SCSI controllers
 * from lowest PCI slot number to highest PCI slot number.  We also force
 * all controllers with their BIOS disabled to the end of the list.  This
 * works on *almost* all computers.  Where it doesn't work, we have this
 * option.  Setting this option to non-0 will reverse the order of the sort
 * to highest first, then lowest, but will still leave cards with their BIOS
 * disabled at the very end.  That should fix everyone up unless there are
 * really strange cirumstances.
 */
static uint32_t aic79xx_reverse_scan;

/*
 * Should we force EXTENDED translation on a controller.
 *     0 == Use whatever is in the SEEPROM or default to off
 *     1 == Use whatever is in the SEEPROM or default to on
 */
static uint32_t aic79xx_extended;

/*
 * PCI bus parity checking of the Adaptec controllers.  This is somewhat
 * dubious at best.  To my knowledge, this option has never actually
 * solved a PCI parity problem, but on certain machines with broken PCI
 * chipset configurations, it can generate tons of false error messages.
 * It's included in the driver for completeness.
 *   0	   = Shut off PCI parity check
 *   non-0 = Enable PCI parity check
 *
 * NOTE: you can't actually pass -1 on the lilo prompt.  So, to set this
 * variable to -1 you would actually want to simply pass the variable
 * name without a number.  That will invert the 0 which will result in
 * -1.
 */
static uint32_t aic79xx_pci_parity = ~0;

/*
 * There are lots of broken chipsets in the world.  Some of them will
 * violate the PCI spec when we issue byte sized memory writes to our
 * controller.  I/O mapped register access, if allowed by the given
 * platform, will work in almost all cases.
 */
uint32_t aic79xx_allow_memio = ~0;

/*
 * So that we can set how long each device is given as a selection timeout.
 * The table of values goes like this:
 *   0 - 256ms
 *   1 - 128ms
 *   2 - 64ms
 *   3 - 32ms
 * We default to 256ms because some older devices need a longer time
 * to respond to initial selection.
 */
static uint32_t aic79xx_seltime;

/*
 * Certain devices do not perform any aging on commands.  Should the
 * device be saturated by commands in one portion of the disk, it is
 * possible for transactions on far away sectors to never be serviced.
 * To handle these devices, we can periodically send an ordered tag to
 * force all outstanding transactions to be serviced prior to a new
 * transaction.
 */
uint32_t aic79xx_periodic_otag;

/*
 * Module information and settable options.
 */
static char *aic79xx = NULL;

MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
MODULE_DESCRIPTION("Adaptec Aic790X U320 SCSI Host Bus Adapter driver");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_VERSION(AIC79XX_DRIVER_VERSION);
326
module_param(aic79xx, charp, 0444);
L
Linus Torvalds 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
MODULE_PARM_DESC(aic79xx,
"period delimited, options string.\n"
"	verbose			Enable verbose/diagnostic logging\n"
"	allow_memio		Allow device registers to be memory mapped\n"
"	debug			Bitmask of debug values to enable\n"
"	no_reset		Supress initial bus resets\n"
"	extended		Enable extended geometry on all controllers\n"
"	periodic_otag		Send an ordered tagged transaction\n"
"				periodically to prevent tag starvation.\n"
"				This may be required by some older disk\n"
"				or drives/RAID arrays.\n"
"	reverse_scan		Sort PCI devices highest Bus/Slot to lowest\n"
"	tag_info:<tag_str>	Set per-target tag depth\n"
"	global_tag_depth:<int>	Global tag depth for all targets on all buses\n"
"	slewrate:<slewrate_list>Set the signal slew rate (0-15).\n"
"	precomp:<pcomp_list>	Set the signal precompensation (0-7).\n"
"	amplitude:<int>		Set the signal amplitude (0-7).\n"
"	seltime:<int>		Selection Timeout:\n"
"				(0/256ms,1/128ms,2/64ms,3/32ms)\n"
"\n"
"	Sample /etc/modprobe.conf line:\n"
"		Enable verbose logging\n"
"		Set tag depth on Controller 2/Target 2 to 10 tags\n"
"		Shorten the selection timeout to 128ms\n"
"\n"
"	options aic79xx 'aic79xx=verbose.tag_info:{{}.{}.{..10}}.seltime:1'\n"
353
"\n");
L
Linus Torvalds 已提交
354 355

static void ahd_linux_handle_scsi_status(struct ahd_softc *,
356
					 struct scsi_device *,
L
Linus Torvalds 已提交
357 358
					 struct scb *);
static void ahd_linux_queue_cmd_complete(struct ahd_softc *ahd,
359
					 struct scsi_cmnd *cmd);
L
Linus Torvalds 已提交
360
static void ahd_linux_sem_timeout(u_long arg);
361
static int  ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag);
L
Linus Torvalds 已提交
362 363 364
static void ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd);
static u_int ahd_linux_user_tagdepth(struct ahd_softc *ahd,
				     struct ahd_devinfo *devinfo);
365
static void ahd_linux_device_queue_depth(struct scsi_device *);
H
Hannes Reinecke 已提交
366
static int ahd_linux_run_command(struct ahd_softc*,
367
				 struct ahd_linux_device *,
H
Hannes Reinecke 已提交
368
				 struct scsi_cmnd *);
L
Linus Torvalds 已提交
369
static void ahd_linux_setup_tag_info_global(char *p);
370
static int  aic79xx_setup(char *c);
371 372 373

static int ahd_linux_unit;

L
Linus Torvalds 已提交
374 375 376 377 378 379 380

/****************************** Inlines ***************************************/
static __inline void ahd_linux_unmap_scb(struct ahd_softc*, struct scb*);

static __inline void
ahd_linux_unmap_scb(struct ahd_softc *ahd, struct scb *scb)
{
381
	struct scsi_cmnd *cmd;
L
Linus Torvalds 已提交
382 383 384
	int direction;

	cmd = scb->io_ctx;
385
	direction = cmd->sc_data_direction;
L
Linus Torvalds 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
	ahd_sync_sglist(ahd, scb, BUS_DMASYNC_POSTWRITE);
	if (cmd->use_sg != 0) {
		struct scatterlist *sg;

		sg = (struct scatterlist *)cmd->request_buffer;
		pci_unmap_sg(ahd->dev_softc, sg, cmd->use_sg, direction);
	} else if (cmd->request_bufflen != 0) {
		pci_unmap_single(ahd->dev_softc,
				 scb->platform_data->buf_busaddr,
				 cmd->request_bufflen, direction);
	}
}

/******************************** Macros **************************************/
#define BUILD_SCSIID(ahd, cmd)						\
401
	(((scmd_id(cmd) << TID_SHIFT) & TID) | (ahd)->our_id)
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434

/*
 * Return a string describing the driver.
 */
static const char *
ahd_linux_info(struct Scsi_Host *host)
{
	static char buffer[512];
	char	ahd_info[256];
	char   *bp;
	struct ahd_softc *ahd;

	bp = &buffer[0];
	ahd = *(struct ahd_softc **)host->hostdata;
	memset(bp, 0, sizeof(buffer));
	strcpy(bp, "Adaptec AIC79XX PCI-X SCSI HBA DRIVER, Rev ");
	strcat(bp, AIC79XX_DRIVER_VERSION);
	strcat(bp, "\n");
	strcat(bp, "        <");
	strcat(bp, ahd->description);
	strcat(bp, ">\n");
	strcat(bp, "        ");
	ahd_controller_info(ahd, ahd_info);
	strcat(bp, ahd_info);
	strcat(bp, "\n");

	return (bp);
}

/*
 * Queue an SCB to the controller.
 */
static int
435
ahd_linux_queue(struct scsi_cmnd * cmd, void (*scsi_done) (struct scsi_cmnd *))
L
Linus Torvalds 已提交
436 437
{
	struct	 ahd_softc *ahd;
438
	struct	 ahd_linux_device *dev = scsi_transport_device_data(cmd->device);
L
Linus Torvalds 已提交
439 440 441 442 443 444 445 446 447

	ahd = *(struct ahd_softc **)cmd->device->host->hostdata;

	/*
	 * Close the race of a command that was in the process of
	 * being queued to us just as our simq was frozen.  Let
	 * DV commands through so long as we are only frozen to
	 * perform DV.
	 */
448
	if (ahd->platform_data->qfrozen != 0) {
H
Hannes Reinecke 已提交
449
		printf("%s: queue frozen\n", ahd_name(ahd));
L
Linus Torvalds 已提交
450

H
Hannes Reinecke 已提交
451
		return SCSI_MLQUEUE_HOST_BUSY;
L
Linus Torvalds 已提交
452
	}
H
Hannes Reinecke 已提交
453 454 455 456 457 458

	/*
	 * Save the callback on completion function.
	 */
	cmd->scsi_done = scsi_done;

L
Linus Torvalds 已提交
459
	cmd->result = CAM_REQ_INPROG << 16;
H
Hannes Reinecke 已提交
460 461

	return ahd_linux_run_command(ahd, dev, cmd);
L
Linus Torvalds 已提交
462 463
}

464 465 466 467 468 469 470 471 472 473 474 475 476 477
static inline struct scsi_target **
ahd_linux_target_in_softc(struct scsi_target *starget)
{
	struct	ahd_softc *ahd =
		*((struct ahd_softc **)dev_to_shost(&starget->dev)->hostdata);
	unsigned int target_offset;

	target_offset = starget->id;
	if (starget->channel != 0)
		target_offset += 8;

	return &ahd->platform_data->starget[target_offset];
}

L
Linus Torvalds 已提交
478
static int
479
ahd_linux_target_alloc(struct scsi_target *starget)
L
Linus Torvalds 已提交
480
{
481 482 483 484 485 486 487 488 489
	struct	ahd_softc *ahd =
		*((struct ahd_softc **)dev_to_shost(&starget->dev)->hostdata);
	unsigned long flags;
	struct scsi_target **ahd_targp = ahd_linux_target_in_softc(starget);
	struct ahd_linux_target *targ = scsi_transport_target_data(starget);
	struct ahd_devinfo devinfo;
	struct ahd_initiator_tinfo *tinfo;
	struct ahd_tmode_tstate *tstate;
	char channel = starget->channel + 'A';
L
Linus Torvalds 已提交
490

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	ahd_lock(ahd, &flags);

	BUG_ON(*ahd_targp != NULL);

	*ahd_targp = starget;
	memset(targ, 0, sizeof(*targ));

	tinfo = ahd_fetch_transinfo(ahd, channel, ahd->our_id,
				    starget->id, &tstate);
	ahd_compile_devinfo(&devinfo, ahd->our_id, starget->id,
			    CAM_LUN_WILDCARD, channel,
			    ROLE_INITIATOR);
	spi_min_period(starget) = AHD_SYNCRATE_MAX; /* We can do U320 */
	if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0)
		spi_max_offset(starget) = MAX_OFFSET_PACED_BUG;
	else
		spi_max_offset(starget) = MAX_OFFSET_PACED;
	spi_max_width(starget) = ahd->features & AHD_WIDE;

	ahd_set_syncrate(ahd, &devinfo, 0, 0, 0,
			 AHD_TRANS_GOAL, /*paused*/FALSE);
	ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
		      AHD_TRANS_GOAL, /*paused*/FALSE);
	ahd_unlock(ahd, &flags);

	return 0;
}

static void
ahd_linux_target_destroy(struct scsi_target *starget)
{
	struct scsi_target **ahd_targp = ahd_linux_target_in_softc(starget);

	*ahd_targp = NULL;
L
Linus Torvalds 已提交
525 526 527
}

static int
528
ahd_linux_slave_alloc(struct scsi_device *sdev)
L
Linus Torvalds 已提交
529
{
530 531 532 533 534
	struct	ahd_softc *ahd =
		*((struct ahd_softc **)sdev->host->hostdata);
	struct scsi_target *starget = sdev->sdev_target;
	struct ahd_linux_target *targ = scsi_transport_target_data(starget);
	struct ahd_linux_device *dev;
L
Linus Torvalds 已提交
535 536

	if (bootverbose)
537 538 539 540 541 542 543 544 545 546 547 548 549
		printf("%s: Slave Alloc %d\n", ahd_name(ahd), sdev->id);

	BUG_ON(targ->sdev[sdev->lun] != NULL);

	dev = scsi_transport_device_data(sdev);
	memset(dev, 0, sizeof(*dev));

	/*
	 * We start out life using untagged
	 * transactions of which we allow one.
	 */
	dev->openings = 1;

L
Linus Torvalds 已提交
550
	/*
551 552 553
	 * Set maxtags to 0.  This will be changed if we
	 * later determine that we are dealing with
	 * a tagged queuing capable device.
L
Linus Torvalds 已提交
554
	 */
555 556 557 558
	dev->maxtags = 0;
	
	targ->sdev[sdev->lun] = sdev;

L
Linus Torvalds 已提交
559 560 561
	return (0);
}

562 563 564 565 566 567 568
static int
ahd_linux_slave_configure(struct scsi_device *sdev)
{
	struct	ahd_softc *ahd;

	ahd = *((struct ahd_softc **)sdev->host->hostdata);
	if (bootverbose)
J
Jeff Garzik 已提交
569
		sdev_printk(KERN_INFO, sdev, "Slave Configure\n");
570 571 572 573 574 575 576 577 578 579

	ahd_linux_device_queue_depth(sdev);

	/* Initial Domain Validation */
	if (!spi_initial_dv(sdev->sdev_target))
		spi_dv_device(sdev);

	return 0;
}

L
Linus Torvalds 已提交
580
static void
581
ahd_linux_slave_destroy(struct scsi_device *sdev)
L
Linus Torvalds 已提交
582 583
{
	struct	ahd_softc *ahd;
584 585
	struct	ahd_linux_device *dev = scsi_transport_device_data(sdev);
	struct  ahd_linux_target *targ = scsi_transport_target_data(sdev->sdev_target);
L
Linus Torvalds 已提交
586

587
	ahd = *((struct ahd_softc **)sdev->host->hostdata);
L
Linus Torvalds 已提交
588
	if (bootverbose)
589 590 591 592 593
		printf("%s: Slave Destroy %d\n", ahd_name(ahd), sdev->id);

	BUG_ON(dev->active);

	targ->sdev[sdev->lun] = NULL;
L
Linus Torvalds 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

}

#if defined(__i386__)
/*
 * Return the disk geometry for the given SCSI device.
 */
static int
ahd_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
		    sector_t capacity, int geom[])
{
	uint8_t *bh;
	int	 heads;
	int	 sectors;
	int	 cylinders;
	int	 ret;
	int	 extended;
	struct	 ahd_softc *ahd;

	ahd = *((struct ahd_softc **)sdev->host->hostdata);

	bh = scsi_bios_ptable(bdev);
	if (bh) {
		ret = scsi_partsize(bh, capacity,
				    &geom[2], &geom[0], &geom[1]);
		kfree(bh);
		if (ret != -1)
			return (ret);
	}
	heads = 64;
	sectors = 32;
	cylinders = aic_sector_div(capacity, heads, sectors);

	if (aic79xx_extended != 0)
		extended = 1;
	else
		extended = (ahd->flags & AHD_EXTENDED_TRANS_A) != 0;
	if (extended && cylinders >= 1024) {
		heads = 255;
		sectors = 63;
		cylinders = aic_sector_div(capacity, heads, sectors);
	}
	geom[0] = heads;
	geom[1] = sectors;
	geom[2] = cylinders;
	return (0);
}
#endif

/*
 * Abort the current SCSI command(s).
 */
static int
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
ahd_linux_abort(struct scsi_cmnd *cmd)
{
	int error;

	error = ahd_linux_queue_recovery_cmd(cmd, SCB_ABORT);
	if (error != 0)
		printf("aic79xx_abort returns 0x%x\n", error);
	return error;
}

/*
 * Attempt to send a target reset message to the device that timed out.
 */
static int
ahd_linux_dev_reset(struct scsi_cmnd *cmd)
{
	int error;

	error = ahd_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
	if (error != 0)
		printf("aic79xx_dev_reset returns 0x%x\n", error);
	return error;
}

/*
 * Reset the SCSI bus.
 */
static int
ahd_linux_bus_reset(struct scsi_cmnd *cmd)
L
Linus Torvalds 已提交
676 677 678
{
	struct ahd_softc *ahd;
	u_long s;
679
	int    found;
L
Linus Torvalds 已提交
680 681

	ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
682 683 684 685 686 687
#ifdef AHD_DEBUG
	if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
		printf("%s: Bus reset called for cmd %p\n",
		       ahd_name(ahd), cmd);
#endif
	ahd_lock(ahd, &s);
688
	found = ahd_reset_channel(ahd, scmd_channel(cmd) + 'A',
689 690
				  /*initiate reset*/TRUE);
	ahd_unlock(ahd, &s);
L
Linus Torvalds 已提交
691

692 693 694
	if (bootverbose)
		printf("%s: SCSI bus reset delivered. "
		       "%d SCBs aborted.\n", ahd_name(ahd), found);
L
Linus Torvalds 已提交
695

696 697
	return (SUCCESS);
}
L
Linus Torvalds 已提交
698

699 700 701
struct scsi_host_template aic79xx_driver_template = {
	.module			= THIS_MODULE,
	.name			= "aic79xx",
702
	.proc_name		= "aic79xx",
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
	.proc_info		= ahd_linux_proc_info,
	.info			= ahd_linux_info,
	.queuecommand		= ahd_linux_queue,
	.eh_abort_handler	= ahd_linux_abort,
	.eh_device_reset_handler = ahd_linux_dev_reset,
	.eh_bus_reset_handler	= ahd_linux_bus_reset,
#if defined(__i386__)
	.bios_param		= ahd_linux_biosparam,
#endif
	.can_queue		= AHD_MAX_QUEUE,
	.this_id		= -1,
	.cmd_per_lun		= 2,
	.use_clustering		= ENABLE_CLUSTERING,
	.slave_alloc		= ahd_linux_slave_alloc,
	.slave_configure	= ahd_linux_slave_configure,
	.slave_destroy		= ahd_linux_slave_destroy,
	.target_alloc		= ahd_linux_target_alloc,
	.target_destroy		= ahd_linux_target_destroy,
};
L
Linus Torvalds 已提交
722

723 724 725 726 727 728 729 730 731 732 733 734 735 736
/******************************** Bus DMA *************************************/
int
ahd_dma_tag_create(struct ahd_softc *ahd, bus_dma_tag_t parent,
		   bus_size_t alignment, bus_size_t boundary,
		   dma_addr_t lowaddr, dma_addr_t highaddr,
		   bus_dma_filter_t *filter, void *filterarg,
		   bus_size_t maxsize, int nsegments,
		   bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
{
	bus_dma_tag_t dmat;

	dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
	if (dmat == NULL)
		return (ENOMEM);
L
Linus Torvalds 已提交
737 738

	/*
739 740 741 742 743
	 * Linux is very simplistic about DMA memory.  For now don't
	 * maintain all specification information.  Once Linux supplies
	 * better facilities for doing these operations, or the
	 * needs of this particular driver change, we might need to do
	 * more here.
L
Linus Torvalds 已提交
744
	 */
745 746 747 748 749 750
	dmat->alignment = alignment;
	dmat->boundary = boundary;
	dmat->maxsize = maxsize;
	*ret_tag = dmat;
	return (0);
}
L
Linus Torvalds 已提交
751

752 753 754 755 756
void
ahd_dma_tag_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat)
{
	free(dmat, M_DEVBUF);
}
L
Linus Torvalds 已提交
757

758 759 760 761 762 763 764 765 766 767
int
ahd_dmamem_alloc(struct ahd_softc *ahd, bus_dma_tag_t dmat, void** vaddr,
		 int flags, bus_dmamap_t *mapp)
{
	*vaddr = pci_alloc_consistent(ahd->dev_softc,
				      dmat->maxsize, mapp);
	if (*vaddr == NULL)
		return (ENOMEM);
	return(0);
}
L
Linus Torvalds 已提交
768

769 770 771 772 773 774 775 776 777 778 779 780 781
void
ahd_dmamem_free(struct ahd_softc *ahd, bus_dma_tag_t dmat,
		void* vaddr, bus_dmamap_t map)
{
	pci_free_consistent(ahd->dev_softc, dmat->maxsize,
			    vaddr, map);
}

int
ahd_dmamap_load(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map,
		void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
		void *cb_arg, int flags)
{
L
Linus Torvalds 已提交
782
	/*
783 784
	 * Assume for now that this will only be used during
	 * initialization and not for per-transaction buffer mapping.
L
Linus Torvalds 已提交
785
	 */
786
	bus_dma_segment_t stack_sg;
L
Linus Torvalds 已提交
787

788 789 790 791 792
	stack_sg.ds_addr = map;
	stack_sg.ds_len = dmat->maxsize;
	cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
	return (0);
}
L
Linus Torvalds 已提交
793

794 795 796 797
void
ahd_dmamap_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
{
}
L
Linus Torvalds 已提交
798

799 800 801 802 803 804
int
ahd_dmamap_unload(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
{
	/* Nothing to do */
	return (0);
}
L
Linus Torvalds 已提交
805

806 807 808 809 810 811 812 813 814 815 816
/********************* Platform Dependent Functions ***************************/
/*
 * Compare "left hand" softc with "right hand" softc, returning:
 * < 0 - lahd has a lower priority than rahd
 *   0 - Softcs are equal
 * > 0 - lahd has a higher priority than rahd
 */
int
ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
{
	int	value;
L
Linus Torvalds 已提交
817 818

	/*
819 820 821 822
	 * Under Linux, cards are ordered as follows:
	 *	1) PCI devices that are marked as the boot controller.
	 *	2) PCI devices with BIOS enabled sorted by bus/slot/func.
	 *	3) All remaining PCI devices sorted by bus/slot/func.
L
Linus Torvalds 已提交
823
	 */
824 825 826 827 828 829 830
#if 0
	value = (lahd->flags & AHD_BOOT_CHANNEL)
	      - (rahd->flags & AHD_BOOT_CHANNEL);
	if (value != 0)
		/* Controllers set for boot have a *higher* priority */
		return (value);
#endif
L
Linus Torvalds 已提交
831

832 833 834 835 836
	value = (lahd->flags & AHD_BIOS_ENABLED)
	      - (rahd->flags & AHD_BIOS_ENABLED);
	if (value != 0)
		/* Controllers with BIOS enabled have a *higher* priority */
		return (value);
L
Linus Torvalds 已提交
837

838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
	/* Still equal.  Sort by bus/slot/func. */
	if (aic79xx_reverse_scan != 0)
		value = ahd_get_pci_bus(lahd->dev_softc)
		      - ahd_get_pci_bus(rahd->dev_softc);
	else
		value = ahd_get_pci_bus(rahd->dev_softc)
		      - ahd_get_pci_bus(lahd->dev_softc);
	if (value != 0)
		return (value);
	if (aic79xx_reverse_scan != 0)
		value = ahd_get_pci_slot(lahd->dev_softc)
		      - ahd_get_pci_slot(rahd->dev_softc);
	else
		value = ahd_get_pci_slot(rahd->dev_softc)
		      - ahd_get_pci_slot(lahd->dev_softc);
	if (value != 0)
		return (value);
L
Linus Torvalds 已提交
855

856 857 858
	value = rahd->channel - lahd->channel;
	return (value);
}
L
Linus Torvalds 已提交
859

860 861 862
static void
ahd_linux_setup_iocell_info(u_long index, int instance, int targ, int32_t value)
{
L
Linus Torvalds 已提交
863

864 865 866 867 868 869 870 871
	if ((instance >= 0)
	 && (instance < NUM_ELEMENTS(aic79xx_iocell_info))) {
		uint8_t *iocell_info;

		iocell_info = (uint8_t*)&aic79xx_iocell_info[instance];
		iocell_info[index] = value & 0xFFFF;
		if (bootverbose)
			printf("iocell[%d:%ld] = %d\n", instance, index, value);
L
Linus Torvalds 已提交
872
	}
873
}
L
Linus Torvalds 已提交
874

875 876 877 878
static void
ahd_linux_setup_tag_info_global(char *p)
{
	int tags, i, j;
L
Linus Torvalds 已提交
879

880 881 882 883 884 885
	tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
	printf("Setting Global Tags= %d\n", tags);

	for (i = 0; i < NUM_ELEMENTS(aic79xx_tag_info); i++) {
		for (j = 0; j < AHD_NUM_TARGETS; j++) {
			aic79xx_tag_info[i].tag_commands[j] = tags;
L
Linus Torvalds 已提交
886 887 888 889 890
		}
	}
}

static void
891
ahd_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
L
Linus Torvalds 已提交
892 893
{

894 895 896 897 898 899
	if ((instance >= 0) && (targ >= 0)
	 && (instance < NUM_ELEMENTS(aic79xx_tag_info))
	 && (targ < AHD_NUM_TARGETS)) {
		aic79xx_tag_info[instance].tag_commands[targ] = value & 0x1FF;
		if (bootverbose)
			printf("tag_info[%d:%d] = %d\n", instance, targ, value);
L
Linus Torvalds 已提交
900 901 902
	}
}

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
static char *
ahd_parse_brace_option(char *opt_name, char *opt_arg, char *end, int depth,
		       void (*callback)(u_long, int, int, int32_t),
		       u_long callback_arg)
{
	char	*tok_end;
	char	*tok_end2;
	int      i;
	int      instance;
	int	 targ;
	int	 done;
	char	 tok_list[] = {'.', ',', '{', '}', '\0'};

	/* All options use a ':' name/arg separator */
	if (*opt_arg != ':')
		return (opt_arg);
	opt_arg++;
	instance = -1;
	targ = -1;
	done = FALSE;
	/*
	 * Restore separator that may be in
	 * the middle of our option argument.
	 */
	tok_end = strchr(opt_arg, '\0');
	if (tok_end < end)
		*tok_end = ',';
	while (!done) {
		switch (*opt_arg) {
		case '{':
			if (instance == -1) {
				instance = 0;
			} else {
				if (depth > 1) {
					if (targ == -1)
						targ = 0;
				} else {
					printf("Malformed Option %s\n",
					       opt_name);
					done = TRUE;
				}
			}
			opt_arg++;
			break;
		case '}':
			if (targ != -1)
				targ = -1;
			else if (instance != -1)
				instance = -1;
			opt_arg++;
			break;
		case ',':
		case '.':
			if (instance == -1)
				done = TRUE;
			else if (targ >= 0)
				targ++;
			else if (instance >= 0)
				instance++;
			opt_arg++;
			break;
		case '\0':
			done = TRUE;
			break;
		default:
			tok_end = end;
			for (i = 0; tok_list[i]; i++) {
				tok_end2 = strchr(opt_arg, tok_list[i]);
				if ((tok_end2) && (tok_end2 < tok_end))
					tok_end = tok_end2;
			}
			callback(callback_arg, instance, targ,
				 simple_strtol(opt_arg, NULL, 0));
			opt_arg = tok_end;
			break;
		}
	}
	return (opt_arg);
}

L
Linus Torvalds 已提交
983
/*
984 985 986
 * Handle Linux boot parameters. This routine allows for assigning a value
 * to a parameter with a ':' between the parameter and the value.
 * ie. aic79xx=stpwlev:1,extended
L
Linus Torvalds 已提交
987 988
 */
static int
989
aic79xx_setup(char *s)
L
Linus Torvalds 已提交
990
{
991 992 993
	int	i, n;
	char   *p;
	char   *end;
L
Linus Torvalds 已提交
994

995 996 997 998 999 1000 1001 1002
	static struct {
		const char *name;
		uint32_t *flag;
	} options[] = {
		{ "extended", &aic79xx_extended },
		{ "no_reset", &aic79xx_no_reset },
		{ "verbose", &aic79xx_verbose },
		{ "allow_memio", &aic79xx_allow_memio},
L
Linus Torvalds 已提交
1003
#ifdef AHD_DEBUG
1004
		{ "debug", &ahd_debug },
L
Linus Torvalds 已提交
1005
#endif
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
		{ "reverse_scan", &aic79xx_reverse_scan },
		{ "periodic_otag", &aic79xx_periodic_otag },
		{ "pci_parity", &aic79xx_pci_parity },
		{ "seltime", &aic79xx_seltime },
		{ "tag_info", NULL },
		{ "global_tag_depth", NULL},
		{ "slewrate", NULL },
		{ "precomp", NULL },
		{ "amplitude", NULL },
	};
L
Linus Torvalds 已提交
1016

1017
	end = strchr(s, '\0');
L
Linus Torvalds 已提交
1018

1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	/*
	 * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS
	 * will never be 0 in this case.
	 */      
	n = 0;  

	while ((p = strsep(&s, ",.")) != NULL) {
		if (*p == '\0')
			continue;
		for (i = 0; i < NUM_ELEMENTS(options); i++) {

			n = strlen(options[i].name);
			if (strncmp(options[i].name, p, n) == 0)
				break;
		}
		if (i == NUM_ELEMENTS(options))
			continue;

		if (strncmp(p, "global_tag_depth", n) == 0) {
			ahd_linux_setup_tag_info_global(p + n);
		} else if (strncmp(p, "tag_info", n) == 0) {
1040
			s = ahd_parse_brace_option("tag_info", p + n, end,
1041 1042
			    2, ahd_linux_setup_tag_info, 0);
		} else if (strncmp(p, "slewrate", n) == 0) {
1043
			s = ahd_parse_brace_option("slewrate",
1044 1045 1046
			    p + n, end, 1, ahd_linux_setup_iocell_info,
			    AIC79XX_SLEWRATE_INDEX);
		} else if (strncmp(p, "precomp", n) == 0) {
1047
			s = ahd_parse_brace_option("precomp",
1048 1049 1050
			    p + n, end, 1, ahd_linux_setup_iocell_info,
			    AIC79XX_PRECOMP_INDEX);
		} else if (strncmp(p, "amplitude", n) == 0) {
1051
			s = ahd_parse_brace_option("amplitude",
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
			    p + n, end, 1, ahd_linux_setup_iocell_info,
			    AIC79XX_AMPLITUDE_INDEX);
		} else if (p[n] == ':') {
			*(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
		} else if (!strncmp(p, "verbose", n)) {
			*(options[i].flag) = 1;
		} else {
			*(options[i].flag) ^= 0xFFFFFFFF;
		}
	}
	return 1;
L
Linus Torvalds 已提交
1063 1064
}

1065 1066 1067
__setup("aic79xx=", aic79xx_setup);

uint32_t aic79xx_verbose;
L
Linus Torvalds 已提交
1068 1069

int
1070
ahd_linux_register_host(struct ahd_softc *ahd, struct scsi_host_template *template)
L
Linus Torvalds 已提交
1071
{
1072 1073 1074 1075
	char	buf[80];
	struct	Scsi_Host *host;
	char	*new_name;
	u_long	s;
L
Linus Torvalds 已提交
1076

1077 1078 1079
	template->name = ahd->description;
	host = scsi_host_alloc(template, sizeof(struct ahd_softc *));
	if (host == NULL)
L
Linus Torvalds 已提交
1080 1081
		return (ENOMEM);

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
	*((struct ahd_softc **)host->hostdata) = ahd;
	ahd_lock(ahd, &s);
	scsi_assign_lock(host, &ahd->platform_data->spin_lock);
	ahd->platform_data->host = host;
	host->can_queue = AHD_MAX_QUEUE;
	host->cmd_per_lun = 2;
	host->sg_tablesize = AHD_NSEG;
	host->this_id = ahd->our_id;
	host->irq = ahd->platform_data->irq;
	host->max_id = (ahd->features & AHD_WIDE) ? 16 : 8;
	host->max_lun = AHD_NUM_LUNS;
	host->max_channel = 0;
	host->sg_tablesize = AHD_NSEG;
1095
	ahd_set_unit(ahd, ahd_linux_unit++);
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
	sprintf(buf, "scsi%d", host->host_no);
	new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
	if (new_name != NULL) {
		strcpy(new_name, buf);
		ahd_set_name(ahd, new_name);
	}
	host->unique_id = ahd->unit;
	ahd_linux_initialize_scsi_bus(ahd);
	ahd_intr_enable(ahd, TRUE);
	ahd_unlock(ahd, &s);

	host->transportt = ahd_linux_transport_template;

	scsi_add_host(host, &ahd->dev_softc->dev); /* XXX handle failure */
	scsi_scan_host(host);
L
Linus Torvalds 已提交
1111 1112 1113
	return (0);
}

1114 1115
uint64_t
ahd_linux_get_memsize(void)
L
Linus Torvalds 已提交
1116
{
1117 1118 1119 1120
	struct sysinfo si;

	si_meminfo(&si);
	return ((uint64_t)si.totalram << PAGE_SHIFT);
L
Linus Torvalds 已提交
1121 1122
}

1123 1124 1125 1126 1127 1128 1129
/*
 * Place the SCSI bus into a known state by either resetting it,
 * or forcing transfer negotiations on the next command to any
 * target.
 */
static void
ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd)
L
Linus Torvalds 已提交
1130
{
1131 1132
	u_int target_id;
	u_int numtarg;
L
Linus Torvalds 已提交
1133

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	target_id = 0;
	numtarg = 0;

	if (aic79xx_no_reset != 0)
		ahd->flags &= ~AHD_RESET_BUS_A;

	if ((ahd->flags & AHD_RESET_BUS_A) != 0)
		ahd_reset_channel(ahd, 'A', /*initiate_reset*/TRUE);
	else
		numtarg = (ahd->features & AHD_WIDE) ? 16 : 8;
L
Linus Torvalds 已提交
1144 1145

	/*
1146 1147
	 * Force negotiation to async for all targets that
	 * will not see an initial bus reset.
L
Linus Torvalds 已提交
1148
	 */
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
	for (; target_id < numtarg; target_id++) {
		struct ahd_devinfo devinfo;
		struct ahd_initiator_tinfo *tinfo;
		struct ahd_tmode_tstate *tstate;

		tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
					    target_id, &tstate);
		ahd_compile_devinfo(&devinfo, ahd->our_id, target_id,
				    CAM_LUN_WILDCARD, 'A', ROLE_INITIATOR);
		ahd_update_neg_request(ahd, &devinfo, tstate,
				       tinfo, AHD_NEG_ALWAYS);
	}
	/* Give the bus some time to recover */
	if ((ahd->flags & AHD_RESET_BUS_A) != 0) {
		ahd_freeze_simq(ahd);
		init_timer(&ahd->platform_data->reset_timer);
		ahd->platform_data->reset_timer.data = (u_long)ahd;
		ahd->platform_data->reset_timer.expires =
		    jiffies + (AIC79XX_RESET_DELAY * HZ)/1000;
		ahd->platform_data->reset_timer.function =
		    (ahd_linux_callback_t *)ahd_release_simq;
		add_timer(&ahd->platform_data->reset_timer);
	}
L
Linus Torvalds 已提交
1172 1173 1174
}

int
1175
ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
L
Linus Torvalds 已提交
1176
{
1177 1178 1179 1180 1181 1182 1183 1184 1185
	ahd->platform_data =
	    malloc(sizeof(struct ahd_platform_data), M_DEVBUF, M_NOWAIT);
	if (ahd->platform_data == NULL)
		return (ENOMEM);
	memset(ahd->platform_data, 0, sizeof(struct ahd_platform_data));
	ahd->platform_data->irq = AHD_LINUX_NOIRQ;
	ahd_lockinit(ahd);
	init_MUTEX_LOCKED(&ahd->platform_data->eh_sem);
	ahd->seltime = (aic79xx_seltime & 0x3) << 4;
L
Linus Torvalds 已提交
1186 1187 1188
	return (0);
}

1189 1190
void
ahd_platform_free(struct ahd_softc *ahd)
L
Linus Torvalds 已提交
1191
{
1192 1193
	struct scsi_target *starget;
	int i, j;
L
Linus Torvalds 已提交
1194

1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
	if (ahd->platform_data != NULL) {
		/* destroy all of the device and target objects */
		for (i = 0; i < AHD_NUM_TARGETS; i++) {
			starget = ahd->platform_data->starget[i];
			if (starget != NULL) {
				for (j = 0; j < AHD_NUM_LUNS; j++) {
					struct ahd_linux_target *targ =
						scsi_transport_target_data(starget);
					if (targ->sdev[j] == NULL)
						continue;
					targ->sdev[j] = NULL;
				}
				ahd->platform_data->starget[i] = NULL;
			}
		}
L
Linus Torvalds 已提交
1210

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
		if (ahd->platform_data->irq != AHD_LINUX_NOIRQ)
			free_irq(ahd->platform_data->irq, ahd);
		if (ahd->tags[0] == BUS_SPACE_PIO
		 && ahd->bshs[0].ioport != 0)
			release_region(ahd->bshs[0].ioport, 256);
		if (ahd->tags[1] == BUS_SPACE_PIO
		 && ahd->bshs[1].ioport != 0)
			release_region(ahd->bshs[1].ioport, 256);
		if (ahd->tags[0] == BUS_SPACE_MEMIO
		 && ahd->bshs[0].maddr != NULL) {
			iounmap(ahd->bshs[0].maddr);
			release_mem_region(ahd->platform_data->mem_busaddr,
					   0x1000);
		}
1225 1226 1227
		if (ahd->platform_data->host)
			scsi_host_put(ahd->platform_data->host);

1228 1229
		free(ahd->platform_data, M_DEVBUF);
	}
L
Linus Torvalds 已提交
1230 1231
}

1232 1233
void
ahd_platform_init(struct ahd_softc *ahd)
L
Linus Torvalds 已提交
1234
{
1235 1236 1237 1238 1239
	/*
	 * Lookup and commit any modified IO Cell options.
	 */
	if (ahd->unit < NUM_ELEMENTS(aic79xx_iocell_info)) {
		struct ahd_linux_iocell_opts *iocell_opts;
L
Linus Torvalds 已提交
1240

1241 1242 1243 1244 1245 1246 1247
		iocell_opts = &aic79xx_iocell_info[ahd->unit];
		if (iocell_opts->precomp != AIC79XX_DEFAULT_PRECOMP)
			AHD_SET_PRECOMP(ahd, iocell_opts->precomp);
		if (iocell_opts->slewrate != AIC79XX_DEFAULT_SLEWRATE)
			AHD_SET_SLEWRATE(ahd, iocell_opts->slewrate);
		if (iocell_opts->amplitude != AIC79XX_DEFAULT_AMPLITUDE)
			AHD_SET_AMPLITUDE(ahd, iocell_opts->amplitude);
L
Linus Torvalds 已提交
1248 1249 1250 1251
	}

}

1252 1253
void
ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
L
Linus Torvalds 已提交
1254
{
1255 1256 1257 1258
	ahd_platform_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
				SCB_GET_CHANNEL(ahd, scb),
				SCB_GET_LUN(scb), SCB_LIST_NULL,
				ROLE_UNKNOWN, CAM_REQUEUE_REQ);
L
Linus Torvalds 已提交
1259 1260
}

1261 1262 1263
void
ahd_platform_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
		      ahd_queue_alg alg)
L
Linus Torvalds 已提交
1264
{
1265 1266 1267 1268 1269 1270
	struct scsi_target *starget;
	struct ahd_linux_target *targ;
	struct ahd_linux_device *dev;
	struct scsi_device *sdev;
	int was_queuing;
	int now_queuing;
L
Linus Torvalds 已提交
1271

1272 1273 1274 1275 1276 1277
	starget = ahd->platform_data->starget[devinfo->target];
	targ = scsi_transport_target_data(starget);
	BUG_ON(targ == NULL);
	sdev = targ->sdev[devinfo->lun];
	if (sdev == NULL)
		return;
L
Linus Torvalds 已提交
1278

1279
	dev = scsi_transport_device_data(sdev);
L
Linus Torvalds 已提交
1280

1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
	if (dev == NULL)
		return;
	was_queuing = dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED);
	switch (alg) {
	default:
	case AHD_QUEUE_NONE:
		now_queuing = 0;
		break; 
	case AHD_QUEUE_BASIC:
		now_queuing = AHD_DEV_Q_BASIC;
		break;
	case AHD_QUEUE_TAGGED:
		now_queuing = AHD_DEV_Q_TAGGED;
		break;
	}
	if ((dev->flags & AHD_DEV_FREEZE_TIL_EMPTY) == 0
	 && (was_queuing != now_queuing)
	 && (dev->active != 0)) {
		dev->flags |= AHD_DEV_FREEZE_TIL_EMPTY;
		dev->qfrozen++;
L
Linus Torvalds 已提交
1301 1302
	}

1303 1304 1305
	dev->flags &= ~(AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED|AHD_DEV_PERIODIC_OTAG);
	if (now_queuing) {
		u_int usertags;
L
Linus Torvalds 已提交
1306

1307 1308 1309 1310 1311 1312 1313 1314 1315
		usertags = ahd_linux_user_tagdepth(ahd, devinfo);
		if (!was_queuing) {
			/*
			 * Start out agressively and allow our
			 * dynamic queue depth algorithm to take
			 * care of the rest.
			 */
			dev->maxtags = usertags;
			dev->openings = dev->maxtags - dev->active;
L
Linus Torvalds 已提交
1316
		}
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
		if (dev->maxtags == 0) {
			/*
			 * Queueing is disabled by the user.
			 */
			dev->openings = 1;
		} else if (alg == AHD_QUEUE_TAGGED) {
			dev->flags |= AHD_DEV_Q_TAGGED;
			if (aic79xx_periodic_otag != 0)
				dev->flags |= AHD_DEV_PERIODIC_OTAG;
		} else
			dev->flags |= AHD_DEV_Q_BASIC;
	} else {
		/* We can only have one opening. */
		dev->maxtags = 0;
		dev->openings =  1 - dev->active;
L
Linus Torvalds 已提交
1332 1333
	}

1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
	switch ((dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED))) {
	case AHD_DEV_Q_BASIC:
		scsi_adjust_queue_depth(sdev,
					MSG_SIMPLE_TASK,
					dev->openings + dev->active);
		break;
	case AHD_DEV_Q_TAGGED:
		scsi_adjust_queue_depth(sdev,
					MSG_ORDERED_TASK,
					dev->openings + dev->active);
		break;
	default:
L
Linus Torvalds 已提交
1346
		/*
1347 1348 1349 1350
		 * We allow the OS to queue 2 untagged transactions to
		 * us at any time even though we can only execute them
		 * serially on the controller/device.  This should
		 * remove some latency.
L
Linus Torvalds 已提交
1351
		 */
1352 1353 1354 1355
		scsi_adjust_queue_depth(sdev,
					/*NON-TAGGED*/0,
					/*queue depth*/2);
		break;
L
Linus Torvalds 已提交
1356 1357 1358
	}
}

1359 1360 1361
int
ahd_platform_abort_scbs(struct ahd_softc *ahd, int target, char channel,
			int lun, u_int tag, role_t role, uint32_t status)
L
Linus Torvalds 已提交
1362
{
1363
	return 0;
L
Linus Torvalds 已提交
1364 1365
}

1366 1367
static u_int
ahd_linux_user_tagdepth(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
L
Linus Torvalds 已提交
1368
{
1369 1370
	static int warned_user;
	u_int tags;
L
Linus Torvalds 已提交
1371

1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
	tags = 0;
	if ((ahd->user_discenable & devinfo->target_mask) != 0) {
		if (ahd->unit >= NUM_ELEMENTS(aic79xx_tag_info)) {

			if (warned_user == 0) {
				printf(KERN_WARNING
"aic79xx: WARNING: Insufficient tag_info instances\n"
"aic79xx: for installed controllers.  Using defaults\n"
"aic79xx: Please update the aic79xx_tag_info array in\n"
"aic79xx: the aic79xx_osm.c source file.\n");
				warned_user++;
			}
			tags = AHD_MAX_QUEUE;
		} else {
			adapter_tag_info_t *tag_info;

			tag_info = &aic79xx_tag_info[ahd->unit];
			tags = tag_info->tag_commands[devinfo->target_offset];
			if (tags > AHD_MAX_QUEUE)
				tags = AHD_MAX_QUEUE;
L
Linus Torvalds 已提交
1392 1393
		}
	}
1394
	return (tags);
L
Linus Torvalds 已提交
1395 1396 1397
}

/*
1398
 * Determines the queue depth for a given device.
L
Linus Torvalds 已提交
1399 1400
 */
static void
1401
ahd_linux_device_queue_depth(struct scsi_device *sdev)
L
Linus Torvalds 已提交
1402
{
1403 1404 1405
	struct	ahd_devinfo devinfo;
	u_int	tags;
	struct ahd_softc *ahd = *((struct ahd_softc **)sdev->host->hostdata);
L
Linus Torvalds 已提交
1406

1407 1408 1409 1410 1411 1412 1413
	ahd_compile_devinfo(&devinfo,
			    ahd->our_id,
			    sdev->sdev_target->id, sdev->lun,
			    sdev->sdev_target->channel == 0 ? 'A' : 'B',
			    ROLE_INITIATOR);
	tags = ahd_linux_user_tagdepth(ahd, &devinfo);
	if (tags != 0 && sdev->tagged_supported != 0) {
L
Linus Torvalds 已提交
1414

1415 1416 1417 1418 1419 1420 1421
		ahd_set_tags(ahd, &devinfo, AHD_QUEUE_TAGGED);
		ahd_print_devinfo(ahd, &devinfo);
		printf("Tagged Queuing enabled.  Depth %d\n", tags);
	} else {
		ahd_set_tags(ahd, &devinfo, AHD_QUEUE_NONE);
	}
}
L
Linus Torvalds 已提交
1422

1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
static int
ahd_linux_run_command(struct ahd_softc *ahd, struct ahd_linux_device *dev,
		      struct scsi_cmnd *cmd)
{
	struct	 scb *scb;
	struct	 hardware_scb *hscb;
	struct	 ahd_initiator_tinfo *tinfo;
	struct	 ahd_tmode_tstate *tstate;
	u_int	 col_idx;
	uint16_t mask;
L
Linus Torvalds 已提交
1433 1434

	/*
1435
	 * Get an scb to use.
L
Linus Torvalds 已提交
1436
	 */
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
	tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
				    cmd->device->id, &tstate);
	if ((dev->flags & (AHD_DEV_Q_TAGGED|AHD_DEV_Q_BASIC)) == 0
	 || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
		col_idx = AHD_NEVER_COL_IDX;
	} else {
		col_idx = AHD_BUILD_COL_IDX(cmd->device->id,
					    cmd->device->lun);
	}
	if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
		ahd->flags |= AHD_RESOURCE_SHORTAGE;
		return SCSI_MLQUEUE_HOST_BUSY;
L
Linus Torvalds 已提交
1449 1450
	}

1451 1452 1453 1454
	scb->io_ctx = cmd;
	scb->platform_data->dev = dev;
	hscb = scb->hscb;
	cmd->host_scribble = (char *)scb;
L
Linus Torvalds 已提交
1455

1456 1457 1458 1459 1460 1461 1462 1463
	/*
	 * Fill out basics of the HSCB.
	 */
	hscb->control = 0;
	hscb->scsiid = BUILD_SCSIID(ahd, cmd);
	hscb->lun = cmd->device->lun;
	scb->hscb->task_management = 0;
	mask = SCB_GET_TARGET_MASK(ahd, scb);
L
Linus Torvalds 已提交
1464

1465 1466
	if ((ahd->user_discenable & mask) != 0)
		hscb->control |= DISCENB;
L
Linus Torvalds 已提交
1467

1468 1469
	if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0)
		scb->flags |= SCB_PACKETIZED;
L
Linus Torvalds 已提交
1470

1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
	if ((tstate->auto_negotiate & mask) != 0) {
		scb->flags |= SCB_AUTO_NEGOTIATE;
		scb->hscb->control |= MK_MESSAGE;
	}

	if ((dev->flags & (AHD_DEV_Q_TAGGED|AHD_DEV_Q_BASIC)) != 0) {
		int	msg_bytes;
		uint8_t tag_msgs[2];

		msg_bytes = scsi_populate_tag_msg(cmd, tag_msgs);
		if (msg_bytes && tag_msgs[0] != MSG_SIMPLE_TASK) {
			hscb->control |= tag_msgs[0];
			if (tag_msgs[0] == MSG_ORDERED_TASK)
				dev->commands_since_idle_or_otag = 0;
		} else
		if (dev->commands_since_idle_or_otag == AHD_OTAG_THRESH
		 && (dev->flags & AHD_DEV_Q_TAGGED) != 0) {
			hscb->control |= MSG_ORDERED_TASK;
			dev->commands_since_idle_or_otag = 0;
		} else {
			hscb->control |= MSG_SIMPLE_TASK;
L
Linus Torvalds 已提交
1492
		}
1493
	}
L
Linus Torvalds 已提交
1494

1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
	hscb->cdb_len = cmd->cmd_len;
	memcpy(hscb->shared_data.idata.cdb, cmd->cmnd, hscb->cdb_len);

	scb->platform_data->xfer_len = 0;
	ahd_set_residual(scb, 0);
	ahd_set_sense_residual(scb, 0);
	scb->sg_count = 0;
	if (cmd->use_sg != 0) {
		void	*sg;
		struct	 scatterlist *cur_seg;
		u_int	 nseg;
		int	 dir;

		cur_seg = (struct scatterlist *)cmd->request_buffer;
		dir = cmd->sc_data_direction;
		nseg = pci_map_sg(ahd->dev_softc, cur_seg,
				  cmd->use_sg, dir);
		scb->platform_data->xfer_len = 0;
		for (sg = scb->sg_list; nseg > 0; nseg--, cur_seg++) {
			dma_addr_t addr;
			bus_size_t len;

			addr = sg_dma_address(cur_seg);
			len = sg_dma_len(cur_seg);
			scb->platform_data->xfer_len += len;
			sg = ahd_sg_setup(ahd, scb, sg, addr, len,
					  /*last*/nseg == 1);
L
Linus Torvalds 已提交
1522
		}
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
	} else if (cmd->request_bufflen != 0) {
		void *sg;
		dma_addr_t addr;
		int dir;

		sg = scb->sg_list;
		dir = cmd->sc_data_direction;
		addr = pci_map_single(ahd->dev_softc,
				      cmd->request_buffer,
				      cmd->request_bufflen, dir);
		scb->platform_data->xfer_len = cmd->request_bufflen;
		scb->platform_data->buf_busaddr = addr;
		sg = ahd_sg_setup(ahd, scb, sg, addr,
				  cmd->request_bufflen, /*last*/TRUE);
L
Linus Torvalds 已提交
1537
	}
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549

	LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
	dev->openings--;
	dev->active++;
	dev->commands_issued++;

	if ((dev->flags & AHD_DEV_PERIODIC_OTAG) != 0)
		dev->commands_since_idle_or_otag++;
	scb->flags |= SCB_ACTIVE;
	ahd_queue_scb(ahd, scb);

	return 0;
L
Linus Torvalds 已提交
1550 1551
}

1552 1553 1554 1555 1556
/*
 * SCSI controller interrupt handler.
 */
irqreturn_t
ahd_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
L
Linus Torvalds 已提交
1557
{
1558 1559 1560
	struct	ahd_softc *ahd;
	u_long	flags;
	int	ours;
L
Linus Torvalds 已提交
1561

1562 1563 1564 1565 1566
	ahd = (struct ahd_softc *) dev_id;
	ahd_lock(ahd, &flags); 
	ours = ahd_intr(ahd);
	ahd_unlock(ahd, &flags);
	return IRQ_RETVAL(ours);
L
Linus Torvalds 已提交
1567 1568 1569
}

void
1570
ahd_platform_flushwork(struct ahd_softc *ahd)
L
Linus Torvalds 已提交
1571
{
1572

L
Linus Torvalds 已提交
1573 1574 1575
}

void
1576 1577
ahd_send_async(struct ahd_softc *ahd, char channel,
	       u_int target, u_int lun, ac_code code, void *arg)
L
Linus Torvalds 已提交
1578
{
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
	switch (code) {
	case AC_TRANSFER_NEG:
	{
		char	buf[80];
		struct  scsi_target *starget;
		struct	ahd_linux_target *targ;
		struct	info_str info;
		struct	ahd_initiator_tinfo *tinfo;
		struct	ahd_tmode_tstate *tstate;
		unsigned int target_ppr_options;
L
Linus Torvalds 已提交
1589

1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
		BUG_ON(target == CAM_TARGET_WILDCARD);

		info.buffer = buf;
		info.length = sizeof(buf);
		info.offset = 0;
		info.pos = 0;
		tinfo = ahd_fetch_transinfo(ahd, channel, ahd->our_id,
					    target, &tstate);

		/*
		 * Don't bother reporting results while
		 * negotiations are still pending.
		 */
		if (tinfo->curr.period != tinfo->goal.period
		 || tinfo->curr.width != tinfo->goal.width
		 || tinfo->curr.offset != tinfo->goal.offset
		 || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
			if (bootverbose == 0)
				break;

		/*
		 * Don't bother reporting results that
		 * are identical to those last reported.
		 */
		starget = ahd->platform_data->starget[target];
1615
		if (starget == NULL)
1616
			break;
1617
		targ = scsi_transport_target_data(starget);
1618 1619 1620 1621

		target_ppr_options =
			(spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
			+ (spi_qas(starget) ? MSG_EXT_PPR_QAS_REQ : 0)
1622 1623 1624 1625
			+ (spi_iu(starget) ?  MSG_EXT_PPR_IU_REQ : 0)
			+ (spi_rd_strm(starget) ? MSG_EXT_PPR_RD_STRM : 0)
			+ (spi_pcomp_en(starget) ? MSG_EXT_PPR_PCOMP_EN : 0)
			+ (spi_rti(starget) ? MSG_EXT_PPR_RTI : 0)
1626 1627
			+ (spi_wr_flow(starget) ? MSG_EXT_PPR_WR_FLOW : 0)
			+ (spi_hold_mcs(starget) ? MSG_EXT_PPR_HOLD_MCS : 0);
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638

		if (tinfo->curr.period == spi_period(starget)
		    && tinfo->curr.width == spi_width(starget)
		    && tinfo->curr.offset == spi_offset(starget)
		 && tinfo->curr.ppr_options == target_ppr_options)
			if (bootverbose == 0)
				break;

		spi_period(starget) = tinfo->curr.period;
		spi_width(starget) = tinfo->curr.width;
		spi_offset(starget) = tinfo->curr.offset;
1639 1640 1641
		spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ ? 1 : 0;
		spi_qas(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ ? 1 : 0;
		spi_iu(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ ? 1 : 0;
1642 1643 1644 1645
		spi_rd_strm(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_RD_STRM ? 1 : 0;
		spi_pcomp_en(starget) =  tinfo->curr.ppr_options & MSG_EXT_PPR_PCOMP_EN ? 1 : 0;
		spi_rti(starget) =  tinfo->curr.ppr_options &  MSG_EXT_PPR_RTI ? 1 : 0;
		spi_wr_flow(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_WR_FLOW ? 1 : 0;
1646
		spi_hold_mcs(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_HOLD_MCS ? 1 : 0;
1647
		spi_display_xfer_agreement(starget);
L
Linus Torvalds 已提交
1648
		break;
1649 1650 1651 1652 1653 1654
	}
        case AC_SENT_BDR:
	{
		WARN_ON(lun != CAM_LUN_WILDCARD);
		scsi_report_device_reset(ahd->platform_data->host,
					 channel - 'A', target);
L
Linus Torvalds 已提交
1655 1656
		break;
	}
1657 1658 1659 1660
        case AC_BUS_RESET:
		if (ahd->platform_data->host != NULL) {
			scsi_report_bus_reset(ahd->platform_data->host,
					      channel - 'A');
L
Linus Torvalds 已提交
1661
		}
1662 1663 1664 1665
                break;
        default:
                panic("ahd_send_async: Unexpected async event");
        }
L
Linus Torvalds 已提交
1666 1667
}

1668 1669 1670 1671 1672
/*
 * Calls the higher level scsi done function and frees the scb.
 */
void
ahd_done(struct ahd_softc *ahd, struct scb *scb)
L
Linus Torvalds 已提交
1673
{
1674 1675
	struct scsi_cmnd *cmd;
	struct	  ahd_linux_device *dev;
L
Linus Torvalds 已提交
1676

1677 1678 1679 1680
	if ((scb->flags & SCB_ACTIVE) == 0) {
		printf("SCB %d done'd twice\n", SCB_GET_TAG(scb));
		ahd_dump_card_state(ahd);
		panic("Stopping for safety");
L
Linus Torvalds 已提交
1681
	}
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
	LIST_REMOVE(scb, pending_links);
	cmd = scb->io_ctx;
	dev = scb->platform_data->dev;
	dev->active--;
	dev->openings++;
	if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
		cmd->result &= ~(CAM_DEV_QFRZN << 16);
		dev->qfrozen--;
	}
	ahd_linux_unmap_scb(ahd, scb);
L
Linus Torvalds 已提交
1692 1693

	/*
1694 1695 1696 1697
	 * Guard against stale sense data.
	 * The Linux mid-layer assumes that sense
	 * was retrieved anytime the first byte of
	 * the sense buffer looks "sane".
L
Linus Torvalds 已提交
1698
	 */
1699 1700 1701
	cmd->sense_buffer[0] = 0;
	if (ahd_get_transaction_status(scb) == CAM_REQ_INPROG) {
		uint32_t amount_xferred;
L
Linus Torvalds 已提交
1702

1703 1704 1705
		amount_xferred =
		    ahd_get_transfer_length(scb) - ahd_get_residual(scb);
		if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
L
Linus Torvalds 已提交
1706
#ifdef AHD_DEBUG
1707 1708 1709 1710
			if ((ahd_debug & AHD_SHOW_MISC) != 0) {
				ahd_print_path(ahd, scb);
				printf("Set CAM_UNCOR_PARITY\n");
			}
L
Linus Torvalds 已提交
1711
#endif
1712 1713
			ahd_set_transaction_status(scb, CAM_UNCOR_PARITY);
#ifdef AHD_REPORT_UNDERFLOWS
L
Linus Torvalds 已提交
1714
		/*
1715 1716 1717 1718 1719 1720 1721
		 * This code is disabled by default as some
		 * clients of the SCSI system do not properly
		 * initialize the underflow parameter.  This
		 * results in spurious termination of commands
		 * that complete as expected (e.g. underflow is
		 * allowed as command can return variable amounts
		 * of data.
L
Linus Torvalds 已提交
1722
		 */
1723 1724
		} else if (amount_xferred < scb->io_ctx->underflow) {
			u_int i;
L
Linus Torvalds 已提交
1725

1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
			ahd_print_path(ahd, scb);
			printf("CDB:");
			for (i = 0; i < scb->io_ctx->cmd_len; i++)
				printf(" 0x%x", scb->io_ctx->cmnd[i]);
			printf("\n");
			ahd_print_path(ahd, scb);
			printf("Saw underflow (%ld of %ld bytes). "
			       "Treated as error\n",
				ahd_get_residual(scb),
				ahd_get_transfer_length(scb));
			ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
#endif
		} else {
			ahd_set_transaction_status(scb, CAM_REQ_CMP);
L
Linus Torvalds 已提交
1740
		}
1741 1742 1743
	} else if (ahd_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
		ahd_linux_handle_scsi_status(ahd, cmd->device, scb);
	}
L
Linus Torvalds 已提交
1744

1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
	if (dev->openings == 1
	 && ahd_get_transaction_status(scb) == CAM_REQ_CMP
	 && ahd_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
		dev->tag_success_count++;
	/*
	 * Some devices deal with temporary internal resource
	 * shortages by returning queue full.  When the queue
	 * full occurrs, we throttle back.  Slowly try to get
	 * back to our previous queue depth.
	 */
	if ((dev->openings + dev->active) < dev->maxtags
	 && dev->tag_success_count > AHD_TAG_SUCCESS_INTERVAL) {
		dev->tag_success_count = 0;
		dev->openings++;
	}
L
Linus Torvalds 已提交
1760

1761 1762
	if (dev->active == 0)
		dev->commands_since_idle_or_otag = 0;
L
Linus Torvalds 已提交
1763

1764 1765 1766 1767 1768 1769 1770 1771 1772
	if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
		printf("Recovery SCB completes\n");
		if (ahd_get_transaction_status(scb) == CAM_BDR_SENT
		 || ahd_get_transaction_status(scb) == CAM_REQ_ABORTED)
			ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
		if ((ahd->platform_data->flags & AHD_SCB_UP_EH_SEM) != 0) {
			ahd->platform_data->flags &= ~AHD_SCB_UP_EH_SEM;
			up(&ahd->platform_data->eh_sem);
		}
L
Linus Torvalds 已提交
1773
	}
1774 1775 1776

	ahd_free_scb(ahd, scb);
	ahd_linux_queue_cmd_complete(ahd, cmd);
L
Linus Torvalds 已提交
1777 1778 1779
}

static void
1780 1781
ahd_linux_handle_scsi_status(struct ahd_softc *ahd,
			     struct scsi_device *sdev, struct scb *scb)
L
Linus Torvalds 已提交
1782
{
1783 1784
	struct	ahd_devinfo devinfo;
	struct ahd_linux_device *dev = scsi_transport_device_data(sdev);
L
Linus Torvalds 已提交
1785

1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
	ahd_compile_devinfo(&devinfo,
			    ahd->our_id,
			    sdev->sdev_target->id, sdev->lun,
			    sdev->sdev_target->channel == 0 ? 'A' : 'B',
			    ROLE_INITIATOR);
	
	/*
	 * We don't currently trust the mid-layer to
	 * properly deal with queue full or busy.  So,
	 * when one occurs, we tell the mid-layer to
	 * unconditionally requeue the command to us
	 * so that we can retry it ourselves.  We also
	 * implement our own throttling mechanism so
	 * we don't clobber the device with too many
	 * commands.
	 */
	switch (ahd_get_scsi_status(scb)) {
	default:
		break;
	case SCSI_STATUS_CHECK_COND:
	case SCSI_STATUS_CMD_TERMINATED:
	{
		struct scsi_cmnd *cmd;
L
Linus Torvalds 已提交
1809 1810

		/*
1811 1812
		 * Copy sense information to the OS's cmd
		 * structure if it is available.
L
Linus Torvalds 已提交
1813
		 */
1814 1815 1816 1817 1818
		cmd = scb->io_ctx;
		if ((scb->flags & (SCB_SENSE|SCB_PKT_SENSE)) != 0) {
			struct scsi_status_iu_header *siu;
			u_int sense_size;
			u_int sense_offset;
L
Linus Torvalds 已提交
1819

1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
			if (scb->flags & SCB_SENSE) {
				sense_size = MIN(sizeof(struct scsi_sense_data)
					       - ahd_get_sense_residual(scb),
						 sizeof(cmd->sense_buffer));
				sense_offset = 0;
			} else {
				/*
				 * Copy only the sense data into the provided
				 * buffer.
				 */
				siu = (struct scsi_status_iu_header *)
				    scb->sense_data;
				sense_size = MIN(scsi_4btoul(siu->sense_length),
						sizeof(cmd->sense_buffer));
				sense_offset = SIU_SENSE_OFFSET(siu);
			}
L
Linus Torvalds 已提交
1836

1837 1838 1839 1840 1841
			memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
			memcpy(cmd->sense_buffer,
			       ahd_get_sense_buf(ahd, scb)
			       + sense_offset, sense_size);
			cmd->result |= (DRIVER_SENSE << 24);
L
Linus Torvalds 已提交
1842 1843

#ifdef AHD_DEBUG
1844 1845
			if (ahd_debug & AHD_SHOW_SENSE) {
				int i;
L
Linus Torvalds 已提交
1846

1847 1848 1849
				printf("Copied %d bytes of sense data at %d:",
				       sense_size, sense_offset);
				for (i = 0; i < sense_size; i++) {
L
Linus Torvalds 已提交
1850
					if ((i & 0xF) == 0)
1851 1852
						printf("\n");
					printf("0x%x ", cmd->sense_buffer[i]);
L
Linus Torvalds 已提交
1853 1854 1855 1856 1857 1858
				}
				printf("\n");
			}
#endif
		}
		break;
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
	}
	case SCSI_STATUS_QUEUE_FULL:
		/*
		 * By the time the core driver has returned this
		 * command, all other commands that were queued
		 * to us but not the device have been returned.
		 * This ensures that dev->active is equal to
		 * the number of commands actually queued to
		 * the device.
		 */
		dev->tag_success_count = 0;
		if (dev->active != 0) {
L
Linus Torvalds 已提交
1871
			/*
1872 1873
			 * Drop our opening count to the number
			 * of commands currently outstanding.
L
Linus Torvalds 已提交
1874
			 */
1875
			dev->openings = 0;
L
Linus Torvalds 已提交
1876
#ifdef AHD_DEBUG
1877 1878 1879 1880
			if ((ahd_debug & AHD_SHOW_QFULL) != 0) {
				ahd_print_path(ahd, scb);
				printf("Dropping tag count to %d\n",
				       dev->active);
L
Linus Torvalds 已提交
1881 1882
			}
#endif
1883
			if (dev->active == dev->tags_on_last_queuefull) {
L
Linus Torvalds 已提交
1884

1885
				dev->last_queuefull_same_count++;
L
Linus Torvalds 已提交
1886
				/*
1887 1888 1889 1890 1891 1892
				 * If we repeatedly see a queue full
				 * at the same queue depth, this
				 * device has a fixed number of tag
				 * slots.  Lock in this tag depth
				 * so we stop seeing queue fulls from
				 * this device.
L
Linus Torvalds 已提交
1893
				 */
1894 1895 1896 1897 1898 1899
				if (dev->last_queuefull_same_count
				 == AHD_LOCK_TAGS_COUNT) {
					dev->maxtags = dev->active;
					ahd_print_path(ahd, scb);
					printf("Locking max tag count at %d\n",
					       dev->active);
L
Linus Torvalds 已提交
1900
				}
1901 1902 1903
			} else {
				dev->tags_on_last_queuefull = dev->active;
				dev->last_queuefull_same_count = 0;
L
Linus Torvalds 已提交
1904
			}
1905 1906 1907 1908 1909
			ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
			ahd_set_scsi_status(scb, SCSI_STATUS_OK);
			ahd_platform_set_tags(ahd, &devinfo,
				     (dev->flags & AHD_DEV_Q_BASIC)
				   ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
L
Linus Torvalds 已提交
1910 1911 1912
			break;
		}
		/*
1913 1914
		 * Drop down to a single opening, and treat this
		 * as if the target returned BUSY SCSI status.
L
Linus Torvalds 已提交
1915
		 */
1916 1917 1918 1919 1920 1921
		dev->openings = 1;
		ahd_platform_set_tags(ahd, &devinfo,
			     (dev->flags & AHD_DEV_Q_BASIC)
			   ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
		ahd_set_scsi_status(scb, SCSI_STATUS_BUSY);
	}
L
Linus Torvalds 已提交
1922 1923
}

1924 1925
static void
ahd_linux_queue_cmd_complete(struct ahd_softc *ahd, struct scsi_cmnd *cmd)
L
Linus Torvalds 已提交
1926 1927
{
	/*
1928 1929 1930 1931
	 * Map CAM error codes into Linux Error codes.  We
	 * avoid the conversion so that the DV code has the
	 * full error information available when making
	 * state change decisions.
L
Linus Torvalds 已提交
1932
	 */
1933 1934 1935
	{
		uint32_t status;
		u_int new_status;
L
Linus Torvalds 已提交
1936

1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
		status = ahd_cmd_get_transaction_status(cmd);
		switch (status) {
		case CAM_REQ_INPROG:
		case CAM_REQ_CMP:
		case CAM_SCSI_STATUS_ERROR:
			new_status = DID_OK;
			break;
		case CAM_REQ_ABORTED:
			new_status = DID_ABORT;
			break;
		case CAM_BUSY:
			new_status = DID_BUS_BUSY;
			break;
		case CAM_REQ_INVALID:
		case CAM_PATH_INVALID:
			new_status = DID_BAD_TARGET;
			break;
		case CAM_SEL_TIMEOUT:
			new_status = DID_NO_CONNECT;
			break;
		case CAM_SCSI_BUS_RESET:
		case CAM_BDR_SENT:
			new_status = DID_RESET;
			break;
		case CAM_UNCOR_PARITY:
			new_status = DID_PARITY;
			break;
		case CAM_CMD_TIMEOUT:
			new_status = DID_TIME_OUT;
			break;
		case CAM_UA_ABORT:
		case CAM_REQ_CMP_ERR:
		case CAM_AUTOSENSE_FAIL:
		case CAM_NO_HBA:
		case CAM_DATA_RUN_ERR:
		case CAM_UNEXP_BUSFREE:
		case CAM_SEQUENCE_FAIL:
		case CAM_CCB_LEN_ERR:
		case CAM_PROVIDE_FAIL:
		case CAM_REQ_TERMIO:
		case CAM_UNREC_HBA_ERROR:
		case CAM_REQ_TOO_BIG:
			new_status = DID_ERROR;
			break;
		case CAM_REQUEUE_REQ:
			new_status = DID_REQUEUE;
			break;
		default:
			/* We should never get here */
			new_status = DID_ERROR;
			break;
		}

		ahd_cmd_set_transaction_status(cmd, new_status);
	}

	cmd->scsi_done(cmd);
L
Linus Torvalds 已提交
1994 1995 1996
}

static void
1997
ahd_linux_sem_timeout(u_long arg)
L
Linus Torvalds 已提交
1998
{
1999 2000
	struct	ahd_softc *ahd;
	u_long	s;
L
Linus Torvalds 已提交
2001

2002 2003 2004 2005 2006 2007 2008 2009
	ahd = (struct ahd_softc *)arg;

	ahd_lock(ahd, &s);
	if ((ahd->platform_data->flags & AHD_SCB_UP_EH_SEM) != 0) {
		ahd->platform_data->flags &= ~AHD_SCB_UP_EH_SEM;
		up(&ahd->platform_data->eh_sem);
	}
	ahd_unlock(ahd, &s);
L
Linus Torvalds 已提交
2010 2011 2012
}

void
2013
ahd_freeze_simq(struct ahd_softc *ahd)
L
Linus Torvalds 已提交
2014
{
2015 2016 2017 2018 2019 2020
	ahd->platform_data->qfrozen++;
	if (ahd->platform_data->qfrozen == 1) {
		scsi_block_requests(ahd->platform_data->host);
		ahd_platform_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
					CAM_LUN_WILDCARD, SCB_LIST_NULL,
					ROLE_INITIATOR, CAM_REQUEUE_REQ);
L
Linus Torvalds 已提交
2021 2022 2023 2024
	}
}

void
2025
ahd_release_simq(struct ahd_softc *ahd)
L
Linus Torvalds 已提交
2026
{
2027 2028
	u_long s;
	int    unblock_reqs;
L
Linus Torvalds 已提交
2029

2030 2031 2032 2033 2034 2035
	unblock_reqs = 0;
	ahd_lock(ahd, &s);
	if (ahd->platform_data->qfrozen > 0)
		ahd->platform_data->qfrozen--;
	if (ahd->platform_data->qfrozen == 0) {
		unblock_reqs = 1;
L
Linus Torvalds 已提交
2036
	}
2037
	ahd_unlock(ahd, &s);
L
Linus Torvalds 已提交
2038
	/*
2039 2040 2041 2042
	 * There is still a race here.  The mid-layer
	 * should keep its own freeze count and use
	 * a bottom half handler to run the queues
	 * so we can unblock with our own lock held.
L
Linus Torvalds 已提交
2043
	 */
2044 2045 2046
	if (unblock_reqs)
		scsi_unblock_requests(ahd->platform_data->host);
}
L
Linus Torvalds 已提交
2047

2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
static int
ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
{
	struct ahd_softc *ahd;
	struct ahd_linux_device *dev;
	struct scb *pending_scb;
	u_int  saved_scbptr;
	u_int  active_scbptr;
	u_int  last_phase;
	u_int  saved_scsiid;
	u_int  cdb_byte;
	int    retval;
	int    was_paused;
	int    paused;
	int    wait;
	int    disconnected;
	ahd_mode_state saved_modes;

	pending_scb = NULL;
	paused = FALSE;
	wait = FALSE;
	ahd = *(struct ahd_softc **)cmd->device->host->hostdata;

J
Jeff Garzik 已提交
2071 2072
	scmd_printk(KERN_INFO, cmd,
	       "Attempting to queue a%s message:",
2073 2074 2075 2076 2077 2078 2079 2080
	       flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");

	printf("CDB:");
	for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
		printf(" 0x%x", cmd->cmnd[cdb_byte]);
	printf("\n");

	spin_lock_irq(&ahd->platform_data->spin_lock);
L
Linus Torvalds 已提交
2081 2082

	/*
2083 2084 2085 2086 2087
	 * First determine if we currently own this command.
	 * Start by searching the device queue.  If not found
	 * there, check the pending_scb list.  If not found
	 * at all, and the system wanted us to just abort the
	 * command, return success.
L
Linus Torvalds 已提交
2088
	 */
2089 2090 2091 2092 2093 2094 2095
	dev = scsi_transport_device_data(cmd->device);

	if (dev == NULL) {
		/*
		 * No target device for this command exists,
		 * so we must not still own the command.
		 */
J
Jeff Garzik 已提交
2096
		scmd_printk(KERN_INFO, cmd, "Is not an active device\n");
2097 2098 2099
		retval = SUCCESS;
		goto no_cmd;
	}
L
Linus Torvalds 已提交
2100 2101

	/*
2102
	 * See if we can find a matching cmd in the pending list.
L
Linus Torvalds 已提交
2103
	 */
2104 2105 2106
	LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
		if (pending_scb->io_ctx == cmd)
			break;
L
Linus Torvalds 已提交
2107 2108
	}

2109
	if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
L
Linus Torvalds 已提交
2110

2111 2112
		/* Any SCB for this device will do for a target reset */
		LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
2113 2114 2115
		  	if (ahd_match_scb(ahd, pending_scb,
					  scmd_id(cmd),
					  scmd_channel(cmd) + 'A',
2116 2117 2118
					  CAM_LUN_WILDCARD,
					  SCB_LIST_NULL, ROLE_INITIATOR) == 0)
				break;
L
Linus Torvalds 已提交
2119 2120 2121
		}
	}

2122
	if (pending_scb == NULL) {
J
Jeff Garzik 已提交
2123
		scmd_printk(KERN_INFO, cmd, "Command not found\n");
2124
		goto no_cmd;
L
Linus Torvalds 已提交
2125 2126
	}

2127 2128 2129 2130 2131 2132 2133
	if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
		/*
		 * We can't queue two recovery actions using the same SCB
		 */
		retval = FAILED;
		goto  done;
	}
L
Linus Torvalds 已提交
2134 2135

	/*
2136 2137 2138 2139
	 * Ensure that the card doesn't do anything
	 * behind our back.  Also make sure that we
	 * didn't "just" miss an interrupt that would
	 * affect this cmd.
L
Linus Torvalds 已提交
2140
	 */
2141 2142 2143
	was_paused = ahd_is_paused(ahd);
	ahd_pause_and_flushwork(ahd);
	paused = TRUE;
L
Linus Torvalds 已提交
2144

2145
	if ((pending_scb->flags & SCB_ACTIVE) == 0) {
J
Jeff Garzik 已提交
2146
		scmd_printk(KERN_INFO, cmd, "Command already completed\n");
2147 2148
		goto no_cmd;
	}
L
Linus Torvalds 已提交
2149

2150 2151 2152
	printf("%s: At time of recovery, card was %spaused\n",
	       ahd_name(ahd), was_paused ? "" : "not ");
	ahd_dump_card_state(ahd);
L
Linus Torvalds 已提交
2153

2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
	disconnected = TRUE;
	if (flag == SCB_ABORT) {
		if (ahd_search_qinfifo(ahd, cmd->device->id, 
				       cmd->device->channel + 'A',
				       cmd->device->lun, 
				       pending_scb->hscb->tag,
				       ROLE_INITIATOR, CAM_REQ_ABORTED,
				       SEARCH_COMPLETE) > 0) {
			printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
			       ahd_name(ahd), cmd->device->channel, 
			       cmd->device->id, cmd->device->lun);
			retval = SUCCESS;
			goto done;
		}
	} else if (ahd_search_qinfifo(ahd, cmd->device->id,
				      cmd->device->channel + 'A',
				      cmd->device->lun, pending_scb->hscb->tag,
				      ROLE_INITIATOR, /*status*/0,
				      SEARCH_COUNT) > 0) {
		disconnected = FALSE;
	}
L
Linus Torvalds 已提交
2175

2176 2177 2178 2179 2180 2181 2182
	saved_modes = ahd_save_modes(ahd);
	ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
	last_phase = ahd_inb(ahd, LASTPHASE);
	saved_scbptr = ahd_get_scbptr(ahd);
	active_scbptr = saved_scbptr;
	if (disconnected && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
		struct scb *bus_scb;
L
Linus Torvalds 已提交
2183

2184 2185 2186 2187 2188 2189 2190
		bus_scb = ahd_lookup_scb(ahd, active_scbptr);
		if (bus_scb == pending_scb)
			disconnected = FALSE;
		else if (flag != SCB_ABORT
			 && ahd_inb(ahd, SAVED_SCSIID) == pending_scb->hscb->scsiid
			 && ahd_inb(ahd, SAVED_LUN) == SCB_GET_LUN(pending_scb))
			disconnected = FALSE;
L
Linus Torvalds 已提交
2191
	}
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201

	/*
	 * At this point, pending_scb is the scb associated with the
	 * passed in command.  That command is currently active on the
	 * bus or is in the disconnected state.
	 */
	saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
	if (last_phase != P_BUSFREE
	 && (SCB_GET_TAG(pending_scb) == active_scbptr
	     || (flag == SCB_DEVICE_RESET
2202
		 && SCSIID_TARGET(ahd, saved_scsiid) == scmd_id(cmd)))) {
2203

L
Linus Torvalds 已提交
2204
		/*
2205 2206
		 * We're active on the bus, so assert ATN
		 * and hope that the target responds.
L
Linus Torvalds 已提交
2207
		 */
2208 2209 2210 2211
		pending_scb = ahd_lookup_scb(ahd, active_scbptr);
		pending_scb->flags |= SCB_RECOVERY_SCB|flag;
		ahd_outb(ahd, MSG_OUT, HOST_MSG);
		ahd_outb(ahd, SCSISIGO, last_phase|ATNO);
J
Jeff Garzik 已提交
2212
		scmd_printk(KERN_INFO, cmd, "Device is active, asserting ATN\n");
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
		wait = TRUE;
	} else if (disconnected) {

		/*
		 * Actually re-queue this SCB in an attempt
		 * to select the device before it reconnects.
		 */
		pending_scb->flags |= SCB_RECOVERY_SCB|SCB_ABORT;
		ahd_set_scbptr(ahd, SCB_GET_TAG(pending_scb));
		pending_scb->hscb->cdb_len = 0;
		pending_scb->hscb->task_attribute = 0;
		pending_scb->hscb->task_management = SIU_TASKMGMT_ABORT_TASK;

		if ((pending_scb->flags & SCB_PACKETIZED) != 0) {
L
Linus Torvalds 已提交
2227
			/*
2228 2229 2230 2231 2232
			 * Mark the SCB has having an outstanding
			 * task management function.  Should the command
			 * complete normally before the task management
			 * function can be sent, the host will be notified
			 * to abort our requeued SCB.
L
Linus Torvalds 已提交
2233
			 */
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
			ahd_outb(ahd, SCB_TASK_MANAGEMENT,
				 pending_scb->hscb->task_management);
		} else {
			/*
			 * If non-packetized, set the MK_MESSAGE control
			 * bit indicating that we desire to send a message.
			 * We also set the disconnected flag since there is
			 * no guarantee that our SCB control byte matches
			 * the version on the card.  We don't want the
			 * sequencer to abort the command thinking an
			 * unsolicited reselection occurred.
			 */
			pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;

			/*
			 * The sequencer will never re-reference the
			 * in-core SCB.  To make sure we are notified
			 * during reslection, set the MK_MESSAGE flag in
			 * the card's copy of the SCB.
			 */
			ahd_outb(ahd, SCB_CONTROL,
				 ahd_inb(ahd, SCB_CONTROL)|MK_MESSAGE);
L
Linus Torvalds 已提交
2256
		}
2257

L
Linus Torvalds 已提交
2258
		/*
2259 2260 2261
		 * Clear out any entries in the QINFIFO first
		 * so we are the next SCB for this target
		 * to run.
L
Linus Torvalds 已提交
2262
		 */
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272
		ahd_search_qinfifo(ahd, cmd->device->id,
				   cmd->device->channel + 'A', cmd->device->lun,
				   SCB_LIST_NULL, ROLE_INITIATOR,
				   CAM_REQUEUE_REQ, SEARCH_COMPLETE);
		ahd_qinfifo_requeue_tail(ahd, pending_scb);
		ahd_set_scbptr(ahd, saved_scbptr);
		ahd_print_path(ahd, pending_scb);
		printf("Device is disconnected, re-queuing SCB\n");
		wait = TRUE;
	} else {
J
Jeff Garzik 已提交
2273
		scmd_printk(KERN_INFO, cmd, "Unable to deliver message\n");
2274 2275
		retval = FAILED;
		goto done;
L
Linus Torvalds 已提交
2276 2277
	}

2278
no_cmd:
L
Linus Torvalds 已提交
2279
	/*
2280 2281 2282 2283
	 * Our assumption is that if we don't have the command, no
	 * recovery action was required, so we return success.  Again,
	 * the semantics of the mid-layer recovery engine are not
	 * well defined, so this may change in time.
L
Linus Torvalds 已提交
2284
	 */
2285 2286 2287 2288 2289 2290 2291
	retval = SUCCESS;
done:
	if (paused)
		ahd_unpause(ahd);
	if (wait) {
		struct timer_list timer;
		int ret;
L
Linus Torvalds 已提交
2292

2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
		ahd->platform_data->flags |= AHD_SCB_UP_EH_SEM;
		spin_unlock_irq(&ahd->platform_data->spin_lock);
		init_timer(&timer);
		timer.data = (u_long)ahd;
		timer.expires = jiffies + (5 * HZ);
		timer.function = ahd_linux_sem_timeout;
		add_timer(&timer);
		printf("Recovery code sleeping\n");
		down(&ahd->platform_data->eh_sem);
		printf("Recovery code awake\n");
        	ret = del_timer_sync(&timer);
		if (ret == 0) {
			printf("Timer Expired\n");
			retval = FAILED;
L
Linus Torvalds 已提交
2307
		}
2308
		spin_lock_irq(&ahd->platform_data->spin_lock);
L
Linus Torvalds 已提交
2309
	}
2310 2311
	spin_unlock_irq(&ahd->platform_data->spin_lock);
	return (retval);
L
Linus Torvalds 已提交
2312 2313
}

2314
static void ahd_linux_set_width(struct scsi_target *starget, int width)
L
Linus Torvalds 已提交
2315
{
2316 2317 2318 2319
	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
	struct ahd_devinfo devinfo;
	unsigned long flags;
L
Linus Torvalds 已提交
2320

2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
			    starget->channel + 'A', ROLE_INITIATOR);
	ahd_lock(ahd, &flags);
	ahd_set_width(ahd, &devinfo, width, AHD_TRANS_GOAL, FALSE);
	ahd_unlock(ahd, &flags);
}

static void ahd_linux_set_period(struct scsi_target *starget, int period)
{
	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
	struct ahd_tmode_tstate *tstate;
	struct ahd_initiator_tinfo *tinfo 
		= ahd_fetch_transinfo(ahd,
				      starget->channel + 'A',
				      shost->this_id, starget->id, &tstate);
	struct ahd_devinfo devinfo;
	unsigned int ppr_options = tinfo->goal.ppr_options;
2339
	unsigned int dt;
2340 2341
	unsigned long flags;
	unsigned long offset = tinfo->goal.offset;
L
Linus Torvalds 已提交
2342

2343 2344 2345 2346
#ifdef AHD_DEBUG
	if ((ahd_debug & AHD_SHOW_DV) != 0)
		printf("%s: set period to %d\n", ahd_name(ahd), period);
#endif
2347 2348
	if (offset == 0)
		offset = MAX_OFFSET;
L
Linus Torvalds 已提交
2349

2350 2351 2352 2353 2354 2355
	if (period < 8)
		period = 8;
	if (period < 10) {
		ppr_options |= MSG_EXT_PPR_DT_REQ;
		if (period == 8)
			ppr_options |= MSG_EXT_PPR_IU_REQ;
L
Linus Torvalds 已提交
2356 2357
	}

2358 2359
	dt = ppr_options & MSG_EXT_PPR_DT_REQ;

2360 2361
	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
			    starget->channel + 'A', ROLE_INITIATOR);
L
Linus Torvalds 已提交
2362

2363 2364 2365 2366
	/* all PPR requests apart from QAS require wide transfers */
	if (ppr_options & ~MSG_EXT_PPR_QAS_REQ) {
		if (spi_width(starget) == 0)
			ppr_options &= MSG_EXT_PPR_QAS_REQ;
L
Linus Torvalds 已提交
2367
	}
2368

2369 2370 2371
	ahd_find_syncrate(ahd, &period, &ppr_options,
			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);

2372 2373 2374 2375
	ahd_lock(ahd, &flags);
	ahd_set_syncrate(ahd, &devinfo, period, offset,
			 ppr_options, AHD_TRANS_GOAL, FALSE);
	ahd_unlock(ahd, &flags);
L
Linus Torvalds 已提交
2376 2377
}

2378
static void ahd_linux_set_offset(struct scsi_target *starget, int offset)
L
Linus Torvalds 已提交
2379
{
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
	struct ahd_tmode_tstate *tstate;
	struct ahd_initiator_tinfo *tinfo 
		= ahd_fetch_transinfo(ahd,
				      starget->channel + 'A',
				      shost->this_id, starget->id, &tstate);
	struct ahd_devinfo devinfo;
	unsigned int ppr_options = 0;
	unsigned int period = 0;
2390
	unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2391 2392
	unsigned long flags;

2393 2394 2395 2396 2397
#ifdef AHD_DEBUG
	if ((ahd_debug & AHD_SHOW_DV) != 0)
		printf("%s: set offset to %d\n", ahd_name(ahd), offset);
#endif

2398 2399 2400 2401 2402
	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
			    starget->channel + 'A', ROLE_INITIATOR);
	if (offset != 0) {
		period = tinfo->goal.period;
		ppr_options = tinfo->goal.ppr_options;
2403 2404
		ahd_find_syncrate(ahd, &period, &ppr_options, 
				  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
L
Linus Torvalds 已提交
2405
	}
2406

2407 2408 2409 2410
	ahd_lock(ahd, &flags);
	ahd_set_syncrate(ahd, &devinfo, period, offset, ppr_options,
			 AHD_TRANS_GOAL, FALSE);
	ahd_unlock(ahd, &flags);
L
Linus Torvalds 已提交
2411 2412
}

2413
static void ahd_linux_set_dt(struct scsi_target *starget, int dt)
L
Linus Torvalds 已提交
2414
{
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
	struct ahd_tmode_tstate *tstate;
	struct ahd_initiator_tinfo *tinfo 
		= ahd_fetch_transinfo(ahd,
				      starget->channel + 'A',
				      shost->this_id, starget->id, &tstate);
	struct ahd_devinfo devinfo;
	unsigned int ppr_options = tinfo->goal.ppr_options
		& ~MSG_EXT_PPR_DT_REQ;
	unsigned int period = tinfo->goal.period;
2426
	unsigned int width = tinfo->goal.width;
2427 2428
	unsigned long flags;

2429 2430 2431 2432 2433
#ifdef AHD_DEBUG
	if ((ahd_debug & AHD_SHOW_DV) != 0)
		printf("%s: %s DT\n", ahd_name(ahd), 
		       dt ? "enabling" : "disabling");
#endif
2434 2435
	if (dt) {
		ppr_options |= MSG_EXT_PPR_DT_REQ;
2436 2437
		if (!width)
			ahd_linux_set_width(starget, 1);
2438 2439 2440 2441 2442 2443
	} else {
		if (period <= 9)
			period = 10; /* If resetting DT, period must be >= 25ns */
		/* IU is invalid without DT set */
		ppr_options &= ~MSG_EXT_PPR_IU_REQ;
	}
2444 2445 2446 2447
	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
			    starget->channel + 'A', ROLE_INITIATOR);
	ahd_find_syncrate(ahd, &period, &ppr_options,
			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2448

2449 2450 2451 2452
	ahd_lock(ahd, &flags);
	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
			 ppr_options, AHD_TRANS_GOAL, FALSE);
	ahd_unlock(ahd, &flags);
L
Linus Torvalds 已提交
2453 2454
}

2455
static void ahd_linux_set_qas(struct scsi_target *starget, int qas)
L
Linus Torvalds 已提交
2456
{
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
	struct ahd_tmode_tstate *tstate;
	struct ahd_initiator_tinfo *tinfo 
		= ahd_fetch_transinfo(ahd,
				      starget->channel + 'A',
				      shost->this_id, starget->id, &tstate);
	struct ahd_devinfo devinfo;
	unsigned int ppr_options = tinfo->goal.ppr_options
		& ~MSG_EXT_PPR_QAS_REQ;
	unsigned int period = tinfo->goal.period;
2468
	unsigned int dt;
2469 2470
	unsigned long flags;

2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481
#ifdef AHD_DEBUG
	if ((ahd_debug & AHD_SHOW_DV) != 0)
		printf("%s: %s QAS\n", ahd_name(ahd), 
		       qas ? "enabling" : "disabling");
#endif

	if (qas) {
		ppr_options |= MSG_EXT_PPR_QAS_REQ; 
	}

	dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2482 2483 2484 2485 2486

	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
			    starget->channel + 'A', ROLE_INITIATOR);
	ahd_find_syncrate(ahd, &period, &ppr_options,
			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2487

2488 2489 2490 2491
	ahd_lock(ahd, &flags);
	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
			 ppr_options, AHD_TRANS_GOAL, FALSE);
	ahd_unlock(ahd, &flags);
L
Linus Torvalds 已提交
2492 2493
}

2494
static void ahd_linux_set_iu(struct scsi_target *starget, int iu)
L
Linus Torvalds 已提交
2495
{
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
	struct ahd_tmode_tstate *tstate;
	struct ahd_initiator_tinfo *tinfo 
		= ahd_fetch_transinfo(ahd,
				      starget->channel + 'A',
				      shost->this_id, starget->id, &tstate);
	struct ahd_devinfo devinfo;
	unsigned int ppr_options = tinfo->goal.ppr_options
		& ~MSG_EXT_PPR_IU_REQ;
	unsigned int period = tinfo->goal.period;
2507
	unsigned int dt;
2508 2509
	unsigned long flags;

2510 2511 2512 2513 2514 2515 2516
#ifdef AHD_DEBUG
	if ((ahd_debug & AHD_SHOW_DV) != 0)
		printf("%s: %s IU\n", ahd_name(ahd),
		       iu ? "enabling" : "disabling");
#endif

	if (iu) {
2517
		ppr_options |= MSG_EXT_PPR_IU_REQ;
2518 2519 2520 2521
		ppr_options |= MSG_EXT_PPR_DT_REQ; /* IU requires DT */
	}

	dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2522 2523 2524 2525 2526

	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
			    starget->channel + 'A', ROLE_INITIATOR);
	ahd_find_syncrate(ahd, &period, &ppr_options,
			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2527

2528 2529 2530 2531 2532
	ahd_lock(ahd, &flags);
	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
			 ppr_options, AHD_TRANS_GOAL, FALSE);
	ahd_unlock(ahd, &flags);
}
L
Linus Torvalds 已提交
2533

2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549
static void ahd_linux_set_rd_strm(struct scsi_target *starget, int rdstrm)
{
	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
	struct ahd_tmode_tstate *tstate;
	struct ahd_initiator_tinfo *tinfo 
		= ahd_fetch_transinfo(ahd,
				      starget->channel + 'A',
				      shost->this_id, starget->id, &tstate);
	struct ahd_devinfo devinfo;
	unsigned int ppr_options = tinfo->goal.ppr_options
		& ~MSG_EXT_PPR_RD_STRM;
	unsigned int period = tinfo->goal.period;
	unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
	unsigned long flags;

2550 2551 2552 2553 2554 2555
#ifdef AHD_DEBUG
	if ((ahd_debug & AHD_SHOW_DV) != 0)
		printf("%s: %s Read Streaming\n", ahd_name(ahd), 
		       rdstrm  ? "enabling" : "disabling");
#endif

2556 2557 2558 2559 2560 2561 2562
	if (rdstrm)
		ppr_options |= MSG_EXT_PPR_RD_STRM;

	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
			    starget->channel + 'A', ROLE_INITIATOR);
	ahd_find_syncrate(ahd, &period, &ppr_options,
			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679

	ahd_lock(ahd, &flags);
	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
			 ppr_options, AHD_TRANS_GOAL, FALSE);
	ahd_unlock(ahd, &flags);
}

static void ahd_linux_set_wr_flow(struct scsi_target *starget, int wrflow)
{
	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
	struct ahd_tmode_tstate *tstate;
	struct ahd_initiator_tinfo *tinfo 
		= ahd_fetch_transinfo(ahd,
				      starget->channel + 'A',
				      shost->this_id, starget->id, &tstate);
	struct ahd_devinfo devinfo;
	unsigned int ppr_options = tinfo->goal.ppr_options
		& ~MSG_EXT_PPR_WR_FLOW;
	unsigned int period = tinfo->goal.period;
	unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
	unsigned long flags;

#ifdef AHD_DEBUG
	if ((ahd_debug & AHD_SHOW_DV) != 0)
		printf("%s: %s Write Flow Control\n", ahd_name(ahd),
		       wrflow ? "enabling" : "disabling");
#endif

	if (wrflow)
		ppr_options |= MSG_EXT_PPR_WR_FLOW;

	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
			    starget->channel + 'A', ROLE_INITIATOR);
	ahd_find_syncrate(ahd, &period, &ppr_options,
			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);

	ahd_lock(ahd, &flags);
	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
			 ppr_options, AHD_TRANS_GOAL, FALSE);
	ahd_unlock(ahd, &flags);
}

static void ahd_linux_set_rti(struct scsi_target *starget, int rti)
{
	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
	struct ahd_tmode_tstate *tstate;
	struct ahd_initiator_tinfo *tinfo 
		= ahd_fetch_transinfo(ahd,
				      starget->channel + 'A',
				      shost->this_id, starget->id, &tstate);
	struct ahd_devinfo devinfo;
	unsigned int ppr_options = tinfo->goal.ppr_options
		& ~MSG_EXT_PPR_RTI;
	unsigned int period = tinfo->goal.period;
	unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
	unsigned long flags;

	if ((ahd->features & AHD_RTI) == 0) {
#ifdef AHD_DEBUG
		if ((ahd_debug & AHD_SHOW_DV) != 0)
			printf("%s: RTI not available\n", ahd_name(ahd));
#endif
		return;
	}

#ifdef AHD_DEBUG
	if ((ahd_debug & AHD_SHOW_DV) != 0)
		printf("%s: %s RTI\n", ahd_name(ahd),
		       rti ? "enabling" : "disabling");
#endif

	if (rti)
		ppr_options |= MSG_EXT_PPR_RTI;

	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
			    starget->channel + 'A', ROLE_INITIATOR);
	ahd_find_syncrate(ahd, &period, &ppr_options,
			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);

	ahd_lock(ahd, &flags);
	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
			 ppr_options, AHD_TRANS_GOAL, FALSE);
	ahd_unlock(ahd, &flags);
}

static void ahd_linux_set_pcomp_en(struct scsi_target *starget, int pcomp)
{
	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
	struct ahd_tmode_tstate *tstate;
	struct ahd_initiator_tinfo *tinfo 
		= ahd_fetch_transinfo(ahd,
				      starget->channel + 'A',
				      shost->this_id, starget->id, &tstate);
	struct ahd_devinfo devinfo;
	unsigned int ppr_options = tinfo->goal.ppr_options
		& ~MSG_EXT_PPR_PCOMP_EN;
	unsigned int period = tinfo->goal.period;
	unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
	unsigned long flags;

#ifdef AHD_DEBUG
	if ((ahd_debug & AHD_SHOW_DV) != 0)
		printf("%s: %s Precompensation\n", ahd_name(ahd), 
		       pcomp ? "Enable" : "Disable");
#endif

	if (pcomp)
		ppr_options |= MSG_EXT_PPR_PCOMP_EN;

	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
			    starget->channel + 'A', ROLE_INITIATOR);
	ahd_find_syncrate(ahd, &period, &ppr_options,
			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);

2680 2681 2682 2683
	ahd_lock(ahd, &flags);
	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
			 ppr_options, AHD_TRANS_GOAL, FALSE);
	ahd_unlock(ahd, &flags);
L
Linus Torvalds 已提交
2684 2685
}

2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717
static void ahd_linux_set_hold_mcs(struct scsi_target *starget, int hold)
{
	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
	struct ahd_tmode_tstate *tstate;
	struct ahd_initiator_tinfo *tinfo 
		= ahd_fetch_transinfo(ahd,
				      starget->channel + 'A',
				      shost->this_id, starget->id, &tstate);
	struct ahd_devinfo devinfo;
	unsigned int ppr_options = tinfo->goal.ppr_options
		& ~MSG_EXT_PPR_HOLD_MCS;
	unsigned int period = tinfo->goal.period;
	unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
	unsigned long flags;

	if (hold)
		ppr_options |= MSG_EXT_PPR_HOLD_MCS;

	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
			    starget->channel + 'A', ROLE_INITIATOR);
	ahd_find_syncrate(ahd, &period, &ppr_options,
			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);

	ahd_lock(ahd, &flags);
	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
			 ppr_options, AHD_TRANS_GOAL, FALSE);
	ahd_unlock(ahd, &flags);
}



2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
static struct spi_function_template ahd_linux_transport_functions = {
	.set_offset	= ahd_linux_set_offset,
	.show_offset	= 1,
	.set_period	= ahd_linux_set_period,
	.show_period	= 1,
	.set_width	= ahd_linux_set_width,
	.show_width	= 1,
	.set_dt		= ahd_linux_set_dt,
	.show_dt	= 1,
	.set_iu		= ahd_linux_set_iu,
	.show_iu	= 1,
	.set_qas	= ahd_linux_set_qas,
	.show_qas	= 1,
	.set_rd_strm	= ahd_linux_set_rd_strm,
	.show_rd_strm	= 1,
2733 2734 2735 2736 2737 2738
	.set_wr_flow	= ahd_linux_set_wr_flow,
	.show_wr_flow	= 1,
	.set_rti	= ahd_linux_set_rti,
	.show_rti	= 1,
	.set_pcomp_en	= ahd_linux_set_pcomp_en,
	.show_pcomp_en	= 1,
2739 2740
	.set_hold_mcs	= ahd_linux_set_hold_mcs,
	.show_hold_mcs	= 1,
2741 2742
};

L
Linus Torvalds 已提交
2743 2744 2745
static int __init
ahd_linux_init(void)
{
2746 2747 2748 2749 2750 2751 2752 2753 2754 2755
	int	error = 0;

	/*
	 * If we've been passed any parameters, process them now.
	 */
	if (aic79xx)
		aic79xx_setup(aic79xx);

	ahd_linux_transport_template =
		spi_attach_transport(&ahd_linux_transport_functions);
2756 2757
	if (!ahd_linux_transport_template)
		return -ENODEV;
2758

2759 2760 2761 2762
	scsi_transport_reserve_target(ahd_linux_transport_template,
				      sizeof(struct ahd_linux_target));
	scsi_transport_reserve_device(ahd_linux_transport_template,
				      sizeof(struct ahd_linux_device));
2763

2764 2765 2766 2767
	error = ahd_linux_pci_init();
	if (error)
		spi_release_transport(ahd_linux_transport_template);
	return error;
L
Linus Torvalds 已提交
2768 2769 2770 2771 2772 2773
}

static void __exit
ahd_linux_exit(void)
{
	ahd_linux_pci_exit();
2774
	spi_release_transport(ahd_linux_transport_template);
L
Linus Torvalds 已提交
2775 2776 2777 2778
}

module_init(ahd_linux_init);
module_exit(ahd_linux_exit);