Blame view

drivers/block/cciss_scsi.c 48.2 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
  #include <linux/timer.h>
  #include <linux/completion.h>
  #include <linux/slab.h>
  #include <linux/string.h>
60063497a   Arun Sharma   atomic: use <linu...
34
  #include <linux/atomic.h>
4e57b6817   Tim Schmielau   [PATCH] fix missi...
35

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
f70dba836   Stephen M. Cameron   cciss: use consis...
43
  static int fill_cmd(ctlr_info_t *h, CommandList_struct *c, __u8 cmd, void *buff,
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
44
  	size_t size,
b57695fe1   scameron@beardog.cca.cpqcorp.net   cciss: simplify i...
45
  	__u8 page_code, unsigned char *scsi3addr,
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
46
  	int cmd_type);
6b4d96b87   Stephen M. Cameron   cciss: separate c...
47
48
49
50
  static CommandList_struct *cmd_alloc(ctlr_info_t *h);
  static CommandList_struct *cmd_special_alloc(ctlr_info_t *h);
  static void cmd_free(ctlr_info_t *h, CommandList_struct *c);
  static void cmd_special_free(ctlr_info_t *h, CommandList_struct *c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
54
55
56
57
58
  
  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 */
f281233d3   Jeff Garzik   SCSI host lock pu...
59
60
  static int cciss_scsi_queue_command (struct Scsi_Host *h,
  				     struct scsi_cmnd *cmd);
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
61
62
  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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  
  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,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
  	.this_id		= 7,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
  	.cmd_per_lun		= 1,
  	.use_clustering		= DISABLE_CLUSTERING,
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
84
85
86
  	/* 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
87
88
89
  };
  
  #pragma pack(1)
1b7d0d28a   Stephen M. Cameron   cciss: detect bad...
90

5e216153c   Mike Miller   cciss: add perfor...
91
92
  #define SCSI_PAD_32 8
  #define SCSI_PAD_64 8
1b7d0d28a   Stephen M. Cameron   cciss: detect bad...
93

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
95
96
97
  struct cciss_scsi_cmd_stack_elem_t {
  	CommandList_struct cmd;
  	ErrorInfo_struct Err;
  	__u32 busaddr;
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
98
  	int cmdindex;
1b7d0d28a   Stephen M. Cameron   cciss: detect bad...
99
  	u8 pad[IS_32_BIT * SCSI_PAD_32 + IS_64_BIT * SCSI_PAD_64];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
102
  };
  
  #pragma pack()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
104
105
  #pragma pack(1)
  struct cciss_scsi_cmd_stack_t {
  	struct cciss_scsi_cmd_stack_elem_t *pool;
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
106
  	struct cciss_scsi_cmd_stack_elem_t **elem;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
108
  	dma_addr_t cmd_pool_handle;
  	int top;
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
109
  	int nelems;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
111
112
113
114
115
  };
  #pragma pack()
  
  struct cciss_scsi_adapter_data_t {
  	struct Scsi_Host *scsi_host;
  	struct cciss_scsi_cmd_stack_t cmd_stack;
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
116
  	SGDescriptor_struct **cmd_sg_list;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
118
119
  	int registered;
  	spinlock_t lock; // to protect ccissscsi[ctlr]; 
  };
f70dba836   Stephen M. Cameron   cciss: use consis...
120
121
122
123
  #define CPQ_TAPE_LOCK(h, flags) spin_lock_irqsave( \
  	&h->scsi_ctlr->lock, flags);
  #define CPQ_TAPE_UNLOCK(h, flags) spin_unlock_irqrestore( \
  	&h->scsi_ctlr->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
125
126
127
128
  
  static CommandList_struct *
  scsi_cmd_alloc(ctlr_info_t *h)
  {
  	/* assume only one process in here at a time, locking done by caller. */
f70dba836   Stephen M. Cameron   cciss: use consis...
129
  	/* use h->lock */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130
131
132
133
134
135
136
137
  	/* 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;
aad9fb6f2   Stephen M. Cameron   cciss: do not use...
138
  	sa = h->scsi_ctlr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
142
143
144
145
146
147
148
  	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; 
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
149
  	c->cmd.cmdindex = c->cmdindex;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  	/* (__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 
f70dba836   Stephen M. Cameron   cciss: use consis...
169
  scsi_cmd_free(ctlr_info_t *h, CommandList_struct *c)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
171
  {
  	/* assume only one process in here at a time, locking done by caller. */
f70dba836   Stephen M. Cameron   cciss: use consis...
172
  	/* use h->lock */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
174
175
176
  	/* drop the free memory chunk on top of the stack. */
  
  	struct cciss_scsi_adapter_data_t *sa;
  	struct cciss_scsi_cmd_stack_t *stk;
aad9fb6f2   Stephen M. Cameron   cciss: do not use...
177
  	sa = h->scsi_ctlr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
  	stk = &sa->cmd_stack; 
713b68649   Dan Carpenter   cciss: call BUG()...
179
  	stk->top++;
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
180
  	if (stk->top >= stk->nelems) {
b2a4a43db   Stephen M. Cameron   cciss: change pri...
181
182
183
  		dev_err(&h->pdev->dev,
  			"scsi_cmd_free called too many times.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
185
  		BUG();
  	}
f70dba836   Stephen M. Cameron   cciss: use consis...
186
  	stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) c;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
188
189
  }
  
  static int
f70dba836   Stephen M. Cameron   cciss: use consis...
190
  scsi_cmd_stack_setup(ctlr_info_t *h, struct cciss_scsi_adapter_data_t *sa)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
192
193
194
  {
  	int i;
  	struct cciss_scsi_cmd_stack_t *stk;
  	size_t size;
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
195
196
  	stk = &sa->cmd_stack;
  	stk->nelems = cciss_tape_cmds + 2;
f70dba836   Stephen M. Cameron   cciss: use consis...
197
  	sa->cmd_sg_list = cciss_allocate_sg_chain_blocks(h,
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
198
  		h->chainsize, stk->nelems);
f70dba836   Stephen M. Cameron   cciss: use consis...
199
  	if (!sa->cmd_sg_list && h->chainsize > 0)
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
200
  		return -ENOMEM;
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
201
  	size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * stk->nelems;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202

1b7d0d28a   Stephen M. Cameron   cciss: detect bad...
203
204
205
  	/* Check alignment, see cciss_cmd.h near CommandList_struct def. */
  	BUILD_BUG_ON((sizeof(*stk->pool) % COMMANDLIST_ALIGNMENT) != 0);
  	/* pci_alloc_consistent guarantees 32-bit DMA address will be used */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
  	stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)
f70dba836   Stephen M. Cameron   cciss: use consis...
207
  		pci_alloc_consistent(h->pdev, size, &stk->cmd_pool_handle);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
  
  	if (stk->pool == NULL) {
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
210
  		cciss_free_sg_chain_blocks(sa->cmd_sg_list, stk->nelems);
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
211
212
  		sa->cmd_sg_list = NULL;
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
  	}
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
214
215
216
217
218
219
220
  	stk->elem = kmalloc(sizeof(stk->elem[0]) * stk->nelems, GFP_KERNEL);
  	if (!stk->elem) {
  		pci_free_consistent(h->pdev, size, stk->pool,
  		stk->cmd_pool_handle);
  		return -1;
  	}
  	for (i = 0; i < stk->nelems; i++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
221
222
223
  		stk->elem[i] = &stk->pool[i];
  		stk->elem[i]->busaddr = (__u32) (stk->cmd_pool_handle + 
  			(sizeof(struct cciss_scsi_cmd_stack_elem_t) * i));
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
224
  		stk->elem[i]->cmdindex = i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225
  	}
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
226
  	stk->top = stk->nelems-1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
229
230
  	return 0;
  }
  
  static void
f70dba836   Stephen M. Cameron   cciss: use consis...
231
  scsi_cmd_stack_free(ctlr_info_t *h)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
