[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / drivers / block / cciss.c
blob8f7c1a1ed7f4402128c7a7477758aea040a1ddbc
1 /*
2 * Disk Array driver for HP SA 5xxx and 6xxx Controllers
3 * Copyright 2000, 2002 Hewlett-Packard Development Company, L.P.
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.
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.
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.
19 * Questions/Comments/Bugfixes to iss_storagedev@hp.com
23 #include <linux/config.h> /* CONFIG_PROC_FS */
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/types.h>
27 #include <linux/pci.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/major.h>
32 #include <linux/fs.h>
33 #include <linux/bio.h>
34 #include <linux/blkpg.h>
35 #include <linux/timer.h>
36 #include <linux/proc_fs.h>
37 #include <linux/init.h>
38 #include <linux/hdreg.h>
39 #include <linux/spinlock.h>
40 #include <linux/compat.h>
41 #include <asm/uaccess.h>
42 #include <asm/io.h>
44 #include <linux/blkdev.h>
45 #include <linux/genhd.h>
46 #include <linux/completion.h>
48 #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
49 #define DRIVER_NAME "HP CISS Driver (v 2.6.6)"
50 #define DRIVER_VERSION CCISS_DRIVER_VERSION(2,6,6)
52 /* Embedded module documentation macros - see modules.h */
53 MODULE_AUTHOR("Hewlett-Packard Company");
54 MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 2.6.6");
55 MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
56 " SA6i P600 P800 E400");
57 MODULE_LICENSE("GPL");
59 #include "cciss_cmd.h"
60 #include "cciss.h"
61 #include <linux/cciss_ioctl.h>
63 /* define the PCI info for the cards we can control */
64 static const struct pci_device_id cciss_pci_device_id[] = {
65 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS,
66 0x0E11, 0x4070, 0, 0, 0},
67 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
68 0x0E11, 0x4080, 0, 0, 0},
69 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
70 0x0E11, 0x4082, 0, 0, 0},
71 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
72 0x0E11, 0x4083, 0, 0, 0},
73 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
74 0x0E11, 0x409A, 0, 0, 0},
75 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
76 0x0E11, 0x409B, 0, 0, 0},
77 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
78 0x0E11, 0x409C, 0, 0, 0},
79 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
80 0x0E11, 0x409D, 0, 0, 0},
81 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
82 0x0E11, 0x4091, 0, 0, 0},
83 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA,
84 0x103C, 0x3225, 0, 0, 0},
85 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSB,
86 0x103c, 0x3223, 0, 0, 0},
87 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSB,
88 0x103c, 0x3231, 0, 0, 0},
89 {0,}
91 MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
93 #define NR_PRODUCTS (sizeof(products)/sizeof(struct board_type))
95 /* board_id = Subsystem Device ID & Vendor ID
96 * product = Marketing Name for the board
97 * access = Address of the struct of function pointers
99 static struct board_type products[] = {
100 { 0x40700E11, "Smart Array 5300", &SA5_access },
101 { 0x40800E11, "Smart Array 5i", &SA5B_access},
102 { 0x40820E11, "Smart Array 532", &SA5B_access},
103 { 0x40830E11, "Smart Array 5312", &SA5B_access},
104 { 0x409A0E11, "Smart Array 641", &SA5_access},
105 { 0x409B0E11, "Smart Array 642", &SA5_access},
106 { 0x409C0E11, "Smart Array 6400", &SA5_access},
107 { 0x409D0E11, "Smart Array 6400 EM", &SA5_access},
108 { 0x40910E11, "Smart Array 6i", &SA5_access},
109 { 0x3225103C, "Smart Array P600", &SA5_access},
110 { 0x3223103C, "Smart Array P800", &SA5_access},
111 { 0x3231103C, "Smart Array E400", &SA5_access},
114 /* How long to wait (in millesconds) for board to go into simple mode */
115 #define MAX_CONFIG_WAIT 30000
116 #define MAX_IOCTL_CONFIG_WAIT 1000
118 /*define how many times we will try a command because of bus resets */
119 #define MAX_CMD_RETRIES 3
121 #define READ_AHEAD 1024
122 #define NR_CMDS 384 /* #commands that can be outstanding */
123 #define MAX_CTLR 32
125 /* Originally cciss driver only supports 8 major numbers */
126 #define MAX_CTLR_ORIG 8
129 #define CCISS_DMA_MASK 0xFFFFFFFF /* 32 bit DMA */
131 static ctlr_info_t *hba[MAX_CTLR];
133 static void do_cciss_request(request_queue_t *q);
134 static int cciss_open(struct inode *inode, struct file *filep);
135 static int cciss_release(struct inode *inode, struct file *filep);
136 static int cciss_ioctl(struct inode *inode, struct file *filep,
137 unsigned int cmd, unsigned long arg);
139 static int revalidate_allvol(ctlr_info_t *host);
140 static int cciss_revalidate(struct gendisk *disk);
141 static int deregister_disk(struct gendisk *disk);
142 static int register_new_disk(ctlr_info_t *h);
144 static void cciss_getgeometry(int cntl_num);
146 static void start_io( ctlr_info_t *h);
147 static int sendcmd( __u8 cmd, int ctlr, void *buff, size_t size,
148 unsigned int use_unit_num, unsigned int log_unit, __u8 page_code,
149 unsigned char *scsi3addr, int cmd_type);
151 #ifdef CONFIG_PROC_FS
152 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
153 int length, int *eof, void *data);
154 static void cciss_procinit(int i);
155 #else
156 static void cciss_procinit(int i) {}
157 #endif /* CONFIG_PROC_FS */
159 #ifdef CONFIG_COMPAT
160 static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg);
161 #endif
163 static struct block_device_operations cciss_fops = {
164 .owner = THIS_MODULE,
165 .open = cciss_open,
166 .release = cciss_release,
167 .ioctl = cciss_ioctl,
168 #ifdef CONFIG_COMPAT
169 .compat_ioctl = cciss_compat_ioctl,
170 #endif
171 .revalidate_disk= cciss_revalidate,
175 * Enqueuing and dequeuing functions for cmdlists.
177 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
179 if (*Qptr == NULL) {
180 *Qptr = c;
181 c->next = c->prev = c;
182 } else {
183 c->prev = (*Qptr)->prev;
184 c->next = (*Qptr);
185 (*Qptr)->prev->next = c;
186 (*Qptr)->prev = c;
190 static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
191 CommandList_struct *c)
193 if (c && c->next != c) {
194 if (*Qptr == c) *Qptr = c->next;
195 c->prev->next = c->next;
196 c->next->prev = c->prev;
197 } else {
198 *Qptr = NULL;
200 return c;
203 #include "cciss_scsi.c" /* For SCSI tape support */
205 #ifdef CONFIG_PROC_FS
208 * Report information about this controller.
210 #define ENG_GIG 1000000000
211 #define ENG_GIG_FACTOR (ENG_GIG/512)
212 #define RAID_UNKNOWN 6
213 static const char *raid_label[] = {"0","4","1(1+0)","5","5+1","ADG",
214 "UNKNOWN"};
216 static struct proc_dir_entry *proc_cciss;
218 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
219 int length, int *eof, void *data)
221 off_t pos = 0;
222 off_t len = 0;
223 int size, i, ctlr;
224 ctlr_info_t *h = (ctlr_info_t*)data;
225 drive_info_struct *drv;
226 unsigned long flags;
227 sector_t vol_sz, vol_sz_frac;
229 ctlr = h->ctlr;
231 /* prevent displaying bogus info during configuration
232 * or deconfiguration of a logical volume
234 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
235 if (h->busy_configuring) {
236 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
237 return -EBUSY;
239 h->busy_configuring = 1;
240 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
242 size = sprintf(buffer, "%s: HP %s Controller\n"
243 "Board ID: 0x%08lx\n"
244 "Firmware Version: %c%c%c%c\n"
245 "IRQ: %d\n"
246 "Logical drives: %d\n"
247 "Current Q depth: %d\n"
248 "Current # commands on controller: %d\n"
249 "Max Q depth since init: %d\n"
250 "Max # commands on controller since init: %d\n"
251 "Max SG entries since init: %d\n\n",
252 h->devname,
253 h->product_name,
254 (unsigned long)h->board_id,
255 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], h->firm_ver[3],
256 (unsigned int)h->intr,
257 h->num_luns,
258 h->Qdepth, h->commands_outstanding,
259 h->maxQsinceinit, h->max_outstanding, h->maxSG);
261 pos += size; len += size;
262 cciss_proc_tape_report(ctlr, buffer, &pos, &len);
263 for(i=0; i<=h->highest_lun; i++) {
265 drv = &h->drv[i];
266 if (drv->block_size == 0)
267 continue;
269 vol_sz = drv->nr_blocks;
270 vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
271 vol_sz_frac *= 100;
272 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
274 if (drv->raid_level > 5)
275 drv->raid_level = RAID_UNKNOWN;
276 size = sprintf(buffer+len, "cciss/c%dd%d:"
277 "\t%4u.%02uGB\tRAID %s\n",
278 ctlr, i, (int)vol_sz, (int)vol_sz_frac,
279 raid_label[drv->raid_level]);
280 pos += size; len += size;
283 *eof = 1;
284 *start = buffer+offset;
285 len -= offset;
286 if (len>length)
287 len = length;
288 h->busy_configuring = 0;
289 return len;
292 static int
293 cciss_proc_write(struct file *file, const char __user *buffer,
294 unsigned long count, void *data)
296 unsigned char cmd[80];
297 int len;
298 #ifdef CONFIG_CISS_SCSI_TAPE
299 ctlr_info_t *h = (ctlr_info_t *) data;
300 int rc;
301 #endif
303 if (count > sizeof(cmd)-1) return -EINVAL;
304 if (copy_from_user(cmd, buffer, count)) return -EFAULT;
305 cmd[count] = '\0';
306 len = strlen(cmd); // above 3 lines ensure safety
307 if (len && cmd[len-1] == '\n')
308 cmd[--len] = '\0';
309 # ifdef CONFIG_CISS_SCSI_TAPE
310 if (strcmp("engage scsi", cmd)==0) {
311 rc = cciss_engage_scsi(h->ctlr);
312 if (rc != 0) return -rc;
313 return count;
315 /* might be nice to have "disengage" too, but it's not
316 safely possible. (only 1 module use count, lock issues.) */
317 # endif
318 return -EINVAL;
322 * Get us a file in /proc/cciss that says something about each controller.
323 * Create /proc/cciss if it doesn't exist yet.
325 static void __devinit cciss_procinit(int i)
327 struct proc_dir_entry *pde;
329 if (proc_cciss == NULL) {
330 proc_cciss = proc_mkdir("cciss", proc_root_driver);
331 if (!proc_cciss)
332 return;
335 pde = create_proc_read_entry(hba[i]->devname,
336 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
337 proc_cciss, cciss_proc_get_info, hba[i]);
338 pde->write_proc = cciss_proc_write;
340 #endif /* CONFIG_PROC_FS */
343 * For operations that cannot sleep, a command block is allocated at init,
344 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
345 * which ones are free or in use. For operations that can wait for kmalloc
346 * to possible sleep, this routine can be called with get_from_pool set to 0.
347 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
349 static CommandList_struct * cmd_alloc(ctlr_info_t *h, int get_from_pool)
351 CommandList_struct *c;
352 int i;
353 u64bit temp64;
354 dma_addr_t cmd_dma_handle, err_dma_handle;
356 if (!get_from_pool)
358 c = (CommandList_struct *) pci_alloc_consistent(
359 h->pdev, sizeof(CommandList_struct), &cmd_dma_handle);
360 if(c==NULL)
361 return NULL;
362 memset(c, 0, sizeof(CommandList_struct));
364 c->err_info = (ErrorInfo_struct *)pci_alloc_consistent(
365 h->pdev, sizeof(ErrorInfo_struct),
366 &err_dma_handle);
368 if (c->err_info == NULL)
370 pci_free_consistent(h->pdev,
371 sizeof(CommandList_struct), c, cmd_dma_handle);
372 return NULL;
374 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
375 } else /* get it out of the controllers pool */
377 do {
378 i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
379 if (i == NR_CMDS)
380 return NULL;
381 } while(test_and_set_bit(i & (BITS_PER_LONG - 1), h->cmd_pool_bits+(i/BITS_PER_LONG)) != 0);
382 #ifdef CCISS_DEBUG
383 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
384 #endif
385 c = h->cmd_pool + i;
386 memset(c, 0, sizeof(CommandList_struct));
387 cmd_dma_handle = h->cmd_pool_dhandle
388 + i*sizeof(CommandList_struct);
389 c->err_info = h->errinfo_pool + i;
390 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
391 err_dma_handle = h->errinfo_pool_dhandle
392 + i*sizeof(ErrorInfo_struct);
393 h->nr_allocs++;
396 c->busaddr = (__u32) cmd_dma_handle;
397 temp64.val = (__u64) err_dma_handle;
398 c->ErrDesc.Addr.lower = temp64.val32.lower;
399 c->ErrDesc.Addr.upper = temp64.val32.upper;
400 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
402 c->ctlr = h->ctlr;
403 return c;
409 * Frees a command block that was previously allocated with cmd_alloc().
411 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
413 int i;
414 u64bit temp64;
416 if( !got_from_pool)
418 temp64.val32.lower = c->ErrDesc.Addr.lower;
419 temp64.val32.upper = c->ErrDesc.Addr.upper;
420 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
421 c->err_info, (dma_addr_t) temp64.val);
422 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
423 c, (dma_addr_t) c->busaddr);
424 } else
426 i = c - h->cmd_pool;
427 clear_bit(i&(BITS_PER_LONG-1), h->cmd_pool_bits+(i/BITS_PER_LONG));
428 h->nr_frees++;
432 static inline ctlr_info_t *get_host(struct gendisk *disk)
434 return disk->queue->queuedata;
437 static inline drive_info_struct *get_drv(struct gendisk *disk)
439 return disk->private_data;
443 * Open. Make sure the device is really there.
445 static int cciss_open(struct inode *inode, struct file *filep)
447 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
448 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
450 #ifdef CCISS_DEBUG
451 printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
452 #endif /* CCISS_DEBUG */
455 * Root is allowed to open raw volume zero even if it's not configured
456 * so array config can still work. Root is also allowed to open any
457 * volume that has a LUN ID, so it can issue IOCTL to reread the
458 * disk information. I don't think I really like this
459 * but I'm already using way to many device nodes to claim another one
460 * for "raw controller".
462 if (drv->nr_blocks == 0) {
463 if (iminor(inode) != 0) { /* not node 0? */
464 /* if not node 0 make sure it is a partition = 0 */
465 if (iminor(inode) & 0x0f) {
466 return -ENXIO;
467 /* if it is, make sure we have a LUN ID */
468 } else if (drv->LunID == 0) {
469 return -ENXIO;
472 if (!capable(CAP_SYS_ADMIN))
473 return -EPERM;
475 drv->usage_count++;
476 host->usage_count++;
477 return 0;
480 * Close. Sync first.
482 static int cciss_release(struct inode *inode, struct file *filep)
484 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
485 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
487 #ifdef CCISS_DEBUG
488 printk(KERN_DEBUG "cciss_release %s\n", inode->i_bdev->bd_disk->disk_name);
489 #endif /* CCISS_DEBUG */
491 drv->usage_count--;
492 host->usage_count--;
493 return 0;
496 #ifdef CONFIG_COMPAT
498 static int do_ioctl(struct file *f, unsigned cmd, unsigned long arg)
500 int ret;
501 lock_kernel();
502 ret = cciss_ioctl(f->f_dentry->d_inode, f, cmd, arg);
503 unlock_kernel();
504 return ret;
507 static int cciss_ioctl32_passthru(struct file *f, unsigned cmd, unsigned long arg);
508 static int cciss_ioctl32_big_passthru(struct file *f, unsigned cmd, unsigned long arg);
510 static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg)
512 switch (cmd) {
513 case CCISS_GETPCIINFO:
514 case CCISS_GETINTINFO:
515 case CCISS_SETINTINFO:
516 case CCISS_GETNODENAME:
517 case CCISS_SETNODENAME:
518 case CCISS_GETHEARTBEAT:
519 case CCISS_GETBUSTYPES:
520 case CCISS_GETFIRMVER:
521 case CCISS_GETDRIVVER:
522 case CCISS_REVALIDVOLS:
523 case CCISS_DEREGDISK:
524 case CCISS_REGNEWDISK:
525 case CCISS_REGNEWD:
526 case CCISS_RESCANDISK:
527 case CCISS_GETLUNINFO:
528 return do_ioctl(f, cmd, arg);
530 case CCISS_PASSTHRU32:
531 return cciss_ioctl32_passthru(f, cmd, arg);
532 case CCISS_BIG_PASSTHRU32:
533 return cciss_ioctl32_big_passthru(f, cmd, arg);
535 default:
536 return -ENOIOCTLCMD;
540 static int cciss_ioctl32_passthru(struct file *f, unsigned cmd, unsigned long arg)
542 IOCTL32_Command_struct __user *arg32 =
543 (IOCTL32_Command_struct __user *) arg;
544 IOCTL_Command_struct arg64;
545 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
546 int err;
547 u32 cp;
549 err = 0;
550 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
551 err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
552 err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
553 err |= get_user(arg64.buf_size, &arg32->buf_size);
554 err |= get_user(cp, &arg32->buf);
555 arg64.buf = compat_ptr(cp);
556 err |= copy_to_user(p, &arg64, sizeof(arg64));
558 if (err)
559 return -EFAULT;
561 err = do_ioctl(f, CCISS_PASSTHRU, (unsigned long) p);
562 if (err)
563 return err;
564 err |= copy_in_user(&arg32->error_info, &p->error_info, sizeof(arg32->error_info));
565 if (err)
566 return -EFAULT;
567 return err;
570 static int cciss_ioctl32_big_passthru(struct file *file, unsigned cmd, unsigned long arg)
572 BIG_IOCTL32_Command_struct __user *arg32 =
573 (BIG_IOCTL32_Command_struct __user *) arg;
574 BIG_IOCTL_Command_struct arg64;
575 BIG_IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
576 int err;
577 u32 cp;
579 err = 0;
580 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
581 err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
582 err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
583 err |= get_user(arg64.buf_size, &arg32->buf_size);
584 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
585 err |= get_user(cp, &arg32->buf);
586 arg64.buf = compat_ptr(cp);
587 err |= copy_to_user(p, &arg64, sizeof(arg64));
589 if (err)
590 return -EFAULT;
592 err = do_ioctl(file, CCISS_BIG_PASSTHRU, (unsigned long) p);
593 if (err)
594 return err;
595 err |= copy_in_user(&arg32->error_info, &p->error_info, sizeof(arg32->error_info));
596 if (err)
597 return -EFAULT;
598 return err;
600 #endif
602 * ioctl
604 static int cciss_ioctl(struct inode *inode, struct file *filep,
605 unsigned int cmd, unsigned long arg)
607 struct block_device *bdev = inode->i_bdev;
608 struct gendisk *disk = bdev->bd_disk;
609 ctlr_info_t *host = get_host(disk);
610 drive_info_struct *drv = get_drv(disk);
611 int ctlr = host->ctlr;
612 void __user *argp = (void __user *)arg;
614 #ifdef CCISS_DEBUG
615 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
616 #endif /* CCISS_DEBUG */
618 switch(cmd) {
619 case HDIO_GETGEO:
621 struct hd_geometry driver_geo;
622 if (drv->cylinders) {
623 driver_geo.heads = drv->heads;
624 driver_geo.sectors = drv->sectors;
625 driver_geo.cylinders = drv->cylinders;
626 } else
627 return -ENXIO;
628 driver_geo.start= get_start_sect(inode->i_bdev);
629 if (copy_to_user(argp, &driver_geo, sizeof(struct hd_geometry)))
630 return -EFAULT;
631 return(0);
634 case CCISS_GETPCIINFO:
636 cciss_pci_info_struct pciinfo;
638 if (!arg) return -EINVAL;
639 pciinfo.bus = host->pdev->bus->number;
640 pciinfo.dev_fn = host->pdev->devfn;
641 pciinfo.board_id = host->board_id;
642 if (copy_to_user(argp, &pciinfo, sizeof( cciss_pci_info_struct )))
643 return -EFAULT;
644 return(0);
646 case CCISS_GETINTINFO:
648 cciss_coalint_struct intinfo;
649 if (!arg) return -EINVAL;
650 intinfo.delay = readl(&host->cfgtable->HostWrite.CoalIntDelay);
651 intinfo.count = readl(&host->cfgtable->HostWrite.CoalIntCount);
652 if (copy_to_user(argp, &intinfo, sizeof( cciss_coalint_struct )))
653 return -EFAULT;
654 return(0);
656 case CCISS_SETINTINFO:
658 cciss_coalint_struct intinfo;
659 unsigned long flags;
660 int i;
662 if (!arg) return -EINVAL;
663 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
664 if (copy_from_user(&intinfo, argp, sizeof( cciss_coalint_struct)))
665 return -EFAULT;
666 if ( (intinfo.delay == 0 ) && (intinfo.count == 0))
669 // printk("cciss_ioctl: delay and count cannot be 0\n");
670 return( -EINVAL);
672 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
673 /* Update the field, and then ring the doorbell */
674 writel( intinfo.delay,
675 &(host->cfgtable->HostWrite.CoalIntDelay));
676 writel( intinfo.count,
677 &(host->cfgtable->HostWrite.CoalIntCount));
678 writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
680 for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
681 if (!(readl(host->vaddr + SA5_DOORBELL)
682 & CFGTBL_ChangeReq))
683 break;
684 /* delay and try again */
685 udelay(1000);
687 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
688 if (i >= MAX_IOCTL_CONFIG_WAIT)
689 return -EAGAIN;
690 return(0);
692 case CCISS_GETNODENAME:
694 NodeName_type NodeName;
695 int i;
697 if (!arg) return -EINVAL;
698 for(i=0;i<16;i++)
699 NodeName[i] = readb(&host->cfgtable->ServerName[i]);
700 if (copy_to_user(argp, NodeName, sizeof( NodeName_type)))
701 return -EFAULT;
702 return(0);
704 case CCISS_SETNODENAME:
706 NodeName_type NodeName;
707 unsigned long flags;
708 int i;
710 if (!arg) return -EINVAL;
711 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
713 if (copy_from_user(NodeName, argp, sizeof( NodeName_type)))
714 return -EFAULT;
716 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
718 /* Update the field, and then ring the doorbell */
719 for(i=0;i<16;i++)
720 writeb( NodeName[i], &host->cfgtable->ServerName[i]);
722 writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
724 for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
725 if (!(readl(host->vaddr + SA5_DOORBELL)
726 & CFGTBL_ChangeReq))
727 break;
728 /* delay and try again */
729 udelay(1000);
731 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
732 if (i >= MAX_IOCTL_CONFIG_WAIT)
733 return -EAGAIN;
734 return(0);
737 case CCISS_GETHEARTBEAT:
739 Heartbeat_type heartbeat;
741 if (!arg) return -EINVAL;
742 heartbeat = readl(&host->cfgtable->HeartBeat);
743 if (copy_to_user(argp, &heartbeat, sizeof( Heartbeat_type)))
744 return -EFAULT;
745 return(0);
747 case CCISS_GETBUSTYPES:
749 BusTypes_type BusTypes;
751 if (!arg) return -EINVAL;
752 BusTypes = readl(&host->cfgtable->BusTypes);
753 if (copy_to_user(argp, &BusTypes, sizeof( BusTypes_type) ))
754 return -EFAULT;
755 return(0);
757 case CCISS_GETFIRMVER:
759 FirmwareVer_type firmware;
761 if (!arg) return -EINVAL;
762 memcpy(firmware, host->firm_ver, 4);
764 if (copy_to_user(argp, firmware, sizeof( FirmwareVer_type)))
765 return -EFAULT;
766 return(0);
768 case CCISS_GETDRIVVER:
770 DriverVer_type DriverVer = DRIVER_VERSION;
772 if (!arg) return -EINVAL;
774 if (copy_to_user(argp, &DriverVer, sizeof( DriverVer_type) ))
775 return -EFAULT;
776 return(0);
779 case CCISS_REVALIDVOLS:
780 if (bdev != bdev->bd_contains || drv != host->drv)
781 return -ENXIO;
782 return revalidate_allvol(host);
784 case CCISS_GETLUNINFO: {
785 LogvolInfo_struct luninfo;
786 int i;
788 luninfo.LunID = drv->LunID;
789 luninfo.num_opens = drv->usage_count;
790 luninfo.num_parts = 0;
791 /* count partitions 1 to 15 with sizes > 0 */
792 for (i = 0; i < MAX_PART - 1; i++) {
793 if (!disk->part[i])
794 continue;
795 if (disk->part[i]->nr_sects != 0)
796 luninfo.num_parts++;
798 if (copy_to_user(argp, &luninfo,
799 sizeof(LogvolInfo_struct)))
800 return -EFAULT;
801 return(0);
803 case CCISS_DEREGDISK:
804 return deregister_disk(disk);
806 case CCISS_REGNEWD:
807 return register_new_disk(host);
809 case CCISS_PASSTHRU:
811 IOCTL_Command_struct iocommand;
812 CommandList_struct *c;
813 char *buff = NULL;
814 u64bit temp64;
815 unsigned long flags;
816 DECLARE_COMPLETION(wait);
818 if (!arg) return -EINVAL;
820 if (!capable(CAP_SYS_RAWIO)) return -EPERM;
822 if (copy_from_user(&iocommand, argp, sizeof( IOCTL_Command_struct) ))
823 return -EFAULT;
824 if((iocommand.buf_size < 1) &&
825 (iocommand.Request.Type.Direction != XFER_NONE))
827 return -EINVAL;
829 #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
830 /* Check kmalloc limits */
831 if(iocommand.buf_size > 128000)
832 return -EINVAL;
833 #endif
834 if(iocommand.buf_size > 0)
836 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
837 if( buff == NULL)
838 return -EFAULT;
840 if (iocommand.Request.Type.Direction == XFER_WRITE)
842 /* Copy the data into the buffer we created */
843 if (copy_from_user(buff, iocommand.buf, iocommand.buf_size))
845 kfree(buff);
846 return -EFAULT;
848 } else {
849 memset(buff, 0, iocommand.buf_size);
851 if ((c = cmd_alloc(host , 0)) == NULL)
853 kfree(buff);
854 return -ENOMEM;
856 // Fill in the command type
857 c->cmd_type = CMD_IOCTL_PEND;
858 // Fill in Command Header
859 c->Header.ReplyQueue = 0; // unused in simple mode
860 if( iocommand.buf_size > 0) // buffer to fill
862 c->Header.SGList = 1;
863 c->Header.SGTotal= 1;
864 } else // no buffers to fill
866 c->Header.SGList = 0;
867 c->Header.SGTotal= 0;
869 c->Header.LUN = iocommand.LUN_info;
870 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
872 // Fill in Request block
873 c->Request = iocommand.Request;
875 // Fill in the scatter gather information
876 if (iocommand.buf_size > 0 )
878 temp64.val = pci_map_single( host->pdev, buff,
879 iocommand.buf_size,
880 PCI_DMA_BIDIRECTIONAL);
881 c->SG[0].Addr.lower = temp64.val32.lower;
882 c->SG[0].Addr.upper = temp64.val32.upper;
883 c->SG[0].Len = iocommand.buf_size;
884 c->SG[0].Ext = 0; // we are not chaining
886 c->waiting = &wait;
888 /* Put the request on the tail of the request queue */
889 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
890 addQ(&host->reqQ, c);
891 host->Qdepth++;
892 start_io(host);
893 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
895 wait_for_completion(&wait);
897 /* unlock the buffers from DMA */
898 temp64.val32.lower = c->SG[0].Addr.lower;
899 temp64.val32.upper = c->SG[0].Addr.upper;
900 pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
901 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
903 /* Copy the error information out */
904 iocommand.error_info = *(c->err_info);
905 if ( copy_to_user(argp, &iocommand, sizeof( IOCTL_Command_struct) ) )
907 kfree(buff);
908 cmd_free(host, c, 0);
909 return( -EFAULT);
912 if (iocommand.Request.Type.Direction == XFER_READ)
914 /* Copy the data out of the buffer we created */
915 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size))
917 kfree(buff);
918 cmd_free(host, c, 0);
919 return -EFAULT;
922 kfree(buff);
923 cmd_free(host, c, 0);
924 return(0);
926 case CCISS_BIG_PASSTHRU: {
927 BIG_IOCTL_Command_struct *ioc;
928 CommandList_struct *c;
929 unsigned char **buff = NULL;
930 int *buff_size = NULL;
931 u64bit temp64;
932 unsigned long flags;
933 BYTE sg_used = 0;
934 int status = 0;
935 int i;
936 DECLARE_COMPLETION(wait);
937 __u32 left;
938 __u32 sz;
939 BYTE __user *data_ptr;
941 if (!arg)
942 return -EINVAL;
943 if (!capable(CAP_SYS_RAWIO))
944 return -EPERM;
945 ioc = (BIG_IOCTL_Command_struct *)
946 kmalloc(sizeof(*ioc), GFP_KERNEL);
947 if (!ioc) {
948 status = -ENOMEM;
949 goto cleanup1;
951 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
952 status = -EFAULT;
953 goto cleanup1;
955 if ((ioc->buf_size < 1) &&
956 (ioc->Request.Type.Direction != XFER_NONE)) {
957 status = -EINVAL;
958 goto cleanup1;
960 /* Check kmalloc limits using all SGs */
961 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
962 status = -EINVAL;
963 goto cleanup1;
965 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
966 status = -EINVAL;
967 goto cleanup1;
969 buff = (unsigned char **) kmalloc(MAXSGENTRIES *
970 sizeof(char *), GFP_KERNEL);
971 if (!buff) {
972 status = -ENOMEM;
973 goto cleanup1;
975 memset(buff, 0, MAXSGENTRIES);
976 buff_size = (int *) kmalloc(MAXSGENTRIES * sizeof(int),
977 GFP_KERNEL);
978 if (!buff_size) {
979 status = -ENOMEM;
980 goto cleanup1;
982 left = ioc->buf_size;
983 data_ptr = ioc->buf;
984 while (left) {
985 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
986 buff_size[sg_used] = sz;
987 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
988 if (buff[sg_used] == NULL) {
989 status = -ENOMEM;
990 goto cleanup1;
992 if (ioc->Request.Type.Direction == XFER_WRITE &&
993 copy_from_user(buff[sg_used], data_ptr, sz)) {
994 status = -ENOMEM;
995 goto cleanup1;
996 } else {
997 memset(buff[sg_used], 0, sz);
999 left -= sz;
1000 data_ptr += sz;
1001 sg_used++;
1003 if ((c = cmd_alloc(host , 0)) == NULL) {
1004 status = -ENOMEM;
1005 goto cleanup1;
1007 c->cmd_type = CMD_IOCTL_PEND;
1008 c->Header.ReplyQueue = 0;
1010 if( ioc->buf_size > 0) {
1011 c->Header.SGList = sg_used;
1012 c->Header.SGTotal= sg_used;
1013 } else {
1014 c->Header.SGList = 0;
1015 c->Header.SGTotal= 0;
1017 c->Header.LUN = ioc->LUN_info;
1018 c->Header.Tag.lower = c->busaddr;
1020 c->Request = ioc->Request;
1021 if (ioc->buf_size > 0 ) {
1022 int i;
1023 for(i=0; i<sg_used; i++) {
1024 temp64.val = pci_map_single( host->pdev, buff[i],
1025 buff_size[i],
1026 PCI_DMA_BIDIRECTIONAL);
1027 c->SG[i].Addr.lower = temp64.val32.lower;
1028 c->SG[i].Addr.upper = temp64.val32.upper;
1029 c->SG[i].Len = buff_size[i];
1030 c->SG[i].Ext = 0; /* we are not chaining */
1033 c->waiting = &wait;
1034 /* Put the request on the tail of the request queue */
1035 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1036 addQ(&host->reqQ, c);
1037 host->Qdepth++;
1038 start_io(host);
1039 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1040 wait_for_completion(&wait);
1041 /* unlock the buffers from DMA */
1042 for(i=0; i<sg_used; i++) {
1043 temp64.val32.lower = c->SG[i].Addr.lower;
1044 temp64.val32.upper = c->SG[i].Addr.upper;
1045 pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
1046 buff_size[i], PCI_DMA_BIDIRECTIONAL);
1048 /* Copy the error information out */
1049 ioc->error_info = *(c->err_info);
1050 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
1051 cmd_free(host, c, 0);
1052 status = -EFAULT;
1053 goto cleanup1;
1055 if (ioc->Request.Type.Direction == XFER_READ) {
1056 /* Copy the data out of the buffer we created */
1057 BYTE __user *ptr = ioc->buf;
1058 for(i=0; i< sg_used; i++) {
1059 if (copy_to_user(ptr, buff[i], buff_size[i])) {
1060 cmd_free(host, c, 0);
1061 status = -EFAULT;
1062 goto cleanup1;
1064 ptr += buff_size[i];
1067 cmd_free(host, c, 0);
1068 status = 0;
1069 cleanup1:
1070 if (buff) {
1071 for(i=0; i<sg_used; i++)
1072 if(buff[i] != NULL)
1073 kfree(buff[i]);
1074 kfree(buff);
1076 if (buff_size)
1077 kfree(buff_size);
1078 if (ioc)
1079 kfree(ioc);
1080 return(status);
1082 default:
1083 return -ENOTTY;
1089 * revalidate_allvol is for online array config utilities. After a
1090 * utility reconfigures the drives in the array, it can use this function
1091 * (through an ioctl) to make the driver zap any previous disk structs for
1092 * that controller and get new ones.
1094 * Right now I'm using the getgeometry() function to do this, but this
1095 * function should probably be finer grained and allow you to revalidate one
1096 * particualar logical volume (instead of all of them on a particular
1097 * controller).
1099 static int revalidate_allvol(ctlr_info_t *host)
1101 int ctlr = host->ctlr, i;
1102 unsigned long flags;
1104 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1105 if (host->usage_count > 1) {
1106 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1107 printk(KERN_WARNING "cciss: Device busy for volume"
1108 " revalidation (usage=%d)\n", host->usage_count);
1109 return -EBUSY;
1111 host->usage_count++;
1112 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1114 for(i=0; i< NWD; i++) {
1115 struct gendisk *disk = host->gendisk[i];
1116 if (disk->flags & GENHD_FL_UP)
1117 del_gendisk(disk);
1121 * Set the partition and block size structures for all volumes
1122 * on this controller to zero. We will reread all of this data
1124 memset(host->drv, 0, sizeof(drive_info_struct)
1125 * CISS_MAX_LUN);
1127 * Tell the array controller not to give us any interrupts while
1128 * we check the new geometry. Then turn interrupts back on when
1129 * we're done.
1131 host->access.set_intr_mask(host, CCISS_INTR_OFF);
1132 cciss_getgeometry(ctlr);
1133 host->access.set_intr_mask(host, CCISS_INTR_ON);
1135 /* Loop through each real device */
1136 for (i = 0; i < NWD; i++) {
1137 struct gendisk *disk = host->gendisk[i];
1138 drive_info_struct *drv = &(host->drv[i]);
1139 /* we must register the controller even if no disks exist */
1140 /* this is for the online array utilities */
1141 if (!drv->heads && i)
1142 continue;
1143 blk_queue_hardsect_size(host->queue, drv->block_size);
1144 set_capacity(disk, drv->nr_blocks);
1145 add_disk(disk);
1147 host->usage_count--;
1148 return 0;
1151 static int deregister_disk(struct gendisk *disk)
1153 unsigned long flags;
1154 ctlr_info_t *h = get_host(disk);
1155 drive_info_struct *drv = get_drv(disk);
1156 int ctlr = h->ctlr;
1158 if (!capable(CAP_SYS_RAWIO))
1159 return -EPERM;
1161 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1162 /* make sure logical volume is NOT is use */
1163 if( drv->usage_count > 1) {
1164 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1165 return -EBUSY;
1167 drv->usage_count++;
1168 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1170 /* invalidate the devices and deregister the disk */
1171 if (disk->flags & GENHD_FL_UP)
1172 del_gendisk(disk);
1173 /* check to see if it was the last disk */
1174 if (drv == h->drv + h->highest_lun) {
1175 /* if so, find the new hightest lun */
1176 int i, newhighest =-1;
1177 for(i=0; i<h->highest_lun; i++) {
1178 /* if the disk has size > 0, it is available */
1179 if (h->drv[i].nr_blocks)
1180 newhighest = i;
1182 h->highest_lun = newhighest;
1185 --h->num_luns;
1186 /* zero out the disk size info */
1187 drv->nr_blocks = 0;
1188 drv->block_size = 0;
1189 drv->cylinders = 0;
1190 drv->LunID = 0;
1191 return(0);
1193 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
1194 size_t size,
1195 unsigned int use_unit_num, /* 0: address the controller,
1196 1: address logical volume log_unit,
1197 2: periph device address is scsi3addr */
1198 unsigned int log_unit, __u8 page_code, unsigned char *scsi3addr,
1199 int cmd_type)
1201 ctlr_info_t *h= hba[ctlr];
1202 u64bit buff_dma_handle;
1203 int status = IO_OK;
1205 c->cmd_type = CMD_IOCTL_PEND;
1206 c->Header.ReplyQueue = 0;
1207 if( buff != NULL) {
1208 c->Header.SGList = 1;
1209 c->Header.SGTotal= 1;
1210 } else {
1211 c->Header.SGList = 0;
1212 c->Header.SGTotal= 0;
1214 c->Header.Tag.lower = c->busaddr;
1216 c->Request.Type.Type = cmd_type;
1217 if (cmd_type == TYPE_CMD) {
1218 switch(cmd) {
1219 case CISS_INQUIRY:
1220 /* If the logical unit number is 0 then, this is going
1221 to controller so It's a physical command
1222 mode = 0 target = 0. So we have nothing to write.
1223 otherwise, if use_unit_num == 1,
1224 mode = 1(volume set addressing) target = LUNID
1225 otherwise, if use_unit_num == 2,
1226 mode = 0(periph dev addr) target = scsi3addr */
1227 if (use_unit_num == 1) {
1228 c->Header.LUN.LogDev.VolId=
1229 h->drv[log_unit].LunID;
1230 c->Header.LUN.LogDev.Mode = 1;
1231 } else if (use_unit_num == 2) {
1232 memcpy(c->Header.LUN.LunAddrBytes,scsi3addr,8);
1233 c->Header.LUN.LogDev.Mode = 0;
1235 /* are we trying to read a vital product page */
1236 if(page_code != 0) {
1237 c->Request.CDB[1] = 0x01;
1238 c->Request.CDB[2] = page_code;
1240 c->Request.CDBLen = 6;
1241 c->Request.Type.Attribute = ATTR_SIMPLE;
1242 c->Request.Type.Direction = XFER_READ;
1243 c->Request.Timeout = 0;
1244 c->Request.CDB[0] = CISS_INQUIRY;
1245 c->Request.CDB[4] = size & 0xFF;
1246 break;
1247 case CISS_REPORT_LOG:
1248 case CISS_REPORT_PHYS:
1249 /* Talking to controller so It's a physical command
1250 mode = 00 target = 0. Nothing to write.
1252 c->Request.CDBLen = 12;
1253 c->Request.Type.Attribute = ATTR_SIMPLE;
1254 c->Request.Type.Direction = XFER_READ;
1255 c->Request.Timeout = 0;
1256 c->Request.CDB[0] = cmd;
1257 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
1258 c->Request.CDB[7] = (size >> 16) & 0xFF;
1259 c->Request.CDB[8] = (size >> 8) & 0xFF;
1260 c->Request.CDB[9] = size & 0xFF;
1261 break;
1263 case CCISS_READ_CAPACITY:
1264 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1265 c->Header.LUN.LogDev.Mode = 1;
1266 c->Request.CDBLen = 10;
1267 c->Request.Type.Attribute = ATTR_SIMPLE;
1268 c->Request.Type.Direction = XFER_READ;
1269 c->Request.Timeout = 0;
1270 c->Request.CDB[0] = cmd;
1271 break;
1272 case CCISS_CACHE_FLUSH:
1273 c->Request.CDBLen = 12;
1274 c->Request.Type.Attribute = ATTR_SIMPLE;
1275 c->Request.Type.Direction = XFER_WRITE;
1276 c->Request.Timeout = 0;
1277 c->Request.CDB[0] = BMIC_WRITE;
1278 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
1279 break;
1280 default:
1281 printk(KERN_WARNING
1282 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
1283 return(IO_ERROR);
1285 } else if (cmd_type == TYPE_MSG) {
1286 switch (cmd) {
1287 case 3: /* No-Op message */
1288 c->Request.CDBLen = 1;
1289 c->Request.Type.Attribute = ATTR_SIMPLE;
1290 c->Request.Type.Direction = XFER_WRITE;
1291 c->Request.Timeout = 0;
1292 c->Request.CDB[0] = cmd;
1293 break;
1294 default:
1295 printk(KERN_WARNING
1296 "cciss%d: unknown message type %d\n",
1297 ctlr, cmd);
1298 return IO_ERROR;
1300 } else {
1301 printk(KERN_WARNING
1302 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
1303 return IO_ERROR;
1305 /* Fill in the scatter gather information */
1306 if (size > 0) {
1307 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
1308 buff, size, PCI_DMA_BIDIRECTIONAL);
1309 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1310 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1311 c->SG[0].Len = size;
1312 c->SG[0].Ext = 0; /* we are not chaining */
1314 return status;
1316 static int sendcmd_withirq(__u8 cmd,
1317 int ctlr,
1318 void *buff,
1319 size_t size,
1320 unsigned int use_unit_num,
1321 unsigned int log_unit,
1322 __u8 page_code,
1323 int cmd_type)
1325 ctlr_info_t *h = hba[ctlr];
1326 CommandList_struct *c;
1327 u64bit buff_dma_handle;
1328 unsigned long flags;
1329 int return_status;
1330 DECLARE_COMPLETION(wait);
1332 if ((c = cmd_alloc(h , 0)) == NULL)
1333 return -ENOMEM;
1334 return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1335 log_unit, page_code, NULL, cmd_type);
1336 if (return_status != IO_OK) {
1337 cmd_free(h, c, 0);
1338 return return_status;
1340 resend_cmd2:
1341 c->waiting = &wait;
1343 /* Put the request on the tail of the queue and send it */
1344 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1345 addQ(&h->reqQ, c);
1346 h->Qdepth++;
1347 start_io(h);
1348 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1350 wait_for_completion(&wait);
1352 if(c->err_info->CommandStatus != 0)
1353 { /* an error has occurred */
1354 switch(c->err_info->CommandStatus)
1356 case CMD_TARGET_STATUS:
1357 printk(KERN_WARNING "cciss: cmd %p has "
1358 " completed with errors\n", c);
1359 if( c->err_info->ScsiStatus)
1361 printk(KERN_WARNING "cciss: cmd %p "
1362 "has SCSI Status = %x\n",
1364 c->err_info->ScsiStatus);
1367 break;
1368 case CMD_DATA_UNDERRUN:
1369 case CMD_DATA_OVERRUN:
1370 /* expected for inquire and report lun commands */
1371 break;
1372 case CMD_INVALID:
1373 printk(KERN_WARNING "cciss: Cmd %p is "
1374 "reported invalid\n", c);
1375 return_status = IO_ERROR;
1376 break;
1377 case CMD_PROTOCOL_ERR:
1378 printk(KERN_WARNING "cciss: cmd %p has "
1379 "protocol error \n", c);
1380 return_status = IO_ERROR;
1381 break;
1382 case CMD_HARDWARE_ERR:
1383 printk(KERN_WARNING "cciss: cmd %p had "
1384 " hardware error\n", c);
1385 return_status = IO_ERROR;
1386 break;
1387 case CMD_CONNECTION_LOST:
1388 printk(KERN_WARNING "cciss: cmd %p had "
1389 "connection lost\n", c);
1390 return_status = IO_ERROR;
1391 break;
1392 case CMD_ABORTED:
1393 printk(KERN_WARNING "cciss: cmd %p was "
1394 "aborted\n", c);
1395 return_status = IO_ERROR;
1396 break;
1397 case CMD_ABORT_FAILED:
1398 printk(KERN_WARNING "cciss: cmd %p reports "
1399 "abort failed\n", c);
1400 return_status = IO_ERROR;
1401 break;
1402 case CMD_UNSOLICITED_ABORT:
1403 printk(KERN_WARNING
1404 "cciss%d: unsolicited abort %p\n",
1405 ctlr, c);
1406 if (c->retry_count < MAX_CMD_RETRIES) {
1407 printk(KERN_WARNING
1408 "cciss%d: retrying %p\n",
1409 ctlr, c);
1410 c->retry_count++;
1411 /* erase the old error information */
1412 memset(c->err_info, 0,
1413 sizeof(ErrorInfo_struct));
1414 return_status = IO_OK;
1415 INIT_COMPLETION(wait);
1416 goto resend_cmd2;
1418 return_status = IO_ERROR;
1419 break;
1420 default:
1421 printk(KERN_WARNING "cciss: cmd %p returned "
1422 "unknown status %x\n", c,
1423 c->err_info->CommandStatus);
1424 return_status = IO_ERROR;
1427 /* unlock the buffers from DMA */
1428 pci_unmap_single( h->pdev, (dma_addr_t) buff_dma_handle.val,
1429 size, PCI_DMA_BIDIRECTIONAL);
1430 cmd_free(h, c, 0);
1431 return(return_status);
1434 static void cciss_geometry_inquiry(int ctlr, int logvol,
1435 int withirq, unsigned int total_size,
1436 unsigned int block_size, InquiryData_struct *inq_buff,
1437 drive_info_struct *drv)
1439 int return_code;
1440 memset(inq_buff, 0, sizeof(InquiryData_struct));
1441 if (withirq)
1442 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
1443 inq_buff, sizeof(*inq_buff), 1, logvol ,0xC1, TYPE_CMD);
1444 else
1445 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
1446 sizeof(*inq_buff), 1, logvol ,0xC1, NULL, TYPE_CMD);
1447 if (return_code == IO_OK) {
1448 if(inq_buff->data_byte[8] == 0xFF) {
1449 printk(KERN_WARNING
1450 "cciss: reading geometry failed, volume "
1451 "does not support reading geometry\n");
1452 drv->block_size = block_size;
1453 drv->nr_blocks = total_size;
1454 drv->heads = 255;
1455 drv->sectors = 32; // Sectors per track
1456 drv->cylinders = total_size / 255 / 32;
1457 } else {
1458 unsigned int t;
1460 drv->block_size = block_size;
1461 drv->nr_blocks = total_size;
1462 drv->heads = inq_buff->data_byte[6];
1463 drv->sectors = inq_buff->data_byte[7];
1464 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
1465 drv->cylinders += inq_buff->data_byte[5];
1466 drv->raid_level = inq_buff->data_byte[8];
1467 t = drv->heads * drv->sectors;
1468 if (t > 1) {
1469 drv->cylinders = total_size/t;
1472 } else { /* Get geometry failed */
1473 printk(KERN_WARNING "cciss: reading geometry failed\n");
1475 printk(KERN_INFO " heads= %d, sectors= %d, cylinders= %d\n\n",
1476 drv->heads, drv->sectors, drv->cylinders);
1478 static void
1479 cciss_read_capacity(int ctlr, int logvol, ReadCapdata_struct *buf,
1480 int withirq, unsigned int *total_size, unsigned int *block_size)
1482 int return_code;
1483 memset(buf, 0, sizeof(*buf));
1484 if (withirq)
1485 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
1486 ctlr, buf, sizeof(*buf), 1, logvol, 0, TYPE_CMD);
1487 else
1488 return_code = sendcmd(CCISS_READ_CAPACITY,
1489 ctlr, buf, sizeof(*buf), 1, logvol, 0, NULL, TYPE_CMD);
1490 if (return_code == IO_OK) {
1491 *total_size = be32_to_cpu(*((__be32 *) &buf->total_size[0]))+1;
1492 *block_size = be32_to_cpu(*((__be32 *) &buf->block_size[0]));
1493 } else { /* read capacity command failed */
1494 printk(KERN_WARNING "cciss: read capacity failed\n");
1495 *total_size = 0;
1496 *block_size = BLOCK_SIZE;
1498 printk(KERN_INFO " blocks= %u block_size= %d\n",
1499 *total_size, *block_size);
1500 return;
1503 static int register_new_disk(ctlr_info_t *h)
1505 struct gendisk *disk;
1506 int ctlr = h->ctlr;
1507 int i;
1508 int num_luns;
1509 int logvol;
1510 int new_lun_found = 0;
1511 int new_lun_index = 0;
1512 int free_index_found = 0;
1513 int free_index = 0;
1514 ReportLunData_struct *ld_buff = NULL;
1515 ReadCapdata_struct *size_buff = NULL;
1516 InquiryData_struct *inq_buff = NULL;
1517 int return_code;
1518 int listlength = 0;
1519 __u32 lunid = 0;
1520 unsigned int block_size;
1521 unsigned int total_size;
1523 if (!capable(CAP_SYS_RAWIO))
1524 return -EPERM;
1525 /* if we have no space in our disk array left to add anything */
1526 if( h->num_luns >= CISS_MAX_LUN)
1527 return -EINVAL;
1529 ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1530 if (ld_buff == NULL)
1531 goto mem_msg;
1532 memset(ld_buff, 0, sizeof(ReportLunData_struct));
1533 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
1534 if (size_buff == NULL)
1535 goto mem_msg;
1536 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1537 if (inq_buff == NULL)
1538 goto mem_msg;
1540 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
1541 sizeof(ReportLunData_struct), 0, 0, 0, TYPE_CMD);
1543 if( return_code == IO_OK)
1546 // printk("LUN Data\n--------------------------\n");
1548 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
1549 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
1550 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
1551 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
1552 } else /* reading number of logical volumes failed */
1554 printk(KERN_WARNING "cciss: report logical volume"
1555 " command failed\n");
1556 listlength = 0;
1557 goto free_err;
1559 num_luns = listlength / 8; // 8 bytes pre entry
1560 if (num_luns > CISS_MAX_LUN)
1562 num_luns = CISS_MAX_LUN;
1564 #ifdef CCISS_DEBUG
1565 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
1566 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
1567 ld_buff->LUNListLength[3], num_luns);
1568 #endif
1569 for(i=0; i< num_luns; i++)
1571 int j;
1572 int lunID_found = 0;
1574 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
1575 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
1576 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
1577 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
1579 /* check to see if this is a new lun */
1580 for(j=0; j <= h->highest_lun; j++)
1582 #ifdef CCISS_DEBUG
1583 printk("Checking %d %x against %x\n", j,h->drv[j].LunID,
1584 lunid);
1585 #endif /* CCISS_DEBUG */
1586 if (h->drv[j].LunID == lunid)
1588 lunID_found = 1;
1589 break;
1593 if( lunID_found == 1)
1594 continue;
1595 else
1596 { /* It is the new lun we have been looking for */
1597 #ifdef CCISS_DEBUG
1598 printk("new lun found at %d\n", i);
1599 #endif /* CCISS_DEBUG */
1600 new_lun_index = i;
1601 new_lun_found = 1;
1602 break;
1605 if (!new_lun_found)
1607 printk(KERN_WARNING "cciss: New Logical Volume not found\n");
1608 goto free_err;
1610 /* Now find the free index */
1611 for(i=0; i <CISS_MAX_LUN; i++)
1613 #ifdef CCISS_DEBUG
1614 printk("Checking Index %d\n", i);
1615 #endif /* CCISS_DEBUG */
1616 if(h->drv[i].LunID == 0)
1618 #ifdef CCISS_DEBUG
1619 printk("free index found at %d\n", i);
1620 #endif /* CCISS_DEBUG */
1621 free_index_found = 1;
1622 free_index = i;
1623 break;
1626 if (!free_index_found)
1628 printk(KERN_WARNING "cciss: unable to find free slot for disk\n");
1629 goto free_err;
1632 logvol = free_index;
1633 h->drv[logvol].LunID = lunid;
1634 /* there could be gaps in lun numbers, track hightest */
1635 if(h->highest_lun < lunid)
1636 h->highest_lun = logvol;
1637 cciss_read_capacity(ctlr, logvol, size_buff, 1,
1638 &total_size, &block_size);
1639 cciss_geometry_inquiry(ctlr, logvol, 1, total_size, block_size,
1640 inq_buff, &h->drv[logvol]);
1641 h->drv[logvol].usage_count = 0;
1642 ++h->num_luns;
1643 /* setup partitions per disk */
1644 disk = h->gendisk[logvol];
1645 set_capacity(disk, h->drv[logvol].nr_blocks);
1646 /* if it's the controller it's already added */
1647 if(logvol)
1648 add_disk(disk);
1649 freeret:
1650 kfree(ld_buff);
1651 kfree(size_buff);
1652 kfree(inq_buff);
1653 return (logvol);
1654 mem_msg:
1655 printk(KERN_ERR "cciss: out of memory\n");
1656 free_err:
1657 logvol = -1;
1658 goto freeret;
1661 static int cciss_revalidate(struct gendisk *disk)
1663 ctlr_info_t *h = get_host(disk);
1664 drive_info_struct *drv = get_drv(disk);
1665 int logvol;
1666 int FOUND=0;
1667 unsigned int block_size;
1668 unsigned int total_size;
1669 ReadCapdata_struct *size_buff = NULL;
1670 InquiryData_struct *inq_buff = NULL;
1672 for(logvol=0; logvol < CISS_MAX_LUN; logvol++)
1674 if(h->drv[logvol].LunID == drv->LunID) {
1675 FOUND=1;
1676 break;
1680 if (!FOUND) return 1;
1682 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
1683 if (size_buff == NULL)
1685 printk(KERN_WARNING "cciss: out of memory\n");
1686 return 1;
1688 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1689 if (inq_buff == NULL)
1691 printk(KERN_WARNING "cciss: out of memory\n");
1692 kfree(size_buff);
1693 return 1;
1696 cciss_read_capacity(h->ctlr, logvol, size_buff, 1, &total_size, &block_size);
1697 cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size, inq_buff, drv);
1699 blk_queue_hardsect_size(h->queue, drv->block_size);
1700 set_capacity(disk, drv->nr_blocks);
1702 kfree(size_buff);
1703 kfree(inq_buff);
1704 return 0;
1708 * Wait polling for a command to complete.
1709 * The memory mapped FIFO is polled for the completion.
1710 * Used only at init time, interrupts from the HBA are disabled.
1712 static unsigned long pollcomplete(int ctlr)
1714 unsigned long done;
1715 int i;
1717 /* Wait (up to 20 seconds) for a command to complete */
1719 for (i = 20 * HZ; i > 0; i--) {
1720 done = hba[ctlr]->access.command_completed(hba[ctlr]);
1721 if (done == FIFO_EMPTY) {
1722 set_current_state(TASK_UNINTERRUPTIBLE);
1723 schedule_timeout(1);
1724 } else
1725 return (done);
1727 /* Invalid address to tell caller we ran out of time */
1728 return 1;
1731 * Send a command to the controller, and wait for it to complete.
1732 * Only used at init time.
1734 static int sendcmd(
1735 __u8 cmd,
1736 int ctlr,
1737 void *buff,
1738 size_t size,
1739 unsigned int use_unit_num, /* 0: address the controller,
1740 1: address logical volume log_unit,
1741 2: periph device address is scsi3addr */
1742 unsigned int log_unit,
1743 __u8 page_code,
1744 unsigned char *scsi3addr,
1745 int cmd_type)
1747 CommandList_struct *c;
1748 int i;
1749 unsigned long complete;
1750 ctlr_info_t *info_p= hba[ctlr];
1751 u64bit buff_dma_handle;
1752 int status;
1754 if ((c = cmd_alloc(info_p, 1)) == NULL) {
1755 printk(KERN_WARNING "cciss: unable to get memory");
1756 return(IO_ERROR);
1758 status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1759 log_unit, page_code, scsi3addr, cmd_type);
1760 if (status != IO_OK) {
1761 cmd_free(info_p, c, 1);
1762 return status;
1764 resend_cmd1:
1766 * Disable interrupt
1768 #ifdef CCISS_DEBUG
1769 printk(KERN_DEBUG "cciss: turning intr off\n");
1770 #endif /* CCISS_DEBUG */
1771 info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
1773 /* Make sure there is room in the command FIFO */
1774 /* Actually it should be completely empty at this time. */
1775 for (i = 200000; i > 0; i--)
1777 /* if fifo isn't full go */
1778 if (!(info_p->access.fifo_full(info_p)))
1781 break;
1783 udelay(10);
1784 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
1785 " waiting!\n", ctlr);
1788 * Send the cmd
1790 info_p->access.submit_command(info_p, c);
1791 complete = pollcomplete(ctlr);
1793 #ifdef CCISS_DEBUG
1794 printk(KERN_DEBUG "cciss: command completed\n");
1795 #endif /* CCISS_DEBUG */
1797 if (complete != 1) {
1798 if ( (complete & CISS_ERROR_BIT)
1799 && (complete & ~CISS_ERROR_BIT) == c->busaddr)
1801 /* if data overrun or underun on Report command
1802 ignore it
1804 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
1805 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
1806 (c->Request.CDB[0] == CISS_INQUIRY)) &&
1807 ((c->err_info->CommandStatus ==
1808 CMD_DATA_OVERRUN) ||
1809 (c->err_info->CommandStatus ==
1810 CMD_DATA_UNDERRUN)
1813 complete = c->busaddr;
1814 } else {
1815 if (c->err_info->CommandStatus ==
1816 CMD_UNSOLICITED_ABORT) {
1817 printk(KERN_WARNING "cciss%d: "
1818 "unsolicited abort %p\n",
1819 ctlr, c);
1820 if (c->retry_count < MAX_CMD_RETRIES) {
1821 printk(KERN_WARNING
1822 "cciss%d: retrying %p\n",
1823 ctlr, c);
1824 c->retry_count++;
1825 /* erase the old error */
1826 /* information */
1827 memset(c->err_info, 0,
1828 sizeof(ErrorInfo_struct));
1829 goto resend_cmd1;
1830 } else {
1831 printk(KERN_WARNING
1832 "cciss%d: retried %p too "
1833 "many times\n", ctlr, c);
1834 status = IO_ERROR;
1835 goto cleanup1;
1838 printk(KERN_WARNING "ciss ciss%d: sendcmd"
1839 " Error %x \n", ctlr,
1840 c->err_info->CommandStatus);
1841 printk(KERN_WARNING "ciss ciss%d: sendcmd"
1842 " offensive info\n"
1843 " size %x\n num %x value %x\n", ctlr,
1844 c->err_info->MoreErrInfo.Invalid_Cmd.offense_size,
1845 c->err_info->MoreErrInfo.Invalid_Cmd.offense_num,
1846 c->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
1847 status = IO_ERROR;
1848 goto cleanup1;
1851 if (complete != c->busaddr) {
1852 printk( KERN_WARNING "cciss cciss%d: SendCmd "
1853 "Invalid command list address returned! (%lx)\n",
1854 ctlr, complete);
1855 status = IO_ERROR;
1856 goto cleanup1;
1858 } else {
1859 printk( KERN_WARNING
1860 "cciss cciss%d: SendCmd Timeout out, "
1861 "No command list address returned!\n",
1862 ctlr);
1863 status = IO_ERROR;
1866 cleanup1:
1867 /* unlock the data buffer from DMA */
1868 pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
1869 size, PCI_DMA_BIDIRECTIONAL);
1870 cmd_free(info_p, c, 1);
1871 return (status);
1874 * Map (physical) PCI mem into (virtual) kernel space
1876 static void __iomem *remap_pci_mem(ulong base, ulong size)
1878 ulong page_base = ((ulong) base) & PAGE_MASK;
1879 ulong page_offs = ((ulong) base) - page_base;
1880 void __iomem *page_remapped = ioremap(page_base, page_offs+size);
1882 return page_remapped ? (page_remapped + page_offs) : NULL;
1886 * Takes jobs of the Q and sends them to the hardware, then puts it on
1887 * the Q to wait for completion.
1889 static void start_io( ctlr_info_t *h)
1891 CommandList_struct *c;
1893 while(( c = h->reqQ) != NULL )
1895 /* can't do anything if fifo is full */
1896 if ((h->access.fifo_full(h))) {
1897 printk(KERN_WARNING "cciss: fifo full\n");
1898 break;
1901 /* Get the frist entry from the Request Q */
1902 removeQ(&(h->reqQ), c);
1903 h->Qdepth--;
1905 /* Tell the controller execute command */
1906 h->access.submit_command(h, c);
1908 /* Put job onto the completed Q */
1909 addQ (&(h->cmpQ), c);
1913 static inline void complete_buffers(struct bio *bio, int status)
1915 while (bio) {
1916 struct bio *xbh = bio->bi_next;
1917 int nr_sectors = bio_sectors(bio);
1919 bio->bi_next = NULL;
1920 blk_finished_io(len);
1921 bio_endio(bio, nr_sectors << 9, status ? 0 : -EIO);
1922 bio = xbh;
1926 /* Assumes that CCISS_LOCK(h->ctlr) is held. */
1927 /* Zeros out the error record and then resends the command back */
1928 /* to the controller */
1929 static inline void resend_cciss_cmd( ctlr_info_t *h, CommandList_struct *c)
1931 /* erase the old error information */
1932 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
1934 /* add it to software queue and then send it to the controller */
1935 addQ(&(h->reqQ),c);
1936 h->Qdepth++;
1937 if(h->Qdepth > h->maxQsinceinit)
1938 h->maxQsinceinit = h->Qdepth;
1940 start_io(h);
1942 /* checks the status of the job and calls complete buffers to mark all
1943 * buffers for the completed job.
1945 static inline void complete_command( ctlr_info_t *h, CommandList_struct *cmd,
1946 int timeout)
1948 int status = 1;
1949 int i;
1950 int retry_cmd = 0;
1951 u64bit temp64;
1953 if (timeout)
1954 status = 0;
1956 if(cmd->err_info->CommandStatus != 0)
1957 { /* an error has occurred */
1958 switch(cmd->err_info->CommandStatus)
1960 unsigned char sense_key;
1961 case CMD_TARGET_STATUS:
1962 status = 0;
1964 if( cmd->err_info->ScsiStatus == 0x02)
1966 printk(KERN_WARNING "cciss: cmd %p "
1967 "has CHECK CONDITION "
1968 " byte 2 = 0x%x\n", cmd,
1969 cmd->err_info->SenseInfo[2]
1971 /* check the sense key */
1972 sense_key = 0xf &
1973 cmd->err_info->SenseInfo[2];
1974 /* no status or recovered error */
1975 if((sense_key == 0x0) ||
1976 (sense_key == 0x1))
1978 status = 1;
1980 } else
1982 printk(KERN_WARNING "cciss: cmd %p "
1983 "has SCSI Status 0x%x\n",
1984 cmd, cmd->err_info->ScsiStatus);
1986 break;
1987 case CMD_DATA_UNDERRUN:
1988 printk(KERN_WARNING "cciss: cmd %p has"
1989 " completed with data underrun "
1990 "reported\n", cmd);
1991 break;
1992 case CMD_DATA_OVERRUN:
1993 printk(KERN_WARNING "cciss: cmd %p has"
1994 " completed with data overrun "
1995 "reported\n", cmd);
1996 break;
1997 case CMD_INVALID:
1998 printk(KERN_WARNING "cciss: cmd %p is "
1999 "reported invalid\n", cmd);
2000 status = 0;
2001 break;
2002 case CMD_PROTOCOL_ERR:
2003 printk(KERN_WARNING "cciss: cmd %p has "
2004 "protocol error \n", cmd);
2005 status = 0;
2006 break;
2007 case CMD_HARDWARE_ERR:
2008 printk(KERN_WARNING "cciss: cmd %p had "
2009 " hardware error\n", cmd);
2010 status = 0;
2011 break;
2012 case CMD_CONNECTION_LOST:
2013 printk(KERN_WARNING "cciss: cmd %p had "
2014 "connection lost\n", cmd);
2015 status=0;
2016 break;
2017 case CMD_ABORTED:
2018 printk(KERN_WARNING "cciss: cmd %p was "
2019 "aborted\n", cmd);
2020 status=0;
2021 break;
2022 case CMD_ABORT_FAILED:
2023 printk(KERN_WARNING "cciss: cmd %p reports "
2024 "abort failed\n", cmd);
2025 status=0;
2026 break;
2027 case CMD_UNSOLICITED_ABORT:
2028 printk(KERN_WARNING "cciss%d: unsolicited "
2029 "abort %p\n", h->ctlr, cmd);
2030 if (cmd->retry_count < MAX_CMD_RETRIES) {
2031 retry_cmd=1;
2032 printk(KERN_WARNING
2033 "cciss%d: retrying %p\n",
2034 h->ctlr, cmd);
2035 cmd->retry_count++;
2036 } else
2037 printk(KERN_WARNING
2038 "cciss%d: %p retried too "
2039 "many times\n", h->ctlr, cmd);
2040 status=0;
2041 break;
2042 case CMD_TIMEOUT:
2043 printk(KERN_WARNING "cciss: cmd %p timedout\n",
2044 cmd);
2045 status=0;
2046 break;
2047 default:
2048 printk(KERN_WARNING "cciss: cmd %p returned "
2049 "unknown status %x\n", cmd,
2050 cmd->err_info->CommandStatus);
2051 status=0;
2054 /* We need to return this command */
2055 if(retry_cmd) {
2056 resend_cciss_cmd(h,cmd);
2057 return;
2059 /* command did not need to be retried */
2060 /* unmap the DMA mapping for all the scatter gather elements */
2061 for(i=0; i<cmd->Header.SGList; i++) {
2062 temp64.val32.lower = cmd->SG[i].Addr.lower;
2063 temp64.val32.upper = cmd->SG[i].Addr.upper;
2064 pci_unmap_page(hba[cmd->ctlr]->pdev,
2065 temp64.val, cmd->SG[i].Len,
2066 (cmd->Request.Type.Direction == XFER_READ) ?
2067 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
2069 complete_buffers(cmd->rq->bio, status);
2071 #ifdef CCISS_DEBUG
2072 printk("Done with %p\n", cmd->rq);
2073 #endif /* CCISS_DEBUG */
2075 end_that_request_last(cmd->rq);
2076 cmd_free(h,cmd,1);
2080 * Get a request and submit it to the controller.
2082 static void do_cciss_request(request_queue_t *q)
2084 ctlr_info_t *h= q->queuedata;
2085 CommandList_struct *c;
2086 int start_blk, seg;
2087 struct request *creq;
2088 u64bit temp64;
2089 struct scatterlist tmp_sg[MAXSGENTRIES];
2090 drive_info_struct *drv;
2091 int i, dir;
2093 /* We call start_io here in case there is a command waiting on the
2094 * queue that has not been sent.
2096 if (blk_queue_plugged(q))
2097 goto startio;
2099 queue:
2100 creq = elv_next_request(q);
2101 if (!creq)
2102 goto startio;
2104 if (creq->nr_phys_segments > MAXSGENTRIES)
2105 BUG();
2107 if (( c = cmd_alloc(h, 1)) == NULL)
2108 goto full;
2110 blkdev_dequeue_request(creq);
2112 spin_unlock_irq(q->queue_lock);
2114 c->cmd_type = CMD_RWREQ;
2115 c->rq = creq;
2117 /* fill in the request */
2118 drv = creq->rq_disk->private_data;
2119 c->Header.ReplyQueue = 0; // unused in simple mode
2120 c->Header.Tag.lower = c->busaddr; // use the physical address the cmd block for tag
2121 c->Header.LUN.LogDev.VolId= drv->LunID;
2122 c->Header.LUN.LogDev.Mode = 1;
2123 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
2124 c->Request.Type.Type = TYPE_CMD; // It is a command.
2125 c->Request.Type.Attribute = ATTR_SIMPLE;
2126 c->Request.Type.Direction =
2127 (rq_data_dir(creq) == READ) ? XFER_READ: XFER_WRITE;
2128 c->Request.Timeout = 0; // Don't time out
2129 c->Request.CDB[0] = (rq_data_dir(creq) == READ) ? CCISS_READ : CCISS_WRITE;
2130 start_blk = creq->sector;
2131 #ifdef CCISS_DEBUG
2132 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",(int) creq->sector,
2133 (int) creq->nr_sectors);
2134 #endif /* CCISS_DEBUG */
2136 seg = blk_rq_map_sg(q, creq, tmp_sg);
2138 /* get the DMA records for the setup */
2139 if (c->Request.Type.Direction == XFER_READ)
2140 dir = PCI_DMA_FROMDEVICE;
2141 else
2142 dir = PCI_DMA_TODEVICE;
2144 for (i=0; i<seg; i++)
2146 c->SG[i].Len = tmp_sg[i].length;
2147 temp64.val = (__u64) pci_map_page(h->pdev, tmp_sg[i].page,
2148 tmp_sg[i].offset, tmp_sg[i].length,
2149 dir);
2150 c->SG[i].Addr.lower = temp64.val32.lower;
2151 c->SG[i].Addr.upper = temp64.val32.upper;
2152 c->SG[i].Ext = 0; // we are not chaining
2154 /* track how many SG entries we are using */
2155 if( seg > h->maxSG)
2156 h->maxSG = seg;
2158 #ifdef CCISS_DEBUG
2159 printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n", creq->nr_sectors, seg);
2160 #endif /* CCISS_DEBUG */
2162 c->Header.SGList = c->Header.SGTotal = seg;
2163 c->Request.CDB[1]= 0;
2164 c->Request.CDB[2]= (start_blk >> 24) & 0xff; //MSB
2165 c->Request.CDB[3]= (start_blk >> 16) & 0xff;
2166 c->Request.CDB[4]= (start_blk >> 8) & 0xff;
2167 c->Request.CDB[5]= start_blk & 0xff;
2168 c->Request.CDB[6]= 0; // (sect >> 24) & 0xff; MSB
2169 c->Request.CDB[7]= (creq->nr_sectors >> 8) & 0xff;
2170 c->Request.CDB[8]= creq->nr_sectors & 0xff;
2171 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2173 spin_lock_irq(q->queue_lock);
2175 addQ(&(h->reqQ),c);
2176 h->Qdepth++;
2177 if(h->Qdepth > h->maxQsinceinit)
2178 h->maxQsinceinit = h->Qdepth;
2180 goto queue;
2181 full:
2182 blk_stop_queue(q);
2183 startio:
2184 /* We will already have the driver lock here so not need
2185 * to lock it.
2187 start_io(h);
2190 static irqreturn_t do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs)
2192 ctlr_info_t *h = dev_id;
2193 CommandList_struct *c;
2194 unsigned long flags;
2195 __u32 a, a1;
2196 int j;
2197 int start_queue = h->next_to_run;
2199 /* Is this interrupt for us? */
2200 if (( h->access.intr_pending(h) == 0) || (h->interrupts_enabled == 0))
2201 return IRQ_NONE;
2204 * If there are completed commands in the completion queue,
2205 * we had better do something about it.
2207 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2208 while( h->access.intr_pending(h))
2210 while((a = h->access.command_completed(h)) != FIFO_EMPTY)
2212 a1 = a;
2213 a &= ~3;
2214 if ((c = h->cmpQ) == NULL)
2216 printk(KERN_WARNING "cciss: Completion of %08lx ignored\n", (unsigned long)a1);
2217 continue;
2219 while(c->busaddr != a) {
2220 c = c->next;
2221 if (c == h->cmpQ)
2222 break;
2225 * If we've found the command, take it off the
2226 * completion Q and free it
2228 if (c->busaddr == a) {
2229 removeQ(&h->cmpQ, c);
2230 if (c->cmd_type == CMD_RWREQ) {
2231 complete_command(h, c, 0);
2232 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2233 complete(c->waiting);
2235 # ifdef CONFIG_CISS_SCSI_TAPE
2236 else if (c->cmd_type == CMD_SCSI)
2237 complete_scsi_command(c, 0, a1);
2238 # endif
2239 continue;
2244 /* check to see if we have maxed out the number of commands that can
2245 * be placed on the queue. If so then exit. We do this check here
2246 * in case the interrupt we serviced was from an ioctl and did not
2247 * free any new commands.
2249 if ((find_first_zero_bit(h->cmd_pool_bits, NR_CMDS)) == NR_CMDS)
2250 goto cleanup;
2252 /* We have room on the queue for more commands. Now we need to queue
2253 * them up. We will also keep track of the next queue to run so
2254 * that every queue gets a chance to be started first.
2256 for (j=0; j < NWD; j++){
2257 int curr_queue = (start_queue + j) % NWD;
2258 /* make sure the disk has been added and the drive is real
2259 * because this can be called from the middle of init_one.
2261 if(!(h->gendisk[curr_queue]->queue) ||
2262 !(h->drv[curr_queue].heads))
2263 continue;
2264 blk_start_queue(h->gendisk[curr_queue]->queue);
2266 /* check to see if we have maxed out the number of commands
2267 * that can be placed on the queue.
2269 if ((find_first_zero_bit(h->cmd_pool_bits, NR_CMDS)) == NR_CMDS)
2271 if (curr_queue == start_queue){
2272 h->next_to_run = (start_queue + 1) % NWD;
2273 goto cleanup;
2274 } else {
2275 h->next_to_run = curr_queue;
2276 goto cleanup;
2278 } else {
2279 curr_queue = (curr_queue + 1) % NWD;
2283 cleanup:
2284 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2285 return IRQ_HANDLED;
2289 * We cannot read the structure directly, for portablity we must use
2290 * the io functions.
2291 * This is for debug only.
2293 #ifdef CCISS_DEBUG
2294 static void print_cfg_table( CfgTable_struct *tb)
2296 int i;
2297 char temp_name[17];
2299 printk("Controller Configuration information\n");
2300 printk("------------------------------------\n");
2301 for(i=0;i<4;i++)
2302 temp_name[i] = readb(&(tb->Signature[i]));
2303 temp_name[4]='\0';
2304 printk(" Signature = %s\n", temp_name);
2305 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
2306 printk(" Transport methods supported = 0x%x\n",
2307 readl(&(tb-> TransportSupport)));
2308 printk(" Transport methods active = 0x%x\n",
2309 readl(&(tb->TransportActive)));
2310 printk(" Requested transport Method = 0x%x\n",
2311 readl(&(tb->HostWrite.TransportRequest)));
2312 printk(" Coalese Interrupt Delay = 0x%x\n",
2313 readl(&(tb->HostWrite.CoalIntDelay)));
2314 printk(" Coalese Interrupt Count = 0x%x\n",
2315 readl(&(tb->HostWrite.CoalIntCount)));
2316 printk(" Max outstanding commands = 0x%d\n",
2317 readl(&(tb->CmdsOutMax)));
2318 printk(" Bus Types = 0x%x\n", readl(&(tb-> BusTypes)));
2319 for(i=0;i<16;i++)
2320 temp_name[i] = readb(&(tb->ServerName[i]));
2321 temp_name[16] = '\0';
2322 printk(" Server Name = %s\n", temp_name);
2323 printk(" Heartbeat Counter = 0x%x\n\n\n",
2324 readl(&(tb->HeartBeat)));
2326 #endif /* CCISS_DEBUG */
2328 static void release_io_mem(ctlr_info_t *c)
2330 /* if IO mem was not protected do nothing */
2331 if( c->io_mem_addr == 0)
2332 return;
2333 release_region(c->io_mem_addr, c->io_mem_length);
2334 c->io_mem_addr = 0;
2335 c->io_mem_length = 0;
2338 static int find_PCI_BAR_index(struct pci_dev *pdev,
2339 unsigned long pci_bar_addr)
2341 int i, offset, mem_type, bar_type;
2342 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
2343 return 0;
2344 offset = 0;
2345 for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
2346 bar_type = pci_resource_flags(pdev, i) &
2347 PCI_BASE_ADDRESS_SPACE;
2348 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
2349 offset += 4;
2350 else {
2351 mem_type = pci_resource_flags(pdev, i) &
2352 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
2353 switch (mem_type) {
2354 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2355 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
2356 offset += 4; /* 32 bit */
2357 break;
2358 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2359 offset += 8;
2360 break;
2361 default: /* reserved in PCI 2.2 */
2362 printk(KERN_WARNING "Base address is invalid\n");
2363 return -1;
2364 break;
2367 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
2368 return i+1;
2370 return -1;
2373 static int cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
2375 ushort subsystem_vendor_id, subsystem_device_id, command;
2376 __u32 board_id, scratchpad = 0;
2377 __u64 cfg_offset;
2378 __u32 cfg_base_addr;
2379 __u64 cfg_base_addr_index;
2380 int i;
2382 /* check to see if controller has been disabled */
2383 /* BEFORE trying to enable it */
2384 (void) pci_read_config_word(pdev, PCI_COMMAND,&command);
2385 if(!(command & 0x02))
2387 printk(KERN_WARNING "cciss: controller appears to be disabled\n");
2388 return(-1);
2391 if (pci_enable_device(pdev))
2393 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
2394 return( -1);
2396 if (pci_set_dma_mask(pdev, CCISS_DMA_MASK ) != 0)
2398 printk(KERN_ERR "cciss: Unable to set DMA mask\n");
2399 return(-1);
2402 subsystem_vendor_id = pdev->subsystem_vendor;
2403 subsystem_device_id = pdev->subsystem_device;
2404 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
2405 subsystem_vendor_id);
2407 /* search for our IO range so we can protect it */
2408 for(i=0; i<DEVICE_COUNT_RESOURCE; i++)
2410 /* is this an IO range */
2411 if( pci_resource_flags(pdev, i) & 0x01 ) {
2412 c->io_mem_addr = pci_resource_start(pdev, i);
2413 c->io_mem_length = pci_resource_end(pdev, i) -
2414 pci_resource_start(pdev, i) +1;
2415 #ifdef CCISS_DEBUG
2416 printk("IO value found base_addr[%d] %lx %lx\n", i,
2417 c->io_mem_addr, c->io_mem_length);
2418 #endif /* CCISS_DEBUG */
2419 /* register the IO range */
2420 if(!request_region( c->io_mem_addr,
2421 c->io_mem_length, "cciss"))
2423 printk(KERN_WARNING "cciss I/O memory range already in use addr=%lx length=%ld\n",
2424 c->io_mem_addr, c->io_mem_length);
2425 c->io_mem_addr= 0;
2426 c->io_mem_length = 0;
2428 break;
2432 #ifdef CCISS_DEBUG
2433 printk("command = %x\n", command);
2434 printk("irq = %x\n", pdev->irq);
2435 printk("board_id = %x\n", board_id);
2436 #endif /* CCISS_DEBUG */
2438 c->intr = pdev->irq;
2441 * Memory base addr is first addr , the second points to the config
2442 * table
2445 c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
2446 #ifdef CCISS_DEBUG
2447 printk("address 0 = %x\n", c->paddr);
2448 #endif /* CCISS_DEBUG */
2449 c->vaddr = remap_pci_mem(c->paddr, 200);
2451 /* Wait for the board to become ready. (PCI hotplug needs this.)
2452 * We poll for up to 120 secs, once per 100ms. */
2453 for (i=0; i < 1200; i++) {
2454 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
2455 if (scratchpad == CCISS_FIRMWARE_READY)
2456 break;
2457 set_current_state(TASK_INTERRUPTIBLE);
2458 schedule_timeout(HZ / 10); /* wait 100ms */
2460 if (scratchpad != CCISS_FIRMWARE_READY) {
2461 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
2462 return -1;
2465 /* get the address index number */
2466 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
2467 cfg_base_addr &= (__u32) 0x0000ffff;
2468 #ifdef CCISS_DEBUG
2469 printk("cfg base address = %x\n", cfg_base_addr);
2470 #endif /* CCISS_DEBUG */
2471 cfg_base_addr_index =
2472 find_PCI_BAR_index(pdev, cfg_base_addr);
2473 #ifdef CCISS_DEBUG
2474 printk("cfg base address index = %x\n", cfg_base_addr_index);
2475 #endif /* CCISS_DEBUG */
2476 if (cfg_base_addr_index == -1) {
2477 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
2478 release_io_mem(c);
2479 return -1;
2482 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
2483 #ifdef CCISS_DEBUG
2484 printk("cfg offset = %x\n", cfg_offset);
2485 #endif /* CCISS_DEBUG */
2486 c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
2487 cfg_base_addr_index) + cfg_offset,
2488 sizeof(CfgTable_struct));
2489 c->board_id = board_id;
2491 #ifdef CCISS_DEBUG
2492 print_cfg_table(c->cfgtable);
2493 #endif /* CCISS_DEBUG */
2495 for(i=0; i<NR_PRODUCTS; i++) {
2496 if (board_id == products[i].board_id) {
2497 c->product_name = products[i].product_name;
2498 c->access = *(products[i].access);
2499 break;
2502 if (i == NR_PRODUCTS) {
2503 printk(KERN_WARNING "cciss: Sorry, I don't know how"
2504 " to access the Smart Array controller %08lx\n",
2505 (unsigned long)board_id);
2506 return -1;
2508 if ( (readb(&c->cfgtable->Signature[0]) != 'C') ||
2509 (readb(&c->cfgtable->Signature[1]) != 'I') ||
2510 (readb(&c->cfgtable->Signature[2]) != 'S') ||
2511 (readb(&c->cfgtable->Signature[3]) != 'S') )
2513 printk("Does not appear to be a valid CISS config table\n");
2514 return -1;
2517 #ifdef CONFIG_X86
2519 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
2520 __u32 prefetch;
2521 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
2522 prefetch |= 0x100;
2523 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
2525 #endif
2527 #ifdef CCISS_DEBUG
2528 printk("Trying to put board into Simple mode\n");
2529 #endif /* CCISS_DEBUG */
2530 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
2531 /* Update the field, and then ring the doorbell */
2532 writel( CFGTBL_Trans_Simple,
2533 &(c->cfgtable->HostWrite.TransportRequest));
2534 writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
2536 /* under certain very rare conditions, this can take awhile.
2537 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
2538 * as we enter this code.) */
2539 for(i=0;i<MAX_CONFIG_WAIT;i++) {
2540 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
2541 break;
2542 /* delay and try again */
2543 set_current_state(TASK_INTERRUPTIBLE);
2544 schedule_timeout(10);
2547 #ifdef CCISS_DEBUG
2548 printk(KERN_DEBUG "I counter got to %d %x\n", i, readl(c->vaddr + SA5_DOORBELL));
2549 #endif /* CCISS_DEBUG */
2550 #ifdef CCISS_DEBUG
2551 print_cfg_table(c->cfgtable);
2552 #endif /* CCISS_DEBUG */
2554 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
2556 printk(KERN_WARNING "cciss: unable to get board into"
2557 " simple mode\n");
2558 return -1;
2560 return 0;
2565 * Gets information about the local volumes attached to the controller.
2567 static void cciss_getgeometry(int cntl_num)
2569 ReportLunData_struct *ld_buff;
2570 ReadCapdata_struct *size_buff;
2571 InquiryData_struct *inq_buff;
2572 int return_code;
2573 int i;
2574 int listlength = 0;
2575 __u32 lunid = 0;
2576 int block_size;
2577 int total_size;
2579 ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
2580 if (ld_buff == NULL)
2582 printk(KERN_ERR "cciss: out of memory\n");
2583 return;
2585 memset(ld_buff, 0, sizeof(ReportLunData_struct));
2586 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
2587 if (size_buff == NULL)
2589 printk(KERN_ERR "cciss: out of memory\n");
2590 kfree(ld_buff);
2591 return;
2593 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
2594 if (inq_buff == NULL)
2596 printk(KERN_ERR "cciss: out of memory\n");
2597 kfree(ld_buff);
2598 kfree(size_buff);
2599 return;
2601 /* Get the firmware version */
2602 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
2603 sizeof(InquiryData_struct), 0, 0 ,0, NULL, TYPE_CMD);
2604 if (return_code == IO_OK)
2606 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
2607 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
2608 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
2609 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
2610 } else /* send command failed */
2612 printk(KERN_WARNING "cciss: unable to determine firmware"
2613 " version of controller\n");
2615 /* Get the number of logical volumes */
2616 return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
2617 sizeof(ReportLunData_struct), 0, 0, 0, NULL, TYPE_CMD);
2619 if( return_code == IO_OK)
2621 #ifdef CCISS_DEBUG
2622 printk("LUN Data\n--------------------------\n");
2623 #endif /* CCISS_DEBUG */
2625 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
2626 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
2627 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
2628 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
2629 } else /* reading number of logical volumes failed */
2631 printk(KERN_WARNING "cciss: report logical volume"
2632 " command failed\n");
2633 listlength = 0;
2635 hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
2636 if (hba[cntl_num]->num_luns > CISS_MAX_LUN)
2638 printk(KERN_ERR "ciss: only %d number of logical volumes supported\n",
2639 CISS_MAX_LUN);
2640 hba[cntl_num]->num_luns = CISS_MAX_LUN;
2642 #ifdef CCISS_DEBUG
2643 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
2644 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
2645 ld_buff->LUNListLength[3], hba[cntl_num]->num_luns);
2646 #endif /* CCISS_DEBUG */
2648 hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns-1;
2649 for(i=0; i< hba[cntl_num]->num_luns; i++)
2652 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
2653 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
2654 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
2655 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
2657 hba[cntl_num]->drv[i].LunID = lunid;
2660 #ifdef CCISS_DEBUG
2661 printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
2662 ld_buff->LUN[i][0], ld_buff->LUN[i][1],ld_buff->LUN[i][2],
2663 ld_buff->LUN[i][3], hba[cntl_num]->drv[i].LunID);
2664 #endif /* CCISS_DEBUG */
2665 cciss_read_capacity(cntl_num, i, size_buff, 0,
2666 &total_size, &block_size);
2667 cciss_geometry_inquiry(cntl_num, i, 0, total_size, block_size,
2668 inq_buff, &hba[cntl_num]->drv[i]);
2670 kfree(ld_buff);
2671 kfree(size_buff);
2672 kfree(inq_buff);
2675 /* Function to find the first free pointer into our hba[] array */
2676 /* Returns -1 if no free entries are left. */
2677 static int alloc_cciss_hba(void)
2679 struct gendisk *disk[NWD];
2680 int i, n;
2681 for (n = 0; n < NWD; n++) {
2682 disk[n] = alloc_disk(1 << NWD_SHIFT);
2683 if (!disk[n])
2684 goto out;
2687 for(i=0; i< MAX_CTLR; i++) {
2688 if (!hba[i]) {
2689 ctlr_info_t *p;
2690 p = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
2691 if (!p)
2692 goto Enomem;
2693 memset(p, 0, sizeof(ctlr_info_t));
2694 for (n = 0; n < NWD; n++)
2695 p->gendisk[n] = disk[n];
2696 hba[i] = p;
2697 return i;
2700 printk(KERN_WARNING "cciss: This driver supports a maximum"
2701 " of %d controllers.\n", MAX_CTLR);
2702 goto out;
2703 Enomem:
2704 printk(KERN_ERR "cciss: out of memory.\n");
2705 out:
2706 while (n--)
2707 put_disk(disk[n]);
2708 return -1;
2711 static void free_hba(int i)
2713 ctlr_info_t *p = hba[i];
2714 int n;
2716 hba[i] = NULL;
2717 for (n = 0; n < NWD; n++)
2718 put_disk(p->gendisk[n]);
2719 kfree(p);
2723 * This is it. Find all the controllers and register them. I really hate
2724 * stealing all these major device numbers.
2725 * returns the number of block devices registered.
2727 static int __devinit cciss_init_one(struct pci_dev *pdev,
2728 const struct pci_device_id *ent)
2730 request_queue_t *q;
2731 int i;
2732 int j;
2733 int rc;
2735 printk(KERN_DEBUG "cciss: Device 0x%x has been found at"
2736 " bus %d dev %d func %d\n",
2737 pdev->device, pdev->bus->number, PCI_SLOT(pdev->devfn),
2738 PCI_FUNC(pdev->devfn));
2739 i = alloc_cciss_hba();
2740 if(i < 0)
2741 return (-1);
2742 if (cciss_pci_init(hba[i], pdev) != 0)
2743 goto clean1;
2745 sprintf(hba[i]->devname, "cciss%d", i);
2746 hba[i]->ctlr = i;
2747 hba[i]->pdev = pdev;
2749 /* configure PCI DMA stuff */
2750 if (!pci_set_dma_mask(pdev, 0xffffffffffffffffULL))
2751 printk("cciss: using DAC cycles\n");
2752 else if (!pci_set_dma_mask(pdev, 0xffffffff))
2753 printk("cciss: not using DAC cycles\n");
2754 else {
2755 printk("cciss: no suitable DMA available\n");
2756 goto clean1;
2760 * register with the major number, or get a dynamic major number
2761 * by passing 0 as argument. This is done for greater than
2762 * 8 controller support.
2764 if (i < MAX_CTLR_ORIG)
2765 hba[i]->major = MAJOR_NR + i;
2766 rc = register_blkdev(hba[i]->major, hba[i]->devname);
2767 if(rc == -EBUSY || rc == -EINVAL) {
2768 printk(KERN_ERR
2769 "cciss: Unable to get major number %d for %s "
2770 "on hba %d\n", hba[i]->major, hba[i]->devname, i);
2771 goto clean1;
2773 else {
2774 if (i >= MAX_CTLR_ORIG)
2775 hba[i]->major = rc;
2778 /* make sure the board interrupts are off */
2779 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
2780 if( request_irq(hba[i]->intr, do_cciss_intr,
2781 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
2782 hba[i]->devname, hba[i])) {
2783 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
2784 hba[i]->intr, hba[i]->devname);
2785 goto clean2;
2787 hba[i]->cmd_pool_bits = kmalloc(((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long), GFP_KERNEL);
2788 hba[i]->cmd_pool = (CommandList_struct *)pci_alloc_consistent(
2789 hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
2790 &(hba[i]->cmd_pool_dhandle));
2791 hba[i]->errinfo_pool = (ErrorInfo_struct *)pci_alloc_consistent(
2792 hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
2793 &(hba[i]->errinfo_pool_dhandle));
2794 if((hba[i]->cmd_pool_bits == NULL)
2795 || (hba[i]->cmd_pool == NULL)
2796 || (hba[i]->errinfo_pool == NULL)) {
2797 printk( KERN_ERR "cciss: out of memory");
2798 goto clean4;
2801 spin_lock_init(&hba[i]->lock);
2802 q = blk_init_queue(do_cciss_request, &hba[i]->lock);
2803 if (!q)
2804 goto clean4;
2806 q->backing_dev_info.ra_pages = READ_AHEAD;
2807 hba[i]->queue = q;
2808 q->queuedata = hba[i];
2810 /* Initialize the pdev driver private data.
2811 have it point to hba[i]. */
2812 pci_set_drvdata(pdev, hba[i]);
2813 /* command and error info recs zeroed out before
2814 they are used */
2815 memset(hba[i]->cmd_pool_bits, 0, ((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long));
2817 #ifdef CCISS_DEBUG
2818 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n",i);
2819 #endif /* CCISS_DEBUG */
2821 cciss_getgeometry(i);
2823 cciss_scsi_setup(i);
2825 /* Turn the interrupts on so we can service requests */
2826 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
2828 cciss_procinit(i);
2830 blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
2832 /* This is a hardware imposed limit. */
2833 blk_queue_max_hw_segments(q, MAXSGENTRIES);
2835 /* This is a limit in the driver and could be eliminated. */
2836 blk_queue_max_phys_segments(q, MAXSGENTRIES);
2838 blk_queue_max_sectors(q, 512);
2841 for(j=0; j<NWD; j++) {
2842 drive_info_struct *drv = &(hba[i]->drv[j]);
2843 struct gendisk *disk = hba[i]->gendisk[j];
2845 sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
2846 sprintf(disk->devfs_name, "cciss/host%d/target%d", i, j);
2847 disk->major = hba[i]->major;
2848 disk->first_minor = j << NWD_SHIFT;
2849 disk->fops = &cciss_fops;
2850 disk->queue = hba[i]->queue;
2851 disk->private_data = drv;
2852 /* we must register the controller even if no disks exist */
2853 /* this is for the online array utilities */
2854 if(!drv->heads && j)
2855 continue;
2856 blk_queue_hardsect_size(hba[i]->queue, drv->block_size);
2857 set_capacity(disk, drv->nr_blocks);
2858 add_disk(disk);
2860 return(1);
2862 clean4:
2863 if(hba[i]->cmd_pool_bits)
2864 kfree(hba[i]->cmd_pool_bits);
2865 if(hba[i]->cmd_pool)
2866 pci_free_consistent(hba[i]->pdev,
2867 NR_CMDS * sizeof(CommandList_struct),
2868 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
2869 if(hba[i]->errinfo_pool)
2870 pci_free_consistent(hba[i]->pdev,
2871 NR_CMDS * sizeof( ErrorInfo_struct),
2872 hba[i]->errinfo_pool,
2873 hba[i]->errinfo_pool_dhandle);
2874 free_irq(hba[i]->intr, hba[i]);
2875 clean2:
2876 unregister_blkdev(hba[i]->major, hba[i]->devname);
2877 clean1:
2878 release_io_mem(hba[i]);
2879 free_hba(i);
2880 return(-1);
2883 static void __devexit cciss_remove_one (struct pci_dev *pdev)
2885 ctlr_info_t *tmp_ptr;
2886 int i, j;
2887 char flush_buf[4];
2888 int return_code;
2890 if (pci_get_drvdata(pdev) == NULL)
2892 printk( KERN_ERR "cciss: Unable to remove device \n");
2893 return;
2895 tmp_ptr = pci_get_drvdata(pdev);
2896 i = tmp_ptr->ctlr;
2897 if (hba[i] == NULL)
2899 printk(KERN_ERR "cciss: device appears to "
2900 "already be removed \n");
2901 return;
2903 /* Turn board interrupts off and send the flush cache command */
2904 /* sendcmd will turn off interrupt, and send the flush...
2905 * To write all data in the battery backed cache to disks */
2906 memset(flush_buf, 0, 4);
2907 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
2908 TYPE_CMD);
2909 if(return_code != IO_OK)
2911 printk(KERN_WARNING "Error Flushing cache on controller %d\n",
2914 free_irq(hba[i]->intr, hba[i]);
2915 pci_set_drvdata(pdev, NULL);
2916 iounmap(hba[i]->vaddr);
2917 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
2918 unregister_blkdev(hba[i]->major, hba[i]->devname);
2919 remove_proc_entry(hba[i]->devname, proc_cciss);
2921 /* remove it from the disk list */
2922 for (j = 0; j < NWD; j++) {
2923 struct gendisk *disk = hba[i]->gendisk[j];
2924 if (disk->flags & GENHD_FL_UP)
2925 del_gendisk(disk);
2928 blk_cleanup_queue(hba[i]->queue);
2929 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
2930 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
2931 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
2932 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
2933 kfree(hba[i]->cmd_pool_bits);
2934 release_io_mem(hba[i]);
2935 free_hba(i);
2938 static struct pci_driver cciss_pci_driver = {
2939 .name = "cciss",
2940 .probe = cciss_init_one,
2941 .remove = __devexit_p(cciss_remove_one),
2942 .id_table = cciss_pci_device_id, /* id_table */
2946 * This is it. Register the PCI driver information for the cards we control
2947 * the OS will call our registered routines when it finds one of our cards.
2949 static int __init cciss_init(void)
2951 printk(KERN_INFO DRIVER_NAME "\n");
2953 /* Register for our PCI devices */
2954 return pci_module_init(&cciss_pci_driver);
2957 static void __exit cciss_cleanup(void)
2959 int i;
2961 pci_unregister_driver(&cciss_pci_driver);
2962 /* double check that all controller entrys have been removed */
2963 for (i=0; i< MAX_CTLR; i++)
2965 if (hba[i] != NULL)
2967 printk(KERN_WARNING "cciss: had to remove"
2968 " controller %d\n", i);
2969 cciss_remove_one(hba[i]->pdev);
2972 remove_proc_entry("cciss", proc_root_driver);
2975 module_init(cciss_init);
2976 module_exit(cciss_cleanup);