aic79xx_osm.c 75.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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;

Linus Torvalds's avatar
Linus Torvalds committed
51
52
#include <linux/init.h>		/* __setup */
#include <linux/mm.h>		/* For fetching system memory size */
53
#include <linux/blkdev.h>		/* For block_size() */
Linus Torvalds's avatar
Linus Torvalds committed
54
#include <linux/delay.h>	/* For ssleep/msleep */
55
#include <linux/device.h>
Linus Torvalds's avatar
Linus Torvalds committed
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

/*
 * 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;

/*
 * 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.
 */
Adrian Bunk's avatar
Adrian Bunk committed
296
static uint32_t aic79xx_periodic_otag;
Linus Torvalds's avatar
Linus Torvalds committed
297

Hannes Reinecke's avatar
Hannes Reinecke committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* Some storage boxes are using an LSI chip which has a bug making it
 * impossible to use aic79xx Rev B chip in 320 speeds.  The following
 * storage boxes have been reported to be buggy:
 * EonStor 3U 16-Bay: U16U-G3A3
 * EonStor 2U 12-Bay: U12U-G3A3
 * SentinelRAID: 2500F R5 / R6
 * SentinelRAID: 2500F R1
 * SentinelRAID: 2500F/1500F
 * SentinelRAID: 150F
 * 
 * To get around this LSI bug, you can set your board to 160 mode
 * or you can enable the SLOWCRC bit.
 */
uint32_t aic79xx_slowcrc;

