blob: 55bd35c0f082344a1d33822d081fcd7f9a104663 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Mike Millerbd4f36d2007-10-24 10:30:34 +02002 * Disk Array driver for HP Smart Array controllers.
3 * (C) Copyright 2000, 2007 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
Mike Millerbd4f36d2007-10-24 10:30:34 +02007 * the Free Software Foundation; version 2 of the License.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Mike Millerbd4f36d2007-10-24 10:30:34 +020011 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
Mike Millerbd4f36d2007-10-24 10:30:34 +020016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 * 02111-1307, USA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
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>
Mike Miller89b6e742008-02-21 08:54:03 +010036#include <linux/seq_file.h>
Bjorn Helgaas7c832832006-06-25 05:49:06 -070037#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/hdreg.h>
39#include <linux/spinlock.h>
40#include <linux/compat.h>
Jens Axboe2056a782006-03-23 20:00:26 +010041#include <linux/blktrace_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
43#include <asm/io.h>
44
mike.miller@hp.comeb0df992005-06-10 14:51:04 -050045#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/blkdev.h>
47#include <linux/genhd.h>
48#include <linux/completion.h>
Stephen Camerond5d3b732007-05-08 00:30:02 -070049#include <scsi/scsi.h>
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -070050#include <scsi/sg.h>
51#include <scsi/scsi_ioctl.h>
52#include <linux/cdrom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
Mike Miller9d827c92006-12-06 20:34:58 -080055#define DRIVER_NAME "HP CISS Driver (v 3.6.14)"
56#define DRIVER_VERSION CCISS_DRIVER_VERSION(3,6,14)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/* Embedded module documentation macros - see modules.h */
59MODULE_AUTHOR("Hewlett-Packard Company");
Mike Miller9d827c92006-12-06 20:34:58 -080060MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 3.6.14");
Linus Torvalds1da177e2005-04-16 15:20:36 -070061MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
Mike Miller1883c5a2006-09-12 20:36:07 -070062 " SA6i P600 P800 P400 P400i E200 E200i E500");
Mike Miller9d827c92006-12-06 20:34:58 -080063MODULE_VERSION("3.6.14");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064MODULE_LICENSE("GPL");
65
66#include "cciss_cmd.h"
67#include "cciss.h"
68#include <linux/cciss_ioctl.h>
69
70/* define the PCI info for the cards we can control */
71static const struct pci_device_id cciss_pci_device_id[] = {
Bjorn Helgaasf82ccdb2006-06-25 05:49:07 -070072 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS, 0x0E11, 0x4070},
73 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4080},
74 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4082},
75 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4083},
76 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x4091},
77 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409A},
78 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409B},
79 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409C},
80 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409D},
81 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA, 0x103C, 0x3225},
82 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3223},
83 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3234},
84 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3235},
85 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3211},
86 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3212},
87 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3213},
88 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3214},
89 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3215},
Mike Millerde923912006-12-06 20:35:03 -080090 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3237},
Mike Miller (OS Dev9cff3b32007-06-19 20:52:18 +020091 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x323D},
Mike Miller4ff9a9a2006-12-06 20:35:00 -080092 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
93 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 {0,}
95};
Bjorn Helgaas7c832832006-06-25 05:49:06 -070096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* board_id = Subsystem Device ID & Vendor ID
100 * product = Marketing Name for the board
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700101 * access = Address of the struct of function pointers
Mike Millerf8806322006-12-06 20:35:01 -0800102 * nr_cmds = Number of commands supported by controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
104static struct board_type products[] = {
Mike Millerf8806322006-12-06 20:35:01 -0800105 {0x40700E11, "Smart Array 5300", &SA5_access, 512},
106 {0x40800E11, "Smart Array 5i", &SA5B_access, 512},
107 {0x40820E11, "Smart Array 532", &SA5B_access, 512},
108 {0x40830E11, "Smart Array 5312", &SA5B_access, 512},
109 {0x409A0E11, "Smart Array 641", &SA5_access, 512},
110 {0x409B0E11, "Smart Array 642", &SA5_access, 512},
111 {0x409C0E11, "Smart Array 6400", &SA5_access, 512},
112 {0x409D0E11, "Smart Array 6400 EM", &SA5_access, 512},
113 {0x40910E11, "Smart Array 6i", &SA5_access, 512},
114 {0x3225103C, "Smart Array P600", &SA5_access, 512},
115 {0x3223103C, "Smart Array P800", &SA5_access, 512},
116 {0x3234103C, "Smart Array P400", &SA5_access, 512},
117 {0x3235103C, "Smart Array P400i", &SA5_access, 512},
118 {0x3211103C, "Smart Array E200i", &SA5_access, 120},
119 {0x3212103C, "Smart Array E200", &SA5_access, 120},
120 {0x3213103C, "Smart Array E200i", &SA5_access, 120},
121 {0x3214103C, "Smart Array E200i", &SA5_access, 120},
122 {0x3215103C, "Smart Array E200i", &SA5_access, 120},
Mike Millerde923912006-12-06 20:35:03 -0800123 {0x3237103C, "Smart Array E500", &SA5_access, 512},
Mike Miller (OS Dev9cff3b32007-06-19 20:52:18 +0200124 {0x323D103C, "Smart Array P700m", &SA5_access, 512},
Mike Millerf8806322006-12-06 20:35:01 -0800125 {0xFFFF103C, "Unknown Smart Array", &SA5_access, 120},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -0700128/* How long to wait (in milliseconds) for board to go into simple mode */
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700129#define MAX_CONFIG_WAIT 30000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define MAX_IOCTL_CONFIG_WAIT 1000
131
132/*define how many times we will try a command because of bus resets */
133#define MAX_CMD_RETRIES 3
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define MAX_CTLR 32
136
137/* Originally cciss driver only supports 8 major numbers */
138#define MAX_CTLR_ORIG 8
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static ctlr_info_t *hba[MAX_CTLR];
141
Jens Axboe165125e2007-07-24 09:28:11 +0200142static void do_cciss_request(struct request_queue *q);
David Howells7d12e782006-10-05 14:55:46 +0100143static irqreturn_t do_cciss_intr(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static int cciss_open(struct inode *inode, struct file *filep);
145static int cciss_release(struct inode *inode, struct file *filep);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700146static int cciss_ioctl(struct inode *inode, struct file *filep,
147 unsigned int cmd, unsigned long arg);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800148static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static int cciss_revalidate(struct gendisk *disk);
Mike Millerddd47442005-09-13 01:25:22 -0700151static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700152static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
153 int clear_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -0700155static void cciss_read_capacity(int ctlr, int logvol, int withirq,
156 sector_t *total_size, unsigned int *block_size);
157static void cciss_read_capacity_16(int ctlr, int logvol, int withirq,
158 sector_t *total_size, unsigned int *block_size);
159static void cciss_geometry_inquiry(int ctlr, int logvol,
160 int withirq, sector_t total_size,
161 unsigned int block_size, InquiryData_struct *inq_buff,
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700162 drive_info_struct *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static void cciss_getgeometry(int cntl_num);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700164static void __devinit cciss_interrupt_mode(ctlr_info_t *, struct pci_dev *,
165 __u32);
166static void start_io(ctlr_info_t *h);
167static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size,
168 unsigned int use_unit_num, unsigned int log_unit,
169 __u8 page_code, unsigned char *scsi3addr, int cmd_type);
170static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
171 unsigned int use_unit_num, unsigned int log_unit,
172 __u8 page_code, int cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Mike Miller33079b22005-09-13 01:25:22 -0700174static void fail_all_cmds(unsigned long ctlr);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static void cciss_procinit(int i);
178#else
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700179static void cciss_procinit(int i)
180{
181}
182#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184#ifdef CONFIG_COMPAT
185static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg);
186#endif
187
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700188static struct block_device_operations cciss_fops = {
189 .owner = THIS_MODULE,
190 .open = cciss_open,
191 .release = cciss_release,
192 .ioctl = cciss_ioctl,
193 .getgeo = cciss_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#ifdef CONFIG_COMPAT
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700195 .compat_ioctl = cciss_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700197 .revalidate_disk = cciss_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
200/*
201 * Enqueuing and dequeuing functions for cmdlists.
202 */
203static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
204{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700205 if (*Qptr == NULL) {
206 *Qptr = c;
207 c->next = c->prev = c;
208 } else {
209 c->prev = (*Qptr)->prev;
210 c->next = (*Qptr);
211 (*Qptr)->prev->next = c;
212 (*Qptr)->prev = c;
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700216static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
217 CommandList_struct *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700219 if (c && c->next != c) {
220 if (*Qptr == c)
221 *Qptr = c->next;
222 c->prev->next = c->next;
223 c->next->prev = c->prev;
224 } else {
225 *Qptr = NULL;
226 }
227 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230#include "cciss_scsi.c" /* For SCSI tape support */
231
Randy Dunlap0f5486e2006-12-29 16:48:31 -0800232#define RAID_UNKNOWN 6
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#ifdef CONFIG_PROC_FS
235
236/*
237 * Report information about this controller.
238 */
239#define ENG_GIG 1000000000
240#define ENG_GIG_FACTOR (ENG_GIG/512)
Mike Miller89b6e742008-02-21 08:54:03 +0100241#define ENGAGE_SCSI "engage scsi"
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700242static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
243 "UNKNOWN"
244};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246static struct proc_dir_entry *proc_cciss;
247
Mike Miller89b6e742008-02-21 08:54:03 +0100248static void cciss_seq_show_header(struct seq_file *seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Mike Miller89b6e742008-02-21 08:54:03 +0100250 ctlr_info_t *h = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Mike Miller89b6e742008-02-21 08:54:03 +0100252 seq_printf(seq, "%s: HP %s Controller\n"
253 "Board ID: 0x%08lx\n"
254 "Firmware Version: %c%c%c%c\n"
255 "IRQ: %d\n"
256 "Logical drives: %d\n"
257 "Current Q depth: %d\n"
258 "Current # commands on controller: %d\n"
259 "Max Q depth since init: %d\n"
260 "Max # commands on controller since init: %d\n"
261 "Max SG entries since init: %d\n",
262 h->devname,
263 h->product_name,
264 (unsigned long)h->board_id,
265 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
266 h->firm_ver[3], (unsigned int)h->intr[SIMPLE_MODE_INT],
267 h->num_luns,
268 h->Qdepth, h->commands_outstanding,
269 h->maxQsinceinit, h->max_outstanding, h->maxSG);
270
271#ifdef CONFIG_CISS_SCSI_TAPE
272 cciss_seq_tape_report(seq, h->ctlr);
273#endif /* CONFIG_CISS_SCSI_TAPE */
274}
275
276static void *cciss_seq_start(struct seq_file *seq, loff_t *pos)
277{
278 ctlr_info_t *h = seq->private;
279 unsigned ctlr = h->ctlr;
280 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /* prevent displaying bogus info during configuration
283 * or deconfiguration of a logical volume
284 */
285 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
286 if (h->busy_configuring) {
287 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
Mike Miller89b6e742008-02-21 08:54:03 +0100288 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 h->busy_configuring = 1;
291 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
292
Mike Miller89b6e742008-02-21 08:54:03 +0100293 if (*pos == 0)
294 cciss_seq_show_header(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Mike Miller89b6e742008-02-21 08:54:03 +0100296 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Mike Miller89b6e742008-02-21 08:54:03 +0100299static int cciss_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Mike Miller89b6e742008-02-21 08:54:03 +0100301 sector_t vol_sz, vol_sz_frac;
302 ctlr_info_t *h = seq->private;
303 unsigned ctlr = h->ctlr;
304 loff_t *pos = v;
305 drive_info_struct *drv = &h->drv[*pos];
306
307 if (*pos > h->highest_lun)
308 return 0;
309
310 if (drv->heads == 0)
311 return 0;
312
313 vol_sz = drv->nr_blocks;
314 vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
315 vol_sz_frac *= 100;
316 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
317
318 if (drv->raid_level > 5)
319 drv->raid_level = RAID_UNKNOWN;
320 seq_printf(seq, "cciss/c%dd%d:"
321 "\t%4u.%02uGB\tRAID %s\n",
322 ctlr, (int) *pos, (int)vol_sz, (int)vol_sz_frac,
323 raid_label[drv->raid_level]);
324 return 0;
325}
326
327static void *cciss_seq_next(struct seq_file *seq, void *v, loff_t *pos)
328{
329 ctlr_info_t *h = seq->private;
330
331 if (*pos > h->highest_lun)
332 return NULL;
333 *pos += 1;
334
335 return pos;
336}
337
338static void cciss_seq_stop(struct seq_file *seq, void *v)
339{
340 ctlr_info_t *h = seq->private;
341
342 /* Only reset h->busy_configuring if we succeeded in setting
343 * it during cciss_seq_start. */
344 if (v == ERR_PTR(-EBUSY))
345 return;
346
347 h->busy_configuring = 0;
348}
349
350static struct seq_operations cciss_seq_ops = {
351 .start = cciss_seq_start,
352 .show = cciss_seq_show,
353 .next = cciss_seq_next,
354 .stop = cciss_seq_stop,
355};
356
357static int cciss_seq_open(struct inode *inode, struct file *file)
358{
359 int ret = seq_open(file, &cciss_seq_ops);
360 struct seq_file *seq = file->private_data;
361
362 if (!ret)
363 seq->private = PDE(inode)->data;
364
365 return ret;
366}
367
368static ssize_t
369cciss_proc_write(struct file *file, const char __user *buf,
370 size_t length, loff_t *ppos)
371{
372 int err;
373 char *buffer;
374
375#ifndef CONFIG_CISS_SCSI_TAPE
376 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#endif
378
Mike Miller89b6e742008-02-21 08:54:03 +0100379 if (!buf || length > PAGE_SIZE - 1)
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700380 return -EINVAL;
Mike Miller89b6e742008-02-21 08:54:03 +0100381
382 buffer = (char *)__get_free_page(GFP_KERNEL);
383 if (!buffer)
384 return -ENOMEM;
385
386 err = -EFAULT;
387 if (copy_from_user(buffer, buf, length))
388 goto out;
389 buffer[length] = '\0';
390
391#ifdef CONFIG_CISS_SCSI_TAPE
392 if (strncmp(ENGAGE_SCSI, buffer, sizeof ENGAGE_SCSI - 1) == 0) {
393 struct seq_file *seq = file->private_data;
394 ctlr_info_t *h = seq->private;
395 int rc;
396
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700397 rc = cciss_engage_scsi(h->ctlr);
398 if (rc != 0)
Mike Miller89b6e742008-02-21 08:54:03 +0100399 err = -rc;
400 else
401 err = length;
402 } else
403#endif /* CONFIG_CISS_SCSI_TAPE */
404 err = -EINVAL;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700405 /* might be nice to have "disengage" too, but it's not
406 safely possible. (only 1 module use count, lock issues.) */
Mike Miller89b6e742008-02-21 08:54:03 +0100407
408out:
409 free_page((unsigned long)buffer);
410 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Mike Miller89b6e742008-02-21 08:54:03 +0100413static struct file_operations cciss_proc_fops = {
414 .owner = THIS_MODULE,
415 .open = cciss_seq_open,
416 .read = seq_read,
417 .llseek = seq_lseek,
418 .release = seq_release,
419 .write = cciss_proc_write,
420};
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static void __devinit cciss_procinit(int i)
423{
424 struct proc_dir_entry *pde;
425
Mike Miller89b6e742008-02-21 08:54:03 +0100426 if (proc_cciss == NULL)
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700427 proc_cciss = proc_mkdir("cciss", proc_root_driver);
Mike Miller89b6e742008-02-21 08:54:03 +0100428 if (!proc_cciss)
429 return;
430 pde = proc_create(hba[i]->devname, S_IWUSR | S_IRUSR | S_IRGRP |
431 S_IROTH, proc_cciss,
432 &cciss_proc_fops);
433 if (!pde)
434 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Mike Miller89b6e742008-02-21 08:54:03 +0100436 pde->data = hba[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700438#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700440/*
441 * For operations that cannot sleep, a command block is allocated at init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700443 * which ones are free or in use. For operations that can wait for kmalloc
444 * to possible sleep, this routine can be called with get_from_pool set to 0.
445 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
446 */
447static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 CommandList_struct *c;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700450 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 u64bit temp64;
452 dma_addr_t cmd_dma_handle, err_dma_handle;
453
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700454 if (!get_from_pool) {
455 c = (CommandList_struct *) pci_alloc_consistent(h->pdev,
456 sizeof(CommandList_struct), &cmd_dma_handle);
457 if (c == NULL)
458 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 memset(c, 0, sizeof(CommandList_struct));
460
Mike Miller33079b22005-09-13 01:25:22 -0700461 c->cmdindex = -1;
462
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700463 c->err_info = (ErrorInfo_struct *)
464 pci_alloc_consistent(h->pdev, sizeof(ErrorInfo_struct),
465 &err_dma_handle);
466
467 if (c->err_info == NULL) {
468 pci_free_consistent(h->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 sizeof(CommandList_struct), c, cmd_dma_handle);
470 return NULL;
471 }
472 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700473 } else { /* get it out of the controllers pool */
474
475 do {
Mike Millerf8806322006-12-06 20:35:01 -0800476 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
477 if (i == h->nr_cmds)
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700478 return NULL;
479 } while (test_and_set_bit
480 (i & (BITS_PER_LONG - 1),
481 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482#ifdef CCISS_DEBUG
483 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
484#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700485 c = h->cmd_pool + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 memset(c, 0, sizeof(CommandList_struct));
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700487 cmd_dma_handle = h->cmd_pool_dhandle
488 + i * sizeof(CommandList_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 c->err_info = h->errinfo_pool + i;
490 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700491 err_dma_handle = h->errinfo_pool_dhandle
492 + i * sizeof(ErrorInfo_struct);
493 h->nr_allocs++;
Mike Miller33079b22005-09-13 01:25:22 -0700494
495 c->cmdindex = i;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 c->busaddr = (__u32) cmd_dma_handle;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700499 temp64.val = (__u64) err_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 c->ErrDesc.Addr.lower = temp64.val32.lower;
501 c->ErrDesc.Addr.upper = temp64.val32.upper;
502 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 c->ctlr = h->ctlr;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700505 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700508/*
509 * Frees a command block that was previously allocated with cmd_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
511static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
512{
513 int i;
514 u64bit temp64;
515
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700516 if (!got_from_pool) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 temp64.val32.lower = c->ErrDesc.Addr.lower;
518 temp64.val32.upper = c->ErrDesc.Addr.upper;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700519 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
520 c->err_info, (dma_addr_t) temp64.val);
521 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
522 c, (dma_addr_t) c->busaddr);
523 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 i = c - h->cmd_pool;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700525 clear_bit(i & (BITS_PER_LONG - 1),
526 h->cmd_pool_bits + (i / BITS_PER_LONG));
527 h->nr_frees++;
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531static inline ctlr_info_t *get_host(struct gendisk *disk)
532{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700533 return disk->queue->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536static inline drive_info_struct *get_drv(struct gendisk *disk)
537{
538 return disk->private_data;
539}
540
541/*
542 * Open. Make sure the device is really there.
543 */
544static int cciss_open(struct inode *inode, struct file *filep)
545{
546 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
547 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
548
549#ifdef CCISS_DEBUG
550 printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700551#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Mike Millerddd47442005-09-13 01:25:22 -0700553 if (host->busy_initializing || drv->busy_configuring)
554 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /*
556 * Root is allowed to open raw volume zero even if it's not configured
557 * so array config can still work. Root is also allowed to open any
558 * volume that has a LUN ID, so it can issue IOCTL to reread the
559 * disk information. I don't think I really like this
560 * but I'm already using way to many device nodes to claim another one
561 * for "raw controller".
562 */
Mike Miller7a06f782006-12-06 20:35:08 -0800563 if (drv->heads == 0) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700564 if (iminor(inode) != 0) { /* not node 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* if not node 0 make sure it is a partition = 0 */
566 if (iminor(inode) & 0x0f) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700567 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* if it is, make sure we have a LUN ID */
569 } else if (drv->LunID == 0) {
570 return -ENXIO;
571 }
572 }
573 if (!capable(CAP_SYS_ADMIN))
574 return -EPERM;
575 }
576 drv->usage_count++;
577 host->usage_count++;
578 return 0;
579}
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/*
582 * Close. Sync first.
583 */
584static int cciss_release(struct inode *inode, struct file *filep)
585{
586 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
587 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
588
589#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700590 printk(KERN_DEBUG "cciss_release %s\n",
591 inode->i_bdev->bd_disk->disk_name);
592#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 drv->usage_count--;
595 host->usage_count--;
596 return 0;
597}
598
599#ifdef CONFIG_COMPAT
600
601static int do_ioctl(struct file *f, unsigned cmd, unsigned long arg)
602{
603 int ret;
604 lock_kernel();
Josef Sipek6c648be2006-12-08 02:36:55 -0800605 ret = cciss_ioctl(f->f_path.dentry->d_inode, f, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 unlock_kernel();
607 return ret;
608}
609
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700610static int cciss_ioctl32_passthru(struct file *f, unsigned cmd,
611 unsigned long arg);
612static int cciss_ioctl32_big_passthru(struct file *f, unsigned cmd,
613 unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg)
616{
617 switch (cmd) {
618 case CCISS_GETPCIINFO:
619 case CCISS_GETINTINFO:
620 case CCISS_SETINTINFO:
621 case CCISS_GETNODENAME:
622 case CCISS_SETNODENAME:
623 case CCISS_GETHEARTBEAT:
624 case CCISS_GETBUSTYPES:
625 case CCISS_GETFIRMVER:
626 case CCISS_GETDRIVVER:
627 case CCISS_REVALIDVOLS:
628 case CCISS_DEREGDISK:
629 case CCISS_REGNEWDISK:
630 case CCISS_REGNEWD:
631 case CCISS_RESCANDISK:
632 case CCISS_GETLUNINFO:
633 return do_ioctl(f, cmd, arg);
634
635 case CCISS_PASSTHRU32:
636 return cciss_ioctl32_passthru(f, cmd, arg);
637 case CCISS_BIG_PASSTHRU32:
638 return cciss_ioctl32_big_passthru(f, cmd, arg);
639
640 default:
641 return -ENOIOCTLCMD;
642 }
643}
644
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700645static int cciss_ioctl32_passthru(struct file *f, unsigned cmd,
646 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 IOCTL32_Command_struct __user *arg32 =
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700649 (IOCTL32_Command_struct __user *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 IOCTL_Command_struct arg64;
651 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
652 int err;
653 u32 cp;
654
655 err = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700656 err |=
657 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
658 sizeof(arg64.LUN_info));
659 err |=
660 copy_from_user(&arg64.Request, &arg32->Request,
661 sizeof(arg64.Request));
662 err |=
663 copy_from_user(&arg64.error_info, &arg32->error_info,
664 sizeof(arg64.error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 err |= get_user(arg64.buf_size, &arg32->buf_size);
666 err |= get_user(cp, &arg32->buf);
667 arg64.buf = compat_ptr(cp);
668 err |= copy_to_user(p, &arg64, sizeof(arg64));
669
670 if (err)
671 return -EFAULT;
672
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700673 err = do_ioctl(f, CCISS_PASSTHRU, (unsigned long)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (err)
675 return err;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700676 err |=
677 copy_in_user(&arg32->error_info, &p->error_info,
678 sizeof(arg32->error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (err)
680 return -EFAULT;
681 return err;
682}
683
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700684static int cciss_ioctl32_big_passthru(struct file *file, unsigned cmd,
685 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 BIG_IOCTL32_Command_struct __user *arg32 =
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700688 (BIG_IOCTL32_Command_struct __user *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 BIG_IOCTL_Command_struct arg64;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700690 BIG_IOCTL_Command_struct __user *p =
691 compat_alloc_user_space(sizeof(arg64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int err;
693 u32 cp;
694
695 err = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700696 err |=
697 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
698 sizeof(arg64.LUN_info));
699 err |=
700 copy_from_user(&arg64.Request, &arg32->Request,
701 sizeof(arg64.Request));
702 err |=
703 copy_from_user(&arg64.error_info, &arg32->error_info,
704 sizeof(arg64.error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 err |= get_user(arg64.buf_size, &arg32->buf_size);
706 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
707 err |= get_user(cp, &arg32->buf);
708 arg64.buf = compat_ptr(cp);
709 err |= copy_to_user(p, &arg64, sizeof(arg64));
710
711 if (err)
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700712 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700714 err = do_ioctl(file, CCISS_BIG_PASSTHRU, (unsigned long)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (err)
716 return err;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700717 err |=
718 copy_in_user(&arg32->error_info, &p->error_info,
719 sizeof(arg32->error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (err)
721 return -EFAULT;
722 return err;
723}
724#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800725
726static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo)
727{
728 drive_info_struct *drv = get_drv(bdev->bd_disk);
729
730 if (!drv->cylinders)
731 return -ENXIO;
732
733 geo->heads = drv->heads;
734 geo->sectors = drv->sectors;
735 geo->cylinders = drv->cylinders;
736 return 0;
737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/*
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700740 * ioctl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700742static int cciss_ioctl(struct inode *inode, struct file *filep,
743 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct block_device *bdev = inode->i_bdev;
746 struct gendisk *disk = bdev->bd_disk;
747 ctlr_info_t *host = get_host(disk);
748 drive_info_struct *drv = get_drv(disk);
749 int ctlr = host->ctlr;
750 void __user *argp = (void __user *)arg;
751
752#ifdef CCISS_DEBUG
753 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700754#endif /* CCISS_DEBUG */
755
756 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 case CCISS_GETPCIINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700759 cciss_pci_info_struct pciinfo;
760
761 if (!arg)
762 return -EINVAL;
763 pciinfo.domain = pci_domain_nr(host->pdev->bus);
764 pciinfo.bus = host->pdev->bus->number;
765 pciinfo.dev_fn = host->pdev->devfn;
766 pciinfo.board_id = host->board_id;
767 if (copy_to_user
768 (argp, &pciinfo, sizeof(cciss_pci_info_struct)))
769 return -EFAULT;
770 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700772 case CCISS_GETINTINFO:
773 {
774 cciss_coalint_struct intinfo;
775 if (!arg)
776 return -EINVAL;
777 intinfo.delay =
778 readl(&host->cfgtable->HostWrite.CoalIntDelay);
779 intinfo.count =
780 readl(&host->cfgtable->HostWrite.CoalIntCount);
781 if (copy_to_user
782 (argp, &intinfo, sizeof(cciss_coalint_struct)))
783 return -EFAULT;
784 return 0;
785 }
786 case CCISS_SETINTINFO:
787 {
788 cciss_coalint_struct intinfo;
789 unsigned long flags;
790 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700792 if (!arg)
793 return -EINVAL;
794 if (!capable(CAP_SYS_ADMIN))
795 return -EPERM;
796 if (copy_from_user
797 (&intinfo, argp, sizeof(cciss_coalint_struct)))
798 return -EFAULT;
799 if ((intinfo.delay == 0) && (intinfo.count == 0))
800 {
801// printk("cciss_ioctl: delay and count cannot be 0\n");
802 return -EINVAL;
803 }
804 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
805 /* Update the field, and then ring the doorbell */
806 writel(intinfo.delay,
807 &(host->cfgtable->HostWrite.CoalIntDelay));
808 writel(intinfo.count,
809 &(host->cfgtable->HostWrite.CoalIntCount));
810 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
811
812 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
813 if (!(readl(host->vaddr + SA5_DOORBELL)
814 & CFGTBL_ChangeReq))
815 break;
816 /* delay and try again */
817 udelay(1000);
818 }
819 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
820 if (i >= MAX_IOCTL_CONFIG_WAIT)
821 return -EAGAIN;
822 return 0;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 case CCISS_GETNODENAME:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700825 {
826 NodeName_type NodeName;
827 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700829 if (!arg)
830 return -EINVAL;
831 for (i = 0; i < 16; i++)
832 NodeName[i] =
833 readb(&host->cfgtable->ServerName[i]);
834 if (copy_to_user(argp, NodeName, sizeof(NodeName_type)))
835 return -EFAULT;
836 return 0;
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 case CCISS_SETNODENAME:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700839 {
840 NodeName_type NodeName;
841 unsigned long flags;
842 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700844 if (!arg)
845 return -EINVAL;
846 if (!capable(CAP_SYS_ADMIN))
847 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700849 if (copy_from_user
850 (NodeName, argp, sizeof(NodeName_type)))
851 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700853 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700855 /* Update the field, and then ring the doorbell */
856 for (i = 0; i < 16; i++)
857 writeb(NodeName[i],
858 &host->cfgtable->ServerName[i]);
859
860 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
861
862 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
863 if (!(readl(host->vaddr + SA5_DOORBELL)
864 & CFGTBL_ChangeReq))
865 break;
866 /* delay and try again */
867 udelay(1000);
868 }
869 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
870 if (i >= MAX_IOCTL_CONFIG_WAIT)
871 return -EAGAIN;
872 return 0;
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 case CCISS_GETHEARTBEAT:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700876 {
877 Heartbeat_type heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700879 if (!arg)
880 return -EINVAL;
881 heartbeat = readl(&host->cfgtable->HeartBeat);
882 if (copy_to_user
883 (argp, &heartbeat, sizeof(Heartbeat_type)))
884 return -EFAULT;
885 return 0;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 case CCISS_GETBUSTYPES:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700888 {
889 BusTypes_type BusTypes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700891 if (!arg)
892 return -EINVAL;
893 BusTypes = readl(&host->cfgtable->BusTypes);
894 if (copy_to_user
895 (argp, &BusTypes, sizeof(BusTypes_type)))
896 return -EFAULT;
897 return 0;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 case CCISS_GETFIRMVER:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700900 {
901 FirmwareVer_type firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700903 if (!arg)
904 return -EINVAL;
905 memcpy(firmware, host->firm_ver, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700907 if (copy_to_user
908 (argp, firmware, sizeof(FirmwareVer_type)))
909 return -EFAULT;
910 return 0;
911 }
912 case CCISS_GETDRIVVER:
913 {
914 DriverVer_type DriverVer = DRIVER_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700916 if (!arg)
917 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700919 if (copy_to_user
920 (argp, &DriverVer, sizeof(DriverVer_type)))
921 return -EFAULT;
922 return 0;
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 case CCISS_REVALIDVOLS:
Mike Miller3833a742006-12-06 20:35:10 -0800926 return rebuild_lun_table(host, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700928 case CCISS_GETLUNINFO:{
929 LogvolInfo_struct luninfo;
930
931 luninfo.LunID = drv->LunID;
932 luninfo.num_opens = drv->usage_count;
933 luninfo.num_parts = 0;
934 if (copy_to_user(argp, &luninfo,
935 sizeof(LogvolInfo_struct)))
936 return -EFAULT;
937 return 0;
938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 case CCISS_DEREGDISK:
Mike Millerddd47442005-09-13 01:25:22 -0700940 return rebuild_lun_table(host, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 case CCISS_REGNEWD:
Mike Millerddd47442005-09-13 01:25:22 -0700943 return rebuild_lun_table(host, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 case CCISS_PASSTHRU:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700947 IOCTL_Command_struct iocommand;
948 CommandList_struct *c;
949 char *buff = NULL;
950 u64bit temp64;
951 unsigned long flags;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700952 DECLARE_COMPLETION_ONSTACK(wait);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700953
954 if (!arg)
955 return -EINVAL;
956
957 if (!capable(CAP_SYS_RAWIO))
958 return -EPERM;
959
960 if (copy_from_user
961 (&iocommand, argp, sizeof(IOCTL_Command_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return -EFAULT;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700963 if ((iocommand.buf_size < 1) &&
964 (iocommand.Request.Type.Direction != XFER_NONE)) {
965 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700967#if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
968 /* Check kmalloc limits */
969 if (iocommand.buf_size > 128000)
970 return -EINVAL;
971#endif
972 if (iocommand.buf_size > 0) {
973 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
974 if (buff == NULL)
975 return -EFAULT;
976 }
977 if (iocommand.Request.Type.Direction == XFER_WRITE) {
978 /* Copy the data into the buffer we created */
979 if (copy_from_user
980 (buff, iocommand.buf, iocommand.buf_size)) {
981 kfree(buff);
982 return -EFAULT;
983 }
984 } else {
985 memset(buff, 0, iocommand.buf_size);
986 }
987 if ((c = cmd_alloc(host, 0)) == NULL) {
988 kfree(buff);
989 return -ENOMEM;
990 }
991 // Fill in the command type
992 c->cmd_type = CMD_IOCTL_PEND;
993 // Fill in Command Header
994 c->Header.ReplyQueue = 0; // unused in simple mode
995 if (iocommand.buf_size > 0) // buffer to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700997 c->Header.SGList = 1;
998 c->Header.SGTotal = 1;
999 } else // no buffers to fill
1000 {
1001 c->Header.SGList = 0;
1002 c->Header.SGTotal = 0;
1003 }
1004 c->Header.LUN = iocommand.LUN_info;
1005 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
1006
1007 // Fill in Request block
1008 c->Request = iocommand.Request;
1009
1010 // Fill in the scatter gather information
1011 if (iocommand.buf_size > 0) {
1012 temp64.val = pci_map_single(host->pdev, buff,
1013 iocommand.buf_size,
1014 PCI_DMA_BIDIRECTIONAL);
1015 c->SG[0].Addr.lower = temp64.val32.lower;
1016 c->SG[0].Addr.upper = temp64.val32.upper;
1017 c->SG[0].Len = iocommand.buf_size;
1018 c->SG[0].Ext = 0; // we are not chaining
1019 }
1020 c->waiting = &wait;
1021
1022 /* Put the request on the tail of the request queue */
1023 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1024 addQ(&host->reqQ, c);
1025 host->Qdepth++;
1026 start_io(host);
1027 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1028
1029 wait_for_completion(&wait);
1030
1031 /* unlock the buffers from DMA */
1032 temp64.val32.lower = c->SG[0].Addr.lower;
1033 temp64.val32.upper = c->SG[0].Addr.upper;
1034 pci_unmap_single(host->pdev, (dma_addr_t) temp64.val,
1035 iocommand.buf_size,
1036 PCI_DMA_BIDIRECTIONAL);
1037
1038 /* Copy the error information out */
1039 iocommand.error_info = *(c->err_info);
1040 if (copy_to_user
1041 (argp, &iocommand, sizeof(IOCTL_Command_struct))) {
1042 kfree(buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 cmd_free(host, c, 0);
1044 return -EFAULT;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001047 if (iocommand.Request.Type.Direction == XFER_READ) {
1048 /* Copy the data out of the buffer we created */
1049 if (copy_to_user
1050 (iocommand.buf, buff, iocommand.buf_size)) {
1051 kfree(buff);
1052 cmd_free(host, c, 0);
1053 return -EFAULT;
1054 }
1055 }
1056 kfree(buff);
1057 cmd_free(host, c, 0);
1058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001060 case CCISS_BIG_PASSTHRU:{
1061 BIG_IOCTL_Command_struct *ioc;
1062 CommandList_struct *c;
1063 unsigned char **buff = NULL;
1064 int *buff_size = NULL;
1065 u64bit temp64;
1066 unsigned long flags;
1067 BYTE sg_used = 0;
1068 int status = 0;
1069 int i;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001070 DECLARE_COMPLETION_ONSTACK(wait);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001071 __u32 left;
1072 __u32 sz;
1073 BYTE __user *data_ptr;
1074
1075 if (!arg)
1076 return -EINVAL;
1077 if (!capable(CAP_SYS_RAWIO))
1078 return -EPERM;
1079 ioc = (BIG_IOCTL_Command_struct *)
1080 kmalloc(sizeof(*ioc), GFP_KERNEL);
1081 if (!ioc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 status = -ENOMEM;
1083 goto cleanup1;
1084 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001085 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
1086 status = -EFAULT;
1087 goto cleanup1;
1088 }
1089 if ((ioc->buf_size < 1) &&
1090 (ioc->Request.Type.Direction != XFER_NONE)) {
1091 status = -EINVAL;
1092 goto cleanup1;
1093 }
1094 /* Check kmalloc limits using all SGs */
1095 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
1096 status = -EINVAL;
1097 goto cleanup1;
1098 }
1099 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
1100 status = -EINVAL;
1101 goto cleanup1;
1102 }
1103 buff =
1104 kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
1105 if (!buff) {
1106 status = -ENOMEM;
1107 goto cleanup1;
1108 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001109 buff_size = kmalloc(MAXSGENTRIES * sizeof(int),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001110 GFP_KERNEL);
1111 if (!buff_size) {
1112 status = -ENOMEM;
1113 goto cleanup1;
1114 }
1115 left = ioc->buf_size;
1116 data_ptr = ioc->buf;
1117 while (left) {
1118 sz = (left >
1119 ioc->malloc_size) ? ioc->
1120 malloc_size : left;
1121 buff_size[sg_used] = sz;
1122 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
1123 if (buff[sg_used] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 status = -ENOMEM;
Jens Axboe15534d32005-11-18 22:02:44 +01001125 goto cleanup1;
1126 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001127 if (ioc->Request.Type.Direction == XFER_WRITE) {
1128 if (copy_from_user
1129 (buff[sg_used], data_ptr, sz)) {
1130 status = -ENOMEM;
1131 goto cleanup1;
1132 }
1133 } else {
1134 memset(buff[sg_used], 0, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001136 left -= sz;
1137 data_ptr += sz;
1138 sg_used++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001140 if ((c = cmd_alloc(host, 0)) == NULL) {
1141 status = -ENOMEM;
1142 goto cleanup1;
1143 }
1144 c->cmd_type = CMD_IOCTL_PEND;
1145 c->Header.ReplyQueue = 0;
1146
1147 if (ioc->buf_size > 0) {
1148 c->Header.SGList = sg_used;
1149 c->Header.SGTotal = sg_used;
1150 } else {
1151 c->Header.SGList = 0;
1152 c->Header.SGTotal = 0;
1153 }
1154 c->Header.LUN = ioc->LUN_info;
1155 c->Header.Tag.lower = c->busaddr;
1156
1157 c->Request = ioc->Request;
1158 if (ioc->buf_size > 0) {
1159 int i;
1160 for (i = 0; i < sg_used; i++) {
1161 temp64.val =
1162 pci_map_single(host->pdev, buff[i],
1163 buff_size[i],
1164 PCI_DMA_BIDIRECTIONAL);
1165 c->SG[i].Addr.lower =
1166 temp64.val32.lower;
1167 c->SG[i].Addr.upper =
1168 temp64.val32.upper;
1169 c->SG[i].Len = buff_size[i];
1170 c->SG[i].Ext = 0; /* we are not chaining */
1171 }
1172 }
1173 c->waiting = &wait;
1174 /* Put the request on the tail of the request queue */
1175 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1176 addQ(&host->reqQ, c);
1177 host->Qdepth++;
1178 start_io(host);
1179 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1180 wait_for_completion(&wait);
1181 /* unlock the buffers from DMA */
1182 for (i = 0; i < sg_used; i++) {
1183 temp64.val32.lower = c->SG[i].Addr.lower;
1184 temp64.val32.upper = c->SG[i].Addr.upper;
1185 pci_unmap_single(host->pdev,
1186 (dma_addr_t) temp64.val, buff_size[i],
1187 PCI_DMA_BIDIRECTIONAL);
1188 }
1189 /* Copy the error information out */
1190 ioc->error_info = *(c->err_info);
1191 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
1192 cmd_free(host, c, 0);
1193 status = -EFAULT;
1194 goto cleanup1;
1195 }
1196 if (ioc->Request.Type.Direction == XFER_READ) {
1197 /* Copy the data out of the buffer we created */
1198 BYTE __user *ptr = ioc->buf;
1199 for (i = 0; i < sg_used; i++) {
1200 if (copy_to_user
1201 (ptr, buff[i], buff_size[i])) {
1202 cmd_free(host, c, 0);
1203 status = -EFAULT;
1204 goto cleanup1;
1205 }
1206 ptr += buff_size[i];
1207 }
1208 }
1209 cmd_free(host, c, 0);
1210 status = 0;
1211 cleanup1:
1212 if (buff) {
1213 for (i = 0; i < sg_used; i++)
1214 kfree(buff[i]);
1215 kfree(buff);
1216 }
1217 kfree(buff_size);
1218 kfree(ioc);
1219 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07001221
1222 /* scsi_cmd_ioctl handles these, below, though some are not */
1223 /* very meaningful for cciss. SG_IO is the main one people want. */
1224
1225 case SG_GET_VERSION_NUM:
1226 case SG_SET_TIMEOUT:
1227 case SG_GET_TIMEOUT:
1228 case SG_GET_RESERVED_SIZE:
1229 case SG_SET_RESERVED_SIZE:
1230 case SG_EMULATED_HOST:
1231 case SG_IO:
1232 case SCSI_IOCTL_SEND_COMMAND:
S.Çağlar Onur9793c322007-07-16 21:56:51 +03001233 return scsi_cmd_ioctl(filep, disk->queue, disk, cmd, argp);
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07001234
1235 /* scsi_cmd_ioctl would normally handle these, below, but */
1236 /* they aren't a good fit for cciss, as CD-ROMs are */
1237 /* not supported, and we don't have any bus/target/lun */
1238 /* which we present to the kernel. */
1239
1240 case CDROM_SEND_PACKET:
1241 case CDROMCLOSETRAY:
1242 case CDROMEJECT:
1243 case SCSI_IOCTL_GET_IDLUN:
1244 case SCSI_IOCTL_GET_BUS_NUMBER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 default:
1246 return -ENOTTY;
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Jens Axboe7b30f092006-07-25 15:02:48 +02001250static void cciss_check_queues(ctlr_info_t *h)
1251{
1252 int start_queue = h->next_to_run;
1253 int i;
1254
1255 /* check to see if we have maxed out the number of commands that can
1256 * be placed on the queue. If so then exit. We do this check here
1257 * in case the interrupt we serviced was from an ioctl and did not
1258 * free any new commands.
1259 */
Mike Millerf8806322006-12-06 20:35:01 -08001260 if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds)
Jens Axboe7b30f092006-07-25 15:02:48 +02001261 return;
1262
1263 /* We have room on the queue for more commands. Now we need to queue
1264 * them up. We will also keep track of the next queue to run so
1265 * that every queue gets a chance to be started first.
1266 */
1267 for (i = 0; i < h->highest_lun + 1; i++) {
1268 int curr_queue = (start_queue + i) % (h->highest_lun + 1);
1269 /* make sure the disk has been added and the drive is real
1270 * because this can be called from the middle of init_one.
1271 */
1272 if (!(h->drv[curr_queue].queue) || !(h->drv[curr_queue].heads))
1273 continue;
1274 blk_start_queue(h->gendisk[curr_queue]->queue);
1275
1276 /* check to see if we have maxed out the number of commands
1277 * that can be placed on the queue.
1278 */
Mike Millerf8806322006-12-06 20:35:01 -08001279 if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds) {
Jens Axboe7b30f092006-07-25 15:02:48 +02001280 if (curr_queue == start_queue) {
1281 h->next_to_run =
1282 (start_queue + 1) % (h->highest_lun + 1);
1283 break;
1284 } else {
1285 h->next_to_run = curr_queue;
1286 break;
1287 }
1288 } else {
1289 curr_queue = (curr_queue + 1) % (h->highest_lun + 1);
1290 }
1291 }
1292}
1293
Mike Millerca1e0482006-04-10 15:38:07 -07001294static void cciss_softirq_done(struct request *rq)
1295{
1296 CommandList_struct *cmd = rq->completion_data;
1297 ctlr_info_t *h = hba[cmd->ctlr];
1298 unsigned long flags;
1299 u64bit temp64;
1300 int i, ddir;
1301
1302 if (cmd->Request.Type.Direction == XFER_READ)
1303 ddir = PCI_DMA_FROMDEVICE;
1304 else
1305 ddir = PCI_DMA_TODEVICE;
1306
1307 /* command did not need to be retried */
1308 /* unmap the DMA mapping for all the scatter gather elements */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001309 for (i = 0; i < cmd->Header.SGList; i++) {
Mike Millerca1e0482006-04-10 15:38:07 -07001310 temp64.val32.lower = cmd->SG[i].Addr.lower;
1311 temp64.val32.upper = cmd->SG[i].Addr.upper;
1312 pci_unmap_page(h->pdev, temp64.val, cmd->SG[i].Len, ddir);
1313 }
1314
Mike Millerca1e0482006-04-10 15:38:07 -07001315#ifdef CCISS_DEBUG
1316 printk("Done with %p\n", rq);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001317#endif /* CCISS_DEBUG */
Mike Millerca1e0482006-04-10 15:38:07 -07001318
Kiyoshi Ueda3daeea22007-12-11 17:50:03 -05001319 if (blk_end_request(rq, (rq->errors == 0) ? 0 : -EIO, blk_rq_bytes(rq)))
1320 BUG();
1321
Mike Millerca1e0482006-04-10 15:38:07 -07001322 spin_lock_irqsave(&h->lock, flags);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001323 cmd_free(h, cmd, 1);
Jens Axboe7b30f092006-07-25 15:02:48 +02001324 cciss_check_queues(h);
Mike Millerca1e0482006-04-10 15:38:07 -07001325 spin_unlock_irqrestore(&h->lock, flags);
1326}
1327
Mike Millerddd47442005-09-13 01:25:22 -07001328/* This function will check the usage_count of the drive to be updated/added.
1329 * If the usage_count is zero then the drive information will be updated and
1330 * the disk will be re-registered with the kernel. If not then it will be
1331 * left alone for the next reboot. The exception to this is disk 0 which
1332 * will always be left registered with the kernel since it is also the
1333 * controller node. Any changes to disk 0 will show up on the next
1334 * reboot.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001335 */
Mike Millerddd47442005-09-13 01:25:22 -07001336static void cciss_update_drive_info(int ctlr, int drv_index)
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001337{
Mike Millerddd47442005-09-13 01:25:22 -07001338 ctlr_info_t *h = hba[ctlr];
1339 struct gendisk *disk;
Mike Millerddd47442005-09-13 01:25:22 -07001340 InquiryData_struct *inq_buff = NULL;
1341 unsigned int block_size;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001342 sector_t total_size;
Mike Millerddd47442005-09-13 01:25:22 -07001343 unsigned long flags = 0;
1344 int ret = 0;
1345
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001346 /* if the disk already exists then deregister it before proceeding */
1347 if (h->drv[drv_index].raid_level != -1) {
Mike Millerddd47442005-09-13 01:25:22 -07001348 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
1349 h->drv[drv_index].busy_configuring = 1;
1350 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1351 ret = deregister_disk(h->gendisk[drv_index],
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001352 &h->drv[drv_index], 0);
Mike Millerddd47442005-09-13 01:25:22 -07001353 h->drv[drv_index].busy_configuring = 0;
1354 }
1355
1356 /* If the disk is in use return */
1357 if (ret)
1358 return;
1359
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001360 /* Get information about the disk and modify the driver structure */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001361 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
Mike Millerddd47442005-09-13 01:25:22 -07001362 if (inq_buff == NULL)
1363 goto mem_msg;
1364
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001365 /* testing to see if 16-byte CDBs are already being used */
1366 if (h->cciss_read == CCISS_READ_16) {
1367 cciss_read_capacity_16(h->ctlr, drv_index, 1,
1368 &total_size, &block_size);
1369 goto geo_inq;
1370 }
1371
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001372 cciss_read_capacity(ctlr, drv_index, 1,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001373 &total_size, &block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001374
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001375 /* if read_capacity returns all F's this volume is >2TB in size */
1376 /* so we switch to 16-byte CDB's for all read/write ops */
1377 if (total_size == 0xFFFFFFFFULL) {
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001378 cciss_read_capacity_16(ctlr, drv_index, 1,
1379 &total_size, &block_size);
1380 h->cciss_read = CCISS_READ_16;
1381 h->cciss_write = CCISS_WRITE_16;
1382 } else {
1383 h->cciss_read = CCISS_READ_10;
1384 h->cciss_write = CCISS_WRITE_10;
1385 }
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001386geo_inq:
Mike Millerddd47442005-09-13 01:25:22 -07001387 cciss_geometry_inquiry(ctlr, drv_index, 1, total_size, block_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001388 inq_buff, &h->drv[drv_index]);
Mike Millerddd47442005-09-13 01:25:22 -07001389
1390 ++h->num_luns;
1391 disk = h->gendisk[drv_index];
1392 set_capacity(disk, h->drv[drv_index].nr_blocks);
1393
Mike Millerddd47442005-09-13 01:25:22 -07001394 /* if it's the controller it's already added */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001395 if (drv_index) {
Mike Millerddd47442005-09-13 01:25:22 -07001396 disk->queue = blk_init_queue(do_cciss_request, &h->lock);
Mike Miller799202c2006-12-06 20:35:12 -08001397 sprintf(disk->disk_name, "cciss/c%dd%d", ctlr, drv_index);
1398 disk->major = h->major;
1399 disk->first_minor = drv_index << NWD_SHIFT;
1400 disk->fops = &cciss_fops;
1401 disk->private_data = &h->drv[drv_index];
Mike Millerddd47442005-09-13 01:25:22 -07001402
1403 /* Set up queue information */
Mike Millerddd47442005-09-13 01:25:22 -07001404 blk_queue_bounce_limit(disk->queue, hba[ctlr]->pdev->dma_mask);
1405
1406 /* This is a hardware imposed limit. */
1407 blk_queue_max_hw_segments(disk->queue, MAXSGENTRIES);
1408
1409 /* This is a limit in the driver and could be eliminated. */
1410 blk_queue_max_phys_segments(disk->queue, MAXSGENTRIES);
1411
Mike Miller92c4231a2006-12-06 20:35:06 -08001412 blk_queue_max_sectors(disk->queue, h->cciss_max_sectors);
Mike Millerddd47442005-09-13 01:25:22 -07001413
Mike Millerca1e0482006-04-10 15:38:07 -07001414 blk_queue_softirq_done(disk->queue, cciss_softirq_done);
1415
Mike Millerddd47442005-09-13 01:25:22 -07001416 disk->queue->queuedata = hba[ctlr];
1417
1418 blk_queue_hardsect_size(disk->queue,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001419 hba[ctlr]->drv[drv_index].block_size);
Mike Millerddd47442005-09-13 01:25:22 -07001420
1421 h->drv[drv_index].queue = disk->queue;
1422 add_disk(disk);
1423 }
1424
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001425 freeret:
Mike Millerddd47442005-09-13 01:25:22 -07001426 kfree(inq_buff);
1427 return;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001428 mem_msg:
Mike Millerddd47442005-09-13 01:25:22 -07001429 printk(KERN_ERR "cciss: out of memory\n");
1430 goto freeret;
1431}
1432
1433/* This function will find the first index of the controllers drive array
1434 * that has a -1 for the raid_level and will return that index. This is
1435 * where new drives will be added. If the index to be returned is greater
1436 * than the highest_lun index for the controller then highest_lun is set
1437 * to this new index. If there are no available indexes then -1 is returned.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001438 */
Mike Millerddd47442005-09-13 01:25:22 -07001439static int cciss_find_free_drive_index(int ctlr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Mike Millerddd47442005-09-13 01:25:22 -07001441 int i;
1442
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001443 for (i = 0; i < CISS_MAX_LUN; i++) {
1444 if (hba[ctlr]->drv[i].raid_level == -1) {
Mike Millerddd47442005-09-13 01:25:22 -07001445 if (i > hba[ctlr]->highest_lun)
1446 hba[ctlr]->highest_lun = i;
1447 return i;
1448 }
1449 }
1450 return -1;
1451}
1452
1453/* This function will add and remove logical drives from the Logical
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001454 * drive array of the controller and maintain persistency of ordering
Mike Millerddd47442005-09-13 01:25:22 -07001455 * so that mount points are preserved until the next reboot. This allows
1456 * for the removal of logical drives in the middle of the drive array
1457 * without a re-ordering of those drives.
1458 * INPUT
1459 * h = The controller to perform the operations on
1460 * del_disk = The disk to remove if specified. If the value given
1461 * is NULL then no disk is removed.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001462 */
Mike Millerddd47442005-09-13 01:25:22 -07001463static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk)
1464{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 int ctlr = h->ctlr;
Mike Millerddd47442005-09-13 01:25:22 -07001466 int num_luns;
1467 ReportLunData_struct *ld_buff = NULL;
1468 drive_info_struct *drv = NULL;
1469 int return_code;
1470 int listlength = 0;
1471 int i;
1472 int drv_found;
1473 int drv_index = 0;
1474 __u32 lunid = 0;
1475 unsigned long flags;
1476
1477 /* Set busy_configuring flag for this operation */
1478 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001479 if (h->busy_configuring) {
Mike Millerddd47442005-09-13 01:25:22 -07001480 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1481 return -EBUSY;
1482 }
1483 h->busy_configuring = 1;
1484
1485 /* if del_disk is NULL then we are being called to add a new disk
1486 * and update the logical drive table. If it is not NULL then
1487 * we will check if the disk is in use or not.
1488 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001489 if (del_disk != NULL) {
Mike Millerddd47442005-09-13 01:25:22 -07001490 drv = get_drv(del_disk);
1491 drv->busy_configuring = 1;
1492 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1493 return_code = deregister_disk(del_disk, drv, 1);
1494 drv->busy_configuring = 0;
1495 h->busy_configuring = 0;
1496 return return_code;
1497 } else {
1498 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1499 if (!capable(CAP_SYS_RAWIO))
1500 return -EPERM;
1501
1502 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1503 if (ld_buff == NULL)
1504 goto mem_msg;
1505
1506 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001507 sizeof(ReportLunData_struct), 0,
1508 0, 0, TYPE_CMD);
Mike Millerddd47442005-09-13 01:25:22 -07001509
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001510 if (return_code == IO_OK) {
Mike Miller799202c2006-12-06 20:35:12 -08001511 listlength =
Al Viro4c1f2b312007-03-14 09:19:10 +00001512 be32_to_cpu(*(__be32 *) ld_buff->LUNListLength);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001513 } else { /* reading number of logical volumes failed */
Mike Millerddd47442005-09-13 01:25:22 -07001514 printk(KERN_WARNING "cciss: report logical volume"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001515 " command failed\n");
Mike Millerddd47442005-09-13 01:25:22 -07001516 listlength = 0;
1517 goto freeret;
1518 }
1519
1520 num_luns = listlength / 8; /* 8 bytes per entry */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001521 if (num_luns > CISS_MAX_LUN) {
Mike Millerddd47442005-09-13 01:25:22 -07001522 num_luns = CISS_MAX_LUN;
1523 printk(KERN_WARNING "cciss: more luns configured"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001524 " on controller than can be handled by"
1525 " this driver.\n");
Mike Millerddd47442005-09-13 01:25:22 -07001526 }
1527
1528 /* Compare controller drive array to drivers drive array.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001529 * Check for updates in the drive information and any new drives
1530 * on the controller.
1531 */
1532 for (i = 0; i < num_luns; i++) {
Mike Millerddd47442005-09-13 01:25:22 -07001533 int j;
1534
1535 drv_found = 0;
1536
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001537 lunid = (0xff &
1538 (unsigned int)(ld_buff->LUN[i][3])) << 24;
1539 lunid |= (0xff &
1540 (unsigned int)(ld_buff->LUN[i][2])) << 16;
1541 lunid |= (0xff &
1542 (unsigned int)(ld_buff->LUN[i][1])) << 8;
1543 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
Mike Millerddd47442005-09-13 01:25:22 -07001544
1545 /* Find if the LUN is already in the drive array
1546 * of the controller. If so then update its info
1547 * if not is use. If it does not exist then find
1548 * the first free index and add it.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001549 */
1550 for (j = 0; j <= h->highest_lun; j++) {
1551 if (h->drv[j].LunID == lunid) {
Mike Millerddd47442005-09-13 01:25:22 -07001552 drv_index = j;
1553 drv_found = 1;
1554 }
1555 }
1556
1557 /* check if the drive was found already in the array */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001558 if (!drv_found) {
Mike Millerddd47442005-09-13 01:25:22 -07001559 drv_index = cciss_find_free_drive_index(ctlr);
1560 if (drv_index == -1)
1561 goto freeret;
1562
Mike Miller799202c2006-12-06 20:35:12 -08001563 /*Check if the gendisk needs to be allocated */
1564 if (!h->gendisk[drv_index]){
1565 h->gendisk[drv_index] = alloc_disk(1 << NWD_SHIFT);
1566 if (!h->gendisk[drv_index]){
1567 printk(KERN_ERR "cciss: could not allocate new disk %d\n", drv_index);
1568 goto mem_msg;
1569 }
1570 }
Mike Millerddd47442005-09-13 01:25:22 -07001571 }
1572 h->drv[drv_index].LunID = lunid;
1573 cciss_update_drive_info(ctlr, drv_index);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001574 } /* end for */
1575 } /* end else */
Mike Millerddd47442005-09-13 01:25:22 -07001576
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001577 freeret:
Mike Millerddd47442005-09-13 01:25:22 -07001578 kfree(ld_buff);
1579 h->busy_configuring = 0;
1580 /* We return -1 here to tell the ACU that we have registered/updated
1581 * all of the drives that we can and to keep it from calling us
1582 * additional times.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001583 */
Mike Millerddd47442005-09-13 01:25:22 -07001584 return -1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001585 mem_msg:
Mike Millerddd47442005-09-13 01:25:22 -07001586 printk(KERN_ERR "cciss: out of memory\n");
1587 goto freeret;
1588}
1589
1590/* This function will deregister the disk and it's queue from the
1591 * kernel. It must be called with the controller lock held and the
1592 * drv structures busy_configuring flag set. It's parameters are:
1593 *
1594 * disk = This is the disk to be deregistered
1595 * drv = This is the drive_info_struct associated with the disk to be
1596 * deregistered. It contains information about the disk used
1597 * by the driver.
1598 * clear_all = This flag determines whether or not the disk information
1599 * is going to be completely cleared out and the highest_lun
1600 * reset. Sometimes we want to clear out information about
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001601 * the disk in preparation for re-adding it. In this case
Mike Millerddd47442005-09-13 01:25:22 -07001602 * the highest_lun should be left unchanged and the LunID
1603 * should not be cleared.
1604*/
1605static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
1606 int clear_all)
1607{
Mike Miller799202c2006-12-06 20:35:12 -08001608 int i;
Mike Millerddd47442005-09-13 01:25:22 -07001609 ctlr_info_t *h = get_host(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 if (!capable(CAP_SYS_RAWIO))
1612 return -EPERM;
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /* make sure logical volume is NOT is use */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001615 if (clear_all || (h->gendisk[0] == disk)) {
1616 if (drv->usage_count > 1)
1617 return -EBUSY;
1618 } else if (drv->usage_count > 0)
1619 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Mike Millerddd47442005-09-13 01:25:22 -07001621 /* invalidate the devices and deregister the disk. If it is disk
1622 * zero do not deregister it but just zero out it's values. This
1623 * allows us to delete disk zero but keep the controller registered.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001624 */
1625 if (h->gendisk[0] != disk) {
Adrian Bunk5a9df732007-10-16 23:29:26 -07001626 struct request_queue *q = disk->queue;
1627 if (disk->flags & GENHD_FL_UP)
1628 del_gendisk(disk);
1629 if (q) {
1630 blk_cleanup_queue(q);
1631 /* Set drv->queue to NULL so that we do not try
1632 * to call blk_start_queue on this queue in the
1633 * interrupt handler
1634 */
1635 drv->queue = NULL;
1636 }
1637 /* If clear_all is set then we are deleting the logical
1638 * drive, not just refreshing its info. For drives
1639 * other than disk 0 we will call put_disk. We do not
1640 * do this for disk 0 as we need it to be able to
1641 * configure the controller.
1642 */
1643 if (clear_all){
1644 /* This isn't pretty, but we need to find the
1645 * disk in our array and NULL our the pointer.
1646 * This is so that we will call alloc_disk if
1647 * this index is used again later.
Mike Miller799202c2006-12-06 20:35:12 -08001648 */
Adrian Bunk5a9df732007-10-16 23:29:26 -07001649 for (i=0; i < CISS_MAX_LUN; i++){
1650 if(h->gendisk[i] == disk){
1651 h->gendisk[i] = NULL;
1652 break;
Mike Miller799202c2006-12-06 20:35:12 -08001653 }
Mike Miller799202c2006-12-06 20:35:12 -08001654 }
Adrian Bunk5a9df732007-10-16 23:29:26 -07001655 put_disk(disk);
Mike Millerddd47442005-09-13 01:25:22 -07001656 }
Mike Miller799202c2006-12-06 20:35:12 -08001657 } else {
1658 set_capacity(disk, 0);
Mike Millerddd47442005-09-13 01:25:22 -07001659 }
1660
1661 --h->num_luns;
1662 /* zero out the disk size info */
1663 drv->nr_blocks = 0;
1664 drv->block_size = 0;
1665 drv->heads = 0;
1666 drv->sectors = 0;
1667 drv->cylinders = 0;
1668 drv->raid_level = -1; /* This can be used as a flag variable to
1669 * indicate that this element of the drive
1670 * array is free.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001671 */
Mike Millerddd47442005-09-13 01:25:22 -07001672
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001673 if (clear_all) {
1674 /* check to see if it was the last disk */
1675 if (drv == h->drv + h->highest_lun) {
1676 /* if so, find the new hightest lun */
1677 int i, newhighest = -1;
1678 for (i = 0; i < h->highest_lun; i++) {
1679 /* if the disk has size > 0, it is available */
Mike Millerddd47442005-09-13 01:25:22 -07001680 if (h->drv[i].heads)
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001681 newhighest = i;
1682 }
1683 h->highest_lun = newhighest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
Mike Millerddd47442005-09-13 01:25:22 -07001685
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001686 drv->LunID = 0;
Mike Millerddd47442005-09-13 01:25:22 -07001687 }
Bjorn Helgaase2019b52006-06-25 05:49:05 -07001688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
Mike Millerddd47442005-09-13 01:25:22 -07001690
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001691static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, /* 0: address the controller,
1692 1: address logical volume log_unit,
1693 2: periph device address is scsi3addr */
1694 unsigned int log_unit, __u8 page_code,
1695 unsigned char *scsi3addr, int cmd_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001697 ctlr_info_t *h = hba[ctlr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 u64bit buff_dma_handle;
1699 int status = IO_OK;
1700
1701 c->cmd_type = CMD_IOCTL_PEND;
1702 c->Header.ReplyQueue = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001703 if (buff != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 c->Header.SGList = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001705 c->Header.SGTotal = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 } else {
1707 c->Header.SGList = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001708 c->Header.SGTotal = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710 c->Header.Tag.lower = c->busaddr;
1711
1712 c->Request.Type.Type = cmd_type;
1713 if (cmd_type == TYPE_CMD) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001714 switch (cmd) {
1715 case CISS_INQUIRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /* If the logical unit number is 0 then, this is going
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001717 to controller so It's a physical command
1718 mode = 0 target = 0. So we have nothing to write.
1719 otherwise, if use_unit_num == 1,
1720 mode = 1(volume set addressing) target = LUNID
1721 otherwise, if use_unit_num == 2,
1722 mode = 0(periph dev addr) target = scsi3addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (use_unit_num == 1) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001724 c->Header.LUN.LogDev.VolId =
1725 h->drv[log_unit].LunID;
1726 c->Header.LUN.LogDev.Mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 } else if (use_unit_num == 2) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001728 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr,
1729 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 c->Header.LUN.LogDev.Mode = 0;
1731 }
1732 /* are we trying to read a vital product page */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001733 if (page_code != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 c->Request.CDB[1] = 0x01;
1735 c->Request.CDB[2] = page_code;
1736 }
1737 c->Request.CDBLen = 6;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001738 c->Request.Type.Attribute = ATTR_SIMPLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 c->Request.Type.Direction = XFER_READ;
1740 c->Request.Timeout = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001741 c->Request.CDB[0] = CISS_INQUIRY;
1742 c->Request.CDB[4] = size & 0xFF;
1743 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 case CISS_REPORT_LOG:
1745 case CISS_REPORT_PHYS:
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001746 /* Talking to controller so It's a physical command
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 mode = 00 target = 0. Nothing to write.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001748 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 c->Request.CDBLen = 12;
1750 c->Request.Type.Attribute = ATTR_SIMPLE;
1751 c->Request.Type.Direction = XFER_READ;
1752 c->Request.Timeout = 0;
1753 c->Request.CDB[0] = cmd;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001754 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 c->Request.CDB[7] = (size >> 16) & 0xFF;
1756 c->Request.CDB[8] = (size >> 8) & 0xFF;
1757 c->Request.CDB[9] = size & 0xFF;
1758 break;
1759
1760 case CCISS_READ_CAPACITY:
1761 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1762 c->Header.LUN.LogDev.Mode = 1;
1763 c->Request.CDBLen = 10;
1764 c->Request.Type.Attribute = ATTR_SIMPLE;
1765 c->Request.Type.Direction = XFER_READ;
1766 c->Request.Timeout = 0;
1767 c->Request.CDB[0] = cmd;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001768 break;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001769 case CCISS_READ_CAPACITY_16:
1770 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1771 c->Header.LUN.LogDev.Mode = 1;
1772 c->Request.CDBLen = 16;
1773 c->Request.Type.Attribute = ATTR_SIMPLE;
1774 c->Request.Type.Direction = XFER_READ;
1775 c->Request.Timeout = 0;
1776 c->Request.CDB[0] = cmd;
1777 c->Request.CDB[1] = 0x10;
1778 c->Request.CDB[10] = (size >> 24) & 0xFF;
1779 c->Request.CDB[11] = (size >> 16) & 0xFF;
1780 c->Request.CDB[12] = (size >> 8) & 0xFF;
1781 c->Request.CDB[13] = size & 0xFF;
1782 c->Request.Timeout = 0;
1783 c->Request.CDB[0] = cmd;
1784 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 case CCISS_CACHE_FLUSH:
1786 c->Request.CDBLen = 12;
1787 c->Request.Type.Attribute = ATTR_SIMPLE;
1788 c->Request.Type.Direction = XFER_WRITE;
1789 c->Request.Timeout = 0;
1790 c->Request.CDB[0] = BMIC_WRITE;
1791 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001792 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 default:
1794 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001795 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
Bjorn Helgaase2019b52006-06-25 05:49:05 -07001796 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798 } else if (cmd_type == TYPE_MSG) {
1799 switch (cmd) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001800 case 0: /* ABORT message */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001801 c->Request.CDBLen = 12;
1802 c->Request.Type.Attribute = ATTR_SIMPLE;
1803 c->Request.Type.Direction = XFER_WRITE;
1804 c->Request.Timeout = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001805 c->Request.CDB[0] = cmd; /* abort */
1806 c->Request.CDB[1] = 0; /* abort a command */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001807 /* buff contains the tag of the command to abort */
1808 memcpy(&c->Request.CDB[4], buff, 8);
1809 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001810 case 1: /* RESET message */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001811 c->Request.CDBLen = 12;
1812 c->Request.Type.Attribute = ATTR_SIMPLE;
1813 c->Request.Type.Direction = XFER_WRITE;
1814 c->Request.Timeout = 0;
1815 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001816 c->Request.CDB[0] = cmd; /* reset */
1817 c->Request.CDB[1] = 0x04; /* reset a LUN */
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 case 3: /* No-Op message */
1820 c->Request.CDBLen = 1;
1821 c->Request.Type.Attribute = ATTR_SIMPLE;
1822 c->Request.Type.Direction = XFER_WRITE;
1823 c->Request.Timeout = 0;
1824 c->Request.CDB[0] = cmd;
1825 break;
1826 default:
1827 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001828 "cciss%d: unknown message type %d\n", ctlr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return IO_ERROR;
1830 }
1831 } else {
1832 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001833 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return IO_ERROR;
1835 }
1836 /* Fill in the scatter gather information */
1837 if (size > 0) {
1838 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001839 buff, size,
1840 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1842 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1843 c->SG[0].Len = size;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001844 c->SG[0].Ext = 0; /* we are not chaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846 return status;
1847}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001848
1849static int sendcmd_withirq(__u8 cmd,
1850 int ctlr,
1851 void *buff,
1852 size_t size,
1853 unsigned int use_unit_num,
1854 unsigned int log_unit, __u8 page_code, int cmd_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 ctlr_info_t *h = hba[ctlr];
1857 CommandList_struct *c;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001858 u64bit buff_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 unsigned long flags;
1860 int return_status;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001861 DECLARE_COMPLETION_ONSTACK(wait);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001862
1863 if ((c = cmd_alloc(h, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return -ENOMEM;
1865 return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001866 log_unit, page_code, NULL, cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (return_status != IO_OK) {
1868 cmd_free(h, c, 0);
1869 return return_status;
1870 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001871 resend_cmd2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 c->waiting = &wait;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 /* Put the request on the tail of the queue and send it */
1875 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1876 addQ(&h->reqQ, c);
1877 h->Qdepth++;
1878 start_io(h);
1879 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 wait_for_completion(&wait);
1882
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001883 if (c->err_info->CommandStatus != 0) { /* an error has occurred */
1884 switch (c->err_info->CommandStatus) {
1885 case CMD_TARGET_STATUS:
1886 printk(KERN_WARNING "cciss: cmd %p has "
1887 " completed with errors\n", c);
1888 if (c->err_info->ScsiStatus) {
1889 printk(KERN_WARNING "cciss: cmd %p "
1890 "has SCSI Status = %x\n",
1891 c, c->err_info->ScsiStatus);
1892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001895 case CMD_DATA_UNDERRUN:
1896 case CMD_DATA_OVERRUN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /* expected for inquire and report lun commands */
1898 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001899 case CMD_INVALID:
1900 printk(KERN_WARNING "cciss: Cmd %p is "
1901 "reported invalid\n", c);
1902 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001904 case CMD_PROTOCOL_ERR:
1905 printk(KERN_WARNING "cciss: cmd %p has "
1906 "protocol error \n", c);
1907 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001909 case CMD_HARDWARE_ERR:
1910 printk(KERN_WARNING "cciss: cmd %p had "
1911 " hardware error\n", c);
1912 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001914 case CMD_CONNECTION_LOST:
1915 printk(KERN_WARNING "cciss: cmd %p had "
1916 "connection lost\n", c);
1917 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001919 case CMD_ABORTED:
1920 printk(KERN_WARNING "cciss: cmd %p was "
1921 "aborted\n", c);
1922 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001924 case CMD_ABORT_FAILED:
1925 printk(KERN_WARNING "cciss: cmd %p reports "
1926 "abort failed\n", c);
1927 return_status = IO_ERROR;
1928 break;
1929 case CMD_UNSOLICITED_ABORT:
1930 printk(KERN_WARNING
1931 "cciss%d: unsolicited abort %p\n", ctlr, c);
1932 if (c->retry_count < MAX_CMD_RETRIES) {
1933 printk(KERN_WARNING
1934 "cciss%d: retrying %p\n", ctlr, c);
1935 c->retry_count++;
1936 /* erase the old error information */
1937 memset(c->err_info, 0,
1938 sizeof(ErrorInfo_struct));
1939 return_status = IO_OK;
1940 INIT_COMPLETION(wait);
1941 goto resend_cmd2;
1942 }
1943 return_status = IO_ERROR;
1944 break;
1945 default:
1946 printk(KERN_WARNING "cciss: cmd %p returned "
1947 "unknown status %x\n", c,
1948 c->err_info->CommandStatus);
1949 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 /* unlock the buffers from DMA */
Mike Millerbb2a37b2005-09-13 01:25:24 -07001953 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
1954 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001955 pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
1956 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 cmd_free(h, c, 0);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001958 return return_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961static void cciss_geometry_inquiry(int ctlr, int logvol,
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001962 int withirq, sector_t total_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001963 unsigned int block_size,
1964 InquiryData_struct *inq_buff,
1965 drive_info_struct *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 int return_code;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001968 unsigned long t;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 memset(inq_buff, 0, sizeof(InquiryData_struct));
1971 if (withirq)
1972 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001973 inq_buff, sizeof(*inq_buff), 1,
1974 logvol, 0xC1, TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 else
1976 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001977 sizeof(*inq_buff), 1, logvol, 0xC1, NULL,
1978 TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (return_code == IO_OK) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001980 if (inq_buff->data_byte[8] == 0xFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001982 "cciss: reading geometry failed, volume "
1983 "does not support reading geometry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 drv->heads = 255;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001985 drv->sectors = 32; // Sectors per track
Mike Miller (OS Dev)7f42d3b2007-04-04 19:08:23 -07001986 drv->cylinders = total_size + 1;
Mike Miller89f97ad2006-12-18 10:59:39 +01001987 drv->raid_level = RAID_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 drv->heads = inq_buff->data_byte[6];
1990 drv->sectors = inq_buff->data_byte[7];
1991 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
1992 drv->cylinders += inq_buff->data_byte[5];
1993 drv->raid_level = inq_buff->data_byte[8];
Matthew Wilcox3f7705e2006-10-21 10:24:19 -07001994 }
1995 drv->block_size = block_size;
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001996 drv->nr_blocks = total_size + 1;
Matthew Wilcox3f7705e2006-10-21 10:24:19 -07001997 t = drv->heads * drv->sectors;
1998 if (t > 1) {
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001999 sector_t real_size = total_size + 1;
2000 unsigned long rem = sector_div(real_size, t);
Matthew Wilcox3f7705e2006-10-21 10:24:19 -07002001 if (rem)
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08002002 real_size++;
2003 drv->cylinders = real_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002005 } else { /* Get geometry failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 printk(KERN_WARNING "cciss: reading geometry failed\n");
2007 }
Metathronius Galabantcc088d12006-09-30 23:27:47 -07002008 printk(KERN_INFO " heads=%d, sectors=%d, cylinders=%d\n\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002009 drv->heads, drv->sectors, drv->cylinders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012static void
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002013cciss_read_capacity(int ctlr, int logvol, int withirq, sector_t *total_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002014 unsigned int *block_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002016 ReadCapdata_struct *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 int return_code;
Mariusz Kozlowski1aebe182007-08-11 22:34:29 +02002018
2019 buf = kzalloc(sizeof(ReadCapdata_struct), GFP_KERNEL);
2020 if (!buf) {
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002021 printk(KERN_WARNING "cciss: out of memory\n");
2022 return;
2023 }
Mariusz Kozlowski1aebe182007-08-11 22:34:29 +02002024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (withirq)
2026 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002027 ctlr, buf, sizeof(ReadCapdata_struct),
2028 1, logvol, 0, TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 else
2030 return_code = sendcmd(CCISS_READ_CAPACITY,
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002031 ctlr, buf, sizeof(ReadCapdata_struct),
2032 1, logvol, 0, NULL, TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (return_code == IO_OK) {
Al Viro4c1f2b312007-03-14 09:19:10 +00002034 *total_size = be32_to_cpu(*(__be32 *) buf->total_size);
2035 *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002036 } else { /* read capacity command failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 printk(KERN_WARNING "cciss: read capacity failed\n");
2038 *total_size = 0;
2039 *block_size = BLOCK_SIZE;
2040 }
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08002041 if (*total_size != 0)
Randy Dunlap7b92aad2006-10-28 10:38:40 -07002042 printk(KERN_INFO " blocks= %llu block_size= %d\n",
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08002043 (unsigned long long)*total_size+1, *block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002044 kfree(buf);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002045}
2046
2047static void
2048cciss_read_capacity_16(int ctlr, int logvol, int withirq, sector_t *total_size, unsigned int *block_size)
2049{
2050 ReadCapdata_struct_16 *buf;
2051 int return_code;
Mariusz Kozlowski1aebe182007-08-11 22:34:29 +02002052
2053 buf = kzalloc(sizeof(ReadCapdata_struct_16), GFP_KERNEL);
2054 if (!buf) {
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002055 printk(KERN_WARNING "cciss: out of memory\n");
2056 return;
2057 }
Mariusz Kozlowski1aebe182007-08-11 22:34:29 +02002058
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002059 if (withirq) {
2060 return_code = sendcmd_withirq(CCISS_READ_CAPACITY_16,
2061 ctlr, buf, sizeof(ReadCapdata_struct_16),
2062 1, logvol, 0, TYPE_CMD);
2063 }
2064 else {
2065 return_code = sendcmd(CCISS_READ_CAPACITY_16,
2066 ctlr, buf, sizeof(ReadCapdata_struct_16),
2067 1, logvol, 0, NULL, TYPE_CMD);
2068 }
2069 if (return_code == IO_OK) {
Al Viro4c1f2b312007-03-14 09:19:10 +00002070 *total_size = be64_to_cpu(*(__be64 *) buf->total_size);
2071 *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002072 } else { /* read capacity command failed */
2073 printk(KERN_WARNING "cciss: read capacity failed\n");
2074 *total_size = 0;
2075 *block_size = BLOCK_SIZE;
2076 }
Randy Dunlap7b92aad2006-10-28 10:38:40 -07002077 printk(KERN_INFO " blocks= %llu block_size= %d\n",
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08002078 (unsigned long long)*total_size+1, *block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002079 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082static int cciss_revalidate(struct gendisk *disk)
2083{
2084 ctlr_info_t *h = get_host(disk);
2085 drive_info_struct *drv = get_drv(disk);
2086 int logvol;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002087 int FOUND = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 unsigned int block_size;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002089 sector_t total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 InquiryData_struct *inq_buff = NULL;
2091
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002092 for (logvol = 0; logvol < CISS_MAX_LUN; logvol++) {
2093 if (h->drv[logvol].LunID == drv->LunID) {
2094 FOUND = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 break;
2096 }
2097 }
2098
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002099 if (!FOUND)
2100 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002102 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
2103 if (inq_buff == NULL) {
2104 printk(KERN_WARNING "cciss: out of memory\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002105 return 1;
2106 }
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002107 if (h->cciss_read == CCISS_READ_10) {
2108 cciss_read_capacity(h->ctlr, logvol, 1,
2109 &total_size, &block_size);
2110 } else {
2111 cciss_read_capacity_16(h->ctlr, logvol, 1,
2112 &total_size, &block_size);
2113 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002114 cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size,
2115 inq_buff, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Mike Millerad2b9312005-07-28 01:07:31 -07002117 blk_queue_hardsect_size(drv->queue, drv->block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 set_capacity(disk, drv->nr_blocks);
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 kfree(inq_buff);
2121 return 0;
2122}
2123
2124/*
2125 * Wait polling for a command to complete.
2126 * The memory mapped FIFO is polled for the completion.
2127 * Used only at init time, interrupts from the HBA are disabled.
2128 */
2129static unsigned long pollcomplete(int ctlr)
2130{
2131 unsigned long done;
2132 int i;
2133
2134 /* Wait (up to 20 seconds) for a command to complete */
2135
2136 for (i = 20 * HZ; i > 0; i--) {
2137 done = hba[ctlr]->access.command_completed(hba[ctlr]);
Nishanth Aravamudan86e84862005-09-10 00:27:28 -07002138 if (done == FIFO_EMPTY)
2139 schedule_timeout_uninterruptible(1);
2140 else
Bjorn Helgaase2019b52006-06-25 05:49:05 -07002141 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 /* Invalid address to tell caller we ran out of time */
2144 return 1;
2145}
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002146
2147static int add_sendcmd_reject(__u8 cmd, int ctlr, unsigned long complete)
2148{
2149 /* We get in here if sendcmd() is polling for completions
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002150 and gets some command back that it wasn't expecting --
2151 something other than that which it just sent down.
2152 Ordinarily, that shouldn't happen, but it can happen when
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002153 the scsi tape stuff gets into error handling mode, and
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002154 starts using sendcmd() to try to abort commands and
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002155 reset tape drives. In that case, sendcmd may pick up
2156 completions of commands that were sent to logical drives
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002157 through the block i/o system, or cciss ioctls completing, etc.
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002158 In that case, we need to save those completions for later
2159 processing by the interrupt handler.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002160 */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002161
2162#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002163 struct sendcmd_reject_list *srl = &hba[ctlr]->scsi_rejects;
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002164
2165 /* If it's not the scsi tape stuff doing error handling, (abort */
2166 /* or reset) then we don't expect anything weird. */
2167 if (cmd != CCISS_RESET_MSG && cmd != CCISS_ABORT_MSG) {
2168#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002169 printk(KERN_WARNING "cciss cciss%d: SendCmd "
2170 "Invalid command list address returned! (%lx)\n",
2171 ctlr, complete);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002172 /* not much we can do. */
2173#ifdef CONFIG_CISS_SCSI_TAPE
2174 return 1;
2175 }
2176
2177 /* We've sent down an abort or reset, but something else
2178 has completed */
Mike Millerf8806322006-12-06 20:35:01 -08002179 if (srl->ncompletions >= (hba[ctlr]->nr_cmds + 2)) {
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002180 /* Uh oh. No room to save it for later... */
2181 printk(KERN_WARNING "cciss%d: Sendcmd: Invalid command addr, "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002182 "reject list overflow, command lost!\n", ctlr);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002183 return 1;
2184 }
2185 /* Save it for later */
2186 srl->complete[srl->ncompletions] = complete;
2187 srl->ncompletions++;
2188#endif
2189 return 0;
2190}
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192/*
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002193 * Send a command to the controller, and wait for it to complete.
2194 * Only used at init time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002196static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, /* 0: address the controller,
2197 1: address logical volume log_unit,
2198 2: periph device address is scsi3addr */
2199 unsigned int log_unit,
2200 __u8 page_code, unsigned char *scsi3addr, int cmd_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 CommandList_struct *c;
2203 int i;
2204 unsigned long complete;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002205 ctlr_info_t *info_p = hba[ctlr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 u64bit buff_dma_handle;
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002207 int status, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 if ((c = cmd_alloc(info_p, 1)) == NULL) {
2210 printk(KERN_WARNING "cciss: unable to get memory");
Bjorn Helgaase2019b52006-06-25 05:49:05 -07002211 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213 status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002214 log_unit, page_code, scsi3addr, cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (status != IO_OK) {
2216 cmd_free(info_p, c, 1);
2217 return status;
2218 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002219 resend_cmd1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /*
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002221 * Disable interrupt
2222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223#ifdef CCISS_DEBUG
2224 printk(KERN_DEBUG "cciss: turning intr off\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002225#endif /* CCISS_DEBUG */
2226 info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 /* Make sure there is room in the command FIFO */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002229 /* Actually it should be completely empty at this time */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002230 /* unless we are in here doing error handling for the scsi */
2231 /* tape side of the driver. */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002232 for (i = 200000; i > 0; i--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 /* if fifo isn't full go */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002234 if (!(info_p->access.fifo_full(info_p))) {
2235
2236 break;
2237 }
2238 udelay(10);
2239 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
2240 " waiting!\n", ctlr);
2241 }
2242 /*
2243 * Send the cmd
2244 */
2245 info_p->access.submit_command(info_p, c);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002246 done = 0;
2247 do {
2248 complete = pollcomplete(ctlr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250#ifdef CCISS_DEBUG
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002251 printk(KERN_DEBUG "cciss: command completed\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002252#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002254 if (complete == 1) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002255 printk(KERN_WARNING
2256 "cciss cciss%d: SendCmd Timeout out, "
2257 "No command list address returned!\n", ctlr);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002258 status = IO_ERROR;
2259 done = 1;
2260 break;
2261 }
2262
2263 /* This will need to change for direct lookup completions */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002264 if ((complete & CISS_ERROR_BIT)
2265 && (complete & ~CISS_ERROR_BIT) == c->busaddr) {
2266 /* if data overrun or underun on Report command
2267 ignore it
2268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
2270 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
2271 (c->Request.CDB[0] == CISS_INQUIRY)) &&
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002272 ((c->err_info->CommandStatus ==
2273 CMD_DATA_OVERRUN) ||
2274 (c->err_info->CommandStatus == CMD_DATA_UNDERRUN)
2275 )) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 complete = c->busaddr;
2277 } else {
2278 if (c->err_info->CommandStatus ==
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002279 CMD_UNSOLICITED_ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 printk(KERN_WARNING "cciss%d: "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002281 "unsolicited abort %p\n",
2282 ctlr, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (c->retry_count < MAX_CMD_RETRIES) {
2284 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002285 "cciss%d: retrying %p\n",
2286 ctlr, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 c->retry_count++;
2288 /* erase the old error */
2289 /* information */
2290 memset(c->err_info, 0,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002291 sizeof
2292 (ErrorInfo_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 goto resend_cmd1;
2294 } else {
2295 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002296 "cciss%d: retried %p too "
2297 "many times\n", ctlr, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 status = IO_ERROR;
2299 goto cleanup1;
2300 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002301 } else if (c->err_info->CommandStatus ==
2302 CMD_UNABORTABLE) {
2303 printk(KERN_WARNING
2304 "cciss%d: command could not be aborted.\n",
2305 ctlr);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002306 status = IO_ERROR;
2307 goto cleanup1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309 printk(KERN_WARNING "ciss ciss%d: sendcmd"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002310 " Error %x \n", ctlr,
2311 c->err_info->CommandStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 printk(KERN_WARNING "ciss ciss%d: sendcmd"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002313 " offensive info\n"
2314 " size %x\n num %x value %x\n",
2315 ctlr,
2316 c->err_info->MoreErrInfo.Invalid_Cmd.
2317 offense_size,
2318 c->err_info->MoreErrInfo.Invalid_Cmd.
2319 offense_num,
2320 c->err_info->MoreErrInfo.Invalid_Cmd.
2321 offense_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 status = IO_ERROR;
2323 goto cleanup1;
2324 }
2325 }
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002326 /* This will need changing for direct lookup completions */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002327 if (complete != c->busaddr) {
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002328 if (add_sendcmd_reject(cmd, ctlr, complete) != 0) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002329 BUG(); /* we are pretty much hosed if we get here. */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002330 }
2331 continue;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002332 } else
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002333 done = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002334 } while (!done);
2335
2336 cleanup1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 /* unlock the data buffer from DMA */
Mike Millerbb2a37b2005-09-13 01:25:24 -07002338 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
2339 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002341 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002342#ifdef CONFIG_CISS_SCSI_TAPE
2343 /* if we saved some commands for later, process them now. */
2344 if (info_p->scsi_rejects.ncompletions > 0)
David Howells7d12e782006-10-05 14:55:46 +01002345 do_cciss_intr(0, info_p);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 cmd_free(info_p, c, 1);
Bjorn Helgaase2019b52006-06-25 05:49:05 -07002348 return status;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002349}
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351/*
2352 * Map (physical) PCI mem into (virtual) kernel space
2353 */
2354static void __iomem *remap_pci_mem(ulong base, ulong size)
2355{
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002356 ulong page_base = ((ulong) base) & PAGE_MASK;
2357 ulong page_offs = ((ulong) base) - page_base;
2358 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002360 return page_remapped ? (page_remapped + page_offs) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002363/*
2364 * Takes jobs of the Q and sends them to the hardware, then puts it on
2365 * the Q to wait for completion.
2366 */
2367static void start_io(ctlr_info_t *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
2369 CommandList_struct *c;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002370
2371 while ((c = h->reqQ) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 /* can't do anything if fifo is full */
2373 if ((h->access.fifo_full(h))) {
2374 printk(KERN_WARNING "cciss: fifo full\n");
2375 break;
2376 }
2377
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002378 /* Get the first entry from the Request Q */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 removeQ(&(h->reqQ), c);
2380 h->Qdepth--;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002381
2382 /* Tell the controller execute command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 h->access.submit_command(h, c);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002384
2385 /* Put job onto the completed Q */
2386 addQ(&(h->cmpQ), c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
2388}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002389
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390/* Assumes that CCISS_LOCK(h->ctlr) is held. */
2391/* Zeros out the error record and then resends the command back */
2392/* to the controller */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002393static inline void resend_cciss_cmd(ctlr_info_t *h, CommandList_struct *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
2395 /* erase the old error information */
2396 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
2397
2398 /* add it to software queue and then send it to the controller */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002399 addQ(&(h->reqQ), c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 h->Qdepth++;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002401 if (h->Qdepth > h->maxQsinceinit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 h->maxQsinceinit = h->Qdepth;
2403
2404 start_io(h);
2405}
Jens Axboea9925a02006-01-09 16:04:06 +01002406
Steve Cameron1a614f52007-10-16 23:27:37 -07002407static inline unsigned int make_status_bytes(unsigned int scsi_status_byte,
2408 unsigned int msg_byte, unsigned int host_byte,
2409 unsigned int driver_byte)
2410{
2411 /* inverse of macros in scsi.h */
2412 return (scsi_status_byte & 0xff) |
2413 ((msg_byte & 0xff) << 8) |
2414 ((host_byte & 0xff) << 16) |
2415 ((driver_byte & 0xff) << 24);
2416}
2417
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002418static inline int evaluate_target_status(CommandList_struct *cmd)
2419{
2420 unsigned char sense_key;
Steve Cameron1a614f52007-10-16 23:27:37 -07002421 unsigned char status_byte, msg_byte, host_byte, driver_byte;
2422 int error_value;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002423
Steve Cameron1a614f52007-10-16 23:27:37 -07002424 /* If we get in here, it means we got "target status", that is, scsi status */
2425 status_byte = cmd->err_info->ScsiStatus;
2426 driver_byte = DRIVER_OK;
2427 msg_byte = cmd->err_info->CommandStatus; /* correct? seems too device specific */
2428
2429 if (blk_pc_request(cmd->rq))
2430 host_byte = DID_PASSTHROUGH;
2431 else
2432 host_byte = DID_OK;
2433
2434 error_value = make_status_bytes(status_byte, msg_byte,
2435 host_byte, driver_byte);
2436
2437 if (cmd->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) {
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002438 if (!blk_pc_request(cmd->rq))
2439 printk(KERN_WARNING "cciss: cmd %p "
2440 "has SCSI Status 0x%x\n",
2441 cmd, cmd->err_info->ScsiStatus);
Steve Cameron1a614f52007-10-16 23:27:37 -07002442 return error_value;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002443 }
2444
2445 /* check the sense key */
2446 sense_key = 0xf & cmd->err_info->SenseInfo[2];
2447 /* no status or recovered error */
Steve Cameron1a614f52007-10-16 23:27:37 -07002448 if (((sense_key == 0x0) || (sense_key == 0x1)) && !blk_pc_request(cmd->rq))
2449 error_value = 0;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002450
2451 if (!blk_pc_request(cmd->rq)) { /* Not SG_IO or similar? */
Steve Cameron1a614f52007-10-16 23:27:37 -07002452 if (error_value != 0)
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002453 printk(KERN_WARNING "cciss: cmd %p has CHECK CONDITION"
2454 " sense key = 0x%x\n", cmd, sense_key);
Steve Cameron1a614f52007-10-16 23:27:37 -07002455 return error_value;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002456 }
2457
2458 /* SG_IO or similar, copy sense data back */
2459 if (cmd->rq->sense) {
2460 if (cmd->rq->sense_len > cmd->err_info->SenseLen)
2461 cmd->rq->sense_len = cmd->err_info->SenseLen;
2462 memcpy(cmd->rq->sense, cmd->err_info->SenseInfo,
2463 cmd->rq->sense_len);
2464 } else
2465 cmd->rq->sense_len = 0;
2466
Steve Cameron1a614f52007-10-16 23:27:37 -07002467 return error_value;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002468}
2469
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002470/* checks the status of the job and calls complete buffers to mark all
Jens Axboea9925a02006-01-09 16:04:06 +01002471 * buffers for the completed job. Note that this function does not need
2472 * to hold the hba/queue lock.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002473 */
2474static inline void complete_command(ctlr_info_t *h, CommandList_struct *cmd,
2475 int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 int retry_cmd = 0;
Mike Miller (OS Dev)198b7662007-05-08 00:29:34 -07002478 struct request *rq = cmd->rq;
2479
2480 rq->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002482 if (timeout)
Steve Cameron1a614f52007-10-16 23:27:37 -07002483 rq->errors = make_status_bytes(0, 0, 0, DRIVER_TIMEOUT);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002484
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002485 if (cmd->err_info->CommandStatus == 0) /* no error has occurred */
2486 goto after_error_processing;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002487
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002488 switch (cmd->err_info->CommandStatus) {
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002489 case CMD_TARGET_STATUS:
Mike Miller (OS Dev)198b7662007-05-08 00:29:34 -07002490 rq->errors = evaluate_target_status(cmd);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002491 break;
2492 case CMD_DATA_UNDERRUN:
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002493 if (blk_fs_request(cmd->rq)) {
2494 printk(KERN_WARNING "cciss: cmd %p has"
2495 " completed with data underrun "
2496 "reported\n", cmd);
2497 cmd->rq->data_len = cmd->err_info->ResidualCnt;
2498 }
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002499 break;
2500 case CMD_DATA_OVERRUN:
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002501 if (blk_fs_request(cmd->rq))
2502 printk(KERN_WARNING "cciss: cmd %p has"
2503 " completed with data overrun "
2504 "reported\n", cmd);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002505 break;
2506 case CMD_INVALID:
2507 printk(KERN_WARNING "cciss: cmd %p is "
2508 "reported invalid\n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002509 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2510 cmd->err_info->CommandStatus, DRIVER_OK,
2511 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002512 break;
2513 case CMD_PROTOCOL_ERR:
2514 printk(KERN_WARNING "cciss: cmd %p has "
2515 "protocol error \n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002516 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2517 cmd->err_info->CommandStatus, DRIVER_OK,
2518 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002519 break;
2520 case CMD_HARDWARE_ERR:
2521 printk(KERN_WARNING "cciss: cmd %p had "
2522 " hardware error\n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002523 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2524 cmd->err_info->CommandStatus, DRIVER_OK,
2525 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002526 break;
2527 case CMD_CONNECTION_LOST:
2528 printk(KERN_WARNING "cciss: cmd %p had "
2529 "connection lost\n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002530 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2531 cmd->err_info->CommandStatus, DRIVER_OK,
2532 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002533 break;
2534 case CMD_ABORTED:
2535 printk(KERN_WARNING "cciss: cmd %p was "
2536 "aborted\n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002537 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2538 cmd->err_info->CommandStatus, DRIVER_OK,
2539 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002540 break;
2541 case CMD_ABORT_FAILED:
2542 printk(KERN_WARNING "cciss: cmd %p reports "
2543 "abort failed\n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002544 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2545 cmd->err_info->CommandStatus, DRIVER_OK,
2546 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002547 break;
2548 case CMD_UNSOLICITED_ABORT:
2549 printk(KERN_WARNING "cciss%d: unsolicited "
2550 "abort %p\n", h->ctlr, cmd);
2551 if (cmd->retry_count < MAX_CMD_RETRIES) {
2552 retry_cmd = 1;
2553 printk(KERN_WARNING
2554 "cciss%d: retrying %p\n", h->ctlr, cmd);
2555 cmd->retry_count++;
2556 } else
2557 printk(KERN_WARNING
2558 "cciss%d: %p retried too "
2559 "many times\n", h->ctlr, cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002560 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2561 cmd->err_info->CommandStatus, DRIVER_OK,
2562 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002563 break;
2564 case CMD_TIMEOUT:
2565 printk(KERN_WARNING "cciss: cmd %p timedout\n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002566 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2567 cmd->err_info->CommandStatus, DRIVER_OK,
2568 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002569 break;
2570 default:
2571 printk(KERN_WARNING "cciss: cmd %p returned "
2572 "unknown status %x\n", cmd,
2573 cmd->err_info->CommandStatus);
Steve Cameron1a614f52007-10-16 23:27:37 -07002574 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2575 cmd->err_info->CommandStatus, DRIVER_OK,
2576 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002578
2579after_error_processing:
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 /* We need to return this command */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002582 if (retry_cmd) {
2583 resend_cciss_cmd(h, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002585 }
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002586 cmd->rq->completion_data = cmd;
Jens Axboea9925a02006-01-09 16:04:06 +01002587 blk_complete_request(cmd->rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002590/*
2591 * Get a request and submit it to the controller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 */
Jens Axboe165125e2007-07-24 09:28:11 +02002593static void do_cciss_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002595 ctlr_info_t *h = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 CommandList_struct *c;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002597 sector_t start_blk;
2598 int seg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 struct request *creq;
2600 u64bit temp64;
2601 struct scatterlist tmp_sg[MAXSGENTRIES];
2602 drive_info_struct *drv;
2603 int i, dir;
2604
2605 /* We call start_io here in case there is a command waiting on the
2606 * queue that has not been sent.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if (blk_queue_plugged(q))
2609 goto startio;
2610
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002611 queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 creq = elv_next_request(q);
2613 if (!creq)
2614 goto startio;
2615
Eric Sesterhenn089fe1b2006-03-24 18:50:27 +01002616 BUG_ON(creq->nr_phys_segments > MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002618 if ((c = cmd_alloc(h, 1)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 goto full;
2620
2621 blkdev_dequeue_request(creq);
2622
2623 spin_unlock_irq(q->queue_lock);
2624
2625 c->cmd_type = CMD_RWREQ;
2626 c->rq = creq;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002627
2628 /* fill in the request */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 drv = creq->rq_disk->private_data;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002630 c->Header.ReplyQueue = 0; // unused in simple mode
Mike Miller33079b22005-09-13 01:25:22 -07002631 /* got command from pool, so use the command block index instead */
2632 /* for direct lookups. */
2633 /* The first 2 bits are reserved for controller error reporting. */
2634 c->Header.Tag.lower = (c->cmdindex << 3);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002635 c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */
2636 c->Header.LUN.LogDev.VolId = drv->LunID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 c->Header.LUN.LogDev.Mode = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002638 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
2639 c->Request.Type.Type = TYPE_CMD; // It is a command.
2640 c->Request.Type.Attribute = ATTR_SIMPLE;
2641 c->Request.Type.Direction =
Mike Millera52de242006-12-18 11:00:14 +01002642 (rq_data_dir(creq) == READ) ? XFER_READ : XFER_WRITE;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002643 c->Request.Timeout = 0; // Don't time out
2644 c->Request.CDB[0] =
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002645 (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 start_blk = creq->sector;
2647#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002648 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n", (int)creq->sector,
2649 (int)creq->nr_sectors);
2650#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Jens Axboe45711f12007-10-22 21:19:53 +02002652 sg_init_table(tmp_sg, MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 seg = blk_rq_map_sg(q, creq, tmp_sg);
2654
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002655 /* get the DMA records for the setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (c->Request.Type.Direction == XFER_READ)
2657 dir = PCI_DMA_FROMDEVICE;
2658 else
2659 dir = PCI_DMA_TODEVICE;
2660
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002661 for (i = 0; i < seg; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 c->SG[i].Len = tmp_sg[i].length;
Jens Axboe45711f12007-10-22 21:19:53 +02002663 temp64.val = (__u64) pci_map_page(h->pdev, sg_page(&tmp_sg[i]),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002664 tmp_sg[i].offset,
2665 tmp_sg[i].length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 c->SG[i].Addr.lower = temp64.val32.lower;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002667 c->SG[i].Addr.upper = temp64.val32.upper;
2668 c->SG[i].Ext = 0; // we are not chaining
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002670 /* track how many SG entries we are using */
2671 if (seg > h->maxSG)
2672 h->maxSG = seg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
2674#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002675 printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n",
2676 creq->nr_sectors, seg);
2677#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679 c->Header.SGList = c->Header.SGTotal = seg;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002680 if (likely(blk_fs_request(creq))) {
2681 if(h->cciss_read == CCISS_READ_10) {
2682 c->Request.CDB[1] = 0;
2683 c->Request.CDB[2] = (start_blk >> 24) & 0xff; //MSB
2684 c->Request.CDB[3] = (start_blk >> 16) & 0xff;
2685 c->Request.CDB[4] = (start_blk >> 8) & 0xff;
2686 c->Request.CDB[5] = start_blk & 0xff;
2687 c->Request.CDB[6] = 0; // (sect >> 24) & 0xff; MSB
2688 c->Request.CDB[7] = (creq->nr_sectors >> 8) & 0xff;
2689 c->Request.CDB[8] = creq->nr_sectors & 0xff;
2690 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2691 } else {
Randy Dunlap582539e2008-02-06 01:36:54 -08002692 u32 upper32 = upper_32_bits(start_blk);
2693
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002694 c->Request.CDBLen = 16;
2695 c->Request.CDB[1]= 0;
Randy Dunlap582539e2008-02-06 01:36:54 -08002696 c->Request.CDB[2]= (upper32 >> 24) & 0xff; //MSB
2697 c->Request.CDB[3]= (upper32 >> 16) & 0xff;
2698 c->Request.CDB[4]= (upper32 >> 8) & 0xff;
2699 c->Request.CDB[5]= upper32 & 0xff;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002700 c->Request.CDB[6]= (start_blk >> 24) & 0xff;
2701 c->Request.CDB[7]= (start_blk >> 16) & 0xff;
2702 c->Request.CDB[8]= (start_blk >> 8) & 0xff;
2703 c->Request.CDB[9]= start_blk & 0xff;
2704 c->Request.CDB[10]= (creq->nr_sectors >> 24) & 0xff;
2705 c->Request.CDB[11]= (creq->nr_sectors >> 16) & 0xff;
2706 c->Request.CDB[12]= (creq->nr_sectors >> 8) & 0xff;
2707 c->Request.CDB[13]= creq->nr_sectors & 0xff;
2708 c->Request.CDB[14] = c->Request.CDB[15] = 0;
2709 }
2710 } else if (blk_pc_request(creq)) {
2711 c->Request.CDBLen = creq->cmd_len;
2712 memcpy(c->Request.CDB, creq->cmd, BLK_MAX_CDB);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002713 } else {
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002714 printk(KERN_WARNING "cciss%d: bad request type %d\n", h->ctlr, creq->cmd_type);
2715 BUG();
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 spin_lock_irq(q->queue_lock);
2719
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002720 addQ(&(h->reqQ), c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 h->Qdepth++;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002722 if (h->Qdepth > h->maxQsinceinit)
2723 h->maxQsinceinit = h->Qdepth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 goto queue;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002726full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 blk_stop_queue(q);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002728startio:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 /* We will already have the driver lock here so not need
2730 * to lock it.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002731 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 start_io(h);
2733}
2734
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002735static inline unsigned long get_next_completion(ctlr_info_t *h)
2736{
2737#ifdef CONFIG_CISS_SCSI_TAPE
2738 /* Any rejects from sendcmd() lying around? Process them first */
2739 if (h->scsi_rejects.ncompletions == 0)
2740 return h->access.command_completed(h);
2741 else {
2742 struct sendcmd_reject_list *srl;
2743 int n;
2744 srl = &h->scsi_rejects;
2745 n = --srl->ncompletions;
2746 /* printk("cciss%d: processing saved reject\n", h->ctlr); */
2747 printk("p");
2748 return srl->complete[n];
2749 }
2750#else
2751 return h->access.command_completed(h);
2752#endif
2753}
2754
2755static inline int interrupt_pending(ctlr_info_t *h)
2756{
2757#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002758 return (h->access.intr_pending(h)
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002759 || (h->scsi_rejects.ncompletions > 0));
2760#else
2761 return h->access.intr_pending(h);
2762#endif
2763}
2764
2765static inline long interrupt_not_for_us(ctlr_info_t *h)
2766{
2767#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002768 return (((h->access.intr_pending(h) == 0) ||
2769 (h->interrupts_enabled == 0))
2770 && (h->scsi_rejects.ncompletions == 0));
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002771#else
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002772 return (((h->access.intr_pending(h) == 0) ||
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002773 (h->interrupts_enabled == 0)));
2774#endif
2775}
2776
David Howells7d12e782006-10-05 14:55:46 +01002777static irqreturn_t do_cciss_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
2779 ctlr_info_t *h = dev_id;
2780 CommandList_struct *c;
2781 unsigned long flags;
Mike Miller33079b22005-09-13 01:25:22 -07002782 __u32 a, a1, a2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002784 if (interrupt_not_for_us(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 /*
2787 * If there are completed commands in the completion queue,
2788 * we had better do something about it.
2789 */
2790 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002791 while (interrupt_pending(h)) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002792 while ((a = get_next_completion(h)) != FIFO_EMPTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 a1 = a;
Mike Miller33079b22005-09-13 01:25:22 -07002794 if ((a & 0x04)) {
2795 a2 = (a >> 3);
Mike Millerf8806322006-12-06 20:35:01 -08002796 if (a2 >= h->nr_cmds) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002797 printk(KERN_WARNING
2798 "cciss: controller cciss%d failed, stopping.\n",
2799 h->ctlr);
Mike Miller33079b22005-09-13 01:25:22 -07002800 fail_all_cmds(h->ctlr);
2801 return IRQ_HANDLED;
2802 }
2803
2804 c = h->cmd_pool + a2;
2805 a = c->busaddr;
2806
2807 } else {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002808 a &= ~3;
Mike Miller33079b22005-09-13 01:25:22 -07002809 if ((c = h->cmpQ) == NULL) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002810 printk(KERN_WARNING
2811 "cciss: Completion of %08x ignored\n",
2812 a1);
2813 continue;
2814 }
2815 while (c->busaddr != a) {
2816 c = c->next;
2817 if (c == h->cmpQ)
2818 break;
2819 }
Mike Miller33079b22005-09-13 01:25:22 -07002820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 /*
2822 * If we've found the command, take it off the
2823 * completion Q and free it
2824 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002825 if (c->busaddr == a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 removeQ(&h->cmpQ, c);
2827 if (c->cmd_type == CMD_RWREQ) {
2828 complete_command(h, c, 0);
2829 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2830 complete(c->waiting);
2831 }
2832# ifdef CONFIG_CISS_SCSI_TAPE
2833 else if (c->cmd_type == CMD_SCSI)
2834 complete_scsi_command(c, 0, a1);
2835# endif
2836 continue;
2837 }
2838 }
2839 }
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2842 return IRQ_HANDLED;
2843}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002844
2845/*
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07002846 * We cannot read the structure directly, for portability we must use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 * the io functions.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002848 * This is for debug only.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 */
2850#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002851static void print_cfg_table(CfgTable_struct *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
2853 int i;
2854 char temp_name[17];
2855
2856 printk("Controller Configuration information\n");
2857 printk("------------------------------------\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002858 for (i = 0; i < 4; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 temp_name[i] = readb(&(tb->Signature[i]));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002860 temp_name[4] = '\0';
2861 printk(" Signature = %s\n", temp_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002863 printk(" Transport methods supported = 0x%x\n",
2864 readl(&(tb->TransportSupport)));
2865 printk(" Transport methods active = 0x%x\n",
2866 readl(&(tb->TransportActive)));
2867 printk(" Requested transport Method = 0x%x\n",
2868 readl(&(tb->HostWrite.TransportRequest)));
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07002869 printk(" Coalesce Interrupt Delay = 0x%x\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002870 readl(&(tb->HostWrite.CoalIntDelay)));
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07002871 printk(" Coalesce Interrupt Count = 0x%x\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002872 readl(&(tb->HostWrite.CoalIntCount)));
2873 printk(" Max outstanding commands = 0x%d\n",
2874 readl(&(tb->CmdsOutMax)));
2875 printk(" Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
2876 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 temp_name[i] = readb(&(tb->ServerName[i]));
2878 temp_name[16] = '\0';
2879 printk(" Server Name = %s\n", temp_name);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002880 printk(" Heartbeat Counter = 0x%x\n\n\n", readl(&(tb->HeartBeat)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002882#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002884static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
2886 int i, offset, mem_type, bar_type;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002887 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return 0;
2889 offset = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002890 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2891 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
2893 offset += 4;
2894 else {
2895 mem_type = pci_resource_flags(pdev, i) &
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002896 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 switch (mem_type) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002898 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2899 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
2900 offset += 4; /* 32 bit */
2901 break;
2902 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2903 offset += 8;
2904 break;
2905 default: /* reserved in PCI 2.2 */
2906 printk(KERN_WARNING
2907 "Base address is invalid\n");
2908 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 break;
2910 }
2911 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002912 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
2913 return i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
2915 return -1;
2916}
2917
Mike Millerfb86a352006-01-08 01:03:50 -08002918/* If MSI/MSI-X is supported by the kernel we will try to enable it on
2919 * controllers that are capable. If not, we use IO-APIC mode.
2920 */
2921
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002922static void __devinit cciss_interrupt_mode(ctlr_info_t *c,
2923 struct pci_dev *pdev, __u32 board_id)
Mike Millerfb86a352006-01-08 01:03:50 -08002924{
2925#ifdef CONFIG_PCI_MSI
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002926 int err;
2927 struct msix_entry cciss_msix_entries[4] = { {0, 0}, {0, 1},
2928 {0, 2}, {0, 3}
2929 };
Mike Millerfb86a352006-01-08 01:03:50 -08002930
2931 /* Some boards advertise MSI but don't really support it */
2932 if ((board_id == 0x40700E11) ||
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002933 (board_id == 0x40800E11) ||
2934 (board_id == 0x40820E11) || (board_id == 0x40830E11))
Mike Millerfb86a352006-01-08 01:03:50 -08002935 goto default_int_mode;
2936
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002937 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
2938 err = pci_enable_msix(pdev, cciss_msix_entries, 4);
2939 if (!err) {
2940 c->intr[0] = cciss_msix_entries[0].vector;
2941 c->intr[1] = cciss_msix_entries[1].vector;
2942 c->intr[2] = cciss_msix_entries[2].vector;
2943 c->intr[3] = cciss_msix_entries[3].vector;
2944 c->msix_vector = 1;
2945 return;
2946 }
2947 if (err > 0) {
2948 printk(KERN_WARNING "cciss: only %d MSI-X vectors "
2949 "available\n", err);
Mike Miller1ecb9c02006-12-06 20:35:13 -08002950 goto default_int_mode;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002951 } else {
2952 printk(KERN_WARNING "cciss: MSI-X init failed %d\n",
2953 err);
Mike Miller1ecb9c02006-12-06 20:35:13 -08002954 goto default_int_mode;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002955 }
2956 }
2957 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
2958 if (!pci_enable_msi(pdev)) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002959 c->msi_vector = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002960 } else {
2961 printk(KERN_WARNING "cciss: MSI init failed\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002962 }
2963 }
Mike Miller1ecb9c02006-12-06 20:35:13 -08002964default_int_mode:
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002965#endif /* CONFIG_PCI_MSI */
Mike Millerfb86a352006-01-08 01:03:50 -08002966 /* if we get here we're going to use the default interrupt mode */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002967 c->intr[SIMPLE_MODE_INT] = pdev->irq;
Mike Millerfb86a352006-01-08 01:03:50 -08002968 return;
2969}
2970
Randy Dunlap7d1fd972008-01-14 00:55:17 -08002971static int __devinit cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
2973 ushort subsystem_vendor_id, subsystem_device_id, command;
2974 __u32 board_id, scratchpad = 0;
2975 __u64 cfg_offset;
2976 __u32 cfg_base_addr;
2977 __u64 cfg_base_addr_index;
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002978 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
2980 /* check to see if controller has been disabled */
2981 /* BEFORE trying to enable it */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002982 (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
2983 if (!(command & 0x02)) {
2984 printk(KERN_WARNING
2985 "cciss: controller appears to be disabled\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002986 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
2988
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002989 err = pci_enable_device(pdev);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002990 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Bjorn Helgaas4e570302006-06-25 05:49:02 -07002995 err = pci_request_regions(pdev, "cciss");
2996 if (err) {
2997 printk(KERN_ERR "cciss: Cannot obtain PCI resources, "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002998 "aborting\n");
Mike Miller872225c2006-12-13 00:34:22 -08002999 return err;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003000 }
3001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 subsystem_vendor_id = pdev->subsystem_vendor;
3003 subsystem_device_id = pdev->subsystem_device;
3004 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003005 subsystem_vendor_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007#ifdef CCISS_DEBUG
3008 printk("command = %x\n", command);
3009 printk("irq = %x\n", pdev->irq);
3010 printk("board_id = %x\n", board_id);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003011#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Mike Millerfb86a352006-01-08 01:03:50 -08003013/* If the kernel supports MSI/MSI-X we will try to enable that functionality,
3014 * else we use the IO-APIC interrupt assigned to us by system ROM.
3015 */
3016 cciss_interrupt_mode(c, pdev, board_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 /*
3019 * Memory base addr is first addr , the second points to the config
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003020 * table
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 */
3022
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003023 c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024#ifdef CCISS_DEBUG
3025 printk("address 0 = %x\n", c->paddr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003026#endif /* CCISS_DEBUG */
Mike Millera5b92872006-12-13 00:34:21 -08003027 c->vaddr = remap_pci_mem(c->paddr, 0x250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
3029 /* Wait for the board to become ready. (PCI hotplug needs this.)
3030 * We poll for up to 120 secs, once per 100ms. */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003031 for (i = 0; i < 1200; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
3033 if (scratchpad == CCISS_FIRMWARE_READY)
3034 break;
3035 set_current_state(TASK_INTERRUPTIBLE);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003036 schedule_timeout(HZ / 10); /* wait 100ms */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
3038 if (scratchpad != CCISS_FIRMWARE_READY) {
3039 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003040 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003041 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
3043
3044 /* get the address index number */
3045 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
3046 cfg_base_addr &= (__u32) 0x0000ffff;
3047#ifdef CCISS_DEBUG
3048 printk("cfg base address = %x\n", cfg_base_addr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003049#endif /* CCISS_DEBUG */
3050 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051#ifdef CCISS_DEBUG
3052 printk("cfg base address index = %x\n", cfg_base_addr_index);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003053#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 if (cfg_base_addr_index == -1) {
3055 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003056 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003057 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
3059
3060 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
3061#ifdef CCISS_DEBUG
3062 printk("cfg offset = %x\n", cfg_offset);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003063#endif /* CCISS_DEBUG */
3064 c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
3065 cfg_base_addr_index) +
3066 cfg_offset, sizeof(CfgTable_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 c->board_id = board_id;
3068
3069#ifdef CCISS_DEBUG
Tobias Klauser945f3902006-01-08 01:05:11 -08003070 print_cfg_table(c->cfgtable);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003071#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003073 for (i = 0; i < ARRAY_SIZE(products); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 if (board_id == products[i].board_id) {
3075 c->product_name = products[i].product_name;
3076 c->access = *(products[i].access);
Mike Millerf8806322006-12-06 20:35:01 -08003077 c->nr_cmds = products[i].nr_cmds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 break;
3079 }
3080 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003081 if ((readb(&c->cfgtable->Signature[0]) != 'C') ||
3082 (readb(&c->cfgtable->Signature[1]) != 'I') ||
3083 (readb(&c->cfgtable->Signature[2]) != 'S') ||
3084 (readb(&c->cfgtable->Signature[3]) != 'S')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 printk("Does not appear to be a valid CISS config table\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003086 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003087 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
Mike Miller4ff9a9a2006-12-06 20:35:00 -08003089 /* We didn't find the controller in our list. We know the
3090 * signature is valid. If it's an HP device let's try to
3091 * bind to the device and fire it up. Otherwise we bail.
3092 */
3093 if (i == ARRAY_SIZE(products)) {
3094 if (subsystem_vendor_id == PCI_VENDOR_ID_HP) {
3095 c->product_name = products[i-1].product_name;
3096 c->access = *(products[i-1].access);
Mike Millerf8806322006-12-06 20:35:01 -08003097 c->nr_cmds = products[i-1].nr_cmds;
Mike Miller4ff9a9a2006-12-06 20:35:00 -08003098 printk(KERN_WARNING "cciss: This is an unknown "
3099 "Smart Array controller.\n"
3100 "cciss: Please update to the latest driver "
3101 "available from www.hp.com.\n");
3102 } else {
3103 printk(KERN_WARNING "cciss: Sorry, I don't know how"
3104 " to access the Smart Array controller %08lx\n"
3105 , (unsigned long)board_id);
3106 err = -ENODEV;
3107 goto err_out_free_res;
3108 }
3109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110#ifdef CONFIG_X86
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003111 {
3112 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3113 __u32 prefetch;
3114 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
3115 prefetch |= 0x100;
3116 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
3117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118#endif
3119
Mike Miller (OS Dev)8bf50f72007-10-17 10:10:04 +02003120 /* Disabling DMA prefetch and refetch for the P600.
3121 * An ASIC bug may result in accesses to invalid memory addresses.
3122 * We've disabled prefetch for some time now. Testing with XEN
3123 * kernels revealed a bug in the refetch if dom0 resides on a P600.
Mike Millerf92e2f52006-12-06 20:35:04 -08003124 */
3125 if(board_id == 0x3225103C) {
3126 __u32 dma_prefetch;
Mike Miller (OS Dev)8bf50f72007-10-17 10:10:04 +02003127 __u32 dma_refetch;
Mike Millerf92e2f52006-12-06 20:35:04 -08003128 dma_prefetch = readl(c->vaddr + I2O_DMA1_CFG);
3129 dma_prefetch |= 0x8000;
3130 writel(dma_prefetch, c->vaddr + I2O_DMA1_CFG);
Mike Miller (OS Dev)8bf50f72007-10-17 10:10:04 +02003131 pci_read_config_dword(pdev, PCI_COMMAND_PARITY, &dma_refetch);
3132 dma_refetch |= 0x1;
3133 pci_write_config_dword(pdev, PCI_COMMAND_PARITY, dma_refetch);
Mike Millerf92e2f52006-12-06 20:35:04 -08003134 }
3135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136#ifdef CCISS_DEBUG
3137 printk("Trying to put board into Simple mode\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003138#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003140 /* Update the field, and then ring the doorbell */
3141 writel(CFGTBL_Trans_Simple, &(c->cfgtable->HostWrite.TransportRequest));
3142 writel(CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
3144 /* under certain very rare conditions, this can take awhile.
3145 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3146 * as we enter this code.) */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003147 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3149 break;
3150 /* delay and try again */
3151 set_current_state(TASK_INTERRUPTIBLE);
3152 schedule_timeout(10);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003156 printk(KERN_DEBUG "I counter got to %d %x\n", i,
3157 readl(c->vaddr + SA5_DOORBELL));
3158#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003160 print_cfg_table(c->cfgtable);
3161#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003163 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 printk(KERN_WARNING "cciss: unable to get board into"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003165 " simple mode\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003166 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003167 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 }
3169 return 0;
3170
Linus Torvalds5faad622006-12-13 09:15:34 -08003171err_out_free_res:
Mike Miller872225c2006-12-13 00:34:22 -08003172 /*
3173 * Deliberately omit pci_disable_device(): it does something nasty to
3174 * Smart Array controllers that pci_enable_device does not undo
3175 */
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003176 pci_release_regions(pdev);
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003177 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003180/*
3181 * Gets information about the local volumes attached to the controller.
3182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183static void cciss_getgeometry(int cntl_num)
3184{
3185 ReportLunData_struct *ld_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 InquiryData_struct *inq_buff;
3187 int return_code;
3188 int i;
3189 int listlength = 0;
3190 __u32 lunid = 0;
Al Virob4482a42007-10-14 19:35:40 +01003191 unsigned block_size;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003192 sector_t total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Eric Sesterhenn06ff37f2006-03-08 11:21:52 +01003194 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003195 if (ld_buff == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 printk(KERN_ERR "cciss: out of memory\n");
3197 return;
3198 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003199 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
3200 if (inq_buff == NULL) {
3201 printk(KERN_ERR "cciss: out of memory\n");
3202 kfree(ld_buff);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003203 return;
3204 }
3205 /* Get the firmware version */
3206 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
3207 sizeof(InquiryData_struct), 0, 0, 0, NULL,
3208 TYPE_CMD);
3209 if (return_code == IO_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
3211 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
3212 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
3213 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003214 } else { /* send command failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003216 printk(KERN_WARNING "cciss: unable to determine firmware"
3217 " version of controller\n");
3218 }
3219 /* Get the number of logical volumes */
3220 return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
3221 sizeof(ReportLunData_struct), 0, 0, 0, NULL,
3222 TYPE_CMD);
3223
3224 if (return_code == IO_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225#ifdef CCISS_DEBUG
3226 printk("LUN Data\n--------------------------\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003227#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003229 listlength |=
3230 (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
3231 listlength |=
3232 (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
3233 listlength |=
3234 (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003236 } else { /* reading number of logical volumes failed */
3237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 printk(KERN_WARNING "cciss: report logical volume"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003239 " command failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 listlength = 0;
3241 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003242 hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
3243 if (hba[cntl_num]->num_luns > CISS_MAX_LUN) {
3244 printk(KERN_ERR
3245 "ciss: only %d number of logical volumes supported\n",
3246 CISS_MAX_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 hba[cntl_num]->num_luns = CISS_MAX_LUN;
3248 }
3249#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003250 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n",
3251 ld_buff->LUNListLength[0], ld_buff->LUNListLength[1],
3252 ld_buff->LUNListLength[2], ld_buff->LUNListLength[3],
3253 hba[cntl_num]->num_luns);
3254#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003256 hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns - 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003257 for (i = 0; i < CISS_MAX_LUN; i++) {
3258 if (i < hba[cntl_num]->num_luns) {
3259 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3]))
3260 << 24;
3261 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2]))
3262 << 16;
3263 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1]))
3264 << 8;
3265 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003267 hba[cntl_num]->drv[i].LunID = lunid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003270 printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
3271 ld_buff->LUN[i][0], ld_buff->LUN[i][1],
3272 ld_buff->LUN[i][2], ld_buff->LUN[i][3],
3273 hba[cntl_num]->drv[i].LunID);
3274#endif /* CCISS_DEBUG */
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003275
3276 /* testing to see if 16-byte CDBs are already being used */
3277 if(hba[cntl_num]->cciss_read == CCISS_READ_16) {
3278 cciss_read_capacity_16(cntl_num, i, 0,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003279 &total_size, &block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003280 goto geo_inq;
3281 }
3282 cciss_read_capacity(cntl_num, i, 0, &total_size, &block_size);
3283
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08003284 /* If read_capacity returns all F's the logical is >2TB */
3285 /* so we switch to 16-byte CDBs for all read/write ops */
3286 if(total_size == 0xFFFFFFFFULL) {
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003287 cciss_read_capacity_16(cntl_num, i, 0,
3288 &total_size, &block_size);
3289 hba[cntl_num]->cciss_read = CCISS_READ_16;
3290 hba[cntl_num]->cciss_write = CCISS_WRITE_16;
3291 } else {
3292 hba[cntl_num]->cciss_read = CCISS_READ_10;
3293 hba[cntl_num]->cciss_write = CCISS_WRITE_10;
3294 }
3295geo_inq:
Mike Millerddd47442005-09-13 01:25:22 -07003296 cciss_geometry_inquiry(cntl_num, i, 0, total_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003297 block_size, inq_buff,
3298 &hba[cntl_num]->drv[i]);
Mike Millerddd47442005-09-13 01:25:22 -07003299 } else {
3300 /* initialize raid_level to indicate a free space */
3301 hba[cntl_num]->drv[i].raid_level = -1;
3302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
3304 kfree(ld_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 kfree(inq_buff);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003306}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
3308/* Function to find the first free pointer into our hba[] array */
3309/* Returns -1 if no free entries are left. */
3310static int alloc_cciss_hba(void)
3311{
Mike Miller799202c2006-12-06 20:35:12 -08003312 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003314 for (i = 0; i < MAX_CTLR; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 if (!hba[i]) {
3316 ctlr_info_t *p;
Jesper Juhlf2912a12007-07-31 00:39:39 -07003317
Eric Sesterhenn06ff37f2006-03-08 11:21:52 +01003318 p = kzalloc(sizeof(ctlr_info_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 if (!p)
3320 goto Enomem;
Mike Miller799202c2006-12-06 20:35:12 -08003321 p->gendisk[0] = alloc_disk(1 << NWD_SHIFT);
Jesper Juhlf2912a12007-07-31 00:39:39 -07003322 if (!p->gendisk[0]) {
3323 kfree(p);
Mike Miller799202c2006-12-06 20:35:12 -08003324 goto Enomem;
Jesper Juhlf2912a12007-07-31 00:39:39 -07003325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 hba[i] = p;
3327 return i;
3328 }
3329 }
3330 printk(KERN_WARNING "cciss: This driver supports a maximum"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003331 " of %d controllers.\n", MAX_CTLR);
Mike Miller799202c2006-12-06 20:35:12 -08003332 return -1;
3333Enomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 printk(KERN_ERR "cciss: out of memory.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return -1;
3336}
3337
3338static void free_hba(int i)
3339{
3340 ctlr_info_t *p = hba[i];
3341 int n;
3342
3343 hba[i] = NULL;
Mike Miller799202c2006-12-06 20:35:12 -08003344 for (n = 0; n < CISS_MAX_LUN; n++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 put_disk(p->gendisk[n]);
3346 kfree(p);
3347}
3348
3349/*
3350 * This is it. Find all the controllers and register them. I really hate
3351 * stealing all these major device numbers.
3352 * returns the number of block devices registered.
3353 */
3354static int __devinit cciss_init_one(struct pci_dev *pdev,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003355 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 int i;
Mike Miller799202c2006-12-06 20:35:12 -08003358 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 int rc;
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003360 int dac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 i = alloc_cciss_hba();
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003363 if (i < 0)
Bjorn Helgaase2019b52006-06-25 05:49:05 -07003364 return -1;
Mike Miller1f8ef382005-09-13 01:25:21 -07003365
3366 hba[i]->busy_initializing = 1;
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 if (cciss_pci_init(hba[i], pdev) != 0)
3369 goto clean1;
3370
3371 sprintf(hba[i]->devname, "cciss%d", i);
3372 hba[i]->ctlr = i;
3373 hba[i]->pdev = pdev;
3374
3375 /* configure PCI DMA stuff */
mike.miller@hp.comeb0df992005-06-10 14:51:04 -05003376 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK))
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003377 dac = 1;
mike.miller@hp.comeb0df992005-06-10 14:51:04 -05003378 else if (!pci_set_dma_mask(pdev, DMA_32BIT_MASK))
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003379 dac = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 else {
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003381 printk(KERN_ERR "cciss: no suitable DMA available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 goto clean1;
3383 }
3384
3385 /*
3386 * register with the major number, or get a dynamic major number
3387 * by passing 0 as argument. This is done for greater than
3388 * 8 controller support.
3389 */
3390 if (i < MAX_CTLR_ORIG)
Christoph Hellwig564de742006-01-08 01:05:17 -08003391 hba[i]->major = COMPAQ_CISS_MAJOR + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 rc = register_blkdev(hba[i]->major, hba[i]->devname);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003393 if (rc == -EBUSY || rc == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 printk(KERN_ERR
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003395 "cciss: Unable to get major number %d for %s "
3396 "on hba %d\n", hba[i]->major, hba[i]->devname, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 goto clean1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003398 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 if (i >= MAX_CTLR_ORIG)
3400 hba[i]->major = rc;
3401 }
3402
3403 /* make sure the board interrupts are off */
3404 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003405 if (request_irq(hba[i]->intr[SIMPLE_MODE_INT], do_cciss_intr,
Thomas Gleixner69ab3912006-07-01 19:29:32 -07003406 IRQF_DISABLED | IRQF_SHARED, hba[i]->devname, hba[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003408 hba[i]->intr[SIMPLE_MODE_INT], hba[i]->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 goto clean2;
3410 }
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003411
3412 printk(KERN_INFO "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003413 hba[i]->devname, pdev->device, pci_name(pdev),
3414 hba[i]->intr[SIMPLE_MODE_INT], dac ? "" : " not");
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003415
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003416 hba[i]->cmd_pool_bits =
Mike Millerf8806322006-12-06 20:35:01 -08003417 kmalloc(((hba[i]->nr_cmds + BITS_PER_LONG -
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003418 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
3419 hba[i]->cmd_pool = (CommandList_struct *)
3420 pci_alloc_consistent(hba[i]->pdev,
Mike Millerf8806322006-12-06 20:35:01 -08003421 hba[i]->nr_cmds * sizeof(CommandList_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003422 &(hba[i]->cmd_pool_dhandle));
3423 hba[i]->errinfo_pool = (ErrorInfo_struct *)
3424 pci_alloc_consistent(hba[i]->pdev,
Mike Millerf8806322006-12-06 20:35:01 -08003425 hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003426 &(hba[i]->errinfo_pool_dhandle));
3427 if ((hba[i]->cmd_pool_bits == NULL)
3428 || (hba[i]->cmd_pool == NULL)
3429 || (hba[i]->errinfo_pool == NULL)) {
3430 printk(KERN_ERR "cciss: out of memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 goto clean4;
3432 }
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003433#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003434 hba[i]->scsi_rejects.complete =
3435 kmalloc(sizeof(hba[i]->scsi_rejects.complete[0]) *
Mike Millerf8806322006-12-06 20:35:01 -08003436 (hba[i]->nr_cmds + 5), GFP_KERNEL);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003437 if (hba[i]->scsi_rejects.complete == NULL) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003438 printk(KERN_ERR "cciss: out of memory");
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003439 goto clean4;
3440 }
3441#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 spin_lock_init(&hba[i]->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003444 /* Initialize the pdev driver private data.
3445 have it point to hba[i]. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 pci_set_drvdata(pdev, hba[i]);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003447 /* command and error info recs zeroed out before
3448 they are used */
3449 memset(hba[i]->cmd_pool_bits, 0,
Mike Millerf8806322006-12-06 20:35:01 -08003450 ((hba[i]->nr_cmds + BITS_PER_LONG -
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003451 1) / BITS_PER_LONG) * sizeof(unsigned long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003453#ifdef CCISS_DEBUG
3454 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n", i);
3455#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 cciss_getgeometry(i);
3458
3459 cciss_scsi_setup(i);
3460
3461 /* Turn the interrupts on so we can service requests */
3462 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
3463
3464 cciss_procinit(i);
Mike Miller92c4231a2006-12-06 20:35:06 -08003465
3466 hba[i]->cciss_max_sectors = 2048;
3467
Mike Millerd6dbf422005-09-21 09:55:32 -07003468 hba[i]->busy_initializing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Mike Miller799202c2006-12-06 20:35:12 -08003470 do {
Mike Millerad2b9312005-07-28 01:07:31 -07003471 drive_info_struct *drv = &(hba[i]->drv[j]);
3472 struct gendisk *disk = hba[i]->gendisk[j];
Jens Axboe165125e2007-07-24 09:28:11 +02003473 struct request_queue *q;
Mike Miller799202c2006-12-06 20:35:12 -08003474
3475 /* Check if the disk was allocated already */
3476 if (!disk){
3477 hba[i]->gendisk[j] = alloc_disk(1 << NWD_SHIFT);
3478 disk = hba[i]->gendisk[j];
3479 }
3480
3481 /* Check that the disk was able to be allocated */
3482 if (!disk) {
3483 printk(KERN_ERR "cciss: unable to allocate memory for disk %d\n", j);
3484 goto clean4;
3485 }
Mike Millerad2b9312005-07-28 01:07:31 -07003486
3487 q = blk_init_queue(do_cciss_request, &hba[i]->lock);
3488 if (!q) {
3489 printk(KERN_ERR
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003490 "cciss: unable to allocate queue for disk %d\n",
3491 j);
Mike Miller799202c2006-12-06 20:35:12 -08003492 goto clean4;
Mike Millerad2b9312005-07-28 01:07:31 -07003493 }
3494 drv->queue = q;
3495
Jens Axboea9925a02006-01-09 16:04:06 +01003496 blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Jens Axboea9925a02006-01-09 16:04:06 +01003498 /* This is a hardware imposed limit. */
3499 blk_queue_max_hw_segments(q, MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Jens Axboea9925a02006-01-09 16:04:06 +01003501 /* This is a limit in the driver and could be eliminated. */
3502 blk_queue_max_phys_segments(q, MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Mike Miller92c4231a2006-12-06 20:35:06 -08003504 blk_queue_max_sectors(q, hba[i]->cciss_max_sectors);
Jens Axboea9925a02006-01-09 16:04:06 +01003505
3506 blk_queue_softirq_done(q, cciss_softirq_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Mike Millerad2b9312005-07-28 01:07:31 -07003508 q->queuedata = hba[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 disk->major = hba[i]->major;
3511 disk->first_minor = j << NWD_SHIFT;
3512 disk->fops = &cciss_fops;
Mike Millerad2b9312005-07-28 01:07:31 -07003513 disk->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 disk->private_data = drv;
Greg Kroah-Hartman27c0ff82006-04-27 15:46:39 -07003515 disk->driverfs_dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 /* we must register the controller even if no disks exist */
3517 /* this is for the online array utilities */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003518 if (!drv->heads && j)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 continue;
Mike Millerad2b9312005-07-28 01:07:31 -07003520 blk_queue_hardsect_size(q, drv->block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 set_capacity(disk, drv->nr_blocks);
3522 add_disk(disk);
Mike Miller799202c2006-12-06 20:35:12 -08003523 j++;
3524 } while (j <= hba[i]->highest_lun);
Mike Millerad2b9312005-07-28 01:07:31 -07003525
Bjorn Helgaase2019b52006-06-25 05:49:05 -07003526 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003528 clean4:
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003529#ifdef CONFIG_CISS_SCSI_TAPE
Andrew Morton1acc0b02006-01-04 18:30:03 -08003530 kfree(hba[i]->scsi_rejects.complete);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003531#endif
Jesper Juhl6044ec82005-11-07 01:01:32 -08003532 kfree(hba[i]->cmd_pool_bits);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003533 if (hba[i]->cmd_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 pci_free_consistent(hba[i]->pdev,
Mike Millerf8806322006-12-06 20:35:01 -08003535 hba[i]->nr_cmds * sizeof(CommandList_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003536 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
3537 if (hba[i]->errinfo_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 pci_free_consistent(hba[i]->pdev,
Mike Millerf8806322006-12-06 20:35:01 -08003539 hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003540 hba[i]->errinfo_pool,
3541 hba[i]->errinfo_pool_dhandle);
Mike Millerfb86a352006-01-08 01:03:50 -08003542 free_irq(hba[i]->intr[SIMPLE_MODE_INT], hba[i]);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003543 clean2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 unregister_blkdev(hba[i]->major, hba[i]->devname);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003545 clean1:
Mike Miller1f8ef382005-09-13 01:25:21 -07003546 hba[i]->busy_initializing = 0;
Mike Miller799202c2006-12-06 20:35:12 -08003547 /* cleanup any queues that may have been initialized */
3548 for (j=0; j <= hba[i]->highest_lun; j++){
3549 drive_info_struct *drv = &(hba[i]->drv[j]);
3550 if (drv->queue)
3551 blk_cleanup_queue(drv->queue);
3552 }
Mike Miller872225c2006-12-13 00:34:22 -08003553 /*
3554 * Deliberately omit pci_disable_device(): it does something nasty to
3555 * Smart Array controllers that pci_enable_device does not undo
3556 */
Mike Miller799202c2006-12-06 20:35:12 -08003557 pci_release_regions(pdev);
Mike Miller799202c2006-12-06 20:35:12 -08003558 pci_set_drvdata(pdev, NULL);
Patrick McHardy61808c22006-03-23 02:59:24 -08003559 free_hba(i);
Bjorn Helgaase2019b52006-06-25 05:49:05 -07003560 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
Gerald Brittone9ca75b2007-05-14 13:53:01 -04003563static void cciss_shutdown(struct pci_dev *pdev)
3564{
3565 ctlr_info_t *tmp_ptr;
3566 int i;
3567 char flush_buf[4];
3568 int return_code;
3569
3570 tmp_ptr = pci_get_drvdata(pdev);
3571 if (tmp_ptr == NULL)
3572 return;
3573 i = tmp_ptr->ctlr;
3574 if (hba[i] == NULL)
3575 return;
3576
3577 /* Turn board interrupts off and send the flush cache command */
3578 /* sendcmd will turn off interrupt, and send the flush...
3579 * To write all data in the battery backed cache to disks */
3580 memset(flush_buf, 0, 4);
3581 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
3582 TYPE_CMD);
3583 if (return_code == IO_OK) {
3584 printk(KERN_INFO "Completed flushing cache on controller %d\n", i);
3585 } else {
3586 printk(KERN_WARNING "Error flushing cache on controller %d\n", i);
3587 }
3588 free_irq(hba[i]->intr[2], hba[i]);
3589}
3590
3591static void __devexit cciss_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
3593 ctlr_info_t *tmp_ptr;
3594 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003596 if (pci_get_drvdata(pdev) == NULL) {
3597 printk(KERN_ERR "cciss: Unable to remove device \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 return;
3599 }
3600 tmp_ptr = pci_get_drvdata(pdev);
3601 i = tmp_ptr->ctlr;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003602 if (hba[i] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 printk(KERN_ERR "cciss: device appears to "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003604 "already be removed \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 return;
3606 }
Bjorn Helgaasb6550772007-04-11 23:28:43 -07003607
3608 remove_proc_entry(hba[i]->devname, proc_cciss);
3609 unregister_blkdev(hba[i]->major, hba[i]->devname);
3610
3611 /* remove it from the disk list */
3612 for (j = 0; j < CISS_MAX_LUN; j++) {
3613 struct gendisk *disk = hba[i]->gendisk[j];
3614 if (disk) {
Jens Axboe165125e2007-07-24 09:28:11 +02003615 struct request_queue *q = disk->queue;
Bjorn Helgaasb6550772007-04-11 23:28:43 -07003616
3617 if (disk->flags & GENHD_FL_UP)
3618 del_gendisk(disk);
3619 if (q)
3620 blk_cleanup_queue(q);
3621 }
3622 }
3623
3624 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
3625
Gerald Brittone9ca75b2007-05-14 13:53:01 -04003626 cciss_shutdown(pdev);
Mike Millerfb86a352006-01-08 01:03:50 -08003627
3628#ifdef CONFIG_PCI_MSI
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003629 if (hba[i]->msix_vector)
3630 pci_disable_msix(hba[i]->pdev);
3631 else if (hba[i]->msi_vector)
3632 pci_disable_msi(hba[i]->pdev);
3633#endif /* CONFIG_PCI_MSI */
Mike Millerfb86a352006-01-08 01:03:50 -08003634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 iounmap(hba[i]->vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Mike Millerf8806322006-12-06 20:35:01 -08003637 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(CommandList_struct),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
Mike Millerf8806322006-12-06 20:35:01 -08003639 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003640 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 kfree(hba[i]->cmd_pool_bits);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003642#ifdef CONFIG_CISS_SCSI_TAPE
3643 kfree(hba[i]->scsi_rejects.complete);
3644#endif
Mike Miller872225c2006-12-13 00:34:22 -08003645 /*
3646 * Deliberately omit pci_disable_device(): it does something nasty to
3647 * Smart Array controllers that pci_enable_device does not undo
3648 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003649 pci_release_regions(pdev);
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003650 pci_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 free_hba(i);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003652}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
3654static struct pci_driver cciss_pci_driver = {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003655 .name = "cciss",
3656 .probe = cciss_init_one,
3657 .remove = __devexit_p(cciss_remove_one),
3658 .id_table = cciss_pci_device_id, /* id_table */
Gerald Brittone9ca75b2007-05-14 13:53:01 -04003659 .shutdown = cciss_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660};
3661
3662/*
3663 * This is it. Register the PCI driver information for the cards we control
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003664 * the OS will call our registered routines when it finds one of our cards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 */
3666static int __init cciss_init(void)
3667{
3668 printk(KERN_INFO DRIVER_NAME "\n");
3669
3670 /* Register for our PCI devices */
Richard Knutsson9bfab8c2005-11-30 00:59:34 +01003671 return pci_register_driver(&cciss_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672}
3673
3674static void __exit cciss_cleanup(void)
3675{
3676 int i;
3677
3678 pci_unregister_driver(&cciss_pci_driver);
3679 /* double check that all controller entrys have been removed */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003680 for (i = 0; i < MAX_CTLR; i++) {
3681 if (hba[i] != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 printk(KERN_WARNING "cciss: had to remove"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003683 " controller %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 cciss_remove_one(hba[i]->pdev);
3685 }
3686 }
3687 remove_proc_entry("cciss", proc_root_driver);
3688}
3689
Mike Miller33079b22005-09-13 01:25:22 -07003690static void fail_all_cmds(unsigned long ctlr)
3691{
3692 /* If we get here, the board is apparently dead. */
3693 ctlr_info_t *h = hba[ctlr];
3694 CommandList_struct *c;
3695 unsigned long flags;
3696
3697 printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003698 h->alive = 0; /* the controller apparently died... */
Mike Miller33079b22005-09-13 01:25:22 -07003699
3700 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
3701
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003702 pci_disable_device(h->pdev); /* Make sure it is really dead. */
Mike Miller33079b22005-09-13 01:25:22 -07003703
3704 /* move everything off the request queue onto the completed queue */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003705 while ((c = h->reqQ) != NULL) {
Mike Miller33079b22005-09-13 01:25:22 -07003706 removeQ(&(h->reqQ), c);
3707 h->Qdepth--;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003708 addQ(&(h->cmpQ), c);
Mike Miller33079b22005-09-13 01:25:22 -07003709 }
3710
3711 /* Now, fail everything on the completed queue with a HW error */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003712 while ((c = h->cmpQ) != NULL) {
Mike Miller33079b22005-09-13 01:25:22 -07003713 removeQ(&h->cmpQ, c);
3714 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
3715 if (c->cmd_type == CMD_RWREQ) {
3716 complete_command(h, c, 0);
3717 } else if (c->cmd_type == CMD_IOCTL_PEND)
3718 complete(c->waiting);
3719#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003720 else if (c->cmd_type == CMD_SCSI)
3721 complete_scsi_command(c, 0, 0);
Mike Miller33079b22005-09-13 01:25:22 -07003722#endif
3723 }
3724 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
3725 return;
3726}
3727
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728module_init(cciss_init);
3729module_exit(cciss_cleanup);