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