Linus Torvalds's avatar
Linus Torvalds committed
313
314
315
316
317
318
319
320
321
/*
 * 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);
322
module_param(aic79xx, charp, 0444);
Linus Torvalds's avatar
Linus Torvalds committed
323
MODULE_PARM_DESC(aic79xx,
324
"period-delimited options string:\n"
Linus Torvalds's avatar
Linus Torvalds committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"	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"
"	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"
Hannes Reinecke's avatar
Hannes Reinecke committed
341
"	slowcrc			Turn on the SLOWCRC bit (Rev B only)\n"		 
Linus Torvalds's avatar
Linus Torvalds committed
342
343
344
345
346
347
348
"\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"
349
);
Linus Torvalds's avatar
Linus Torvalds committed
350
351

static void ahd_linux_handle_scsi_status(struct ahd_softc *,
352
					 struct scsi_device *,
Linus Torvalds's avatar
Linus Torvalds committed
353
354
					 struct scb *);
static void ahd_linux_queue_cmd_complete(struct ahd_softc *ahd,
355
					 struct scsi_cmnd *cmd);
356
static int ahd_linux_queue_abort_cmd(struct scsi_cmnd *cmd);
Linus Torvalds's avatar
Linus Torvalds committed
357
358
359
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);
360
static void ahd_linux_device_queue_depth(struct scsi_device *);
Hannes Reinecke's avatar
Hannes Reinecke committed
361
static int ahd_linux_run_command(struct ahd_softc*,
362
				 struct ahd_linux_device *,
Hannes Reinecke's avatar
Hannes Reinecke committed
363
				 struct scsi_cmnd *);
Linus Torvalds's avatar
Linus Torvalds committed
364
static void ahd_linux_setup_tag_info_global(char *p);
365
static int  aic79xx_setup(char *c);
366
367
368

static int ahd_linux_unit;

Linus Torvalds's avatar
Linus Torvalds committed
369
370
371
372
373
374
375

/****************************** 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)
{
376
	struct scsi_cmnd *cmd;
Linus Torvalds's avatar
Linus Torvalds committed
377
378
379
	int direction;

	cmd = scb->io_ctx;
380
	direction = cmd->sc_data_direction;
Linus Torvalds's avatar
Linus Torvalds committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
	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)						\
396
	(((scmd_id(cmd) << TID_SHIFT) & TID) | (ahd)->our_id)
Linus Torvalds's avatar
Linus Torvalds committed
397
398
399
400
401
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

/*
 * 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
430
ahd_linux_queue(struct scsi_cmnd * cmd, void (*scsi_done) (struct scsi_cmnd *))
Linus Torvalds's avatar
Linus Torvalds committed
431
432
{
	struct	 ahd_softc *ahd;
433
	struct	 ahd_linux_device *dev = scsi_transport_device_data(cmd->device);
434
	int rtn = SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds's avatar
Linus Torvalds committed
435
436
437

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

438
439
440
	cmd->scsi_done = scsi_done;
	cmd->result = CAM_REQ_INPROG << 16;
	rtn = ahd_linux_run_command(ahd, dev, cmd);
Linus Torvalds's avatar
Linus Torvalds committed
441

442
	return rtn;
Linus Torvalds's avatar
Linus Torvalds committed
443
444
}

445
446
447
448
449
450
451
452
453
454
455
456
457
458
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];
}

Linus Torvalds's avatar
Linus Torvalds committed
459
static int
460
ahd_linux_target_alloc(struct scsi_target *starget)
Linus Torvalds's avatar
Linus Torvalds committed
461
{
462
463
	struct	ahd_softc *ahd =
		*((struct ahd_softc **)dev_to_shost(&starget->dev)->hostdata);
464
	struct seeprom_config *sc = ahd->seep_config;
465
466
467
468
469
470
	unsigned long flags;
	struct scsi_target **ahd_targp = ahd_linux_target_in_softc(starget);
	struct ahd_devinfo devinfo;
	struct ahd_initiator_tinfo *tinfo;
	struct ahd_tmode_tstate *tstate;
	char channel = starget->channel + 'A';
Linus Torvalds's avatar
Linus Torvalds committed
471

472
473
474
475
476
477
	ahd_lock(ahd, &flags);

	BUG_ON(*ahd_targp != NULL);

	*ahd_targp = starget;

478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
	if (sc) {
		int flags = sc->device_flags[starget->id];

		tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
					    starget->id, &tstate);

		if ((flags  & CFPACKETIZED) == 0) {
			/* Do not negotiate packetized transfers */
			spi_rd_strm(starget) = 0;
			spi_pcomp_en(starget) = 0;
			spi_rti(starget) = 0;
			spi_wr_flow(starget) = 0;
			spi_hold_mcs(starget) = 0;
		} else {
			if ((ahd->features & AHD_RTI) == 0)
				spi_rti(starget) = 0;
		}

		if ((flags & CFQAS) == 0)
			spi_qas(starget) = 0;

		/* Transinfo values have been set to BIOS settings */
		spi_max_width(starget) = (flags & CFWIDEB) ? 1 : 0;
		spi_min_period(starget) = tinfo->user.period;
		spi_max_offset(starget) = tinfo->user.offset;
	}

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
	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);
	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;
Linus Torvalds's avatar
Linus Torvalds committed
525
526
527
}

static int
528
ahd_linux_slave_alloc(struct scsi_device *sdev)
Linus Torvalds's avatar
Linus Torvalds committed
529
{
530
531
532
	struct	ahd_softc *ahd =
		*((struct ahd_softc **)sdev->host->hostdata);
	struct ahd_linux_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
533
534

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

	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;

Linus Torvalds's avatar
Linus Torvalds committed
546
	/*
547
548
549
	 * Set maxtags to 0.  This will be changed if we
	 * later determine that we are dealing with
	 * a tagged queuing capable device.
Linus Torvalds's avatar
Linus Torvalds committed
550
	 */
551
552
	dev->maxtags = 0;
	
Linus Torvalds's avatar
Linus Torvalds committed
553
554
555
	return (0);
}

