Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / block / cciss.c
blob21d819161aac8c19463749316edbf93d57d921fa
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/delay.h>
30 #include <linux/major.h>
31 #include <linux/fs.h>
32 #include <linux/bio.h>
33 #include <linux/blkpg.h>
34 #include <linux/timer.h>
35 #include <linux/proc_fs.h>
36 <<<<<<< HEAD:drivers/block/cciss.c
37 =======
38 #include <linux/seq_file.h>
39 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
40 #include <linux/init.h>
41 #include <linux/hdreg.h>
42 #include <linux/spinlock.h>
43 #include <linux/compat.h>
44 #include <linux/blktrace_api.h>
45 #include <asm/uaccess.h>
46 #include <asm/io.h>
48 #include <linux/dma-mapping.h>
49 #include <linux/blkdev.h>
50 #include <linux/genhd.h>
51 #include <linux/completion.h>
52 #include <scsi/scsi.h>
53 #include <scsi/sg.h>
54 #include <scsi/scsi_ioctl.h>
55 #include <linux/cdrom.h>
57 #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
58 #define DRIVER_NAME "HP CISS Driver (v 3.6.14)"
59 #define DRIVER_VERSION CCISS_DRIVER_VERSION(3,6,14)
61 /* Embedded module documentation macros - see modules.h */
62 MODULE_AUTHOR("Hewlett-Packard Company");
63 MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 3.6.14");
64 MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
65 " SA6i P600 P800 P400 P400i E200 E200i E500");
66 MODULE_VERSION("3.6.14");
67 MODULE_LICENSE("GPL");
69 #include "cciss_cmd.h"
70 #include "cciss.h"
71 #include <linux/cciss_ioctl.h>
73 /* define the PCI info for the cards we can control */
74 static const struct pci_device_id cciss_pci_device_id[] = {
75 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS, 0x0E11, 0x4070},
76 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4080},
77 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4082},
78 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4083},
79 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x4091},
80 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409A},
81 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409B},
82 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409C},
83 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409D},
84 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA, 0x103C, 0x3225},
85 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3223},
86 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3234},
87 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3235},
88 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3211},
89 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3212},
90 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3213},
91 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3214},
92 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3215},
93 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3237},
94 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x323D},
95 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
96 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
97 {0,}
100 MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
102 /* board_id = Subsystem Device ID & Vendor ID
103 * product = Marketing Name for the board
104 * access = Address of the struct of function pointers
105 * nr_cmds = Number of commands supported by controller
107 static struct board_type products[] = {
108 {0x40700E11, "Smart Array 5300", &SA5_access, 512},
109 {0x40800E11, "Smart Array 5i", &SA5B_access, 512},
110 {0x40820E11, "Smart Array 532", &SA5B_access, 512},
111 {0x40830E11, "Smart Array 5312", &SA5B_access, 512},
112 {0x409A0E11, "Smart Array 641", &SA5_access, 512},
113 {0x409B0E11, "Smart Array 642", &SA5_access, 512},
114 {0x409C0E11, "Smart Array 6400", &SA5_access, 512},
115 {0x409D0E11, "Smart Array 6400 EM", &SA5_access, 512},
116 {0x40910E11, "Smart Array 6i", &SA5_access, 512},
117 {0x3225103C, "Smart Array P600", &SA5_access, 512},
118 {0x3223103C, "Smart Array P800", &SA5_access, 512},
119 {0x3234103C, "Smart Array P400", &SA5_access, 512},
120 {0x3235103C, "Smart Array P400i", &SA5_access, 512},
121 {0x3211103C, "Smart Array E200i", &SA5_access, 120},
122 {0x3212103C, "Smart Array E200", &SA5_access, 120},
123 {0x3213103C, "Smart Array E200i", &SA5_access, 120},
124 {0x3214103C, "Smart Array E200i", &SA5_access, 120},
125 {0x3215103C, "Smart Array E200i", &SA5_access, 120},
126 {0x3237103C, "Smart Array E500", &SA5_access, 512},
127 {0x323D103C, "Smart Array P700m", &SA5_access, 512},
128 {0xFFFF103C, "Unknown Smart Array", &SA5_access, 120},
131 /* How long to wait (in milliseconds) for board to go into simple mode */
132 #define MAX_CONFIG_WAIT 30000
133 #define MAX_IOCTL_CONFIG_WAIT 1000
135 /*define how many times we will try a command because of bus resets */
136 #define MAX_CMD_RETRIES 3
138 <<<<<<< HEAD:drivers/block/cciss.c
139 #define READ_AHEAD 1024
140 =======
141 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
142 #define MAX_CTLR 32
144 /* Originally cciss driver only supports 8 major numbers */
145 #define MAX_CTLR_ORIG 8
147 static ctlr_info_t *hba[MAX_CTLR];
149 static void do_cciss_request(struct request_queue *q);
150 static irqreturn_t do_cciss_intr(int irq, void *dev_id);
151 static int cciss_open(struct inode *inode, struct file *filep);
152 static int cciss_release(struct inode *inode, struct file *filep);
153 static int cciss_ioctl(struct inode *inode, struct file *filep,
154 unsigned int cmd, unsigned long arg);
155 static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo);
157 static int cciss_revalidate(struct gendisk *disk);
158 static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk);
159 static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
160 int clear_all);
162 static void cciss_read_capacity(int ctlr, int logvol, int withirq,
163 sector_t *total_size, unsigned int *block_size);
164 static void cciss_read_capacity_16(int ctlr, int logvol, int withirq,
165 sector_t *total_size, unsigned int *block_size);
166 static void cciss_geometry_inquiry(int ctlr, int logvol,
167 int withirq, sector_t total_size,
168 unsigned int block_size, InquiryData_struct *inq_buff,
169 drive_info_struct *drv);
170 static void cciss_getgeometry(int cntl_num);
171 static void __devinit cciss_interrupt_mode(ctlr_info_t *, struct pci_dev *,
172 __u32);
173 static void start_io(ctlr_info_t *h);
174 static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size,
175 unsigned int use_unit_num, unsigned int log_unit,
176 __u8 page_code, unsigned char *scsi3addr, int cmd_type);
177 static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
178 unsigned int use_unit_num, unsigned int log_unit,
179 __u8 page_code, int cmd_type);
181 static void fail_all_cmds(unsigned long ctlr);
183 #ifdef CONFIG_PROC_FS
184 <<<<<<< HEAD:drivers/block/cciss.c
185 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
186 int length, int *eof, void *data);
187 =======
188 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
189 static void cciss_procinit(int i);
190 #else
191 static void cciss_procinit(int i)
194 #endif /* CONFIG_PROC_FS */
196 #ifdef CONFIG_COMPAT
197 static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg);
198 #endif
200 static struct block_device_operations cciss_fops = {
201 .owner = THIS_MODULE,
202 .open = cciss_open,
203 .release = cciss_release,
204 .ioctl = cciss_ioctl,
205 .getgeo = cciss_getgeo,
206 #ifdef CONFIG_COMPAT
207 .compat_ioctl = cciss_compat_ioctl,
208 #endif
209 .revalidate_disk = cciss_revalidate,
213 * Enqueuing and dequeuing functions for cmdlists.
215 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
217 if (*Qptr == NULL) {
218 *Qptr = c;
219 c->next = c->prev = c;
220 } else {
221 c->prev = (*Qptr)->prev;
222 c->next = (*Qptr);
223 (*Qptr)->prev->next = c;
224 (*Qptr)->prev = c;
228 static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
229 CommandList_struct *c)
231 if (c && c->next != c) {
232 if (*Qptr == c)
233 *Qptr = c->next;
234 c->prev->next = c->next;
235 c->next->prev = c->prev;
236 } else {
237 *Qptr = NULL;
239 return c;
242 #include "cciss_scsi.c" /* For SCSI tape support */
244 #define RAID_UNKNOWN 6
246 #ifdef CONFIG_PROC_FS
249 * Report information about this controller.
251 #define ENG_GIG 1000000000
252 #define ENG_GIG_FACTOR (ENG_GIG/512)
253 <<<<<<< HEAD:drivers/block/cciss.c
254 =======
255 #define ENGAGE_SCSI "engage scsi"
256 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
257 static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
258 "UNKNOWN"
261 static struct proc_dir_entry *proc_cciss;
263 <<<<<<< HEAD:drivers/block/cciss.c
264 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
265 int length, int *eof, void *data)
266 =======
267 static void cciss_seq_show_header(struct seq_file *seq)
268 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
270 <<<<<<< HEAD:drivers/block/cciss.c
271 off_t pos = 0;
272 off_t len = 0;
273 int size, i, ctlr;
274 ctlr_info_t *h = (ctlr_info_t *) data;
275 drive_info_struct *drv;
276 unsigned long flags;
277 sector_t vol_sz, vol_sz_frac;
278 =======
279 ctlr_info_t *h = seq->private;
281 seq_printf(seq, "%s: HP %s Controller\n"
282 "Board ID: 0x%08lx\n"
283 "Firmware Version: %c%c%c%c\n"
284 "IRQ: %d\n"
285 "Logical drives: %d\n"
286 "Current Q depth: %d\n"
287 "Current # commands on controller: %d\n"
288 "Max Q depth since init: %d\n"
289 "Max # commands on controller since init: %d\n"
290 "Max SG entries since init: %d\n",
291 h->devname,
292 h->product_name,
293 (unsigned long)h->board_id,
294 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
295 h->firm_ver[3], (unsigned int)h->intr[SIMPLE_MODE_INT],
296 h->num_luns,
297 h->Qdepth, h->commands_outstanding,
298 h->maxQsinceinit, h->max_outstanding, h->maxSG);
299 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
301 <<<<<<< HEAD:drivers/block/cciss.c
302 ctlr = h->ctlr;
303 =======
304 #ifdef CONFIG_CISS_SCSI_TAPE
305 cciss_seq_tape_report(seq, h->ctlr);
306 #endif /* CONFIG_CISS_SCSI_TAPE */
309 static void *cciss_seq_start(struct seq_file *seq, loff_t *pos)
311 ctlr_info_t *h = seq->private;
312 unsigned ctlr = h->ctlr;
313 unsigned long flags;
314 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
316 /* prevent displaying bogus info during configuration
317 * or deconfiguration of a logical volume
319 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
320 if (h->busy_configuring) {
321 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
322 <<<<<<< HEAD:drivers/block/cciss.c
323 return -EBUSY;
324 =======
325 return ERR_PTR(-EBUSY);
326 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
328 h->busy_configuring = 1;
329 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
331 <<<<<<< HEAD:drivers/block/cciss.c
332 size = sprintf(buffer, "%s: HP %s Controller\n"
333 "Board ID: 0x%08lx\n"
334 "Firmware Version: %c%c%c%c\n"
335 "IRQ: %d\n"
336 "Logical drives: %d\n"
337 "Max sectors: %d\n"
338 "Current Q depth: %d\n"
339 "Current # commands on controller: %d\n"
340 "Max Q depth since init: %d\n"
341 "Max # commands on controller since init: %d\n"
342 "Max SG entries since init: %d\n\n",
343 h->devname,
344 h->product_name,
345 (unsigned long)h->board_id,
346 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
347 h->firm_ver[3], (unsigned int)h->intr[SIMPLE_MODE_INT],
348 h->num_luns,
349 h->cciss_max_sectors,
350 h->Qdepth, h->commands_outstanding,
351 h->maxQsinceinit, h->max_outstanding, h->maxSG);
353 pos += size;
354 len += size;
355 cciss_proc_tape_report(ctlr, buffer, &pos, &len);
356 for (i = 0; i <= h->highest_lun; i++) {
358 drv = &h->drv[i];
359 if (drv->heads == 0)
360 continue;
361 =======
362 if (*pos == 0)
363 cciss_seq_show_header(seq);
364 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
366 <<<<<<< HEAD:drivers/block/cciss.c
367 vol_sz = drv->nr_blocks;
368 vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
369 vol_sz_frac *= 100;
370 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
371 =======
372 return pos;
375 static int cciss_seq_show(struct seq_file *seq, void *v)
377 sector_t vol_sz, vol_sz_frac;
378 ctlr_info_t *h = seq->private;
379 unsigned ctlr = h->ctlr;
380 loff_t *pos = v;
381 drive_info_struct *drv = &h->drv[*pos];
383 if (*pos > h->highest_lun)
384 return 0;
386 if (drv->heads == 0)
387 return 0;
389 vol_sz = drv->nr_blocks;
390 vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
391 vol_sz_frac *= 100;
392 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
394 if (drv->raid_level > 5)
395 drv->raid_level = RAID_UNKNOWN;
396 seq_printf(seq, "cciss/c%dd%d:"
397 "\t%4u.%02uGB\tRAID %s\n",
398 ctlr, (int) *pos, (int)vol_sz, (int)vol_sz_frac,
399 raid_label[drv->raid_level]);
400 return 0;
403 static void *cciss_seq_next(struct seq_file *seq, void *v, loff_t *pos)
405 ctlr_info_t *h = seq->private;
407 if (*pos > h->highest_lun)
408 return NULL;
409 *pos += 1;
411 return pos;
414 static void cciss_seq_stop(struct seq_file *seq, void *v)
416 ctlr_info_t *h = seq->private;
418 /* Only reset h->busy_configuring if we succeeded in setting
419 * it during cciss_seq_start. */
420 if (v == ERR_PTR(-EBUSY))
421 return;
422 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
424 <<<<<<< HEAD:drivers/block/cciss.c
425 if (drv->raid_level > 5)
426 drv->raid_level = RAID_UNKNOWN;
427 size = sprintf(buffer + len, "cciss/c%dd%d:"
428 "\t%4u.%02uGB\tRAID %s\n",
429 ctlr, i, (int)vol_sz, (int)vol_sz_frac,
430 raid_label[drv->raid_level]);
431 pos += size;
432 len += size;
435 *eof = 1;
436 *start = buffer + offset;
437 len -= offset;
438 if (len > length)
439 len = length;
440 =======
441 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
442 h->busy_configuring = 0;
443 <<<<<<< HEAD:drivers/block/cciss.c
444 return len;
445 =======
446 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
449 <<<<<<< HEAD:drivers/block/cciss.c
450 static int
451 cciss_proc_write(struct file *file, const char __user *buffer,
452 unsigned long count, void *data)
453 =======
454 static struct seq_operations cciss_seq_ops = {
455 .start = cciss_seq_start,
456 .show = cciss_seq_show,
457 .next = cciss_seq_next,
458 .stop = cciss_seq_stop,
461 static int cciss_seq_open(struct inode *inode, struct file *file)
462 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
464 <<<<<<< HEAD:drivers/block/cciss.c
465 unsigned char cmd[80];
466 int len;
467 #ifdef CONFIG_CISS_SCSI_TAPE
468 ctlr_info_t *h = (ctlr_info_t *) data;
469 int rc;
470 =======
471 int ret = seq_open(file, &cciss_seq_ops);
472 struct seq_file *seq = file->private_data;
474 if (!ret)
475 seq->private = PDE(inode)->data;
477 return ret;
480 static ssize_t
481 cciss_proc_write(struct file *file, const char __user *buf,
482 size_t length, loff_t *ppos)
484 int err;
485 char *buffer;
487 #ifndef CONFIG_CISS_SCSI_TAPE
488 return -EINVAL;
489 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
490 #endif
492 <<<<<<< HEAD:drivers/block/cciss.c
493 if (count > sizeof(cmd) - 1)
494 =======
495 if (!buf || length > PAGE_SIZE - 1)
496 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
497 return -EINVAL;
498 <<<<<<< HEAD:drivers/block/cciss.c
499 if (copy_from_user(cmd, buffer, count))
500 return -EFAULT;
501 cmd[count] = '\0';
502 len = strlen(cmd); // above 3 lines ensure safety
503 if (len && cmd[len - 1] == '\n')
504 cmd[--len] = '\0';
505 # ifdef CONFIG_CISS_SCSI_TAPE
506 if (strcmp("engage scsi", cmd) == 0) {
507 =======
509 buffer = (char *)__get_free_page(GFP_KERNEL);
510 if (!buffer)
511 return -ENOMEM;
513 err = -EFAULT;
514 if (copy_from_user(buffer, buf, length))
515 goto out;
516 buffer[length] = '\0';
518 #ifdef CONFIG_CISS_SCSI_TAPE
519 if (strncmp(ENGAGE_SCSI, buffer, sizeof ENGAGE_SCSI - 1) == 0) {
520 struct seq_file *seq = file->private_data;
521 ctlr_info_t *h = seq->private;
522 int rc;
524 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
525 rc = cciss_engage_scsi(h->ctlr);
526 if (rc != 0)
527 <<<<<<< HEAD:drivers/block/cciss.c
528 return -rc;
529 return count;
531 =======
532 err = -rc;
533 else
534 err = length;
535 } else
536 #endif /* CONFIG_CISS_SCSI_TAPE */
537 err = -EINVAL;
538 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
539 /* might be nice to have "disengage" too, but it's not
540 safely possible. (only 1 module use count, lock issues.) */
541 <<<<<<< HEAD:drivers/block/cciss.c
542 # endif
543 return -EINVAL;
544 =======
546 out:
547 free_page((unsigned long)buffer);
548 return err;
549 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
552 <<<<<<< HEAD:drivers/block/cciss.c
554 * Get us a file in /proc/cciss that says something about each controller.
555 * Create /proc/cciss if it doesn't exist yet.
557 =======
558 static struct file_operations cciss_proc_fops = {
559 .owner = THIS_MODULE,
560 .open = cciss_seq_open,
561 .read = seq_read,
562 .llseek = seq_lseek,
563 .release = seq_release,
564 .write = cciss_proc_write,
567 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
568 static void __devinit cciss_procinit(int i)
570 struct proc_dir_entry *pde;
572 <<<<<<< HEAD:drivers/block/cciss.c
573 if (proc_cciss == NULL) {
574 =======
575 if (proc_cciss == NULL)
576 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
577 proc_cciss = proc_mkdir("cciss", proc_root_driver);
578 <<<<<<< HEAD:drivers/block/cciss.c
579 if (!proc_cciss)
580 return;
582 =======
583 if (!proc_cciss)
584 return;
585 pde = proc_create(hba[i]->devname, S_IWUSR | S_IRUSR | S_IRGRP |
586 S_IROTH, proc_cciss,
587 &cciss_proc_fops);
588 if (!pde)
589 return;
590 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
592 <<<<<<< HEAD:drivers/block/cciss.c
593 pde = create_proc_read_entry(hba[i]->devname,
594 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
595 proc_cciss, cciss_proc_get_info, hba[i]);
596 pde->write_proc = cciss_proc_write;
597 =======
598 pde->data = hba[i];
599 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
601 #endif /* CONFIG_PROC_FS */
604 * For operations that cannot sleep, a command block is allocated at init,
605 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
606 * which ones are free or in use. For operations that can wait for kmalloc
607 * to possible sleep, this routine can be called with get_from_pool set to 0.
608 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
610 static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool)
612 CommandList_struct *c;
613 int i;
614 u64bit temp64;
615 dma_addr_t cmd_dma_handle, err_dma_handle;
617 if (!get_from_pool) {
618 c = (CommandList_struct *) pci_alloc_consistent(h->pdev,
619 sizeof(CommandList_struct), &cmd_dma_handle);
620 if (c == NULL)
621 return NULL;
622 memset(c, 0, sizeof(CommandList_struct));
624 c->cmdindex = -1;
626 c->err_info = (ErrorInfo_struct *)
627 pci_alloc_consistent(h->pdev, sizeof(ErrorInfo_struct),
628 &err_dma_handle);
630 if (c->err_info == NULL) {
631 pci_free_consistent(h->pdev,
632 sizeof(CommandList_struct), c, cmd_dma_handle);
633 return NULL;
635 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
636 } else { /* get it out of the controllers pool */
638 do {
639 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
640 if (i == h->nr_cmds)
641 return NULL;
642 } while (test_and_set_bit
643 (i & (BITS_PER_LONG - 1),
644 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
645 #ifdef CCISS_DEBUG
646 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
647 #endif
648 c = h->cmd_pool + i;
649 memset(c, 0, sizeof(CommandList_struct));
650 cmd_dma_handle = h->cmd_pool_dhandle
651 + i * sizeof(CommandList_struct);
652 c->err_info = h->errinfo_pool + i;
653 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
654 err_dma_handle = h->errinfo_pool_dhandle
655 + i * sizeof(ErrorInfo_struct);
656 h->nr_allocs++;
658 c->cmdindex = i;
661 c->busaddr = (__u32) cmd_dma_handle;
662 temp64.val = (__u64) err_dma_handle;
663 c->ErrDesc.Addr.lower = temp64.val32.lower;
664 c->ErrDesc.Addr.upper = temp64.val32.upper;
665 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
667 c->ctlr = h->ctlr;
668 return c;
672 * Frees a command block that was previously allocated with cmd_alloc().
674 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
676 int i;
677 u64bit temp64;
679 if (!got_from_pool) {
680 temp64.val32.lower = c->ErrDesc.Addr.lower;
681 temp64.val32.upper = c->ErrDesc.Addr.upper;
682 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
683 c->err_info, (dma_addr_t) temp64.val);
684 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
685 c, (dma_addr_t) c->busaddr);
686 } else {
687 i = c - h->cmd_pool;
688 clear_bit(i & (BITS_PER_LONG - 1),
689 h->cmd_pool_bits + (i / BITS_PER_LONG));
690 h->nr_frees++;
694 static inline ctlr_info_t *get_host(struct gendisk *disk)
696 return disk->queue->queuedata;
699 static inline drive_info_struct *get_drv(struct gendisk *disk)
701 return disk->private_data;
705 * Open. Make sure the device is really there.
707 static int cciss_open(struct inode *inode, struct file *filep)
709 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
710 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
712 #ifdef CCISS_DEBUG
713 printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
714 #endif /* CCISS_DEBUG */
716 if (host->busy_initializing || drv->busy_configuring)
717 return -EBUSY;
719 * Root is allowed to open raw volume zero even if it's not configured
720 * so array config can still work. Root is also allowed to open any
721 * volume that has a LUN ID, so it can issue IOCTL to reread the
722 * disk information. I don't think I really like this
723 * but I'm already using way to many device nodes to claim another one
724 * for "raw controller".
726 if (drv->heads == 0) {
727 if (iminor(inode) != 0) { /* not node 0? */
728 /* if not node 0 make sure it is a partition = 0 */
729 if (iminor(inode) & 0x0f) {
730 return -ENXIO;
731 /* if it is, make sure we have a LUN ID */
732 } else if (drv->LunID == 0) {
733 return -ENXIO;
736 if (!capable(CAP_SYS_ADMIN))
737 return -EPERM;
739 drv->usage_count++;
740 host->usage_count++;
741 return 0;
745 * Close. Sync first.
747 static int cciss_release(struct inode *inode, struct file *filep)
749 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
750 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
752 #ifdef CCISS_DEBUG
753 printk(KERN_DEBUG "cciss_release %s\n",
754 inode->i_bdev->bd_disk->disk_name);
755 #endif /* CCISS_DEBUG */
757 drv->usage_count--;
758 host->usage_count--;
759 return 0;
762 #ifdef CONFIG_COMPAT
764 static int do_ioctl(struct file *f, unsigned cmd, unsigned long arg)
766 int ret;
767 lock_kernel();
768 ret = cciss_ioctl(f->f_path.dentry->d_inode, f, cmd, arg);
769 unlock_kernel();
770 return ret;
773 static int cciss_ioctl32_passthru(struct file *f, unsigned cmd,
774 unsigned long arg);
775 static int cciss_ioctl32_big_passthru(struct file *f, unsigned cmd,
776 unsigned long arg);
778 static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg)
780 switch (cmd) {
781 case CCISS_GETPCIINFO:
782 case CCISS_GETINTINFO:
783 case CCISS_SETINTINFO:
784 case CCISS_GETNODENAME:
785 case CCISS_SETNODENAME:
786 case CCISS_GETHEARTBEAT:
787 case CCISS_GETBUSTYPES:
788 case CCISS_GETFIRMVER:
789 case CCISS_GETDRIVVER:
790 case CCISS_REVALIDVOLS:
791 case CCISS_DEREGDISK:
792 case CCISS_REGNEWDISK:
793 case CCISS_REGNEWD:
794 case CCISS_RESCANDISK:
795 case CCISS_GETLUNINFO:
796 return do_ioctl(f, cmd, arg);
798 case CCISS_PASSTHRU32:
799 return cciss_ioctl32_passthru(f, cmd, arg);
800 case CCISS_BIG_PASSTHRU32:
801 return cciss_ioctl32_big_passthru(f, cmd, arg);
803 default:
804 return -ENOIOCTLCMD;
808 static int cciss_ioctl32_passthru(struct file *f, unsigned cmd,
809 unsigned long arg)
811 IOCTL32_Command_struct __user *arg32 =
812 (IOCTL32_Command_struct __user *) arg;
813 IOCTL_Command_struct arg64;
814 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
815 int err;
816 u32 cp;
818 err = 0;
819 err |=
820 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
821 sizeof(arg64.LUN_info));
822 err |=
823 copy_from_user(&arg64.Request, &arg32->Request,
824 sizeof(arg64.Request));
825 err |=
826 copy_from_user(&arg64.error_info, &arg32->error_info,
827 sizeof(arg64.error_info));
828 err |= get_user(arg64.buf_size, &arg32->buf_size);
829 err |= get_user(cp, &arg32->buf);
830 arg64.buf = compat_ptr(cp);
831 err |= copy_to_user(p, &arg64, sizeof(arg64));
833 if (err)
834 return -EFAULT;
836 err = do_ioctl(f, CCISS_PASSTHRU, (unsigned long)p);
837 if (err)
838 return err;
839 err |=
840 copy_in_user(&arg32->error_info, &p->error_info,
841 sizeof(arg32->error_info));
842 if (err)
843 return -EFAULT;
844 return err;
847 static int cciss_ioctl32_big_passthru(struct file *file, unsigned cmd,
848 unsigned long arg)
850 BIG_IOCTL32_Command_struct __user *arg32 =
851 (BIG_IOCTL32_Command_struct __user *) arg;
852 BIG_IOCTL_Command_struct arg64;
853 BIG_IOCTL_Command_struct __user *p =
854 compat_alloc_user_space(sizeof(arg64));
855 int err;
856 u32 cp;
858 err = 0;
859 err |=
860 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
861 sizeof(arg64.LUN_info));
862 err |=
863 copy_from_user(&arg64.Request, &arg32->Request,
864 sizeof(arg64.Request));
865 err |=
866 copy_from_user(&arg64.error_info, &arg32->error_info,
867 sizeof(arg64.error_info));
868 err |= get_user(arg64.buf_size, &arg32->buf_size);
869 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
870 err |= get_user(cp, &arg32->buf);
871 arg64.buf = compat_ptr(cp);
872 err |= copy_to_user(p, &arg64, sizeof(arg64));
874 if (err)
875 return -EFAULT;
877 err = do_ioctl(file, CCISS_BIG_PASSTHRU, (unsigned long)p);
878 if (err)
879 return err;
880 err |=
881 copy_in_user(&arg32->error_info, &p->error_info,
882 sizeof(arg32->error_info));
883 if (err)
884 return -EFAULT;
885 return err;
887 #endif
889 static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo)
891 drive_info_struct *drv = get_drv(bdev->bd_disk);
893 if (!drv->cylinders)
894 return -ENXIO;
896 geo->heads = drv->heads;
897 geo->sectors = drv->sectors;
898 geo->cylinders = drv->cylinders;
899 return 0;
903 * ioctl
905 static int cciss_ioctl(struct inode *inode, struct file *filep,
906 unsigned int cmd, unsigned long arg)
908 struct block_device *bdev = inode->i_bdev;
909 struct gendisk *disk = bdev->bd_disk;
910 ctlr_info_t *host = get_host(disk);
911 drive_info_struct *drv = get_drv(disk);
912 int ctlr = host->ctlr;
913 void __user *argp = (void __user *)arg;
915 #ifdef CCISS_DEBUG
916 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
917 #endif /* CCISS_DEBUG */
919 switch (cmd) {
920 case CCISS_GETPCIINFO:
922 cciss_pci_info_struct pciinfo;
924 if (!arg)
925 return -EINVAL;
926 pciinfo.domain = pci_domain_nr(host->pdev->bus);
927 pciinfo.bus = host->pdev->bus->number;
928 pciinfo.dev_fn = host->pdev->devfn;
929 pciinfo.board_id = host->board_id;
930 if (copy_to_user
931 (argp, &pciinfo, sizeof(cciss_pci_info_struct)))
932 return -EFAULT;
933 return 0;
935 case CCISS_GETINTINFO:
937 cciss_coalint_struct intinfo;
938 if (!arg)
939 return -EINVAL;
940 intinfo.delay =
941 readl(&host->cfgtable->HostWrite.CoalIntDelay);
942 intinfo.count =
943 readl(&host->cfgtable->HostWrite.CoalIntCount);
944 if (copy_to_user
945 (argp, &intinfo, sizeof(cciss_coalint_struct)))
946 return -EFAULT;
947 return 0;
949 case CCISS_SETINTINFO:
951 cciss_coalint_struct intinfo;
952 unsigned long flags;
953 int i;
955 if (!arg)
956 return -EINVAL;
957 if (!capable(CAP_SYS_ADMIN))
958 return -EPERM;
959 if (copy_from_user
960 (&intinfo, argp, sizeof(cciss_coalint_struct)))
961 return -EFAULT;
962 if ((intinfo.delay == 0) && (intinfo.count == 0))
964 // printk("cciss_ioctl: delay and count cannot be 0\n");
965 return -EINVAL;
967 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
968 /* Update the field, and then ring the doorbell */
969 writel(intinfo.delay,
970 &(host->cfgtable->HostWrite.CoalIntDelay));
971 writel(intinfo.count,
972 &(host->cfgtable->HostWrite.CoalIntCount));
973 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
975 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
976 if (!(readl(host->vaddr + SA5_DOORBELL)
977 & CFGTBL_ChangeReq))
978 break;
979 /* delay and try again */
980 udelay(1000);
982 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
983 if (i >= MAX_IOCTL_CONFIG_WAIT)
984 return -EAGAIN;
985 return 0;
987 case CCISS_GETNODENAME:
989 NodeName_type NodeName;
990 int i;
992 if (!arg)
993 return -EINVAL;
994 for (i = 0; i < 16; i++)
995 NodeName[i] =
996 readb(&host->cfgtable->ServerName[i]);
997 if (copy_to_user(argp, NodeName, sizeof(NodeName_type)))
998 return -EFAULT;
999 return 0;
1001 case CCISS_SETNODENAME:
1003 NodeName_type NodeName;
1004 unsigned long flags;
1005 int i;
1007 if (!arg)
1008 return -EINVAL;
1009 if (!capable(CAP_SYS_ADMIN))
1010 return -EPERM;
1012 if (copy_from_user
1013 (NodeName, argp, sizeof(NodeName_type)))
1014 return -EFAULT;
1016 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1018 /* Update the field, and then ring the doorbell */
1019 for (i = 0; i < 16; i++)
1020 writeb(NodeName[i],
1021 &host->cfgtable->ServerName[i]);
1023 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
1025 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
1026 if (!(readl(host->vaddr + SA5_DOORBELL)
1027 & CFGTBL_ChangeReq))
1028 break;
1029 /* delay and try again */
1030 udelay(1000);
1032 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1033 if (i >= MAX_IOCTL_CONFIG_WAIT)
1034 return -EAGAIN;
1035 return 0;
1038 case CCISS_GETHEARTBEAT:
1040 Heartbeat_type heartbeat;
1042 if (!arg)
1043 return -EINVAL;
1044 heartbeat = readl(&host->cfgtable->HeartBeat);
1045 if (copy_to_user
1046 (argp, &heartbeat, sizeof(Heartbeat_type)))
1047 return -EFAULT;
1048 return 0;
1050 case CCISS_GETBUSTYPES:
1052 BusTypes_type BusTypes;
1054 if (!arg)
1055 return -EINVAL;
1056 BusTypes = readl(&host->cfgtable->BusTypes);
1057 if (copy_to_user
1058 (argp, &BusTypes, sizeof(BusTypes_type)))
1059 return -EFAULT;
1060 return 0;
1062 case CCISS_GETFIRMVER:
1064 FirmwareVer_type firmware;
1066 if (!arg)
1067 return -EINVAL;
1068 memcpy(firmware, host->firm_ver, 4);
1070 if (copy_to_user
1071 (argp, firmware, sizeof(FirmwareVer_type)))
1072 return -EFAULT;
1073 return 0;
1075 case CCISS_GETDRIVVER:
1077 DriverVer_type DriverVer = DRIVER_VERSION;
1079 if (!arg)
1080 return -EINVAL;
1082 if (copy_to_user
1083 (argp, &DriverVer, sizeof(DriverVer_type)))
1084 return -EFAULT;
1085 return 0;
1088 case CCISS_REVALIDVOLS:
1089 return rebuild_lun_table(host, NULL);
1091 case CCISS_GETLUNINFO:{
1092 LogvolInfo_struct luninfo;
1094 luninfo.LunID = drv->LunID;
1095 luninfo.num_opens = drv->usage_count;
1096 luninfo.num_parts = 0;
1097 if (copy_to_user(argp, &luninfo,
1098 sizeof(LogvolInfo_struct)))
1099 return -EFAULT;
1100 return 0;
1102 case CCISS_DEREGDISK:
1103 return rebuild_lun_table(host, disk);
1105 case CCISS_REGNEWD:
1106 return rebuild_lun_table(host, NULL);
1108 case CCISS_PASSTHRU:
1110 IOCTL_Command_struct iocommand;
1111 CommandList_struct *c;
1112 char *buff = NULL;
1113 u64bit temp64;
1114 unsigned long flags;
1115 DECLARE_COMPLETION_ONSTACK(wait);
1117 if (!arg)
1118 return -EINVAL;
1120 if (!capable(CAP_SYS_RAWIO))
1121 return -EPERM;
1123 if (copy_from_user
1124 (&iocommand, argp, sizeof(IOCTL_Command_struct)))
1125 return -EFAULT;
1126 if ((iocommand.buf_size < 1) &&
1127 (iocommand.Request.Type.Direction != XFER_NONE)) {
1128 return -EINVAL;
1130 #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
1131 /* Check kmalloc limits */
1132 if (iocommand.buf_size > 128000)
1133 return -EINVAL;
1134 #endif
1135 if (iocommand.buf_size > 0) {
1136 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
1137 if (buff == NULL)
1138 return -EFAULT;
1140 if (iocommand.Request.Type.Direction == XFER_WRITE) {
1141 /* Copy the data into the buffer we created */
1142 if (copy_from_user
1143 (buff, iocommand.buf, iocommand.buf_size)) {
1144 kfree(buff);
1145 return -EFAULT;
1147 } else {
1148 memset(buff, 0, iocommand.buf_size);
1150 if ((c = cmd_alloc(host, 0)) == NULL) {
1151 kfree(buff);
1152 return -ENOMEM;
1154 // Fill in the command type
1155 c->cmd_type = CMD_IOCTL_PEND;
1156 // Fill in Command Header
1157 c->Header.ReplyQueue = 0; // unused in simple mode
1158 if (iocommand.buf_size > 0) // buffer to fill
1160 c->Header.SGList = 1;
1161 c->Header.SGTotal = 1;
1162 } else // no buffers to fill
1164 c->Header.SGList = 0;
1165 c->Header.SGTotal = 0;
1167 c->Header.LUN = iocommand.LUN_info;
1168 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
1170 // Fill in Request block
1171 c->Request = iocommand.Request;
1173 // Fill in the scatter gather information
1174 if (iocommand.buf_size > 0) {
1175 temp64.val = pci_map_single(host->pdev, buff,
1176 iocommand.buf_size,
1177 PCI_DMA_BIDIRECTIONAL);
1178 c->SG[0].Addr.lower = temp64.val32.lower;
1179 c->SG[0].Addr.upper = temp64.val32.upper;
1180 c->SG[0].Len = iocommand.buf_size;
1181 c->SG[0].Ext = 0; // we are not chaining
1183 c->waiting = &wait;
1185 /* Put the request on the tail of the request queue */
1186 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1187 addQ(&host->reqQ, c);
1188 host->Qdepth++;
1189 start_io(host);
1190 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1192 wait_for_completion(&wait);
1194 /* unlock the buffers from DMA */
1195 temp64.val32.lower = c->SG[0].Addr.lower;
1196 temp64.val32.upper = c->SG[0].Addr.upper;
1197 pci_unmap_single(host->pdev, (dma_addr_t) temp64.val,
1198 iocommand.buf_size,
1199 PCI_DMA_BIDIRECTIONAL);
1201 /* Copy the error information out */
1202 iocommand.error_info = *(c->err_info);
1203 if (copy_to_user
1204 (argp, &iocommand, sizeof(IOCTL_Command_struct))) {
1205 kfree(buff);
1206 cmd_free(host, c, 0);
1207 return -EFAULT;
1210 if (iocommand.Request.Type.Direction == XFER_READ) {
1211 /* Copy the data out of the buffer we created */
1212 if (copy_to_user
1213 (iocommand.buf, buff, iocommand.buf_size)) {
1214 kfree(buff);
1215 cmd_free(host, c, 0);
1216 return -EFAULT;
1219 kfree(buff);
1220 cmd_free(host, c, 0);
1221 return 0;
1223 case CCISS_BIG_PASSTHRU:{
1224 BIG_IOCTL_Command_struct *ioc;
1225 CommandList_struct *c;
1226 unsigned char **buff = NULL;
1227 int *buff_size = NULL;
1228 u64bit temp64;
1229 unsigned long flags;
1230 BYTE sg_used = 0;
1231 int status = 0;
1232 int i;
1233 DECLARE_COMPLETION_ONSTACK(wait);
1234 __u32 left;
1235 __u32 sz;
1236 BYTE __user *data_ptr;
1238 if (!arg)
1239 return -EINVAL;
1240 if (!capable(CAP_SYS_RAWIO))
1241 return -EPERM;
1242 ioc = (BIG_IOCTL_Command_struct *)
1243 kmalloc(sizeof(*ioc), GFP_KERNEL);
1244 if (!ioc) {
1245 status = -ENOMEM;
1246 goto cleanup1;
1248 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
1249 status = -EFAULT;
1250 goto cleanup1;
1252 if ((ioc->buf_size < 1) &&
1253 (ioc->Request.Type.Direction != XFER_NONE)) {
1254 status = -EINVAL;
1255 goto cleanup1;
1257 /* Check kmalloc limits using all SGs */
1258 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
1259 status = -EINVAL;
1260 goto cleanup1;
1262 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
1263 status = -EINVAL;
1264 goto cleanup1;
1266 buff =
1267 kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
1268 if (!buff) {
1269 status = -ENOMEM;
1270 goto cleanup1;
1272 buff_size = kmalloc(MAXSGENTRIES * sizeof(int),
1273 GFP_KERNEL);
1274 if (!buff_size) {
1275 status = -ENOMEM;
1276 goto cleanup1;
1278 left = ioc->buf_size;
1279 data_ptr = ioc->buf;
1280 while (left) {
1281 sz = (left >
1282 ioc->malloc_size) ? ioc->
1283 malloc_size : left;
1284 buff_size[sg_used] = sz;
1285 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
1286 if (buff[sg_used] == NULL) {
1287 status = -ENOMEM;
1288 goto cleanup1;
1290 if (ioc->Request.Type.Direction == XFER_WRITE) {
1291 if (copy_from_user
1292 (buff[sg_used], data_ptr, sz)) {
1293 status = -ENOMEM;
1294 goto cleanup1;
1296 } else {
1297 memset(buff[sg_used], 0, sz);
1299 left -= sz;
1300 data_ptr += sz;
1301 sg_used++;
1303 if ((c = cmd_alloc(host, 0)) == NULL) {
1304 status = -ENOMEM;
1305 goto cleanup1;
1307 c->cmd_type = CMD_IOCTL_PEND;
1308 c->Header.ReplyQueue = 0;
1310 if (ioc->buf_size > 0) {
1311 c->Header.SGList = sg_used;
1312 c->Header.SGTotal = sg_used;
1313 } else {
1314 c->Header.SGList = 0;
1315 c->Header.SGTotal = 0;
1317 c->Header.LUN = ioc->LUN_info;
1318 c->Header.Tag.lower = c->busaddr;
1320 c->Request = ioc->Request;
1321 if (ioc->buf_size > 0) {
1322 int i;
1323 for (i = 0; i < sg_used; i++) {
1324 temp64.val =
1325 pci_map_single(host->pdev, buff[i],
1326 buff_size[i],
1327 PCI_DMA_BIDIRECTIONAL);
1328 c->SG[i].Addr.lower =
1329 temp64.val32.lower;
1330 c->SG[i].Addr.upper =
1331 temp64.val32.upper;
1332 c->SG[i].Len = buff_size[i];
1333 c->SG[i].Ext = 0; /* we are not chaining */
1336 c->waiting = &wait;
1337 /* Put the request on the tail of the request queue */
1338 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1339 addQ(&host->reqQ, c);
1340 host->Qdepth++;
1341 start_io(host);
1342 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1343 wait_for_completion(&wait);
1344 /* unlock the buffers from DMA */
1345 for (i = 0; i < sg_used; i++) {
1346 temp64.val32.lower = c->SG[i].Addr.lower;
1347 temp64.val32.upper = c->SG[i].Addr.upper;
1348 pci_unmap_single(host->pdev,
1349 (dma_addr_t) temp64.val, buff_size[i],
1350 PCI_DMA_BIDIRECTIONAL);
1352 /* Copy the error information out */
1353 ioc->error_info = *(c->err_info);
1354 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
1355 cmd_free(host, c, 0);
1356 status = -EFAULT;
1357 goto cleanup1;
1359 if (ioc->Request.Type.Direction == XFER_READ) {
1360 /* Copy the data out of the buffer we created */
1361 BYTE __user *ptr = ioc->buf;
1362 for (i = 0; i < sg_used; i++) {
1363 if (copy_to_user
1364 (ptr, buff[i], buff_size[i])) {
1365 cmd_free(host, c, 0);
1366 status = -EFAULT;
1367 goto cleanup1;
1369 ptr += buff_size[i];
1372 cmd_free(host, c, 0);
1373 status = 0;
1374 cleanup1:
1375 if (buff) {
1376 for (i = 0; i < sg_used; i++)
1377 kfree(buff[i]);
1378 kfree(buff);
1380 kfree(buff_size);
1381 kfree(ioc);
1382 return status;
1385 /* scsi_cmd_ioctl handles these, below, though some are not */
1386 /* very meaningful for cciss. SG_IO is the main one people want. */
1388 case SG_GET_VERSION_NUM:
1389 case SG_SET_TIMEOUT:
1390 case SG_GET_TIMEOUT:
1391 case SG_GET_RESERVED_SIZE:
1392 case SG_SET_RESERVED_SIZE:
1393 case SG_EMULATED_HOST:
1394 case SG_IO:
1395 case SCSI_IOCTL_SEND_COMMAND:
1396 return scsi_cmd_ioctl(filep, disk->queue, disk, cmd, argp);
1398 /* scsi_cmd_ioctl would normally handle these, below, but */
1399 /* they aren't a good fit for cciss, as CD-ROMs are */
1400 /* not supported, and we don't have any bus/target/lun */
1401 /* which we present to the kernel. */
1403 case CDROM_SEND_PACKET:
1404 case CDROMCLOSETRAY:
1405 case CDROMEJECT:
1406 case SCSI_IOCTL_GET_IDLUN:
1407 case SCSI_IOCTL_GET_BUS_NUMBER:
1408 default:
1409 return -ENOTTY;
1413 static void cciss_check_queues(ctlr_info_t *h)
1415 int start_queue = h->next_to_run;
1416 int i;
1418 /* check to see if we have maxed out the number of commands that can
1419 * be placed on the queue. If so then exit. We do this check here
1420 * in case the interrupt we serviced was from an ioctl and did not
1421 * free any new commands.
1423 if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds)
1424 return;
1426 /* We have room on the queue for more commands. Now we need to queue
1427 * them up. We will also keep track of the next queue to run so
1428 * that every queue gets a chance to be started first.
1430 for (i = 0; i < h->highest_lun + 1; i++) {
1431 int curr_queue = (start_queue + i) % (h->highest_lun + 1);
1432 /* make sure the disk has been added and the drive is real
1433 * because this can be called from the middle of init_one.
1435 if (!(h->drv[curr_queue].queue) || !(h->drv[curr_queue].heads))
1436 continue;
1437 blk_start_queue(h->gendisk[curr_queue]->queue);
1439 /* check to see if we have maxed out the number of commands
1440 * that can be placed on the queue.
1442 if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds) {
1443 if (curr_queue == start_queue) {
1444 h->next_to_run =
1445 (start_queue + 1) % (h->highest_lun + 1);
1446 break;
1447 } else {
1448 h->next_to_run = curr_queue;
1449 break;
1451 } else {
1452 curr_queue = (curr_queue + 1) % (h->highest_lun + 1);
1457 static void cciss_softirq_done(struct request *rq)
1459 CommandList_struct *cmd = rq->completion_data;
1460 ctlr_info_t *h = hba[cmd->ctlr];
1461 unsigned long flags;
1462 u64bit temp64;
1463 int i, ddir;
1465 if (cmd->Request.Type.Direction == XFER_READ)
1466 ddir = PCI_DMA_FROMDEVICE;
1467 else
1468 ddir = PCI_DMA_TODEVICE;
1470 /* command did not need to be retried */
1471 /* unmap the DMA mapping for all the scatter gather elements */
1472 for (i = 0; i < cmd->Header.SGList; i++) {
1473 temp64.val32.lower = cmd->SG[i].Addr.lower;
1474 temp64.val32.upper = cmd->SG[i].Addr.upper;
1475 pci_unmap_page(h->pdev, temp64.val, cmd->SG[i].Len, ddir);
1478 #ifdef CCISS_DEBUG
1479 printk("Done with %p\n", rq);
1480 #endif /* CCISS_DEBUG */
1482 if (blk_end_request(rq, (rq->errors == 0) ? 0 : -EIO, blk_rq_bytes(rq)))
1483 BUG();
1485 spin_lock_irqsave(&h->lock, flags);
1486 cmd_free(h, cmd, 1);
1487 cciss_check_queues(h);
1488 spin_unlock_irqrestore(&h->lock, flags);
1491 /* This function will check the usage_count of the drive to be updated/added.
1492 * If the usage_count is zero then the drive information will be updated and
1493 * the disk will be re-registered with the kernel. If not then it will be
1494 * left alone for the next reboot. The exception to this is disk 0 which
1495 * will always be left registered with the kernel since it is also the
1496 * controller node. Any changes to disk 0 will show up on the next
1497 * reboot.
1499 static void cciss_update_drive_info(int ctlr, int drv_index)
1501 ctlr_info_t *h = hba[ctlr];
1502 struct gendisk *disk;
1503 InquiryData_struct *inq_buff = NULL;
1504 unsigned int block_size;
1505 sector_t total_size;
1506 unsigned long flags = 0;
1507 int ret = 0;
1509 /* if the disk already exists then deregister it before proceeding */
1510 if (h->drv[drv_index].raid_level != -1) {
1511 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
1512 h->drv[drv_index].busy_configuring = 1;
1513 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1514 ret = deregister_disk(h->gendisk[drv_index],
1515 &h->drv[drv_index], 0);
1516 h->drv[drv_index].busy_configuring = 0;
1519 /* If the disk is in use return */
1520 if (ret)
1521 return;
1523 /* Get information about the disk and modify the driver structure */
1524 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
1525 if (inq_buff == NULL)
1526 goto mem_msg;
1528 /* testing to see if 16-byte CDBs are already being used */
1529 if (h->cciss_read == CCISS_READ_16) {
1530 cciss_read_capacity_16(h->ctlr, drv_index, 1,
1531 &total_size, &block_size);
1532 goto geo_inq;
1535 cciss_read_capacity(ctlr, drv_index, 1,
1536 &total_size, &block_size);
1538 /* if read_capacity returns all F's this volume is >2TB in size */
1539 /* so we switch to 16-byte CDB's for all read/write ops */
1540 if (total_size == 0xFFFFFFFFULL) {
1541 cciss_read_capacity_16(ctlr, drv_index, 1,
1542 &total_size, &block_size);
1543 h->cciss_read = CCISS_READ_16;
1544 h->cciss_write = CCISS_WRITE_16;
1545 } else {
1546 h->cciss_read = CCISS_READ_10;
1547 h->cciss_write = CCISS_WRITE_10;
1549 geo_inq:
1550 cciss_geometry_inquiry(ctlr, drv_index, 1, total_size, block_size,
1551 inq_buff, &h->drv[drv_index]);
1553 ++h->num_luns;
1554 disk = h->gendisk[drv_index];
1555 set_capacity(disk, h->drv[drv_index].nr_blocks);
1557 /* if it's the controller it's already added */
1558 if (drv_index) {
1559 disk->queue = blk_init_queue(do_cciss_request, &h->lock);
1560 sprintf(disk->disk_name, "cciss/c%dd%d", ctlr, drv_index);
1561 disk->major = h->major;
1562 disk->first_minor = drv_index << NWD_SHIFT;
1563 disk->fops = &cciss_fops;
1564 disk->private_data = &h->drv[drv_index];
1566 /* Set up queue information */
1567 <<<<<<< HEAD:drivers/block/cciss.c
1568 disk->queue->backing_dev_info.ra_pages = READ_AHEAD;
1569 =======
1570 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
1571 blk_queue_bounce_limit(disk->queue, hba[ctlr]->pdev->dma_mask);
1573 /* This is a hardware imposed limit. */
1574 blk_queue_max_hw_segments(disk->queue, MAXSGENTRIES);
1576 /* This is a limit in the driver and could be eliminated. */
1577 blk_queue_max_phys_segments(disk->queue, MAXSGENTRIES);
1579 blk_queue_max_sectors(disk->queue, h->cciss_max_sectors);
1581 blk_queue_softirq_done(disk->queue, cciss_softirq_done);
1583 disk->queue->queuedata = hba[ctlr];
1585 blk_queue_hardsect_size(disk->queue,
1586 hba[ctlr]->drv[drv_index].block_size);
1588 h->drv[drv_index].queue = disk->queue;
1589 add_disk(disk);
1592 freeret:
1593 kfree(inq_buff);
1594 return;
1595 mem_msg:
1596 printk(KERN_ERR "cciss: out of memory\n");
1597 goto freeret;
1600 /* This function will find the first index of the controllers drive array
1601 * that has a -1 for the raid_level and will return that index. This is
1602 * where new drives will be added. If the index to be returned is greater
1603 * than the highest_lun index for the controller then highest_lun is set
1604 * to this new index. If there are no available indexes then -1 is returned.
1606 static int cciss_find_free_drive_index(int ctlr)
1608 int i;
1610 for (i = 0; i < CISS_MAX_LUN; i++) {
1611 if (hba[ctlr]->drv[i].raid_level == -1) {
1612 if (i > hba[ctlr]->highest_lun)
1613 hba[ctlr]->highest_lun = i;
1614 return i;
1617 return -1;
1620 /* This function will add and remove logical drives from the Logical
1621 * drive array of the controller and maintain persistency of ordering
1622 * so that mount points are preserved until the next reboot. This allows
1623 * for the removal of logical drives in the middle of the drive array
1624 * without a re-ordering of those drives.
1625 * INPUT
1626 * h = The controller to perform the operations on
1627 * del_disk = The disk to remove if specified. If the value given
1628 * is NULL then no disk is removed.
1630 static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk)
1632 int ctlr = h->ctlr;
1633 int num_luns;
1634 ReportLunData_struct *ld_buff = NULL;
1635 drive_info_struct *drv = NULL;
1636 int return_code;
1637 int listlength = 0;
1638 int i;
1639 int drv_found;
1640 int drv_index = 0;
1641 __u32 lunid = 0;
1642 unsigned long flags;
1644 /* Set busy_configuring flag for this operation */
1645 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
1646 if (h->busy_configuring) {
1647 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1648 return -EBUSY;
1650 h->busy_configuring = 1;
1652 /* if del_disk is NULL then we are being called to add a new disk
1653 * and update the logical drive table. If it is not NULL then
1654 * we will check if the disk is in use or not.
1656 if (del_disk != NULL) {
1657 drv = get_drv(del_disk);
1658 drv->busy_configuring = 1;
1659 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1660 return_code = deregister_disk(del_disk, drv, 1);
1661 drv->busy_configuring = 0;
1662 h->busy_configuring = 0;
1663 return return_code;
1664 } else {
1665 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1666 if (!capable(CAP_SYS_RAWIO))
1667 return -EPERM;
1669 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1670 if (ld_buff == NULL)
1671 goto mem_msg;
1673 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
1674 sizeof(ReportLunData_struct), 0,
1675 0, 0, TYPE_CMD);
1677 if (return_code == IO_OK) {
1678 listlength =
1679 be32_to_cpu(*(__be32 *) ld_buff->LUNListLength);
1680 } else { /* reading number of logical volumes failed */
1681 printk(KERN_WARNING "cciss: report logical volume"
1682 " command failed\n");
1683 listlength = 0;
1684 goto freeret;
1687 num_luns = listlength / 8; /* 8 bytes per entry */
1688 if (num_luns > CISS_MAX_LUN) {
1689 num_luns = CISS_MAX_LUN;
1690 printk(KERN_WARNING "cciss: more luns configured"
1691 " on controller than can be handled by"
1692 " this driver.\n");
1695 /* Compare controller drive array to drivers drive array.
1696 * Check for updates in the drive information and any new drives
1697 * on the controller.
1699 for (i = 0; i < num_luns; i++) {
1700 int j;
1702 drv_found = 0;
1704 lunid = (0xff &
1705 (unsigned int)(ld_buff->LUN[i][3])) << 24;
1706 lunid |= (0xff &
1707 (unsigned int)(ld_buff->LUN[i][2])) << 16;
1708 lunid |= (0xff &
1709 (unsigned int)(ld_buff->LUN[i][1])) << 8;
1710 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
1712 /* Find if the LUN is already in the drive array
1713 * of the controller. If so then update its info
1714 * if not is use. If it does not exist then find
1715 * the first free index and add it.
1717 for (j = 0; j <= h->highest_lun; j++) {
1718 if (h->drv[j].LunID == lunid) {
1719 drv_index = j;
1720 drv_found = 1;
1724 /* check if the drive was found already in the array */
1725 if (!drv_found) {
1726 drv_index = cciss_find_free_drive_index(ctlr);
1727 if (drv_index == -1)
1728 goto freeret;
1730 /*Check if the gendisk needs to be allocated */
1731 if (!h->gendisk[drv_index]){
1732 h->gendisk[drv_index] = alloc_disk(1 << NWD_SHIFT);
1733 if (!h->gendisk[drv_index]){
1734 printk(KERN_ERR "cciss: could not allocate new disk %d\n", drv_index);
1735 goto mem_msg;
1739 h->drv[drv_index].LunID = lunid;
1740 cciss_update_drive_info(ctlr, drv_index);
1741 } /* end for */
1742 } /* end else */
1744 freeret:
1745 kfree(ld_buff);
1746 h->busy_configuring = 0;
1747 /* We return -1 here to tell the ACU that we have registered/updated
1748 * all of the drives that we can and to keep it from calling us
1749 * additional times.
1751 return -1;
1752 mem_msg:
1753 printk(KERN_ERR "cciss: out of memory\n");
1754 goto freeret;
1757 /* This function will deregister the disk and it's queue from the
1758 * kernel. It must be called with the controller lock held and the
1759 * drv structures busy_configuring flag set. It's parameters are:
1761 * disk = This is the disk to be deregistered
1762 * drv = This is the drive_info_struct associated with the disk to be
1763 * deregistered. It contains information about the disk used
1764 * by the driver.
1765 * clear_all = This flag determines whether or not the disk information
1766 * is going to be completely cleared out and the highest_lun
1767 * reset. Sometimes we want to clear out information about
1768 * the disk in preparation for re-adding it. In this case
1769 * the highest_lun should be left unchanged and the LunID
1770 * should not be cleared.
1772 static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
1773 int clear_all)
1775 int i;
1776 ctlr_info_t *h = get_host(disk);
1778 if (!capable(CAP_SYS_RAWIO))
1779 return -EPERM;
1781 /* make sure logical volume is NOT is use */
1782 if (clear_all || (h->gendisk[0] == disk)) {
1783 if (drv->usage_count > 1)
1784 return -EBUSY;
1785 } else if (drv->usage_count > 0)
1786 return -EBUSY;
1788 /* invalidate the devices and deregister the disk. If it is disk
1789 * zero do not deregister it but just zero out it's values. This
1790 * allows us to delete disk zero but keep the controller registered.
1792 if (h->gendisk[0] != disk) {
1793 struct request_queue *q = disk->queue;
1794 if (disk->flags & GENHD_FL_UP)
1795 del_gendisk(disk);
1796 if (q) {
1797 blk_cleanup_queue(q);
1798 /* Set drv->queue to NULL so that we do not try
1799 * to call blk_start_queue on this queue in the
1800 * interrupt handler
1802 drv->queue = NULL;
1804 /* If clear_all is set then we are deleting the logical
1805 * drive, not just refreshing its info. For drives
1806 * other than disk 0 we will call put_disk. We do not
1807 * do this for disk 0 as we need it to be able to
1808 * configure the controller.
1810 if (clear_all){
1811 /* This isn't pretty, but we need to find the
1812 * disk in our array and NULL our the pointer.
1813 * This is so that we will call alloc_disk if
1814 * this index is used again later.
1816 for (i=0; i < CISS_MAX_LUN; i++){
1817 if(h->gendisk[i] == disk){
1818 h->gendisk[i] = NULL;
1819 break;
1822 put_disk(disk);
1824 } else {
1825 set_capacity(disk, 0);
1828 --h->num_luns;
1829 /* zero out the disk size info */
1830 drv->nr_blocks = 0;
1831 drv->block_size = 0;
1832 drv->heads = 0;
1833 drv->sectors = 0;
1834 drv->cylinders = 0;
1835 drv->raid_level = -1; /* This can be used as a flag variable to
1836 * indicate that this element of the drive
1837 * array is free.
1840 if (clear_all) {
1841 /* check to see if it was the last disk */
1842 if (drv == h->drv + h->highest_lun) {
1843 /* if so, find the new hightest lun */
1844 int i, newhighest = -1;
1845 for (i = 0; i < h->highest_lun; i++) {
1846 /* if the disk has size > 0, it is available */
1847 if (h->drv[i].heads)
1848 newhighest = i;
1850 h->highest_lun = newhighest;
1853 drv->LunID = 0;
1855 return 0;
1858 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, /* 0: address the controller,
1859 1: address logical volume log_unit,
1860 2: periph device address is scsi3addr */
1861 unsigned int log_unit, __u8 page_code,
1862 unsigned char *scsi3addr, int cmd_type)
1864 ctlr_info_t *h = hba[ctlr];
1865 u64bit buff_dma_handle;
1866 int status = IO_OK;
1868 c->cmd_type = CMD_IOCTL_PEND;
1869 c->Header.ReplyQueue = 0;
1870 if (buff != NULL) {
1871 c->Header.SGList = 1;
1872 c->Header.SGTotal = 1;
1873 } else {
1874 c->Header.SGList = 0;
1875 c->Header.SGTotal = 0;
1877 c->Header.Tag.lower = c->busaddr;
1879 c->Request.Type.Type = cmd_type;
1880 if (cmd_type == TYPE_CMD) {
1881 switch (cmd) {
1882 case CISS_INQUIRY:
1883 /* If the logical unit number is 0 then, this is going
1884 to controller so It's a physical command
1885 mode = 0 target = 0. So we have nothing to write.
1886 otherwise, if use_unit_num == 1,
1887 mode = 1(volume set addressing) target = LUNID
1888 otherwise, if use_unit_num == 2,
1889 mode = 0(periph dev addr) target = scsi3addr */
1890 if (use_unit_num == 1) {
1891 c->Header.LUN.LogDev.VolId =
1892 h->drv[log_unit].LunID;
1893 c->Header.LUN.LogDev.Mode = 1;
1894 } else if (use_unit_num == 2) {
1895 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr,
1897 c->Header.LUN.LogDev.Mode = 0;
1899 /* are we trying to read a vital product page */
1900 if (page_code != 0) {
1901 c->Request.CDB[1] = 0x01;
1902 c->Request.CDB[2] = page_code;
1904 c->Request.CDBLen = 6;
1905 c->Request.Type.Attribute = ATTR_SIMPLE;
1906 c->Request.Type.Direction = XFER_READ;
1907 c->Request.Timeout = 0;
1908 c->Request.CDB[0] = CISS_INQUIRY;
1909 c->Request.CDB[4] = size & 0xFF;
1910 break;
1911 case CISS_REPORT_LOG:
1912 case CISS_REPORT_PHYS:
1913 /* Talking to controller so It's a physical command
1914 mode = 00 target = 0. Nothing to write.
1916 c->Request.CDBLen = 12;
1917 c->Request.Type.Attribute = ATTR_SIMPLE;
1918 c->Request.Type.Direction = XFER_READ;
1919 c->Request.Timeout = 0;
1920 c->Request.CDB[0] = cmd;
1921 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
1922 c->Request.CDB[7] = (size >> 16) & 0xFF;
1923 c->Request.CDB[8] = (size >> 8) & 0xFF;
1924 c->Request.CDB[9] = size & 0xFF;
1925 break;
1927 case CCISS_READ_CAPACITY:
1928 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1929 c->Header.LUN.LogDev.Mode = 1;
1930 c->Request.CDBLen = 10;
1931 c->Request.Type.Attribute = ATTR_SIMPLE;
1932 c->Request.Type.Direction = XFER_READ;
1933 c->Request.Timeout = 0;
1934 c->Request.CDB[0] = cmd;
1935 break;
1936 case CCISS_READ_CAPACITY_16:
1937 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1938 c->Header.LUN.LogDev.Mode = 1;
1939 c->Request.CDBLen = 16;
1940 c->Request.Type.Attribute = ATTR_SIMPLE;
1941 c->Request.Type.Direction = XFER_READ;
1942 c->Request.Timeout = 0;
1943 c->Request.CDB[0] = cmd;
1944 c->Request.CDB[1] = 0x10;
1945 c->Request.CDB[10] = (size >> 24) & 0xFF;
1946 c->Request.CDB[11] = (size >> 16) & 0xFF;
1947 c->Request.CDB[12] = (size >> 8) & 0xFF;
1948 c->Request.CDB[13] = size & 0xFF;
1949 c->Request.Timeout = 0;
1950 c->Request.CDB[0] = cmd;
1951 break;
1952 case CCISS_CACHE_FLUSH:
1953 c->Request.CDBLen = 12;
1954 c->Request.Type.Attribute = ATTR_SIMPLE;
1955 c->Request.Type.Direction = XFER_WRITE;
1956 c->Request.Timeout = 0;
1957 c->Request.CDB[0] = BMIC_WRITE;
1958 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
1959 break;
1960 default:
1961 printk(KERN_WARNING
1962 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
1963 return IO_ERROR;
1965 } else if (cmd_type == TYPE_MSG) {
1966 switch (cmd) {
1967 case 0: /* ABORT message */
1968 c->Request.CDBLen = 12;
1969 c->Request.Type.Attribute = ATTR_SIMPLE;
1970 c->Request.Type.Direction = XFER_WRITE;
1971 c->Request.Timeout = 0;
1972 c->Request.CDB[0] = cmd; /* abort */
1973 c->Request.CDB[1] = 0; /* abort a command */
1974 /* buff contains the tag of the command to abort */
1975 memcpy(&c->Request.CDB[4], buff, 8);
1976 break;
1977 case 1: /* RESET message */
1978 c->Request.CDBLen = 12;
1979 c->Request.Type.Attribute = ATTR_SIMPLE;
1980 c->Request.Type.Direction = XFER_WRITE;
1981 c->Request.Timeout = 0;
1982 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
1983 c->Request.CDB[0] = cmd; /* reset */
1984 c->Request.CDB[1] = 0x04; /* reset a LUN */
1985 break;
1986 case 3: /* No-Op message */
1987 c->Request.CDBLen = 1;
1988 c->Request.Type.Attribute = ATTR_SIMPLE;
1989 c->Request.Type.Direction = XFER_WRITE;
1990 c->Request.Timeout = 0;
1991 c->Request.CDB[0] = cmd;
1992 break;
1993 default:
1994 printk(KERN_WARNING
1995 "cciss%d: unknown message type %d\n", ctlr, cmd);
1996 return IO_ERROR;
1998 } else {
1999 printk(KERN_WARNING
2000 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
2001 return IO_ERROR;
2003 /* Fill in the scatter gather information */
2004 if (size > 0) {
2005 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
2006 buff, size,
2007 PCI_DMA_BIDIRECTIONAL);
2008 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
2009 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
2010 c->SG[0].Len = size;
2011 c->SG[0].Ext = 0; /* we are not chaining */
2013 return status;
2016 static int sendcmd_withirq(__u8 cmd,
2017 int ctlr,
2018 void *buff,
2019 size_t size,
2020 unsigned int use_unit_num,
2021 unsigned int log_unit, __u8 page_code, int cmd_type)
2023 ctlr_info_t *h = hba[ctlr];
2024 CommandList_struct *c;
2025 u64bit buff_dma_handle;
2026 unsigned long flags;
2027 int return_status;
2028 DECLARE_COMPLETION_ONSTACK(wait);
2030 if ((c = cmd_alloc(h, 0)) == NULL)
2031 return -ENOMEM;
2032 return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
2033 log_unit, page_code, NULL, cmd_type);
2034 if (return_status != IO_OK) {
2035 cmd_free(h, c, 0);
2036 return return_status;
2038 resend_cmd2:
2039 c->waiting = &wait;
2041 /* Put the request on the tail of the queue and send it */
2042 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
2043 addQ(&h->reqQ, c);
2044 h->Qdepth++;
2045 start_io(h);
2046 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
2048 wait_for_completion(&wait);
2050 if (c->err_info->CommandStatus != 0) { /* an error has occurred */
2051 switch (c->err_info->CommandStatus) {
2052 case CMD_TARGET_STATUS:
2053 printk(KERN_WARNING "cciss: cmd %p has "
2054 " completed with errors\n", c);
2055 if (c->err_info->ScsiStatus) {
2056 printk(KERN_WARNING "cciss: cmd %p "
2057 "has SCSI Status = %x\n",
2058 c, c->err_info->ScsiStatus);
2061 break;
2062 case CMD_DATA_UNDERRUN:
2063 case CMD_DATA_OVERRUN:
2064 /* expected for inquire and report lun commands */
2065 break;
2066 case CMD_INVALID:
2067 printk(KERN_WARNING "cciss: Cmd %p is "
2068 "reported invalid\n", c);
2069 return_status = IO_ERROR;
2070 break;
2071 case CMD_PROTOCOL_ERR:
2072 printk(KERN_WARNING "cciss: cmd %p has "
2073 "protocol error \n", c);
2074 return_status = IO_ERROR;
2075 break;
2076 case CMD_HARDWARE_ERR:
2077 printk(KERN_WARNING "cciss: cmd %p had "
2078 " hardware error\n", c);
2079 return_status = IO_ERROR;
2080 break;
2081 case CMD_CONNECTION_LOST:
2082 printk(KERN_WARNING "cciss: cmd %p had "
2083 "connection lost\n", c);
2084 return_status = IO_ERROR;
2085 break;
2086 case CMD_ABORTED:
2087 printk(KERN_WARNING "cciss: cmd %p was "
2088 "aborted\n", c);
2089 return_status = IO_ERROR;
2090 break;
2091 case CMD_ABORT_FAILED:
2092 printk(KERN_WARNING "cciss: cmd %p reports "
2093 "abort failed\n", c);
2094 return_status = IO_ERROR;
2095 break;
2096 case CMD_UNSOLICITED_ABORT:
2097 printk(KERN_WARNING
2098 "cciss%d: unsolicited abort %p\n", ctlr, c);
2099 if (c->retry_count < MAX_CMD_RETRIES) {
2100 printk(KERN_WARNING
2101 "cciss%d: retrying %p\n", ctlr, c);
2102 c->retry_count++;
2103 /* erase the old error information */
2104 memset(c->err_info, 0,
2105 sizeof(ErrorInfo_struct));
2106 return_status = IO_OK;
2107 INIT_COMPLETION(wait);
2108 goto resend_cmd2;
2110 return_status = IO_ERROR;
2111 break;
2112 default:
2113 printk(KERN_WARNING "cciss: cmd %p returned "
2114 "unknown status %x\n", c,
2115 c->err_info->CommandStatus);
2116 return_status = IO_ERROR;
2119 /* unlock the buffers from DMA */
2120 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
2121 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
2122 pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
2123 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
2124 cmd_free(h, c, 0);
2125 return return_status;
2128 static void cciss_geometry_inquiry(int ctlr, int logvol,
2129 int withirq, sector_t total_size,
2130 unsigned int block_size,
2131 InquiryData_struct *inq_buff,
2132 drive_info_struct *drv)
2134 int return_code;
2135 unsigned long t;
2137 memset(inq_buff, 0, sizeof(InquiryData_struct));
2138 if (withirq)
2139 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
2140 inq_buff, sizeof(*inq_buff), 1,
2141 logvol, 0xC1, TYPE_CMD);
2142 else
2143 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
2144 sizeof(*inq_buff), 1, logvol, 0xC1, NULL,
2145 TYPE_CMD);
2146 if (return_code == IO_OK) {
2147 if (inq_buff->data_byte[8] == 0xFF) {
2148 printk(KERN_WARNING
2149 "cciss: reading geometry failed, volume "
2150 "does not support reading geometry\n");
2151 drv->heads = 255;
2152 drv->sectors = 32; // Sectors per track
2153 drv->cylinders = total_size + 1;
2154 drv->raid_level = RAID_UNKNOWN;
2155 } else {
2156 drv->heads = inq_buff->data_byte[6];
2157 drv->sectors = inq_buff->data_byte[7];
2158 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
2159 drv->cylinders += inq_buff->data_byte[5];
2160 drv->raid_level = inq_buff->data_byte[8];
2162 drv->block_size = block_size;
2163 drv->nr_blocks = total_size + 1;
2164 t = drv->heads * drv->sectors;
2165 if (t > 1) {
2166 sector_t real_size = total_size + 1;
2167 unsigned long rem = sector_div(real_size, t);
2168 if (rem)
2169 real_size++;
2170 drv->cylinders = real_size;
2172 } else { /* Get geometry failed */
2173 printk(KERN_WARNING "cciss: reading geometry failed\n");
2175 printk(KERN_INFO " heads=%d, sectors=%d, cylinders=%d\n\n",
2176 drv->heads, drv->sectors, drv->cylinders);
2179 static void
2180 cciss_read_capacity(int ctlr, int logvol, int withirq, sector_t *total_size,
2181 unsigned int *block_size)
2183 ReadCapdata_struct *buf;
2184 int return_code;
2186 buf = kzalloc(sizeof(ReadCapdata_struct), GFP_KERNEL);
2187 if (!buf) {
2188 printk(KERN_WARNING "cciss: out of memory\n");
2189 return;
2192 if (withirq)
2193 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
2194 ctlr, buf, sizeof(ReadCapdata_struct),
2195 1, logvol, 0, TYPE_CMD);
2196 else
2197 return_code = sendcmd(CCISS_READ_CAPACITY,
2198 ctlr, buf, sizeof(ReadCapdata_struct),
2199 1, logvol, 0, NULL, TYPE_CMD);
2200 if (return_code == IO_OK) {
2201 *total_size = be32_to_cpu(*(__be32 *) buf->total_size);
2202 *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
2203 } else { /* read capacity command failed */
2204 printk(KERN_WARNING "cciss: read capacity failed\n");
2205 *total_size = 0;
2206 *block_size = BLOCK_SIZE;
2208 if (*total_size != 0)
2209 printk(KERN_INFO " blocks= %llu block_size= %d\n",
2210 (unsigned long long)*total_size+1, *block_size);
2211 kfree(buf);
2214 static void
2215 cciss_read_capacity_16(int ctlr, int logvol, int withirq, sector_t *total_size, unsigned int *block_size)
2217 ReadCapdata_struct_16 *buf;
2218 int return_code;
2220 buf = kzalloc(sizeof(ReadCapdata_struct_16), GFP_KERNEL);
2221 if (!buf) {
2222 printk(KERN_WARNING "cciss: out of memory\n");
2223 return;
2226 if (withirq) {
2227 return_code = sendcmd_withirq(CCISS_READ_CAPACITY_16,
2228 ctlr, buf, sizeof(ReadCapdata_struct_16),
2229 1, logvol, 0, TYPE_CMD);
2231 else {
2232 return_code = sendcmd(CCISS_READ_CAPACITY_16,
2233 ctlr, buf, sizeof(ReadCapdata_struct_16),
2234 1, logvol, 0, NULL, TYPE_CMD);
2236 if (return_code == IO_OK) {
2237 *total_size = be64_to_cpu(*(__be64 *) buf->total_size);
2238 *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
2239 } else { /* read capacity command failed */
2240 printk(KERN_WARNING "cciss: read capacity failed\n");
2241 *total_size = 0;
2242 *block_size = BLOCK_SIZE;
2244 printk(KERN_INFO " blocks= %llu block_size= %d\n",
2245 (unsigned long long)*total_size+1, *block_size);
2246 kfree(buf);
2249 static int cciss_revalidate(struct gendisk *disk)
2251 ctlr_info_t *h = get_host(disk);
2252 drive_info_struct *drv = get_drv(disk);
2253 int logvol;
2254 int FOUND = 0;
2255 unsigned int block_size;
2256 sector_t total_size;
2257 InquiryData_struct *inq_buff = NULL;
2259 for (logvol = 0; logvol < CISS_MAX_LUN; logvol++) {
2260 if (h->drv[logvol].LunID == drv->LunID) {
2261 FOUND = 1;
2262 break;
2266 if (!FOUND)
2267 return 1;
2269 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
2270 if (inq_buff == NULL) {
2271 printk(KERN_WARNING "cciss: out of memory\n");
2272 return 1;
2274 if (h->cciss_read == CCISS_READ_10) {
2275 cciss_read_capacity(h->ctlr, logvol, 1,
2276 &total_size, &block_size);
2277 } else {
2278 cciss_read_capacity_16(h->ctlr, logvol, 1,
2279 &total_size, &block_size);
2281 cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size,
2282 inq_buff, drv);
2284 blk_queue_hardsect_size(drv->queue, drv->block_size);
2285 set_capacity(disk, drv->nr_blocks);
2287 kfree(inq_buff);
2288 return 0;
2292 * Wait polling for a command to complete.
2293 * The memory mapped FIFO is polled for the completion.
2294 * Used only at init time, interrupts from the HBA are disabled.
2296 static unsigned long pollcomplete(int ctlr)
2298 unsigned long done;
2299 int i;
2301 /* Wait (up to 20 seconds) for a command to complete */
2303 for (i = 20 * HZ; i > 0; i--) {
2304 done = hba[ctlr]->access.command_completed(hba[ctlr]);
2305 if (done == FIFO_EMPTY)
2306 schedule_timeout_uninterruptible(1);
2307 else
2308 return done;
2310 /* Invalid address to tell caller we ran out of time */
2311 return 1;
2314 static int add_sendcmd_reject(__u8 cmd, int ctlr, unsigned long complete)
2316 /* We get in here if sendcmd() is polling for completions
2317 and gets some command back that it wasn't expecting --
2318 something other than that which it just sent down.
2319 Ordinarily, that shouldn't happen, but it can happen when
2320 the scsi tape stuff gets into error handling mode, and
2321 starts using sendcmd() to try to abort commands and
2322 reset tape drives. In that case, sendcmd may pick up
2323 completions of commands that were sent to logical drives
2324 through the block i/o system, or cciss ioctls completing, etc.
2325 In that case, we need to save those completions for later
2326 processing by the interrupt handler.
2329 #ifdef CONFIG_CISS_SCSI_TAPE
2330 struct sendcmd_reject_list *srl = &hba[ctlr]->scsi_rejects;
2332 /* If it's not the scsi tape stuff doing error handling, (abort */
2333 /* or reset) then we don't expect anything weird. */
2334 if (cmd != CCISS_RESET_MSG && cmd != CCISS_ABORT_MSG) {
2335 #endif
2336 printk(KERN_WARNING "cciss cciss%d: SendCmd "
2337 "Invalid command list address returned! (%lx)\n",
2338 ctlr, complete);
2339 /* not much we can do. */
2340 #ifdef CONFIG_CISS_SCSI_TAPE
2341 return 1;
2344 /* We've sent down an abort or reset, but something else
2345 has completed */
2346 if (srl->ncompletions >= (hba[ctlr]->nr_cmds + 2)) {
2347 /* Uh oh. No room to save it for later... */
2348 printk(KERN_WARNING "cciss%d: Sendcmd: Invalid command addr, "
2349 "reject list overflow, command lost!\n", ctlr);
2350 return 1;
2352 /* Save it for later */
2353 srl->complete[srl->ncompletions] = complete;
2354 srl->ncompletions++;
2355 #endif
2356 return 0;
2360 * Send a command to the controller, and wait for it to complete.
2361 * Only used at init time.
2363 static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, /* 0: address the controller,
2364 1: address logical volume log_unit,
2365 2: periph device address is scsi3addr */
2366 unsigned int log_unit,
2367 __u8 page_code, unsigned char *scsi3addr, int cmd_type)
2369 CommandList_struct *c;
2370 int i;
2371 unsigned long complete;
2372 ctlr_info_t *info_p = hba[ctlr];
2373 u64bit buff_dma_handle;
2374 int status, done = 0;
2376 if ((c = cmd_alloc(info_p, 1)) == NULL) {
2377 printk(KERN_WARNING "cciss: unable to get memory");
2378 return IO_ERROR;
2380 status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
2381 log_unit, page_code, scsi3addr, cmd_type);
2382 if (status != IO_OK) {
2383 cmd_free(info_p, c, 1);
2384 return status;
2386 resend_cmd1:
2388 * Disable interrupt
2390 #ifdef CCISS_DEBUG
2391 printk(KERN_DEBUG "cciss: turning intr off\n");
2392 #endif /* CCISS_DEBUG */
2393 info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
2395 /* Make sure there is room in the command FIFO */
2396 /* Actually it should be completely empty at this time */
2397 /* unless we are in here doing error handling for the scsi */
2398 /* tape side of the driver. */
2399 for (i = 200000; i > 0; i--) {
2400 /* if fifo isn't full go */
2401 if (!(info_p->access.fifo_full(info_p))) {
2403 break;
2405 udelay(10);
2406 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
2407 " waiting!\n", ctlr);
2410 * Send the cmd
2412 info_p->access.submit_command(info_p, c);
2413 done = 0;
2414 do {
2415 complete = pollcomplete(ctlr);
2417 #ifdef CCISS_DEBUG
2418 printk(KERN_DEBUG "cciss: command completed\n");
2419 #endif /* CCISS_DEBUG */
2421 if (complete == 1) {
2422 printk(KERN_WARNING
2423 "cciss cciss%d: SendCmd Timeout out, "
2424 "No command list address returned!\n", ctlr);
2425 status = IO_ERROR;
2426 done = 1;
2427 break;
2430 /* This will need to change for direct lookup completions */
2431 if ((complete & CISS_ERROR_BIT)
2432 && (complete & ~CISS_ERROR_BIT) == c->busaddr) {
2433 /* if data overrun or underun on Report command
2434 ignore it
2436 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
2437 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
2438 (c->Request.CDB[0] == CISS_INQUIRY)) &&
2439 ((c->err_info->CommandStatus ==
2440 CMD_DATA_OVERRUN) ||
2441 (c->err_info->CommandStatus == CMD_DATA_UNDERRUN)
2442 )) {
2443 complete = c->busaddr;
2444 } else {
2445 if (c->err_info->CommandStatus ==
2446 CMD_UNSOLICITED_ABORT) {
2447 printk(KERN_WARNING "cciss%d: "
2448 "unsolicited abort %p\n",
2449 ctlr, c);
2450 if (c->retry_count < MAX_CMD_RETRIES) {
2451 printk(KERN_WARNING
2452 "cciss%d: retrying %p\n",
2453 ctlr, c);
2454 c->retry_count++;
2455 /* erase the old error */
2456 /* information */
2457 memset(c->err_info, 0,
2458 sizeof
2459 (ErrorInfo_struct));
2460 goto resend_cmd1;
2461 } else {
2462 printk(KERN_WARNING
2463 "cciss%d: retried %p too "
2464 "many times\n", ctlr, c);
2465 status = IO_ERROR;
2466 goto cleanup1;
2468 } else if (c->err_info->CommandStatus ==
2469 CMD_UNABORTABLE) {
2470 printk(KERN_WARNING
2471 "cciss%d: command could not be aborted.\n",
2472 ctlr);
2473 status = IO_ERROR;
2474 goto cleanup1;
2476 printk(KERN_WARNING "ciss ciss%d: sendcmd"
2477 " Error %x \n", ctlr,
2478 c->err_info->CommandStatus);
2479 printk(KERN_WARNING "ciss ciss%d: sendcmd"
2480 " offensive info\n"
2481 " size %x\n num %x value %x\n",
2482 ctlr,
2483 c->err_info->MoreErrInfo.Invalid_Cmd.
2484 offense_size,
2485 c->err_info->MoreErrInfo.Invalid_Cmd.
2486 offense_num,
2487 c->err_info->MoreErrInfo.Invalid_Cmd.
2488 offense_value);
2489 status = IO_ERROR;
2490 goto cleanup1;
2493 /* This will need changing for direct lookup completions */
2494 if (complete != c->busaddr) {
2495 if (add_sendcmd_reject(cmd, ctlr, complete) != 0) {
2496 BUG(); /* we are pretty much hosed if we get here. */
2498 continue;
2499 } else
2500 done = 1;
2501 } while (!done);
2503 cleanup1:
2504 /* unlock the data buffer from DMA */
2505 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
2506 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
2507 pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
2508 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
2509 #ifdef CONFIG_CISS_SCSI_TAPE
2510 /* if we saved some commands for later, process them now. */
2511 if (info_p->scsi_rejects.ncompletions > 0)
2512 do_cciss_intr(0, info_p);
2513 #endif
2514 cmd_free(info_p, c, 1);
2515 return status;
2519 * Map (physical) PCI mem into (virtual) kernel space
2521 static void __iomem *remap_pci_mem(ulong base, ulong size)
2523 ulong page_base = ((ulong) base) & PAGE_MASK;
2524 ulong page_offs = ((ulong) base) - page_base;
2525 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
2527 return page_remapped ? (page_remapped + page_offs) : NULL;
2531 * Takes jobs of the Q and sends them to the hardware, then puts it on
2532 * the Q to wait for completion.
2534 static void start_io(ctlr_info_t *h)
2536 CommandList_struct *c;
2538 while ((c = h->reqQ) != NULL) {
2539 /* can't do anything if fifo is full */
2540 if ((h->access.fifo_full(h))) {
2541 printk(KERN_WARNING "cciss: fifo full\n");
2542 break;
2545 /* Get the first entry from the Request Q */
2546 removeQ(&(h->reqQ), c);
2547 h->Qdepth--;
2549 /* Tell the controller execute command */
2550 h->access.submit_command(h, c);
2552 /* Put job onto the completed Q */
2553 addQ(&(h->cmpQ), c);
2557 /* Assumes that CCISS_LOCK(h->ctlr) is held. */
2558 /* Zeros out the error record and then resends the command back */
2559 /* to the controller */
2560 static inline void resend_cciss_cmd(ctlr_info_t *h, CommandList_struct *c)
2562 /* erase the old error information */
2563 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
2565 /* add it to software queue and then send it to the controller */
2566 addQ(&(h->reqQ), c);
2567 h->Qdepth++;
2568 if (h->Qdepth > h->maxQsinceinit)
2569 h->maxQsinceinit = h->Qdepth;
2571 start_io(h);
2574 static inline unsigned int make_status_bytes(unsigned int scsi_status_byte,
2575 unsigned int msg_byte, unsigned int host_byte,
2576 unsigned int driver_byte)
2578 /* inverse of macros in scsi.h */
2579 return (scsi_status_byte & 0xff) |
2580 ((msg_byte & 0xff) << 8) |
2581 ((host_byte & 0xff) << 16) |
2582 ((driver_byte & 0xff) << 24);
2585 static inline int evaluate_target_status(CommandList_struct *cmd)
2587 unsigned char sense_key;
2588 unsigned char status_byte, msg_byte, host_byte, driver_byte;
2589 int error_value;
2591 /* If we get in here, it means we got "target status", that is, scsi status */
2592 status_byte = cmd->err_info->ScsiStatus;
2593 driver_byte = DRIVER_OK;
2594 msg_byte = cmd->err_info->CommandStatus; /* correct? seems too device specific */
2596 if (blk_pc_request(cmd->rq))
2597 host_byte = DID_PASSTHROUGH;
2598 else
2599 host_byte = DID_OK;
2601 error_value = make_status_bytes(status_byte, msg_byte,
2602 host_byte, driver_byte);
2604 if (cmd->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) {
2605 if (!blk_pc_request(cmd->rq))
2606 printk(KERN_WARNING "cciss: cmd %p "
2607 "has SCSI Status 0x%x\n",
2608 cmd, cmd->err_info->ScsiStatus);
2609 return error_value;
2612 /* check the sense key */
2613 sense_key = 0xf & cmd->err_info->SenseInfo[2];
2614 /* no status or recovered error */
2615 if (((sense_key == 0x0) || (sense_key == 0x1)) && !blk_pc_request(cmd->rq))
2616 error_value = 0;
2618 if (!blk_pc_request(cmd->rq)) { /* Not SG_IO or similar? */
2619 if (error_value != 0)
2620 printk(KERN_WARNING "cciss: cmd %p has CHECK CONDITION"
2621 " sense key = 0x%x\n", cmd, sense_key);
2622 return error_value;
2625 /* SG_IO or similar, copy sense data back */
2626 if (cmd->rq->sense) {
2627 if (cmd->rq->sense_len > cmd->err_info->SenseLen)
2628 cmd->rq->sense_len = cmd->err_info->SenseLen;
2629 memcpy(cmd->rq->sense, cmd->err_info->SenseInfo,
2630 cmd->rq->sense_len);
2631 } else
2632 cmd->rq->sense_len = 0;
2634 return error_value;
2637 /* checks the status of the job and calls complete buffers to mark all
2638 * buffers for the completed job. Note that this function does not need
2639 * to hold the hba/queue lock.
2641 static inline void complete_command(ctlr_info_t *h, CommandList_struct *cmd,
2642 int timeout)
2644 int retry_cmd = 0;
2645 struct request *rq = cmd->rq;
2647 rq->errors = 0;
2649 if (timeout)
2650 rq->errors = make_status_bytes(0, 0, 0, DRIVER_TIMEOUT);
2652 if (cmd->err_info->CommandStatus == 0) /* no error has occurred */
2653 goto after_error_processing;
2655 switch (cmd->err_info->CommandStatus) {
2656 case CMD_TARGET_STATUS:
2657 rq->errors = evaluate_target_status(cmd);
2658 break;
2659 case CMD_DATA_UNDERRUN:
2660 if (blk_fs_request(cmd->rq)) {
2661 printk(KERN_WARNING "cciss: cmd %p has"
2662 " completed with data underrun "
2663 "reported\n", cmd);
2664 cmd->rq->data_len = cmd->err_info->ResidualCnt;
2666 break;
2667 case CMD_DATA_OVERRUN:
2668 if (blk_fs_request(cmd->rq))
2669 printk(KERN_WARNING "cciss: cmd %p has"
2670 " completed with data overrun "
2671 "reported\n", cmd);
2672 break;
2673 case CMD_INVALID:
2674 printk(KERN_WARNING "cciss: cmd %p is "
2675 "reported invalid\n", cmd);
2676 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2677 cmd->err_info->CommandStatus, DRIVER_OK,
2678 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
2679 break;
2680 case CMD_PROTOCOL_ERR:
2681 printk(KERN_WARNING "cciss: cmd %p has "
2682 "protocol error \n", cmd);
2683 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2684 cmd->err_info->CommandStatus, DRIVER_OK,
2685 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
2686 break;
2687 case CMD_HARDWARE_ERR:
2688 printk(KERN_WARNING "cciss: cmd %p had "
2689 " hardware error\n", cmd);
2690 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2691 cmd->err_info->CommandStatus, DRIVER_OK,
2692 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
2693 break;
2694 case CMD_CONNECTION_LOST:
2695 printk(KERN_WARNING "cciss: cmd %p had "
2696 "connection lost\n", cmd);
2697 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2698 cmd->err_info->CommandStatus, DRIVER_OK,
2699 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
2700 break;
2701 case CMD_ABORTED:
2702 printk(KERN_WARNING "cciss: cmd %p was "
2703 "aborted\n", cmd);
2704 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2705 cmd->err_info->CommandStatus, DRIVER_OK,
2706 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT);
2707 break;
2708 case CMD_ABORT_FAILED:
2709 printk(KERN_WARNING "cciss: cmd %p reports "
2710 "abort failed\n", cmd);
2711 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2712 cmd->err_info->CommandStatus, DRIVER_OK,
2713 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
2714 break;
2715 case CMD_UNSOLICITED_ABORT:
2716 printk(KERN_WARNING "cciss%d: unsolicited "
2717 "abort %p\n", h->ctlr, cmd);
2718 if (cmd->retry_count < MAX_CMD_RETRIES) {
2719 retry_cmd = 1;
2720 printk(KERN_WARNING
2721 "cciss%d: retrying %p\n", h->ctlr, cmd);
2722 cmd->retry_count++;
2723 } else
2724 printk(KERN_WARNING
2725 "cciss%d: %p retried too "
2726 "many times\n", h->ctlr, cmd);
2727 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2728 cmd->err_info->CommandStatus, DRIVER_OK,
2729 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT);
2730 break;
2731 case CMD_TIMEOUT:
2732 printk(KERN_WARNING "cciss: cmd %p timedout\n", cmd);
2733 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2734 cmd->err_info->CommandStatus, DRIVER_OK,
2735 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
2736 break;
2737 default:
2738 printk(KERN_WARNING "cciss: cmd %p returned "
2739 "unknown status %x\n", cmd,
2740 cmd->err_info->CommandStatus);
2741 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2742 cmd->err_info->CommandStatus, DRIVER_OK,
2743 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
2746 after_error_processing:
2748 /* We need to return this command */
2749 if (retry_cmd) {
2750 resend_cciss_cmd(h, cmd);
2751 return;
2753 cmd->rq->completion_data = cmd;
2754 blk_complete_request(cmd->rq);
2758 * Get a request and submit it to the controller.
2760 static void do_cciss_request(struct request_queue *q)
2762 ctlr_info_t *h = q->queuedata;
2763 CommandList_struct *c;
2764 sector_t start_blk;
2765 int seg;
2766 struct request *creq;
2767 u64bit temp64;
2768 struct scatterlist tmp_sg[MAXSGENTRIES];
2769 drive_info_struct *drv;
2770 int i, dir;
2772 /* We call start_io here in case there is a command waiting on the
2773 * queue that has not been sent.
2775 if (blk_queue_plugged(q))
2776 goto startio;
2778 queue:
2779 creq = elv_next_request(q);
2780 if (!creq)
2781 goto startio;
2783 BUG_ON(creq->nr_phys_segments > MAXSGENTRIES);
2785 if ((c = cmd_alloc(h, 1)) == NULL)
2786 goto full;
2788 blkdev_dequeue_request(creq);
2790 spin_unlock_irq(q->queue_lock);
2792 c->cmd_type = CMD_RWREQ;
2793 c->rq = creq;
2795 /* fill in the request */
2796 drv = creq->rq_disk->private_data;
2797 c->Header.ReplyQueue = 0; // unused in simple mode
2798 /* got command from pool, so use the command block index instead */
2799 /* for direct lookups. */
2800 /* The first 2 bits are reserved for controller error reporting. */
2801 c->Header.Tag.lower = (c->cmdindex << 3);
2802 c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */
2803 c->Header.LUN.LogDev.VolId = drv->LunID;
2804 c->Header.LUN.LogDev.Mode = 1;
2805 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
2806 c->Request.Type.Type = TYPE_CMD; // It is a command.
2807 c->Request.Type.Attribute = ATTR_SIMPLE;
2808 c->Request.Type.Direction =
2809 (rq_data_dir(creq) == READ) ? XFER_READ : XFER_WRITE;
2810 c->Request.Timeout = 0; // Don't time out
2811 c->Request.CDB[0] =
2812 (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write;
2813 start_blk = creq->sector;
2814 #ifdef CCISS_DEBUG
2815 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n", (int)creq->sector,
2816 (int)creq->nr_sectors);
2817 #endif /* CCISS_DEBUG */
2819 sg_init_table(tmp_sg, MAXSGENTRIES);
2820 seg = blk_rq_map_sg(q, creq, tmp_sg);
2822 /* get the DMA records for the setup */
2823 if (c->Request.Type.Direction == XFER_READ)
2824 dir = PCI_DMA_FROMDEVICE;
2825 else
2826 dir = PCI_DMA_TODEVICE;
2828 for (i = 0; i < seg; i++) {
2829 c->SG[i].Len = tmp_sg[i].length;
2830 temp64.val = (__u64) pci_map_page(h->pdev, sg_page(&tmp_sg[i]),
2831 tmp_sg[i].offset,
2832 tmp_sg[i].length, dir);
2833 c->SG[i].Addr.lower = temp64.val32.lower;
2834 c->SG[i].Addr.upper = temp64.val32.upper;
2835 c->SG[i].Ext = 0; // we are not chaining
2837 /* track how many SG entries we are using */
2838 if (seg > h->maxSG)
2839 h->maxSG = seg;
2841 #ifdef CCISS_DEBUG
2842 printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n",
2843 creq->nr_sectors, seg);
2844 #endif /* CCISS_DEBUG */
2846 c->Header.SGList = c->Header.SGTotal = seg;
2847 if (likely(blk_fs_request(creq))) {
2848 if(h->cciss_read == CCISS_READ_10) {
2849 c->Request.CDB[1] = 0;
2850 c->Request.CDB[2] = (start_blk >> 24) & 0xff; //MSB
2851 c->Request.CDB[3] = (start_blk >> 16) & 0xff;
2852 c->Request.CDB[4] = (start_blk >> 8) & 0xff;
2853 c->Request.CDB[5] = start_blk & 0xff;
2854 c->Request.CDB[6] = 0; // (sect >> 24) & 0xff; MSB
2855 c->Request.CDB[7] = (creq->nr_sectors >> 8) & 0xff;
2856 c->Request.CDB[8] = creq->nr_sectors & 0xff;
2857 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2858 } else {
2859 u32 upper32 = upper_32_bits(start_blk);
2861 c->Request.CDBLen = 16;
2862 c->Request.CDB[1]= 0;
2863 c->Request.CDB[2]= (upper32 >> 24) & 0xff; //MSB
2864 c->Request.CDB[3]= (upper32 >> 16) & 0xff;
2865 c->Request.CDB[4]= (upper32 >> 8) & 0xff;
2866 c->Request.CDB[5]= upper32 & 0xff;
2867 c->Request.CDB[6]= (start_blk >> 24) & 0xff;
2868 c->Request.CDB[7]= (start_blk >> 16) & 0xff;
2869 c->Request.CDB[8]= (start_blk >> 8) & 0xff;
2870 c->Request.CDB[9]= start_blk & 0xff;
2871 c->Request.CDB[10]= (creq->nr_sectors >> 24) & 0xff;
2872 c->Request.CDB[11]= (creq->nr_sectors >> 16) & 0xff;
2873 c->Request.CDB[12]= (creq->nr_sectors >> 8) & 0xff;
2874 c->Request.CDB[13]= creq->nr_sectors & 0xff;
2875 c->Request.CDB[14] = c->Request.CDB[15] = 0;
2877 } else if (blk_pc_request(creq)) {
2878 c->Request.CDBLen = creq->cmd_len;
2879 memcpy(c->Request.CDB, creq->cmd, BLK_MAX_CDB);
2880 } else {
2881 printk(KERN_WARNING "cciss%d: bad request type %d\n", h->ctlr, creq->cmd_type);
2882 BUG();
2885 spin_lock_irq(q->queue_lock);
2887 addQ(&(h->reqQ), c);
2888 h->Qdepth++;
2889 if (h->Qdepth > h->maxQsinceinit)
2890 h->maxQsinceinit = h->Qdepth;
2892 goto queue;
2893 full:
2894 blk_stop_queue(q);
2895 startio:
2896 /* We will already have the driver lock here so not need
2897 * to lock it.
2899 start_io(h);
2902 static inline unsigned long get_next_completion(ctlr_info_t *h)
2904 #ifdef CONFIG_CISS_SCSI_TAPE
2905 /* Any rejects from sendcmd() lying around? Process them first */
2906 if (h->scsi_rejects.ncompletions == 0)
2907 return h->access.command_completed(h);
2908 else {
2909 struct sendcmd_reject_list *srl;
2910 int n;
2911 srl = &h->scsi_rejects;
2912 n = --srl->ncompletions;
2913 /* printk("cciss%d: processing saved reject\n", h->ctlr); */
2914 printk("p");
2915 return srl->complete[n];
2917 #else
2918 return h->access.command_completed(h);
2919 #endif
2922 static inline int interrupt_pending(ctlr_info_t *h)
2924 #ifdef CONFIG_CISS_SCSI_TAPE
2925 return (h->access.intr_pending(h)
2926 || (h->scsi_rejects.ncompletions > 0));
2927 #else
2928 return h->access.intr_pending(h);
2929 #endif
2932 static inline long interrupt_not_for_us(ctlr_info_t *h)
2934 #ifdef CONFIG_CISS_SCSI_TAPE
2935 return (((h->access.intr_pending(h) == 0) ||
2936 (h->interrupts_enabled == 0))
2937 && (h->scsi_rejects.ncompletions == 0));
2938 #else
2939 return (((h->access.intr_pending(h) == 0) ||
2940 (h->interrupts_enabled == 0)));
2941 #endif
2944 static irqreturn_t do_cciss_intr(int irq, void *dev_id)
2946 ctlr_info_t *h = dev_id;
2947 CommandList_struct *c;
2948 unsigned long flags;
2949 __u32 a, a1, a2;
2951 if (interrupt_not_for_us(h))
2952 return IRQ_NONE;
2954 * If there are completed commands in the completion queue,
2955 * we had better do something about it.
2957 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2958 while (interrupt_pending(h)) {
2959 while ((a = get_next_completion(h)) != FIFO_EMPTY) {
2960 a1 = a;
2961 if ((a & 0x04)) {
2962 a2 = (a >> 3);
2963 if (a2 >= h->nr_cmds) {
2964 printk(KERN_WARNING
2965 "cciss: controller cciss%d failed, stopping.\n",
2966 h->ctlr);
2967 fail_all_cmds(h->ctlr);
2968 return IRQ_HANDLED;
2971 c = h->cmd_pool + a2;
2972 a = c->busaddr;
2974 } else {
2975 a &= ~3;
2976 if ((c = h->cmpQ) == NULL) {
2977 printk(KERN_WARNING
2978 "cciss: Completion of %08x ignored\n",
2979 a1);
2980 continue;
2982 while (c->busaddr != a) {
2983 c = c->next;
2984 if (c == h->cmpQ)
2985 break;
2989 * If we've found the command, take it off the
2990 * completion Q and free it
2992 if (c->busaddr == a) {
2993 removeQ(&h->cmpQ, c);
2994 if (c->cmd_type == CMD_RWREQ) {
2995 complete_command(h, c, 0);
2996 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2997 complete(c->waiting);
2999 # ifdef CONFIG_CISS_SCSI_TAPE
3000 else if (c->cmd_type == CMD_SCSI)
3001 complete_scsi_command(c, 0, a1);
3002 # endif
3003 continue;
3008 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
3009 return IRQ_HANDLED;
3013 * We cannot read the structure directly, for portability we must use
3014 * the io functions.
3015 * This is for debug only.
3017 #ifdef CCISS_DEBUG
3018 static void print_cfg_table(CfgTable_struct *tb)
3020 int i;
3021 char temp_name[17];
3023 printk("Controller Configuration information\n");
3024 printk("------------------------------------\n");
3025 for (i = 0; i < 4; i++)
3026 temp_name[i] = readb(&(tb->Signature[i]));
3027 temp_name[4] = '\0';
3028 printk(" Signature = %s\n", temp_name);
3029 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
3030 printk(" Transport methods supported = 0x%x\n",
3031 readl(&(tb->TransportSupport)));
3032 printk(" Transport methods active = 0x%x\n",
3033 readl(&(tb->TransportActive)));
3034 printk(" Requested transport Method = 0x%x\n",
3035 readl(&(tb->HostWrite.TransportRequest)));
3036 printk(" Coalesce Interrupt Delay = 0x%x\n",
3037 readl(&(tb->HostWrite.CoalIntDelay)));
3038 printk(" Coalesce Interrupt Count = 0x%x\n",
3039 readl(&(tb->HostWrite.CoalIntCount)));
3040 printk(" Max outstanding commands = 0x%d\n",
3041 readl(&(tb->CmdsOutMax)));
3042 printk(" Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
3043 for (i = 0; i < 16; i++)
3044 temp_name[i] = readb(&(tb->ServerName[i]));
3045 temp_name[16] = '\0';
3046 printk(" Server Name = %s\n", temp_name);
3047 printk(" Heartbeat Counter = 0x%x\n\n\n", readl(&(tb->HeartBeat)));
3049 #endif /* CCISS_DEBUG */
3051 static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
3053 int i, offset, mem_type, bar_type;
3054 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
3055 return 0;
3056 offset = 0;
3057 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3058 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
3059 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
3060 offset += 4;
3061 else {
3062 mem_type = pci_resource_flags(pdev, i) &
3063 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
3064 switch (mem_type) {
3065 case PCI_BASE_ADDRESS_MEM_TYPE_32:
3066 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
3067 offset += 4; /* 32 bit */
3068 break;
3069 case PCI_BASE_ADDRESS_MEM_TYPE_64:
3070 offset += 8;
3071 break;
3072 default: /* reserved in PCI 2.2 */
3073 printk(KERN_WARNING
3074 "Base address is invalid\n");
3075 return -1;
3076 break;
3079 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
3080 return i + 1;
3082 return -1;
3085 /* If MSI/MSI-X is supported by the kernel we will try to enable it on
3086 * controllers that are capable. If not, we use IO-APIC mode.
3089 static void __devinit cciss_interrupt_mode(ctlr_info_t *c,
3090 struct pci_dev *pdev, __u32 board_id)
3092 #ifdef CONFIG_PCI_MSI
3093 int err;
3094 struct msix_entry cciss_msix_entries[4] = { {0, 0}, {0, 1},
3095 {0, 2}, {0, 3}
3098 /* Some boards advertise MSI but don't really support it */
3099 if ((board_id == 0x40700E11) ||
3100 (board_id == 0x40800E11) ||
3101 (board_id == 0x40820E11) || (board_id == 0x40830E11))
3102 goto default_int_mode;
3104 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
3105 err = pci_enable_msix(pdev, cciss_msix_entries, 4);
3106 if (!err) {
3107 c->intr[0] = cciss_msix_entries[0].vector;
3108 c->intr[1] = cciss_msix_entries[1].vector;
3109 c->intr[2] = cciss_msix_entries[2].vector;
3110 c->intr[3] = cciss_msix_entries[3].vector;
3111 c->msix_vector = 1;
3112 return;
3114 if (err > 0) {
3115 printk(KERN_WARNING "cciss: only %d MSI-X vectors "
3116 "available\n", err);
3117 goto default_int_mode;
3118 } else {
3119 printk(KERN_WARNING "cciss: MSI-X init failed %d\n",
3120 err);
3121 goto default_int_mode;
3124 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
3125 if (!pci_enable_msi(pdev)) {
3126 c->msi_vector = 1;
3127 } else {
3128 printk(KERN_WARNING "cciss: MSI init failed\n");
3131 default_int_mode:
3132 #endif /* CONFIG_PCI_MSI */
3133 /* if we get here we're going to use the default interrupt mode */
3134 c->intr[SIMPLE_MODE_INT] = pdev->irq;
3135 return;
3138 static int __devinit cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
3140 ushort subsystem_vendor_id, subsystem_device_id, command;
3141 __u32 board_id, scratchpad = 0;
3142 __u64 cfg_offset;
3143 __u32 cfg_base_addr;
3144 __u64 cfg_base_addr_index;
3145 int i, err;
3147 /* check to see if controller has been disabled */
3148 /* BEFORE trying to enable it */
3149 (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
3150 if (!(command & 0x02)) {
3151 printk(KERN_WARNING
3152 "cciss: controller appears to be disabled\n");
3153 return -ENODEV;
3156 err = pci_enable_device(pdev);
3157 if (err) {
3158 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
3159 return err;
3162 err = pci_request_regions(pdev, "cciss");
3163 if (err) {
3164 printk(KERN_ERR "cciss: Cannot obtain PCI resources, "
3165 "aborting\n");
3166 return err;
3169 subsystem_vendor_id = pdev->subsystem_vendor;
3170 subsystem_device_id = pdev->subsystem_device;
3171 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
3172 subsystem_vendor_id);
3174 #ifdef CCISS_DEBUG
3175 printk("command = %x\n", command);
3176 printk("irq = %x\n", pdev->irq);
3177 printk("board_id = %x\n", board_id);
3178 #endif /* CCISS_DEBUG */
3180 /* If the kernel supports MSI/MSI-X we will try to enable that functionality,
3181 * else we use the IO-APIC interrupt assigned to us by system ROM.
3183 cciss_interrupt_mode(c, pdev, board_id);
3186 * Memory base addr is first addr , the second points to the config
3187 * table
3190 c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
3191 #ifdef CCISS_DEBUG
3192 printk("address 0 = %x\n", c->paddr);
3193 #endif /* CCISS_DEBUG */
3194 c->vaddr = remap_pci_mem(c->paddr, 0x250);
3196 /* Wait for the board to become ready. (PCI hotplug needs this.)
3197 * We poll for up to 120 secs, once per 100ms. */
3198 for (i = 0; i < 1200; i++) {
3199 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
3200 if (scratchpad == CCISS_FIRMWARE_READY)
3201 break;
3202 set_current_state(TASK_INTERRUPTIBLE);
3203 schedule_timeout(HZ / 10); /* wait 100ms */
3205 if (scratchpad != CCISS_FIRMWARE_READY) {
3206 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
3207 err = -ENODEV;
3208 goto err_out_free_res;
3211 /* get the address index number */
3212 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
3213 cfg_base_addr &= (__u32) 0x0000ffff;
3214 #ifdef CCISS_DEBUG
3215 printk("cfg base address = %x\n", cfg_base_addr);
3216 #endif /* CCISS_DEBUG */
3217 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
3218 #ifdef CCISS_DEBUG
3219 printk("cfg base address index = %x\n", cfg_base_addr_index);
3220 #endif /* CCISS_DEBUG */
3221 if (cfg_base_addr_index == -1) {
3222 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
3223 err = -ENODEV;
3224 goto err_out_free_res;
3227 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
3228 #ifdef CCISS_DEBUG
3229 printk("cfg offset = %x\n", cfg_offset);
3230 #endif /* CCISS_DEBUG */
3231 c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
3232 cfg_base_addr_index) +
3233 cfg_offset, sizeof(CfgTable_struct));
3234 c->board_id = board_id;
3236 #ifdef CCISS_DEBUG
3237 print_cfg_table(c->cfgtable);
3238 #endif /* CCISS_DEBUG */
3240 for (i = 0; i < ARRAY_SIZE(products); i++) {
3241 if (board_id == products[i].board_id) {
3242 c->product_name = products[i].product_name;
3243 c->access = *(products[i].access);
3244 c->nr_cmds = products[i].nr_cmds;
3245 break;
3248 if ((readb(&c->cfgtable->Signature[0]) != 'C') ||
3249 (readb(&c->cfgtable->Signature[1]) != 'I') ||
3250 (readb(&c->cfgtable->Signature[2]) != 'S') ||
3251 (readb(&c->cfgtable->Signature[3]) != 'S')) {
3252 printk("Does not appear to be a valid CISS config table\n");
3253 err = -ENODEV;
3254 goto err_out_free_res;
3256 /* We didn't find the controller in our list. We know the
3257 * signature is valid. If it's an HP device let's try to
3258 * bind to the device and fire it up. Otherwise we bail.
3260 if (i == ARRAY_SIZE(products)) {
3261 if (subsystem_vendor_id == PCI_VENDOR_ID_HP) {
3262 c->product_name = products[i-1].product_name;
3263 c->access = *(products[i-1].access);
3264 c->nr_cmds = products[i-1].nr_cmds;
3265 printk(KERN_WARNING "cciss: This is an unknown "
3266 "Smart Array controller.\n"
3267 "cciss: Please update to the latest driver "
3268 "available from www.hp.com.\n");
3269 } else {
3270 printk(KERN_WARNING "cciss: Sorry, I don't know how"
3271 " to access the Smart Array controller %08lx\n"
3272 , (unsigned long)board_id);
3273 err = -ENODEV;
3274 goto err_out_free_res;
3277 #ifdef CONFIG_X86
3279 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3280 __u32 prefetch;
3281 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
3282 prefetch |= 0x100;
3283 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
3285 #endif
3287 /* Disabling DMA prefetch and refetch for the P600.
3288 * An ASIC bug may result in accesses to invalid memory addresses.
3289 * We've disabled prefetch for some time now. Testing with XEN
3290 * kernels revealed a bug in the refetch if dom0 resides on a P600.
3292 if(board_id == 0x3225103C) {
3293 __u32 dma_prefetch;
3294 __u32 dma_refetch;
3295 dma_prefetch = readl(c->vaddr + I2O_DMA1_CFG);
3296 dma_prefetch |= 0x8000;
3297 writel(dma_prefetch, c->vaddr + I2O_DMA1_CFG);
3298 pci_read_config_dword(pdev, PCI_COMMAND_PARITY, &dma_refetch);
3299 dma_refetch |= 0x1;
3300 pci_write_config_dword(pdev, PCI_COMMAND_PARITY, dma_refetch);
3303 #ifdef CCISS_DEBUG
3304 printk("Trying to put board into Simple mode\n");
3305 #endif /* CCISS_DEBUG */
3306 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
3307 /* Update the field, and then ring the doorbell */
3308 writel(CFGTBL_Trans_Simple, &(c->cfgtable->HostWrite.TransportRequest));
3309 writel(CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
3311 /* under certain very rare conditions, this can take awhile.
3312 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3313 * as we enter this code.) */
3314 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
3315 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3316 break;
3317 /* delay and try again */
3318 set_current_state(TASK_INTERRUPTIBLE);
3319 schedule_timeout(10);
3322 #ifdef CCISS_DEBUG
3323 printk(KERN_DEBUG "I counter got to %d %x\n", i,
3324 readl(c->vaddr + SA5_DOORBELL));
3325 #endif /* CCISS_DEBUG */
3326 #ifdef CCISS_DEBUG
3327 print_cfg_table(c->cfgtable);
3328 #endif /* CCISS_DEBUG */
3330 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
3331 printk(KERN_WARNING "cciss: unable to get board into"
3332 " simple mode\n");
3333 err = -ENODEV;
3334 goto err_out_free_res;
3336 return 0;
3338 err_out_free_res:
3340 * Deliberately omit pci_disable_device(): it does something nasty to
3341 * Smart Array controllers that pci_enable_device does not undo
3343 pci_release_regions(pdev);
3344 return err;
3348 * Gets information about the local volumes attached to the controller.
3350 static void cciss_getgeometry(int cntl_num)
3352 ReportLunData_struct *ld_buff;
3353 InquiryData_struct *inq_buff;
3354 int return_code;
3355 int i;
3356 int listlength = 0;
3357 __u32 lunid = 0;
3358 unsigned block_size;
3359 sector_t total_size;
3361 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
3362 if (ld_buff == NULL) {
3363 printk(KERN_ERR "cciss: out of memory\n");
3364 return;
3366 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
3367 if (inq_buff == NULL) {
3368 printk(KERN_ERR "cciss: out of memory\n");
3369 kfree(ld_buff);
3370 return;
3372 /* Get the firmware version */
3373 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
3374 sizeof(InquiryData_struct), 0, 0, 0, NULL,
3375 TYPE_CMD);
3376 if (return_code == IO_OK) {
3377 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
3378 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
3379 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
3380 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
3381 } else { /* send command failed */
3383 printk(KERN_WARNING "cciss: unable to determine firmware"
3384 " version of controller\n");
3386 /* Get the number of logical volumes */
3387 return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
3388 sizeof(ReportLunData_struct), 0, 0, 0, NULL,
3389 TYPE_CMD);
3391 if (return_code == IO_OK) {
3392 #ifdef CCISS_DEBUG
3393 printk("LUN Data\n--------------------------\n");
3394 #endif /* CCISS_DEBUG */
3396 listlength |=
3397 (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
3398 listlength |=
3399 (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
3400 listlength |=
3401 (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
3402 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
3403 } else { /* reading number of logical volumes failed */
3405 printk(KERN_WARNING "cciss: report logical volume"
3406 " command failed\n");
3407 listlength = 0;
3409 hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
3410 if (hba[cntl_num]->num_luns > CISS_MAX_LUN) {
3411 printk(KERN_ERR
3412 "ciss: only %d number of logical volumes supported\n",
3413 CISS_MAX_LUN);
3414 hba[cntl_num]->num_luns = CISS_MAX_LUN;
3416 #ifdef CCISS_DEBUG
3417 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n",
3418 ld_buff->LUNListLength[0], ld_buff->LUNListLength[1],
3419 ld_buff->LUNListLength[2], ld_buff->LUNListLength[3],
3420 hba[cntl_num]->num_luns);
3421 #endif /* CCISS_DEBUG */
3423 hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns - 1;
3424 for (i = 0; i < CISS_MAX_LUN; i++) {
3425 if (i < hba[cntl_num]->num_luns) {
3426 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3]))
3427 << 24;
3428 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2]))
3429 << 16;
3430 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1]))
3431 << 8;
3432 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
3434 hba[cntl_num]->drv[i].LunID = lunid;
3436 #ifdef CCISS_DEBUG
3437 printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
3438 ld_buff->LUN[i][0], ld_buff->LUN[i][1],
3439 ld_buff->LUN[i][2], ld_buff->LUN[i][3],
3440 hba[cntl_num]->drv[i].LunID);
3441 #endif /* CCISS_DEBUG */
3443 /* testing to see if 16-byte CDBs are already being used */
3444 if(hba[cntl_num]->cciss_read == CCISS_READ_16) {
3445 cciss_read_capacity_16(cntl_num, i, 0,
3446 &total_size, &block_size);
3447 goto geo_inq;
3449 cciss_read_capacity(cntl_num, i, 0, &total_size, &block_size);
3451 /* If read_capacity returns all F's the logical is >2TB */
3452 /* so we switch to 16-byte CDBs for all read/write ops */
3453 if(total_size == 0xFFFFFFFFULL) {
3454 cciss_read_capacity_16(cntl_num, i, 0,
3455 &total_size, &block_size);
3456 hba[cntl_num]->cciss_read = CCISS_READ_16;
3457 hba[cntl_num]->cciss_write = CCISS_WRITE_16;
3458 } else {
3459 hba[cntl_num]->cciss_read = CCISS_READ_10;
3460 hba[cntl_num]->cciss_write = CCISS_WRITE_10;
3462 geo_inq:
3463 cciss_geometry_inquiry(cntl_num, i, 0, total_size,
3464 block_size, inq_buff,
3465 &hba[cntl_num]->drv[i]);
3466 } else {
3467 /* initialize raid_level to indicate a free space */
3468 hba[cntl_num]->drv[i].raid_level = -1;
3471 kfree(ld_buff);
3472 kfree(inq_buff);
3475 /* Function to find the first free pointer into our hba[] array */
3476 /* Returns -1 if no free entries are left. */
3477 static int alloc_cciss_hba(void)
3479 int i;
3481 for (i = 0; i < MAX_CTLR; i++) {
3482 if (!hba[i]) {
3483 ctlr_info_t *p;
3485 p = kzalloc(sizeof(ctlr_info_t), GFP_KERNEL);
3486 if (!p)
3487 goto Enomem;
3488 p->gendisk[0] = alloc_disk(1 << NWD_SHIFT);
3489 if (!p->gendisk[0]) {
3490 kfree(p);
3491 goto Enomem;
3493 hba[i] = p;
3494 return i;
3497 printk(KERN_WARNING "cciss: This driver supports a maximum"
3498 " of %d controllers.\n", MAX_CTLR);
3499 return -1;
3500 Enomem:
3501 printk(KERN_ERR "cciss: out of memory.\n");
3502 return -1;
3505 static void free_hba(int i)
3507 ctlr_info_t *p = hba[i];
3508 int n;
3510 hba[i] = NULL;
3511 for (n = 0; n < CISS_MAX_LUN; n++)
3512 put_disk(p->gendisk[n]);
3513 kfree(p);
3517 * This is it. Find all the controllers and register them. I really hate
3518 * stealing all these major device numbers.
3519 * returns the number of block devices registered.
3521 static int __devinit cciss_init_one(struct pci_dev *pdev,
3522 const struct pci_device_id *ent)
3524 int i;
3525 int j = 0;
3526 int rc;
3527 int dac;
3529 i = alloc_cciss_hba();
3530 if (i < 0)
3531 return -1;
3533 hba[i]->busy_initializing = 1;
3535 if (cciss_pci_init(hba[i], pdev) != 0)
3536 goto clean1;
3538 sprintf(hba[i]->devname, "cciss%d", i);
3539 hba[i]->ctlr = i;
3540 hba[i]->pdev = pdev;
3542 /* configure PCI DMA stuff */
3543 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK))
3544 dac = 1;
3545 else if (!pci_set_dma_mask(pdev, DMA_32BIT_MASK))
3546 dac = 0;
3547 else {
3548 printk(KERN_ERR "cciss: no suitable DMA available\n");
3549 goto clean1;
3553 * register with the major number, or get a dynamic major number
3554 * by passing 0 as argument. This is done for greater than
3555 * 8 controller support.
3557 if (i < MAX_CTLR_ORIG)
3558 hba[i]->major = COMPAQ_CISS_MAJOR + i;
3559 rc = register_blkdev(hba[i]->major, hba[i]->devname);
3560 if (rc == -EBUSY || rc == -EINVAL) {
3561 printk(KERN_ERR
3562 "cciss: Unable to get major number %d for %s "
3563 "on hba %d\n", hba[i]->major, hba[i]->devname, i);
3564 goto clean1;
3565 } else {
3566 if (i >= MAX_CTLR_ORIG)
3567 hba[i]->major = rc;
3570 /* make sure the board interrupts are off */
3571 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
3572 if (request_irq(hba[i]->intr[SIMPLE_MODE_INT], do_cciss_intr,
3573 IRQF_DISABLED | IRQF_SHARED, hba[i]->devname, hba[i])) {
3574 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
3575 hba[i]->intr[SIMPLE_MODE_INT], hba[i]->devname);
3576 goto clean2;
3579 printk(KERN_INFO "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
3580 hba[i]->devname, pdev->device, pci_name(pdev),
3581 hba[i]->intr[SIMPLE_MODE_INT], dac ? "" : " not");
3583 hba[i]->cmd_pool_bits =
3584 kmalloc(((hba[i]->nr_cmds + BITS_PER_LONG -
3585 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
3586 hba[i]->cmd_pool = (CommandList_struct *)
3587 pci_alloc_consistent(hba[i]->pdev,
3588 hba[i]->nr_cmds * sizeof(CommandList_struct),
3589 &(hba[i]->cmd_pool_dhandle));
3590 hba[i]->errinfo_pool = (ErrorInfo_struct *)
3591 pci_alloc_consistent(hba[i]->pdev,
3592 hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
3593 &(hba[i]->errinfo_pool_dhandle));
3594 if ((hba[i]->cmd_pool_bits == NULL)
3595 || (hba[i]->cmd_pool == NULL)
3596 || (hba[i]->errinfo_pool == NULL)) {
3597 printk(KERN_ERR "cciss: out of memory");
3598 goto clean4;
3600 #ifdef CONFIG_CISS_SCSI_TAPE
3601 hba[i]->scsi_rejects.complete =
3602 kmalloc(sizeof(hba[i]->scsi_rejects.complete[0]) *
3603 (hba[i]->nr_cmds + 5), GFP_KERNEL);
3604 if (hba[i]->scsi_rejects.complete == NULL) {
3605 printk(KERN_ERR "cciss: out of memory");
3606 goto clean4;
3608 #endif
3609 spin_lock_init(&hba[i]->lock);
3611 /* Initialize the pdev driver private data.
3612 have it point to hba[i]. */
3613 pci_set_drvdata(pdev, hba[i]);
3614 /* command and error info recs zeroed out before
3615 they are used */
3616 memset(hba[i]->cmd_pool_bits, 0,
3617 ((hba[i]->nr_cmds + BITS_PER_LONG -
3618 1) / BITS_PER_LONG) * sizeof(unsigned long));
3620 #ifdef CCISS_DEBUG
3621 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n", i);
3622 #endif /* CCISS_DEBUG */
3624 cciss_getgeometry(i);
3626 cciss_scsi_setup(i);
3628 /* Turn the interrupts on so we can service requests */
3629 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
3631 cciss_procinit(i);
3633 hba[i]->cciss_max_sectors = 2048;
3635 hba[i]->busy_initializing = 0;
3637 do {
3638 drive_info_struct *drv = &(hba[i]->drv[j]);
3639 struct gendisk *disk = hba[i]->gendisk[j];
3640 struct request_queue *q;
3642 /* Check if the disk was allocated already */
3643 if (!disk){
3644 hba[i]->gendisk[j] = alloc_disk(1 << NWD_SHIFT);
3645 disk = hba[i]->gendisk[j];
3648 /* Check that the disk was able to be allocated */
3649 if (!disk) {
3650 printk(KERN_ERR "cciss: unable to allocate memory for disk %d\n", j);
3651 goto clean4;
3654 q = blk_init_queue(do_cciss_request, &hba[i]->lock);
3655 if (!q) {
3656 printk(KERN_ERR
3657 "cciss: unable to allocate queue for disk %d\n",
3659 goto clean4;
3661 drv->queue = q;
3663 <<<<<<< HEAD:drivers/block/cciss.c
3664 q->backing_dev_info.ra_pages = READ_AHEAD;
3665 =======
3666 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/cciss.c
3667 blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
3669 /* This is a hardware imposed limit. */
3670 blk_queue_max_hw_segments(q, MAXSGENTRIES);
3672 /* This is a limit in the driver and could be eliminated. */
3673 blk_queue_max_phys_segments(q, MAXSGENTRIES);
3675 blk_queue_max_sectors(q, hba[i]->cciss_max_sectors);
3677 blk_queue_softirq_done(q, cciss_softirq_done);
3679 q->queuedata = hba[i];
3680 sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
3681 disk->major = hba[i]->major;
3682 disk->first_minor = j << NWD_SHIFT;
3683 disk->fops = &cciss_fops;
3684 disk->queue = q;
3685 disk->private_data = drv;
3686 disk->driverfs_dev = &pdev->dev;
3687 /* we must register the controller even if no disks exist */
3688 /* this is for the online array utilities */
3689 if (!drv->heads && j)
3690 continue;
3691 blk_queue_hardsect_size(q, drv->block_size);
3692 set_capacity(disk, drv->nr_blocks);
3693 add_disk(disk);
3694 j++;
3695 } while (j <= hba[i]->highest_lun);
3697 return 1;
3699 clean4:
3700 #ifdef CONFIG_CISS_SCSI_TAPE
3701 kfree(hba[i]->scsi_rejects.complete);
3702 #endif
3703 kfree(hba[i]->cmd_pool_bits);
3704 if (hba[i]->cmd_pool)
3705 pci_free_consistent(hba[i]->pdev,
3706 hba[i]->nr_cmds * sizeof(CommandList_struct),
3707 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
3708 if (hba[i]->errinfo_pool)
3709 pci_free_consistent(hba[i]->pdev,
3710 hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
3711 hba[i]->errinfo_pool,
3712 hba[i]->errinfo_pool_dhandle);
3713 free_irq(hba[i]->intr[SIMPLE_MODE_INT], hba[i]);
3714 clean2:
3715 unregister_blkdev(hba[i]->major, hba[i]->devname);
3716 clean1:
3717 hba[i]->busy_initializing = 0;
3718 /* cleanup any queues that may have been initialized */
3719 for (j=0; j <= hba[i]->highest_lun; j++){
3720 drive_info_struct *drv = &(hba[i]->drv[j]);
3721 if (drv->queue)
3722 blk_cleanup_queue(drv->queue);
3725 * Deliberately omit pci_disable_device(): it does something nasty to
3726 * Smart Array controllers that pci_enable_device does not undo
3728 pci_release_regions(pdev);
3729 pci_set_drvdata(pdev, NULL);
3730 free_hba(i);
3731 return -1;
3734 static void cciss_shutdown(struct pci_dev *pdev)
3736 ctlr_info_t *tmp_ptr;
3737 int i;
3738 char flush_buf[4];
3739 int return_code;
3741 tmp_ptr = pci_get_drvdata(pdev);
3742 if (tmp_ptr == NULL)
3743 return;
3744 i = tmp_ptr->ctlr;
3745 if (hba[i] == NULL)
3746 return;
3748 /* Turn board interrupts off and send the flush cache command */
3749 /* sendcmd will turn off interrupt, and send the flush...
3750 * To write all data in the battery backed cache to disks */
3751 memset(flush_buf, 0, 4);
3752 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
3753 TYPE_CMD);
3754 if (return_code == IO_OK) {
3755 printk(KERN_INFO "Completed flushing cache on controller %d\n", i);
3756 } else {
3757 printk(KERN_WARNING "Error flushing cache on controller %d\n", i);
3759 free_irq(hba[i]->intr[2], hba[i]);
3762 static void __devexit cciss_remove_one(struct pci_dev *pdev)
3764 ctlr_info_t *tmp_ptr;
3765 int i, j;
3767 if (pci_get_drvdata(pdev) == NULL) {
3768 printk(KERN_ERR "cciss: Unable to remove device \n");
3769 return;
3771 tmp_ptr = pci_get_drvdata(pdev);
3772 i = tmp_ptr->ctlr;
3773 if (hba[i] == NULL) {
3774 printk(KERN_ERR "cciss: device appears to "
3775 "already be removed \n");
3776 return;
3779 remove_proc_entry(hba[i]->devname, proc_cciss);
3780 unregister_blkdev(hba[i]->major, hba[i]->devname);
3782 /* remove it from the disk list */
3783 for (j = 0; j < CISS_MAX_LUN; j++) {
3784 struct gendisk *disk = hba[i]->gendisk[j];
3785 if (disk) {
3786 struct request_queue *q = disk->queue;
3788 if (disk->flags & GENHD_FL_UP)
3789 del_gendisk(disk);
3790 if (q)
3791 blk_cleanup_queue(q);
3795 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
3797 cciss_shutdown(pdev);
3799 #ifdef CONFIG_PCI_MSI
3800 if (hba[i]->msix_vector)
3801 pci_disable_msix(hba[i]->pdev);
3802 else if (hba[i]->msi_vector)
3803 pci_disable_msi(hba[i]->pdev);
3804 #endif /* CONFIG_PCI_MSI */
3806 iounmap(hba[i]->vaddr);
3808 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(CommandList_struct),
3809 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
3810 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
3811 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
3812 kfree(hba[i]->cmd_pool_bits);
3813 #ifdef CONFIG_CISS_SCSI_TAPE
3814 kfree(hba[i]->scsi_rejects.complete);
3815 #endif
3817 * Deliberately omit pci_disable_device(): it does something nasty to
3818 * Smart Array controllers that pci_enable_device does not undo
3820 pci_release_regions(pdev);
3821 pci_set_drvdata(pdev, NULL);
3822 free_hba(i);
3825 static struct pci_driver cciss_pci_driver = {
3826 .name = "cciss",
3827 .probe = cciss_init_one,
3828 .remove = __devexit_p(cciss_remove_one),
3829 .id_table = cciss_pci_device_id, /* id_table */
3830 .shutdown = cciss_shutdown,
3834 * This is it. Register the PCI driver information for the cards we control
3835 * the OS will call our registered routines when it finds one of our cards.
3837 static int __init cciss_init(void)
3839 printk(KERN_INFO DRIVER_NAME "\n");
3841 /* Register for our PCI devices */
3842 return pci_register_driver(&cciss_pci_driver);
3845 static void __exit cciss_cleanup(void)
3847 int i;
3849 pci_unregister_driver(&cciss_pci_driver);
3850 /* double check that all controller entrys have been removed */
3851 for (i = 0; i < MAX_CTLR; i++) {
3852 if (hba[i] != NULL) {
3853 printk(KERN_WARNING "cciss: had to remove"
3854 " controller %d\n", i);
3855 cciss_remove_one(hba[i]->pdev);
3858 remove_proc_entry("cciss", proc_root_driver);
3861 static void fail_all_cmds(unsigned long ctlr)
3863 /* If we get here, the board is apparently dead. */
3864 ctlr_info_t *h = hba[ctlr];
3865 CommandList_struct *c;
3866 unsigned long flags;
3868 printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
3869 h->alive = 0; /* the controller apparently died... */
3871 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
3873 pci_disable_device(h->pdev); /* Make sure it is really dead. */
3875 /* move everything off the request queue onto the completed queue */
3876 while ((c = h->reqQ) != NULL) {
3877 removeQ(&(h->reqQ), c);
3878 h->Qdepth--;
3879 addQ(&(h->cmpQ), c);
3882 /* Now, fail everything on the completed queue with a HW error */
3883 while ((c = h->cmpQ) != NULL) {
3884 removeQ(&h->cmpQ, c);
3885 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
3886 if (c->cmd_type == CMD_RWREQ) {
3887 complete_command(h, c, 0);
3888 } else if (c->cmd_type == CMD_IOCTL_PEND)
3889 complete(c->waiting);
3890 #ifdef CONFIG_CISS_SCSI_TAPE
3891 else if (c->cmd_type == CMD_SCSI)
3892 complete_scsi_command(c, 0, 0);
3893 #endif
3895 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
3896 return;
3899 module_init(cciss_init);
3900 module_exit(cciss_cleanup);