blob: 90961a8ea8953f4b44c4aafeac544a7421d4c437 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Disk Array driver for Compaq SA53xx Controllers, SCSI Tape module
3 * Copyright 2001 Compaq Computer Corporation
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; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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
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;
558 u64bit addr64;
559 ErrorInfo_struct *ei;
560
561 ei = cp->err_info;
562
563 /* First, see if it was a message rather than a command */
564 if (cp->Request.Type.Type == TYPE_MSG) {
565 cp->cmd_type = CMD_MSG_DONE;
566 return;
567 }
568
569 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
570 ctlr = hba[cp->ctlr];
571
572 /* undo the DMA mappings */
573
574 if (cmd->use_sg) {
575 pci_unmap_sg(ctlr->pdev,
Christoph Hellwig5d5ff442006-06-03 13:21:13 +0200576 cmd->request_buffer, cmd->use_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 cmd->sc_data_direction);
578 }
579 else if (cmd->request_bufflen) {
580 addr64.val32.lower = cp->SG[0].Addr.lower;
581 addr64.val32.upper = cp->SG[0].Addr.upper;
582 pci_unmap_single(ctlr->pdev, (dma_addr_t) addr64.val,
583 cmd->request_bufflen,
584 cmd->sc_data_direction);
585 }
586
587 cmd->result = (DID_OK << 16); /* host byte */
588 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
589 /* cmd->result |= (GOOD < 1); */ /* status byte */
590
591 cmd->result |= (ei->ScsiStatus);
592 /* printk("Scsistatus is 0x%02x\n", ei->ScsiStatus); */
593
594 /* copy the sense data whether we need to or not. */
595
596 memcpy(cmd->sense_buffer, ei->SenseInfo,
597 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
598 SCSI_SENSE_BUFFERSIZE :
599 ei->SenseLen);
600 cmd->resid = ei->ResidualCnt;
601
602 if(ei->CommandStatus != 0)
603 { /* an error has occurred */
604 switch(ei->CommandStatus)
605 {
606 case CMD_TARGET_STATUS:
607 /* Pass it up to the upper layers... */
608 if( ei->ScsiStatus)
609 {
610#if 0
611 printk(KERN_WARNING "cciss: cmd %p "
612 "has SCSI Status = %x\n",
613 cp,
614 ei->ScsiStatus);
615#endif
616 cmd->result |= (ei->ScsiStatus < 1);
617 }
618 else { /* scsi status is zero??? How??? */
619
620 /* Ordinarily, this case should never happen, but there is a bug
621 in some released firmware revisions that allows it to happen
622 if, for example, a 4100 backplane loses power and the tape
623 drive is in it. We assume that it's a fatal error of some
624 kind because we can't show that it wasn't. We will make it
625 look like selection timeout since that is the most common
626 reason for this to occur, and it's severe enough. */
627
628 cmd->result = DID_NO_CONNECT << 16;
629 }
630 break;
631 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
632 break;
633 case CMD_DATA_OVERRUN:
634 printk(KERN_WARNING "cciss: cp %p has"
635 " completed with data overrun "
636 "reported\n", cp);
637 break;
638 case CMD_INVALID: {
639 /* print_bytes(cp, sizeof(*cp), 1, 0);
640 print_cmd(cp); */
641 /* We get CMD_INVALID if you address a non-existent tape drive instead
642 of a selection timeout (no response). You will see this if you yank
643 out a tape drive, then try to access it. This is kind of a shame
644 because it means that any other CMD_INVALID (e.g. driver bug) will
645 get interpreted as a missing target. */
646 cmd->result = DID_NO_CONNECT << 16;
647 }
648 break;
649 case CMD_PROTOCOL_ERR:
650 printk(KERN_WARNING "cciss: cp %p has "
651 "protocol error \n", cp);
652 break;
653 case CMD_HARDWARE_ERR:
654 cmd->result = DID_ERROR << 16;
655 printk(KERN_WARNING "cciss: cp %p had "
656 " hardware error\n", cp);
657 break;
658 case CMD_CONNECTION_LOST:
659 cmd->result = DID_ERROR << 16;
660 printk(KERN_WARNING "cciss: cp %p had "
661 "connection lost\n", cp);
662 break;
663 case CMD_ABORTED:
664 cmd->result = DID_ABORT << 16;
665 printk(KERN_WARNING "cciss: cp %p was "
666 "aborted\n", cp);
667 break;
668 case CMD_ABORT_FAILED:
669 cmd->result = DID_ERROR << 16;
670 printk(KERN_WARNING "cciss: cp %p reports "
671 "abort failed\n", cp);
672 break;
673 case CMD_UNSOLICITED_ABORT:
674 cmd->result = DID_ABORT << 16;
675 printk(KERN_WARNING "cciss: cp %p aborted "
676 "do to an unsolicited abort\n", cp);
677 break;
678 case CMD_TIMEOUT:
679 cmd->result = DID_TIME_OUT << 16;
680 printk(KERN_WARNING "cciss: cp %p timedout\n",
681 cp);
682 break;
683 default:
684 cmd->result = DID_ERROR << 16;
685 printk(KERN_WARNING "cciss: cp %p returned "
686 "unknown status %x\n", cp,
687 ei->CommandStatus);
688 }
689 }
690 // printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel,
691 // cmd->target, cmd->lun);
692 cmd->scsi_done(cmd);
693 scsi_cmd_free(ctlr, cp);
694}
695
696static int
697cciss_scsi_detect(int ctlr)
698{
699 struct Scsi_Host *sh;
700 int error;
701
702 sh = scsi_host_alloc(&cciss_driver_template, sizeof(struct ctlr_info *));
703 if (sh == NULL)
704 goto fail;
705 sh->io_port = 0; // good enough? FIXME,
706 sh->n_io_port = 0; // I don't think we use these two...
707 sh->this_id = SELF_SCSI_ID;
708
709 ((struct cciss_scsi_adapter_data_t *)
710 hba[ctlr]->scsi_ctlr)->scsi_host = (void *) sh;
711 sh->hostdata[0] = (unsigned long) hba[ctlr];
Mike Millerfb86a352006-01-08 01:03:50 -0800712 sh->irq = hba[ctlr]->intr[SIMPLE_MODE_INT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 sh->unique_id = sh->irq;
714 error = scsi_add_host(sh, &hba[ctlr]->pdev->dev);
715 if (error)
716 goto fail_host_put;
717 scsi_scan_host(sh);
718 return 1;
719
720 fail_host_put:
721 scsi_host_put(sh);
722 fail:
723 return 0;
724}
725
726static void
727cciss_unmap_one(struct pci_dev *pdev,
728 CommandList_struct *cp,
729 size_t buflen,
730 int data_direction)
731{
732 u64bit addr64;
733
734 addr64.val32.lower = cp->SG[0].Addr.lower;
735 addr64.val32.upper = cp->SG[0].Addr.upper;
736 pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
737}
738
739static void
740cciss_map_one(struct pci_dev *pdev,
741 CommandList_struct *cp,
742 unsigned char *buf,
743 size_t buflen,
744 int data_direction)
745{
746 __u64 addr64;
747
748 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
749 cp->SG[0].Addr.lower =
750 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
751 cp->SG[0].Addr.upper =
752 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
753 cp->SG[0].Len = buflen;
754 cp->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */
755 cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
756}
757
758static int
759cciss_scsi_do_simple_cmd(ctlr_info_t *c,
760 CommandList_struct *cp,
761 unsigned char *scsi3addr,
762 unsigned char *cdb,
763 unsigned char cdblen,
764 unsigned char *buf, int bufsize,
765 int direction)
766{
767 unsigned long flags;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700768 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 cp->cmd_type = CMD_IOCTL_PEND; // treat this like an ioctl
771 cp->scsi_cmd = NULL;
772 cp->Header.ReplyQueue = 0; // unused in simple mode
773 memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN));
774 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
775 // Fill in the request block...
776
777 /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2\n",
778 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
779 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
780
781 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
782 memcpy(cp->Request.CDB, cdb, cdblen);
783 cp->Request.Timeout = 0;
784 cp->Request.CDBLen = cdblen;
785 cp->Request.Type.Type = TYPE_CMD;
786 cp->Request.Type.Attribute = ATTR_SIMPLE;
787 cp->Request.Type.Direction = direction;
788
789 /* Fill in the SG list and do dma mapping */
790 cciss_map_one(c->pdev, cp, (unsigned char *) buf,
791 bufsize, DMA_FROM_DEVICE);
792
793 cp->waiting = &wait;
794
795 /* Put the request on the tail of the request queue */
796 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
797 addQ(&c->reqQ, cp);
798 c->Qdepth++;
799 start_io(c);
800 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
801
802 wait_for_completion(&wait);
803
804 /* undo the dma mapping */
805 cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE);
806 return(0);
807}
808
809static void
810cciss_scsi_interpret_error(CommandList_struct *cp)
811{
812 ErrorInfo_struct *ei;
813
814 ei = cp->err_info;
815 switch(ei->CommandStatus)
816 {
817 case CMD_TARGET_STATUS:
818 printk(KERN_WARNING "cciss: cmd %p has "
819 "completed with errors\n", cp);
820 printk(KERN_WARNING "cciss: cmd %p "
821 "has SCSI Status = %x\n",
822 cp,
823 ei->ScsiStatus);
824 if (ei->ScsiStatus == 0)
825 printk(KERN_WARNING
826 "cciss:SCSI status is abnormally zero. "
827 "(probably indicates selection timeout "
828 "reported incorrectly due to a known "
829 "firmware bug, circa July, 2001.)\n");
830 break;
831 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
832 printk("UNDERRUN\n");
833 break;
834 case CMD_DATA_OVERRUN:
835 printk(KERN_WARNING "cciss: cp %p has"
836 " completed with data overrun "
837 "reported\n", cp);
838 break;
839 case CMD_INVALID: {
840 /* controller unfortunately reports SCSI passthru's */
841 /* to non-existent targets as invalid commands. */
842 printk(KERN_WARNING "cciss: cp %p is "
843 "reported invalid (probably means "
844 "target device no longer present)\n",
845 cp);
846 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
847 print_cmd(cp); */
848 }
849 break;
850 case CMD_PROTOCOL_ERR:
851 printk(KERN_WARNING "cciss: cp %p has "
852 "protocol error \n", cp);
853 break;
854 case CMD_HARDWARE_ERR:
855 /* cmd->result = DID_ERROR << 16; */
856 printk(KERN_WARNING "cciss: cp %p had "
857 " hardware error\n", cp);
858 break;
859 case CMD_CONNECTION_LOST:
860 printk(KERN_WARNING "cciss: cp %p had "
861 "connection lost\n", cp);
862 break;
863 case CMD_ABORTED:
864 printk(KERN_WARNING "cciss: cp %p was "
865 "aborted\n", cp);
866 break;
867 case CMD_ABORT_FAILED:
868 printk(KERN_WARNING "cciss: cp %p reports "
869 "abort failed\n", cp);
870 break;
871 case CMD_UNSOLICITED_ABORT:
872 printk(KERN_WARNING "cciss: cp %p aborted "
873 "do to an unsolicited abort\n", cp);
874 break;
875 case CMD_TIMEOUT:
876 printk(KERN_WARNING "cciss: cp %p timedout\n",
877 cp);
878 break;
879 default:
880 printk(KERN_WARNING "cciss: cp %p returned "
881 "unknown status %x\n", cp,
882 ei->CommandStatus);
883 }
884}
885
886static int
887cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr,
Mike Miller47922d02005-09-13 01:25:25 -0700888 unsigned char *buf, unsigned char bufsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 int rc;
891 CommandList_struct *cp;
892 char cdb[6];
893 ErrorInfo_struct *ei;
894 unsigned long flags;
895
896 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
897 cp = scsi_cmd_alloc(c);
898 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
899
900 if (cp == NULL) { /* trouble... */
901 printk("cmd_alloc returned NULL!\n");
902 return -1;
903 }
904
905 ei = cp->err_info;
906
907 cdb[0] = CISS_INQUIRY;
908 cdb[1] = 0;
909 cdb[2] = 0;
910 cdb[3] = 0;
Mike Miller47922d02005-09-13 01:25:25 -0700911 cdb[4] = bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 cdb[5] = 0;
913 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb,
Mike Miller47922d02005-09-13 01:25:25 -0700914 6, buf, bufsize, XFER_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (rc != 0) return rc; /* something went wrong */
917
918 if (ei->CommandStatus != 0 &&
919 ei->CommandStatus != CMD_DATA_UNDERRUN) {
920 cciss_scsi_interpret_error(cp);
921 rc = -1;
922 }
923 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
924 scsi_cmd_free(c, cp);
925 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
926 return rc;
927}
928
929static int
930cciss_scsi_do_report_phys_luns(ctlr_info_t *c,
931 ReportLunData_struct *buf, int bufsize)
932{
933 int rc;
934 CommandList_struct *cp;
935 unsigned char cdb[12];
936 unsigned char scsi3addr[8];
937 ErrorInfo_struct *ei;
938 unsigned long flags;
939
940 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
941 cp = scsi_cmd_alloc(c);
942 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
943 if (cp == NULL) { /* trouble... */
944 printk("cmd_alloc returned NULL!\n");
945 return -1;
946 }
947
948 memset(&scsi3addr[0], 0, 8); /* address the controller */
949 cdb[0] = CISS_REPORT_PHYS;
950 cdb[1] = 0;
951 cdb[2] = 0;
952 cdb[3] = 0;
953 cdb[4] = 0;
954 cdb[5] = 0;
955 cdb[6] = (bufsize >> 24) & 0xFF; //MSB
956 cdb[7] = (bufsize >> 16) & 0xFF;
957 cdb[8] = (bufsize >> 8) & 0xFF;
958 cdb[9] = bufsize & 0xFF;
959 cdb[10] = 0;
960 cdb[11] = 0;
961
962 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr,
963 cdb, 12,
964 (unsigned char *) buf,
965 bufsize, XFER_READ);
966
967 if (rc != 0) return rc; /* something went wrong */
968
969 ei = cp->err_info;
970 if (ei->CommandStatus != 0 &&
971 ei->CommandStatus != CMD_DATA_UNDERRUN) {
972 cciss_scsi_interpret_error(cp);
973 rc = -1;
974 }
975 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
976 scsi_cmd_free(c, cp);
977 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
978 return rc;
979}
980
981static void
982cciss_update_non_disk_devices(int cntl_num, int hostno)
983{
984 /* the idea here is we could get notified from /proc
985 that some devices have changed, so we do a report
986 physical luns cmd, and adjust our list of devices
987 accordingly. (We can't rely on the scsi-mid layer just
988 doing inquiries, because the "busses" that the scsi
989 mid-layer probes are totally fabricated by this driver,
990 so new devices wouldn't show up.
991
992 the scsi3addr's of devices won't change so long as the
993 adapter is not reset. That means we can rescan and
994 tell which devices we already know about, vs. new
995 devices, vs. disappearing devices.
996
997 Also, if you yank out a tape drive, then put in a disk
998 in it's place, (say, a configured volume from another
999 array controller for instance) _don't_ poke this driver
1000 (so it thinks it's still a tape, but _do_ poke the scsi
1001 mid layer, so it does an inquiry... the scsi mid layer
1002 will see the physical disk. This would be bad. Need to
1003 think about how to prevent that. One idea would be to
1004 snoop all scsi responses and if an inquiry repsonse comes
1005 back that reports a disk, chuck it an return selection
1006 timeout instead and adjust our table... Not sure i like
1007 that though.
1008
1009 */
Mike Miller47922d02005-09-13 01:25:25 -07001010#define OBDR_TAPE_INQ_SIZE 49
1011#define OBDR_TAPE_SIG "$DR-10"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 ReportLunData_struct *ld_buff;
Mike Miller47922d02005-09-13 01:25:25 -07001013 unsigned char *inq_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 unsigned char scsi3addr[8];
1015 ctlr_info_t *c;
1016 __u32 num_luns=0;
1017 unsigned char *ch;
1018 /* unsigned char found[CCISS_MAX_SCSI_DEVS_PER_HBA]; */
1019 struct cciss_scsi_dev_t currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1020 int ncurrent=0;
1021 int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
1022 int i;
1023
1024 c = (ctlr_info_t *) hba[cntl_num];
Eric Sesterhenn06ff37f2006-03-08 11:21:52 +01001025 ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (ld_buff == NULL) {
1027 printk(KERN_ERR "cciss: out of memory\n");
1028 return;
1029 }
Mike Miller47922d02005-09-13 01:25:25 -07001030 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (inq_buff == NULL) {
1032 printk(KERN_ERR "cciss: out of memory\n");
1033 kfree(ld_buff);
1034 return;
1035 }
1036
1037 if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) {
1038 ch = &ld_buff->LUNListLength[0];
1039 num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
1040 if (num_luns > CISS_MAX_PHYS_LUN) {
1041 printk(KERN_WARNING
1042 "cciss: Maximum physical LUNs (%d) exceeded. "
1043 "%d LUNs ignored.\n", CISS_MAX_PHYS_LUN,
1044 num_luns - CISS_MAX_PHYS_LUN);
1045 num_luns = CISS_MAX_PHYS_LUN;
1046 }
1047 }
1048 else {
1049 printk(KERN_ERR "cciss: Report physical LUNs failed.\n");
1050 goto out;
1051 }
1052
1053
1054 /* adjust our table of devices */
1055 for(i=0; i<num_luns; i++)
1056 {
1057 int devtype;
1058
1059 /* for each physical lun, do an inquiry */
1060 if (ld_buff->LUN[i][3] & 0xC0) continue;
Mike Miller47922d02005-09-13 01:25:25 -07001061 memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
1063
Mike Miller47922d02005-09-13 01:25:25 -07001064 if (cciss_scsi_do_inquiry(hba[cntl_num], scsi3addr, inq_buff,
1065 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* Inquiry failed (msg printed already) */
1067 devtype = 0; /* so we will skip this device. */
1068 } else /* what kind of device is this? */
Mike Miller47922d02005-09-13 01:25:25 -07001069 devtype = (inq_buff[0] & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 switch (devtype)
1072 {
Mike Miller47922d02005-09-13 01:25:25 -07001073 case 0x05: /* CD-ROM */ {
1074
1075 /* We don't *really* support actual CD-ROM devices,
1076 * just this "One Button Disaster Recovery" tape drive
1077 * which temporarily pretends to be a CD-ROM drive.
1078 * So we check that the device is really an OBDR tape
1079 * device by checking for "$DR-10" in bytes 43-48 of
1080 * the inquiry data.
1081 */
1082 char obdr_sig[7];
1083
1084 strncpy(obdr_sig, &inq_buff[43], 6);
1085 obdr_sig[6] = '\0';
1086 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1087 /* Not OBDR device, ignore it. */
1088 break;
1089 }
1090 /* fall through . . . */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 case 0x01: /* sequential access, (tape) */
1092 case 0x08: /* medium changer */
1093 if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
1094 printk(KERN_INFO "cciss%d: %s ignored, "
1095 "too many devices.\n", cntl_num,
Matthew Wilcox4ff36712006-07-04 12:15:20 -06001096 scsi_device_type(devtype));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 break;
1098 }
1099 memcpy(&currentsd[ncurrent].scsi3addr[0],
1100 &scsi3addr[0], 8);
1101 currentsd[ncurrent].devtype = devtype;
1102 currentsd[ncurrent].bus = -1;
1103 currentsd[ncurrent].target = -1;
1104 currentsd[ncurrent].lun = -1;
1105 ncurrent++;
1106 break;
1107 default:
1108 break;
1109 }
1110 }
1111
1112 adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
1113out:
1114 kfree(inq_buff);
1115 kfree(ld_buff);
1116 return;
1117}
1118
1119static int
1120is_keyword(char *ptr, int len, char *verb) // Thanks to ncr53c8xx.c
1121{
1122 int verb_len = strlen(verb);
1123 if (len >= verb_len && !memcmp(verb,ptr,verb_len))
1124 return verb_len;
1125 else
1126 return 0;
1127}
1128
1129static int
1130cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
1131{
1132 int arg_len;
1133
1134 if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
1135 cciss_update_non_disk_devices(ctlr, hostno);
1136 else
1137 return -EINVAL;
1138 return length;
1139}
1140
1141
1142static int
1143cciss_scsi_proc_info(struct Scsi_Host *sh,
1144 char *buffer, /* data buffer */
1145 char **start, /* where data in buffer starts */
1146 off_t offset, /* offset from start of imaginary file */
1147 int length, /* length of data in buffer */
1148 int func) /* 0 == read, 1 == write */
1149{
1150
1151 int buflen, datalen;
1152 ctlr_info_t *ci;
Mike Millerb9f0bd02005-09-13 01:25:26 -07001153 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 int cntl_num;
1155
1156
1157 ci = (ctlr_info_t *) sh->hostdata[0];
1158 if (ci == NULL) /* This really shouldn't ever happen. */
1159 return -EINVAL;
1160
1161 cntl_num = ci->ctlr; /* Get our index into the hba[] array */
1162
1163 if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?* */
Mike Millerb9f0bd02005-09-13 01:25:26 -07001164 buflen = sprintf(buffer, "cciss%d: SCSI host: %d\n",
1165 cntl_num, sh->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Mike Millerb9f0bd02005-09-13 01:25:26 -07001167 /* this information is needed by apps to know which cciss
1168 device corresponds to which scsi host number without
1169 having to open a scsi target device node. The device
1170 information is not a duplicate of /proc/scsi/scsi because
1171 the two may be out of sync due to scsi hotplug, rather
1172 this info is for an app to be able to use to know how to
1173 get them back in sync. */
1174
1175 for (i=0;i<ccissscsi[cntl_num].ndevices;i++) {
1176 struct cciss_scsi_dev_t *sd = &ccissscsi[cntl_num].dev[i];
1177 buflen += sprintf(&buffer[buflen], "c%db%dt%dl%d %02d "
1178 "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1179 sh->host_no, sd->bus, sd->target, sd->lun,
1180 sd->devtype,
1181 sd->scsi3addr[0], sd->scsi3addr[1],
1182 sd->scsi3addr[2], sd->scsi3addr[3],
1183 sd->scsi3addr[4], sd->scsi3addr[5],
1184 sd->scsi3addr[6], sd->scsi3addr[7]);
1185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 datalen = buflen - offset;
1187 if (datalen < 0) { /* they're reading past EOF. */
1188 datalen = 0;
1189 *start = buffer+buflen;
1190 } else
1191 *start = buffer + offset;
1192 return(datalen);
1193 } else /* User is writing to /proc/scsi/cciss*?/?* ... */
1194 return cciss_scsi_user_command(cntl_num, sh->host_no,
1195 buffer, length);
1196}
1197
1198/* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1199 dma mapping and fills in the scatter gather entries of the
1200 cciss command, cp. */
1201
1202static void
1203cciss_scatter_gather(struct pci_dev *pdev,
1204 CommandList_struct *cp,
1205 struct scsi_cmnd *cmd)
1206{
1207 unsigned int use_sg, nsegs=0, len;
Christoph Hellwig5d5ff442006-06-03 13:21:13 +02001208 struct scatterlist *scatter = (struct scatterlist *) cmd->request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 __u64 addr64;
1210
1211 /* is it just one virtual address? */
1212 if (!cmd->use_sg) {
1213 if (cmd->request_bufflen) { /* anything to xfer? */
1214
1215 addr64 = (__u64) pci_map_single(pdev,
1216 cmd->request_buffer,
1217 cmd->request_bufflen,
1218 cmd->sc_data_direction);
1219
1220 cp->SG[0].Addr.lower =
1221 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1222 cp->SG[0].Addr.upper =
1223 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1224 cp->SG[0].Len = cmd->request_bufflen;
1225 nsegs=1;
1226 }
1227 } /* else, must be a list of virtual addresses.... */
1228 else if (cmd->use_sg <= MAXSGENTRIES) { /* not too many addrs? */
1229
Christoph Hellwig5d5ff442006-06-03 13:21:13 +02001230 use_sg = pci_map_sg(pdev, cmd->request_buffer, cmd->use_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 cmd->sc_data_direction);
1232
1233 for (nsegs=0; nsegs < use_sg; nsegs++) {
1234 addr64 = (__u64) sg_dma_address(&scatter[nsegs]);
1235 len = sg_dma_len(&scatter[nsegs]);
1236 cp->SG[nsegs].Addr.lower =
1237 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1238 cp->SG[nsegs].Addr.upper =
1239 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1240 cp->SG[nsegs].Len = len;
1241 cp->SG[nsegs].Ext = 0; // we are not chaining
1242 }
1243 } else BUG();
1244
1245 cp->Header.SGList = (__u8) nsegs; /* no. SGs contig in this cmd */
1246 cp->Header.SGTotal = (__u16) nsegs; /* total sgs in this cmd list */
1247 return;
1248}
1249
1250
1251static int
1252cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
1253{
1254 ctlr_info_t **c;
1255 int ctlr, rc;
1256 unsigned char scsi3addr[8];
1257 CommandList_struct *cp;
1258 unsigned long flags;
1259
1260 // Get the ptr to our adapter structure (hba[i]) out of cmd->host.
1261 // We violate cmd->host privacy here. (Is there another way?)
1262 c = (ctlr_info_t **) &cmd->device->host->hostdata[0];
1263 ctlr = (*c)->ctlr;
1264
1265 rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id,
1266 cmd->device->lun, scsi3addr);
1267 if (rc != 0) {
1268 /* the scsi nexus does not match any that we presented... */
1269 /* pretend to mid layer that we got selection timeout */
1270 cmd->result = DID_NO_CONNECT << 16;
1271 done(cmd);
1272 /* we might want to think about registering controller itself
1273 as a processor device on the bus so sg binds to it. */
1274 return 0;
1275 }
1276
1277 /* printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%d\n",
1278 cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);*/
1279 // printk("q:%p:c%db%dt%dl%d ", cmd, ctlr, cmd->channel,
1280 // cmd->target, cmd->lun);
1281
1282 /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
1283 see what the device thinks of it. */
1284
1285 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1286 cp = scsi_cmd_alloc(*c);
1287 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1288 if (cp == NULL) { /* trouble... */
1289 printk("scsi_cmd_alloc returned NULL!\n");
1290 /* FIXME: next 3 lines are -> BAD! <- */
1291 cmd->result = DID_NO_CONNECT << 16;
1292 done(cmd);
1293 return 0;
1294 }
1295
1296 // Fill in the command list header
1297
1298 cmd->scsi_done = done; // save this for use by completion code
1299
1300 // save cp in case we have to abort it
1301 cmd->host_scribble = (unsigned char *) cp;
1302
1303 cp->cmd_type = CMD_SCSI;
1304 cp->scsi_cmd = cmd;
1305 cp->Header.ReplyQueue = 0; // unused in simple mode
1306 memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1307 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
1308
1309 // Fill in the request block...
1310
1311 cp->Request.Timeout = 0;
1312 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
Eric Sesterhenn089fe1b2006-03-24 18:50:27 +01001313 BUG_ON(cmd->cmd_len > sizeof(cp->Request.CDB));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 cp->Request.CDBLen = cmd->cmd_len;
1315 memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len);
1316 cp->Request.Type.Type = TYPE_CMD;
1317 cp->Request.Type.Attribute = ATTR_SIMPLE;
1318 switch(cmd->sc_data_direction)
1319 {
1320 case DMA_TO_DEVICE: cp->Request.Type.Direction = XFER_WRITE; break;
1321 case DMA_FROM_DEVICE: cp->Request.Type.Direction = XFER_READ; break;
1322 case DMA_NONE: cp->Request.Type.Direction = XFER_NONE; break;
1323 case DMA_BIDIRECTIONAL:
1324 // This can happen if a buggy application does a scsi passthru
1325 // and sets both inlen and outlen to non-zero. ( see
1326 // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1327
1328 cp->Request.Type.Direction = XFER_RSVD;
1329 // This is technically wrong, and cciss controllers should
1330 // reject it with CMD_INVALID, which is the most correct
1331 // response, but non-fibre backends appear to let it
1332 // slide by, and give the same results as if this field
1333 // were set correctly. Either way is acceptable for
1334 // our purposes here.
1335
1336 break;
1337
1338 default:
1339 printk("cciss: unknown data direction: %d\n",
1340 cmd->sc_data_direction);
1341 BUG();
1342 break;
1343 }
1344
1345 cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
1346
1347 /* Put the request on the tail of the request queue */
1348
1349 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1350 addQ(&(*c)->reqQ, cp);
1351 (*c)->Qdepth++;
1352 start_io(*c);
1353 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1354
1355 /* the cmd'll come back via intr handler in complete_scsi_command() */
1356 return 0;
1357}
1358
1359static void
1360cciss_unregister_scsi(int ctlr)
1361{
1362 struct cciss_scsi_adapter_data_t *sa;
1363 struct cciss_scsi_cmd_stack_t *stk;
1364 unsigned long flags;
1365
1366 /* we are being forcibly unloaded, and may not refuse. */
1367
1368 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1369 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1370 stk = &sa->cmd_stack;
1371
1372 /* if we weren't ever actually registered, don't unregister */
1373 if (sa->registered) {
1374 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1375 scsi_remove_host(sa->scsi_host);
1376 scsi_host_put(sa->scsi_host);
1377 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1378 }
1379
1380 /* set scsi_host to NULL so our detect routine will
1381 find us on register */
1382 sa->scsi_host = NULL;
1383 scsi_cmd_stack_free(ctlr);
1384 kfree(sa);
1385 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1386}
1387
1388static int
1389cciss_register_scsi(int ctlr)
1390{
1391 unsigned long flags;
1392
1393 CPQ_TAPE_LOCK(ctlr, flags);
1394
1395 /* Since this is really a block driver, the SCSI core may not be
1396 initialized at init time, in which case, calling scsi_register_host
1397 would hang. Instead, we do it later, via /proc filesystem
1398 and rc scripts, when we know SCSI core is good to go. */
1399
1400 /* Only register if SCSI devices are detected. */
1401 if (ccissscsi[ctlr].ndevices != 0) {
1402 ((struct cciss_scsi_adapter_data_t *)
1403 hba[ctlr]->scsi_ctlr)->registered = 1;
1404 CPQ_TAPE_UNLOCK(ctlr, flags);
1405 return cciss_scsi_detect(ctlr);
1406 }
1407 CPQ_TAPE_UNLOCK(ctlr, flags);
1408 printk(KERN_INFO
1409 "cciss%d: No appropriate SCSI device detected, "
1410 "SCSI subsystem not engaged.\n", ctlr);
1411 return 0;
1412}
1413
1414static int
1415cciss_engage_scsi(int ctlr)
1416{
1417 struct cciss_scsi_adapter_data_t *sa;
1418 struct cciss_scsi_cmd_stack_t *stk;
1419 unsigned long flags;
1420
1421 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1422 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1423 stk = &sa->cmd_stack;
1424
1425 if (((struct cciss_scsi_adapter_data_t *)
1426 hba[ctlr]->scsi_ctlr)->registered) {
1427 printk("cciss%d: SCSI subsystem already engaged.\n", ctlr);
1428 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1429 return ENXIO;
1430 }
1431 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1432 cciss_update_non_disk_devices(ctlr, -1);
1433 cciss_register_scsi(ctlr);
1434 return 0;
1435}
1436
1437static void
1438cciss_proc_tape_report(int ctlr, unsigned char *buffer, off_t *pos, off_t *len)
1439{
1440 unsigned long flags;
1441 int size;
1442
1443 *pos = *pos -1; *len = *len - 1; // cut off the last trailing newline
1444
1445 CPQ_TAPE_LOCK(ctlr, flags);
1446 size = sprintf(buffer + *len,
Mike Millerb9f0bd02005-09-13 01:25:26 -07001447 "Sequential access devices: %d\n\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 ccissscsi[ctlr].ndevices);
1449 CPQ_TAPE_UNLOCK(ctlr, flags);
1450 *pos += size; *len += size;
1451}
1452
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001453/* Need at least one of these error handlers to keep ../scsi/hosts.c from
1454 * complaining. Doing a host- or bus-reset can't do anything good here.
1455 * Despite what it might say in scsi_error.c, there may well be commands
1456 * on the controller, as the cciss driver registers twice, once as a block
1457 * device for the logical drives, and once as a scsi device, for any tape
1458 * drives. So we know there are no commands out on the tape drives, but we
1459 * don't know there are no commands on the controller, and it is likely
1460 * that there probably are, as the cciss block device is most commonly used
1461 * as a boot device (embedded controller on HP/Compaq systems.)
1462*/
1463
1464static int cciss_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
1465{
1466 int rc;
1467 CommandList_struct *cmd_in_trouble;
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: resetting tape drive or medium changer.\n", ctlr);
1477
1478 /* find the command that's giving us trouble */
1479 cmd_in_trouble = (CommandList_struct *) scsicmd->host_scribble;
1480 if (cmd_in_trouble == NULL) { /* paranoia */
1481 return FAILED;
1482 }
1483 /* send a reset to the SCSI LUN which the command was sent to */
1484 rc = sendcmd(CCISS_RESET_MSG, ctlr, NULL, 0, 2, 0, 0,
1485 (unsigned char *) &cmd_in_trouble->Header.LUN.LunAddrBytes[0],
1486 TYPE_MSG);
1487 /* sendcmd turned off interrputs on the board, turn 'em back on. */
1488 (*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
1489 if (rc == 0)
1490 return SUCCESS;
1491 printk(KERN_WARNING "cciss%d: resetting device failed.\n", ctlr);
1492 return FAILED;
1493}
1494
1495static int cciss_eh_abort_handler(struct scsi_cmnd *scsicmd)
1496{
1497 int rc;
1498 CommandList_struct *cmd_to_abort;
1499 ctlr_info_t **c;
1500 int ctlr;
1501
1502 /* find the controller to which the command to be aborted was sent */
1503 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1504 if (c == NULL) /* paranoia */
1505 return FAILED;
1506 ctlr = (*c)->ctlr;
1507 printk(KERN_WARNING "cciss%d: aborting tardy SCSI cmd\n", ctlr);
1508
1509 /* find the command to be aborted */
1510 cmd_to_abort = (CommandList_struct *) scsicmd->host_scribble;
1511 if (cmd_to_abort == NULL) /* paranoia */
1512 return FAILED;
1513 rc = sendcmd(CCISS_ABORT_MSG, ctlr, &cmd_to_abort->Header.Tag,
1514 0, 2, 0, 0,
1515 (unsigned char *) &cmd_to_abort->Header.LUN.LunAddrBytes[0],
1516 TYPE_MSG);
1517 /* sendcmd turned off interrputs on the board, turn 'em back on. */
1518 (*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
1519 if (rc == 0)
1520 return SUCCESS;
1521 return FAILED;
1522
1523}
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525#else /* no CONFIG_CISS_SCSI_TAPE */
1526
1527/* If no tape support, then these become defined out of existence */
1528
1529#define cciss_scsi_setup(cntl_num)
1530#define cciss_unregister_scsi(ctlr)
1531#define cciss_register_scsi(ctlr)
1532#define cciss_proc_tape_report(ctlr, buffer, pos, len)
1533
1534#endif /* CONFIG_CISS_SCSI_TAPE */