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