556
557
558
559
560
561
562
static int
ahd_linux_slave_configure(struct scsi_device *sdev)
{
	struct	ahd_softc *ahd;

	ahd = *((struct ahd_softc **)sdev->host->hostdata);
	if (bootverbose)
563
		sdev_printk(KERN_INFO, sdev, "Slave Configure\n");
564
565
566
567
568
569
570
571
572
573

	ahd_linux_device_queue_depth(sdev);

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

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
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
#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
624
625
626
ahd_linux_abort(struct scsi_cmnd *cmd)
{
	int error;
627
628
	
	error = ahd_linux_queue_abort_cmd(cmd);
629
630
631
632
633
634
635
636
637
638

	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)
{
639
640
641
642
643
644
645
646
647
648
	struct ahd_softc *ahd;
	struct ahd_linux_device *dev;
	struct scb *reset_scb;
	u_int  cdb_byte;
	int    retval = SUCCESS;
	int    paused;
	int    wait;
	struct	ahd_initiator_tinfo *tinfo;
	struct	ahd_tmode_tstate *tstate;
	unsigned long flags;
649
	DECLARE_COMPLETION_ONSTACK(done);
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
	reset_scb = NULL;
	paused = FALSE;
	wait = FALSE;
	ahd = *(struct ahd_softc **)cmd->device->host->hostdata;

	scmd_printk(KERN_INFO, cmd,
		    "Attempting to queue a TARGET RESET message:");

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

	/*
	 * Determine if we currently own this command.
	 */
	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.
		 */
		scmd_printk(KERN_INFO, cmd, "Is not an active device\n");
		return SUCCESS;
	}

	/*
	 * Generate us a new SCB
	 */
	reset_scb = ahd_get_scb(ahd, AHD_NEVER_COL_IDX);
	if (!reset_scb) {
		scmd_printk(KERN_INFO, cmd, "No SCB available\n");
		return FAILED;
	}

	tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
				    cmd->device->id, &tstate);
	reset_scb->io_ctx = cmd;
	reset_scb->platform_data->dev = dev;
	reset_scb->sg_count = 0;
	ahd_set_residual(reset_scb, 0);
	ahd_set_sense_residual(reset_scb, 0);
	reset_scb->platform_data->xfer_len = 0;
	reset_scb->hscb->control = 0;
	reset_scb->hscb->scsiid = BUILD_SCSIID(ahd,cmd);
	reset_scb->hscb->lun = cmd->device->lun;
	reset_scb->hscb->cdb_len = 0;
	reset_scb->hscb->task_management = SIU_TASKMGMT_LUN_RESET;
	reset_scb->flags |= SCB_DEVICE_RESET|SCB_RECOVERY_SCB|SCB_ACTIVE;
	if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
		reset_scb->flags |= SCB_PACKETIZED;
	} else {
		reset_scb->hscb->control |= MK_MESSAGE;
	}
	dev->openings--;
	dev->active++;
	dev->commands_issued++;

	ahd_lock(ahd, &flags);

	LIST_INSERT_HEAD(&ahd->pending_scbs, reset_scb, pending_links);
	ahd_queue_scb(ahd, reset_scb);

	ahd->platform_data->eh_done = &done;
	ahd_unlock(ahd, &flags);

	printf("%s: Device reset code sleeping\n", ahd_name(ahd));
	if (!wait_for_completion_timeout(&done, 5 * HZ)) {
		ahd_lock(ahd, &flags);
		ahd->platform_data->eh_done = NULL;
		ahd_unlock(ahd, &flags);
		printf("%s: Device reset timer expired (active %d)\n",
		       ahd_name(ahd), dev->active);
		retval = FAILED;
	}
	printf("%s: Device reset returning 0x%x\n", ahd_name(ahd), retval);

	return (retval);
730
731
732
733
734
735
736
}

/*
 * Reset the SCSI bus.
 */
static int
ahd_linux_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds's avatar
Linus Torvalds committed
737
738
{
	struct ahd_softc *ahd;
739
	int    found;
740
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
741
742

	ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
743
744
745
746
747
#ifdef AHD_DEBUG
	if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
		printf("%s: Bus reset called for cmd %p\n",
		       ahd_name(ahd), cmd);
#endif
748
749
	ahd_lock(ahd, &flags);

750
	found = ahd_reset_channel(ahd, scmd_channel(cmd) + 'A',
751
				  /*initiate reset*/TRUE);
752
	ahd_unlock(ahd, &flags);
Linus Torvalds's avatar
Linus Torvalds committed
753

754
755
756
	if (bootverbose)
		printf("%s: SCSI bus reset delivered. "
		       "%d SCBs aborted.\n", ahd_name(ahd), found);
Linus Torvalds's avatar
Linus Torvalds committed
757

758
759
	return (SUCCESS);
}
Linus Torvalds's avatar
Linus Torvalds committed
760

