V4L/DVB: v4l2-ioctl: integer overflow in video_usercopy()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / block / cciss.c
blobca9c54870f29b296ae031e15db3bdd32d44f660a
1 /*
2 * Disk Array driver for HP Smart Array controllers.
3 * (C) Copyright 2000, 2007 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; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 * 02111-1307, 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/smp_lock.h>
30 #include <linux/delay.h>
31 #include <linux/major.h>
32 #include <linux/fs.h>
33 #include <linux/bio.h>
34 #include <linux/blkpg.h>
35 #include <linux/timer.h>
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <linux/init.h>
39 #include <linux/jiffies.h>
40 #include <linux/hdreg.h>
41 #include <linux/spinlock.h>
42 #include <linux/compat.h>
43 #include <linux/mutex.h>
44 #include <asm/uaccess.h>
45 #include <asm/io.h>
47 #include <linux/dma-mapping.h>
48 #include <linux/blkdev.h>
49 #include <linux/genhd.h>
50 #include <linux/completion.h>
51 #include <scsi/scsi.h>
52 #include <scsi/sg.h>
53 #include <scsi/scsi_ioctl.h>
54 #include <linux/cdrom.h>
55 #include <linux/scatterlist.h>
56 #include <linux/kthread.h>
58 #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
59 #define DRIVER_NAME "HP CISS Driver (v 3.6.20)"
60 #define DRIVER_VERSION CCISS_DRIVER_VERSION(3, 6, 20)
62 /* Embedded module documentation macros - see modules.h */
63 MODULE_AUTHOR("Hewlett-Packard Company");
64 MODULE_DESCRIPTION("Driver for HP Smart Array Controllers");
65 MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
66 " SA6i P600 P800 P400 P400i E200 E200i E500 P700m"
67 " Smart Array G2 Series SAS/SATA Controllers");
68 MODULE_VERSION("3.6.20");
69 MODULE_LICENSE("GPL");
71 static int cciss_allow_hpsa;
72 module_param(cciss_allow_hpsa, int, S_IRUGO|S_IWUSR);
73 MODULE_PARM_DESC(cciss_allow_hpsa,
74 "Prevent cciss driver from accessing hardware known to be "
75 " supported by the hpsa driver");
77 #include "cciss_cmd.h"
78 #include "cciss.h"
79 #include <linux/cciss_ioctl.h>
81 /* define the PCI info for the cards we can control */
82 static const struct pci_device_id cciss_pci_device_id[] = {
83 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS, 0x0E11, 0x4070},
84 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4080},
85 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4082},
86 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4083},
87 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x4091},
88 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409A},
89 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409B},
90 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409C},
91 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409D},
92 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA, 0x103C, 0x3225},
93 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3223},
94 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3234},
95 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3235},
96 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3211},
97 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3212},
98 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3213},
99 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3214},
100 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3215},
101 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3237},
102 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x323D},
103 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
104 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
105 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
106 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
107 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
108 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
109 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
110 {0,}
113 MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
115 /* board_id = Subsystem Device ID & Vendor ID
116 * product = Marketing Name for the board
117 * access = Address of the struct of function pointers
119 static struct board_type products[] = {
120 {0x40700E11, "Smart Array 5300", &SA5_access},
121 {0x40800E11, "Smart Array 5i", &SA5B_access},
122 {0x40820E11, "Smart Array 532", &SA5B_access},
123 {0x40830E11, "Smart Array 5312", &SA5B_access},
124 {0x409A0E11, "Smart Array 641", &SA5_access},
125 {0x409B0E11, "Smart Array 642", &SA5_access},
126 {0x409C0E11, "Smart Array 6400", &SA5_access},
127 {0x409D0E11, "Smart Array 6400 EM", &SA5_access},
128 {0x40910E11, "Smart Array 6i", &SA5_access},
129 {0x3225103C, "Smart Array P600", &SA5_access},
130 {0x3235103C, "Smart Array P400i", &SA5_access},
131 {0x3211103C, "Smart Array E200i", &SA5_access},
132 {0x3212103C, "Smart Array E200", &SA5_access},
133 {0x3213103C, "Smart Array E200i", &SA5_access},
134 {0x3214103C, "Smart Array E200i", &SA5_access},
135 {0x3215103C, "Smart Array E200i", &SA5_access},
136 {0x3237103C, "Smart Array E500", &SA5_access},
137 /* controllers below this line are also supported by the hpsa driver. */
138 #define HPSA_BOUNDARY 0x3223103C
139 {0x3223103C, "Smart Array P800", &SA5_access},
140 {0x3234103C, "Smart Array P400", &SA5_access},
141 {0x323D103C, "Smart Array P700m", &SA5_access},
142 {0x3241103C, "Smart Array P212", &SA5_access},
143 {0x3243103C, "Smart Array P410", &SA5_access},
144 {0x3245103C, "Smart Array P410i", &SA5_access},
145 {0x3247103C, "Smart Array P411", &SA5_access},
146 {0x3249103C, "Smart Array P812", &SA5_access},
147 {0x324A103C, "Smart Array P712m", &SA5_access},
148 {0x324B103C, "Smart Array P711m", &SA5_access},
151 /* How long to wait (in milliseconds) for board to go into simple mode */
152 #define MAX_CONFIG_WAIT 30000
153 #define MAX_IOCTL_CONFIG_WAIT 1000
155 /*define how many times we will try a command because of bus resets */
156 #define MAX_CMD_RETRIES 3
158 #define MAX_CTLR 32
160 /* Originally cciss driver only supports 8 major numbers */
161 #define MAX_CTLR_ORIG 8
163 static ctlr_info_t *hba[MAX_CTLR];
165 static struct task_struct *cciss_scan_thread;
166 static DEFINE_MUTEX(scan_mutex);
167 static LIST_HEAD(scan_q);
169 static void do_cciss_request(struct request_queue *q);
170 static irqreturn_t do_cciss_intr(int irq, void *dev_id);
171 static int cciss_open(struct block_device *bdev, fmode_t mode);
172 static int cciss_release(struct gendisk *disk, fmode_t mode);
173 static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
174 unsigned int cmd, unsigned long arg);
175 static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo);
177 static int cciss_revalidate(struct gendisk *disk);
178 static int rebuild_lun_table(ctlr_info_t *h, int first_time, int via_ioctl);
179 static int deregister_disk(ctlr_info_t *h, int drv_index,
180 int clear_all, int via_ioctl);
182 static void cciss_read_capacity(int ctlr, int logvol, int withirq,
183 sector_t *total_size, unsigned int *block_size);
184 static void cciss_read_capacity_16(int ctlr, int logvol, int withirq,
185 sector_t *total_size, unsigned int *block_size);
186 static void cciss_geometry_inquiry(int ctlr, int logvol,
187 int withirq, sector_t total_size,
188 unsigned int block_size, InquiryData_struct *inq_buff,
189 drive_info_struct *drv);
190 static void __devinit cciss_interrupt_mode(ctlr_info_t *, struct pci_dev *,
191 __u32);
192 static void start_io(ctlr_info_t *h);
193 static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size,
194 __u8 page_code, unsigned char *scsi3addr, int cmd_type);
195 static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
196 __u8 page_code, unsigned char scsi3addr[],
197 int cmd_type);
198 static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c,
199 int attempt_retry);
200 static int process_sendcmd_error(ctlr_info_t *h, CommandList_struct *c);
202 static void fail_all_cmds(unsigned long ctlr);
203 static int add_to_scan_list(struct ctlr_info *h);
204 static int scan_thread(void *data);
205 static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c);
206 static void cciss_hba_release(struct device *dev);
207 static void cciss_device_release(struct device *dev);
208 static void cciss_free_gendisk(ctlr_info_t *h, int drv_index);
209 static void cciss_free_drive_info(ctlr_info_t *h, int drv_index);
211 #ifdef CONFIG_PROC_FS
212 static void cciss_procinit(int i);
213 #else
214 static void cciss_procinit(int i)
217 #endif /* CONFIG_PROC_FS */
219 #ifdef CONFIG_COMPAT
220 static int cciss_compat_ioctl(struct block_device *, fmode_t,
221 unsigned, unsigned long);
222 #endif
224 static const struct block_device_operations cciss_fops = {
225 .owner = THIS_MODULE,
226 .open = cciss_open,
227 .release = cciss_release,
228 .locked_ioctl = cciss_ioctl,
229 .getgeo = cciss_getgeo,
230 #ifdef CONFIG_COMPAT
231 .compat_ioctl = cciss_compat_ioctl,
232 #endif
233 .revalidate_disk = cciss_revalidate,
237 * Enqueuing and dequeuing functions for cmdlists.
239 static inline void addQ(struct hlist_head *list, CommandList_struct *c)
241 hlist_add_head(&c->list, list);
244 static inline void removeQ(CommandList_struct *c)
247 * After kexec/dump some commands might still
248 * be in flight, which the firmware will try
249 * to complete. Resetting the firmware doesn't work
250 * with old fw revisions, so we have to mark
251 * them off as 'stale' to prevent the driver from
252 * falling over.
254 if (WARN_ON(hlist_unhashed(&c->list))) {
255 c->cmd_type = CMD_MSG_STALE;
256 return;
259 hlist_del_init(&c->list);
262 #include "cciss_scsi.c" /* For SCSI tape support */
264 static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
265 "UNKNOWN"
267 #define RAID_UNKNOWN (sizeof(raid_label) / sizeof(raid_label[0])-1)
269 #ifdef CONFIG_PROC_FS
272 * Report information about this controller.
274 #define ENG_GIG 1000000000
275 #define ENG_GIG_FACTOR (ENG_GIG/512)
276 #define ENGAGE_SCSI "engage scsi"
278 static struct proc_dir_entry *proc_cciss;
280 static void cciss_seq_show_header(struct seq_file *seq)
282 ctlr_info_t *h = seq->private;
284 seq_printf(seq, "%s: HP %s Controller\n"
285 "Board ID: 0x%08lx\n"
286 "Firmware Version: %c%c%c%c\n"
287 "IRQ: %d\n"
288 "Logical drives: %d\n"
289 "Current Q depth: %d\n"
290 "Current # commands on controller: %d\n"
291 "Max Q depth since init: %d\n"
292 "Max # commands on controller since init: %d\n"
293 "Max SG entries since init: %d\n",
294 h->devname,
295 h->product_name,
296 (unsigned long)h->board_id,
297 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
298 h->firm_ver[3], (unsigned int)h->intr[SIMPLE_MODE_INT],
299 h->num_luns,
300 h->Qdepth, h->commands_outstanding,
301 h->maxQsinceinit, h->max_outstanding, h->maxSG);
303 #ifdef CONFIG_CISS_SCSI_TAPE
304 cciss_seq_tape_report(seq, h->ctlr);
305 #endif /* CONFIG_CISS_SCSI_TAPE */
308 static void *cciss_seq_start(struct seq_file *seq, loff_t *pos)
310 ctlr_info_t *h = seq->private;
311 unsigned ctlr = h->ctlr;
312 unsigned long flags;
314 /* prevent displaying bogus info during configuration
315 * or deconfiguration of a logical volume
317 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
318 if (h->busy_configuring) {
319 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
320 return ERR_PTR(-EBUSY);
322 h->busy_configuring = 1;
323 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
325 if (*pos == 0)
326 cciss_seq_show_header(seq);
328 return pos;
331 static int cciss_seq_show(struct seq_file *seq, void *v)
333 sector_t vol_sz, vol_sz_frac;
334 ctlr_info_t *h = seq->private;
335 unsigned ctlr = h->ctlr;
336 loff_t *pos = v;
337 drive_info_struct *drv = h->drv[*pos];
339 if (*pos > h->highest_lun)
340 return 0;
342 if (drv == NULL) /* it's possible for h->drv[] to have holes. */
343 return 0;
345 if (drv->heads == 0)
346 return 0;
348 vol_sz = drv->nr_blocks;
349 vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
350 vol_sz_frac *= 100;
351 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
353 if (drv->raid_level < 0 || drv->raid_level > RAID_UNKNOWN)
354 drv->raid_level = RAID_UNKNOWN;
355 seq_printf(seq, "cciss/c%dd%d:"
356 "\t%4u.%02uGB\tRAID %s\n",
357 ctlr, (int) *pos, (int)vol_sz, (int)vol_sz_frac,
358 raid_label[drv->raid_level]);
359 return 0;
362 static void *cciss_seq_next(struct seq_file *seq, void *v, loff_t *pos)
364 ctlr_info_t *h = seq->private;
366 if (*pos > h->highest_lun)
367 return NULL;
368 *pos += 1;
370 return pos;
373 static void cciss_seq_stop(struct seq_file *seq, void *v)
375 ctlr_info_t *h = seq->private;
377 /* Only reset h->busy_configuring if we succeeded in setting
378 * it during cciss_seq_start. */
379 if (v == ERR_PTR(-EBUSY))
380 return;
382 h->busy_configuring = 0;
385 static const struct seq_operations cciss_seq_ops = {
386 .start = cciss_seq_start,
387 .show = cciss_seq_show,
388 .next = cciss_seq_next,
389 .stop = cciss_seq_stop,
392 static int cciss_seq_open(struct inode *inode, struct file *file)
394 int ret = seq_open(file, &cciss_seq_ops);
395 struct seq_file *seq = file->private_data;
397 if (!ret)
398 seq->private = PDE(inode)->data;
400 return ret;
403 static ssize_t
404 cciss_proc_write(struct file *file, const char __user *buf,
405 size_t length, loff_t *ppos)
407 int err;
408 char *buffer;
410 #ifndef CONFIG_CISS_SCSI_TAPE
411 return -EINVAL;
412 #endif
414 if (!buf || length > PAGE_SIZE - 1)
415 return -EINVAL;
417 buffer = (char *)__get_free_page(GFP_KERNEL);
418 if (!buffer)
419 return -ENOMEM;
421 err = -EFAULT;
422 if (copy_from_user(buffer, buf, length))
423 goto out;
424 buffer[length] = '\0';
426 #ifdef CONFIG_CISS_SCSI_TAPE
427 if (strncmp(ENGAGE_SCSI, buffer, sizeof ENGAGE_SCSI - 1) == 0) {
428 struct seq_file *seq = file->private_data;
429 ctlr_info_t *h = seq->private;
430 int rc;
432 rc = cciss_engage_scsi(h->ctlr);
433 if (rc != 0)
434 err = -rc;
435 else
436 err = length;
437 } else
438 #endif /* CONFIG_CISS_SCSI_TAPE */
439 err = -EINVAL;
440 /* might be nice to have "disengage" too, but it's not
441 safely possible. (only 1 module use count, lock issues.) */
443 out:
444 free_page((unsigned long)buffer);
445 return err;
448 static const struct file_operations cciss_proc_fops = {
449 .owner = THIS_MODULE,
450 .open = cciss_seq_open,
451 .read = seq_read,
452 .llseek = seq_lseek,
453 .release = seq_release,
454 .write = cciss_proc_write,
457 static void __devinit cciss_procinit(int i)
459 struct proc_dir_entry *pde;
461 if (proc_cciss == NULL)
462 proc_cciss = proc_mkdir("driver/cciss", NULL);
463 if (!proc_cciss)
464 return;
465 pde = proc_create_data(hba[i]->devname, S_IWUSR | S_IRUSR | S_IRGRP |
466 S_IROTH, proc_cciss,
467 &cciss_proc_fops, hba[i]);
469 #endif /* CONFIG_PROC_FS */
471 #define MAX_PRODUCT_NAME_LEN 19
473 #define to_hba(n) container_of(n, struct ctlr_info, dev)
474 #define to_drv(n) container_of(n, drive_info_struct, dev)
476 static ssize_t host_store_rescan(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
480 struct ctlr_info *h = to_hba(dev);
482 add_to_scan_list(h);
483 wake_up_process(cciss_scan_thread);
484 wait_for_completion_interruptible(&h->scan_wait);
486 return count;
488 static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
490 static ssize_t dev_show_unique_id(struct device *dev,
491 struct device_attribute *attr,
492 char *buf)
494 drive_info_struct *drv = to_drv(dev);
495 struct ctlr_info *h = to_hba(drv->dev.parent);
496 __u8 sn[16];
497 unsigned long flags;
498 int ret = 0;
500 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
501 if (h->busy_configuring)
502 ret = -EBUSY;
503 else
504 memcpy(sn, drv->serial_no, sizeof(sn));
505 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
507 if (ret)
508 return ret;
509 else
510 return snprintf(buf, 16 * 2 + 2,
511 "%02X%02X%02X%02X%02X%02X%02X%02X"
512 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
513 sn[0], sn[1], sn[2], sn[3],
514 sn[4], sn[5], sn[6], sn[7],
515 sn[8], sn[9], sn[10], sn[11],
516 sn[12], sn[13], sn[14], sn[15]);
518 static DEVICE_ATTR(unique_id, S_IRUGO, dev_show_unique_id, NULL);
520 static ssize_t dev_show_vendor(struct device *dev,
521 struct device_attribute *attr,
522 char *buf)
524 drive_info_struct *drv = to_drv(dev);
525 struct ctlr_info *h = to_hba(drv->dev.parent);
526 char vendor[VENDOR_LEN + 1];
527 unsigned long flags;
528 int ret = 0;
530 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
531 if (h->busy_configuring)
532 ret = -EBUSY;
533 else
534 memcpy(vendor, drv->vendor, VENDOR_LEN + 1);
535 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
537 if (ret)
538 return ret;
539 else
540 return snprintf(buf, sizeof(vendor) + 1, "%s\n", drv->vendor);
542 static DEVICE_ATTR(vendor, S_IRUGO, dev_show_vendor, NULL);
544 static ssize_t dev_show_model(struct device *dev,
545 struct device_attribute *attr,
546 char *buf)
548 drive_info_struct *drv = to_drv(dev);
549 struct ctlr_info *h = to_hba(drv->dev.parent);
550 char model[MODEL_LEN + 1];
551 unsigned long flags;
552 int ret = 0;
554 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
555 if (h->busy_configuring)
556 ret = -EBUSY;
557 else
558 memcpy(model, drv->model, MODEL_LEN + 1);
559 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
561 if (ret)
562 return ret;
563 else
564 return snprintf(buf, sizeof(model) + 1, "%s\n", drv->model);
566 static DEVICE_ATTR(model, S_IRUGO, dev_show_model, NULL);
568 static ssize_t dev_show_rev(struct device *dev,
569 struct device_attribute *attr,
570 char *buf)
572 drive_info_struct *drv = to_drv(dev);
573 struct ctlr_info *h = to_hba(drv->dev.parent);
574 char rev[REV_LEN + 1];
575 unsigned long flags;
576 int ret = 0;
578 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
579 if (h->busy_configuring)
580 ret = -EBUSY;
581 else
582 memcpy(rev, drv->rev, REV_LEN + 1);
583 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
585 if (ret)
586 return ret;
587 else
588 return snprintf(buf, sizeof(rev) + 1, "%s\n", drv->rev);
590 static DEVICE_ATTR(rev, S_IRUGO, dev_show_rev, NULL);
592 static ssize_t cciss_show_lunid(struct device *dev,
593 struct device_attribute *attr, char *buf)
595 drive_info_struct *drv = to_drv(dev);
596 struct ctlr_info *h = to_hba(drv->dev.parent);
597 unsigned long flags;
598 unsigned char lunid[8];
600 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
601 if (h->busy_configuring) {
602 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
603 return -EBUSY;
605 if (!drv->heads) {
606 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
607 return -ENOTTY;
609 memcpy(lunid, drv->LunID, sizeof(lunid));
610 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
611 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
612 lunid[0], lunid[1], lunid[2], lunid[3],
613 lunid[4], lunid[5], lunid[6], lunid[7]);
615 static DEVICE_ATTR(lunid, S_IRUGO, cciss_show_lunid, NULL);
617 static ssize_t cciss_show_raid_level(struct device *dev,
618 struct device_attribute *attr, char *buf)
620 drive_info_struct *drv = to_drv(dev);
621 struct ctlr_info *h = to_hba(drv->dev.parent);
622 int raid;
623 unsigned long flags;
625 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
626 if (h->busy_configuring) {
627 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
628 return -EBUSY;
630 raid = drv->raid_level;
631 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
632 if (raid < 0 || raid > RAID_UNKNOWN)
633 raid = RAID_UNKNOWN;
635 return snprintf(buf, strlen(raid_label[raid]) + 7, "RAID %s\n",
636 raid_label[raid]);
638 static DEVICE_ATTR(raid_level, S_IRUGO, cciss_show_raid_level, NULL);
640 static ssize_t cciss_show_usage_count(struct device *dev,
641 struct device_attribute *attr, char *buf)
643 drive_info_struct *drv = to_drv(dev);
644 struct ctlr_info *h = to_hba(drv->dev.parent);
645 unsigned long flags;
646 int count;
648 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
649 if (h->busy_configuring) {
650 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
651 return -EBUSY;
653 count = drv->usage_count;
654 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
655 return snprintf(buf, 20, "%d\n", count);
657 static DEVICE_ATTR(usage_count, S_IRUGO, cciss_show_usage_count, NULL);
659 static struct attribute *cciss_host_attrs[] = {
660 &dev_attr_rescan.attr,
661 NULL
664 static struct attribute_group cciss_host_attr_group = {
665 .attrs = cciss_host_attrs,
668 static const struct attribute_group *cciss_host_attr_groups[] = {
669 &cciss_host_attr_group,
670 NULL
673 static struct device_type cciss_host_type = {
674 .name = "cciss_host",
675 .groups = cciss_host_attr_groups,
676 .release = cciss_hba_release,
679 static struct attribute *cciss_dev_attrs[] = {
680 &dev_attr_unique_id.attr,
681 &dev_attr_model.attr,
682 &dev_attr_vendor.attr,
683 &dev_attr_rev.attr,
684 &dev_attr_lunid.attr,
685 &dev_attr_raid_level.attr,
686 &dev_attr_usage_count.attr,
687 NULL
690 static struct attribute_group cciss_dev_attr_group = {
691 .attrs = cciss_dev_attrs,
694 static const struct attribute_group *cciss_dev_attr_groups[] = {
695 &cciss_dev_attr_group,
696 NULL
699 static struct device_type cciss_dev_type = {
700 .name = "cciss_device",
701 .groups = cciss_dev_attr_groups,
702 .release = cciss_device_release,
705 static struct bus_type cciss_bus_type = {
706 .name = "cciss",
710 * cciss_hba_release is called when the reference count
711 * of h->dev goes to zero.
713 static void cciss_hba_release(struct device *dev)
716 * nothing to do, but need this to avoid a warning
717 * about not having a release handler from lib/kref.c.
722 * Initialize sysfs entry for each controller. This sets up and registers
723 * the 'cciss#' directory for each individual controller under
724 * /sys/bus/pci/devices/<dev>/.
726 static int cciss_create_hba_sysfs_entry(struct ctlr_info *h)
728 device_initialize(&h->dev);
729 h->dev.type = &cciss_host_type;
730 h->dev.bus = &cciss_bus_type;
731 dev_set_name(&h->dev, "%s", h->devname);
732 h->dev.parent = &h->pdev->dev;
734 return device_add(&h->dev);
738 * Remove sysfs entries for an hba.
740 static void cciss_destroy_hba_sysfs_entry(struct ctlr_info *h)
742 device_del(&h->dev);
743 put_device(&h->dev); /* final put. */
746 /* cciss_device_release is called when the reference count
747 * of h->drv[x]dev goes to zero.
749 static void cciss_device_release(struct device *dev)
751 drive_info_struct *drv = to_drv(dev);
752 kfree(drv);
756 * Initialize sysfs for each logical drive. This sets up and registers
757 * the 'c#d#' directory for each individual logical drive under
758 * /sys/bus/pci/devices/<dev/ccis#/. We also create a link from
759 * /sys/block/cciss!c#d# to this entry.
761 static long cciss_create_ld_sysfs_entry(struct ctlr_info *h,
762 int drv_index)
764 struct device *dev;
766 if (h->drv[drv_index]->device_initialized)
767 return 0;
769 dev = &h->drv[drv_index]->dev;
770 device_initialize(dev);
771 dev->type = &cciss_dev_type;
772 dev->bus = &cciss_bus_type;
773 dev_set_name(dev, "c%dd%d", h->ctlr, drv_index);
774 dev->parent = &h->dev;
775 h->drv[drv_index]->device_initialized = 1;
776 return device_add(dev);
780 * Remove sysfs entries for a logical drive.
782 static void cciss_destroy_ld_sysfs_entry(struct ctlr_info *h, int drv_index,
783 int ctlr_exiting)
785 struct device *dev = &h->drv[drv_index]->dev;
787 /* special case for c*d0, we only destroy it on controller exit */
788 if (drv_index == 0 && !ctlr_exiting)
789 return;
791 device_del(dev);
792 put_device(dev); /* the "final" put. */
793 h->drv[drv_index] = NULL;
797 * For operations that cannot sleep, a command block is allocated at init,
798 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
799 * which ones are free or in use. For operations that can wait for kmalloc
800 * to possible sleep, this routine can be called with get_from_pool set to 0.
801 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
803 static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool)
805 CommandList_struct *c;
806 int i;
807 u64bit temp64;
808 dma_addr_t cmd_dma_handle, err_dma_handle;
810 if (!get_from_pool) {
811 c = (CommandList_struct *) pci_alloc_consistent(h->pdev,
812 sizeof(CommandList_struct), &cmd_dma_handle);
813 if (c == NULL)
814 return NULL;
815 memset(c, 0, sizeof(CommandList_struct));
817 c->cmdindex = -1;
819 c->err_info = (ErrorInfo_struct *)
820 pci_alloc_consistent(h->pdev, sizeof(ErrorInfo_struct),
821 &err_dma_handle);
823 if (c->err_info == NULL) {
824 pci_free_consistent(h->pdev,
825 sizeof(CommandList_struct), c, cmd_dma_handle);
826 return NULL;
828 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
829 } else { /* get it out of the controllers pool */
831 do {
832 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
833 if (i == h->nr_cmds)
834 return NULL;
835 } while (test_and_set_bit
836 (i & (BITS_PER_LONG - 1),
837 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
838 #ifdef CCISS_DEBUG
839 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
840 #endif
841 c = h->cmd_pool + i;
842 memset(c, 0, sizeof(CommandList_struct));
843 cmd_dma_handle = h->cmd_pool_dhandle
844 + i * sizeof(CommandList_struct);
845 c->err_info = h->errinfo_pool + i;
846 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
847 err_dma_handle = h->errinfo_pool_dhandle
848 + i * sizeof(ErrorInfo_struct);
849 h->nr_allocs++;
851 c->cmdindex = i;
854 INIT_HLIST_NODE(&c->list);
855 c->busaddr = (__u32) cmd_dma_handle;
856 temp64.val = (__u64) err_dma_handle;
857 c->ErrDesc.Addr.lower = temp64.val32.lower;
858 c->ErrDesc.Addr.upper = temp64.val32.upper;
859 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
861 c->ctlr = h->ctlr;
862 return c;
866 * Frees a command block that was previously allocated with cmd_alloc().
868 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
870 int i;
871 u64bit temp64;
873 if (!got_from_pool) {
874 temp64.val32.lower = c->ErrDesc.Addr.lower;
875 temp64.val32.upper = c->ErrDesc.Addr.upper;
876 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
877 c->err_info, (dma_addr_t) temp64.val);
878 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
879 c, (dma_addr_t) c->busaddr);
880 } else {
881 i = c - h->cmd_pool;
882 clear_bit(i & (BITS_PER_LONG - 1),
883 h->cmd_pool_bits + (i / BITS_PER_LONG));
884 h->nr_frees++;
888 static inline ctlr_info_t *get_host(struct gendisk *disk)
890 return disk->queue->queuedata;
893 static inline drive_info_struct *get_drv(struct gendisk *disk)
895 return disk->private_data;
899 * Open. Make sure the device is really there.
901 static int cciss_open(struct block_device *bdev, fmode_t mode)
903 ctlr_info_t *host = get_host(bdev->bd_disk);
904 drive_info_struct *drv = get_drv(bdev->bd_disk);
906 #ifdef CCISS_DEBUG
907 printk(KERN_DEBUG "cciss_open %s\n", bdev->bd_disk->disk_name);
908 #endif /* CCISS_DEBUG */
910 if (drv->busy_configuring)
911 return -EBUSY;
913 * Root is allowed to open raw volume zero even if it's not configured
914 * so array config can still work. Root is also allowed to open any
915 * volume that has a LUN ID, so it can issue IOCTL to reread the
916 * disk information. I don't think I really like this
917 * but I'm already using way to many device nodes to claim another one
918 * for "raw controller".
920 if (drv->heads == 0) {
921 if (MINOR(bdev->bd_dev) != 0) { /* not node 0? */
922 /* if not node 0 make sure it is a partition = 0 */
923 if (MINOR(bdev->bd_dev) & 0x0f) {
924 return -ENXIO;
925 /* if it is, make sure we have a LUN ID */
926 } else if (memcmp(drv->LunID, CTLR_LUNID,
927 sizeof(drv->LunID))) {
928 return -ENXIO;
931 if (!capable(CAP_SYS_ADMIN))
932 return -EPERM;
934 drv->usage_count++;
935 host->usage_count++;
936 return 0;
940 * Close. Sync first.
942 static int cciss_release(struct gendisk *disk, fmode_t mode)
944 ctlr_info_t *host = get_host(disk);
945 drive_info_struct *drv = get_drv(disk);
947 #ifdef CCISS_DEBUG
948 printk(KERN_DEBUG "cciss_release %s\n", disk->disk_name);
949 #endif /* CCISS_DEBUG */
951 drv->usage_count--;
952 host->usage_count--;
953 return 0;
956 #ifdef CONFIG_COMPAT
958 static int do_ioctl(struct block_device *bdev, fmode_t mode,
959 unsigned cmd, unsigned long arg)
961 int ret;
962 lock_kernel();
963 ret = cciss_ioctl(bdev, mode, cmd, arg);
964 unlock_kernel();
965 return ret;
968 static int cciss_ioctl32_passthru(struct block_device *bdev, fmode_t mode,
969 unsigned cmd, unsigned long arg);
970 static int cciss_ioctl32_big_passthru(struct block_device *bdev, fmode_t mode,
971 unsigned cmd, unsigned long arg);
973 static int cciss_compat_ioctl(struct block_device *bdev, fmode_t mode,
974 unsigned cmd, unsigned long arg)
976 switch (cmd) {
977 case CCISS_GETPCIINFO:
978 case CCISS_GETINTINFO:
979 case CCISS_SETINTINFO:
980 case CCISS_GETNODENAME:
981 case CCISS_SETNODENAME:
982 case CCISS_GETHEARTBEAT:
983 case CCISS_GETBUSTYPES:
984 case CCISS_GETFIRMVER:
985 case CCISS_GETDRIVVER:
986 case CCISS_REVALIDVOLS:
987 case CCISS_DEREGDISK:
988 case CCISS_REGNEWDISK:
989 case CCISS_REGNEWD:
990 case CCISS_RESCANDISK:
991 case CCISS_GETLUNINFO:
992 return do_ioctl(bdev, mode, cmd, arg);
994 case CCISS_PASSTHRU32:
995 return cciss_ioctl32_passthru(bdev, mode, cmd, arg);
996 case CCISS_BIG_PASSTHRU32:
997 return cciss_ioctl32_big_passthru(bdev, mode, cmd, arg);
999 default:
1000 return -ENOIOCTLCMD;
1004 static int cciss_ioctl32_passthru(struct block_device *bdev, fmode_t mode,
1005 unsigned cmd, unsigned long arg)
1007 IOCTL32_Command_struct __user *arg32 =
1008 (IOCTL32_Command_struct __user *) arg;
1009 IOCTL_Command_struct arg64;
1010 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
1011 int err;
1012 u32 cp;
1014 err = 0;
1015 err |=
1016 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
1017 sizeof(arg64.LUN_info));
1018 err |=
1019 copy_from_user(&arg64.Request, &arg32->Request,
1020 sizeof(arg64.Request));
1021 err |=
1022 copy_from_user(&arg64.error_info, &arg32->error_info,
1023 sizeof(arg64.error_info));
1024 err |= get_user(arg64.buf_size, &arg32->buf_size);
1025 err |= get_user(cp, &arg32->buf);
1026 arg64.buf = compat_ptr(cp);
1027 err |= copy_to_user(p, &arg64, sizeof(arg64));
1029 if (err)
1030 return -EFAULT;
1032 err = do_ioctl(bdev, mode, CCISS_PASSTHRU, (unsigned long)p);
1033 if (err)
1034 return err;
1035 err |=
1036 copy_in_user(&arg32->error_info, &p->error_info,
1037 sizeof(arg32->error_info));
1038 if (err)
1039 return -EFAULT;
1040 return err;
1043 static int cciss_ioctl32_big_passthru(struct block_device *bdev, fmode_t mode,
1044 unsigned cmd, unsigned long arg)
1046 BIG_IOCTL32_Command_struct __user *arg32 =
1047 (BIG_IOCTL32_Command_struct __user *) arg;
1048 BIG_IOCTL_Command_struct arg64;
1049 BIG_IOCTL_Command_struct __user *p =
1050 compat_alloc_user_space(sizeof(arg64));
1051 int err;
1052 u32 cp;
1054 err = 0;
1055 err |=
1056 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
1057 sizeof(arg64.LUN_info));
1058 err |=
1059 copy_from_user(&arg64.Request, &arg32->Request,
1060 sizeof(arg64.Request));
1061 err |=
1062 copy_from_user(&arg64.error_info, &arg32->error_info,
1063 sizeof(arg64.error_info));
1064 err |= get_user(arg64.buf_size, &arg32->buf_size);
1065 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
1066 err |= get_user(cp, &arg32->buf);
1067 arg64.buf = compat_ptr(cp);
1068 err |= copy_to_user(p, &arg64, sizeof(arg64));
1070 if (err)
1071 return -EFAULT;
1073 err = do_ioctl(bdev, mode, CCISS_BIG_PASSTHRU, (unsigned long)p);
1074 if (err)
1075 return err;
1076 err |=
1077 copy_in_user(&arg32->error_info, &p->error_info,
1078 sizeof(arg32->error_info));
1079 if (err)
1080 return -EFAULT;
1081 return err;
1083 #endif
1085 static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1087 drive_info_struct *drv = get_drv(bdev->bd_disk);
1089 if (!drv->cylinders)
1090 return -ENXIO;
1092 geo->heads = drv->heads;
1093 geo->sectors = drv->sectors;
1094 geo->cylinders = drv->cylinders;
1095 return 0;
1098 static void check_ioctl_unit_attention(ctlr_info_t *host, CommandList_struct *c)
1100 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
1101 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
1102 (void)check_for_unit_attention(host, c);
1105 * ioctl
1107 static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
1108 unsigned int cmd, unsigned long arg)
1110 struct gendisk *disk = bdev->bd_disk;
1111 ctlr_info_t *host = get_host(disk);
1112 drive_info_struct *drv = get_drv(disk);
1113 int ctlr = host->ctlr;
1114 void __user *argp = (void __user *)arg;
1116 #ifdef CCISS_DEBUG
1117 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
1118 #endif /* CCISS_DEBUG */
1120 switch (cmd) {
1121 case CCISS_GETPCIINFO:
1123 cciss_pci_info_struct pciinfo;
1125 if (!arg)
1126 return -EINVAL;
1127 pciinfo.domain = pci_domain_nr(host->pdev->bus);
1128 pciinfo.bus = host->pdev->bus->number;
1129 pciinfo.dev_fn = host->pdev->devfn;
1130 pciinfo.board_id = host->board_id;
1131 if (copy_to_user
1132 (argp, &pciinfo, sizeof(cciss_pci_info_struct)))
1133 return -EFAULT;
1134 return 0;
1136 case CCISS_GETINTINFO:
1138 cciss_coalint_struct intinfo;
1139 if (!arg)
1140 return -EINVAL;
1141 intinfo.delay =
1142 readl(&host->cfgtable->HostWrite.CoalIntDelay);
1143 intinfo.count =
1144 readl(&host->cfgtable->HostWrite.CoalIntCount);
1145 if (copy_to_user
1146 (argp, &intinfo, sizeof(cciss_coalint_struct)))
1147 return -EFAULT;
1148 return 0;
1150 case CCISS_SETINTINFO:
1152 cciss_coalint_struct intinfo;
1153 unsigned long flags;
1154 int i;
1156 if (!arg)
1157 return -EINVAL;
1158 if (!capable(CAP_SYS_ADMIN))
1159 return -EPERM;
1160 if (copy_from_user
1161 (&intinfo, argp, sizeof(cciss_coalint_struct)))
1162 return -EFAULT;
1163 if ((intinfo.delay == 0) && (intinfo.count == 0))
1165 // printk("cciss_ioctl: delay and count cannot be 0\n");
1166 return -EINVAL;
1168 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1169 /* Update the field, and then ring the doorbell */
1170 writel(intinfo.delay,
1171 &(host->cfgtable->HostWrite.CoalIntDelay));
1172 writel(intinfo.count,
1173 &(host->cfgtable->HostWrite.CoalIntCount));
1174 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
1176 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
1177 if (!(readl(host->vaddr + SA5_DOORBELL)
1178 & CFGTBL_ChangeReq))
1179 break;
1180 /* delay and try again */
1181 udelay(1000);
1183 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1184 if (i >= MAX_IOCTL_CONFIG_WAIT)
1185 return -EAGAIN;
1186 return 0;
1188 case CCISS_GETNODENAME:
1190 NodeName_type NodeName;
1191 int i;
1193 if (!arg)
1194 return -EINVAL;
1195 for (i = 0; i < 16; i++)
1196 NodeName[i] =
1197 readb(&host->cfgtable->ServerName[i]);
1198 if (copy_to_user(argp, NodeName, sizeof(NodeName_type)))
1199 return -EFAULT;
1200 return 0;
1202 case CCISS_SETNODENAME:
1204 NodeName_type NodeName;
1205 unsigned long flags;
1206 int i;
1208 if (!arg)
1209 return -EINVAL;
1210 if (!capable(CAP_SYS_ADMIN))
1211 return -EPERM;
1213 if (copy_from_user
1214 (NodeName, argp, sizeof(NodeName_type)))
1215 return -EFAULT;
1217 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1219 /* Update the field, and then ring the doorbell */
1220 for (i = 0; i < 16; i++)
1221 writeb(NodeName[i],
1222 &host->cfgtable->ServerName[i]);
1224 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
1226 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
1227 if (!(readl(host->vaddr + SA5_DOORBELL)
1228 & CFGTBL_ChangeReq))
1229 break;
1230 /* delay and try again */
1231 udelay(1000);
1233 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1234 if (i >= MAX_IOCTL_CONFIG_WAIT)
1235 return -EAGAIN;
1236 return 0;
1239 case CCISS_GETHEARTBEAT:
1241 Heartbeat_type heartbeat;
1243 if (!arg)
1244 return -EINVAL;
1245 heartbeat = readl(&host->cfgtable->HeartBeat);
1246 if (copy_to_user
1247 (argp, &heartbeat, sizeof(Heartbeat_type)))
1248 return -EFAULT;
1249 return 0;
1251 case CCISS_GETBUSTYPES:
1253 BusTypes_type BusTypes;
1255 if (!arg)
1256 return -EINVAL;
1257 BusTypes = readl(&host->cfgtable->BusTypes);
1258 if (copy_to_user
1259 (argp, &BusTypes, sizeof(BusTypes_type)))
1260 return -EFAULT;
1261 return 0;
1263 case CCISS_GETFIRMVER:
1265 FirmwareVer_type firmware;
1267 if (!arg)
1268 return -EINVAL;
1269 memcpy(firmware, host->firm_ver, 4);
1271 if (copy_to_user
1272 (argp, firmware, sizeof(FirmwareVer_type)))
1273 return -EFAULT;
1274 return 0;
1276 case CCISS_GETDRIVVER:
1278 DriverVer_type DriverVer = DRIVER_VERSION;
1280 if (!arg)
1281 return -EINVAL;
1283 if (copy_to_user
1284 (argp, &DriverVer, sizeof(DriverVer_type)))
1285 return -EFAULT;
1286 return 0;
1289 case CCISS_DEREGDISK:
1290 case CCISS_REGNEWD:
1291 case CCISS_REVALIDVOLS:
1292 return rebuild_lun_table(host, 0, 1);
1294 case CCISS_GETLUNINFO:{
1295 LogvolInfo_struct luninfo;
1297 memcpy(&luninfo.LunID, drv->LunID,
1298 sizeof(luninfo.LunID));
1299 luninfo.num_opens = drv->usage_count;
1300 luninfo.num_parts = 0;
1301 if (copy_to_user(argp, &luninfo,
1302 sizeof(LogvolInfo_struct)))
1303 return -EFAULT;
1304 return 0;
1306 case CCISS_PASSTHRU:
1308 IOCTL_Command_struct iocommand;
1309 CommandList_struct *c;
1310 char *buff = NULL;
1311 u64bit temp64;
1312 unsigned long flags;
1313 DECLARE_COMPLETION_ONSTACK(wait);
1315 if (!arg)
1316 return -EINVAL;
1318 if (!capable(CAP_SYS_RAWIO))
1319 return -EPERM;
1321 if (copy_from_user
1322 (&iocommand, argp, sizeof(IOCTL_Command_struct)))
1323 return -EFAULT;
1324 if ((iocommand.buf_size < 1) &&
1325 (iocommand.Request.Type.Direction != XFER_NONE)) {
1326 return -EINVAL;
1328 #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
1329 /* Check kmalloc limits */
1330 if (iocommand.buf_size > 128000)
1331 return -EINVAL;
1332 #endif
1333 if (iocommand.buf_size > 0) {
1334 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
1335 if (buff == NULL)
1336 return -EFAULT;
1338 if (iocommand.Request.Type.Direction == XFER_WRITE) {
1339 /* Copy the data into the buffer we created */
1340 if (copy_from_user
1341 (buff, iocommand.buf, iocommand.buf_size)) {
1342 kfree(buff);
1343 return -EFAULT;
1345 } else {
1346 memset(buff, 0, iocommand.buf_size);
1348 if ((c = cmd_alloc(host, 0)) == NULL) {
1349 kfree(buff);
1350 return -ENOMEM;
1352 // Fill in the command type
1353 c->cmd_type = CMD_IOCTL_PEND;
1354 // Fill in Command Header
1355 c->Header.ReplyQueue = 0; // unused in simple mode
1356 if (iocommand.buf_size > 0) // buffer to fill
1358 c->Header.SGList = 1;
1359 c->Header.SGTotal = 1;
1360 } else // no buffers to fill
1362 c->Header.SGList = 0;
1363 c->Header.SGTotal = 0;
1365 c->Header.LUN = iocommand.LUN_info;
1366 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
1368 // Fill in Request block
1369 c->Request = iocommand.Request;
1371 // Fill in the scatter gather information
1372 if (iocommand.buf_size > 0) {
1373 temp64.val = pci_map_single(host->pdev, buff,
1374 iocommand.buf_size,
1375 PCI_DMA_BIDIRECTIONAL);
1376 c->SG[0].Addr.lower = temp64.val32.lower;
1377 c->SG[0].Addr.upper = temp64.val32.upper;
1378 c->SG[0].Len = iocommand.buf_size;
1379 c->SG[0].Ext = 0; // we are not chaining
1381 c->waiting = &wait;
1383 /* Put the request on the tail of the request queue */
1384 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1385 addQ(&host->reqQ, c);
1386 host->Qdepth++;
1387 start_io(host);
1388 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1390 wait_for_completion(&wait);
1392 /* unlock the buffers from DMA */
1393 temp64.val32.lower = c->SG[0].Addr.lower;
1394 temp64.val32.upper = c->SG[0].Addr.upper;
1395 pci_unmap_single(host->pdev, (dma_addr_t) temp64.val,
1396 iocommand.buf_size,
1397 PCI_DMA_BIDIRECTIONAL);
1399 check_ioctl_unit_attention(host, c);
1401 /* Copy the error information out */
1402 iocommand.error_info = *(c->err_info);
1403 if (copy_to_user
1404 (argp, &iocommand, sizeof(IOCTL_Command_struct))) {
1405 kfree(buff);
1406 cmd_free(host, c, 0);
1407 return -EFAULT;
1410 if (iocommand.Request.Type.Direction == XFER_READ) {
1411 /* Copy the data out of the buffer we created */
1412 if (copy_to_user
1413 (iocommand.buf, buff, iocommand.buf_size)) {
1414 kfree(buff);
1415 cmd_free(host, c, 0);
1416 return -EFAULT;
1419 kfree(buff);
1420 cmd_free(host, c, 0);
1421 return 0;
1423 case CCISS_BIG_PASSTHRU:{
1424 BIG_IOCTL_Command_struct *ioc;
1425 CommandList_struct *c;
1426 unsigned char **buff = NULL;
1427 int *buff_size = NULL;
1428 u64bit temp64;
1429 unsigned long flags;
1430 BYTE sg_used = 0;
1431 int status = 0;
1432 int i;
1433 DECLARE_COMPLETION_ONSTACK(wait);
1434 __u32 left;
1435 __u32 sz;
1436 BYTE __user *data_ptr;
1438 if (!arg)
1439 return -EINVAL;
1440 if (!capable(CAP_SYS_RAWIO))
1441 return -EPERM;
1442 ioc = (BIG_IOCTL_Command_struct *)
1443 kmalloc(sizeof(*ioc), GFP_KERNEL);
1444 if (!ioc) {
1445 status = -ENOMEM;
1446 goto cleanup1;
1448 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
1449 status = -EFAULT;
1450 goto cleanup1;
1452 if ((ioc->buf_size < 1) &&
1453 (ioc->Request.Type.Direction != XFER_NONE)) {
1454 status = -EINVAL;
1455 goto cleanup1;
1457 /* Check kmalloc limits using all SGs */
1458 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
1459 status = -EINVAL;
1460 goto cleanup1;
1462 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
1463 status = -EINVAL;
1464 goto cleanup1;
1466 buff =
1467 kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
1468 if (!buff) {
1469 status = -ENOMEM;
1470 goto cleanup1;
1472 buff_size = kmalloc(MAXSGENTRIES * sizeof(int),
1473 GFP_KERNEL);
1474 if (!buff_size) {
1475 status = -ENOMEM;
1476 goto cleanup1;
1478 left = ioc->buf_size;
1479 data_ptr = ioc->buf;
1480 while (left) {
1481 sz = (left >
1482 ioc->malloc_size) ? ioc->
1483 malloc_size : left;
1484 buff_size[sg_used] = sz;
1485 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
1486 if (buff[sg_used] == NULL) {
1487 status = -ENOMEM;
1488 goto cleanup1;
1490 if (ioc->Request.Type.Direction == XFER_WRITE) {
1491 if (copy_from_user
1492 (buff[sg_used], data_ptr, sz)) {
1493 status = -EFAULT;
1494 goto cleanup1;
1496 } else {
1497 memset(buff[sg_used], 0, sz);
1499 left -= sz;
1500 data_ptr += sz;
1501 sg_used++;
1503 if ((c = cmd_alloc(host, 0)) == NULL) {
1504 status = -ENOMEM;
1505 goto cleanup1;
1507 c->cmd_type = CMD_IOCTL_PEND;
1508 c->Header.ReplyQueue = 0;
1510 if (ioc->buf_size > 0) {
1511 c->Header.SGList = sg_used;
1512 c->Header.SGTotal = sg_used;
1513 } else {
1514 c->Header.SGList = 0;
1515 c->Header.SGTotal = 0;
1517 c->Header.LUN = ioc->LUN_info;
1518 c->Header.Tag.lower = c->busaddr;
1520 c->Request = ioc->Request;
1521 if (ioc->buf_size > 0) {
1522 int i;
1523 for (i = 0; i < sg_used; i++) {
1524 temp64.val =
1525 pci_map_single(host->pdev, buff[i],
1526 buff_size[i],
1527 PCI_DMA_BIDIRECTIONAL);
1528 c->SG[i].Addr.lower =
1529 temp64.val32.lower;
1530 c->SG[i].Addr.upper =
1531 temp64.val32.upper;
1532 c->SG[i].Len = buff_size[i];
1533 c->SG[i].Ext = 0; /* we are not chaining */
1536 c->waiting = &wait;
1537 /* Put the request on the tail of the request queue */
1538 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1539 addQ(&host->reqQ, c);
1540 host->Qdepth++;
1541 start_io(host);
1542 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1543 wait_for_completion(&wait);
1544 /* unlock the buffers from DMA */
1545 for (i = 0; i < sg_used; i++) {
1546 temp64.val32.lower = c->SG[i].Addr.lower;
1547 temp64.val32.upper = c->SG[i].Addr.upper;
1548 pci_unmap_single(host->pdev,
1549 (dma_addr_t) temp64.val, buff_size[i],
1550 PCI_DMA_BIDIRECTIONAL);
1552 check_ioctl_unit_attention(host, c);
1553 /* Copy the error information out */
1554 ioc->error_info = *(c->err_info);
1555 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
1556 cmd_free(host, c, 0);
1557 status = -EFAULT;
1558 goto cleanup1;
1560 if (ioc->Request.Type.Direction == XFER_READ) {
1561 /* Copy the data out of the buffer we created */
1562 BYTE __user *ptr = ioc->buf;
1563 for (i = 0; i < sg_used; i++) {
1564 if (copy_to_user
1565 (ptr, buff[i], buff_size[i])) {
1566 cmd_free(host, c, 0);
1567 status = -EFAULT;
1568 goto cleanup1;
1570 ptr += buff_size[i];
1573 cmd_free(host, c, 0);
1574 status = 0;
1575 cleanup1:
1576 if (buff) {
1577 for (i = 0; i < sg_used; i++)
1578 kfree(buff[i]);
1579 kfree(buff);
1581 kfree(buff_size);
1582 kfree(ioc);
1583 return status;
1586 /* scsi_cmd_ioctl handles these, below, though some are not */
1587 /* very meaningful for cciss. SG_IO is the main one people want. */
1589 case SG_GET_VERSION_NUM:
1590 case SG_SET_TIMEOUT:
1591 case SG_GET_TIMEOUT:
1592 case SG_GET_RESERVED_SIZE:
1593 case SG_SET_RESERVED_SIZE:
1594 case SG_EMULATED_HOST:
1595 case SG_IO:
1596 case SCSI_IOCTL_SEND_COMMAND:
1597 return scsi_cmd_ioctl(disk->queue, disk, mode, cmd, argp);
1599 /* scsi_cmd_ioctl would normally handle these, below, but */
1600 /* they aren't a good fit for cciss, as CD-ROMs are */
1601 /* not supported, and we don't have any bus/target/lun */
1602 /* which we present to the kernel. */
1604 case CDROM_SEND_PACKET:
1605 case CDROMCLOSETRAY:
1606 case CDROMEJECT:
1607 case SCSI_IOCTL_GET_IDLUN:
1608 case SCSI_IOCTL_GET_BUS_NUMBER:
1609 default:
1610 return -ENOTTY;
1614 static void cciss_check_queues(ctlr_info_t *h)
1616 int start_queue = h->next_to_run;
1617 int i;
1619 /* check to see if we have maxed out the number of commands that can
1620 * be placed on the queue. If so then exit. We do this check here
1621 * in case the interrupt we serviced was from an ioctl and did not
1622 * free any new commands.
1624 if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds)
1625 return;
1627 /* We have room on the queue for more commands. Now we need to queue
1628 * them up. We will also keep track of the next queue to run so
1629 * that every queue gets a chance to be started first.
1631 for (i = 0; i < h->highest_lun + 1; i++) {
1632 int curr_queue = (start_queue + i) % (h->highest_lun + 1);
1633 /* make sure the disk has been added and the drive is real
1634 * because this can be called from the middle of init_one.
1636 if (!h->drv[curr_queue])
1637 continue;
1638 if (!(h->drv[curr_queue]->queue) ||
1639 !(h->drv[curr_queue]->heads))
1640 continue;
1641 blk_start_queue(h->gendisk[curr_queue]->queue);
1643 /* check to see if we have maxed out the number of commands
1644 * that can be placed on the queue.
1646 if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds) {
1647 if (curr_queue == start_queue) {
1648 h->next_to_run =
1649 (start_queue + 1) % (h->highest_lun + 1);
1650 break;
1651 } else {
1652 h->next_to_run = curr_queue;
1653 break;
1659 static void cciss_softirq_done(struct request *rq)
1661 CommandList_struct *cmd = rq->completion_data;
1662 ctlr_info_t *h = hba[cmd->ctlr];
1663 unsigned long flags;
1664 u64bit temp64;
1665 int i, ddir;
1667 if (cmd->Request.Type.Direction == XFER_READ)
1668 ddir = PCI_DMA_FROMDEVICE;
1669 else
1670 ddir = PCI_DMA_TODEVICE;
1672 /* command did not need to be retried */
1673 /* unmap the DMA mapping for all the scatter gather elements */
1674 for (i = 0; i < cmd->Header.SGList; i++) {
1675 temp64.val32.lower = cmd->SG[i].Addr.lower;
1676 temp64.val32.upper = cmd->SG[i].Addr.upper;
1677 pci_unmap_page(h->pdev, temp64.val, cmd->SG[i].Len, ddir);
1680 #ifdef CCISS_DEBUG
1681 printk("Done with %p\n", rq);
1682 #endif /* CCISS_DEBUG */
1684 /* set the residual count for pc requests */
1685 if (blk_pc_request(rq))
1686 rq->resid_len = cmd->err_info->ResidualCnt;
1688 blk_end_request_all(rq, (rq->errors == 0) ? 0 : -EIO);
1690 spin_lock_irqsave(&h->lock, flags);
1691 cmd_free(h, cmd, 1);
1692 cciss_check_queues(h);
1693 spin_unlock_irqrestore(&h->lock, flags);
1696 static inline void log_unit_to_scsi3addr(ctlr_info_t *h,
1697 unsigned char scsi3addr[], uint32_t log_unit)
1699 memcpy(scsi3addr, h->drv[log_unit]->LunID,
1700 sizeof(h->drv[log_unit]->LunID));
1703 /* This function gets the SCSI vendor, model, and revision of a logical drive
1704 * via the inquiry page 0. Model, vendor, and rev are set to empty strings if
1705 * they cannot be read.
1707 static void cciss_get_device_descr(int ctlr, int logvol, int withirq,
1708 char *vendor, char *model, char *rev)
1710 int rc;
1711 InquiryData_struct *inq_buf;
1712 unsigned char scsi3addr[8];
1714 *vendor = '\0';
1715 *model = '\0';
1716 *rev = '\0';
1718 inq_buf = kzalloc(sizeof(InquiryData_struct), GFP_KERNEL);
1719 if (!inq_buf)
1720 return;
1722 log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
1723 if (withirq)
1724 rc = sendcmd_withirq(CISS_INQUIRY, ctlr, inq_buf,
1725 sizeof(InquiryData_struct), 0,
1726 scsi3addr, TYPE_CMD);
1727 else
1728 rc = sendcmd(CISS_INQUIRY, ctlr, inq_buf,
1729 sizeof(InquiryData_struct), 0,
1730 scsi3addr, TYPE_CMD);
1731 if (rc == IO_OK) {
1732 memcpy(vendor, &inq_buf->data_byte[8], VENDOR_LEN);
1733 vendor[VENDOR_LEN] = '\0';
1734 memcpy(model, &inq_buf->data_byte[16], MODEL_LEN);
1735 model[MODEL_LEN] = '\0';
1736 memcpy(rev, &inq_buf->data_byte[32], REV_LEN);
1737 rev[REV_LEN] = '\0';
1740 kfree(inq_buf);
1741 return;
1744 /* This function gets the serial number of a logical drive via
1745 * inquiry page 0x83. Serial no. is 16 bytes. If the serial
1746 * number cannot be had, for whatever reason, 16 bytes of 0xff
1747 * are returned instead.
1749 static void cciss_get_serial_no(int ctlr, int logvol, int withirq,
1750 unsigned char *serial_no, int buflen)
1752 #define PAGE_83_INQ_BYTES 64
1753 int rc;
1754 unsigned char *buf;
1755 unsigned char scsi3addr[8];
1757 if (buflen > 16)
1758 buflen = 16;
1759 memset(serial_no, 0xff, buflen);
1760 buf = kzalloc(PAGE_83_INQ_BYTES, GFP_KERNEL);
1761 if (!buf)
1762 return;
1763 memset(serial_no, 0, buflen);
1764 log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
1765 if (withirq)
1766 rc = sendcmd_withirq(CISS_INQUIRY, ctlr, buf,
1767 PAGE_83_INQ_BYTES, 0x83, scsi3addr, TYPE_CMD);
1768 else
1769 rc = sendcmd(CISS_INQUIRY, ctlr, buf,
1770 PAGE_83_INQ_BYTES, 0x83, scsi3addr, TYPE_CMD);
1771 if (rc == IO_OK)
1772 memcpy(serial_no, &buf[8], buflen);
1773 kfree(buf);
1774 return;
1778 * cciss_add_disk sets up the block device queue for a logical drive
1780 static int cciss_add_disk(ctlr_info_t *h, struct gendisk *disk,
1781 int drv_index)
1783 disk->queue = blk_init_queue(do_cciss_request, &h->lock);
1784 if (!disk->queue)
1785 goto init_queue_failure;
1786 sprintf(disk->disk_name, "cciss/c%dd%d", h->ctlr, drv_index);
1787 disk->major = h->major;
1788 disk->first_minor = drv_index << NWD_SHIFT;
1789 disk->fops = &cciss_fops;
1790 if (cciss_create_ld_sysfs_entry(h, drv_index))
1791 goto cleanup_queue;
1792 disk->private_data = h->drv[drv_index];
1793 disk->driverfs_dev = &h->drv[drv_index]->dev;
1795 /* Set up queue information */
1796 blk_queue_bounce_limit(disk->queue, h->pdev->dma_mask);
1798 /* This is a hardware imposed limit. */
1799 blk_queue_max_hw_segments(disk->queue, MAXSGENTRIES);
1801 /* This is a limit in the driver and could be eliminated. */
1802 blk_queue_max_phys_segments(disk->queue, MAXSGENTRIES);
1804 blk_queue_max_sectors(disk->queue, h->cciss_max_sectors);
1806 blk_queue_softirq_done(disk->queue, cciss_softirq_done);
1808 disk->queue->queuedata = h;
1810 blk_queue_logical_block_size(disk->queue,
1811 h->drv[drv_index]->block_size);
1813 /* Make sure all queue data is written out before */
1814 /* setting h->drv[drv_index]->queue, as setting this */
1815 /* allows the interrupt handler to start the queue */
1816 wmb();
1817 h->drv[drv_index]->queue = disk->queue;
1818 add_disk(disk);
1819 return 0;
1821 cleanup_queue:
1822 blk_cleanup_queue(disk->queue);
1823 disk->queue = NULL;
1824 init_queue_failure:
1825 return -1;
1828 /* This function will check the usage_count of the drive to be updated/added.
1829 * If the usage_count is zero and it is a heretofore unknown drive, or,
1830 * the drive's capacity, geometry, or serial number has changed,
1831 * then the drive information will be updated and the disk will be
1832 * re-registered with the kernel. If these conditions don't hold,
1833 * then it will be left alone for the next reboot. The exception to this
1834 * is disk 0 which will always be left registered with the kernel since it
1835 * is also the controller node. Any changes to disk 0 will show up on
1836 * the next reboot.
1838 static void cciss_update_drive_info(int ctlr, int drv_index, int first_time,
1839 int via_ioctl)
1841 ctlr_info_t *h = hba[ctlr];
1842 struct gendisk *disk;
1843 InquiryData_struct *inq_buff = NULL;
1844 unsigned int block_size;
1845 sector_t total_size;
1846 unsigned long flags = 0;
1847 int ret = 0;
1848 drive_info_struct *drvinfo;
1850 /* Get information about the disk and modify the driver structure */
1851 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
1852 drvinfo = kzalloc(sizeof(*drvinfo), GFP_KERNEL);
1853 if (inq_buff == NULL || drvinfo == NULL)
1854 goto mem_msg;
1856 /* testing to see if 16-byte CDBs are already being used */
1857 if (h->cciss_read == CCISS_READ_16) {
1858 cciss_read_capacity_16(h->ctlr, drv_index, 1,
1859 &total_size, &block_size);
1861 } else {
1862 cciss_read_capacity(ctlr, drv_index, 1,
1863 &total_size, &block_size);
1865 /* if read_capacity returns all F's this volume is >2TB */
1866 /* in size so we switch to 16-byte CDB's for all */
1867 /* read/write ops */
1868 if (total_size == 0xFFFFFFFFULL) {
1869 cciss_read_capacity_16(ctlr, drv_index, 1,
1870 &total_size, &block_size);
1871 h->cciss_read = CCISS_READ_16;
1872 h->cciss_write = CCISS_WRITE_16;
1873 } else {
1874 h->cciss_read = CCISS_READ_10;
1875 h->cciss_write = CCISS_WRITE_10;
1879 cciss_geometry_inquiry(ctlr, drv_index, 1, total_size, block_size,
1880 inq_buff, drvinfo);
1881 drvinfo->block_size = block_size;
1882 drvinfo->nr_blocks = total_size + 1;
1884 cciss_get_device_descr(ctlr, drv_index, 1, drvinfo->vendor,
1885 drvinfo->model, drvinfo->rev);
1886 cciss_get_serial_no(ctlr, drv_index, 1, drvinfo->serial_no,
1887 sizeof(drvinfo->serial_no));
1888 /* Save the lunid in case we deregister the disk, below. */
1889 memcpy(drvinfo->LunID, h->drv[drv_index]->LunID,
1890 sizeof(drvinfo->LunID));
1892 /* Is it the same disk we already know, and nothing's changed? */
1893 if (h->drv[drv_index]->raid_level != -1 &&
1894 ((memcmp(drvinfo->serial_no,
1895 h->drv[drv_index]->serial_no, 16) == 0) &&
1896 drvinfo->block_size == h->drv[drv_index]->block_size &&
1897 drvinfo->nr_blocks == h->drv[drv_index]->nr_blocks &&
1898 drvinfo->heads == h->drv[drv_index]->heads &&
1899 drvinfo->sectors == h->drv[drv_index]->sectors &&
1900 drvinfo->cylinders == h->drv[drv_index]->cylinders))
1901 /* The disk is unchanged, nothing to update */
1902 goto freeret;
1904 /* If we get here it's not the same disk, or something's changed,
1905 * so we need to * deregister it, and re-register it, if it's not
1906 * in use.
1907 * If the disk already exists then deregister it before proceeding
1908 * (unless it's the first disk (for the controller node).
1910 if (h->drv[drv_index]->raid_level != -1 && drv_index != 0) {
1911 printk(KERN_WARNING "disk %d has changed.\n", drv_index);
1912 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
1913 h->drv[drv_index]->busy_configuring = 1;
1914 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1916 /* deregister_disk sets h->drv[drv_index]->queue = NULL
1917 * which keeps the interrupt handler from starting
1918 * the queue.
1920 ret = deregister_disk(h, drv_index, 0, via_ioctl);
1923 /* If the disk is in use return */
1924 if (ret)
1925 goto freeret;
1927 /* Save the new information from cciss_geometry_inquiry
1928 * and serial number inquiry. If the disk was deregistered
1929 * above, then h->drv[drv_index] will be NULL.
1931 if (h->drv[drv_index] == NULL) {
1932 drvinfo->device_initialized = 0;
1933 h->drv[drv_index] = drvinfo;
1934 drvinfo = NULL; /* so it won't be freed below. */
1935 } else {
1936 /* special case for cxd0 */
1937 h->drv[drv_index]->block_size = drvinfo->block_size;
1938 h->drv[drv_index]->nr_blocks = drvinfo->nr_blocks;
1939 h->drv[drv_index]->heads = drvinfo->heads;
1940 h->drv[drv_index]->sectors = drvinfo->sectors;
1941 h->drv[drv_index]->cylinders = drvinfo->cylinders;
1942 h->drv[drv_index]->raid_level = drvinfo->raid_level;
1943 memcpy(h->drv[drv_index]->serial_no, drvinfo->serial_no, 16);
1944 memcpy(h->drv[drv_index]->vendor, drvinfo->vendor,
1945 VENDOR_LEN + 1);
1946 memcpy(h->drv[drv_index]->model, drvinfo->model, MODEL_LEN + 1);
1947 memcpy(h->drv[drv_index]->rev, drvinfo->rev, REV_LEN + 1);
1950 ++h->num_luns;
1951 disk = h->gendisk[drv_index];
1952 set_capacity(disk, h->drv[drv_index]->nr_blocks);
1954 /* If it's not disk 0 (drv_index != 0)
1955 * or if it was disk 0, but there was previously
1956 * no actual corresponding configured logical drive
1957 * (raid_leve == -1) then we want to update the
1958 * logical drive's information.
1960 if (drv_index || first_time) {
1961 if (cciss_add_disk(h, disk, drv_index) != 0) {
1962 cciss_free_gendisk(h, drv_index);
1963 cciss_free_drive_info(h, drv_index);
1964 printk(KERN_WARNING "cciss:%d could not update "
1965 "disk %d\n", h->ctlr, drv_index);
1966 --h->num_luns;
1970 freeret:
1971 kfree(inq_buff);
1972 kfree(drvinfo);
1973 return;
1974 mem_msg:
1975 printk(KERN_ERR "cciss: out of memory\n");
1976 goto freeret;
1979 /* This function will find the first index of the controllers drive array
1980 * that has a null drv pointer and allocate the drive info struct and
1981 * will return that index This is where new drives will be added.
1982 * If the index to be returned is greater than the highest_lun index for
1983 * the controller then highest_lun is set * to this new index.
1984 * If there are no available indexes or if tha allocation fails, then -1
1985 * is returned. * "controller_node" is used to know if this is a real
1986 * logical drive, or just the controller node, which determines if this
1987 * counts towards highest_lun.
1989 static int cciss_alloc_drive_info(ctlr_info_t *h, int controller_node)
1991 int i;
1992 drive_info_struct *drv;
1994 /* Search for an empty slot for our drive info */
1995 for (i = 0; i < CISS_MAX_LUN; i++) {
1997 /* if not cxd0 case, and it's occupied, skip it. */
1998 if (h->drv[i] && i != 0)
1999 continue;
2001 * If it's cxd0 case, and drv is alloc'ed already, and a
2002 * disk is configured there, skip it.
2004 if (i == 0 && h->drv[i] && h->drv[i]->raid_level != -1)
2005 continue;
2008 * We've found an empty slot. Update highest_lun
2009 * provided this isn't just the fake cxd0 controller node.
2011 if (i > h->highest_lun && !controller_node)
2012 h->highest_lun = i;
2014 /* If adding a real disk at cxd0, and it's already alloc'ed */
2015 if (i == 0 && h->drv[i] != NULL)
2016 return i;
2019 * Found an empty slot, not already alloc'ed. Allocate it.
2020 * Mark it with raid_level == -1, so we know it's new later on.
2022 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
2023 if (!drv)
2024 return -1;
2025 drv->raid_level = -1; /* so we know it's new */
2026 h->drv[i] = drv;
2027 return i;
2029 return -1;
2032 static void cciss_free_drive_info(ctlr_info_t *h, int drv_index)
2034 kfree(h->drv[drv_index]);
2035 h->drv[drv_index] = NULL;
2038 static void cciss_free_gendisk(ctlr_info_t *h, int drv_index)
2040 put_disk(h->gendisk[drv_index]);
2041 h->gendisk[drv_index] = NULL;
2044 /* cciss_add_gendisk finds a free hba[]->drv structure
2045 * and allocates a gendisk if needed, and sets the lunid
2046 * in the drvinfo structure. It returns the index into
2047 * the ->drv[] array, or -1 if none are free.
2048 * is_controller_node indicates whether highest_lun should
2049 * count this disk, or if it's only being added to provide
2050 * a means to talk to the controller in case no logical
2051 * drives have yet been configured.
2053 static int cciss_add_gendisk(ctlr_info_t *h, unsigned char lunid[],
2054 int controller_node)
2056 int drv_index;
2058 drv_index = cciss_alloc_drive_info(h, controller_node);
2059 if (drv_index == -1)
2060 return -1;
2062 /*Check if the gendisk needs to be allocated */
2063 if (!h->gendisk[drv_index]) {
2064 h->gendisk[drv_index] =
2065 alloc_disk(1 << NWD_SHIFT);
2066 if (!h->gendisk[drv_index]) {
2067 printk(KERN_ERR "cciss%d: could not "
2068 "allocate a new disk %d\n",
2069 h->ctlr, drv_index);
2070 goto err_free_drive_info;
2073 memcpy(h->drv[drv_index]->LunID, lunid,
2074 sizeof(h->drv[drv_index]->LunID));
2075 if (cciss_create_ld_sysfs_entry(h, drv_index))
2076 goto err_free_disk;
2077 /* Don't need to mark this busy because nobody */
2078 /* else knows about this disk yet to contend */
2079 /* for access to it. */
2080 h->drv[drv_index]->busy_configuring = 0;
2081 wmb();
2082 return drv_index;
2084 err_free_disk:
2085 cciss_free_gendisk(h, drv_index);
2086 err_free_drive_info:
2087 cciss_free_drive_info(h, drv_index);
2088 return -1;
2091 /* This is for the special case of a controller which
2092 * has no logical drives. In this case, we still need
2093 * to register a disk so the controller can be accessed
2094 * by the Array Config Utility.
2096 static void cciss_add_controller_node(ctlr_info_t *h)
2098 struct gendisk *disk;
2099 int drv_index;
2101 if (h->gendisk[0] != NULL) /* already did this? Then bail. */
2102 return;
2104 drv_index = cciss_add_gendisk(h, CTLR_LUNID, 1);
2105 if (drv_index == -1)
2106 goto error;
2107 h->drv[drv_index]->block_size = 512;
2108 h->drv[drv_index]->nr_blocks = 0;
2109 h->drv[drv_index]->heads = 0;
2110 h->drv[drv_index]->sectors = 0;
2111 h->drv[drv_index]->cylinders = 0;
2112 h->drv[drv_index]->raid_level = -1;
2113 memset(h->drv[drv_index]->serial_no, 0, 16);
2114 disk = h->gendisk[drv_index];
2115 if (cciss_add_disk(h, disk, drv_index) == 0)
2116 return;
2117 cciss_free_gendisk(h, drv_index);
2118 cciss_free_drive_info(h, drv_index);
2119 error:
2120 printk(KERN_WARNING "cciss%d: could not "
2121 "add disk 0.\n", h->ctlr);
2122 return;
2125 /* This function will add and remove logical drives from the Logical
2126 * drive array of the controller and maintain persistency of ordering
2127 * so that mount points are preserved until the next reboot. This allows
2128 * for the removal of logical drives in the middle of the drive array
2129 * without a re-ordering of those drives.
2130 * INPUT
2131 * h = The controller to perform the operations on
2133 static int rebuild_lun_table(ctlr_info_t *h, int first_time,
2134 int via_ioctl)
2136 int ctlr = h->ctlr;
2137 int num_luns;
2138 ReportLunData_struct *ld_buff = NULL;
2139 int return_code;
2140 int listlength = 0;
2141 int i;
2142 int drv_found;
2143 int drv_index = 0;
2144 unsigned char lunid[8] = CTLR_LUNID;
2145 unsigned long flags;
2147 if (!capable(CAP_SYS_RAWIO))
2148 return -EPERM;
2150 /* Set busy_configuring flag for this operation */
2151 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2152 if (h->busy_configuring) {
2153 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2154 return -EBUSY;
2156 h->busy_configuring = 1;
2157 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2159 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
2160 if (ld_buff == NULL)
2161 goto mem_msg;
2163 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
2164 sizeof(ReportLunData_struct),
2165 0, CTLR_LUNID, TYPE_CMD);
2167 if (return_code == IO_OK)
2168 listlength = be32_to_cpu(*(__be32 *) ld_buff->LUNListLength);
2169 else { /* reading number of logical volumes failed */
2170 printk(KERN_WARNING "cciss: report logical volume"
2171 " command failed\n");
2172 listlength = 0;
2173 goto freeret;
2176 num_luns = listlength / 8; /* 8 bytes per entry */
2177 if (num_luns > CISS_MAX_LUN) {
2178 num_luns = CISS_MAX_LUN;
2179 printk(KERN_WARNING "cciss: more luns configured"
2180 " on controller than can be handled by"
2181 " this driver.\n");
2184 if (num_luns == 0)
2185 cciss_add_controller_node(h);
2187 /* Compare controller drive array to driver's drive array
2188 * to see if any drives are missing on the controller due
2189 * to action of Array Config Utility (user deletes drive)
2190 * and deregister logical drives which have disappeared.
2192 for (i = 0; i <= h->highest_lun; i++) {
2193 int j;
2194 drv_found = 0;
2196 /* skip holes in the array from already deleted drives */
2197 if (h->drv[i] == NULL)
2198 continue;
2200 for (j = 0; j < num_luns; j++) {
2201 memcpy(lunid, &ld_buff->LUN[j][0], sizeof(lunid));
2202 if (memcmp(h->drv[i]->LunID, lunid,
2203 sizeof(lunid)) == 0) {
2204 drv_found = 1;
2205 break;
2208 if (!drv_found) {
2209 /* Deregister it from the OS, it's gone. */
2210 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2211 h->drv[i]->busy_configuring = 1;
2212 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2213 return_code = deregister_disk(h, i, 1, via_ioctl);
2214 if (h->drv[i] != NULL)
2215 h->drv[i]->busy_configuring = 0;
2219 /* Compare controller drive array to driver's drive array.
2220 * Check for updates in the drive information and any new drives
2221 * on the controller due to ACU adding logical drives, or changing
2222 * a logical drive's size, etc. Reregister any new/changed drives
2224 for (i = 0; i < num_luns; i++) {
2225 int j;
2227 drv_found = 0;
2229 memcpy(lunid, &ld_buff->LUN[i][0], sizeof(lunid));
2230 /* Find if the LUN is already in the drive array
2231 * of the driver. If so then update its info
2232 * if not in use. If it does not exist then find
2233 * the first free index and add it.
2235 for (j = 0; j <= h->highest_lun; j++) {
2236 if (h->drv[j] != NULL &&
2237 memcmp(h->drv[j]->LunID, lunid,
2238 sizeof(h->drv[j]->LunID)) == 0) {
2239 drv_index = j;
2240 drv_found = 1;
2241 break;
2245 /* check if the drive was found already in the array */
2246 if (!drv_found) {
2247 drv_index = cciss_add_gendisk(h, lunid, 0);
2248 if (drv_index == -1)
2249 goto freeret;
2251 cciss_update_drive_info(ctlr, drv_index, first_time,
2252 via_ioctl);
2253 } /* end for */
2255 freeret:
2256 kfree(ld_buff);
2257 h->busy_configuring = 0;
2258 /* We return -1 here to tell the ACU that we have registered/updated
2259 * all of the drives that we can and to keep it from calling us
2260 * additional times.
2262 return -1;
2263 mem_msg:
2264 printk(KERN_ERR "cciss: out of memory\n");
2265 h->busy_configuring = 0;
2266 goto freeret;
2269 static void cciss_clear_drive_info(drive_info_struct *drive_info)
2271 /* zero out the disk size info */
2272 drive_info->nr_blocks = 0;
2273 drive_info->block_size = 0;
2274 drive_info->heads = 0;
2275 drive_info->sectors = 0;
2276 drive_info->cylinders = 0;
2277 drive_info->raid_level = -1;
2278 memset(drive_info->serial_no, 0, sizeof(drive_info->serial_no));
2279 memset(drive_info->model, 0, sizeof(drive_info->model));
2280 memset(drive_info->rev, 0, sizeof(drive_info->rev));
2281 memset(drive_info->vendor, 0, sizeof(drive_info->vendor));
2283 * don't clear the LUNID though, we need to remember which
2284 * one this one is.
2288 /* This function will deregister the disk and it's queue from the
2289 * kernel. It must be called with the controller lock held and the
2290 * drv structures busy_configuring flag set. It's parameters are:
2292 * disk = This is the disk to be deregistered
2293 * drv = This is the drive_info_struct associated with the disk to be
2294 * deregistered. It contains information about the disk used
2295 * by the driver.
2296 * clear_all = This flag determines whether or not the disk information
2297 * is going to be completely cleared out and the highest_lun
2298 * reset. Sometimes we want to clear out information about
2299 * the disk in preparation for re-adding it. In this case
2300 * the highest_lun should be left unchanged and the LunID
2301 * should not be cleared.
2302 * via_ioctl
2303 * This indicates whether we've reached this path via ioctl.
2304 * This affects the maximum usage count allowed for c0d0 to be messed with.
2305 * If this path is reached via ioctl(), then the max_usage_count will
2306 * be 1, as the process calling ioctl() has got to have the device open.
2307 * If we get here via sysfs, then the max usage count will be zero.
2309 static int deregister_disk(ctlr_info_t *h, int drv_index,
2310 int clear_all, int via_ioctl)
2312 int i;
2313 struct gendisk *disk;
2314 drive_info_struct *drv;
2315 int recalculate_highest_lun;
2317 if (!capable(CAP_SYS_RAWIO))
2318 return -EPERM;
2320 drv = h->drv[drv_index];
2321 disk = h->gendisk[drv_index];
2323 /* make sure logical volume is NOT is use */
2324 if (clear_all || (h->gendisk[0] == disk)) {
2325 if (drv->usage_count > via_ioctl)
2326 return -EBUSY;
2327 } else if (drv->usage_count > 0)
2328 return -EBUSY;
2330 recalculate_highest_lun = (drv == h->drv[h->highest_lun]);
2332 /* invalidate the devices and deregister the disk. If it is disk
2333 * zero do not deregister it but just zero out it's values. This
2334 * allows us to delete disk zero but keep the controller registered.
2336 if (h->gendisk[0] != disk) {
2337 struct request_queue *q = disk->queue;
2338 if (disk->flags & GENHD_FL_UP) {
2339 cciss_destroy_ld_sysfs_entry(h, drv_index, 0);
2340 del_gendisk(disk);
2342 if (q)
2343 blk_cleanup_queue(q);
2344 /* If clear_all is set then we are deleting the logical
2345 * drive, not just refreshing its info. For drives
2346 * other than disk 0 we will call put_disk. We do not
2347 * do this for disk 0 as we need it to be able to
2348 * configure the controller.
2350 if (clear_all){
2351 /* This isn't pretty, but we need to find the
2352 * disk in our array and NULL our the pointer.
2353 * This is so that we will call alloc_disk if
2354 * this index is used again later.
2356 for (i=0; i < CISS_MAX_LUN; i++){
2357 if (h->gendisk[i] == disk) {
2358 h->gendisk[i] = NULL;
2359 break;
2362 put_disk(disk);
2364 } else {
2365 set_capacity(disk, 0);
2366 cciss_clear_drive_info(drv);
2369 --h->num_luns;
2371 /* if it was the last disk, find the new hightest lun */
2372 if (clear_all && recalculate_highest_lun) {
2373 int i, newhighest = -1;
2374 for (i = 0; i <= h->highest_lun; i++) {
2375 /* if the disk has size > 0, it is available */
2376 if (h->drv[i] && h->drv[i]->heads)
2377 newhighest = i;
2379 h->highest_lun = newhighest;
2381 return 0;
2384 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
2385 size_t size, __u8 page_code, unsigned char *scsi3addr,
2386 int cmd_type)
2388 ctlr_info_t *h = hba[ctlr];
2389 u64bit buff_dma_handle;
2390 int status = IO_OK;
2392 c->cmd_type = CMD_IOCTL_PEND;
2393 c->Header.ReplyQueue = 0;
2394 if (buff != NULL) {
2395 c->Header.SGList = 1;
2396 c->Header.SGTotal = 1;
2397 } else {
2398 c->Header.SGList = 0;
2399 c->Header.SGTotal = 0;
2401 c->Header.Tag.lower = c->busaddr;
2402 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
2404 c->Request.Type.Type = cmd_type;
2405 if (cmd_type == TYPE_CMD) {
2406 switch (cmd) {
2407 case CISS_INQUIRY:
2408 /* are we trying to read a vital product page */
2409 if (page_code != 0) {
2410 c->Request.CDB[1] = 0x01;
2411 c->Request.CDB[2] = page_code;
2413 c->Request.CDBLen = 6;
2414 c->Request.Type.Attribute = ATTR_SIMPLE;
2415 c->Request.Type.Direction = XFER_READ;
2416 c->Request.Timeout = 0;
2417 c->Request.CDB[0] = CISS_INQUIRY;
2418 c->Request.CDB[4] = size & 0xFF;
2419 break;
2420 case CISS_REPORT_LOG:
2421 case CISS_REPORT_PHYS:
2422 /* Talking to controller so It's a physical command
2423 mode = 00 target = 0. Nothing to write.
2425 c->Request.CDBLen = 12;
2426 c->Request.Type.Attribute = ATTR_SIMPLE;
2427 c->Request.Type.Direction = XFER_READ;
2428 c->Request.Timeout = 0;
2429 c->Request.CDB[0] = cmd;
2430 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
2431 c->Request.CDB[7] = (size >> 16) & 0xFF;
2432 c->Request.CDB[8] = (size >> 8) & 0xFF;
2433 c->Request.CDB[9] = size & 0xFF;
2434 break;
2436 case CCISS_READ_CAPACITY:
2437 c->Request.CDBLen = 10;
2438 c->Request.Type.Attribute = ATTR_SIMPLE;
2439 c->Request.Type.Direction = XFER_READ;
2440 c->Request.Timeout = 0;
2441 c->Request.CDB[0] = cmd;
2442 break;
2443 case CCISS_READ_CAPACITY_16:
2444 c->Request.CDBLen = 16;
2445 c->Request.Type.Attribute = ATTR_SIMPLE;
2446 c->Request.Type.Direction = XFER_READ;
2447 c->Request.Timeout = 0;
2448 c->Request.CDB[0] = cmd;
2449 c->Request.CDB[1] = 0x10;
2450 c->Request.CDB[10] = (size >> 24) & 0xFF;
2451 c->Request.CDB[11] = (size >> 16) & 0xFF;
2452 c->Request.CDB[12] = (size >> 8) & 0xFF;
2453 c->Request.CDB[13] = size & 0xFF;
2454 c->Request.Timeout = 0;
2455 c->Request.CDB[0] = cmd;
2456 break;
2457 case CCISS_CACHE_FLUSH:
2458 c->Request.CDBLen = 12;
2459 c->Request.Type.Attribute = ATTR_SIMPLE;
2460 c->Request.Type.Direction = XFER_WRITE;
2461 c->Request.Timeout = 0;
2462 c->Request.CDB[0] = BMIC_WRITE;
2463 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
2464 break;
2465 case TEST_UNIT_READY:
2466 c->Request.CDBLen = 6;
2467 c->Request.Type.Attribute = ATTR_SIMPLE;
2468 c->Request.Type.Direction = XFER_NONE;
2469 c->Request.Timeout = 0;
2470 break;
2471 default:
2472 printk(KERN_WARNING
2473 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
2474 return IO_ERROR;
2476 } else if (cmd_type == TYPE_MSG) {
2477 switch (cmd) {
2478 case 0: /* ABORT message */
2479 c->Request.CDBLen = 12;
2480 c->Request.Type.Attribute = ATTR_SIMPLE;
2481 c->Request.Type.Direction = XFER_WRITE;
2482 c->Request.Timeout = 0;
2483 c->Request.CDB[0] = cmd; /* abort */
2484 c->Request.CDB[1] = 0; /* abort a command */
2485 /* buff contains the tag of the command to abort */
2486 memcpy(&c->Request.CDB[4], buff, 8);
2487 break;
2488 case 1: /* RESET message */
2489 c->Request.CDBLen = 16;
2490 c->Request.Type.Attribute = ATTR_SIMPLE;
2491 c->Request.Type.Direction = XFER_NONE;
2492 c->Request.Timeout = 0;
2493 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
2494 c->Request.CDB[0] = cmd; /* reset */
2495 c->Request.CDB[1] = 0x03; /* reset a target */
2496 break;
2497 case 3: /* No-Op message */
2498 c->Request.CDBLen = 1;
2499 c->Request.Type.Attribute = ATTR_SIMPLE;
2500 c->Request.Type.Direction = XFER_WRITE;
2501 c->Request.Timeout = 0;
2502 c->Request.CDB[0] = cmd;
2503 break;
2504 default:
2505 printk(KERN_WARNING
2506 "cciss%d: unknown message type %d\n", ctlr, cmd);
2507 return IO_ERROR;
2509 } else {
2510 printk(KERN_WARNING
2511 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
2512 return IO_ERROR;
2514 /* Fill in the scatter gather information */
2515 if (size > 0) {
2516 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
2517 buff, size,
2518 PCI_DMA_BIDIRECTIONAL);
2519 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
2520 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
2521 c->SG[0].Len = size;
2522 c->SG[0].Ext = 0; /* we are not chaining */
2524 return status;
2527 static int check_target_status(ctlr_info_t *h, CommandList_struct *c)
2529 switch (c->err_info->ScsiStatus) {
2530 case SAM_STAT_GOOD:
2531 return IO_OK;
2532 case SAM_STAT_CHECK_CONDITION:
2533 switch (0xf & c->err_info->SenseInfo[2]) {
2534 case 0: return IO_OK; /* no sense */
2535 case 1: return IO_OK; /* recovered error */
2536 default:
2537 printk(KERN_WARNING "cciss%d: cmd 0x%02x "
2538 "check condition, sense key = 0x%02x\n",
2539 h->ctlr, c->Request.CDB[0],
2540 c->err_info->SenseInfo[2]);
2542 break;
2543 default:
2544 printk(KERN_WARNING "cciss%d: cmd 0x%02x"
2545 "scsi status = 0x%02x\n", h->ctlr,
2546 c->Request.CDB[0], c->err_info->ScsiStatus);
2547 break;
2549 return IO_ERROR;
2552 static int process_sendcmd_error(ctlr_info_t *h, CommandList_struct *c)
2554 int return_status = IO_OK;
2556 if (c->err_info->CommandStatus == CMD_SUCCESS)
2557 return IO_OK;
2559 switch (c->err_info->CommandStatus) {
2560 case CMD_TARGET_STATUS:
2561 return_status = check_target_status(h, c);
2562 break;
2563 case CMD_DATA_UNDERRUN:
2564 case CMD_DATA_OVERRUN:
2565 /* expected for inquiry and report lun commands */
2566 break;
2567 case CMD_INVALID:
2568 printk(KERN_WARNING "cciss: cmd 0x%02x is "
2569 "reported invalid\n", c->Request.CDB[0]);
2570 return_status = IO_ERROR;
2571 break;
2572 case CMD_PROTOCOL_ERR:
2573 printk(KERN_WARNING "cciss: cmd 0x%02x has "
2574 "protocol error \n", c->Request.CDB[0]);
2575 return_status = IO_ERROR;
2576 break;
2577 case CMD_HARDWARE_ERR:
2578 printk(KERN_WARNING "cciss: cmd 0x%02x had "
2579 " hardware error\n", c->Request.CDB[0]);
2580 return_status = IO_ERROR;
2581 break;
2582 case CMD_CONNECTION_LOST:
2583 printk(KERN_WARNING "cciss: cmd 0x%02x had "
2584 "connection lost\n", c->Request.CDB[0]);
2585 return_status = IO_ERROR;
2586 break;
2587 case CMD_ABORTED:
2588 printk(KERN_WARNING "cciss: cmd 0x%02x was "
2589 "aborted\n", c->Request.CDB[0]);
2590 return_status = IO_ERROR;
2591 break;
2592 case CMD_ABORT_FAILED:
2593 printk(KERN_WARNING "cciss: cmd 0x%02x reports "
2594 "abort failed\n", c->Request.CDB[0]);
2595 return_status = IO_ERROR;
2596 break;
2597 case CMD_UNSOLICITED_ABORT:
2598 printk(KERN_WARNING
2599 "cciss%d: unsolicited abort 0x%02x\n", h->ctlr,
2600 c->Request.CDB[0]);
2601 return_status = IO_NEEDS_RETRY;
2602 break;
2603 default:
2604 printk(KERN_WARNING "cciss: cmd 0x%02x returned "
2605 "unknown status %x\n", c->Request.CDB[0],
2606 c->err_info->CommandStatus);
2607 return_status = IO_ERROR;
2609 return return_status;
2612 static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c,
2613 int attempt_retry)
2615 DECLARE_COMPLETION_ONSTACK(wait);
2616 u64bit buff_dma_handle;
2617 unsigned long flags;
2618 int return_status = IO_OK;
2620 resend_cmd2:
2621 c->waiting = &wait;
2622 /* Put the request on the tail of the queue and send it */
2623 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2624 addQ(&h->reqQ, c);
2625 h->Qdepth++;
2626 start_io(h);
2627 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2629 wait_for_completion(&wait);
2631 if (c->err_info->CommandStatus == 0 || !attempt_retry)
2632 goto command_done;
2634 return_status = process_sendcmd_error(h, c);
2636 if (return_status == IO_NEEDS_RETRY &&
2637 c->retry_count < MAX_CMD_RETRIES) {
2638 printk(KERN_WARNING "cciss%d: retrying 0x%02x\n", h->ctlr,
2639 c->Request.CDB[0]);
2640 c->retry_count++;
2641 /* erase the old error information */
2642 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
2643 return_status = IO_OK;
2644 INIT_COMPLETION(wait);
2645 goto resend_cmd2;
2648 command_done:
2649 /* unlock the buffers from DMA */
2650 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
2651 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
2652 pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
2653 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
2654 return return_status;
2657 static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
2658 __u8 page_code, unsigned char scsi3addr[],
2659 int cmd_type)
2661 ctlr_info_t *h = hba[ctlr];
2662 CommandList_struct *c;
2663 int return_status;
2665 c = cmd_alloc(h, 0);
2666 if (!c)
2667 return -ENOMEM;
2668 return_status = fill_cmd(c, cmd, ctlr, buff, size, page_code,
2669 scsi3addr, cmd_type);
2670 if (return_status == IO_OK)
2671 return_status = sendcmd_withirq_core(h, c, 1);
2673 cmd_free(h, c, 0);
2674 return return_status;
2677 static void cciss_geometry_inquiry(int ctlr, int logvol,
2678 int withirq, sector_t total_size,
2679 unsigned int block_size,
2680 InquiryData_struct *inq_buff,
2681 drive_info_struct *drv)
2683 int return_code;
2684 unsigned long t;
2685 unsigned char scsi3addr[8];
2687 memset(inq_buff, 0, sizeof(InquiryData_struct));
2688 log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
2689 if (withirq)
2690 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
2691 inq_buff, sizeof(*inq_buff),
2692 0xC1, scsi3addr, TYPE_CMD);
2693 else
2694 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
2695 sizeof(*inq_buff), 0xC1, scsi3addr,
2696 TYPE_CMD);
2697 if (return_code == IO_OK) {
2698 if (inq_buff->data_byte[8] == 0xFF) {
2699 printk(KERN_WARNING
2700 "cciss: reading geometry failed, volume "
2701 "does not support reading geometry\n");
2702 drv->heads = 255;
2703 drv->sectors = 32; // Sectors per track
2704 drv->cylinders = total_size + 1;
2705 drv->raid_level = RAID_UNKNOWN;
2706 } else {
2707 drv->heads = inq_buff->data_byte[6];
2708 drv->sectors = inq_buff->data_byte[7];
2709 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
2710 drv->cylinders += inq_buff->data_byte[5];
2711 drv->raid_level = inq_buff->data_byte[8];
2713 drv->block_size = block_size;
2714 drv->nr_blocks = total_size + 1;
2715 t = drv->heads * drv->sectors;
2716 if (t > 1) {
2717 sector_t real_size = total_size + 1;
2718 unsigned long rem = sector_div(real_size, t);
2719 if (rem)
2720 real_size++;
2721 drv->cylinders = real_size;
2723 } else { /* Get geometry failed */
2724 printk(KERN_WARNING "cciss: reading geometry failed\n");
2728 static void
2729 cciss_read_capacity(int ctlr, int logvol, int withirq, sector_t *total_size,
2730 unsigned int *block_size)
2732 ReadCapdata_struct *buf;
2733 int return_code;
2734 unsigned char scsi3addr[8];
2736 buf = kzalloc(sizeof(ReadCapdata_struct), GFP_KERNEL);
2737 if (!buf) {
2738 printk(KERN_WARNING "cciss: out of memory\n");
2739 return;
2742 log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
2743 if (withirq)
2744 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
2745 ctlr, buf, sizeof(ReadCapdata_struct),
2746 0, scsi3addr, TYPE_CMD);
2747 else
2748 return_code = sendcmd(CCISS_READ_CAPACITY,
2749 ctlr, buf, sizeof(ReadCapdata_struct),
2750 0, scsi3addr, TYPE_CMD);
2751 if (return_code == IO_OK) {
2752 *total_size = be32_to_cpu(*(__be32 *) buf->total_size);
2753 *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
2754 } else { /* read capacity command failed */
2755 printk(KERN_WARNING "cciss: read capacity failed\n");
2756 *total_size = 0;
2757 *block_size = BLOCK_SIZE;
2759 kfree(buf);
2762 static void
2763 cciss_read_capacity_16(int ctlr, int logvol, int withirq, sector_t *total_size, unsigned int *block_size)
2765 ReadCapdata_struct_16 *buf;
2766 int return_code;
2767 unsigned char scsi3addr[8];
2769 buf = kzalloc(sizeof(ReadCapdata_struct_16), GFP_KERNEL);
2770 if (!buf) {
2771 printk(KERN_WARNING "cciss: out of memory\n");
2772 return;
2775 log_unit_to_scsi3addr(hba[ctlr], scsi3addr, logvol);
2776 if (withirq) {
2777 return_code = sendcmd_withirq(CCISS_READ_CAPACITY_16,
2778 ctlr, buf, sizeof(ReadCapdata_struct_16),
2779 0, scsi3addr, TYPE_CMD);
2781 else {
2782 return_code = sendcmd(CCISS_READ_CAPACITY_16,
2783 ctlr, buf, sizeof(ReadCapdata_struct_16),
2784 0, scsi3addr, TYPE_CMD);
2786 if (return_code == IO_OK) {
2787 *total_size = be64_to_cpu(*(__be64 *) buf->total_size);
2788 *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
2789 } else { /* read capacity command failed */
2790 printk(KERN_WARNING "cciss: read capacity failed\n");
2791 *total_size = 0;
2792 *block_size = BLOCK_SIZE;
2794 printk(KERN_INFO " blocks= %llu block_size= %d\n",
2795 (unsigned long long)*total_size+1, *block_size);
2796 kfree(buf);
2799 static int cciss_revalidate(struct gendisk *disk)
2801 ctlr_info_t *h = get_host(disk);
2802 drive_info_struct *drv = get_drv(disk);
2803 int logvol;
2804 int FOUND = 0;
2805 unsigned int block_size;
2806 sector_t total_size;
2807 InquiryData_struct *inq_buff = NULL;
2809 for (logvol = 0; logvol < CISS_MAX_LUN; logvol++) {
2810 if (memcmp(h->drv[logvol]->LunID, drv->LunID,
2811 sizeof(drv->LunID)) == 0) {
2812 FOUND = 1;
2813 break;
2817 if (!FOUND)
2818 return 1;
2820 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
2821 if (inq_buff == NULL) {
2822 printk(KERN_WARNING "cciss: out of memory\n");
2823 return 1;
2825 if (h->cciss_read == CCISS_READ_10) {
2826 cciss_read_capacity(h->ctlr, logvol, 1,
2827 &total_size, &block_size);
2828 } else {
2829 cciss_read_capacity_16(h->ctlr, logvol, 1,
2830 &total_size, &block_size);
2832 cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size,
2833 inq_buff, drv);
2835 blk_queue_logical_block_size(drv->queue, drv->block_size);
2836 set_capacity(disk, drv->nr_blocks);
2838 kfree(inq_buff);
2839 return 0;
2843 * Wait polling for a command to complete.
2844 * The memory mapped FIFO is polled for the completion.
2845 * Used only at init time, interrupts from the HBA are disabled.
2847 static unsigned long pollcomplete(int ctlr)
2849 unsigned long done;
2850 int i;
2852 /* Wait (up to 20 seconds) for a command to complete */
2854 for (i = 20 * HZ; i > 0; i--) {
2855 done = hba[ctlr]->access.command_completed(hba[ctlr]);
2856 if (done == FIFO_EMPTY)
2857 schedule_timeout_uninterruptible(1);
2858 else
2859 return done;
2861 /* Invalid address to tell caller we ran out of time */
2862 return 1;
2865 /* Send command c to controller h and poll for it to complete.
2866 * Turns interrupts off on the board. Used at driver init time
2867 * and during SCSI error recovery.
2869 static int sendcmd_core(ctlr_info_t *h, CommandList_struct *c)
2871 int i;
2872 unsigned long complete;
2873 int status = IO_ERROR;
2874 u64bit buff_dma_handle;
2876 resend_cmd1:
2878 /* Disable interrupt on the board. */
2879 h->access.set_intr_mask(h, CCISS_INTR_OFF);
2881 /* Make sure there is room in the command FIFO */
2882 /* Actually it should be completely empty at this time */
2883 /* unless we are in here doing error handling for the scsi */
2884 /* tape side of the driver. */
2885 for (i = 200000; i > 0; i--) {
2886 /* if fifo isn't full go */
2887 if (!(h->access.fifo_full(h)))
2888 break;
2889 udelay(10);
2890 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
2891 " waiting!\n", h->ctlr);
2893 h->access.submit_command(h, c); /* Send the cmd */
2894 do {
2895 complete = pollcomplete(h->ctlr);
2897 #ifdef CCISS_DEBUG
2898 printk(KERN_DEBUG "cciss: command completed\n");
2899 #endif /* CCISS_DEBUG */
2901 if (complete == 1) {
2902 printk(KERN_WARNING
2903 "cciss cciss%d: SendCmd Timeout out, "
2904 "No command list address returned!\n", h->ctlr);
2905 status = IO_ERROR;
2906 break;
2909 /* Make sure it's the command we're expecting. */
2910 if ((complete & ~CISS_ERROR_BIT) != c->busaddr) {
2911 printk(KERN_WARNING "cciss%d: Unexpected command "
2912 "completion.\n", h->ctlr);
2913 continue;
2916 /* It is our command. If no error, we're done. */
2917 if (!(complete & CISS_ERROR_BIT)) {
2918 status = IO_OK;
2919 break;
2922 /* There is an error... */
2924 /* if data overrun or underun on Report command ignore it */
2925 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
2926 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
2927 (c->Request.CDB[0] == CISS_INQUIRY)) &&
2928 ((c->err_info->CommandStatus == CMD_DATA_OVERRUN) ||
2929 (c->err_info->CommandStatus == CMD_DATA_UNDERRUN))) {
2930 complete = c->busaddr;
2931 status = IO_OK;
2932 break;
2935 if (c->err_info->CommandStatus == CMD_UNSOLICITED_ABORT) {
2936 printk(KERN_WARNING "cciss%d: unsolicited abort %p\n",
2937 h->ctlr, c);
2938 if (c->retry_count < MAX_CMD_RETRIES) {
2939 printk(KERN_WARNING "cciss%d: retrying %p\n",
2940 h->ctlr, c);
2941 c->retry_count++;
2942 /* erase the old error information */
2943 memset(c->err_info, 0, sizeof(c->err_info));
2944 goto resend_cmd1;
2946 printk(KERN_WARNING "cciss%d: retried %p too many "
2947 "times\n", h->ctlr, c);
2948 status = IO_ERROR;
2949 break;
2952 if (c->err_info->CommandStatus == CMD_UNABORTABLE) {
2953 printk(KERN_WARNING "cciss%d: command could not be "
2954 "aborted.\n", h->ctlr);
2955 status = IO_ERROR;
2956 break;
2959 if (c->err_info->CommandStatus == CMD_TARGET_STATUS) {
2960 status = check_target_status(h, c);
2961 break;
2964 printk(KERN_WARNING "cciss%d: sendcmd error\n", h->ctlr);
2965 printk(KERN_WARNING "cmd = 0x%02x, CommandStatus = 0x%02x\n",
2966 c->Request.CDB[0], c->err_info->CommandStatus);
2967 status = IO_ERROR;
2968 break;
2970 } while (1);
2972 /* unlock the data buffer from DMA */
2973 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
2974 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
2975 pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
2976 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
2977 return status;
2981 * Send a command to the controller, and wait for it to complete.
2982 * Used at init time, and during SCSI error recovery.
2984 static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size,
2985 __u8 page_code, unsigned char *scsi3addr, int cmd_type)
2987 CommandList_struct *c;
2988 int status;
2990 c = cmd_alloc(hba[ctlr], 1);
2991 if (!c) {
2992 printk(KERN_WARNING "cciss: unable to get memory");
2993 return IO_ERROR;
2995 status = fill_cmd(c, cmd, ctlr, buff, size, page_code,
2996 scsi3addr, cmd_type);
2997 if (status == IO_OK)
2998 status = sendcmd_core(hba[ctlr], c);
2999 cmd_free(hba[ctlr], c, 1);
3000 return status;
3004 * Map (physical) PCI mem into (virtual) kernel space
3006 static void __iomem *remap_pci_mem(ulong base, ulong size)
3008 ulong page_base = ((ulong) base) & PAGE_MASK;
3009 ulong page_offs = ((ulong) base) - page_base;
3010 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
3012 return page_remapped ? (page_remapped + page_offs) : NULL;
3016 * Takes jobs of the Q and sends them to the hardware, then puts it on
3017 * the Q to wait for completion.
3019 static void start_io(ctlr_info_t *h)
3021 CommandList_struct *c;
3023 while (!hlist_empty(&h->reqQ)) {
3024 c = hlist_entry(h->reqQ.first, CommandList_struct, list);
3025 /* can't do anything if fifo is full */
3026 if ((h->access.fifo_full(h))) {
3027 printk(KERN_WARNING "cciss: fifo full\n");
3028 break;
3031 /* Get the first entry from the Request Q */
3032 removeQ(c);
3033 h->Qdepth--;
3035 /* Tell the controller execute command */
3036 h->access.submit_command(h, c);
3038 /* Put job onto the completed Q */
3039 addQ(&h->cmpQ, c);
3043 /* Assumes that CCISS_LOCK(h->ctlr) is held. */
3044 /* Zeros out the error record and then resends the command back */
3045 /* to the controller */
3046 static inline void resend_cciss_cmd(ctlr_info_t *h, CommandList_struct *c)
3048 /* erase the old error information */
3049 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
3051 /* add it to software queue and then send it to the controller */
3052 addQ(&h->reqQ, c);
3053 h->Qdepth++;
3054 if (h->Qdepth > h->maxQsinceinit)
3055 h->maxQsinceinit = h->Qdepth;
3057 start_io(h);
3060 static inline unsigned int make_status_bytes(unsigned int scsi_status_byte,
3061 unsigned int msg_byte, unsigned int host_byte,
3062 unsigned int driver_byte)
3064 /* inverse of macros in scsi.h */
3065 return (scsi_status_byte & 0xff) |
3066 ((msg_byte & 0xff) << 8) |
3067 ((host_byte & 0xff) << 16) |
3068 ((driver_byte & 0xff) << 24);
3071 static inline int evaluate_target_status(ctlr_info_t *h,
3072 CommandList_struct *cmd, int *retry_cmd)
3074 unsigned char sense_key;
3075 unsigned char status_byte, msg_byte, host_byte, driver_byte;
3076 int error_value;
3078 *retry_cmd = 0;
3079 /* If we get in here, it means we got "target status", that is, scsi status */
3080 status_byte = cmd->err_info->ScsiStatus;
3081 driver_byte = DRIVER_OK;
3082 msg_byte = cmd->err_info->CommandStatus; /* correct? seems too device specific */
3084 if (blk_pc_request(cmd->rq))
3085 host_byte = DID_PASSTHROUGH;
3086 else
3087 host_byte = DID_OK;
3089 error_value = make_status_bytes(status_byte, msg_byte,
3090 host_byte, driver_byte);
3092 if (cmd->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) {
3093 if (!blk_pc_request(cmd->rq))
3094 printk(KERN_WARNING "cciss: cmd %p "
3095 "has SCSI Status 0x%x\n",
3096 cmd, cmd->err_info->ScsiStatus);
3097 return error_value;
3100 /* check the sense key */
3101 sense_key = 0xf & cmd->err_info->SenseInfo[2];
3102 /* no status or recovered error */
3103 if (((sense_key == 0x0) || (sense_key == 0x1)) && !blk_pc_request(cmd->rq))
3104 error_value = 0;
3106 if (check_for_unit_attention(h, cmd)) {
3107 *retry_cmd = !blk_pc_request(cmd->rq);
3108 return 0;
3111 if (!blk_pc_request(cmd->rq)) { /* Not SG_IO or similar? */
3112 if (error_value != 0)
3113 printk(KERN_WARNING "cciss: cmd %p has CHECK CONDITION"
3114 " sense key = 0x%x\n", cmd, sense_key);
3115 return error_value;
3118 /* SG_IO or similar, copy sense data back */
3119 if (cmd->rq->sense) {
3120 if (cmd->rq->sense_len > cmd->err_info->SenseLen)
3121 cmd->rq->sense_len = cmd->err_info->SenseLen;
3122 memcpy(cmd->rq->sense, cmd->err_info->SenseInfo,
3123 cmd->rq->sense_len);
3124 } else
3125 cmd->rq->sense_len = 0;
3127 return error_value;
3130 /* checks the status of the job and calls complete buffers to mark all
3131 * buffers for the completed job. Note that this function does not need
3132 * to hold the hba/queue lock.
3134 static inline void complete_command(ctlr_info_t *h, CommandList_struct *cmd,
3135 int timeout)
3137 int retry_cmd = 0;
3138 struct request *rq = cmd->rq;
3140 rq->errors = 0;
3142 if (timeout)
3143 rq->errors = make_status_bytes(0, 0, 0, DRIVER_TIMEOUT);
3145 if (cmd->err_info->CommandStatus == 0) /* no error has occurred */
3146 goto after_error_processing;
3148 switch (cmd->err_info->CommandStatus) {
3149 case CMD_TARGET_STATUS:
3150 rq->errors = evaluate_target_status(h, cmd, &retry_cmd);
3151 break;
3152 case CMD_DATA_UNDERRUN:
3153 if (blk_fs_request(cmd->rq)) {
3154 printk(KERN_WARNING "cciss: cmd %p has"
3155 " completed with data underrun "
3156 "reported\n", cmd);
3157 cmd->rq->resid_len = cmd->err_info->ResidualCnt;
3159 break;
3160 case CMD_DATA_OVERRUN:
3161 if (blk_fs_request(cmd->rq))
3162 printk(KERN_WARNING "cciss: cmd %p has"
3163 " completed with data overrun "
3164 "reported\n", cmd);
3165 break;
3166 case CMD_INVALID:
3167 printk(KERN_WARNING "cciss: cmd %p is "
3168 "reported invalid\n", cmd);
3169 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3170 cmd->err_info->CommandStatus, DRIVER_OK,
3171 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
3172 break;
3173 case CMD_PROTOCOL_ERR:
3174 printk(KERN_WARNING "cciss: cmd %p has "
3175 "protocol error \n", cmd);
3176 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3177 cmd->err_info->CommandStatus, DRIVER_OK,
3178 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
3179 break;
3180 case CMD_HARDWARE_ERR:
3181 printk(KERN_WARNING "cciss: cmd %p had "
3182 " hardware error\n", cmd);
3183 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3184 cmd->err_info->CommandStatus, DRIVER_OK,
3185 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
3186 break;
3187 case CMD_CONNECTION_LOST:
3188 printk(KERN_WARNING "cciss: cmd %p had "
3189 "connection lost\n", cmd);
3190 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3191 cmd->err_info->CommandStatus, DRIVER_OK,
3192 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
3193 break;
3194 case CMD_ABORTED:
3195 printk(KERN_WARNING "cciss: cmd %p was "
3196 "aborted\n", cmd);
3197 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3198 cmd->err_info->CommandStatus, DRIVER_OK,
3199 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT);
3200 break;
3201 case CMD_ABORT_FAILED:
3202 printk(KERN_WARNING "cciss: cmd %p reports "
3203 "abort failed\n", cmd);
3204 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3205 cmd->err_info->CommandStatus, DRIVER_OK,
3206 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
3207 break;
3208 case CMD_UNSOLICITED_ABORT:
3209 printk(KERN_WARNING "cciss%d: unsolicited "
3210 "abort %p\n", h->ctlr, cmd);
3211 if (cmd->retry_count < MAX_CMD_RETRIES) {
3212 retry_cmd = 1;
3213 printk(KERN_WARNING
3214 "cciss%d: retrying %p\n", h->ctlr, cmd);
3215 cmd->retry_count++;
3216 } else
3217 printk(KERN_WARNING
3218 "cciss%d: %p retried too "
3219 "many times\n", h->ctlr, cmd);
3220 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3221 cmd->err_info->CommandStatus, DRIVER_OK,
3222 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT);
3223 break;
3224 case CMD_TIMEOUT:
3225 printk(KERN_WARNING "cciss: cmd %p timedout\n", cmd);
3226 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3227 cmd->err_info->CommandStatus, DRIVER_OK,
3228 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
3229 break;
3230 default:
3231 printk(KERN_WARNING "cciss: cmd %p returned "
3232 "unknown status %x\n", cmd,
3233 cmd->err_info->CommandStatus);
3234 rq->errors = make_status_bytes(SAM_STAT_GOOD,
3235 cmd->err_info->CommandStatus, DRIVER_OK,
3236 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
3239 after_error_processing:
3241 /* We need to return this command */
3242 if (retry_cmd) {
3243 resend_cciss_cmd(h, cmd);
3244 return;
3246 cmd->rq->completion_data = cmd;
3247 blk_complete_request(cmd->rq);
3251 * Get a request and submit it to the controller.
3253 static void do_cciss_request(struct request_queue *q)
3255 ctlr_info_t *h = q->queuedata;
3256 CommandList_struct *c;
3257 sector_t start_blk;
3258 int seg;
3259 struct request *creq;
3260 u64bit temp64;
3261 struct scatterlist tmp_sg[MAXSGENTRIES];
3262 drive_info_struct *drv;
3263 int i, dir;
3265 /* We call start_io here in case there is a command waiting on the
3266 * queue that has not been sent.
3268 if (blk_queue_plugged(q))
3269 goto startio;
3271 queue:
3272 creq = blk_peek_request(q);
3273 if (!creq)
3274 goto startio;
3276 BUG_ON(creq->nr_phys_segments > MAXSGENTRIES);
3278 if ((c = cmd_alloc(h, 1)) == NULL)
3279 goto full;
3281 blk_start_request(creq);
3283 spin_unlock_irq(q->queue_lock);
3285 c->cmd_type = CMD_RWREQ;
3286 c->rq = creq;
3288 /* fill in the request */
3289 drv = creq->rq_disk->private_data;
3290 c->Header.ReplyQueue = 0; // unused in simple mode
3291 /* got command from pool, so use the command block index instead */
3292 /* for direct lookups. */
3293 /* The first 2 bits are reserved for controller error reporting. */
3294 c->Header.Tag.lower = (c->cmdindex << 3);
3295 c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */
3296 memcpy(&c->Header.LUN, drv->LunID, sizeof(drv->LunID));
3297 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
3298 c->Request.Type.Type = TYPE_CMD; // It is a command.
3299 c->Request.Type.Attribute = ATTR_SIMPLE;
3300 c->Request.Type.Direction =
3301 (rq_data_dir(creq) == READ) ? XFER_READ : XFER_WRITE;
3302 c->Request.Timeout = 0; // Don't time out
3303 c->Request.CDB[0] =
3304 (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write;
3305 start_blk = blk_rq_pos(creq);
3306 #ifdef CCISS_DEBUG
3307 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",
3308 (int)blk_rq_pos(creq), (int)blk_rq_sectors(creq));
3309 #endif /* CCISS_DEBUG */
3311 sg_init_table(tmp_sg, MAXSGENTRIES);
3312 seg = blk_rq_map_sg(q, creq, tmp_sg);
3314 /* get the DMA records for the setup */
3315 if (c->Request.Type.Direction == XFER_READ)
3316 dir = PCI_DMA_FROMDEVICE;
3317 else
3318 dir = PCI_DMA_TODEVICE;
3320 for (i = 0; i < seg; i++) {
3321 c->SG[i].Len = tmp_sg[i].length;
3322 temp64.val = (__u64) pci_map_page(h->pdev, sg_page(&tmp_sg[i]),
3323 tmp_sg[i].offset,
3324 tmp_sg[i].length, dir);
3325 c->SG[i].Addr.lower = temp64.val32.lower;
3326 c->SG[i].Addr.upper = temp64.val32.upper;
3327 c->SG[i].Ext = 0; // we are not chaining
3329 /* track how many SG entries we are using */
3330 if (seg > h->maxSG)
3331 h->maxSG = seg;
3333 #ifdef CCISS_DEBUG
3334 printk(KERN_DEBUG "cciss: Submitting %u sectors in %d segments\n",
3335 blk_rq_sectors(creq), seg);
3336 #endif /* CCISS_DEBUG */
3338 c->Header.SGList = c->Header.SGTotal = seg;
3339 if (likely(blk_fs_request(creq))) {
3340 if(h->cciss_read == CCISS_READ_10) {
3341 c->Request.CDB[1] = 0;
3342 c->Request.CDB[2] = (start_blk >> 24) & 0xff; //MSB
3343 c->Request.CDB[3] = (start_blk >> 16) & 0xff;
3344 c->Request.CDB[4] = (start_blk >> 8) & 0xff;
3345 c->Request.CDB[5] = start_blk & 0xff;
3346 c->Request.CDB[6] = 0; // (sect >> 24) & 0xff; MSB
3347 c->Request.CDB[7] = (blk_rq_sectors(creq) >> 8) & 0xff;
3348 c->Request.CDB[8] = blk_rq_sectors(creq) & 0xff;
3349 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
3350 } else {
3351 u32 upper32 = upper_32_bits(start_blk);
3353 c->Request.CDBLen = 16;
3354 c->Request.CDB[1]= 0;
3355 c->Request.CDB[2]= (upper32 >> 24) & 0xff; //MSB
3356 c->Request.CDB[3]= (upper32 >> 16) & 0xff;
3357 c->Request.CDB[4]= (upper32 >> 8) & 0xff;
3358 c->Request.CDB[5]= upper32 & 0xff;
3359 c->Request.CDB[6]= (start_blk >> 24) & 0xff;
3360 c->Request.CDB[7]= (start_blk >> 16) & 0xff;
3361 c->Request.CDB[8]= (start_blk >> 8) & 0xff;
3362 c->Request.CDB[9]= start_blk & 0xff;
3363 c->Request.CDB[10]= (blk_rq_sectors(creq) >> 24) & 0xff;
3364 c->Request.CDB[11]= (blk_rq_sectors(creq) >> 16) & 0xff;
3365 c->Request.CDB[12]= (blk_rq_sectors(creq) >> 8) & 0xff;
3366 c->Request.CDB[13]= blk_rq_sectors(creq) & 0xff;
3367 c->Request.CDB[14] = c->Request.CDB[15] = 0;
3369 } else if (blk_pc_request(creq)) {
3370 c->Request.CDBLen = creq->cmd_len;
3371 memcpy(c->Request.CDB, creq->cmd, BLK_MAX_CDB);
3372 } else {
3373 printk(KERN_WARNING "cciss%d: bad request type %d\n", h->ctlr, creq->cmd_type);
3374 BUG();
3377 spin_lock_irq(q->queue_lock);
3379 addQ(&h->reqQ, c);
3380 h->Qdepth++;
3381 if (h->Qdepth > h->maxQsinceinit)
3382 h->maxQsinceinit = h->Qdepth;
3384 goto queue;
3385 full:
3386 blk_stop_queue(q);
3387 startio:
3388 /* We will already have the driver lock here so not need
3389 * to lock it.
3391 start_io(h);
3394 static inline unsigned long get_next_completion(ctlr_info_t *h)
3396 return h->access.command_completed(h);
3399 static inline int interrupt_pending(ctlr_info_t *h)
3401 return h->access.intr_pending(h);
3404 static inline long interrupt_not_for_us(ctlr_info_t *h)
3406 return (((h->access.intr_pending(h) == 0) ||
3407 (h->interrupts_enabled == 0)));
3410 static irqreturn_t do_cciss_intr(int irq, void *dev_id)
3412 ctlr_info_t *h = dev_id;
3413 CommandList_struct *c;
3414 unsigned long flags;
3415 __u32 a, a1, a2;
3417 if (interrupt_not_for_us(h))
3418 return IRQ_NONE;
3420 * If there are completed commands in the completion queue,
3421 * we had better do something about it.
3423 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
3424 while (interrupt_pending(h)) {
3425 while ((a = get_next_completion(h)) != FIFO_EMPTY) {
3426 a1 = a;
3427 if ((a & 0x04)) {
3428 a2 = (a >> 3);
3429 if (a2 >= h->nr_cmds) {
3430 printk(KERN_WARNING
3431 "cciss: controller cciss%d failed, stopping.\n",
3432 h->ctlr);
3433 fail_all_cmds(h->ctlr);
3434 return IRQ_HANDLED;
3437 c = h->cmd_pool + a2;
3438 a = c->busaddr;
3440 } else {
3441 struct hlist_node *tmp;
3443 a &= ~3;
3444 c = NULL;
3445 hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
3446 if (c->busaddr == a)
3447 break;
3451 * If we've found the command, take it off the
3452 * completion Q and free it
3454 if (c && c->busaddr == a) {
3455 removeQ(c);
3456 if (c->cmd_type == CMD_RWREQ) {
3457 complete_command(h, c, 0);
3458 } else if (c->cmd_type == CMD_IOCTL_PEND) {
3459 complete(c->waiting);
3461 # ifdef CONFIG_CISS_SCSI_TAPE
3462 else if (c->cmd_type == CMD_SCSI)
3463 complete_scsi_command(c, 0, a1);
3464 # endif
3465 continue;
3470 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
3471 return IRQ_HANDLED;
3475 * add_to_scan_list() - add controller to rescan queue
3476 * @h: Pointer to the controller.
3478 * Adds the controller to the rescan queue if not already on the queue.
3480 * returns 1 if added to the queue, 0 if skipped (could be on the
3481 * queue already, or the controller could be initializing or shutting
3482 * down).
3484 static int add_to_scan_list(struct ctlr_info *h)
3486 struct ctlr_info *test_h;
3487 int found = 0;
3488 int ret = 0;
3490 if (h->busy_initializing)
3491 return 0;
3493 if (!mutex_trylock(&h->busy_shutting_down))
3494 return 0;
3496 mutex_lock(&scan_mutex);
3497 list_for_each_entry(test_h, &scan_q, scan_list) {
3498 if (test_h == h) {
3499 found = 1;
3500 break;
3503 if (!found && !h->busy_scanning) {
3504 INIT_COMPLETION(h->scan_wait);
3505 list_add_tail(&h->scan_list, &scan_q);
3506 ret = 1;
3508 mutex_unlock(&scan_mutex);
3509 mutex_unlock(&h->busy_shutting_down);
3511 return ret;
3515 * remove_from_scan_list() - remove controller from rescan queue
3516 * @h: Pointer to the controller.
3518 * Removes the controller from the rescan queue if present. Blocks if
3519 * the controller is currently conducting a rescan.
3521 static void remove_from_scan_list(struct ctlr_info *h)
3523 struct ctlr_info *test_h, *tmp_h;
3524 int scanning = 0;
3526 mutex_lock(&scan_mutex);
3527 list_for_each_entry_safe(test_h, tmp_h, &scan_q, scan_list) {
3528 if (test_h == h) {
3529 list_del(&h->scan_list);
3530 complete_all(&h->scan_wait);
3531 mutex_unlock(&scan_mutex);
3532 return;
3535 if (&h->busy_scanning)
3536 scanning = 0;
3537 mutex_unlock(&scan_mutex);
3539 if (scanning)
3540 wait_for_completion(&h->scan_wait);
3544 * scan_thread() - kernel thread used to rescan controllers
3545 * @data: Ignored.
3547 * A kernel thread used scan for drive topology changes on
3548 * controllers. The thread processes only one controller at a time
3549 * using a queue. Controllers are added to the queue using
3550 * add_to_scan_list() and removed from the queue either after done
3551 * processing or using remove_from_scan_list().
3553 * returns 0.
3555 static int scan_thread(void *data)
3557 struct ctlr_info *h;
3559 while (1) {
3560 set_current_state(TASK_INTERRUPTIBLE);
3561 schedule();
3562 if (kthread_should_stop())
3563 break;
3565 while (1) {
3566 mutex_lock(&scan_mutex);
3567 if (list_empty(&scan_q)) {
3568 mutex_unlock(&scan_mutex);
3569 break;
3572 h = list_entry(scan_q.next,
3573 struct ctlr_info,
3574 scan_list);
3575 list_del(&h->scan_list);
3576 h->busy_scanning = 1;
3577 mutex_unlock(&scan_mutex);
3579 if (h) {
3580 rebuild_lun_table(h, 0, 0);
3581 complete_all(&h->scan_wait);
3582 mutex_lock(&scan_mutex);
3583 h->busy_scanning = 0;
3584 mutex_unlock(&scan_mutex);
3589 return 0;
3592 static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c)
3594 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
3595 return 0;
3597 switch (c->err_info->SenseInfo[12]) {
3598 case STATE_CHANGED:
3599 printk(KERN_WARNING "cciss%d: a state change "
3600 "detected, command retried\n", h->ctlr);
3601 return 1;
3602 break;
3603 case LUN_FAILED:
3604 printk(KERN_WARNING "cciss%d: LUN failure "
3605 "detected, action required\n", h->ctlr);
3606 return 1;
3607 break;
3608 case REPORT_LUNS_CHANGED:
3609 printk(KERN_WARNING "cciss%d: report LUN data "
3610 "changed\n", h->ctlr);
3611 add_to_scan_list(h);
3612 wake_up_process(cciss_scan_thread);
3613 return 1;
3614 break;
3615 case POWER_OR_RESET:
3616 printk(KERN_WARNING "cciss%d: a power on "
3617 "or device reset detected\n", h->ctlr);
3618 return 1;
3619 break;
3620 case UNIT_ATTENTION_CLEARED:
3621 printk(KERN_WARNING "cciss%d: unit attention "
3622 "cleared by another initiator\n", h->ctlr);
3623 return 1;
3624 break;
3625 default:
3626 printk(KERN_WARNING "cciss%d: unknown "
3627 "unit attention detected\n", h->ctlr);
3628 return 1;
3633 * We cannot read the structure directly, for portability we must use
3634 * the io functions.
3635 * This is for debug only.
3637 #ifdef CCISS_DEBUG
3638 static void print_cfg_table(CfgTable_struct *tb)
3640 int i;
3641 char temp_name[17];
3643 printk("Controller Configuration information\n");
3644 printk("------------------------------------\n");
3645 for (i = 0; i < 4; i++)
3646 temp_name[i] = readb(&(tb->Signature[i]));
3647 temp_name[4] = '\0';
3648 printk(" Signature = %s\n", temp_name);
3649 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
3650 printk(" Transport methods supported = 0x%x\n",
3651 readl(&(tb->TransportSupport)));
3652 printk(" Transport methods active = 0x%x\n",
3653 readl(&(tb->TransportActive)));
3654 printk(" Requested transport Method = 0x%x\n",
3655 readl(&(tb->HostWrite.TransportRequest)));
3656 printk(" Coalesce Interrupt Delay = 0x%x\n",
3657 readl(&(tb->HostWrite.CoalIntDelay)));
3658 printk(" Coalesce Interrupt Count = 0x%x\n",
3659 readl(&(tb->HostWrite.CoalIntCount)));
3660 printk(" Max outstanding commands = 0x%d\n",
3661 readl(&(tb->CmdsOutMax)));
3662 printk(" Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
3663 for (i = 0; i < 16; i++)
3664 temp_name[i] = readb(&(tb->ServerName[i]));
3665 temp_name[16] = '\0';
3666 printk(" Server Name = %s\n", temp_name);
3667 printk(" Heartbeat Counter = 0x%x\n\n\n", readl(&(tb->HeartBeat)));
3669 #endif /* CCISS_DEBUG */
3671 static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
3673 int i, offset, mem_type, bar_type;
3674 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
3675 return 0;
3676 offset = 0;
3677 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3678 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
3679 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
3680 offset += 4;
3681 else {
3682 mem_type = pci_resource_flags(pdev, i) &
3683 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
3684 switch (mem_type) {
3685 case PCI_BASE_ADDRESS_MEM_TYPE_32:
3686 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
3687 offset += 4; /* 32 bit */
3688 break;
3689 case PCI_BASE_ADDRESS_MEM_TYPE_64:
3690 offset += 8;
3691 break;
3692 default: /* reserved in PCI 2.2 */
3693 printk(KERN_WARNING
3694 "Base address is invalid\n");
3695 return -1;
3696 break;
3699 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
3700 return i + 1;
3702 return -1;
3705 /* If MSI/MSI-X is supported by the kernel we will try to enable it on
3706 * controllers that are capable. If not, we use IO-APIC mode.
3709 static void __devinit cciss_interrupt_mode(ctlr_info_t *c,
3710 struct pci_dev *pdev, __u32 board_id)
3712 #ifdef CONFIG_PCI_MSI
3713 int err;
3714 struct msix_entry cciss_msix_entries[4] = { {0, 0}, {0, 1},
3715 {0, 2}, {0, 3}
3718 /* Some boards advertise MSI but don't really support it */
3719 if ((board_id == 0x40700E11) ||
3720 (board_id == 0x40800E11) ||
3721 (board_id == 0x40820E11) || (board_id == 0x40830E11))
3722 goto default_int_mode;
3724 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
3725 err = pci_enable_msix(pdev, cciss_msix_entries, 4);
3726 if (!err) {
3727 c->intr[0] = cciss_msix_entries[0].vector;
3728 c->intr[1] = cciss_msix_entries[1].vector;
3729 c->intr[2] = cciss_msix_entries[2].vector;
3730 c->intr[3] = cciss_msix_entries[3].vector;
3731 c->msix_vector = 1;
3732 return;
3734 if (err > 0) {
3735 printk(KERN_WARNING "cciss: only %d MSI-X vectors "
3736 "available\n", err);
3737 goto default_int_mode;
3738 } else {
3739 printk(KERN_WARNING "cciss: MSI-X init failed %d\n",
3740 err);
3741 goto default_int_mode;
3744 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
3745 if (!pci_enable_msi(pdev)) {
3746 c->msi_vector = 1;
3747 } else {
3748 printk(KERN_WARNING "cciss: MSI init failed\n");
3751 default_int_mode:
3752 #endif /* CONFIG_PCI_MSI */
3753 /* if we get here we're going to use the default interrupt mode */
3754 c->intr[SIMPLE_MODE_INT] = pdev->irq;
3755 return;
3758 static int __devinit cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
3760 ushort subsystem_vendor_id, subsystem_device_id, command;
3761 __u32 board_id, scratchpad = 0;
3762 __u64 cfg_offset;
3763 __u32 cfg_base_addr;
3764 __u64 cfg_base_addr_index;
3765 int i, prod_index, err;
3767 subsystem_vendor_id = pdev->subsystem_vendor;
3768 subsystem_device_id = pdev->subsystem_device;
3769 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
3770 subsystem_vendor_id);
3772 for (i = 0; i < ARRAY_SIZE(products); i++) {
3773 /* Stand aside for hpsa driver on request */
3774 if (cciss_allow_hpsa && products[i].board_id == HPSA_BOUNDARY)
3775 return -ENODEV;
3776 if (board_id == products[i].board_id)
3777 break;
3779 prod_index = i;
3780 if (prod_index == ARRAY_SIZE(products)) {
3781 dev_warn(&pdev->dev,
3782 "unrecognized board ID: 0x%08lx, ignoring.\n",
3783 (unsigned long) board_id);
3784 return -ENODEV;
3787 /* check to see if controller has been disabled */
3788 /* BEFORE trying to enable it */
3789 (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
3790 if (!(command & 0x02)) {
3791 printk(KERN_WARNING
3792 "cciss: controller appears to be disabled\n");
3793 return -ENODEV;
3796 err = pci_enable_device(pdev);
3797 if (err) {
3798 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
3799 return err;
3802 err = pci_request_regions(pdev, "cciss");
3803 if (err) {
3804 printk(KERN_ERR "cciss: Cannot obtain PCI resources, "
3805 "aborting\n");
3806 return err;
3809 #ifdef CCISS_DEBUG
3810 printk("command = %x\n", command);
3811 printk("irq = %x\n", pdev->irq);
3812 printk("board_id = %x\n", board_id);
3813 #endif /* CCISS_DEBUG */
3815 /* If the kernel supports MSI/MSI-X we will try to enable that functionality,
3816 * else we use the IO-APIC interrupt assigned to us by system ROM.
3818 cciss_interrupt_mode(c, pdev, board_id);
3820 /* find the memory BAR */
3821 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3822 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
3823 break;
3825 if (i == DEVICE_COUNT_RESOURCE) {
3826 printk(KERN_WARNING "cciss: No memory BAR found\n");
3827 err = -ENODEV;
3828 goto err_out_free_res;
3831 c->paddr = pci_resource_start(pdev, i); /* addressing mode bits
3832 * already removed
3835 #ifdef CCISS_DEBUG
3836 printk("address 0 = %lx\n", c->paddr);
3837 #endif /* CCISS_DEBUG */
3838 c->vaddr = remap_pci_mem(c->paddr, 0x250);
3840 /* Wait for the board to become ready. (PCI hotplug needs this.)
3841 * We poll for up to 120 secs, once per 100ms. */
3842 for (i = 0; i < 1200; i++) {
3843 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
3844 if (scratchpad == CCISS_FIRMWARE_READY)
3845 break;
3846 set_current_state(TASK_INTERRUPTIBLE);
3847 schedule_timeout(msecs_to_jiffies(100)); /* wait 100ms */
3849 if (scratchpad != CCISS_FIRMWARE_READY) {
3850 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
3851 err = -ENODEV;
3852 goto err_out_free_res;
3855 /* get the address index number */
3856 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
3857 cfg_base_addr &= (__u32) 0x0000ffff;
3858 #ifdef CCISS_DEBUG
3859 printk("cfg base address = %x\n", cfg_base_addr);
3860 #endif /* CCISS_DEBUG */
3861 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
3862 #ifdef CCISS_DEBUG
3863 printk("cfg base address index = %llx\n",
3864 (unsigned long long)cfg_base_addr_index);
3865 #endif /* CCISS_DEBUG */
3866 if (cfg_base_addr_index == -1) {
3867 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
3868 err = -ENODEV;
3869 goto err_out_free_res;
3872 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
3873 #ifdef CCISS_DEBUG
3874 printk("cfg offset = %llx\n", (unsigned long long)cfg_offset);
3875 #endif /* CCISS_DEBUG */
3876 c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
3877 cfg_base_addr_index) +
3878 cfg_offset, sizeof(CfgTable_struct));
3879 c->board_id = board_id;
3881 #ifdef CCISS_DEBUG
3882 print_cfg_table(c->cfgtable);
3883 #endif /* CCISS_DEBUG */
3885 /* Some controllers support Zero Memory Raid (ZMR).
3886 * When configured in ZMR mode the number of supported
3887 * commands drops to 64. So instead of just setting an
3888 * arbitrary value we make the driver a little smarter.
3889 * We read the config table to tell us how many commands
3890 * are supported on the controller then subtract 4 to
3891 * leave a little room for ioctl calls.
3893 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
3894 c->product_name = products[prod_index].product_name;
3895 c->access = *(products[prod_index].access);
3896 c->nr_cmds = c->max_commands - 4;
3897 if ((readb(&c->cfgtable->Signature[0]) != 'C') ||
3898 (readb(&c->cfgtable->Signature[1]) != 'I') ||
3899 (readb(&c->cfgtable->Signature[2]) != 'S') ||
3900 (readb(&c->cfgtable->Signature[3]) != 'S')) {
3901 printk("Does not appear to be a valid CISS config table\n");
3902 err = -ENODEV;
3903 goto err_out_free_res;
3905 #ifdef CONFIG_X86
3907 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3908 __u32 prefetch;
3909 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
3910 prefetch |= 0x100;
3911 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
3913 #endif
3915 /* Disabling DMA prefetch and refetch for the P600.
3916 * An ASIC bug may result in accesses to invalid memory addresses.
3917 * We've disabled prefetch for some time now. Testing with XEN
3918 * kernels revealed a bug in the refetch if dom0 resides on a P600.
3920 if(board_id == 0x3225103C) {
3921 __u32 dma_prefetch;
3922 __u32 dma_refetch;
3923 dma_prefetch = readl(c->vaddr + I2O_DMA1_CFG);
3924 dma_prefetch |= 0x8000;
3925 writel(dma_prefetch, c->vaddr + I2O_DMA1_CFG);
3926 pci_read_config_dword(pdev, PCI_COMMAND_PARITY, &dma_refetch);
3927 dma_refetch |= 0x1;
3928 pci_write_config_dword(pdev, PCI_COMMAND_PARITY, dma_refetch);
3931 #ifdef CCISS_DEBUG
3932 printk("Trying to put board into Simple mode\n");
3933 #endif /* CCISS_DEBUG */
3934 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
3935 /* Update the field, and then ring the doorbell */
3936 writel(CFGTBL_Trans_Simple, &(c->cfgtable->HostWrite.TransportRequest));
3937 writel(CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
3939 /* under certain very rare conditions, this can take awhile.
3940 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3941 * as we enter this code.) */
3942 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
3943 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3944 break;
3945 /* delay and try again */
3946 set_current_state(TASK_INTERRUPTIBLE);
3947 schedule_timeout(msecs_to_jiffies(1));
3950 #ifdef CCISS_DEBUG
3951 printk(KERN_DEBUG "I counter got to %d %x\n", i,
3952 readl(c->vaddr + SA5_DOORBELL));
3953 #endif /* CCISS_DEBUG */
3954 #ifdef CCISS_DEBUG
3955 print_cfg_table(c->cfgtable);
3956 #endif /* CCISS_DEBUG */
3958 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
3959 printk(KERN_WARNING "cciss: unable to get board into"
3960 " simple mode\n");
3961 err = -ENODEV;
3962 goto err_out_free_res;
3964 return 0;
3966 err_out_free_res:
3968 * Deliberately omit pci_disable_device(): it does something nasty to
3969 * Smart Array controllers that pci_enable_device does not undo
3971 pci_release_regions(pdev);
3972 return err;
3975 /* Function to find the first free pointer into our hba[] array
3976 * Returns -1 if no free entries are left.
3978 static int alloc_cciss_hba(void)
3980 int i;
3982 for (i = 0; i < MAX_CTLR; i++) {
3983 if (!hba[i]) {
3984 ctlr_info_t *p;
3986 p = kzalloc(sizeof(ctlr_info_t), GFP_KERNEL);
3987 if (!p)
3988 goto Enomem;
3989 hba[i] = p;
3990 return i;
3993 printk(KERN_WARNING "cciss: This driver supports a maximum"
3994 " of %d controllers.\n", MAX_CTLR);
3995 return -1;
3996 Enomem:
3997 printk(KERN_ERR "cciss: out of memory.\n");
3998 return -1;
4001 static void free_hba(int n)
4003 ctlr_info_t *h = hba[n];
4004 int i;
4006 hba[n] = NULL;
4007 for (i = 0; i < h->highest_lun + 1; i++)
4008 if (h->gendisk[i] != NULL)
4009 put_disk(h->gendisk[i]);
4010 kfree(h);
4013 /* Send a message CDB to the firmware. */
4014 static __devinit int cciss_message(struct pci_dev *pdev, unsigned char opcode, unsigned char type)
4016 typedef struct {
4017 CommandListHeader_struct CommandHeader;
4018 RequestBlock_struct Request;
4019 ErrDescriptor_struct ErrorDescriptor;
4020 } Command;
4021 static const size_t cmd_sz = sizeof(Command) + sizeof(ErrorInfo_struct);
4022 Command *cmd;
4023 dma_addr_t paddr64;
4024 uint32_t paddr32, tag;
4025 void __iomem *vaddr;
4026 int i, err;
4028 vaddr = ioremap_nocache(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
4029 if (vaddr == NULL)
4030 return -ENOMEM;
4032 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
4033 CCISS commands, so they must be allocated from the lower 4GiB of
4034 memory. */
4035 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
4036 if (err) {
4037 iounmap(vaddr);
4038 return -ENOMEM;
4041 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
4042 if (cmd == NULL) {
4043 iounmap(vaddr);
4044 return -ENOMEM;
4047 /* This must fit, because of the 32-bit consistent DMA mask. Also,
4048 although there's no guarantee, we assume that the address is at
4049 least 4-byte aligned (most likely, it's page-aligned). */
4050 paddr32 = paddr64;
4052 cmd->CommandHeader.ReplyQueue = 0;
4053 cmd->CommandHeader.SGList = 0;
4054 cmd->CommandHeader.SGTotal = 0;
4055 cmd->CommandHeader.Tag.lower = paddr32;
4056 cmd->CommandHeader.Tag.upper = 0;
4057 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
4059 cmd->Request.CDBLen = 16;
4060 cmd->Request.Type.Type = TYPE_MSG;
4061 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
4062 cmd->Request.Type.Direction = XFER_NONE;
4063 cmd->Request.Timeout = 0; /* Don't time out */
4064 cmd->Request.CDB[0] = opcode;
4065 cmd->Request.CDB[1] = type;
4066 memset(&cmd->Request.CDB[2], 0, 14); /* the rest of the CDB is reserved */
4068 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(Command);
4069 cmd->ErrorDescriptor.Addr.upper = 0;
4070 cmd->ErrorDescriptor.Len = sizeof(ErrorInfo_struct);
4072 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
4074 for (i = 0; i < 10; i++) {
4075 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
4076 if ((tag & ~3) == paddr32)
4077 break;
4078 schedule_timeout_uninterruptible(HZ);
4081 iounmap(vaddr);
4083 /* we leak the DMA buffer here ... no choice since the controller could
4084 still complete the command. */
4085 if (i == 10) {
4086 printk(KERN_ERR "cciss: controller message %02x:%02x timed out\n",
4087 opcode, type);
4088 return -ETIMEDOUT;
4091 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
4093 if (tag & 2) {
4094 printk(KERN_ERR "cciss: controller message %02x:%02x failed\n",
4095 opcode, type);
4096 return -EIO;
4099 printk(KERN_INFO "cciss: controller message %02x:%02x succeeded\n",
4100 opcode, type);
4101 return 0;
4104 #define cciss_soft_reset_controller(p) cciss_message(p, 1, 0)
4105 #define cciss_noop(p) cciss_message(p, 3, 0)
4107 static __devinit int cciss_reset_msi(struct pci_dev *pdev)
4109 /* the #defines are stolen from drivers/pci/msi.h. */
4110 #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
4111 #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
4113 int pos;
4114 u16 control = 0;
4116 pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
4117 if (pos) {
4118 pci_read_config_word(pdev, msi_control_reg(pos), &control);
4119 if (control & PCI_MSI_FLAGS_ENABLE) {
4120 printk(KERN_INFO "cciss: resetting MSI\n");
4121 pci_write_config_word(pdev, msi_control_reg(pos), control & ~PCI_MSI_FLAGS_ENABLE);
4125 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
4126 if (pos) {
4127 pci_read_config_word(pdev, msi_control_reg(pos), &control);
4128 if (control & PCI_MSIX_FLAGS_ENABLE) {
4129 printk(KERN_INFO "cciss: resetting MSI-X\n");
4130 pci_write_config_word(pdev, msi_control_reg(pos), control & ~PCI_MSIX_FLAGS_ENABLE);
4134 return 0;
4137 /* This does a hard reset of the controller using PCI power management
4138 * states. */
4139 static __devinit int cciss_hard_reset_controller(struct pci_dev *pdev)
4141 u16 pmcsr, saved_config_space[32];
4142 int i, pos;
4144 printk(KERN_INFO "cciss: using PCI PM to reset controller\n");
4146 /* This is very nearly the same thing as
4148 pci_save_state(pci_dev);
4149 pci_set_power_state(pci_dev, PCI_D3hot);
4150 pci_set_power_state(pci_dev, PCI_D0);
4151 pci_restore_state(pci_dev);
4153 but we can't use these nice canned kernel routines on
4154 kexec, because they also check the MSI/MSI-X state in PCI
4155 configuration space and do the wrong thing when it is
4156 set/cleared. Also, the pci_save/restore_state functions
4157 violate the ordering requirements for restoring the
4158 configuration space from the CCISS document (see the
4159 comment below). So we roll our own .... */
4161 for (i = 0; i < 32; i++)
4162 pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
4164 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
4165 if (pos == 0) {
4166 printk(KERN_ERR "cciss_reset_controller: PCI PM not supported\n");
4167 return -ENODEV;
4170 /* Quoting from the Open CISS Specification: "The Power
4171 * Management Control/Status Register (CSR) controls the power
4172 * state of the device. The normal operating state is D0,
4173 * CSR=00h. The software off state is D3, CSR=03h. To reset
4174 * the controller, place the interface device in D3 then to
4175 * D0, this causes a secondary PCI reset which will reset the
4176 * controller." */
4178 /* enter the D3hot power management state */
4179 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
4180 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4181 pmcsr |= PCI_D3hot;
4182 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
4184 schedule_timeout_uninterruptible(HZ >> 1);
4186 /* enter the D0 power management state */
4187 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4188 pmcsr |= PCI_D0;
4189 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
4191 schedule_timeout_uninterruptible(HZ >> 1);
4193 /* Restore the PCI configuration space. The Open CISS
4194 * Specification says, "Restore the PCI Configuration
4195 * Registers, offsets 00h through 60h. It is important to
4196 * restore the command register, 16-bits at offset 04h,
4197 * last. Do not restore the configuration status register,
4198 * 16-bits at offset 06h." Note that the offset is 2*i. */
4199 for (i = 0; i < 32; i++) {
4200 if (i == 2 || i == 3)
4201 continue;
4202 pci_write_config_word(pdev, 2*i, saved_config_space[i]);
4204 wmb();
4205 pci_write_config_word(pdev, 4, saved_config_space[2]);
4207 return 0;
4211 * This is it. Find all the controllers and register them. I really hate
4212 * stealing all these major device numbers.
4213 * returns the number of block devices registered.
4215 static int __devinit cciss_init_one(struct pci_dev *pdev,
4216 const struct pci_device_id *ent)
4218 int i;
4219 int j = 0;
4220 int rc;
4221 int dac, return_code;
4222 InquiryData_struct *inq_buff;
4224 if (reset_devices) {
4225 /* Reset the controller with a PCI power-cycle */
4226 if (cciss_hard_reset_controller(pdev) || cciss_reset_msi(pdev))
4227 return -ENODEV;
4229 /* Now try to get the controller to respond to a no-op. Some
4230 devices (notably the HP Smart Array 5i Controller) need
4231 up to 30 seconds to respond. */
4232 for (i=0; i<30; i++) {
4233 if (cciss_noop(pdev) == 0)
4234 break;
4236 schedule_timeout_uninterruptible(HZ);
4238 if (i == 30) {
4239 printk(KERN_ERR "cciss: controller seems dead\n");
4240 return -EBUSY;
4244 i = alloc_cciss_hba();
4245 if (i < 0)
4246 return -1;
4248 hba[i]->busy_initializing = 1;
4249 INIT_HLIST_HEAD(&hba[i]->cmpQ);
4250 INIT_HLIST_HEAD(&hba[i]->reqQ);
4251 mutex_init(&hba[i]->busy_shutting_down);
4253 if (cciss_pci_init(hba[i], pdev) != 0)
4254 goto clean_no_release_regions;
4256 sprintf(hba[i]->devname, "cciss%d", i);
4257 hba[i]->ctlr = i;
4258 hba[i]->pdev = pdev;
4260 init_completion(&hba[i]->scan_wait);
4262 if (cciss_create_hba_sysfs_entry(hba[i]))
4263 goto clean0;
4265 /* configure PCI DMA stuff */
4266 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
4267 dac = 1;
4268 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))
4269 dac = 0;
4270 else {
4271 printk(KERN_ERR "cciss: no suitable DMA available\n");
4272 goto clean1;
4276 * register with the major number, or get a dynamic major number
4277 * by passing 0 as argument. This is done for greater than
4278 * 8 controller support.
4280 if (i < MAX_CTLR_ORIG)
4281 hba[i]->major = COMPAQ_CISS_MAJOR + i;
4282 rc = register_blkdev(hba[i]->major, hba[i]->devname);
4283 if (rc == -EBUSY || rc == -EINVAL) {
4284 printk(KERN_ERR
4285 "cciss: Unable to get major number %d for %s "
4286 "on hba %d\n", hba[i]->major, hba[i]->devname, i);
4287 goto clean1;
4288 } else {
4289 if (i >= MAX_CTLR_ORIG)
4290 hba[i]->major = rc;
4293 /* make sure the board interrupts are off */
4294 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
4295 if (request_irq(hba[i]->intr[SIMPLE_MODE_INT], do_cciss_intr,
4296 IRQF_DISABLED | IRQF_SHARED, hba[i]->devname, hba[i])) {
4297 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
4298 hba[i]->intr[SIMPLE_MODE_INT], hba[i]->devname);
4299 goto clean2;
4302 printk(KERN_INFO "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
4303 hba[i]->devname, pdev->device, pci_name(pdev),
4304 hba[i]->intr[SIMPLE_MODE_INT], dac ? "" : " not");
4306 hba[i]->cmd_pool_bits =
4307 kmalloc(DIV_ROUND_UP(hba[i]->nr_cmds, BITS_PER_LONG)
4308 * sizeof(unsigned long), GFP_KERNEL);
4309 hba[i]->cmd_pool = (CommandList_struct *)
4310 pci_alloc_consistent(hba[i]->pdev,
4311 hba[i]->nr_cmds * sizeof(CommandList_struct),
4312 &(hba[i]->cmd_pool_dhandle));
4313 hba[i]->errinfo_pool = (ErrorInfo_struct *)
4314 pci_alloc_consistent(hba[i]->pdev,
4315 hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
4316 &(hba[i]->errinfo_pool_dhandle));
4317 if ((hba[i]->cmd_pool_bits == NULL)
4318 || (hba[i]->cmd_pool == NULL)
4319 || (hba[i]->errinfo_pool == NULL)) {
4320 printk(KERN_ERR "cciss: out of memory");
4321 goto clean4;
4323 spin_lock_init(&hba[i]->lock);
4325 /* Initialize the pdev driver private data.
4326 have it point to hba[i]. */
4327 pci_set_drvdata(pdev, hba[i]);
4328 /* command and error info recs zeroed out before
4329 they are used */
4330 memset(hba[i]->cmd_pool_bits, 0,
4331 DIV_ROUND_UP(hba[i]->nr_cmds, BITS_PER_LONG)
4332 * sizeof(unsigned long));
4334 hba[i]->num_luns = 0;
4335 hba[i]->highest_lun = -1;
4336 for (j = 0; j < CISS_MAX_LUN; j++) {
4337 hba[i]->drv[j] = NULL;
4338 hba[i]->gendisk[j] = NULL;
4341 cciss_scsi_setup(i);
4343 /* Turn the interrupts on so we can service requests */
4344 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
4346 /* Get the firmware version */
4347 inq_buff = kzalloc(sizeof(InquiryData_struct), GFP_KERNEL);
4348 if (inq_buff == NULL) {
4349 printk(KERN_ERR "cciss: out of memory\n");
4350 goto clean4;
4353 return_code = sendcmd_withirq(CISS_INQUIRY, i, inq_buff,
4354 sizeof(InquiryData_struct), 0, CTLR_LUNID, TYPE_CMD);
4355 if (return_code == IO_OK) {
4356 hba[i]->firm_ver[0] = inq_buff->data_byte[32];
4357 hba[i]->firm_ver[1] = inq_buff->data_byte[33];
4358 hba[i]->firm_ver[2] = inq_buff->data_byte[34];
4359 hba[i]->firm_ver[3] = inq_buff->data_byte[35];
4360 } else { /* send command failed */
4361 printk(KERN_WARNING "cciss: unable to determine firmware"
4362 " version of controller\n");
4364 kfree(inq_buff);
4366 cciss_procinit(i);
4368 hba[i]->cciss_max_sectors = 2048;
4370 rebuild_lun_table(hba[i], 1, 0);
4371 hba[i]->busy_initializing = 0;
4372 return 1;
4374 clean4:
4375 kfree(hba[i]->cmd_pool_bits);
4376 if (hba[i]->cmd_pool)
4377 pci_free_consistent(hba[i]->pdev,
4378 hba[i]->nr_cmds * sizeof(CommandList_struct),
4379 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
4380 if (hba[i]->errinfo_pool)
4381 pci_free_consistent(hba[i]->pdev,
4382 hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
4383 hba[i]->errinfo_pool,
4384 hba[i]->errinfo_pool_dhandle);
4385 free_irq(hba[i]->intr[SIMPLE_MODE_INT], hba[i]);
4386 clean2:
4387 unregister_blkdev(hba[i]->major, hba[i]->devname);
4388 clean1:
4389 cciss_destroy_hba_sysfs_entry(hba[i]);
4390 clean0:
4391 pci_release_regions(pdev);
4392 clean_no_release_regions:
4393 hba[i]->busy_initializing = 0;
4396 * Deliberately omit pci_disable_device(): it does something nasty to
4397 * Smart Array controllers that pci_enable_device does not undo
4399 pci_set_drvdata(pdev, NULL);
4400 free_hba(i);
4401 return -1;
4404 static void cciss_shutdown(struct pci_dev *pdev)
4406 ctlr_info_t *tmp_ptr;
4407 int i;
4408 char flush_buf[4];
4409 int return_code;
4411 tmp_ptr = pci_get_drvdata(pdev);
4412 if (tmp_ptr == NULL)
4413 return;
4414 i = tmp_ptr->ctlr;
4415 if (hba[i] == NULL)
4416 return;
4418 /* Turn board interrupts off and send the flush cache command */
4419 /* sendcmd will turn off interrupt, and send the flush...
4420 * To write all data in the battery backed cache to disks */
4421 memset(flush_buf, 0, 4);
4422 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0,
4423 CTLR_LUNID, TYPE_CMD);
4424 if (return_code == IO_OK) {
4425 printk(KERN_INFO "Completed flushing cache on controller %d\n", i);
4426 } else {
4427 printk(KERN_WARNING "Error flushing cache on controller %d\n", i);
4429 free_irq(hba[i]->intr[2], hba[i]);
4432 static void __devexit cciss_remove_one(struct pci_dev *pdev)
4434 ctlr_info_t *tmp_ptr;
4435 int i, j;
4437 if (pci_get_drvdata(pdev) == NULL) {
4438 printk(KERN_ERR "cciss: Unable to remove device \n");
4439 return;
4442 tmp_ptr = pci_get_drvdata(pdev);
4443 i = tmp_ptr->ctlr;
4444 if (hba[i] == NULL) {
4445 printk(KERN_ERR "cciss: device appears to "
4446 "already be removed \n");
4447 return;
4450 mutex_lock(&hba[i]->busy_shutting_down);
4452 remove_from_scan_list(hba[i]);
4453 remove_proc_entry(hba[i]->devname, proc_cciss);
4454 unregister_blkdev(hba[i]->major, hba[i]->devname);
4456 /* remove it from the disk list */
4457 for (j = 0; j < CISS_MAX_LUN; j++) {
4458 struct gendisk *disk = hba[i]->gendisk[j];
4459 if (disk) {
4460 struct request_queue *q = disk->queue;
4462 if (disk->flags & GENHD_FL_UP) {
4463 cciss_destroy_ld_sysfs_entry(hba[i], j, 1);
4464 del_gendisk(disk);
4466 if (q)
4467 blk_cleanup_queue(q);
4471 #ifdef CONFIG_CISS_SCSI_TAPE
4472 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
4473 #endif
4475 cciss_shutdown(pdev);
4477 #ifdef CONFIG_PCI_MSI
4478 if (hba[i]->msix_vector)
4479 pci_disable_msix(hba[i]->pdev);
4480 else if (hba[i]->msi_vector)
4481 pci_disable_msi(hba[i]->pdev);
4482 #endif /* CONFIG_PCI_MSI */
4484 iounmap(hba[i]->vaddr);
4486 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(CommandList_struct),
4487 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
4488 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
4489 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
4490 kfree(hba[i]->cmd_pool_bits);
4492 * Deliberately omit pci_disable_device(): it does something nasty to
4493 * Smart Array controllers that pci_enable_device does not undo
4495 pci_release_regions(pdev);
4496 pci_set_drvdata(pdev, NULL);
4497 cciss_destroy_hba_sysfs_entry(hba[i]);
4498 mutex_unlock(&hba[i]->busy_shutting_down);
4499 free_hba(i);
4502 static struct pci_driver cciss_pci_driver = {
4503 .name = "cciss",
4504 .probe = cciss_init_one,
4505 .remove = __devexit_p(cciss_remove_one),
4506 .id_table = cciss_pci_device_id, /* id_table */
4507 .shutdown = cciss_shutdown,
4511 * This is it. Register the PCI driver information for the cards we control
4512 * the OS will call our registered routines when it finds one of our cards.
4514 static int __init cciss_init(void)
4516 int err;
4519 * The hardware requires that commands are aligned on a 64-bit
4520 * boundary. Given that we use pci_alloc_consistent() to allocate an
4521 * array of them, the size must be a multiple of 8 bytes.
4523 BUILD_BUG_ON(sizeof(CommandList_struct) % 8);
4525 printk(KERN_INFO DRIVER_NAME "\n");
4527 err = bus_register(&cciss_bus_type);
4528 if (err)
4529 return err;
4531 /* Start the scan thread */
4532 cciss_scan_thread = kthread_run(scan_thread, NULL, "cciss_scan");
4533 if (IS_ERR(cciss_scan_thread)) {
4534 err = PTR_ERR(cciss_scan_thread);
4535 goto err_bus_unregister;
4538 /* Register for our PCI devices */
4539 err = pci_register_driver(&cciss_pci_driver);
4540 if (err)
4541 goto err_thread_stop;
4543 return err;
4545 err_thread_stop:
4546 kthread_stop(cciss_scan_thread);
4547 err_bus_unregister:
4548 bus_unregister(&cciss_bus_type);
4550 return err;
4553 static void __exit cciss_cleanup(void)
4555 int i;
4557 pci_unregister_driver(&cciss_pci_driver);
4558 /* double check that all controller entrys have been removed */
4559 for (i = 0; i < MAX_CTLR; i++) {
4560 if (hba[i] != NULL) {
4561 printk(KERN_WARNING "cciss: had to remove"
4562 " controller %d\n", i);
4563 cciss_remove_one(hba[i]->pdev);
4566 kthread_stop(cciss_scan_thread);
4567 remove_proc_entry("driver/cciss", NULL);
4568 bus_unregister(&cciss_bus_type);
4571 static void fail_all_cmds(unsigned long ctlr)
4573 /* If we get here, the board is apparently dead. */
4574 ctlr_info_t *h = hba[ctlr];
4575 CommandList_struct *c;
4576 unsigned long flags;
4578 printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
4579 h->alive = 0; /* the controller apparently died... */
4581 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
4583 pci_disable_device(h->pdev); /* Make sure it is really dead. */
4585 /* move everything off the request queue onto the completed queue */
4586 while (!hlist_empty(&h->reqQ)) {
4587 c = hlist_entry(h->reqQ.first, CommandList_struct, list);
4588 removeQ(c);
4589 h->Qdepth--;
4590 addQ(&h->cmpQ, c);
4593 /* Now, fail everything on the completed queue with a HW error */
4594 while (!hlist_empty(&h->cmpQ)) {
4595 c = hlist_entry(h->cmpQ.first, CommandList_struct, list);
4596 removeQ(c);
4597 if (c->cmd_type != CMD_MSG_STALE)
4598 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
4599 if (c->cmd_type == CMD_RWREQ) {
4600 complete_command(h, c, 0);
4601 } else if (c->cmd_type == CMD_IOCTL_PEND)
4602 complete(c->waiting);
4603 #ifdef CONFIG_CISS_SCSI_TAPE
4604 else if (c->cmd_type == CMD_SCSI)
4605 complete_scsi_command(c, 0, 0);
4606 #endif
4608 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
4609 return;
4612 module_init(cciss_init);
4613 module_exit(cciss_cleanup);