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