761
762
763
struct scsi_host_template aic79xx_driver_template = {
	.module			= THIS_MODULE,
	.name			= "aic79xx",
764
	.proc_name		= "aic79xx",
765
766
767
768
769
770
771
772
773
774
775
	.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,
776
	.max_sectors		= 8192,
777
778
779
780
781
782
783
	.cmd_per_lun		= 2,
	.use_clustering		= ENABLE_CLUSTERING,
	.slave_alloc		= ahd_linux_slave_alloc,
	.slave_configure	= ahd_linux_slave_configure,
	.target_alloc		= ahd_linux_target_alloc,
	.target_destroy		= ahd_linux_target_destroy,
};
Linus Torvalds's avatar
Linus Torvalds committed
784

785
786
787
788
789
790
791
792
793
794
795
796
797
798
/******************************** 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);
Linus Torvalds's avatar
Linus Torvalds committed
799
800

	/*
801
802
803
804
805
	 * 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.
Linus Torvalds's avatar
Linus Torvalds committed
806
	 */
807
808
809
810
811
812
	dmat->alignment = alignment;
	dmat->boundary = boundary;
	dmat->maxsize = maxsize;
	*ret_tag = dmat;
	return (0);
}
Linus Torvalds's avatar
Linus Torvalds committed
813

814
815
816
817
818
void
ahd_dma_tag_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat)
{
	free(dmat, M_DEVBUF);
}
Linus Torvalds's avatar
Linus Torvalds committed
819

820
821
822
823
824
825
826
827
828
829
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);
}
Linus Torvalds's avatar
Linus Torvalds committed
830

831
832
833
834
835
836
837
838
839
840
841
842
843
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)
{
Linus Torvalds's avatar
Linus Torvalds committed
844
	/*
845
846
	 * Assume for now that this will only be used during
	 * initialization and not for per-transaction buffer mapping.
Linus Torvalds's avatar
Linus Torvalds committed
847
	 */
848
	bus_dma_segment_t stack_sg;
Linus Torvalds's avatar
Linus Torvalds committed
849

850
851
852
853
854
	stack_sg.ds_addr = map;
	stack_sg.ds_len = dmat->maxsize;
	cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
	return (0);
}
Linus Torvalds's avatar
Linus Torvalds committed
855

856
857
858
859
void
ahd_dmamap_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
{
}
Linus Torvalds's avatar
Linus Torvalds committed
860

861
862
863
864
865
866
int
ahd_dmamap_unload(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
{
	/* Nothing to do */
	return (0);
}
Linus Torvalds's avatar
Linus Torvalds committed
867

868
869
870
871
/********************* Platform Dependent Functions ***************************/
static void
ahd_linux_setup_iocell_info(u_long index, int instance, int targ, int32_t value)
{
Linus Torvalds's avatar
Linus Torvalds committed
872

873
	if ((instance >= 0)
874
	 && (instance < ARRAY_SIZE(aic79xx_iocell_info))) {
875
876
877
878
879
880
		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);
Linus Torvalds's avatar
Linus Torvalds committed
881
	}
882
}
Linus Torvalds's avatar
Linus Torvalds committed
883

884
885
886
887
static void
ahd_linux_setup_tag_info_global(char *p)
{
	int tags, i, j;
Linus Torvalds's avatar
Linus Torvalds committed
888

889
890
891
	tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
	printf("Setting Global Tags= %d\n", tags);

892
	for (i = 0; i < ARRAY_SIZE(aic79xx_tag_info); i++) {
893
894
		for (j = 0; j < AHD_NUM_TARGETS; j++) {
			aic79xx_tag_info[i].tag_commands[j] = tags;
Linus Torvalds's avatar
Linus Torvalds committed
895
896
897
898
899
		}
	}
}

