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