233
234
235
  {
  	struct cciss_scsi_adapter_data_t *sa;
  	struct cciss_scsi_cmd_stack_t *stk;
  	size_t size;
f70dba836   Stephen M. Cameron   cciss: use consis...
236
  	sa = h->scsi_ctlr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
  	stk = &sa->cmd_stack; 
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
238
  	if (stk->top != stk->nelems-1) {
b2a4a43db   Stephen M. Cameron   cciss: change pri...
239
240
241
  		dev_warn(&h->pdev->dev,
  			"bug: %d scsi commands are still outstanding.
  ",
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
242
  			stk->nelems - stk->top);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
  	}
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
244
  	size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * stk->nelems;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245

f70dba836   Stephen M. Cameron   cciss: use consis...
246
  	pci_free_consistent(h->pdev, size, stk->pool, stk->cmd_pool_handle);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
  	stk->pool = NULL;
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
248
249
250
  	cciss_free_sg_chain_blocks(sa->cmd_sg_list, stk->nelems);
  	kfree(stk->elem);
  	stk->elem = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
  }
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
  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 
f70dba836   Stephen M. Cameron   cciss: use consis...
363
  find_bus_target_lun(ctlr_info_t *h, int *bus, int *target, int *lun)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
  {
  	/* finds an unused bus, target, lun for a new device */
f70dba836   Stephen M. Cameron   cciss: use consis...
366
  	/* assumes h->scsi_ctlr->lock is held */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
368
369
370
371
372
  	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;	
f70dba836   Stephen M. Cameron   cciss: use consis...
373
374
  	for (i = 0; i < ccissscsi[h->ctlr].ndevices; i++)
  		target_taken[ccissscsi[h->ctlr].dev[i].target] = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
  	
f70dba836   Stephen M. Cameron   cciss: use consis...
376
  	for (i = 0; i < CCISS_MAX_SCSI_DEVS_PER_HBA; i++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377
378
379
380
381
382
383
  		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
  
  static int 
f70dba836   Stephen M. Cameron   cciss: use consis...
390
  cciss_scsi_add_entry(ctlr_info_t *h, 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
  {
f70dba836   Stephen M. Cameron   cciss: use consis...
394
395
  	/* assumes h->scsi_ctlr->lock is held */
  	int n = ccissscsi[h->ctlr].ndevices;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396
  	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
  
  	if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
b2a4a43db   Stephen M. Cameron   cciss: change pri...
401
402
403
  		dev_warn(&h->pdev->dev, "Too many devices, "
  			"some will be inaccessible.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
404
405
  		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
  		addr1[4] = 0;
  		for (i = 0; i < n; i++) {
f70dba836   Stephen M. Cameron   cciss: use consis...
419
  			sd = &ccissscsi[h->ctlr].dev[i];
935dc8d75   Mike Miller   cciss: add suppor...
420
421
422
423
424
425
  			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
  				break;
  			}
  		}
  	}
f70dba836   Stephen M. Cameron   cciss: use consis...
431
  	sd = &ccissscsi[h->ctlr].dev[n];
935dc8d75   Mike Miller   cciss: add suppor...
432
  	if (lun == 0) {
f70dba836   Stephen M. Cameron   cciss: use consis...
433
  		if (find_bus_target_lun(h,
935dc8d75   Mike Miller   cciss: add suppor...
434
435
436
437
438
439
440
  			&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...
441
442
443
444
  	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 ...
445
446
447
448
449
  	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;
f70dba836   Stephen M. Cameron   cciss: use consis...
450
  	ccissscsi[h->ctlr].ndevices++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
451
452
453
454
455
  
  	/* 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)
b2a4a43db   Stephen M. Cameron   cciss: change pri...
456
457
458
  		dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.
  ",
  			scsi_device_type(sd->devtype), hostno,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
459
460
461
462
463
  			sd->bus, sd->target, sd->lun);
  	return 0;
  }
  
  static void
f70dba836   Stephen M. Cameron   cciss: use consis...
464
  cciss_scsi_remove_entry(ctlr_info_t *h, int hostno, int entry,
f4a93bcda   Mike Miller   cciss: change the...
465
  	struct scsi2map *removed, int *nremoved)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
  {
f70dba836   Stephen M. Cameron   cciss: use consis...
467
  	/* assumes h->ctlr]->scsi_ctlr->lock is held */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468
469
470
471
  	int i;
  	struct cciss_scsi_dev_t sd;
  
  	if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
f70dba836   Stephen M. Cameron   cciss: use consis...
472
  	sd = ccissscsi[h->ctlr].dev[entry];
f4a93bcda   Mike Miller   cciss: change the...
473
474
475
476
  	removed[*nremoved].bus    = sd.bus;
  	removed[*nremoved].target = sd.target;
  	removed[*nremoved].lun    = sd.lun;
  	(*nremoved)++;
f70dba836   Stephen M. Cameron   cciss: use consis...
477
478
479
  	for (i = entry; i < ccissscsi[h->ctlr].ndevices-1; i++)
  		ccissscsi[h->ctlr].dev[i] = ccissscsi[h->ctlr].dev[i+1];
  	ccissscsi[h->ctlr].ndevices--;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
480
481
482
  	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.
  ",
  		scsi_device_type(sd.devtype), hostno,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
483
484
485
486
487
488
489
490
491
492
493
494
495
  			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])
f70dba836   Stephen M. Cameron   cciss: use consis...
496
  static void fixup_botched_add(ctlr_info_t *h, char *scsi3addr)
f4a93bcda   Mike Miller   cciss: change the...
497
498
499
500
501
  {
  	/* 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;
f70dba836   Stephen M. Cameron   cciss: use consis...
502
503
  	CPQ_TAPE_LOCK(h, flags);
  	for (i = 0; i < ccissscsi[h->ctlr].ndevices; i++) {
f4a93bcda   Mike Miller   cciss: change the...
504
  		if (memcmp(scsi3addr,
f70dba836   Stephen M. Cameron   cciss: use consis...
505
506
507
508
509
  				ccissscsi[h->ctlr].dev[i].scsi3addr, 8) == 0) {
  			for (j = i; j < ccissscsi[h->ctlr].ndevices-1; j++)
  				ccissscsi[h->ctlr].dev[j] =
  					ccissscsi[h->ctlr].dev[j+1];
  			ccissscsi[h->ctlr].ndevices--;
f4a93bcda   Mike Miller   cciss: change the...
510
511
512
  			break;
  		}
  	}
f70dba836   Stephen M. Cameron   cciss: use consis...
513
  	CPQ_TAPE_UNLOCK(h, flags);
f4a93bcda   Mike Miller   cciss: change the...
514
  }
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
  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
530
  static int
f70dba836   Stephen M. Cameron   cciss: use consis...
531
  adjust_cciss_scsi_table(ctlr_info_t *h, int hostno,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
532
533
534
535
536
537
538
539
540
541
  	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...
542
543
544
545
546
547
548
549
550
551
  	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) {
b2a4a43db   Stephen M. Cameron   cciss: change pri...
552
553
554
  		dev_warn(&h->pdev->dev,
  			"Out of memory in adjust_cciss_scsi_table
  ");
f4a93bcda   Mike Miller   cciss: change the...
555
556
  		goto free_and_out;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
557

f70dba836   Stephen M. Cameron   cciss: use consis...
558
  	CPQ_TAPE_LOCK(h, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559

f4a93bcda   Mike Miller   cciss: change the...
560
  	if (hostno != -1)  /* if it's not the first time... */
f70dba836   Stephen M. Cameron   cciss: use consis...
561
  		sh = h->scsi_ctlr->scsi_host;
f4a93bcda   Mike Miller   cciss: change the...
562

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;
f70dba836   Stephen M. Cameron   cciss: use consis...
569
570
  	while (i < ccissscsi[h->ctlr].ndevices) {
  		csd = &ccissscsi[h->ctlr].dev[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
571
572
573
574
  		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
  					found=2;
  				else
  					found=1;
  				break;
  			}
  		}
  
  		if (found == 0) { /* device no longer present. */ 
  			changes++;
f70dba836   Stephen M. Cameron   cciss: use consis...
585
  			cciss_scsi_remove_entry(h, hostno, i,
f4a93bcda   Mike Miller   cciss: change the...
586
587
  				removed, &nremoved);
  			/* remove ^^^, hence i not incremented */
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
588
  		} else if (found == 1) { /* device is different in some way */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
589
  			changes++;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
590
591
592
593
  			dev_info(&h->pdev->dev,
  				"device c%db%dt%dl%d has changed.
  ",
  				hostno, csd->bus, csd->target, csd->lun);
f70dba836   Stephen M. Cameron   cciss: use consis...
594
  			cciss_scsi_remove_entry(h, hostno, i,
f4a93bcda   Mike Miller   cciss: change the...
595
596
  				removed, &nremoved);
  			/* remove ^^^, hence i not incremented */
f70dba836   Stephen M. Cameron   cciss: use consis...
597
  			if (cciss_scsi_add_entry(h, hostno, &sd[j],
f4a93bcda   Mike Miller   cciss: change the...
598
599
600
  				added, &nadded) != 0)
  				/* we just removed one, so add can't fail. */
  					BUG();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
601
  			csd->devtype = sd[j].devtype;
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
602
603
604
605
606
607
608
609
  			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
610
611
612
613
614
615
616
617
618
  		} 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;
f70dba836   Stephen M. Cameron   cciss: use consis...
619
620
  		for (j = 0; j < ccissscsi[h->ctlr].ndevices; j++) {
  			csd = &ccissscsi[h->ctlr].dev[j];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
621
622
  			if (SCSI3ADDR_EQ(sd[i].scsi3addr,
  				csd->scsi3addr)) {
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
623
  				if (device_is_the_same(&sd[i], csd))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
624
625
626
627
628
629
630
631
  					found=2;	/* found device */
  				else
  					found=1; 	/* found a bug. */
  				break;
  			}
  		}
  		if (!found) {
  			changes++;
f70dba836   Stephen M. Cameron   cciss: use consis...
632
  			if (cciss_scsi_add_entry(h, hostno, &sd[i],
f4a93bcda   Mike Miller   cciss: change the...
633
  				added, &nadded) != 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634
635
636
637
  				break;
  		} else if (found == 1) {
  			/* should never happen... */
  			changes++;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
638
639
640
  			dev_warn(&h->pdev->dev,
  				"device unexpectedly changed
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
642
643
  			/* but if it does happen, we just ignore that device */
  		}
  	}
f70dba836   Stephen M. Cameron   cciss: use consis...
644
  	CPQ_TAPE_UNLOCK(h, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
645

f4a93bcda   Mike Miller   cciss: change the...
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
  	/* 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. */
b2a4a43db   Stephen M. Cameron   cciss: change pri...
664
  			dev_warn(&h->pdev->dev, "didn't find "
f4a93bcda   Mike Miller   cciss: change the...
665
666
  				"c%db%dt%dl%d
   for removal.",
b2a4a43db   Stephen M. Cameron   cciss: change pri...
667
  				hostno, removed[i].bus,
f4a93bcda   Mike Miller   cciss: change the...
668
669
670
671
672
673
674
675
676
677
678
  				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;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
679
  		dev_warn(&h->pdev->dev, "scsi_add_device "
f4a93bcda   Mike Miller   cciss: change the...
680
681
  			"c%db%dt%dl%d failed, device not added.
  ",
b2a4a43db   Stephen M. Cameron   cciss: change pri...
682
  			hostno, added[i].bus, added[i].target, added[i].lun);
f4a93bcda   Mike Miller   cciss: change the...
683
684
  		/* now we have to remove it from ccissscsi, */
  		/* since it didn't get added to scsi mid layer */
f70dba836   Stephen M. Cameron   cciss: use consis...
685
  		fixup_botched_add(h, added[i].scsi3addr);
f4a93bcda   Mike Miller   cciss: change the...
686
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687

f4a93bcda   Mike Miller   cciss: change the...
688
689
690
  free_and_out:
  	kfree(added);
  	kfree(removed);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
691
692
693
694
  	return 0;
  }
  
  static int
f70dba836   Stephen M. Cameron   cciss: use consis...
695
  lookup_scsi3addr(ctlr_info_t *h, int bus, int target, int lun, char *scsi3addr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
697
698
699
  {
  	int i;
  	struct cciss_scsi_dev_t *sd;
  	unsigned long flags;
f70dba836   Stephen M. Cameron   cciss: use consis...
700
701
702
  	CPQ_TAPE_LOCK(h, flags);
  	for (i = 0; i < ccissscsi[h->ctlr].ndevices; i++) {
  		sd = &ccissscsi[h->ctlr].dev[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703
704
705
706
  		if (sd->bus == bus &&
  		    sd->target == target &&
  		    sd->lun == lun) {
  			memcpy(scsi3addr, &sd->scsi3addr[0], 8);
f70dba836   Stephen M. Cameron   cciss: use consis...
707
  			CPQ_TAPE_UNLOCK(h, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708
709
710
  			return 0;
  		}
  	}
f70dba836   Stephen M. Cameron   cciss: use consis...
711
  	CPQ_TAPE_UNLOCK(h, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
712
713
714
715
  	return -1;
  }
  
  static void 
f70dba836   Stephen M. Cameron   cciss: use consis...
716
  cciss_scsi_setup(ctlr_info_t *h)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
717
718
  {
  	struct cciss_scsi_adapter_data_t * shba;
f70dba836   Stephen M. Cameron   cciss: use consis...
719
  	ccissscsi[h->ctlr].ndevices = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
720
721
722
723
724
725
726
  	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;
f70dba836   Stephen M. Cameron   cciss: use consis...
727
  	if (scsi_cmd_stack_setup(h, shba) != 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
728
729
730
  		kfree(shba);
  		shba = NULL;
  	}
f70dba836   Stephen M. Cameron   cciss: use consis...
731
  	h->scsi_ctlr = shba;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
732
733
  	return;
  }
f70dba836   Stephen M. Cameron   cciss: use consis...
734
735
  static void complete_scsi_command(CommandList_struct *c, int timeout,
  	__u32 tag)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
737
  {
  	struct scsi_cmnd *cmd;
f70dba836   Stephen M. Cameron   cciss: use consis...
738
  	ctlr_info_t *h;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
739
  	ErrorInfo_struct *ei;
f70dba836   Stephen M. Cameron   cciss: use consis...
740
  	ei = c->err_info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
741
742
  
  	/* First, see if it was a message rather than a command */
f70dba836   Stephen M. Cameron   cciss: use consis...
743
744
  	if (c->Request.Type.Type == TYPE_MSG)  {
  		c->cmd_type = CMD_MSG_DONE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
745
746
  		return;
  	}
f70dba836   Stephen M. Cameron   cciss: use consis...
747
748
  	cmd = (struct scsi_cmnd *) c->scsi_cmd;
  	h = hba[c->ctlr];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
749

41ce639a1   FUJITA Tomonori   [SCSI] cciss: con...
750
  	scsi_dma_unmap(cmd);
f70dba836   Stephen M. Cameron   cciss: use consis...
751
752
  	if (c->Header.SGTotal > h->max_cmd_sgentries)
  		cciss_unmap_sg_chain_block(h, c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
  
  	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...
768
  	scsi_set_resid(cmd, ei->ResidualCnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
769
770
771
772
773
774
775
776
777
778
779
  
  	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 "
f70dba836   Stephen M. Cameron   cciss: use consis...
780
781
782
  						"has SCSI Status = %x
  ",
  						c, ei->ScsiStatus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
783
  #endif
b0e15f6db   Stephen M. Cameron   cciss: fix typo t...
784
  					cmd->result |= (ei->ScsiStatus << 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
                  		}
  				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:
b2a4a43db   Stephen M. Cameron   cciss: change pri...
802
  				dev_warn(&h->pdev->dev, "%p has"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
803
  					" completed with data overrun "
f70dba836   Stephen M. Cameron   cciss: use consis...
804
805
  					"reported
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
806
807
  			break;
  			case CMD_INVALID: {
f70dba836   Stephen M. Cameron   cciss: use consis...
808
809
  				/* print_bytes(c, sizeof(*c), 1, 0);
  				print_cmd(c); */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
810
811
812
813
814
815
816
817
818
       /* 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:
b2a4a43db   Stephen M. Cameron   cciss: change pri...
819
820
821
  				dev_warn(&h->pdev->dev,
  					"%p has protocol error
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
822
823
824
                          break;
  			case CMD_HARDWARE_ERR:
  				cmd->result = DID_ERROR << 16;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
825
826
827
  				dev_warn(&h->pdev->dev,
  					"%p had hardware error
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
828
829
830
                          break;
  			case CMD_CONNECTION_LOST:
  				cmd->result = DID_ERROR << 16;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
831
832
833
  				dev_warn(&h->pdev->dev,
  					"%p had connection lost
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
834
835
836
  			break;
  			case CMD_ABORTED:
  				cmd->result = DID_ABORT << 16;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
837
838
  				dev_warn(&h->pdev->dev, "%p was aborted
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
839
840
841
  			break;
  			case CMD_ABORT_FAILED:
  				cmd->result = DID_ERROR << 16;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
842
843
844
  				dev_warn(&h->pdev->dev,
  					"%p reports abort failed
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845
846
847
  			break;
  			case CMD_UNSOLICITED_ABORT:
  				cmd->result = DID_ABORT << 16;
6d9a4f9e2   Stephen M. Cameron   cciss: fix missed...
848
  				dev_warn(&h->pdev->dev, "%p aborted due to an "
b2a4a43db   Stephen M. Cameron   cciss: change pri...
849
850
  					"unsolicited abort
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
851
852
853
  			break;
  			case CMD_TIMEOUT:
  				cmd->result = DID_TIME_OUT << 16;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
854
855
  				dev_warn(&h->pdev->dev, "%p timedout
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
856
  			break;
6d9a4f9e2   Stephen M. Cameron   cciss: fix missed...
857
858
859
860
861
862
  			case CMD_UNABORTABLE:
  				cmd->result = DID_ERROR << 16;
  				dev_warn(&h->pdev->dev, "c %p command "
  					"unabortable
  ", c);
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
863
864
  			default:
  				cmd->result = DID_ERROR << 16;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
865
866
867
  				dev_warn(&h->pdev->dev,
  					"%p returned unknown status %x
  ", c,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
868
869
870
  						ei->CommandStatus); 
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
871
  	cmd->scsi_done(cmd);
f70dba836   Stephen M. Cameron   cciss: use consis...
872
  	scsi_cmd_free(h, c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
873
874
875
  }
  
  static int
f70dba836   Stephen M. Cameron   cciss: use consis...
876
  cciss_scsi_detect(ctlr_info_t *h)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
877
878
879
880
881
882
883
884
885
886
  {
  	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;  
8a4ec67bd   Stephen M. Cameron   cciss: add cciss_...
887
  	sh->can_queue = cciss_tape_cmds;
f70dba836   Stephen M. Cameron   cciss: use consis...
888
  	sh->sg_tablesize = h->maxsgentries;
79600aadc   Stephen M. Cameron   cciss: set SCSI m...
889
  	sh->max_cmd_len = MAX_COMMAND_SIZE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
890
891
  
  	((struct cciss_scsi_adapter_data_t *) 
f70dba836   Stephen M. Cameron   cciss: use consis...
892
893
894
  		h->scsi_ctlr)->scsi_host = sh;
  	sh->hostdata[0] = (unsigned long) h;
  	sh->irq = h->intr[SIMPLE_MODE_INT];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
895
  	sh->unique_id = sh->irq;
f70dba836   Stephen M. Cameron   cciss: use consis...
896
  	error = scsi_add_host(sh, &h->pdev->dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
897
898
899
900
901
902
903
904
905
906
907
908
909
  	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,
f70dba836   Stephen M. Cameron   cciss: use consis...
910
  		CommandList_struct *c,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
911
912
913
914
  		size_t buflen,
  		int data_direction)
  {
  	u64bit addr64;
f70dba836   Stephen M. Cameron   cciss: use consis...
915
916
  	addr64.val32.lower = c->SG[0].Addr.lower;
  	addr64.val32.upper = c->SG[0].Addr.upper;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
917
918
919
920
921
  	pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
  }
  
  static void
  cciss_map_one(struct pci_dev *pdev,
f70dba836   Stephen M. Cameron   cciss: use consis...
922
  		CommandList_struct *c,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
923
924
925
926
927
928
929
  		unsigned char *buf,
  		size_t buflen,
  		int data_direction)
  {
  	__u64 addr64;
  
  	addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
f70dba836   Stephen M. Cameron   cciss: use consis...
930
  	c->SG[0].Addr.lower =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
931
  	  (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
f70dba836   Stephen M. Cameron   cciss: use consis...
932
  	c->SG[0].Addr.upper =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
933
  	  (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
f70dba836   Stephen M. Cameron   cciss: use consis...
934
935
936
  	c->SG[0].Len = buflen;
  	c->Header.SGList = (__u8) 1;   /* no. SGs contig in this cmd */
  	c->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
937
938
939
  }
  
  static int
f70dba836   Stephen M. Cameron   cciss: use consis...
940
941
  cciss_scsi_do_simple_cmd(ctlr_info_t *h,
  			CommandList_struct *c,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
942
943
944
945
946
947
  			unsigned char *scsi3addr, 
  			unsigned char *cdb,
  			unsigned char cdblen,
  			unsigned char *buf, int bufsize,
  			int direction)
  {
6e9a4738c   Peter Zijlstra   [PATCH] completio...
948
  	DECLARE_COMPLETION_ONSTACK(wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
949

f70dba836   Stephen M. Cameron   cciss: use consis...
950
951
952
953
954
  	c->cmd_type = CMD_IOCTL_PEND; /* treat this like an ioctl */
  	c->scsi_cmd = NULL;
  	c->Header.ReplyQueue = 0;  /* unused in simple mode */
  	memcpy(&c->Header.LUN, scsi3addr, sizeof(c->Header.LUN));
  	c->Header.Tag.lower = c->busaddr;  /* Use k. address of cmd as tag */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
955
956
957
958
959
960
  	// 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]); */
f70dba836   Stephen M. Cameron   cciss: use consis...
961
962
963
964
965
966
967
  	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
  	memcpy(c->Request.CDB, cdb, cdblen);
  	c->Request.Timeout = 0;
  	c->Request.CDBLen = cdblen;
  	c->Request.Type.Type = TYPE_CMD;
  	c->Request.Type.Attribute = ATTR_SIMPLE;
  	c->Request.Type.Direction = direction;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
968
969
  
  	/* Fill in the SG list and do dma mapping */
f70dba836   Stephen M. Cameron   cciss: use consis...
970
  	cciss_map_one(h->pdev, c, (unsigned char *) buf,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
971
  			bufsize, DMA_FROM_DEVICE); 
f70dba836   Stephen M. Cameron   cciss: use consis...
972
973
  	c->waiting = &wait;
  	enqueue_cmd_and_start_io(h, c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
974
975
976
  	wait_for_completion(&wait);
  
  	/* undo the dma mapping */
f70dba836   Stephen M. Cameron   cciss: use consis...
977
  	cciss_unmap_one(h->pdev, c, bufsize, DMA_FROM_DEVICE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
978
979
980
981
  	return(0);
  }
  
  static void 
b2a4a43db   Stephen M. Cameron   cciss: change pri...
982
  cciss_scsi_interpret_error(ctlr_info_t *h, CommandList_struct *c)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
983
984
  {
  	ErrorInfo_struct *ei;
f70dba836   Stephen M. Cameron   cciss: use consis...
985
  	ei = c->err_info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
986
987
988
  	switch(ei->CommandStatus)
  	{
  		case CMD_TARGET_STATUS:
b2a4a43db   Stephen M. Cameron   cciss: change pri...
989
990
991
992
993
994
995
  			dev_warn(&h->pdev->dev,
  				"cmd %p has completed with errors
  ", c);
  			dev_warn(&h->pdev->dev,
  				"cmd %p has SCSI Status = %x
  ",
  				c, ei->ScsiStatus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
996
  			if (ei->ScsiStatus == 0)
b2a4a43db   Stephen M. Cameron   cciss: change pri...
997
998
  				dev_warn(&h->pdev->dev,
  				"SCSI status is abnormally zero.  "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
999
1000
1001
1002
1003
1004
  				"(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. */
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1005
1006
  			dev_info(&h->pdev->dev, "UNDERRUN
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1007
1008
  		break;
  		case CMD_DATA_OVERRUN:
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1009
  			dev_warn(&h->pdev->dev, "%p has"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1010
  				" completed with data overrun "
f70dba836   Stephen M. Cameron   cciss: use consis...
1011
1012
  				"reported
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1013
1014
1015
1016
  		break;
  		case CMD_INVALID: {
  			/* controller unfortunately reports SCSI passthru's */
  			/* to non-existent targets as invalid commands. */
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1017
1018
  			dev_warn(&h->pdev->dev,
  				"%p is reported invalid (probably means "
f70dba836   Stephen M. Cameron   cciss: use consis...
1019
1020
1021
1022
  				"target device no longer present)
  ", c);
  			/* print_bytes((unsigned char *) c, sizeof(*c), 1, 0);
  			print_cmd(c);  */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1023
1024
1025
  			}
  		break;
  		case CMD_PROTOCOL_ERR:
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1026
1027
  			dev_warn(&h->pdev->dev, "%p has protocol error
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1028
1029
1030
  		break;
  		case CMD_HARDWARE_ERR:
  			/* cmd->result = DID_ERROR << 16; */
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1031
1032
  			dev_warn(&h->pdev->dev, "%p had hardware error
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1033
1034
  		break;
  		case CMD_CONNECTION_LOST:
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1035
1036
  			dev_warn(&h->pdev->dev, "%p had connection lost
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1037
1038
  		break;
  		case CMD_ABORTED:
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1039
1040
  			dev_warn(&h->pdev->dev, "%p was aborted
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1041
1042
  		break;
  		case CMD_ABORT_FAILED:
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1043
1044
1045
  			dev_warn(&h->pdev->dev,
  				"%p reports abort failed
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1046
1047
  		break;
  		case CMD_UNSOLICITED_ABORT:
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1048
  			dev_warn(&h->pdev->dev,
6d9a4f9e2   Stephen M. Cameron   cciss: fix missed...
1049
1050
  				"%p aborted due to an unsolicited abort
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1051
1052
  		break;
  		case CMD_TIMEOUT:
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1053
1054
  			dev_warn(&h->pdev->dev, "%p timedout
  ", c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1055
  		break;
6d9a4f9e2   Stephen M. Cameron   cciss: fix missed...
1056
1057
1058
1059
1060
  		case CMD_UNABORTABLE:
  			dev_warn(&h->pdev->dev,
  				"%p unabortable
  ", c);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1061
  		default:
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1062
1063
1064
1065
  			dev_warn(&h->pdev->dev,
  				"%p returned unknown status %x
  ",
  				c, ei->CommandStatus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1066
1067
1068
1069
  	}
  }
  
  static int
f70dba836   Stephen M. Cameron   cciss: use consis...
1070
  cciss_scsi_do_inquiry(ctlr_info_t *h, unsigned char *scsi3addr,
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1071
1072
  	unsigned char page, unsigned char *buf,
  	unsigned char bufsize)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1073
1074
  {
  	int rc;
f70dba836   Stephen M. Cameron   cciss: use consis...
1075
  	CommandList_struct *c;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1076
1077
1078
  	char cdb[6];
  	ErrorInfo_struct *ei;
  	unsigned long flags;
f70dba836   Stephen M. Cameron   cciss: use consis...
1079
1080
1081
  	spin_lock_irqsave(&h->lock, flags);
  	c = scsi_cmd_alloc(h);
  	spin_unlock_irqrestore(&h->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1082

f70dba836   Stephen M. Cameron   cciss: use consis...
1083
  	if (c == NULL) {			/* trouble... */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1084
1085
1086
1087
  		printk("cmd_alloc returned NULL!
  ");
  		return -1;
  	}
f70dba836   Stephen M. Cameron   cciss: use consis...
1088
  	ei = c->err_info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1089
1090
  
  	cdb[0] = CISS_INQUIRY;
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1091
1092
  	cdb[1] = (page != 0);
  	cdb[2] = page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1093
  	cdb[3] = 0;
47922d068   Mike Miller   [PATCH] cciss: On...
1094
  	cdb[4] = bufsize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1095
  	cdb[5] = 0;
f70dba836   Stephen M. Cameron   cciss: use consis...
1096
  	rc = cciss_scsi_do_simple_cmd(h, c, scsi3addr, cdb,
47922d068   Mike Miller   [PATCH] cciss: On...
1097
  				6, buf, bufsize, XFER_READ);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1098
1099
1100
1101
1102
  
  	if (rc != 0) return rc; /* something went wrong */
  
  	if (ei->CommandStatus != 0 && 
  	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1103
  		cciss_scsi_interpret_error(h, c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1104
1105
  		rc = -1;
  	}
f70dba836   Stephen M. Cameron   cciss: use consis...
1106
1107
1108
  	spin_lock_irqsave(&h->lock, flags);
  	scsi_cmd_free(h, c);
  	spin_unlock_irqrestore(&h->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1109
1110
  	return rc;	
  }
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1111
  /* Get the device id from inquiry page 0x83 */
f70dba836   Stephen M. Cameron   cciss: use consis...
1112
  static int cciss_scsi_get_device_id(ctlr_info_t *h, unsigned char *scsi3addr,
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
  	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;
f70dba836   Stephen M. Cameron   cciss: use consis...
1123
  	rc = cciss_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1124
1125
1126
1127
1128
  	if (rc == 0)
  		memcpy(device_id, &buf[8], buflen);
  	kfree(buf);
  	return rc != 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1129
  static int
f70dba836   Stephen M. Cameron   cciss: use consis...
1130
  cciss_scsi_do_report_phys_luns(ctlr_info_t *h,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1131
1132
1133
  		ReportLunData_struct *buf, int bufsize)
  {
  	int rc;
f70dba836   Stephen M. Cameron   cciss: use consis...
1134
  	CommandList_struct *c;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1135
1136
1137
1138
  	unsigned char cdb[12];
  	unsigned char scsi3addr[8]; 
  	ErrorInfo_struct *ei;
  	unsigned long flags;
f70dba836   Stephen M. Cameron   cciss: use consis...
1139
1140
1141
1142
  	spin_lock_irqsave(&h->lock, flags);
  	c = scsi_cmd_alloc(h);
  	spin_unlock_irqrestore(&h->lock, flags);
  	if (c == NULL) {			/* trouble... */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
  		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;
f70dba836   Stephen M. Cameron   cciss: use consis...
1161
  	rc = cciss_scsi_do_simple_cmd(h, c, scsi3addr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1162
1163
1164
1165
1166
  				cdb, 12, 
  				(unsigned char *) buf, 
  				bufsize, XFER_READ);
  
  	if (rc != 0) return rc; /* something went wrong */
f70dba836   Stephen M. Cameron   cciss: use consis...
1167
  	ei = c->err_info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1168
1169
  	if (ei->CommandStatus != 0 && 
  	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1170
  		cciss_scsi_interpret_error(h, c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1171
1172
  		rc = -1;
  	}
f70dba836   Stephen M. Cameron   cciss: use consis...
1173
1174
1175
  	spin_lock_irqsave(&h->lock, flags);
  	scsi_cmd_free(h, c);
  	spin_unlock_irqrestore(&h->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1176
1177
1178
1179
  	return rc;	
  }
  
  static void
f70dba836   Stephen M. Cameron   cciss: use consis...
1180
  cciss_update_non_disk_devices(ctlr_info_t *h, int hostno)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  {
  	/* 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...
1208
1209
  #define OBDR_TAPE_INQ_SIZE 49
  #define OBDR_TAPE_SIG "$DR-10"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1210
  	ReportLunData_struct *ld_buff;
47922d068   Mike Miller   [PATCH] cciss: On...
1211
  	unsigned char *inq_buff;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1212
  	unsigned char scsi3addr[8];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1213
1214
  	__u32 num_luns=0;
  	unsigned char *ch;
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1215
  	struct cciss_scsi_dev_t *currentsd, *this_device;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1216
1217
1218
  	int ncurrent=0;
  	int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
  	int i;
06ff37ffb   Eric Sesterhenn   [PATCH] kzalloc()...
1219
  	ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
47922d068   Mike Miller   [PATCH] cciss: On...
1220
  	inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1221
1222
1223
1224
1225
1226
  	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
1227
  	}
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1228
  	this_device = &currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
f70dba836   Stephen M. Cameron   cciss: use consis...
1229
  	if (cciss_scsi_do_report_phys_luns(h, ld_buff, reportlunsize) == 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
  		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 ...
1249
  	for (i = 0; i < num_luns; i++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1250
1251
  		/* for each physical lun, do an inquiry */
  		if (ld_buff->LUN[i][3] & 0xC0) continue;
47922d068   Mike Miller   [PATCH] cciss: On...
1252
  		memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1253
  		memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
f70dba836   Stephen M. Cameron   cciss: use consis...
1254
  		if (cciss_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1255
  			(unsigned char) OBDR_TAPE_INQ_SIZE) != 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1256
  			/* Inquiry failed (msg printed already) */
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
  			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));
f70dba836   Stephen M. Cameron   cciss: use consis...
1272
  		cciss_scsi_get_device_id(h, scsi3addr,
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1273
1274
1275
  			this_device->device_id, sizeof(this_device->device_id));
  
  		switch (this_device->devtype)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1276
  		{
47922d068   Mike Miller   [PATCH] cciss: On...
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
  		  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
1295
1296
1297
1298
  		  case 0x01: /* sequential access, (tape) */
  		  case 0x08: /* medium changer */
  			if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
  				printk(KERN_INFO "cciss%d: %s ignored, "
f70dba836   Stephen M. Cameron   cciss: use consis...
1299
1300
  					"too many devices.
  ", h->ctlr,
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1301
  					scsi_device_type(this_device->devtype));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1302
1303
  				break;
  			}
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1304
  			currentsd[ncurrent] = *this_device;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1305
1306
1307
1308
1309
1310
  			ncurrent++;
  			break;
  		  default: 
  			break;
  		}
  	}
f70dba836   Stephen M. Cameron   cciss: use consis...
1311
  	adjust_cciss_scsi_table(h, hostno, currentsd, ncurrent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1312
1313
1314
  out:
  	kfree(inq_buff);
  	kfree(ld_buff);
905bd78f2   scameron@beardog.cca.cpqcorp.net   cciss: Fix cciss ...
1315
  	kfree(currentsd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
  	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
f70dba836   Stephen M. Cameron   cciss: use consis...
1330
  cciss_scsi_user_command(ctlr_info_t *h, int hostno, char *buffer, int length)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1331
1332
1333
1334
  {
  	int arg_len;
  
  	if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
f70dba836   Stephen M. Cameron   cciss: use consis...
1335
  		cciss_update_non_disk_devices(h, hostno);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
  	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;
f70dba836   Stephen M. Cameron   cciss: use consis...
1352
  	ctlr_info_t *h;
b9f0bd089   Mike Miller   [PATCH] cciss: SC...
1353
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1354

f70dba836   Stephen M. Cameron   cciss: use consis...
1355
1356
  	h = (ctlr_info_t *) sh->hostdata[0];
  	if (h == NULL)  /* This really shouldn't ever happen. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1357
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1358
  	if (func == 0) {	/* User is reading from /proc/scsi/ciss*?/?*  */
b9f0bd089   Mike Miller   [PATCH] cciss: SC...
1359
1360
  		buflen = sprintf(buffer, "cciss%d: SCSI host: %d
  ",
f70dba836   Stephen M. Cameron   cciss: use consis...
1361
  				h->ctlr, sh->host_no);
b9f0bd089   Mike Miller   [PATCH] cciss: SC...
1362
1363
1364
1365
1366
1367
1368
1369
  
  		/* 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. */
f70dba836   Stephen M. Cameron   cciss: use consis...
1370
1371
1372
  		for (i = 0; i < ccissscsi[h->ctlr].ndevices; i++) {
  			struct cciss_scsi_dev_t *sd =
  				&ccissscsi[h->ctlr].dev[i];
b9f0bd089   Mike Miller   [PATCH] cciss: SC...
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
  			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
1383
1384
1385
1386
1387
1388
1389
1390
  		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*?/?*  ... */
f70dba836   Stephen M. Cameron   cciss: use consis...
1391
  		return cciss_scsi_user_command(h, sh->host_no,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1392
1393
1394
1395
1396
  			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 
f70dba836   Stephen M. Cameron   cciss: use consis...
1397
     cciss command, c. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1398

f70dba836   Stephen M. Cameron   cciss: use consis...
1399
  static void cciss_scatter_gather(ctlr_info_t *h, CommandList_struct *c,
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
1400
  	struct scsi_cmnd *cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1401
  {
41ce639a1   FUJITA Tomonori   [SCSI] cciss: con...
1402
1403
  	unsigned int len;
  	struct scatterlist *sg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1404
  	__u64 addr64;
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
1405
1406
1407
1408
1409
1410
1411
1412
  	int request_nsgs, i, chained, sg_index;
  	struct cciss_scsi_adapter_data_t *sa = h->scsi_ctlr;
  	SGDescriptor_struct *curr_sg;
  
  	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
  
  	chained = 0;
  	sg_index = 0;
f70dba836   Stephen M. Cameron   cciss: use consis...
1413
  	curr_sg = c->SG;
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
1414
1415
1416
1417
1418
1419
1420
  	request_nsgs = scsi_dma_map(cmd);
  	if (request_nsgs) {
  		scsi_for_each_sg(cmd, sg, request_nsgs, i) {
  			if (sg_index + 1 == h->max_cmd_sgentries &&
  				!chained && request_nsgs - i > 1) {
  				chained = 1;
  				sg_index = 0;
f70dba836   Stephen M. Cameron   cciss: use consis...
1421
  				curr_sg = sa->cmd_sg_list[c->cmdindex];
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
1422
  			}
41ce639a1   FUJITA Tomonori   [SCSI] cciss: con...
1423
1424
  			addr64 = (__u64) sg_dma_address(sg);
  			len  = sg_dma_len(sg);
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
1425
1426
1427
1428
1429
1430
1431
  			curr_sg[sg_index].Addr.lower =
  				(__u32) (addr64 & 0x0FFFFFFFFULL);
  			curr_sg[sg_index].Addr.upper =
  				(__u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
  			curr_sg[sg_index].Len = len;
  			curr_sg[sg_index].Ext = 0;
  			++sg_index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1432
  		}
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
1433
  		if (chained)
f70dba836   Stephen M. Cameron   cciss: use consis...
1434
1435
  			cciss_map_sg_chain_block(h, c,
  				sa->cmd_sg_list[c->cmdindex],
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
1436
1437
  				(request_nsgs - (h->max_cmd_sgentries - 1)) *
  					sizeof(SGDescriptor_struct));
41ce639a1   FUJITA Tomonori   [SCSI] cciss: con...
1438
  	}
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
1439
1440
1441
  	/* track how many SG entries we are using */
  	if (request_nsgs > h->maxSG)
  		h->maxSG = request_nsgs;
f70dba836   Stephen M. Cameron   cciss: use consis...
1442
  	c->Header.SGTotal = (__u8) request_nsgs + chained;
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
1443
  	if (request_nsgs > h->max_cmd_sgentries)
f70dba836   Stephen M. Cameron   cciss: use consis...
1444
  		c->Header.SGList = h->max_cmd_sgentries;
87c3a922a   Stephen M. Cameron   cciss: Fix proble...
1445
  	else
f70dba836   Stephen M. Cameron   cciss: use consis...
1446
  		c->Header.SGList = c->Header.SGTotal;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1447
1448
1449
1450
1451
  	return;
  }
  
  
  static int
f281233d3   Jeff Garzik   SCSI host lock pu...
1452
  cciss_scsi_queue_command_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1453
  {
f70dba836   Stephen M. Cameron   cciss: use consis...
1454
1455
  	ctlr_info_t *h;
  	int rc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1456
  	unsigned char scsi3addr[8];
f70dba836   Stephen M. Cameron   cciss: use consis...
1457
  	CommandList_struct *c;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1458
1459
1460
1461
  	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?)
f70dba836   Stephen M. Cameron   cciss: use consis...
1462
  	h = (ctlr_info_t *) cmd->device->host->hostdata[0];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1463

f70dba836   Stephen M. Cameron   cciss: use consis...
1464
  	rc = lookup_scsi3addr(h, cmd->device->channel, cmd->device->id,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
  			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;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1475
1476
  	/* Ok, we have a reasonable scsi nexus, so send the cmd down, and
             see what the device thinks of it. */
f70dba836   Stephen M. Cameron   cciss: use consis...
1477
1478
1479
1480
  	spin_lock_irqsave(&h->lock, flags);
  	c = scsi_cmd_alloc(h);
  	spin_unlock_irqrestore(&h->lock, flags);
  	if (c == NULL) {			/* trouble... */
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1481
1482
  		dev_warn(&h->pdev->dev, "scsi_cmd_alloc returned NULL!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1483
1484
1485
1486
1487
1488
1489
1490
1491
  		/* 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 
f70dba836   Stephen M. Cameron   cciss: use consis...
1492
1493
  	/* save c in case we have to abort it */
  	cmd->host_scribble = (unsigned char *) c;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1494

f70dba836   Stephen M. Cameron   cciss: use consis...
1495
1496
1497
1498
1499
  	c->cmd_type = CMD_SCSI;
  	c->scsi_cmd = cmd;
  	c->Header.ReplyQueue = 0;  /* unused in simple mode */
  	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
  	c->Header.Tag.lower = c->busaddr;  /* Use k. address of cmd as tag */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1500
1501
  	
  	// Fill in the request block...
f70dba836   Stephen M. Cameron   cciss: use consis...
1502
1503
1504
1505
1506
1507
1508
  	c->Request.Timeout = 0;
  	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
  	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
  	c->Request.CDBLen = cmd->cmd_len;
  	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
  	c->Request.Type.Type = TYPE_CMD;
  	c->Request.Type.Attribute = ATTR_SIMPLE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1509
1510
  	switch(cmd->sc_data_direction)
  	{
f70dba836   Stephen M. Cameron   cciss: use consis...
1511
1512
1513
1514
1515
1516
1517
1518
1519
  	  case DMA_TO_DEVICE:
  		c->Request.Type.Direction = XFER_WRITE;
  		break;
  	  case DMA_FROM_DEVICE:
  		c->Request.Type.Direction = XFER_READ;
  		break;
  	  case DMA_NONE:
  		c->Request.Type.Direction = XFER_NONE;
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1520
1521
1522
1523
  	  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() )
f70dba836   Stephen M. Cameron   cciss: use consis...
1524
  		c->Request.Type.Direction = XFER_RSVD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
  		// 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: 
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1535
1536
  		dev_warn(&h->pdev->dev, "unknown data direction: %d
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1537
1538
1539
1540
  			cmd->sc_data_direction);
  		BUG();
  		break;
  	}
f70dba836   Stephen M. Cameron   cciss: use consis...
1541
1542
  	cciss_scatter_gather(h, c, cmd);
  	enqueue_cmd_and_start_io(h, c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1543
1544
1545
  	/* the cmd'll come back via intr handler in complete_scsi_command()  */
  	return 0;
  }
f281233d3   Jeff Garzik   SCSI host lock pu...
1546
  static DEF_SCSI_QCMD(cciss_scsi_queue_command)
f70dba836   Stephen M. Cameron   cciss: use consis...
1547
  static void cciss_unregister_scsi(ctlr_info_t *h)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1548
1549
1550
1551
1552
1553
  {
  	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. */
f70dba836   Stephen M. Cameron   cciss: use consis...
1554
1555
  	spin_lock_irqsave(&h->lock, flags);
  	sa = h->scsi_ctlr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1556
1557
1558
1559
  	stk = &sa->cmd_stack; 
  
  	/* if we weren't ever actually registered, don't unregister */ 
  	if (sa->registered) {
f70dba836   Stephen M. Cameron   cciss: use consis...
1560
  		spin_unlock_irqrestore(&h->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1561
1562
  		scsi_remove_host(sa->scsi_host);
  		scsi_host_put(sa->scsi_host);
f70dba836   Stephen M. Cameron   cciss: use consis...
1563
  		spin_lock_irqsave(&h->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1564
1565
1566
1567
1568
  	}
  
  	/* set scsi_host to NULL so our detect routine will 
  	   find us on register */
  	sa->scsi_host = NULL;
f70dba836   Stephen M. Cameron   cciss: use consis...
1569
1570
  	spin_unlock_irqrestore(&h->lock, flags);
  	scsi_cmd_stack_free(h);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1571
  	kfree(sa);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1572
  }
f70dba836   Stephen M. Cameron   cciss: use consis...
1573
  static int cciss_engage_scsi(ctlr_info_t *h)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1574
1575
1576
1577
  {
  	struct cciss_scsi_adapter_data_t *sa;
  	struct cciss_scsi_cmd_stack_t *stk;
  	unsigned long flags;
f70dba836   Stephen M. Cameron   cciss: use consis...
1578
1579
  	spin_lock_irqsave(&h->lock, flags);
  	sa = h->scsi_ctlr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1580
  	stk = &sa->cmd_stack; 
f4a93bcda   Mike Miller   cciss: change the...
1581
  	if (sa->registered) {
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1582
1583
  		dev_info(&h->pdev->dev, "SCSI subsystem already engaged.
  ");
f70dba836   Stephen M. Cameron   cciss: use consis...
1584
  		spin_unlock_irqrestore(&h->lock, flags);
8721c81f6   Stephen M. Cameron   cciss: Fix weird ...
1585
  		return -ENXIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1586
  	}
f4a93bcda   Mike Miller   cciss: change the...
1587
  	sa->registered = 1;
f70dba836   Stephen M. Cameron   cciss: use consis...
1588
1589
1590
  	spin_unlock_irqrestore(&h->lock, flags);
  	cciss_update_non_disk_devices(h, -1);
  	cciss_scsi_detect(h);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1591
1592
1593
1594
  	return 0;
  }
  
  static void
f70dba836   Stephen M. Cameron   cciss: use consis...
1595
  cciss_seq_tape_report(struct seq_file *seq, ctlr_info_t *h)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1596
1597
  {
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1598

f70dba836   Stephen M. Cameron   cciss: use consis...
1599
  	CPQ_TAPE_LOCK(h, flags);
89b6e7437   Mike Miller   resubmit: cciss: ...
1600
  	seq_printf(seq,
b9f0bd089   Mike Miller   [PATCH] cciss: SC...
1601
1602
1603
  		"Sequential access devices: %d
  
  ",
f70dba836   Stephen M. Cameron   cciss: use consis...
1604
1605
  			ccissscsi[h->ctlr].ndevices);
  	CPQ_TAPE_UNLOCK(h, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1606
  }
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1607
1608
1609
1610
1611
1612
1613
  static int wait_for_device_to_become_ready(ctlr_info_t *h,
  	unsigned char lunaddr[])
  {
  	int rc;
  	int count = 0;
  	int waittime = HZ;
  	CommandList_struct *c;
6b4d96b87   Stephen M. Cameron   cciss: separate c...
1614
  	c = cmd_alloc(h);
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1615
  	if (!c) {
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1616
1617
1618
  		dev_warn(&h->pdev->dev, "out of memory in "
  			"wait_for_device_to_become_ready.
  ");
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1619
1620
1621
1622
1623
1624
1625
1626
1627
  		return IO_ERROR;
  	}
  
  	/* Send test unit ready until device ready, or give up. */
  	while (count < 20) {
  
  		/* Wait for a bit.  do this first, because if we send
  		 * the TUR right away, the reset will just abort it.
  		 */
40df6ae42   scameron@beardog.cca.cpqcorp.net   cciss: Use schedu...
1628
  		schedule_timeout_uninterruptible(waittime);
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1629
1630
1631
1632
1633
1634
1635
  		count++;
  
  		/* Increase wait time with each try, up to a point. */
  		if (waittime < (HZ * 30))
  			waittime = waittime * 2;
  
  		/* Send the Test Unit Ready */
f70dba836   Stephen M. Cameron   cciss: use consis...
1636
  		rc = fill_cmd(h, c, TEST_UNIT_READY, NULL, 0, 0,
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1637
  			lunaddr, TYPE_CMD);
85cc61ae4   scameron@beardog.cca.cpqcorp.net   cciss: change SCS...
1638
1639
1640
1641
  		if (rc == 0)
  			rc = sendcmd_withirq_core(h, c, 0);
  
  		(void) process_sendcmd_error(h, c);
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1642

3969251b8   scameron@beardog.cca.cpqcorp.net   cciss: decode uni...
1643
1644
  		if (rc != 0)
  			goto retry_tur;
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1645

3969251b8   scameron@beardog.cca.cpqcorp.net   cciss: decode uni...
1646
  		if (c->err_info->CommandStatus == CMD_SUCCESS)
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1647
  			break;
3969251b8   scameron@beardog.cca.cpqcorp.net   cciss: decode uni...
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
  		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
  			if (c->err_info->SenseInfo[2] == NO_SENSE)
  				break;
  			if (c->err_info->SenseInfo[2] == UNIT_ATTENTION) {
  				unsigned char asc;
  				asc = c->err_info->SenseInfo[12];
  				check_for_unit_attention(h, c);
  				if (asc == POWER_OR_RESET)
  					break;
  			}
  		}
  retry_tur:
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1661
  		dev_warn(&h->pdev->dev, "Waiting %d secs "
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1662
1663
  			"for device to become ready.
  ",
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1664
  			waittime / HZ);
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1665
1666
1667
1668
  		rc = 1; /* device not ready. */
  	}
  
  	if (rc)
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1669
1670
  		dev_warn(&h->pdev->dev, "giving up on device.
  ");
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1671
  	else
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1672
1673
  		dev_warn(&h->pdev->dev, "device is ready.
  ");
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1674

6b4d96b87   Stephen M. Cameron   cciss: separate c...
1675
  	cmd_free(h, c);
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1676
1677
  	return rc;
  }
89b6e7437   Mike Miller   resubmit: cciss: ...
1678

3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
  /* 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;
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1694
  	unsigned char lunaddr[8];
f70dba836   Stephen M. Cameron   cciss: use consis...
1695
  	ctlr_info_t *h;
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1696
1697
  
  	/* find the controller to which the command to be aborted was sent */
f70dba836   Stephen M. Cameron   cciss: use consis...
1698
1699
  	h = (ctlr_info_t *) scsicmd->device->host->hostdata[0];
  	if (h == NULL) /* paranoia */
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1700
  		return FAILED;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1701
1702
  	dev_warn(&h->pdev->dev, "resetting tape drive or medium changer.
  ");
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1703
1704
  	/* find the command that's giving us trouble */
  	cmd_in_trouble = (CommandList_struct *) scsicmd->host_scribble;
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1705
  	if (cmd_in_trouble == NULL) /* paranoia */
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1706
  		return FAILED;
88f627ae3   Stephen M. Cameron   cciss: fix SCSI d...
1707
  	memcpy(lunaddr, &cmd_in_trouble->Header.LUN.LunAddrBytes[0], 8);
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1708
  	/* send a reset to the SCSI LUN which the command was sent to */
f70dba836   Stephen M. Cameron   cciss: use consis...
1709
  	rc = sendcmd_withirq(h, CCISS_RESET_MSG, NULL, 0, 0, lunaddr,
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1710
  		TYPE_MSG);
f70dba836   Stephen M. Cameron   cciss: use consis...
1711
  	if (rc == 0 && wait_for_device_to_become_ready(h, lunaddr) == 0)
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1712
  		return SUCCESS;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1713
1714
  	dev_warn(&h->pdev->dev, "resetting device failed.
  ");
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1715
1716
1717
1718
1719
1720
1721
  	return FAILED;
  }
  
  static int  cciss_eh_abort_handler(struct scsi_cmnd *scsicmd)
  {
  	int rc;
  	CommandList_struct *cmd_to_abort;
85cc61ae4   scameron@beardog.cca.cpqcorp.net   cciss: change SCS...
1722
  	unsigned char lunaddr[8];
f70dba836   Stephen M. Cameron   cciss: use consis...
1723
  	ctlr_info_t *h;
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1724
1725
  
  	/* find the controller to which the command to be aborted was sent */
f70dba836   Stephen M. Cameron   cciss: use consis...
1726
1727
  	h = (ctlr_info_t *) scsicmd->device->host->hostdata[0];
  	if (h == NULL) /* paranoia */
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1728
  		return FAILED;
b2a4a43db   Stephen M. Cameron   cciss: change pri...
1729
1730
  	dev_warn(&h->pdev->dev, "aborting tardy SCSI cmd
  ");
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1731
1732
1733
1734
1735
  
  	/* find the command to be aborted */
  	cmd_to_abort = (CommandList_struct *) scsicmd->host_scribble;
  	if (cmd_to_abort == NULL) /* paranoia */
  		return FAILED;
85cc61ae4   scameron@beardog.cca.cpqcorp.net   cciss: change SCS...
1736
  	memcpy(lunaddr, &cmd_to_abort->Header.LUN.LunAddrBytes[0], 8);
f70dba836   Stephen M. Cameron   cciss: use consis...
1737
  	rc = sendcmd_withirq(h, CCISS_ABORT_MSG, &cmd_to_abort->Header.Tag,
85cc61ae4   scameron@beardog.cca.cpqcorp.net   cciss: change SCS...
1738
  		0, 0, lunaddr, TYPE_MSG);
3da8b713d   mike.miller@hp.com   [SCSI] cciss: scs...
1739
1740
1741
1742
1743
  	if (rc == 0)
  		return SUCCESS;
  	return FAILED;
  
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1744
1745
1746
1747
1748
  #else /* no CONFIG_CISS_SCSI_TAPE */
  
  /* If no tape support, then these become defined out of existence */
  
  #define cciss_scsi_setup(cntl_num)
0007a4c90   Stephen M. Cameron   cciss: auto engag...
1749
  #define cciss_engage_scsi(h)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1750
1751
  
  #endif /* CONFIG_CISS_SCSI_TAPE */