blob: f1e7390683fdb6e9ff98e79d6a17c82ecf8f318c [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
135#define READ_AHEAD 1024
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define MAX_CTLR 32
137
138/* Originally cciss driver only supports 8 major numbers */
139#define MAX_CTLR_ORIG 8
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static ctlr_info_t *hba[MAX_CTLR];
142
Jens Axboe165125e2007-07-24 09:28:11 +0200143static void do_cciss_request(struct request_queue *q);
David Howells7d12e782006-10-05 14:55:46 +0100144static irqreturn_t do_cciss_intr(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static int cciss_open(struct inode *inode, struct file *filep);
146static int cciss_release(struct inode *inode, struct file *filep);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700147static int cciss_ioctl(struct inode *inode, struct file *filep,
148 unsigned int cmd, unsigned long arg);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800149static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int cciss_revalidate(struct gendisk *disk);
Mike Millerddd47442005-09-13 01:25:22 -0700152static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700153static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
154 int clear_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -0700156static void cciss_read_capacity(int ctlr, int logvol, int withirq,
157 sector_t *total_size, unsigned int *block_size);
158static void cciss_read_capacity_16(int ctlr, int logvol, int withirq,
159 sector_t *total_size, unsigned int *block_size);
160static void cciss_geometry_inquiry(int ctlr, int logvol,
161 int withirq, sector_t total_size,
162 unsigned int block_size, InquiryData_struct *inq_buff,
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700163 drive_info_struct *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static void cciss_getgeometry(int cntl_num);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700165static void __devinit cciss_interrupt_mode(ctlr_info_t *, struct pci_dev *,
166 __u32);
167static void start_io(ctlr_info_t *h);
168static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size,
169 unsigned int use_unit_num, unsigned int log_unit,
170 __u8 page_code, unsigned char *scsi3addr, int cmd_type);
171static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
172 unsigned int use_unit_num, unsigned int log_unit,
173 __u8 page_code, int cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Mike Miller33079b22005-09-13 01:25:22 -0700175static void fail_all_cmds(unsigned long ctlr);
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static void cciss_procinit(int i);
179#else
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700180static void cciss_procinit(int i)
181{
182}
183#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185#ifdef CONFIG_COMPAT
186static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg);
187#endif
188
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700189static struct block_device_operations cciss_fops = {
190 .owner = THIS_MODULE,
191 .open = cciss_open,
192 .release = cciss_release,
193 .ioctl = cciss_ioctl,
194 .getgeo = cciss_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#ifdef CONFIG_COMPAT
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700196 .compat_ioctl = cciss_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700198 .revalidate_disk = cciss_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
201/*
202 * Enqueuing and dequeuing functions for cmdlists.
203 */
204static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
205{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700206 if (*Qptr == NULL) {
207 *Qptr = c;
208 c->next = c->prev = c;
209 } else {
210 c->prev = (*Qptr)->prev;
211 c->next = (*Qptr);
212 (*Qptr)->prev->next = c;
213 (*Qptr)->prev = c;
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700217static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
218 CommandList_struct *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700220 if (c && c->next != c) {
221 if (*Qptr == c)
222 *Qptr = c->next;
223 c->prev->next = c->next;
224 c->next->prev = c->prev;
225 } else {
226 *Qptr = NULL;
227 }
228 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231#include "cciss_scsi.c" /* For SCSI tape support */
232
Randy Dunlap0f5486e2006-12-29 16:48:31 -0800233#define RAID_UNKNOWN 6
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#ifdef CONFIG_PROC_FS
236
237/*
238 * Report information about this controller.
239 */
240#define ENG_GIG 1000000000
241#define ENG_GIG_FACTOR (ENG_GIG/512)
Mike Miller89b6e742008-02-21 08:54:03 +0100242#define ENGAGE_SCSI "engage scsi"
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700243static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
244 "UNKNOWN"
245};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247static struct proc_dir_entry *proc_cciss;
248
Mike Miller89b6e742008-02-21 08:54:03 +0100249static void cciss_seq_show_header(struct seq_file *seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Mike Miller89b6e742008-02-21 08:54:03 +0100251 ctlr_info_t *h = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Mike Miller89b6e742008-02-21 08:54:03 +0100253 seq_printf(seq, "%s: HP %s Controller\n"
254 "Board ID: 0x%08lx\n"
255 "Firmware Version: %c%c%c%c\n"
256 "IRQ: %d\n"
257 "Logical drives: %d\n"
258 "Current Q depth: %d\n"
259 "Current # commands on controller: %d\n"
260 "Max Q depth since init: %d\n"
261 "Max # commands on controller since init: %d\n"
262 "Max SG entries since init: %d\n",
263 h->devname,
264 h->product_name,
265 (unsigned long)h->board_id,
266 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
267 h->firm_ver[3], (unsigned int)h->intr[SIMPLE_MODE_INT],
268 h->num_luns,
269 h->Qdepth, h->commands_outstanding,
270 h->maxQsinceinit, h->max_outstanding, h->maxSG);
271
272#ifdef CONFIG_CISS_SCSI_TAPE
273 cciss_seq_tape_report(seq, h->ctlr);
274#endif /* CONFIG_CISS_SCSI_TAPE */
275}
276
277static void *cciss_seq_start(struct seq_file *seq, loff_t *pos)
278{
279 ctlr_info_t *h = seq->private;
280 unsigned ctlr = h->ctlr;
281 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* prevent displaying bogus info during configuration
284 * or deconfiguration of a logical volume
285 */
286 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
287 if (h->busy_configuring) {
288 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
Mike Miller89b6e742008-02-21 08:54:03 +0100289 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291 h->busy_configuring = 1;
292 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
293
Mike Miller89b6e742008-02-21 08:54:03 +0100294 if (*pos == 0)
295 cciss_seq_show_header(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Mike Miller89b6e742008-02-21 08:54:03 +0100297 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Mike Miller89b6e742008-02-21 08:54:03 +0100300static int cciss_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Mike Miller89b6e742008-02-21 08:54:03 +0100302 sector_t vol_sz, vol_sz_frac;
303 ctlr_info_t *h = seq->private;
304 unsigned ctlr = h->ctlr;
305 loff_t *pos = v;
306 drive_info_struct *drv = &h->drv[*pos];
307
308 if (*pos > h->highest_lun)
309 return 0;
310
311 if (drv->heads == 0)
312 return 0;
313
314 vol_sz = drv->nr_blocks;
315 vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
316 vol_sz_frac *= 100;
317 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
318
319 if (drv->raid_level > 5)
320 drv->raid_level = RAID_UNKNOWN;
321 seq_printf(seq, "cciss/c%dd%d:"
322 "\t%4u.%02uGB\tRAID %s\n",
323 ctlr, (int) *pos, (int)vol_sz, (int)vol_sz_frac,
324 raid_label[drv->raid_level]);
325 return 0;
326}
327
328static void *cciss_seq_next(struct seq_file *seq, void *v, loff_t *pos)
329{
330 ctlr_info_t *h = seq->private;
331
332 if (*pos > h->highest_lun)
333 return NULL;
334 *pos += 1;
335
336 return pos;
337}
338
339static void cciss_seq_stop(struct seq_file *seq, void *v)
340{
341 ctlr_info_t *h = seq->private;
342
343 /* Only reset h->busy_configuring if we succeeded in setting
344 * it during cciss_seq_start. */
345 if (v == ERR_PTR(-EBUSY))
346 return;
347
348 h->busy_configuring = 0;
349}
350
351static struct seq_operations cciss_seq_ops = {
352 .start = cciss_seq_start,
353 .show = cciss_seq_show,
354 .next = cciss_seq_next,
355 .stop = cciss_seq_stop,
356};
357
358static int cciss_seq_open(struct inode *inode, struct file *file)
359{
360 int ret = seq_open(file, &cciss_seq_ops);
361 struct seq_file *seq = file->private_data;
362
363 if (!ret)
364 seq->private = PDE(inode)->data;
365
366 return ret;
367}
368
369static ssize_t
370cciss_proc_write(struct file *file, const char __user *buf,
371 size_t length, loff_t *ppos)
372{
373 int err;
374 char *buffer;
375
376#ifndef CONFIG_CISS_SCSI_TAPE
377 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378#endif
379
Mike Miller89b6e742008-02-21 08:54:03 +0100380 if (!buf || length > PAGE_SIZE - 1)
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700381 return -EINVAL;
Mike Miller89b6e742008-02-21 08:54:03 +0100382
383 buffer = (char *)__get_free_page(GFP_KERNEL);
384 if (!buffer)
385 return -ENOMEM;
386
387 err = -EFAULT;
388 if (copy_from_user(buffer, buf, length))
389 goto out;
390 buffer[length] = '\0';
391
392#ifdef CONFIG_CISS_SCSI_TAPE
393 if (strncmp(ENGAGE_SCSI, buffer, sizeof ENGAGE_SCSI - 1) == 0) {
394 struct seq_file *seq = file->private_data;
395 ctlr_info_t *h = seq->private;
396 int rc;
397
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700398 rc = cciss_engage_scsi(h->ctlr);
399 if (rc != 0)
Mike Miller89b6e742008-02-21 08:54:03 +0100400 err = -rc;
401 else
402 err = length;
403 } else
404#endif /* CONFIG_CISS_SCSI_TAPE */
405 err = -EINVAL;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700406 /* might be nice to have "disengage" too, but it's not
407 safely possible. (only 1 module use count, lock issues.) */
Mike Miller89b6e742008-02-21 08:54:03 +0100408
409out:
410 free_page((unsigned long)buffer);
411 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Mike Miller89b6e742008-02-21 08:54:03 +0100414static struct file_operations cciss_proc_fops = {
415 .owner = THIS_MODULE,
416 .open = cciss_seq_open,
417 .read = seq_read,
418 .llseek = seq_lseek,
419 .release = seq_release,
420 .write = cciss_proc_write,
421};
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static void __devinit cciss_procinit(int i)
424{
425 struct proc_dir_entry *pde;
426
Mike Miller89b6e742008-02-21 08:54:03 +0100427 if (proc_cciss == NULL)
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700428 proc_cciss = proc_mkdir("cciss", proc_root_driver);
Mike Miller89b6e742008-02-21 08:54:03 +0100429 if (!proc_cciss)
430 return;
431 pde = proc_create(hba[i]->devname, S_IWUSR | S_IRUSR | S_IRGRP |
432 S_IROTH, proc_cciss,
433 &cciss_proc_fops);
434 if (!pde)
435 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Mike Miller89b6e742008-02-21 08:54:03 +0100437 pde->data = hba[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700439#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700441/*
442 * For operations that cannot sleep, a command block is allocated at init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700444 * which ones are free or in use. For operations that can wait for kmalloc
445 * to possible sleep, this routine can be called with get_from_pool set to 0.
446 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
447 */
448static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 CommandList_struct *c;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700451 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 u64bit temp64;
453 dma_addr_t cmd_dma_handle, err_dma_handle;
454
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700455 if (!get_from_pool) {
456 c = (CommandList_struct *) pci_alloc_consistent(h->pdev,
457 sizeof(CommandList_struct), &cmd_dma_handle);
458 if (c == NULL)
459 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 memset(c, 0, sizeof(CommandList_struct));
461
Mike Miller33079b22005-09-13 01:25:22 -0700462 c->cmdindex = -1;
463
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700464 c->err_info = (ErrorInfo_struct *)
465 pci_alloc_consistent(h->pdev, sizeof(ErrorInfo_struct),
466 &err_dma_handle);
467
468 if (c->err_info == NULL) {
469 pci_free_consistent(h->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 sizeof(CommandList_struct), c, cmd_dma_handle);
471 return NULL;
472 }
473 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700474 } else { /* get it out of the controllers pool */
475
476 do {
Mike Millerf8806322006-12-06 20:35:01 -0800477 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
478 if (i == h->nr_cmds)
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700479 return NULL;
480 } while (test_and_set_bit
481 (i & (BITS_PER_LONG - 1),
482 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#ifdef CCISS_DEBUG
484 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
485#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700486 c = h->cmd_pool + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 memset(c, 0, sizeof(CommandList_struct));
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700488 cmd_dma_handle = h->cmd_pool_dhandle
489 + i * sizeof(CommandList_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 c->err_info = h->errinfo_pool + i;
491 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700492 err_dma_handle = h->errinfo_pool_dhandle
493 + i * sizeof(ErrorInfo_struct);
494 h->nr_allocs++;
Mike Miller33079b22005-09-13 01:25:22 -0700495
496 c->cmdindex = i;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 c->busaddr = (__u32) cmd_dma_handle;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700500 temp64.val = (__u64) err_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 c->ErrDesc.Addr.lower = temp64.val32.lower;
502 c->ErrDesc.Addr.upper = temp64.val32.upper;
503 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 c->ctlr = h->ctlr;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700506 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700509/*
510 * Frees a command block that was previously allocated with cmd_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
512static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
513{
514 int i;
515 u64bit temp64;
516
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700517 if (!got_from_pool) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 temp64.val32.lower = c->ErrDesc.Addr.lower;
519 temp64.val32.upper = c->ErrDesc.Addr.upper;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700520 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
521 c->err_info, (dma_addr_t) temp64.val);
522 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
523 c, (dma_addr_t) c->busaddr);
524 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 i = c - h->cmd_pool;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700526 clear_bit(i & (BITS_PER_LONG - 1),
527 h->cmd_pool_bits + (i / BITS_PER_LONG));
528 h->nr_frees++;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532static inline ctlr_info_t *get_host(struct gendisk *disk)
533{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700534 return disk->queue->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537static inline drive_info_struct *get_drv(struct gendisk *disk)
538{
539 return disk->private_data;
540}
541
542/*
543 * Open. Make sure the device is really there.
544 */
545static int cciss_open(struct inode *inode, struct file *filep)
546{
547 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
548 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
549
550#ifdef CCISS_DEBUG
551 printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700552#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Mike Millerddd47442005-09-13 01:25:22 -0700554 if (host->busy_initializing || drv->busy_configuring)
555 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /*
557 * Root is allowed to open raw volume zero even if it's not configured
558 * so array config can still work. Root is also allowed to open any
559 * volume that has a LUN ID, so it can issue IOCTL to reread the
560 * disk information. I don't think I really like this
561 * but I'm already using way to many device nodes to claim another one
562 * for "raw controller".
563 */
Mike Miller7a06f782006-12-06 20:35:08 -0800564 if (drv->heads == 0) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700565 if (iminor(inode) != 0) { /* not node 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* if not node 0 make sure it is a partition = 0 */
567 if (iminor(inode) & 0x0f) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700568 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* if it is, make sure we have a LUN ID */
570 } else if (drv->LunID == 0) {
571 return -ENXIO;
572 }
573 }
574 if (!capable(CAP_SYS_ADMIN))
575 return -EPERM;
576 }
577 drv->usage_count++;
578 host->usage_count++;
579 return 0;
580}
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/*
583 * Close. Sync first.
584 */
585static int cciss_release(struct inode *inode, struct file *filep)
586{
587 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
588 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
589
590#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700591 printk(KERN_DEBUG "cciss_release %s\n",
592 inode->i_bdev->bd_disk->disk_name);
593#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 drv->usage_count--;
596 host->usage_count--;
597 return 0;
598}
599
600#ifdef CONFIG_COMPAT
601
602static int do_ioctl(struct file *f, unsigned cmd, unsigned long arg)
603{
604 int ret;
605 lock_kernel();
Josef Sipek6c648be2006-12-08 02:36:55 -0800606 ret = cciss_ioctl(f->f_path.dentry->d_inode, f, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 unlock_kernel();
608 return ret;
609}
610
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700611static int cciss_ioctl32_passthru(struct file *f, unsigned cmd,
612 unsigned long arg);
613static int cciss_ioctl32_big_passthru(struct file *f, unsigned cmd,
614 unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg)
617{
618 switch (cmd) {
619 case CCISS_GETPCIINFO:
620 case CCISS_GETINTINFO:
621 case CCISS_SETINTINFO:
622 case CCISS_GETNODENAME:
623 case CCISS_SETNODENAME:
624 case CCISS_GETHEARTBEAT:
625 case CCISS_GETBUSTYPES:
626 case CCISS_GETFIRMVER:
627 case CCISS_GETDRIVVER:
628 case CCISS_REVALIDVOLS:
629 case CCISS_DEREGDISK:
630 case CCISS_REGNEWDISK:
631 case CCISS_REGNEWD:
632 case CCISS_RESCANDISK:
633 case CCISS_GETLUNINFO:
634 return do_ioctl(f, cmd, arg);
635
636 case CCISS_PASSTHRU32:
637 return cciss_ioctl32_passthru(f, cmd, arg);
638 case CCISS_BIG_PASSTHRU32:
639 return cciss_ioctl32_big_passthru(f, cmd, arg);
640
641 default:
642 return -ENOIOCTLCMD;
643 }
644}
645
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700646static int cciss_ioctl32_passthru(struct file *f, unsigned cmd,
647 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 IOCTL32_Command_struct __user *arg32 =
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700650 (IOCTL32_Command_struct __user *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 IOCTL_Command_struct arg64;
652 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
653 int err;
654 u32 cp;
655
656 err = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700657 err |=
658 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
659 sizeof(arg64.LUN_info));
660 err |=
661 copy_from_user(&arg64.Request, &arg32->Request,
662 sizeof(arg64.Request));
663 err |=
664 copy_from_user(&arg64.error_info, &arg32->error_info,
665 sizeof(arg64.error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 err |= get_user(arg64.buf_size, &arg32->buf_size);
667 err |= get_user(cp, &arg32->buf);
668 arg64.buf = compat_ptr(cp);
669 err |= copy_to_user(p, &arg64, sizeof(arg64));
670
671 if (err)
672 return -EFAULT;
673
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700674 err = do_ioctl(f, CCISS_PASSTHRU, (unsigned long)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (err)
676 return err;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700677 err |=
678 copy_in_user(&arg32->error_info, &p->error_info,
679 sizeof(arg32->error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (err)
681 return -EFAULT;
682 return err;
683}
684
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700685static int cciss_ioctl32_big_passthru(struct file *file, unsigned cmd,
686 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 BIG_IOCTL32_Command_struct __user *arg32 =
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700689 (BIG_IOCTL32_Command_struct __user *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 BIG_IOCTL_Command_struct arg64;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700691 BIG_IOCTL_Command_struct __user *p =
692 compat_alloc_user_space(sizeof(arg64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int err;
694 u32 cp;
695
696 err = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700697 err |=
698 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
699 sizeof(arg64.LUN_info));
700 err |=
701 copy_from_user(&arg64.Request, &arg32->Request,
702 sizeof(arg64.Request));
703 err |=
704 copy_from_user(&arg64.error_info, &arg32->error_info,
705 sizeof(arg64.error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 err |= get_user(arg64.buf_size, &arg32->buf_size);
707 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
708 err |= get_user(cp, &arg32->buf);
709 arg64.buf = compat_ptr(cp);
710 err |= copy_to_user(p, &arg64, sizeof(arg64));
711
712 if (err)
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700713 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700715 err = do_ioctl(file, CCISS_BIG_PASSTHRU, (unsigned long)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (err)
717 return err;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700718 err |=
719 copy_in_user(&arg32->error_info, &p->error_info,
720 sizeof(arg32->error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (err)
722 return -EFAULT;
723 return err;
724}
725#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800726
727static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo)
728{
729 drive_info_struct *drv = get_drv(bdev->bd_disk);
730
731 if (!drv->cylinders)
732 return -ENXIO;
733
734 geo->heads = drv->heads;
735 geo->sectors = drv->sectors;
736 geo->cylinders = drv->cylinders;
737 return 0;
738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/*
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700741 * ioctl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700743static int cciss_ioctl(struct inode *inode, struct file *filep,
744 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct block_device *bdev = inode->i_bdev;
747 struct gendisk *disk = bdev->bd_disk;
748 ctlr_info_t *host = get_host(disk);
749 drive_info_struct *drv = get_drv(disk);
750 int ctlr = host->ctlr;
751 void __user *argp = (void __user *)arg;
752
753#ifdef CCISS_DEBUG
754 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700755#endif /* CCISS_DEBUG */
756
757 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 case CCISS_GETPCIINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700760 cciss_pci_info_struct pciinfo;
761
762 if (!arg)
763 return -EINVAL;
764 pciinfo.domain = pci_domain_nr(host->pdev->bus);
765 pciinfo.bus = host->pdev->bus->number;
766 pciinfo.dev_fn = host->pdev->devfn;
767 pciinfo.board_id = host->board_id;
768 if (copy_to_user
769 (argp, &pciinfo, sizeof(cciss_pci_info_struct)))
770 return -EFAULT;
771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700773 case CCISS_GETINTINFO:
774 {
775 cciss_coalint_struct intinfo;
776 if (!arg)
777 return -EINVAL;
778 intinfo.delay =
779 readl(&host->cfgtable->HostWrite.CoalIntDelay);
780 intinfo.count =
781 readl(&host->cfgtable->HostWrite.CoalIntCount);
782 if (copy_to_user
783 (argp, &intinfo, sizeof(cciss_coalint_struct)))
784 return -EFAULT;
785 return 0;
786 }
787 case CCISS_SETINTINFO:
788 {
789 cciss_coalint_struct intinfo;
790 unsigned long flags;
791 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700793 if (!arg)
794 return -EINVAL;
795 if (!capable(CAP_SYS_ADMIN))
796 return -EPERM;
797 if (copy_from_user
798 (&intinfo, argp, sizeof(cciss_coalint_struct)))
799 return -EFAULT;
800 if ((intinfo.delay == 0) && (intinfo.count == 0))
801 {
802// printk("cciss_ioctl: delay and count cannot be 0\n");
803 return -EINVAL;
804 }
805 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
806 /* Update the field, and then ring the doorbell */
807 writel(intinfo.delay,
808 &(host->cfgtable->HostWrite.CoalIntDelay));
809 writel(intinfo.count,
810 &(host->cfgtable->HostWrite.CoalIntCount));
811 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
812
813 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
814 if (!(readl(host->vaddr + SA5_DOORBELL)
815 & CFGTBL_ChangeReq))
816 break;
817 /* delay and try again */
818 udelay(1000);
819 }
820 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
821 if (i >= MAX_IOCTL_CONFIG_WAIT)
822 return -EAGAIN;
823 return 0;
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 case CCISS_GETNODENAME:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700826 {
827 NodeName_type NodeName;
828 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700830 if (!arg)
831 return -EINVAL;
832 for (i = 0; i < 16; i++)
833 NodeName[i] =
834 readb(&host->cfgtable->ServerName[i]);
835 if (copy_to_user(argp, NodeName, sizeof(NodeName_type)))
836 return -EFAULT;
837 return 0;
838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 case CCISS_SETNODENAME:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700840 {
841 NodeName_type NodeName;
842 unsigned long flags;
843 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700845 if (!arg)
846 return -EINVAL;
847 if (!capable(CAP_SYS_ADMIN))
848 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700850 if (copy_from_user
851 (NodeName, argp, sizeof(NodeName_type)))
852 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700854 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700856 /* Update the field, and then ring the doorbell */
857 for (i = 0; i < 16; i++)
858 writeb(NodeName[i],
859 &host->cfgtable->ServerName[i]);
860
861 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
862
863 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
864 if (!(readl(host->vaddr + SA5_DOORBELL)
865 & CFGTBL_ChangeReq))
866 break;
867 /* delay and try again */
868 udelay(1000);
869 }
870 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
871 if (i >= MAX_IOCTL_CONFIG_WAIT)
872 return -EAGAIN;
873 return 0;
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 case CCISS_GETHEARTBEAT:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700877 {
878 Heartbeat_type heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700880 if (!arg)
881 return -EINVAL;
882 heartbeat = readl(&host->cfgtable->HeartBeat);
883 if (copy_to_user
884 (argp, &heartbeat, sizeof(Heartbeat_type)))
885 return -EFAULT;
886 return 0;
887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 case CCISS_GETBUSTYPES:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700889 {
890 BusTypes_type BusTypes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700892 if (!arg)
893 return -EINVAL;
894 BusTypes = readl(&host->cfgtable->BusTypes);
895 if (copy_to_user
896 (argp, &BusTypes, sizeof(BusTypes_type)))
897 return -EFAULT;
898 return 0;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 case CCISS_GETFIRMVER:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700901 {
902 FirmwareVer_type firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700904 if (!arg)
905 return -EINVAL;
906 memcpy(firmware, host->firm_ver, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700908 if (copy_to_user
909 (argp, firmware, sizeof(FirmwareVer_type)))
910 return -EFAULT;
911 return 0;
912 }
913 case CCISS_GETDRIVVER:
914 {
915 DriverVer_type DriverVer = DRIVER_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700917 if (!arg)
918 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700920 if (copy_to_user
921 (argp, &DriverVer, sizeof(DriverVer_type)))
922 return -EFAULT;
923 return 0;
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 case CCISS_REVALIDVOLS:
Mike Miller3833a742006-12-06 20:35:10 -0800927 return rebuild_lun_table(host, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700929 case CCISS_GETLUNINFO:{
930 LogvolInfo_struct luninfo;
931
932 luninfo.LunID = drv->LunID;
933 luninfo.num_opens = drv->usage_count;
934 luninfo.num_parts = 0;
935 if (copy_to_user(argp, &luninfo,
936 sizeof(LogvolInfo_struct)))
937 return -EFAULT;
938 return 0;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 case CCISS_DEREGDISK:
Mike Millerddd47442005-09-13 01:25:22 -0700941 return rebuild_lun_table(host, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 case CCISS_REGNEWD:
Mike Millerddd47442005-09-13 01:25:22 -0700944 return rebuild_lun_table(host, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 case CCISS_PASSTHRU:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700948 IOCTL_Command_struct iocommand;
949 CommandList_struct *c;
950 char *buff = NULL;
951 u64bit temp64;
952 unsigned long flags;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700953 DECLARE_COMPLETION_ONSTACK(wait);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700954
955 if (!arg)
956 return -EINVAL;
957
958 if (!capable(CAP_SYS_RAWIO))
959 return -EPERM;
960
961 if (copy_from_user
962 (&iocommand, argp, sizeof(IOCTL_Command_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return -EFAULT;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700964 if ((iocommand.buf_size < 1) &&
965 (iocommand.Request.Type.Direction != XFER_NONE)) {
966 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700968#if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
969 /* Check kmalloc limits */
970 if (iocommand.buf_size > 128000)
971 return -EINVAL;
972#endif
973 if (iocommand.buf_size > 0) {
974 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
975 if (buff == NULL)
976 return -EFAULT;
977 }
978 if (iocommand.Request.Type.Direction == XFER_WRITE) {
979 /* Copy the data into the buffer we created */
980 if (copy_from_user
981 (buff, iocommand.buf, iocommand.buf_size)) {
982 kfree(buff);
983 return -EFAULT;
984 }
985 } else {
986 memset(buff, 0, iocommand.buf_size);
987 }
988 if ((c = cmd_alloc(host, 0)) == NULL) {
989 kfree(buff);
990 return -ENOMEM;
991 }
992 // Fill in the command type
993 c->cmd_type = CMD_IOCTL_PEND;
994 // Fill in Command Header
995 c->Header.ReplyQueue = 0; // unused in simple mode
996 if (iocommand.buf_size > 0) // buffer to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700998 c->Header.SGList = 1;
999 c->Header.SGTotal = 1;
1000 } else // no buffers to fill
1001 {
1002 c->Header.SGList = 0;
1003 c->Header.SGTotal = 0;
1004 }
1005 c->Header.LUN = iocommand.LUN_info;
1006 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
1007
1008 // Fill in Request block
1009 c->Request = iocommand.Request;
1010
1011 // Fill in the scatter gather information
1012 if (iocommand.buf_size > 0) {
1013 temp64.val = pci_map_single(host->pdev, buff,
1014 iocommand.buf_size,
1015 PCI_DMA_BIDIRECTIONAL);
1016 c->SG[0].Addr.lower = temp64.val32.lower;
1017 c->SG[0].Addr.upper = temp64.val32.upper;
1018 c->SG[0].Len = iocommand.buf_size;
1019 c->SG[0].Ext = 0; // we are not chaining
1020 }
1021 c->waiting = &wait;
1022
1023 /* Put the request on the tail of the request queue */
1024 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1025 addQ(&host->reqQ, c);
1026 host->Qdepth++;
1027 start_io(host);
1028 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1029
1030 wait_for_completion(&wait);
1031
1032 /* unlock the buffers from DMA */
1033 temp64.val32.lower = c->SG[0].Addr.lower;
1034 temp64.val32.upper = c->SG[0].Addr.upper;
1035 pci_unmap_single(host->pdev, (dma_addr_t) temp64.val,
1036 iocommand.buf_size,
1037 PCI_DMA_BIDIRECTIONAL);
1038
1039 /* Copy the error information out */
1040 iocommand.error_info = *(c->err_info);
1041 if (copy_to_user
1042 (argp, &iocommand, sizeof(IOCTL_Command_struct))) {
1043 kfree(buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 cmd_free(host, c, 0);
1045 return -EFAULT;
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001048 if (iocommand.Request.Type.Direction == XFER_READ) {
1049 /* Copy the data out of the buffer we created */
1050 if (copy_to_user
1051 (iocommand.buf, buff, iocommand.buf_size)) {
1052 kfree(buff);
1053 cmd_free(host, c, 0);
1054 return -EFAULT;
1055 }
1056 }
1057 kfree(buff);
1058 cmd_free(host, c, 0);
1059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001061 case CCISS_BIG_PASSTHRU:{
1062 BIG_IOCTL_Command_struct *ioc;
1063 CommandList_struct *c;
1064 unsigned char **buff = NULL;
1065 int *buff_size = NULL;
1066 u64bit temp64;
1067 unsigned long flags;
1068 BYTE sg_used = 0;
1069 int status = 0;
1070 int i;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001071 DECLARE_COMPLETION_ONSTACK(wait);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001072 __u32 left;
1073 __u32 sz;
1074 BYTE __user *data_ptr;
1075
1076 if (!arg)
1077 return -EINVAL;
1078 if (!capable(CAP_SYS_RAWIO))
1079 return -EPERM;
1080 ioc = (BIG_IOCTL_Command_struct *)
1081 kmalloc(sizeof(*ioc), GFP_KERNEL);
1082 if (!ioc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 status = -ENOMEM;
1084 goto cleanup1;
1085 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001086 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
1087 status = -EFAULT;
1088 goto cleanup1;
1089 }
1090 if ((ioc->buf_size < 1) &&
1091 (ioc->Request.Type.Direction != XFER_NONE)) {
1092 status = -EINVAL;
1093 goto cleanup1;
1094 }
1095 /* Check kmalloc limits using all SGs */
1096 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
1097 status = -EINVAL;
1098 goto cleanup1;
1099 }
1100 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
1101 status = -EINVAL;
1102 goto cleanup1;
1103 }
1104 buff =
1105 kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
1106 if (!buff) {
1107 status = -ENOMEM;
1108 goto cleanup1;
1109 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001110 buff_size = kmalloc(MAXSGENTRIES * sizeof(int),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001111 GFP_KERNEL);
1112 if (!buff_size) {
1113 status = -ENOMEM;
1114 goto cleanup1;
1115 }
1116 left = ioc->buf_size;
1117 data_ptr = ioc->buf;
1118 while (left) {
1119 sz = (left >
1120 ioc->malloc_size) ? ioc->
1121 malloc_size : left;
1122 buff_size[sg_used] = sz;
1123 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
1124 if (buff[sg_used] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 status = -ENOMEM;
Jens Axboe15534d32005-11-18 22:02:44 +01001126 goto cleanup1;
1127 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001128 if (ioc->Request.Type.Direction == XFER_WRITE) {
1129 if (copy_from_user
1130 (buff[sg_used], data_ptr, sz)) {
1131 status = -ENOMEM;
1132 goto cleanup1;
1133 }
1134 } else {
1135 memset(buff[sg_used], 0, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001137 left -= sz;
1138 data_ptr += sz;
1139 sg_used++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001141 if ((c = cmd_alloc(host, 0)) == NULL) {
1142 status = -ENOMEM;
1143 goto cleanup1;
1144 }
1145 c->cmd_type = CMD_IOCTL_PEND;
1146 c->Header.ReplyQueue = 0;
1147
1148 if (ioc->buf_size > 0) {
1149 c->Header.SGList = sg_used;
1150 c->Header.SGTotal = sg_used;
1151 } else {
1152 c->Header.SGList = 0;
1153 c->Header.SGTotal = 0;
1154 }
1155 c->Header.LUN = ioc->LUN_info;
1156 c->Header.Tag.lower = c->busaddr;
1157
1158 c->Request = ioc->Request;
1159 if (ioc->buf_size > 0) {
1160 int i;
1161 for (i = 0; i < sg_used; i++) {
1162 temp64.val =
1163 pci_map_single(host->pdev, buff[i],
1164 buff_size[i],
1165 PCI_DMA_BIDIRECTIONAL);
1166 c->SG[i].Addr.lower =
1167 temp64.val32.lower;
1168 c->SG[i].Addr.upper =
1169 temp64.val32.upper;
1170 c->SG[i].Len = buff_size[i];
1171 c->SG[i].Ext = 0; /* we are not chaining */
1172 }
1173 }
1174 c->waiting = &wait;
1175 /* Put the request on the tail of the request queue */
1176 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1177 addQ(&host->reqQ, c);
1178 host->Qdepth++;
1179 start_io(host);
1180 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1181 wait_for_completion(&wait);
1182 /* unlock the buffers from DMA */
1183 for (i = 0; i < sg_used; i++) {
1184 temp64.val32.lower = c->SG[i].Addr.lower;
1185 temp64.val32.upper = c->SG[i].Addr.upper;
1186 pci_unmap_single(host->pdev,
1187 (dma_addr_t) temp64.val, buff_size[i],
1188 PCI_DMA_BIDIRECTIONAL);
1189 }
1190 /* Copy the error information out */
1191 ioc->error_info = *(c->err_info);
1192 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
1193 cmd_free(host, c, 0);
1194 status = -EFAULT;
1195 goto cleanup1;
1196 }
1197 if (ioc->Request.Type.Direction == XFER_READ) {
1198 /* Copy the data out of the buffer we created */
1199 BYTE __user *ptr = ioc->buf;
1200 for (i = 0; i < sg_used; i++) {
1201 if (copy_to_user
1202 (ptr, buff[i], buff_size[i])) {
1203 cmd_free(host, c, 0);
1204 status = -EFAULT;
1205 goto cleanup1;
1206 }
1207 ptr += buff_size[i];
1208 }
1209 }
1210 cmd_free(host, c, 0);
1211 status = 0;
1212 cleanup1:
1213 if (buff) {
1214 for (i = 0; i < sg_used; i++)
1215 kfree(buff[i]);
1216 kfree(buff);
1217 }
1218 kfree(buff_size);
1219 kfree(ioc);
1220 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07001222
1223 /* scsi_cmd_ioctl handles these, below, though some are not */
1224 /* very meaningful for cciss. SG_IO is the main one people want. */
1225
1226 case SG_GET_VERSION_NUM:
1227 case SG_SET_TIMEOUT:
1228 case SG_GET_TIMEOUT:
1229 case SG_GET_RESERVED_SIZE:
1230 case SG_SET_RESERVED_SIZE:
1231 case SG_EMULATED_HOST:
1232 case SG_IO:
1233 case SCSI_IOCTL_SEND_COMMAND:
S.Çağlar Onur9793c322007-07-16 21:56:51 +03001234 return scsi_cmd_ioctl(filep, disk->queue, disk, cmd, argp);
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07001235
1236 /* scsi_cmd_ioctl would normally handle these, below, but */
1237 /* they aren't a good fit for cciss, as CD-ROMs are */
1238 /* not supported, and we don't have any bus/target/lun */
1239 /* which we present to the kernel. */
1240
1241 case CDROM_SEND_PACKET:
1242 case CDROMCLOSETRAY:
1243 case CDROMEJECT:
1244 case SCSI_IOCTL_GET_IDLUN:
1245 case SCSI_IOCTL_GET_BUS_NUMBER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 default:
1247 return -ENOTTY;
1248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Jens Axboe7b30f092006-07-25 15:02:48 +02001251static void cciss_check_queues(ctlr_info_t *h)
1252{
1253 int start_queue = h->next_to_run;
1254 int i;
1255
1256 /* check to see if we have maxed out the number of commands that can
1257 * be placed on the queue. If so then exit. We do this check here
1258 * in case the interrupt we serviced was from an ioctl and did not
1259 * free any new commands.
1260 */
Mike Millerf8806322006-12-06 20:35:01 -08001261 if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds)
Jens Axboe7b30f092006-07-25 15:02:48 +02001262 return;
1263
1264 /* We have room on the queue for more commands. Now we need to queue
1265 * them up. We will also keep track of the next queue to run so
1266 * that every queue gets a chance to be started first.
1267 */
1268 for (i = 0; i < h->highest_lun + 1; i++) {
1269 int curr_queue = (start_queue + i) % (h->highest_lun + 1);
1270 /* make sure the disk has been added and the drive is real
1271 * because this can be called from the middle of init_one.
1272 */
1273 if (!(h->drv[curr_queue].queue) || !(h->drv[curr_queue].heads))
1274 continue;
1275 blk_start_queue(h->gendisk[curr_queue]->queue);
1276
1277 /* check to see if we have maxed out the number of commands
1278 * that can be placed on the queue.
1279 */
Mike Millerf8806322006-12-06 20:35:01 -08001280 if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds) {
Jens Axboe7b30f092006-07-25 15:02:48 +02001281 if (curr_queue == start_queue) {
1282 h->next_to_run =
1283 (start_queue + 1) % (h->highest_lun + 1);
1284 break;
1285 } else {
1286 h->next_to_run = curr_queue;
1287 break;
1288 }
1289 } else {
1290 curr_queue = (curr_queue + 1) % (h->highest_lun + 1);
1291 }
1292 }
1293}
1294
Mike Millerca1e0482006-04-10 15:38:07 -07001295static void cciss_softirq_done(struct request *rq)
1296{
1297 CommandList_struct *cmd = rq->completion_data;
1298 ctlr_info_t *h = hba[cmd->ctlr];
1299 unsigned long flags;
1300 u64bit temp64;
1301 int i, ddir;
1302
1303 if (cmd->Request.Type.Direction == XFER_READ)
1304 ddir = PCI_DMA_FROMDEVICE;
1305 else
1306 ddir = PCI_DMA_TODEVICE;
1307
1308 /* command did not need to be retried */
1309 /* unmap the DMA mapping for all the scatter gather elements */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001310 for (i = 0; i < cmd->Header.SGList; i++) {
Mike Millerca1e0482006-04-10 15:38:07 -07001311 temp64.val32.lower = cmd->SG[i].Addr.lower;
1312 temp64.val32.upper = cmd->SG[i].Addr.upper;
1313 pci_unmap_page(h->pdev, temp64.val, cmd->SG[i].Len, ddir);
1314 }
1315
Mike Millerca1e0482006-04-10 15:38:07 -07001316#ifdef CCISS_DEBUG
1317 printk("Done with %p\n", rq);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001318#endif /* CCISS_DEBUG */
Mike Millerca1e0482006-04-10 15:38:07 -07001319
Kiyoshi Ueda3daeea22007-12-11 17:50:03 -05001320 if (blk_end_request(rq, (rq->errors == 0) ? 0 : -EIO, blk_rq_bytes(rq)))
1321 BUG();
1322
Mike Millerca1e0482006-04-10 15:38:07 -07001323 spin_lock_irqsave(&h->lock, flags);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001324 cmd_free(h, cmd, 1);
Jens Axboe7b30f092006-07-25 15:02:48 +02001325 cciss_check_queues(h);
Mike Millerca1e0482006-04-10 15:38:07 -07001326 spin_unlock_irqrestore(&h->lock, flags);
1327}
1328
Mike Millerddd47442005-09-13 01:25:22 -07001329/* This function will check the usage_count of the drive to be updated/added.
1330 * If the usage_count is zero then the drive information will be updated and
1331 * the disk will be re-registered with the kernel. If not then it will be
1332 * left alone for the next reboot. The exception to this is disk 0 which
1333 * will always be left registered with the kernel since it is also the
1334 * controller node. Any changes to disk 0 will show up on the next
1335 * reboot.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001336 */
Mike Millerddd47442005-09-13 01:25:22 -07001337static void cciss_update_drive_info(int ctlr, int drv_index)
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001338{
Mike Millerddd47442005-09-13 01:25:22 -07001339 ctlr_info_t *h = hba[ctlr];
1340 struct gendisk *disk;
Mike Millerddd47442005-09-13 01:25:22 -07001341 InquiryData_struct *inq_buff = NULL;
1342 unsigned int block_size;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001343 sector_t total_size;
Mike Millerddd47442005-09-13 01:25:22 -07001344 unsigned long flags = 0;
1345 int ret = 0;
1346
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001347 /* if the disk already exists then deregister it before proceeding */
1348 if (h->drv[drv_index].raid_level != -1) {
Mike Millerddd47442005-09-13 01:25:22 -07001349 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
1350 h->drv[drv_index].busy_configuring = 1;
1351 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1352 ret = deregister_disk(h->gendisk[drv_index],
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001353 &h->drv[drv_index], 0);
Mike Millerddd47442005-09-13 01:25:22 -07001354 h->drv[drv_index].busy_configuring = 0;
1355 }
1356
1357 /* If the disk is in use return */
1358 if (ret)
1359 return;
1360
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001361 /* Get information about the disk and modify the driver structure */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001362 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
Mike Millerddd47442005-09-13 01:25:22 -07001363 if (inq_buff == NULL)
1364 goto mem_msg;
1365
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001366 /* testing to see if 16-byte CDBs are already being used */
1367 if (h->cciss_read == CCISS_READ_16) {
1368 cciss_read_capacity_16(h->ctlr, drv_index, 1,
1369 &total_size, &block_size);
1370 goto geo_inq;
1371 }
1372
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001373 cciss_read_capacity(ctlr, drv_index, 1,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001374 &total_size, &block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001375
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001376 /* if read_capacity returns all F's this volume is >2TB in size */
1377 /* so we switch to 16-byte CDB's for all read/write ops */
1378 if (total_size == 0xFFFFFFFFULL) {
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001379 cciss_read_capacity_16(ctlr, drv_index, 1,
1380 &total_size, &block_size);
1381 h->cciss_read = CCISS_READ_16;
1382 h->cciss_write = CCISS_WRITE_16;
1383 } else {
1384 h->cciss_read = CCISS_READ_10;
1385 h->cciss_write = CCISS_WRITE_10;
1386 }
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001387geo_inq:
Mike Millerddd47442005-09-13 01:25:22 -07001388 cciss_geometry_inquiry(ctlr, drv_index, 1, total_size, block_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001389 inq_buff, &h->drv[drv_index]);
Mike Millerddd47442005-09-13 01:25:22 -07001390
1391 ++h->num_luns;
1392 disk = h->gendisk[drv_index];
1393 set_capacity(disk, h->drv[drv_index].nr_blocks);
1394
Mike Millerddd47442005-09-13 01:25:22 -07001395 /* if it's the controller it's already added */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001396 if (drv_index) {
Mike Millerddd47442005-09-13 01:25:22 -07001397 disk->queue = blk_init_queue(do_cciss_request, &h->lock);
Mike Miller799202c2006-12-06 20:35:12 -08001398 sprintf(disk->disk_name, "cciss/c%dd%d", ctlr, drv_index);
1399 disk->major = h->major;
1400 disk->first_minor = drv_index << NWD_SHIFT;
1401 disk->fops = &cciss_fops;
1402 disk->private_data = &h->drv[drv_index];
Mike Millerddd47442005-09-13 01:25:22 -07001403
1404 /* Set up queue information */
1405 disk->queue->backing_dev_info.ra_pages = READ_AHEAD;
1406 blk_queue_bounce_limit(disk->queue, hba[ctlr]->pdev->dma_mask);
1407
1408 /* This is a hardware imposed limit. */
1409 blk_queue_max_hw_segments(disk->queue, MAXSGENTRIES);
1410
1411 /* This is a limit in the driver and could be eliminated. */
1412 blk_queue_max_phys_segments(disk->queue, MAXSGENTRIES);
1413
Mike Miller92c4231a2006-12-06 20:35:06 -08001414 blk_queue_max_sectors(disk->queue, h->cciss_max_sectors);
Mike Millerddd47442005-09-13 01:25:22 -07001415
Mike Millerca1e0482006-04-10 15:38:07 -07001416 blk_queue_softirq_done(disk->queue, cciss_softirq_done);
1417
Mike Millerddd47442005-09-13 01:25:22 -07001418 disk->queue->queuedata = hba[ctlr];
1419
1420 blk_queue_hardsect_size(disk->queue,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001421 hba[ctlr]->drv[drv_index].block_size);
Mike Millerddd47442005-09-13 01:25:22 -07001422
1423 h->drv[drv_index].queue = disk->queue;
1424 add_disk(disk);
1425 }
1426
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001427 freeret:
Mike Millerddd47442005-09-13 01:25:22 -07001428 kfree(inq_buff);
1429 return;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001430 mem_msg:
Mike Millerddd47442005-09-13 01:25:22 -07001431 printk(KERN_ERR "cciss: out of memory\n");
1432 goto freeret;
1433}
1434
1435/* This function will find the first index of the controllers drive array
1436 * that has a -1 for the raid_level and will return that index. This is
1437 * where new drives will be added. If the index to be returned is greater
1438 * than the highest_lun index for the controller then highest_lun is set
1439 * to this new index. If there are no available indexes then -1 is returned.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001440 */
Mike Millerddd47442005-09-13 01:25:22 -07001441static int cciss_find_free_drive_index(int ctlr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Mike Millerddd47442005-09-13 01:25:22 -07001443 int i;
1444
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001445 for (i = 0; i < CISS_MAX_LUN; i++) {
1446 if (hba[ctlr]->drv[i].raid_level == -1) {
Mike Millerddd47442005-09-13 01:25:22 -07001447 if (i > hba[ctlr]->highest_lun)
1448 hba[ctlr]->highest_lun = i;
1449 return i;
1450 }
1451 }
1452 return -1;
1453}
1454
1455/* This function will add and remove logical drives from the Logical
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001456 * drive array of the controller and maintain persistency of ordering
Mike Millerddd47442005-09-13 01:25:22 -07001457 * so that mount points are preserved until the next reboot. This allows
1458 * for the removal of logical drives in the middle of the drive array
1459 * without a re-ordering of those drives.
1460 * INPUT
1461 * h = The controller to perform the operations on
1462 * del_disk = The disk to remove if specified. If the value given
1463 * is NULL then no disk is removed.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001464 */
Mike Millerddd47442005-09-13 01:25:22 -07001465static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk)
1466{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int ctlr = h->ctlr;
Mike Millerddd47442005-09-13 01:25:22 -07001468 int num_luns;
1469 ReportLunData_struct *ld_buff = NULL;
1470 drive_info_struct *drv = NULL;
1471 int return_code;
1472 int listlength = 0;
1473 int i;
1474 int drv_found;
1475 int drv_index = 0;
1476 __u32 lunid = 0;
1477 unsigned long flags;
1478
1479 /* Set busy_configuring flag for this operation */
1480 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001481 if (h->busy_configuring) {
Mike Millerddd47442005-09-13 01:25:22 -07001482 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1483 return -EBUSY;
1484 }
1485 h->busy_configuring = 1;
1486
1487 /* if del_disk is NULL then we are being called to add a new disk
1488 * and update the logical drive table. If it is not NULL then
1489 * we will check if the disk is in use or not.
1490 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001491 if (del_disk != NULL) {
Mike Millerddd47442005-09-13 01:25:22 -07001492 drv = get_drv(del_disk);
1493 drv->busy_configuring = 1;
1494 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1495 return_code = deregister_disk(del_disk, drv, 1);
1496 drv->busy_configuring = 0;
1497 h->busy_configuring = 0;
1498 return return_code;
1499 } else {
1500 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1501 if (!capable(CAP_SYS_RAWIO))
1502 return -EPERM;
1503
1504 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1505 if (ld_buff == NULL)
1506 goto mem_msg;
1507
1508 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001509 sizeof(ReportLunData_struct), 0,
1510 0, 0, TYPE_CMD);
Mike Millerddd47442005-09-13 01:25:22 -07001511
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001512 if (return_code == IO_OK) {
Mike Miller799202c2006-12-06 20:35:12 -08001513 listlength =
Al Viro4c1f2b32007-03-14 09:19:10 +00001514 be32_to_cpu(*(__be32 *) ld_buff->LUNListLength);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001515 } else { /* reading number of logical volumes failed */
Mike Millerddd47442005-09-13 01:25:22 -07001516 printk(KERN_WARNING "cciss: report logical volume"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001517 " command failed\n");
Mike Millerddd47442005-09-13 01:25:22 -07001518 listlength = 0;
1519 goto freeret;
1520 }
1521
1522 num_luns = listlength / 8; /* 8 bytes per entry */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001523 if (num_luns > CISS_MAX_LUN) {
Mike Millerddd47442005-09-13 01:25:22 -07001524 num_luns = CISS_MAX_LUN;
1525 printk(KERN_WARNING "cciss: more luns configured"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001526 " on controller than can be handled by"
1527 " this driver.\n");
Mike Millerddd47442005-09-13 01:25:22 -07001528 }
1529
1530 /* Compare controller drive array to drivers drive array.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001531 * Check for updates in the drive information and any new drives
1532 * on the controller.
1533 */
1534 for (i = 0; i < num_luns; i++) {
Mike Millerddd47442005-09-13 01:25:22 -07001535 int j;
1536
1537 drv_found = 0;
1538
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001539 lunid = (0xff &
1540 (unsigned int)(ld_buff->LUN[i][3])) << 24;
1541 lunid |= (0xff &
1542 (unsigned int)(ld_buff->LUN[i][2])) << 16;
1543 lunid |= (0xff &
1544 (unsigned int)(ld_buff->LUN[i][1])) << 8;
1545 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
Mike Millerddd47442005-09-13 01:25:22 -07001546
1547 /* Find if the LUN is already in the drive array
1548 * of the controller. If so then update its info
1549 * if not is use. If it does not exist then find
1550 * the first free index and add it.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001551 */
1552 for (j = 0; j <= h->highest_lun; j++) {
1553 if (h->drv[j].LunID == lunid) {
Mike Millerddd47442005-09-13 01:25:22 -07001554 drv_index = j;
1555 drv_found = 1;
1556 }
1557 }
1558
1559 /* check if the drive was found already in the array */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001560 if (!drv_found) {
Mike Millerddd47442005-09-13 01:25:22 -07001561 drv_index = cciss_find_free_drive_index(ctlr);
1562 if (drv_index == -1)
1563 goto freeret;
1564
Mike Miller799202c2006-12-06 20:35:12 -08001565 /*Check if the gendisk needs to be allocated */
1566 if (!h->gendisk[drv_index]){
1567 h->gendisk[drv_index] = alloc_disk(1 << NWD_SHIFT);
1568 if (!h->gendisk[drv_index]){
1569 printk(KERN_ERR "cciss: could not allocate new disk %d\n", drv_index);
1570 goto mem_msg;
1571 }
1572 }
Mike Millerddd47442005-09-13 01:25:22 -07001573 }
1574 h->drv[drv_index].LunID = lunid;
1575 cciss_update_drive_info(ctlr, drv_index);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001576 } /* end for */
1577 } /* end else */
Mike Millerddd47442005-09-13 01:25:22 -07001578
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001579 freeret:
Mike Millerddd47442005-09-13 01:25:22 -07001580 kfree(ld_buff);
1581 h->busy_configuring = 0;
1582 /* We return -1 here to tell the ACU that we have registered/updated
1583 * all of the drives that we can and to keep it from calling us
1584 * additional times.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001585 */
Mike Millerddd47442005-09-13 01:25:22 -07001586 return -1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001587 mem_msg:
Mike Millerddd47442005-09-13 01:25:22 -07001588 printk(KERN_ERR "cciss: out of memory\n");
1589 goto freeret;
1590}
1591
1592/* This function will deregister the disk and it's queue from the
1593 * kernel. It must be called with the controller lock held and the
1594 * drv structures busy_configuring flag set. It's parameters are:
1595 *
1596 * disk = This is the disk to be deregistered
1597 * drv = This is the drive_info_struct associated with the disk to be
1598 * deregistered. It contains information about the disk used
1599 * by the driver.
1600 * clear_all = This flag determines whether or not the disk information
1601 * is going to be completely cleared out and the highest_lun
1602 * reset. Sometimes we want to clear out information about
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001603 * the disk in preparation for re-adding it. In this case
Mike Millerddd47442005-09-13 01:25:22 -07001604 * the highest_lun should be left unchanged and the LunID
1605 * should not be cleared.
1606*/
1607static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
1608 int clear_all)
1609{
Mike Miller799202c2006-12-06 20:35:12 -08001610 int i;
Mike Millerddd47442005-09-13 01:25:22 -07001611 ctlr_info_t *h = get_host(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 if (!capable(CAP_SYS_RAWIO))
1614 return -EPERM;
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /* make sure logical volume is NOT is use */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001617 if (clear_all || (h->gendisk[0] == disk)) {
1618 if (drv->usage_count > 1)
1619 return -EBUSY;
1620 } else if (drv->usage_count > 0)
1621 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Mike Millerddd47442005-09-13 01:25:22 -07001623 /* invalidate the devices and deregister the disk. If it is disk
1624 * zero do not deregister it but just zero out it's values. This
1625 * allows us to delete disk zero but keep the controller registered.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001626 */
1627 if (h->gendisk[0] != disk) {
Adrian Bunk5a9df732007-10-16 23:29:26 -07001628 struct request_queue *q = disk->queue;
1629 if (disk->flags & GENHD_FL_UP)
1630 del_gendisk(disk);
1631 if (q) {
1632 blk_cleanup_queue(q);
1633 /* Set drv->queue to NULL so that we do not try
1634 * to call blk_start_queue on this queue in the
1635 * interrupt handler
1636 */
1637 drv->queue = NULL;
1638 }
1639 /* If clear_all is set then we are deleting the logical
1640 * drive, not just refreshing its info. For drives
1641 * other than disk 0 we will call put_disk. We do not
1642 * do this for disk 0 as we need it to be able to
1643 * configure the controller.
1644 */
1645 if (clear_all){
1646 /* This isn't pretty, but we need to find the
1647 * disk in our array and NULL our the pointer.
1648 * This is so that we will call alloc_disk if
1649 * this index is used again later.
Mike Miller799202c2006-12-06 20:35:12 -08001650 */
Adrian Bunk5a9df732007-10-16 23:29:26 -07001651 for (i=0; i < CISS_MAX_LUN; i++){
1652 if(h->gendisk[i] == disk){
1653 h->gendisk[i] = NULL;
1654 break;
Mike Miller799202c2006-12-06 20:35:12 -08001655 }
Mike Miller799202c2006-12-06 20:35:12 -08001656 }
Adrian Bunk5a9df732007-10-16 23:29:26 -07001657 put_disk(disk);
Mike Millerddd47442005-09-13 01:25:22 -07001658 }
Mike Miller799202c2006-12-06 20:35:12 -08001659 } else {
1660 set_capacity(disk, 0);
Mike Millerddd47442005-09-13 01:25:22 -07001661 }
1662
1663 --h->num_luns;
1664 /* zero out the disk size info */
1665 drv->nr_blocks = 0;
1666 drv->block_size = 0;
1667 drv->heads = 0;
1668 drv->sectors = 0;
1669 drv->cylinders = 0;
1670 drv->raid_level = -1; /* This can be used as a flag variable to
1671 * indicate that this element of the drive
1672 * array is free.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001673 */
Mike Millerddd47442005-09-13 01:25:22 -07001674
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001675 if (clear_all) {
1676 /* check to see if it was the last disk */
1677 if (drv == h->drv + h->highest_lun) {
1678 /* if so, find the new hightest lun */
1679 int i, newhighest = -1;
1680 for (i = 0; i < h->highest_lun; i++) {
1681 /* if the disk has size > 0, it is available */
Mike Millerddd47442005-09-13 01:25:22 -07001682 if (h->drv[i].heads)
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001683 newhighest = i;
1684 }
1685 h->highest_lun = newhighest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
Mike Millerddd47442005-09-13 01:25:22 -07001687
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001688 drv->LunID = 0;
Mike Millerddd47442005-09-13 01:25:22 -07001689 }
Bjorn Helgaase2019b52006-06-25 05:49:05 -07001690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
Mike Millerddd47442005-09-13 01:25:22 -07001692
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001693static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, /* 0: address the controller,
1694 1: address logical volume log_unit,
1695 2: periph device address is scsi3addr */
1696 unsigned int log_unit, __u8 page_code,
1697 unsigned char *scsi3addr, int cmd_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001699 ctlr_info_t *h = hba[ctlr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 u64bit buff_dma_handle;
1701 int status = IO_OK;
1702
1703 c->cmd_type = CMD_IOCTL_PEND;
1704 c->Header.ReplyQueue = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001705 if (buff != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 c->Header.SGList = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001707 c->Header.SGTotal = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 } else {
1709 c->Header.SGList = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001710 c->Header.SGTotal = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712 c->Header.Tag.lower = c->busaddr;
1713
1714 c->Request.Type.Type = cmd_type;
1715 if (cmd_type == TYPE_CMD) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001716 switch (cmd) {
1717 case CISS_INQUIRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 /* If the logical unit number is 0 then, this is going
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001719 to controller so It's a physical command
1720 mode = 0 target = 0. So we have nothing to write.
1721 otherwise, if use_unit_num == 1,
1722 mode = 1(volume set addressing) target = LUNID
1723 otherwise, if use_unit_num == 2,
1724 mode = 0(periph dev addr) target = scsi3addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (use_unit_num == 1) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001726 c->Header.LUN.LogDev.VolId =
1727 h->drv[log_unit].LunID;
1728 c->Header.LUN.LogDev.Mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 } else if (use_unit_num == 2) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001730 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr,
1731 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 c->Header.LUN.LogDev.Mode = 0;
1733 }
1734 /* are we trying to read a vital product page */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001735 if (page_code != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 c->Request.CDB[1] = 0x01;
1737 c->Request.CDB[2] = page_code;
1738 }
1739 c->Request.CDBLen = 6;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001740 c->Request.Type.Attribute = ATTR_SIMPLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 c->Request.Type.Direction = XFER_READ;
1742 c->Request.Timeout = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001743 c->Request.CDB[0] = CISS_INQUIRY;
1744 c->Request.CDB[4] = size & 0xFF;
1745 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 case CISS_REPORT_LOG:
1747 case CISS_REPORT_PHYS:
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001748 /* Talking to controller so It's a physical command
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 mode = 00 target = 0. Nothing to write.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 c->Request.CDBLen = 12;
1752 c->Request.Type.Attribute = ATTR_SIMPLE;
1753 c->Request.Type.Direction = XFER_READ;
1754 c->Request.Timeout = 0;
1755 c->Request.CDB[0] = cmd;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001756 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 c->Request.CDB[7] = (size >> 16) & 0xFF;
1758 c->Request.CDB[8] = (size >> 8) & 0xFF;
1759 c->Request.CDB[9] = size & 0xFF;
1760 break;
1761
1762 case CCISS_READ_CAPACITY:
1763 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1764 c->Header.LUN.LogDev.Mode = 1;
1765 c->Request.CDBLen = 10;
1766 c->Request.Type.Attribute = ATTR_SIMPLE;
1767 c->Request.Type.Direction = XFER_READ;
1768 c->Request.Timeout = 0;
1769 c->Request.CDB[0] = cmd;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001770 break;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001771 case CCISS_READ_CAPACITY_16:
1772 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1773 c->Header.LUN.LogDev.Mode = 1;
1774 c->Request.CDBLen = 16;
1775 c->Request.Type.Attribute = ATTR_SIMPLE;
1776 c->Request.Type.Direction = XFER_READ;
1777 c->Request.Timeout = 0;
1778 c->Request.CDB[0] = cmd;
1779 c->Request.CDB[1] = 0x10;
1780 c->Request.CDB[10] = (size >> 24) & 0xFF;
1781 c->Request.CDB[11] = (size >> 16) & 0xFF;
1782 c->Request.CDB[12] = (size >> 8) & 0xFF;
1783 c->Request.CDB[13] = size & 0xFF;
1784 c->Request.Timeout = 0;
1785 c->Request.CDB[0] = cmd;
1786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 case CCISS_CACHE_FLUSH:
1788 c->Request.CDBLen = 12;
1789 c->Request.Type.Attribute = ATTR_SIMPLE;
1790 c->Request.Type.Direction = XFER_WRITE;
1791 c->Request.Timeout = 0;
1792 c->Request.CDB[0] = BMIC_WRITE;
1793 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001794 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 default:
1796 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001797 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
Bjorn Helgaase2019b52006-06-25 05:49:05 -07001798 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800 } else if (cmd_type == TYPE_MSG) {
1801 switch (cmd) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001802 case 0: /* ABORT message */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001803 c->Request.CDBLen = 12;
1804 c->Request.Type.Attribute = ATTR_SIMPLE;
1805 c->Request.Type.Direction = XFER_WRITE;
1806 c->Request.Timeout = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001807 c->Request.CDB[0] = cmd; /* abort */
1808 c->Request.CDB[1] = 0; /* abort a command */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001809 /* buff contains the tag of the command to abort */
1810 memcpy(&c->Request.CDB[4], buff, 8);
1811 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001812 case 1: /* RESET message */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001813 c->Request.CDBLen = 12;
1814 c->Request.Type.Attribute = ATTR_SIMPLE;
1815 c->Request.Type.Direction = XFER_WRITE;
1816 c->Request.Timeout = 0;
1817 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001818 c->Request.CDB[0] = cmd; /* reset */
1819 c->Request.CDB[1] = 0x04; /* reset a LUN */
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 case 3: /* No-Op message */
1822 c->Request.CDBLen = 1;
1823 c->Request.Type.Attribute = ATTR_SIMPLE;
1824 c->Request.Type.Direction = XFER_WRITE;
1825 c->Request.Timeout = 0;
1826 c->Request.CDB[0] = cmd;
1827 break;
1828 default:
1829 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001830 "cciss%d: unknown message type %d\n", ctlr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return IO_ERROR;
1832 }
1833 } else {
1834 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001835 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return IO_ERROR;
1837 }
1838 /* Fill in the scatter gather information */
1839 if (size > 0) {
1840 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001841 buff, size,
1842 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1844 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1845 c->SG[0].Len = size;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001846 c->SG[0].Ext = 0; /* we are not chaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848 return status;
1849}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001850
1851static int sendcmd_withirq(__u8 cmd,
1852 int ctlr,
1853 void *buff,
1854 size_t size,
1855 unsigned int use_unit_num,
1856 unsigned int log_unit, __u8 page_code, int cmd_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
1858 ctlr_info_t *h = hba[ctlr];
1859 CommandList_struct *c;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001860 u64bit buff_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 unsigned long flags;
1862 int return_status;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001863 DECLARE_COMPLETION_ONSTACK(wait);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001864
1865 if ((c = cmd_alloc(h, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return -ENOMEM;
1867 return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001868 log_unit, page_code, NULL, cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (return_status != IO_OK) {
1870 cmd_free(h, c, 0);
1871 return return_status;
1872 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001873 resend_cmd2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 c->waiting = &wait;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /* Put the request on the tail of the queue and send it */
1877 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1878 addQ(&h->reqQ, c);
1879 h->Qdepth++;
1880 start_io(h);
1881 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 wait_for_completion(&wait);
1884
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001885 if (c->err_info->CommandStatus != 0) { /* an error has occurred */
1886 switch (c->err_info->CommandStatus) {
1887 case CMD_TARGET_STATUS:
1888 printk(KERN_WARNING "cciss: cmd %p has "
1889 " completed with errors\n", c);
1890 if (c->err_info->ScsiStatus) {
1891 printk(KERN_WARNING "cciss: cmd %p "
1892 "has SCSI Status = %x\n",
1893 c, c->err_info->ScsiStatus);
1894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001897 case CMD_DATA_UNDERRUN:
1898 case CMD_DATA_OVERRUN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /* expected for inquire and report lun commands */
1900 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001901 case CMD_INVALID:
1902 printk(KERN_WARNING "cciss: Cmd %p is "
1903 "reported invalid\n", c);
1904 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001906 case CMD_PROTOCOL_ERR:
1907 printk(KERN_WARNING "cciss: cmd %p has "
1908 "protocol error \n", c);
1909 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001911 case CMD_HARDWARE_ERR:
1912 printk(KERN_WARNING "cciss: cmd %p had "
1913 " hardware error\n", c);
1914 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001916 case CMD_CONNECTION_LOST:
1917 printk(KERN_WARNING "cciss: cmd %p had "
1918 "connection lost\n", c);
1919 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001921 case CMD_ABORTED:
1922 printk(KERN_WARNING "cciss: cmd %p was "
1923 "aborted\n", c);
1924 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001926 case CMD_ABORT_FAILED:
1927 printk(KERN_WARNING "cciss: cmd %p reports "
1928 "abort failed\n", c);
1929 return_status = IO_ERROR;
1930 break;
1931 case CMD_UNSOLICITED_ABORT:
1932 printk(KERN_WARNING
1933 "cciss%d: unsolicited abort %p\n", ctlr, c);
1934 if (c->retry_count < MAX_CMD_RETRIES) {
1935 printk(KERN_WARNING
1936 "cciss%d: retrying %p\n", ctlr, c);
1937 c->retry_count++;
1938 /* erase the old error information */
1939 memset(c->err_info, 0,
1940 sizeof(ErrorInfo_struct));
1941 return_status = IO_OK;
1942 INIT_COMPLETION(wait);
1943 goto resend_cmd2;
1944 }
1945 return_status = IO_ERROR;
1946 break;
1947 default:
1948 printk(KERN_WARNING "cciss: cmd %p returned "
1949 "unknown status %x\n", c,
1950 c->err_info->CommandStatus);
1951 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /* unlock the buffers from DMA */
Mike Millerbb2a37b2005-09-13 01:25:24 -07001955 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
1956 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001957 pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
1958 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 cmd_free(h, c, 0);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001960 return return_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963static void cciss_geometry_inquiry(int ctlr, int logvol,
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001964 int withirq, sector_t total_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001965 unsigned int block_size,
1966 InquiryData_struct *inq_buff,
1967 drive_info_struct *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 int return_code;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001970 unsigned long t;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 memset(inq_buff, 0, sizeof(InquiryData_struct));
1973 if (withirq)
1974 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001975 inq_buff, sizeof(*inq_buff), 1,
1976 logvol, 0xC1, TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 else
1978 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001979 sizeof(*inq_buff), 1, logvol, 0xC1, NULL,
1980 TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (return_code == IO_OK) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001982 if (inq_buff->data_byte[8] == 0xFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001984 "cciss: reading geometry failed, volume "
1985 "does not support reading geometry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 drv->heads = 255;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001987 drv->sectors = 32; // Sectors per track
Mike Miller (OS Dev)7f42d3b2007-04-04 19:08:23 -07001988 drv->cylinders = total_size + 1;
Mike Miller89f97ad2006-12-18 10:59:39 +01001989 drv->raid_level = RAID_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 drv->heads = inq_buff->data_byte[6];
1992 drv->sectors = inq_buff->data_byte[7];
1993 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
1994 drv->cylinders += inq_buff->data_byte[5];
1995 drv->raid_level = inq_buff->data_byte[8];
Matthew Wilcox3f7705e2006-10-21 10:24:19 -07001996 }
1997 drv->block_size = block_size;
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08001998 drv->nr_blocks = total_size + 1;
Matthew Wilcox3f7705e2006-10-21 10:24:19 -07001999 t = drv->heads * drv->sectors;
2000 if (t > 1) {
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08002001 sector_t real_size = total_size + 1;
2002 unsigned long rem = sector_div(real_size, t);
Matthew Wilcox3f7705e2006-10-21 10:24:19 -07002003 if (rem)
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08002004 real_size++;
2005 drv->cylinders = real_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002007 } else { /* Get geometry failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 printk(KERN_WARNING "cciss: reading geometry failed\n");
2009 }
Metathronius Galabantcc088d12006-09-30 23:27:47 -07002010 printk(KERN_INFO " heads=%d, sectors=%d, cylinders=%d\n\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002011 drv->heads, drv->sectors, drv->cylinders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014static void
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002015cciss_read_capacity(int ctlr, int logvol, int withirq, sector_t *total_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002016 unsigned int *block_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002018 ReadCapdata_struct *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 int return_code;
Mariusz Kozlowski1aebe182007-08-11 22:34:29 +02002020
2021 buf = kzalloc(sizeof(ReadCapdata_struct), GFP_KERNEL);
2022 if (!buf) {
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002023 printk(KERN_WARNING "cciss: out of memory\n");
2024 return;
2025 }
Mariusz Kozlowski1aebe182007-08-11 22:34:29 +02002026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (withirq)
2028 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002029 ctlr, buf, sizeof(ReadCapdata_struct),
2030 1, logvol, 0, TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 else
2032 return_code = sendcmd(CCISS_READ_CAPACITY,
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002033 ctlr, buf, sizeof(ReadCapdata_struct),
2034 1, logvol, 0, NULL, TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (return_code == IO_OK) {
Al Viro4c1f2b32007-03-14 09:19:10 +00002036 *total_size = be32_to_cpu(*(__be32 *) buf->total_size);
2037 *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002038 } else { /* read capacity command failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 printk(KERN_WARNING "cciss: read capacity failed\n");
2040 *total_size = 0;
2041 *block_size = BLOCK_SIZE;
2042 }
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08002043 if (*total_size != 0)
Randy Dunlap7b92aad2006-10-28 10:38:40 -07002044 printk(KERN_INFO " blocks= %llu block_size= %d\n",
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08002045 (unsigned long long)*total_size+1, *block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002046 kfree(buf);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002047}
2048
2049static void
2050cciss_read_capacity_16(int ctlr, int logvol, int withirq, sector_t *total_size, unsigned int *block_size)
2051{
2052 ReadCapdata_struct_16 *buf;
2053 int return_code;
Mariusz Kozlowski1aebe182007-08-11 22:34:29 +02002054
2055 buf = kzalloc(sizeof(ReadCapdata_struct_16), GFP_KERNEL);
2056 if (!buf) {
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002057 printk(KERN_WARNING "cciss: out of memory\n");
2058 return;
2059 }
Mariusz Kozlowski1aebe182007-08-11 22:34:29 +02002060
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002061 if (withirq) {
2062 return_code = sendcmd_withirq(CCISS_READ_CAPACITY_16,
2063 ctlr, buf, sizeof(ReadCapdata_struct_16),
2064 1, logvol, 0, TYPE_CMD);
2065 }
2066 else {
2067 return_code = sendcmd(CCISS_READ_CAPACITY_16,
2068 ctlr, buf, sizeof(ReadCapdata_struct_16),
2069 1, logvol, 0, NULL, TYPE_CMD);
2070 }
2071 if (return_code == IO_OK) {
Al Viro4c1f2b32007-03-14 09:19:10 +00002072 *total_size = be64_to_cpu(*(__be64 *) buf->total_size);
2073 *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002074 } else { /* read capacity command failed */
2075 printk(KERN_WARNING "cciss: read capacity failed\n");
2076 *total_size = 0;
2077 *block_size = BLOCK_SIZE;
2078 }
Randy Dunlap7b92aad2006-10-28 10:38:40 -07002079 printk(KERN_INFO " blocks= %llu block_size= %d\n",
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08002080 (unsigned long long)*total_size+1, *block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002081 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084static int cciss_revalidate(struct gendisk *disk)
2085{
2086 ctlr_info_t *h = get_host(disk);
2087 drive_info_struct *drv = get_drv(disk);
2088 int logvol;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002089 int FOUND = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 unsigned int block_size;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002091 sector_t total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 InquiryData_struct *inq_buff = NULL;
2093
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002094 for (logvol = 0; logvol < CISS_MAX_LUN; logvol++) {
2095 if (h->drv[logvol].LunID == drv->LunID) {
2096 FOUND = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 break;
2098 }
2099 }
2100
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002101 if (!FOUND)
2102 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002104 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
2105 if (inq_buff == NULL) {
2106 printk(KERN_WARNING "cciss: out of memory\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002107 return 1;
2108 }
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002109 if (h->cciss_read == CCISS_READ_10) {
2110 cciss_read_capacity(h->ctlr, logvol, 1,
2111 &total_size, &block_size);
2112 } else {
2113 cciss_read_capacity_16(h->ctlr, logvol, 1,
2114 &total_size, &block_size);
2115 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002116 cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size,
2117 inq_buff, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Mike Millerad2b9312005-07-28 01:07:31 -07002119 blk_queue_hardsect_size(drv->queue, drv->block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 set_capacity(disk, drv->nr_blocks);
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 kfree(inq_buff);
2123 return 0;
2124}
2125
2126/*
2127 * Wait polling for a command to complete.
2128 * The memory mapped FIFO is polled for the completion.
2129 * Used only at init time, interrupts from the HBA are disabled.
2130 */
2131static unsigned long pollcomplete(int ctlr)
2132{
2133 unsigned long done;
2134 int i;
2135
2136 /* Wait (up to 20 seconds) for a command to complete */
2137
2138 for (i = 20 * HZ; i > 0; i--) {
2139 done = hba[ctlr]->access.command_completed(hba[ctlr]);
Nishanth Aravamudan86e84862005-09-10 00:27:28 -07002140 if (done == FIFO_EMPTY)
2141 schedule_timeout_uninterruptible(1);
2142 else
Bjorn Helgaase2019b52006-06-25 05:49:05 -07002143 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145 /* Invalid address to tell caller we ran out of time */
2146 return 1;
2147}
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002148
2149static int add_sendcmd_reject(__u8 cmd, int ctlr, unsigned long complete)
2150{
2151 /* We get in here if sendcmd() is polling for completions
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002152 and gets some command back that it wasn't expecting --
2153 something other than that which it just sent down.
2154 Ordinarily, that shouldn't happen, but it can happen when
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002155 the scsi tape stuff gets into error handling mode, and
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002156 starts using sendcmd() to try to abort commands and
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002157 reset tape drives. In that case, sendcmd may pick up
2158 completions of commands that were sent to logical drives
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002159 through the block i/o system, or cciss ioctls completing, etc.
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002160 In that case, we need to save those completions for later
2161 processing by the interrupt handler.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002162 */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002163
2164#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002165 struct sendcmd_reject_list *srl = &hba[ctlr]->scsi_rejects;
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002166
2167 /* If it's not the scsi tape stuff doing error handling, (abort */
2168 /* or reset) then we don't expect anything weird. */
2169 if (cmd != CCISS_RESET_MSG && cmd != CCISS_ABORT_MSG) {
2170#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002171 printk(KERN_WARNING "cciss cciss%d: SendCmd "
2172 "Invalid command list address returned! (%lx)\n",
2173 ctlr, complete);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002174 /* not much we can do. */
2175#ifdef CONFIG_CISS_SCSI_TAPE
2176 return 1;
2177 }
2178
2179 /* We've sent down an abort or reset, but something else
2180 has completed */
Mike Millerf8806322006-12-06 20:35:01 -08002181 if (srl->ncompletions >= (hba[ctlr]->nr_cmds + 2)) {
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002182 /* Uh oh. No room to save it for later... */
2183 printk(KERN_WARNING "cciss%d: Sendcmd: Invalid command addr, "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002184 "reject list overflow, command lost!\n", ctlr);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002185 return 1;
2186 }
2187 /* Save it for later */
2188 srl->complete[srl->ncompletions] = complete;
2189 srl->ncompletions++;
2190#endif
2191 return 0;
2192}
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194/*
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002195 * Send a command to the controller, and wait for it to complete.
2196 * Only used at init time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002198static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, /* 0: address the controller,
2199 1: address logical volume log_unit,
2200 2: periph device address is scsi3addr */
2201 unsigned int log_unit,
2202 __u8 page_code, unsigned char *scsi3addr, int cmd_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 CommandList_struct *c;
2205 int i;
2206 unsigned long complete;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002207 ctlr_info_t *info_p = hba[ctlr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 u64bit buff_dma_handle;
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002209 int status, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 if ((c = cmd_alloc(info_p, 1)) == NULL) {
2212 printk(KERN_WARNING "cciss: unable to get memory");
Bjorn Helgaase2019b52006-06-25 05:49:05 -07002213 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215 status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002216 log_unit, page_code, scsi3addr, cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (status != IO_OK) {
2218 cmd_free(info_p, c, 1);
2219 return status;
2220 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002221 resend_cmd1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 /*
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002223 * Disable interrupt
2224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225#ifdef CCISS_DEBUG
2226 printk(KERN_DEBUG "cciss: turning intr off\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002227#endif /* CCISS_DEBUG */
2228 info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 /* Make sure there is room in the command FIFO */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002231 /* Actually it should be completely empty at this time */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002232 /* unless we are in here doing error handling for the scsi */
2233 /* tape side of the driver. */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002234 for (i = 200000; i > 0; i--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 /* if fifo isn't full go */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002236 if (!(info_p->access.fifo_full(info_p))) {
2237
2238 break;
2239 }
2240 udelay(10);
2241 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
2242 " waiting!\n", ctlr);
2243 }
2244 /*
2245 * Send the cmd
2246 */
2247 info_p->access.submit_command(info_p, c);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002248 done = 0;
2249 do {
2250 complete = pollcomplete(ctlr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252#ifdef CCISS_DEBUG
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002253 printk(KERN_DEBUG "cciss: command completed\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002254#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002256 if (complete == 1) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002257 printk(KERN_WARNING
2258 "cciss cciss%d: SendCmd Timeout out, "
2259 "No command list address returned!\n", ctlr);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002260 status = IO_ERROR;
2261 done = 1;
2262 break;
2263 }
2264
2265 /* This will need to change for direct lookup completions */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002266 if ((complete & CISS_ERROR_BIT)
2267 && (complete & ~CISS_ERROR_BIT) == c->busaddr) {
2268 /* if data overrun or underun on Report command
2269 ignore it
2270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
2272 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
2273 (c->Request.CDB[0] == CISS_INQUIRY)) &&
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002274 ((c->err_info->CommandStatus ==
2275 CMD_DATA_OVERRUN) ||
2276 (c->err_info->CommandStatus == CMD_DATA_UNDERRUN)
2277 )) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 complete = c->busaddr;
2279 } else {
2280 if (c->err_info->CommandStatus ==
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002281 CMD_UNSOLICITED_ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 printk(KERN_WARNING "cciss%d: "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002283 "unsolicited abort %p\n",
2284 ctlr, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (c->retry_count < MAX_CMD_RETRIES) {
2286 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002287 "cciss%d: retrying %p\n",
2288 ctlr, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 c->retry_count++;
2290 /* erase the old error */
2291 /* information */
2292 memset(c->err_info, 0,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002293 sizeof
2294 (ErrorInfo_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 goto resend_cmd1;
2296 } else {
2297 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002298 "cciss%d: retried %p too "
2299 "many times\n", ctlr, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 status = IO_ERROR;
2301 goto cleanup1;
2302 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002303 } else if (c->err_info->CommandStatus ==
2304 CMD_UNABORTABLE) {
2305 printk(KERN_WARNING
2306 "cciss%d: command could not be aborted.\n",
2307 ctlr);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002308 status = IO_ERROR;
2309 goto cleanup1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
2311 printk(KERN_WARNING "ciss ciss%d: sendcmd"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002312 " Error %x \n", ctlr,
2313 c->err_info->CommandStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 printk(KERN_WARNING "ciss ciss%d: sendcmd"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002315 " offensive info\n"
2316 " size %x\n num %x value %x\n",
2317 ctlr,
2318 c->err_info->MoreErrInfo.Invalid_Cmd.
2319 offense_size,
2320 c->err_info->MoreErrInfo.Invalid_Cmd.
2321 offense_num,
2322 c->err_info->MoreErrInfo.Invalid_Cmd.
2323 offense_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 status = IO_ERROR;
2325 goto cleanup1;
2326 }
2327 }
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002328 /* This will need changing for direct lookup completions */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002329 if (complete != c->busaddr) {
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002330 if (add_sendcmd_reject(cmd, ctlr, complete) != 0) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002331 BUG(); /* we are pretty much hosed if we get here. */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002332 }
2333 continue;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002334 } else
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002335 done = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002336 } while (!done);
2337
2338 cleanup1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /* unlock the data buffer from DMA */
Mike Millerbb2a37b2005-09-13 01:25:24 -07002340 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
2341 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002343 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002344#ifdef CONFIG_CISS_SCSI_TAPE
2345 /* if we saved some commands for later, process them now. */
2346 if (info_p->scsi_rejects.ncompletions > 0)
David Howells7d12e782006-10-05 14:55:46 +01002347 do_cciss_intr(0, info_p);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002348#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 cmd_free(info_p, c, 1);
Bjorn Helgaase2019b52006-06-25 05:49:05 -07002350 return status;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002351}
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353/*
2354 * Map (physical) PCI mem into (virtual) kernel space
2355 */
2356static void __iomem *remap_pci_mem(ulong base, ulong size)
2357{
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002358 ulong page_base = ((ulong) base) & PAGE_MASK;
2359 ulong page_offs = ((ulong) base) - page_base;
2360 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002362 return page_remapped ? (page_remapped + page_offs) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002365/*
2366 * Takes jobs of the Q and sends them to the hardware, then puts it on
2367 * the Q to wait for completion.
2368 */
2369static void start_io(ctlr_info_t *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
2371 CommandList_struct *c;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002372
2373 while ((c = h->reqQ) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 /* can't do anything if fifo is full */
2375 if ((h->access.fifo_full(h))) {
2376 printk(KERN_WARNING "cciss: fifo full\n");
2377 break;
2378 }
2379
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002380 /* Get the first entry from the Request Q */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 removeQ(&(h->reqQ), c);
2382 h->Qdepth--;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002383
2384 /* Tell the controller execute command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 h->access.submit_command(h, c);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002386
2387 /* Put job onto the completed Q */
2388 addQ(&(h->cmpQ), c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392/* Assumes that CCISS_LOCK(h->ctlr) is held. */
2393/* Zeros out the error record and then resends the command back */
2394/* to the controller */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002395static inline void resend_cciss_cmd(ctlr_info_t *h, CommandList_struct *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397 /* erase the old error information */
2398 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
2399
2400 /* add it to software queue and then send it to the controller */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002401 addQ(&(h->reqQ), c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 h->Qdepth++;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002403 if (h->Qdepth > h->maxQsinceinit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 h->maxQsinceinit = h->Qdepth;
2405
2406 start_io(h);
2407}
Jens Axboea9925a02006-01-09 16:04:06 +01002408
Steve Cameron1a614f52007-10-16 23:27:37 -07002409static inline unsigned int make_status_bytes(unsigned int scsi_status_byte,
2410 unsigned int msg_byte, unsigned int host_byte,
2411 unsigned int driver_byte)
2412{
2413 /* inverse of macros in scsi.h */
2414 return (scsi_status_byte & 0xff) |
2415 ((msg_byte & 0xff) << 8) |
2416 ((host_byte & 0xff) << 16) |
2417 ((driver_byte & 0xff) << 24);
2418}
2419
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002420static inline int evaluate_target_status(CommandList_struct *cmd)
2421{
2422 unsigned char sense_key;
Steve Cameron1a614f52007-10-16 23:27:37 -07002423 unsigned char status_byte, msg_byte, host_byte, driver_byte;
2424 int error_value;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002425
Steve Cameron1a614f52007-10-16 23:27:37 -07002426 /* If we get in here, it means we got "target status", that is, scsi status */
2427 status_byte = cmd->err_info->ScsiStatus;
2428 driver_byte = DRIVER_OK;
2429 msg_byte = cmd->err_info->CommandStatus; /* correct? seems too device specific */
2430
2431 if (blk_pc_request(cmd->rq))
2432 host_byte = DID_PASSTHROUGH;
2433 else
2434 host_byte = DID_OK;
2435
2436 error_value = make_status_bytes(status_byte, msg_byte,
2437 host_byte, driver_byte);
2438
2439 if (cmd->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) {
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002440 if (!blk_pc_request(cmd->rq))
2441 printk(KERN_WARNING "cciss: cmd %p "
2442 "has SCSI Status 0x%x\n",
2443 cmd, cmd->err_info->ScsiStatus);
Steve Cameron1a614f52007-10-16 23:27:37 -07002444 return error_value;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002445 }
2446
2447 /* check the sense key */
2448 sense_key = 0xf & cmd->err_info->SenseInfo[2];
2449 /* no status or recovered error */
Steve Cameron1a614f52007-10-16 23:27:37 -07002450 if (((sense_key == 0x0) || (sense_key == 0x1)) && !blk_pc_request(cmd->rq))
2451 error_value = 0;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002452
2453 if (!blk_pc_request(cmd->rq)) { /* Not SG_IO or similar? */
Steve Cameron1a614f52007-10-16 23:27:37 -07002454 if (error_value != 0)
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002455 printk(KERN_WARNING "cciss: cmd %p has CHECK CONDITION"
2456 " sense key = 0x%x\n", cmd, sense_key);
Steve Cameron1a614f52007-10-16 23:27:37 -07002457 return error_value;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002458 }
2459
2460 /* SG_IO or similar, copy sense data back */
2461 if (cmd->rq->sense) {
2462 if (cmd->rq->sense_len > cmd->err_info->SenseLen)
2463 cmd->rq->sense_len = cmd->err_info->SenseLen;
2464 memcpy(cmd->rq->sense, cmd->err_info->SenseInfo,
2465 cmd->rq->sense_len);
2466 } else
2467 cmd->rq->sense_len = 0;
2468
Steve Cameron1a614f52007-10-16 23:27:37 -07002469 return error_value;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002470}
2471
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002472/* checks the status of the job and calls complete buffers to mark all
Jens Axboea9925a02006-01-09 16:04:06 +01002473 * buffers for the completed job. Note that this function does not need
2474 * to hold the hba/queue lock.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002475 */
2476static inline void complete_command(ctlr_info_t *h, CommandList_struct *cmd,
2477 int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 int retry_cmd = 0;
Mike Miller (OS Dev)198b7662007-05-08 00:29:34 -07002480 struct request *rq = cmd->rq;
2481
2482 rq->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002484 if (timeout)
Steve Cameron1a614f52007-10-16 23:27:37 -07002485 rq->errors = make_status_bytes(0, 0, 0, DRIVER_TIMEOUT);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002486
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002487 if (cmd->err_info->CommandStatus == 0) /* no error has occurred */
2488 goto after_error_processing;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002489
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002490 switch (cmd->err_info->CommandStatus) {
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002491 case CMD_TARGET_STATUS:
Mike Miller (OS Dev)198b7662007-05-08 00:29:34 -07002492 rq->errors = evaluate_target_status(cmd);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002493 break;
2494 case CMD_DATA_UNDERRUN:
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002495 if (blk_fs_request(cmd->rq)) {
2496 printk(KERN_WARNING "cciss: cmd %p has"
2497 " completed with data underrun "
2498 "reported\n", cmd);
2499 cmd->rq->data_len = cmd->err_info->ResidualCnt;
2500 }
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002501 break;
2502 case CMD_DATA_OVERRUN:
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002503 if (blk_fs_request(cmd->rq))
2504 printk(KERN_WARNING "cciss: cmd %p has"
2505 " completed with data overrun "
2506 "reported\n", cmd);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002507 break;
2508 case CMD_INVALID:
2509 printk(KERN_WARNING "cciss: cmd %p is "
2510 "reported invalid\n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002511 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2512 cmd->err_info->CommandStatus, DRIVER_OK,
2513 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002514 break;
2515 case CMD_PROTOCOL_ERR:
2516 printk(KERN_WARNING "cciss: cmd %p has "
2517 "protocol error \n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002518 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2519 cmd->err_info->CommandStatus, DRIVER_OK,
2520 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002521 break;
2522 case CMD_HARDWARE_ERR:
2523 printk(KERN_WARNING "cciss: cmd %p had "
2524 " hardware error\n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002525 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2526 cmd->err_info->CommandStatus, DRIVER_OK,
2527 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002528 break;
2529 case CMD_CONNECTION_LOST:
2530 printk(KERN_WARNING "cciss: cmd %p had "
2531 "connection lost\n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002532 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2533 cmd->err_info->CommandStatus, DRIVER_OK,
2534 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002535 break;
2536 case CMD_ABORTED:
2537 printk(KERN_WARNING "cciss: cmd %p was "
2538 "aborted\n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002539 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2540 cmd->err_info->CommandStatus, DRIVER_OK,
2541 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002542 break;
2543 case CMD_ABORT_FAILED:
2544 printk(KERN_WARNING "cciss: cmd %p reports "
2545 "abort failed\n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002546 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2547 cmd->err_info->CommandStatus, DRIVER_OK,
2548 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002549 break;
2550 case CMD_UNSOLICITED_ABORT:
2551 printk(KERN_WARNING "cciss%d: unsolicited "
2552 "abort %p\n", h->ctlr, cmd);
2553 if (cmd->retry_count < MAX_CMD_RETRIES) {
2554 retry_cmd = 1;
2555 printk(KERN_WARNING
2556 "cciss%d: retrying %p\n", h->ctlr, cmd);
2557 cmd->retry_count++;
2558 } else
2559 printk(KERN_WARNING
2560 "cciss%d: %p retried too "
2561 "many times\n", h->ctlr, cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002562 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2563 cmd->err_info->CommandStatus, DRIVER_OK,
2564 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ABORT);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002565 break;
2566 case CMD_TIMEOUT:
2567 printk(KERN_WARNING "cciss: cmd %p timedout\n", cmd);
Steve Cameron1a614f52007-10-16 23:27:37 -07002568 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2569 cmd->err_info->CommandStatus, DRIVER_OK,
2570 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002571 break;
2572 default:
2573 printk(KERN_WARNING "cciss: cmd %p returned "
2574 "unknown status %x\n", cmd,
2575 cmd->err_info->CommandStatus);
Steve Cameron1a614f52007-10-16 23:27:37 -07002576 rq->errors = make_status_bytes(SAM_STAT_GOOD,
2577 cmd->err_info->CommandStatus, DRIVER_OK,
2578 blk_pc_request(cmd->rq) ? DID_PASSTHROUGH : DID_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
Mike Miller (OS Dev)d38ae162007-05-08 00:29:29 -07002580
2581after_error_processing:
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 /* We need to return this command */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002584 if (retry_cmd) {
2585 resend_cciss_cmd(h, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002587 }
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002588 cmd->rq->completion_data = cmd;
Jens Axboea9925a02006-01-09 16:04:06 +01002589 blk_complete_request(cmd->rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002592/*
2593 * Get a request and submit it to the controller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 */
Jens Axboe165125e2007-07-24 09:28:11 +02002595static void do_cciss_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002597 ctlr_info_t *h = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 CommandList_struct *c;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002599 sector_t start_blk;
2600 int seg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 struct request *creq;
2602 u64bit temp64;
2603 struct scatterlist tmp_sg[MAXSGENTRIES];
2604 drive_info_struct *drv;
2605 int i, dir;
2606
2607 /* We call start_io here in case there is a command waiting on the
2608 * queue that has not been sent.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 if (blk_queue_plugged(q))
2611 goto startio;
2612
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002613 queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 creq = elv_next_request(q);
2615 if (!creq)
2616 goto startio;
2617
Eric Sesterhenn089fe1b2006-03-24 18:50:27 +01002618 BUG_ON(creq->nr_phys_segments > MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002620 if ((c = cmd_alloc(h, 1)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 goto full;
2622
2623 blkdev_dequeue_request(creq);
2624
2625 spin_unlock_irq(q->queue_lock);
2626
2627 c->cmd_type = CMD_RWREQ;
2628 c->rq = creq;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002629
2630 /* fill in the request */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 drv = creq->rq_disk->private_data;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002632 c->Header.ReplyQueue = 0; // unused in simple mode
Mike Miller33079b22005-09-13 01:25:22 -07002633 /* got command from pool, so use the command block index instead */
2634 /* for direct lookups. */
2635 /* The first 2 bits are reserved for controller error reporting. */
2636 c->Header.Tag.lower = (c->cmdindex << 3);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002637 c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */
2638 c->Header.LUN.LogDev.VolId = drv->LunID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 c->Header.LUN.LogDev.Mode = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002640 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
2641 c->Request.Type.Type = TYPE_CMD; // It is a command.
2642 c->Request.Type.Attribute = ATTR_SIMPLE;
2643 c->Request.Type.Direction =
Mike Millera52de242006-12-18 11:00:14 +01002644 (rq_data_dir(creq) == READ) ? XFER_READ : XFER_WRITE;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002645 c->Request.Timeout = 0; // Don't time out
2646 c->Request.CDB[0] =
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002647 (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 start_blk = creq->sector;
2649#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002650 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n", (int)creq->sector,
2651 (int)creq->nr_sectors);
2652#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Jens Axboe45711f12007-10-22 21:19:53 +02002654 sg_init_table(tmp_sg, MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 seg = blk_rq_map_sg(q, creq, tmp_sg);
2656
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002657 /* get the DMA records for the setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (c->Request.Type.Direction == XFER_READ)
2659 dir = PCI_DMA_FROMDEVICE;
2660 else
2661 dir = PCI_DMA_TODEVICE;
2662
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002663 for (i = 0; i < seg; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 c->SG[i].Len = tmp_sg[i].length;
Jens Axboe45711f12007-10-22 21:19:53 +02002665 temp64.val = (__u64) pci_map_page(h->pdev, sg_page(&tmp_sg[i]),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002666 tmp_sg[i].offset,
2667 tmp_sg[i].length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 c->SG[i].Addr.lower = temp64.val32.lower;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002669 c->SG[i].Addr.upper = temp64.val32.upper;
2670 c->SG[i].Ext = 0; // we are not chaining
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002672 /* track how many SG entries we are using */
2673 if (seg > h->maxSG)
2674 h->maxSG = seg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
2676#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002677 printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n",
2678 creq->nr_sectors, seg);
2679#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681 c->Header.SGList = c->Header.SGTotal = seg;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002682 if (likely(blk_fs_request(creq))) {
2683 if(h->cciss_read == CCISS_READ_10) {
2684 c->Request.CDB[1] = 0;
2685 c->Request.CDB[2] = (start_blk >> 24) & 0xff; //MSB
2686 c->Request.CDB[3] = (start_blk >> 16) & 0xff;
2687 c->Request.CDB[4] = (start_blk >> 8) & 0xff;
2688 c->Request.CDB[5] = start_blk & 0xff;
2689 c->Request.CDB[6] = 0; // (sect >> 24) & 0xff; MSB
2690 c->Request.CDB[7] = (creq->nr_sectors >> 8) & 0xff;
2691 c->Request.CDB[8] = creq->nr_sectors & 0xff;
2692 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2693 } else {
Randy Dunlap582539e2008-02-06 01:36:54 -08002694 u32 upper32 = upper_32_bits(start_blk);
2695
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002696 c->Request.CDBLen = 16;
2697 c->Request.CDB[1]= 0;
Randy Dunlap582539e2008-02-06 01:36:54 -08002698 c->Request.CDB[2]= (upper32 >> 24) & 0xff; //MSB
2699 c->Request.CDB[3]= (upper32 >> 16) & 0xff;
2700 c->Request.CDB[4]= (upper32 >> 8) & 0xff;
2701 c->Request.CDB[5]= upper32 & 0xff;
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002702 c->Request.CDB[6]= (start_blk >> 24) & 0xff;
2703 c->Request.CDB[7]= (start_blk >> 16) & 0xff;
2704 c->Request.CDB[8]= (start_blk >> 8) & 0xff;
2705 c->Request.CDB[9]= start_blk & 0xff;
2706 c->Request.CDB[10]= (creq->nr_sectors >> 24) & 0xff;
2707 c->Request.CDB[11]= (creq->nr_sectors >> 16) & 0xff;
2708 c->Request.CDB[12]= (creq->nr_sectors >> 8) & 0xff;
2709 c->Request.CDB[13]= creq->nr_sectors & 0xff;
2710 c->Request.CDB[14] = c->Request.CDB[15] = 0;
2711 }
2712 } else if (blk_pc_request(creq)) {
2713 c->Request.CDBLen = creq->cmd_len;
2714 memcpy(c->Request.CDB, creq->cmd, BLK_MAX_CDB);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002715 } else {
Mike Miller (OS Dev)03bbfee2007-05-08 00:29:32 -07002716 printk(KERN_WARNING "cciss%d: bad request type %d\n", h->ctlr, creq->cmd_type);
2717 BUG();
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720 spin_lock_irq(q->queue_lock);
2721
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002722 addQ(&(h->reqQ), c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 h->Qdepth++;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002724 if (h->Qdepth > h->maxQsinceinit)
2725 h->maxQsinceinit = h->Qdepth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
2727 goto queue;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002728full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 blk_stop_queue(q);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002730startio:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 /* We will already have the driver lock here so not need
2732 * to lock it.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 start_io(h);
2735}
2736
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002737static inline unsigned long get_next_completion(ctlr_info_t *h)
2738{
2739#ifdef CONFIG_CISS_SCSI_TAPE
2740 /* Any rejects from sendcmd() lying around? Process them first */
2741 if (h->scsi_rejects.ncompletions == 0)
2742 return h->access.command_completed(h);
2743 else {
2744 struct sendcmd_reject_list *srl;
2745 int n;
2746 srl = &h->scsi_rejects;
2747 n = --srl->ncompletions;
2748 /* printk("cciss%d: processing saved reject\n", h->ctlr); */
2749 printk("p");
2750 return srl->complete[n];
2751 }
2752#else
2753 return h->access.command_completed(h);
2754#endif
2755}
2756
2757static inline int interrupt_pending(ctlr_info_t *h)
2758{
2759#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002760 return (h->access.intr_pending(h)
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002761 || (h->scsi_rejects.ncompletions > 0));
2762#else
2763 return h->access.intr_pending(h);
2764#endif
2765}
2766
2767static inline long interrupt_not_for_us(ctlr_info_t *h)
2768{
2769#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002770 return (((h->access.intr_pending(h) == 0) ||
2771 (h->interrupts_enabled == 0))
2772 && (h->scsi_rejects.ncompletions == 0));
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002773#else
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002774 return (((h->access.intr_pending(h) == 0) ||
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002775 (h->interrupts_enabled == 0)));
2776#endif
2777}
2778
David Howells7d12e782006-10-05 14:55:46 +01002779static irqreturn_t do_cciss_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
2781 ctlr_info_t *h = dev_id;
2782 CommandList_struct *c;
2783 unsigned long flags;
Mike Miller33079b22005-09-13 01:25:22 -07002784 __u32 a, a1, a2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002786 if (interrupt_not_for_us(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 /*
2789 * If there are completed commands in the completion queue,
2790 * we had better do something about it.
2791 */
2792 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002793 while (interrupt_pending(h)) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002794 while ((a = get_next_completion(h)) != FIFO_EMPTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 a1 = a;
Mike Miller33079b22005-09-13 01:25:22 -07002796 if ((a & 0x04)) {
2797 a2 = (a >> 3);
Mike Millerf8806322006-12-06 20:35:01 -08002798 if (a2 >= h->nr_cmds) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002799 printk(KERN_WARNING
2800 "cciss: controller cciss%d failed, stopping.\n",
2801 h->ctlr);
Mike Miller33079b22005-09-13 01:25:22 -07002802 fail_all_cmds(h->ctlr);
2803 return IRQ_HANDLED;
2804 }
2805
2806 c = h->cmd_pool + a2;
2807 a = c->busaddr;
2808
2809 } else {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002810 a &= ~3;
Mike Miller33079b22005-09-13 01:25:22 -07002811 if ((c = h->cmpQ) == NULL) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002812 printk(KERN_WARNING
2813 "cciss: Completion of %08x ignored\n",
2814 a1);
2815 continue;
2816 }
2817 while (c->busaddr != a) {
2818 c = c->next;
2819 if (c == h->cmpQ)
2820 break;
2821 }
Mike Miller33079b22005-09-13 01:25:22 -07002822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 /*
2824 * If we've found the command, take it off the
2825 * completion Q and free it
2826 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002827 if (c->busaddr == a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 removeQ(&h->cmpQ, c);
2829 if (c->cmd_type == CMD_RWREQ) {
2830 complete_command(h, c, 0);
2831 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2832 complete(c->waiting);
2833 }
2834# ifdef CONFIG_CISS_SCSI_TAPE
2835 else if (c->cmd_type == CMD_SCSI)
2836 complete_scsi_command(c, 0, a1);
2837# endif
2838 continue;
2839 }
2840 }
2841 }
2842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2844 return IRQ_HANDLED;
2845}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002846
2847/*
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07002848 * We cannot read the structure directly, for portability we must use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 * the io functions.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002850 * This is for debug only.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 */
2852#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002853static void print_cfg_table(CfgTable_struct *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
2855 int i;
2856 char temp_name[17];
2857
2858 printk("Controller Configuration information\n");
2859 printk("------------------------------------\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002860 for (i = 0; i < 4; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 temp_name[i] = readb(&(tb->Signature[i]));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002862 temp_name[4] = '\0';
2863 printk(" Signature = %s\n", temp_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002865 printk(" Transport methods supported = 0x%x\n",
2866 readl(&(tb->TransportSupport)));
2867 printk(" Transport methods active = 0x%x\n",
2868 readl(&(tb->TransportActive)));
2869 printk(" Requested transport Method = 0x%x\n",
2870 readl(&(tb->HostWrite.TransportRequest)));
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07002871 printk(" Coalesce Interrupt Delay = 0x%x\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002872 readl(&(tb->HostWrite.CoalIntDelay)));
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07002873 printk(" Coalesce Interrupt Count = 0x%x\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002874 readl(&(tb->HostWrite.CoalIntCount)));
2875 printk(" Max outstanding commands = 0x%d\n",
2876 readl(&(tb->CmdsOutMax)));
2877 printk(" Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
2878 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 temp_name[i] = readb(&(tb->ServerName[i]));
2880 temp_name[16] = '\0';
2881 printk(" Server Name = %s\n", temp_name);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002882 printk(" Heartbeat Counter = 0x%x\n\n\n", readl(&(tb->HeartBeat)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002884#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002886static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
2888 int i, offset, mem_type, bar_type;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002889 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return 0;
2891 offset = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002892 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2893 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
2895 offset += 4;
2896 else {
2897 mem_type = pci_resource_flags(pdev, i) &
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002898 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 switch (mem_type) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002900 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2901 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
2902 offset += 4; /* 32 bit */
2903 break;
2904 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2905 offset += 8;
2906 break;
2907 default: /* reserved in PCI 2.2 */
2908 printk(KERN_WARNING
2909 "Base address is invalid\n");
2910 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 break;
2912 }
2913 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002914 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
2915 return i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 }
2917 return -1;
2918}
2919
Mike Millerfb86a352006-01-08 01:03:50 -08002920/* If MSI/MSI-X is supported by the kernel we will try to enable it on
2921 * controllers that are capable. If not, we use IO-APIC mode.
2922 */
2923
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002924static void __devinit cciss_interrupt_mode(ctlr_info_t *c,
2925 struct pci_dev *pdev, __u32 board_id)
Mike Millerfb86a352006-01-08 01:03:50 -08002926{
2927#ifdef CONFIG_PCI_MSI
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002928 int err;
2929 struct msix_entry cciss_msix_entries[4] = { {0, 0}, {0, 1},
2930 {0, 2}, {0, 3}
2931 };
Mike Millerfb86a352006-01-08 01:03:50 -08002932
2933 /* Some boards advertise MSI but don't really support it */
2934 if ((board_id == 0x40700E11) ||
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002935 (board_id == 0x40800E11) ||
2936 (board_id == 0x40820E11) || (board_id == 0x40830E11))
Mike Millerfb86a352006-01-08 01:03:50 -08002937 goto default_int_mode;
2938
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002939 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
2940 err = pci_enable_msix(pdev, cciss_msix_entries, 4);
2941 if (!err) {
2942 c->intr[0] = cciss_msix_entries[0].vector;
2943 c->intr[1] = cciss_msix_entries[1].vector;
2944 c->intr[2] = cciss_msix_entries[2].vector;
2945 c->intr[3] = cciss_msix_entries[3].vector;
2946 c->msix_vector = 1;
2947 return;
2948 }
2949 if (err > 0) {
2950 printk(KERN_WARNING "cciss: only %d MSI-X vectors "
2951 "available\n", err);
Mike Miller1ecb9c02006-12-06 20:35:13 -08002952 goto default_int_mode;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002953 } else {
2954 printk(KERN_WARNING "cciss: MSI-X init failed %d\n",
2955 err);
Mike Miller1ecb9c02006-12-06 20:35:13 -08002956 goto default_int_mode;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002957 }
2958 }
2959 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
2960 if (!pci_enable_msi(pdev)) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002961 c->msi_vector = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002962 } else {
2963 printk(KERN_WARNING "cciss: MSI init failed\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002964 }
2965 }
Mike Miller1ecb9c02006-12-06 20:35:13 -08002966default_int_mode:
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002967#endif /* CONFIG_PCI_MSI */
Mike Millerfb86a352006-01-08 01:03:50 -08002968 /* if we get here we're going to use the default interrupt mode */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002969 c->intr[SIMPLE_MODE_INT] = pdev->irq;
Mike Millerfb86a352006-01-08 01:03:50 -08002970 return;
2971}
2972
Randy Dunlap7d1fd972008-01-14 00:55:17 -08002973static int __devinit cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
2975 ushort subsystem_vendor_id, subsystem_device_id, command;
2976 __u32 board_id, scratchpad = 0;
2977 __u64 cfg_offset;
2978 __u32 cfg_base_addr;
2979 __u64 cfg_base_addr_index;
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002980 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
2982 /* check to see if controller has been disabled */
2983 /* BEFORE trying to enable it */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002984 (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
2985 if (!(command & 0x02)) {
2986 printk(KERN_WARNING
2987 "cciss: controller appears to be disabled\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002988 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
2990
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002991 err = pci_enable_device(pdev);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002992 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002994 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Bjorn Helgaas4e570302006-06-25 05:49:02 -07002997 err = pci_request_regions(pdev, "cciss");
2998 if (err) {
2999 printk(KERN_ERR "cciss: Cannot obtain PCI resources, "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003000 "aborting\n");
Mike Miller872225c2006-12-13 00:34:22 -08003001 return err;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003002 }
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 subsystem_vendor_id = pdev->subsystem_vendor;
3005 subsystem_device_id = pdev->subsystem_device;
3006 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003007 subsystem_vendor_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009#ifdef CCISS_DEBUG
3010 printk("command = %x\n", command);
3011 printk("irq = %x\n", pdev->irq);
3012 printk("board_id = %x\n", board_id);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003013#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Mike Millerfb86a352006-01-08 01:03:50 -08003015/* If the kernel supports MSI/MSI-X we will try to enable that functionality,
3016 * else we use the IO-APIC interrupt assigned to us by system ROM.
3017 */
3018 cciss_interrupt_mode(c, pdev, board_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
3020 /*
3021 * Memory base addr is first addr , the second points to the config
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003022 * table
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 */
3024
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003025 c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026#ifdef CCISS_DEBUG
3027 printk("address 0 = %x\n", c->paddr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003028#endif /* CCISS_DEBUG */
Mike Millera5b92872006-12-13 00:34:21 -08003029 c->vaddr = remap_pci_mem(c->paddr, 0x250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 /* Wait for the board to become ready. (PCI hotplug needs this.)
3032 * We poll for up to 120 secs, once per 100ms. */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003033 for (i = 0; i < 1200; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
3035 if (scratchpad == CCISS_FIRMWARE_READY)
3036 break;
3037 set_current_state(TASK_INTERRUPTIBLE);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003038 schedule_timeout(HZ / 10); /* wait 100ms */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 }
3040 if (scratchpad != CCISS_FIRMWARE_READY) {
3041 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003042 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003043 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 }
3045
3046 /* get the address index number */
3047 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
3048 cfg_base_addr &= (__u32) 0x0000ffff;
3049#ifdef CCISS_DEBUG
3050 printk("cfg base address = %x\n", cfg_base_addr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003051#endif /* CCISS_DEBUG */
3052 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053#ifdef CCISS_DEBUG
3054 printk("cfg base address index = %x\n", cfg_base_addr_index);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003055#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 if (cfg_base_addr_index == -1) {
3057 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003058 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003059 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
3061
3062 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
3063#ifdef CCISS_DEBUG
3064 printk("cfg offset = %x\n", cfg_offset);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003065#endif /* CCISS_DEBUG */
3066 c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
3067 cfg_base_addr_index) +
3068 cfg_offset, sizeof(CfgTable_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 c->board_id = board_id;
3070
3071#ifdef CCISS_DEBUG
Tobias Klauser945f3902006-01-08 01:05:11 -08003072 print_cfg_table(c->cfgtable);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003073#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003075 for (i = 0; i < ARRAY_SIZE(products); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 if (board_id == products[i].board_id) {
3077 c->product_name = products[i].product_name;
3078 c->access = *(products[i].access);
Mike Millerf8806322006-12-06 20:35:01 -08003079 c->nr_cmds = products[i].nr_cmds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 break;
3081 }
3082 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003083 if ((readb(&c->cfgtable->Signature[0]) != 'C') ||
3084 (readb(&c->cfgtable->Signature[1]) != 'I') ||
3085 (readb(&c->cfgtable->Signature[2]) != 'S') ||
3086 (readb(&c->cfgtable->Signature[3]) != 'S')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 printk("Does not appear to be a valid CISS config table\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003088 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003089 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
Mike Miller4ff9a9a2006-12-06 20:35:00 -08003091 /* We didn't find the controller in our list. We know the
3092 * signature is valid. If it's an HP device let's try to
3093 * bind to the device and fire it up. Otherwise we bail.
3094 */
3095 if (i == ARRAY_SIZE(products)) {
3096 if (subsystem_vendor_id == PCI_VENDOR_ID_HP) {
3097 c->product_name = products[i-1].product_name;
3098 c->access = *(products[i-1].access);
Mike Millerf8806322006-12-06 20:35:01 -08003099 c->nr_cmds = products[i-1].nr_cmds;
Mike Miller4ff9a9a2006-12-06 20:35:00 -08003100 printk(KERN_WARNING "cciss: This is an unknown "
3101 "Smart Array controller.\n"
3102 "cciss: Please update to the latest driver "
3103 "available from www.hp.com.\n");
3104 } else {
3105 printk(KERN_WARNING "cciss: Sorry, I don't know how"
3106 " to access the Smart Array controller %08lx\n"
3107 , (unsigned long)board_id);
3108 err = -ENODEV;
3109 goto err_out_free_res;
3110 }
3111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112#ifdef CONFIG_X86
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003113 {
3114 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3115 __u32 prefetch;
3116 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
3117 prefetch |= 0x100;
3118 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
3119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120#endif
3121
Mike Miller (OS Dev)8bf50f72007-10-17 10:10:04 +02003122 /* Disabling DMA prefetch and refetch for the P600.
3123 * An ASIC bug may result in accesses to invalid memory addresses.
3124 * We've disabled prefetch for some time now. Testing with XEN
3125 * kernels revealed a bug in the refetch if dom0 resides on a P600.
Mike Millerf92e2f52006-12-06 20:35:04 -08003126 */
3127 if(board_id == 0x3225103C) {
3128 __u32 dma_prefetch;
Mike Miller (OS Dev)8bf50f72007-10-17 10:10:04 +02003129 __u32 dma_refetch;
Mike Millerf92e2f52006-12-06 20:35:04 -08003130 dma_prefetch = readl(c->vaddr + I2O_DMA1_CFG);
3131 dma_prefetch |= 0x8000;
3132 writel(dma_prefetch, c->vaddr + I2O_DMA1_CFG);
Mike Miller (OS Dev)8bf50f72007-10-17 10:10:04 +02003133 pci_read_config_dword(pdev, PCI_COMMAND_PARITY, &dma_refetch);
3134 dma_refetch |= 0x1;
3135 pci_write_config_dword(pdev, PCI_COMMAND_PARITY, dma_refetch);
Mike Millerf92e2f52006-12-06 20:35:04 -08003136 }
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138#ifdef CCISS_DEBUG
3139 printk("Trying to put board into Simple mode\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003140#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003142 /* Update the field, and then ring the doorbell */
3143 writel(CFGTBL_Trans_Simple, &(c->cfgtable->HostWrite.TransportRequest));
3144 writel(CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
3146 /* under certain very rare conditions, this can take awhile.
3147 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3148 * as we enter this code.) */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003149 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3151 break;
3152 /* delay and try again */
3153 set_current_state(TASK_INTERRUPTIBLE);
3154 schedule_timeout(10);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
3157#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003158 printk(KERN_DEBUG "I counter got to %d %x\n", i,
3159 readl(c->vaddr + SA5_DOORBELL));
3160#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003162 print_cfg_table(c->cfgtable);
3163#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003165 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 printk(KERN_WARNING "cciss: unable to get board into"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003167 " simple mode\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003168 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003169 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 }
3171 return 0;
3172
Linus Torvalds5faad622006-12-13 09:15:34 -08003173err_out_free_res:
Mike Miller872225c2006-12-13 00:34:22 -08003174 /*
3175 * Deliberately omit pci_disable_device(): it does something nasty to
3176 * Smart Array controllers that pci_enable_device does not undo
3177 */
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003178 pci_release_regions(pdev);
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003179 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003182/*
3183 * Gets information about the local volumes attached to the controller.
3184 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185static void cciss_getgeometry(int cntl_num)
3186{
3187 ReportLunData_struct *ld_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 InquiryData_struct *inq_buff;
3189 int return_code;
3190 int i;
3191 int listlength = 0;
3192 __u32 lunid = 0;
Al Virob4482a42007-10-14 19:35:40 +01003193 unsigned block_size;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003194 sector_t total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Eric Sesterhenn06ff37f2006-03-08 11:21:52 +01003196 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003197 if (ld_buff == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 printk(KERN_ERR "cciss: out of memory\n");
3199 return;
3200 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003201 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
3202 if (inq_buff == NULL) {
3203 printk(KERN_ERR "cciss: out of memory\n");
3204 kfree(ld_buff);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003205 return;
3206 }
3207 /* Get the firmware version */
3208 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
3209 sizeof(InquiryData_struct), 0, 0, 0, NULL,
3210 TYPE_CMD);
3211 if (return_code == IO_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
3213 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
3214 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
3215 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003216 } else { /* send command failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003218 printk(KERN_WARNING "cciss: unable to determine firmware"
3219 " version of controller\n");
3220 }
3221 /* Get the number of logical volumes */
3222 return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
3223 sizeof(ReportLunData_struct), 0, 0, 0, NULL,
3224 TYPE_CMD);
3225
3226 if (return_code == IO_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227#ifdef CCISS_DEBUG
3228 printk("LUN Data\n--------------------------\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003229#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003231 listlength |=
3232 (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
3233 listlength |=
3234 (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
3235 listlength |=
3236 (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003238 } else { /* reading number of logical volumes failed */
3239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 printk(KERN_WARNING "cciss: report logical volume"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003241 " command failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 listlength = 0;
3243 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003244 hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
3245 if (hba[cntl_num]->num_luns > CISS_MAX_LUN) {
3246 printk(KERN_ERR
3247 "ciss: only %d number of logical volumes supported\n",
3248 CISS_MAX_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 hba[cntl_num]->num_luns = CISS_MAX_LUN;
3250 }
3251#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003252 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n",
3253 ld_buff->LUNListLength[0], ld_buff->LUNListLength[1],
3254 ld_buff->LUNListLength[2], ld_buff->LUNListLength[3],
3255 hba[cntl_num]->num_luns);
3256#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003258 hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns - 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003259 for (i = 0; i < CISS_MAX_LUN; i++) {
3260 if (i < hba[cntl_num]->num_luns) {
3261 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3]))
3262 << 24;
3263 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2]))
3264 << 16;
3265 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1]))
3266 << 8;
3267 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003269 hba[cntl_num]->drv[i].LunID = lunid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
3271#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003272 printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
3273 ld_buff->LUN[i][0], ld_buff->LUN[i][1],
3274 ld_buff->LUN[i][2], ld_buff->LUN[i][3],
3275 hba[cntl_num]->drv[i].LunID);
3276#endif /* CCISS_DEBUG */
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003277
3278 /* testing to see if 16-byte CDBs are already being used */
3279 if(hba[cntl_num]->cciss_read == CCISS_READ_16) {
3280 cciss_read_capacity_16(cntl_num, i, 0,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003281 &total_size, &block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003282 goto geo_inq;
3283 }
3284 cciss_read_capacity(cntl_num, i, 0, &total_size, &block_size);
3285
Mike Miller (OS Dev)97c06972007-03-06 01:42:14 -08003286 /* If read_capacity returns all F's the logical is >2TB */
3287 /* so we switch to 16-byte CDBs for all read/write ops */
3288 if(total_size == 0xFFFFFFFFULL) {
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003289 cciss_read_capacity_16(cntl_num, i, 0,
3290 &total_size, &block_size);
3291 hba[cntl_num]->cciss_read = CCISS_READ_16;
3292 hba[cntl_num]->cciss_write = CCISS_WRITE_16;
3293 } else {
3294 hba[cntl_num]->cciss_read = CCISS_READ_10;
3295 hba[cntl_num]->cciss_write = CCISS_WRITE_10;
3296 }
3297geo_inq:
Mike Millerddd47442005-09-13 01:25:22 -07003298 cciss_geometry_inquiry(cntl_num, i, 0, total_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003299 block_size, inq_buff,
3300 &hba[cntl_num]->drv[i]);
Mike Millerddd47442005-09-13 01:25:22 -07003301 } else {
3302 /* initialize raid_level to indicate a free space */
3303 hba[cntl_num]->drv[i].raid_level = -1;
3304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 }
3306 kfree(ld_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 kfree(inq_buff);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003308}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
3310/* Function to find the first free pointer into our hba[] array */
3311/* Returns -1 if no free entries are left. */
3312static int alloc_cciss_hba(void)
3313{
Mike Miller799202c2006-12-06 20:35:12 -08003314 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003316 for (i = 0; i < MAX_CTLR; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 if (!hba[i]) {
3318 ctlr_info_t *p;
Jesper Juhlf2912a12007-07-31 00:39:39 -07003319
Eric Sesterhenn06ff37f2006-03-08 11:21:52 +01003320 p = kzalloc(sizeof(ctlr_info_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 if (!p)
3322 goto Enomem;
Mike Miller799202c2006-12-06 20:35:12 -08003323 p->gendisk[0] = alloc_disk(1 << NWD_SHIFT);
Jesper Juhlf2912a12007-07-31 00:39:39 -07003324 if (!p->gendisk[0]) {
3325 kfree(p);
Mike Miller799202c2006-12-06 20:35:12 -08003326 goto Enomem;
Jesper Juhlf2912a12007-07-31 00:39:39 -07003327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 hba[i] = p;
3329 return i;
3330 }
3331 }
3332 printk(KERN_WARNING "cciss: This driver supports a maximum"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003333 " of %d controllers.\n", MAX_CTLR);
Mike Miller799202c2006-12-06 20:35:12 -08003334 return -1;
3335Enomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 printk(KERN_ERR "cciss: out of memory.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 return -1;
3338}
3339
3340static void free_hba(int i)
3341{
3342 ctlr_info_t *p = hba[i];
3343 int n;
3344
3345 hba[i] = NULL;
Mike Miller799202c2006-12-06 20:35:12 -08003346 for (n = 0; n < CISS_MAX_LUN; n++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 put_disk(p->gendisk[n]);
3348 kfree(p);
3349}
3350
3351/*
3352 * This is it. Find all the controllers and register them. I really hate
3353 * stealing all these major device numbers.
3354 * returns the number of block devices registered.
3355 */
3356static int __devinit cciss_init_one(struct pci_dev *pdev,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003357 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 int i;
Mike Miller799202c2006-12-06 20:35:12 -08003360 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 int rc;
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003362 int dac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 i = alloc_cciss_hba();
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003365 if (i < 0)
Bjorn Helgaase2019b52006-06-25 05:49:05 -07003366 return -1;
Mike Miller1f8ef382005-09-13 01:25:21 -07003367
3368 hba[i]->busy_initializing = 1;
3369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (cciss_pci_init(hba[i], pdev) != 0)
3371 goto clean1;
3372
3373 sprintf(hba[i]->devname, "cciss%d", i);
3374 hba[i]->ctlr = i;
3375 hba[i]->pdev = pdev;
3376
3377 /* configure PCI DMA stuff */
mike.miller@hp.comeb0df992005-06-10 14:51:04 -05003378 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK))
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003379 dac = 1;
mike.miller@hp.comeb0df992005-06-10 14:51:04 -05003380 else if (!pci_set_dma_mask(pdev, DMA_32BIT_MASK))
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003381 dac = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 else {
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003383 printk(KERN_ERR "cciss: no suitable DMA available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 goto clean1;
3385 }
3386
3387 /*
3388 * register with the major number, or get a dynamic major number
3389 * by passing 0 as argument. This is done for greater than
3390 * 8 controller support.
3391 */
3392 if (i < MAX_CTLR_ORIG)
Christoph Hellwig564de742006-01-08 01:05:17 -08003393 hba[i]->major = COMPAQ_CISS_MAJOR + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 rc = register_blkdev(hba[i]->major, hba[i]->devname);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003395 if (rc == -EBUSY || rc == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 printk(KERN_ERR
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003397 "cciss: Unable to get major number %d for %s "
3398 "on hba %d\n", hba[i]->major, hba[i]->devname, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 goto clean1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003400 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 if (i >= MAX_CTLR_ORIG)
3402 hba[i]->major = rc;
3403 }
3404
3405 /* make sure the board interrupts are off */
3406 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003407 if (request_irq(hba[i]->intr[SIMPLE_MODE_INT], do_cciss_intr,
Thomas Gleixner69ab3912006-07-01 19:29:32 -07003408 IRQF_DISABLED | IRQF_SHARED, hba[i]->devname, hba[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003410 hba[i]->intr[SIMPLE_MODE_INT], hba[i]->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 goto clean2;
3412 }
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003413
3414 printk(KERN_INFO "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003415 hba[i]->devname, pdev->device, pci_name(pdev),
3416 hba[i]->intr[SIMPLE_MODE_INT], dac ? "" : " not");
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003417
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003418 hba[i]->cmd_pool_bits =
Mike Millerf8806322006-12-06 20:35:01 -08003419 kmalloc(((hba[i]->nr_cmds + BITS_PER_LONG -
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003420 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
3421 hba[i]->cmd_pool = (CommandList_struct *)
3422 pci_alloc_consistent(hba[i]->pdev,
Mike Millerf8806322006-12-06 20:35:01 -08003423 hba[i]->nr_cmds * sizeof(CommandList_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003424 &(hba[i]->cmd_pool_dhandle));
3425 hba[i]->errinfo_pool = (ErrorInfo_struct *)
3426 pci_alloc_consistent(hba[i]->pdev,
Mike Millerf8806322006-12-06 20:35:01 -08003427 hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003428 &(hba[i]->errinfo_pool_dhandle));
3429 if ((hba[i]->cmd_pool_bits == NULL)
3430 || (hba[i]->cmd_pool == NULL)
3431 || (hba[i]->errinfo_pool == NULL)) {
3432 printk(KERN_ERR "cciss: out of memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 goto clean4;
3434 }
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003435#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003436 hba[i]->scsi_rejects.complete =
3437 kmalloc(sizeof(hba[i]->scsi_rejects.complete[0]) *
Mike Millerf8806322006-12-06 20:35:01 -08003438 (hba[i]->nr_cmds + 5), GFP_KERNEL);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003439 if (hba[i]->scsi_rejects.complete == NULL) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003440 printk(KERN_ERR "cciss: out of memory");
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003441 goto clean4;
3442 }
3443#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 spin_lock_init(&hba[i]->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003446 /* Initialize the pdev driver private data.
3447 have it point to hba[i]. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 pci_set_drvdata(pdev, hba[i]);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003449 /* command and error info recs zeroed out before
3450 they are used */
3451 memset(hba[i]->cmd_pool_bits, 0,
Mike Millerf8806322006-12-06 20:35:01 -08003452 ((hba[i]->nr_cmds + BITS_PER_LONG -
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003453 1) / BITS_PER_LONG) * sizeof(unsigned long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003455#ifdef CCISS_DEBUG
3456 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n", i);
3457#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
3459 cciss_getgeometry(i);
3460
3461 cciss_scsi_setup(i);
3462
3463 /* Turn the interrupts on so we can service requests */
3464 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
3465
3466 cciss_procinit(i);
Mike Miller92c4231a2006-12-06 20:35:06 -08003467
3468 hba[i]->cciss_max_sectors = 2048;
3469
Mike Millerd6dbf422005-09-21 09:55:32 -07003470 hba[i]->busy_initializing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Mike Miller799202c2006-12-06 20:35:12 -08003472 do {
Mike Millerad2b9312005-07-28 01:07:31 -07003473 drive_info_struct *drv = &(hba[i]->drv[j]);
3474 struct gendisk *disk = hba[i]->gendisk[j];
Jens Axboe165125e2007-07-24 09:28:11 +02003475 struct request_queue *q;
Mike Miller799202c2006-12-06 20:35:12 -08003476
3477 /* Check if the disk was allocated already */
3478 if (!disk){
3479 hba[i]->gendisk[j] = alloc_disk(1 << NWD_SHIFT);
3480 disk = hba[i]->gendisk[j];
3481 }
3482
3483 /* Check that the disk was able to be allocated */
3484 if (!disk) {
3485 printk(KERN_ERR "cciss: unable to allocate memory for disk %d\n", j);
3486 goto clean4;
3487 }
Mike Millerad2b9312005-07-28 01:07:31 -07003488
3489 q = blk_init_queue(do_cciss_request, &hba[i]->lock);
3490 if (!q) {
3491 printk(KERN_ERR
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003492 "cciss: unable to allocate queue for disk %d\n",
3493 j);
Mike Miller799202c2006-12-06 20:35:12 -08003494 goto clean4;
Mike Millerad2b9312005-07-28 01:07:31 -07003495 }
3496 drv->queue = q;
3497
3498 q->backing_dev_info.ra_pages = READ_AHEAD;
Jens Axboea9925a02006-01-09 16:04:06 +01003499 blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Jens Axboea9925a02006-01-09 16:04:06 +01003501 /* This is a hardware imposed limit. */
3502 blk_queue_max_hw_segments(q, MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Jens Axboea9925a02006-01-09 16:04:06 +01003504 /* This is a limit in the driver and could be eliminated. */
3505 blk_queue_max_phys_segments(q, MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Mike Miller92c4231a2006-12-06 20:35:06 -08003507 blk_queue_max_sectors(q, hba[i]->cciss_max_sectors);
Jens Axboea9925a02006-01-09 16:04:06 +01003508
3509 blk_queue_softirq_done(q, cciss_softirq_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Mike Millerad2b9312005-07-28 01:07:31 -07003511 q->queuedata = hba[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 disk->major = hba[i]->major;
3514 disk->first_minor = j << NWD_SHIFT;
3515 disk->fops = &cciss_fops;
Mike Millerad2b9312005-07-28 01:07:31 -07003516 disk->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 disk->private_data = drv;
Greg Kroah-Hartman27c0ff82006-04-27 15:46:39 -07003518 disk->driverfs_dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 /* we must register the controller even if no disks exist */
3520 /* this is for the online array utilities */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003521 if (!drv->heads && j)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 continue;
Mike Millerad2b9312005-07-28 01:07:31 -07003523 blk_queue_hardsect_size(q, drv->block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 set_capacity(disk, drv->nr_blocks);
3525 add_disk(disk);
Mike Miller799202c2006-12-06 20:35:12 -08003526 j++;
3527 } while (j <= hba[i]->highest_lun);
Mike Millerad2b9312005-07-28 01:07:31 -07003528
Bjorn Helgaase2019b52006-06-25 05:49:05 -07003529 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003531 clean4:
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003532#ifdef CONFIG_CISS_SCSI_TAPE
Andrew Morton1acc0b02006-01-04 18:30:03 -08003533 kfree(hba[i]->scsi_rejects.complete);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003534#endif
Jesper Juhl6044ec82005-11-07 01:01:32 -08003535 kfree(hba[i]->cmd_pool_bits);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003536 if (hba[i]->cmd_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 pci_free_consistent(hba[i]->pdev,
Mike Millerf8806322006-12-06 20:35:01 -08003538 hba[i]->nr_cmds * sizeof(CommandList_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003539 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
3540 if (hba[i]->errinfo_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 pci_free_consistent(hba[i]->pdev,
Mike Millerf8806322006-12-06 20:35:01 -08003542 hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003543 hba[i]->errinfo_pool,
3544 hba[i]->errinfo_pool_dhandle);
Mike Millerfb86a352006-01-08 01:03:50 -08003545 free_irq(hba[i]->intr[SIMPLE_MODE_INT], hba[i]);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003546 clean2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 unregister_blkdev(hba[i]->major, hba[i]->devname);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003548 clean1:
Mike Miller1f8ef382005-09-13 01:25:21 -07003549 hba[i]->busy_initializing = 0;
Mike Miller799202c2006-12-06 20:35:12 -08003550 /* cleanup any queues that may have been initialized */
3551 for (j=0; j <= hba[i]->highest_lun; j++){
3552 drive_info_struct *drv = &(hba[i]->drv[j]);
3553 if (drv->queue)
3554 blk_cleanup_queue(drv->queue);
3555 }
Mike Miller872225c2006-12-13 00:34:22 -08003556 /*
3557 * Deliberately omit pci_disable_device(): it does something nasty to
3558 * Smart Array controllers that pci_enable_device does not undo
3559 */
Mike Miller799202c2006-12-06 20:35:12 -08003560 pci_release_regions(pdev);
Mike Miller799202c2006-12-06 20:35:12 -08003561 pci_set_drvdata(pdev, NULL);
Patrick McHardy61808c22006-03-23 02:59:24 -08003562 free_hba(i);
Bjorn Helgaase2019b52006-06-25 05:49:05 -07003563 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
Gerald Brittone9ca75b2007-05-14 13:53:01 -04003566static void cciss_shutdown(struct pci_dev *pdev)
3567{
3568 ctlr_info_t *tmp_ptr;
3569 int i;
3570 char flush_buf[4];
3571 int return_code;
3572
3573 tmp_ptr = pci_get_drvdata(pdev);
3574 if (tmp_ptr == NULL)
3575 return;
3576 i = tmp_ptr->ctlr;
3577 if (hba[i] == NULL)
3578 return;
3579
3580 /* Turn board interrupts off and send the flush cache command */
3581 /* sendcmd will turn off interrupt, and send the flush...
3582 * To write all data in the battery backed cache to disks */
3583 memset(flush_buf, 0, 4);
3584 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
3585 TYPE_CMD);
3586 if (return_code == IO_OK) {
3587 printk(KERN_INFO "Completed flushing cache on controller %d\n", i);
3588 } else {
3589 printk(KERN_WARNING "Error flushing cache on controller %d\n", i);
3590 }
3591 free_irq(hba[i]->intr[2], hba[i]);
3592}
3593
3594static void __devexit cciss_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595{
3596 ctlr_info_t *tmp_ptr;
3597 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003599 if (pci_get_drvdata(pdev) == NULL) {
3600 printk(KERN_ERR "cciss: Unable to remove device \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 return;
3602 }
3603 tmp_ptr = pci_get_drvdata(pdev);
3604 i = tmp_ptr->ctlr;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003605 if (hba[i] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 printk(KERN_ERR "cciss: device appears to "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003607 "already be removed \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 return;
3609 }
Bjorn Helgaasb6550772007-04-11 23:28:43 -07003610
3611 remove_proc_entry(hba[i]->devname, proc_cciss);
3612 unregister_blkdev(hba[i]->major, hba[i]->devname);
3613
3614 /* remove it from the disk list */
3615 for (j = 0; j < CISS_MAX_LUN; j++) {
3616 struct gendisk *disk = hba[i]->gendisk[j];
3617 if (disk) {
Jens Axboe165125e2007-07-24 09:28:11 +02003618 struct request_queue *q = disk->queue;
Bjorn Helgaasb6550772007-04-11 23:28:43 -07003619
3620 if (disk->flags & GENHD_FL_UP)
3621 del_gendisk(disk);
3622 if (q)
3623 blk_cleanup_queue(q);
3624 }
3625 }
3626
3627 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
3628
Gerald Brittone9ca75b2007-05-14 13:53:01 -04003629 cciss_shutdown(pdev);
Mike Millerfb86a352006-01-08 01:03:50 -08003630
3631#ifdef CONFIG_PCI_MSI
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003632 if (hba[i]->msix_vector)
3633 pci_disable_msix(hba[i]->pdev);
3634 else if (hba[i]->msi_vector)
3635 pci_disable_msi(hba[i]->pdev);
3636#endif /* CONFIG_PCI_MSI */
Mike Millerfb86a352006-01-08 01:03:50 -08003637
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 iounmap(hba[i]->vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Mike Millerf8806322006-12-06 20:35:01 -08003640 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(CommandList_struct),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
Mike Millerf8806322006-12-06 20:35:01 -08003642 pci_free_consistent(hba[i]->pdev, hba[i]->nr_cmds * sizeof(ErrorInfo_struct),
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003643 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 kfree(hba[i]->cmd_pool_bits);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003645#ifdef CONFIG_CISS_SCSI_TAPE
3646 kfree(hba[i]->scsi_rejects.complete);
3647#endif
Mike Miller872225c2006-12-13 00:34:22 -08003648 /*
3649 * Deliberately omit pci_disable_device(): it does something nasty to
3650 * Smart Array controllers that pci_enable_device does not undo
3651 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003652 pci_release_regions(pdev);
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003653 pci_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 free_hba(i);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657static struct pci_driver cciss_pci_driver = {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003658 .name = "cciss",
3659 .probe = cciss_init_one,
3660 .remove = __devexit_p(cciss_remove_one),
3661 .id_table = cciss_pci_device_id, /* id_table */
Gerald Brittone9ca75b2007-05-14 13:53:01 -04003662 .shutdown = cciss_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663};
3664
3665/*
3666 * This is it. Register the PCI driver information for the cards we control
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003667 * the OS will call our registered routines when it finds one of our cards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 */
3669static int __init cciss_init(void)
3670{
3671 printk(KERN_INFO DRIVER_NAME "\n");
3672
3673 /* Register for our PCI devices */
Richard Knutsson9bfab8c2005-11-30 00:59:34 +01003674 return pci_register_driver(&cciss_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675}
3676
3677static void __exit cciss_cleanup(void)
3678{
3679 int i;
3680
3681 pci_unregister_driver(&cciss_pci_driver);
3682 /* double check that all controller entrys have been removed */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003683 for (i = 0; i < MAX_CTLR; i++) {
3684 if (hba[i] != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 printk(KERN_WARNING "cciss: had to remove"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003686 " controller %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 cciss_remove_one(hba[i]->pdev);
3688 }
3689 }
3690 remove_proc_entry("cciss", proc_root_driver);
3691}
3692
Mike Miller33079b22005-09-13 01:25:22 -07003693static void fail_all_cmds(unsigned long ctlr)
3694{
3695 /* If we get here, the board is apparently dead. */
3696 ctlr_info_t *h = hba[ctlr];
3697 CommandList_struct *c;
3698 unsigned long flags;
3699
3700 printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003701 h->alive = 0; /* the controller apparently died... */
Mike Miller33079b22005-09-13 01:25:22 -07003702
3703 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
3704
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003705 pci_disable_device(h->pdev); /* Make sure it is really dead. */
Mike Miller33079b22005-09-13 01:25:22 -07003706
3707 /* move everything off the request queue onto the completed queue */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003708 while ((c = h->reqQ) != NULL) {
Mike Miller33079b22005-09-13 01:25:22 -07003709 removeQ(&(h->reqQ), c);
3710 h->Qdepth--;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003711 addQ(&(h->cmpQ), c);
Mike Miller33079b22005-09-13 01:25:22 -07003712 }
3713
3714 /* Now, fail everything on the completed queue with a HW error */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003715 while ((c = h->cmpQ) != NULL) {
Mike Miller33079b22005-09-13 01:25:22 -07003716 removeQ(&h->cmpQ, c);
3717 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
3718 if (c->cmd_type == CMD_RWREQ) {
3719 complete_command(h, c, 0);
3720 } else if (c->cmd_type == CMD_IOCTL_PEND)
3721 complete(c->waiting);
3722#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003723 else if (c->cmd_type == CMD_SCSI)
3724 complete_scsi_command(c, 0, 0);
Mike Miller33079b22005-09-13 01:25:22 -07003725#endif
3726 }
3727 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
3728 return;
3729}
3730
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731module_init(cciss_init);
3732module_exit(cciss_cleanup);