cciss: detect bad alignment of scsi commands at build time
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / block / cciss_scsi.c
1 /*
2 * Disk Array driver for HP Smart Array controllers, SCSI Tape module.
3 * (C) Copyright 2001, 2007 Hewlett-Packard Development Company, L.P.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 300, Boston, MA
17 * 02111-1307, USA.
18 *
19 * Questions/Comments/Bugfixes to iss_storagedev@hp.com
20 *
21 * Author: Stephen M. Cameron
22 */
23 #ifdef CONFIG_CISS_SCSI_TAPE
24
25 /* Here we have code to present the driver as a scsi driver
26 as it is simultaneously presented as a block driver. The
27 reason for doing this is to allow access to SCSI tape drives
28 through the array controller. Note in particular, neither
29 physical nor logical disks are presented through the scsi layer. */
30
31 #include <linux/timer.h>
32 #include <linux/completion.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35
36 #include <asm/atomic.h>
37
38 #include <scsi/scsi_cmnd.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_host.h>
41
42 #include "cciss_scsi.h"
43
44 #define CCISS_ABORT_MSG 0x00
45 #define CCISS_RESET_MSG 0x01
46
47 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
48 size_t size,
49 __u8 page_code, unsigned char *scsi3addr,
50 int cmd_type);
51
52 static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool);
53 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool);
54
55 static int cciss_scsi_proc_info(
56 struct Scsi_Host *sh,
57 char *buffer, /* data buffer */
58 char **start, /* where data in buffer starts */
59 off_t offset, /* offset from start of imaginary file */
60 int length, /* length of data in buffer */
61 int func); /* 0 == read, 1 == write */
62
63 static int cciss_scsi_queue_command (struct scsi_cmnd *cmd,
64 void (* done)(struct scsi_cmnd *));
65 static int cciss_eh_device_reset_handler(struct scsi_cmnd *);
66 static int cciss_eh_abort_handler(struct scsi_cmnd *);
67
68 static struct cciss_scsi_hba_t ccissscsi[MAX_CTLR] = {
69 { .name = "cciss0", .ndevices = 0 },
70 { .name = "cciss1", .ndevices = 0 },
71 { .name = "cciss2", .ndevices = 0 },
72 { .name = "cciss3", .ndevices = 0 },
73 { .name = "cciss4", .ndevices = 0 },
74 { .name = "cciss5", .ndevices = 0 },
75 { .name = "cciss6", .ndevices = 0 },
76 { .name = "cciss7", .ndevices = 0 },
77 };
78
79 static struct scsi_host_template cciss_driver_template = {
80 .module = THIS_MODULE,
81 .name = "cciss",
82 .proc_name = "cciss",
83 .proc_info = cciss_scsi_proc_info,
84 .queuecommand = cciss_scsi_queue_command,
85 .can_queue = SCSI_CCISS_CAN_QUEUE,
86 .this_id = 7,
87 .sg_tablesize = MAXSGENTRIES,
88 .cmd_per_lun = 1,
89 .use_clustering = DISABLE_CLUSTERING,
90 /* Can't have eh_bus_reset_handler or eh_host_reset_handler for cciss */
91 .eh_device_reset_handler= cciss_eh_device_reset_handler,
92 .eh_abort_handler = cciss_eh_abort_handler,
93 };
94
95 #pragma pack(1)
96
97 #define SCSI_PAD_32 4
98 #define SCSI_PAD_64 4
99
100 struct cciss_scsi_cmd_stack_elem_t {
101 CommandList_struct cmd;
102 ErrorInfo_struct Err;
103 __u32 busaddr;
104 u8 pad[IS_32_BIT * SCSI_PAD_32 + IS_64_BIT * SCSI_PAD_64];
105 };
106
107 #pragma pack()
108
109 #define CMD_STACK_SIZE (SCSI_CCISS_CAN_QUEUE * \
110 CCISS_MAX_SCSI_DEVS_PER_HBA + 2)
111 // plus two for init time usage
112
113 #pragma pack(1)
114 struct cciss_scsi_cmd_stack_t {
115 struct cciss_scsi_cmd_stack_elem_t *pool;
116 struct cciss_scsi_cmd_stack_elem_t *elem[CMD_STACK_SIZE];
117 dma_addr_t cmd_pool_handle;
118 int top;
119 };
120 #pragma pack()
121
122 struct cciss_scsi_adapter_data_t {
123 struct Scsi_Host *scsi_host;
124 struct cciss_scsi_cmd_stack_t cmd_stack;
125 int registered;
126 spinlock_t lock; // to protect ccissscsi[ctlr];
127 };
128
129 #define CPQ_TAPE_LOCK(ctlr, flags) spin_lock_irqsave( \
130 &(((struct cciss_scsi_adapter_data_t *) \
131 hba[ctlr]->scsi_ctlr)->lock), flags);
132 #define CPQ_TAPE_UNLOCK(ctlr, flags) spin_unlock_irqrestore( \
133 &(((struct cciss_scsi_adapter_data_t *) \
134 hba[ctlr]->scsi_ctlr)->lock), flags);
135
136 static CommandList_struct *
137 scsi_cmd_alloc(ctlr_info_t *h)
138 {
139 /* assume only one process in here at a time, locking done by caller. */
140 /* use CCISS_LOCK(ctlr) */
141 /* might be better to rewrite how we allocate scsi commands in a way that */
142 /* needs no locking at all. */
143
144 /* take the top memory chunk off the stack and return it, if any. */
145 struct cciss_scsi_cmd_stack_elem_t *c;
146 struct cciss_scsi_adapter_data_t *sa;
147 struct cciss_scsi_cmd_stack_t *stk;
148 u64bit temp64;
149
150 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
151 stk = &sa->cmd_stack;
152
153 if (stk->top < 0)
154 return NULL;
155 c = stk->elem[stk->top];
156 /* memset(c, 0, sizeof(*c)); */
157 memset(&c->cmd, 0, sizeof(c->cmd));
158 memset(&c->Err, 0, sizeof(c->Err));
159 /* set physical addr of cmd and addr of scsi parameters */
160 c->cmd.busaddr = c->busaddr;
161 /* (__u32) (stk->cmd_pool_handle +
162 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top)); */
163
164 temp64.val = (__u64) (c->busaddr + sizeof(CommandList_struct));
165 /* (__u64) (stk->cmd_pool_handle +
166 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top) +
167 sizeof(CommandList_struct)); */
168 stk->top--;
169 c->cmd.ErrDesc.Addr.lower = temp64.val32.lower;
170 c->cmd.ErrDesc.Addr.upper = temp64.val32.upper;
171 c->cmd.ErrDesc.Len = sizeof(ErrorInfo_struct);
172
173 c->cmd.ctlr = h->ctlr;
174 c->cmd.err_info = &c->Err;
175
176 return (CommandList_struct *) c;
177 }
178
179 static void
180 scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd)
181 {
182 /* assume only one process in here at a time, locking done by caller. */
183 /* use CCISS_LOCK(ctlr) */
184 /* drop the free memory chunk on top of the stack. */
185
186 struct cciss_scsi_adapter_data_t *sa;
187 struct cciss_scsi_cmd_stack_t *stk;
188
189 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
190 stk = &sa->cmd_stack;
191 if (stk->top >= CMD_STACK_SIZE) {
192 printk("cciss: scsi_cmd_free called too many times.\n");
193 BUG();
194 }
195 stk->top++;
196 stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd;
197 }
198
199 static int
200 scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa)
201 {
202 int i;
203 struct cciss_scsi_cmd_stack_t *stk;
204 size_t size;
205
206 stk = &sa->cmd_stack;
207 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
208
209 /* Check alignment, see cciss_cmd.h near CommandList_struct def. */
210 BUILD_BUG_ON((sizeof(*stk->pool) % COMMANDLIST_ALIGNMENT) != 0);
211 /* pci_alloc_consistent guarantees 32-bit DMA address will be used */
212 stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)
213 pci_alloc_consistent(hba[ctlr]->pdev, size, &stk->cmd_pool_handle);
214
215 if (stk->pool == NULL) {
216 printk("stk->pool is null\n");
217 return -1;
218 }
219
220 for (i=0; i<CMD_STACK_SIZE; i++) {
221 stk->elem[i] = &stk->pool[i];
222 stk->elem[i]->busaddr = (__u32) (stk->cmd_pool_handle +
223 (sizeof(struct cciss_scsi_cmd_stack_elem_t) * i));
224 }
225 stk->top = CMD_STACK_SIZE-1;
226 return 0;
227 }
228
229 static void
230 scsi_cmd_stack_free(int ctlr)
231 {
232 struct cciss_scsi_adapter_data_t *sa;
233 struct cciss_scsi_cmd_stack_t *stk;
234 size_t size;
235
236 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
237 stk = &sa->cmd_stack;
238 if (stk->top != CMD_STACK_SIZE-1) {
239 printk( "cciss: %d scsi commands are still outstanding.\n",
240 CMD_STACK_SIZE - stk->top);
241 // BUG();
242 printk("WE HAVE A BUG HERE!!! stk=0x%p\n", stk);
243 }
244 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
245
246 pci_free_consistent(hba[ctlr]->pdev, size, stk->pool, stk->cmd_pool_handle);
247 stk->pool = NULL;
248 }
249
250 #if 0
251 static int xmargin=8;
252 static int amargin=60;
253
254 static void
255 print_bytes (unsigned char *c, int len, int hex, int ascii)
256 {
257
258 int i;
259 unsigned char *x;
260
261 if (hex)
262 {
263 x = c;
264 for (i=0;i<len;i++)
265 {
266 if ((i % xmargin) == 0 && i>0) printk("\n");
267 if ((i % xmargin) == 0) printk("0x%04x:", i);
268 printk(" %02x", *x);
269 x++;
270 }
271 printk("\n");
272 }
273 if (ascii)
274 {
275 x = c;
276 for (i=0;i<len;i++)
277 {
278 if ((i % amargin) == 0 && i>0) printk("\n");
279 if ((i % amargin) == 0) printk("0x%04x:", i);
280 if (*x > 26 && *x < 128) printk("%c", *x);
281 else printk(".");
282 x++;
283 }
284 printk("\n");
285 }
286 }
287
288 static void
289 print_cmd(CommandList_struct *cp)
290 {
291 printk("queue:%d\n", cp->Header.ReplyQueue);
292 printk("sglist:%d\n", cp->Header.SGList);
293 printk("sgtot:%d\n", cp->Header.SGTotal);
294 printk("Tag:0x%08x/0x%08x\n", cp->Header.Tag.upper,
295 cp->Header.Tag.lower);
296 printk("LUN:0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
297 cp->Header.LUN.LunAddrBytes[0],
298 cp->Header.LUN.LunAddrBytes[1],
299 cp->Header.LUN.LunAddrBytes[2],
300 cp->Header.LUN.LunAddrBytes[3],
301 cp->Header.LUN.LunAddrBytes[4],
302 cp->Header.LUN.LunAddrBytes[5],
303 cp->Header.LUN.LunAddrBytes[6],
304 cp->Header.LUN.LunAddrBytes[7]);
305 printk("CDBLen:%d\n", cp->Request.CDBLen);
306 printk("Type:%d\n",cp->Request.Type.Type);
307 printk("Attr:%d\n",cp->Request.Type.Attribute);
308 printk(" Dir:%d\n",cp->Request.Type.Direction);
309 printk("Timeout:%d\n",cp->Request.Timeout);
310 printk( "CDB: %02x %02x %02x %02x %02x %02x %02x %02x"
311 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
312 cp->Request.CDB[0], cp->Request.CDB[1],
313 cp->Request.CDB[2], cp->Request.CDB[3],
314 cp->Request.CDB[4], cp->Request.CDB[5],
315 cp->Request.CDB[6], cp->Request.CDB[7],
316 cp->Request.CDB[8], cp->Request.CDB[9],
317 cp->Request.CDB[10], cp->Request.CDB[11],
318 cp->Request.CDB[12], cp->Request.CDB[13],
319 cp->Request.CDB[14], cp->Request.CDB[15]),
320 printk("edesc.Addr: 0x%08x/0%08x, Len = %d\n",
321 cp->ErrDesc.Addr.upper, cp->ErrDesc.Addr.lower,
322 cp->ErrDesc.Len);
323 printk("sgs..........Errorinfo:\n");
324 printk("scsistatus:%d\n", cp->err_info->ScsiStatus);
325 printk("senselen:%d\n", cp->err_info->SenseLen);
326 printk("cmd status:%d\n", cp->err_info->CommandStatus);
327 printk("resid cnt:%d\n", cp->err_info->ResidualCnt);
328 printk("offense size:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_size);
329 printk("offense byte:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_num);
330 printk("offense value:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
331
332 }
333
334 #endif
335
336 static int
337 find_bus_target_lun(int ctlr, int *bus, int *target, int *lun)
338 {
339 /* finds an unused bus, target, lun for a new device */
340 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
341 int i, found=0;
342 unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA];
343
344 memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA);
345
346 target_taken[SELF_SCSI_ID] = 1;
347 for (i=0;i<ccissscsi[ctlr].ndevices;i++)
348 target_taken[ccissscsi[ctlr].dev[i].target] = 1;
349
350 for (i=0;i<CCISS_MAX_SCSI_DEVS_PER_HBA;i++) {
351 if (!target_taken[i]) {
352 *bus = 0; *target=i; *lun = 0; found=1;
353 break;
354 }
355 }
356 return (!found);
357 }
358 struct scsi2map {
359 char scsi3addr[8];
360 int bus, target, lun;
361 };
362
363 static int
364 cciss_scsi_add_entry(int ctlr, int hostno,
365 struct cciss_scsi_dev_t *device,
366 struct scsi2map *added, int *nadded)
367 {
368 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
369 int n = ccissscsi[ctlr].ndevices;
370 struct cciss_scsi_dev_t *sd;
371 int i, bus, target, lun;
372 unsigned char addr1[8], addr2[8];
373
374 if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
375 printk("cciss%d: Too many devices, "
376 "some will be inaccessible.\n", ctlr);
377 return -1;
378 }
379
380 bus = target = -1;
381 lun = 0;
382 /* Is this device a non-zero lun of a multi-lun device */
383 /* byte 4 of the 8-byte LUN addr will contain the logical unit no. */
384 if (device->scsi3addr[4] != 0) {
385 /* Search through our list and find the device which */
386 /* has the same 8 byte LUN address, excepting byte 4. */
387 /* Assign the same bus and target for this new LUN. */
388 /* Use the logical unit number from the firmware. */
389 memcpy(addr1, device->scsi3addr, 8);
390 addr1[4] = 0;
391 for (i = 0; i < n; i++) {
392 sd = &ccissscsi[ctlr].dev[i];
393 memcpy(addr2, sd->scsi3addr, 8);
394 addr2[4] = 0;
395 /* differ only in byte 4? */
396 if (memcmp(addr1, addr2, 8) == 0) {
397 bus = sd->bus;
398 target = sd->target;
399 lun = device->scsi3addr[4];
400 break;
401 }
402 }
403 }
404
405 sd = &ccissscsi[ctlr].dev[n];
406 if (lun == 0) {
407 if (find_bus_target_lun(ctlr,
408 &sd->bus, &sd->target, &sd->lun) != 0)
409 return -1;
410 } else {
411 sd->bus = bus;
412 sd->target = target;
413 sd->lun = lun;
414 }
415 added[*nadded].bus = sd->bus;
416 added[*nadded].target = sd->target;
417 added[*nadded].lun = sd->lun;
418 (*nadded)++;
419
420 memcpy(sd->scsi3addr, device->scsi3addr, 8);
421 memcpy(sd->vendor, device->vendor, sizeof(sd->vendor));
422 memcpy(sd->revision, device->revision, sizeof(sd->revision));
423 memcpy(sd->device_id, device->device_id, sizeof(sd->device_id));
424 sd->devtype = device->devtype;
425
426 ccissscsi[ctlr].ndevices++;
427
428 /* initially, (before registering with scsi layer) we don't
429 know our hostno and we don't want to print anything first
430 time anyway (the scsi layer's inquiries will show that info) */
431 if (hostno != -1)
432 printk("cciss%d: %s device c%db%dt%dl%d added.\n",
433 ctlr, scsi_device_type(sd->devtype), hostno,
434 sd->bus, sd->target, sd->lun);
435 return 0;
436 }
437
438 static void
439 cciss_scsi_remove_entry(int ctlr, int hostno, int entry,
440 struct scsi2map *removed, int *nremoved)
441 {
442 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
443 int i;
444 struct cciss_scsi_dev_t sd;
445
446 if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
447 sd = ccissscsi[ctlr].dev[entry];
448 removed[*nremoved].bus = sd.bus;
449 removed[*nremoved].target = sd.target;
450 removed[*nremoved].lun = sd.lun;
451 (*nremoved)++;
452 for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++)
453 ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1];
454 ccissscsi[ctlr].ndevices--;
455 printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
456 ctlr, scsi_device_type(sd.devtype), hostno,
457 sd.bus, sd.target, sd.lun);
458 }
459
460
461 #define SCSI3ADDR_EQ(a,b) ( \
462 (a)[7] == (b)[7] && \
463 (a)[6] == (b)[6] && \
464 (a)[5] == (b)[5] && \
465 (a)[4] == (b)[4] && \
466 (a)[3] == (b)[3] && \
467 (a)[2] == (b)[2] && \
468 (a)[1] == (b)[1] && \
469 (a)[0] == (b)[0])
470
471 static void fixup_botched_add(int ctlr, char *scsi3addr)
472 {
473 /* called when scsi_add_device fails in order to re-adjust */
474 /* ccissscsi[] to match the mid layer's view. */
475 unsigned long flags;
476 int i, j;
477 CPQ_TAPE_LOCK(ctlr, flags);
478 for (i = 0; i < ccissscsi[ctlr].ndevices; i++) {
479 if (memcmp(scsi3addr,
480 ccissscsi[ctlr].dev[i].scsi3addr, 8) == 0) {
481 for (j = i; j < ccissscsi[ctlr].ndevices-1; j++)
482 ccissscsi[ctlr].dev[j] =
483 ccissscsi[ctlr].dev[j+1];
484 ccissscsi[ctlr].ndevices--;
485 break;
486 }
487 }
488 CPQ_TAPE_UNLOCK(ctlr, flags);
489 }
490
491 static int device_is_the_same(struct cciss_scsi_dev_t *dev1,
492 struct cciss_scsi_dev_t *dev2)
493 {
494 return dev1->devtype == dev2->devtype &&
495 memcmp(dev1->scsi3addr, dev2->scsi3addr,
496 sizeof(dev1->scsi3addr)) == 0 &&
497 memcmp(dev1->device_id, dev2->device_id,
498 sizeof(dev1->device_id)) == 0 &&
499 memcmp(dev1->vendor, dev2->vendor,
500 sizeof(dev1->vendor)) == 0 &&
501 memcmp(dev1->model, dev2->model,
502 sizeof(dev1->model)) == 0 &&
503 memcmp(dev1->revision, dev2->revision,
504 sizeof(dev1->revision)) == 0;
505 }
506
507 static int
508 adjust_cciss_scsi_table(int ctlr, int hostno,
509 struct cciss_scsi_dev_t sd[], int nsds)
510 {
511 /* sd contains scsi3 addresses and devtypes, but
512 bus target and lun are not filled in. This funciton
513 takes what's in sd to be the current and adjusts
514 ccissscsi[] to be in line with what's in sd. */
515
516 int i,j, found, changes=0;
517 struct cciss_scsi_dev_t *csd;
518 unsigned long flags;
519 struct scsi2map *added, *removed;
520 int nadded, nremoved;
521 struct Scsi_Host *sh = NULL;
522
523 added = kzalloc(sizeof(*added) * CCISS_MAX_SCSI_DEVS_PER_HBA,
524 GFP_KERNEL);
525 removed = kzalloc(sizeof(*removed) * CCISS_MAX_SCSI_DEVS_PER_HBA,
526 GFP_KERNEL);
527
528 if (!added || !removed) {
529 printk(KERN_WARNING "cciss%d: Out of memory in "
530 "adjust_cciss_scsi_table\n", ctlr);
531 goto free_and_out;
532 }
533
534 CPQ_TAPE_LOCK(ctlr, flags);
535
536 if (hostno != -1) /* if it's not the first time... */
537 sh = ((struct cciss_scsi_adapter_data_t *)
538 hba[ctlr]->scsi_ctlr)->scsi_host;
539
540 /* find any devices in ccissscsi[] that are not in
541 sd[] and remove them from ccissscsi[] */
542
543 i = 0;
544 nremoved = 0;
545 nadded = 0;
546 while(i<ccissscsi[ctlr].ndevices) {
547 csd = &ccissscsi[ctlr].dev[i];
548 found=0;
549 for (j=0;j<nsds;j++) {
550 if (SCSI3ADDR_EQ(sd[j].scsi3addr,
551 csd->scsi3addr)) {
552 if (device_is_the_same(&sd[j], csd))
553 found=2;
554 else
555 found=1;
556 break;
557 }
558 }
559
560 if (found == 0) { /* device no longer present. */
561 changes++;
562 /* printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
563 ctlr, scsi_device_type(csd->devtype), hostno,
564 csd->bus, csd->target, csd->lun); */
565 cciss_scsi_remove_entry(ctlr, hostno, i,
566 removed, &nremoved);
567 /* remove ^^^, hence i not incremented */
568 } else if (found == 1) { /* device is different in some way */
569 changes++;
570 printk("cciss%d: device c%db%dt%dl%d has changed.\n",
571 ctlr, hostno, csd->bus, csd->target, csd->lun);
572 cciss_scsi_remove_entry(ctlr, hostno, i,
573 removed, &nremoved);
574 /* remove ^^^, hence i not incremented */
575 if (cciss_scsi_add_entry(ctlr, hostno, &sd[j],
576 added, &nadded) != 0)
577 /* we just removed one, so add can't fail. */
578 BUG();
579 csd->devtype = sd[j].devtype;
580 memcpy(csd->device_id, sd[j].device_id,
581 sizeof(csd->device_id));
582 memcpy(csd->vendor, sd[j].vendor,
583 sizeof(csd->vendor));
584 memcpy(csd->model, sd[j].model,
585 sizeof(csd->model));
586 memcpy(csd->revision, sd[j].revision,
587 sizeof(csd->revision));
588 } else /* device is same as it ever was, */
589 i++; /* so just move along. */
590 }
591
592 /* Now, make sure every device listed in sd[] is also
593 listed in ccissscsi[], adding them if they aren't found */
594
595 for (i=0;i<nsds;i++) {
596 found=0;
597 for (j=0;j<ccissscsi[ctlr].ndevices;j++) {
598 csd = &ccissscsi[ctlr].dev[j];
599 if (SCSI3ADDR_EQ(sd[i].scsi3addr,
600 csd->scsi3addr)) {
601 if (device_is_the_same(&sd[i], csd))
602 found=2; /* found device */
603 else
604 found=1; /* found a bug. */
605 break;
606 }
607 }
608 if (!found) {
609 changes++;
610 if (cciss_scsi_add_entry(ctlr, hostno, &sd[i],
611 added, &nadded) != 0)
612 break;
613 } else if (found == 1) {
614 /* should never happen... */
615 changes++;
616 printk(KERN_WARNING "cciss%d: device "
617 "unexpectedly changed\n", ctlr);
618 /* but if it does happen, we just ignore that device */
619 }
620 }
621 CPQ_TAPE_UNLOCK(ctlr, flags);
622
623 /* Don't notify scsi mid layer of any changes the first time through */
624 /* (or if there are no changes) scsi_scan_host will do it later the */
625 /* first time through. */
626 if (hostno == -1 || !changes)
627 goto free_and_out;
628
629 /* Notify scsi mid layer of any removed devices */
630 for (i = 0; i < nremoved; i++) {
631 struct scsi_device *sdev =
632 scsi_device_lookup(sh, removed[i].bus,
633 removed[i].target, removed[i].lun);
634 if (sdev != NULL) {
635 scsi_remove_device(sdev);
636 scsi_device_put(sdev);
637 } else {
638 /* We don't expect to get here. */
639 /* future cmds to this device will get selection */
640 /* timeout as if the device was gone. */
641 printk(KERN_WARNING "cciss%d: didn't find "
642 "c%db%dt%dl%d\n for removal.",
643 ctlr, hostno, removed[i].bus,
644 removed[i].target, removed[i].lun);
645 }
646 }
647
648 /* Notify scsi mid layer of any added devices */
649 for (i = 0; i < nadded; i++) {
650 int rc;
651 rc = scsi_add_device(sh, added[i].bus,
652 added[i].target, added[i].lun);
653 if (rc == 0)
654 continue;
655 printk(KERN_WARNING "cciss%d: scsi_add_device "
656 "c%db%dt%dl%d failed, device not added.\n",
657 ctlr, hostno,
658 added[i].bus, added[i].target, added[i].lun);
659 /* now we have to remove it from ccissscsi, */
660 /* since it didn't get added to scsi mid layer */
661 fixup_botched_add(ctlr, added[i].scsi3addr);
662 }
663
664 free_and_out:
665 kfree(added);
666 kfree(removed);
667 return 0;
668 }
669
670 static int
671 lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr)
672 {
673 int i;
674 struct cciss_scsi_dev_t *sd;
675 unsigned long flags;
676
677 CPQ_TAPE_LOCK(ctlr, flags);
678 for (i=0;i<ccissscsi[ctlr].ndevices;i++) {
679 sd = &ccissscsi[ctlr].dev[i];
680 if (sd->bus == bus &&
681 sd->target == target &&
682 sd->lun == lun) {
683 memcpy(scsi3addr, &sd->scsi3addr[0], 8);
684 CPQ_TAPE_UNLOCK(ctlr, flags);
685 return 0;
686 }
687 }
688 CPQ_TAPE_UNLOCK(ctlr, flags);
689 return -1;
690 }
691
692 static void
693 cciss_scsi_setup(int cntl_num)
694 {
695 struct cciss_scsi_adapter_data_t * shba;
696
697 ccissscsi[cntl_num].ndevices = 0;
698 shba = (struct cciss_scsi_adapter_data_t *)
699 kmalloc(sizeof(*shba), GFP_KERNEL);
700 if (shba == NULL)
701 return;
702 shba->scsi_host = NULL;
703 spin_lock_init(&shba->lock);
704 shba->registered = 0;
705 if (scsi_cmd_stack_setup(cntl_num, shba) != 0) {
706 kfree(shba);
707 shba = NULL;
708 }
709 hba[cntl_num]->scsi_ctlr = (void *) shba;
710 return;
711 }
712
713 static void
714 complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
715 {
716 struct scsi_cmnd *cmd;
717 ctlr_info_t *ctlr;
718 ErrorInfo_struct *ei;
719
720 ei = cp->err_info;
721
722 /* First, see if it was a message rather than a command */
723 if (cp->Request.Type.Type == TYPE_MSG) {
724 cp->cmd_type = CMD_MSG_DONE;
725 return;
726 }
727
728 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
729 ctlr = hba[cp->ctlr];
730
731 scsi_dma_unmap(cmd);
732
733 cmd->result = (DID_OK << 16); /* host byte */
734 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
735 /* cmd->result |= (GOOD < 1); */ /* status byte */
736
737 cmd->result |= (ei->ScsiStatus);
738 /* printk("Scsistatus is 0x%02x\n", ei->ScsiStatus); */
739
740 /* copy the sense data whether we need to or not. */
741
742 memcpy(cmd->sense_buffer, ei->SenseInfo,
743 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
744 SCSI_SENSE_BUFFERSIZE :
745 ei->SenseLen);
746 scsi_set_resid(cmd, ei->ResidualCnt);
747
748 if(ei->CommandStatus != 0)
749 { /* an error has occurred */
750 switch(ei->CommandStatus)
751 {
752 case CMD_TARGET_STATUS:
753 /* Pass it up to the upper layers... */
754 if( ei->ScsiStatus)
755 {
756 #if 0
757 printk(KERN_WARNING "cciss: cmd %p "
758 "has SCSI Status = %x\n",
759 cp,
760 ei->ScsiStatus);
761 #endif
762 cmd->result |= (ei->ScsiStatus << 1);
763 }
764 else { /* scsi status is zero??? How??? */
765
766 /* Ordinarily, this case should never happen, but there is a bug
767 in some released firmware revisions that allows it to happen
768 if, for example, a 4100 backplane loses power and the tape
769 drive is in it. We assume that it's a fatal error of some
770 kind because we can't show that it wasn't. We will make it
771 look like selection timeout since that is the most common
772 reason for this to occur, and it's severe enough. */
773
774 cmd->result = DID_NO_CONNECT << 16;
775 }
776 break;
777 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
778 break;
779 case CMD_DATA_OVERRUN:
780 printk(KERN_WARNING "cciss: cp %p has"
781 " completed with data overrun "
782 "reported\n", cp);
783 break;
784 case CMD_INVALID: {
785 /* print_bytes(cp, sizeof(*cp), 1, 0);
786 print_cmd(cp); */
787 /* We get CMD_INVALID if you address a non-existent tape drive instead
788 of a selection timeout (no response). You will see this if you yank
789 out a tape drive, then try to access it. This is kind of a shame
790 because it means that any other CMD_INVALID (e.g. driver bug) will
791 get interpreted as a missing target. */
792 cmd->result = DID_NO_CONNECT << 16;
793 }
794 break;
795 case CMD_PROTOCOL_ERR:
796 printk(KERN_WARNING "cciss: cp %p has "
797 "protocol error \n", cp);
798 break;
799 case CMD_HARDWARE_ERR:
800 cmd->result = DID_ERROR << 16;
801 printk(KERN_WARNING "cciss: cp %p had "
802 " hardware error\n", cp);
803 break;
804 case CMD_CONNECTION_LOST:
805 cmd->result = DID_ERROR << 16;
806 printk(KERN_WARNING "cciss: cp %p had "
807 "connection lost\n", cp);
808 break;
809 case CMD_ABORTED:
810 cmd->result = DID_ABORT << 16;
811 printk(KERN_WARNING "cciss: cp %p was "
812 "aborted\n", cp);
813 break;
814 case CMD_ABORT_FAILED:
815 cmd->result = DID_ERROR << 16;
816 printk(KERN_WARNING "cciss: cp %p reports "
817 "abort failed\n", cp);
818 break;
819 case CMD_UNSOLICITED_ABORT:
820 cmd->result = DID_ABORT << 16;
821 printk(KERN_WARNING "cciss: cp %p aborted "
822 "do to an unsolicited abort\n", cp);
823 break;
824 case CMD_TIMEOUT:
825 cmd->result = DID_TIME_OUT << 16;
826 printk(KERN_WARNING "cciss: cp %p timedout\n",
827 cp);
828 break;
829 default:
830 cmd->result = DID_ERROR << 16;
831 printk(KERN_WARNING "cciss: cp %p returned "
832 "unknown status %x\n", cp,
833 ei->CommandStatus);
834 }
835 }
836 // printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel,
837 // cmd->target, cmd->lun);
838 cmd->scsi_done(cmd);
839 scsi_cmd_free(ctlr, cp);
840 }
841
842 static int
843 cciss_scsi_detect(int ctlr)
844 {
845 struct Scsi_Host *sh;
846 int error;
847
848 sh = scsi_host_alloc(&cciss_driver_template, sizeof(struct ctlr_info *));
849 if (sh == NULL)
850 goto fail;
851 sh->io_port = 0; // good enough? FIXME,
852 sh->n_io_port = 0; // I don't think we use these two...
853 sh->this_id = SELF_SCSI_ID;
854
855 ((struct cciss_scsi_adapter_data_t *)
856 hba[ctlr]->scsi_ctlr)->scsi_host = (void *) sh;
857 sh->hostdata[0] = (unsigned long) hba[ctlr];
858 sh->irq = hba[ctlr]->intr[SIMPLE_MODE_INT];
859 sh->unique_id = sh->irq;
860 error = scsi_add_host(sh, &hba[ctlr]->pdev->dev);
861 if (error)
862 goto fail_host_put;
863 scsi_scan_host(sh);
864 return 1;
865
866 fail_host_put:
867 scsi_host_put(sh);
868 fail:
869 return 0;
870 }
871
872 static void
873 cciss_unmap_one(struct pci_dev *pdev,
874 CommandList_struct *cp,
875 size_t buflen,
876 int data_direction)
877 {
878 u64bit addr64;
879
880 addr64.val32.lower = cp->SG[0].Addr.lower;
881 addr64.val32.upper = cp->SG[0].Addr.upper;
882 pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
883 }
884
885 static void
886 cciss_map_one(struct pci_dev *pdev,
887 CommandList_struct *cp,
888 unsigned char *buf,
889 size_t buflen,
890 int data_direction)
891 {
892 __u64 addr64;
893
894 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
895 cp->SG[0].Addr.lower =
896 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
897 cp->SG[0].Addr.upper =
898 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
899 cp->SG[0].Len = buflen;
900 cp->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */
901 cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
902 }
903
904 static int
905 cciss_scsi_do_simple_cmd(ctlr_info_t *c,
906 CommandList_struct *cp,
907 unsigned char *scsi3addr,
908 unsigned char *cdb,
909 unsigned char cdblen,
910 unsigned char *buf, int bufsize,
911 int direction)
912 {
913 unsigned long flags;
914 DECLARE_COMPLETION_ONSTACK(wait);
915
916 cp->cmd_type = CMD_IOCTL_PEND; // treat this like an ioctl
917 cp->scsi_cmd = NULL;
918 cp->Header.ReplyQueue = 0; // unused in simple mode
919 memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN));
920 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
921 // Fill in the request block...
922
923 /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2\n",
924 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
925 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
926
927 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
928 memcpy(cp->Request.CDB, cdb, cdblen);
929 cp->Request.Timeout = 0;
930 cp->Request.CDBLen = cdblen;
931 cp->Request.Type.Type = TYPE_CMD;
932 cp->Request.Type.Attribute = ATTR_SIMPLE;
933 cp->Request.Type.Direction = direction;
934
935 /* Fill in the SG list and do dma mapping */
936 cciss_map_one(c->pdev, cp, (unsigned char *) buf,
937 bufsize, DMA_FROM_DEVICE);
938
939 cp->waiting = &wait;
940
941 /* Put the request on the tail of the request queue */
942 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
943 addQ(&c->reqQ, cp);
944 c->Qdepth++;
945 start_io(c);
946 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
947
948 wait_for_completion(&wait);
949
950 /* undo the dma mapping */
951 cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE);
952 return(0);
953 }
954
955 static void
956 cciss_scsi_interpret_error(CommandList_struct *cp)
957 {
958 ErrorInfo_struct *ei;
959
960 ei = cp->err_info;
961 switch(ei->CommandStatus)
962 {
963 case CMD_TARGET_STATUS:
964 printk(KERN_WARNING "cciss: cmd %p has "
965 "completed with errors\n", cp);
966 printk(KERN_WARNING "cciss: cmd %p "
967 "has SCSI Status = %x\n",
968 cp,
969 ei->ScsiStatus);
970 if (ei->ScsiStatus == 0)
971 printk(KERN_WARNING
972 "cciss:SCSI status is abnormally zero. "
973 "(probably indicates selection timeout "
974 "reported incorrectly due to a known "
975 "firmware bug, circa July, 2001.)\n");
976 break;
977 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
978 printk("UNDERRUN\n");
979 break;
980 case CMD_DATA_OVERRUN:
981 printk(KERN_WARNING "cciss: cp %p has"
982 " completed with data overrun "
983 "reported\n", cp);
984 break;
985 case CMD_INVALID: {
986 /* controller unfortunately reports SCSI passthru's */
987 /* to non-existent targets as invalid commands. */
988 printk(KERN_WARNING "cciss: cp %p is "
989 "reported invalid (probably means "
990 "target device no longer present)\n",
991 cp);
992 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
993 print_cmd(cp); */
994 }
995 break;
996 case CMD_PROTOCOL_ERR:
997 printk(KERN_WARNING "cciss: cp %p has "
998 "protocol error \n", cp);
999 break;
1000 case CMD_HARDWARE_ERR:
1001 /* cmd->result = DID_ERROR << 16; */
1002 printk(KERN_WARNING "cciss: cp %p had "
1003 " hardware error\n", cp);
1004 break;
1005 case CMD_CONNECTION_LOST:
1006 printk(KERN_WARNING "cciss: cp %p had "
1007 "connection lost\n", cp);
1008 break;
1009 case CMD_ABORTED:
1010 printk(KERN_WARNING "cciss: cp %p was "
1011 "aborted\n", cp);
1012 break;
1013 case CMD_ABORT_FAILED:
1014 printk(KERN_WARNING "cciss: cp %p reports "
1015 "abort failed\n", cp);
1016 break;
1017 case CMD_UNSOLICITED_ABORT:
1018 printk(KERN_WARNING "cciss: cp %p aborted "
1019 "do to an unsolicited abort\n", cp);
1020 break;
1021 case CMD_TIMEOUT:
1022 printk(KERN_WARNING "cciss: cp %p timedout\n",
1023 cp);
1024 break;
1025 default:
1026 printk(KERN_WARNING "cciss: cp %p returned "
1027 "unknown status %x\n", cp,
1028 ei->CommandStatus);
1029 }
1030 }
1031
1032 static int
1033 cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr,
1034 unsigned char page, unsigned char *buf,
1035 unsigned char bufsize)
1036 {
1037 int rc;
1038 CommandList_struct *cp;
1039 char cdb[6];
1040 ErrorInfo_struct *ei;
1041 unsigned long flags;
1042
1043 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1044 cp = scsi_cmd_alloc(c);
1045 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1046
1047 if (cp == NULL) { /* trouble... */
1048 printk("cmd_alloc returned NULL!\n");
1049 return -1;
1050 }
1051
1052 ei = cp->err_info;
1053
1054 cdb[0] = CISS_INQUIRY;
1055 cdb[1] = (page != 0);
1056 cdb[2] = page;
1057 cdb[3] = 0;
1058 cdb[4] = bufsize;
1059 cdb[5] = 0;
1060 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb,
1061 6, buf, bufsize, XFER_READ);
1062
1063 if (rc != 0) return rc; /* something went wrong */
1064
1065 if (ei->CommandStatus != 0 &&
1066 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1067 cciss_scsi_interpret_error(cp);
1068 rc = -1;
1069 }
1070 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1071 scsi_cmd_free(c, cp);
1072 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1073 return rc;
1074 }
1075
1076 /* Get the device id from inquiry page 0x83 */
1077 static int cciss_scsi_get_device_id(ctlr_info_t *c, unsigned char *scsi3addr,
1078 unsigned char *device_id, int buflen)
1079 {
1080 int rc;
1081 unsigned char *buf;
1082
1083 if (buflen > 16)
1084 buflen = 16;
1085 buf = kzalloc(64, GFP_KERNEL);
1086 if (!buf)
1087 return -1;
1088 rc = cciss_scsi_do_inquiry(c, scsi3addr, 0x83, buf, 64);
1089 if (rc == 0)
1090 memcpy(device_id, &buf[8], buflen);
1091 kfree(buf);
1092 return rc != 0;
1093 }
1094
1095 static int
1096 cciss_scsi_do_report_phys_luns(ctlr_info_t *c,
1097 ReportLunData_struct *buf, int bufsize)
1098 {
1099 int rc;
1100 CommandList_struct *cp;
1101 unsigned char cdb[12];
1102 unsigned char scsi3addr[8];
1103 ErrorInfo_struct *ei;
1104 unsigned long flags;
1105
1106 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1107 cp = scsi_cmd_alloc(c);
1108 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1109 if (cp == NULL) { /* trouble... */
1110 printk("cmd_alloc returned NULL!\n");
1111 return -1;
1112 }
1113
1114 memset(&scsi3addr[0], 0, 8); /* address the controller */
1115 cdb[0] = CISS_REPORT_PHYS;
1116 cdb[1] = 0;
1117 cdb[2] = 0;
1118 cdb[3] = 0;
1119 cdb[4] = 0;
1120 cdb[5] = 0;
1121 cdb[6] = (bufsize >> 24) & 0xFF; //MSB
1122 cdb[7] = (bufsize >> 16) & 0xFF;
1123 cdb[8] = (bufsize >> 8) & 0xFF;
1124 cdb[9] = bufsize & 0xFF;
1125 cdb[10] = 0;
1126 cdb[11] = 0;
1127
1128 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr,
1129 cdb, 12,
1130 (unsigned char *) buf,
1131 bufsize, XFER_READ);
1132
1133 if (rc != 0) return rc; /* something went wrong */
1134
1135 ei = cp->err_info;
1136 if (ei->CommandStatus != 0 &&
1137 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1138 cciss_scsi_interpret_error(cp);
1139 rc = -1;
1140 }
1141 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1142 scsi_cmd_free(c, cp);
1143 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1144 return rc;
1145 }
1146
1147 static void
1148 cciss_update_non_disk_devices(int cntl_num, int hostno)
1149 {
1150 /* the idea here is we could get notified from /proc
1151 that some devices have changed, so we do a report
1152 physical luns cmd, and adjust our list of devices
1153 accordingly. (We can't rely on the scsi-mid layer just
1154 doing inquiries, because the "busses" that the scsi
1155 mid-layer probes are totally fabricated by this driver,
1156 so new devices wouldn't show up.
1157
1158 the scsi3addr's of devices won't change so long as the
1159 adapter is not reset. That means we can rescan and
1160 tell which devices we already know about, vs. new
1161 devices, vs. disappearing devices.
1162
1163 Also, if you yank out a tape drive, then put in a disk
1164 in it's place, (say, a configured volume from another
1165 array controller for instance) _don't_ poke this driver
1166 (so it thinks it's still a tape, but _do_ poke the scsi
1167 mid layer, so it does an inquiry... the scsi mid layer
1168 will see the physical disk. This would be bad. Need to
1169 think about how to prevent that. One idea would be to
1170 snoop all scsi responses and if an inquiry repsonse comes
1171 back that reports a disk, chuck it an return selection
1172 timeout instead and adjust our table... Not sure i like
1173 that though.
1174
1175 */
1176 #define OBDR_TAPE_INQ_SIZE 49
1177 #define OBDR_TAPE_SIG "$DR-10"
1178 ReportLunData_struct *ld_buff;
1179 unsigned char *inq_buff;
1180 unsigned char scsi3addr[8];
1181 ctlr_info_t *c;
1182 __u32 num_luns=0;
1183 unsigned char *ch;
1184 struct cciss_scsi_dev_t *currentsd, *this_device;
1185 int ncurrent=0;
1186 int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
1187 int i;
1188
1189 c = (ctlr_info_t *) hba[cntl_num];
1190 ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
1191 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1192 currentsd = kzalloc(sizeof(*currentsd) *
1193 (CCISS_MAX_SCSI_DEVS_PER_HBA+1), GFP_KERNEL);
1194 if (ld_buff == NULL || inq_buff == NULL || currentsd == NULL) {
1195 printk(KERN_ERR "cciss: out of memory\n");
1196 goto out;
1197 }
1198 this_device = &currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1199 if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) {
1200 ch = &ld_buff->LUNListLength[0];
1201 num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
1202 if (num_luns > CISS_MAX_PHYS_LUN) {
1203 printk(KERN_WARNING
1204 "cciss: Maximum physical LUNs (%d) exceeded. "
1205 "%d LUNs ignored.\n", CISS_MAX_PHYS_LUN,
1206 num_luns - CISS_MAX_PHYS_LUN);
1207 num_luns = CISS_MAX_PHYS_LUN;
1208 }
1209 }
1210 else {
1211 printk(KERN_ERR "cciss: Report physical LUNs failed.\n");
1212 goto out;
1213 }
1214
1215
1216 /* adjust our table of devices */
1217 for (i = 0; i < num_luns; i++) {
1218 /* for each physical lun, do an inquiry */
1219 if (ld_buff->LUN[i][3] & 0xC0) continue;
1220 memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE);
1221 memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
1222
1223 if (cciss_scsi_do_inquiry(hba[cntl_num], scsi3addr, 0, inq_buff,
1224 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0)
1225 /* Inquiry failed (msg printed already) */
1226 continue; /* so we will skip this device. */
1227
1228 this_device->devtype = (inq_buff[0] & 0x1f);
1229 this_device->bus = -1;
1230 this_device->target = -1;
1231 this_device->lun = -1;
1232 memcpy(this_device->scsi3addr, scsi3addr, 8);
1233 memcpy(this_device->vendor, &inq_buff[8],
1234 sizeof(this_device->vendor));
1235 memcpy(this_device->model, &inq_buff[16],
1236 sizeof(this_device->model));
1237 memcpy(this_device->revision, &inq_buff[32],
1238 sizeof(this_device->revision));
1239 memset(this_device->device_id, 0,
1240 sizeof(this_device->device_id));
1241 cciss_scsi_get_device_id(hba[cntl_num], scsi3addr,
1242 this_device->device_id, sizeof(this_device->device_id));
1243
1244 switch (this_device->devtype)
1245 {
1246 case 0x05: /* CD-ROM */ {
1247
1248 /* We don't *really* support actual CD-ROM devices,
1249 * just this "One Button Disaster Recovery" tape drive
1250 * which temporarily pretends to be a CD-ROM drive.
1251 * So we check that the device is really an OBDR tape
1252 * device by checking for "$DR-10" in bytes 43-48 of
1253 * the inquiry data.
1254 */
1255 char obdr_sig[7];
1256
1257 strncpy(obdr_sig, &inq_buff[43], 6);
1258 obdr_sig[6] = '\0';
1259 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1260 /* Not OBDR device, ignore it. */
1261 break;
1262 }
1263 /* fall through . . . */
1264 case 0x01: /* sequential access, (tape) */
1265 case 0x08: /* medium changer */
1266 if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
1267 printk(KERN_INFO "cciss%d: %s ignored, "
1268 "too many devices.\n", cntl_num,
1269 scsi_device_type(this_device->devtype));
1270 break;
1271 }
1272 currentsd[ncurrent] = *this_device;
1273 ncurrent++;
1274 break;
1275 default:
1276 break;
1277 }
1278 }
1279
1280 adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
1281 out:
1282 kfree(inq_buff);
1283 kfree(ld_buff);
1284 kfree(currentsd);
1285 return;
1286 }
1287
1288 static int
1289 is_keyword(char *ptr, int len, char *verb) // Thanks to ncr53c8xx.c
1290 {
1291 int verb_len = strlen(verb);
1292 if (len >= verb_len && !memcmp(verb,ptr,verb_len))
1293 return verb_len;
1294 else
1295 return 0;
1296 }
1297
1298 static int
1299 cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
1300 {
1301 int arg_len;
1302
1303 if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
1304 cciss_update_non_disk_devices(ctlr, hostno);
1305 else
1306 return -EINVAL;
1307 return length;
1308 }
1309
1310
1311 static int
1312 cciss_scsi_proc_info(struct Scsi_Host *sh,
1313 char *buffer, /* data buffer */
1314 char **start, /* where data in buffer starts */
1315 off_t offset, /* offset from start of imaginary file */
1316 int length, /* length of data in buffer */
1317 int func) /* 0 == read, 1 == write */
1318 {
1319
1320 int buflen, datalen;
1321 ctlr_info_t *ci;
1322 int i;
1323 int cntl_num;
1324
1325
1326 ci = (ctlr_info_t *) sh->hostdata[0];
1327 if (ci == NULL) /* This really shouldn't ever happen. */
1328 return -EINVAL;
1329
1330 cntl_num = ci->ctlr; /* Get our index into the hba[] array */
1331
1332 if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?* */
1333 buflen = sprintf(buffer, "cciss%d: SCSI host: %d\n",
1334 cntl_num, sh->host_no);
1335
1336 /* this information is needed by apps to know which cciss
1337 device corresponds to which scsi host number without
1338 having to open a scsi target device node. The device
1339 information is not a duplicate of /proc/scsi/scsi because
1340 the two may be out of sync due to scsi hotplug, rather
1341 this info is for an app to be able to use to know how to
1342 get them back in sync. */
1343
1344 for (i=0;i<ccissscsi[cntl_num].ndevices;i++) {
1345 struct cciss_scsi_dev_t *sd = &ccissscsi[cntl_num].dev[i];
1346 buflen += sprintf(&buffer[buflen], "c%db%dt%dl%d %02d "
1347 "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1348 sh->host_no, sd->bus, sd->target, sd->lun,
1349 sd->devtype,
1350 sd->scsi3addr[0], sd->scsi3addr[1],
1351 sd->scsi3addr[2], sd->scsi3addr[3],
1352 sd->scsi3addr[4], sd->scsi3addr[5],
1353 sd->scsi3addr[6], sd->scsi3addr[7]);
1354 }
1355 datalen = buflen - offset;
1356 if (datalen < 0) { /* they're reading past EOF. */
1357 datalen = 0;
1358 *start = buffer+buflen;
1359 } else
1360 *start = buffer + offset;
1361 return(datalen);
1362 } else /* User is writing to /proc/scsi/cciss*?/?* ... */
1363 return cciss_scsi_user_command(cntl_num, sh->host_no,
1364 buffer, length);
1365 }
1366
1367 /* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1368 dma mapping and fills in the scatter gather entries of the
1369 cciss command, cp. */
1370
1371 static void
1372 cciss_scatter_gather(struct pci_dev *pdev,
1373 CommandList_struct *cp,
1374 struct scsi_cmnd *cmd)
1375 {
1376 unsigned int len;
1377 struct scatterlist *sg;
1378 __u64 addr64;
1379 int use_sg, i;
1380
1381 BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
1382
1383 use_sg = scsi_dma_map(cmd);
1384 if (use_sg) { /* not too many addrs? */
1385 scsi_for_each_sg(cmd, sg, use_sg, i) {
1386 addr64 = (__u64) sg_dma_address(sg);
1387 len = sg_dma_len(sg);
1388 cp->SG[i].Addr.lower =
1389 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1390 cp->SG[i].Addr.upper =
1391 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1392 cp->SG[i].Len = len;
1393 cp->SG[i].Ext = 0; // we are not chaining
1394 }
1395 }
1396
1397 cp->Header.SGList = (__u8) use_sg; /* no. SGs contig in this cmd */
1398 cp->Header.SGTotal = (__u16) use_sg; /* total sgs in this cmd list */
1399 return;
1400 }
1401
1402
1403 static int
1404 cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
1405 {
1406 ctlr_info_t **c;
1407 int ctlr, rc;
1408 unsigned char scsi3addr[8];
1409 CommandList_struct *cp;
1410 unsigned long flags;
1411
1412 // Get the ptr to our adapter structure (hba[i]) out of cmd->host.
1413 // We violate cmd->host privacy here. (Is there another way?)
1414 c = (ctlr_info_t **) &cmd->device->host->hostdata[0];
1415 ctlr = (*c)->ctlr;
1416
1417 rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id,
1418 cmd->device->lun, scsi3addr);
1419 if (rc != 0) {
1420 /* the scsi nexus does not match any that we presented... */
1421 /* pretend to mid layer that we got selection timeout */
1422 cmd->result = DID_NO_CONNECT << 16;
1423 done(cmd);
1424 /* we might want to think about registering controller itself
1425 as a processor device on the bus so sg binds to it. */
1426 return 0;
1427 }
1428
1429 /* printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%d\n",
1430 cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);*/
1431 // printk("q:%p:c%db%dt%dl%d ", cmd, ctlr, cmd->channel,
1432 // cmd->target, cmd->lun);
1433
1434 /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
1435 see what the device thinks of it. */
1436
1437 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1438 cp = scsi_cmd_alloc(*c);
1439 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1440 if (cp == NULL) { /* trouble... */
1441 printk("scsi_cmd_alloc returned NULL!\n");
1442 /* FIXME: next 3 lines are -> BAD! <- */
1443 cmd->result = DID_NO_CONNECT << 16;
1444 done(cmd);
1445 return 0;
1446 }
1447
1448 // Fill in the command list header
1449
1450 cmd->scsi_done = done; // save this for use by completion code
1451
1452 // save cp in case we have to abort it
1453 cmd->host_scribble = (unsigned char *) cp;
1454
1455 cp->cmd_type = CMD_SCSI;
1456 cp->scsi_cmd = cmd;
1457 cp->Header.ReplyQueue = 0; // unused in simple mode
1458 memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1459 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
1460
1461 // Fill in the request block...
1462
1463 cp->Request.Timeout = 0;
1464 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
1465 BUG_ON(cmd->cmd_len > sizeof(cp->Request.CDB));
1466 cp->Request.CDBLen = cmd->cmd_len;
1467 memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len);
1468 cp->Request.Type.Type = TYPE_CMD;
1469 cp->Request.Type.Attribute = ATTR_SIMPLE;
1470 switch(cmd->sc_data_direction)
1471 {
1472 case DMA_TO_DEVICE: cp->Request.Type.Direction = XFER_WRITE; break;
1473 case DMA_FROM_DEVICE: cp->Request.Type.Direction = XFER_READ; break;
1474 case DMA_NONE: cp->Request.Type.Direction = XFER_NONE; break;
1475 case DMA_BIDIRECTIONAL:
1476 // This can happen if a buggy application does a scsi passthru
1477 // and sets both inlen and outlen to non-zero. ( see
1478 // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1479
1480 cp->Request.Type.Direction = XFER_RSVD;
1481 // This is technically wrong, and cciss controllers should
1482 // reject it with CMD_INVALID, which is the most correct
1483 // response, but non-fibre backends appear to let it
1484 // slide by, and give the same results as if this field
1485 // were set correctly. Either way is acceptable for
1486 // our purposes here.
1487
1488 break;
1489
1490 default:
1491 printk("cciss: unknown data direction: %d\n",
1492 cmd->sc_data_direction);
1493 BUG();
1494 break;
1495 }
1496
1497 cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
1498
1499 /* Put the request on the tail of the request queue */
1500
1501 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1502 addQ(&(*c)->reqQ, cp);
1503 (*c)->Qdepth++;
1504 start_io(*c);
1505 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1506
1507 /* the cmd'll come back via intr handler in complete_scsi_command() */
1508 return 0;
1509 }
1510
1511 static void
1512 cciss_unregister_scsi(int ctlr)
1513 {
1514 struct cciss_scsi_adapter_data_t *sa;
1515 struct cciss_scsi_cmd_stack_t *stk;
1516 unsigned long flags;
1517
1518 /* we are being forcibly unloaded, and may not refuse. */
1519
1520 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1521 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1522 stk = &sa->cmd_stack;
1523
1524 /* if we weren't ever actually registered, don't unregister */
1525 if (sa->registered) {
1526 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1527 scsi_remove_host(sa->scsi_host);
1528 scsi_host_put(sa->scsi_host);
1529 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1530 }
1531
1532 /* set scsi_host to NULL so our detect routine will
1533 find us on register */
1534 sa->scsi_host = NULL;
1535 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1536 scsi_cmd_stack_free(ctlr);
1537 kfree(sa);
1538 }
1539
1540 static int
1541 cciss_engage_scsi(int ctlr)
1542 {
1543 struct cciss_scsi_adapter_data_t *sa;
1544 struct cciss_scsi_cmd_stack_t *stk;
1545 unsigned long flags;
1546
1547 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1548 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1549 stk = &sa->cmd_stack;
1550
1551 if (sa->registered) {
1552 printk("cciss%d: SCSI subsystem already engaged.\n", ctlr);
1553 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1554 return -ENXIO;
1555 }
1556 sa->registered = 1;
1557 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1558 cciss_update_non_disk_devices(ctlr, -1);
1559 cciss_scsi_detect(ctlr);
1560 return 0;
1561 }
1562
1563 static void
1564 cciss_seq_tape_report(struct seq_file *seq, int ctlr)
1565 {
1566 unsigned long flags;
1567
1568 CPQ_TAPE_LOCK(ctlr, flags);
1569 seq_printf(seq,
1570 "Sequential access devices: %d\n\n",
1571 ccissscsi[ctlr].ndevices);
1572 CPQ_TAPE_UNLOCK(ctlr, flags);
1573 }
1574
1575 static int wait_for_device_to_become_ready(ctlr_info_t *h,
1576 unsigned char lunaddr[])
1577 {
1578 int rc;
1579 int count = 0;
1580 int waittime = HZ;
1581 CommandList_struct *c;
1582
1583 c = cmd_alloc(h, 1);
1584 if (!c) {
1585 printk(KERN_WARNING "cciss%d: out of memory in "
1586 "wait_for_device_to_become_ready.\n", h->ctlr);
1587 return IO_ERROR;
1588 }
1589
1590 /* Send test unit ready until device ready, or give up. */
1591 while (count < 20) {
1592
1593 /* Wait for a bit. do this first, because if we send
1594 * the TUR right away, the reset will just abort it.
1595 */
1596 schedule_timeout_uninterruptible(waittime);
1597 count++;
1598
1599 /* Increase wait time with each try, up to a point. */
1600 if (waittime < (HZ * 30))
1601 waittime = waittime * 2;
1602
1603 /* Send the Test Unit Ready */
1604 rc = fill_cmd(c, TEST_UNIT_READY, h->ctlr, NULL, 0, 0,
1605 lunaddr, TYPE_CMD);
1606 if (rc == 0)
1607 rc = sendcmd_withirq_core(h, c, 0);
1608
1609 (void) process_sendcmd_error(h, c);
1610
1611 if (rc != 0)
1612 goto retry_tur;
1613
1614 if (c->err_info->CommandStatus == CMD_SUCCESS)
1615 break;
1616
1617 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
1618 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
1619 if (c->err_info->SenseInfo[2] == NO_SENSE)
1620 break;
1621 if (c->err_info->SenseInfo[2] == UNIT_ATTENTION) {
1622 unsigned char asc;
1623 asc = c->err_info->SenseInfo[12];
1624 check_for_unit_attention(h, c);
1625 if (asc == POWER_OR_RESET)
1626 break;
1627 }
1628 }
1629 retry_tur:
1630 printk(KERN_WARNING "cciss%d: Waiting %d secs "
1631 "for device to become ready.\n",
1632 h->ctlr, waittime / HZ);
1633 rc = 1; /* device not ready. */
1634 }
1635
1636 if (rc)
1637 printk("cciss%d: giving up on device.\n", h->ctlr);
1638 else
1639 printk(KERN_WARNING "cciss%d: device is ready.\n", h->ctlr);
1640
1641 cmd_free(h, c, 1);
1642 return rc;
1643 }
1644
1645 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
1646 * complaining. Doing a host- or bus-reset can't do anything good here.
1647 * Despite what it might say in scsi_error.c, there may well be commands
1648 * on the controller, as the cciss driver registers twice, once as a block
1649 * device for the logical drives, and once as a scsi device, for any tape
1650 * drives. So we know there are no commands out on the tape drives, but we
1651 * don't know there are no commands on the controller, and it is likely
1652 * that there probably are, as the cciss block device is most commonly used
1653 * as a boot device (embedded controller on HP/Compaq systems.)
1654 */
1655
1656 static int cciss_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
1657 {
1658 int rc;
1659 CommandList_struct *cmd_in_trouble;
1660 unsigned char lunaddr[8];
1661 ctlr_info_t **c;
1662 int ctlr;
1663
1664 /* find the controller to which the command to be aborted was sent */
1665 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1666 if (c == NULL) /* paranoia */
1667 return FAILED;
1668 ctlr = (*c)->ctlr;
1669 printk(KERN_WARNING "cciss%d: resetting tape drive or medium changer.\n", ctlr);
1670 /* find the command that's giving us trouble */
1671 cmd_in_trouble = (CommandList_struct *) scsicmd->host_scribble;
1672 if (cmd_in_trouble == NULL) /* paranoia */
1673 return FAILED;
1674 memcpy(lunaddr, &cmd_in_trouble->Header.LUN.LunAddrBytes[0], 8);
1675 /* send a reset to the SCSI LUN which the command was sent to */
1676 rc = sendcmd_withirq(CCISS_RESET_MSG, ctlr, NULL, 0, 0, lunaddr,
1677 TYPE_MSG);
1678 if (rc == 0 && wait_for_device_to_become_ready(*c, lunaddr) == 0)
1679 return SUCCESS;
1680 printk(KERN_WARNING "cciss%d: resetting device failed.\n", ctlr);
1681 return FAILED;
1682 }
1683
1684 static int cciss_eh_abort_handler(struct scsi_cmnd *scsicmd)
1685 {
1686 int rc;
1687 CommandList_struct *cmd_to_abort;
1688 unsigned char lunaddr[8];
1689 ctlr_info_t **c;
1690 int ctlr;
1691
1692 /* find the controller to which the command to be aborted was sent */
1693 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1694 if (c == NULL) /* paranoia */
1695 return FAILED;
1696 ctlr = (*c)->ctlr;
1697 printk(KERN_WARNING "cciss%d: aborting tardy SCSI cmd\n", ctlr);
1698
1699 /* find the command to be aborted */
1700 cmd_to_abort = (CommandList_struct *) scsicmd->host_scribble;
1701 if (cmd_to_abort == NULL) /* paranoia */
1702 return FAILED;
1703 memcpy(lunaddr, &cmd_to_abort->Header.LUN.LunAddrBytes[0], 8);
1704 rc = sendcmd_withirq(CCISS_ABORT_MSG, ctlr, &cmd_to_abort->Header.Tag,
1705 0, 0, lunaddr, TYPE_MSG);
1706 if (rc == 0)
1707 return SUCCESS;
1708 return FAILED;
1709
1710 }
1711
1712 #else /* no CONFIG_CISS_SCSI_TAPE */
1713
1714 /* If no tape support, then these become defined out of existence */
1715
1716 #define cciss_scsi_setup(cntl_num)
1717
1718 #endif /* CONFIG_CISS_SCSI_TAPE */