static void
900
ahd_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
Linus Torvalds's avatar
Linus Torvalds committed
901
902
{

903
	if ((instance >= 0) && (targ >= 0)
904
	 && (instance < ARRAY_SIZE(aic79xx_tag_info))
905
906
907
908
	 && (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);
Linus Torvalds's avatar
Linus Torvalds committed
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
983
984
985
986
987
988
989
990
991
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);
}

Linus Torvalds's avatar
Linus Torvalds committed
992
/*
993
994
995
 * 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
Linus Torvalds's avatar
Linus Torvalds committed
996
997
 */
static int
998
aic79xx_setup(char *s)
Linus Torvalds's avatar
Linus Torvalds committed
999
{
1000
1001
1002
	int	i, n;
	char   *p;
	char   *end;
Linus Torvalds's avatar
Linus Torvalds committed
1003

1004
1005
1006
1007
1008
1009
1010
1011
	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},
Linus Torvalds's avatar
Linus Torvalds committed
1012
#ifdef AHD_DEBUG
1013
		{ "debug", &ahd_debug },
Linus Torvalds's avatar
Linus Torvalds committed
1014
#endif
1015
1016
1017
1018
1019
1020
1021
1022
		{ "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 },
Hannes Reinecke's avatar
Hannes Reinecke committed
1023
		{ "slowcrc", &aic79xx_slowcrc },
1024
	};
Linus Torvalds's avatar
Linus Torvalds committed
1025

1026
	end = strchr(s, '\0');
Linus Torvalds's avatar
Linus Torvalds committed
1027

1028
	/*
1029
	 * XXX ia64 gcc isn't smart enough to know that ARRAY_SIZE
1030
	 * will never be 0 in this case.
1031
1032
	 */
	n = 0;
1033
1034
1035
1036

	while ((p = strsep(&s, ",.")) != NULL) {
		if (*p == '\0')
			continue;
1037
		for (i = 0; i < ARRAY_SIZE(options); i++) {
1038
1039
1040
1041
1042

			n = strlen(options[i].name);
			if (strncmp(options[i].name, p, n) == 0)
				break;
		}
1043
		if (i == ARRAY_SIZE(options))
1044
1045
1046
1047
1048
			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) {
1049
			s = ahd_parse_brace_option("tag_info", p + n, end,
1050
1051
			    2, ahd_linux_setup_tag_info, 0);
		} else if (strncmp(p, "slewrate", n) == 0) {
1052
			s = ahd_parse_brace_option("slewrate",
1053
1054
1055
			    p + n, end, 1, ahd_linux_setup_iocell_info,
			    AIC79XX_SLEWRATE_INDEX);
		} else if (strncmp(p, "precomp", n) == 0) {
1056
			s = ahd_parse_brace_option("precomp",
1057
1058
1059
			    p + n, end, 1, ahd_linux_setup_iocell_info,
			    AIC79XX_PRECOMP_INDEX);
		} else if (strncmp(p, "amplitude", n) == 0) {
1060
			s = ahd_parse_brace_option("amplitude",
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
			    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;
Linus Torvalds's avatar
Linus Torvalds committed
1072
1073
}

1074
1075
1076
__setup("aic79xx=", aic79xx_setup);

uint32_t aic79xx_verbose;
Linus Torvalds's avatar
Linus Torvalds committed
1077
1078

int
1079
ahd_linux_register_host(struct ahd_softc *ahd, struct scsi_host_template *template)
Linus Torvalds's avatar
Linus Torvalds committed
1080
{
1081
1082
1083
1084
	char	buf[80];
	struct	Scsi_Host *host;
	char	*new_name;
	u_long	s;
1085
	int	retval;
Linus Torvalds's avatar
Linus Torvalds committed
1086

1087
1088
1089
	template->name = ahd->description;
	host = scsi_host_alloc(template, sizeof(struct ahd_softc *));
	if (host == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
1090
1091
		return (ENOMEM);

1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
	*((struct ahd_softc **)host->hostdata) = ahd;
	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;
1103
	ahd_lock(ahd, &s);
1104
	ahd_set_unit(ahd, ahd_linux_unit++);
1105
	ahd_unlock(ahd, &s);
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
	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);

	host->transportt = ahd_linux_transport_template;

1118
1119
1120
1121
1122
1123
1124
	retval = scsi_add_host(host, &ahd->dev_softc->dev);
	if (retval) {
		printk(KERN_WARNING "aic79xx: scsi_add_host failed\n");
		scsi_host_put(host);
		return retval;
	}

1125
	scsi_scan_host(host);
1126
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1127
1128
}

1129
1130
uint64_t
ahd_linux_get_memsize(void)
Linus Torvalds's avatar
Linus Torvalds committed
1131
{
1132
1133
1134
1135
	struct sysinfo si;

	si_meminfo(&si);
	return ((uint64_t)si.totalram << PAGE_SHIFT);
Linus Torvalds's avatar
Linus Torvalds committed
1136
1137
}

1138
1139
1140
1141
1142
1143
1144
/*
 * 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)
Linus Torvalds's avatar
Linus Torvalds committed
1145
{
1146
1147
	u_int target_id;
	u_int numtarg;
1148
	unsigned long s;
Linus Torvalds's avatar
Linus Torvalds committed
1149

1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
	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;
Linus Torvalds's avatar
Linus Torvalds committed
1160

1161
1162
	ahd_lock(ahd, &s);

Linus Torvalds's avatar
Linus Torvalds committed
1163
	/*
1164
1165
	 * Force negotiation to async for all targets that
	 * will not see an initial bus reset.
Linus Torvalds's avatar
Linus Torvalds committed
1166
	 */
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
	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);
	}
