2 * Disk Array driver for HP SA 5xxx and 6xxx Controllers
3 * Copyright 2000, 2006 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/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/types.h>
26 #include <linux/pci.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/delay.h>
30 #include <linux/major.h>
32 #include <linux/bio.h>
33 #include <linux/blkpg.h>
34 #include <linux/timer.h>
35 #include <linux/proc_fs.h>
36 #include <linux/init.h>
37 #include <linux/hdreg.h>
38 #include <linux/spinlock.h>
39 #include <linux/compat.h>
40 #include <linux/blktrace_api.h>
41 #include <asm/uaccess.h>
44 #include <linux/dma-mapping.h>
45 #include <linux/blkdev.h>
46 #include <linux/genhd.h>
47 #include <linux/completion.h>
49 #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
50 #define DRIVER_NAME "HP CISS Driver (v 3.6.14)"
51 #define DRIVER_VERSION CCISS_DRIVER_VERSION(3,6,14)
53 /* Embedded module documentation macros - see modules.h */
54 MODULE_AUTHOR("Hewlett-Packard Company");
55 MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 3.6.14");
56 MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
57 " SA6i P600 P800 P400 P400i E200 E200i E500");
58 MODULE_VERSION("3.6.14");
59 MODULE_LICENSE("GPL");
60 MODULE_VERSION("2.6.8");
62 #include "cciss_cmd.h"
64 #include <linux/cciss_ioctl.h>
66 /* define the PCI info for the cards we can control */
67 static const struct pci_device_id cciss_pci_device_id[] = {
68 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS, 0x0E11, 0x4070},
69 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4080},
70 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4082},
71 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4083},
72 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x4091},
73 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409A},
74 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409B},
75 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409C},
76 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409D},
77 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA, 0x103C, 0x3225},
78 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3223},
79 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3234},
80 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3235},
81 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3211},
82 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3212},
83 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3213},
84 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3214},
85 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3215},
86 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3237},
87 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
88 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
92 MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
94 /* board_id = Subsystem Device ID & Vendor ID
95 * product = Marketing Name for the board
96 * access = Address of the struct of function pointers
97 * nr_cmds = Number of commands supported by controller
99 static struct board_type products[] = {
100 {0x40700E11, "Smart Array 5300", &SA5_access, 512},
101 {0x40800E11, "Smart Array 5i", &SA5B_access, 512},
102 {0x40820E11, "Smart Array 532", &SA5B_access, 512},
103 {0x40830E11, "Smart Array 5312", &SA5B_access, 512},
104 {0x409A0E11, "Smart Array 641", &SA5_access, 512},
105 {0x409B0E11, "Smart Array 642", &SA5_access, 512},
106 {0x409C0E11, "Smart Array 6400", &SA5_access, 512},
107 {0x409D0E11, "Smart Array 6400 EM", &SA5_access, 512},
108 {0x40910E11, "Smart Array 6i", &SA5_access, 512},
109 {0x3225103C, "Smart Array P600", &SA5_access, 512},
110 {0x3223103C, "Smart Array P800", &SA5_access, 512},
111 {0x3234103C, "Smart Array P400", &SA5_access, 512},
112 {0x3235103C, "Smart Array P400i", &SA5_access, 512},
113 {0x3211103C, "Smart Array E200i", &SA5_access, 120},
114 {0x3212103C, "Smart Array E200", &SA5_access, 120},
115 {0x3213103C, "Smart Array E200i", &SA5_access, 120},
116 {0x3214103C, "Smart Array E200i", &SA5_access, 120},
117 {0x3215103C, "Smart Array E200i", &SA5_access, 120},
118 {0x3237103C, "Smart Array E500", &SA5_access, 512},
119 {0xFFFF103C, "Unknown Smart Array", &SA5_access, 120},
122 /* How long to wait (in milliseconds) for board to go into simple mode */
123 #define MAX_CONFIG_WAIT 30000
124 #define MAX_IOCTL_CONFIG_WAIT 1000
126 /*define how many times we will try a command because of bus resets */
127 #define MAX_CMD_RETRIES 3
129 #define READ_AHEAD 1024
132 /* Originally cciss driver only supports 8 major numbers */
133 #define MAX_CTLR_ORIG 8
135 static ctlr_info_t *hba[MAX_CTLR];
137 static void do_cciss_request(request_queue_t *q);
138 static irqreturn_t do_cciss_intr(int irq, void *dev_id);
139 static int cciss_open(struct inode *inode, struct file *filep);
140 static int cciss_release(struct inode *inode, struct file *filep);
141 static int cciss_ioctl(struct inode *inode, struct file *filep,
142 unsigned int cmd, unsigned long arg);
143 static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo);
145 static int cciss_revalidate(struct gendisk *disk);
146 static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk);
147 static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
150 static void cciss_read_capacity(int ctlr, int logvol, int withirq,
151 sector_t *total_size, unsigned int *block_size);
152 static void cciss_read_capacity_16(int ctlr, int logvol, int withirq,
153 sector_t *total_size, unsigned int *block_size);
154 static void cciss_geometry_inquiry(int ctlr, int logvol,
155 int withirq, sector_t total_size,
156 unsigned int block_size, InquiryData_struct *inq_buff,
157 drive_info_struct *drv);
158 static void cciss_getgeometry(int cntl_num);
159 static void __devinit cciss_interrupt_mode(ctlr_info_t *, struct pci_dev *,
161 static void start_io(ctlr_info_t *h);
162 static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size,
163 unsigned int use_unit_num, unsigned int log_unit,
164 __u8 page_code, unsigned char *scsi3addr, int cmd_type);
165 static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
166 unsigned int use_unit_num, unsigned int log_unit,
167 __u8 page_code, int cmd_type);
169 static void fail_all_cmds(unsigned long ctlr);
171 #ifdef CONFIG_PROC_FS
172 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
173 int length, int *eof, void *data);
174 static void cciss_procinit(int i);
176 static void cciss_procinit(int i)
179 #endif /* CONFIG_PROC_FS */
182 static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg);
185 static struct block_device_operations cciss_fops = {
186 .owner = THIS_MODULE,
188 .release = cciss_release,
189 .ioctl = cciss_ioctl,
190 .getgeo = cciss_getgeo,
192 .compat_ioctl = cciss_compat_ioctl,
194 .revalidate_disk = cciss_revalidate,
198 * Enqueuing and dequeuing functions for cmdlists.
200 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
204 c->next = c->prev = c;
206 c->prev = (*Qptr)->prev;
208 (*Qptr)->prev->next = c;
213 static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
214 CommandList_struct *c)
216 if (c && c->next != c) {
219 c->prev->next = c->next;
220 c->next->prev = c->prev;
227 #include "cciss_scsi.c" /* For SCSI tape support */
229 #define RAID_UNKNOWN 6
231 #ifdef CONFIG_PROC_FS
234 * Report information about this controller.
236 #define ENG_GIG 1000000000
237 #define ENG_GIG_FACTOR (ENG_GIG/512)
238 static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
242 static struct proc_dir_entry *proc_cciss;
244 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
245 int length, int *eof, void *data)
250 ctlr_info_t *h = (ctlr_info_t *) data;
251 drive_info_struct *drv;
253 sector_t vol_sz, vol_sz_frac;
257 /* prevent displaying bogus info during configuration
258 * or deconfiguration of a logical volume
260 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
261 if (h->busy_configuring) {
262 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
265 h->busy_configuring = 1;
266 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
268 size = sprintf(buffer, "%s: HP %s Controller\n"
269 "Board ID: 0x%08lx\n"
270 "Firmware Version: %c%c%c%c\n"
272 "Logical drives: %d\n"
274 "Current Q depth: %d\n"
275 "Current # commands on controller: %d\n"
276 "Max Q depth since init: %d\n"
277 "Max # commands on controller since init: %d\n"
278 "Max SG entries since init: %d\n\n",
281 (unsigned long)h->board_id,
282 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
283 h->firm_ver[3], (unsigned int)h->intr[SIMPLE_MODE_INT],
285 h->cciss_max_sectors,
286 h->Qdepth, h->commands_outstanding,
287 h->maxQsinceinit, h->max_outstanding, h->maxSG);
291 cciss_proc_tape_report(ctlr, buffer, &pos, &len);
292 for (i = 0; i <= h->highest_lun; i++) {
298 vol_sz = drv->nr_blocks;
299 vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
301 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
303 if (drv->raid_level > 5)
304 drv->raid_level = RAID_UNKNOWN;
305 size = sprintf(buffer + len, "cciss/c%dd%d:"
306 "\t%4u.%02uGB\tRAID %s\n",
307 ctlr, i, (int)vol_sz, (int)vol_sz_frac,
308 raid_label[drv->raid_level]);
314 *start = buffer + offset;
318 h->busy_configuring = 0;
323 cciss_proc_write(struct file *file, const char __user *buffer,
324 unsigned long count, void *data)
326 unsigned char cmd[80];
328 #ifdef CONFIG_CISS_SCSI_TAPE
329 ctlr_info_t *h = (ctlr_info_t *) data;
333 if (count > sizeof(cmd) - 1)
335 if (copy_from_user(cmd, buffer, count))
338 len = strlen(cmd); // above 3 lines ensure safety
339 if (len && cmd[len - 1] == '\n')
341 # ifdef CONFIG_CISS_SCSI_TAPE
342 if (strcmp("engage scsi", cmd) == 0) {
343 rc = cciss_engage_scsi(h->ctlr);
348 /* might be nice to have "disengage" too, but it's not
349 safely possible. (only 1 module use count, lock issues.) */
355 * Get us a file in /proc/cciss that says something about each controller.
356 * Create /proc/cciss if it doesn't exist yet.
358 static void __devinit cciss_procinit(int i)
360 struct proc_dir_entry *pde;
362 if (proc_cciss == NULL) {
363 proc_cciss = proc_mkdir("cciss", proc_root_driver);
368 pde = create_proc_read_entry(hba[i]->devname,
369 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
370 proc_cciss, cciss_proc_get_info, hba[i]);
371 pde->write_proc = cciss_proc_write;
373 #endif /* CONFIG_PROC_FS */
376 * For operations that cannot sleep, a command block is allocated at init,
377 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
378 * which ones are free or in use. For operations that can wait for kmalloc
379 * to possible sleep, this routine can be called with get_from_pool set to 0.
380 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
382 static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool)
384 CommandList_struct *c;
387 dma_addr_t cmd_dma_handle, err_dma_handle;
389 if (!get_from_pool) {
390 c = (CommandList_struct *) pci_alloc_consistent(h->pdev,
391 sizeof(CommandList_struct), &cmd_dma_handle);
394 memset(c, 0, sizeof(CommandList_struct));
398 c->err_info = (ErrorInfo_struct *)
399 pci_alloc_consistent(h->pdev, sizeof(ErrorInfo_struct),
402 if (c->err_info == NULL) {
403 pci_free_consistent(h->pdev,
404 sizeof(CommandList_struct), c, cmd_dma_handle);
407 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
408 } else { /* get it out of the controllers pool */
411 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
414 } while (test_and_set_bit
415 (i & (BITS_PER_LONG - 1),
416 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
418 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
421 memset(c, 0, sizeof(CommandList_struct));
422 cmd_dma_handle = h->cmd_pool_dhandle
423 + i * sizeof(CommandList_struct);
424 c->err_info = h->errinfo_pool + i;
425 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
426 err_dma_handle = h->errinfo_pool_dhandle
427 + i * sizeof(ErrorInfo_struct);
433 c->busaddr = (__u32) cmd_dma_handle;
434 temp64.val = (__u64) err_dma_handle;
435 c->ErrDesc.Addr.lower = temp64.val32.lower;
436 c->ErrDesc.Addr.upper = temp64.val32.upper;
437 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
444 * Frees a command block that was previously allocated with cmd_alloc().
446 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
451 if (!got_from_pool) {
452 temp64.val32.lower = c->ErrDesc.Addr.lower;
453 temp64.val32.upper = c->ErrDesc.Addr.upper;
454 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
455 c->err_info, (dma_addr_t) temp64.val);
456 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
457 c, (dma_addr_t) c->busaddr);
460 clear_bit(i & (BITS_PER_LONG - 1),
461 h->cmd_pool_bits + (i / BITS_PER_LONG));
466 static inline ctlr_info_t *get_host(struct gendisk *disk)
468 return disk->queue->queuedata;
471 static inline drive_info_struct *get_drv(struct gendisk *disk)
473 return disk->private_data;
477 * Open. Make sure the device is really there.
479 static int cciss_open(struct inode *inode, struct file *filep)
481 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
482 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
485 printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
486 #endif /* CCISS_DEBUG */
488 if (host->busy_initializing || drv->busy_configuring)
491 * Root is allowed to open raw volume zero even if it's not configured
492 * so array config can still work. Root is also allowed to open any
493 * volume that has a LUN ID, so it can issue IOCTL to reread the
494 * disk information. I don't think I really like this
495 * but I'm already using way to many device nodes to claim another one
496 * for "raw controller".
498 if (drv->heads == 0) {
499 if (iminor(inode) != 0) { /* not node 0? */
500 /* if not node 0 make sure it is a partition = 0 */
501 if (iminor(inode) & 0x0f) {
503 /* if it is, make sure we have a LUN ID */
504 } else if (drv->LunID == 0) {
508 if (!capable(CAP_SYS_ADMIN))
519 static int cciss_release(struct inode *inode, struct file *filep)
521 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
522 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
525 printk(KERN_DEBUG "cciss_release %s\n",
526 inode->i_bdev->bd_disk->disk_name);
527 #endif /* CCISS_DEBUG */
536 static int do_ioctl(struct file *f, unsigned cmd, unsigned long arg)
540 ret = cciss_ioctl(f->f_path.dentry->d_inode, f, cmd, arg);
545 static int cciss_ioctl32_passthru(struct file *f, unsigned cmd,
547 static int cciss_ioctl32_big_passthru(struct file *f, unsigned cmd,
550 static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg)
553 case CCISS_GETPCIINFO:
554 case CCISS_GETINTINFO:
555 case CCISS_SETINTINFO:
556 case CCISS_GETNODENAME:
557 case CCISS_SETNODENAME:
558 case CCISS_GETHEARTBEAT:
559 case CCISS_GETBUSTYPES:
560 case CCISS_GETFIRMVER:
561 case CCISS_GETDRIVVER:
562 case CCISS_REVALIDVOLS:
563 case CCISS_DEREGDISK:
564 case CCISS_REGNEWDISK:
566 case CCISS_RESCANDISK:
567 case CCISS_GETLUNINFO:
568 return do_ioctl(f, cmd, arg);
570 case CCISS_PASSTHRU32:
571 return cciss_ioctl32_passthru(f, cmd, arg);
572 case CCISS_BIG_PASSTHRU32:
573 return cciss_ioctl32_big_passthru(f, cmd, arg);
580 static int cciss_ioctl32_passthru(struct file *f, unsigned cmd,
583 IOCTL32_Command_struct __user *arg32 =
584 (IOCTL32_Command_struct __user *) arg;
585 IOCTL_Command_struct arg64;
586 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
592 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
593 sizeof(arg64.LUN_info));
595 copy_from_user(&arg64.Request, &arg32->Request,
596 sizeof(arg64.Request));
598 copy_from_user(&arg64.error_info, &arg32->error_info,
599 sizeof(arg64.error_info));
600 err |= get_user(arg64.buf_size, &arg32->buf_size);
601 err |= get_user(cp, &arg32->buf);
602 arg64.buf = compat_ptr(cp);
603 err |= copy_to_user(p, &arg64, sizeof(arg64));
608 err = do_ioctl(f, CCISS_PASSTHRU, (unsigned long)p);
612 copy_in_user(&arg32->error_info, &p->error_info,
613 sizeof(arg32->error_info));
619 static int cciss_ioctl32_big_passthru(struct file *file, unsigned cmd,
622 BIG_IOCTL32_Command_struct __user *arg32 =
623 (BIG_IOCTL32_Command_struct __user *) arg;
624 BIG_IOCTL_Command_struct arg64;
625 BIG_IOCTL_Command_struct __user *p =
626 compat_alloc_user_space(sizeof(arg64));
632 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
633 sizeof(arg64.LUN_info));
635 copy_from_user(&arg64.Request, &arg32->Request,
636 sizeof(arg64.Request));
638 copy_from_user(&arg64.error_info, &arg32->error_info,
639 sizeof(arg64.error_info));
640 err |= get_user(arg64.buf_size, &arg32->buf_size);
641 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
642 err |= get_user(cp, &arg32->buf);
643 arg64.buf = compat_ptr(cp);
644 err |= copy_to_user(p, &arg64, sizeof(arg64));
649 err = do_ioctl(file, CCISS_BIG_PASSTHRU, (unsigned long)p);
653 copy_in_user(&arg32->error_info, &p->error_info,
654 sizeof(arg32->error_info));
661 static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo)
663 drive_info_struct *drv = get_drv(bdev->bd_disk);
668 geo->heads = drv->heads;
669 geo->sectors = drv->sectors;
670 geo->cylinders = drv->cylinders;
677 static int cciss_ioctl(struct inode *inode, struct file *filep,
678 unsigned int cmd, unsigned long arg)
680 struct block_device *bdev = inode->i_bdev;
681 struct gendisk *disk = bdev->bd_disk;
682 ctlr_info_t *host = get_host(disk);
683 drive_info_struct *drv = get_drv(disk);
684 int ctlr = host->ctlr;
685 void __user *argp = (void __user *)arg;
688 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
689 #endif /* CCISS_DEBUG */
692 case CCISS_GETPCIINFO:
694 cciss_pci_info_struct pciinfo;
698 pciinfo.domain = pci_domain_nr(host->pdev->bus);
699 pciinfo.bus = host->pdev->bus->number;
700 pciinfo.dev_fn = host->pdev->devfn;
701 pciinfo.board_id = host->board_id;
703 (argp, &pciinfo, sizeof(cciss_pci_info_struct)))
707 case CCISS_GETINTINFO:
709 cciss_coalint_struct intinfo;
713 readl(&host->cfgtable->HostWrite.CoalIntDelay);
715 readl(&host->cfgtable->HostWrite.CoalIntCount);
717 (argp, &intinfo, sizeof(cciss_coalint_struct)))
721 case CCISS_SETINTINFO:
723 cciss_coalint_struct intinfo;
729 if (!capable(CAP_SYS_ADMIN))
732 (&intinfo, argp, sizeof(cciss_coalint_struct)))
734 if ((intinfo.delay == 0) && (intinfo.count == 0))
736 // printk("cciss_ioctl: delay and count cannot be 0\n");
739 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
740 /* Update the field, and then ring the doorbell */
741 writel(intinfo.delay,
742 &(host->cfgtable->HostWrite.CoalIntDelay));
743 writel(intinfo.count,
744 &(host->cfgtable->HostWrite.CoalIntCount));
745 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
747 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
748 if (!(readl(host->vaddr + SA5_DOORBELL)
751 /* delay and try again */
754 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
755 if (i >= MAX_IOCTL_CONFIG_WAIT)
759 case CCISS_GETNODENAME:
761 NodeName_type NodeName;
766 for (i = 0; i < 16; i++)
768 readb(&host->cfgtable->ServerName[i]);
769 if (copy_to_user(argp, NodeName, sizeof(NodeName_type)))
773 case CCISS_SETNODENAME:
775 NodeName_type NodeName;
781 if (!capable(CAP_SYS_ADMIN))
785 (NodeName, argp, sizeof(NodeName_type)))
788 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
790 /* Update the field, and then ring the doorbell */
791 for (i = 0; i < 16; i++)
793 &host->cfgtable->ServerName[i]);
795 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
797 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
798 if (!(readl(host->vaddr + SA5_DOORBELL)
801 /* delay and try again */
804 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
805 if (i >= MAX_IOCTL_CONFIG_WAIT)
810 case CCISS_GETHEARTBEAT:
812 Heartbeat_type heartbeat;
816 heartbeat = readl(&host->cfgtable->HeartBeat);
818 (argp, &heartbeat, sizeof(Heartbeat_type)))
822 case CCISS_GETBUSTYPES:
824 BusTypes_type BusTypes;
828 BusTypes = readl(&host->cfgtable->BusTypes);
830 (argp, &BusTypes, sizeof(BusTypes_type)))
834 case CCISS_GETFIRMVER:
836 FirmwareVer_type firmware;
840 memcpy(firmware, host->firm_ver, 4);
843 (argp, firmware, sizeof(FirmwareVer_type)))
847 case CCISS_GETDRIVVER:
849 DriverVer_type DriverVer = DRIVER_VERSION;
855 (argp, &DriverVer, sizeof(DriverVer_type)))
860 case CCISS_REVALIDVOLS:
861 return rebuild_lun_table(host, NULL);
863 case CCISS_GETLUNINFO:{
864 LogvolInfo_struct luninfo;
866 luninfo.LunID = drv->LunID;
867 luninfo.num_opens = drv->usage_count;
868 luninfo.num_parts = 0;
869 if (copy_to_user(argp, &luninfo,
870 sizeof(LogvolInfo_struct)))
874 case CCISS_DEREGDISK:
875 return rebuild_lun_table(host, disk);
878 return rebuild_lun_table(host, NULL);
882 IOCTL_Command_struct iocommand;
883 CommandList_struct *c;
887 DECLARE_COMPLETION_ONSTACK(wait);
892 if (!capable(CAP_SYS_RAWIO))
896 (&iocommand, argp, sizeof(IOCTL_Command_struct)))
898 if ((iocommand.buf_size < 1) &&
899 (iocommand.Request.Type.Direction != XFER_NONE)) {
902 #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
903 /* Check kmalloc limits */
904 if (iocommand.buf_size > 128000)
907 if (iocommand.buf_size > 0) {
908 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
912 if (iocommand.Request.Type.Direction == XFER_WRITE) {
913 /* Copy the data into the buffer we created */
915 (buff, iocommand.buf, iocommand.buf_size)) {
920 memset(buff, 0, iocommand.buf_size);
922 if ((c = cmd_alloc(host, 0)) == NULL) {
926 // Fill in the command type
927 c->cmd_type = CMD_IOCTL_PEND;
928 // Fill in Command Header
929 c->Header.ReplyQueue = 0; // unused in simple mode
930 if (iocommand.buf_size > 0) // buffer to fill
932 c->Header.SGList = 1;
933 c->Header.SGTotal = 1;
934 } else // no buffers to fill
936 c->Header.SGList = 0;
937 c->Header.SGTotal = 0;
939 c->Header.LUN = iocommand.LUN_info;
940 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
942 // Fill in Request block
943 c->Request = iocommand.Request;
945 // Fill in the scatter gather information
946 if (iocommand.buf_size > 0) {
947 temp64.val = pci_map_single(host->pdev, buff,
949 PCI_DMA_BIDIRECTIONAL);
950 c->SG[0].Addr.lower = temp64.val32.lower;
951 c->SG[0].Addr.upper = temp64.val32.upper;
952 c->SG[0].Len = iocommand.buf_size;
953 c->SG[0].Ext = 0; // we are not chaining
957 /* Put the request on the tail of the request queue */
958 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
959 addQ(&host->reqQ, c);
962 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
964 wait_for_completion(&wait);
966 /* unlock the buffers from DMA */
967 temp64.val32.lower = c->SG[0].Addr.lower;
968 temp64.val32.upper = c->SG[0].Addr.upper;
969 pci_unmap_single(host->pdev, (dma_addr_t) temp64.val,
971 PCI_DMA_BIDIRECTIONAL);
973 /* Copy the error information out */
974 iocommand.error_info = *(c->err_info);
976 (argp, &iocommand, sizeof(IOCTL_Command_struct))) {
978 cmd_free(host, c, 0);
982 if (iocommand.Request.Type.Direction == XFER_READ) {
983 /* Copy the data out of the buffer we created */
985 (iocommand.buf, buff, iocommand.buf_size)) {
987 cmd_free(host, c, 0);
992 cmd_free(host, c, 0);
995 case CCISS_BIG_PASSTHRU:{
996 BIG_IOCTL_Command_struct *ioc;
997 CommandList_struct *c;
998 unsigned char **buff = NULL;
999 int *buff_size = NULL;
1001 unsigned long flags;
1005 DECLARE_COMPLETION_ONSTACK(wait);
1008 BYTE __user *data_ptr;
1012 if (!capable(CAP_SYS_RAWIO))
1014 ioc = (BIG_IOCTL_Command_struct *)
1015 kmalloc(sizeof(*ioc), GFP_KERNEL);
1020 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
1024 if ((ioc->buf_size < 1) &&
1025 (ioc->Request.Type.Direction != XFER_NONE)) {
1029 /* Check kmalloc limits using all SGs */
1030 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
1034 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
1039 kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
1044 buff_size = kmalloc(MAXSGENTRIES * sizeof(int),
1050 left = ioc->buf_size;
1051 data_ptr = ioc->buf;
1054 ioc->malloc_size) ? ioc->
1056 buff_size[sg_used] = sz;
1057 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
1058 if (buff[sg_used] == NULL) {
1062 if (ioc->Request.Type.Direction == XFER_WRITE) {
1064 (buff[sg_used], data_ptr, sz)) {
1069 memset(buff[sg_used], 0, sz);
1075 if ((c = cmd_alloc(host, 0)) == NULL) {
1079 c->cmd_type = CMD_IOCTL_PEND;
1080 c->Header.ReplyQueue = 0;
1082 if (ioc->buf_size > 0) {
1083 c->Header.SGList = sg_used;
1084 c->Header.SGTotal = sg_used;
1086 c->Header.SGList = 0;
1087 c->Header.SGTotal = 0;
1089 c->Header.LUN = ioc->LUN_info;
1090 c->Header.Tag.lower = c->busaddr;
1092 c->Request = ioc->Request;
1093 if (ioc->buf_size > 0) {
1095 for (i = 0; i < sg_used; i++) {
1097 pci_map_single(host->pdev, buff[i],
1099 PCI_DMA_BIDIRECTIONAL);
1100 c->SG[i].Addr.lower =
1102 c->SG[i].Addr.upper =
1104 c->SG[i].Len = buff_size[i];
1105 c->SG[i].Ext = 0; /* we are not chaining */
1109 /* Put the request on the tail of the request queue */
1110 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1111 addQ(&host->reqQ, c);
1114 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1115 wait_for_completion(&wait);
1116 /* unlock the buffers from DMA */
1117 for (i = 0; i < sg_used; i++) {
1118 temp64.val32.lower = c->SG[i].Addr.lower;
1119 temp64.val32.upper = c->SG[i].Addr.upper;
1120 pci_unmap_single(host->pdev,
1121 (dma_addr_t) temp64.val, buff_size[i],
1122 PCI_DMA_BIDIRECTIONAL);
1124 /* Copy the error information out */
1125 ioc->error_info = *(c->err_info);
1126 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
1127 cmd_free(host, c, 0);
1131 if (ioc->Request.Type.Direction == XFER_READ) {
1132 /* Copy the data out of the buffer we created */
1133 BYTE __user *ptr = ioc->buf;
1134 for (i = 0; i < sg_used; i++) {
1136 (ptr, buff[i], buff_size[i])) {
1137 cmd_free(host, c, 0);
1141 ptr += buff_size[i];
1144 cmd_free(host, c, 0);
1148 for (i = 0; i < sg_used; i++)
1161 static inline void complete_buffers(struct bio *bio, int status)
1164 struct bio *xbh = bio->bi_next;
1165 int nr_sectors = bio_sectors(bio);
1167 bio->bi_next = NULL;
1168 bio_endio(bio, nr_sectors << 9, status ? 0 : -EIO);
1173 static void cciss_check_queues(ctlr_info_t *h)
1175 int start_queue = h->next_to_run;
1178 /* check to see if we have maxed out the number of commands that can
1179 * be placed on the queue. If so then exit. We do this check here
1180 * in case the interrupt we serviced was from an ioctl and did not
1181 * free any new commands.
1183 if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds)
1186 /* We have room on the queue for more commands. Now we need to queue
1187 * them up. We will also keep track of the next queue to run so
1188 * that every queue gets a chance to be started first.
1190 for (i = 0; i < h->highest_lun + 1; i++) {
1191 int curr_queue = (start_queue + i) % (h->highest_lun + 1);
1192 /* make sure the disk has been added and the drive is real
1193 * because this can be called from the middle of init_one.
1195 if (!(h->drv[curr_queue].queue) || !(h->drv[curr_queue].heads))
1197 blk_start_queue(h->gendisk[curr_queue]->queue);
1199 /* check to see if we have maxed out the number of commands
1200 * that can be placed on the queue.
1202 if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds) {
1203 if (curr_queue == start_queue) {
1205 (start_queue + 1) % (h->highest_lun + 1);
1208 h->next_to_run = curr_queue;
1212 curr_queue = (curr_queue + 1) % (h->highest_lun + 1);
1217 static void cciss_softirq_done(struct request *rq)
1219 CommandList_struct *cmd = rq->completion_data;
1220 ctlr_info_t *h = hba[cmd->ctlr];
1221 unsigned long flags;
1225 if (cmd->Request.Type.Direction == XFER_READ)
1226 ddir = PCI_DMA_FROMDEVICE;
1228 ddir = PCI_DMA_TODEVICE;
1230 /* command did not need to be retried */
1231 /* unmap the DMA mapping for all the scatter gather elements */
1232 for (i = 0; i < cmd->Header.SGList; i++) {
1233 temp64.val32.lower = cmd->SG[i].Addr.lower;
1234 temp64.val32.upper = cmd->SG[i].Addr.upper;
1235 pci_unmap_page(h->pdev, temp64.val, cmd->SG[i].Len, ddir);
1238 complete_buffers(rq->bio, rq->errors);
1240 if (blk_fs_request(rq)) {
1241 const int rw = rq_data_dir(rq);
1243 disk_stat_add(rq->rq_disk, sectors[rw], rq->nr_sectors);
1247 printk("Done with %p\n", rq);
1248 #endif /* CCISS_DEBUG */
1250 add_disk_randomness(rq->rq_disk);
1251 spin_lock_irqsave(&h->lock, flags);
1252 end_that_request_last(rq, rq->errors);
1253 cmd_free(h, cmd, 1);
1254 cciss_check_queues(h);
1255 spin_unlock_irqrestore(&h->lock, flags);
1258 /* This function will check the usage_count of the drive to be updated/added.
1259 * If the usage_count is zero then the drive information will be updated and
1260 * the disk will be re-registered with the kernel. If not then it will be
1261 * left alone for the next reboot. The exception to this is disk 0 which
1262 * will always be left registered with the kernel since it is also the
1263 * controller node. Any changes to disk 0 will show up on the next
1266 static void cciss_update_drive_info(int ctlr, int drv_index)
1268 ctlr_info_t *h = hba[ctlr];
1269 struct gendisk *disk;
1270 InquiryData_struct *inq_buff = NULL;
1271 unsigned int block_size;
1272 sector_t total_size;
1273 unsigned long flags = 0;
1276 /* if the disk already exists then deregister it before proceeding */
1277 if (h->drv[drv_index].raid_level != -1) {
1278 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
1279 h->drv[drv_index].busy_configuring = 1;
1280 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1281 ret = deregister_disk(h->gendisk[drv_index],
1282 &h->drv[drv_index], 0);
1283 h->drv[drv_index].busy_configuring = 0;
1286 /* If the disk is in use return */
1290 /* Get information about the disk and modify the driver structure */
1291 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
1292 if (inq_buff == NULL)
1295 cciss_read_capacity(ctlr, drv_index, 1,
1296 &total_size, &block_size);
1298 /* total size = last LBA + 1 */
1299 /* FFFFFFFF + 1 = 0, cannot have a logical volume of size 0 */
1300 /* so we assume this volume this must be >2TB in size */
1301 if (total_size == (__u32) 0) {
1302 cciss_read_capacity_16(ctlr, drv_index, 1,
1303 &total_size, &block_size);
1304 h->cciss_read = CCISS_READ_16;
1305 h->cciss_write = CCISS_WRITE_16;
1307 h->cciss_read = CCISS_READ_10;
1308 h->cciss_write = CCISS_WRITE_10;
1310 cciss_geometry_inquiry(ctlr, drv_index, 1, total_size, block_size,
1311 inq_buff, &h->drv[drv_index]);
1314 disk = h->gendisk[drv_index];
1315 set_capacity(disk, h->drv[drv_index].nr_blocks);
1317 /* if it's the controller it's already added */
1319 disk->queue = blk_init_queue(do_cciss_request, &h->lock);
1320 sprintf(disk->disk_name, "cciss/c%dd%d", ctlr, drv_index);
1321 disk->major = h->major;
1322 disk->first_minor = drv_index << NWD_SHIFT;
1323 disk->fops = &cciss_fops;
1324 disk->private_data = &h->drv[drv_index];
1326 /* Set up queue information */
1327 disk->queue->backing_dev_info.ra_pages = READ_AHEAD;
1328 blk_queue_bounce_limit(disk->queue, hba[ctlr]->pdev->dma_mask);
1330 /* This is a hardware imposed limit. */
1331 blk_queue_max_hw_segments(disk->queue, MAXSGENTRIES);
1333 /* This is a limit in the driver and could be eliminated. */
1334 blk_queue_max_phys_segments(disk->queue, MAXSGENTRIES);
1336 blk_queue_max_sectors(disk->queue, h->cciss_max_sectors);
1338 blk_queue_softirq_done(disk->queue, cciss_softirq_done);
1340 disk->queue->queuedata = hba[ctlr];
1342 blk_queue_hardsect_size(disk->queue,
1343 hba[ctlr]->drv[drv_index].block_size);
1345 h->drv[drv_index].queue = disk->queue;
1353 printk(KERN_ERR "cciss: out of memory\n");
1357 /* This function will find the first index of the controllers drive array
1358 * that has a -1 for the raid_level and will return that index. This is
1359 * where new drives will be added. If the index to be returned is greater
1360 * than the highest_lun index for the controller then highest_lun is set
1361 * to this new index. If there are no available indexes then -1 is returned.
1363 static int cciss_find_free_drive_index(int ctlr)
1367 for (i = 0; i < CISS_MAX_LUN; i++) {
1368 if (hba[ctlr]->drv[i].raid_level == -1) {
1369 if (i > hba[ctlr]->highest_lun)
1370 hba[ctlr]->highest_lun = i;
1377 /* This function will add and remove logical drives from the Logical
1378 * drive array of the controller and maintain persistency of ordering
1379 * so that mount points are preserved until the next reboot. This allows
1380 * for the removal of logical drives in the middle of the drive array
1381 * without a re-ordering of those drives.
1383 * h = The controller to perform the operations on
1384 * del_disk = The disk to remove if specified. If the value given
1385 * is NULL then no disk is removed.
1387 static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk)
1391 ReportLunData_struct *ld_buff = NULL;
1392 drive_info_struct *drv = NULL;
1399 unsigned long flags;
1401 /* Set busy_configuring flag for this operation */
1402 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
1403 if (h->busy_configuring) {
1404 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1407 h->busy_configuring = 1;
1409 /* if del_disk is NULL then we are being called to add a new disk
1410 * and update the logical drive table. If it is not NULL then
1411 * we will check if the disk is in use or not.
1413 if (del_disk != NULL) {
1414 drv = get_drv(del_disk);
1415 drv->busy_configuring = 1;
1416 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1417 return_code = deregister_disk(del_disk, drv, 1);
1418 drv->busy_configuring = 0;
1419 h->busy_configuring = 0;
1422 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1423 if (!capable(CAP_SYS_RAWIO))
1426 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1427 if (ld_buff == NULL)
1430 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
1431 sizeof(ReportLunData_struct), 0,
1434 if (return_code == IO_OK) {
1436 be32_to_cpu(*(__u32 *) ld_buff->LUNListLength);
1437 } else { /* reading number of logical volumes failed */
1438 printk(KERN_WARNING "cciss: report logical volume"
1439 " command failed\n");
1444 num_luns = listlength / 8; /* 8 bytes per entry */
1445 if (num_luns > CISS_MAX_LUN) {
1446 num_luns = CISS_MAX_LUN;
1447 printk(KERN_WARNING "cciss: more luns configured"
1448 " on controller than can be handled by"
1452 /* Compare controller drive array to drivers drive array.
1453 * Check for updates in the drive information and any new drives
1454 * on the controller.
1456 for (i = 0; i < num_luns; i++) {
1462 (unsigned int)(ld_buff->LUN[i][3])) << 24;
1464 (unsigned int)(ld_buff->LUN[i][2])) << 16;
1466 (unsigned int)(ld_buff->LUN[i][1])) << 8;
1467 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
1469 /* Find if the LUN is already in the drive array
1470 * of the controller. If so then update its info
1471 * if not is use. If it does not exist then find
1472 * the first free index and add it.
1474 for (j = 0; j <= h->highest_lun; j++) {
1475 if (h->drv[j].LunID == lunid) {
1481 /* check if the drive was found already in the array */
1483 drv_index = cciss_find_free_drive_index(ctlr);
1484 if (drv_index == -1)
1487 /*Check if the gendisk needs to be allocated */
1488 if (!h->gendisk[drv_index]){
1489 h->gendisk[drv_index] = alloc_disk(1 << NWD_SHIFT);
1490 if (!h->gendisk[drv_index]){
1491 printk(KERN_ERR "cciss: could not allocate new disk %d\n", drv_index);
1496 h->drv[drv_index].LunID = lunid;
1497 cciss_update_drive_info(ctlr, drv_index);
1503 h->busy_configuring = 0;
1504 /* We return -1 here to tell the ACU that we have registered/updated
1505 * all of the drives that we can and to keep it from calling us
1510 printk(KERN_ERR "cciss: out of memory\n");
1514 /* This function will deregister the disk and it's queue from the
1515 * kernel. It must be called with the controller lock held and the
1516 * drv structures busy_configuring flag set. It's parameters are:
1518 * disk = This is the disk to be deregistered
1519 * drv = This is the drive_info_struct associated with the disk to be
1520 * deregistered. It contains information about the disk used
1522 * clear_all = This flag determines whether or not the disk information
1523 * is going to be completely cleared out and the highest_lun
1524 * reset. Sometimes we want to clear out information about
1525 * the disk in preparation for re-adding it. In this case
1526 * the highest_lun should be left unchanged and the LunID
1527 * should not be cleared.
1529 static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
1533 ctlr_info_t *h = get_host(disk);
1535 if (!capable(CAP_SYS_RAWIO))
1538 /* make sure logical volume is NOT is use */
1539 if (clear_all || (h->gendisk[0] == disk)) {
1540 if (drv->usage_count > 1)
1542 } else if (drv->usage_count > 0)
1545 /* invalidate the devices and deregister the disk. If it is disk
1546 * zero do not deregister it but just zero out it's values. This
1547 * allows us to delete disk zero but keep the controller registered.
1549 if (h->gendisk[0] != disk) {
1551 request_queue_t *q = disk->queue;
1552 if (disk->flags & GENHD_FL_UP)
1555 blk_cleanup_queue(q);
1556 /* Set drv->queue to NULL so that we do not try
1557 * to call blk_start_queue on this queue in the
1562 /* If clear_all is set then we are deleting the logical
1563 * drive, not just refreshing its info. For drives
1564 * other than disk 0 we will call put_disk. We do not
1565 * do this for disk 0 as we need it to be able to
1566 * configure the controller.
1569 /* This isn't pretty, but we need to find the
1570 * disk in our array and NULL our the pointer.
1571 * This is so that we will call alloc_disk if
1572 * this index is used again later.
1574 for (i=0; i < CISS_MAX_LUN; i++){
1575 if(h->gendisk[i] == disk){
1576 h->gendisk[i] = NULL;
1584 set_capacity(disk, 0);
1588 /* zero out the disk size info */
1590 drv->block_size = 0;
1594 drv->raid_level = -1; /* This can be used as a flag variable to
1595 * indicate that this element of the drive
1600 /* check to see if it was the last disk */
1601 if (drv == h->drv + h->highest_lun) {
1602 /* if so, find the new hightest lun */
1603 int i, newhighest = -1;
1604 for (i = 0; i < h->highest_lun; i++) {
1605 /* if the disk has size > 0, it is available */
1606 if (h->drv[i].heads)
1609 h->highest_lun = newhighest;
1617 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, /* 0: address the controller,
1618 1: address logical volume log_unit,
1619 2: periph device address is scsi3addr */
1620 unsigned int log_unit, __u8 page_code,
1621 unsigned char *scsi3addr, int cmd_type)
1623 ctlr_info_t *h = hba[ctlr];
1624 u64bit buff_dma_handle;
1627 c->cmd_type = CMD_IOCTL_PEND;
1628 c->Header.ReplyQueue = 0;
1630 c->Header.SGList = 1;
1631 c->Header.SGTotal = 1;
1633 c->Header.SGList = 0;
1634 c->Header.SGTotal = 0;
1636 c->Header.Tag.lower = c->busaddr;
1638 c->Request.Type.Type = cmd_type;
1639 if (cmd_type == TYPE_CMD) {
1642 /* If the logical unit number is 0 then, this is going
1643 to controller so It's a physical command
1644 mode = 0 target = 0. So we have nothing to write.
1645 otherwise, if use_unit_num == 1,
1646 mode = 1(volume set addressing) target = LUNID
1647 otherwise, if use_unit_num == 2,
1648 mode = 0(periph dev addr) target = scsi3addr */
1649 if (use_unit_num == 1) {
1650 c->Header.LUN.LogDev.VolId =
1651 h->drv[log_unit].LunID;
1652 c->Header.LUN.LogDev.Mode = 1;
1653 } else if (use_unit_num == 2) {
1654 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr,
1656 c->Header.LUN.LogDev.Mode = 0;
1658 /* are we trying to read a vital product page */
1659 if (page_code != 0) {
1660 c->Request.CDB[1] = 0x01;
1661 c->Request.CDB[2] = page_code;
1663 c->Request.CDBLen = 6;
1664 c->Request.Type.Attribute = ATTR_SIMPLE;
1665 c->Request.Type.Direction = XFER_READ;
1666 c->Request.Timeout = 0;
1667 c->Request.CDB[0] = CISS_INQUIRY;
1668 c->Request.CDB[4] = size & 0xFF;
1670 case CISS_REPORT_LOG:
1671 case CISS_REPORT_PHYS:
1672 /* Talking to controller so It's a physical command
1673 mode = 00 target = 0. Nothing to write.
1675 c->Request.CDBLen = 12;
1676 c->Request.Type.Attribute = ATTR_SIMPLE;
1677 c->Request.Type.Direction = XFER_READ;
1678 c->Request.Timeout = 0;
1679 c->Request.CDB[0] = cmd;
1680 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
1681 c->Request.CDB[7] = (size >> 16) & 0xFF;
1682 c->Request.CDB[8] = (size >> 8) & 0xFF;
1683 c->Request.CDB[9] = size & 0xFF;
1686 case CCISS_READ_CAPACITY:
1687 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1688 c->Header.LUN.LogDev.Mode = 1;
1689 c->Request.CDBLen = 10;
1690 c->Request.Type.Attribute = ATTR_SIMPLE;
1691 c->Request.Type.Direction = XFER_READ;
1692 c->Request.Timeout = 0;
1693 c->Request.CDB[0] = cmd;
1695 case CCISS_READ_CAPACITY_16:
1696 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1697 c->Header.LUN.LogDev.Mode = 1;
1698 c->Request.CDBLen = 16;
1699 c->Request.Type.Attribute = ATTR_SIMPLE;
1700 c->Request.Type.Direction = XFER_READ;
1701 c->Request.Timeout = 0;
1702 c->Request.CDB[0] = cmd;
1703 c->Request.CDB[1] = 0x10;
1704 c->Request.CDB[10] = (size >> 24) & 0xFF;
1705 c->Request.CDB[11] = (size >> 16) & 0xFF;
1706 c->Request.CDB[12] = (size >> 8) & 0xFF;
1707 c->Request.CDB[13] = size & 0xFF;
1708 c->Request.Timeout = 0;
1709 c->Request.CDB[0] = cmd;
1711 case CCISS_CACHE_FLUSH:
1712 c->Request.CDBLen = 12;
1713 c->Request.Type.Attribute = ATTR_SIMPLE;
1714 c->Request.Type.Direction = XFER_WRITE;
1715 c->Request.Timeout = 0;
1716 c->Request.CDB[0] = BMIC_WRITE;
1717 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
1721 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
1724 } else if (cmd_type == TYPE_MSG) {
1726 case 0: /* ABORT message */
1727 c->Request.CDBLen = 12;
1728 c->Request.Type.Attribute = ATTR_SIMPLE;
1729 c->Request.Type.Direction = XFER_WRITE;
1730 c->Request.Timeout = 0;
1731 c->Request.CDB[0] = cmd; /* abort */
1732 c->Request.CDB[1] = 0; /* abort a command */
1733 /* buff contains the tag of the command to abort */
1734 memcpy(&c->Request.CDB[4], buff, 8);
1736 case 1: /* RESET message */
1737 c->Request.CDBLen = 12;
1738 c->Request.Type.Attribute = ATTR_SIMPLE;
1739 c->Request.Type.Direction = XFER_WRITE;
1740 c->Request.Timeout = 0;
1741 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
1742 c->Request.CDB[0] = cmd; /* reset */
1743 c->Request.CDB[1] = 0x04; /* reset a LUN */
1745 case 3: /* No-Op message */
1746 c->Request.CDBLen = 1;
1747 c->Request.Type.Attribute = ATTR_SIMPLE;
1748 c->Request.Type.Direction = XFER_WRITE;
1749 c->Request.Timeout = 0;
1750 c->Request.CDB[0] = cmd;
1754 "cciss%d: unknown message type %d\n", ctlr, cmd);
1759 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
1762 /* Fill in the scatter gather information */
1764 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
1766 PCI_DMA_BIDIRECTIONAL);
1767 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1768 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1769 c->SG[0].Len = size;
1770 c->SG[0].Ext = 0; /* we are not chaining */
1775 static int sendcmd_withirq(__u8 cmd,
1779 unsigned int use_unit_num,
1780 unsigned int log_unit, __u8 page_code, int cmd_type)
1782 ctlr_info_t *h = hba[ctlr];
1783 CommandList_struct *c;
1784 u64bit buff_dma_handle;
1785 unsigned long flags;
1787 DECLARE_COMPLETION_ONSTACK(wait);
1789 if ((c = cmd_alloc(h, 0)) == NULL)
1791 return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1792 log_unit, page_code, NULL, cmd_type);
1793 if (return_status != IO_OK) {
1795 return return_status;
1800 /* Put the request on the tail of the queue and send it */
1801 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1805 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1807 wait_for_completion(&wait);
1809 if (c->err_info->CommandStatus != 0) { /* an error has occurred */
1810 switch (c->err_info->CommandStatus) {
1811 case CMD_TARGET_STATUS:
1812 printk(KERN_WARNING "cciss: cmd %p has "
1813 " completed with errors\n", c);
1814 if (c->err_info->ScsiStatus) {
1815 printk(KERN_WARNING "cciss: cmd %p "
1816 "has SCSI Status = %x\n",
1817 c, c->err_info->ScsiStatus);
1821 case CMD_DATA_UNDERRUN:
1822 case CMD_DATA_OVERRUN:
1823 /* expected for inquire and report lun commands */
1826 printk(KERN_WARNING "cciss: Cmd %p is "
1827 "reported invalid\n", c);
1828 return_status = IO_ERROR;
1830 case CMD_PROTOCOL_ERR:
1831 printk(KERN_WARNING "cciss: cmd %p has "
1832 "protocol error \n", c);
1833 return_status = IO_ERROR;
1835 case CMD_HARDWARE_ERR:
1836 printk(KERN_WARNING "cciss: cmd %p had "
1837 " hardware error\n", c);
1838 return_status = IO_ERROR;
1840 case CMD_CONNECTION_LOST:
1841 printk(KERN_WARNING "cciss: cmd %p had "
1842 "connection lost\n", c);
1843 return_status = IO_ERROR;
1846 printk(KERN_WARNING "cciss: cmd %p was "
1848 return_status = IO_ERROR;
1850 case CMD_ABORT_FAILED:
1851 printk(KERN_WARNING "cciss: cmd %p reports "
1852 "abort failed\n", c);
1853 return_status = IO_ERROR;
1855 case CMD_UNSOLICITED_ABORT:
1857 "cciss%d: unsolicited abort %p\n", ctlr, c);
1858 if (c->retry_count < MAX_CMD_RETRIES) {
1860 "cciss%d: retrying %p\n", ctlr, c);
1862 /* erase the old error information */
1863 memset(c->err_info, 0,
1864 sizeof(ErrorInfo_struct));
1865 return_status = IO_OK;
1866 INIT_COMPLETION(wait);
1869 return_status = IO_ERROR;
1872 printk(KERN_WARNING "cciss: cmd %p returned "
1873 "unknown status %x\n", c,
1874 c->err_info->CommandStatus);
1875 return_status = IO_ERROR;
1878 /* unlock the buffers from DMA */
1879 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
1880 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
1881 pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
1882 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
1884 return return_status;
1887 static void cciss_geometry_inquiry(int ctlr, int logvol,
1888 int withirq, sector_t total_size,
1889 unsigned int block_size,
1890 InquiryData_struct *inq_buff,
1891 drive_info_struct *drv)
1896 memset(inq_buff, 0, sizeof(InquiryData_struct));
1898 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
1899 inq_buff, sizeof(*inq_buff), 1,
1900 logvol, 0xC1, TYPE_CMD);
1902 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
1903 sizeof(*inq_buff), 1, logvol, 0xC1, NULL,
1905 if (return_code == IO_OK) {
1906 if (inq_buff->data_byte[8] == 0xFF) {
1908 "cciss: reading geometry failed, volume "
1909 "does not support reading geometry\n");
1911 drv->sectors = 32; // Sectors per track
1912 drv->raid_level = RAID_UNKNOWN;
1914 drv->heads = inq_buff->data_byte[6];
1915 drv->sectors = inq_buff->data_byte[7];
1916 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
1917 drv->cylinders += inq_buff->data_byte[5];
1918 drv->raid_level = inq_buff->data_byte[8];
1920 drv->block_size = block_size;
1921 drv->nr_blocks = total_size;
1922 t = drv->heads * drv->sectors;
1924 unsigned rem = sector_div(total_size, t);
1927 drv->cylinders = total_size;
1929 } else { /* Get geometry failed */
1930 printk(KERN_WARNING "cciss: reading geometry failed\n");
1932 printk(KERN_INFO " heads=%d, sectors=%d, cylinders=%d\n\n",
1933 drv->heads, drv->sectors, drv->cylinders);
1937 cciss_read_capacity(int ctlr, int logvol, int withirq, sector_t *total_size,
1938 unsigned int *block_size)
1940 ReadCapdata_struct *buf;
1942 buf = kmalloc(sizeof(ReadCapdata_struct), GFP_KERNEL);
1944 printk(KERN_WARNING "cciss: out of memory\n");
1947 memset(buf, 0, sizeof(ReadCapdata_struct));
1949 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
1950 ctlr, buf, sizeof(ReadCapdata_struct),
1951 1, logvol, 0, TYPE_CMD);
1953 return_code = sendcmd(CCISS_READ_CAPACITY,
1954 ctlr, buf, sizeof(ReadCapdata_struct),
1955 1, logvol, 0, NULL, TYPE_CMD);
1956 if (return_code == IO_OK) {
1957 *total_size = be32_to_cpu(*(__u32 *) buf->total_size)+1;
1958 *block_size = be32_to_cpu(*(__u32 *) buf->block_size);
1959 } else { /* read capacity command failed */
1960 printk(KERN_WARNING "cciss: read capacity failed\n");
1962 *block_size = BLOCK_SIZE;
1964 if (*total_size != (__u32) 0)
1965 printk(KERN_INFO " blocks= %llu block_size= %d\n",
1966 (unsigned long long)*total_size, *block_size);
1972 cciss_read_capacity_16(int ctlr, int logvol, int withirq, sector_t *total_size, unsigned int *block_size)
1974 ReadCapdata_struct_16 *buf;
1976 buf = kmalloc(sizeof(ReadCapdata_struct_16), GFP_KERNEL);
1978 printk(KERN_WARNING "cciss: out of memory\n");
1981 memset(buf, 0, sizeof(ReadCapdata_struct_16));
1983 return_code = sendcmd_withirq(CCISS_READ_CAPACITY_16,
1984 ctlr, buf, sizeof(ReadCapdata_struct_16),
1985 1, logvol, 0, TYPE_CMD);
1988 return_code = sendcmd(CCISS_READ_CAPACITY_16,
1989 ctlr, buf, sizeof(ReadCapdata_struct_16),
1990 1, logvol, 0, NULL, TYPE_CMD);
1992 if (return_code == IO_OK) {
1993 *total_size = be64_to_cpu(*(__u64 *) buf->total_size)+1;
1994 *block_size = be32_to_cpu(*(__u32 *) buf->block_size);
1995 } else { /* read capacity command failed */
1996 printk(KERN_WARNING "cciss: read capacity failed\n");
1998 *block_size = BLOCK_SIZE;
2000 printk(KERN_INFO " blocks= %llu block_size= %d\n",
2001 (unsigned long long)*total_size, *block_size);
2006 static int cciss_revalidate(struct gendisk *disk)
2008 ctlr_info_t *h = get_host(disk);
2009 drive_info_struct *drv = get_drv(disk);
2012 unsigned int block_size;
2013 sector_t total_size;
2014 InquiryData_struct *inq_buff = NULL;
2016 for (logvol = 0; logvol < CISS_MAX_LUN; logvol++) {
2017 if (h->drv[logvol].LunID == drv->LunID) {
2026 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
2027 if (inq_buff == NULL) {
2028 printk(KERN_WARNING "cciss: out of memory\n");
2031 if (h->cciss_read == CCISS_READ_10) {
2032 cciss_read_capacity(h->ctlr, logvol, 1,
2033 &total_size, &block_size);
2035 cciss_read_capacity_16(h->ctlr, logvol, 1,
2036 &total_size, &block_size);
2038 cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size,
2041 blk_queue_hardsect_size(drv->queue, drv->block_size);
2042 set_capacity(disk, drv->nr_blocks);
2049 * Wait polling for a command to complete.
2050 * The memory mapped FIFO is polled for the completion.
2051 * Used only at init time, interrupts from the HBA are disabled.
2053 static unsigned long pollcomplete(int ctlr)
2058 /* Wait (up to 20 seconds) for a command to complete */
2060 for (i = 20 * HZ; i > 0; i--) {
2061 done = hba[ctlr]->access.command_completed(hba[ctlr]);
2062 if (done == FIFO_EMPTY)
2063 schedule_timeout_uninterruptible(1);
2067 /* Invalid address to tell caller we ran out of time */
2071 static int add_sendcmd_reject(__u8 cmd, int ctlr, unsigned long complete)
2073 /* We get in here if sendcmd() is polling for completions
2074 and gets some command back that it wasn't expecting --
2075 something other than that which it just sent down.
2076 Ordinarily, that shouldn't happen, but it can happen when
2077 the scsi tape stuff gets into error handling mode, and
2078 starts using sendcmd() to try to abort commands and
2079 reset tape drives. In that case, sendcmd may pick up
2080 completions of commands that were sent to logical drives
2081 through the block i/o system, or cciss ioctls completing, etc.
2082 In that case, we need to save those completions for later
2083 processing by the interrupt handler.
2086 #ifdef CONFIG_CISS_SCSI_TAPE
2087 struct sendcmd_reject_list *srl = &hba[ctlr]->scsi_rejects;
2089 /* If it's not the scsi tape stuff doing error handling, (abort */
2090 /* or reset) then we don't expect anything weird. */
2091 if (cmd != CCISS_RESET_MSG && cmd != CCISS_ABORT_MSG) {
2093 printk(KERN_WARNING "cciss cciss%d: SendCmd "
2094 "Invalid command list address returned! (%lx)\n",
2096 /* not much we can do. */
2097 #ifdef CONFIG_CISS_SCSI_TAPE
2101 /* We've sent down an abort or reset, but something else
2103 if (srl->ncompletions >= (hba[ctlr]->nr_cmds + 2)) {
2104 /* Uh oh. No room to save it for later... */
2105 printk(KERN_WARNING "cciss%d: Sendcmd: Invalid command addr, "
2106 "reject list overflow, command lost!\n", ctlr);
2109 /* Save it for later */
2110 srl->complete[srl->ncompletions] = complete;
2111 srl->ncompletions++;
2117 * Send a command to the controller, and wait for it to complete.
2118 * Only used at init time.
2120 static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, /* 0: address the controller,
2121 1: address logical volume log_unit,
2122 2: periph device address is scsi3addr */
2123 unsigned int log_unit,
2124 __u8 page_code, unsigned char *scsi3addr, int cmd_type)
2126 CommandList_struct *c;
2128 unsigned long complete;
2129 ctlr_info_t *info_p = hba[ctlr];
2130 u64bit buff_dma_handle;
2131 int status, done = 0;
2133 if ((c = cmd_alloc(info_p, 1)) == NULL) {
2134 printk(KERN_WARNING "cciss: unable to get memory");
2137 status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
2138 log_unit, page_code, scsi3addr, cmd_type);
2139 if (status != IO_OK) {
2140 cmd_free(info_p, c, 1);
2148 printk(KERN_DEBUG "cciss: turning intr off\n");
2149 #endif /* CCISS_DEBUG */
2150 info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
2152 /* Make sure there is room in the command FIFO */
2153 /* Actually it should be completely empty at this time */
2154 /* unless we are in here doing error handling for the scsi */
2155 /* tape side of the driver. */
2156 for (i = 200000; i > 0; i--) {
2157 /* if fifo isn't full go */
2158 if (!(info_p->access.fifo_full(info_p))) {
2163 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
2164 " waiting!\n", ctlr);
2169 info_p->access.submit_command(info_p, c);
2172 complete = pollcomplete(ctlr);
2175 printk(KERN_DEBUG "cciss: command completed\n");
2176 #endif /* CCISS_DEBUG */
2178 if (complete == 1) {
2180 "cciss cciss%d: SendCmd Timeout out, "
2181 "No command list address returned!\n", ctlr);
2187 /* This will need to change for direct lookup completions */
2188 if ((complete & CISS_ERROR_BIT)
2189 && (complete & ~CISS_ERROR_BIT) == c->busaddr) {
2190 /* if data overrun or underun on Report command
2193 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
2194 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
2195 (c->Request.CDB[0] == CISS_INQUIRY)) &&
2196 ((c->err_info->CommandStatus ==
2197 CMD_DATA_OVERRUN) ||
2198 (c->err_info->CommandStatus == CMD_DATA_UNDERRUN)
2200 complete = c->busaddr;
2202 if (c->err_info->CommandStatus ==
2203 CMD_UNSOLICITED_ABORT) {
2204 printk(KERN_WARNING "cciss%d: "
2205 "unsolicited abort %p\n",
2207 if (c->retry_count < MAX_CMD_RETRIES) {
2209 "cciss%d: retrying %p\n",
2212 /* erase the old error */
2214 memset(c->err_info, 0,
2216 (ErrorInfo_struct));
2220 "cciss%d: retried %p too "
2221 "many times\n", ctlr, c);
2225 } else if (c->err_info->CommandStatus ==
2228 "cciss%d: command could not be aborted.\n",
2233 printk(KERN_WARNING "ciss ciss%d: sendcmd"
2234 " Error %x \n", ctlr,
2235 c->err_info->CommandStatus);
2236 printk(KERN_WARNING "ciss ciss%d: sendcmd"
2238 " size %x\n num %x value %x\n",
2240 c->err_info->MoreErrInfo.Invalid_Cmd.
2242 c->err_info->MoreErrInfo.Invalid_Cmd.
2244 c->err_info->MoreErrInfo.Invalid_Cmd.
2250 /* This will need changing for direct lookup completions */
2251 if (complete != c->busaddr) {
2252 if (add_sendcmd_reject(cmd, ctlr, complete) != 0) {
2253 BUG(); /* we are pretty much hosed if we get here. */
2261 /* unlock the data buffer from DMA */
2262 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
2263 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
2264 pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
2265 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
2266 #ifdef CONFIG_CISS_SCSI_TAPE
2267 /* if we saved some commands for later, process them now. */
2268 if (info_p->scsi_rejects.ncompletions > 0)
2269 do_cciss_intr(0, info_p);
2271 cmd_free(info_p, c, 1);
2276 * Map (physical) PCI mem into (virtual) kernel space
2278 static void __iomem *remap_pci_mem(ulong base, ulong size)
2280 ulong page_base = ((ulong) base) & PAGE_MASK;
2281 ulong page_offs = ((ulong) base) - page_base;
2282 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
2284 return page_remapped ? (page_remapped + page_offs) : NULL;
2288 * Takes jobs of the Q and sends them to the hardware, then puts it on
2289 * the Q to wait for completion.
2291 static void start_io(ctlr_info_t *h)
2293 CommandList_struct *c;
2295 while ((c = h->reqQ) != NULL) {
2296 /* can't do anything if fifo is full */
2297 if ((h->access.fifo_full(h))) {
2298 printk(KERN_WARNING "cciss: fifo full\n");
2302 /* Get the first entry from the Request Q */
2303 removeQ(&(h->reqQ), c);
2306 /* Tell the controller execute command */
2307 h->access.submit_command(h, c);
2309 /* Put job onto the completed Q */
2310 addQ(&(h->cmpQ), c);
2314 /* Assumes that CCISS_LOCK(h->ctlr) is held. */
2315 /* Zeros out the error record and then resends the command back */
2316 /* to the controller */
2317 static inline void resend_cciss_cmd(ctlr_info_t *h, CommandList_struct *c)
2319 /* erase the old error information */
2320 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
2322 /* add it to software queue and then send it to the controller */
2323 addQ(&(h->reqQ), c);
2325 if (h->Qdepth > h->maxQsinceinit)
2326 h->maxQsinceinit = h->Qdepth;
2331 /* checks the status of the job and calls complete buffers to mark all
2332 * buffers for the completed job. Note that this function does not need
2333 * to hold the hba/queue lock.
2335 static inline void complete_command(ctlr_info_t *h, CommandList_struct *cmd,
2344 if (cmd->err_info->CommandStatus != 0) { /* an error has occurred */
2345 switch (cmd->err_info->CommandStatus) {
2346 unsigned char sense_key;
2347 case CMD_TARGET_STATUS:
2350 if (cmd->err_info->ScsiStatus == 0x02) {
2351 printk(KERN_WARNING "cciss: cmd %p "
2352 "has CHECK CONDITION "
2353 " byte 2 = 0x%x\n", cmd,
2354 cmd->err_info->SenseInfo[2]
2356 /* check the sense key */
2357 sense_key = 0xf & cmd->err_info->SenseInfo[2];
2358 /* no status or recovered error */
2359 if ((sense_key == 0x0) || (sense_key == 0x1)) {
2363 printk(KERN_WARNING "cciss: cmd %p "
2364 "has SCSI Status 0x%x\n",
2365 cmd, cmd->err_info->ScsiStatus);
2368 case CMD_DATA_UNDERRUN:
2369 printk(KERN_WARNING "cciss: cmd %p has"
2370 " completed with data underrun "
2373 case CMD_DATA_OVERRUN:
2374 printk(KERN_WARNING "cciss: cmd %p has"
2375 " completed with data overrun "
2379 printk(KERN_WARNING "cciss: cmd %p is "
2380 "reported invalid\n", cmd);
2383 case CMD_PROTOCOL_ERR:
2384 printk(KERN_WARNING "cciss: cmd %p has "
2385 "protocol error \n", cmd);
2388 case CMD_HARDWARE_ERR:
2389 printk(KERN_WARNING "cciss: cmd %p had "
2390 " hardware error\n", cmd);
2393 case CMD_CONNECTION_LOST:
2394 printk(KERN_WARNING "cciss: cmd %p had "
2395 "connection lost\n", cmd);
2399 printk(KERN_WARNING "cciss: cmd %p was "
2403 case CMD_ABORT_FAILED:
2404 printk(KERN_WARNING "cciss: cmd %p reports "
2405 "abort failed\n", cmd);
2408 case CMD_UNSOLICITED_ABORT:
2409 printk(KERN_WARNING "cciss%d: unsolicited "
2410 "abort %p\n", h->ctlr, cmd);
2411 if (cmd->retry_count < MAX_CMD_RETRIES) {
2414 "cciss%d: retrying %p\n", h->ctlr, cmd);
2418 "cciss%d: %p retried too "
2419 "many times\n", h->ctlr, cmd);
2423 printk(KERN_WARNING "cciss: cmd %p timedout\n", cmd);
2427 printk(KERN_WARNING "cciss: cmd %p returned "
2428 "unknown status %x\n", cmd,
2429 cmd->err_info->CommandStatus);
2433 /* We need to return this command */
2435 resend_cciss_cmd(h, cmd);
2439 cmd->rq->completion_data = cmd;
2440 cmd->rq->errors = status;
2441 blk_add_trace_rq(cmd->rq->q, cmd->rq, BLK_TA_COMPLETE);
2442 blk_complete_request(cmd->rq);
2446 * Get a request and submit it to the controller.
2448 static void do_cciss_request(request_queue_t *q)
2450 ctlr_info_t *h = q->queuedata;
2451 CommandList_struct *c;
2454 struct request *creq;
2456 struct scatterlist tmp_sg[MAXSGENTRIES];
2457 drive_info_struct *drv;
2460 /* We call start_io here in case there is a command waiting on the
2461 * queue that has not been sent.
2463 if (blk_queue_plugged(q))
2467 creq = elv_next_request(q);
2471 BUG_ON(creq->nr_phys_segments > MAXSGENTRIES);
2473 if ((c = cmd_alloc(h, 1)) == NULL)
2476 blkdev_dequeue_request(creq);
2478 spin_unlock_irq(q->queue_lock);
2480 c->cmd_type = CMD_RWREQ;
2483 /* fill in the request */
2484 drv = creq->rq_disk->private_data;
2485 c->Header.ReplyQueue = 0; // unused in simple mode
2486 /* got command from pool, so use the command block index instead */
2487 /* for direct lookups. */
2488 /* The first 2 bits are reserved for controller error reporting. */
2489 c->Header.Tag.lower = (c->cmdindex << 3);
2490 c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */
2491 c->Header.LUN.LogDev.VolId = drv->LunID;
2492 c->Header.LUN.LogDev.Mode = 1;
2493 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
2494 c->Request.Type.Type = TYPE_CMD; // It is a command.
2495 c->Request.Type.Attribute = ATTR_SIMPLE;
2496 c->Request.Type.Direction =
2497 (rq_data_dir(creq) == READ) ? XFER_READ : XFER_WRITE;
2498 c->Request.Timeout = 0; // Don't time out
2500 (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write;
2501 start_blk = creq->sector;
2503 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n", (int)creq->sector,
2504 (int)creq->nr_sectors);
2505 #endif /* CCISS_DEBUG */
2507 seg = blk_rq_map_sg(q, creq, tmp_sg);
2509 /* get the DMA records for the setup */
2510 if (c->Request.Type.Direction == XFER_READ)
2511 dir = PCI_DMA_FROMDEVICE;
2513 dir = PCI_DMA_TODEVICE;
2515 for (i = 0; i < seg; i++) {
2516 c->SG[i].Len = tmp_sg[i].length;
2517 temp64.val = (__u64) pci_map_page(h->pdev, tmp_sg[i].page,
2519 tmp_sg[i].length, dir);
2520 c->SG[i].Addr.lower = temp64.val32.lower;
2521 c->SG[i].Addr.upper = temp64.val32.upper;
2522 c->SG[i].Ext = 0; // we are not chaining
2524 /* track how many SG entries we are using */
2529 printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n",
2530 creq->nr_sectors, seg);
2531 #endif /* CCISS_DEBUG */
2533 c->Header.SGList = c->Header.SGTotal = seg;
2534 if(h->cciss_read == CCISS_READ_10) {
2535 c->Request.CDB[1] = 0;
2536 c->Request.CDB[2] = (start_blk >> 24) & 0xff; //MSB
2537 c->Request.CDB[3] = (start_blk >> 16) & 0xff;
2538 c->Request.CDB[4] = (start_blk >> 8) & 0xff;
2539 c->Request.CDB[5] = start_blk & 0xff;
2540 c->Request.CDB[6] = 0; // (sect >> 24) & 0xff; MSB
2541 c->Request.CDB[7] = (creq->nr_sectors >> 8) & 0xff;
2542 c->Request.CDB[8] = creq->nr_sectors & 0xff;
2543 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2545 c->Request.CDBLen = 16;
2546 c->Request.CDB[1]= 0;
2547 c->Request.CDB[2]= (start_blk >> 56) & 0xff; //MSB
2548 c->Request.CDB[3]= (start_blk >> 48) & 0xff;
2549 c->Request.CDB[4]= (start_blk >> 40) & 0xff;
2550 c->Request.CDB[5]= (start_blk >> 32) & 0xff;
2551 c->Request.CDB[6]= (start_blk >> 24) & 0xff;
2552 c->Request.CDB[7]= (start_blk >> 16) & 0xff;
2553 c->Request.CDB[8]= (start_blk >> 8) & 0xff;
2554 c->Request.CDB[9]= start_blk & 0xff;
2555 c->Request.CDB[10]= (creq->nr_sectors >> 24) & 0xff;
2556 c->Request.CDB[11]= (creq->nr_sectors >> 16) & 0xff;
2557 c->Request.CDB[12]= (creq->nr_sectors >> 8) & 0xff;
2558 c->Request.CDB[13]= creq->nr_sectors & 0xff;
2559 c->Request.CDB[14] = c->Request.CDB[15] = 0;
2562 spin_lock_irq(q->queue_lock);
2564 addQ(&(h->reqQ), c);
2566 if (h->Qdepth > h->maxQsinceinit)
2567 h->maxQsinceinit = h->Qdepth;
2573 /* We will already have the driver lock here so not need
2579 static inline unsigned long get_next_completion(ctlr_info_t *h)
2581 #ifdef CONFIG_CISS_SCSI_TAPE
2582 /* Any rejects from sendcmd() lying around? Process them first */
2583 if (h->scsi_rejects.ncompletions == 0)
2584 return h->access.command_completed(h);
2586 struct sendcmd_reject_list *srl;
2588 srl = &h->scsi_rejects;
2589 n = --srl->ncompletions;
2590 /* printk("cciss%d: processing saved reject\n", h->ctlr); */
2592 return srl->complete[n];
2595 return h->access.command_completed(h);
2599 static inline int interrupt_pending(ctlr_info_t *h)
2601 #ifdef CONFIG_CISS_SCSI_TAPE
2602 return (h->access.intr_pending(h)
2603 || (h->scsi_rejects.ncompletions > 0));
2605 return h->access.intr_pending(h);
2609 static inline long interrupt_not_for_us(ctlr_info_t *h)
2611 #ifdef CONFIG_CISS_SCSI_TAPE
2612 return (((h->access.intr_pending(h) == 0) ||
2613 (h->interrupts_enabled == 0))
2614 && (h->scsi_rejects.ncompletions == 0));
2616 return (((h->access.intr_pending(h) == 0) ||
2617 (h->interrupts_enabled == 0)));
2621 static irqreturn_t do_cciss_intr(int irq, void *dev_id)
2623 ctlr_info_t *h = dev_id;
2624 CommandList_struct *c;
2625 unsigned long flags;
2628 if (interrupt_not_for_us(h))
2631 * If there are completed commands in the completion queue,
2632 * we had better do something about it.
2634 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2635 while (interrupt_pending(h)) {
2636 while ((a = get_next_completion(h)) != FIFO_EMPTY) {
2640 if (a2 >= h->nr_cmds) {
2642 "cciss: controller cciss%d failed, stopping.\n",
2644 fail_all_cmds(h->ctlr);
2648 c = h->cmd_pool + a2;
2653 if ((c = h->cmpQ) == NULL) {
2655 "cciss: Completion of %08x ignored\n",
2659 while (c->busaddr != a) {
2666 * If we've found the command, take it off the
2667 * completion Q and free it
2669 if (c->busaddr == a) {
2670 removeQ(&h->cmpQ, c);
2671 if (c->cmd_type == CMD_RWREQ) {
2672 complete_command(h, c, 0);
2673 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2674 complete(c->waiting);
2676 # ifdef CONFIG_CISS_SCSI_TAPE
2677 else if (c->cmd_type == CMD_SCSI)
2678 complete_scsi_command(c, 0, a1);
2685 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2690 * We cannot read the structure directly, for portability we must use
2692 * This is for debug only.
2695 static void print_cfg_table(CfgTable_struct *tb)
2700 printk("Controller Configuration information\n");
2701 printk("------------------------------------\n");
2702 for (i = 0; i < 4; i++)
2703 temp_name[i] = readb(&(tb->Signature[i]));
2704 temp_name[4] = '\0';
2705 printk(" Signature = %s\n", temp_name);
2706 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
2707 printk(" Transport methods supported = 0x%x\n",
2708 readl(&(tb->TransportSupport)));
2709 printk(" Transport methods active = 0x%x\n",
2710 readl(&(tb->TransportActive)));
2711 printk(" Requested transport Method = 0x%x\n",
2712 readl(&(tb->HostWrite.TransportRequest)));
2713 printk(" Coalesce Interrupt Delay = 0x%x\n",
2714 readl(&(tb->HostWrite.CoalIntDelay)));
2715 printk(" Coalesce Interrupt Count = 0x%x\n",
2716 readl(&(tb->HostWrite.CoalIntCount)));
2717 printk(" Max outstanding commands = 0x%d\n",
2718 readl(&(tb->CmdsOutMax)));
2719 printk(" Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
2720 for (i = 0; i < 16; i++)
2721 temp_name[i] = readb(&(tb->ServerName[i]));
2722 temp_name[16] = '\0';
2723 printk(" Server Name = %s\n", temp_name);
2724 printk(" Heartbeat Counter = 0x%x\n\n\n", readl(&(tb->HeartBeat)));
2726 #endif /* CCISS_DEBUG */
2728 static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
2730 int i, offset, mem_type, bar_type;
2731 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
2734 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2735 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
2736 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
2739 mem_type = pci_resource_flags(pdev, i) &
2740 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
2742 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2743 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
2744 offset += 4; /* 32 bit */
2746 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2749 default: /* reserved in PCI 2.2 */
2751 "Base address is invalid\n");
2756 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
2762 /* If MSI/MSI-X is supported by the kernel we will try to enable it on
2763 * controllers that are capable. If not, we use IO-APIC mode.
2766 static void __devinit cciss_interrupt_mode(ctlr_info_t *c,
2767 struct pci_dev *pdev, __u32 board_id)
2769 #ifdef CONFIG_PCI_MSI
2771 struct msix_entry cciss_msix_entries[4] = { {0, 0}, {0, 1},
2775 /* Some boards advertise MSI but don't really support it */
2776 if ((board_id == 0x40700E11) ||
2777 (board_id == 0x40800E11) ||
2778 (board_id == 0x40820E11) || (board_id == 0x40830E11))
2779 goto default_int_mode;
2781 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
2782 err = pci_enable_msix(pdev, cciss_msix_entries, 4);
2784 c->intr[0] = cciss_msix_entries[0].vector;
2785 c->intr[1] = cciss_msix_entries[1].vector;
2786 c->intr[2] = cciss_msix_entries[2].vector;
2787 c->intr[3] = cciss_msix_entries[3].vector;
2792 printk(KERN_WARNING "cciss: only %d MSI-X vectors "
2793 "available\n", err);
2794 goto default_int_mode;
2796 printk(KERN_WARNING "cciss: MSI-X init failed %d\n",
2798 goto default_int_mode;
2801 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
2802 if (!pci_enable_msi(pdev)) {
2805 printk(KERN_WARNING "cciss: MSI init failed\n");
2809 #endif /* CONFIG_PCI_MSI */
2810 /* if we get here we're going to use the default interrupt mode */
2811 c->intr[SIMPLE_MODE_INT] = pdev->irq;
2815 static int cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
2817 ushort subsystem_vendor_id, subsystem_device_id, command;
2818 __u32 board_id, scratchpad = 0;
2820 __u32 cfg_base_addr;
2821 __u64 cfg_base_addr_index;
2824 /* check to see if controller has been disabled */
2825 /* BEFORE trying to enable it */
2826 (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
2827 if (!(command & 0x02)) {
2829 "cciss: controller appears to be disabled\n");
2833 err = pci_enable_device(pdev);
2835 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
2839 err = pci_request_regions(pdev, "cciss");
2841 printk(KERN_ERR "cciss: Cannot obtain PCI resources, "
2846 subsystem_vendor_id = pdev->subsystem_vendor;
2847 subsystem_device_id = pdev->subsystem_device;
2848 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
2849 subsystem_vendor_id);
2852 printk("command = %x\n", command);
2853 printk("irq = %x\n", pdev->irq);
2854 printk("board_id = %x\n", board_id);
2855 #endif /* CCISS_DEBUG */
2857 /* If the kernel supports MSI/MSI-X we will try to enable that functionality,
2858 * else we use the IO-APIC interrupt assigned to us by system ROM.
2860 cciss_interrupt_mode(c, pdev, board_id);
2863 * Memory base addr is first addr , the second points to the config
2867 c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
2869 printk("address 0 = %x\n", c->paddr);
2870 #endif /* CCISS_DEBUG */
2871 c->vaddr = remap_pci_mem(c->paddr, 0x250);
2873 /* Wait for the board to become ready. (PCI hotplug needs this.)
2874 * We poll for up to 120 secs, once per 100ms. */
2875 for (i = 0; i < 1200; i++) {
2876 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
2877 if (scratchpad == CCISS_FIRMWARE_READY)
2879 set_current_state(TASK_INTERRUPTIBLE);
2880 schedule_timeout(HZ / 10); /* wait 100ms */
2882 if (scratchpad != CCISS_FIRMWARE_READY) {
2883 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
2885 goto err_out_free_res;
2888 /* get the address index number */
2889 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
2890 cfg_base_addr &= (__u32) 0x0000ffff;
2892 printk("cfg base address = %x\n", cfg_base_addr);
2893 #endif /* CCISS_DEBUG */
2894 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
2896 printk("cfg base address index = %x\n", cfg_base_addr_index);
2897 #endif /* CCISS_DEBUG */
2898 if (cfg_base_addr_index == -1) {
2899 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
2901 goto err_out_free_res;
2904 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
2906 printk("cfg offset = %x\n", cfg_offset);
2907 #endif /* CCISS_DEBUG */
2908 c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
2909 cfg_base_addr_index) +
2910 cfg_offset, sizeof(CfgTable_struct));
2911 c->board_id = board_id;
2914 print_cfg_table(c->cfgtable);
2915 #endif /* CCISS_DEBUG */
2917 for (i = 0; i < ARRAY_SIZE(products); i++) {
2918 if (board_id == products[i].board_id) {
2919 c->product_name = products[i].product_name;
2920 c->access = *(products[i].access);
2921 c->nr_cmds = products[i].nr_cmds;
2925 if ((readb(&c->cfgtable->Signature[0]) != 'C') ||
2926 (readb(&c->cfgtable->Signature[1]) != 'I') ||
2927 (readb(&c->cfgtable->Signature[2]) != 'S') ||
2928 (readb(&c->cfgtable->Signature[3]) != 'S')) {
2929 printk("Does not appear to be a valid CISS config table\n");
2931 goto err_out_free_res;
2933 /* We didn't find the controller in our list. We know the
2934 * signature is valid. If it's an HP device let's try to
2935 * bind to the device and fire it up. Otherwise we bail.
2937 if (i == ARRAY_SIZE(products)) {
2938 if (subsystem_vendor_id == PCI_VENDOR_ID_HP) {
2939 c->product_name = products[i-1].product_name;
2940 c->access = *(products[i-1].access);
2941 c->nr_cmds = products[i-1].nr_cmds;
2942 printk(KERN_WARNING "cciss: This is an unknown "
2943 "Smart Array controller.\n"
2944 "cciss: Please update to the latest driver "
2945 "available from www.hp.com.\n");
2947 printk(KERN_WARNING "cciss: Sorry, I don't know how"
2948 " to access the Smart Array controller %08lx\n"
2949 , (unsigned long)board_id);
2951 goto err_out_free_res;
2956 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
2958 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
2960 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
2964 /* Disabling DMA prefetch for the P600
2965 * An ASIC bug may result in a prefetch beyond
2968 if(board_id == 0x3225103C) {
2970 dma_prefetch = readl(c->vaddr + I2O_DMA1_CFG);
2971 dma_prefetch |= 0x8000;
2972 writel(dma_prefetch, c->vaddr + I2O_DMA1_CFG);
2976 printk("Trying to put board into Simple mode\n");
2977 #endif /* CCISS_DEBUG */
2978 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
2979 /* Update the field, and then ring the doorbell */
2980 writel(CFGTBL_Trans_Simple, &(c->cfgtable->HostWrite.TransportRequest));
2981 writel(CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
2983 /* under certain very rare conditions, this can take awhile.
2984 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
2985 * as we enter this code.) */
2986 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
2987 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
2989 /* delay and try again */
2990 set_current_state(TASK_INTERRUPTIBLE);
2991 schedule_timeout(10);
2995 printk(KERN_DEBUG "I counter got to %d %x\n", i,
2996 readl(c->vaddr + SA5_DOORBELL));
2997 #endif /* CCISS_DEBUG */
2999 print_cfg_table(c->cfgtable);
3000 #endif /* CCISS_DEBUG */
3002 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
3003 printk(KERN_WARNING "cciss: unable to get board into"
3006 goto err_out_free_res;
3012 * Deliberately omit pci_disable_device(): it does something nasty to
3013 * Smart Array controllers that pci_enable_device does not undo
3015 pci_release_regions(pdev);
3020 * Gets information about the local volumes attached to the controller.
3022 static void cciss_getgeometry(int cntl_num)
3024 ReportLunData_struct *ld_buff;
3025 InquiryData_struct *inq_buff;
3031 sector_t total_size;
3033 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
3034 if (ld_buff == NULL) {
3035 printk(KERN_ERR "cciss: out of memory\n");
3038 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
3039 if (inq_buff == NULL) {
3040 printk(KERN_ERR "cciss: out of memory\n");
3044 /* Get the firmware version */
3045 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
3046 sizeof(InquiryData_struct), 0, 0, 0, NULL,
3048 if (return_code == IO_OK) {
3049 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
3050 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
3051 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
3052 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
3053 } else { /* send command failed */
3055 printk(KERN_WARNING "cciss: unable to determine firmware"
3056 " version of controller\n");
3058 /* Get the number of logical volumes */
3059 return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
3060 sizeof(ReportLunData_struct), 0, 0, 0, NULL,
3063 if (return_code == IO_OK) {
3065 printk("LUN Data\n--------------------------\n");
3066 #endif /* CCISS_DEBUG */
3069 (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
3071 (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
3073 (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
3074 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
3075 } else { /* reading number of logical volumes failed */
3077 printk(KERN_WARNING "cciss: report logical volume"
3078 " command failed\n");
3081 hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
3082 if (hba[cntl_num]->num_luns > CISS_MAX_LUN) {
3084 "ciss: only %d number of logical volumes supported\n",
3086 hba[cntl_num]->num_luns = CISS_MAX_LUN;
3089 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n",
3090 ld_buff->LUNListLength[0], ld_buff->LUNListLength[1],
3091 ld_buff->LUNListLength[2], ld_buff->LUNListLength[3],
3092 hba[cntl_num]->num_luns);
3093 #endif /* CCISS_DEBUG */
3095 hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns - 1;
3096 for (i = 0; i < CISS_MAX_LUN; i++) {
3097 if (i < hba[cntl_num]->num_luns) {
3098 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3]))
3100 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2]))
3102 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1]))
3104 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
3106 hba[cntl_num]->drv[i].LunID = lunid;
3109 printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
3110 ld_buff->LUN[i][0], ld_buff->LUN[i][1],
3111 ld_buff->LUN[i][2], ld_buff->LUN[i][3],
3112 hba[cntl_num]->drv[i].LunID);
3113 #endif /* CCISS_DEBUG */
3115 /* testing to see if 16-byte CDBs are already being used */
3116 if(hba[cntl_num]->cciss_read == CCISS_READ_16) {
3117 cciss_read_capacity_16(cntl_num, i, 0,
3118 &total_size, &block_size);
3121 cciss_read_capacity(cntl_num, i, 0, &total_size, &block_size);
3123 /* total_size = last LBA + 1 */
3124 if(total_size == (__u32) 0) {
3125 cciss_read_capacity_16(cntl_num, i, 0,
3126 &total_size, &block_size);
3127 hba[cntl_num]->cciss_read = CCISS_READ_16;
3128 hba[cntl_num]->cciss_write = CCISS_WRITE_16;
3130 hba[cntl_num]->cciss_read = CCISS_READ_10;
3131 hba[cntl_num]->cciss_write = CCISS_WRITE_10;
3134 cciss_geometry_inquiry(cntl_num, i, 0, total_size,
3135 block_size, inq_buff,
3136 &hba[cntl_num]->drv[i]);
3138 /* initialize raid_level to indicate a free space */
3139 hba[cntl_num]->drv[i].raid_level = -1;
3146 /* Function to find the first free pointer into our hba[] array */
3147 /* Returns -1 if no free entries are left. */
3148 static int alloc_cciss_hba(void)
3152 for (i = 0; i < MAX_CTLR; i++) {
3155 p = kzalloc(sizeof(ctlr_info_t), GFP_KERNEL);
3158 p->gendisk[0] = alloc_disk(1 << NWD_SHIFT);
3165 printk(KERN_WARNING "cciss: This driver supports a maximum"
3166 " of %d controllers.\n", MAX_CTLR);
3169 printk(KERN_ERR "cciss: out of memory.\n");
3173 static void free_hba(int i)
3175 ctlr_info_t *p = hba[i];
3179 for (n = 0; n < CISS_MAX_LUN; n++)
3180 put_disk(p->gendisk[n]);
3185 * This is it. Find all the controllers and register them. I really hate
3186 * stealing all these major device numbers.
3187 * returns the number of block devices registered.
3189 static int __devinit cciss_init_one(struct pci_dev *pdev,
3190 const struct pci_device_id *ent)
3197 i = alloc_cciss_hba();
3201 hba[i]->busy_initializing = 1;
3203 if (cciss_pci_init(hba[i], pdev) != 0)
3206 sprintf(hba[i]->devname, "cciss%d", i);
3208 hba[i]->pdev = pdev;
3210 /* configure PCI DMA stuff */
3211 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK))
3213 else if (!pci_set_dma_mask(pdev, DMA_32BIT_MASK))
3216 printk(KERN_ERR "cciss: no suitable DMA available\n");
3221 * register with the major number, or get a dynamic major number
3222 * by passing 0 as argument. This is done for greater than
3223 * 8 controller support.
3225 if (i < MAX_CTLR_ORIG)
3226 hba[i]->major = COMPAQ_CISS_MAJOR + i;
3227 rc = register_blkdev(hba[i]->major, hba[i]->devname);
3228 if (rc == -EBUSY || rc == -EINVAL) {
3230 "cciss: Unable to get major number %d for %s "
3231 "on hba %d\n", hba[i]->major, hba[i]->devname, i);
3234 if (i >= MAX_CTLR_ORIG)
3238 /* make sure the board interrupts are off */
3239 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
3240 if (request_irq(hba[i]->intr[SIMPLE_MODE_INT], do_cciss_intr,
3241 IRQF_DISABLED | IRQF_SHARED, hba[i]->devname, hba[i])) {
3242 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
3243 hba[i]->intr[SIMPLE_MODE_INT], hba[i]->devname);
3247 printk(KERN_INFO "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
3248 hba[i]->devname, pdev->device, pci_name(pdev),
3249 hba[i]->intr[SIMPLE_MODE_INT], dac ? "" : " not");
3251 hba[i]->cmd_pool_bits =
3252 kmalloc(((hba[i]->nr_cmds + BITS_PER_LONG -
3253 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
3254 hba[i]->cmd_pool = (CommandList_struct *)
3255 pci_alloc_consistent(hba[i]->pdev,
3256 hba[i]->nr_cmds * sizeof(CommandList_struct),
3257 &(hba[i]->cmd_pool_dhandle));
3258 hba[i]->errinfo_pool = (ErrorInfo_struct *)
3259 pci_alloc_consistent(hba[i]->pdev,
3260 hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
3261 &(hba[i]->errinfo_pool_dhandle));
3262 if ((hba[i]->cmd_pool_bits == NULL)
3263 || (hba[i]->cmd_pool == NULL)
3264 || (hba[i]->errinfo_pool == NULL)) {
3265 printk(KERN_ERR "cciss: out of memory");
3268 #ifdef CONFIG_CISS_SCSI_TAPE
3269 hba[i]->scsi_rejects.complete =
3270 kmalloc(sizeof(hba[i]->scsi_rejects.complete[0]) *
3271 (hba[i]->nr_cmds + 5), GFP_KERNEL);
3272 if (hba[i]->scsi_rejects.complete == NULL) {
3273 printk(KERN_ERR "cciss: out of memory");
3277 spin_lock_init(&hba[i]->lock);
3279 /* Initialize the pdev driver private data.
3280 have it point to hba[i]. */
3281 pci_set_drvdata(pdev, hba[i]);
3282 /* command and error info recs zeroed out before
3284 memset(hba[i]->cmd_pool_bits, 0,
3285 ((hba[i]->nr_cmds + BITS_PER_LONG -
3286 1) / BITS_PER_LONG) * sizeof(unsigned long));
3289 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n", i);
3290 #endif /* CCISS_DEBUG */
3292 cciss_getgeometry(i);
3294 cciss_scsi_setup(i);
3296 /* Turn the interrupts on so we can service requests */
3297 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
3301 hba[i]->cciss_max_sectors = 2048;
3303 hba[i]->busy_initializing = 0;
3306 drive_info_struct *drv = &(hba[i]->drv[j]);
3307 struct gendisk *disk = hba[i]->gendisk[j];
3310 /* Check if the disk was allocated already */
3312 hba[i]->gendisk[j] = alloc_disk(1 << NWD_SHIFT);
3313 disk = hba[i]->gendisk[j];
3316 /* Check that the disk was able to be allocated */
3318 printk(KERN_ERR "cciss: unable to allocate memory for disk %d\n", j);
3322 q = blk_init_queue(do_cciss_request, &hba[i]->lock);
3325 "cciss: unable to allocate queue for disk %d\n",
3331 q->backing_dev_info.ra_pages = READ_AHEAD;
3332 blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
3334 /* This is a hardware imposed limit. */
3335 blk_queue_max_hw_segments(q, MAXSGENTRIES);
3337 /* This is a limit in the driver and could be eliminated. */
3338 blk_queue_max_phys_segments(q, MAXSGENTRIES);
3340 blk_queue_max_sectors(q, hba[i]->cciss_max_sectors);
3342 blk_queue_softirq_done(q, cciss_softirq_done);
3344 q->queuedata = hba[i];
3345 sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
3346 disk->major = hba[i]->major;
3347 disk->first_minor = j << NWD_SHIFT;
3348 disk->fops = &cciss_fops;
3350 disk->private_data = drv;
3351 disk->driverfs_dev = &pdev->dev;
3352 /* we must register the controller even if no disks exist */
3353 /* this is for the online array utilities */
3354 if (!drv->heads && j)
3356 blk_queue_hardsect_size(q, drv->block_size);
3357 set_capacity(disk, drv->nr_blocks);
3360 } while (j <= hba[i]->highest_lun);
3365 #ifdef CONFIG_CISS_SCSI_TAPE
3366 kfree(hba[i]->scsi_rejects.complete);
3368 kfree(hba[i]->cmd_pool_bits);
3369 if (hba[i]->cmd_pool)
3370 pci_free_consistent(hba[i]->pdev,
3371 hba[i]->nr_cmds * sizeof(CommandList_struct),
3372 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
3373 if (hba[i]->errinfo_pool)
3374 pci_free_consistent(hba[i]->pdev,
3375 hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
3376 hba[i]->errinfo_pool,
3377 hba[i]->errinfo_pool_dhandle);
3378 free_irq(hba[i]->intr[SIMPLE_MODE_INT], hba[i]);
3380 unregister_blkdev(hba[i]->major, hba[i]->devname);
3382 hba[i]->busy_initializing = 0;
3383 /* cleanup any queues that may have been initialized */
3384 for (j=0; j <= hba[i]->highest_lun; j++){
3385 drive_info_struct *drv = &(hba[i]->drv[j]);
3387 blk_cleanup_queue(drv->queue);
3390 * Deliberately omit pci_disable_device(): it does something nasty to
3391 * Smart Array controllers that pci_enable_device does not undo
3393 pci_release_regions(pdev);
3394 pci_set_drvdata(pdev, NULL);
3399 static void __devexit cciss_remove_one(struct pci_dev *pdev)
3401 ctlr_info_t *tmp_ptr;
3406 if (pci_get_drvdata(pdev) == NULL) {
3407 printk(KERN_ERR "cciss: Unable to remove device \n");
3410 tmp_ptr = pci_get_drvdata(pdev);
3412 if (hba[i] == NULL) {
3413 printk(KERN_ERR "cciss: device appears to "
3414 "already be removed \n");
3417 /* Turn board interrupts off and send the flush cache command */
3418 /* sendcmd will turn off interrupt, and send the flush...
3419 * To write all data in the battery backed cache to disks */
3420 memset(flush_buf, 0, 4);
3421 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
3423 if (return_code != IO_OK) {
3424 printk(KERN_WARNING "Error Flushing cache on controller %d\n",
3427 free_irq(hba[i]->intr[2], hba[i]);
3429 #ifdef CONFIG_PCI_MSI
3430 if (hba[i]->msix_vector)
3431 pci_disable_msix(hba[i]->pdev);
3432 else if (hba[i]->msi_vector)
3433 pci_disable_msi(hba[i]->pdev);
3434 #endif /* CONFIG_PCI_MSI */
3436 iounmap(hba[i]->vaddr);
3437 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
3438 unregister_blkdev(hba[i]->major, hba[i]->devname);
3439 remove_proc_entry(hba[i]->devname, proc_cciss);
3441 /* remove it from the disk list */
3442 for (j = 0; j < CISS_MAX_LUN; j++) {
3443 struct gendisk *disk = hba[i]->gendisk[j];
3445 request_queue_t *q = disk->queue;
3447 if (disk->flags & GENHD_FL_UP)
3450 blk_cleanup_queue(q);
3454 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(CommandList_struct),
3455 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
3456 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
3457 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
3458 kfree(hba[i]->cmd_pool_bits);
3459 #ifdef CONFIG_CISS_SCSI_TAPE
3460 kfree(hba[i]->scsi_rejects.complete);
3463 * Deliberately omit pci_disable_device(): it does something nasty to
3464 * Smart Array controllers that pci_enable_device does not undo
3466 pci_release_regions(pdev);
3467 pci_set_drvdata(pdev, NULL);
3471 static struct pci_driver cciss_pci_driver = {
3473 .probe = cciss_init_one,
3474 .remove = __devexit_p(cciss_remove_one),
3475 .id_table = cciss_pci_device_id, /* id_table */
3479 * This is it. Register the PCI driver information for the cards we control
3480 * the OS will call our registered routines when it finds one of our cards.
3482 static int __init cciss_init(void)
3484 printk(KERN_INFO DRIVER_NAME "\n");
3486 /* Register for our PCI devices */
3487 return pci_register_driver(&cciss_pci_driver);
3490 static void __exit cciss_cleanup(void)
3494 pci_unregister_driver(&cciss_pci_driver);
3495 /* double check that all controller entrys have been removed */
3496 for (i = 0; i < MAX_CTLR; i++) {
3497 if (hba[i] != NULL) {
3498 printk(KERN_WARNING "cciss: had to remove"
3499 " controller %d\n", i);
3500 cciss_remove_one(hba[i]->pdev);
3503 remove_proc_entry("cciss", proc_root_driver);
3506 static void fail_all_cmds(unsigned long ctlr)
3508 /* If we get here, the board is apparently dead. */
3509 ctlr_info_t *h = hba[ctlr];
3510 CommandList_struct *c;
3511 unsigned long flags;
3513 printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
3514 h->alive = 0; /* the controller apparently died... */
3516 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
3518 pci_disable_device(h->pdev); /* Make sure it is really dead. */
3520 /* move everything off the request queue onto the completed queue */
3521 while ((c = h->reqQ) != NULL) {
3522 removeQ(&(h->reqQ), c);
3524 addQ(&(h->cmpQ), c);
3527 /* Now, fail everything on the completed queue with a HW error */
3528 while ((c = h->cmpQ) != NULL) {
3529 removeQ(&h->cmpQ, c);
3530 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
3531 if (c->cmd_type == CMD_RWREQ) {
3532 complete_command(h, c, 0);
3533 } else if (c->cmd_type == CMD_IOCTL_PEND)
3534 complete(c->waiting);
3535 #ifdef CONFIG_CISS_SCSI_TAPE
3536 else if (c->cmd_type == CMD_SCSI)
3537 complete_scsi_command(c, 0, 0);
3540 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
3544 module_init(cciss_init);
3545 module_exit(cciss_cleanup);