Blame view

drivers/block/cciss_scsi.c 47.1 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
bd4f36d6d   Mike Miller   cciss: update cop...
2
3
   *    Disk Array driver for HP Smart Array controllers, SCSI Tape module.
   *    (C) Copyright 2001, 2007 Hewlett-Packard Development Company, L.P.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
   *
   *    This program is free software; you can redistribute it and/or modify
   *    it under the terms of the GNU General Public License as published by
bd4f36d6d   Mike Miller   cciss: update cop...
7
   *    the Free Software Foundation; version 2 of the License.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
   *
   *    This program is distributed in the hope that it will be useful,
   *    but WITHOUT ANY WARRANTY; without even the implied warranty of
bd4f36d6d   Mike Miller   cciss: update cop...
11
12
   *    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   *    General Public License for more details.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
   *
   *    You should have received a copy of the GNU General Public License
   *    along with this program; if not, write to the Free Software
bd4f36d6d   Mike Miller   cciss: update cop...
16
17
   *    Foundation, Inc., 59 Temple Place, Suite 300, Boston, MA
   *    02111-1307, USA.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
21
22
23
24
25
26
27
28
29
   *
   *    Questions/Comments/Bugfixes to iss_storagedev@hp.com
   *    
   *    Author: Stephen M. Cameron
   */
  #ifdef CONFIG_CISS_SCSI_TAPE
  
  /* Here we have code to present the driver as a scsi driver 
     as it is simultaneously presented as a block driver.  The 
     reason for doing this is to allow access to SCSI tape drives
     through the array controller.  Note in particular, neither 
     physical nor logical disks are presented through the scsi layer. */
4e57b6817   Tim Schmielau   [PATCH] fix missi...
30
31
32
33
34
35
  #include <linux/timer.h>
  #include <linux/completion.h>
  #include <linux/slab.h>
  #include <linux/string.h>
  
  #include <asm/atomic.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