1179
	ahd_unlock(ahd, &s);
1180
1181
1182
	/* Give the bus some time to recover */
	if ((ahd->flags & AHD_RESET_BUS_A) != 0) {
		ahd_freeze_simq(ahd);
1183
1184
		msleep(AIC79XX_RESET_DELAY);
		ahd_release_simq(ahd);
1185
	}
Linus Torvalds's avatar
Linus Torvalds committed
1186
1187
1188
}

int
1189
ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
Linus Torvalds's avatar
Linus Torvalds committed
1190
{
1191
1192
1193
1194
1195
1196
1197
1198
	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);
	ahd->seltime = (aic79xx_seltime & 0x3) << 4;
Linus Torvalds's avatar
Linus Torvalds committed
1199
1200
1201
	return (0);
}

1202
1203
void
ahd_platform_free(struct ahd_softc *ahd)
Linus Torvalds's avatar
Linus Torvalds committed
1204
{
1205
	struct scsi_target *starget;
1206
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
1207

1208
1209
1210
1211
1212
1213
1214
1215
	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) {
				ahd->platform_data->starget[i] = NULL;
			}
		}
Linus Torvalds's avatar
Linus Torvalds committed
1216

1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
		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);
		}
1231
1232
1233
		if (ahd->platform_data->host)
			scsi_host_put(ahd->platform_data->host);

1234
1235
		free(ahd->platform_data, M_DEVBUF);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1236
1237
}

1238
1239
void
ahd_platform_init(struct ahd_softc *ahd)
Linus Torvalds's avatar
Linus Torvalds committed
1240
{
1241
1242
1243
	/*
	 * Lookup and commit any modified IO Cell options.
	 */
1244
	if (ahd->unit < ARRAY_SIZE(aic79xx_iocell_info)) {
1245
		struct ahd_linux_iocell_opts *iocell_opts;
Linus Torvalds's avatar
Linus Torvalds committed
1246

1247
1248
1249
1250
1251
1252
1253
		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);
Linus Torvalds's avatar
Linus Torvalds committed
1254
1255
1256
1257
	}

}

