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