blob: 0af6f356fba5226ed34b4b5707c18b19102ccafd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Disk Array driver for HP SA 5xxx and 6xxx Controllers
Mike Millerfb86a352006-01-08 01:03:50 -08003 * Copyright 2000, 2006 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * Questions/Comments/Bugfixes to iss_storagedev@hp.com
20 *
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/interrupt.h>
25#include <linux/types.h>
26#include <linux/pci.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/delay.h>
30#include <linux/major.h>
31#include <linux/fs.h>
32#include <linux/bio.h>
33#include <linux/blkpg.h>
34#include <linux/timer.h>
35#include <linux/proc_fs.h>
Bjorn Helgaas7c832832006-06-25 05:49:06 -070036#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/hdreg.h>
38#include <linux/spinlock.h>
39#include <linux/compat.h>
Jens Axboe2056a782006-03-23 20:00:26 +010040#include <linux/blktrace_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/uaccess.h>
42#include <asm/io.h>
43
mike.miller@hp.comeb0df992005-06-10 14:51:04 -050044#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/blkdev.h>
46#include <linux/genhd.h>
47#include <linux/completion.h>
48
49#define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
Mike Miller9d827c92006-12-06 20:34:58 -080050#define DRIVER_NAME "HP CISS Driver (v 3.6.14)"
51#define DRIVER_VERSION CCISS_DRIVER_VERSION(3,6,14)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* Embedded module documentation macros - see modules.h */
54MODULE_AUTHOR("Hewlett-Packard Company");
Mike Miller9d827c92006-12-06 20:34:58 -080055MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 3.6.14");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
Mike Miller1883c5a2006-09-12 20:36:07 -070057 " SA6i P600 P800 P400 P400i E200 E200i E500");
Mike Miller9d827c92006-12-06 20:34:58 -080058MODULE_VERSION("3.6.14");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059MODULE_LICENSE("GPL");
60
61#include "cciss_cmd.h"
62#include "cciss.h"
63#include <linux/cciss_ioctl.h>
64
65/* define the PCI info for the cards we can control */
66static const struct pci_device_id cciss_pci_device_id[] = {
Bjorn Helgaasf82ccdb2006-06-25 05:49:07 -070067 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS, 0x0E11, 0x4070},
68 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4080},
69 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4082},
70 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4083},
71 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x4091},
72 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409A},
73 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409B},
74 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409C},
75 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409D},
76 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA, 0x103C, 0x3225},
77 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3223},
78 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3234},
79 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3235},
80 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3211},
81 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3212},
82 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3213},
83 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3214},
84 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3215},
Mike Miller1883c5a2006-09-12 20:36:07 -070085 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3233},
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 {0,}
87};
Bjorn Helgaas7c832832006-06-25 05:49:06 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* board_id = Subsystem Device ID & Vendor ID
92 * product = Marketing Name for the board
Bjorn Helgaas7c832832006-06-25 05:49:06 -070093 * access = Address of the struct of function pointers
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95static struct board_type products[] = {
Bjorn Helgaas7c832832006-06-25 05:49:06 -070096 {0x40700E11, "Smart Array 5300", &SA5_access},
97 {0x40800E11, "Smart Array 5i", &SA5B_access},
98 {0x40820E11, "Smart Array 532", &SA5B_access},
99 {0x40830E11, "Smart Array 5312", &SA5B_access},
100 {0x409A0E11, "Smart Array 641", &SA5_access},
101 {0x409B0E11, "Smart Array 642", &SA5_access},
102 {0x409C0E11, "Smart Array 6400", &SA5_access},
103 {0x409D0E11, "Smart Array 6400 EM", &SA5_access},
104 {0x40910E11, "Smart Array 6i", &SA5_access},
105 {0x3225103C, "Smart Array P600", &SA5_access},
106 {0x3223103C, "Smart Array P800", &SA5_access},
107 {0x3234103C, "Smart Array P400", &SA5_access},
108 {0x3235103C, "Smart Array P400i", &SA5_access},
109 {0x3211103C, "Smart Array E200i", &SA5_access},
110 {0x3212103C, "Smart Array E200", &SA5_access},
111 {0x3213103C, "Smart Array E200i", &SA5_access},
112 {0x3214103C, "Smart Array E200i", &SA5_access},
113 {0x3215103C, "Smart Array E200i", &SA5_access},
Mike Miller1883c5a2006-09-12 20:36:07 -0700114 {0x3233103C, "Smart Array E500", &SA5_access},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -0700117/* How long to wait (in milliseconds) for board to go into simple mode */
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700118#define MAX_CONFIG_WAIT 30000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define MAX_IOCTL_CONFIG_WAIT 1000
120
121/*define how many times we will try a command because of bus resets */
122#define MAX_CMD_RETRIES 3
123
124#define READ_AHEAD 1024
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700125#define NR_CMDS 384 /* #commands that can be outstanding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define MAX_CTLR 32
127
128/* Originally cciss driver only supports 8 major numbers */
129#define MAX_CTLR_ORIG 8
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static ctlr_info_t *hba[MAX_CTLR];
132
133static void do_cciss_request(request_queue_t *q);
David Howells7d12e782006-10-05 14:55:46 +0100134static irqreturn_t do_cciss_intr(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static int cciss_open(struct inode *inode, struct file *filep);
136static int cciss_release(struct inode *inode, struct file *filep);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700137static int cciss_ioctl(struct inode *inode, struct file *filep,
138 unsigned int cmd, unsigned long arg);
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800139static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141static int revalidate_allvol(ctlr_info_t *host);
142static int cciss_revalidate(struct gendisk *disk);
Mike Millerddd47442005-09-13 01:25:22 -0700143static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700144static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
145 int clear_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -0700147static void cciss_read_capacity(int ctlr, int logvol, int withirq,
148 sector_t *total_size, unsigned int *block_size);
149static void cciss_read_capacity_16(int ctlr, int logvol, int withirq,
150 sector_t *total_size, unsigned int *block_size);
151static void cciss_geometry_inquiry(int ctlr, int logvol,
152 int withirq, sector_t total_size,
153 unsigned int block_size, InquiryData_struct *inq_buff,
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700154 drive_info_struct *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static void cciss_getgeometry(int cntl_num);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700156static void __devinit cciss_interrupt_mode(ctlr_info_t *, struct pci_dev *,
157 __u32);
158static void start_io(ctlr_info_t *h);
159static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size,
160 unsigned int use_unit_num, unsigned int log_unit,
161 __u8 page_code, unsigned char *scsi3addr, int cmd_type);
162static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
163 unsigned int use_unit_num, unsigned int log_unit,
164 __u8 page_code, int cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Mike Miller33079b22005-09-13 01:25:22 -0700166static void fail_all_cmds(unsigned long ctlr);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#ifdef CONFIG_PROC_FS
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700169static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
170 int length, int *eof, void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static void cciss_procinit(int i);
172#else
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700173static void cciss_procinit(int i)
174{
175}
176#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178#ifdef CONFIG_COMPAT
179static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg);
180#endif
181
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700182static struct block_device_operations cciss_fops = {
183 .owner = THIS_MODULE,
184 .open = cciss_open,
185 .release = cciss_release,
186 .ioctl = cciss_ioctl,
187 .getgeo = cciss_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#ifdef CONFIG_COMPAT
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700189 .compat_ioctl = cciss_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700191 .revalidate_disk = cciss_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
194/*
195 * Enqueuing and dequeuing functions for cmdlists.
196 */
197static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
198{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700199 if (*Qptr == NULL) {
200 *Qptr = c;
201 c->next = c->prev = c;
202 } else {
203 c->prev = (*Qptr)->prev;
204 c->next = (*Qptr);
205 (*Qptr)->prev->next = c;
206 (*Qptr)->prev = c;
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700210static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
211 CommandList_struct *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700213 if (c && c->next != c) {
214 if (*Qptr == c)
215 *Qptr = c->next;
216 c->prev->next = c->next;
217 c->next->prev = c->prev;
218 } else {
219 *Qptr = NULL;
220 }
221 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224#include "cciss_scsi.c" /* For SCSI tape support */
225
226#ifdef CONFIG_PROC_FS
227
228/*
229 * Report information about this controller.
230 */
231#define ENG_GIG 1000000000
232#define ENG_GIG_FACTOR (ENG_GIG/512)
233#define RAID_UNKNOWN 6
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700234static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
235 "UNKNOWN"
236};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238static struct proc_dir_entry *proc_cciss;
239
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700240static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
241 int length, int *eof, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700243 off_t pos = 0;
244 off_t len = 0;
245 int size, i, ctlr;
246 ctlr_info_t *h = (ctlr_info_t *) data;
247 drive_info_struct *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 unsigned long flags;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700249 sector_t vol_sz, vol_sz_frac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700251 ctlr = h->ctlr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* prevent displaying bogus info during configuration
254 * or deconfiguration of a logical volume
255 */
256 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
257 if (h->busy_configuring) {
258 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700259 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 h->busy_configuring = 1;
262 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
263
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700264 size = sprintf(buffer, "%s: HP %s Controller\n"
265 "Board ID: 0x%08lx\n"
266 "Firmware Version: %c%c%c%c\n"
267 "IRQ: %d\n"
268 "Logical drives: %d\n"
269 "Current Q depth: %d\n"
270 "Current # commands on controller: %d\n"
271 "Max Q depth since init: %d\n"
272 "Max # commands on controller since init: %d\n"
273 "Max SG entries since init: %d\n\n",
274 h->devname,
275 h->product_name,
276 (unsigned long)h->board_id,
277 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
278 h->firm_ver[3], (unsigned int)h->intr[SIMPLE_MODE_INT],
279 h->num_luns, h->Qdepth, h->commands_outstanding,
280 h->maxQsinceinit, h->max_outstanding, h->maxSG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700282 pos += size;
283 len += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 cciss_proc_tape_report(ctlr, buffer, &pos, &len);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700285 for (i = 0; i <= h->highest_lun; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700287 drv = &h->drv[i];
Mike Millerddd47442005-09-13 01:25:22 -0700288 if (drv->heads == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 continue;
290
291 vol_sz = drv->nr_blocks;
292 vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
293 vol_sz_frac *= 100;
294 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
295
296 if (drv->raid_level > 5)
297 drv->raid_level = RAID_UNKNOWN;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700298 size = sprintf(buffer + len, "cciss/c%dd%d:"
299 "\t%4u.%02uGB\tRAID %s\n",
300 ctlr, i, (int)vol_sz, (int)vol_sz_frac,
301 raid_label[drv->raid_level]);
302 pos += size;
303 len += size;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700306 *eof = 1;
307 *start = buffer + offset;
308 len -= offset;
309 if (len > length)
310 len = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 h->busy_configuring = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700312 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700315static int
316cciss_proc_write(struct file *file, const char __user *buffer,
317 unsigned long count, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 unsigned char cmd[80];
320 int len;
321#ifdef CONFIG_CISS_SCSI_TAPE
322 ctlr_info_t *h = (ctlr_info_t *) data;
323 int rc;
324#endif
325
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700326 if (count > sizeof(cmd) - 1)
327 return -EINVAL;
328 if (copy_from_user(cmd, buffer, count))
329 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 cmd[count] = '\0';
331 len = strlen(cmd); // above 3 lines ensure safety
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700332 if (len && cmd[len - 1] == '\n')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 cmd[--len] = '\0';
334# ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700335 if (strcmp("engage scsi", cmd) == 0) {
336 rc = cciss_engage_scsi(h->ctlr);
337 if (rc != 0)
338 return -rc;
339 return count;
340 }
341 /* might be nice to have "disengage" too, but it's not
342 safely possible. (only 1 module use count, lock issues.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343# endif
344 return -EINVAL;
345}
346
347/*
348 * Get us a file in /proc/cciss that says something about each controller.
349 * Create /proc/cciss if it doesn't exist yet.
350 */
351static void __devinit cciss_procinit(int i)
352{
353 struct proc_dir_entry *pde;
354
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700355 if (proc_cciss == NULL) {
356 proc_cciss = proc_mkdir("cciss", proc_root_driver);
357 if (!proc_cciss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700361 pde = create_proc_read_entry(hba[i]->devname,
362 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
363 proc_cciss, cciss_proc_get_info, hba[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 pde->write_proc = cciss_proc_write;
365}
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700366#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700368/*
369 * For operations that cannot sleep, a command block is allocated at init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700371 * which ones are free or in use. For operations that can wait for kmalloc
372 * to possible sleep, this routine can be called with get_from_pool set to 0.
373 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
374 */
375static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 CommandList_struct *c;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700378 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 u64bit temp64;
380 dma_addr_t cmd_dma_handle, err_dma_handle;
381
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700382 if (!get_from_pool) {
383 c = (CommandList_struct *) pci_alloc_consistent(h->pdev,
384 sizeof(CommandList_struct), &cmd_dma_handle);
385 if (c == NULL)
386 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 memset(c, 0, sizeof(CommandList_struct));
388
Mike Miller33079b22005-09-13 01:25:22 -0700389 c->cmdindex = -1;
390
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700391 c->err_info = (ErrorInfo_struct *)
392 pci_alloc_consistent(h->pdev, sizeof(ErrorInfo_struct),
393 &err_dma_handle);
394
395 if (c->err_info == NULL) {
396 pci_free_consistent(h->pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 sizeof(CommandList_struct), c, cmd_dma_handle);
398 return NULL;
399 }
400 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700401 } else { /* get it out of the controllers pool */
402
403 do {
404 i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
405 if (i == NR_CMDS)
406 return NULL;
407 } while (test_and_set_bit
408 (i & (BITS_PER_LONG - 1),
409 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410#ifdef CCISS_DEBUG
411 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
412#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700413 c = h->cmd_pool + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 memset(c, 0, sizeof(CommandList_struct));
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700415 cmd_dma_handle = h->cmd_pool_dhandle
416 + i * sizeof(CommandList_struct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 c->err_info = h->errinfo_pool + i;
418 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700419 err_dma_handle = h->errinfo_pool_dhandle
420 + i * sizeof(ErrorInfo_struct);
421 h->nr_allocs++;
Mike Miller33079b22005-09-13 01:25:22 -0700422
423 c->cmdindex = i;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 c->busaddr = (__u32) cmd_dma_handle;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700427 temp64.val = (__u64) err_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 c->ErrDesc.Addr.lower = temp64.val32.lower;
429 c->ErrDesc.Addr.upper = temp64.val32.upper;
430 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 c->ctlr = h->ctlr;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700433 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700436/*
437 * Frees a command block that was previously allocated with cmd_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
439static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
440{
441 int i;
442 u64bit temp64;
443
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700444 if (!got_from_pool) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 temp64.val32.lower = c->ErrDesc.Addr.lower;
446 temp64.val32.upper = c->ErrDesc.Addr.upper;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700447 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
448 c->err_info, (dma_addr_t) temp64.val);
449 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
450 c, (dma_addr_t) c->busaddr);
451 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 i = c - h->cmd_pool;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700453 clear_bit(i & (BITS_PER_LONG - 1),
454 h->cmd_pool_bits + (i / BITS_PER_LONG));
455 h->nr_frees++;
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459static inline ctlr_info_t *get_host(struct gendisk *disk)
460{
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700461 return disk->queue->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464static inline drive_info_struct *get_drv(struct gendisk *disk)
465{
466 return disk->private_data;
467}
468
469/*
470 * Open. Make sure the device is really there.
471 */
472static int cciss_open(struct inode *inode, struct file *filep)
473{
474 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
475 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
476
477#ifdef CCISS_DEBUG
478 printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700479#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Mike Millerddd47442005-09-13 01:25:22 -0700481 if (host->busy_initializing || drv->busy_configuring)
482 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /*
484 * Root is allowed to open raw volume zero even if it's not configured
485 * so array config can still work. Root is also allowed to open any
486 * volume that has a LUN ID, so it can issue IOCTL to reread the
487 * disk information. I don't think I really like this
488 * but I'm already using way to many device nodes to claim another one
489 * for "raw controller".
490 */
491 if (drv->nr_blocks == 0) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700492 if (iminor(inode) != 0) { /* not node 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* if not node 0 make sure it is a partition = 0 */
494 if (iminor(inode) & 0x0f) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700495 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* if it is, make sure we have a LUN ID */
497 } else if (drv->LunID == 0) {
498 return -ENXIO;
499 }
500 }
501 if (!capable(CAP_SYS_ADMIN))
502 return -EPERM;
503 }
504 drv->usage_count++;
505 host->usage_count++;
506 return 0;
507}
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/*
510 * Close. Sync first.
511 */
512static int cciss_release(struct inode *inode, struct file *filep)
513{
514 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
515 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
516
517#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700518 printk(KERN_DEBUG "cciss_release %s\n",
519 inode->i_bdev->bd_disk->disk_name);
520#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 drv->usage_count--;
523 host->usage_count--;
524 return 0;
525}
526
527#ifdef CONFIG_COMPAT
528
529static int do_ioctl(struct file *f, unsigned cmd, unsigned long arg)
530{
531 int ret;
532 lock_kernel();
533 ret = cciss_ioctl(f->f_dentry->d_inode, f, cmd, arg);
534 unlock_kernel();
535 return ret;
536}
537
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700538static int cciss_ioctl32_passthru(struct file *f, unsigned cmd,
539 unsigned long arg);
540static int cciss_ioctl32_big_passthru(struct file *f, unsigned cmd,
541 unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg)
544{
545 switch (cmd) {
546 case CCISS_GETPCIINFO:
547 case CCISS_GETINTINFO:
548 case CCISS_SETINTINFO:
549 case CCISS_GETNODENAME:
550 case CCISS_SETNODENAME:
551 case CCISS_GETHEARTBEAT:
552 case CCISS_GETBUSTYPES:
553 case CCISS_GETFIRMVER:
554 case CCISS_GETDRIVVER:
555 case CCISS_REVALIDVOLS:
556 case CCISS_DEREGDISK:
557 case CCISS_REGNEWDISK:
558 case CCISS_REGNEWD:
559 case CCISS_RESCANDISK:
560 case CCISS_GETLUNINFO:
561 return do_ioctl(f, cmd, arg);
562
563 case CCISS_PASSTHRU32:
564 return cciss_ioctl32_passthru(f, cmd, arg);
565 case CCISS_BIG_PASSTHRU32:
566 return cciss_ioctl32_big_passthru(f, cmd, arg);
567
568 default:
569 return -ENOIOCTLCMD;
570 }
571}
572
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700573static int cciss_ioctl32_passthru(struct file *f, unsigned cmd,
574 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 IOCTL32_Command_struct __user *arg32 =
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700577 (IOCTL32_Command_struct __user *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 IOCTL_Command_struct arg64;
579 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
580 int err;
581 u32 cp;
582
583 err = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700584 err |=
585 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
586 sizeof(arg64.LUN_info));
587 err |=
588 copy_from_user(&arg64.Request, &arg32->Request,
589 sizeof(arg64.Request));
590 err |=
591 copy_from_user(&arg64.error_info, &arg32->error_info,
592 sizeof(arg64.error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 err |= get_user(arg64.buf_size, &arg32->buf_size);
594 err |= get_user(cp, &arg32->buf);
595 arg64.buf = compat_ptr(cp);
596 err |= copy_to_user(p, &arg64, sizeof(arg64));
597
598 if (err)
599 return -EFAULT;
600
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700601 err = do_ioctl(f, CCISS_PASSTHRU, (unsigned long)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (err)
603 return err;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700604 err |=
605 copy_in_user(&arg32->error_info, &p->error_info,
606 sizeof(arg32->error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (err)
608 return -EFAULT;
609 return err;
610}
611
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700612static int cciss_ioctl32_big_passthru(struct file *file, unsigned cmd,
613 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 BIG_IOCTL32_Command_struct __user *arg32 =
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700616 (BIG_IOCTL32_Command_struct __user *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 BIG_IOCTL_Command_struct arg64;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700618 BIG_IOCTL_Command_struct __user *p =
619 compat_alloc_user_space(sizeof(arg64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int err;
621 u32 cp;
622
623 err = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700624 err |=
625 copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
626 sizeof(arg64.LUN_info));
627 err |=
628 copy_from_user(&arg64.Request, &arg32->Request,
629 sizeof(arg64.Request));
630 err |=
631 copy_from_user(&arg64.error_info, &arg32->error_info,
632 sizeof(arg64.error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 err |= get_user(arg64.buf_size, &arg32->buf_size);
634 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
635 err |= get_user(cp, &arg32->buf);
636 arg64.buf = compat_ptr(cp);
637 err |= copy_to_user(p, &arg64, sizeof(arg64));
638
639 if (err)
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700640 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700642 err = do_ioctl(file, CCISS_BIG_PASSTHRU, (unsigned long)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (err)
644 return err;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700645 err |=
646 copy_in_user(&arg32->error_info, &p->error_info,
647 sizeof(arg32->error_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (err)
649 return -EFAULT;
650 return err;
651}
652#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800653
654static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo)
655{
656 drive_info_struct *drv = get_drv(bdev->bd_disk);
657
658 if (!drv->cylinders)
659 return -ENXIO;
660
661 geo->heads = drv->heads;
662 geo->sectors = drv->sectors;
663 geo->cylinders = drv->cylinders;
664 return 0;
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/*
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700668 * ioctl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700670static int cciss_ioctl(struct inode *inode, struct file *filep,
671 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct block_device *bdev = inode->i_bdev;
674 struct gendisk *disk = bdev->bd_disk;
675 ctlr_info_t *host = get_host(disk);
676 drive_info_struct *drv = get_drv(disk);
677 int ctlr = host->ctlr;
678 void __user *argp = (void __user *)arg;
679
680#ifdef CCISS_DEBUG
681 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700682#endif /* CCISS_DEBUG */
683
684 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 case CCISS_GETPCIINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700687 cciss_pci_info_struct pciinfo;
688
689 if (!arg)
690 return -EINVAL;
691 pciinfo.domain = pci_domain_nr(host->pdev->bus);
692 pciinfo.bus = host->pdev->bus->number;
693 pciinfo.dev_fn = host->pdev->devfn;
694 pciinfo.board_id = host->board_id;
695 if (copy_to_user
696 (argp, &pciinfo, sizeof(cciss_pci_info_struct)))
697 return -EFAULT;
698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700700 case CCISS_GETINTINFO:
701 {
702 cciss_coalint_struct intinfo;
703 if (!arg)
704 return -EINVAL;
705 intinfo.delay =
706 readl(&host->cfgtable->HostWrite.CoalIntDelay);
707 intinfo.count =
708 readl(&host->cfgtable->HostWrite.CoalIntCount);
709 if (copy_to_user
710 (argp, &intinfo, sizeof(cciss_coalint_struct)))
711 return -EFAULT;
712 return 0;
713 }
714 case CCISS_SETINTINFO:
715 {
716 cciss_coalint_struct intinfo;
717 unsigned long flags;
718 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700720 if (!arg)
721 return -EINVAL;
722 if (!capable(CAP_SYS_ADMIN))
723 return -EPERM;
724 if (copy_from_user
725 (&intinfo, argp, sizeof(cciss_coalint_struct)))
726 return -EFAULT;
727 if ((intinfo.delay == 0) && (intinfo.count == 0))
728 {
729// printk("cciss_ioctl: delay and count cannot be 0\n");
730 return -EINVAL;
731 }
732 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
733 /* Update the field, and then ring the doorbell */
734 writel(intinfo.delay,
735 &(host->cfgtable->HostWrite.CoalIntDelay));
736 writel(intinfo.count,
737 &(host->cfgtable->HostWrite.CoalIntCount));
738 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
739
740 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
741 if (!(readl(host->vaddr + SA5_DOORBELL)
742 & CFGTBL_ChangeReq))
743 break;
744 /* delay and try again */
745 udelay(1000);
746 }
747 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
748 if (i >= MAX_IOCTL_CONFIG_WAIT)
749 return -EAGAIN;
750 return 0;
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 case CCISS_GETNODENAME:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700753 {
754 NodeName_type NodeName;
755 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700757 if (!arg)
758 return -EINVAL;
759 for (i = 0; i < 16; i++)
760 NodeName[i] =
761 readb(&host->cfgtable->ServerName[i]);
762 if (copy_to_user(argp, NodeName, sizeof(NodeName_type)))
763 return -EFAULT;
764 return 0;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 case CCISS_SETNODENAME:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700767 {
768 NodeName_type NodeName;
769 unsigned long flags;
770 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700772 if (!arg)
773 return -EINVAL;
774 if (!capable(CAP_SYS_ADMIN))
775 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700777 if (copy_from_user
778 (NodeName, argp, sizeof(NodeName_type)))
779 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700781 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700783 /* Update the field, and then ring the doorbell */
784 for (i = 0; i < 16; i++)
785 writeb(NodeName[i],
786 &host->cfgtable->ServerName[i]);
787
788 writel(CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
789
790 for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
791 if (!(readl(host->vaddr + SA5_DOORBELL)
792 & CFGTBL_ChangeReq))
793 break;
794 /* delay and try again */
795 udelay(1000);
796 }
797 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
798 if (i >= MAX_IOCTL_CONFIG_WAIT)
799 return -EAGAIN;
800 return 0;
801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 case CCISS_GETHEARTBEAT:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700804 {
805 Heartbeat_type heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700807 if (!arg)
808 return -EINVAL;
809 heartbeat = readl(&host->cfgtable->HeartBeat);
810 if (copy_to_user
811 (argp, &heartbeat, sizeof(Heartbeat_type)))
812 return -EFAULT;
813 return 0;
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 case CCISS_GETBUSTYPES:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700816 {
817 BusTypes_type BusTypes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700819 if (!arg)
820 return -EINVAL;
821 BusTypes = readl(&host->cfgtable->BusTypes);
822 if (copy_to_user
823 (argp, &BusTypes, sizeof(BusTypes_type)))
824 return -EFAULT;
825 return 0;
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 case CCISS_GETFIRMVER:
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700828 {
829 FirmwareVer_type firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700831 if (!arg)
832 return -EINVAL;
833 memcpy(firmware, host->firm_ver, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700835 if (copy_to_user
836 (argp, firmware, sizeof(FirmwareVer_type)))
837 return -EFAULT;
838 return 0;
839 }
840 case CCISS_GETDRIVVER:
841 {
842 DriverVer_type DriverVer = DRIVER_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700844 if (!arg)
845 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700847 if (copy_to_user
848 (argp, &DriverVer, sizeof(DriverVer_type)))
849 return -EFAULT;
850 return 0;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 case CCISS_REVALIDVOLS:
854 if (bdev != bdev->bd_contains || drv != host->drv)
855 return -ENXIO;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700856 return revalidate_allvol(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700858 case CCISS_GETLUNINFO:{
859 LogvolInfo_struct luninfo;
860
861 luninfo.LunID = drv->LunID;
862 luninfo.num_opens = drv->usage_count;
863 luninfo.num_parts = 0;
864 if (copy_to_user(argp, &luninfo,
865 sizeof(LogvolInfo_struct)))
866 return -EFAULT;
867 return 0;
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 case CCISS_DEREGDISK:
Mike Millerddd47442005-09-13 01:25:22 -0700870 return rebuild_lun_table(host, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 case CCISS_REGNEWD:
Mike Millerddd47442005-09-13 01:25:22 -0700873 return rebuild_lun_table(host, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 case CCISS_PASSTHRU:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700877 IOCTL_Command_struct iocommand;
878 CommandList_struct *c;
879 char *buff = NULL;
880 u64bit temp64;
881 unsigned long flags;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700882 DECLARE_COMPLETION_ONSTACK(wait);
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700883
884 if (!arg)
885 return -EINVAL;
886
887 if (!capable(CAP_SYS_RAWIO))
888 return -EPERM;
889
890 if (copy_from_user
891 (&iocommand, argp, sizeof(IOCTL_Command_struct)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -EFAULT;
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700893 if ((iocommand.buf_size < 1) &&
894 (iocommand.Request.Type.Direction != XFER_NONE)) {
895 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700897#if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
898 /* Check kmalloc limits */
899 if (iocommand.buf_size > 128000)
900 return -EINVAL;
901#endif
902 if (iocommand.buf_size > 0) {
903 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
904 if (buff == NULL)
905 return -EFAULT;
906 }
907 if (iocommand.Request.Type.Direction == XFER_WRITE) {
908 /* Copy the data into the buffer we created */
909 if (copy_from_user
910 (buff, iocommand.buf, iocommand.buf_size)) {
911 kfree(buff);
912 return -EFAULT;
913 }
914 } else {
915 memset(buff, 0, iocommand.buf_size);
916 }
917 if ((c = cmd_alloc(host, 0)) == NULL) {
918 kfree(buff);
919 return -ENOMEM;
920 }
921 // Fill in the command type
922 c->cmd_type = CMD_IOCTL_PEND;
923 // Fill in Command Header
924 c->Header.ReplyQueue = 0; // unused in simple mode
925 if (iocommand.buf_size > 0) // buffer to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 {
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700927 c->Header.SGList = 1;
928 c->Header.SGTotal = 1;
929 } else // no buffers to fill
930 {
931 c->Header.SGList = 0;
932 c->Header.SGTotal = 0;
933 }
934 c->Header.LUN = iocommand.LUN_info;
935 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
936
937 // Fill in Request block
938 c->Request = iocommand.Request;
939
940 // Fill in the scatter gather information
941 if (iocommand.buf_size > 0) {
942 temp64.val = pci_map_single(host->pdev, buff,
943 iocommand.buf_size,
944 PCI_DMA_BIDIRECTIONAL);
945 c->SG[0].Addr.lower = temp64.val32.lower;
946 c->SG[0].Addr.upper = temp64.val32.upper;
947 c->SG[0].Len = iocommand.buf_size;
948 c->SG[0].Ext = 0; // we are not chaining
949 }
950 c->waiting = &wait;
951
952 /* Put the request on the tail of the request queue */
953 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
954 addQ(&host->reqQ, c);
955 host->Qdepth++;
956 start_io(host);
957 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
958
959 wait_for_completion(&wait);
960
961 /* unlock the buffers from DMA */
962 temp64.val32.lower = c->SG[0].Addr.lower;
963 temp64.val32.upper = c->SG[0].Addr.upper;
964 pci_unmap_single(host->pdev, (dma_addr_t) temp64.val,
965 iocommand.buf_size,
966 PCI_DMA_BIDIRECTIONAL);
967
968 /* Copy the error information out */
969 iocommand.error_info = *(c->err_info);
970 if (copy_to_user
971 (argp, &iocommand, sizeof(IOCTL_Command_struct))) {
972 kfree(buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 cmd_free(host, c, 0);
974 return -EFAULT;
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700977 if (iocommand.Request.Type.Direction == XFER_READ) {
978 /* Copy the data out of the buffer we created */
979 if (copy_to_user
980 (iocommand.buf, buff, iocommand.buf_size)) {
981 kfree(buff);
982 cmd_free(host, c, 0);
983 return -EFAULT;
984 }
985 }
986 kfree(buff);
987 cmd_free(host, c, 0);
988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -0700990 case CCISS_BIG_PASSTHRU:{
991 BIG_IOCTL_Command_struct *ioc;
992 CommandList_struct *c;
993 unsigned char **buff = NULL;
994 int *buff_size = NULL;
995 u64bit temp64;
996 unsigned long flags;
997 BYTE sg_used = 0;
998 int status = 0;
999 int i;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001000 DECLARE_COMPLETION_ONSTACK(wait);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001001 __u32 left;
1002 __u32 sz;
1003 BYTE __user *data_ptr;
1004
1005 if (!arg)
1006 return -EINVAL;
1007 if (!capable(CAP_SYS_RAWIO))
1008 return -EPERM;
1009 ioc = (BIG_IOCTL_Command_struct *)
1010 kmalloc(sizeof(*ioc), GFP_KERNEL);
1011 if (!ioc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 status = -ENOMEM;
1013 goto cleanup1;
1014 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001015 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
1016 status = -EFAULT;
1017 goto cleanup1;
1018 }
1019 if ((ioc->buf_size < 1) &&
1020 (ioc->Request.Type.Direction != XFER_NONE)) {
1021 status = -EINVAL;
1022 goto cleanup1;
1023 }
1024 /* Check kmalloc limits using all SGs */
1025 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
1026 status = -EINVAL;
1027 goto cleanup1;
1028 }
1029 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
1030 status = -EINVAL;
1031 goto cleanup1;
1032 }
1033 buff =
1034 kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
1035 if (!buff) {
1036 status = -ENOMEM;
1037 goto cleanup1;
1038 }
1039 buff_size = (int *)kmalloc(MAXSGENTRIES * sizeof(int),
1040 GFP_KERNEL);
1041 if (!buff_size) {
1042 status = -ENOMEM;
1043 goto cleanup1;
1044 }
1045 left = ioc->buf_size;
1046 data_ptr = ioc->buf;
1047 while (left) {
1048 sz = (left >
1049 ioc->malloc_size) ? ioc->
1050 malloc_size : left;
1051 buff_size[sg_used] = sz;
1052 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
1053 if (buff[sg_used] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 status = -ENOMEM;
Jens Axboe15534d32005-11-18 22:02:44 +01001055 goto cleanup1;
1056 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001057 if (ioc->Request.Type.Direction == XFER_WRITE) {
1058 if (copy_from_user
1059 (buff[sg_used], data_ptr, sz)) {
1060 status = -ENOMEM;
1061 goto cleanup1;
1062 }
1063 } else {
1064 memset(buff[sg_used], 0, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001066 left -= sz;
1067 data_ptr += sz;
1068 sg_used++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001070 if ((c = cmd_alloc(host, 0)) == NULL) {
1071 status = -ENOMEM;
1072 goto cleanup1;
1073 }
1074 c->cmd_type = CMD_IOCTL_PEND;
1075 c->Header.ReplyQueue = 0;
1076
1077 if (ioc->buf_size > 0) {
1078 c->Header.SGList = sg_used;
1079 c->Header.SGTotal = sg_used;
1080 } else {
1081 c->Header.SGList = 0;
1082 c->Header.SGTotal = 0;
1083 }
1084 c->Header.LUN = ioc->LUN_info;
1085 c->Header.Tag.lower = c->busaddr;
1086
1087 c->Request = ioc->Request;
1088 if (ioc->buf_size > 0) {
1089 int i;
1090 for (i = 0; i < sg_used; i++) {
1091 temp64.val =
1092 pci_map_single(host->pdev, buff[i],
1093 buff_size[i],
1094 PCI_DMA_BIDIRECTIONAL);
1095 c->SG[i].Addr.lower =
1096 temp64.val32.lower;
1097 c->SG[i].Addr.upper =
1098 temp64.val32.upper;
1099 c->SG[i].Len = buff_size[i];
1100 c->SG[i].Ext = 0; /* we are not chaining */
1101 }
1102 }
1103 c->waiting = &wait;
1104 /* Put the request on the tail of the request queue */
1105 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1106 addQ(&host->reqQ, c);
1107 host->Qdepth++;
1108 start_io(host);
1109 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1110 wait_for_completion(&wait);
1111 /* unlock the buffers from DMA */
1112 for (i = 0; i < sg_used; i++) {
1113 temp64.val32.lower = c->SG[i].Addr.lower;
1114 temp64.val32.upper = c->SG[i].Addr.upper;
1115 pci_unmap_single(host->pdev,
1116 (dma_addr_t) temp64.val, buff_size[i],
1117 PCI_DMA_BIDIRECTIONAL);
1118 }
1119 /* Copy the error information out */
1120 ioc->error_info = *(c->err_info);
1121 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
1122 cmd_free(host, c, 0);
1123 status = -EFAULT;
1124 goto cleanup1;
1125 }
1126 if (ioc->Request.Type.Direction == XFER_READ) {
1127 /* Copy the data out of the buffer we created */
1128 BYTE __user *ptr = ioc->buf;
1129 for (i = 0; i < sg_used; i++) {
1130 if (copy_to_user
1131 (ptr, buff[i], buff_size[i])) {
1132 cmd_free(host, c, 0);
1133 status = -EFAULT;
1134 goto cleanup1;
1135 }
1136 ptr += buff_size[i];
1137 }
1138 }
1139 cmd_free(host, c, 0);
1140 status = 0;
1141 cleanup1:
1142 if (buff) {
1143 for (i = 0; i < sg_used; i++)
1144 kfree(buff[i]);
1145 kfree(buff);
1146 }
1147 kfree(buff_size);
1148 kfree(ioc);
1149 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 default:
1152 return -ENOTTY;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156/*
1157 * revalidate_allvol is for online array config utilities. After a
1158 * utility reconfigures the drives in the array, it can use this function
1159 * (through an ioctl) to make the driver zap any previous disk structs for
1160 * that controller and get new ones.
1161 *
1162 * Right now I'm using the getgeometry() function to do this, but this
1163 * function should probably be finer grained and allow you to revalidate one
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001164 * particular logical volume (instead of all of them on a particular
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 * controller).
1166 */
1167static int revalidate_allvol(ctlr_info_t *host)
1168{
1169 int ctlr = host->ctlr, i;
1170 unsigned long flags;
1171
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001172 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1173 if (host->usage_count > 1) {
1174 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1175 printk(KERN_WARNING "cciss: Device busy for volume"
1176 " revalidation (usage=%d)\n", host->usage_count);
1177 return -EBUSY;
1178 }
1179 host->usage_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1181
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001182 for (i = 0; i < NWD; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct gendisk *disk = host->gendisk[i];
mikem6f5a0f72005-11-18 22:05:36 +01001184 if (disk) {
1185 request_queue_t *q = disk->queue;
1186
1187 if (disk->flags & GENHD_FL_UP)
1188 del_gendisk(disk);
1189 if (q)
1190 blk_cleanup_queue(q);
mikem6f5a0f72005-11-18 22:05:36 +01001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001194 /*
1195 * Set the partition and block size structures for all volumes
1196 * on this controller to zero. We will reread all of this data
1197 */
1198 memset(host->drv, 0, sizeof(drive_info_struct)
1199 * CISS_MAX_LUN);
1200 /*
1201 * Tell the array controller not to give us any interrupts while
1202 * we check the new geometry. Then turn interrupts back on when
1203 * we're done.
1204 */
1205 host->access.set_intr_mask(host, CCISS_INTR_OFF);
1206 cciss_getgeometry(ctlr);
1207 host->access.set_intr_mask(host, CCISS_INTR_ON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001209 /* Loop through each real device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 for (i = 0; i < NWD; i++) {
1211 struct gendisk *disk = host->gendisk[i];
1212 drive_info_struct *drv = &(host->drv[i]);
1213 /* we must register the controller even if no disks exist */
1214 /* this is for the online array utilities */
1215 if (!drv->heads && i)
1216 continue;
Mike Millerad2b9312005-07-28 01:07:31 -07001217 blk_queue_hardsect_size(drv->queue, drv->block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 set_capacity(disk, drv->nr_blocks);
1219 add_disk(disk);
1220 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001221 host->usage_count--;
1222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Mike Millerca1e0482006-04-10 15:38:07 -07001225static inline void complete_buffers(struct bio *bio, int status)
1226{
1227 while (bio) {
1228 struct bio *xbh = bio->bi_next;
1229 int nr_sectors = bio_sectors(bio);
1230
1231 bio->bi_next = NULL;
Mike Millerca1e0482006-04-10 15:38:07 -07001232 bio_endio(bio, nr_sectors << 9, status ? 0 : -EIO);
1233 bio = xbh;
1234 }
Mike Millerca1e0482006-04-10 15:38:07 -07001235}
1236
Jens Axboe7b30f092006-07-25 15:02:48 +02001237static void cciss_check_queues(ctlr_info_t *h)
1238{
1239 int start_queue = h->next_to_run;
1240 int i;
1241
1242 /* check to see if we have maxed out the number of commands that can
1243 * be placed on the queue. If so then exit. We do this check here
1244 * in case the interrupt we serviced was from an ioctl and did not
1245 * free any new commands.
1246 */
1247 if ((find_first_zero_bit(h->cmd_pool_bits, NR_CMDS)) == NR_CMDS)
1248 return;
1249
1250 /* We have room on the queue for more commands. Now we need to queue
1251 * them up. We will also keep track of the next queue to run so
1252 * that every queue gets a chance to be started first.
1253 */
1254 for (i = 0; i < h->highest_lun + 1; i++) {
1255 int curr_queue = (start_queue + i) % (h->highest_lun + 1);
1256 /* make sure the disk has been added and the drive is real
1257 * because this can be called from the middle of init_one.
1258 */
1259 if (!(h->drv[curr_queue].queue) || !(h->drv[curr_queue].heads))
1260 continue;
1261 blk_start_queue(h->gendisk[curr_queue]->queue);
1262
1263 /* check to see if we have maxed out the number of commands
1264 * that can be placed on the queue.
1265 */
1266 if ((find_first_zero_bit(h->cmd_pool_bits, NR_CMDS)) == NR_CMDS) {
1267 if (curr_queue == start_queue) {
1268 h->next_to_run =
1269 (start_queue + 1) % (h->highest_lun + 1);
1270 break;
1271 } else {
1272 h->next_to_run = curr_queue;
1273 break;
1274 }
1275 } else {
1276 curr_queue = (curr_queue + 1) % (h->highest_lun + 1);
1277 }
1278 }
1279}
1280
Mike Millerca1e0482006-04-10 15:38:07 -07001281static void cciss_softirq_done(struct request *rq)
1282{
1283 CommandList_struct *cmd = rq->completion_data;
1284 ctlr_info_t *h = hba[cmd->ctlr];
1285 unsigned long flags;
1286 u64bit temp64;
1287 int i, ddir;
1288
1289 if (cmd->Request.Type.Direction == XFER_READ)
1290 ddir = PCI_DMA_FROMDEVICE;
1291 else
1292 ddir = PCI_DMA_TODEVICE;
1293
1294 /* command did not need to be retried */
1295 /* unmap the DMA mapping for all the scatter gather elements */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001296 for (i = 0; i < cmd->Header.SGList; i++) {
Mike Millerca1e0482006-04-10 15:38:07 -07001297 temp64.val32.lower = cmd->SG[i].Addr.lower;
1298 temp64.val32.upper = cmd->SG[i].Addr.upper;
1299 pci_unmap_page(h->pdev, temp64.val, cmd->SG[i].Len, ddir);
1300 }
1301
1302 complete_buffers(rq->bio, rq->errors);
1303
Jens Axboeb369c2c2006-11-14 12:36:03 +01001304 if (blk_fs_request(rq)) {
1305 const int rw = rq_data_dir(rq);
1306
1307 disk_stat_add(rq->rq_disk, sectors[rw], rq->nr_sectors);
1308 }
1309
Mike Millerca1e0482006-04-10 15:38:07 -07001310#ifdef CCISS_DEBUG
1311 printk("Done with %p\n", rq);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001312#endif /* CCISS_DEBUG */
Mike Millerca1e0482006-04-10 15:38:07 -07001313
Matt Mackall8bd0b972006-06-25 05:47:11 -07001314 add_disk_randomness(rq->rq_disk);
Mike Millerca1e0482006-04-10 15:38:07 -07001315 spin_lock_irqsave(&h->lock, flags);
1316 end_that_request_last(rq, rq->errors);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001317 cmd_free(h, cmd, 1);
Jens Axboe7b30f092006-07-25 15:02:48 +02001318 cciss_check_queues(h);
Mike Millerca1e0482006-04-10 15:38:07 -07001319 spin_unlock_irqrestore(&h->lock, flags);
1320}
1321
Mike Millerddd47442005-09-13 01:25:22 -07001322/* This function will check the usage_count of the drive to be updated/added.
1323 * If the usage_count is zero then the drive information will be updated and
1324 * the disk will be re-registered with the kernel. If not then it will be
1325 * left alone for the next reboot. The exception to this is disk 0 which
1326 * will always be left registered with the kernel since it is also the
1327 * controller node. Any changes to disk 0 will show up on the next
1328 * reboot.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001329 */
Mike Millerddd47442005-09-13 01:25:22 -07001330static void cciss_update_drive_info(int ctlr, int drv_index)
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001331{
Mike Millerddd47442005-09-13 01:25:22 -07001332 ctlr_info_t *h = hba[ctlr];
1333 struct gendisk *disk;
Mike Millerddd47442005-09-13 01:25:22 -07001334 InquiryData_struct *inq_buff = NULL;
1335 unsigned int block_size;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001336 sector_t total_size;
Mike Millerddd47442005-09-13 01:25:22 -07001337 unsigned long flags = 0;
1338 int ret = 0;
1339
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001340 /* if the disk already exists then deregister it before proceeding */
1341 if (h->drv[drv_index].raid_level != -1) {
Mike Millerddd47442005-09-13 01:25:22 -07001342 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
1343 h->drv[drv_index].busy_configuring = 1;
1344 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1345 ret = deregister_disk(h->gendisk[drv_index],
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001346 &h->drv[drv_index], 0);
Mike Millerddd47442005-09-13 01:25:22 -07001347 h->drv[drv_index].busy_configuring = 0;
1348 }
1349
1350 /* If the disk is in use return */
1351 if (ret)
1352 return;
1353
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001354 /* Get information about the disk and modify the driver structure */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001355 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
Mike Millerddd47442005-09-13 01:25:22 -07001356 if (inq_buff == NULL)
1357 goto mem_msg;
1358
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001359 cciss_read_capacity(ctlr, drv_index, 1,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001360 &total_size, &block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001361
1362 /* total size = last LBA + 1 */
1363 /* FFFFFFFF + 1 = 0, cannot have a logical volume of size 0 */
1364 /* so we assume this volume this must be >2TB in size */
1365 if (total_size == (__u32) 0) {
1366 cciss_read_capacity_16(ctlr, drv_index, 1,
1367 &total_size, &block_size);
1368 h->cciss_read = CCISS_READ_16;
1369 h->cciss_write = CCISS_WRITE_16;
1370 } else {
1371 h->cciss_read = CCISS_READ_10;
1372 h->cciss_write = CCISS_WRITE_10;
1373 }
Mike Millerddd47442005-09-13 01:25:22 -07001374 cciss_geometry_inquiry(ctlr, drv_index, 1, total_size, block_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001375 inq_buff, &h->drv[drv_index]);
Mike Millerddd47442005-09-13 01:25:22 -07001376
1377 ++h->num_luns;
1378 disk = h->gendisk[drv_index];
1379 set_capacity(disk, h->drv[drv_index].nr_blocks);
1380
Mike Millerddd47442005-09-13 01:25:22 -07001381 /* if it's the controller it's already added */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001382 if (drv_index) {
Mike Millerddd47442005-09-13 01:25:22 -07001383 disk->queue = blk_init_queue(do_cciss_request, &h->lock);
1384
1385 /* Set up queue information */
1386 disk->queue->backing_dev_info.ra_pages = READ_AHEAD;
1387 blk_queue_bounce_limit(disk->queue, hba[ctlr]->pdev->dma_mask);
1388
1389 /* This is a hardware imposed limit. */
1390 blk_queue_max_hw_segments(disk->queue, MAXSGENTRIES);
1391
1392 /* This is a limit in the driver and could be eliminated. */
1393 blk_queue_max_phys_segments(disk->queue, MAXSGENTRIES);
1394
1395 blk_queue_max_sectors(disk->queue, 512);
1396
Mike Millerca1e0482006-04-10 15:38:07 -07001397 blk_queue_softirq_done(disk->queue, cciss_softirq_done);
1398
Mike Millerddd47442005-09-13 01:25:22 -07001399 disk->queue->queuedata = hba[ctlr];
1400
1401 blk_queue_hardsect_size(disk->queue,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001402 hba[ctlr]->drv[drv_index].block_size);
Mike Millerddd47442005-09-13 01:25:22 -07001403
1404 h->drv[drv_index].queue = disk->queue;
1405 add_disk(disk);
1406 }
1407
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001408 freeret:
Mike Millerddd47442005-09-13 01:25:22 -07001409 kfree(inq_buff);
1410 return;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001411 mem_msg:
Mike Millerddd47442005-09-13 01:25:22 -07001412 printk(KERN_ERR "cciss: out of memory\n");
1413 goto freeret;
1414}
1415
1416/* This function will find the first index of the controllers drive array
1417 * that has a -1 for the raid_level and will return that index. This is
1418 * where new drives will be added. If the index to be returned is greater
1419 * than the highest_lun index for the controller then highest_lun is set
1420 * to this new index. If there are no available indexes then -1 is returned.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001421 */
Mike Millerddd47442005-09-13 01:25:22 -07001422static int cciss_find_free_drive_index(int ctlr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Mike Millerddd47442005-09-13 01:25:22 -07001424 int i;
1425
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001426 for (i = 0; i < CISS_MAX_LUN; i++) {
1427 if (hba[ctlr]->drv[i].raid_level == -1) {
Mike Millerddd47442005-09-13 01:25:22 -07001428 if (i > hba[ctlr]->highest_lun)
1429 hba[ctlr]->highest_lun = i;
1430 return i;
1431 }
1432 }
1433 return -1;
1434}
1435
1436/* This function will add and remove logical drives from the Logical
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001437 * drive array of the controller and maintain persistency of ordering
Mike Millerddd47442005-09-13 01:25:22 -07001438 * so that mount points are preserved until the next reboot. This allows
1439 * for the removal of logical drives in the middle of the drive array
1440 * without a re-ordering of those drives.
1441 * INPUT
1442 * h = The controller to perform the operations on
1443 * del_disk = The disk to remove if specified. If the value given
1444 * is NULL then no disk is removed.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001445 */
Mike Millerddd47442005-09-13 01:25:22 -07001446static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk)
1447{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 int ctlr = h->ctlr;
Mike Millerddd47442005-09-13 01:25:22 -07001449 int num_luns;
1450 ReportLunData_struct *ld_buff = NULL;
1451 drive_info_struct *drv = NULL;
1452 int return_code;
1453 int listlength = 0;
1454 int i;
1455 int drv_found;
1456 int drv_index = 0;
1457 __u32 lunid = 0;
1458 unsigned long flags;
1459
1460 /* Set busy_configuring flag for this operation */
1461 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001462 if (h->num_luns >= CISS_MAX_LUN) {
Mike Millerddd47442005-09-13 01:25:22 -07001463 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1464 return -EINVAL;
1465 }
1466
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001467 if (h->busy_configuring) {
Mike Millerddd47442005-09-13 01:25:22 -07001468 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1469 return -EBUSY;
1470 }
1471 h->busy_configuring = 1;
1472
1473 /* if del_disk is NULL then we are being called to add a new disk
1474 * and update the logical drive table. If it is not NULL then
1475 * we will check if the disk is in use or not.
1476 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001477 if (del_disk != NULL) {
Mike Millerddd47442005-09-13 01:25:22 -07001478 drv = get_drv(del_disk);
1479 drv->busy_configuring = 1;
1480 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1481 return_code = deregister_disk(del_disk, drv, 1);
1482 drv->busy_configuring = 0;
1483 h->busy_configuring = 0;
1484 return return_code;
1485 } else {
1486 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
1487 if (!capable(CAP_SYS_RAWIO))
1488 return -EPERM;
1489
1490 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1491 if (ld_buff == NULL)
1492 goto mem_msg;
1493
1494 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001495 sizeof(ReportLunData_struct), 0,
1496 0, 0, TYPE_CMD);
Mike Millerddd47442005-09-13 01:25:22 -07001497
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001498 if (return_code == IO_OK) {
1499 listlength |=
1500 (0xff & (unsigned int)(ld_buff->LUNListLength[0]))
1501 << 24;
1502 listlength |=
1503 (0xff & (unsigned int)(ld_buff->LUNListLength[1]))
1504 << 16;
1505 listlength |=
1506 (0xff & (unsigned int)(ld_buff->LUNListLength[2]))
1507 << 8;
1508 listlength |=
1509 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
1510 } else { /* reading number of logical volumes failed */
Mike Millerddd47442005-09-13 01:25:22 -07001511 printk(KERN_WARNING "cciss: report logical volume"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001512 " command failed\n");
Mike Millerddd47442005-09-13 01:25:22 -07001513 listlength = 0;
1514 goto freeret;
1515 }
1516
1517 num_luns = listlength / 8; /* 8 bytes per entry */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001518 if (num_luns > CISS_MAX_LUN) {
Mike Millerddd47442005-09-13 01:25:22 -07001519 num_luns = CISS_MAX_LUN;
1520 printk(KERN_WARNING "cciss: more luns configured"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001521 " on controller than can be handled by"
1522 " this driver.\n");
Mike Millerddd47442005-09-13 01:25:22 -07001523 }
1524
1525 /* Compare controller drive array to drivers drive array.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001526 * Check for updates in the drive information and any new drives
1527 * on the controller.
1528 */
1529 for (i = 0; i < num_luns; i++) {
Mike Millerddd47442005-09-13 01:25:22 -07001530 int j;
1531
1532 drv_found = 0;
1533
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001534 lunid = (0xff &
1535 (unsigned int)(ld_buff->LUN[i][3])) << 24;
1536 lunid |= (0xff &
1537 (unsigned int)(ld_buff->LUN[i][2])) << 16;
1538 lunid |= (0xff &
1539 (unsigned int)(ld_buff->LUN[i][1])) << 8;
1540 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
Mike Millerddd47442005-09-13 01:25:22 -07001541
1542 /* Find if the LUN is already in the drive array
1543 * of the controller. If so then update its info
1544 * if not is use. If it does not exist then find
1545 * the first free index and add it.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001546 */
1547 for (j = 0; j <= h->highest_lun; j++) {
1548 if (h->drv[j].LunID == lunid) {
Mike Millerddd47442005-09-13 01:25:22 -07001549 drv_index = j;
1550 drv_found = 1;
1551 }
1552 }
1553
1554 /* check if the drive was found already in the array */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001555 if (!drv_found) {
Mike Millerddd47442005-09-13 01:25:22 -07001556 drv_index = cciss_find_free_drive_index(ctlr);
1557 if (drv_index == -1)
1558 goto freeret;
1559
1560 }
1561 h->drv[drv_index].LunID = lunid;
1562 cciss_update_drive_info(ctlr, drv_index);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001563 } /* end for */
1564 } /* end else */
Mike Millerddd47442005-09-13 01:25:22 -07001565
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001566 freeret:
Mike Millerddd47442005-09-13 01:25:22 -07001567 kfree(ld_buff);
1568 h->busy_configuring = 0;
1569 /* We return -1 here to tell the ACU that we have registered/updated
1570 * all of the drives that we can and to keep it from calling us
1571 * additional times.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001572 */
Mike Millerddd47442005-09-13 01:25:22 -07001573 return -1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001574 mem_msg:
Mike Millerddd47442005-09-13 01:25:22 -07001575 printk(KERN_ERR "cciss: out of memory\n");
1576 goto freeret;
1577}
1578
1579/* This function will deregister the disk and it's queue from the
1580 * kernel. It must be called with the controller lock held and the
1581 * drv structures busy_configuring flag set. It's parameters are:
1582 *
1583 * disk = This is the disk to be deregistered
1584 * drv = This is the drive_info_struct associated with the disk to be
1585 * deregistered. It contains information about the disk used
1586 * by the driver.
1587 * clear_all = This flag determines whether or not the disk information
1588 * is going to be completely cleared out and the highest_lun
1589 * reset. Sometimes we want to clear out information about
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07001590 * the disk in preparation for re-adding it. In this case
Mike Millerddd47442005-09-13 01:25:22 -07001591 * the highest_lun should be left unchanged and the LunID
1592 * should not be cleared.
1593*/
1594static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
1595 int clear_all)
1596{
1597 ctlr_info_t *h = get_host(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 if (!capable(CAP_SYS_RAWIO))
1600 return -EPERM;
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /* make sure logical volume is NOT is use */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001603 if (clear_all || (h->gendisk[0] == disk)) {
1604 if (drv->usage_count > 1)
1605 return -EBUSY;
1606 } else if (drv->usage_count > 0)
1607 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Mike Millerddd47442005-09-13 01:25:22 -07001609 /* invalidate the devices and deregister the disk. If it is disk
1610 * zero do not deregister it but just zero out it's values. This
1611 * allows us to delete disk zero but keep the controller registered.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001612 */
1613 if (h->gendisk[0] != disk) {
mikem6f5a0f72005-11-18 22:05:36 +01001614 if (disk) {
1615 request_queue_t *q = disk->queue;
1616 if (disk->flags & GENHD_FL_UP)
1617 del_gendisk(disk);
Mike Miller2f6331f2005-12-12 22:17:14 -08001618 if (q) {
mikem6f5a0f72005-11-18 22:05:36 +01001619 blk_cleanup_queue(q);
Mike Miller2f6331f2005-12-12 22:17:14 -08001620 drv->queue = NULL;
1621 }
Mike Millerddd47442005-09-13 01:25:22 -07001622 }
1623 }
1624
1625 --h->num_luns;
1626 /* zero out the disk size info */
1627 drv->nr_blocks = 0;
1628 drv->block_size = 0;
1629 drv->heads = 0;
1630 drv->sectors = 0;
1631 drv->cylinders = 0;
1632 drv->raid_level = -1; /* This can be used as a flag variable to
1633 * indicate that this element of the drive
1634 * array is free.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001635 */
Mike Millerddd47442005-09-13 01:25:22 -07001636
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001637 if (clear_all) {
1638 /* check to see if it was the last disk */
1639 if (drv == h->drv + h->highest_lun) {
1640 /* if so, find the new hightest lun */
1641 int i, newhighest = -1;
1642 for (i = 0; i < h->highest_lun; i++) {
1643 /* if the disk has size > 0, it is available */
Mike Millerddd47442005-09-13 01:25:22 -07001644 if (h->drv[i].heads)
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001645 newhighest = i;
1646 }
1647 h->highest_lun = newhighest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Mike Millerddd47442005-09-13 01:25:22 -07001649
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001650 drv->LunID = 0;
Mike Millerddd47442005-09-13 01:25:22 -07001651 }
Bjorn Helgaase2019b52006-06-25 05:49:05 -07001652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
Mike Millerddd47442005-09-13 01:25:22 -07001654
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001655static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, /* 0: address the controller,
1656 1: address logical volume log_unit,
1657 2: periph device address is scsi3addr */
1658 unsigned int log_unit, __u8 page_code,
1659 unsigned char *scsi3addr, int cmd_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001661 ctlr_info_t *h = hba[ctlr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 u64bit buff_dma_handle;
1663 int status = IO_OK;
1664
1665 c->cmd_type = CMD_IOCTL_PEND;
1666 c->Header.ReplyQueue = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001667 if (buff != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 c->Header.SGList = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001669 c->Header.SGTotal = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 } else {
1671 c->Header.SGList = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001672 c->Header.SGTotal = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674 c->Header.Tag.lower = c->busaddr;
1675
1676 c->Request.Type.Type = cmd_type;
1677 if (cmd_type == TYPE_CMD) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001678 switch (cmd) {
1679 case CISS_INQUIRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /* If the logical unit number is 0 then, this is going
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001681 to controller so It's a physical command
1682 mode = 0 target = 0. So we have nothing to write.
1683 otherwise, if use_unit_num == 1,
1684 mode = 1(volume set addressing) target = LUNID
1685 otherwise, if use_unit_num == 2,
1686 mode = 0(periph dev addr) target = scsi3addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (use_unit_num == 1) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001688 c->Header.LUN.LogDev.VolId =
1689 h->drv[log_unit].LunID;
1690 c->Header.LUN.LogDev.Mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 } else if (use_unit_num == 2) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001692 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr,
1693 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 c->Header.LUN.LogDev.Mode = 0;
1695 }
1696 /* are we trying to read a vital product page */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001697 if (page_code != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 c->Request.CDB[1] = 0x01;
1699 c->Request.CDB[2] = page_code;
1700 }
1701 c->Request.CDBLen = 6;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001702 c->Request.Type.Attribute = ATTR_SIMPLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 c->Request.Type.Direction = XFER_READ;
1704 c->Request.Timeout = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001705 c->Request.CDB[0] = CISS_INQUIRY;
1706 c->Request.CDB[4] = size & 0xFF;
1707 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 case CISS_REPORT_LOG:
1709 case CISS_REPORT_PHYS:
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001710 /* Talking to controller so It's a physical command
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 mode = 00 target = 0. Nothing to write.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001712 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 c->Request.CDBLen = 12;
1714 c->Request.Type.Attribute = ATTR_SIMPLE;
1715 c->Request.Type.Direction = XFER_READ;
1716 c->Request.Timeout = 0;
1717 c->Request.CDB[0] = cmd;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001718 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 c->Request.CDB[7] = (size >> 16) & 0xFF;
1720 c->Request.CDB[8] = (size >> 8) & 0xFF;
1721 c->Request.CDB[9] = size & 0xFF;
1722 break;
1723
1724 case CCISS_READ_CAPACITY:
1725 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1726 c->Header.LUN.LogDev.Mode = 1;
1727 c->Request.CDBLen = 10;
1728 c->Request.Type.Attribute = ATTR_SIMPLE;
1729 c->Request.Type.Direction = XFER_READ;
1730 c->Request.Timeout = 0;
1731 c->Request.CDB[0] = cmd;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001732 break;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001733 case CCISS_READ_CAPACITY_16:
1734 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1735 c->Header.LUN.LogDev.Mode = 1;
1736 c->Request.CDBLen = 16;
1737 c->Request.Type.Attribute = ATTR_SIMPLE;
1738 c->Request.Type.Direction = XFER_READ;
1739 c->Request.Timeout = 0;
1740 c->Request.CDB[0] = cmd;
1741 c->Request.CDB[1] = 0x10;
1742 c->Request.CDB[10] = (size >> 24) & 0xFF;
1743 c->Request.CDB[11] = (size >> 16) & 0xFF;
1744 c->Request.CDB[12] = (size >> 8) & 0xFF;
1745 c->Request.CDB[13] = size & 0xFF;
1746 c->Request.Timeout = 0;
1747 c->Request.CDB[0] = cmd;
1748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 case CCISS_CACHE_FLUSH:
1750 c->Request.CDBLen = 12;
1751 c->Request.Type.Attribute = ATTR_SIMPLE;
1752 c->Request.Type.Direction = XFER_WRITE;
1753 c->Request.Timeout = 0;
1754 c->Request.CDB[0] = BMIC_WRITE;
1755 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001756 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 default:
1758 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001759 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
Bjorn Helgaase2019b52006-06-25 05:49:05 -07001760 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762 } else if (cmd_type == TYPE_MSG) {
1763 switch (cmd) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001764 case 0: /* ABORT message */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001765 c->Request.CDBLen = 12;
1766 c->Request.Type.Attribute = ATTR_SIMPLE;
1767 c->Request.Type.Direction = XFER_WRITE;
1768 c->Request.Timeout = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001769 c->Request.CDB[0] = cmd; /* abort */
1770 c->Request.CDB[1] = 0; /* abort a command */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001771 /* buff contains the tag of the command to abort */
1772 memcpy(&c->Request.CDB[4], buff, 8);
1773 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001774 case 1: /* RESET message */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06001775 c->Request.CDBLen = 12;
1776 c->Request.Type.Attribute = ATTR_SIMPLE;
1777 c->Request.Type.Direction = XFER_WRITE;
1778 c->Request.Timeout = 0;
1779 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001780 c->Request.CDB[0] = cmd; /* reset */
1781 c->Request.CDB[1] = 0x04; /* reset a LUN */
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001782 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 case 3: /* No-Op message */
1784 c->Request.CDBLen = 1;
1785 c->Request.Type.Attribute = ATTR_SIMPLE;
1786 c->Request.Type.Direction = XFER_WRITE;
1787 c->Request.Timeout = 0;
1788 c->Request.CDB[0] = cmd;
1789 break;
1790 default:
1791 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001792 "cciss%d: unknown message type %d\n", ctlr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return IO_ERROR;
1794 }
1795 } else {
1796 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001797 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return IO_ERROR;
1799 }
1800 /* Fill in the scatter gather information */
1801 if (size > 0) {
1802 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001803 buff, size,
1804 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1806 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1807 c->SG[0].Len = size;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001808 c->SG[0].Ext = 0; /* we are not chaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810 return status;
1811}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001812
1813static int sendcmd_withirq(__u8 cmd,
1814 int ctlr,
1815 void *buff,
1816 size_t size,
1817 unsigned int use_unit_num,
1818 unsigned int log_unit, __u8 page_code, int cmd_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 ctlr_info_t *h = hba[ctlr];
1821 CommandList_struct *c;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001822 u64bit buff_dma_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 unsigned long flags;
1824 int return_status;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07001825 DECLARE_COMPLETION_ONSTACK(wait);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001826
1827 if ((c = cmd_alloc(h, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return -ENOMEM;
1829 return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001830 log_unit, page_code, NULL, cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (return_status != IO_OK) {
1832 cmd_free(h, c, 0);
1833 return return_status;
1834 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001835 resend_cmd2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 c->waiting = &wait;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 /* Put the request on the tail of the queue and send it */
1839 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1840 addQ(&h->reqQ, c);
1841 h->Qdepth++;
1842 start_io(h);
1843 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 wait_for_completion(&wait);
1846
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001847 if (c->err_info->CommandStatus != 0) { /* an error has occurred */
1848 switch (c->err_info->CommandStatus) {
1849 case CMD_TARGET_STATUS:
1850 printk(KERN_WARNING "cciss: cmd %p has "
1851 " completed with errors\n", c);
1852 if (c->err_info->ScsiStatus) {
1853 printk(KERN_WARNING "cciss: cmd %p "
1854 "has SCSI Status = %x\n",
1855 c, c->err_info->ScsiStatus);
1856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001859 case CMD_DATA_UNDERRUN:
1860 case CMD_DATA_OVERRUN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 /* expected for inquire and report lun commands */
1862 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001863 case CMD_INVALID:
1864 printk(KERN_WARNING "cciss: Cmd %p is "
1865 "reported invalid\n", c);
1866 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001868 case CMD_PROTOCOL_ERR:
1869 printk(KERN_WARNING "cciss: cmd %p has "
1870 "protocol error \n", c);
1871 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001873 case CMD_HARDWARE_ERR:
1874 printk(KERN_WARNING "cciss: cmd %p had "
1875 " hardware error\n", c);
1876 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001878 case CMD_CONNECTION_LOST:
1879 printk(KERN_WARNING "cciss: cmd %p had "
1880 "connection lost\n", c);
1881 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001883 case CMD_ABORTED:
1884 printk(KERN_WARNING "cciss: cmd %p was "
1885 "aborted\n", c);
1886 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001888 case CMD_ABORT_FAILED:
1889 printk(KERN_WARNING "cciss: cmd %p reports "
1890 "abort failed\n", c);
1891 return_status = IO_ERROR;
1892 break;
1893 case CMD_UNSOLICITED_ABORT:
1894 printk(KERN_WARNING
1895 "cciss%d: unsolicited abort %p\n", ctlr, c);
1896 if (c->retry_count < MAX_CMD_RETRIES) {
1897 printk(KERN_WARNING
1898 "cciss%d: retrying %p\n", ctlr, c);
1899 c->retry_count++;
1900 /* erase the old error information */
1901 memset(c->err_info, 0,
1902 sizeof(ErrorInfo_struct));
1903 return_status = IO_OK;
1904 INIT_COMPLETION(wait);
1905 goto resend_cmd2;
1906 }
1907 return_status = IO_ERROR;
1908 break;
1909 default:
1910 printk(KERN_WARNING "cciss: cmd %p returned "
1911 "unknown status %x\n", c,
1912 c->err_info->CommandStatus);
1913 return_status = IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /* unlock the buffers from DMA */
Mike Millerbb2a37b2005-09-13 01:25:24 -07001917 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
1918 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001919 pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
1920 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 cmd_free(h, c, 0);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001922 return return_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925static void cciss_geometry_inquiry(int ctlr, int logvol,
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001926 int withirq, sector_t total_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001927 unsigned int block_size,
1928 InquiryData_struct *inq_buff,
1929 drive_info_struct *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 int return_code;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001932 unsigned long t;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 memset(inq_buff, 0, sizeof(InquiryData_struct));
1935 if (withirq)
1936 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001937 inq_buff, sizeof(*inq_buff), 1,
1938 logvol, 0xC1, TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 else
1940 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001941 sizeof(*inq_buff), 1, logvol, 0xC1, NULL,
1942 TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (return_code == IO_OK) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001944 if (inq_buff->data_byte[8] == 0xFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001946 "cciss: reading geometry failed, volume "
1947 "does not support reading geometry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 drv->heads = 255;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001949 drv->sectors = 32; // Sectors per track
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 drv->heads = inq_buff->data_byte[6];
1952 drv->sectors = inq_buff->data_byte[7];
1953 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
1954 drv->cylinders += inq_buff->data_byte[5];
1955 drv->raid_level = inq_buff->data_byte[8];
Matthew Wilcox3f7705e2006-10-21 10:24:19 -07001956 }
1957 drv->block_size = block_size;
1958 drv->nr_blocks = total_size;
1959 t = drv->heads * drv->sectors;
1960 if (t > 1) {
1961 unsigned rem = sector_div(total_size, t);
1962 if (rem)
1963 total_size++;
1964 drv->cylinders = total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001966 } else { /* Get geometry failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 printk(KERN_WARNING "cciss: reading geometry failed\n");
1968 }
Metathronius Galabantcc088d12006-09-30 23:27:47 -07001969 printk(KERN_INFO " heads=%d, sectors=%d, cylinders=%d\n\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001970 drv->heads, drv->sectors, drv->cylinders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973static void
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001974cciss_read_capacity(int ctlr, int logvol, int withirq, sector_t *total_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001975 unsigned int *block_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001977 ReadCapdata_struct *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 int return_code;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001979 buf = kmalloc(sizeof(ReadCapdata_struct), GFP_KERNEL);
1980 if (buf == NULL) {
1981 printk(KERN_WARNING "cciss: out of memory\n");
1982 return;
1983 }
1984 memset(buf, 0, sizeof(ReadCapdata_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (withirq)
1986 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001987 ctlr, buf, sizeof(ReadCapdata_struct),
1988 1, logvol, 0, TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 else
1990 return_code = sendcmd(CCISS_READ_CAPACITY,
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001991 ctlr, buf, sizeof(ReadCapdata_struct),
1992 1, logvol, 0, NULL, TYPE_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if (return_code == IO_OK) {
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07001994 *total_size = be32_to_cpu(*(__u32 *) buf->total_size)+1;
1995 *block_size = be32_to_cpu(*(__u32 *) buf->block_size);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07001996 } else { /* read capacity command failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 printk(KERN_WARNING "cciss: read capacity failed\n");
1998 *total_size = 0;
1999 *block_size = BLOCK_SIZE;
2000 }
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002001 if (*total_size != (__u32) 0)
Randy Dunlap7b92aad2006-10-28 10:38:40 -07002002 printk(KERN_INFO " blocks= %llu block_size= %d\n",
2003 (unsigned long long)*total_size, *block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002004 kfree(buf);
2005 return;
2006}
2007
2008static void
2009cciss_read_capacity_16(int ctlr, int logvol, int withirq, sector_t *total_size, unsigned int *block_size)
2010{
2011 ReadCapdata_struct_16 *buf;
2012 int return_code;
2013 buf = kmalloc(sizeof(ReadCapdata_struct_16), GFP_KERNEL);
2014 if (buf == NULL) {
2015 printk(KERN_WARNING "cciss: out of memory\n");
2016 return;
2017 }
2018 memset(buf, 0, sizeof(ReadCapdata_struct_16));
2019 if (withirq) {
2020 return_code = sendcmd_withirq(CCISS_READ_CAPACITY_16,
2021 ctlr, buf, sizeof(ReadCapdata_struct_16),
2022 1, logvol, 0, TYPE_CMD);
2023 }
2024 else {
2025 return_code = sendcmd(CCISS_READ_CAPACITY_16,
2026 ctlr, buf, sizeof(ReadCapdata_struct_16),
2027 1, logvol, 0, NULL, TYPE_CMD);
2028 }
2029 if (return_code == IO_OK) {
2030 *total_size = be64_to_cpu(*(__u64 *) buf->total_size)+1;
2031 *block_size = be32_to_cpu(*(__u32 *) buf->block_size);
2032 } else { /* read capacity command failed */
2033 printk(KERN_WARNING "cciss: read capacity failed\n");
2034 *total_size = 0;
2035 *block_size = BLOCK_SIZE;
2036 }
Randy Dunlap7b92aad2006-10-28 10:38:40 -07002037 printk(KERN_INFO " blocks= %llu block_size= %d\n",
2038 (unsigned long long)*total_size, *block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002039 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return;
2041}
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043static int cciss_revalidate(struct gendisk *disk)
2044{
2045 ctlr_info_t *h = get_host(disk);
2046 drive_info_struct *drv = get_drv(disk);
2047 int logvol;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002048 int FOUND = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 unsigned int block_size;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002050 sector_t total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 InquiryData_struct *inq_buff = NULL;
2052
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002053 for (logvol = 0; logvol < CISS_MAX_LUN; logvol++) {
2054 if (h->drv[logvol].LunID == drv->LunID) {
2055 FOUND = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 break;
2057 }
2058 }
2059
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002060 if (!FOUND)
2061 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002063 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
2064 if (inq_buff == NULL) {
2065 printk(KERN_WARNING "cciss: out of memory\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002066 return 1;
2067 }
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002068 if (h->cciss_read == CCISS_READ_10) {
2069 cciss_read_capacity(h->ctlr, logvol, 1,
2070 &total_size, &block_size);
2071 } else {
2072 cciss_read_capacity_16(h->ctlr, logvol, 1,
2073 &total_size, &block_size);
2074 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002075 cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size,
2076 inq_buff, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Mike Millerad2b9312005-07-28 01:07:31 -07002078 blk_queue_hardsect_size(drv->queue, drv->block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 set_capacity(disk, drv->nr_blocks);
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 kfree(inq_buff);
2082 return 0;
2083}
2084
2085/*
2086 * Wait polling for a command to complete.
2087 * The memory mapped FIFO is polled for the completion.
2088 * Used only at init time, interrupts from the HBA are disabled.
2089 */
2090static unsigned long pollcomplete(int ctlr)
2091{
2092 unsigned long done;
2093 int i;
2094
2095 /* Wait (up to 20 seconds) for a command to complete */
2096
2097 for (i = 20 * HZ; i > 0; i--) {
2098 done = hba[ctlr]->access.command_completed(hba[ctlr]);
Nishanth Aravamudan86e84862005-09-10 00:27:28 -07002099 if (done == FIFO_EMPTY)
2100 schedule_timeout_uninterruptible(1);
2101 else
Bjorn Helgaase2019b52006-06-25 05:49:05 -07002102 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
2104 /* Invalid address to tell caller we ran out of time */
2105 return 1;
2106}
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002107
2108static int add_sendcmd_reject(__u8 cmd, int ctlr, unsigned long complete)
2109{
2110 /* We get in here if sendcmd() is polling for completions
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002111 and gets some command back that it wasn't expecting --
2112 something other than that which it just sent down.
2113 Ordinarily, that shouldn't happen, but it can happen when
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002114 the scsi tape stuff gets into error handling mode, and
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002115 starts using sendcmd() to try to abort commands and
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002116 reset tape drives. In that case, sendcmd may pick up
2117 completions of commands that were sent to logical drives
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002118 through the block i/o system, or cciss ioctls completing, etc.
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002119 In that case, we need to save those completions for later
2120 processing by the interrupt handler.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002121 */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002122
2123#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002124 struct sendcmd_reject_list *srl = &hba[ctlr]->scsi_rejects;
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002125
2126 /* If it's not the scsi tape stuff doing error handling, (abort */
2127 /* or reset) then we don't expect anything weird. */
2128 if (cmd != CCISS_RESET_MSG && cmd != CCISS_ABORT_MSG) {
2129#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002130 printk(KERN_WARNING "cciss cciss%d: SendCmd "
2131 "Invalid command list address returned! (%lx)\n",
2132 ctlr, complete);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002133 /* not much we can do. */
2134#ifdef CONFIG_CISS_SCSI_TAPE
2135 return 1;
2136 }
2137
2138 /* We've sent down an abort or reset, but something else
2139 has completed */
2140 if (srl->ncompletions >= (NR_CMDS + 2)) {
2141 /* Uh oh. No room to save it for later... */
2142 printk(KERN_WARNING "cciss%d: Sendcmd: Invalid command addr, "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002143 "reject list overflow, command lost!\n", ctlr);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002144 return 1;
2145 }
2146 /* Save it for later */
2147 srl->complete[srl->ncompletions] = complete;
2148 srl->ncompletions++;
2149#endif
2150 return 0;
2151}
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153/*
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002154 * Send a command to the controller, and wait for it to complete.
2155 * Only used at init time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002157static int sendcmd(__u8 cmd, int ctlr, void *buff, size_t size, unsigned int use_unit_num, /* 0: address the controller,
2158 1: address logical volume log_unit,
2159 2: periph device address is scsi3addr */
2160 unsigned int log_unit,
2161 __u8 page_code, unsigned char *scsi3addr, int cmd_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 CommandList_struct *c;
2164 int i;
2165 unsigned long complete;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002166 ctlr_info_t *info_p = hba[ctlr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 u64bit buff_dma_handle;
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002168 int status, done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 if ((c = cmd_alloc(info_p, 1)) == NULL) {
2171 printk(KERN_WARNING "cciss: unable to get memory");
Bjorn Helgaase2019b52006-06-25 05:49:05 -07002172 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174 status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002175 log_unit, page_code, scsi3addr, cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (status != IO_OK) {
2177 cmd_free(info_p, c, 1);
2178 return status;
2179 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002180 resend_cmd1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /*
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002182 * Disable interrupt
2183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184#ifdef CCISS_DEBUG
2185 printk(KERN_DEBUG "cciss: turning intr off\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002186#endif /* CCISS_DEBUG */
2187 info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /* Make sure there is room in the command FIFO */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002190 /* Actually it should be completely empty at this time */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002191 /* unless we are in here doing error handling for the scsi */
2192 /* tape side of the driver. */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002193 for (i = 200000; i > 0; i--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /* if fifo isn't full go */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002195 if (!(info_p->access.fifo_full(info_p))) {
2196
2197 break;
2198 }
2199 udelay(10);
2200 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
2201 " waiting!\n", ctlr);
2202 }
2203 /*
2204 * Send the cmd
2205 */
2206 info_p->access.submit_command(info_p, c);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002207 done = 0;
2208 do {
2209 complete = pollcomplete(ctlr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211#ifdef CCISS_DEBUG
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002212 printk(KERN_DEBUG "cciss: command completed\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002213#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002215 if (complete == 1) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002216 printk(KERN_WARNING
2217 "cciss cciss%d: SendCmd Timeout out, "
2218 "No command list address returned!\n", ctlr);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002219 status = IO_ERROR;
2220 done = 1;
2221 break;
2222 }
2223
2224 /* This will need to change for direct lookup completions */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002225 if ((complete & CISS_ERROR_BIT)
2226 && (complete & ~CISS_ERROR_BIT) == c->busaddr) {
2227 /* if data overrun or underun on Report command
2228 ignore it
2229 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
2231 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
2232 (c->Request.CDB[0] == CISS_INQUIRY)) &&
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002233 ((c->err_info->CommandStatus ==
2234 CMD_DATA_OVERRUN) ||
2235 (c->err_info->CommandStatus == CMD_DATA_UNDERRUN)
2236 )) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 complete = c->busaddr;
2238 } else {
2239 if (c->err_info->CommandStatus ==
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002240 CMD_UNSOLICITED_ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 printk(KERN_WARNING "cciss%d: "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002242 "unsolicited abort %p\n",
2243 ctlr, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (c->retry_count < MAX_CMD_RETRIES) {
2245 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002246 "cciss%d: retrying %p\n",
2247 ctlr, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 c->retry_count++;
2249 /* erase the old error */
2250 /* information */
2251 memset(c->err_info, 0,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002252 sizeof
2253 (ErrorInfo_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 goto resend_cmd1;
2255 } else {
2256 printk(KERN_WARNING
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002257 "cciss%d: retried %p too "
2258 "many times\n", ctlr, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 status = IO_ERROR;
2260 goto cleanup1;
2261 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002262 } else if (c->err_info->CommandStatus ==
2263 CMD_UNABORTABLE) {
2264 printk(KERN_WARNING
2265 "cciss%d: command could not be aborted.\n",
2266 ctlr);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002267 status = IO_ERROR;
2268 goto cleanup1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
2270 printk(KERN_WARNING "ciss ciss%d: sendcmd"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002271 " Error %x \n", ctlr,
2272 c->err_info->CommandStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 printk(KERN_WARNING "ciss ciss%d: sendcmd"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002274 " offensive info\n"
2275 " size %x\n num %x value %x\n",
2276 ctlr,
2277 c->err_info->MoreErrInfo.Invalid_Cmd.
2278 offense_size,
2279 c->err_info->MoreErrInfo.Invalid_Cmd.
2280 offense_num,
2281 c->err_info->MoreErrInfo.Invalid_Cmd.
2282 offense_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 status = IO_ERROR;
2284 goto cleanup1;
2285 }
2286 }
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002287 /* This will need changing for direct lookup completions */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002288 if (complete != c->busaddr) {
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002289 if (add_sendcmd_reject(cmd, ctlr, complete) != 0) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002290 BUG(); /* we are pretty much hosed if we get here. */
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002291 }
2292 continue;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002293 } else
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002294 done = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002295 } while (!done);
2296
2297 cleanup1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /* unlock the data buffer from DMA */
Mike Millerbb2a37b2005-09-13 01:25:24 -07002299 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
2300 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002302 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002303#ifdef CONFIG_CISS_SCSI_TAPE
2304 /* if we saved some commands for later, process them now. */
2305 if (info_p->scsi_rejects.ncompletions > 0)
David Howells7d12e782006-10-05 14:55:46 +01002306 do_cciss_intr(0, info_p);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 cmd_free(info_p, c, 1);
Bjorn Helgaase2019b52006-06-25 05:49:05 -07002309 return status;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002310}
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312/*
2313 * Map (physical) PCI mem into (virtual) kernel space
2314 */
2315static void __iomem *remap_pci_mem(ulong base, ulong size)
2316{
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002317 ulong page_base = ((ulong) base) & PAGE_MASK;
2318 ulong page_offs = ((ulong) base) - page_base;
2319 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002321 return page_remapped ? (page_remapped + page_offs) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002324/*
2325 * Takes jobs of the Q and sends them to the hardware, then puts it on
2326 * the Q to wait for completion.
2327 */
2328static void start_io(ctlr_info_t *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
2330 CommandList_struct *c;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002331
2332 while ((c = h->reqQ) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 /* can't do anything if fifo is full */
2334 if ((h->access.fifo_full(h))) {
2335 printk(KERN_WARNING "cciss: fifo full\n");
2336 break;
2337 }
2338
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002339 /* Get the first entry from the Request Q */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 removeQ(&(h->reqQ), c);
2341 h->Qdepth--;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002342
2343 /* Tell the controller execute command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 h->access.submit_command(h, c);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002345
2346 /* Put job onto the completed Q */
2347 addQ(&(h->cmpQ), c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351/* Assumes that CCISS_LOCK(h->ctlr) is held. */
2352/* Zeros out the error record and then resends the command back */
2353/* to the controller */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002354static inline void resend_cciss_cmd(ctlr_info_t *h, CommandList_struct *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
2356 /* erase the old error information */
2357 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
2358
2359 /* add it to software queue and then send it to the controller */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002360 addQ(&(h->reqQ), c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 h->Qdepth++;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002362 if (h->Qdepth > h->maxQsinceinit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 h->maxQsinceinit = h->Qdepth;
2364
2365 start_io(h);
2366}
Jens Axboea9925a02006-01-09 16:04:06 +01002367
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002368/* checks the status of the job and calls complete buffers to mark all
Jens Axboea9925a02006-01-09 16:04:06 +01002369 * buffers for the completed job. Note that this function does not need
2370 * to hold the hba/queue lock.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002371 */
2372static inline void complete_command(ctlr_info_t *h, CommandList_struct *cmd,
2373 int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
2375 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 int retry_cmd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002378 if (timeout)
2379 status = 0;
2380
2381 if (cmd->err_info->CommandStatus != 0) { /* an error has occurred */
2382 switch (cmd->err_info->CommandStatus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 unsigned char sense_key;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002384 case CMD_TARGET_STATUS:
2385 status = 0;
2386
2387 if (cmd->err_info->ScsiStatus == 0x02) {
2388 printk(KERN_WARNING "cciss: cmd %p "
2389 "has CHECK CONDITION "
2390 " byte 2 = 0x%x\n", cmd,
2391 cmd->err_info->SenseInfo[2]
2392 );
2393 /* check the sense key */
2394 sense_key = 0xf & cmd->err_info->SenseInfo[2];
2395 /* no status or recovered error */
2396 if ((sense_key == 0x0) || (sense_key == 0x1)) {
2397 status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002399 } else {
2400 printk(KERN_WARNING "cciss: cmd %p "
2401 "has SCSI Status 0x%x\n",
2402 cmd, cmd->err_info->ScsiStatus);
2403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002405 case CMD_DATA_UNDERRUN:
2406 printk(KERN_WARNING "cciss: cmd %p has"
2407 " completed with data underrun "
2408 "reported\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002410 case CMD_DATA_OVERRUN:
2411 printk(KERN_WARNING "cciss: cmd %p has"
2412 " completed with data overrun "
2413 "reported\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002415 case CMD_INVALID:
2416 printk(KERN_WARNING "cciss: cmd %p is "
2417 "reported invalid\n", cmd);
2418 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002420 case CMD_PROTOCOL_ERR:
2421 printk(KERN_WARNING "cciss: cmd %p has "
2422 "protocol error \n", cmd);
2423 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002425 case CMD_HARDWARE_ERR:
2426 printk(KERN_WARNING "cciss: cmd %p had "
2427 " hardware error\n", cmd);
2428 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002430 case CMD_CONNECTION_LOST:
2431 printk(KERN_WARNING "cciss: cmd %p had "
2432 "connection lost\n", cmd);
2433 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002435 case CMD_ABORTED:
2436 printk(KERN_WARNING "cciss: cmd %p was "
2437 "aborted\n", cmd);
2438 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002440 case CMD_ABORT_FAILED:
2441 printk(KERN_WARNING "cciss: cmd %p reports "
2442 "abort failed\n", cmd);
2443 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 break;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002445 case CMD_UNSOLICITED_ABORT:
2446 printk(KERN_WARNING "cciss%d: unsolicited "
2447 "abort %p\n", h->ctlr, cmd);
2448 if (cmd->retry_count < MAX_CMD_RETRIES) {
2449 retry_cmd = 1;
2450 printk(KERN_WARNING
2451 "cciss%d: retrying %p\n", h->ctlr, cmd);
2452 cmd->retry_count++;
2453 } else
2454 printk(KERN_WARNING
2455 "cciss%d: %p retried too "
2456 "many times\n", h->ctlr, cmd);
2457 status = 0;
2458 break;
2459 case CMD_TIMEOUT:
2460 printk(KERN_WARNING "cciss: cmd %p timedout\n", cmd);
2461 status = 0;
2462 break;
2463 default:
2464 printk(KERN_WARNING "cciss: cmd %p returned "
2465 "unknown status %x\n", cmd,
2466 cmd->err_info->CommandStatus);
2467 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
2469 }
2470 /* We need to return this command */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002471 if (retry_cmd) {
2472 resend_cciss_cmd(h, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Jens Axboea9925a02006-01-09 16:04:06 +01002476 cmd->rq->completion_data = cmd;
2477 cmd->rq->errors = status;
Jens Axboe2056a782006-03-23 20:00:26 +01002478 blk_add_trace_rq(cmd->rq->q, cmd->rq, BLK_TA_COMPLETE);
Jens Axboea9925a02006-01-09 16:04:06 +01002479 blk_complete_request(cmd->rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002482/*
2483 * Get a request and submit it to the controller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 */
2485static void do_cciss_request(request_queue_t *q)
2486{
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002487 ctlr_info_t *h = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 CommandList_struct *c;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002489 sector_t start_blk;
2490 int seg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 struct request *creq;
2492 u64bit temp64;
2493 struct scatterlist tmp_sg[MAXSGENTRIES];
2494 drive_info_struct *drv;
2495 int i, dir;
2496
2497 /* We call start_io here in case there is a command waiting on the
2498 * queue that has not been sent.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (blk_queue_plugged(q))
2501 goto startio;
2502
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002503 queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 creq = elv_next_request(q);
2505 if (!creq)
2506 goto startio;
2507
Eric Sesterhenn089fe1b2006-03-24 18:50:27 +01002508 BUG_ON(creq->nr_phys_segments > MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002510 if ((c = cmd_alloc(h, 1)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 goto full;
2512
2513 blkdev_dequeue_request(creq);
2514
2515 spin_unlock_irq(q->queue_lock);
2516
2517 c->cmd_type = CMD_RWREQ;
2518 c->rq = creq;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002519
2520 /* fill in the request */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 drv = creq->rq_disk->private_data;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002522 c->Header.ReplyQueue = 0; // unused in simple mode
Mike Miller33079b22005-09-13 01:25:22 -07002523 /* got command from pool, so use the command block index instead */
2524 /* for direct lookups. */
2525 /* The first 2 bits are reserved for controller error reporting. */
2526 c->Header.Tag.lower = (c->cmdindex << 3);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002527 c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */
2528 c->Header.LUN.LogDev.VolId = drv->LunID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 c->Header.LUN.LogDev.Mode = 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002530 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
2531 c->Request.Type.Type = TYPE_CMD; // It is a command.
2532 c->Request.Type.Attribute = ATTR_SIMPLE;
2533 c->Request.Type.Direction =
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002534 (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002535 c->Request.Timeout = 0; // Don't time out
2536 c->Request.CDB[0] =
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002537 (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 start_blk = creq->sector;
2539#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002540 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n", (int)creq->sector,
2541 (int)creq->nr_sectors);
2542#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 seg = blk_rq_map_sg(q, creq, tmp_sg);
2545
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002546 /* get the DMA records for the setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (c->Request.Type.Direction == XFER_READ)
2548 dir = PCI_DMA_FROMDEVICE;
2549 else
2550 dir = PCI_DMA_TODEVICE;
2551
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002552 for (i = 0; i < seg; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 c->SG[i].Len = tmp_sg[i].length;
2554 temp64.val = (__u64) pci_map_page(h->pdev, tmp_sg[i].page,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002555 tmp_sg[i].offset,
2556 tmp_sg[i].length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 c->SG[i].Addr.lower = temp64.val32.lower;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002558 c->SG[i].Addr.upper = temp64.val32.upper;
2559 c->SG[i].Ext = 0; // we are not chaining
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002561 /* track how many SG entries we are using */
2562 if (seg > h->maxSG)
2563 h->maxSG = seg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002566 printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n",
2567 creq->nr_sectors, seg);
2568#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570 c->Header.SGList = c->Header.SGTotal = seg;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002571 if(h->cciss_read == CCISS_READ_10) {
2572 c->Request.CDB[1] = 0;
2573 c->Request.CDB[2] = (start_blk >> 24) & 0xff; //MSB
2574 c->Request.CDB[3] = (start_blk >> 16) & 0xff;
2575 c->Request.CDB[4] = (start_blk >> 8) & 0xff;
2576 c->Request.CDB[5] = start_blk & 0xff;
2577 c->Request.CDB[6] = 0; // (sect >> 24) & 0xff; MSB
2578 c->Request.CDB[7] = (creq->nr_sectors >> 8) & 0xff;
2579 c->Request.CDB[8] = creq->nr_sectors & 0xff;
2580 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2581 } else {
2582 c->Request.CDBLen = 16;
2583 c->Request.CDB[1]= 0;
2584 c->Request.CDB[2]= (start_blk >> 56) & 0xff; //MSB
2585 c->Request.CDB[3]= (start_blk >> 48) & 0xff;
2586 c->Request.CDB[4]= (start_blk >> 40) & 0xff;
2587 c->Request.CDB[5]= (start_blk >> 32) & 0xff;
2588 c->Request.CDB[6]= (start_blk >> 24) & 0xff;
2589 c->Request.CDB[7]= (start_blk >> 16) & 0xff;
2590 c->Request.CDB[8]= (start_blk >> 8) & 0xff;
2591 c->Request.CDB[9]= start_blk & 0xff;
2592 c->Request.CDB[10]= (creq->nr_sectors >> 24) & 0xff;
2593 c->Request.CDB[11]= (creq->nr_sectors >> 16) & 0xff;
2594 c->Request.CDB[12]= (creq->nr_sectors >> 8) & 0xff;
2595 c->Request.CDB[13]= creq->nr_sectors & 0xff;
2596 c->Request.CDB[14] = c->Request.CDB[15] = 0;
2597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 spin_lock_irq(q->queue_lock);
2600
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002601 addQ(&(h->reqQ), c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 h->Qdepth++;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002603 if (h->Qdepth > h->maxQsinceinit)
2604 h->maxQsinceinit = h->Qdepth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 goto queue;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002607full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 blk_stop_queue(q);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07002609startio:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /* We will already have the driver lock here so not need
2611 * to lock it.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 start_io(h);
2614}
2615
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002616static inline unsigned long get_next_completion(ctlr_info_t *h)
2617{
2618#ifdef CONFIG_CISS_SCSI_TAPE
2619 /* Any rejects from sendcmd() lying around? Process them first */
2620 if (h->scsi_rejects.ncompletions == 0)
2621 return h->access.command_completed(h);
2622 else {
2623 struct sendcmd_reject_list *srl;
2624 int n;
2625 srl = &h->scsi_rejects;
2626 n = --srl->ncompletions;
2627 /* printk("cciss%d: processing saved reject\n", h->ctlr); */
2628 printk("p");
2629 return srl->complete[n];
2630 }
2631#else
2632 return h->access.command_completed(h);
2633#endif
2634}
2635
2636static inline int interrupt_pending(ctlr_info_t *h)
2637{
2638#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002639 return (h->access.intr_pending(h)
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002640 || (h->scsi_rejects.ncompletions > 0));
2641#else
2642 return h->access.intr_pending(h);
2643#endif
2644}
2645
2646static inline long interrupt_not_for_us(ctlr_info_t *h)
2647{
2648#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002649 return (((h->access.intr_pending(h) == 0) ||
2650 (h->interrupts_enabled == 0))
2651 && (h->scsi_rejects.ncompletions == 0));
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002652#else
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002653 return (((h->access.intr_pending(h) == 0) ||
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002654 (h->interrupts_enabled == 0)));
2655#endif
2656}
2657
David Howells7d12e782006-10-05 14:55:46 +01002658static irqreturn_t do_cciss_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 ctlr_info_t *h = dev_id;
2661 CommandList_struct *c;
2662 unsigned long flags;
Mike Miller33079b22005-09-13 01:25:22 -07002663 __u32 a, a1, a2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002665 if (interrupt_not_for_us(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 /*
2668 * If there are completed commands in the completion queue,
2669 * we had better do something about it.
2670 */
2671 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06002672 while (interrupt_pending(h)) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002673 while ((a = get_next_completion(h)) != FIFO_EMPTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 a1 = a;
Mike Miller33079b22005-09-13 01:25:22 -07002675 if ((a & 0x04)) {
2676 a2 = (a >> 3);
2677 if (a2 >= NR_CMDS) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002678 printk(KERN_WARNING
2679 "cciss: controller cciss%d failed, stopping.\n",
2680 h->ctlr);
Mike Miller33079b22005-09-13 01:25:22 -07002681 fail_all_cmds(h->ctlr);
2682 return IRQ_HANDLED;
2683 }
2684
2685 c = h->cmd_pool + a2;
2686 a = c->busaddr;
2687
2688 } else {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002689 a &= ~3;
Mike Miller33079b22005-09-13 01:25:22 -07002690 if ((c = h->cmpQ) == NULL) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002691 printk(KERN_WARNING
2692 "cciss: Completion of %08x ignored\n",
2693 a1);
2694 continue;
2695 }
2696 while (c->busaddr != a) {
2697 c = c->next;
2698 if (c == h->cmpQ)
2699 break;
2700 }
Mike Miller33079b22005-09-13 01:25:22 -07002701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 /*
2703 * If we've found the command, take it off the
2704 * completion Q and free it
2705 */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002706 if (c->busaddr == a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 removeQ(&h->cmpQ, c);
2708 if (c->cmd_type == CMD_RWREQ) {
2709 complete_command(h, c, 0);
2710 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2711 complete(c->waiting);
2712 }
2713# ifdef CONFIG_CISS_SCSI_TAPE
2714 else if (c->cmd_type == CMD_SCSI)
2715 complete_scsi_command(c, 0, a1);
2716# endif
2717 continue;
2718 }
2719 }
2720 }
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2723 return IRQ_HANDLED;
2724}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002725
2726/*
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07002727 * We cannot read the structure directly, for portability we must use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 * the io functions.
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002729 * This is for debug only.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 */
2731#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002732static void print_cfg_table(CfgTable_struct *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
2734 int i;
2735 char temp_name[17];
2736
2737 printk("Controller Configuration information\n");
2738 printk("------------------------------------\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002739 for (i = 0; i < 4; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 temp_name[i] = readb(&(tb->Signature[i]));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002741 temp_name[4] = '\0';
2742 printk(" Signature = %s\n", temp_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002744 printk(" Transport methods supported = 0x%x\n",
2745 readl(&(tb->TransportSupport)));
2746 printk(" Transport methods active = 0x%x\n",
2747 readl(&(tb->TransportActive)));
2748 printk(" Requested transport Method = 0x%x\n",
2749 readl(&(tb->HostWrite.TransportRequest)));
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07002750 printk(" Coalesce Interrupt Delay = 0x%x\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002751 readl(&(tb->HostWrite.CoalIntDelay)));
Bjorn Helgaasd14c4ab2006-06-25 05:49:04 -07002752 printk(" Coalesce Interrupt Count = 0x%x\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002753 readl(&(tb->HostWrite.CoalIntCount)));
2754 printk(" Max outstanding commands = 0x%d\n",
2755 readl(&(tb->CmdsOutMax)));
2756 printk(" Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
2757 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 temp_name[i] = readb(&(tb->ServerName[i]));
2759 temp_name[16] = '\0';
2760 printk(" Server Name = %s\n", temp_name);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002761 printk(" Heartbeat Counter = 0x%x\n\n\n", readl(&(tb->HeartBeat)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002763#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002765static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
2767 int i, offset, mem_type, bar_type;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002768 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return 0;
2770 offset = 0;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002771 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2772 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
2774 offset += 4;
2775 else {
2776 mem_type = pci_resource_flags(pdev, i) &
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002777 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 switch (mem_type) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002779 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2780 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
2781 offset += 4; /* 32 bit */
2782 break;
2783 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2784 offset += 8;
2785 break;
2786 default: /* reserved in PCI 2.2 */
2787 printk(KERN_WARNING
2788 "Base address is invalid\n");
2789 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 break;
2791 }
2792 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002793 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
2794 return i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 }
2796 return -1;
2797}
2798
Mike Millerfb86a352006-01-08 01:03:50 -08002799/* If MSI/MSI-X is supported by the kernel we will try to enable it on
2800 * controllers that are capable. If not, we use IO-APIC mode.
2801 */
2802
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002803static void __devinit cciss_interrupt_mode(ctlr_info_t *c,
2804 struct pci_dev *pdev, __u32 board_id)
Mike Millerfb86a352006-01-08 01:03:50 -08002805{
2806#ifdef CONFIG_PCI_MSI
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002807 int err;
2808 struct msix_entry cciss_msix_entries[4] = { {0, 0}, {0, 1},
2809 {0, 2}, {0, 3}
2810 };
Mike Millerfb86a352006-01-08 01:03:50 -08002811
2812 /* Some boards advertise MSI but don't really support it */
2813 if ((board_id == 0x40700E11) ||
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002814 (board_id == 0x40800E11) ||
2815 (board_id == 0x40820E11) || (board_id == 0x40830E11))
Mike Millerfb86a352006-01-08 01:03:50 -08002816 goto default_int_mode;
2817
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002818 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
2819 err = pci_enable_msix(pdev, cciss_msix_entries, 4);
2820 if (!err) {
2821 c->intr[0] = cciss_msix_entries[0].vector;
2822 c->intr[1] = cciss_msix_entries[1].vector;
2823 c->intr[2] = cciss_msix_entries[2].vector;
2824 c->intr[3] = cciss_msix_entries[3].vector;
2825 c->msix_vector = 1;
2826 return;
2827 }
2828 if (err > 0) {
2829 printk(KERN_WARNING "cciss: only %d MSI-X vectors "
2830 "available\n", err);
2831 } else {
2832 printk(KERN_WARNING "cciss: MSI-X init failed %d\n",
2833 err);
2834 }
2835 }
2836 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
2837 if (!pci_enable_msi(pdev)) {
2838 c->intr[SIMPLE_MODE_INT] = pdev->irq;
2839 c->msi_vector = 1;
2840 return;
2841 } else {
2842 printk(KERN_WARNING "cciss: MSI init failed\n");
2843 c->intr[SIMPLE_MODE_INT] = pdev->irq;
2844 return;
2845 }
2846 }
2847 default_int_mode:
2848#endif /* CONFIG_PCI_MSI */
Mike Millerfb86a352006-01-08 01:03:50 -08002849 /* if we get here we're going to use the default interrupt mode */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002850 c->intr[SIMPLE_MODE_INT] = pdev->irq;
Mike Millerfb86a352006-01-08 01:03:50 -08002851 return;
2852}
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854static int cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
2855{
2856 ushort subsystem_vendor_id, subsystem_device_id, command;
2857 __u32 board_id, scratchpad = 0;
2858 __u64 cfg_offset;
2859 __u32 cfg_base_addr;
2860 __u64 cfg_base_addr_index;
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002861 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 /* check to see if controller has been disabled */
2864 /* BEFORE trying to enable it */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002865 (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
2866 if (!(command & 0x02)) {
2867 printk(KERN_WARNING
2868 "cciss: controller appears to be disabled\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002869 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 }
2871
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002872 err = pci_enable_device(pdev);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002873 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002875 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Bjorn Helgaas4e570302006-06-25 05:49:02 -07002878 err = pci_request_regions(pdev, "cciss");
2879 if (err) {
2880 printk(KERN_ERR "cciss: Cannot obtain PCI resources, "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002881 "aborting\n");
Bjorn Helgaas4e570302006-06-25 05:49:02 -07002882 goto err_out_disable_pdev;
2883 }
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 subsystem_vendor_id = pdev->subsystem_vendor;
2886 subsystem_device_id = pdev->subsystem_device;
2887 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002888 subsystem_vendor_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890#ifdef CCISS_DEBUG
2891 printk("command = %x\n", command);
2892 printk("irq = %x\n", pdev->irq);
2893 printk("board_id = %x\n", board_id);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002894#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Mike Millerfb86a352006-01-08 01:03:50 -08002896/* If the kernel supports MSI/MSI-X we will try to enable that functionality,
2897 * else we use the IO-APIC interrupt assigned to us by system ROM.
2898 */
2899 cciss_interrupt_mode(c, pdev, board_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 /*
2902 * Memory base addr is first addr , the second points to the config
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002903 * table
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 */
2905
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002906 c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907#ifdef CCISS_DEBUG
2908 printk("address 0 = %x\n", c->paddr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002909#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 c->vaddr = remap_pci_mem(c->paddr, 200);
2911
2912 /* Wait for the board to become ready. (PCI hotplug needs this.)
2913 * We poll for up to 120 secs, once per 100ms. */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002914 for (i = 0; i < 1200; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
2916 if (scratchpad == CCISS_FIRMWARE_READY)
2917 break;
2918 set_current_state(TASK_INTERRUPTIBLE);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002919 schedule_timeout(HZ / 10); /* wait 100ms */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 }
2921 if (scratchpad != CCISS_FIRMWARE_READY) {
2922 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002923 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07002924 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 }
2926
2927 /* get the address index number */
2928 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
2929 cfg_base_addr &= (__u32) 0x0000ffff;
2930#ifdef CCISS_DEBUG
2931 printk("cfg base address = %x\n", cfg_base_addr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002932#endif /* CCISS_DEBUG */
2933 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934#ifdef CCISS_DEBUG
2935 printk("cfg base address index = %x\n", cfg_base_addr_index);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002936#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (cfg_base_addr_index == -1) {
2938 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002939 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07002940 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
2942
2943 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
2944#ifdef CCISS_DEBUG
2945 printk("cfg offset = %x\n", cfg_offset);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002946#endif /* CCISS_DEBUG */
2947 c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
2948 cfg_base_addr_index) +
2949 cfg_offset, sizeof(CfgTable_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 c->board_id = board_id;
2951
2952#ifdef CCISS_DEBUG
Tobias Klauser945f3902006-01-08 01:05:11 -08002953 print_cfg_table(c->cfgtable);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002954#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002956 for (i = 0; i < ARRAY_SIZE(products); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 if (board_id == products[i].board_id) {
2958 c->product_name = products[i].product_name;
2959 c->access = *(products[i].access);
2960 break;
2961 }
2962 }
Bjorn Helgaas5e8621e2006-06-25 05:49:03 -07002963 if (i == ARRAY_SIZE(products)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 printk(KERN_WARNING "cciss: Sorry, I don't know how"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002965 " to access the Smart Array controller %08lx\n",
2966 (unsigned long)board_id);
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002967 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07002968 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002970 if ((readb(&c->cfgtable->Signature[0]) != 'C') ||
2971 (readb(&c->cfgtable->Signature[1]) != 'I') ||
2972 (readb(&c->cfgtable->Signature[2]) != 'S') ||
2973 (readb(&c->cfgtable->Signature[3]) != 'S')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 printk("Does not appear to be a valid CISS config table\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07002975 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07002976 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978#ifdef CONFIG_X86
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002979 {
2980 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
2981 __u32 prefetch;
2982 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
2983 prefetch |= 0x100;
2984 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
2985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986#endif
2987
2988#ifdef CCISS_DEBUG
2989 printk("Trying to put board into Simple mode\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002990#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002992 /* Update the field, and then ring the doorbell */
2993 writel(CFGTBL_Trans_Simple, &(c->cfgtable->HostWrite.TransportRequest));
2994 writel(CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 /* under certain very rare conditions, this can take awhile.
2997 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
2998 * as we enter this code.) */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07002999 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3001 break;
3002 /* delay and try again */
3003 set_current_state(TASK_INTERRUPTIBLE);
3004 schedule_timeout(10);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003008 printk(KERN_DEBUG "I counter got to %d %x\n", i,
3009 readl(c->vaddr + SA5_DOORBELL));
3010#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003012 print_cfg_table(c->cfgtable);
3013#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003015 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 printk(KERN_WARNING "cciss: unable to get board into"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003017 " simple mode\n");
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003018 err = -ENODEV;
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003019 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021 return 0;
3022
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003023 err_out_free_res:
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003024 pci_release_regions(pdev);
3025
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003026 err_out_disable_pdev:
Bjorn Helgaasc33ac892006-06-25 05:49:01 -07003027 pci_disable_device(pdev);
3028 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003031/*
3032 * Gets information about the local volumes attached to the controller.
3033 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034static void cciss_getgeometry(int cntl_num)
3035{
3036 ReportLunData_struct *ld_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 InquiryData_struct *inq_buff;
3038 int return_code;
3039 int i;
3040 int listlength = 0;
3041 __u32 lunid = 0;
3042 int block_size;
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003043 sector_t total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Eric Sesterhenn06ff37f2006-03-08 11:21:52 +01003045 ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003046 if (ld_buff == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 printk(KERN_ERR "cciss: out of memory\n");
3048 return;
3049 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003050 inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
3051 if (inq_buff == NULL) {
3052 printk(KERN_ERR "cciss: out of memory\n");
3053 kfree(ld_buff);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003054 return;
3055 }
3056 /* Get the firmware version */
3057 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
3058 sizeof(InquiryData_struct), 0, 0, 0, NULL,
3059 TYPE_CMD);
3060 if (return_code == IO_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
3062 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
3063 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
3064 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003065 } else { /* send command failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003067 printk(KERN_WARNING "cciss: unable to determine firmware"
3068 " version of controller\n");
3069 }
3070 /* Get the number of logical volumes */
3071 return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
3072 sizeof(ReportLunData_struct), 0, 0, 0, NULL,
3073 TYPE_CMD);
3074
3075 if (return_code == IO_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076#ifdef CCISS_DEBUG
3077 printk("LUN Data\n--------------------------\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003078#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003080 listlength |=
3081 (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
3082 listlength |=
3083 (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
3084 listlength |=
3085 (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003087 } else { /* reading number of logical volumes failed */
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 printk(KERN_WARNING "cciss: report logical volume"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003090 " command failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 listlength = 0;
3092 }
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003093 hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
3094 if (hba[cntl_num]->num_luns > CISS_MAX_LUN) {
3095 printk(KERN_ERR
3096 "ciss: only %d number of logical volumes supported\n",
3097 CISS_MAX_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 hba[cntl_num]->num_luns = CISS_MAX_LUN;
3099 }
3100#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003101 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n",
3102 ld_buff->LUNListLength[0], ld_buff->LUNListLength[1],
3103 ld_buff->LUNListLength[2], ld_buff->LUNListLength[3],
3104 hba[cntl_num]->num_luns);
3105#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003107 hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns - 1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003108 for (i = 0; i < CISS_MAX_LUN; i++) {
3109 if (i < hba[cntl_num]->num_luns) {
3110 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3]))
3111 << 24;
3112 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2]))
3113 << 16;
3114 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1]))
3115 << 8;
3116 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003118 hba[cntl_num]->drv[i].LunID = lunid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120#ifdef CCISS_DEBUG
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003121 printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
3122 ld_buff->LUN[i][0], ld_buff->LUN[i][1],
3123 ld_buff->LUN[i][2], ld_buff->LUN[i][3],
3124 hba[cntl_num]->drv[i].LunID);
3125#endif /* CCISS_DEBUG */
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003126
3127 /* testing to see if 16-byte CDBs are already being used */
3128 if(hba[cntl_num]->cciss_read == CCISS_READ_16) {
3129 cciss_read_capacity_16(cntl_num, i, 0,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003130 &total_size, &block_size);
Mike Miller (OS Dev)00988a32006-09-30 23:27:23 -07003131 goto geo_inq;
3132 }
3133 cciss_read_capacity(cntl_num, i, 0, &total_size, &block_size);
3134
3135 /* total_size = last LBA + 1 */
3136 if(total_size == (__u32) 0) {
3137 cciss_read_capacity_16(cntl_num, i, 0,
3138 &total_size, &block_size);
3139 hba[cntl_num]->cciss_read = CCISS_READ_16;
3140 hba[cntl_num]->cciss_write = CCISS_WRITE_16;
3141 } else {
3142 hba[cntl_num]->cciss_read = CCISS_READ_10;
3143 hba[cntl_num]->cciss_write = CCISS_WRITE_10;
3144 }
3145geo_inq:
Mike Millerddd47442005-09-13 01:25:22 -07003146 cciss_geometry_inquiry(cntl_num, i, 0, total_size,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003147 block_size, inq_buff,
3148 &hba[cntl_num]->drv[i]);
Mike Millerddd47442005-09-13 01:25:22 -07003149 } else {
3150 /* initialize raid_level to indicate a free space */
3151 hba[cntl_num]->drv[i].raid_level = -1;
3152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 }
3154 kfree(ld_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 kfree(inq_buff);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003156}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
3158/* Function to find the first free pointer into our hba[] array */
3159/* Returns -1 if no free entries are left. */
3160static int alloc_cciss_hba(void)
3161{
3162 struct gendisk *disk[NWD];
3163 int i, n;
3164 for (n = 0; n < NWD; n++) {
3165 disk[n] = alloc_disk(1 << NWD_SHIFT);
3166 if (!disk[n])
3167 goto out;
3168 }
3169
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003170 for (i = 0; i < MAX_CTLR; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 if (!hba[i]) {
3172 ctlr_info_t *p;
Eric Sesterhenn06ff37f2006-03-08 11:21:52 +01003173 p = kzalloc(sizeof(ctlr_info_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 if (!p)
3175 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 for (n = 0; n < NWD; n++)
3177 p->gendisk[n] = disk[n];
3178 hba[i] = p;
3179 return i;
3180 }
3181 }
3182 printk(KERN_WARNING "cciss: This driver supports a maximum"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003183 " of %d controllers.\n", MAX_CTLR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 goto out;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003185 Enomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 printk(KERN_ERR "cciss: out of memory.\n");
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003187 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 while (n--)
3189 put_disk(disk[n]);
3190 return -1;
3191}
3192
3193static void free_hba(int i)
3194{
3195 ctlr_info_t *p = hba[i];
3196 int n;
3197
3198 hba[i] = NULL;
3199 for (n = 0; n < NWD; n++)
3200 put_disk(p->gendisk[n]);
3201 kfree(p);
3202}
3203
3204/*
3205 * This is it. Find all the controllers and register them. I really hate
3206 * stealing all these major device numbers.
3207 * returns the number of block devices registered.
3208 */
3209static int __devinit cciss_init_one(struct pci_dev *pdev,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003210 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
3212 request_queue_t *q;
3213 int i;
3214 int j;
3215 int rc;
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003216 int dac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 i = alloc_cciss_hba();
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003219 if (i < 0)
Bjorn Helgaase2019b52006-06-25 05:49:05 -07003220 return -1;
Mike Miller1f8ef382005-09-13 01:25:21 -07003221
3222 hba[i]->busy_initializing = 1;
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (cciss_pci_init(hba[i], pdev) != 0)
3225 goto clean1;
3226
3227 sprintf(hba[i]->devname, "cciss%d", i);
3228 hba[i]->ctlr = i;
3229 hba[i]->pdev = pdev;
3230
3231 /* configure PCI DMA stuff */
mike.miller@hp.comeb0df992005-06-10 14:51:04 -05003232 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK))
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003233 dac = 1;
mike.miller@hp.comeb0df992005-06-10 14:51:04 -05003234 else if (!pci_set_dma_mask(pdev, DMA_32BIT_MASK))
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003235 dac = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 else {
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003237 printk(KERN_ERR "cciss: no suitable DMA available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 goto clean1;
3239 }
3240
3241 /*
3242 * register with the major number, or get a dynamic major number
3243 * by passing 0 as argument. This is done for greater than
3244 * 8 controller support.
3245 */
3246 if (i < MAX_CTLR_ORIG)
Christoph Hellwig564de742006-01-08 01:05:17 -08003247 hba[i]->major = COMPAQ_CISS_MAJOR + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 rc = register_blkdev(hba[i]->major, hba[i]->devname);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003249 if (rc == -EBUSY || rc == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 printk(KERN_ERR
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003251 "cciss: Unable to get major number %d for %s "
3252 "on hba %d\n", hba[i]->major, hba[i]->devname, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 goto clean1;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003254 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 if (i >= MAX_CTLR_ORIG)
3256 hba[i]->major = rc;
3257 }
3258
3259 /* make sure the board interrupts are off */
3260 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003261 if (request_irq(hba[i]->intr[SIMPLE_MODE_INT], do_cciss_intr,
Thomas Gleixner69ab3912006-07-01 19:29:32 -07003262 IRQF_DISABLED | IRQF_SHARED, hba[i]->devname, hba[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003264 hba[i]->intr[SIMPLE_MODE_INT], hba[i]->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 goto clean2;
3266 }
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003267
3268 printk(KERN_INFO "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003269 hba[i]->devname, pdev->device, pci_name(pdev),
3270 hba[i]->intr[SIMPLE_MODE_INT], dac ? "" : " not");
Bjorn Helgaas40aabb52006-06-25 05:49:03 -07003271
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003272 hba[i]->cmd_pool_bits =
3273 kmalloc(((NR_CMDS + BITS_PER_LONG -
3274 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
3275 hba[i]->cmd_pool = (CommandList_struct *)
3276 pci_alloc_consistent(hba[i]->pdev,
3277 NR_CMDS * sizeof(CommandList_struct),
3278 &(hba[i]->cmd_pool_dhandle));
3279 hba[i]->errinfo_pool = (ErrorInfo_struct *)
3280 pci_alloc_consistent(hba[i]->pdev,
3281 NR_CMDS * sizeof(ErrorInfo_struct),
3282 &(hba[i]->errinfo_pool_dhandle));
3283 if ((hba[i]->cmd_pool_bits == NULL)
3284 || (hba[i]->cmd_pool == NULL)
3285 || (hba[i]->errinfo_pool == NULL)) {
3286 printk(KERN_ERR "cciss: out of memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 goto clean4;
3288 }
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003289#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003290 hba[i]->scsi_rejects.complete =
3291 kmalloc(sizeof(hba[i]->scsi_rejects.complete[0]) *
3292 (NR_CMDS + 5), GFP_KERNEL);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003293 if (hba[i]->scsi_rejects.complete == NULL) {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003294 printk(KERN_ERR "cciss: out of memory");
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003295 goto clean4;
3296 }
3297#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 spin_lock_init(&hba[i]->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003300 /* Initialize the pdev driver private data.
3301 have it point to hba[i]. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 pci_set_drvdata(pdev, hba[i]);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003303 /* command and error info recs zeroed out before
3304 they are used */
3305 memset(hba[i]->cmd_pool_bits, 0,
3306 ((NR_CMDS + BITS_PER_LONG -
3307 1) / BITS_PER_LONG) * sizeof(unsigned long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003309#ifdef CCISS_DEBUG
3310 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n", i);
3311#endif /* CCISS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
3313 cciss_getgeometry(i);
3314
3315 cciss_scsi_setup(i);
3316
3317 /* Turn the interrupts on so we can service requests */
3318 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
3319
3320 cciss_procinit(i);
Mike Millerd6dbf422005-09-21 09:55:32 -07003321 hba[i]->busy_initializing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003323 for (j = 0; j < NWD; j++) { /* mfm */
Mike Millerad2b9312005-07-28 01:07:31 -07003324 drive_info_struct *drv = &(hba[i]->drv[j]);
3325 struct gendisk *disk = hba[i]->gendisk[j];
3326
3327 q = blk_init_queue(do_cciss_request, &hba[i]->lock);
3328 if (!q) {
3329 printk(KERN_ERR
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003330 "cciss: unable to allocate queue for disk %d\n",
3331 j);
Mike Millerad2b9312005-07-28 01:07:31 -07003332 break;
3333 }
3334 drv->queue = q;
3335
3336 q->backing_dev_info.ra_pages = READ_AHEAD;
Jens Axboea9925a02006-01-09 16:04:06 +01003337 blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Jens Axboea9925a02006-01-09 16:04:06 +01003339 /* This is a hardware imposed limit. */
3340 blk_queue_max_hw_segments(q, MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Jens Axboea9925a02006-01-09 16:04:06 +01003342 /* This is a limit in the driver and could be eliminated. */
3343 blk_queue_max_phys_segments(q, MAXSGENTRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Jens Axboea9925a02006-01-09 16:04:06 +01003345 blk_queue_max_sectors(q, 512);
3346
3347 blk_queue_softirq_done(q, cciss_softirq_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Mike Millerad2b9312005-07-28 01:07:31 -07003349 q->queuedata = hba[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 disk->major = hba[i]->major;
3352 disk->first_minor = j << NWD_SHIFT;
3353 disk->fops = &cciss_fops;
Mike Millerad2b9312005-07-28 01:07:31 -07003354 disk->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 disk->private_data = drv;
Greg Kroah-Hartman27c0ff82006-04-27 15:46:39 -07003356 disk->driverfs_dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 /* we must register the controller even if no disks exist */
3358 /* this is for the online array utilities */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003359 if (!drv->heads && j)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 continue;
Mike Millerad2b9312005-07-28 01:07:31 -07003361 blk_queue_hardsect_size(q, drv->block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 set_capacity(disk, drv->nr_blocks);
3363 add_disk(disk);
3364 }
Mike Millerad2b9312005-07-28 01:07:31 -07003365
Bjorn Helgaase2019b52006-06-25 05:49:05 -07003366 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003368 clean4:
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003369#ifdef CONFIG_CISS_SCSI_TAPE
Andrew Morton1acc0b02006-01-04 18:30:03 -08003370 kfree(hba[i]->scsi_rejects.complete);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003371#endif
Jesper Juhl6044ec82005-11-07 01:01:32 -08003372 kfree(hba[i]->cmd_pool_bits);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003373 if (hba[i]->cmd_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 pci_free_consistent(hba[i]->pdev,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003375 NR_CMDS * sizeof(CommandList_struct),
3376 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
3377 if (hba[i]->errinfo_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 pci_free_consistent(hba[i]->pdev,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003379 NR_CMDS * sizeof(ErrorInfo_struct),
3380 hba[i]->errinfo_pool,
3381 hba[i]->errinfo_pool_dhandle);
Mike Millerfb86a352006-01-08 01:03:50 -08003382 free_irq(hba[i]->intr[SIMPLE_MODE_INT], hba[i]);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003383 clean2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 unregister_blkdev(hba[i]->major, hba[i]->devname);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003385 clean1:
Mike Miller1f8ef382005-09-13 01:25:21 -07003386 hba[i]->busy_initializing = 0;
Patrick McHardy61808c22006-03-23 02:59:24 -08003387 free_hba(i);
Bjorn Helgaase2019b52006-06-25 05:49:05 -07003388 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003391static void __devexit cciss_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
3393 ctlr_info_t *tmp_ptr;
3394 int i, j;
3395 char flush_buf[4];
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003396 int return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003398 if (pci_get_drvdata(pdev) == NULL) {
3399 printk(KERN_ERR "cciss: Unable to remove device \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 return;
3401 }
3402 tmp_ptr = pci_get_drvdata(pdev);
3403 i = tmp_ptr->ctlr;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003404 if (hba[i] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 printk(KERN_ERR "cciss: device appears to "
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003406 "already be removed \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 return;
3408 }
3409 /* Turn board interrupts off and send the flush cache command */
3410 /* sendcmd will turn off interrupt, and send the flush...
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003411 * To write all data in the battery backed cache to disks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 memset(flush_buf, 0, 4);
3413 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003414 TYPE_CMD);
3415 if (return_code != IO_OK) {
3416 printk(KERN_WARNING "Error Flushing cache on controller %d\n",
3417 i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
Mike Millerfb86a352006-01-08 01:03:50 -08003419 free_irq(hba[i]->intr[2], hba[i]);
3420
3421#ifdef CONFIG_PCI_MSI
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003422 if (hba[i]->msix_vector)
3423 pci_disable_msix(hba[i]->pdev);
3424 else if (hba[i]->msi_vector)
3425 pci_disable_msi(hba[i]->pdev);
3426#endif /* CONFIG_PCI_MSI */
Mike Millerfb86a352006-01-08 01:03:50 -08003427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 iounmap(hba[i]->vaddr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003429 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 unregister_blkdev(hba[i]->major, hba[i]->devname);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003431 remove_proc_entry(hba[i]->devname, proc_cciss);
3432
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 /* remove it from the disk list */
3434 for (j = 0; j < NWD; j++) {
3435 struct gendisk *disk = hba[i]->gendisk[j];
mikem6f5a0f72005-11-18 22:05:36 +01003436 if (disk) {
3437 request_queue_t *q = disk->queue;
3438
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003439 if (disk->flags & GENHD_FL_UP)
mikem6f5a0f72005-11-18 22:05:36 +01003440 del_gendisk(disk);
3441 if (q)
3442 blk_cleanup_queue(q);
Mike Miller6a445d32005-09-13 01:25:23 -07003443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
3447 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003448 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof(ErrorInfo_struct),
3449 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 kfree(hba[i]->cmd_pool_bits);
mike.miller@hp.com3da8b712005-11-04 12:30:37 -06003451#ifdef CONFIG_CISS_SCSI_TAPE
3452 kfree(hba[i]->scsi_rejects.complete);
3453#endif
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003454 pci_release_regions(pdev);
Bjorn Helgaas4e570302006-06-25 05:49:02 -07003455 pci_disable_device(pdev);
3456 pci_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 free_hba(i);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
3460static struct pci_driver cciss_pci_driver = {
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003461 .name = "cciss",
3462 .probe = cciss_init_one,
3463 .remove = __devexit_p(cciss_remove_one),
3464 .id_table = cciss_pci_device_id, /* id_table */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465};
3466
3467/*
3468 * This is it. Register the PCI driver information for the cards we control
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003469 * the OS will call our registered routines when it finds one of our cards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 */
3471static int __init cciss_init(void)
3472{
3473 printk(KERN_INFO DRIVER_NAME "\n");
3474
3475 /* Register for our PCI devices */
Richard Knutsson9bfab8c2005-11-30 00:59:34 +01003476 return pci_register_driver(&cciss_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
3479static void __exit cciss_cleanup(void)
3480{
3481 int i;
3482
3483 pci_unregister_driver(&cciss_pci_driver);
3484 /* double check that all controller entrys have been removed */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003485 for (i = 0; i < MAX_CTLR; i++) {
3486 if (hba[i] != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 printk(KERN_WARNING "cciss: had to remove"
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003488 " controller %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 cciss_remove_one(hba[i]->pdev);
3490 }
3491 }
3492 remove_proc_entry("cciss", proc_root_driver);
3493}
3494
Mike Miller33079b22005-09-13 01:25:22 -07003495static void fail_all_cmds(unsigned long ctlr)
3496{
3497 /* If we get here, the board is apparently dead. */
3498 ctlr_info_t *h = hba[ctlr];
3499 CommandList_struct *c;
3500 unsigned long flags;
3501
3502 printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003503 h->alive = 0; /* the controller apparently died... */
Mike Miller33079b22005-09-13 01:25:22 -07003504
3505 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
3506
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003507 pci_disable_device(h->pdev); /* Make sure it is really dead. */
Mike Miller33079b22005-09-13 01:25:22 -07003508
3509 /* move everything off the request queue onto the completed queue */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003510 while ((c = h->reqQ) != NULL) {
Mike Miller33079b22005-09-13 01:25:22 -07003511 removeQ(&(h->reqQ), c);
3512 h->Qdepth--;
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003513 addQ(&(h->cmpQ), c);
Mike Miller33079b22005-09-13 01:25:22 -07003514 }
3515
3516 /* Now, fail everything on the completed queue with a HW error */
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003517 while ((c = h->cmpQ) != NULL) {
Mike Miller33079b22005-09-13 01:25:22 -07003518 removeQ(&h->cmpQ, c);
3519 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
3520 if (c->cmd_type == CMD_RWREQ) {
3521 complete_command(h, c, 0);
3522 } else if (c->cmd_type == CMD_IOCTL_PEND)
3523 complete(c->waiting);
3524#ifdef CONFIG_CISS_SCSI_TAPE
Bjorn Helgaas7c832832006-06-25 05:49:06 -07003525 else if (c->cmd_type == CMD_SCSI)
3526 complete_scsi_command(c, 0, 0);
Mike Miller33079b22005-09-13 01:25:22 -07003527#endif
3528 }
3529 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
3530 return;
3531}
3532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533module_init(cciss_init);
3534module_exit(cciss_cleanup);