1258
1259
void
ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
Linus Torvalds's avatar
Linus Torvalds committed
1260
{
1261
1262
1263
1264
	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);
Linus Torvalds's avatar
Linus Torvalds committed
1265
1266
}

1267
void
1268
1269
ahd_platform_set_tags(struct ahd_softc *ahd, struct scsi_device *sdev,
		      struct ahd_devinfo *devinfo, ahd_queue_alg alg)
Linus Torvalds's avatar
Linus Torvalds committed
1270
{
1271
1272
1273
	struct ahd_linux_device *dev;
	int was_queuing;
	int now_queuing;
Linus Torvalds's avatar
Linus Torvalds committed
1274

1275
1276
	if (sdev == NULL)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1277

1278
	dev = scsi_transport_device_data(sdev);
Linus Torvalds's avatar
Linus Torvalds committed
1279

1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
	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++;
Linus Torvalds's avatar
Linus Torvalds committed
1300
1301
	}

1302
1303
1304
	dev->flags &= ~(AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED|AHD_DEV_PERIODIC_OTAG);
	if (now_queuing) {
		u_int usertags;
Linus Torvalds's avatar
Linus Torvalds committed
1305

1306
1307
1308
1309
1310
1311
1312
1313
1314
		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;
Linus Torvalds's avatar
Linus Torvalds committed
1315
		}
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
		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;
Linus Torvalds's avatar
Linus Torvalds committed
1331
1332
	}

1333
1334
	switch ((dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED))) {
	case AHD_DEV_Q_BASIC:
1335
1336
		scsi_set_tag_type(sdev, MSG_SIMPLE_TASK);
		scsi_activate_tcq(sdev, dev->openings + dev->active);
1337
1338
		break;
	case AHD_DEV_Q_TAGGED:
1339
1340
		scsi_set_tag_type(sdev, MSG_ORDERED_TASK);
		scsi_activate_tcq(sdev, dev->openings + dev->active);
1341
1342
		break;
	default:
Linus Torvalds's avatar
Linus Torvalds committed
1343
		/*
1344
1345
1346
1347
		 * 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.
Linus Torvalds's avatar
Linus Torvalds committed
1348
		 */
1349
		scsi_deactivate_tcq(sdev, 1);
1350
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1351
1352
1353
	}
}

1354
1355
1356
int
ahd_platform_abort_scbs(struct ahd_softc *ahd, int target, char channel,
			int lun, u_int tag, role_t role, uint32_t status)
Linus Torvalds's avatar
Linus Torvalds committed
1357
{
1358
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1359
1360
}

1361
1362
static u_int
ahd_linux_user_tagdepth(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1363
{
1364
1365
	static int warned_user;
	u_int tags;
Linus Torvalds's avatar
Linus Torvalds committed
1366

1367
1368
	tags = 0;
	if ((ahd->user_discenable & devinfo->target_mask) != 0) {
1369
		if (ahd->unit >= ARRAY_SIZE(aic79xx_tag_info)) {
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386

			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;
Linus Torvalds's avatar
Linus Torvalds committed
1387
1388
		}
	}
1389
	return (tags);
Linus Torvalds's avatar
Linus Torvalds committed
1390
1391
1392
}

/*
1393
 * Determines the queue depth for a given device.
Linus Torvalds's avatar
Linus Torvalds committed
1394
1395
 */
static void
1396
ahd_linux_device_queue_depth(struct scsi_device *sdev)
Linus Torvalds's avatar
Linus Torvalds committed
1397
{
1398
1399
1400
	struct	ahd_devinfo devinfo;
	u_int	tags;
	struct ahd_softc *ahd = *((struct ahd_softc **)sdev->host->hostdata);
Linus Torvalds's avatar
Linus Torvalds committed
1401

1402
1403
1404
1405
1406
1407
1408
	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) {
Linus Torvalds's avatar
Linus Torvalds committed
1409

1410
1411
1412
		ahd_platform_set_tags(ahd, sdev, &devinfo, AHD_QUEUE_TAGGED);
		ahd_send_async(ahd, devinfo.channel, devinfo.target,
			       devinfo.lun, AC_TRANSFER_NEG);
1413
1414
1415
		ahd_print_devinfo(ahd, &devinfo);
		printf("Tagged Queuing enabled.  Depth %d\n", tags);
	} else {
1416
1417
1418
		ahd_platform_set_tags(ahd, sdev, &devinfo, AHD_QUEUE_NONE);
		ahd_send_async(ahd, devinfo.channel, devinfo.target,
			       devinfo.lun, AC_TRANSFER_NEG);
1419
1420
	}
}
Linus Torvalds's avatar
Linus Torvalds committed
1421

