blob: efcc908490df9fd28c877d9eee16947474facc08 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Disk Array driver for HP SA 5xxx and 6xxx Controllers
Mike Millerfb86a352006-01-08 01:03:50 -08003 * Copyright 2000, 2006 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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>
Bjorn Helgaas7c832832006-06-25 05:49:06 -070036#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/hdreg.h>
38#include <linux/spinlock.h>
39#include <linux/compat.h>
Jens Axboe2056a782006-03-23 20:00:26 +010040#include <linux/blktrace_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/uaccess.h>
42#include <asm/io.h>
43
mike.miller@hp.comeb0df992005-06-10 14:51:04 -050044#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/blkdev.h>
46#include <linux/genhd.h>
47#include <linux/completion.h>
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -070048#include <scsi/sg.h>
49#include <scsi/scsi_ioctl.h>
50#include <linux/cdrom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
Mike Miller9d827c92006-12-06 20:34:58 -080053#define DRIVER_NAME "HP CISS Driver (v 3.6.14)"
54#define DRIVER_VERSION CCISS_DRIVER_VERSION(3,6,14)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* Embedded module documentation macros - see modules.h */
57MODULE_AUTHOR("Hewlett-Packard Company");
Mike Miller9d827c92006-12-06 20:34:58 -080058MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 3.6.14");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
Mike Miller1883c5a2006-09-12 20:36:07 -070060 " SA6i P600 P800 P400 P400i E200 E200i E500");
Mike Miller9d827c92006-12-06 20:34:58 -080061MODULE_VERSION("3.6.14");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062MODULE_LICENSE("GPL");
63
64#include "cciss_cmd.h"
65#include "cciss.h"
66#include <linux/cciss_ioctl.h>
67
68/* define the PCI info for the cards we can control */
69static const struct pci_device_id cciss_pci_device_id[] = {
Bjorn Helgaasf82ccdb2006-06-25 05:49:07 -070070 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS, 0x0E11, 0x4070},
71 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4080},
72 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4082},
73 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4083},
74 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x4091},
75 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409A},
76 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409B},
77 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409C},
78 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409D},
79 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA, 0x103C, 0x3225},
80 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3223},
81 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3234},
82 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3235},
83 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3211},
84 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3212},
85 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3213},
86 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3214},
87 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3215},
Mike Millerde923912006-12-06 20:35:03 -080088 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3237},
Mike Miller4ff9a9a2006-12-06 20:35:00 -080089 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
90 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 {0,}
92};
Bjorn Helgaas7c832832006-06-25 05:49:06 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/* board_id = Subsystem Device ID & Vendor ID
97 * product = Marketing Name for the board
Bjorn Helgaas7c832832006-06-25 05:49:06 -070098 * access = Address of the struct of function pointers
Mike Millerf8806322006-12-06 20:35:01 -080099 * nr_cmds = Number of commands supported by controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101static struct board_type products[] = {
Mike Millerf8806322006-12-06 20:35:01 -0800102 {0x40700E11, "Smart Array 5300", &SA5_access, 512},
103 {0x40800E11, "Smart Array 5i", &SA5B_access, 512},
104 {0x40820E11, "Smart Array 532", &SA5B_access, 512},
105 {0x40830E11, "Smart Array 5312", &SA5B_access, 512},
106 {0x409A0E11, "Smart Array 641", &SA5_access, 512},
107 {0x409B0E11, "Smart Array 642", &SA5_access, 512},
108 {0x409C0E11, "Smart Array 6400", &SA5_access, 512},
109 {0x409D0E11, "Smart Array 6400 EM", &SA5_access, 512},
110 {0x40910E11, "Smart Array 6i", &SA5_access, 512},
111 {0x3225103C, "Smart Array P600", &SA5_access, 512},
112 {0x3223103C, "Smart Array P800", &SA5_access, 512},
113 {0x3234103C, "Smart Array P400", &SA5_access, 512},
114 {0x3235103C, "Smart Array P400i", &SA5_access, 512},
115 {0x3211103C, "Smart Array E200i", &SA5_access, 120},
116 {0x3212103C, "Smart Array E200", &SA5_access, 120},
117 {0x3213103C, "Smart Array E200i", &SA5_access, 120},
118 {0x3214103C, "Smart Array E200i", &SA5_access, 120},
119 {0x3215103C, "Smart Array E200i", &SA5_access, 120},
Mike Millerde923912006-12-06 20:35:03 -0800120 {0x3237103C, "Smart Array E500", &SA5_access, 512},
Mike Millerf8806322006-12-06 20:35:01 -0800121 {0xFFFF103C, "Unknown Smart Array", &SA5_access, 120},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -0700124/* How long to wait (in milliseconds) for board to go into simple mode */
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700125#define MAX_CONFIG_WAIT 30000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define MAX_IOCTL_CONFIG_WAIT 1000
127
128/*define how many times we will try a command because of bus resets */
129#define MAX_CMD_RETRIES 3
130
131#define READ_AHEAD 1024
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#define MAX_CTLR 32
133
134/* Originally cciss driver only supports 8 major numbers */
135#define MAX_CTLR_ORIG 8
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static ctlr_info_t *hba[MAX_CTLR];
138
139static void do_cciss_request(request_queue_t *q);
David Howells7d12e782006-10-05 14:55:46 +0100140static irqreturn_t do_cciss_intr(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static int cciss_open(struct inode *inode, struct file *filep);
142static int cciss_release(struct inode *inode, struct file *filep);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700143static int cciss_ioctl(struct inode *inode, struct file *filep,
144 unsigned int cmd, unsigned long arg);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800145static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static int cciss_revalidate(struct gendisk *disk);
Mike Millerddd47442005-09-13 01:25:22 -0700148static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700149static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
150 int clear_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -0700152static void cciss_read_capacity(int ctlr, int logvol, int withirq,
153 sector_t *total_size, unsigned int *block_size);
154static void cciss_read_capacity_16(int ctlr, int logvol, int withirq,
155 sector_t *total_size, unsigned int *block_size);
156static void cciss_geometry_inquiry(int ctlr, int logvol,
157 int withirq, sector_t total_size,
158 unsigned int block_size, InquiryData_struct *inq_buff,
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700159 drive_info_struct *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static void cciss_getgeometry(int cntl_num);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700161static void __devinit cciss_interrupt_mode(ctlr_info_t *, struct pci_dev *,
162 __u32);
163static void start_io(ctlr_info_t *h);
164static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size,
165 unsigned int use_unit_num, unsigned int log_unit,
166 __u8 page_code, unsigned char *scsi3addr, int cmd_type);
167static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
168 unsigned int use_unit_num, unsigned int log_unit,
169 __u8 page_code, int cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Mike Miller33079b22005-09-13 01:25:22 -0700171static void fail_all_cmds(unsigned long ctlr);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#ifdef CONFIG_PROC_FS
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700174static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
175 int length, int *eof, void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static void cciss_procinit(int i);
177#else
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700178static void cciss_procinit(int i)
179{
180}
181#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183#ifdef CONFIG_COMPAT
184static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg);
185#endif
186
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700187static struct block_device_operations cciss_fops = {
188 .owner = THIS_MODULE,
189 .open = cciss_open,
190 .release = cciss_release,
191 .ioctl = cciss_ioctl,
192 .getgeo = cciss_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#ifdef CONFIG_COMPAT
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700194 .compat_ioctl = cciss_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700196 .revalidate_disk = cciss_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
199/*
200 * Enqueuing and dequeuing functions for cmdlists.
201 */
202static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
203{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700204 if (*Qptr == NULL) {
205 *Qptr = c;
206 c->next = c->prev = c;
207 } else {
208 c->prev = (*Qptr)->prev;
209 c->next = (*Qptr);
210 (*Qptr)->prev->next = c;
211 (*Qptr)->prev = c;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700215static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
216 CommandList_struct *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700218 if (c && c->next != c) {
219 if (*Qptr == c)
220 *Qptr = c->next;
221 c->prev->next = c->next;
222 c->next->prev = c->prev;
223 } else {
224 *Qptr = NULL;
225 }
226 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229#include "cciss_scsi.c" /* For SCSI tape support */
230
Randy Dunlap0f5486e2006-12-29 16:48:31 -0800231#define RAID_UNKNOWN 6
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#ifdef CONFIG_PROC_FS
234
235/*
236 * Report information about this controller.
237 */
238#define ENG_GIG 1000000000
239#define ENG_GIG_FACTOR (ENG_GIG/512)
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700240static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
241 "UNKNOWN"
242};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244static struct proc_dir_entry *proc_cciss;
245
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700246static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
247 int length, int *eof, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700249 off_t pos = 0;
250 off_t len = 0;
251 int size, i, ctlr;
252 ctlr_info_t *h = (ctlr_info_t *) data;
253 drive_info_struct *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned long flags;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700255 sector_t vol_sz, vol_sz_frac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700257 ctlr = h->ctlr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* prevent displaying bogus info during configuration
260 * or deconfiguration of a logical volume
261 */
262 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
263 if (h->busy_configuring) {
264 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700265 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 h->busy_configuring = 1;
268 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
269
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700270 size = sprintf(buffer, "%s: HP %s Controller\n"
271 "Board ID: 0x%08lx\n"
272 "Firmware Version: %c%c%c%c\n"
273 "IRQ: %d\n"
274 "Logical drives: %d\n"
Mike Miller92c4231a2006-12-06 20:35:06 -0800275 "Max sectors: %d\n"
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700276 "Current Q depth: %d\n"
277 "Current # commands on controller: %d\n"
278 "Max Q depth since init: %d\n"
279 "Max # commands on controller since init: %d\n"
280 "Max SG entries since init: %d\n\n",
281 h->devname,
282 h->product_name,
283 (unsigned long)h->board_id,
284 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
285 h->firm_ver[3], (unsigned int)h->intr[SIMPLE_MODE_INT],
Mike Miller92c4231a2006-12-06 20:35:06 -0800286 h->num_luns,
287 h->cciss_max_sectors,
288 h->Qdepth, h->commands_outstanding,
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700289 h->maxQsinceinit, h->max_outstanding, h->maxSG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700291 pos += size;
292 len += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 cciss_proc_tape_report(ctlr, buffer, &pos, &len);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700294 for (i = 0; i <= h->highest_lun; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700296 drv = &h->drv[i];
Mike Millerddd47442005-09-13 01:25:22 -0700297 if (drv->heads == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 continue;
299
300 vol_sz = drv->nr_blocks;
301 vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
302 vol_sz_frac *= 100;
303 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
304
305 if (drv->raid_level > 5)
306 drv->raid_level = RAID_UNKNOWN;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700307 size = sprintf(buffer + len, "cciss/c%dd%d:"
308 "\t%4u.%02uGB\tRAID %s\n",
309 ctlr, i, (int)vol_sz, (int)vol_sz_frac,
310 raid_label[drv->raid_level]);
311 pos += size;
312 len += size;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700315 *eof = 1;
316 *start = buffer + offset;
317 len -= offset;
318 if (len > length)
319 len = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 h->busy_configuring = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700321 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700324static int
325cciss_proc_write(struct file *file, const char __user *buffer,
326 unsigned long count, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 unsigned char cmd[80];
329 int len;
330#ifdef CONFIG_CISS_SCSI_TAPE
331 ctlr_info_t *h = (ctlr_info_t *) data;
332 int rc;
333#endif
334
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700335 if (count > sizeof(cmd) - 1)
336 return -EINVAL;
337 if (copy_from_user(cmd, buffer, count))
338 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 cmd[count] = '\0';
340 len = strlen(cmd); // above 3 lines ensure safety
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700341 if (len && cmd[len - 1] == '\n')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 cmd[--len] = '\0';
343# ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700344 if (strcmp("engage scsi", cmd) == 0) {
345 rc = cciss_engage_scsi(h->ctlr);
346 if (rc != 0)
347 return -rc;
348 return count;
349 }
350 /* might be nice to have "disengage" too, but it's not
351 safely possible. (only 1 module use count, lock issues.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352# endif
353 return -EINVAL;
354}
355
356/*
357 * Get us a file in /proc/cciss that says something about each controller.
358 * Create /proc/cciss if it doesn't exist yet.
359 */
360static void __devinit cciss_procinit(int i)
361{
362 struct proc_dir_entry *pde;
363
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700364 if (proc_cciss == NULL) {
365 proc_cciss = proc_mkdir("cciss", proc_root_driver);
366 if (!proc_cciss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700370 pde = create_proc_read_entry(hba[i]->devname,
371 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
372 proc_cciss, cciss_proc_get_info, hba[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 pde->write_proc = cciss_proc_write;
374}
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700375#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700377/*
378 * For operations that cannot sleep, a command block is allocated at init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700380 * which ones are free or in use. For operations that can wait for kmalloc
381 * to possible sleep, this routine can be called with get_from_pool set to 0.
382 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
383 */
384static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 CommandList_struct *c;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700387 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 u64bit temp64;
389 dma_addr_t cmd_dma_handle, err_dma_handle;
390
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700391 if (!get_from_pool) {
392 c = (CommandList_struct *) pci_alloc_consistent(h->pdev,
393 sizeof(CommandList_struct), &cmd_dma_handle);
394 if (c == NULL)
395 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 memset(c, 0, sizeof(CommandList_struct));
397
Mike Miller33079b22005-09-13 01:25:22 -0700398 c->cmdindex = -1;
399
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700400 c->err_info = (ErrorInfo_struct *)
401 pci_alloc_consistent(h->pdev, sizeof(ErrorInfo_struct),
402 &err_dma_handle);
403
404 if (c->err_info == NULL) {
405 pci_free_consistent(h->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 sizeof(CommandList_struct), c, cmd_dma_handle);
407 return NULL;
408 }
409 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700410 } else { /* get it out of the controllers pool */
411
412 do {
Mike Millerf8806322006-12-06 20:35:01 -0800413 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
414 if (i == h->nr_cmds)
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700415 return NULL;
416 } while (test_and_set_bit
417 (i & (BITS_PER_LONG - 1),
418 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#ifdef CCISS_DEBUG
420 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
421#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700422 c = h->cmd_pool + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 memset(c, 0, sizeof(CommandList_struct));
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700424 cmd_dma_handle = h->cmd_pool_dhandle
425 + i * sizeof(CommandList_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 c->err_info = h->errinfo_pool + i;
427 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700428 err_dma_handle = h->errinfo_pool_dhandle
429 + i * sizeof(ErrorInfo_struct);
430 h->nr_allocs++;
Mike Miller33079b22005-09-13 01:25:22 -0700431
432 c->cmdindex = i;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 c->busaddr = (__u32) cmd_dma_handle;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700436 temp64.val = (__u64) err_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 c->ErrDesc.Addr.lower = temp64.val32.lower;
438 c->ErrDesc.Addr.upper = temp64.val32.upper;
439 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 c->ctlr = h->ctlr;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700442 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700445/*
446 * Frees a command block that was previously allocated with cmd_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
448static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
449{
450 int i;
451 u64bit temp64;
452
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700453 if (!got_from_pool) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 temp64.val32.lower = c->ErrDesc.Addr.lower;
455 temp64.val32.upper = c->ErrDesc.Addr.upper;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700456 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
457 c->err_info, (dma_addr_t) temp64.val);
458 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
459 c, (dma_addr_t) c->busaddr);
460 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 i = c - h->cmd_pool;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700462 clear_bit(i & (BITS_PER_LONG - 1),
463 h->cmd_pool_bits + (i / BITS_PER_LONG));
464 h->nr_frees++;
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468static inline ctlr_info_t *get_host(struct gendisk *disk)
469{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700470 return disk->queue->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473static inline drive_info_struct *get_drv(struct gendisk *disk)
474{
475 return disk->private_data;
476}
477
478/*
479 * Open. Make sure the device is really there.
480 */
481static int cciss_open(struct inode *inode, struct file *filep)
482{
483 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
484 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
485
486#ifdef CCISS_DEBUG
487 printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700488#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Mike Millerddd47442005-09-13 01:25:22 -0700490 if (host->busy_initializing || drv->busy_configuring)
491 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /*
493 * Root is allowed to open raw volume zero even if it's not configured
494 * so array config can still work. Root is also allowed to open any
495 * volume that has a LUN ID, so it can issue IOCTL to reread the
496 * disk information. I don't think I really like this
497 * but I'm already using way to many device nodes to claim another one
498 * for "raw controller".
499 */
Mike Miller7a06f782006-12-06 20:35:08 -0800500 if (drv->heads == 0) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700501 if (iminor(inode) != 0) { /* not node 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* if not node 0 make sure it is a partition = 0 */
503 if (iminor(inode) & 0x0f) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700504 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* if it is, make sure we have a LUN ID */
506 } else if (drv->LunID == 0) {
507 return -ENXIO;
508 }
509 }
510 if (!capable(CAP_SYS_ADMIN))
511 return -EPERM;
512 }
513 drv->usage_count++;
514 host->usage_count++;
515 return 0;
516}
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/*
519 * Close. Sync first.
520 */
521static int cciss_release(struct inode *inode, struct file *filep)
522{
523 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
524 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
525
526#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700527 printk(KERN_DEBUG "cciss_release %s\n",
528 inode->i_bdev->bd_disk->disk_name);
529#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 drv->usage_count--;
532 host->usage_count--;
533 return 0;
534}
535
536#ifdef CONFIG_COMPAT
537
538static int do_ioctl(struct file *f, unsigned cmd, unsigned long arg)
539{
540 int ret;
541 lock_kernel();
Josef Sipek6c648be2006-12-08 02:36:55 -0800542 ret = cciss_ioctl(f->f_path.dentry->d_inode, f, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 unlock_kernel();
544 return ret;
545}
546
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700547static int cciss_ioctl32_passthru(struct file *f, unsigned cmd,
548 unsigned long arg);
549static int cciss_ioctl32_big_passthru(struct file *f, unsigned cmd,
550 unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg)
553{
554 switch (cmd) {
555 case CCISS_GETPCIINFO:
556 case CCISS_GETINTINFO:
557 case CCISS_SETINTINFO:
558 case CCISS_GETNODENAME:
559 case CCISS_SETNODENAME:
560 case CCISS_GETHEARTBEAT:
561 case CCISS_GETBUSTYPES:
562 case CCISS_GETFIRMVER:
563 case CCISS_GETDRIVVER:
564 case CCISS_REVALIDVOLS:
565 case CCISS_DEREGDISK:
566 case CCISS_REGNEWDISK:
567 case CCISS_REGNEWD:
568 case CCISS_RESCANDISK:
569 case CCISS_GETLUNINFO:
570 return do_ioctl(f, cmd, arg);
571
572 case CCISS_PASSTHRU32:
573 return cciss_ioctl32_passthru(f, cmd, arg);
574 case CCISS_BIG_PASSTHRU32:
575 return cciss_ioctl32_big_passthru(f, cmd, arg);
576
577 default:
578 return -ENOIOCTLCMD;
579 }
580}
581
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700582static int cciss_ioctl32_passthru(struct file *f, unsigned cmd,
583 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 IOCTL32_Command_struct __user *arg32 =
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700586 (IOCTL32_Command_struct __user *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 IOCTL_Command_struct arg64;
588 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
589 int err;
590 u32 cp;
591
592 err = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700593 err |=
594 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
595 sizeof(arg64.LUN_info));
596 err |=
597 copy_from_user(&arg64.Request, &arg32->Request,
598 sizeof(arg64.Request));
599 err |=
600 copy_from_user(&arg64.error_info, &arg32->error_info,
601 sizeof(arg64.error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 err |= get_user(arg64.buf_size, &arg32->buf_size);
603 err |= get_user(cp, &arg32->buf);
604 arg64.buf = compat_ptr(cp);
605 err |= copy_to_user(p, &arg64, sizeof(arg64));
606
607 if (err)
608 return -EFAULT;
609
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700610 err = do_ioctl(f, CCISS_PASSTHRU, (unsigned long)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (err)
612 return err;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700613 err |=
614 copy_in_user(&arg32->error_info, &p->error_info,
615 sizeof(arg32->error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (err)
617 return -EFAULT;
618 return err;
619}
620
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700621static int cciss_ioctl32_big_passthru(struct file *file, unsigned cmd,
622 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 BIG_IOCTL32_Command_struct __user *arg32 =
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700625 (BIG_IOCTL32_Command_struct __user *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 BIG_IOCTL_Command_struct arg64;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700627 BIG_IOCTL_Command_struct __user *p =
628 compat_alloc_user_space(sizeof(arg64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 int err;
630 u32 cp;
631
632 err = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700633 err |=
634 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
635 sizeof(arg64.LUN_info));
636 err |=
637 copy_from_user(&arg64.Request, &arg32->Request,
638 sizeof(arg64.Request));
639 err |=
640 copy_from_user(&arg64.error_info, &arg32->error_info,
641 sizeof(arg64.error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 err |= get_user(arg64.buf_size, &arg32->buf_size);
643 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
644 err |= get_user(cp, &arg32->buf);
645 arg64.buf = compat_ptr(cp);
646 err |= copy_to_user(p, &arg64, sizeof(arg64));
647
648 if (err)
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700649 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700651 err = do_ioctl(file, CCISS_BIG_PASSTHRU, (unsigned long)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (err)
653 return err;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700654 err |=
655 copy_in_user(&arg32->error_info, &p->error_info,
656 sizeof(arg32->error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (err)
658 return -EFAULT;
659 return err;
660}
661#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800662
663static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo)
664{
665 drive_info_struct *drv = get_drv(bdev->bd_disk);
666
667 if (!drv->cylinders)
668 return -ENXIO;
669
670 geo->heads = drv->heads;
671 geo->sectors = drv->sectors;
672 geo->cylinders = drv->cylinders;
673 return 0;
674}
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676/*
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700677 * ioctl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700679static int cciss_ioctl(struct inode *inode, struct file *filep,
680 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct block_device *bdev = inode->i_bdev;
683 struct gendisk *disk = bdev->bd_disk;
684 ctlr_info_t *host = get_host(disk);
685 drive_info_struct *drv = get_drv(disk);
686 int ctlr = host->ctlr;
687 void __user *argp = (void __user *)arg;
688
689#ifdef CCISS_DEBUG
690 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700691#endif /* CCISS_DEBUG */
692
693 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 case CCISS_GETPCIINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700696 cciss_pci_info_struct pciinfo;
697
698 if (!arg)
699 return -EINVAL;
700 pciinfo.domain = pci_domain_nr(host->pdev->bus);
701 pciinfo.bus = host->pdev->bus->number;
702 pciinfo.dev_fn = host->pdev->devfn;
703 pciinfo.board_id = host->board_id;
704 if (copy_to_user
705 (argp, &pciinfo, sizeof(cciss_pci_info_struct)))
706 return -EFAULT;
707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700709 case CCISS_GETINTINFO:
710 {
711 cciss_coalint_struct intinfo;
712 if (!arg)
713 return -EINVAL;
714 intinfo.delay =
715 readl(&host->cfgtable->HostWrite.CoalIntDelay);
716 intinfo.count =
717 readl(&host->cfgtable->HostWrite.CoalIntCount);
718 if (copy_to_user
719 (argp, &intinfo, sizeof(cciss_coalint_struct)))
720 return -EFAULT;
721 return 0;
722 }
723 case CCISS_SETINTINFO:
724 {
725 cciss_coalint_struct intinfo;
726 unsigned long flags;
727 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700729 if (!arg)
730 return -EINVAL;
731 if (!capable(CAP_SYS_ADMIN))
732 return -EPERM;
733 if (copy_from_user
734 (&intinfo, argp, sizeof(cciss_coalint_struct)))
735 return -EFAULT;
736 if ((intinfo.delay == 0) && (intinfo.count == 0))
737 {
738// printk("cciss_ioctl: delay and count cannot be 0\n");
739 return -EINVAL;
740 }
741 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
742 /* Update the field, and then ring the doorbell */
743 writel(intinfo.delay,
744 &(host->cfgtable->HostWrite.CoalIntDelay));
745 writel(intinfo.count,
746 &(host->cfgtable->HostWrite.CoalIntCount));
747 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
748
749 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
750 if (!(readl(host->vaddr + SA5_DOORBELL)
751 & CFGTBL_ChangeReq))
752 break;
753 /* delay and try again */
754 udelay(1000);
755 }
756 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
757 if (i >= MAX_IOCTL_CONFIG_WAIT)
758 return -EAGAIN;
759 return 0;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 case CCISS_GETNODENAME:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700762 {
763 NodeName_type NodeName;
764 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700766 if (!arg)
767 return -EINVAL;
768 for (i = 0; i < 16; i++)
769 NodeName[i] =
770 readb(&host->cfgtable->ServerName[i]);
771 if (copy_to_user(argp, NodeName, sizeof(NodeName_type)))
772 return -EFAULT;
773 return 0;
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 case CCISS_SETNODENAME:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700776 {
777 NodeName_type NodeName;
778 unsigned long flags;
779 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700781 if (!arg)
782 return -EINVAL;
783 if (!capable(CAP_SYS_ADMIN))
784 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700786 if (copy_from_user
787 (NodeName, argp, sizeof(NodeName_type)))
788 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700790 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700792 /* Update the field, and then ring the doorbell */
793 for (i = 0; i < 16; i++)
794 writeb(NodeName[i],
795 &host->cfgtable->ServerName[i]);
796
797 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
798
799 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
800 if (!(readl(host->vaddr + SA5_DOORBELL)
801 & CFGTBL_ChangeReq))
802 break;
803 /* delay and try again */
804 udelay(1000);
805 }
806 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
807 if (i >= MAX_IOCTL_CONFIG_WAIT)
808 return -EAGAIN;
809 return 0;
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 case CCISS_GETHEARTBEAT:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700813 {
814 Heartbeat_type heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700816 if (!arg)
817 return -EINVAL;
818 heartbeat = readl(&host->cfgtable->HeartBeat);
819 if (copy_to_user
820 (argp, &heartbeat, sizeof(Heartbeat_type)))
821 return -EFAULT;
822 return 0;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 case CCISS_GETBUSTYPES:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700825 {
826 BusTypes_type BusTypes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700828 if (!arg)
829 return -EINVAL;
830 BusTypes = readl(&host->cfgtable->BusTypes);
831 if (copy_to_user
832 (argp, &BusTypes, sizeof(BusTypes_type)))
833 return -EFAULT;
834 return 0;
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 case CCISS_GETFIRMVER:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700837 {
838 FirmwareVer_type firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700840 if (!arg)
841 return -EINVAL;
842 memcpy(firmware, host->firm_ver, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700844 if (copy_to_user
845 (argp, firmware, sizeof(FirmwareVer_type)))
846 return -EFAULT;
847 return 0;
848 }
849 case CCISS_GETDRIVVER:
850 {
851 DriverVer_type DriverVer = DRIVER_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700853 if (!arg)
854 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700856 if (copy_to_user
857 (argp, &DriverVer, sizeof(DriverVer_type)))
858 return -EFAULT;
859 return 0;
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 case CCISS_REVALIDVOLS:
Mike Miller3833a742006-12-06 20:35:10 -0800863 return rebuild_lun_table(host, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700865 case CCISS_GETLUNINFO:{
866 LogvolInfo_struct luninfo;
867
868 luninfo.LunID = drv->LunID;
869 luninfo.num_opens = drv->usage_count;
870 luninfo.num_parts = 0;
871 if (copy_to_user(argp, &luninfo,
872 sizeof(LogvolInfo_struct)))
873 return -EFAULT;
874 return 0;
875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 case CCISS_DEREGDISK:
Mike Millerddd47442005-09-13 01:25:22 -0700877 return rebuild_lun_table(host, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 case CCISS_REGNEWD:
Mike Millerddd47442005-09-13 01:25:22 -0700880 return rebuild_lun_table(host, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 case CCISS_PASSTHRU:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700884 IOCTL_Command_struct iocommand;
885 CommandList_struct *c;
886 char *buff = NULL;
887 u64bit temp64;
888 unsigned long flags;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700889 DECLARE_COMPLETION_ONSTACK(wait);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700890
891 if (!arg)
892 return -EINVAL;
893
894 if (!capable(CAP_SYS_RAWIO))
895 return -EPERM;
896
897 if (copy_from_user
898 (&iocommand, argp, sizeof(IOCTL_Command_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return -EFAULT;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700900 if ((iocommand.buf_size < 1) &&
901 (iocommand.Request.Type.Direction != XFER_NONE)) {
902 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700904#if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
905 /* Check kmalloc limits */
906 if (iocommand.buf_size > 128000)
907 return -EINVAL;
908#endif
909 if (iocommand.buf_size > 0) {
910 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
911 if (buff == NULL)
912 return -EFAULT;
913 }
914 if (iocommand.Request.Type.Direction == XFER_WRITE) {
915 /* Copy the data into the buffer we created */
916 if (copy_from_user
917 (buff, iocommand.buf, iocommand.buf_size)) {
918 kfree(buff);
919 return -EFAULT;
920 }
921 } else {
922 memset(buff, 0, iocommand.buf_size);
923 }
924 if ((c = cmd_alloc(host, 0)) == NULL) {
925 kfree(buff);
926 return -ENOMEM;
927 }
928 // Fill in the command type
929 c->cmd_type = CMD_IOCTL_PEND;
930 // Fill in Command Header
931 c->Header.ReplyQueue = 0; // unused in simple mode
932 if (iocommand.buf_size > 0) // buffer to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700934 c->Header.SGList = 1;
935 c->Header.SGTotal = 1;
936 } else // no buffers to fill
937 {
938 c->Header.SGList = 0;
939 c->Header.SGTotal = 0;
940 }
941 c->Header.LUN = iocommand.LUN_info;
942 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
943
944 // Fill in Request block
945 c->Request = iocommand.Request;
946
947 // Fill in the scatter gather information
948 if (iocommand.buf_size > 0) {
949 temp64.val = pci_map_single(host->pdev, buff,
950 iocommand.buf_size,
951 PCI_DMA_BIDIRECTIONAL);
952 c->SG[0].Addr.lower = temp64.val32.lower;
953 c->SG[0].Addr.upper = temp64.val32.upper;
954 c->SG[0].Len = iocommand.buf_size;
955 c->SG[0].Ext = 0; // we are not chaining
956 }
957 c->waiting = &wait;
958
959 /* Put the request on the tail of the request queue */
960 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
961 addQ(&host->reqQ, c);
962 host->Qdepth++;
963 start_io(host);
964 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
965
966 wait_for_completion(&wait);
967
968 /* unlock the buffers from DMA */
969 temp64.val32.lower = c->SG[0].Addr.lower;
970 temp64.val32.upper = c->SG[0].Addr.upper;
971 pci_unmap_single(host->pdev, (dma_addr_t) temp64.val,
972 iocommand.buf_size,
973 PCI_DMA_BIDIRECTIONAL);
974
975 /* Copy the error information out */
976 iocommand.error_info = *(c->err_info);
977 if (copy_to_user
978 (argp, &iocommand, sizeof(IOCTL_Command_struct))) {
979 kfree(buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 cmd_free(host, c, 0);
981 return -EFAULT;
982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700984 if (iocommand.Request.Type.Direction == XFER_READ) {
985 /* Copy the data out of the buffer we created */
986 if (copy_to_user
987 (iocommand.buf, buff, iocommand.buf_size)) {
988 kfree(buff);
989 cmd_free(host, c, 0);
990 return -EFAULT;
991 }
992 }
993 kfree(buff);
994 cmd_free(host, c, 0);
995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700997 case CCISS_BIG_PASSTHRU:{
998 BIG_IOCTL_Command_struct *ioc;
999 CommandList_struct *c;
1000 unsigned char **buff = NULL;
1001 int *buff_size = NULL;
1002 u64bit temp64;
1003 unsigned long flags;
1004 BYTE sg_used = 0;
1005 int status = 0;
1006 int i;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001007 DECLARE_COMPLETION_ONSTACK(wait);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001008 __u32 left;
1009 __u32 sz;
1010 BYTE __user *data_ptr;
1011
1012 if (!arg)
1013 return -EINVAL;
1014 if (!capable(CAP_SYS_RAWIO))
1015 return -EPERM;
1016 ioc = (BIG_IOCTL_Command_struct *)
1017 kmalloc(sizeof(*ioc), GFP_KERNEL);
1018 if (!ioc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 status = -ENOMEM;
1020 goto cleanup1;
1021 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001022 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
1023 status = -EFAULT;
1024 goto cleanup1;
1025 }
1026 if ((ioc->buf_size < 1) &&
1027 (ioc->Request.Type.Direction != XFER_NONE)) {
1028 status = -EINVAL;
1029 goto cleanup1;
1030 }
1031 /* Check kmalloc limits using all SGs */
1032 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
1033 status = -EINVAL;
1034 goto cleanup1;
1035 }
1036 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
1037 status = -EINVAL;
1038 goto cleanup1;
1039 }
1040 buff =
1041 kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
1042 if (!buff) {
1043 status = -ENOMEM;
1044 goto cleanup1;
1045 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001046 buff_size = kmalloc(MAXSGENTRIES * sizeof(int),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001047 GFP_KERNEL);
1048 if (!buff_size) {
1049 status = -ENOMEM;
1050 goto cleanup1;
1051 }
1052 left = ioc->buf_size;
1053 data_ptr = ioc->buf;
1054 while (left) {
1055 sz = (left >
1056 ioc->malloc_size) ? ioc->
1057 malloc_size : left;
1058 buff_size[sg_used] = sz;
1059 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
1060 if (buff[sg_used] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 status = -ENOMEM;
Jens Axboe15534d32005-11-18 22:02:44 +01001062 goto cleanup1;
1063 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001064 if (ioc->Request.Type.Direction == XFER_WRITE) {
1065 if (copy_from_user
1066 (buff[sg_used], data_ptr, sz)) {
1067 status = -ENOMEM;
1068 goto cleanup1;
1069 }
1070 } else {
1071 memset(buff[sg_used], 0, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001073 left -= sz;
1074 data_ptr += sz;
1075 sg_used++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001077 if ((c = cmd_alloc(host, 0)) == NULL) {
1078 status = -ENOMEM;
1079 goto cleanup1;
1080 }
1081 c->cmd_type = CMD_IOCTL_PEND;
1082 c->Header.ReplyQueue = 0;
1083
1084 if (ioc->buf_size > 0) {
1085 c->Header.SGList = sg_used;
1086 c->Header.SGTotal = sg_used;
1087 } else {
1088 c->Header.SGList = 0;
1089 c->Header.SGTotal = 0;
1090 }
1091 c->Header.LUN = ioc->LUN_info;
1092 c->Header.Tag.lower = c->busaddr;
1093
1094 c->Request = ioc->Request;
1095 if (ioc->buf_size > 0) {
1096 int i;
1097 for (i = 0; i < sg_used; i++) {
1098 temp64.val =
1099 pci_map_single(host->pdev, buff[i],
1100 buff_size[i],
1101 PCI_DMA_BIDIRECTIONAL);
1102 c->SG[i].Addr.lower =
1103 temp64.val32.lower;
1104 c->SG[i].Addr.upper =
1105 temp64.val32.upper;
1106 c->SG[i].Len = buff_size[i];
1107 c->SG[i].Ext = 0; /* we are not chaining */
1108 }
1109 }
1110 c->waiting = &wait;
1111 /* Put the request on the tail of the request queue */
1112 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1113 addQ(&host->reqQ, c);
1114 host->Qdepth++;
1115 start_io(host);
1116 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1117 wait_for_completion(&wait);
1118 /* unlock the buffers from DMA */
1119 for (i = 0; i < sg_used; i++) {
1120 temp64.val32.lower = c->SG[i].Addr.lower;
1121 temp64.val32.upper = c->SG[i].Addr.upper;
1122 pci_unmap_single(host->pdev,
1123 (dma_addr_t) temp64.val, buff_size[i],
1124 PCI_DMA_BIDIRECTIONAL);
1125 }
1126 /* Copy the error information out */
1127 ioc->error_info = *(c->err_info);
1128 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
1129 cmd_free(host, c, 0);
1130 status = -EFAULT;
1131 goto cleanup1;
1132 }
1133 if (ioc->Request.Type.Direction == XFER_READ) {
1134 /* Copy the data out of the buffer we created */
1135 BYTE __user *ptr = ioc->buf;
1136 for (i = 0; i < sg_used; i++) {
1137 if (copy_to_user
1138 (ptr, buff[i], buff_size[i])) {
1139 cmd_free(host, c, 0);
1140 status = -EFAULT;
1141 goto cleanup1;
1142 }
1143 ptr += buff_size[i];
1144 }
1145 }
1146 cmd_free(host, c, 0);
1147 status = 0;
1148 cleanup1:
1149 if (buff) {
1150 for (i = 0; i < sg_used; i++)
1151 kfree(buff[i]);
1152 kfree(buff);
1153 }
1154 kfree(buff_size);
1155 kfree(ioc);
1156 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07001158
1159 /* scsi_cmd_ioctl handles these, below, though some are not */
1160 /* very meaningful for cciss. SG_IO is the main one people want. */
1161
1162 case SG_GET_VERSION_NUM:
1163 case SG_SET_TIMEOUT:
1164 case SG_GET_TIMEOUT:
1165 case SG_GET_RESERVED_SIZE:
1166 case SG_SET_RESERVED_SIZE:
1167 case SG_EMULATED_HOST:
1168 case SG_IO:
1169 case SCSI_IOCTL_SEND_COMMAND:
1170 return scsi_cmd_ioctl(filep, disk, cmd, argp);
1171
1172 /* scsi_cmd_ioctl would normally handle these, below, but */
1173 /* they aren't a good fit for cciss, as CD-ROMs are */
1174 /* not supported, and we don't have any bus/target/lun */
1175 /* which we present to the kernel. */
1176
1177 case CDROM_SEND_PACKET:
1178 case CDROMCLOSETRAY:
1179 case CDROMEJECT:
1180 case SCSI_IOCTL_GET_IDLUN:
1181 case SCSI_IOCTL_GET_BUS_NUMBER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 default:
1183 return -ENOTTY;
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Mike Millerca1e0482006-04-10 15:38:07 -07001187static inline void complete_buffers(struct bio *bio, int status)
1188{
1189 while (bio) {
1190 struct bio *xbh = bio->bi_next;
1191 int nr_sectors = bio_sectors(bio);
1192
1193 bio->bi_next = NULL;
Mike Millerca1e0482006-04-10 15:38:07 -07001194 bio_endio(bio, nr_sectors << 9, status ? 0 : -EIO);
1195 bio = xbh;
1196 }
Mike Millerca1e0482006-04-10 15:38:07 -07001197}
1198
Jens Axboe7b30f092006-07-25 15:02:48 +02001199static void cciss_check_queues(ctlr_info_t *h)
1200{
1201 int start_queue = h->next_to_run;
1202 int i;
1203
1204 /* check to see if we have maxed out the number of commands that can
1205 * be placed on the queue. If so then exit. We do this check here
1206 * in case the interrupt we serviced was from an ioctl and did not
1207 * free any new commands.
1208 */
Mike Millerf8806322006-12-06 20:35:01 -08001209 if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds)
Jens Axboe7b30f092006-07-25 15:02:48 +02001210 return;
1211
1212 /* We have room on the queue for more commands. Now we need to queue
1213 * them up. We will also keep track of the next queue to run so
1214 * that every queue gets a chance to be started first.
1215 */
1216 for (i = 0; i < h->highest_lun + 1; i++) {
1217 int curr_queue = (start_queue + i) % (h->highest_lun + 1);
1218 /* make sure the disk has been added and the drive is real
1219 * because this can be called from the middle of init_one.
1220 */
1221 if (!(h->drv[curr_queue].queue) || !(h->drv[curr_queue].heads))
1222 continue;
1223 blk_start_queue(h->gendisk[curr_queue]->queue);
1224
1225 /* check to see if we have maxed out the number of commands
1226 * that can be placed on the queue.
1227 */
Mike Millerf8806322006-12-06 20:35:01 -08001228 if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds) {
Jens Axboe7b30f092006-07-25 15:02:48 +02001229 if (curr_queue == start_queue) {
1230 h->next_to_run =
1231 (start_queue + 1) % (h->highest_lun + 1);
1232 break;
1233 } else {
1234 h->next_to_run = curr_queue;
1235 break;
1236 }
1237 } else {
1238 curr_queue = (curr_queue + 1) % (h->highest_lun + 1);
1239 }
1240 }
1241}
1242
Mike Millerca1e0482006-04-10 15:38:07 -07001243static void cciss_softirq_done(struct request *rq)
1244{
1245 CommandList_struct *cmd = rq->completion_data;
1246 ctlr_info_t *h = hba[cmd->ctlr];
1247 unsigned long flags;
1248 u64bit temp64;
1249 int i, ddir;
1250
1251 if (cmd->Request.Type.Direction == XFER_READ)
1252 ddir = PCI_DMA_FROMDEVICE;
1253 else
1254 ddir = PCI_DMA_TODEVICE;
1255
1256 /* command did not need to be retried */
1257 /* unmap the DMA mapping for all the scatter gather elements */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001258 for (i = 0; i < cmd->Header.SGList; i++) {
Mike Millerca1e0482006-04-10 15:38:07 -07001259 temp64.val32.lower = cmd->SG[i].Addr.lower;
1260 temp64.val32.upper = cmd->SG[i].Addr.upper;
1261 pci_unmap_page(h->pdev, temp64.val, cmd->SG[i].Len, ddir);
1262 }
1263
1264 complete_buffers(rq->bio, rq->errors);
1265
Jens Axboeb369c2c2006-11-14 12:36:03 +01001266 if (blk_fs_request(rq)) {
1267 const int rw = rq_data_dir(rq);
1268
1269 disk_stat_add(rq->rq_disk, sectors[rw], rq->nr_sectors);
1270 }
1271
Mike Millerca1e0482006-04-10 15:38:07 -07001272#ifdef CCISS_DEBUG
1273 printk("Done with %p\n", rq);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001274#endif /* CCISS_DEBUG */
Mike Millerca1e0482006-04-10 15:38:07 -07001275
Matt Mackall8bd0b972006-06-25 05:47:11 -07001276 add_disk_randomness(rq->rq_disk);
Mike Millerca1e0482006-04-10 15:38:07 -07001277 spin_lock_irqsave(&h->lock, flags);
1278 end_that_request_last(rq, rq->errors);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001279 cmd_free(h, cmd, 1);
Jens Axboe7b30f092006-07-25 15:02:48 +02001280 cciss_check_queues(h);
Mike Millerca1e0482006-04-10 15:38:07 -07001281 spin_unlock_irqrestore(&h->lock, flags);
1282}
1283
Mike Millerddd47442005-09-13 01:25:22 -07001284/* This function will check the usage_count of the drive to be updated/added.
1285 * If the usage_count is zero then the drive information will be updated and
1286 * the disk will be re-registered with the kernel. If not then it will be
1287 * left alone for the next reboot. The exception to this is disk 0 which
1288 * will always be left registered with the kernel since it is also the
1289 * controller node. Any changes to disk 0 will show up on the next
1290 * reboot.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001291 */
Mike Millerddd47442005-09-13 01:25:22 -07001292static void cciss_update_drive_info(int ctlr, int drv_index)
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001293{
Mike Millerddd47442005-09-13 01:25:22 -07001294 ctlr_info_t *h = hba[ctlr];
1295 struct gendisk *disk;
Mike Millerddd47442005-09-13 01:25:22 -07001296 InquiryData_struct *inq_buff = NULL;
1297 unsigned int block_size;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001298 sector_t total_size;
Mike Millerddd47442005-09-13 01:25:22 -07001299 unsigned long flags = 0;
1300 int ret = 0;
1301
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001302 /* if the disk already exists then deregister it before proceeding */
1303 if (h->drv[drv_index].raid_level != -1) {
Mike Millerddd47442005-09-13 01:25:22 -07001304 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
1305 h->drv[drv_index].busy_configuring = 1;
1306 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1307 ret = deregister_disk(h->gendisk[drv_index],
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001308 &h->drv[drv_index], 0);
Mike Millerddd47442005-09-13 01:25:22 -07001309 h->drv[drv_index].busy_configuring = 0;
1310 }
1311
1312 /* If the disk is in use return */
1313 if (ret)
1314 return;
1315
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001316 /* Get information about the disk and modify the driver structure */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001317 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
Mike Millerddd47442005-09-13 01:25:22 -07001318 if (inq_buff == NULL)
1319 goto mem_msg;
1320
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001321 /* testing to see if 16-byte CDBs are already being used */
1322 if (h->cciss_read == CCISS_READ_16) {
1323 cciss_read_capacity_16(h->ctlr, drv_index, 1,
1324 &total_size, &block_size);
1325 goto geo_inq;
1326 }
1327
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001328 cciss_read_capacity(ctlr, drv_index, 1,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001329 &total_size, &block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001330
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001331 /* if read_capacity returns all F's this volume is >2TB in size */
1332 /* so we switch to 16-byte CDB's for all read/write ops */
1333 if (total_size == 0xFFFFFFFFULL) {
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001334 cciss_read_capacity_16(ctlr, drv_index, 1,
1335 &total_size, &block_size);
1336 h->cciss_read = CCISS_READ_16;
1337 h->cciss_write = CCISS_WRITE_16;
1338 } else {
1339 h->cciss_read = CCISS_READ_10;
1340 h->cciss_write = CCISS_WRITE_10;
1341 }
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001342geo_inq:
Mike Millerddd47442005-09-13 01:25:22 -07001343 cciss_geometry_inquiry(ctlr, drv_index, 1, total_size, block_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001344 inq_buff, &h->drv[drv_index]);
Mike Millerddd47442005-09-13 01:25:22 -07001345
1346 ++h->num_luns;
1347 disk = h->gendisk[drv_index];
1348 set_capacity(disk, h->drv[drv_index].nr_blocks);
1349
Mike Millerddd47442005-09-13 01:25:22 -07001350 /* if it's the controller it's already added */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001351 if (drv_index) {
Mike Millerddd47442005-09-13 01:25:22 -07001352 disk->queue = blk_init_queue(do_cciss_request, &h->lock);
Mike Miller799202c2006-12-06 20:35:12 -08001353 sprintf(disk->disk_name, "cciss/c%dd%d", ctlr, drv_index);
1354 disk->major = h->major;
1355 disk->first_minor = drv_index << NWD_SHIFT;
1356 disk->fops = &cciss_fops;
1357 disk->private_data = &h->drv[drv_index];
Mike Millerddd47442005-09-13 01:25:22 -07001358
1359 /* Set up queue information */
1360 disk->queue->backing_dev_info.ra_pages = READ_AHEAD;
1361 blk_queue_bounce_limit(disk->queue, hba[ctlr]->pdev->dma_mask);
1362
1363 /* This is a hardware imposed limit. */
1364 blk_queue_max_hw_segments(disk->queue, MAXSGENTRIES);
1365
1366 /* This is a limit in the driver and could be eliminated. */
1367 blk_queue_max_phys_segments(disk->queue, MAXSGENTRIES);
1368
Mike Miller92c4231a2006-12-06 20:35:06 -08001369 blk_queue_max_sectors(disk->queue, h->cciss_max_sectors);
Mike Millerddd47442005-09-13 01:25:22 -07001370
Mike Millerca1e0482006-04-10 15:38:07 -07001371 blk_queue_softirq_done(disk->queue, cciss_softirq_done);
1372
Mike Millerddd47442005-09-13 01:25:22 -07001373 disk->queue->queuedata = hba[ctlr];
1374
1375 blk_queue_hardsect_size(disk->queue,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001376 hba[ctlr]->drv[drv_index].block_size);
Mike Millerddd47442005-09-13 01:25:22 -07001377
1378 h->drv[drv_index].queue = disk->queue;
1379 add_disk(disk);
1380 }
1381
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001382 freeret:
Mike Millerddd47442005-09-13 01:25:22 -07001383 kfree(inq_buff);
1384 return;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001385 mem_msg:
Mike Millerddd47442005-09-13 01:25:22 -07001386 printk(KERN_ERR "cciss: out of memory\n");
1387 goto freeret;
1388}
1389
1390/* This function will find the first index of the controllers drive array
1391 * that has a -1 for the raid_level and will return that index. This is
1392 * where new drives will be added. If the index to be returned is greater
1393 * than the highest_lun index for the controller then highest_lun is set
1394 * to this new index. If there are no available indexes then -1 is returned.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001395 */
Mike Millerddd47442005-09-13 01:25:22 -07001396static int cciss_find_free_drive_index(int ctlr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Mike Millerddd47442005-09-13 01:25:22 -07001398 int i;
1399
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001400 for (i = 0; i < CISS_MAX_LUN; i++) {
1401 if (hba[ctlr]->drv[i].raid_level == -1) {
Mike Millerddd47442005-09-13 01:25:22 -07001402 if (i > hba[ctlr]->highest_lun)
1403 hba[ctlr]->highest_lun = i;
1404 return i;
1405 }
1406 }
1407 return -1;
1408}
1409
1410/* This function will add and remove logical drives from the Logical
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001411 * drive array of the controller and maintain persistency of ordering
Mike Millerddd47442005-09-13 01:25:22 -07001412 * so that mount points are preserved until the next reboot. This allows
1413 * for the removal of logical drives in the middle of the drive array
1414 * without a re-ordering of those drives.
1415 * INPUT
1416 * h = The controller to perform the operations on
1417 * del_disk = The disk to remove if specified. If the value given
1418 * is NULL then no disk is removed.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001419 */
Mike Millerddd47442005-09-13 01:25:22 -07001420static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk)
1421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 int ctlr = h->ctlr;
Mike Millerddd47442005-09-13 01:25:22 -07001423 int num_luns;
1424 ReportLunData_struct *ld_buff = NULL;
1425 drive_info_struct *drv = NULL;
1426 int return_code;
1427 int listlength = 0;
1428 int i;
1429 int drv_found;
1430 int drv_index = 0;
1431 __u32 lunid = 0;
1432 unsigned long flags;
1433
1434 /* Set busy_configuring flag for this operation */
1435 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001436 if (h->busy_configuring) {
Mike Millerddd47442005-09-13 01:25:22 -07001437 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1438 return -EBUSY;
1439 }
1440 h->busy_configuring = 1;
1441
1442 /* if del_disk is NULL then we are being called to add a new disk
1443 * and update the logical drive table. If it is not NULL then
1444 * we will check if the disk is in use or not.
1445 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001446 if (del_disk != NULL) {
Mike Millerddd47442005-09-13 01:25:22 -07001447 drv = get_drv(del_disk);
1448 drv->busy_configuring = 1;
1449 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1450 return_code = deregister_disk(del_disk, drv, 1);
1451 drv->busy_configuring = 0;
1452 h->busy_configuring = 0;
1453 return return_code;
1454 } else {
1455 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1456 if (!capable(CAP_SYS_RAWIO))
1457 return -EPERM;
1458
1459 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1460 if (ld_buff == NULL)
1461 goto mem_msg;
1462
1463 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001464 sizeof(ReportLunData_struct), 0,
1465 0, 0, TYPE_CMD);
Mike Millerddd47442005-09-13 01:25:22 -07001466
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001467 if (return_code == IO_OK) {
Mike Miller799202c2006-12-06 20:35:12 -08001468 listlength =
Al Viro4c1f2b32007-03-14 09:19:10 +00001469 be32_to_cpu(*(__be32 *) ld_buff->LUNListLength);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001470 } else { /* reading number of logical volumes failed */
Mike Millerddd47442005-09-13 01:25:22 -07001471 printk(KERN_WARNING "cciss: report logical volume"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001472 " command failed\n");
Mike Millerddd47442005-09-13 01:25:22 -07001473 listlength = 0;
1474 goto freeret;
1475 }
1476
1477 num_luns = listlength / 8; /* 8 bytes per entry */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001478 if (num_luns > CISS_MAX_LUN) {
Mike Millerddd47442005-09-13 01:25:22 -07001479 num_luns = CISS_MAX_LUN;
1480 printk(KERN_WARNING "cciss: more luns configured"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001481 " on controller than can be handled by"
1482 " this driver.\n");
Mike Millerddd47442005-09-13 01:25:22 -07001483 }
1484
1485 /* Compare controller drive array to drivers drive array.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001486 * Check for updates in the drive information and any new drives
1487 * on the controller.
1488 */
1489 for (i = 0; i < num_luns; i++) {
Mike Millerddd47442005-09-13 01:25:22 -07001490 int j;
1491
1492 drv_found = 0;
1493
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001494 lunid = (0xff &
1495 (unsigned int)(ld_buff->LUN[i][3])) << 24;
1496 lunid |= (0xff &
1497 (unsigned int)(ld_buff->LUN[i][2])) << 16;
1498 lunid |= (0xff &
1499 (unsigned int)(ld_buff->LUN[i][1])) << 8;
1500 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
Mike Millerddd47442005-09-13 01:25:22 -07001501
1502 /* Find if the LUN is already in the drive array
1503 * of the controller. If so then update its info
1504 * if not is use. If it does not exist then find
1505 * the first free index and add it.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001506 */
1507 for (j = 0; j <= h->highest_lun; j++) {
1508 if (h->drv[j].LunID == lunid) {
Mike Millerddd47442005-09-13 01:25:22 -07001509 drv_index = j;
1510 drv_found = 1;
1511 }
1512 }
1513
1514 /* check if the drive was found already in the array */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001515 if (!drv_found) {
Mike Millerddd47442005-09-13 01:25:22 -07001516 drv_index = cciss_find_free_drive_index(ctlr);
1517 if (drv_index == -1)
1518 goto freeret;
1519
Mike Miller799202c2006-12-06 20:35:12 -08001520 /*Check if the gendisk needs to be allocated */
1521 if (!h->gendisk[drv_index]){
1522 h->gendisk[drv_index] = alloc_disk(1 << NWD_SHIFT);
1523 if (!h->gendisk[drv_index]){
1524 printk(KERN_ERR "cciss: could not allocate new disk %d\n", drv_index);
1525 goto mem_msg;
1526 }
1527 }
Mike Millerddd47442005-09-13 01:25:22 -07001528 }
1529 h->drv[drv_index].LunID = lunid;
1530 cciss_update_drive_info(ctlr, drv_index);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001531 } /* end for */
1532 } /* end else */
Mike Millerddd47442005-09-13 01:25:22 -07001533
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001534 freeret:
Mike Millerddd47442005-09-13 01:25:22 -07001535 kfree(ld_buff);
1536 h->busy_configuring = 0;
1537 /* We return -1 here to tell the ACU that we have registered/updated
1538 * all of the drives that we can and to keep it from calling us
1539 * additional times.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001540 */
Mike Millerddd47442005-09-13 01:25:22 -07001541 return -1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001542 mem_msg:
Mike Millerddd47442005-09-13 01:25:22 -07001543 printk(KERN_ERR "cciss: out of memory\n");
1544 goto freeret;
1545}
1546
1547/* This function will deregister the disk and it's queue from the
1548 * kernel. It must be called with the controller lock held and the
1549 * drv structures busy_configuring flag set. It's parameters are:
1550 *
1551 * disk = This is the disk to be deregistered
1552 * drv = This is the drive_info_struct associated with the disk to be
1553 * deregistered. It contains information about the disk used
1554 * by the driver.
1555 * clear_all = This flag determines whether or not the disk information
1556 * is going to be completely cleared out and the highest_lun
1557 * reset. Sometimes we want to clear out information about
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001558 * the disk in preparation for re-adding it. In this case
Mike Millerddd47442005-09-13 01:25:22 -07001559 * the highest_lun should be left unchanged and the LunID
1560 * should not be cleared.
1561*/
1562static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
1563 int clear_all)
1564{
Mike Miller799202c2006-12-06 20:35:12 -08001565 int i;
Mike Millerddd47442005-09-13 01:25:22 -07001566 ctlr_info_t *h = get_host(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 if (!capable(CAP_SYS_RAWIO))
1569 return -EPERM;
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /* make sure logical volume is NOT is use */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001572 if (clear_all || (h->gendisk[0] == disk)) {
1573 if (drv->usage_count > 1)
1574 return -EBUSY;
1575 } else if (drv->usage_count > 0)
1576 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Mike Millerddd47442005-09-13 01:25:22 -07001578 /* invalidate the devices and deregister the disk. If it is disk
1579 * zero do not deregister it but just zero out it's values. This
1580 * allows us to delete disk zero but keep the controller registered.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001581 */
1582 if (h->gendisk[0] != disk) {
mikem6f5a0f72005-11-18 22:05:36 +01001583 if (disk) {
1584 request_queue_t *q = disk->queue;
1585 if (disk->flags & GENHD_FL_UP)
1586 del_gendisk(disk);
Mike Miller2f6331f2005-12-12 22:17:14 -08001587 if (q) {
mikem6f5a0f72005-11-18 22:05:36 +01001588 blk_cleanup_queue(q);
Mike Miller799202c2006-12-06 20:35:12 -08001589 /* Set drv->queue to NULL so that we do not try
1590 * to call blk_start_queue on this queue in the
1591 * interrupt handler
1592 */
Mike Miller2f6331f2005-12-12 22:17:14 -08001593 drv->queue = NULL;
1594 }
Mike Miller799202c2006-12-06 20:35:12 -08001595 /* If clear_all is set then we are deleting the logical
1596 * drive, not just refreshing its info. For drives
1597 * other than disk 0 we will call put_disk. We do not
1598 * do this for disk 0 as we need it to be able to
1599 * configure the controller.
1600 */
1601 if (clear_all){
1602 /* This isn't pretty, but we need to find the
1603 * disk in our array and NULL our the pointer.
1604 * This is so that we will call alloc_disk if
1605 * this index is used again later.
1606 */
1607 for (i=0; i < CISS_MAX_LUN; i++){
1608 if(h->gendisk[i] == disk){
1609 h->gendisk[i] = NULL;
1610 break;
1611 }
1612 }
1613 put_disk(disk);
1614 }
Mike Millerddd47442005-09-13 01:25:22 -07001615 }
Mike Miller799202c2006-12-06 20:35:12 -08001616 } else {
1617 set_capacity(disk, 0);
Mike Millerddd47442005-09-13 01:25:22 -07001618 }
1619
1620 --h->num_luns;
1621 /* zero out the disk size info */
1622 drv->nr_blocks = 0;
1623 drv->block_size = 0;
1624 drv->heads = 0;
1625 drv->sectors = 0;
1626 drv->cylinders = 0;
1627 drv->raid_level = -1; /* This can be used as a flag variable to
1628 * indicate that this element of the drive
1629 * array is free.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001630 */
Mike Millerddd47442005-09-13 01:25:22 -07001631
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001632 if (clear_all) {
1633 /* check to see if it was the last disk */
1634 if (drv == h->drv + h->highest_lun) {
1635 /* if so, find the new hightest lun */
1636 int i, newhighest = -1;
1637 for (i = 0; i < h->highest_lun; i++) {
1638 /* if the disk has size > 0, it is available */
Mike Millerddd47442005-09-13 01:25:22 -07001639 if (h->drv[i].heads)
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001640 newhighest = i;
1641 }
1642 h->highest_lun = newhighest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Mike Millerddd47442005-09-13 01:25:22 -07001644
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001645 drv->LunID = 0;
Mike Millerddd47442005-09-13 01:25:22 -07001646 }
Bjorn Helgaase2019b52006-06-25 05:49:05 -07001647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
Mike Millerddd47442005-09-13 01:25:22 -07001649
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001650static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, /* 0: address the controller,
1651 1: address logical volume log_unit,
1652 2: periph device address is scsi3addr */
1653 unsigned int log_unit, __u8 page_code,
1654 unsigned char *scsi3addr, int cmd_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001656 ctlr_info_t *h = hba[ctlr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 u64bit buff_dma_handle;
1658 int status = IO_OK;
1659
1660 c->cmd_type = CMD_IOCTL_PEND;
1661 c->Header.ReplyQueue = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001662 if (buff != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 c->Header.SGList = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001664 c->Header.SGTotal = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 } else {
1666 c->Header.SGList = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001667 c->Header.SGTotal = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669 c->Header.Tag.lower = c->busaddr;
1670
1671 c->Request.Type.Type = cmd_type;
1672 if (cmd_type == TYPE_CMD) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001673 switch (cmd) {
1674 case CISS_INQUIRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /* If the logical unit number is 0 then, this is going
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001676 to controller so It's a physical command
1677 mode = 0 target = 0. So we have nothing to write.
1678 otherwise, if use_unit_num == 1,
1679 mode = 1(volume set addressing) target = LUNID
1680 otherwise, if use_unit_num == 2,
1681 mode = 0(periph dev addr) target = scsi3addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (use_unit_num == 1) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001683 c->Header.LUN.LogDev.VolId =
1684 h->drv[log_unit].LunID;
1685 c->Header.LUN.LogDev.Mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 } else if (use_unit_num == 2) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001687 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr,
1688 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 c->Header.LUN.LogDev.Mode = 0;
1690 }
1691 /* are we trying to read a vital product page */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001692 if (page_code != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 c->Request.CDB[1] = 0x01;
1694 c->Request.CDB[2] = page_code;
1695 }
1696 c->Request.CDBLen = 6;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001697 c->Request.Type.Attribute = ATTR_SIMPLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 c->Request.Type.Direction = XFER_READ;
1699 c->Request.Timeout = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001700 c->Request.CDB[0] = CISS_INQUIRY;
1701 c->Request.CDB[4] = size & 0xFF;
1702 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 case CISS_REPORT_LOG:
1704 case CISS_REPORT_PHYS:
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001705 /* Talking to controller so It's a physical command
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 mode = 00 target = 0. Nothing to write.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 c->Request.CDBLen = 12;
1709 c->Request.Type.Attribute = ATTR_SIMPLE;
1710 c->Request.Type.Direction = XFER_READ;
1711 c->Request.Timeout = 0;
1712 c->Request.CDB[0] = cmd;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001713 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 c->Request.CDB[7] = (size >> 16) & 0xFF;
1715 c->Request.CDB[8] = (size >> 8) & 0xFF;
1716 c->Request.CDB[9] = size & 0xFF;
1717 break;
1718
1719 case CCISS_READ_CAPACITY:
1720 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1721 c->Header.LUN.LogDev.Mode = 1;
1722 c->Request.CDBLen = 10;
1723 c->Request.Type.Attribute = ATTR_SIMPLE;
1724 c->Request.Type.Direction = XFER_READ;
1725 c->Request.Timeout = 0;
1726 c->Request.CDB[0] = cmd;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001727 break;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001728 case CCISS_READ_CAPACITY_16:
1729 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1730 c->Header.LUN.LogDev.Mode = 1;
1731 c->Request.CDBLen = 16;
1732 c->Request.Type.Attribute = ATTR_SIMPLE;
1733 c->Request.Type.Direction = XFER_READ;
1734 c->Request.Timeout = 0;
1735 c->Request.CDB[0] = cmd;
1736 c->Request.CDB[1] = 0x10;
1737 c->Request.CDB[10] = (size >> 24) & 0xFF;
1738 c->Request.CDB[11] = (size >> 16) & 0xFF;
1739 c->Request.CDB[12] = (size >> 8) & 0xFF;
1740 c->Request.CDB[13] = size & 0xFF;
1741 c->Request.Timeout = 0;
1742 c->Request.CDB[0] = cmd;
1743 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 case CCISS_CACHE_FLUSH:
1745 c->Request.CDBLen = 12;
1746 c->Request.Type.Attribute = ATTR_SIMPLE;
1747 c->Request.Type.Direction = XFER_WRITE;
1748 c->Request.Timeout = 0;
1749 c->Request.CDB[0] = BMIC_WRITE;
1750 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001751 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 default:
1753 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001754 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
Bjorn Helgaase2019b52006-06-25 05:49:05 -07001755 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757 } else if (cmd_type == TYPE_MSG) {
1758 switch (cmd) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001759 case 0: /* ABORT message */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001760 c->Request.CDBLen = 12;
1761 c->Request.Type.Attribute = ATTR_SIMPLE;
1762 c->Request.Type.Direction = XFER_WRITE;
1763 c->Request.Timeout = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001764 c->Request.CDB[0] = cmd; /* abort */
1765 c->Request.CDB[1] = 0; /* abort a command */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001766 /* buff contains the tag of the command to abort */
1767 memcpy(&c->Request.CDB[4], buff, 8);
1768 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001769 case 1: /* RESET message */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001770 c->Request.CDBLen = 12;
1771 c->Request.Type.Attribute = ATTR_SIMPLE;
1772 c->Request.Type.Direction = XFER_WRITE;
1773 c->Request.Timeout = 0;
1774 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001775 c->Request.CDB[0] = cmd; /* reset */
1776 c->Request.CDB[1] = 0x04; /* reset a LUN */
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001777 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 case 3: /* No-Op message */
1779 c->Request.CDBLen = 1;
1780 c->Request.Type.Attribute = ATTR_SIMPLE;
1781 c->Request.Type.Direction = XFER_WRITE;
1782 c->Request.Timeout = 0;
1783 c->Request.CDB[0] = cmd;
1784 break;
1785 default:
1786 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001787 "cciss%d: unknown message type %d\n", ctlr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return IO_ERROR;
1789 }
1790 } else {
1791 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001792 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return IO_ERROR;
1794 }
1795 /* Fill in the scatter gather information */
1796 if (size > 0) {
1797 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001798 buff, size,
1799 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1801 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1802 c->SG[0].Len = size;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001803 c->SG[0].Ext = 0; /* we are not chaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805 return status;
1806}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001807
1808static int sendcmd_withirq(__u8 cmd,
1809 int ctlr,
1810 void *buff,
1811 size_t size,
1812 unsigned int use_unit_num,
1813 unsigned int log_unit, __u8 page_code, int cmd_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 ctlr_info_t *h = hba[ctlr];
1816 CommandList_struct *c;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001817 u64bit buff_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 unsigned long flags;
1819 int return_status;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001820 DECLARE_COMPLETION_ONSTACK(wait);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001821
1822 if ((c = cmd_alloc(h, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return -ENOMEM;
1824 return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001825 log_unit, page_code, NULL, cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (return_status != IO_OK) {
1827 cmd_free(h, c, 0);
1828 return return_status;
1829 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001830 resend_cmd2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 c->waiting = &wait;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /* Put the request on the tail of the queue and send it */
1834 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1835 addQ(&h->reqQ, c);
1836 h->Qdepth++;
1837 start_io(h);
1838 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 wait_for_completion(&wait);
1841
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001842 if (c->err_info->CommandStatus != 0) { /* an error has occurred */
1843 switch (c->err_info->CommandStatus) {
1844 case CMD_TARGET_STATUS:
1845 printk(KERN_WARNING "cciss: cmd %p has "
1846 " completed with errors\n", c);
1847 if (c->err_info->ScsiStatus) {
1848 printk(KERN_WARNING "cciss: cmd %p "
1849 "has SCSI Status = %x\n",
1850 c, c->err_info->ScsiStatus);
1851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001854 case CMD_DATA_UNDERRUN:
1855 case CMD_DATA_OVERRUN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 /* expected for inquire and report lun commands */
1857 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001858 case CMD_INVALID:
1859 printk(KERN_WARNING "cciss: Cmd %p is "
1860 "reported invalid\n", c);
1861 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001863 case CMD_PROTOCOL_ERR:
1864 printk(KERN_WARNING "cciss: cmd %p has "
1865 "protocol error \n", c);
1866 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001868 case CMD_HARDWARE_ERR:
1869 printk(KERN_WARNING "cciss: cmd %p had "
1870 " hardware error\n", c);
1871 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001873 case CMD_CONNECTION_LOST:
1874 printk(KERN_WARNING "cciss: cmd %p had "
1875 "connection lost\n", c);
1876 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001878 case CMD_ABORTED:
1879 printk(KERN_WARNING "cciss: cmd %p was "
1880 "aborted\n", c);
1881 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001883 case CMD_ABORT_FAILED:
1884 printk(KERN_WARNING "cciss: cmd %p reports "
1885 "abort failed\n", c);
1886 return_status = IO_ERROR;
1887 break;
1888 case CMD_UNSOLICITED_ABORT:
1889 printk(KERN_WARNING
1890 "cciss%d: unsolicited abort %p\n", ctlr, c);
1891 if (c->retry_count < MAX_CMD_RETRIES) {
1892 printk(KERN_WARNING
1893 "cciss%d: retrying %p\n", ctlr, c);
1894 c->retry_count++;
1895 /* erase the old error information */
1896 memset(c->err_info, 0,
1897 sizeof(ErrorInfo_struct));
1898 return_status = IO_OK;
1899 INIT_COMPLETION(wait);
1900 goto resend_cmd2;
1901 }
1902 return_status = IO_ERROR;
1903 break;
1904 default:
1905 printk(KERN_WARNING "cciss: cmd %p returned "
1906 "unknown status %x\n", c,
1907 c->err_info->CommandStatus);
1908 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 /* unlock the buffers from DMA */
Mike Millerbb2a37b2005-09-13 01:25:24 -07001912 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
1913 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001914 pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
1915 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 cmd_free(h, c, 0);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001917 return return_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920static void cciss_geometry_inquiry(int ctlr, int logvol,
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001921 int withirq, sector_t total_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001922 unsigned int block_size,
1923 InquiryData_struct *inq_buff,
1924 drive_info_struct *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 int return_code;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001927 unsigned long t;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 memset(inq_buff, 0, sizeof(InquiryData_struct));
1930 if (withirq)
1931 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001932 inq_buff, sizeof(*inq_buff), 1,
1933 logvol, 0xC1, TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 else
1935 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001936 sizeof(*inq_buff), 1, logvol, 0xC1, NULL,
1937 TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (return_code == IO_OK) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001939 if (inq_buff->data_byte[8] == 0xFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001941 "cciss: reading geometry failed, volume "
1942 "does not support reading geometry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 drv->heads = 255;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001944 drv->sectors = 32; // Sectors per track
Mike Miller (OS Dev)7f42d3b2007-04-04 19:08:23 -07001945 drv->cylinders = total_size + 1;
Mike Miller89f97ad2006-12-18 10:59:39 +01001946 drv->raid_level = RAID_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 drv->heads = inq_buff->data_byte[6];
1949 drv->sectors = inq_buff->data_byte[7];
1950 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
1951 drv->cylinders += inq_buff->data_byte[5];
1952 drv->raid_level = inq_buff->data_byte[8];
Matthew Wilcox3f7705e2006-10-21 10:24:19 -07001953 }
1954 drv->block_size = block_size;
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001955 drv->nr_blocks = total_size + 1;
Matthew Wilcox3f7705e2006-10-21 10:24:19 -07001956 t = drv->heads * drv->sectors;
1957 if (t > 1) {
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001958 sector_t real_size = total_size + 1;
1959 unsigned long rem = sector_div(real_size, t);
Matthew Wilcox3f7705e2006-10-21 10:24:19 -07001960 if (rem)
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001961 real_size++;
1962 drv->cylinders = real_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001964 } else { /* Get geometry failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 printk(KERN_WARNING "cciss: reading geometry failed\n");
1966 }
Metathronius Galabantcc088d12006-09-30 23:27:47 -07001967 printk(KERN_INFO " heads=%d, sectors=%d, cylinders=%d\n\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001968 drv->heads, drv->sectors, drv->cylinders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971static void
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001972cciss_read_capacity(int ctlr, int logvol, int withirq, sector_t *total_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001973 unsigned int *block_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001975 ReadCapdata_struct *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 int return_code;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001977 buf = kmalloc(sizeof(ReadCapdata_struct), GFP_KERNEL);
1978 if (buf == NULL) {
1979 printk(KERN_WARNING "cciss: out of memory\n");
1980 return;
1981 }
1982 memset(buf, 0, sizeof(ReadCapdata_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (withirq)
1984 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001985 ctlr, buf, sizeof(ReadCapdata_struct),
1986 1, logvol, 0, TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 else
1988 return_code = sendcmd(CCISS_READ_CAPACITY,
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001989 ctlr, buf, sizeof(ReadCapdata_struct),
1990 1, logvol, 0, NULL, TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (return_code == IO_OK) {
Al Viro4c1f2b32007-03-14 09:19:10 +00001992 *total_size = be32_to_cpu(*(__be32 *) buf->total_size);
1993 *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001994 } else { /* read capacity command failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 printk(KERN_WARNING "cciss: read capacity failed\n");
1996 *total_size = 0;
1997 *block_size = BLOCK_SIZE;
1998 }
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001999 if (*total_size != 0)
Randy Dunlap7b92aad2006-10-28 10:38:40 -07002000 printk(KERN_INFO " blocks= %llu block_size= %d\n",
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08002001 (unsigned long long)*total_size+1, *block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002002 kfree(buf);
2003 return;
2004}
2005
2006static void
2007cciss_read_capacity_16(int ctlr, int logvol, int withirq, sector_t *total_size, unsigned int *block_size)
2008{
2009 ReadCapdata_struct_16 *buf;
2010 int return_code;
2011 buf = kmalloc(sizeof(ReadCapdata_struct_16), GFP_KERNEL);
2012 if (buf == NULL) {
2013 printk(KERN_WARNING "cciss: out of memory\n");
2014 return;
2015 }
2016 memset(buf, 0, sizeof(ReadCapdata_struct_16));
2017 if (withirq) {
2018 return_code = sendcmd_withirq(CCISS_READ_CAPACITY_16,
2019 ctlr, buf, sizeof(ReadCapdata_struct_16),
2020 1, logvol, 0, TYPE_CMD);
2021 }
2022 else {
2023 return_code = sendcmd(CCISS_READ_CAPACITY_16,
2024 ctlr, buf, sizeof(ReadCapdata_struct_16),
2025 1, logvol, 0, NULL, TYPE_CMD);
2026 }
2027 if (return_code == IO_OK) {
Al Viro4c1f2b32007-03-14 09:19:10 +00002028 *total_size = be64_to_cpu(*(__be64 *) buf->total_size);
2029 *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002030 } else { /* read capacity command failed */
2031 printk(KERN_WARNING "cciss: read capacity failed\n");
2032 *total_size = 0;
2033 *block_size = BLOCK_SIZE;
2034 }
Randy Dunlap7b92aad2006-10-28 10:38:40 -07002035 printk(KERN_INFO " blocks= %llu block_size= %d\n",
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08002036 (unsigned long long)*total_size+1, *block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002037 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return;
2039}
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041static int cciss_revalidate(struct gendisk *disk)
2042{
2043 ctlr_info_t *h = get_host(disk);
2044 drive_info_struct *drv = get_drv(disk);
2045 int logvol;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002046 int FOUND = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 unsigned int block_size;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002048 sector_t total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 InquiryData_struct *inq_buff = NULL;
2050
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002051 for (logvol = 0; logvol < CISS_MAX_LUN; logvol++) {
2052 if (h->drv[logvol].LunID == drv->LunID) {
2053 FOUND = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 break;
2055 }
2056 }
2057
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002058 if (!FOUND)
2059 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002061 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
2062 if (inq_buff == NULL) {
2063 printk(KERN_WARNING "cciss: out of memory\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002064 return 1;
2065 }
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002066 if (h->cciss_read == CCISS_READ_10) {
2067 cciss_read_capacity(h->ctlr, logvol, 1,
2068 &total_size, &block_size);
2069 } else {
2070 cciss_read_capacity_16(h->ctlr, logvol, 1,
2071 &total_size, &block_size);
2072 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002073 cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size,
2074 inq_buff, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Mike Millerad2b9312005-07-28 01:07:31 -07002076 blk_queue_hardsect_size(drv->queue, drv->block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 set_capacity(disk, drv->nr_blocks);
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 kfree(inq_buff);
2080 return 0;
2081}
2082
2083/*
2084 * Wait polling for a command to complete.
2085 * The memory mapped FIFO is polled for the completion.
2086 * Used only at init time, interrupts from the HBA are disabled.
2087 */
2088static unsigned long pollcomplete(int ctlr)
2089{
2090 unsigned long done;
2091 int i;
2092
2093 /* Wait (up to 20 seconds) for a command to complete */
2094
2095 for (i = 20 * HZ; i > 0; i--) {
2096 done = hba[ctlr]->access.command_completed(hba[ctlr]);
Nishanth Aravamudan86e84862005-09-10 00:27:28 -07002097 if (done == FIFO_EMPTY)
2098 schedule_timeout_uninterruptible(1);
2099 else
Bjorn Helgaase2019b52006-06-25 05:49:05 -07002100 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
2102 /* Invalid address to tell caller we ran out of time */
2103 return 1;
2104}
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002105
2106static int add_sendcmd_reject(__u8 cmd, int ctlr, unsigned long complete)
2107{
2108 /* We get in here if sendcmd() is polling for completions
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002109 and gets some command back that it wasn't expecting --
2110 something other than that which it just sent down.
2111 Ordinarily, that shouldn't happen, but it can happen when
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002112 the scsi tape stuff gets into error handling mode, and
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002113 starts using sendcmd() to try to abort commands and
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002114 reset tape drives. In that case, sendcmd may pick up
2115 completions of commands that were sent to logical drives
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002116 through the block i/o system, or cciss ioctls completing, etc.
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002117 In that case, we need to save those completions for later
2118 processing by the interrupt handler.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002119 */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002120
2121#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002122 struct sendcmd_reject_list *srl = &hba[ctlr]->scsi_rejects;
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002123
2124 /* If it's not the scsi tape stuff doing error handling, (abort */
2125 /* or reset) then we don't expect anything weird. */
2126 if (cmd != CCISS_RESET_MSG && cmd != CCISS_ABORT_MSG) {
2127#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002128 printk(KERN_WARNING "cciss cciss%d: SendCmd "
2129 "Invalid command list address returned! (%lx)\n",
2130 ctlr, complete);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002131 /* not much we can do. */
2132#ifdef CONFIG_CISS_SCSI_TAPE
2133 return 1;
2134 }
2135
2136 /* We've sent down an abort or reset, but something else
2137 has completed */
Mike Millerf8806322006-12-06 20:35:01 -08002138 if (srl->ncompletions >= (hba[ctlr]->nr_cmds + 2)) {
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002139 /* Uh oh. No room to save it for later... */
2140 printk(KERN_WARNING "cciss%d: Sendcmd: Invalid command addr, "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002141 "reject list overflow, command lost!\n", ctlr);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002142 return 1;
2143 }
2144 /* Save it for later */
2145 srl->complete[srl->ncompletions] = complete;
2146 srl->ncompletions++;
2147#endif
2148 return 0;
2149}
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151/*
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002152 * Send a command to the controller, and wait for it to complete.
2153 * Only used at init time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002155static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, /* 0: address the controller,
2156 1: address logical volume log_unit,
2157 2: periph device address is scsi3addr */
2158 unsigned int log_unit,
2159 __u8 page_code, unsigned char *scsi3addr, int cmd_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 CommandList_struct *c;
2162 int i;
2163 unsigned long complete;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002164 ctlr_info_t *info_p = hba[ctlr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 u64bit buff_dma_handle;
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002166 int status, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 if ((c = cmd_alloc(info_p, 1)) == NULL) {
2169 printk(KERN_WARNING "cciss: unable to get memory");
Bjorn Helgaase2019b52006-06-25 05:49:05 -07002170 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172 status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002173 log_unit, page_code, scsi3addr, cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (status != IO_OK) {
2175 cmd_free(info_p, c, 1);
2176 return status;
2177 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002178 resend_cmd1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /*
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002180 * Disable interrupt
2181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182#ifdef CCISS_DEBUG
2183 printk(KERN_DEBUG "cciss: turning intr off\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002184#endif /* CCISS_DEBUG */
2185 info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 /* Make sure there is room in the command FIFO */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002188 /* Actually it should be completely empty at this time */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002189 /* unless we are in here doing error handling for the scsi */
2190 /* tape side of the driver. */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002191 for (i = 200000; i > 0; i--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /* if fifo isn't full go */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002193 if (!(info_p->access.fifo_full(info_p))) {
2194
2195 break;
2196 }
2197 udelay(10);
2198 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
2199 " waiting!\n", ctlr);
2200 }
2201 /*
2202 * Send the cmd
2203 */
2204 info_p->access.submit_command(info_p, c);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002205 done = 0;
2206 do {
2207 complete = pollcomplete(ctlr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209#ifdef CCISS_DEBUG
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002210 printk(KERN_DEBUG "cciss: command completed\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002211#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002213 if (complete == 1) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002214 printk(KERN_WARNING
2215 "cciss cciss%d: SendCmd Timeout out, "
2216 "No command list address returned!\n", ctlr);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002217 status = IO_ERROR;
2218 done = 1;
2219 break;
2220 }
2221
2222 /* This will need to change for direct lookup completions */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002223 if ((complete & CISS_ERROR_BIT)
2224 && (complete & ~CISS_ERROR_BIT) == c->busaddr) {
2225 /* if data overrun or underun on Report command
2226 ignore it
2227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
2229 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
2230 (c->Request.CDB[0] == CISS_INQUIRY)) &&
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002231 ((c->err_info->CommandStatus ==
2232 CMD_DATA_OVERRUN) ||
2233 (c->err_info->CommandStatus == CMD_DATA_UNDERRUN)
2234 )) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 complete = c->busaddr;
2236 } else {
2237 if (c->err_info->CommandStatus ==
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002238 CMD_UNSOLICITED_ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 printk(KERN_WARNING "cciss%d: "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002240 "unsolicited abort %p\n",
2241 ctlr, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (c->retry_count < MAX_CMD_RETRIES) {
2243 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002244 "cciss%d: retrying %p\n",
2245 ctlr, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 c->retry_count++;
2247 /* erase the old error */
2248 /* information */
2249 memset(c->err_info, 0,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002250 sizeof
2251 (ErrorInfo_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 goto resend_cmd1;
2253 } else {
2254 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002255 "cciss%d: retried %p too "
2256 "many times\n", ctlr, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 status = IO_ERROR;
2258 goto cleanup1;
2259 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002260 } else if (c->err_info->CommandStatus ==
2261 CMD_UNABORTABLE) {
2262 printk(KERN_WARNING
2263 "cciss%d: command could not be aborted.\n",
2264 ctlr);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002265 status = IO_ERROR;
2266 goto cleanup1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
2268 printk(KERN_WARNING "ciss ciss%d: sendcmd"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002269 " Error %x \n", ctlr,
2270 c->err_info->CommandStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 printk(KERN_WARNING "ciss ciss%d: sendcmd"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002272 " offensive info\n"
2273 " size %x\n num %x value %x\n",
2274 ctlr,
2275 c->err_info->MoreErrInfo.Invalid_Cmd.
2276 offense_size,
2277 c->err_info->MoreErrInfo.Invalid_Cmd.
2278 offense_num,
2279 c->err_info->MoreErrInfo.Invalid_Cmd.
2280 offense_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 status = IO_ERROR;
2282 goto cleanup1;
2283 }
2284 }
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002285 /* This will need changing for direct lookup completions */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002286 if (complete != c->busaddr) {
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002287 if (add_sendcmd_reject(cmd, ctlr, complete) != 0) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002288 BUG(); /* we are pretty much hosed if we get here. */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002289 }
2290 continue;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002291 } else
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002292 done = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002293 } while (!done);
2294
2295 cleanup1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /* unlock the data buffer from DMA */
Mike Millerbb2a37b2005-09-13 01:25:24 -07002297 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
2298 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002300 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002301#ifdef CONFIG_CISS_SCSI_TAPE
2302 /* if we saved some commands for later, process them now. */
2303 if (info_p->scsi_rejects.ncompletions > 0)
David Howells7d12e782006-10-05 14:55:46 +01002304 do_cciss_intr(0, info_p);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002305#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 cmd_free(info_p, c, 1);
Bjorn Helgaase2019b52006-06-25 05:49:05 -07002307 return status;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002308}
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310/*
2311 * Map (physical) PCI mem into (virtual) kernel space
2312 */
2313static void __iomem *remap_pci_mem(ulong base, ulong size)
2314{
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002315 ulong page_base = ((ulong) base) & PAGE_MASK;
2316 ulong page_offs = ((ulong) base) - page_base;
2317 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002319 return page_remapped ? (page_remapped + page_offs) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002322/*
2323 * Takes jobs of the Q and sends them to the hardware, then puts it on
2324 * the Q to wait for completion.
2325 */
2326static void start_io(ctlr_info_t *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 CommandList_struct *c;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002329
2330 while ((c = h->reqQ) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 /* can't do anything if fifo is full */
2332 if ((h->access.fifo_full(h))) {
2333 printk(KERN_WARNING "cciss: fifo full\n");
2334 break;
2335 }
2336
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002337 /* Get the first entry from the Request Q */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 removeQ(&(h->reqQ), c);
2339 h->Qdepth--;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002340
2341 /* Tell the controller execute command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 h->access.submit_command(h, c);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002343
2344 /* Put job onto the completed Q */
2345 addQ(&(h->cmpQ), c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349/* Assumes that CCISS_LOCK(h->ctlr) is held. */
2350/* Zeros out the error record and then resends the command back */
2351/* to the controller */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002352static inline void resend_cciss_cmd(ctlr_info_t *h, CommandList_struct *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
2354 /* erase the old error information */
2355 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
2356
2357 /* add it to software queue and then send it to the controller */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002358 addQ(&(h->reqQ), c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 h->Qdepth++;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002360 if (h->Qdepth > h->maxQsinceinit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 h->maxQsinceinit = h->Qdepth;
2362
2363 start_io(h);
2364}
Jens Axboea9925a02006-01-09 16:04:06 +01002365
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002366static inline int evaluate_target_status(CommandList_struct *cmd)
2367{
2368 unsigned char sense_key;
2369 int status = 0; /* 0 means bad, 1 means good. */
2370
2371 if (cmd->err_info->ScsiStatus != 0x02) { /* not check condition? */
2372 if (!blk_pc_request(cmd->rq))
2373 printk(KERN_WARNING "cciss: cmd %p "
2374 "has SCSI Status 0x%x\n",
2375 cmd, cmd->err_info->ScsiStatus);
2376 return status;
2377 }
2378
2379 /* check the sense key */
2380 sense_key = 0xf & cmd->err_info->SenseInfo[2];
2381 /* no status or recovered error */
2382 if ((sense_key == 0x0) || (sense_key == 0x1))
2383 status = 1;
2384
2385 if (!blk_pc_request(cmd->rq)) { /* Not SG_IO or similar? */
2386 if (status == 0)
2387 printk(KERN_WARNING "cciss: cmd %p has CHECK CONDITION"
2388 " sense key = 0x%x\n", cmd, sense_key);
2389 return status;
2390 }
2391
2392 /* SG_IO or similar, copy sense data back */
2393 if (cmd->rq->sense) {
2394 if (cmd->rq->sense_len > cmd->err_info->SenseLen)
2395 cmd->rq->sense_len = cmd->err_info->SenseLen;
2396 memcpy(cmd->rq->sense, cmd->err_info->SenseInfo,
2397 cmd->rq->sense_len);
2398 } else
2399 cmd->rq->sense_len = 0;
2400
2401 return status;
2402}
2403
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002404/* checks the status of the job and calls complete buffers to mark all
Jens Axboea9925a02006-01-09 16:04:06 +01002405 * buffers for the completed job. Note that this function does not need
2406 * to hold the hba/queue lock.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002407 */
2408static inline void complete_command(ctlr_info_t *h, CommandList_struct *cmd,
2409 int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
2411 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 int retry_cmd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002414 if (timeout)
2415 status = 0;
2416
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002417 if (cmd->err_info->CommandStatus == 0) /* no error has occurred */
2418 goto after_error_processing;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002419
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002420 switch (cmd->err_info->CommandStatus) {
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002421 case CMD_TARGET_STATUS:
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002422 status = evaluate_target_status(cmd);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002423 break;
2424 case CMD_DATA_UNDERRUN:
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002425 if (blk_fs_request(cmd->rq)) {
2426 printk(KERN_WARNING "cciss: cmd %p has"
2427 " completed with data underrun "
2428 "reported\n", cmd);
2429 cmd->rq->data_len = cmd->err_info->ResidualCnt;
2430 }
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002431 break;
2432 case CMD_DATA_OVERRUN:
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002433 if (blk_fs_request(cmd->rq))
2434 printk(KERN_WARNING "cciss: cmd %p has"
2435 " completed with data overrun "
2436 "reported\n", cmd);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002437 break;
2438 case CMD_INVALID:
2439 printk(KERN_WARNING "cciss: cmd %p is "
2440 "reported invalid\n", cmd);
2441 status = 0;
2442 break;
2443 case CMD_PROTOCOL_ERR:
2444 printk(KERN_WARNING "cciss: cmd %p has "
2445 "protocol error \n", cmd);
2446 status = 0;
2447 break;
2448 case CMD_HARDWARE_ERR:
2449 printk(KERN_WARNING "cciss: cmd %p had "
2450 " hardware error\n", cmd);
2451 status = 0;
2452 break;
2453 case CMD_CONNECTION_LOST:
2454 printk(KERN_WARNING "cciss: cmd %p had "
2455 "connection lost\n", cmd);
2456 status = 0;
2457 break;
2458 case CMD_ABORTED:
2459 printk(KERN_WARNING "cciss: cmd %p was "
2460 "aborted\n", cmd);
2461 status = 0;
2462 break;
2463 case CMD_ABORT_FAILED:
2464 printk(KERN_WARNING "cciss: cmd %p reports "
2465 "abort failed\n", cmd);
2466 status = 0;
2467 break;
2468 case CMD_UNSOLICITED_ABORT:
2469 printk(KERN_WARNING "cciss%d: unsolicited "
2470 "abort %p\n", h->ctlr, cmd);
2471 if (cmd->retry_count < MAX_CMD_RETRIES) {
2472 retry_cmd = 1;
2473 printk(KERN_WARNING
2474 "cciss%d: retrying %p\n", h->ctlr, cmd);
2475 cmd->retry_count++;
2476 } else
2477 printk(KERN_WARNING
2478 "cciss%d: %p retried too "
2479 "many times\n", h->ctlr, cmd);
2480 status = 0;
2481 break;
2482 case CMD_TIMEOUT:
2483 printk(KERN_WARNING "cciss: cmd %p timedout\n", cmd);
2484 status = 0;
2485 break;
2486 default:
2487 printk(KERN_WARNING "cciss: cmd %p returned "
2488 "unknown status %x\n", cmd,
2489 cmd->err_info->CommandStatus);
2490 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002492
2493after_error_processing:
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 /* We need to return this command */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002496 if (retry_cmd) {
2497 resend_cciss_cmd(h, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002499 }
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002500 cmd->rq->data_len = 0;
Jens Axboea9925a02006-01-09 16:04:06 +01002501 cmd->rq->errors = status;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002502 cmd->rq->completion_data = cmd;
Jens Axboe2056a782006-03-23 20:00:26 +01002503 blk_add_trace_rq(cmd->rq->q, cmd->rq, BLK_TA_COMPLETE);
Jens Axboea9925a02006-01-09 16:04:06 +01002504 blk_complete_request(cmd->rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002507/*
2508 * Get a request and submit it to the controller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 */
2510static void do_cciss_request(request_queue_t *q)
2511{
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002512 ctlr_info_t *h = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 CommandList_struct *c;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002514 sector_t start_blk;
2515 int seg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 struct request *creq;
2517 u64bit temp64;
2518 struct scatterlist tmp_sg[MAXSGENTRIES];
2519 drive_info_struct *drv;
2520 int i, dir;
2521
2522 /* We call start_io here in case there is a command waiting on the
2523 * queue that has not been sent.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002524 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (blk_queue_plugged(q))
2526 goto startio;
2527
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002528 queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 creq = elv_next_request(q);
2530 if (!creq)
2531 goto startio;
2532
Eric Sesterhenn089fe1b2006-03-24 18:50:27 +01002533 BUG_ON(creq->nr_phys_segments > MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002535 if ((c = cmd_alloc(h, 1)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 goto full;
2537
2538 blkdev_dequeue_request(creq);
2539
2540 spin_unlock_irq(q->queue_lock);
2541
2542 c->cmd_type = CMD_RWREQ;
2543 c->rq = creq;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002544
2545 /* fill in the request */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 drv = creq->rq_disk->private_data;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002547 c->Header.ReplyQueue = 0; // unused in simple mode
Mike Miller33079b22005-09-13 01:25:22 -07002548 /* got command from pool, so use the command block index instead */
2549 /* for direct lookups. */
2550 /* The first 2 bits are reserved for controller error reporting. */
2551 c->Header.Tag.lower = (c->cmdindex << 3);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002552 c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */
2553 c->Header.LUN.LogDev.VolId = drv->LunID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 c->Header.LUN.LogDev.Mode = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002555 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
2556 c->Request.Type.Type = TYPE_CMD; // It is a command.
2557 c->Request.Type.Attribute = ATTR_SIMPLE;
2558 c->Request.Type.Direction =
Mike Millera52de242006-12-18 11:00:14 +01002559 (rq_data_dir(creq) == READ) ? XFER_READ : XFER_WRITE;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002560 c->Request.Timeout = 0; // Don't time out
2561 c->Request.CDB[0] =
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002562 (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 start_blk = creq->sector;
2564#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002565 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n", (int)creq->sector,
2566 (int)creq->nr_sectors);
2567#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 seg = blk_rq_map_sg(q, creq, tmp_sg);
2570
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002571 /* get the DMA records for the setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 if (c->Request.Type.Direction == XFER_READ)
2573 dir = PCI_DMA_FROMDEVICE;
2574 else
2575 dir = PCI_DMA_TODEVICE;
2576
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002577 for (i = 0; i < seg; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 c->SG[i].Len = tmp_sg[i].length;
2579 temp64.val = (__u64) pci_map_page(h->pdev, tmp_sg[i].page,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002580 tmp_sg[i].offset,
2581 tmp_sg[i].length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 c->SG[i].Addr.lower = temp64.val32.lower;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002583 c->SG[i].Addr.upper = temp64.val32.upper;
2584 c->SG[i].Ext = 0; // we are not chaining
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002586 /* track how many SG entries we are using */
2587 if (seg > h->maxSG)
2588 h->maxSG = seg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002591 printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n",
2592 creq->nr_sectors, seg);
2593#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 c->Header.SGList = c->Header.SGTotal = seg;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002596 if (likely(blk_fs_request(creq))) {
2597 if(h->cciss_read == CCISS_READ_10) {
2598 c->Request.CDB[1] = 0;
2599 c->Request.CDB[2] = (start_blk >> 24) & 0xff; //MSB
2600 c->Request.CDB[3] = (start_blk >> 16) & 0xff;
2601 c->Request.CDB[4] = (start_blk >> 8) & 0xff;
2602 c->Request.CDB[5] = start_blk & 0xff;
2603 c->Request.CDB[6] = 0; // (sect >> 24) & 0xff; MSB
2604 c->Request.CDB[7] = (creq->nr_sectors >> 8) & 0xff;
2605 c->Request.CDB[8] = creq->nr_sectors & 0xff;
2606 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2607 } else {
2608 c->Request.CDBLen = 16;
2609 c->Request.CDB[1]= 0;
2610 c->Request.CDB[2]= (start_blk >> 56) & 0xff; //MSB
2611 c->Request.CDB[3]= (start_blk >> 48) & 0xff;
2612 c->Request.CDB[4]= (start_blk >> 40) & 0xff;
2613 c->Request.CDB[5]= (start_blk >> 32) & 0xff;
2614 c->Request.CDB[6]= (start_blk >> 24) & 0xff;
2615 c->Request.CDB[7]= (start_blk >> 16) & 0xff;
2616 c->Request.CDB[8]= (start_blk >> 8) & 0xff;
2617 c->Request.CDB[9]= start_blk & 0xff;
2618 c->Request.CDB[10]= (creq->nr_sectors >> 24) & 0xff;
2619 c->Request.CDB[11]= (creq->nr_sectors >> 16) & 0xff;
2620 c->Request.CDB[12]= (creq->nr_sectors >> 8) & 0xff;
2621 c->Request.CDB[13]= creq->nr_sectors & 0xff;
2622 c->Request.CDB[14] = c->Request.CDB[15] = 0;
2623 }
2624 } else if (blk_pc_request(creq)) {
2625 c->Request.CDBLen = creq->cmd_len;
2626 memcpy(c->Request.CDB, creq->cmd, BLK_MAX_CDB);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002627 } else {
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002628 printk(KERN_WARNING "cciss%d: bad request type %d\n", h->ctlr, creq->cmd_type);
2629 BUG();
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 spin_lock_irq(q->queue_lock);
2633
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002634 addQ(&(h->reqQ), c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 h->Qdepth++;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002636 if (h->Qdepth > h->maxQsinceinit)
2637 h->maxQsinceinit = h->Qdepth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 goto queue;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002640full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 blk_stop_queue(q);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002642startio:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /* We will already have the driver lock here so not need
2644 * to lock it.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 start_io(h);
2647}
2648
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002649static inline unsigned long get_next_completion(ctlr_info_t *h)
2650{
2651#ifdef CONFIG_CISS_SCSI_TAPE
2652 /* Any rejects from sendcmd() lying around? Process them first */
2653 if (h->scsi_rejects.ncompletions == 0)
2654 return h->access.command_completed(h);
2655 else {
2656 struct sendcmd_reject_list *srl;
2657 int n;
2658 srl = &h->scsi_rejects;
2659 n = --srl->ncompletions;
2660 /* printk("cciss%d: processing saved reject\n", h->ctlr); */
2661 printk("p");
2662 return srl->complete[n];
2663 }
2664#else
2665 return h->access.command_completed(h);
2666#endif
2667}
2668
2669static inline int interrupt_pending(ctlr_info_t *h)
2670{
2671#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002672 return (h->access.intr_pending(h)
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002673 || (h->scsi_rejects.ncompletions > 0));
2674#else
2675 return h->access.intr_pending(h);
2676#endif
2677}
2678
2679static inline long interrupt_not_for_us(ctlr_info_t *h)
2680{
2681#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002682 return (((h->access.intr_pending(h) == 0) ||
2683 (h->interrupts_enabled == 0))
2684 && (h->scsi_rejects.ncompletions == 0));
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002685#else
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002686 return (((h->access.intr_pending(h) == 0) ||
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002687 (h->interrupts_enabled == 0)));
2688#endif
2689}
2690
David Howells7d12e782006-10-05 14:55:46 +01002691static irqreturn_t do_cciss_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
2693 ctlr_info_t *h = dev_id;
2694 CommandList_struct *c;
2695 unsigned long flags;
Mike Miller33079b22005-09-13 01:25:22 -07002696 __u32 a, a1, a2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002698 if (interrupt_not_for_us(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 /*
2701 * If there are completed commands in the completion queue,
2702 * we had better do something about it.
2703 */
2704 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002705 while (interrupt_pending(h)) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002706 while ((a = get_next_completion(h)) != FIFO_EMPTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 a1 = a;
Mike Miller33079b22005-09-13 01:25:22 -07002708 if ((a & 0x04)) {
2709 a2 = (a >> 3);
Mike Millerf8806322006-12-06 20:35:01 -08002710 if (a2 >= h->nr_cmds) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002711 printk(KERN_WARNING
2712 "cciss: controller cciss%d failed, stopping.\n",
2713 h->ctlr);
Mike Miller33079b22005-09-13 01:25:22 -07002714 fail_all_cmds(h->ctlr);
2715 return IRQ_HANDLED;
2716 }
2717
2718 c = h->cmd_pool + a2;
2719 a = c->busaddr;
2720
2721 } else {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002722 a &= ~3;
Mike Miller33079b22005-09-13 01:25:22 -07002723 if ((c = h->cmpQ) == NULL) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002724 printk(KERN_WARNING
2725 "cciss: Completion of %08x ignored\n",
2726 a1);
2727 continue;
2728 }
2729 while (c->busaddr != a) {
2730 c = c->next;
2731 if (c == h->cmpQ)
2732 break;
2733 }
Mike Miller33079b22005-09-13 01:25:22 -07002734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 /*
2736 * If we've found the command, take it off the
2737 * completion Q and free it
2738 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002739 if (c->busaddr == a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 removeQ(&h->cmpQ, c);
2741 if (c->cmd_type == CMD_RWREQ) {
2742 complete_command(h, c, 0);
2743 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2744 complete(c->waiting);
2745 }
2746# ifdef CONFIG_CISS_SCSI_TAPE
2747 else if (c->cmd_type == CMD_SCSI)
2748 complete_scsi_command(c, 0, a1);
2749# endif
2750 continue;
2751 }
2752 }
2753 }
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2756 return IRQ_HANDLED;
2757}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002758
2759/*
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07002760 * We cannot read the structure directly, for portability we must use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 * the io functions.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002762 * This is for debug only.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 */
2764#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002765static void print_cfg_table(CfgTable_struct *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
2767 int i;
2768 char temp_name[17];
2769
2770 printk("Controller Configuration information\n");
2771 printk("------------------------------------\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002772 for (i = 0; i < 4; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 temp_name[i] = readb(&(tb->Signature[i]));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002774 temp_name[4] = '\0';
2775 printk(" Signature = %s\n", temp_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002777 printk(" Transport methods supported = 0x%x\n",
2778 readl(&(tb->TransportSupport)));
2779 printk(" Transport methods active = 0x%x\n",
2780 readl(&(tb->TransportActive)));
2781 printk(" Requested transport Method = 0x%x\n",
2782 readl(&(tb->HostWrite.TransportRequest)));
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07002783 printk(" Coalesce Interrupt Delay = 0x%x\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002784 readl(&(tb->HostWrite.CoalIntDelay)));
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07002785 printk(" Coalesce Interrupt Count = 0x%x\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002786 readl(&(tb->HostWrite.CoalIntCount)));
2787 printk(" Max outstanding commands = 0x%d\n",
2788 readl(&(tb->CmdsOutMax)));
2789 printk(" Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
2790 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 temp_name[i] = readb(&(tb->ServerName[i]));
2792 temp_name[16] = '\0';
2793 printk(" Server Name = %s\n", temp_name);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002794 printk(" Heartbeat Counter = 0x%x\n\n\n", readl(&(tb->HeartBeat)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002796#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002798static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
2800 int i, offset, mem_type, bar_type;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002801 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 return 0;
2803 offset = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002804 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2805 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
2807 offset += 4;
2808 else {
2809 mem_type = pci_resource_flags(pdev, i) &
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002810 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 switch (mem_type) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002812 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2813 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
2814 offset += 4; /* 32 bit */
2815 break;
2816 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2817 offset += 8;
2818 break;
2819 default: /* reserved in PCI 2.2 */
2820 printk(KERN_WARNING
2821 "Base address is invalid\n");
2822 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 break;
2824 }
2825 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002826 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
2827 return i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
2829 return -1;
2830}
2831
Mike Millerfb86a352006-01-08 01:03:50 -08002832/* If MSI/MSI-X is supported by the kernel we will try to enable it on
2833 * controllers that are capable. If not, we use IO-APIC mode.
2834 */
2835
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002836static void __devinit cciss_interrupt_mode(ctlr_info_t *c,
2837 struct pci_dev *pdev, __u32 board_id)
Mike Millerfb86a352006-01-08 01:03:50 -08002838{
2839#ifdef CONFIG_PCI_MSI
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002840 int err;
2841 struct msix_entry cciss_msix_entries[4] = { {0, 0}, {0, 1},
2842 {0, 2}, {0, 3}
2843 };
Mike Millerfb86a352006-01-08 01:03:50 -08002844
2845 /* Some boards advertise MSI but don't really support it */
2846 if ((board_id == 0x40700E11) ||
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002847 (board_id == 0x40800E11) ||
2848 (board_id == 0x40820E11) || (board_id == 0x40830E11))
Mike Millerfb86a352006-01-08 01:03:50 -08002849 goto default_int_mode;
2850
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002851 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
2852 err = pci_enable_msix(pdev, cciss_msix_entries, 4);
2853 if (!err) {
2854 c->intr[0] = cciss_msix_entries[0].vector;
2855 c->intr[1] = cciss_msix_entries[1].vector;
2856 c->intr[2] = cciss_msix_entries[2].vector;
2857 c->intr[3] = cciss_msix_entries[3].vector;
2858 c->msix_vector = 1;
2859 return;
2860 }
2861 if (err > 0) {
2862 printk(KERN_WARNING "cciss: only %d MSI-X vectors "
2863 "available\n", err);
Mike Miller1ecb9c02006-12-06 20:35:13 -08002864 goto default_int_mode;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002865 } else {
2866 printk(KERN_WARNING "cciss: MSI-X init failed %d\n",
2867 err);
Mike Miller1ecb9c02006-12-06 20:35:13 -08002868 goto default_int_mode;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002869 }
2870 }
2871 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
2872 if (!pci_enable_msi(pdev)) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002873 c->msi_vector = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002874 } else {
2875 printk(KERN_WARNING "cciss: MSI init failed\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002876 }
2877 }
Mike Miller1ecb9c02006-12-06 20:35:13 -08002878default_int_mode:
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002879#endif /* CONFIG_PCI_MSI */
Mike Millerfb86a352006-01-08 01:03:50 -08002880 /* if we get here we're going to use the default interrupt mode */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002881 c->intr[SIMPLE_MODE_INT] = pdev->irq;
Mike Millerfb86a352006-01-08 01:03:50 -08002882 return;
2883}
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885static int cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
2886{
2887 ushort subsystem_vendor_id, subsystem_device_id, command;
2888 __u32 board_id, scratchpad = 0;
2889 __u64 cfg_offset;
2890 __u32 cfg_base_addr;
2891 __u64 cfg_base_addr_index;
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002892 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894 /* check to see if controller has been disabled */
2895 /* BEFORE trying to enable it */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002896 (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
2897 if (!(command & 0x02)) {
2898 printk(KERN_WARNING
2899 "cciss: controller appears to be disabled\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002900 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
2902
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002903 err = pci_enable_device(pdev);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002904 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002906 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Bjorn Helgaas4e570302006-06-25 05:49:02 -07002909 err = pci_request_regions(pdev, "cciss");
2910 if (err) {
2911 printk(KERN_ERR "cciss: Cannot obtain PCI resources, "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002912 "aborting\n");
Mike Miller872225c2006-12-13 00:34:22 -08002913 return err;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07002914 }
2915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 subsystem_vendor_id = pdev->subsystem_vendor;
2917 subsystem_device_id = pdev->subsystem_device;
2918 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002919 subsystem_vendor_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921#ifdef CCISS_DEBUG
2922 printk("command = %x\n", command);
2923 printk("irq = %x\n", pdev->irq);
2924 printk("board_id = %x\n", board_id);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002925#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Mike Millerfb86a352006-01-08 01:03:50 -08002927/* If the kernel supports MSI/MSI-X we will try to enable that functionality,
2928 * else we use the IO-APIC interrupt assigned to us by system ROM.
2929 */
2930 cciss_interrupt_mode(c, pdev, board_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932 /*
2933 * Memory base addr is first addr , the second points to the config
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002934 * table
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 */
2936
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002937 c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938#ifdef CCISS_DEBUG
2939 printk("address 0 = %x\n", c->paddr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002940#endif /* CCISS_DEBUG */
Mike Millera5b92872006-12-13 00:34:21 -08002941 c->vaddr = remap_pci_mem(c->paddr, 0x250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943 /* Wait for the board to become ready. (PCI hotplug needs this.)
2944 * We poll for up to 120 secs, once per 100ms. */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002945 for (i = 0; i < 1200; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
2947 if (scratchpad == CCISS_FIRMWARE_READY)
2948 break;
2949 set_current_state(TASK_INTERRUPTIBLE);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002950 schedule_timeout(HZ / 10); /* wait 100ms */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
2952 if (scratchpad != CCISS_FIRMWARE_READY) {
2953 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002954 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07002955 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
2957
2958 /* get the address index number */
2959 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
2960 cfg_base_addr &= (__u32) 0x0000ffff;
2961#ifdef CCISS_DEBUG
2962 printk("cfg base address = %x\n", cfg_base_addr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002963#endif /* CCISS_DEBUG */
2964 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965#ifdef CCISS_DEBUG
2966 printk("cfg base address index = %x\n", cfg_base_addr_index);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002967#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 if (cfg_base_addr_index == -1) {
2969 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002970 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07002971 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
2973
2974 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
2975#ifdef CCISS_DEBUG
2976 printk("cfg offset = %x\n", cfg_offset);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002977#endif /* CCISS_DEBUG */
2978 c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
2979 cfg_base_addr_index) +
2980 cfg_offset, sizeof(CfgTable_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 c->board_id = board_id;
2982
2983#ifdef CCISS_DEBUG
Tobias Klauser945f3902006-01-08 01:05:11 -08002984 print_cfg_table(c->cfgtable);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002985#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002987 for (i = 0; i < ARRAY_SIZE(products); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (board_id == products[i].board_id) {
2989 c->product_name = products[i].product_name;
2990 c->access = *(products[i].access);
Mike Millerf8806322006-12-06 20:35:01 -08002991 c->nr_cmds = products[i].nr_cmds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 break;
2993 }
2994 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002995 if ((readb(&c->cfgtable->Signature[0]) != 'C') ||
2996 (readb(&c->cfgtable->Signature[1]) != 'I') ||
2997 (readb(&c->cfgtable->Signature[2]) != 'S') ||
2998 (readb(&c->cfgtable->Signature[3]) != 'S')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 printk("Does not appear to be a valid CISS config table\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003000 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003001 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
Mike Miller4ff9a9a2006-12-06 20:35:00 -08003003 /* We didn't find the controller in our list. We know the
3004 * signature is valid. If it's an HP device let's try to
3005 * bind to the device and fire it up. Otherwise we bail.
3006 */
3007 if (i == ARRAY_SIZE(products)) {
3008 if (subsystem_vendor_id == PCI_VENDOR_ID_HP) {
3009 c->product_name = products[i-1].product_name;
3010 c->access = *(products[i-1].access);
Mike Millerf8806322006-12-06 20:35:01 -08003011 c->nr_cmds = products[i-1].nr_cmds;
Mike Miller4ff9a9a2006-12-06 20:35:00 -08003012 printk(KERN_WARNING "cciss: This is an unknown "
3013 "Smart Array controller.\n"
3014 "cciss: Please update to the latest driver "
3015 "available from www.hp.com.\n");
3016 } else {
3017 printk(KERN_WARNING "cciss: Sorry, I don't know how"
3018 " to access the Smart Array controller %08lx\n"
3019 , (unsigned long)board_id);
3020 err = -ENODEV;
3021 goto err_out_free_res;
3022 }
3023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024#ifdef CONFIG_X86
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003025 {
3026 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3027 __u32 prefetch;
3028 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
3029 prefetch |= 0x100;
3030 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
3031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032#endif
3033
Mike Millerf92e2f52006-12-06 20:35:04 -08003034 /* Disabling DMA prefetch for the P600
3035 * An ASIC bug may result in a prefetch beyond
3036 * physical memory.
3037 */
3038 if(board_id == 0x3225103C) {
3039 __u32 dma_prefetch;
3040 dma_prefetch = readl(c->vaddr + I2O_DMA1_CFG);
3041 dma_prefetch |= 0x8000;
3042 writel(dma_prefetch, c->vaddr + I2O_DMA1_CFG);
3043 }
3044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045#ifdef CCISS_DEBUG
3046 printk("Trying to put board into Simple mode\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003047#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003049 /* Update the field, and then ring the doorbell */
3050 writel(CFGTBL_Trans_Simple, &(c->cfgtable->HostWrite.TransportRequest));
3051 writel(CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
3053 /* under certain very rare conditions, this can take awhile.
3054 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3055 * as we enter this code.) */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003056 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3058 break;
3059 /* delay and try again */
3060 set_current_state(TASK_INTERRUPTIBLE);
3061 schedule_timeout(10);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003065 printk(KERN_DEBUG "I counter got to %d %x\n", i,
3066 readl(c->vaddr + SA5_DOORBELL));
3067#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003069 print_cfg_table(c->cfgtable);
3070#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003072 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 printk(KERN_WARNING "cciss: unable to get board into"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003074 " simple mode\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003075 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003076 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 }
3078 return 0;
3079
Linus Torvalds5faad622006-12-13 09:15:34 -08003080err_out_free_res:
Mike Miller872225c2006-12-13 00:34:22 -08003081 /*
3082 * Deliberately omit pci_disable_device(): it does something nasty to
3083 * Smart Array controllers that pci_enable_device does not undo
3084 */
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003085 pci_release_regions(pdev);
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003086 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
3088
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003089/*
3090 * Gets information about the local volumes attached to the controller.
3091 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092static void cciss_getgeometry(int cntl_num)
3093{
3094 ReportLunData_struct *ld_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 InquiryData_struct *inq_buff;
3096 int return_code;
3097 int i;
3098 int listlength = 0;
3099 __u32 lunid = 0;
3100 int block_size;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003101 sector_t total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Eric Sesterhenn06ff37f2006-03-08 11:21:52 +01003103 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003104 if (ld_buff == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 printk(KERN_ERR "cciss: out of memory\n");
3106 return;
3107 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003108 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
3109 if (inq_buff == NULL) {
3110 printk(KERN_ERR "cciss: out of memory\n");
3111 kfree(ld_buff);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003112 return;
3113 }
3114 /* Get the firmware version */
3115 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
3116 sizeof(InquiryData_struct), 0, 0, 0, NULL,
3117 TYPE_CMD);
3118 if (return_code == IO_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
3120 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
3121 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
3122 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003123 } else { /* send command failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003125 printk(KERN_WARNING "cciss: unable to determine firmware"
3126 " version of controller\n");
3127 }
3128 /* Get the number of logical volumes */
3129 return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
3130 sizeof(ReportLunData_struct), 0, 0, 0, NULL,
3131 TYPE_CMD);
3132
3133 if (return_code == IO_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134#ifdef CCISS_DEBUG
3135 printk("LUN Data\n--------------------------\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003136#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003138 listlength |=
3139 (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
3140 listlength |=
3141 (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
3142 listlength |=
3143 (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003145 } else { /* reading number of logical volumes failed */
3146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 printk(KERN_WARNING "cciss: report logical volume"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003148 " command failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 listlength = 0;
3150 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003151 hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
3152 if (hba[cntl_num]->num_luns > CISS_MAX_LUN) {
3153 printk(KERN_ERR
3154 "ciss: only %d number of logical volumes supported\n",
3155 CISS_MAX_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 hba[cntl_num]->num_luns = CISS_MAX_LUN;
3157 }
3158#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003159 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n",
3160 ld_buff->LUNListLength[0], ld_buff->LUNListLength[1],
3161 ld_buff->LUNListLength[2], ld_buff->LUNListLength[3],
3162 hba[cntl_num]->num_luns);
3163#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003165 hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns - 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003166 for (i = 0; i < CISS_MAX_LUN; i++) {
3167 if (i < hba[cntl_num]->num_luns) {
3168 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3]))
3169 << 24;
3170 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2]))
3171 << 16;
3172 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1]))
3173 << 8;
3174 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003176 hba[cntl_num]->drv[i].LunID = lunid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003179 printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
3180 ld_buff->LUN[i][0], ld_buff->LUN[i][1],
3181 ld_buff->LUN[i][2], ld_buff->LUN[i][3],
3182 hba[cntl_num]->drv[i].LunID);
3183#endif /* CCISS_DEBUG */
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003184
3185 /* testing to see if 16-byte CDBs are already being used */
3186 if(hba[cntl_num]->cciss_read == CCISS_READ_16) {
3187 cciss_read_capacity_16(cntl_num, i, 0,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003188 &total_size, &block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003189 goto geo_inq;
3190 }
3191 cciss_read_capacity(cntl_num, i, 0, &total_size, &block_size);
3192
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08003193 /* If read_capacity returns all F's the logical is >2TB */
3194 /* so we switch to 16-byte CDBs for all read/write ops */
3195 if(total_size == 0xFFFFFFFFULL) {
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003196 cciss_read_capacity_16(cntl_num, i, 0,
3197 &total_size, &block_size);
3198 hba[cntl_num]->cciss_read = CCISS_READ_16;
3199 hba[cntl_num]->cciss_write = CCISS_WRITE_16;
3200 } else {
3201 hba[cntl_num]->cciss_read = CCISS_READ_10;
3202 hba[cntl_num]->cciss_write = CCISS_WRITE_10;
3203 }
3204geo_inq:
Mike Millerddd47442005-09-13 01:25:22 -07003205 cciss_geometry_inquiry(cntl_num, i, 0, total_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003206 block_size, inq_buff,
3207 &hba[cntl_num]->drv[i]);
Mike Millerddd47442005-09-13 01:25:22 -07003208 } else {
3209 /* initialize raid_level to indicate a free space */
3210 hba[cntl_num]->drv[i].raid_level = -1;
3211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 }
3213 kfree(ld_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 kfree(inq_buff);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003215}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217/* Function to find the first free pointer into our hba[] array */
3218/* Returns -1 if no free entries are left. */
3219static int alloc_cciss_hba(void)
3220{
Mike Miller799202c2006-12-06 20:35:12 -08003221 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003223 for (i = 0; i < MAX_CTLR; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (!hba[i]) {
3225 ctlr_info_t *p;
Eric Sesterhenn06ff37f2006-03-08 11:21:52 +01003226 p = kzalloc(sizeof(ctlr_info_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 if (!p)
3228 goto Enomem;
Mike Miller799202c2006-12-06 20:35:12 -08003229 p->gendisk[0] = alloc_disk(1 << NWD_SHIFT);
3230 if (!p->gendisk[0])
3231 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 hba[i] = p;
3233 return i;
3234 }
3235 }
3236 printk(KERN_WARNING "cciss: This driver supports a maximum"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003237 " of %d controllers.\n", MAX_CTLR);
Mike Miller799202c2006-12-06 20:35:12 -08003238 return -1;
3239Enomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 printk(KERN_ERR "cciss: out of memory.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return -1;
3242}
3243
3244static void free_hba(int i)
3245{
3246 ctlr_info_t *p = hba[i];
3247 int n;
3248
3249 hba[i] = NULL;
Mike Miller799202c2006-12-06 20:35:12 -08003250 for (n = 0; n < CISS_MAX_LUN; n++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 put_disk(p->gendisk[n]);
3252 kfree(p);
3253}
3254
3255/*
3256 * This is it. Find all the controllers and register them. I really hate
3257 * stealing all these major device numbers.
3258 * returns the number of block devices registered.
3259 */
3260static int __devinit cciss_init_one(struct pci_dev *pdev,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003261 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 int i;
Mike Miller799202c2006-12-06 20:35:12 -08003264 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 int rc;
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003266 int dac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 i = alloc_cciss_hba();
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003269 if (i < 0)
Bjorn Helgaase2019b52006-06-25 05:49:05 -07003270 return -1;
Mike Miller1f8ef382005-09-13 01:25:21 -07003271
3272 hba[i]->busy_initializing = 1;
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 if (cciss_pci_init(hba[i], pdev) != 0)
3275 goto clean1;
3276
3277 sprintf(hba[i]->devname, "cciss%d", i);
3278 hba[i]->ctlr = i;
3279 hba[i]->pdev = pdev;
3280
3281 /* configure PCI DMA stuff */
mike.miller@hp.comeb0df992005-06-10 14:51:04 -05003282 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK))
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003283 dac = 1;
mike.miller@hp.comeb0df992005-06-10 14:51:04 -05003284 else if (!pci_set_dma_mask(pdev, DMA_32BIT_MASK))
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003285 dac = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 else {
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003287 printk(KERN_ERR "cciss: no suitable DMA available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 goto clean1;
3289 }
3290
3291 /*
3292 * register with the major number, or get a dynamic major number
3293 * by passing 0 as argument. This is done for greater than
3294 * 8 controller support.
3295 */
3296 if (i < MAX_CTLR_ORIG)
Christoph Hellwig564de742006-01-08 01:05:17 -08003297 hba[i]->major = COMPAQ_CISS_MAJOR + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 rc = register_blkdev(hba[i]->major, hba[i]->devname);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003299 if (rc == -EBUSY || rc == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 printk(KERN_ERR
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003301 "cciss: Unable to get major number %d for %s "
3302 "on hba %d\n", hba[i]->major, hba[i]->devname, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 goto clean1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003304 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 if (i >= MAX_CTLR_ORIG)
3306 hba[i]->major = rc;
3307 }
3308
3309 /* make sure the board interrupts are off */
3310 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003311 if (request_irq(hba[i]->intr[SIMPLE_MODE_INT], do_cciss_intr,
Thomas Gleixner69ab3912006-07-01 19:29:32 -07003312 IRQF_DISABLED | IRQF_SHARED, hba[i]->devname, hba[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003314 hba[i]->intr[SIMPLE_MODE_INT], hba[i]->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 goto clean2;
3316 }
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003317
3318 printk(KERN_INFO "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003319 hba[i]->devname, pdev->device, pci_name(pdev),
3320 hba[i]->intr[SIMPLE_MODE_INT], dac ? "" : " not");
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003321
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003322 hba[i]->cmd_pool_bits =
Mike Millerf8806322006-12-06 20:35:01 -08003323 kmalloc(((hba[i]->nr_cmds + BITS_PER_LONG -
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003324 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
3325 hba[i]->cmd_pool = (CommandList_struct *)
3326 pci_alloc_consistent(hba[i]->pdev,
Mike Millerf8806322006-12-06 20:35:01 -08003327 hba[i]->nr_cmds * sizeof(CommandList_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003328 &(hba[i]->cmd_pool_dhandle));
3329 hba[i]->errinfo_pool = (ErrorInfo_struct *)
3330 pci_alloc_consistent(hba[i]->pdev,
Mike Millerf8806322006-12-06 20:35:01 -08003331 hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003332 &(hba[i]->errinfo_pool_dhandle));
3333 if ((hba[i]->cmd_pool_bits == NULL)
3334 || (hba[i]->cmd_pool == NULL)
3335 || (hba[i]->errinfo_pool == NULL)) {
3336 printk(KERN_ERR "cciss: out of memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 goto clean4;
3338 }
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003339#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003340 hba[i]->scsi_rejects.complete =
3341 kmalloc(sizeof(hba[i]->scsi_rejects.complete[0]) *
Mike Millerf8806322006-12-06 20:35:01 -08003342 (hba[i]->nr_cmds + 5), GFP_KERNEL);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003343 if (hba[i]->scsi_rejects.complete == NULL) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003344 printk(KERN_ERR "cciss: out of memory");
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003345 goto clean4;
3346 }
3347#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 spin_lock_init(&hba[i]->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003350 /* Initialize the pdev driver private data.
3351 have it point to hba[i]. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 pci_set_drvdata(pdev, hba[i]);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003353 /* command and error info recs zeroed out before
3354 they are used */
3355 memset(hba[i]->cmd_pool_bits, 0,
Mike Millerf8806322006-12-06 20:35:01 -08003356 ((hba[i]->nr_cmds + BITS_PER_LONG -
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003357 1) / BITS_PER_LONG) * sizeof(unsigned long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003359#ifdef CCISS_DEBUG
3360 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n", i);
3361#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 cciss_getgeometry(i);
3364
3365 cciss_scsi_setup(i);
3366
3367 /* Turn the interrupts on so we can service requests */
3368 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
3369
3370 cciss_procinit(i);
Mike Miller92c4231a2006-12-06 20:35:06 -08003371
3372 hba[i]->cciss_max_sectors = 2048;
3373
Mike Millerd6dbf422005-09-21 09:55:32 -07003374 hba[i]->busy_initializing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Mike Miller799202c2006-12-06 20:35:12 -08003376 do {
Mike Millerad2b9312005-07-28 01:07:31 -07003377 drive_info_struct *drv = &(hba[i]->drv[j]);
3378 struct gendisk *disk = hba[i]->gendisk[j];
Mike Miller799202c2006-12-06 20:35:12 -08003379 request_queue_t *q;
3380
3381 /* Check if the disk was allocated already */
3382 if (!disk){
3383 hba[i]->gendisk[j] = alloc_disk(1 << NWD_SHIFT);
3384 disk = hba[i]->gendisk[j];
3385 }
3386
3387 /* Check that the disk was able to be allocated */
3388 if (!disk) {
3389 printk(KERN_ERR "cciss: unable to allocate memory for disk %d\n", j);
3390 goto clean4;
3391 }
Mike Millerad2b9312005-07-28 01:07:31 -07003392
3393 q = blk_init_queue(do_cciss_request, &hba[i]->lock);
3394 if (!q) {
3395 printk(KERN_ERR
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003396 "cciss: unable to allocate queue for disk %d\n",
3397 j);
Mike Miller799202c2006-12-06 20:35:12 -08003398 goto clean4;
Mike Millerad2b9312005-07-28 01:07:31 -07003399 }
3400 drv->queue = q;
3401
3402 q->backing_dev_info.ra_pages = READ_AHEAD;
Jens Axboea9925a02006-01-09 16:04:06 +01003403 blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Jens Axboea9925a02006-01-09 16:04:06 +01003405 /* This is a hardware imposed limit. */
3406 blk_queue_max_hw_segments(q, MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Jens Axboea9925a02006-01-09 16:04:06 +01003408 /* This is a limit in the driver and could be eliminated. */
3409 blk_queue_max_phys_segments(q, MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Mike Miller92c4231a2006-12-06 20:35:06 -08003411 blk_queue_max_sectors(q, hba[i]->cciss_max_sectors);
Jens Axboea9925a02006-01-09 16:04:06 +01003412
3413 blk_queue_softirq_done(q, cciss_softirq_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Mike Millerad2b9312005-07-28 01:07:31 -07003415 q->queuedata = hba[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 disk->major = hba[i]->major;
3418 disk->first_minor = j << NWD_SHIFT;
3419 disk->fops = &cciss_fops;
Mike Millerad2b9312005-07-28 01:07:31 -07003420 disk->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 disk->private_data = drv;
Greg Kroah-Hartman27c0ff82006-04-27 15:46:39 -07003422 disk->driverfs_dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 /* we must register the controller even if no disks exist */
3424 /* this is for the online array utilities */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003425 if (!drv->heads && j)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 continue;
Mike Millerad2b9312005-07-28 01:07:31 -07003427 blk_queue_hardsect_size(q, drv->block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 set_capacity(disk, drv->nr_blocks);
3429 add_disk(disk);
Mike Miller799202c2006-12-06 20:35:12 -08003430 j++;
3431 } while (j <= hba[i]->highest_lun);
Mike Millerad2b9312005-07-28 01:07:31 -07003432
Bjorn Helgaase2019b52006-06-25 05:49:05 -07003433 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003435 clean4:
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003436#ifdef CONFIG_CISS_SCSI_TAPE
Andrew Morton1acc0b02006-01-04 18:30:03 -08003437 kfree(hba[i]->scsi_rejects.complete);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003438#endif
Jesper Juhl6044ec82005-11-07 01:01:32 -08003439 kfree(hba[i]->cmd_pool_bits);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003440 if (hba[i]->cmd_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 pci_free_consistent(hba[i]->pdev,
Mike Millerf8806322006-12-06 20:35:01 -08003442 hba[i]->nr_cmds * sizeof(CommandList_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003443 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
3444 if (hba[i]->errinfo_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 pci_free_consistent(hba[i]->pdev,
Mike Millerf8806322006-12-06 20:35:01 -08003446 hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003447 hba[i]->errinfo_pool,
3448 hba[i]->errinfo_pool_dhandle);
Mike Millerfb86a352006-01-08 01:03:50 -08003449 free_irq(hba[i]->intr[SIMPLE_MODE_INT], hba[i]);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003450 clean2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 unregister_blkdev(hba[i]->major, hba[i]->devname);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003452 clean1:
Mike Miller1f8ef382005-09-13 01:25:21 -07003453 hba[i]->busy_initializing = 0;
Mike Miller799202c2006-12-06 20:35:12 -08003454 /* cleanup any queues that may have been initialized */
3455 for (j=0; j <= hba[i]->highest_lun; j++){
3456 drive_info_struct *drv = &(hba[i]->drv[j]);
3457 if (drv->queue)
3458 blk_cleanup_queue(drv->queue);
3459 }
Mike Miller872225c2006-12-13 00:34:22 -08003460 /*
3461 * Deliberately omit pci_disable_device(): it does something nasty to
3462 * Smart Array controllers that pci_enable_device does not undo
3463 */
Mike Miller799202c2006-12-06 20:35:12 -08003464 pci_release_regions(pdev);
Mike Miller799202c2006-12-06 20:35:12 -08003465 pci_set_drvdata(pdev, NULL);
Patrick McHardy61808c2b2006-03-23 02:59:24 -08003466 free_hba(i);
Bjorn Helgaase2019b52006-06-25 05:49:05 -07003467 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468}
3469
Mike Miller (OS Dev)de69fee2007-03-06 01:42:16 -08003470static void cciss_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
3472 ctlr_info_t *tmp_ptr;
3473 int i, j;
3474 char flush_buf[4];
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003475 int return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003477 if (pci_get_drvdata(pdev) == NULL) {
3478 printk(KERN_ERR "cciss: Unable to remove device \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 return;
3480 }
3481 tmp_ptr = pci_get_drvdata(pdev);
3482 i = tmp_ptr->ctlr;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003483 if (hba[i] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 printk(KERN_ERR "cciss: device appears to "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003485 "already be removed \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return;
3487 }
Bjorn Helgaasb6550772007-04-11 23:28:43 -07003488
3489 remove_proc_entry(hba[i]->devname, proc_cciss);
3490 unregister_blkdev(hba[i]->major, hba[i]->devname);
3491
3492 /* remove it from the disk list */
3493 for (j = 0; j < CISS_MAX_LUN; j++) {
3494 struct gendisk *disk = hba[i]->gendisk[j];
3495 if (disk) {
3496 request_queue_t *q = disk->queue;
3497
3498 if (disk->flags & GENHD_FL_UP)
3499 del_gendisk(disk);
3500 if (q)
3501 blk_cleanup_queue(q);
3502 }
3503 }
3504
3505 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
3506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 /* Turn board interrupts off and send the flush cache command */
3508 /* sendcmd will turn off interrupt, and send the flush...
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003509 * To write all data in the battery backed cache to disks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 memset(flush_buf, 0, 4);
3511 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003512 TYPE_CMD);
Mike Miller (OS Dev)de69fee2007-03-06 01:42:16 -08003513 if (return_code == IO_OK) {
3514 printk(KERN_INFO "Completed flushing cache on controller %d\n", i);
3515 } else {
3516 printk(KERN_WARNING "Error flushing cache on controller %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 }
Mike Millerfb86a352006-01-08 01:03:50 -08003518 free_irq(hba[i]->intr[2], hba[i]);
3519
3520#ifdef CONFIG_PCI_MSI
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003521 if (hba[i]->msix_vector)
3522 pci_disable_msix(hba[i]->pdev);
3523 else if (hba[i]->msi_vector)
3524 pci_disable_msi(hba[i]->pdev);
3525#endif /* CONFIG_PCI_MSI */
Mike Millerfb86a352006-01-08 01:03:50 -08003526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 iounmap(hba[i]->vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Mike Millerf8806322006-12-06 20:35:01 -08003529 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(CommandList_struct),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
Mike Millerf8806322006-12-06 20:35:01 -08003531 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003532 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 kfree(hba[i]->cmd_pool_bits);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003534#ifdef CONFIG_CISS_SCSI_TAPE
3535 kfree(hba[i]->scsi_rejects.complete);
3536#endif
Mike Miller872225c2006-12-13 00:34:22 -08003537 /*
3538 * Deliberately omit pci_disable_device(): it does something nasty to
3539 * Smart Array controllers that pci_enable_device does not undo
3540 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003541 pci_release_regions(pdev);
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003542 pci_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 free_hba(i);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003544}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546static struct pci_driver cciss_pci_driver = {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003547 .name = "cciss",
3548 .probe = cciss_init_one,
3549 .remove = __devexit_p(cciss_remove_one),
3550 .id_table = cciss_pci_device_id, /* id_table */
Mike Miller (OS Dev)de69fee2007-03-06 01:42:16 -08003551 .shutdown = cciss_remove_one,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552};
3553
3554/*
3555 * This is it. Register the PCI driver information for the cards we control
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003556 * the OS will call our registered routines when it finds one of our cards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 */
3558static int __init cciss_init(void)
3559{
3560 printk(KERN_INFO DRIVER_NAME "\n");
3561
3562 /* Register for our PCI devices */
Richard Knutsson9bfab8c2005-11-30 00:59:34 +01003563 return pci_register_driver(&cciss_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
3566static void __exit cciss_cleanup(void)
3567{
3568 int i;
3569
3570 pci_unregister_driver(&cciss_pci_driver);
3571 /* double check that all controller entrys have been removed */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003572 for (i = 0; i < MAX_CTLR; i++) {
3573 if (hba[i] != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 printk(KERN_WARNING "cciss: had to remove"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003575 " controller %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 cciss_remove_one(hba[i]->pdev);
3577 }
3578 }
3579 remove_proc_entry("cciss", proc_root_driver);
3580}
3581
Mike Miller33079b22005-09-13 01:25:22 -07003582static void fail_all_cmds(unsigned long ctlr)
3583{
3584 /* If we get here, the board is apparently dead. */
3585 ctlr_info_t *h = hba[ctlr];
3586 CommandList_struct *c;
3587 unsigned long flags;
3588
3589 printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003590 h->alive = 0; /* the controller apparently died... */
Mike Miller33079b22005-09-13 01:25:22 -07003591
3592 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
3593
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003594 pci_disable_device(h->pdev); /* Make sure it is really dead. */
Mike Miller33079b22005-09-13 01:25:22 -07003595
3596 /* move everything off the request queue onto the completed queue */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003597 while ((c = h->reqQ) != NULL) {
Mike Miller33079b22005-09-13 01:25:22 -07003598 removeQ(&(h->reqQ), c);
3599 h->Qdepth--;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003600 addQ(&(h->cmpQ), c);
Mike Miller33079b22005-09-13 01:25:22 -07003601 }
3602
3603 /* Now, fail everything on the completed queue with a HW error */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003604 while ((c = h->cmpQ) != NULL) {
Mike Miller33079b22005-09-13 01:25:22 -07003605 removeQ(&h->cmpQ, c);
3606 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
3607 if (c->cmd_type == CMD_RWREQ) {
3608 complete_command(h, c, 0);
3609 } else if (c->cmd_type == CMD_IOCTL_PEND)
3610 complete(c->waiting);
3611#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003612 else if (c->cmd_type == CMD_SCSI)
3613 complete_scsi_command(c, 0, 0);
Mike Miller33079b22005-09-13 01:25:22 -07003614#endif
3615 }
3616 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
3617 return;
3618}
3619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620module_init(cciss_init);
3621module_exit(cciss_cleanup);