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 Cciss-discuss@lists.sourceforge.net
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>
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 <asm/uaccess.h>
43 #include <linux/blkdev.h>
44 #include <linux/genhd.h>
45 #include <linux/completion.h>
47 #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
48 #define DRIVER_NAME "Compaq CISS Driver (v 2.6.2)"
49 #define DRIVER_VERSION CCISS_DRIVER_VERSION(2,6,2)
51 /* Embedded module documentation macros - see modules.h */
52 MODULE_AUTHOR("Hewlett-Packard Company");
53 MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 2.6.2");
54 MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
56 MODULE_LICENSE("GPL");
58 #include "cciss_cmd.h"
60 #include <linux/cciss_ioctl.h>
62 /* define the PCI info for the cards we can control */
63 const struct pci_device_id cciss_pci_device_id[] = {
64 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS,
65 0x0E11, 0x4070, 0, 0, 0},
66 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
67 0x0E11, 0x4080, 0, 0, 0},
68 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
69 0x0E11, 0x4082, 0, 0, 0},
70 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
71 0x0E11, 0x4083, 0, 0, 0},
72 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
73 0x0E11, 0x409A, 0, 0, 0},
74 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
75 0x0E11, 0x409B, 0, 0, 0},
76 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
77 0x0E11, 0x409C, 0, 0, 0},
78 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
79 0x0E11, 0x409D, 0, 0, 0},
80 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
81 0x0E11, 0x4091, 0, 0, 0},
82 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
83 0x0E11, 0x409E, 0, 0, 0},
84 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
85 0x103C, 0x3211, 0, 0, 0},
88 MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
90 #define NR_PRODUCTS (sizeof(products)/sizeof(struct board_type))
92 /* board_id = Subsystem Device ID & Vendor ID
93 * product = Marketing Name for the board
94 * access = Address of the struct of function pointers
96 static struct board_type products[] = {
97 { 0x40700E11, "Smart Array 5300", &SA5_access },
98 { 0x40800E11, "Smart Array 5i", &SA5B_access},
99 { 0x40820E11, "Smart Array 532", &SA5B_access},
100 { 0x40830E11, "Smart Array 5312", &SA5B_access},
101 { 0x409A0E11, "Smart Array 641", &SA5_access},
102 { 0x409B0E11, "Smart Array 642", &SA5_access},
103 { 0x409C0E11, "Smart Array 6400", &SA5_access},
104 { 0x409D0E11, "Smart Array 6400 EM", &SA5_access},
105 { 0x40910E11, "Smart Array 6i", &SA5_access},
106 { 0x409E0E11, "Smart Array 6422", &SA5_access},
107 { 0x3211103C, "Smart Array V100", &SA5_access},
110 /* How long to wait (in millesconds) for board to go into simple mode */
111 #define MAX_CONFIG_WAIT 30000
112 #define MAX_IOCTL_CONFIG_WAIT 1000
114 /*define how many times we will try a command because of bus resets */
115 #define MAX_CMD_RETRIES 3
117 #define READ_AHEAD 256
118 #define NR_CMDS 384 /* #commands that can be outstanding */
121 #define CCISS_DMA_MASK 0xFFFFFFFF /* 32 bit DMA */
123 static ctlr_info_t *hba[MAX_CTLR];
125 static void do_cciss_request(request_queue_t *q);
126 static int cciss_open(struct inode *inode, struct file *filep);
127 static int cciss_release(struct inode *inode, struct file *filep);
128 static int cciss_ioctl(struct inode *inode, struct file *filep,
129 unsigned int cmd, unsigned long arg);
131 static int revalidate_allvol(ctlr_info_t *host);
132 static int cciss_revalidate(struct gendisk *disk);
133 static int deregister_disk(struct gendisk *disk);
134 static int register_new_disk(ctlr_info_t *h);
136 static void cciss_getgeometry(int cntl_num);
138 static void start_io( ctlr_info_t *h);
139 static int sendcmd( __u8 cmd, int ctlr, void *buff, size_t size,
140 unsigned int use_unit_num, unsigned int log_unit, __u8 page_code,
141 unsigned char *scsi3addr, int cmd_type);
143 #ifdef CONFIG_PROC_FS
144 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
145 int length, int *eof, void *data);
146 static void cciss_procinit(int i);
148 static void cciss_procinit(int i) {}
149 #endif /* CONFIG_PROC_FS */
151 static struct block_device_operations cciss_fops = {
152 .owner = THIS_MODULE,
154 .release = cciss_release,
155 .ioctl = cciss_ioctl,
156 .revalidate_disk= cciss_revalidate,
160 * Enqueuing and dequeuing functions for cmdlists.
162 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
166 c->next = c->prev = c;
168 c->prev = (*Qptr)->prev;
170 (*Qptr)->prev->next = c;
175 static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
176 CommandList_struct *c)
178 if (c && c->next != c) {
179 if (*Qptr == c) *Qptr = c->next;
180 c->prev->next = c->next;
181 c->next->prev = c->prev;
187 #ifdef CONFIG_PROC_FS
189 #include "cciss_scsi.c" /* For SCSI tape support */
192 * Report information about this controller.
194 #define ENG_GIG 1048576000
195 #define ENG_GIG_FACTOR (ENG_GIG/512)
196 #define RAID_UNKNOWN 6
197 static const char *raid_label[] = {"0","4","1(0+1)","5","5+1","ADG",
200 static struct proc_dir_entry *proc_cciss;
202 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
203 int length, int *eof, void *data)
208 ctlr_info_t *h = (ctlr_info_t*)data;
209 drive_info_struct *drv;
211 unsigned int vol_sz, vol_sz_frac;
215 /* prevent displaying bogus info during configuration
216 * or deconfiguration of a logical volume
218 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
219 if (h->busy_configuring) {
220 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
223 h->busy_configuring = 1;
224 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
226 size = sprintf(buffer, "%s: HP %s Controller\n"
227 "Board ID: 0x%08lx\n"
228 "Firmware Version: %c%c%c%c\n"
230 "Logical drives: %d\n"
231 "Current Q depth: %d\n"
232 "Current # commands on controller: %d\n"
233 "Max Q depth since init: %d\n"
234 "Max # commands on controller since init: %d\n"
235 "Max SG entries since init: %d\n\n",
238 (unsigned long)h->board_id,
239 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], h->firm_ver[3],
240 (unsigned int)h->intr,
242 h->Qdepth, h->commands_outstanding,
243 h->maxQsinceinit, h->max_outstanding, h->maxSG);
245 pos += size; len += size;
246 cciss_proc_tape_report(ctlr, buffer, &pos, &len);
247 for(i=0; i<=h->highest_lun; i++) {
251 if (drv->block_size == 0)
253 vol_sz = drv->nr_blocks;
254 sector_div(vol_sz, ENG_GIG_FACTOR);
259 * (drv->nr_blocks%ENG_GIG_FACTOR)*100/ENG_GIG_FACTOR;
261 tmp = drv->nr_blocks;
262 vol_sz_frac = sector_div(tmp, ENG_GIG_FACTOR);
264 /* Now, vol_sz_frac = (drv->nr_blocks%ENG_GIG_FACTOR) */
267 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
269 if (drv->raid_level > 5)
270 drv->raid_level = RAID_UNKNOWN;
271 size = sprintf(buffer+len, "cciss/c%dd%d:"
272 "\t%4d.%02dGB\tRAID %s\n",
273 ctlr, i, vol_sz,vol_sz_frac,
274 raid_label[drv->raid_level]);
275 pos += size; len += size;
279 *start = buffer+offset;
283 h->busy_configuring = 0;
288 cciss_proc_write(struct file *file, const char __user *buffer,
289 unsigned long count, void *data)
291 unsigned char cmd[80];
293 #ifdef CONFIG_CISS_SCSI_TAPE
294 ctlr_info_t *h = (ctlr_info_t *) data;
298 if (count > sizeof(cmd)-1) return -EINVAL;
299 if (copy_from_user(cmd, buffer, count)) return -EFAULT;
301 len = strlen(cmd); // above 3 lines ensure safety
302 if (cmd[len-1] == '\n')
304 # ifdef CONFIG_CISS_SCSI_TAPE
305 if (strcmp("engage scsi", cmd)==0) {
306 rc = cciss_engage_scsi(h->ctlr);
307 if (rc != 0) return -rc;
310 /* might be nice to have "disengage" too, but it's not
311 safely possible. (only 1 module use count, lock issues.) */
317 * Get us a file in /proc/cciss that says something about each controller.
318 * Create /proc/cciss if it doesn't exist yet.
320 static void __devinit cciss_procinit(int i)
322 struct proc_dir_entry *pde;
324 if (proc_cciss == NULL) {
325 proc_cciss = proc_mkdir("cciss", proc_root_driver);
330 pde = create_proc_read_entry(hba[i]->devname,
331 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
332 proc_cciss, cciss_proc_get_info, hba[i]);
333 pde->write_proc = cciss_proc_write;
335 #endif /* CONFIG_PROC_FS */
338 * For operations that cannot sleep, a command block is allocated at init,
339 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
340 * which ones are free or in use. For operations that can wait for kmalloc
341 * to possible sleep, this routine can be called with get_from_pool set to 0.
342 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
344 static CommandList_struct * cmd_alloc(ctlr_info_t *h, int get_from_pool)
346 CommandList_struct *c;
349 dma_addr_t cmd_dma_handle, err_dma_handle;
353 c = (CommandList_struct *) pci_alloc_consistent(
354 h->pdev, sizeof(CommandList_struct), &cmd_dma_handle);
357 memset(c, 0, sizeof(CommandList_struct));
359 c->err_info = (ErrorInfo_struct *)pci_alloc_consistent(
360 h->pdev, sizeof(ErrorInfo_struct),
363 if (c->err_info == NULL)
365 pci_free_consistent(h->pdev,
366 sizeof(CommandList_struct), c, cmd_dma_handle);
369 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
370 } else /* get it out of the controllers pool */
373 i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
376 } while(test_and_set_bit(i & (BITS_PER_LONG - 1), h->cmd_pool_bits+(i/BITS_PER_LONG)) != 0);
378 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
381 memset(c, 0, sizeof(CommandList_struct));
382 cmd_dma_handle = h->cmd_pool_dhandle
383 + i*sizeof(CommandList_struct);
384 c->err_info = h->errinfo_pool + i;
385 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
386 err_dma_handle = h->errinfo_pool_dhandle
387 + i*sizeof(ErrorInfo_struct);
391 c->busaddr = (__u32) cmd_dma_handle;
392 temp64.val = (__u64) err_dma_handle;
393 c->ErrDesc.Addr.lower = temp64.val32.lower;
394 c->ErrDesc.Addr.upper = temp64.val32.upper;
395 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
404 * Frees a command block that was previously allocated with cmd_alloc().
406 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
413 temp64.val32.lower = c->ErrDesc.Addr.lower;
414 temp64.val32.upper = c->ErrDesc.Addr.upper;
415 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
416 c->err_info, (dma_addr_t) temp64.val);
417 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
418 c, (dma_addr_t) c->busaddr);
422 clear_bit(i&(BITS_PER_LONG-1), h->cmd_pool_bits+(i/BITS_PER_LONG));
427 static inline ctlr_info_t *get_host(struct gendisk *disk)
429 return disk->queue->queuedata;
432 static inline drive_info_struct *get_drv(struct gendisk *disk)
434 return disk->private_data;
438 * Open. Make sure the device is really there.
440 static int cciss_open(struct inode *inode, struct file *filep)
442 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
443 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
446 printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
447 #endif /* CCISS_DEBUG */
450 * Root is allowed to open raw volume zero even if it's not configured
451 * so array config can still work. I don't think I really like this,
452 * but I'm already using way to many device nodes to claim another one
453 * for "raw controller".
455 if (drv->nr_blocks == 0) {
456 if (iminor(inode) != 0)
458 if (!capable(CAP_SYS_ADMIN))
468 static int cciss_release(struct inode *inode, struct file *filep)
470 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
471 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
474 printk(KERN_DEBUG "cciss_release %s\n", inode->i_bdev->bd_disk->disk_name);
475 #endif /* CCISS_DEBUG */
483 /* for AMD 64 bit kernel compatibility with 32-bit userland ioctls */
484 extern long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg);
486 register_ioctl32_conversion(unsigned int cmd, int (*handler)(unsigned int,
487 unsigned int, unsigned long, struct file *));
488 extern int unregister_ioctl32_conversion(unsigned int cmd);
490 static int cciss_ioctl32_passthru(unsigned int fd, unsigned cmd, unsigned long arg, struct file *file);
491 static int cciss_ioctl32_big_passthru(unsigned int fd, unsigned cmd, unsigned long arg,
494 typedef int (*handler_type) (unsigned int, unsigned int, unsigned long, struct file *);
496 static struct ioctl32_map {
498 handler_type handler;
500 } cciss_ioctl32_map[] = {
501 { CCISS_GETPCIINFO, (handler_type) sys_ioctl, 0 },
502 { CCISS_GETINTINFO, (handler_type) sys_ioctl, 0 },
503 { CCISS_SETINTINFO, (handler_type) sys_ioctl, 0 },
504 { CCISS_GETNODENAME, (handler_type) sys_ioctl, 0 },
505 { CCISS_SETNODENAME, (handler_type) sys_ioctl, 0 },
506 { CCISS_GETHEARTBEAT, (handler_type) sys_ioctl, 0 },
507 { CCISS_GETBUSTYPES, (handler_type) sys_ioctl, 0 },
508 { CCISS_GETFIRMVER, (handler_type) sys_ioctl, 0 },
509 { CCISS_GETDRIVVER, (handler_type) sys_ioctl, 0 },
510 { CCISS_REVALIDVOLS, (handler_type) sys_ioctl, 0 },
511 { CCISS_PASSTHRU32, cciss_ioctl32_passthru, 0 },
512 { CCISS_DEREGDISK, (handler_type) sys_ioctl, 0 },
513 { CCISS_REGNEWDISK, (handler_type) sys_ioctl, 0 },
514 { CCISS_REGNEWD, (handler_type) sys_ioctl, 0 },
515 { CCISS_RESCANDISK, (handler_type) sys_ioctl, 0 },
516 { CCISS_GETLUNINFO, (handler_type) sys_ioctl, 0 },
517 { CCISS_BIG_PASSTHRU32, cciss_ioctl32_big_passthru, 0 },
519 #define NCCISS_IOCTL32_ENTRIES (sizeof(cciss_ioctl32_map) / sizeof(cciss_ioctl32_map[0]))
520 static void register_cciss_ioctl32(void)
524 for (i=0; i < NCCISS_IOCTL32_ENTRIES; i++) {
525 rc = register_ioctl32_conversion(
526 cciss_ioctl32_map[i].cmd,
527 cciss_ioctl32_map[i].handler);
529 printk(KERN_WARNING "cciss: failed to register "
530 "32 bit compatible ioctl 0x%08x\n",
531 cciss_ioctl32_map[i].cmd);
532 cciss_ioctl32_map[i].registered = 0;
534 cciss_ioctl32_map[i].registered = 1;
537 static void unregister_cciss_ioctl32(void)
541 for (i=0; i < NCCISS_IOCTL32_ENTRIES; i++) {
542 if (!cciss_ioctl32_map[i].registered)
544 rc = unregister_ioctl32_conversion(
545 cciss_ioctl32_map[i].cmd);
547 cciss_ioctl32_map[i].registered = 0;
550 printk(KERN_WARNING "cciss: failed to unregister "
551 "32 bit compatible ioctl 0x%08x\n",
552 cciss_ioctl32_map[i].cmd);
555 int cciss_ioctl32_passthru(unsigned int fd, unsigned cmd, unsigned long arg,
558 IOCTL32_Command_struct *arg32 =
559 (IOCTL32_Command_struct *) arg;
560 IOCTL_Command_struct arg64;
566 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
567 err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
568 err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
569 err |= get_user(arg64.buf_size, &arg32->buf_size);
570 err |= get_user(cp, &arg32->buf);
571 arg64.buf = (BYTE *)cp;
578 err = sys_ioctl(fd, CCISS_PASSTHRU, (unsigned long) &arg64);
582 err |= copy_to_user(&arg32->error_info, &arg64.error_info, sizeof(&arg32->error_info));
587 int cciss_ioctl32_big_passthru(unsigned int fd, unsigned cmd, unsigned long arg,
590 BIG_IOCTL32_Command_struct *arg32 =
591 (BIG_IOCTL32_Command_struct *) arg;
592 BIG_IOCTL_Command_struct arg64;
598 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
599 err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
600 err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
601 err |= get_user(arg64.buf_size, &arg32->buf_size);
602 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
603 err |= get_user(cp, &arg32->buf);
604 arg64.buf = (BYTE *)cp;
611 err = sys_ioctl(fd, CCISS_BIG_PASSTHRU, (unsigned long) &arg64);
615 err |= copy_to_user(&arg32->error_info, &arg64.error_info, sizeof(&arg32->error_info));
621 static inline void register_cciss_ioctl32(void) {}
622 static inline void unregister_cciss_ioctl32(void) {}
627 static int cciss_ioctl(struct inode *inode, struct file *filep,
628 unsigned int cmd, unsigned long arg)
630 struct block_device *bdev = inode->i_bdev;
631 struct gendisk *disk = bdev->bd_disk;
632 ctlr_info_t *host = get_host(disk);
633 drive_info_struct *drv = get_drv(disk);
634 int ctlr = host->ctlr;
635 void __user *argp = (void __user *)arg;
638 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
639 #endif /* CCISS_DEBUG */
644 struct hd_geometry driver_geo;
645 if (drv->cylinders) {
646 driver_geo.heads = drv->heads;
647 driver_geo.sectors = drv->sectors;
648 driver_geo.cylinders = drv->cylinders;
651 driver_geo.start= get_start_sect(inode->i_bdev);
652 if (copy_to_user(argp, &driver_geo, sizeof(struct hd_geometry)))
657 case CCISS_GETPCIINFO:
659 cciss_pci_info_struct pciinfo;
661 if (!arg) return -EINVAL;
662 pciinfo.bus = host->pdev->bus->number;
663 pciinfo.dev_fn = host->pdev->devfn;
664 pciinfo.board_id = host->board_id;
665 if (copy_to_user(argp, &pciinfo, sizeof( cciss_pci_info_struct )))
669 case CCISS_GETINTINFO:
671 cciss_coalint_struct intinfo;
672 if (!arg) return -EINVAL;
673 intinfo.delay = readl(&host->cfgtable->HostWrite.CoalIntDelay);
674 intinfo.count = readl(&host->cfgtable->HostWrite.CoalIntCount);
675 if (copy_to_user(argp, &intinfo, sizeof( cciss_coalint_struct )))
679 case CCISS_SETINTINFO:
681 cciss_coalint_struct intinfo;
685 if (!arg) return -EINVAL;
686 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
687 if (copy_from_user(&intinfo, argp, sizeof( cciss_coalint_struct)))
689 if ( (intinfo.delay == 0 ) && (intinfo.count == 0))
692 // printk("cciss_ioctl: delay and count cannot be 0\n");
695 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
696 /* Update the field, and then ring the doorbell */
697 writel( intinfo.delay,
698 &(host->cfgtable->HostWrite.CoalIntDelay));
699 writel( intinfo.count,
700 &(host->cfgtable->HostWrite.CoalIntCount));
701 writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
703 for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
704 if (!(readl(host->vaddr + SA5_DOORBELL)
707 /* delay and try again */
710 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
711 if (i >= MAX_IOCTL_CONFIG_WAIT)
715 case CCISS_GETNODENAME:
717 NodeName_type NodeName;
720 if (!arg) return -EINVAL;
722 NodeName[i] = readb(&host->cfgtable->ServerName[i]);
723 if (copy_to_user(argp, NodeName, sizeof( NodeName_type)))
727 case CCISS_SETNODENAME:
729 NodeName_type NodeName;
733 if (!arg) return -EINVAL;
734 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
736 if (copy_from_user(NodeName, argp, sizeof( NodeName_type)))
739 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
741 /* Update the field, and then ring the doorbell */
743 writeb( NodeName[i], &host->cfgtable->ServerName[i]);
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)
760 case CCISS_GETHEARTBEAT:
762 Heartbeat_type heartbeat;
764 if (!arg) return -EINVAL;
765 heartbeat = readl(&host->cfgtable->HeartBeat);
766 if (copy_to_user(argp, &heartbeat, sizeof( Heartbeat_type)))
770 case CCISS_GETBUSTYPES:
772 BusTypes_type BusTypes;
774 if (!arg) return -EINVAL;
775 BusTypes = readl(&host->cfgtable->BusTypes);
776 if (copy_to_user(argp, &BusTypes, sizeof( BusTypes_type) ))
780 case CCISS_GETFIRMVER:
782 FirmwareVer_type firmware;
784 if (!arg) return -EINVAL;
785 memcpy(firmware, host->firm_ver, 4);
787 if (copy_to_user(argp, firmware, sizeof( FirmwareVer_type)))
791 case CCISS_GETDRIVVER:
793 DriverVer_type DriverVer = DRIVER_VERSION;
795 if (!arg) return -EINVAL;
797 if (copy_to_user(argp, &DriverVer, sizeof( DriverVer_type) ))
802 case CCISS_REVALIDVOLS:
803 if (bdev != bdev->bd_contains || drv != host->drv)
805 return revalidate_allvol(host);
807 case CCISS_GETLUNINFO: {
808 LogvolInfo_struct luninfo;
811 luninfo.LunID = drv->LunID;
812 luninfo.num_opens = drv->usage_count;
813 luninfo.num_parts = 0;
814 /* count partitions 1 to 15 with sizes > 0 */
815 for(i=1; i <MAX_PART; i++) {
818 if (disk->part[i]->nr_sects != 0)
821 if (copy_to_user(argp, &luninfo,
822 sizeof(LogvolInfo_struct)))
826 case CCISS_DEREGDISK:
827 return deregister_disk(disk);
830 return register_new_disk(host);
834 IOCTL_Command_struct iocommand;
835 CommandList_struct *c;
839 DECLARE_COMPLETION(wait);
841 if (!arg) return -EINVAL;
843 if (!capable(CAP_SYS_RAWIO)) return -EPERM;
845 if (copy_from_user(&iocommand, argp, sizeof( IOCTL_Command_struct) ))
847 if((iocommand.buf_size < 1) &&
848 (iocommand.Request.Type.Direction != XFER_NONE))
852 #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
853 /* Check kmalloc limits */
854 if(iocommand.buf_size > 128000)
857 if(iocommand.buf_size > 0)
859 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
863 if (iocommand.Request.Type.Direction == XFER_WRITE)
865 /* Copy the data into the buffer we created */
866 if (copy_from_user(buff, iocommand.buf, iocommand.buf_size))
872 if ((c = cmd_alloc(host , 0)) == NULL)
877 // Fill in the command type
878 c->cmd_type = CMD_IOCTL_PEND;
879 // Fill in Command Header
880 c->Header.ReplyQueue = 0; // unused in simple mode
881 if( iocommand.buf_size > 0) // buffer to fill
883 c->Header.SGList = 1;
884 c->Header.SGTotal= 1;
885 } else // no buffers to fill
887 c->Header.SGList = 0;
888 c->Header.SGTotal= 0;
890 c->Header.LUN = iocommand.LUN_info;
891 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
893 // Fill in Request block
894 c->Request = iocommand.Request;
896 // Fill in the scatter gather information
897 if (iocommand.buf_size > 0 )
899 temp64.val = pci_map_single( host->pdev, buff,
901 PCI_DMA_BIDIRECTIONAL);
902 c->SG[0].Addr.lower = temp64.val32.lower;
903 c->SG[0].Addr.upper = temp64.val32.upper;
904 c->SG[0].Len = iocommand.buf_size;
905 c->SG[0].Ext = 0; // we are not chaining
909 /* Put the request on the tail of the request queue */
910 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
911 addQ(&host->reqQ, c);
914 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
916 wait_for_completion(&wait);
918 /* unlock the buffers from DMA */
919 temp64.val32.lower = c->SG[0].Addr.lower;
920 temp64.val32.upper = c->SG[0].Addr.upper;
921 pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
922 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
924 /* Copy the error information out */
925 iocommand.error_info = *(c->err_info);
926 if ( copy_to_user(argp, &iocommand, sizeof( IOCTL_Command_struct) ) )
929 cmd_free(host, c, 0);
933 if (iocommand.Request.Type.Direction == XFER_READ)
935 /* Copy the data out of the buffer we created */
936 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size))
939 cmd_free(host, c, 0);
944 cmd_free(host, c, 0);
947 case CCISS_BIG_PASSTHRU: {
948 BIG_IOCTL_Command_struct *ioc;
949 CommandList_struct *c;
950 unsigned char **buff = NULL;
951 int *buff_size = NULL;
957 DECLARE_COMPLETION(wait);
960 BYTE __user *data_ptr;
964 if (!capable(CAP_SYS_RAWIO))
966 ioc = (BIG_IOCTL_Command_struct *)
967 kmalloc(sizeof(*ioc), GFP_KERNEL);
972 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
976 if ((ioc->buf_size < 1) &&
977 (ioc->Request.Type.Direction != XFER_NONE)) {
981 /* Check kmalloc limits using all SGs */
982 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
986 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
990 buff = (unsigned char **) kmalloc(MAXSGENTRIES *
991 sizeof(char *), GFP_KERNEL);
996 memset(buff, 0, MAXSGENTRIES);
997 buff_size = (int *) kmalloc(MAXSGENTRIES * sizeof(int),
1003 left = ioc->buf_size;
1004 data_ptr = ioc->buf;
1006 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
1007 buff_size[sg_used] = sz;
1008 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
1009 if (buff[sg_used] == NULL) {
1013 if (ioc->Request.Type.Direction == XFER_WRITE &&
1014 copy_from_user(buff[sg_used], data_ptr, sz)) {
1022 if ((c = cmd_alloc(host , 0)) == NULL) {
1026 c->cmd_type = CMD_IOCTL_PEND;
1027 c->Header.ReplyQueue = 0;
1029 if( ioc->buf_size > 0) {
1030 c->Header.SGList = sg_used;
1031 c->Header.SGTotal= sg_used;
1033 c->Header.SGList = 0;
1034 c->Header.SGTotal= 0;
1036 c->Header.LUN = ioc->LUN_info;
1037 c->Header.Tag.lower = c->busaddr;
1039 c->Request = ioc->Request;
1040 if (ioc->buf_size > 0 ) {
1042 for(i=0; i<sg_used; i++) {
1043 temp64.val = pci_map_single( host->pdev, buff[i],
1045 PCI_DMA_BIDIRECTIONAL);
1046 c->SG[i].Addr.lower = temp64.val32.lower;
1047 c->SG[i].Addr.upper = temp64.val32.upper;
1048 c->SG[i].Len = buff_size[i];
1049 c->SG[i].Ext = 0; /* we are not chaining */
1053 /* Put the request on the tail of the request queue */
1054 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1055 addQ(&host->reqQ, c);
1058 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1059 wait_for_completion(&wait);
1060 /* unlock the buffers from DMA */
1061 for(i=0; i<sg_used; i++) {
1062 temp64.val32.lower = c->SG[i].Addr.lower;
1063 temp64.val32.upper = c->SG[i].Addr.upper;
1064 pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
1065 buff_size[i], PCI_DMA_BIDIRECTIONAL);
1067 /* Copy the error information out */
1068 ioc->error_info = *(c->err_info);
1069 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
1070 cmd_free(host, c, 0);
1074 if (ioc->Request.Type.Direction == XFER_READ) {
1075 /* Copy the data out of the buffer we created */
1076 BYTE __user *ptr = ioc->buf;
1077 for(i=0; i< sg_used; i++) {
1078 if (copy_to_user(ptr, buff[i], buff_size[i])) {
1079 cmd_free(host, c, 0);
1083 ptr += buff_size[i];
1086 cmd_free(host, c, 0);
1090 for(i=0; i<sg_used; i++)
1107 static int cciss_revalidate(struct gendisk *disk)
1109 drive_info_struct *drv = disk->private_data;
1110 set_capacity(disk, drv->nr_blocks);
1115 * revalidate_allvol is for online array config utilities. After a
1116 * utility reconfigures the drives in the array, it can use this function
1117 * (through an ioctl) to make the driver zap any previous disk structs for
1118 * that controller and get new ones.
1120 * Right now I'm using the getgeometry() function to do this, but this
1121 * function should probably be finer grained and allow you to revalidate one
1122 * particualar logical volume (instead of all of them on a particular
1125 static int revalidate_allvol(ctlr_info_t *host)
1127 int ctlr = host->ctlr, i;
1128 unsigned long flags;
1130 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1131 if (host->usage_count > 1) {
1132 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1133 printk(KERN_WARNING "cciss: Device busy for volume"
1134 " revalidation (usage=%d)\n", host->usage_count);
1137 host->usage_count++;
1138 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1140 for(i=0; i< NWD; i++) {
1141 struct gendisk *disk = host->gendisk[i];
1142 if (disk->flags & GENHD_FL_UP)
1147 * Set the partition and block size structures for all volumes
1148 * on this controller to zero. We will reread all of this data
1150 memset(host->drv, 0, sizeof(drive_info_struct)
1153 * Tell the array controller not to give us any interrupts while
1154 * we check the new geometry. Then turn interrupts back on when
1157 host->access.set_intr_mask(host, CCISS_INTR_OFF);
1158 cciss_getgeometry(ctlr);
1159 host->access.set_intr_mask(host, CCISS_INTR_ON);
1161 /* Loop through each real device */
1162 for (i = 0; i < NWD; i++) {
1163 struct gendisk *disk = host->gendisk[i];
1164 drive_info_struct *drv = &(host->drv[i]);
1165 if (!drv->nr_blocks)
1167 blk_queue_hardsect_size(host->queue, drv->block_size);
1168 set_capacity(disk, drv->nr_blocks);
1171 host->usage_count--;
1175 static int deregister_disk(struct gendisk *disk)
1177 unsigned long flags;
1178 ctlr_info_t *h = get_host(disk);
1179 drive_info_struct *drv = get_drv(disk);
1182 if (!capable(CAP_SYS_RAWIO))
1185 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1186 /* make sure logical volume is NOT is use */
1187 if( drv->usage_count > 1) {
1188 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1192 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1194 /* invalidate the devices and deregister the disk */
1195 if (disk->flags & GENHD_FL_UP)
1197 /* check to see if it was the last disk */
1198 if (drv == h->drv + h->highest_lun) {
1199 /* if so, find the new hightest lun */
1200 int i, newhighest =-1;
1201 for(i=0; i<h->highest_lun; i++) {
1202 /* if the disk has size > 0, it is available */
1203 if (h->drv[i].nr_blocks)
1206 h->highest_lun = newhighest;
1210 /* zero out the disk size info */
1212 drv->block_size = 0;
1217 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
1219 unsigned int use_unit_num, /* 0: address the controller,
1220 1: address logical volume log_unit,
1221 2: periph device address is scsi3addr */
1222 unsigned int log_unit, __u8 page_code, unsigned char *scsi3addr,
1225 ctlr_info_t *h= hba[ctlr];
1226 u64bit buff_dma_handle;
1229 c->cmd_type = CMD_IOCTL_PEND;
1230 c->Header.ReplyQueue = 0;
1232 c->Header.SGList = 1;
1233 c->Header.SGTotal= 1;
1235 c->Header.SGList = 0;
1236 c->Header.SGTotal= 0;
1238 c->Header.Tag.lower = c->busaddr;
1240 c->Request.Type.Type = cmd_type;
1241 if (cmd_type == TYPE_CMD) {
1244 /* If the logical unit number is 0 then, this is going
1245 to controller so It's a physical command
1246 mode = 0 target = 0. So we have nothing to write.
1247 otherwise, if use_unit_num == 1,
1248 mode = 1(volume set addressing) target = LUNID
1249 otherwise, if use_unit_num == 2,
1250 mode = 0(periph dev addr) target = scsi3addr */
1251 if (use_unit_num == 1) {
1252 c->Header.LUN.LogDev.VolId=
1253 h->drv[log_unit].LunID;
1254 c->Header.LUN.LogDev.Mode = 1;
1255 } else if (use_unit_num == 2) {
1256 memcpy(c->Header.LUN.LunAddrBytes,scsi3addr,8);
1257 c->Header.LUN.LogDev.Mode = 0;
1259 /* are we trying to read a vital product page */
1260 if(page_code != 0) {
1261 c->Request.CDB[1] = 0x01;
1262 c->Request.CDB[2] = page_code;
1264 c->Request.CDBLen = 6;
1265 c->Request.Type.Attribute = ATTR_SIMPLE;
1266 c->Request.Type.Direction = XFER_READ;
1267 c->Request.Timeout = 0;
1268 c->Request.CDB[0] = CISS_INQUIRY;
1269 c->Request.CDB[4] = size & 0xFF;
1271 case CISS_REPORT_LOG:
1272 case CISS_REPORT_PHYS:
1273 /* Talking to controller so It's a physical command
1274 mode = 00 target = 0. Nothing to write.
1276 c->Request.CDBLen = 12;
1277 c->Request.Type.Attribute = ATTR_SIMPLE;
1278 c->Request.Type.Direction = XFER_READ;
1279 c->Request.Timeout = 0;
1280 c->Request.CDB[0] = cmd;
1281 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
1282 c->Request.CDB[7] = (size >> 16) & 0xFF;
1283 c->Request.CDB[8] = (size >> 8) & 0xFF;
1284 c->Request.CDB[9] = size & 0xFF;
1287 case CCISS_READ_CAPACITY:
1288 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1289 c->Header.LUN.LogDev.Mode = 1;
1290 c->Request.CDBLen = 10;
1291 c->Request.Type.Attribute = ATTR_SIMPLE;
1292 c->Request.Type.Direction = XFER_READ;
1293 c->Request.Timeout = 0;
1294 c->Request.CDB[0] = cmd;
1296 case CCISS_CACHE_FLUSH:
1297 c->Request.CDBLen = 12;
1298 c->Request.Type.Attribute = ATTR_SIMPLE;
1299 c->Request.Type.Direction = XFER_WRITE;
1300 c->Request.Timeout = 0;
1301 c->Request.CDB[0] = BMIC_WRITE;
1302 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
1306 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
1309 } else if (cmd_type == TYPE_MSG) {
1311 case 3: /* No-Op message */
1312 c->Request.CDBLen = 1;
1313 c->Request.Type.Attribute = ATTR_SIMPLE;
1314 c->Request.Type.Direction = XFER_WRITE;
1315 c->Request.Timeout = 0;
1316 c->Request.CDB[0] = cmd;
1320 "cciss%d: unknown message type %d\n",
1326 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
1329 /* Fill in the scatter gather information */
1331 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
1332 buff, size, PCI_DMA_BIDIRECTIONAL);
1333 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1334 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1335 c->SG[0].Len = size;
1336 c->SG[0].Ext = 0; /* we are not chaining */
1340 static int sendcmd_withirq(__u8 cmd,
1344 unsigned int use_unit_num,
1345 unsigned int log_unit,
1349 ctlr_info_t *h = hba[ctlr];
1350 CommandList_struct *c;
1351 u64bit buff_dma_handle;
1352 unsigned long flags;
1354 DECLARE_COMPLETION(wait);
1356 if ((c = cmd_alloc(h , 0)) == NULL)
1358 return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1359 log_unit, page_code, NULL, cmd_type);
1360 if (return_status != IO_OK) {
1362 return return_status;
1367 /* Put the request on the tail of the queue and send it */
1368 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1372 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1374 wait_for_completion(&wait);
1376 if(c->err_info->CommandStatus != 0)
1377 { /* an error has occurred */
1378 switch(c->err_info->CommandStatus)
1380 case CMD_TARGET_STATUS:
1381 printk(KERN_WARNING "cciss: cmd %p has "
1382 " completed with errors\n", c);
1383 if( c->err_info->ScsiStatus)
1385 printk(KERN_WARNING "cciss: cmd %p "
1386 "has SCSI Status = %x\n",
1388 c->err_info->ScsiStatus);
1392 case CMD_DATA_UNDERRUN:
1393 case CMD_DATA_OVERRUN:
1394 /* expected for inquire and report lun commands */
1397 printk(KERN_WARNING "cciss: Cmd %p is "
1398 "reported invalid\n", c);
1399 return_status = IO_ERROR;
1401 case CMD_PROTOCOL_ERR:
1402 printk(KERN_WARNING "cciss: cmd %p has "
1403 "protocol error \n", c);
1404 return_status = IO_ERROR;
1406 case CMD_HARDWARE_ERR:
1407 printk(KERN_WARNING "cciss: cmd %p had "
1408 " hardware error\n", c);
1409 return_status = IO_ERROR;
1411 case CMD_CONNECTION_LOST:
1412 printk(KERN_WARNING "cciss: cmd %p had "
1413 "connection lost\n", c);
1414 return_status = IO_ERROR;
1417 printk(KERN_WARNING "cciss: cmd %p was "
1419 return_status = IO_ERROR;
1421 case CMD_ABORT_FAILED:
1422 printk(KERN_WARNING "cciss: cmd %p reports "
1423 "abort failed\n", c);
1424 return_status = IO_ERROR;
1426 case CMD_UNSOLICITED_ABORT:
1428 "cciss%d: unsolicited abort %p\n",
1430 if (c->retry_count < MAX_CMD_RETRIES) {
1432 "cciss%d: retrying %p\n",
1435 /* erase the old error information */
1436 memset(c->err_info, 0,
1437 sizeof(ErrorInfo_struct));
1438 return_status = IO_OK;
1439 INIT_COMPLETION(wait);
1442 return_status = IO_ERROR;
1445 printk(KERN_WARNING "cciss: cmd %p returned "
1446 "unknown status %x\n", c,
1447 c->err_info->CommandStatus);
1448 return_status = IO_ERROR;
1451 /* unlock the buffers from DMA */
1452 pci_unmap_single( h->pdev, (dma_addr_t) buff_dma_handle.val,
1453 size, PCI_DMA_BIDIRECTIONAL);
1455 return(return_status);
1458 static void cciss_geometry_inquiry(int ctlr, int logvol,
1459 int withirq, unsigned int total_size,
1460 unsigned int block_size, InquiryData_struct *inq_buff,
1461 drive_info_struct *drv)
1464 memset(inq_buff, 0, sizeof(InquiryData_struct));
1466 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
1467 inq_buff, sizeof(*inq_buff), 1, logvol ,0xC1, TYPE_CMD);
1469 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
1470 sizeof(*inq_buff), 1, logvol ,0xC1, NULL, TYPE_CMD);
1471 if (return_code == IO_OK) {
1472 if(inq_buff->data_byte[8] == 0xFF) {
1474 "cciss: reading geometry failed, volume "
1475 "does not support reading geometry\n");
1476 drv->block_size = block_size;
1477 drv->nr_blocks = total_size;
1479 drv->sectors = 32; // Sectors per track
1480 drv->cylinders = total_size / 255 / 32;
1482 drv->block_size = block_size;
1483 drv->nr_blocks = total_size;
1484 drv->heads = inq_buff->data_byte[6];
1485 drv->sectors = inq_buff->data_byte[7];
1486 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
1487 drv->cylinders += inq_buff->data_byte[5];
1489 } else { /* Get geometry failed */
1490 printk(KERN_WARNING "cciss: reading geometry failed, "
1491 "continuing with default geometry\n");
1492 drv->block_size = block_size;
1493 drv->nr_blocks = total_size;
1495 drv->sectors = 32; // Sectors per track
1496 drv->cylinders = total_size / 255 / 32;
1498 printk(KERN_INFO " heads= %d, sectors= %d, cylinders= %d\n\n",
1499 drv->heads, drv->sectors, drv->cylinders);
1502 cciss_read_capacity(int ctlr, int logvol, ReadCapdata_struct *buf,
1503 int withirq, unsigned int *total_size, unsigned int *block_size)
1506 memset(buf, 0, sizeof(*buf));
1508 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
1509 ctlr, buf, sizeof(*buf), 1, logvol, 0, TYPE_CMD);
1511 return_code = sendcmd(CCISS_READ_CAPACITY,
1512 ctlr, buf, sizeof(*buf), 1, logvol, 0, NULL, TYPE_CMD);
1513 if (return_code == IO_OK) {
1514 *total_size = be32_to_cpu(*((__u32 *) &buf->total_size[0]))+1;
1515 *block_size = be32_to_cpu(*((__u32 *) &buf->block_size[0]));
1516 } else { /* read capacity command failed */
1517 printk(KERN_WARNING "cciss: read capacity failed\n");
1519 *block_size = BLOCK_SIZE;
1521 printk(KERN_INFO " blocks= %u block_size= %d\n",
1522 *total_size, *block_size);
1525 static int register_new_disk(ctlr_info_t *h)
1527 struct gendisk *disk;
1532 int new_lun_found = 0;
1533 int new_lun_index = 0;
1534 int free_index_found = 0;
1536 ReportLunData_struct *ld_buff = NULL;
1537 ReadCapdata_struct *size_buff = NULL;
1538 InquiryData_struct *inq_buff = NULL;
1542 unsigned int block_size;
1543 unsigned int total_size;
1545 if (!capable(CAP_SYS_RAWIO))
1547 /* if we have no space in our disk array left to add anything */
1548 if( h->num_luns >= CISS_MAX_LUN)
1551 ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1552 if (ld_buff == NULL)
1554 memset(ld_buff, 0, sizeof(ReportLunData_struct));
1555 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
1556 if (size_buff == NULL)
1558 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1559 if (inq_buff == NULL)
1562 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
1563 sizeof(ReportLunData_struct), 0, 0, 0, TYPE_CMD);
1565 if( return_code == IO_OK)
1568 // printk("LUN Data\n--------------------------\n");
1570 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
1571 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
1572 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
1573 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
1574 } else /* reading number of logical volumes failed */
1576 printk(KERN_WARNING "cciss: report logical volume"
1577 " command failed\n");
1581 num_luns = listlength / 8; // 8 bytes pre entry
1582 if (num_luns > CISS_MAX_LUN)
1584 num_luns = CISS_MAX_LUN;
1587 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
1588 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
1589 ld_buff->LUNListLength[3], num_luns);
1591 for(i=0; i< num_luns; i++)
1594 int lunID_found = 0;
1596 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
1597 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
1598 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
1599 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
1601 /* check to see if this is a new lun */
1602 for(j=0; j <= h->highest_lun; j++)
1605 printk("Checking %d %x against %x\n", j,h->drv[j].LunID,
1607 #endif /* CCISS_DEBUG */
1608 if (h->drv[j].LunID == lunid)
1615 if( lunID_found == 1)
1618 { /* It is the new lun we have been looking for */
1620 printk("new lun found at %d\n", i);
1621 #endif /* CCISS_DEBUG */
1629 printk(KERN_WARNING "cciss: New Logical Volume not found\n");
1632 /* Now find the free index */
1633 for(i=0; i <CISS_MAX_LUN; i++)
1636 printk("Checking Index %d\n", i);
1637 #endif /* CCISS_DEBUG */
1638 if(h->drv[i].LunID == 0)
1641 printk("free index found at %d\n", i);
1642 #endif /* CCISS_DEBUG */
1643 free_index_found = 1;
1648 if (!free_index_found)
1650 printk(KERN_WARNING "cciss: unable to find free slot for disk\n");
1654 logvol = free_index;
1655 h->drv[logvol].LunID = lunid;
1656 /* there could be gaps in lun numbers, track hightest */
1657 if(h->highest_lun < lunid)
1658 h->highest_lun = logvol;
1659 cciss_read_capacity(ctlr, logvol, size_buff, 1,
1660 &total_size, &block_size);
1661 cciss_geometry_inquiry(ctlr, logvol, 1, total_size, block_size,
1662 inq_buff, &h->drv[logvol]);
1663 h->drv[logvol].usage_count = 0;
1665 /* setup partitions per disk */
1666 disk = h->gendisk[logvol];
1667 set_capacity(disk, h->drv[logvol].nr_blocks);
1675 printk(KERN_ERR "cciss: out of memory\n");
1681 * Wait polling for a command to complete.
1682 * The memory mapped FIFO is polled for the completion.
1683 * Used only at init time, interrupts from the HBA are disabled.
1685 static unsigned long pollcomplete(int ctlr)
1690 /* Wait (up to 20 seconds) for a command to complete */
1692 for (i = 20 * HZ; i > 0; i--) {
1693 done = hba[ctlr]->access.command_completed(hba[ctlr]);
1694 if (done == FIFO_EMPTY) {
1695 set_current_state(TASK_UNINTERRUPTIBLE);
1696 schedule_timeout(1);
1700 /* Invalid address to tell caller we ran out of time */
1704 * Send a command to the controller, and wait for it to complete.
1705 * Only used at init time.
1712 unsigned int use_unit_num, /* 0: address the controller,
1713 1: address logical volume log_unit,
1714 2: periph device address is scsi3addr */
1715 unsigned int log_unit,
1717 unsigned char *scsi3addr,
1720 CommandList_struct *c;
1722 unsigned long complete;
1723 ctlr_info_t *info_p= hba[ctlr];
1724 u64bit buff_dma_handle;
1727 if ((c = cmd_alloc(info_p, 1)) == NULL) {
1728 printk(KERN_WARNING "cciss: unable to get memory");
1731 status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1732 log_unit, page_code, scsi3addr, cmd_type);
1733 if (status != IO_OK) {
1734 cmd_free(info_p, c, 1);
1742 printk(KERN_DEBUG "cciss: turning intr off\n");
1743 #endif /* CCISS_DEBUG */
1744 info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
1746 /* Make sure there is room in the command FIFO */
1747 /* Actually it should be completely empty at this time. */
1748 for (i = 200000; i > 0; i--)
1750 /* if fifo isn't full go */
1751 if (!(info_p->access.fifo_full(info_p)))
1757 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
1758 " waiting!\n", ctlr);
1763 info_p->access.submit_command(info_p, c);
1764 complete = pollcomplete(ctlr);
1767 printk(KERN_DEBUG "cciss: command completed\n");
1768 #endif /* CCISS_DEBUG */
1770 if (complete != 1) {
1771 if ( (complete & CISS_ERROR_BIT)
1772 && (complete & ~CISS_ERROR_BIT) == c->busaddr)
1774 /* if data overrun or underun on Report command
1777 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
1778 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
1779 (c->Request.CDB[0] == CISS_INQUIRY)) &&
1780 ((c->err_info->CommandStatus ==
1781 CMD_DATA_OVERRUN) ||
1782 (c->err_info->CommandStatus ==
1786 complete = c->busaddr;
1788 if (c->err_info->CommandStatus ==
1789 CMD_UNSOLICITED_ABORT) {
1790 printk(KERN_WARNING "cciss%d: "
1791 "unsolicited abort %p\n",
1793 if (c->retry_count < MAX_CMD_RETRIES) {
1795 "cciss%d: retrying %p\n",
1798 /* erase the old error */
1800 memset(c->err_info, 0,
1801 sizeof(ErrorInfo_struct));
1805 "cciss%d: retried %p too "
1806 "many times\n", ctlr, c);
1811 printk(KERN_WARNING "ciss ciss%d: sendcmd"
1812 " Error %x \n", ctlr,
1813 c->err_info->CommandStatus);
1814 printk(KERN_WARNING "ciss ciss%d: sendcmd"
1816 " size %x\n num %x value %x\n", ctlr,
1817 c->err_info->MoreErrInfo.Invalid_Cmd.offense_size,
1818 c->err_info->MoreErrInfo.Invalid_Cmd.offense_num,
1819 c->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
1824 if (complete != c->busaddr) {
1825 printk( KERN_WARNING "cciss cciss%d: SendCmd "
1826 "Invalid command list address returned! (%lx)\n",
1832 printk( KERN_WARNING
1833 "cciss cciss%d: SendCmd Timeout out, "
1834 "No command list address returned!\n",
1840 /* unlock the data buffer from DMA */
1841 pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
1842 size, PCI_DMA_BIDIRECTIONAL);
1843 cmd_free(info_p, c, 1);
1847 * Map (physical) PCI mem into (virtual) kernel space
1849 static ulong remap_pci_mem(ulong base, ulong size)
1851 ulong page_base = ((ulong) base) & PAGE_MASK;
1852 ulong page_offs = ((ulong) base) - page_base;
1853 ulong page_remapped = (ulong) ioremap(page_base, page_offs+size);
1855 return (ulong) (page_remapped ? (page_remapped + page_offs) : 0UL);
1859 * Takes jobs of the Q and sends them to the hardware, then puts it on
1860 * the Q to wait for completion.
1862 static void start_io( ctlr_info_t *h)
1864 CommandList_struct *c;
1866 while(( c = h->reqQ) != NULL )
1868 /* can't do anything if fifo is full */
1869 if ((h->access.fifo_full(h))) {
1870 printk(KERN_WARNING "cciss: fifo full\n");
1874 /* Get the frist entry from the Request Q */
1875 removeQ(&(h->reqQ), c);
1878 /* Tell the controller execute command */
1879 h->access.submit_command(h, c);
1881 /* Put job onto the completed Q */
1882 addQ (&(h->cmpQ), c);
1886 static inline void complete_buffers(struct bio *bio, int status)
1889 struct bio *xbh = bio->bi_next;
1890 int nr_sectors = bio_sectors(bio);
1892 bio->bi_next = NULL;
1893 blk_finished_io(len);
1894 bio_endio(bio, nr_sectors << 9, status ? 0 : -EIO);
1899 /* Assumes that CCISS_LOCK(h->ctlr) is held. */
1900 /* Zeros out the error record and then resends the command back */
1901 /* to the controller */
1902 static inline void resend_cciss_cmd( ctlr_info_t *h, CommandList_struct *c)
1904 /* erase the old error information */
1905 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
1907 /* add it to software queue and then send it to the controller */
1910 if(h->Qdepth > h->maxQsinceinit)
1911 h->maxQsinceinit = h->Qdepth;
1915 /* checks the status of the job and calls complete buffers to mark all
1916 * buffers for the completed job.
1918 static inline void complete_command( ctlr_info_t *h, CommandList_struct *cmd,
1929 if(cmd->err_info->CommandStatus != 0)
1930 { /* an error has occurred */
1931 switch(cmd->err_info->CommandStatus)
1933 unsigned char sense_key;
1934 case CMD_TARGET_STATUS:
1937 if( cmd->err_info->ScsiStatus == 0x02)
1939 printk(KERN_WARNING "cciss: cmd %p "
1940 "has CHECK CONDITION "
1941 " byte 2 = 0x%x\n", cmd,
1942 cmd->err_info->SenseInfo[2]
1944 /* check the sense key */
1946 cmd->err_info->SenseInfo[2];
1947 /* no status or recovered error */
1948 if((sense_key == 0x0) ||
1955 printk(KERN_WARNING "cciss: cmd %p "
1956 "has SCSI Status 0x%x\n",
1957 cmd, cmd->err_info->ScsiStatus);
1960 case CMD_DATA_UNDERRUN:
1961 printk(KERN_WARNING "cciss: cmd %p has"
1962 " completed with data underrun "
1965 case CMD_DATA_OVERRUN:
1966 printk(KERN_WARNING "cciss: cmd %p has"
1967 " completed with data overrun "
1971 printk(KERN_WARNING "cciss: cmd %p is "
1972 "reported invalid\n", cmd);
1975 case CMD_PROTOCOL_ERR:
1976 printk(KERN_WARNING "cciss: cmd %p has "
1977 "protocol error \n", cmd);
1980 case CMD_HARDWARE_ERR:
1981 printk(KERN_WARNING "cciss: cmd %p had "
1982 " hardware error\n", cmd);
1985 case CMD_CONNECTION_LOST:
1986 printk(KERN_WARNING "cciss: cmd %p had "
1987 "connection lost\n", cmd);
1991 printk(KERN_WARNING "cciss: cmd %p was "
1995 case CMD_ABORT_FAILED:
1996 printk(KERN_WARNING "cciss: cmd %p reports "
1997 "abort failed\n", cmd);
2000 case CMD_UNSOLICITED_ABORT:
2001 printk(KERN_WARNING "cciss%d: unsolicited "
2002 "abort %p\n", h->ctlr, cmd);
2003 if (cmd->retry_count < MAX_CMD_RETRIES) {
2006 "cciss%d: retrying %p\n",
2011 "cciss%d: %p retried too "
2012 "many times\n", h->ctlr, cmd);
2016 printk(KERN_WARNING "cciss: cmd %p timedout\n",
2021 printk(KERN_WARNING "cciss: cmd %p returned "
2022 "unknown status %x\n", cmd,
2023 cmd->err_info->CommandStatus);
2027 /* We need to return this command */
2029 resend_cciss_cmd(h,cmd);
2032 /* command did not need to be retried */
2033 /* unmap the DMA mapping for all the scatter gather elements */
2034 for(i=0; i<cmd->Header.SGList; i++) {
2035 temp64.val32.lower = cmd->SG[i].Addr.lower;
2036 temp64.val32.upper = cmd->SG[i].Addr.upper;
2037 pci_unmap_page(hba[cmd->ctlr]->pdev,
2038 temp64.val, cmd->SG[i].Len,
2039 (cmd->Request.Type.Direction == XFER_READ) ?
2040 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
2042 complete_buffers(cmd->rq->bio, status);
2045 printk("Done with %p\n", cmd->rq);
2046 #endif /* CCISS_DEBUG */
2048 end_that_request_last(cmd->rq);
2053 * Get a request and submit it to the controller.
2055 static void do_cciss_request(request_queue_t *q)
2057 ctlr_info_t *h= q->queuedata;
2058 CommandList_struct *c;
2060 struct request *creq;
2062 struct scatterlist tmp_sg[MAXSGENTRIES];
2063 drive_info_struct *drv;
2066 if (blk_queue_plugged(q))
2070 creq = elv_next_request(q);
2074 if (creq->nr_phys_segments > MAXSGENTRIES)
2077 if (( c = cmd_alloc(h, 1)) == NULL)
2080 blkdev_dequeue_request(creq);
2082 spin_unlock_irq(q->queue_lock);
2084 c->cmd_type = CMD_RWREQ;
2087 /* fill in the request */
2088 drv = creq->rq_disk->private_data;
2089 c->Header.ReplyQueue = 0; // unused in simple mode
2090 c->Header.Tag.lower = c->busaddr; // use the physical address the cmd block for tag
2091 c->Header.LUN.LogDev.VolId= drv->LunID;
2092 c->Header.LUN.LogDev.Mode = 1;
2093 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
2094 c->Request.Type.Type = TYPE_CMD; // It is a command.
2095 c->Request.Type.Attribute = ATTR_SIMPLE;
2096 c->Request.Type.Direction =
2097 (rq_data_dir(creq) == READ) ? XFER_READ: XFER_WRITE;
2098 c->Request.Timeout = 0; // Don't time out
2099 c->Request.CDB[0] = (rq_data_dir(creq) == READ) ? CCISS_READ : CCISS_WRITE;
2100 start_blk = creq->sector;
2102 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",(int) creq->sector,
2103 (int) creq->nr_sectors);
2104 #endif /* CCISS_DEBUG */
2106 seg = blk_rq_map_sg(q, creq, tmp_sg);
2108 /* get the DMA records for the setup */
2109 if (c->Request.Type.Direction == XFER_READ)
2110 dir = PCI_DMA_FROMDEVICE;
2112 dir = PCI_DMA_TODEVICE;
2114 for (i=0; i<seg; i++)
2116 c->SG[i].Len = tmp_sg[i].length;
2117 temp64.val = (__u64) pci_map_page(h->pdev, tmp_sg[i].page,
2118 tmp_sg[i].offset, tmp_sg[i].length,
2120 c->SG[i].Addr.lower = temp64.val32.lower;
2121 c->SG[i].Addr.upper = temp64.val32.upper;
2122 c->SG[i].Ext = 0; // we are not chaining
2124 /* track how many SG entries we are using */
2129 printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n", creq->nr_sectors, seg);
2130 #endif /* CCISS_DEBUG */
2132 c->Header.SGList = c->Header.SGTotal = seg;
2133 c->Request.CDB[1]= 0;
2134 c->Request.CDB[2]= (start_blk >> 24) & 0xff; //MSB
2135 c->Request.CDB[3]= (start_blk >> 16) & 0xff;
2136 c->Request.CDB[4]= (start_blk >> 8) & 0xff;
2137 c->Request.CDB[5]= start_blk & 0xff;
2138 c->Request.CDB[6]= 0; // (sect >> 24) & 0xff; MSB
2139 c->Request.CDB[7]= (creq->nr_sectors >> 8) & 0xff;
2140 c->Request.CDB[8]= creq->nr_sectors & 0xff;
2141 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2143 spin_lock_irq(q->queue_lock);
2147 if(h->Qdepth > h->maxQsinceinit)
2148 h->maxQsinceinit = h->Qdepth;
2157 static irqreturn_t do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs)
2159 ctlr_info_t *h = dev_id;
2160 CommandList_struct *c;
2161 unsigned long flags;
2165 /* Is this interrupt for us? */
2166 if (( h->access.intr_pending(h) == 0) || (h->interrupts_enabled == 0))
2170 * If there are completed commands in the completion queue,
2171 * we had better do something about it.
2173 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2174 while( h->access.intr_pending(h))
2176 while((a = h->access.command_completed(h)) != FIFO_EMPTY)
2180 if ((c = h->cmpQ) == NULL)
2182 printk(KERN_WARNING "cciss: Completion of %08lx ignored\n", (unsigned long)a1);
2185 while(c->busaddr != a) {
2191 * If we've found the command, take it off the
2192 * completion Q and free it
2194 if (c->busaddr == a) {
2195 removeQ(&h->cmpQ, c);
2196 if (c->cmd_type == CMD_RWREQ) {
2197 complete_command(h, c, 0);
2198 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2199 complete(c->waiting);
2201 # ifdef CONFIG_CISS_SCSI_TAPE
2202 else if (c->cmd_type == CMD_SCSI)
2203 complete_scsi_command(c, 0, a1);
2211 * See if we can queue up some more IO
2213 blk_start_queue(h->queue);
2214 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2218 * We cannot read the structure directly, for portablity we must use
2220 * This is for debug only.
2223 static void print_cfg_table( CfgTable_struct *tb)
2228 printk("Controller Configuration information\n");
2229 printk("------------------------------------\n");
2231 temp_name[i] = readb(&(tb->Signature[i]));
2233 printk(" Signature = %s\n", temp_name);
2234 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
2235 printk(" Transport methods supported = 0x%x\n",
2236 readl(&(tb-> TransportSupport)));
2237 printk(" Transport methods active = 0x%x\n",
2238 readl(&(tb->TransportActive)));
2239 printk(" Requested transport Method = 0x%x\n",
2240 readl(&(tb->HostWrite.TransportRequest)));
2241 printk(" Coalese Interrupt Delay = 0x%x\n",
2242 readl(&(tb->HostWrite.CoalIntDelay)));
2243 printk(" Coalese Interrupt Count = 0x%x\n",
2244 readl(&(tb->HostWrite.CoalIntCount)));
2245 printk(" Max outstanding commands = 0x%d\n",
2246 readl(&(tb->CmdsOutMax)));
2247 printk(" Bus Types = 0x%x\n", readl(&(tb-> BusTypes)));
2249 temp_name[i] = readb(&(tb->ServerName[i]));
2250 temp_name[16] = '\0';
2251 printk(" Server Name = %s\n", temp_name);
2252 printk(" Heartbeat Counter = 0x%x\n\n\n",
2253 readl(&(tb->HeartBeat)));
2255 #endif /* CCISS_DEBUG */
2257 static void release_io_mem(ctlr_info_t *c)
2259 /* if IO mem was not protected do nothing */
2260 if( c->io_mem_addr == 0)
2262 release_region(c->io_mem_addr, c->io_mem_length);
2264 c->io_mem_length = 0;
2267 static int find_PCI_BAR_index(struct pci_dev *pdev,
2268 unsigned long pci_bar_addr)
2270 int i, offset, mem_type, bar_type;
2271 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
2274 for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
2275 bar_type = pci_resource_flags(pdev, i) &
2276 PCI_BASE_ADDRESS_SPACE;
2277 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
2280 mem_type = pci_resource_flags(pdev, i) &
2281 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
2283 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2284 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
2285 offset += 4; /* 32 bit */
2287 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2290 default: /* reserved in PCI 2.2 */
2291 printk(KERN_WARNING "Base address is invalid\n");
2296 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
2302 static int cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
2304 ushort subsystem_vendor_id, subsystem_device_id, command;
2305 unchar irq = pdev->irq;
2306 __u32 board_id, scratchpad = 0;
2308 __u32 cfg_base_addr;
2309 __u64 cfg_base_addr_index;
2312 /* check to see if controller has been disabled */
2313 /* BEFORE trying to enable it */
2314 (void) pci_read_config_word(pdev, PCI_COMMAND,&command);
2315 if(!(command & 0x02))
2317 printk(KERN_WARNING "cciss: controller appears to be disabled\n");
2321 if (pci_enable_device(pdev))
2323 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
2326 if (pci_set_dma_mask(pdev, CCISS_DMA_MASK ) != 0)
2328 printk(KERN_ERR "cciss: Unable to set DMA mask\n");
2332 subsystem_vendor_id = pdev->subsystem_vendor;
2333 subsystem_device_id = pdev->subsystem_device;
2334 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
2335 subsystem_vendor_id);
2337 /* search for our IO range so we can protect it */
2338 for(i=0; i<DEVICE_COUNT_RESOURCE; i++)
2340 /* is this an IO range */
2341 if( pci_resource_flags(pdev, i) & 0x01 ) {
2342 c->io_mem_addr = pci_resource_start(pdev, i);
2343 c->io_mem_length = pci_resource_end(pdev, i) -
2344 pci_resource_start(pdev, i) +1;
2346 printk("IO value found base_addr[%d] %lx %lx\n", i,
2347 c->io_mem_addr, c->io_mem_length);
2348 #endif /* CCISS_DEBUG */
2349 /* register the IO range */
2350 if(!request_region( c->io_mem_addr,
2351 c->io_mem_length, "cciss"))
2353 printk(KERN_WARNING "cciss I/O memory range already in use addr=%lx length=%ld\n",
2354 c->io_mem_addr, c->io_mem_length);
2356 c->io_mem_length = 0;
2363 printk("command = %x\n", command);
2364 printk("irq = %x\n", irq);
2365 printk("board_id = %x\n", board_id);
2366 #endif /* CCISS_DEBUG */
2371 * Memory base addr is first addr , the second points to the config
2375 c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
2377 printk("address 0 = %x\n", c->paddr);
2378 #endif /* CCISS_DEBUG */
2379 c->vaddr = remap_pci_mem(c->paddr, 200);
2381 /* Wait for the board to become ready. (PCI hotplug needs this.)
2382 * We poll for up to 120 secs, once per 100ms. */
2383 for (i=0; i < 1200; i++) {
2384 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
2385 if (scratchpad == CCISS_FIRMWARE_READY)
2387 set_current_state(TASK_INTERRUPTIBLE);
2388 schedule_timeout(HZ / 10); /* wait 100ms */
2390 if (scratchpad != CCISS_FIRMWARE_READY) {
2391 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
2395 /* get the address index number */
2396 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
2397 cfg_base_addr &= (__u32) 0x0000ffff;
2399 printk("cfg base address = %x\n", cfg_base_addr);
2400 #endif /* CCISS_DEBUG */
2401 cfg_base_addr_index =
2402 find_PCI_BAR_index(pdev, cfg_base_addr);
2404 printk("cfg base address index = %x\n", cfg_base_addr_index);
2405 #endif /* CCISS_DEBUG */
2406 if (cfg_base_addr_index == -1) {
2407 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
2412 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
2414 printk("cfg offset = %x\n", cfg_offset);
2415 #endif /* CCISS_DEBUG */
2416 c->cfgtable = (CfgTable_struct *)
2417 remap_pci_mem(pci_resource_start(pdev, cfg_base_addr_index)
2418 + cfg_offset, sizeof(CfgTable_struct));
2419 c->board_id = board_id;
2422 print_cfg_table(c->cfgtable);
2423 #endif /* CCISS_DEBUG */
2425 for(i=0; i<NR_PRODUCTS; i++) {
2426 if (board_id == products[i].board_id) {
2427 c->product_name = products[i].product_name;
2428 c->access = *(products[i].access);
2432 if (i == NR_PRODUCTS) {
2433 printk(KERN_WARNING "cciss: Sorry, I don't know how"
2434 " to access the Smart Array controller %08lx\n",
2435 (unsigned long)board_id);
2438 if ( (readb(&c->cfgtable->Signature[0]) != 'C') ||
2439 (readb(&c->cfgtable->Signature[1]) != 'I') ||
2440 (readb(&c->cfgtable->Signature[2]) != 'S') ||
2441 (readb(&c->cfgtable->Signature[3]) != 'S') )
2443 printk("Does not appear to be a valid CISS config table\n");
2449 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
2451 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
2453 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
2458 printk("Trying to put board into Simple mode\n");
2459 #endif /* CCISS_DEBUG */
2460 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
2461 /* Update the field, and then ring the doorbell */
2462 writel( CFGTBL_Trans_Simple,
2463 &(c->cfgtable->HostWrite.TransportRequest));
2464 writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
2466 /* under certain very rare conditions, this can take awhile.
2467 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
2468 * as we enter this code.) */
2469 for(i=0;i<MAX_CONFIG_WAIT;i++) {
2470 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
2472 /* delay and try again */
2473 set_current_state(TASK_INTERRUPTIBLE);
2474 schedule_timeout(10);
2478 printk(KERN_DEBUG "I counter got to %d %x\n", i, readl(c->vaddr + SA5_DOORBELL));
2479 #endif /* CCISS_DEBUG */
2481 print_cfg_table(c->cfgtable);
2482 #endif /* CCISS_DEBUG */
2484 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
2486 printk(KERN_WARNING "cciss: unable to get board into"
2495 * Gets information about the local volumes attached to the controller.
2497 static void cciss_getgeometry(int cntl_num)
2499 ReportLunData_struct *ld_buff;
2500 ReadCapdata_struct *size_buff;
2501 InquiryData_struct *inq_buff;
2509 ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
2510 if (ld_buff == NULL)
2512 printk(KERN_ERR "cciss: out of memory\n");
2515 memset(ld_buff, 0, sizeof(ReportLunData_struct));
2516 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
2517 if (size_buff == NULL)
2519 printk(KERN_ERR "cciss: out of memory\n");
2523 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
2524 if (inq_buff == NULL)
2526 printk(KERN_ERR "cciss: out of memory\n");
2531 /* Get the firmware version */
2532 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
2533 sizeof(InquiryData_struct), 0, 0 ,0, NULL, TYPE_CMD);
2534 if (return_code == IO_OK)
2536 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
2537 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
2538 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
2539 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
2540 } else /* send command failed */
2542 printk(KERN_WARNING "cciss: unable to determine firmware"
2543 " version of controller\n");
2545 /* Get the number of logical volumes */
2546 return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
2547 sizeof(ReportLunData_struct), 0, 0, 0, NULL, TYPE_CMD);
2549 if( return_code == IO_OK)
2552 printk("LUN Data\n--------------------------\n");
2553 #endif /* CCISS_DEBUG */
2555 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
2556 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
2557 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
2558 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
2559 } else /* reading number of logical volumes failed */
2561 printk(KERN_WARNING "cciss: report logical volume"
2562 " command failed\n");
2565 hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
2566 if (hba[cntl_num]->num_luns > CISS_MAX_LUN)
2568 printk(KERN_ERR "ciss: only %d number of logical volumes supported\n",
2570 hba[cntl_num]->num_luns = CISS_MAX_LUN;
2573 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
2574 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
2575 ld_buff->LUNListLength[3], hba[cntl_num]->num_luns);
2576 #endif /* CCISS_DEBUG */
2578 hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns-1;
2579 for(i=0; i< hba[cntl_num]->num_luns; i++)
2582 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
2583 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
2584 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
2585 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
2587 hba[cntl_num]->drv[i].LunID = lunid;
2591 printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
2592 ld_buff->LUN[i][0], ld_buff->LUN[i][1],ld_buff->LUN[i][2],
2593 ld_buff->LUN[i][3], hba[cntl_num]->drv[i].LunID);
2594 #endif /* CCISS_DEBUG */
2595 cciss_read_capacity(cntl_num, i, size_buff, 0,
2596 &total_size, &block_size);
2597 cciss_geometry_inquiry(cntl_num, i, 0, total_size, block_size,
2598 inq_buff, &hba[cntl_num]->drv[i]);
2605 /* Function to find the first free pointer into our hba[] array */
2606 /* Returns -1 if no free entries are left. */
2607 static int alloc_cciss_hba(void)
2609 struct gendisk *disk[NWD];
2611 for (n = 0; n < NWD; n++) {
2612 disk[n] = alloc_disk(1 << NWD_SHIFT);
2617 for(i=0; i< MAX_CTLR; i++) {
2620 p = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
2623 memset(p, 0, sizeof(ctlr_info_t));
2624 for (n = 0; n < NWD; n++)
2625 p->gendisk[n] = disk[n];
2630 printk(KERN_WARNING "cciss: This driver supports a maximum"
2631 " of 8 controllers.\n");
2634 printk(KERN_ERR "cciss: out of memory.\n");
2641 static void free_hba(int i)
2643 ctlr_info_t *p = hba[i];
2647 for (n = 0; n < NWD; n++)
2648 put_disk(p->gendisk[n]);
2653 * This is it. Find all the controllers and register them. I really hate
2654 * stealing all these major device numbers.
2655 * returns the number of block devices registered.
2657 static int __devinit cciss_init_one(struct pci_dev *pdev,
2658 const struct pci_device_id *ent)
2664 printk(KERN_DEBUG "cciss: Device 0x%x has been found at"
2665 " bus %d dev %d func %d\n",
2666 pdev->device, pdev->bus->number, PCI_SLOT(pdev->devfn),
2667 PCI_FUNC(pdev->devfn));
2668 i = alloc_cciss_hba();
2671 if (cciss_pci_init(hba[i], pdev) != 0)
2674 sprintf(hba[i]->devname, "cciss%d", i);
2676 hba[i]->pdev = pdev;
2678 /* configure PCI DMA stuff */
2679 if (!pci_set_dma_mask(pdev, 0xffffffffffffffffULL))
2680 printk("cciss: using DAC cycles\n");
2681 else if (!pci_set_dma_mask(pdev, 0xffffffff))
2682 printk("cciss: not using DAC cycles\n");
2684 printk("cciss: no suitable DMA available\n");
2688 if (register_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname)) {
2689 printk(KERN_ERR "cciss: Unable to register device %s\n",
2694 /* make sure the board interrupts are off */
2695 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
2696 if( request_irq(hba[i]->intr, do_cciss_intr,
2697 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
2698 hba[i]->devname, hba[i])) {
2699 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
2700 hba[i]->intr, hba[i]->devname);
2703 hba[i]->cmd_pool_bits = kmalloc(((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long), GFP_KERNEL);
2704 hba[i]->cmd_pool = (CommandList_struct *)pci_alloc_consistent(
2705 hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
2706 &(hba[i]->cmd_pool_dhandle));
2707 hba[i]->errinfo_pool = (ErrorInfo_struct *)pci_alloc_consistent(
2708 hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
2709 &(hba[i]->errinfo_pool_dhandle));
2710 if((hba[i]->cmd_pool_bits == NULL)
2711 || (hba[i]->cmd_pool == NULL)
2712 || (hba[i]->errinfo_pool == NULL)) {
2713 printk( KERN_ERR "cciss: out of memory");
2717 spin_lock_init(&hba[i]->lock);
2718 q = blk_init_queue(do_cciss_request, &hba[i]->lock);
2722 q->backing_dev_info.ra_pages = READ_AHEAD;
2724 q->queuedata = hba[i];
2726 /* Initialize the pdev driver private data.
2727 have it point to hba[i]. */
2728 pci_set_drvdata(pdev, hba[i]);
2729 /* command and error info recs zeroed out before
2731 memset(hba[i]->cmd_pool_bits, 0, ((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long));
2734 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n",i);
2735 #endif /* CCISS_DEBUG */
2737 cciss_getgeometry(i);
2739 cciss_scsi_setup(i);
2741 /* Turn the interrupts on so we can service requests */
2742 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
2746 blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
2748 /* This is a hardware imposed limit. */
2749 blk_queue_max_hw_segments(q, MAXSGENTRIES);
2751 /* This is a limit in the driver and could be eliminated. */
2752 blk_queue_max_phys_segments(q, MAXSGENTRIES);
2754 blk_queue_max_sectors(q, 512);
2757 for(j=0; j<NWD; j++) {
2758 drive_info_struct *drv = &(hba[i]->drv[j]);
2759 struct gendisk *disk = hba[i]->gendisk[j];
2761 sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
2762 sprintf(disk->devfs_name, "cciss/host%d/target%d", i, j);
2763 disk->major = COMPAQ_CISS_MAJOR + i;
2764 disk->first_minor = j << NWD_SHIFT;
2765 disk->fops = &cciss_fops;
2766 disk->queue = hba[i]->queue;
2767 disk->private_data = drv;
2768 if( !(drv->nr_blocks))
2770 blk_queue_hardsect_size(hba[i]->queue, drv->block_size);
2771 set_capacity(disk, drv->nr_blocks);
2777 if(hba[i]->cmd_pool_bits)
2778 kfree(hba[i]->cmd_pool_bits);
2779 if(hba[i]->cmd_pool)
2780 pci_free_consistent(hba[i]->pdev,
2781 NR_CMDS * sizeof(CommandList_struct),
2782 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
2783 if(hba[i]->errinfo_pool)
2784 pci_free_consistent(hba[i]->pdev,
2785 NR_CMDS * sizeof( ErrorInfo_struct),
2786 hba[i]->errinfo_pool,
2787 hba[i]->errinfo_pool_dhandle);
2788 free_irq(hba[i]->intr, hba[i]);
2790 unregister_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname);
2792 release_io_mem(hba[i]);
2797 static void __devexit cciss_remove_one (struct pci_dev *pdev)
2799 ctlr_info_t *tmp_ptr;
2804 if (pci_get_drvdata(pdev) == NULL)
2806 printk( KERN_ERR "cciss: Unable to remove device \n");
2809 tmp_ptr = pci_get_drvdata(pdev);
2813 printk(KERN_ERR "cciss: device appears to "
2814 "already be removed \n");
2817 /* Turn board interrupts off and send the flush cache command */
2818 /* sendcmd will turn off interrupt, and send the flush...
2819 * To write all data in the battery backed cache to disks */
2820 memset(flush_buf, 0, 4);
2821 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
2823 if(return_code != IO_OK)
2825 printk(KERN_WARNING "Error Flushing cache on controller %d\n",
2828 free_irq(hba[i]->intr, hba[i]);
2829 pci_set_drvdata(pdev, NULL);
2830 iounmap((void*)hba[i]->vaddr);
2831 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
2832 unregister_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname);
2833 remove_proc_entry(hba[i]->devname, proc_cciss);
2835 /* remove it from the disk list */
2836 for (j = 0; j < NWD; j++) {
2837 struct gendisk *disk = hba[i]->gendisk[j];
2838 if (disk->flags & GENHD_FL_UP)
2842 blk_cleanup_queue(hba[i]->queue);
2843 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
2844 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
2845 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
2846 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
2847 kfree(hba[i]->cmd_pool_bits);
2848 release_io_mem(hba[i]);
2852 static struct pci_driver cciss_pci_driver = {
2854 .probe = cciss_init_one,
2855 .remove = __devexit_p(cciss_remove_one),
2856 .id_table = cciss_pci_device_id, /* id_table */
2860 * This is it. Register the PCI driver information for the cards we control
2861 * the OS will call our registered routines when it finds one of our cards.
2863 int __init cciss_init(void)
2865 printk(KERN_INFO DRIVER_NAME "\n");
2867 /* Register for our PCI devices */
2868 return pci_module_init(&cciss_pci_driver);
2871 static int __init init_cciss_module(void)
2873 register_cciss_ioctl32();
2874 return ( cciss_init());
2877 static void __exit cleanup_cciss_module(void)
2881 unregister_cciss_ioctl32();
2882 pci_unregister_driver(&cciss_pci_driver);
2883 /* double check that all controller entrys have been removed */
2884 for (i=0; i< MAX_CTLR; i++)
2888 printk(KERN_WARNING "cciss: had to remove"
2889 " controller %d\n", i);
2890 cciss_remove_one(hba[i]->pdev);
2893 remove_proc_entry("cciss", proc_root_driver);
2896 module_init(init_cciss_module);
2897 module_exit(cleanup_cciss_module);