1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
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;
1432
1433
1434
	unsigned long flags;

	ahd_lock(ahd, &flags);
Linus Torvalds's avatar
Linus Torvalds committed
1435
1436

	/*
1437
	 * Get an scb to use.
Linus Torvalds's avatar
Linus Torvalds committed
1438
	 */
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
	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;
1450
		ahd_unlock(ahd, &flags);
1451
		return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds's avatar
Linus Torvalds committed
1452
1453
	}

1454
1455
1456
1457
	scb->io_ctx = cmd;
	scb->platform_data->dev = dev;
	hscb = scb->hscb;
	cmd->host_scribble = (char *)scb;
Linus Torvalds's avatar
Linus Torvalds committed
1458

1459
1460
1461
1462
1463
1464
1465
1466
	/*
	 * 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);
Linus Torvalds's avatar
Linus Torvalds committed
1467

1468
1469
	if ((ahd->user_discenable & mask) != 0)
		hscb->control |= DISCENB;
Linus Torvalds's avatar
Linus Torvalds committed
1470

1471
1472
	if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0)
		scb->flags |= SCB_PACKETIZED;
Linus Torvalds's avatar
Linus Torvalds committed
1473

1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
	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;
Linus Torvalds's avatar
Linus Torvalds committed
1495
		}
1496
	}
Linus Torvalds's avatar
Linus Torvalds committed
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
1522
1523
1524
	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);
Linus Torvalds's avatar
Linus Torvalds committed
1525
		}
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
	} 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);
Linus Torvalds's avatar
Linus Torvalds committed
1540
	}
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551

	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);

1552
1553
	ahd_unlock(ahd, &flags);

1554
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1555
1556
}

1557
1558
1559
1560
/*
 * SCSI controller interrupt handler.
 */
irqreturn_t
1561
ahd_linux_isr(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
1562
{
1563
1564
1565
	struct	ahd_softc *ahd;
	u_long	flags;
	int	ours;
Linus Torvalds's avatar
Linus Torvalds committed
1566

1567
1568
1569
1570
1571
	ahd = (struct ahd_softc *) dev_id;
	ahd_lock(ahd, &flags); 
	ours = ahd_intr(ahd);
	ahd_unlock(ahd, &flags);
	return IRQ_RETVAL(ours);
Linus Torvalds's avatar
Linus Torvalds committed
1572
1573
1574
}

void
1575
ahd_send_async(struct ahd_softc *ahd, char channel,
1576
	       u_int target, u_int lun, ac_code code)
Linus Torvalds's avatar
Linus Torvalds committed
1577
{
1578
1579
1580
1581
1582
1583
1584
1585
1586
	switch (code) {
	case AC_TRANSFER_NEG:
	{
		char	buf[80];
		struct  scsi_target *starget;
		struct	info_str info;
		struct	ahd_initiator_tinfo *tinfo;
		struct	ahd_tmode_tstate *tstate;
		unsigned int target_ppr_options;
Linus Torvalds's avatar
Linus Torvalds committed
1587

1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
		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];
1613
		if (starget == NULL)
1614
1615
1616
1617
1618
			break;

		target_ppr_options =
			(spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
			+ (spi_qas(starget) ? MSG_EXT_PPR_QAS_REQ : 0)
1619
1620
1621
1622
			+ (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