38
  #include <scsi/scsi_cmnd.h>
  #include <scsi/scsi_device.h>
  #include <scsi/scsi_host.h> 
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
  
  #include "cciss_scsi.h"
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
41
42
  #define CCISS_ABORT_MSG 0x00
  #define CCISS_RESET_MSG 0x01
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  /* some prototypes... */ 
  static int sendcmd(
  	__u8	cmd,
  	int	ctlr,
  	void	*buff,
  	size_t	size,
  	unsigned int use_unit_num, /* 0: address the controller,
  				      1: address logical volume log_unit, 
  				      2: address is in scsi3addr */
  	unsigned int log_unit,
  	__u8	page_code,
  	unsigned char *scsi3addr,
  	int cmd_type);
  
  
  static int cciss_scsi_proc_info(
  		struct Scsi_Host *sh,
  		char *buffer, /* data buffer */
  		char **start, 	   /* where data in buffer starts */
  		off_t offset,	   /* offset from start of imaginary file */
  		int length, 	   /* length of data in buffer */
  		int func);	   /* 0 == read, 1 == write */
  
  static int cciss_scsi_queue_command (struct scsi_cmnd *cmd,
  		void (* done)(struct scsi_cmnd *));
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
68
69
  static int cciss_eh_device_reset_handler(struct scsi_cmnd *);
  static int cciss_eh_abort_handler(struct scsi_cmnd *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  
  static struct cciss_scsi_hba_t ccissscsi[MAX_CTLR] = {
  	{ .name = "cciss0", .ndevices = 0 },
  	{ .name = "cciss1", .ndevices = 0 },
  	{ .name = "cciss2", .ndevices = 0 },
  	{ .name = "cciss3", .ndevices = 0 },
  	{ .name = "cciss4", .ndevices = 0 },
  	{ .name = "cciss5", .ndevices = 0 },
  	{ .name = "cciss6", .ndevices = 0 },
  	{ .name = "cciss7", .ndevices = 0 },
  };
  
  static struct scsi_host_template cciss_driver_template = {
  	.module			= THIS_MODULE,
  	.name			= "cciss",
  	.proc_name		= "cciss",
  	.proc_info		= cciss_scsi_proc_info,
  	.queuecommand		= cciss_scsi_queue_command,
  	.can_queue		= SCSI_CCISS_CAN_QUEUE,
  	.this_id		= 7,
  	.sg_tablesize		= MAXSGENTRIES,
  	.cmd_per_lun		= 1,
  	.use_clustering		= DISABLE_CLUSTERING,
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
93
94
95
  	/* Can't have eh_bus_reset_handler or eh_host_reset_handler for cciss */
  	.eh_device_reset_handler= cciss_eh_device_reset_handler,
  	.eh_abort_handler	= cciss_eh_abort_handler,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
99
100
101
102
  };
  
  #pragma pack(1)
  struct cciss_scsi_cmd_stack_elem_t {
  	CommandList_struct cmd;
  	ErrorInfo_struct Err;
  	__u32 busaddr;
33079b219   Mike Miller   [PATCH] cciss: di...
103
  	__u32 pad;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  };
  
  #pragma pack()
  
  #define CMD_STACK_SIZE (SCSI_CCISS_CAN_QUEUE * \
  		CCISS_MAX_SCSI_DEVS_PER_HBA + 2)
  			// plus two for init time usage
  
  #pragma pack(1)
  struct cciss_scsi_cmd_stack_t {
  	struct cciss_scsi_cmd_stack_elem_t *pool;
  	struct cciss_scsi_cmd_stack_elem_t *elem[CMD_STACK_SIZE];
  	dma_addr_t cmd_pool_handle;
  	int top;
  };
  #pragma pack()
  
  struct cciss_scsi_adapter_data_t {
  	struct Scsi_Host *scsi_host;
  	struct cciss_scsi_cmd_stack_t cmd_stack;
  	int registered;
  	spinlock_t lock; // to protect ccissscsi[ctlr]; 
  };
  
  #define CPQ_TAPE_LOCK(ctlr, flags) spin_lock_irqsave( \
  	&(((struct cciss_scsi_adapter_data_t *) \
  	hba[ctlr]->scsi_ctlr)->lock), flags);
  #define CPQ_TAPE_UNLOCK(ctlr, flags) spin_unlock_irqrestore( \
  	&(((struct cciss_scsi_adapter_data_t *) \
  	hba[ctlr]->scsi_ctlr)->lock), flags);
  
  static CommandList_struct *
  scsi_cmd_alloc(ctlr_info_t *h)
  {
  	/* assume only one process in here at a time, locking done by caller. */
  	/* use CCISS_LOCK(ctlr) */
  	/* might be better to rewrite how we allocate scsi commands in a way that */
  	/* needs no locking at all. */
  
  	/* take the top memory chunk off the stack and return it, if any. */
  	struct cciss_scsi_cmd_stack_elem_t *c;
  	struct cciss_scsi_adapter_data_t *sa;
  	struct cciss_scsi_cmd_stack_t *stk;
  	u64bit temp64;
  
  	sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
  	stk = &sa->cmd_stack; 
  
  	if (stk->top < 0) 
  		return NULL;
  	c = stk->elem[stk->top]; 	
  	/* memset(c, 0, sizeof(*c)); */
  	memset(&c->cmd, 0, sizeof(c->cmd));
  	memset(&c->Err, 0, sizeof(c->Err));
  	/* set physical addr of cmd and addr of scsi parameters */
  	c->cmd.busaddr = c->busaddr; 
  	/* (__u32) (stk->cmd_pool_handle + 
  		(sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top)); */
  
  	temp64.val = (__u64) (c->busaddr + sizeof(CommandList_struct));
  	/* (__u64) (stk->cmd_pool_handle + 
  		(sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top) +
  		 sizeof(CommandList_struct)); */
  	stk->top--;
  	c->cmd.ErrDesc.Addr.lower = temp64.val32.lower;
  	c->cmd.ErrDesc.Addr.upper = temp64.val32.upper;
  	c->cmd.ErrDesc.Len = sizeof(ErrorInfo_struct);
  	
  	c->cmd.ctlr = h->ctlr;
  	c->cmd.err_info = &c->Err;
  
  	return (CommandList_struct *) c;
  }
  
  static void 
  scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd)
  {
  	/* assume only one process in here at a time, locking done by caller. */
  	/* use CCISS_LOCK(ctlr) */
  	/* drop the free memory chunk on top of the stack. */
  
  	struct cciss_scsi_adapter_data_t *sa;
  	struct cciss_scsi_cmd_stack_t *stk;
  
  	sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
  	stk = &sa->cmd_stack; 
  	if (stk->top >= CMD_STACK_SIZE) {
  		printk("cciss: scsi_cmd_free called too many times.
  ");
  		BUG();
  	}
  	stk->top++;
  	stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd;
  }
  
  static int
  scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa)
  {
  	int i;
  	struct cciss_scsi_cmd_stack_t *stk;
  	size_t size;
  
  	stk = &sa->cmd_stack; 
  	size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
  
  	// pci_alloc_consistent guarantees 32-bit DMA address will
  	// be used
  
  	stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)
  		pci_alloc_consistent(hba[ctlr]->pdev, size, &stk->cmd_pool_handle);
  
  	if (stk->pool == NULL) {
  		printk("stk->pool is null
  ");
  		return -1;
  	}
  
  	for (i=0; i<CMD_STACK_SIZE; i++) {
  		stk->elem[i] = &stk->pool[i];
  		stk->elem[i]->busaddr = (__u32) (stk->cmd_pool_handle + 
  			(sizeof(struct cciss_scsi_cmd_stack_elem_t) * i));
  	}
  	stk->top = CMD_STACK_SIZE-1;
  	return 0;
  }
  
  static void
  scsi_cmd_stack_free(int ctlr)
  {
  	struct cciss_scsi_adapter_data_t *sa;
  	struct cciss_scsi_cmd_stack_t *stk;
  	size_t size;
  
  	sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
  	stk = &sa->cmd_stack; 
  	if (stk->top != CMD_STACK_SIZE-1) {
  		printk( "cciss: %d scsi commands are still outstanding.
  ",
  			CMD_STACK_SIZE - stk->top);
  		// BUG();
  		printk("WE HAVE A BUG HERE!!! stk=0x%p
  ", stk);
  	}
  	size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
  
  	pci_free_consistent(hba[ctlr]->pdev, size, stk->pool, stk->cmd_pool_handle);
  	stk->pool = NULL;
  }
400bb2369   Grant Coady   [PATCH] cciss_scs...
252
  #if 0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
326
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
  static int xmargin=8;
  static int amargin=60;
  
  static void
  print_bytes (unsigned char *c, int len, int hex, int ascii)
  {
  
  	int i;
  	unsigned char *x;
  
  	if (hex)
  	{
  		x = c;
  		for (i=0;i<len;i++)
  		{
  			if ((i % xmargin) == 0 && i>0) printk("
  ");
  			if ((i % xmargin) == 0) printk("0x%04x:", i);
  			printk(" %02x", *x);
  			x++;
  		}
  		printk("
  ");
  	}
  	if (ascii)
  	{
  		x = c;
  		for (i=0;i<len;i++)
  		{
  			if ((i % amargin) == 0 && i>0) printk("
  ");
  			if ((i % amargin) == 0) printk("0x%04x:", i);
  			if (*x > 26 && *x < 128) printk("%c", *x);
  			else printk(".");
  			x++;
  		}
  		printk("
  ");
  	}
  }
  
  static void
  print_cmd(CommandList_struct *cp)
  {
  	printk("queue:%d
  ", cp->Header.ReplyQueue);
  	printk("sglist:%d
  ", cp->Header.SGList);
  	printk("sgtot:%d
  ", cp->Header.SGTotal);
  	printk("Tag:0x%08x/0x%08x
  ", cp->Header.Tag.upper, 
  			cp->Header.Tag.lower);
  	printk("LUN:0x%02x%02x%02x%02x%02x%02x%02x%02x
  ",
  		cp->Header.LUN.LunAddrBytes[0],
  		cp->Header.LUN.LunAddrBytes[1],
  		cp->Header.LUN.LunAddrBytes[2],
  		cp->Header.LUN.LunAddrBytes[3],
  		cp->Header.LUN.LunAddrBytes[4],
  		cp->Header.LUN.LunAddrBytes[5],
  		cp->Header.LUN.LunAddrBytes[6],
  		cp->Header.LUN.LunAddrBytes[7]);
  	printk("CDBLen:%d
  ", cp->Request.CDBLen);
  	printk("Type:%d
  ",cp->Request.Type.Type);
  	printk("Attr:%d
  ",cp->Request.Type.Attribute);
  	printk(" Dir:%d
  ",cp->Request.Type.Direction);
  	printk("Timeout:%d
  ",cp->Request.Timeout);
  	printk( "CDB: %02x %02x %02x %02x %02x %02x %02x %02x"
  		" %02x %02x %02x %02x %02x %02x %02x %02x
  ",
  		cp->Request.CDB[0], cp->Request.CDB[1],
  		cp->Request.CDB[2], cp->Request.CDB[3],
  		cp->Request.CDB[4], cp->Request.CDB[5],
  		cp->Request.CDB[6], cp->Request.CDB[7],
  		cp->Request.CDB[8], cp->Request.CDB[9],
  		cp->Request.CDB[10], cp->Request.CDB[11],
  		cp->Request.CDB[12], cp->Request.CDB[13],
  		cp->Request.CDB[14], cp->Request.CDB[15]),
  	printk("edesc.Addr: 0x%08x/0%08x, Len  = %d
  ", 
  		cp->ErrDesc.Addr.upper, cp->ErrDesc.Addr.lower, 
  			cp->ErrDesc.Len);
  	printk("sgs..........Errorinfo:
  ");
  	printk("scsistatus:%d
  ", cp->err_info->ScsiStatus);
  	printk("senselen:%d
  ", cp->err_info->SenseLen);
  	printk("cmd status:%d
  ", cp->err_info->CommandStatus);
  	printk("resid cnt:%d
  ", cp->err_info->ResidualCnt);
  	printk("offense size:%d
  ", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_size);
  	printk("offense byte:%d
  ", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_num);
  	printk("offense value:%d
  ", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
  			
  }
  
  #endif
  
  static int 
  find_bus_target_lun(int ctlr, int *bus, int *target, int *lun)
  {
  	/* finds an unused bus, target, lun for a new device */
  	/* assumes hba[ctlr]->scsi_ctlr->lock is held */ 
  	int i, found=0;
  	unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA];
  
  	memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA);
  
  	target_taken[SELF_SCSI_ID] = 1;	
  	for (i=0;i<ccissscsi[ctlr].ndevices;i++)
  		target_taken[ccissscsi[ctlr].dev[i].target] = 1;
  	
  	for (i=0;i<CCISS_MAX_SCSI_DEVS_PER_HBA;i++) {
  		if (!target_taken[i]) {
  			*bus = 0; *target=i; *lun = 0; found=1;
  			break;
  		}
  	}
  	return (!found);	
  }
f4a93bcda   Mike Miller   cciss: change the...
384
385
386
387
  struct scsi2map {
  	char scsi3addr[8];
  	int bus, target, lun;
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
389
390
  
  static int 
  cciss_scsi_add_entry(int ctlr, int hostno, 
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
391
  		struct cciss_scsi_dev_t *device,
f4a93bcda   Mike Miller   cciss: change the...
392
  		struct scsi2map *added, int *nadded)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
393
394
395
396
  {
  	/* assumes hba[ctlr]->scsi_ctlr->lock is held */ 
  	int n = ccissscsi[ctlr].ndevices;
  	struct cciss_scsi_dev_t *sd;
935dc8d75   Mike Miller   cciss: add suppor...
397
398
  	int i, bus, target, lun;
  	unsigned char addr1[8], addr2[8];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399
400
401
402
403
404
405
  
  	if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
  		printk("cciss%d: Too many devices, "
  			"some will be inaccessible.
  ", ctlr);
  		return -1;
  	}
f4a93bcda   Mike Miller   cciss: change the...
406

935dc8d75   Mike Miller   cciss: add suppor...
407
408
409
410
  	bus = target = -1;
  	lun = 0;
  	/* Is this device a non-zero lun of a multi-lun device */
  	/* byte 4 of the 8-byte LUN addr will contain the logical unit no. */
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
411
  	if (device->scsi3addr[4] != 0) {
935dc8d75   Mike Miller   cciss: add suppor...
412
413
414
415
  		/* Search through our list and find the device which */
  		/* has the same 8 byte LUN address, excepting byte 4. */
  		/* Assign the same bus and target for this new LUN. */
  		/* Use the logical unit number from the firmware. */
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
416
  		memcpy(addr1, device->scsi3addr, 8);
935dc8d75   Mike Miller   cciss: add suppor...
417
418
419
420
421
422
423
424
425
  		addr1[4] = 0;
  		for (i = 0; i < n; i++) {
  			sd = &ccissscsi[ctlr].dev[i];
  			memcpy(addr2, sd->scsi3addr, 8);
  			addr2[4] = 0;
  			/* differ only in byte 4? */
  			if (memcmp(addr1, addr2, 8) == 0) {
  				bus = sd->bus;
  				target = sd->target;
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
426
  				lun = device->scsi3addr[4];
935dc8d75   Mike Miller   cciss: add suppor...
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
  				break;
  			}
  		}
  	}
  
  	sd = &ccissscsi[ctlr].dev[n];
  	if (lun == 0) {
  		if (find_bus_target_lun(ctlr,
  			&sd->bus, &sd->target, &sd->lun) != 0)
  			return -1;
  	} else {
  		sd->bus = bus;
  		sd->target = target;
  		sd->lun = lun;
  	}
f4a93bcda   Mike Miller   cciss: change the...
442
443
444
445
  	added[*nadded].bus = sd->bus;
  	added[*nadded].target = sd->target;
  	added[*nadded].lun = sd->lun;
  	(*nadded)++;
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
446
447
448
449
450
  	memcpy(sd->scsi3addr, device->scsi3addr, 8);
  	memcpy(sd->vendor, device->vendor, sizeof(sd->vendor));
  	memcpy(sd->revision, device->revision, sizeof(sd->revision));
  	memcpy(sd->device_id, device->device_id, sizeof(sd->device_id));
  	sd->devtype = device->devtype;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
451
452
453
454
455
456
457
458
  	ccissscsi[ctlr].ndevices++;
  
  	/* initially, (before registering with scsi layer) we don't 
  	   know our hostno and we don't want to print anything first 
  	   time anyway (the scsi layer's inquiries will show that info) */
  	if (hostno != -1)
  		printk("cciss%d: %s device c%db%dt%dl%d added.
  ", 
4ff36718e   Matthew Wilcox   [SCSI] Improve in...
459
  			ctlr, scsi_device_type(sd->devtype), hostno,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460
461
462
463
464
  			sd->bus, sd->target, sd->lun);
  	return 0;
  }
  
  static void
f4a93bcda   Mike Miller   cciss: change the...
465
466
  cciss_scsi_remove_entry(int ctlr, int hostno, int entry,
  	struct scsi2map *removed, int *nremoved)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
469
470
471
472
473
  {
  	/* assumes hba[ctlr]->scsi_ctlr->lock is held */ 
  	int i;
  	struct cciss_scsi_dev_t sd;
  
  	if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
  	sd = ccissscsi[ctlr].dev[entry];
f4a93bcda   Mike Miller   cciss: change the...
474
475
476
477
  	removed[*nremoved].bus    = sd.bus;
  	removed[*nremoved].target = sd.target;
  	removed[*nremoved].lun    = sd.lun;
  	(*nremoved)++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
479
480
481
482
  	for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++)
  		ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1];
  	ccissscsi[ctlr].ndevices--;
  	printk("cciss%d: %s device c%db%dt%dl%d removed.
  ",
4ff36718e   Matthew Wilcox   [SCSI] Improve in...
483
  		ctlr, scsi_device_type(sd.devtype), hostno,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
485
486
487
488
489
490
491
492
493
494
495
496
  			sd.bus, sd.target, sd.lun);
  }
  
  
  #define SCSI3ADDR_EQ(a,b) ( \
  	(a)[7] == (b)[7] && \
  	(a)[6] == (b)[6] && \
  	(a)[5] == (b)[5] && \
  	(a)[4] == (b)[4] && \
  	(a)[3] == (b)[3] && \
  	(a)[2] == (b)[2] && \
  	(a)[1] == (b)[1] && \
  	(a)[0] == (b)[0])
f4a93bcda   Mike Miller   cciss: change the...
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
  static void fixup_botched_add(int ctlr, char *scsi3addr)
  {
  	/* called when scsi_add_device fails in order to re-adjust */
  	/* ccissscsi[] to match the mid layer's view. */
  	unsigned long flags;
  	int i, j;
  	CPQ_TAPE_LOCK(ctlr, flags);
  	for (i = 0; i < ccissscsi[ctlr].ndevices; i++) {
  		if (memcmp(scsi3addr,
  				ccissscsi[ctlr].dev[i].scsi3addr, 8) == 0) {
  			for (j = i; j < ccissscsi[ctlr].ndevices-1; j++)
  				ccissscsi[ctlr].dev[j] =
  					ccissscsi[ctlr].dev[j+1];
  			ccissscsi[ctlr].ndevices--;
  			break;
  		}
  	}
  	CPQ_TAPE_UNLOCK(ctlr, flags);
  }
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
  static int device_is_the_same(struct cciss_scsi_dev_t *dev1,
  	struct cciss_scsi_dev_t *dev2)
  {
  	return dev1->devtype == dev2->devtype &&
  		memcmp(dev1->scsi3addr, dev2->scsi3addr,
  			sizeof(dev1->scsi3addr)) == 0 &&
  		memcmp(dev1->device_id, dev2->device_id,
  			sizeof(dev1->device_id)) == 0 &&
  		memcmp(dev1->vendor, dev2->vendor,
  			sizeof(dev1->vendor)) == 0 &&
  		memcmp(dev1->model, dev2->model,
  			sizeof(dev1->model)) == 0 &&
  		memcmp(dev1->revision, dev2->revision,
  			sizeof(dev1->revision)) == 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
531
532
533
534
535
536
537
538
539
540
541
542
  static int
  adjust_cciss_scsi_table(int ctlr, int hostno,
  	struct cciss_scsi_dev_t sd[], int nsds)
  {
  	/* sd contains scsi3 addresses and devtypes, but
  	   bus target and lun are not filled in.  This funciton
  	   takes what's in sd to be the current and adjusts
  	   ccissscsi[] to be in line with what's in sd. */ 
  
  	int i,j, found, changes=0;
  	struct cciss_scsi_dev_t *csd;
  	unsigned long flags;
f4a93bcda   Mike Miller   cciss: change the...
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
  	struct scsi2map *added, *removed;
  	int nadded, nremoved;
  	struct Scsi_Host *sh = NULL;
  
  	added = kzalloc(sizeof(*added) * CCISS_MAX_SCSI_DEVS_PER_HBA,
  			GFP_KERNEL);
  	removed = kzalloc(sizeof(*removed) * CCISS_MAX_SCSI_DEVS_PER_HBA,
  			GFP_KERNEL);
  
  	if (!added || !removed) {
  		printk(KERN_WARNING "cciss%d: Out of memory in "
  			"adjust_cciss_scsi_table
  ", ctlr);
  		goto free_and_out;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
558
559
  
  	CPQ_TAPE_LOCK(ctlr, flags);
f4a93bcda   Mike Miller   cciss: change the...
560
561
562
  	if (hostno != -1)  /* if it's not the first time... */
  		sh = ((struct cciss_scsi_adapter_data_t *)
  			hba[ctlr]->scsi_ctlr)->scsi_host;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
563
564
565
566
  	/* find any devices in ccissscsi[] that are not in 
  	   sd[] and remove them from ccissscsi[] */
  
  	i = 0;
f4a93bcda   Mike Miller   cciss: change the...
567
568
  	nremoved = 0;
  	nadded = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
569
570
571
572
573
574
  	while(i<ccissscsi[ctlr].ndevices) {
  		csd = &ccissscsi[ctlr].dev[i];
  		found=0;
  		for (j=0;j<nsds;j++) {
  			if (SCSI3ADDR_EQ(sd[j].scsi3addr,
  				csd->scsi3addr)) {
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
575
  				if (device_is_the_same(&sd[j], csd))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
576
577
578
579
580
581
582
583
584
585
586
  					found=2;
  				else
  					found=1;
  				break;
  			}
  		}
  
  		if (found == 0) { /* device no longer present. */ 
  			changes++;
  			/* printk("cciss%d: %s device c%db%dt%dl%d removed.
  ",
4ff36718e   Matthew Wilcox   [SCSI] Improve in...
587
  				ctlr, scsi_device_type(csd->devtype), hostno,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
588
  					csd->bus, csd->target, csd->lun); */
f4a93bcda   Mike Miller   cciss: change the...
589
590
591
  			cciss_scsi_remove_entry(ctlr, hostno, i,
  				removed, &nremoved);
  			/* remove ^^^, hence i not incremented */
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
592
  		} else if (found == 1) { /* device is different in some way */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
593
  			changes++;
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
594
595
596
  			printk("cciss%d: device c%db%dt%dl%d has changed.
  ",
  				ctlr, hostno, csd->bus, csd->target, csd->lun);
f4a93bcda   Mike Miller   cciss: change the...
597
598
599
  			cciss_scsi_remove_entry(ctlr, hostno, i,
  				removed, &nremoved);
  			/* remove ^^^, hence i not incremented */
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
600
  			if (cciss_scsi_add_entry(ctlr, hostno, &sd[j],
f4a93bcda   Mike Miller   cciss: change the...
601
602
603
  				added, &nadded) != 0)
  				/* we just removed one, so add can't fail. */
  					BUG();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
604
  			csd->devtype = sd[j].devtype;
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
605
606
607
608
609
610
611
612
  			memcpy(csd->device_id, sd[j].device_id,
  				sizeof(csd->device_id));
  			memcpy(csd->vendor, sd[j].vendor,
  				sizeof(csd->vendor));
  			memcpy(csd->model, sd[j].model,
  				sizeof(csd->model));
  			memcpy(csd->revision, sd[j].revision,
  				sizeof(csd->revision));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
613
614
615
616
617
618
619
620
621
622
623
624
625
  		} else 		/* device is same as it ever was, */
  			i++;	/* so just move along. */
  	}
  
  	/* Now, make sure every device listed in sd[] is also
   	   listed in ccissscsi[], adding them if they aren't found */
  
  	for (i=0;i<nsds;i++) {
  		found=0;
  		for (j=0;j<ccissscsi[ctlr].ndevices;j++) {
  			csd = &ccissscsi[ctlr].dev[j];
  			if (SCSI3ADDR_EQ(sd[i].scsi3addr,
  				csd->scsi3addr)) {
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
626
  				if (device_is_the_same(&sd[i], csd))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
627
628
629
630
631
632
633
634
  					found=2;	/* found device */
  				else
  					found=1; 	/* found a bug. */
  				break;
  			}
  		}
  		if (!found) {
  			changes++;
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
635
  			if (cciss_scsi_add_entry(ctlr, hostno, &sd[i],
f4a93bcda   Mike Miller   cciss: change the...
636
  				added, &nadded) != 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
637
638
639
640
  				break;
  		} else if (found == 1) {
  			/* should never happen... */
  			changes++;
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
641
642
643
  			printk(KERN_WARNING "cciss%d: device "
  				"unexpectedly changed
  ", ctlr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644
645
646
647
  			/* but if it does happen, we just ignore that device */
  		}
  	}
  	CPQ_TAPE_UNLOCK(ctlr, flags);
f4a93bcda   Mike Miller   cciss: change the...
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
  	/* Don't notify scsi mid layer of any changes the first time through */
  	/* (or if there are no changes) scsi_scan_host will do it later the */
  	/* first time through. */
  	if (hostno == -1 || !changes)
  		goto free_and_out;
  
  	/* Notify scsi mid layer of any removed devices */
  	for (i = 0; i < nremoved; i++) {
  		struct scsi_device *sdev =
  			scsi_device_lookup(sh, removed[i].bus,
  				removed[i].target, removed[i].lun);
  		if (sdev != NULL) {
  			scsi_remove_device(sdev);
  			scsi_device_put(sdev);
  		} else {
  			/* We don't expect to get here. */
  			/* future cmds to this device will get selection */
  			/* timeout as if the device was gone. */
  			printk(KERN_WARNING "cciss%d: didn't find "
  				"c%db%dt%dl%d
   for removal.",
  				ctlr, hostno, removed[i].bus,
  				removed[i].target, removed[i].lun);
  		}
  	}
  
  	/* Notify scsi mid layer of any added devices */
  	for (i = 0; i < nadded; i++) {
  		int rc;
  		rc = scsi_add_device(sh, added[i].bus,
  			added[i].target, added[i].lun);
  		if (rc == 0)
  			continue;
  		printk(KERN_WARNING "cciss%d: scsi_add_device "
  			"c%db%dt%dl%d failed, device not added.
  ",
  			ctlr, hostno,
  			added[i].bus, added[i].target, added[i].lun);
  		/* now we have to remove it from ccissscsi, */
  		/* since it didn't get added to scsi mid layer */
  		fixup_botched_add(ctlr, added[i].scsi3addr);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
690

f4a93bcda   Mike Miller   cciss: change the...
691
692
693
  free_and_out:
  	kfree(added);
  	kfree(removed);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
  	return 0;
  }
  
  static int
  lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr)
  {
  	int i;
  	struct cciss_scsi_dev_t *sd;
  	unsigned long flags;
  
  	CPQ_TAPE_LOCK(ctlr, flags);
  	for (i=0;i<ccissscsi[ctlr].ndevices;i++) {
  		sd = &ccissscsi[ctlr].dev[i];
  		if (sd->bus == bus &&
  		    sd->target == target &&
  		    sd->lun == lun) {
  			memcpy(scsi3addr, &sd->scsi3addr[0], 8);
  			CPQ_TAPE_UNLOCK(ctlr, flags);
  			return 0;
  		}
  	}
  	CPQ_TAPE_UNLOCK(ctlr, flags);
  	return -1;
  }
  
  static void 
  cciss_scsi_setup(int cntl_num)
  {
  	struct cciss_scsi_adapter_data_t * shba;
  
  	ccissscsi[cntl_num].ndevices = 0;
  	shba = (struct cciss_scsi_adapter_data_t *)
  		kmalloc(sizeof(*shba), GFP_KERNEL);	
  	if (shba == NULL)
  		return;
  	shba->scsi_host = NULL;
  	spin_lock_init(&shba->lock);
  	shba->registered = 0;
  	if (scsi_cmd_stack_setup(cntl_num, shba) != 0) {
  		kfree(shba);
  		shba = NULL;
  	}
  	hba[cntl_num]->scsi_ctlr = (void *) shba;
  	return;
  }
  
  static void
  complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
  {
  	struct scsi_cmnd *cmd;
  	ctlr_info_t *ctlr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
745
746
747
748
749
750
751
752
753
754
755
756
  	ErrorInfo_struct *ei;
  
  	ei = cp->err_info;
  
  	/* First, see if it was a message rather than a command */
  	if (cp->Request.Type.Type == TYPE_MSG)  {
  		cp->cmd_type = CMD_MSG_DONE;
  		return;
  	}
  
  	cmd = (struct scsi_cmnd *) cp->scsi_cmd;	
  	ctlr = hba[cp->ctlr];
41ce639a1   FUJITA Tomonori   [SCSI] cciss: con...
757
  	scsi_dma_unmap(cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
  
  	cmd->result = (DID_OK << 16); 		/* host byte */
  	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
  	/* cmd->result |= (GOOD < 1); */		/* status byte */
  
  	cmd->result |= (ei->ScsiStatus);
  	/* printk("Scsistatus is 0x%02x
  ", ei->ScsiStatus);  */
  
  	/* copy the sense data whether we need to or not. */
  
  	memcpy(cmd->sense_buffer, ei->SenseInfo, 
  		ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
  			SCSI_SENSE_BUFFERSIZE : 
  			ei->SenseLen);
41ce639a1   FUJITA Tomonori   [SCSI] cciss: con...
773
  	scsi_set_resid(cmd, ei->ResidualCnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
  
  	if(ei->CommandStatus != 0) 
  	{ /* an error has occurred */ 
  		switch(ei->CommandStatus)
  		{
  			case CMD_TARGET_STATUS:
  				/* Pass it up to the upper layers... */
  				if( ei->ScsiStatus)
                  		{
  #if 0
                      			printk(KERN_WARNING "cciss: cmd %p "
  					"has SCSI Status = %x
  ",
                          			cp,  
  						ei->ScsiStatus); 
  #endif
  					cmd->result |= (ei->ScsiStatus < 1);
                  		}
  				else {  /* scsi status is zero??? How??? */
  					
  	/* Ordinarily, this case should never happen, but there is a bug
  	   in some released firmware revisions that allows it to happen
  	   if, for example, a 4100 backplane loses power and the tape
  	   drive is in it.  We assume that it's a fatal error of some
  	   kind because we can't show that it wasn't. We will make it
  	   look like selection timeout since that is the most common
  	   reason for this to occur, and it's severe enough. */
  
  					cmd->result = DID_NO_CONNECT << 16;
  				}
  			break;
  			case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  			break;
  			case CMD_DATA_OVERRUN:
  				printk(KERN_WARNING "cciss: cp %p has"
  					" completed with data overrun "
  					"reported
  ", cp);
  			break;
  			case CMD_INVALID: {
  				/* print_bytes(cp, sizeof(*cp), 1, 0);
  				print_cmd(cp); */
       /* We get CMD_INVALID if you address a non-existent tape drive instead
  	of a selection timeout (no response).  You will see this if you yank 
  	out a tape drive, then try to access it. This is kind of a shame
  	because it means that any other CMD_INVALID (e.g. driver bug) will
  	get interpreted as a missing target. */
  				cmd->result = DID_NO_CONNECT << 16;
  				}
  			break;
  			case CMD_PROTOCOL_ERR:
                                  printk(KERN_WARNING "cciss: cp %p has "
  					"protocol error 
  ", cp);
                          break;
  			case CMD_HARDWARE_ERR:
  				cmd->result = DID_ERROR << 16;
                                  printk(KERN_WARNING "cciss: cp %p had " 
                                          " hardware error
  ", cp);
                          break;
  			case CMD_CONNECTION_LOST:
  				cmd->result = DID_ERROR << 16;
  				printk(KERN_WARNING "cciss: cp %p had "
  					"connection lost
  ", cp);
  			break;
  			case CMD_ABORTED:
  				cmd->result = DID_ABORT << 16;
  				printk(KERN_WARNING "cciss: cp %p was "
  					"aborted
  ", cp);
  			break;
  			case CMD_ABORT_FAILED:
  				cmd->result = DID_ERROR << 16;
  				printk(KERN_WARNING "cciss: cp %p reports "
  					"abort failed
  ", cp);
  			break;
  			case CMD_UNSOLICITED_ABORT:
  				cmd->result = DID_ABORT << 16;
  				printk(KERN_WARNING "cciss: cp %p aborted "
  					"do to an unsolicited abort
  ", cp);
  			break;
  			case CMD_TIMEOUT:
  				cmd->result = DID_TIME_OUT << 16;
  				printk(KERN_WARNING "cciss: cp %p timedout
  ",
  					cp);
  			break;
  			default:
  				cmd->result = DID_ERROR << 16;
  				printk(KERN_WARNING "cciss: cp %p returned "
  					"unknown status %x
  ", cp, 
  						ei->CommandStatus); 
  		}
  	}
  	// printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel, 
  	//	cmd->target, cmd->lun);
  	cmd->scsi_done(cmd);
  	scsi_cmd_free(ctlr, cp);
  }
  
  static int
  cciss_scsi_detect(int ctlr)
  {
  	struct Scsi_Host *sh;
  	int error;
  
  	sh = scsi_host_alloc(&cciss_driver_template, sizeof(struct ctlr_info *));
  	if (sh == NULL)
  		goto fail;
  	sh->io_port = 0;	// good enough?  FIXME, 
  	sh->n_io_port = 0;	// I don't think we use these two...
  	sh->this_id = SELF_SCSI_ID;  
  
  	((struct cciss_scsi_adapter_data_t *) 
  		hba[ctlr]->scsi_ctlr)->scsi_host = (void *) sh;
  	sh->hostdata[0] = (unsigned long) hba[ctlr];
fb86a35b9   Mike Miller   [PATCH] cciss: ad...
895
  	sh->irq = hba[ctlr]->intr[SIMPLE_MODE_INT];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
896
897
898
899
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
  	sh->unique_id = sh->irq;
  	error = scsi_add_host(sh, &hba[ctlr]->pdev->dev);
  	if (error)
  		goto fail_host_put;
  	scsi_scan_host(sh);
  	return 1;
  
   fail_host_put:
  	scsi_host_put(sh);
   fail:
  	return 0;
  }
  
  static void
  cciss_unmap_one(struct pci_dev *pdev,
  		CommandList_struct *cp,
  		size_t buflen,
  		int data_direction)
  {
  	u64bit addr64;
  
  	addr64.val32.lower = cp->SG[0].Addr.lower;
  	addr64.val32.upper = cp->SG[0].Addr.upper;
  	pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
  }
  
  static void
  cciss_map_one(struct pci_dev *pdev,
  		CommandList_struct *cp,
  		unsigned char *buf,
  		size_t buflen,
  		int data_direction)
  {
  	__u64 addr64;
  
  	addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
  	cp->SG[0].Addr.lower = 
  	  (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
  	cp->SG[0].Addr.upper =
  	  (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
  	cp->SG[0].Len = buflen;
  	cp->Header.SGList = (__u8) 1;   /* no. SGs contig in this cmd */
  	cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
  }
  
  static int
  cciss_scsi_do_simple_cmd(ctlr_info_t *c,
  			CommandList_struct *cp,
  			unsigned char *scsi3addr, 
  			unsigned char *cdb,
  			unsigned char cdblen,
  			unsigned char *buf, int bufsize,
  			int direction)
  {
  	unsigned long flags;
6e9a4738c   Peter Zijlstra   [PATCH] completio...
951
  	DECLARE_COMPLETION_ONSTACK(wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
  
  	cp->cmd_type = CMD_IOCTL_PEND;		// treat this like an ioctl 
  	cp->scsi_cmd = NULL;
  	cp->Header.ReplyQueue = 0;  // unused in simple mode
  	memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN));
  	cp->Header.Tag.lower = cp->busaddr;  // Use k. address of cmd as tag
  	// Fill in the request block...
  
  	/* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2
  ", 
  		scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
  		scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
  
  	memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
  	memcpy(cp->Request.CDB, cdb, cdblen);
  	cp->Request.Timeout = 0;
  	cp->Request.CDBLen = cdblen;
  	cp->Request.Type.Type = TYPE_CMD;
  	cp->Request.Type.Attribute = ATTR_SIMPLE;
  	cp->Request.Type.Direction = direction;
  
  	/* Fill in the SG list and do dma mapping */
  	cciss_map_one(c->pdev, cp, (unsigned char *) buf,
  			bufsize, DMA_FROM_DEVICE); 
  
  	cp->waiting = &wait;
  
  	/* Put the request on the tail of the request queue */
  	spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
  	addQ(&c->reqQ, cp);
  	c->Qdepth++;
  	start_io(c);
  	spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
  
  	wait_for_completion(&wait);
  
  	/* undo the dma mapping */
  	cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE);
  	return(0);
  }
  
  static void 
  cciss_scsi_interpret_error(CommandList_struct *cp)
  {
  	ErrorInfo_struct *ei;
  
  	ei = cp->err_info; 
  	switch(ei->CommandStatus)
  	{
  		case CMD_TARGET_STATUS:
  			printk(KERN_WARNING "cciss: cmd %p has "
  				"completed with errors
  ", cp);
  			printk(KERN_WARNING "cciss: cmd %p "
  				"has SCSI Status = %x
  ",
  					cp,  
  					ei->ScsiStatus);
  			if (ei->ScsiStatus == 0)
  				printk(KERN_WARNING 
  				"cciss:SCSI status is abnormally zero.  "
  				"(probably indicates selection timeout "
  				"reported incorrectly due to a known "
  				"firmware bug, circa July, 2001.)
  ");
  		break;
  		case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  			printk("UNDERRUN
  ");
  		break;
  		case CMD_DATA_OVERRUN:
  			printk(KERN_WARNING "cciss: cp %p has"
  				" completed with data overrun "
  				"reported
  ", cp);
  		break;
  		case CMD_INVALID: {
  			/* controller unfortunately reports SCSI passthru's */
  			/* to non-existent targets as invalid commands. */
  			printk(KERN_WARNING "cciss: cp %p is "
  				"reported invalid (probably means "
  				"target device no longer present)
  ", 
  				cp); 
  			/* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
  			print_cmd(cp);  */
  			}
  		break;
  		case CMD_PROTOCOL_ERR:
  			printk(KERN_WARNING "cciss: cp %p has "
  				"protocol error 
  ", cp);
  		break;
  		case CMD_HARDWARE_ERR:
  			/* cmd->result = DID_ERROR << 16; */
  			printk(KERN_WARNING "cciss: cp %p had " 
  				" hardware error
  ", cp);
  		break;
  		case CMD_CONNECTION_LOST:
  			printk(KERN_WARNING "cciss: cp %p had "
  				"connection lost
  ", cp);
  		break;
  		case CMD_ABORTED:
  			printk(KERN_WARNING "cciss: cp %p was "
  				"aborted
  ", cp);
  		break;
  		case CMD_ABORT_FAILED:
  			printk(KERN_WARNING "cciss: cp %p reports "
  				"abort failed
  ", cp);
  		break;
  		case CMD_UNSOLICITED_ABORT:
  			printk(KERN_WARNING "cciss: cp %p aborted "
  				"do to an unsolicited abort
  ", cp);
  		break;
  		case CMD_TIMEOUT:
  			printk(KERN_WARNING "cciss: cp %p timedout
  ",
  				cp);
  		break;
  		default:
  			printk(KERN_WARNING "cciss: cp %p returned "
  				"unknown status %x
  ", cp, 
  					ei->CommandStatus); 
  	}
  }
  
  static int
  cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr, 
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1086
1087
  	unsigned char page, unsigned char *buf,
  	unsigned char bufsize)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
  {
  	int rc;
  	CommandList_struct *cp;
  	char cdb[6];
  	ErrorInfo_struct *ei;
  	unsigned long flags;
  
  	spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
  	cp = scsi_cmd_alloc(c);
  	spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
  
  	if (cp == NULL) {			/* trouble... */
  		printk("cmd_alloc returned NULL!
  ");
  		return -1;
  	}
  
  	ei = cp->err_info; 
  
  	cdb[0] = CISS_INQUIRY;
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1108
1109
  	cdb[1] = (page != 0);
  	cdb[2] = page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1110
  	cdb[3] = 0;
47922d068   Mike Miller   [PATCH] cciss: On...
1111
  	cdb[4] = bufsize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1112
1113
  	cdb[5] = 0;
  	rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb, 
47922d068   Mike Miller   [PATCH] cciss: On...
1114
  				6, buf, bufsize, XFER_READ);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
  
  	if (rc != 0) return rc; /* something went wrong */
  
  	if (ei->CommandStatus != 0 && 
  	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
  		cciss_scsi_interpret_error(cp);
  		rc = -1;
  	}
  	spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
  	scsi_cmd_free(c, cp);
  	spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
  	return rc;	
  }
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
  /* Get the device id from inquiry page 0x83 */
  static int cciss_scsi_get_device_id(ctlr_info_t *c, unsigned char *scsi3addr,
  	unsigned char *device_id, int buflen)
  {
  	int rc;
  	unsigned char *buf;
  
  	if (buflen > 16)
  		buflen = 16;
  	buf = kzalloc(64, GFP_KERNEL);
  	if (!buf)
  		return -1;
  	rc = cciss_scsi_do_inquiry(c, scsi3addr, 0x83, buf, 64);
  	if (rc == 0)
  		memcpy(device_id, &buf[8], buflen);
  	kfree(buf);
  	return rc != 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
  static int
  cciss_scsi_do_report_phys_luns(ctlr_info_t *c, 
  		ReportLunData_struct *buf, int bufsize)
  {
  	int rc;
  	CommandList_struct *cp;
  	unsigned char cdb[12];
  	unsigned char scsi3addr[8]; 
  	ErrorInfo_struct *ei;
  	unsigned long flags;
  
  	spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
  	cp = scsi_cmd_alloc(c);
  	spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
  	if (cp == NULL) {			/* trouble... */
  		printk("cmd_alloc returned NULL!
  ");
  		return -1;
  	}
  
  	memset(&scsi3addr[0], 0, 8); /* address the controller */
  	cdb[0] = CISS_REPORT_PHYS;
  	cdb[1] = 0;
  	cdb[2] = 0;
  	cdb[3] = 0;
  	cdb[4] = 0;
  	cdb[5] = 0;
  	cdb[6] = (bufsize >> 24) & 0xFF;  //MSB
  	cdb[7] = (bufsize >> 16) & 0xFF;
  	cdb[8] = (bufsize >> 8) & 0xFF;
  	cdb[9] = bufsize & 0xFF;
  	cdb[10] = 0;
  	cdb[11] = 0;
  
  	rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, 
  				cdb, 12, 
  				(unsigned char *) buf, 
  				bufsize, XFER_READ);
  
  	if (rc != 0) return rc; /* something went wrong */
  
  	ei = cp->err_info; 
  	if (ei->CommandStatus != 0 && 
  	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
  		cciss_scsi_interpret_error(cp);
  		rc = -1;
  	}
  	spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
  	scsi_cmd_free(c, cp);
  	spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
  	return rc;	
  }
  
  static void
  cciss_update_non_disk_devices(int cntl_num, int hostno)
  {
  	/* the idea here is we could get notified from /proc
  	   that some devices have changed, so we do a report 
  	   physical luns cmd, and adjust our list of devices 
  	   accordingly.  (We can't rely on the scsi-mid layer just
  	   doing inquiries, because the "busses" that the scsi 
  	   mid-layer probes are totally fabricated by this driver,
  	   so new devices wouldn't show up.
  
  	   the scsi3addr's of devices won't change so long as the 
  	   adapter is not reset.  That means we can rescan and 
  	   tell which devices we already know about, vs. new 
  	   devices, vs.  disappearing devices.
  
  	   Also, if you yank out a tape drive, then put in a disk
  	   in it's place, (say, a configured volume from another 
  	   array controller for instance)  _don't_ poke this driver 
             (so it thinks it's still a tape, but _do_ poke the scsi 
             mid layer, so it does an inquiry... the scsi mid layer 
             will see the physical disk.  This would be bad.  Need to
  	   think about how to prevent that.  One idea would be to 
  	   snoop all scsi responses and if an inquiry repsonse comes
  	   back that reports a disk, chuck it an return selection
  	   timeout instead and adjust our table...  Not sure i like
  	   that though.  
  
  	 */
47922d068   Mike Miller   [PATCH] cciss: On...
1228
1229
  #define OBDR_TAPE_INQ_SIZE 49
  #define OBDR_TAPE_SIG "$DR-10"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1230
  	ReportLunData_struct *ld_buff;
47922d068   Mike Miller   [PATCH] cciss: On...
1231
  	unsigned char *inq_buff;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1232
1233
1234
1235
  	unsigned char scsi3addr[8];
  	ctlr_info_t *c;
  	__u32 num_luns=0;
  	unsigned char *ch;
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1236
  	struct cciss_scsi_dev_t *currentsd, *this_device;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1237
1238
1239
1240
1241
  	int ncurrent=0;
  	int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
  	int i;
  
  	c = (ctlr_info_t *) hba[cntl_num];	
06ff37ffb   Eric Sesterhenn   [PATCH] kzalloc()...
1242
  	ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
47922d068   Mike Miller   [PATCH] cciss: On...
1243
  	inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1244
1245
1246
1247
1248
1249
  	currentsd = kzalloc(sizeof(*currentsd) *
  			(CCISS_MAX_SCSI_DEVS_PER_HBA+1), GFP_KERNEL);
  	if (ld_buff == NULL || inq_buff == NULL || currentsd == NULL) {
  		printk(KERN_ERR "cciss: out of memory
  ");
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1250
  	}
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1251
  	this_device = &currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
  	if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) {
  		ch = &ld_buff->LUNListLength[0];
  		num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
  		if (num_luns > CISS_MAX_PHYS_LUN) {
  			printk(KERN_WARNING 
  				"cciss: Maximum physical LUNs (%d) exceeded.  "
  				"%d LUNs ignored.
  ", CISS_MAX_PHYS_LUN, 
  				num_luns - CISS_MAX_PHYS_LUN);
  			num_luns = CISS_MAX_PHYS_LUN;
  		}
  	}
  	else {
  		printk(KERN_ERR  "cciss: Report physical LUNs failed.
  ");
  		goto out;
  	}
  
  
  	/* adjust our table of devices */	
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1272
  	for (i = 0; i < num_luns; i++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1273
1274
  		/* for each physical lun, do an inquiry */
  		if (ld_buff->LUN[i][3] & 0xC0) continue;
47922d068   Mike Miller   [PATCH] cciss: On...
1275
  		memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1276
  		memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1277
1278
  		if (cciss_scsi_do_inquiry(hba[cntl_num], scsi3addr, 0, inq_buff,
  			(unsigned char) OBDR_TAPE_INQ_SIZE) != 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1279
  			/* Inquiry failed (msg printed already) */
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
  			continue; /* so we will skip this device. */
  
  		this_device->devtype = (inq_buff[0] & 0x1f);
  		this_device->bus = -1;
  		this_device->target = -1;
  		this_device->lun = -1;
  		memcpy(this_device->scsi3addr, scsi3addr, 8);
  		memcpy(this_device->vendor, &inq_buff[8],
  			sizeof(this_device->vendor));
  		memcpy(this_device->model, &inq_buff[16],
  			sizeof(this_device->model));
  		memcpy(this_device->revision, &inq_buff[32],
  			sizeof(this_device->revision));
  		memset(this_device->device_id, 0,
  			sizeof(this_device->device_id));
  		cciss_scsi_get_device_id(hba[cntl_num], scsi3addr,
  			this_device->device_id, sizeof(this_device->device_id));
  
  		switch (this_device->devtype)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1299
  		{
47922d068   Mike Miller   [PATCH] cciss: On...
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
  		  case 0x05: /* CD-ROM */ {
  
  			/* We don't *really* support actual CD-ROM devices,
  			 * just this "One Button Disaster Recovery" tape drive
  			 * which temporarily pretends to be a CD-ROM drive.
  			 * So we check that the device is really an OBDR tape
  			 * device by checking for "$DR-10" in bytes 43-48 of
  			 * the inquiry data.
  			 */
  				char obdr_sig[7];
  
  				strncpy(obdr_sig, &inq_buff[43], 6);
  				obdr_sig[6] = '\0';
  				if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
  					/* Not OBDR device, ignore it. */
  					break;
  			}
  			/* fall through . . . */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1318
1319
1320
1321
1322
1323
  		  case 0x01: /* sequential access, (tape) */
  		  case 0x08: /* medium changer */
  			if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
  				printk(KERN_INFO "cciss%d: %s ignored, "
  					"too many devices.
  ", cntl_num,
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1324
  					scsi_device_type(this_device->devtype));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1325
1326
  				break;
  			}
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1327
  			currentsd[ncurrent] = *this_device;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
  			ncurrent++;
  			break;
  		  default: 
  			break;
  		}
  	}
  
  	adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
  out:
  	kfree(inq_buff);
  	kfree(ld_buff);
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1339
  	kfree(currentsd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
  	return;
  }
  
  static int
  is_keyword(char *ptr, int len, char *verb)  // Thanks to ncr53c8xx.c
  {
  	int verb_len = strlen(verb);
  	if (len >= verb_len && !memcmp(verb,ptr,verb_len))
  		return verb_len;
  	else
  		return 0;
  }
  
  static int
  cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
  {
  	int arg_len;
  
  	if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
  		cciss_update_non_disk_devices(ctlr, hostno);
  	else
  		return -EINVAL;
  	return length;
  }
  
  
  static int
  cciss_scsi_proc_info(struct Scsi_Host *sh,
  		char *buffer, /* data buffer */
  		char **start, 	   /* where data in buffer starts */
  		off_t offset,	   /* offset from start of imaginary file */
  		int length, 	   /* length of data in buffer */
  		int func)	   /* 0 == read, 1 == write */
  {
  
  	int buflen, datalen;
  	ctlr_info_t *ci;
b9f0bd089   Mike Miller   [PATCH] cciss: SC...
1377
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
  	int cntl_num;
  
  
  	ci = (ctlr_info_t *) sh->hostdata[0];
  	if (ci == NULL)  /* This really shouldn't ever happen. */
  		return -EINVAL;
  
  	cntl_num = ci->ctlr;	/* Get our index into the hba[] array */
  
  	if (func == 0) {	/* User is reading from /proc/scsi/ciss*?/?*  */
b9f0bd089   Mike Miller   [PATCH] cciss: SC...
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
  		buflen = sprintf(buffer, "cciss%d: SCSI host: %d
  ",
  				cntl_num, sh->host_no);
  
  		/* this information is needed by apps to know which cciss
  		   device corresponds to which scsi host number without
  		   having to open a scsi target device node.  The device
  		   information is not a duplicate of /proc/scsi/scsi because
  		   the two may be out of sync due to scsi hotplug, rather
  		   this info is for an app to be able to use to know how to
  		   get them back in sync. */
  
  		for (i=0;i<ccissscsi[cntl_num].ndevices;i++) {
  			struct cciss_scsi_dev_t *sd = &ccissscsi[cntl_num].dev[i];
  			buflen += sprintf(&buffer[buflen], "c%db%dt%dl%d %02d "
  				"0x%02x%02x%02x%02x%02x%02x%02x%02x
  ",
  				sh->host_no, sd->bus, sd->target, sd->lun,
  				sd->devtype,
  				sd->scsi3addr[0], sd->scsi3addr[1],
  				sd->scsi3addr[2], sd->scsi3addr[3],
  				sd->scsi3addr[4], sd->scsi3addr[5],
  				sd->scsi3addr[6], sd->scsi3addr[7]);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
  		datalen = buflen - offset;
  		if (datalen < 0) { 	/* they're reading past EOF. */
  			datalen = 0;
  			*start = buffer+buflen;	
  		} else
  			*start = buffer + offset;
  		return(datalen);
  	} else 	/* User is writing to /proc/scsi/cciss*?/?*  ... */
  		return cciss_scsi_user_command(cntl_num, sh->host_no,
  			buffer, length);	
  } 
  
  /* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci 
     dma mapping  and fills in the scatter gather entries of the 
     cciss command, cp. */
  
  static void
  cciss_scatter_gather(struct pci_dev *pdev, 
  		CommandList_struct *cp,	
  		struct scsi_cmnd *cmd)
  {
41ce639a1   FUJITA Tomonori   [SCSI] cciss: con...
1433
1434
  	unsigned int len;
  	struct scatterlist *sg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1435
  	__u64 addr64;
41ce639a1   FUJITA Tomonori   [SCSI] cciss: con...
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
  	int use_sg, i;
  
  	BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
  
  	use_sg = scsi_dma_map(cmd);
  	if (use_sg) {	/* not too many addrs? */
  		scsi_for_each_sg(cmd, sg, use_sg, i) {
  			addr64 = (__u64) sg_dma_address(sg);
  			len  = sg_dma_len(sg);
  			cp->SG[i].Addr.lower =
  				(__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
  			cp->SG[i].Addr.upper =
  				(__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
  			cp->SG[i].Len = len;
  			cp->SG[i].Ext = 0;  // we are not chaining
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1451
  		}
41ce639a1   FUJITA Tomonori   [SCSI] cciss: con...
1452
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1453

41ce639a1   FUJITA Tomonori   [SCSI] cciss: con...
1454
1455
  	cp->Header.SGList = (__u8) use_sg;   /* no. SGs contig in this cmd */
  	cp->Header.SGTotal = (__u16) use_sg; /* total sgs in this cmd list */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
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
1522
1523
  	return;
  }
  
  
  static int
  cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
  {
  	ctlr_info_t **c;
  	int ctlr, rc;
  	unsigned char scsi3addr[8];
  	CommandList_struct *cp;
  	unsigned long flags;
  
  	// Get the ptr to our adapter structure (hba[i]) out of cmd->host.
  	// We violate cmd->host privacy here.  (Is there another way?)
  	c = (ctlr_info_t **) &cmd->device->host->hostdata[0];	
  	ctlr = (*c)->ctlr;
  
  	rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id, 
  			cmd->device->lun, scsi3addr);
  	if (rc != 0) {
  		/* the scsi nexus does not match any that we presented... */
  		/* pretend to mid layer that we got selection timeout */
  		cmd->result = DID_NO_CONNECT << 16;
  		done(cmd);
  		/* we might want to think about registering controller itself
  		   as a processor device on the bus so sg binds to it. */
  		return 0;
  	}
  
  	/* printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%d
  ", 
  		cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);*/
  	// printk("q:%p:c%db%dt%dl%d ", cmd, ctlr, cmd->channel, 
  	//	cmd->target, cmd->lun);
  
  	/* Ok, we have a reasonable scsi nexus, so send the cmd down, and
             see what the device thinks of it. */
  
  	spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  	cp = scsi_cmd_alloc(*c);
  	spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  	if (cp == NULL) {			/* trouble... */
  		printk("scsi_cmd_alloc returned NULL!
  ");
  		/* FIXME: next 3 lines are -> BAD! <- */
  		cmd->result = DID_NO_CONNECT << 16;
  		done(cmd);
  		return 0;
  	}
  
  	// Fill in the command list header
  
  	cmd->scsi_done = done;    // save this for use by completion code 
  
  	// save cp in case we have to abort it 
  	cmd->host_scribble = (unsigned char *) cp; 
  
  	cp->cmd_type = CMD_SCSI;
  	cp->scsi_cmd = cmd;
  	cp->Header.ReplyQueue = 0;  // unused in simple mode
  	memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
  	cp->Header.Tag.lower = cp->busaddr;  // Use k. address of cmd as tag
  	
  	// Fill in the request block...
  
  	cp->Request.Timeout = 0;
  	memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
089fe1b23   Eric Sesterhenn   BUG_ON() Conversi...
1524
  	BUG_ON(cmd->cmd_len > sizeof(cp->Request.CDB));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
  	cp->Request.CDBLen = cmd->cmd_len;
  	memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len);
  	cp->Request.Type.Type = TYPE_CMD;
  	cp->Request.Type.Attribute = ATTR_SIMPLE;
  	switch(cmd->sc_data_direction)
  	{
  	  case DMA_TO_DEVICE: cp->Request.Type.Direction = XFER_WRITE; break;
  	  case DMA_FROM_DEVICE: cp->Request.Type.Direction = XFER_READ; break;
  	  case DMA_NONE: cp->Request.Type.Direction = XFER_NONE; break;
  	  case DMA_BIDIRECTIONAL:
  		// This can happen if a buggy application does a scsi passthru
  		// and sets both inlen and outlen to non-zero. ( see
  		// ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
  
  	  	cp->Request.Type.Direction = XFER_RSVD;
  		// This is technically wrong, and cciss controllers should
  		// reject it with CMD_INVALID, which is the most correct 
  		// response, but non-fibre backends appear to let it 
  		// slide by, and give the same results as if this field
  		// were set correctly.  Either way is acceptable for
  		// our purposes here.
  
  		break;
  
  	  default: 
  		printk("cciss: unknown data direction: %d
  ", 
  			cmd->sc_data_direction);
  		BUG();
  		break;
  	}
  
  	cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
  
  	/* Put the request on the tail of the request queue */
  
  	spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  	addQ(&(*c)->reqQ, cp);
  	(*c)->Qdepth++;
  	start_io(*c);
  	spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  
  	/* the cmd'll come back via intr handler in complete_scsi_command()  */
  	return 0;
  }
  
  static void 
  cciss_unregister_scsi(int ctlr)
  {
  	struct cciss_scsi_adapter_data_t *sa;
  	struct cciss_scsi_cmd_stack_t *stk;
  	unsigned long flags;
  
  	/* we are being forcibly unloaded, and may not refuse. */
  
  	spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  	sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
  	stk = &sa->cmd_stack; 
  
  	/* if we weren't ever actually registered, don't unregister */ 
  	if (sa->registered) {
  		spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  		scsi_remove_host(sa->scsi_host);
  		scsi_host_put(sa->scsi_host);
  		spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  	}
  
  	/* set scsi_host to NULL so our detect routine will 
  	   find us on register */
  	sa->scsi_host = NULL;
6195057f5   scameron@beardog.cca.cpqcorp.net   cciss: fix warnin...
1595
  	spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1596
1597
  	scsi_cmd_stack_free(ctlr);
  	kfree(sa);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1598
1599
1600
  }
  
  static int 
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1601
1602
1603
1604
1605
1606
1607
1608
1609
  cciss_engage_scsi(int ctlr)
  {
  	struct cciss_scsi_adapter_data_t *sa;
  	struct cciss_scsi_cmd_stack_t *stk;
  	unsigned long flags;
  
  	spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  	sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
  	stk = &sa->cmd_stack; 
f4a93bcda   Mike Miller   cciss: change the...
1610
  	if (sa->registered) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1611
1612
1613
1614
1615
  		printk("cciss%d: SCSI subsystem already engaged.
  ", ctlr);
  		spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  		return ENXIO;
  	}
f4a93bcda   Mike Miller   cciss: change the...
1616
  	sa->registered = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1617
1618
  	spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  	cciss_update_non_disk_devices(ctlr, -1);
f4a93bcda   Mike Miller   cciss: change the...
1619
  	cciss_scsi_detect(ctlr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1620
1621
1622
1623
  	return 0;
  }
  
  static void
89b6e7437   Mike Miller   resubmit: cciss: ...
1624
  cciss_seq_tape_report(struct seq_file *seq, int ctlr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1625
1626
  {
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1627
1628
  
  	CPQ_TAPE_LOCK(ctlr, flags);
89b6e7437   Mike Miller   resubmit: cciss: ...
1629
  	seq_printf(seq,
b9f0bd089   Mike Miller   [PATCH] cciss: SC...
1630
1631
1632
  		"Sequential access devices: %d
  
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1633
1634
  			ccissscsi[ctlr].ndevices);
  	CPQ_TAPE_UNLOCK(ctlr, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1635
  }
89b6e7437   Mike Miller   resubmit: cciss: ...
1636

3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
  /* Need at least one of these error handlers to keep ../scsi/hosts.c from 
   * complaining.  Doing a host- or bus-reset can't do anything good here. 
   * Despite what it might say in scsi_error.c, there may well be commands
   * on the controller, as the cciss driver registers twice, once as a block
   * device for the logical drives, and once as a scsi device, for any tape
   * drives.  So we know there are no commands out on the tape drives, but we
   * don't know there are no commands on the controller, and it is likely 
   * that there probably are, as the cciss block device is most commonly used
   * as a boot device (embedded controller on HP/Compaq systems.)
  */
  
  static int cciss_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
  {
  	int rc;
  	CommandList_struct *cmd_in_trouble;
  	ctlr_info_t **c;
  	int ctlr;
  
  	/* find the controller to which the command to be aborted was sent */
  	c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];	
  	if (c == NULL) /* paranoia */
  		return FAILED;
  	ctlr = (*c)->ctlr;
  	printk(KERN_WARNING "cciss%d: resetting tape drive or medium changer.
  ", ctlr);
  
  	/* find the command that's giving us trouble */
  	cmd_in_trouble = (CommandList_struct *) scsicmd->host_scribble;
  	if (cmd_in_trouble == NULL) { /* paranoia */
  		return FAILED;
  	}
  	/* send a reset to the SCSI LUN which the command was sent to */
  	rc = sendcmd(CCISS_RESET_MSG, ctlr, NULL, 0, 2, 0, 0, 
  		(unsigned char *) &cmd_in_trouble->Header.LUN.LunAddrBytes[0], 
  		TYPE_MSG);
f66083c37   Joe Perches   drivers/block/: S...
1672
  	/* sendcmd turned off interrupts on the board, turn 'em back on. */
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
  	(*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
  	if (rc == 0)
  		return SUCCESS;
  	printk(KERN_WARNING "cciss%d: resetting device failed.
  ", ctlr);
  	return FAILED;
  }
  
  static int  cciss_eh_abort_handler(struct scsi_cmnd *scsicmd)
  {
  	int rc;
  	CommandList_struct *cmd_to_abort;
  	ctlr_info_t **c;
  	int ctlr;
  
  	/* find the controller to which the command to be aborted was sent */
  	c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];	
  	if (c == NULL) /* paranoia */
  		return FAILED;
  	ctlr = (*c)->ctlr;
  	printk(KERN_WARNING "cciss%d: aborting tardy SCSI cmd
  ", ctlr);
  
  	/* find the command to be aborted */
  	cmd_to_abort = (CommandList_struct *) scsicmd->host_scribble;
  	if (cmd_to_abort == NULL) /* paranoia */
  		return FAILED;
  	rc = sendcmd(CCISS_ABORT_MSG, ctlr, &cmd_to_abort->Header.Tag, 
  		0, 2, 0, 0, 
  		(unsigned char *) &cmd_to_abort->Header.LUN.LunAddrBytes[0], 
  		TYPE_MSG);
f66083c37   Joe Perches   drivers/block/: S...
1704
  	/* sendcmd turned off interrupts on the board, turn 'em back on. */
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1705
1706
1707
1708
1709
1710
  	(*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
  	if (rc == 0)
  		return SUCCESS;
  	return FAILED;
  
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1711
1712
1713
1714
1715
  #else /* no CONFIG_CISS_SCSI_TAPE */
  
  /* If no tape support, then these become defined out of existence */
  
  #define cciss_scsi_setup(cntl_num)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1716
1717
  
  #endif /* CONFIG_CISS_SCSI_TAPE */