[SCSI] hpsa: eliminate lock_kernel in compat_ioctl
[linux-2.6/next.git] / drivers / scsi / hpsa.c
blob225a787dedf343d280656ec5f47199529c0226f6
1 /*
2 * Disk Array driver for HP Smart Array SAS controllers
3 * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P.
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; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12 * NON INFRINGEMENT. See the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * Questions/Comments/Bugfixes to iss_storagedev@hp.com
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/types.h>
25 #include <linux/pci.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
29 #include <linux/fs.h>
30 #include <linux/timer.h>
31 #include <linux/seq_file.h>
32 #include <linux/init.h>
33 #include <linux/spinlock.h>
34 #include <linux/smp_lock.h>
35 #include <linux/compat.h>
36 #include <linux/blktrace_api.h>
37 #include <linux/uaccess.h>
38 #include <linux/io.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/completion.h>
41 #include <linux/moduleparam.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_cmnd.h>
44 #include <scsi/scsi_device.h>
45 #include <scsi/scsi_host.h>
46 #include <linux/cciss_ioctl.h>
47 #include <linux/string.h>
48 #include <linux/bitmap.h>
49 #include <asm/atomic.h>
50 #include <linux/kthread.h>
51 #include "hpsa_cmd.h"
52 #include "hpsa.h"
54 /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
55 #define HPSA_DRIVER_VERSION "1.0.0"
56 #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
58 /* How long to wait (in milliseconds) for board to go into simple mode */
59 #define MAX_CONFIG_WAIT 30000
60 #define MAX_IOCTL_CONFIG_WAIT 1000
62 /*define how many times we will try a command because of bus resets */
63 #define MAX_CMD_RETRIES 3
65 /* Embedded module documentation macros - see modules.h */
66 MODULE_AUTHOR("Hewlett-Packard Company");
67 MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
68 HPSA_DRIVER_VERSION);
69 MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
70 MODULE_VERSION(HPSA_DRIVER_VERSION);
71 MODULE_LICENSE("GPL");
73 static int hpsa_allow_any;
74 module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
75 MODULE_PARM_DESC(hpsa_allow_any,
76 "Allow hpsa driver to access unknown HP Smart Array hardware");
78 /* define the PCI info for the cards we can control */
79 static const struct pci_device_id hpsa_pci_device_id[] = {
80 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
81 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
82 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
83 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
84 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
85 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a},
86 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b},
87 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
88 #define PCI_DEVICE_ID_HP_CISSF 0x333f
89 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x333F},
90 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
91 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
92 {0,}
95 MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
97 /* board_id = Subsystem Device ID & Vendor ID
98 * product = Marketing Name for the board
99 * access = Address of the struct of function pointers
101 static struct board_type products[] = {
102 {0x3241103C, "Smart Array P212", &SA5_access},
103 {0x3243103C, "Smart Array P410", &SA5_access},
104 {0x3245103C, "Smart Array P410i", &SA5_access},
105 {0x3247103C, "Smart Array P411", &SA5_access},
106 {0x3249103C, "Smart Array P812", &SA5_access},
107 {0x324a103C, "Smart Array P712m", &SA5_access},
108 {0x324b103C, "Smart Array P711m", &SA5_access},
109 {0x3233103C, "StorageWorks P1210m", &SA5_access},
110 {0x333F103C, "StorageWorks P1210m", &SA5_access},
111 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
114 static int number_of_controllers;
116 static irqreturn_t do_hpsa_intr(int irq, void *dev_id);
117 static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
118 static void start_io(struct ctlr_info *h);
120 #ifdef CONFIG_COMPAT
121 static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
122 #endif
124 static void cmd_free(struct ctlr_info *h, struct CommandList *c);
125 static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
126 static struct CommandList *cmd_alloc(struct ctlr_info *h);
127 static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
128 static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
129 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
130 int cmd_type);
132 static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
133 void (*done)(struct scsi_cmnd *));
134 static void hpsa_scan_start(struct Scsi_Host *);
135 static int hpsa_scan_finished(struct Scsi_Host *sh,
136 unsigned long elapsed_time);
138 static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
139 static int hpsa_slave_alloc(struct scsi_device *sdev);
140 static void hpsa_slave_destroy(struct scsi_device *sdev);
142 static ssize_t raid_level_show(struct device *dev,
143 struct device_attribute *attr, char *buf);
144 static ssize_t lunid_show(struct device *dev,
145 struct device_attribute *attr, char *buf);
146 static ssize_t unique_id_show(struct device *dev,
147 struct device_attribute *attr, char *buf);
148 static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
149 static ssize_t host_store_rescan(struct device *dev,
150 struct device_attribute *attr, const char *buf, size_t count);
151 static int check_for_unit_attention(struct ctlr_info *h,
152 struct CommandList *c);
153 static void check_ioctl_unit_attention(struct ctlr_info *h,
154 struct CommandList *c);
155 /* performant mode helper functions */
156 static void calc_bucket_map(int *bucket, int num_buckets,
157 int nsgs, int *bucket_map);
158 static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
159 static inline u32 next_command(struct ctlr_info *h);
161 static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
162 static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
163 static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
164 static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
166 static struct device_attribute *hpsa_sdev_attrs[] = {
167 &dev_attr_raid_level,
168 &dev_attr_lunid,
169 &dev_attr_unique_id,
170 NULL,
173 static struct device_attribute *hpsa_shost_attrs[] = {
174 &dev_attr_rescan,
175 NULL,
178 static struct scsi_host_template hpsa_driver_template = {
179 .module = THIS_MODULE,
180 .name = "hpsa",
181 .proc_name = "hpsa",
182 .queuecommand = hpsa_scsi_queue_command,
183 .scan_start = hpsa_scan_start,
184 .scan_finished = hpsa_scan_finished,
185 .this_id = -1,
186 .sg_tablesize = MAXSGENTRIES,
187 .use_clustering = ENABLE_CLUSTERING,
188 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
189 .ioctl = hpsa_ioctl,
190 .slave_alloc = hpsa_slave_alloc,
191 .slave_destroy = hpsa_slave_destroy,
192 #ifdef CONFIG_COMPAT
193 .compat_ioctl = hpsa_compat_ioctl,
194 #endif
195 .sdev_attrs = hpsa_sdev_attrs,
196 .shost_attrs = hpsa_shost_attrs,
199 static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
201 unsigned long *priv = shost_priv(sdev->host);
202 return (struct ctlr_info *) *priv;
205 static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
207 unsigned long *priv = shost_priv(sh);
208 return (struct ctlr_info *) *priv;
211 static struct task_struct *hpsa_scan_thread;
212 static DEFINE_MUTEX(hpsa_scan_mutex);
213 static LIST_HEAD(hpsa_scan_q);
214 static int hpsa_scan_func(void *data);
217 * add_to_scan_list() - add controller to rescan queue
218 * @h: Pointer to the controller.
220 * Adds the controller to the rescan queue if not already on the queue.
222 * returns 1 if added to the queue, 0 if skipped (could be on the
223 * queue already, or the controller could be initializing or shutting
224 * down).
226 static int add_to_scan_list(struct ctlr_info *h)
228 struct ctlr_info *test_h;
229 int found = 0;
230 int ret = 0;
232 if (h->busy_initializing)
233 return 0;
236 * If we don't get the lock, it means the driver is unloading
237 * and there's no point in scheduling a new scan.
239 if (!mutex_trylock(&h->busy_shutting_down))
240 return 0;
242 mutex_lock(&hpsa_scan_mutex);
243 list_for_each_entry(test_h, &hpsa_scan_q, scan_list) {
244 if (test_h == h) {
245 found = 1;
246 break;
249 if (!found && !h->busy_scanning) {
250 INIT_COMPLETION(h->scan_wait);
251 list_add_tail(&h->scan_list, &hpsa_scan_q);
252 ret = 1;
254 mutex_unlock(&hpsa_scan_mutex);
255 mutex_unlock(&h->busy_shutting_down);
257 return ret;
261 * remove_from_scan_list() - remove controller from rescan queue
262 * @h: Pointer to the controller.
264 * Removes the controller from the rescan queue if present. Blocks if
265 * the controller is currently conducting a rescan. The controller
266 * can be in one of three states:
267 * 1. Doesn't need a scan
268 * 2. On the scan list, but not scanning yet (we remove it)
269 * 3. Busy scanning (and not on the list). In this case we want to wait for
270 * the scan to complete to make sure the scanning thread for this
271 * controller is completely idle.
273 static void remove_from_scan_list(struct ctlr_info *h)
275 struct ctlr_info *test_h, *tmp_h;
277 mutex_lock(&hpsa_scan_mutex);
278 list_for_each_entry_safe(test_h, tmp_h, &hpsa_scan_q, scan_list) {
279 if (test_h == h) { /* state 2. */
280 list_del(&h->scan_list);
281 complete_all(&h->scan_wait);
282 mutex_unlock(&hpsa_scan_mutex);
283 return;
286 if (h->busy_scanning) { /* state 3. */
287 mutex_unlock(&hpsa_scan_mutex);
288 wait_for_completion(&h->scan_wait);
289 } else { /* state 1, nothing to do. */
290 mutex_unlock(&hpsa_scan_mutex);
294 /* hpsa_scan_func() - kernel thread used to rescan controllers
295 * @data: Ignored.
297 * A kernel thread used scan for drive topology changes on
298 * controllers. The thread processes only one controller at a time
299 * using a queue. Controllers are added to the queue using
300 * add_to_scan_list() and removed from the queue either after done
301 * processing or using remove_from_scan_list().
303 * returns 0.
305 static int hpsa_scan_func(__attribute__((unused)) void *data)
307 struct ctlr_info *h;
308 int host_no;
310 while (1) {
311 set_current_state(TASK_INTERRUPTIBLE);
312 schedule();
313 if (kthread_should_stop())
314 break;
316 while (1) {
317 mutex_lock(&hpsa_scan_mutex);
318 if (list_empty(&hpsa_scan_q)) {
319 mutex_unlock(&hpsa_scan_mutex);
320 break;
322 h = list_entry(hpsa_scan_q.next, struct ctlr_info,
323 scan_list);
324 list_del(&h->scan_list);
325 h->busy_scanning = 1;
326 mutex_unlock(&hpsa_scan_mutex);
327 host_no = h->scsi_host ? h->scsi_host->host_no : -1;
328 hpsa_scan_start(h->scsi_host);
329 complete_all(&h->scan_wait);
330 mutex_lock(&hpsa_scan_mutex);
331 h->busy_scanning = 0;
332 mutex_unlock(&hpsa_scan_mutex);
335 return 0;
338 static int check_for_unit_attention(struct ctlr_info *h,
339 struct CommandList *c)
341 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
342 return 0;
344 switch (c->err_info->SenseInfo[12]) {
345 case STATE_CHANGED:
346 dev_warn(&h->pdev->dev, "hpsa%d: a state change "
347 "detected, command retried\n", h->ctlr);
348 break;
349 case LUN_FAILED:
350 dev_warn(&h->pdev->dev, "hpsa%d: LUN failure "
351 "detected, action required\n", h->ctlr);
352 break;
353 case REPORT_LUNS_CHANGED:
354 dev_warn(&h->pdev->dev, "hpsa%d: report LUN data "
355 "changed\n", h->ctlr);
357 * Here, we could call add_to_scan_list and wake up the scan thread,
358 * except that it's quite likely that we will get more than one
359 * REPORT_LUNS_CHANGED condition in quick succession, which means
360 * that those which occur after the first one will likely happen
361 * *during* the hpsa_scan_thread's rescan. And the rescan code is not
362 * robust enough to restart in the middle, undoing what it has already
363 * done, and it's not clear that it's even possible to do this, since
364 * part of what it does is notify the SCSI mid layer, which starts
365 * doing it's own i/o to read partition tables and so on, and the
366 * driver doesn't have visibility to know what might need undoing.
367 * In any event, if possible, it is horribly complicated to get right
368 * so we just don't do it for now.
370 * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
372 break;
373 case POWER_OR_RESET:
374 dev_warn(&h->pdev->dev, "hpsa%d: a power on "
375 "or device reset detected\n", h->ctlr);
376 break;
377 case UNIT_ATTENTION_CLEARED:
378 dev_warn(&h->pdev->dev, "hpsa%d: unit attention "
379 "cleared by another initiator\n", h->ctlr);
380 break;
381 default:
382 dev_warn(&h->pdev->dev, "hpsa%d: unknown "
383 "unit attention detected\n", h->ctlr);
384 break;
386 return 1;
389 static ssize_t host_store_rescan(struct device *dev,
390 struct device_attribute *attr,
391 const char *buf, size_t count)
393 struct ctlr_info *h;
394 struct Scsi_Host *shost = class_to_shost(dev);
395 h = shost_to_hba(shost);
396 if (add_to_scan_list(h)) {
397 wake_up_process(hpsa_scan_thread);
398 wait_for_completion_interruptible(&h->scan_wait);
400 return count;
403 /* Enqueuing and dequeuing functions for cmdlists. */
404 static inline void addQ(struct hlist_head *list, struct CommandList *c)
406 hlist_add_head(&c->list, list);
409 static inline u32 next_command(struct ctlr_info *h)
411 u32 a;
413 if (unlikely(h->transMethod != CFGTBL_Trans_Performant))
414 return h->access.command_completed(h);
416 if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) {
417 a = *(h->reply_pool_head); /* Next cmd in ring buffer */
418 (h->reply_pool_head)++;
419 h->commands_outstanding--;
420 } else {
421 a = FIFO_EMPTY;
423 /* Check for wraparound */
424 if (h->reply_pool_head == (h->reply_pool + h->max_commands)) {
425 h->reply_pool_head = h->reply_pool;
426 h->reply_pool_wraparound ^= 1;
428 return a;
431 /* set_performant_mode: Modify the tag for cciss performant
432 * set bit 0 for pull model, bits 3-1 for block fetch
433 * register number
435 static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
437 if (likely(h->transMethod == CFGTBL_Trans_Performant))
438 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
441 static void enqueue_cmd_and_start_io(struct ctlr_info *h,
442 struct CommandList *c)
444 unsigned long flags;
446 set_performant_mode(h, c);
447 spin_lock_irqsave(&h->lock, flags);
448 addQ(&h->reqQ, c);
449 h->Qdepth++;
450 start_io(h);
451 spin_unlock_irqrestore(&h->lock, flags);
454 static inline void removeQ(struct CommandList *c)
456 if (WARN_ON(hlist_unhashed(&c->list)))
457 return;
458 hlist_del_init(&c->list);
461 static inline int is_hba_lunid(unsigned char scsi3addr[])
463 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
466 static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
468 return (scsi3addr[3] & 0xC0) == 0x40;
471 static inline int is_scsi_rev_5(struct ctlr_info *h)
473 if (!h->hba_inquiry_data)
474 return 0;
475 if ((h->hba_inquiry_data[2] & 0x07) == 5)
476 return 1;
477 return 0;
480 static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
481 "UNKNOWN"
483 #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
485 static ssize_t raid_level_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
488 ssize_t l = 0;
489 unsigned char rlevel;
490 struct ctlr_info *h;
491 struct scsi_device *sdev;
492 struct hpsa_scsi_dev_t *hdev;
493 unsigned long flags;
495 sdev = to_scsi_device(dev);
496 h = sdev_to_hba(sdev);
497 spin_lock_irqsave(&h->lock, flags);
498 hdev = sdev->hostdata;
499 if (!hdev) {
500 spin_unlock_irqrestore(&h->lock, flags);
501 return -ENODEV;
504 /* Is this even a logical drive? */
505 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
506 spin_unlock_irqrestore(&h->lock, flags);
507 l = snprintf(buf, PAGE_SIZE, "N/A\n");
508 return l;
511 rlevel = hdev->raid_level;
512 spin_unlock_irqrestore(&h->lock, flags);
513 if (rlevel > RAID_UNKNOWN)
514 rlevel = RAID_UNKNOWN;
515 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
516 return l;
519 static ssize_t lunid_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
522 struct ctlr_info *h;
523 struct scsi_device *sdev;
524 struct hpsa_scsi_dev_t *hdev;
525 unsigned long flags;
526 unsigned char lunid[8];
528 sdev = to_scsi_device(dev);
529 h = sdev_to_hba(sdev);
530 spin_lock_irqsave(&h->lock, flags);
531 hdev = sdev->hostdata;
532 if (!hdev) {
533 spin_unlock_irqrestore(&h->lock, flags);
534 return -ENODEV;
536 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
537 spin_unlock_irqrestore(&h->lock, flags);
538 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
539 lunid[0], lunid[1], lunid[2], lunid[3],
540 lunid[4], lunid[5], lunid[6], lunid[7]);
543 static ssize_t unique_id_show(struct device *dev,
544 struct device_attribute *attr, char *buf)
546 struct ctlr_info *h;
547 struct scsi_device *sdev;
548 struct hpsa_scsi_dev_t *hdev;
549 unsigned long flags;
550 unsigned char sn[16];
552 sdev = to_scsi_device(dev);
553 h = sdev_to_hba(sdev);
554 spin_lock_irqsave(&h->lock, flags);
555 hdev = sdev->hostdata;
556 if (!hdev) {
557 spin_unlock_irqrestore(&h->lock, flags);
558 return -ENODEV;
560 memcpy(sn, hdev->device_id, sizeof(sn));
561 spin_unlock_irqrestore(&h->lock, flags);
562 return snprintf(buf, 16 * 2 + 2,
563 "%02X%02X%02X%02X%02X%02X%02X%02X"
564 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
565 sn[0], sn[1], sn[2], sn[3],
566 sn[4], sn[5], sn[6], sn[7],
567 sn[8], sn[9], sn[10], sn[11],
568 sn[12], sn[13], sn[14], sn[15]);
571 static int hpsa_find_target_lun(struct ctlr_info *h,
572 unsigned char scsi3addr[], int bus, int *target, int *lun)
574 /* finds an unused bus, target, lun for a new physical device
575 * assumes h->devlock is held
577 int i, found = 0;
578 DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA);
580 memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3);
582 for (i = 0; i < h->ndevices; i++) {
583 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
584 set_bit(h->dev[i]->target, lun_taken);
587 for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) {
588 if (!test_bit(i, lun_taken)) {
589 /* *bus = 1; */
590 *target = i;
591 *lun = 0;
592 found = 1;
593 break;
596 return !found;
599 /* Add an entry into h->dev[] array. */
600 static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
601 struct hpsa_scsi_dev_t *device,
602 struct hpsa_scsi_dev_t *added[], int *nadded)
604 /* assumes h->devlock is held */
605 int n = h->ndevices;
606 int i;
607 unsigned char addr1[8], addr2[8];
608 struct hpsa_scsi_dev_t *sd;
610 if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) {
611 dev_err(&h->pdev->dev, "too many devices, some will be "
612 "inaccessible.\n");
613 return -1;
616 /* physical devices do not have lun or target assigned until now. */
617 if (device->lun != -1)
618 /* Logical device, lun is already assigned. */
619 goto lun_assigned;
621 /* If this device a non-zero lun of a multi-lun device
622 * byte 4 of the 8-byte LUN addr will contain the logical
623 * unit no, zero otherise.
625 if (device->scsi3addr[4] == 0) {
626 /* This is not a non-zero lun of a multi-lun device */
627 if (hpsa_find_target_lun(h, device->scsi3addr,
628 device->bus, &device->target, &device->lun) != 0)
629 return -1;
630 goto lun_assigned;
633 /* This is a non-zero lun of a multi-lun device.
634 * Search through our list and find the device which
635 * has the same 8 byte LUN address, excepting byte 4.
636 * Assign the same bus and target for this new LUN.
637 * Use the logical unit number from the firmware.
639 memcpy(addr1, device->scsi3addr, 8);
640 addr1[4] = 0;
641 for (i = 0; i < n; i++) {
642 sd = h->dev[i];
643 memcpy(addr2, sd->scsi3addr, 8);
644 addr2[4] = 0;
645 /* differ only in byte 4? */
646 if (memcmp(addr1, addr2, 8) == 0) {
647 device->bus = sd->bus;
648 device->target = sd->target;
649 device->lun = device->scsi3addr[4];
650 break;
653 if (device->lun == -1) {
654 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
655 " suspect firmware bug or unsupported hardware "
656 "configuration.\n");
657 return -1;
660 lun_assigned:
662 h->dev[n] = device;
663 h->ndevices++;
664 added[*nadded] = device;
665 (*nadded)++;
667 /* initially, (before registering with scsi layer) we don't
668 * know our hostno and we don't want to print anything first
669 * time anyway (the scsi layer's inquiries will show that info)
671 /* if (hostno != -1) */
672 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
673 scsi_device_type(device->devtype), hostno,
674 device->bus, device->target, device->lun);
675 return 0;
678 /* Replace an entry from h->dev[] array. */
679 static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
680 int entry, struct hpsa_scsi_dev_t *new_entry,
681 struct hpsa_scsi_dev_t *added[], int *nadded,
682 struct hpsa_scsi_dev_t *removed[], int *nremoved)
684 /* assumes h->devlock is held */
685 BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
686 removed[*nremoved] = h->dev[entry];
687 (*nremoved)++;
688 h->dev[entry] = new_entry;
689 added[*nadded] = new_entry;
690 (*nadded)++;
691 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
692 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
693 new_entry->target, new_entry->lun);
696 /* Remove an entry from h->dev[] array. */
697 static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
698 struct hpsa_scsi_dev_t *removed[], int *nremoved)
700 /* assumes h->devlock is held */
701 int i;
702 struct hpsa_scsi_dev_t *sd;
704 BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
706 sd = h->dev[entry];
707 removed[*nremoved] = h->dev[entry];
708 (*nremoved)++;
710 for (i = entry; i < h->ndevices-1; i++)
711 h->dev[i] = h->dev[i+1];
712 h->ndevices--;
713 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
714 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
715 sd->lun);
718 #define SCSI3ADDR_EQ(a, b) ( \
719 (a)[7] == (b)[7] && \
720 (a)[6] == (b)[6] && \
721 (a)[5] == (b)[5] && \
722 (a)[4] == (b)[4] && \
723 (a)[3] == (b)[3] && \
724 (a)[2] == (b)[2] && \
725 (a)[1] == (b)[1] && \
726 (a)[0] == (b)[0])
728 static void fixup_botched_add(struct ctlr_info *h,
729 struct hpsa_scsi_dev_t *added)
731 /* called when scsi_add_device fails in order to re-adjust
732 * h->dev[] to match the mid layer's view.
734 unsigned long flags;
735 int i, j;
737 spin_lock_irqsave(&h->lock, flags);
738 for (i = 0; i < h->ndevices; i++) {
739 if (h->dev[i] == added) {
740 for (j = i; j < h->ndevices-1; j++)
741 h->dev[j] = h->dev[j+1];
742 h->ndevices--;
743 break;
746 spin_unlock_irqrestore(&h->lock, flags);
747 kfree(added);
750 static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
751 struct hpsa_scsi_dev_t *dev2)
753 if ((is_logical_dev_addr_mode(dev1->scsi3addr) ||
754 (dev1->lun != -1 && dev2->lun != -1)) &&
755 dev1->devtype != 0x0C)
756 return (memcmp(dev1, dev2, sizeof(*dev1)) == 0);
758 /* we compare everything except lun and target as these
759 * are not yet assigned. Compare parts likely
760 * to differ first
762 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
763 sizeof(dev1->scsi3addr)) != 0)
764 return 0;
765 if (memcmp(dev1->device_id, dev2->device_id,
766 sizeof(dev1->device_id)) != 0)
767 return 0;
768 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
769 return 0;
770 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
771 return 0;
772 if (memcmp(dev1->revision, dev2->revision, sizeof(dev1->revision)) != 0)
773 return 0;
774 if (dev1->devtype != dev2->devtype)
775 return 0;
776 if (dev1->raid_level != dev2->raid_level)
777 return 0;
778 if (dev1->bus != dev2->bus)
779 return 0;
780 return 1;
783 /* Find needle in haystack. If exact match found, return DEVICE_SAME,
784 * and return needle location in *index. If scsi3addr matches, but not
785 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
786 * location in *index. If needle not found, return DEVICE_NOT_FOUND.
788 static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
789 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
790 int *index)
792 int i;
793 #define DEVICE_NOT_FOUND 0
794 #define DEVICE_CHANGED 1
795 #define DEVICE_SAME 2
796 for (i = 0; i < haystack_size; i++) {
797 if (haystack[i] == NULL) /* previously removed. */
798 continue;
799 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
800 *index = i;
801 if (device_is_the_same(needle, haystack[i]))
802 return DEVICE_SAME;
803 else
804 return DEVICE_CHANGED;
807 *index = -1;
808 return DEVICE_NOT_FOUND;
811 static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
812 struct hpsa_scsi_dev_t *sd[], int nsds)
814 /* sd contains scsi3 addresses and devtypes, and inquiry
815 * data. This function takes what's in sd to be the current
816 * reality and updates h->dev[] to reflect that reality.
818 int i, entry, device_change, changes = 0;
819 struct hpsa_scsi_dev_t *csd;
820 unsigned long flags;
821 struct hpsa_scsi_dev_t **added, **removed;
822 int nadded, nremoved;
823 struct Scsi_Host *sh = NULL;
825 added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA,
826 GFP_KERNEL);
827 removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA,
828 GFP_KERNEL);
830 if (!added || !removed) {
831 dev_warn(&h->pdev->dev, "out of memory in "
832 "adjust_hpsa_scsi_table\n");
833 goto free_and_out;
836 spin_lock_irqsave(&h->devlock, flags);
838 /* find any devices in h->dev[] that are not in
839 * sd[] and remove them from h->dev[], and for any
840 * devices which have changed, remove the old device
841 * info and add the new device info.
843 i = 0;
844 nremoved = 0;
845 nadded = 0;
846 while (i < h->ndevices) {
847 csd = h->dev[i];
848 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
849 if (device_change == DEVICE_NOT_FOUND) {
850 changes++;
851 hpsa_scsi_remove_entry(h, hostno, i,
852 removed, &nremoved);
853 continue; /* remove ^^^, hence i not incremented */
854 } else if (device_change == DEVICE_CHANGED) {
855 changes++;
856 hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
857 added, &nadded, removed, &nremoved);
858 /* Set it to NULL to prevent it from being freed
859 * at the bottom of hpsa_update_scsi_devices()
861 sd[entry] = NULL;
863 i++;
866 /* Now, make sure every device listed in sd[] is also
867 * listed in h->dev[], adding them if they aren't found
870 for (i = 0; i < nsds; i++) {
871 if (!sd[i]) /* if already added above. */
872 continue;
873 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
874 h->ndevices, &entry);
875 if (device_change == DEVICE_NOT_FOUND) {
876 changes++;
877 if (hpsa_scsi_add_entry(h, hostno, sd[i],
878 added, &nadded) != 0)
879 break;
880 sd[i] = NULL; /* prevent from being freed later. */
881 } else if (device_change == DEVICE_CHANGED) {
882 /* should never happen... */
883 changes++;
884 dev_warn(&h->pdev->dev,
885 "device unexpectedly changed.\n");
886 /* but if it does happen, we just ignore that device */
889 spin_unlock_irqrestore(&h->devlock, flags);
891 /* Don't notify scsi mid layer of any changes the first time through
892 * (or if there are no changes) scsi_scan_host will do it later the
893 * first time through.
895 if (hostno == -1 || !changes)
896 goto free_and_out;
898 sh = h->scsi_host;
899 /* Notify scsi mid layer of any removed devices */
900 for (i = 0; i < nremoved; i++) {
901 struct scsi_device *sdev =
902 scsi_device_lookup(sh, removed[i]->bus,
903 removed[i]->target, removed[i]->lun);
904 if (sdev != NULL) {
905 scsi_remove_device(sdev);
906 scsi_device_put(sdev);
907 } else {
908 /* We don't expect to get here.
909 * future cmds to this device will get selection
910 * timeout as if the device was gone.
912 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
913 " for removal.", hostno, removed[i]->bus,
914 removed[i]->target, removed[i]->lun);
916 kfree(removed[i]);
917 removed[i] = NULL;
920 /* Notify scsi mid layer of any added devices */
921 for (i = 0; i < nadded; i++) {
922 if (scsi_add_device(sh, added[i]->bus,
923 added[i]->target, added[i]->lun) == 0)
924 continue;
925 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
926 "device not added.\n", hostno, added[i]->bus,
927 added[i]->target, added[i]->lun);
928 /* now we have to remove it from h->dev,
929 * since it didn't get added to scsi mid layer
931 fixup_botched_add(h, added[i]);
934 free_and_out:
935 kfree(added);
936 kfree(removed);
940 * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
941 * Assume's h->devlock is held.
943 static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
944 int bus, int target, int lun)
946 int i;
947 struct hpsa_scsi_dev_t *sd;
949 for (i = 0; i < h->ndevices; i++) {
950 sd = h->dev[i];
951 if (sd->bus == bus && sd->target == target && sd->lun == lun)
952 return sd;
954 return NULL;
957 /* link sdev->hostdata to our per-device structure. */
958 static int hpsa_slave_alloc(struct scsi_device *sdev)
960 struct hpsa_scsi_dev_t *sd;
961 unsigned long flags;
962 struct ctlr_info *h;
964 h = sdev_to_hba(sdev);
965 spin_lock_irqsave(&h->devlock, flags);
966 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
967 sdev_id(sdev), sdev->lun);
968 if (sd != NULL)
969 sdev->hostdata = sd;
970 spin_unlock_irqrestore(&h->devlock, flags);
971 return 0;
974 static void hpsa_slave_destroy(struct scsi_device *sdev)
976 /* nothing to do. */
979 static void hpsa_scsi_setup(struct ctlr_info *h)
981 h->ndevices = 0;
982 h->scsi_host = NULL;
983 spin_lock_init(&h->devlock);
986 static void complete_scsi_command(struct CommandList *cp,
987 int timeout, u32 tag)
989 struct scsi_cmnd *cmd;
990 struct ctlr_info *h;
991 struct ErrorInfo *ei;
993 unsigned char sense_key;
994 unsigned char asc; /* additional sense code */
995 unsigned char ascq; /* additional sense code qualifier */
997 ei = cp->err_info;
998 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
999 h = cp->h;
1001 scsi_dma_unmap(cmd); /* undo the DMA mappings */
1003 cmd->result = (DID_OK << 16); /* host byte */
1004 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
1005 cmd->result |= (ei->ScsiStatus << 1);
1007 /* copy the sense data whether we need to or not. */
1008 memcpy(cmd->sense_buffer, ei->SenseInfo,
1009 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
1010 SCSI_SENSE_BUFFERSIZE :
1011 ei->SenseLen);
1012 scsi_set_resid(cmd, ei->ResidualCnt);
1014 if (ei->CommandStatus == 0) {
1015 cmd->scsi_done(cmd);
1016 cmd_free(h, cp);
1017 return;
1020 /* an error has occurred */
1021 switch (ei->CommandStatus) {
1023 case CMD_TARGET_STATUS:
1024 if (ei->ScsiStatus) {
1025 /* Get sense key */
1026 sense_key = 0xf & ei->SenseInfo[2];
1027 /* Get additional sense code */
1028 asc = ei->SenseInfo[12];
1029 /* Get addition sense code qualifier */
1030 ascq = ei->SenseInfo[13];
1033 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
1034 if (check_for_unit_attention(h, cp)) {
1035 cmd->result = DID_SOFT_ERROR << 16;
1036 break;
1038 if (sense_key == ILLEGAL_REQUEST) {
1040 * SCSI REPORT_LUNS is commonly unsupported on
1041 * Smart Array. Suppress noisy complaint.
1043 if (cp->Request.CDB[0] == REPORT_LUNS)
1044 break;
1046 /* If ASC/ASCQ indicate Logical Unit
1047 * Not Supported condition,
1049 if ((asc == 0x25) && (ascq == 0x0)) {
1050 dev_warn(&h->pdev->dev, "cp %p "
1051 "has check condition\n", cp);
1052 break;
1056 if (sense_key == NOT_READY) {
1057 /* If Sense is Not Ready, Logical Unit
1058 * Not ready, Manual Intervention
1059 * required
1061 if ((asc == 0x04) && (ascq == 0x03)) {
1062 dev_warn(&h->pdev->dev, "cp %p "
1063 "has check condition: unit "
1064 "not ready, manual "
1065 "intervention required\n", cp);
1066 break;
1069 if (sense_key == ABORTED_COMMAND) {
1070 /* Aborted command is retryable */
1071 dev_warn(&h->pdev->dev, "cp %p "
1072 "has check condition: aborted command: "
1073 "ASC: 0x%x, ASCQ: 0x%x\n",
1074 cp, asc, ascq);
1075 cmd->result = DID_SOFT_ERROR << 16;
1076 break;
1078 /* Must be some other type of check condition */
1079 dev_warn(&h->pdev->dev, "cp %p has check condition: "
1080 "unknown type: "
1081 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1082 "Returning result: 0x%x, "
1083 "cmd=[%02x %02x %02x %02x %02x "
1084 "%02x %02x %02x %02x %02x %02x "
1085 "%02x %02x %02x %02x %02x]\n",
1086 cp, sense_key, asc, ascq,
1087 cmd->result,
1088 cmd->cmnd[0], cmd->cmnd[1],
1089 cmd->cmnd[2], cmd->cmnd[3],
1090 cmd->cmnd[4], cmd->cmnd[5],
1091 cmd->cmnd[6], cmd->cmnd[7],
1092 cmd->cmnd[8], cmd->cmnd[9],
1093 cmd->cmnd[10], cmd->cmnd[11],
1094 cmd->cmnd[12], cmd->cmnd[13],
1095 cmd->cmnd[14], cmd->cmnd[15]);
1096 break;
1100 /* Problem was not a check condition
1101 * Pass it up to the upper layers...
1103 if (ei->ScsiStatus) {
1104 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1105 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1106 "Returning result: 0x%x\n",
1107 cp, ei->ScsiStatus,
1108 sense_key, asc, ascq,
1109 cmd->result);
1110 } else { /* scsi status is zero??? How??? */
1111 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1112 "Returning no connection.\n", cp),
1114 /* Ordinarily, this case should never happen,
1115 * but there is a bug in some released firmware
1116 * revisions that allows it to happen if, for
1117 * example, a 4100 backplane loses power and
1118 * the tape drive is in it. We assume that
1119 * it's a fatal error of some kind because we
1120 * can't show that it wasn't. We will make it
1121 * look like selection timeout since that is
1122 * the most common reason for this to occur,
1123 * and it's severe enough.
1126 cmd->result = DID_NO_CONNECT << 16;
1128 break;
1130 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1131 break;
1132 case CMD_DATA_OVERRUN:
1133 dev_warn(&h->pdev->dev, "cp %p has"
1134 " completed with data overrun "
1135 "reported\n", cp);
1136 break;
1137 case CMD_INVALID: {
1138 /* print_bytes(cp, sizeof(*cp), 1, 0);
1139 print_cmd(cp); */
1140 /* We get CMD_INVALID if you address a non-existent device
1141 * instead of a selection timeout (no response). You will
1142 * see this if you yank out a drive, then try to access it.
1143 * This is kind of a shame because it means that any other
1144 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1145 * missing target. */
1146 cmd->result = DID_NO_CONNECT << 16;
1148 break;
1149 case CMD_PROTOCOL_ERR:
1150 dev_warn(&h->pdev->dev, "cp %p has "
1151 "protocol error \n", cp);
1152 break;
1153 case CMD_HARDWARE_ERR:
1154 cmd->result = DID_ERROR << 16;
1155 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1156 break;
1157 case CMD_CONNECTION_LOST:
1158 cmd->result = DID_ERROR << 16;
1159 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1160 break;
1161 case CMD_ABORTED:
1162 cmd->result = DID_ABORT << 16;
1163 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1164 cp, ei->ScsiStatus);
1165 break;
1166 case CMD_ABORT_FAILED:
1167 cmd->result = DID_ERROR << 16;
1168 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1169 break;
1170 case CMD_UNSOLICITED_ABORT:
1171 cmd->result = DID_RESET << 16;
1172 dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited "
1173 "abort\n", cp);
1174 break;
1175 case CMD_TIMEOUT:
1176 cmd->result = DID_TIME_OUT << 16;
1177 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1178 break;
1179 default:
1180 cmd->result = DID_ERROR << 16;
1181 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1182 cp, ei->CommandStatus);
1184 cmd->scsi_done(cmd);
1185 cmd_free(h, cp);
1188 static int hpsa_scsi_detect(struct ctlr_info *h)
1190 struct Scsi_Host *sh;
1191 int error;
1193 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
1194 if (sh == NULL)
1195 goto fail;
1197 sh->io_port = 0;
1198 sh->n_io_port = 0;
1199 sh->this_id = -1;
1200 sh->max_channel = 3;
1201 sh->max_cmd_len = MAX_COMMAND_SIZE;
1202 sh->max_lun = HPSA_MAX_LUN;
1203 sh->max_id = HPSA_MAX_LUN;
1204 sh->can_queue = h->nr_cmds;
1205 sh->cmd_per_lun = h->nr_cmds;
1206 h->scsi_host = sh;
1207 sh->hostdata[0] = (unsigned long) h;
1208 sh->irq = h->intr[PERF_MODE_INT];
1209 sh->unique_id = sh->irq;
1210 error = scsi_add_host(sh, &h->pdev->dev);
1211 if (error)
1212 goto fail_host_put;
1213 scsi_scan_host(sh);
1214 return 0;
1216 fail_host_put:
1217 dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host"
1218 " failed for controller %d\n", h->ctlr);
1219 scsi_host_put(sh);
1220 return error;
1221 fail:
1222 dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc"
1223 " failed for controller %d\n", h->ctlr);
1224 return -ENOMEM;
1227 static void hpsa_pci_unmap(struct pci_dev *pdev,
1228 struct CommandList *c, int sg_used, int data_direction)
1230 int i;
1231 union u64bit addr64;
1233 for (i = 0; i < sg_used; i++) {
1234 addr64.val32.lower = c->SG[i].Addr.lower;
1235 addr64.val32.upper = c->SG[i].Addr.upper;
1236 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1237 data_direction);
1241 static void hpsa_map_one(struct pci_dev *pdev,
1242 struct CommandList *cp,
1243 unsigned char *buf,
1244 size_t buflen,
1245 int data_direction)
1247 u64 addr64;
1249 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1250 cp->Header.SGList = 0;
1251 cp->Header.SGTotal = 0;
1252 return;
1255 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
1256 cp->SG[0].Addr.lower =
1257 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
1258 cp->SG[0].Addr.upper =
1259 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
1260 cp->SG[0].Len = buflen;
1261 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1262 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
1265 static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1266 struct CommandList *c)
1268 DECLARE_COMPLETION_ONSTACK(wait);
1270 c->waiting = &wait;
1271 enqueue_cmd_and_start_io(h, c);
1272 wait_for_completion(&wait);
1275 static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1276 struct CommandList *c, int data_direction)
1278 int retry_count = 0;
1280 do {
1281 memset(c->err_info, 0, sizeof(c->err_info));
1282 hpsa_scsi_do_simple_cmd_core(h, c);
1283 retry_count++;
1284 } while (check_for_unit_attention(h, c) && retry_count <= 3);
1285 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1288 static void hpsa_scsi_interpret_error(struct CommandList *cp)
1290 struct ErrorInfo *ei;
1291 struct device *d = &cp->h->pdev->dev;
1293 ei = cp->err_info;
1294 switch (ei->CommandStatus) {
1295 case CMD_TARGET_STATUS:
1296 dev_warn(d, "cmd %p has completed with errors\n", cp);
1297 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1298 ei->ScsiStatus);
1299 if (ei->ScsiStatus == 0)
1300 dev_warn(d, "SCSI status is abnormally zero. "
1301 "(probably indicates selection timeout "
1302 "reported incorrectly due to a known "
1303 "firmware bug, circa July, 2001.)\n");
1304 break;
1305 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1306 dev_info(d, "UNDERRUN\n");
1307 break;
1308 case CMD_DATA_OVERRUN:
1309 dev_warn(d, "cp %p has completed with data overrun\n", cp);
1310 break;
1311 case CMD_INVALID: {
1312 /* controller unfortunately reports SCSI passthru's
1313 * to non-existent targets as invalid commands.
1315 dev_warn(d, "cp %p is reported invalid (probably means "
1316 "target device no longer present)\n", cp);
1317 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1318 print_cmd(cp); */
1320 break;
1321 case CMD_PROTOCOL_ERR:
1322 dev_warn(d, "cp %p has protocol error \n", cp);
1323 break;
1324 case CMD_HARDWARE_ERR:
1325 /* cmd->result = DID_ERROR << 16; */
1326 dev_warn(d, "cp %p had hardware error\n", cp);
1327 break;
1328 case CMD_CONNECTION_LOST:
1329 dev_warn(d, "cp %p had connection lost\n", cp);
1330 break;
1331 case CMD_ABORTED:
1332 dev_warn(d, "cp %p was aborted\n", cp);
1333 break;
1334 case CMD_ABORT_FAILED:
1335 dev_warn(d, "cp %p reports abort failed\n", cp);
1336 break;
1337 case CMD_UNSOLICITED_ABORT:
1338 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1339 break;
1340 case CMD_TIMEOUT:
1341 dev_warn(d, "cp %p timed out\n", cp);
1342 break;
1343 default:
1344 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1345 ei->CommandStatus);
1349 static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1350 unsigned char page, unsigned char *buf,
1351 unsigned char bufsize)
1353 int rc = IO_OK;
1354 struct CommandList *c;
1355 struct ErrorInfo *ei;
1357 c = cmd_special_alloc(h);
1359 if (c == NULL) { /* trouble... */
1360 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1361 return -ENOMEM;
1364 fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD);
1365 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1366 ei = c->err_info;
1367 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1368 hpsa_scsi_interpret_error(c);
1369 rc = -1;
1371 cmd_special_free(h, c);
1372 return rc;
1375 static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
1377 int rc = IO_OK;
1378 struct CommandList *c;
1379 struct ErrorInfo *ei;
1381 c = cmd_special_alloc(h);
1383 if (c == NULL) { /* trouble... */
1384 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1385 return -1;
1388 fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG);
1389 hpsa_scsi_do_simple_cmd_core(h, c);
1390 /* no unmap needed here because no data xfer. */
1392 ei = c->err_info;
1393 if (ei->CommandStatus != 0) {
1394 hpsa_scsi_interpret_error(c);
1395 rc = -1;
1397 cmd_special_free(h, c);
1398 return rc;
1401 static void hpsa_get_raid_level(struct ctlr_info *h,
1402 unsigned char *scsi3addr, unsigned char *raid_level)
1404 int rc;
1405 unsigned char *buf;
1407 *raid_level = RAID_UNKNOWN;
1408 buf = kzalloc(64, GFP_KERNEL);
1409 if (!buf)
1410 return;
1411 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1412 if (rc == 0)
1413 *raid_level = buf[8];
1414 if (*raid_level > RAID_UNKNOWN)
1415 *raid_level = RAID_UNKNOWN;
1416 kfree(buf);
1417 return;
1420 /* Get the device id from inquiry page 0x83 */
1421 static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
1422 unsigned char *device_id, int buflen)
1424 int rc;
1425 unsigned char *buf;
1427 if (buflen > 16)
1428 buflen = 16;
1429 buf = kzalloc(64, GFP_KERNEL);
1430 if (!buf)
1431 return -1;
1432 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1433 if (rc == 0)
1434 memcpy(device_id, &buf[8], buflen);
1435 kfree(buf);
1436 return rc != 0;
1439 static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
1440 struct ReportLUNdata *buf, int bufsize,
1441 int extended_response)
1443 int rc = IO_OK;
1444 struct CommandList *c;
1445 unsigned char scsi3addr[8];
1446 struct ErrorInfo *ei;
1448 c = cmd_special_alloc(h);
1449 if (c == NULL) { /* trouble... */
1450 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1451 return -1;
1453 /* address the controller */
1454 memset(scsi3addr, 0, sizeof(scsi3addr));
1455 fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
1456 buf, bufsize, 0, scsi3addr, TYPE_CMD);
1457 if (extended_response)
1458 c->Request.CDB[1] = extended_response;
1459 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1460 ei = c->err_info;
1461 if (ei->CommandStatus != 0 &&
1462 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1463 hpsa_scsi_interpret_error(c);
1464 rc = -1;
1466 cmd_special_free(h, c);
1467 return rc;
1470 static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
1471 struct ReportLUNdata *buf,
1472 int bufsize, int extended_response)
1474 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
1477 static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
1478 struct ReportLUNdata *buf, int bufsize)
1480 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
1483 static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
1484 int bus, int target, int lun)
1486 device->bus = bus;
1487 device->target = target;
1488 device->lun = lun;
1491 static int hpsa_update_device_info(struct ctlr_info *h,
1492 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device)
1494 #define OBDR_TAPE_INQ_SIZE 49
1495 unsigned char *inq_buff;
1497 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1498 if (!inq_buff)
1499 goto bail_out;
1501 /* Do an inquiry to the device to see what it is. */
1502 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1503 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1504 /* Inquiry failed (msg printed already) */
1505 dev_err(&h->pdev->dev,
1506 "hpsa_update_device_info: inquiry failed\n");
1507 goto bail_out;
1510 /* As a side effect, record the firmware version number
1511 * if we happen to be talking to the RAID controller.
1513 if (is_hba_lunid(scsi3addr))
1514 memcpy(h->firm_ver, &inq_buff[32], 4);
1516 this_device->devtype = (inq_buff[0] & 0x1f);
1517 memcpy(this_device->scsi3addr, scsi3addr, 8);
1518 memcpy(this_device->vendor, &inq_buff[8],
1519 sizeof(this_device->vendor));
1520 memcpy(this_device->model, &inq_buff[16],
1521 sizeof(this_device->model));
1522 memcpy(this_device->revision, &inq_buff[32],
1523 sizeof(this_device->revision));
1524 memset(this_device->device_id, 0,
1525 sizeof(this_device->device_id));
1526 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
1527 sizeof(this_device->device_id));
1529 if (this_device->devtype == TYPE_DISK &&
1530 is_logical_dev_addr_mode(scsi3addr))
1531 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
1532 else
1533 this_device->raid_level = RAID_UNKNOWN;
1535 kfree(inq_buff);
1536 return 0;
1538 bail_out:
1539 kfree(inq_buff);
1540 return 1;
1543 static unsigned char *msa2xxx_model[] = {
1544 "MSA2012",
1545 "MSA2024",
1546 "MSA2312",
1547 "MSA2324",
1548 NULL,
1551 static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1553 int i;
1555 for (i = 0; msa2xxx_model[i]; i++)
1556 if (strncmp(device->model, msa2xxx_model[i],
1557 strlen(msa2xxx_model[i])) == 0)
1558 return 1;
1559 return 0;
1562 /* Helper function to assign bus, target, lun mapping of devices.
1563 * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
1564 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
1565 * Logical drive target and lun are assigned at this time, but
1566 * physical device lun and target assignment are deferred (assigned
1567 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
1569 static void figure_bus_target_lun(struct ctlr_info *h,
1570 u8 *lunaddrbytes, int *bus, int *target, int *lun,
1571 struct hpsa_scsi_dev_t *device)
1573 u32 lunid;
1575 if (is_logical_dev_addr_mode(lunaddrbytes)) {
1576 /* logical device */
1577 if (unlikely(is_scsi_rev_5(h))) {
1578 /* p1210m, logical drives lun assignments
1579 * match SCSI REPORT LUNS data.
1581 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
1582 *bus = 0;
1583 *target = 0;
1584 *lun = (lunid & 0x3fff) + 1;
1585 } else {
1586 /* not p1210m... */
1587 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
1588 if (is_msa2xxx(h, device)) {
1589 /* msa2xxx way, put logicals on bus 1
1590 * and match target/lun numbers box
1591 * reports.
1593 *bus = 1;
1594 *target = (lunid >> 16) & 0x3fff;
1595 *lun = lunid & 0x00ff;
1596 } else {
1597 /* Traditional smart array way. */
1598 *bus = 0;
1599 *lun = 0;
1600 *target = lunid & 0x3fff;
1603 } else {
1604 /* physical device */
1605 if (is_hba_lunid(lunaddrbytes))
1606 if (unlikely(is_scsi_rev_5(h))) {
1607 *bus = 0; /* put p1210m ctlr at 0,0,0 */
1608 *target = 0;
1609 *lun = 0;
1610 return;
1611 } else
1612 *bus = 3; /* traditional smartarray */
1613 else
1614 *bus = 2; /* physical disk */
1615 *target = -1;
1616 *lun = -1; /* we will fill these in later. */
1621 * If there is no lun 0 on a target, linux won't find any devices.
1622 * For the MSA2xxx boxes, we have to manually detect the enclosure
1623 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
1624 * it for some reason. *tmpdevice is the target we're adding,
1625 * this_device is a pointer into the current element of currentsd[]
1626 * that we're building up in update_scsi_devices(), below.
1627 * lunzerobits is a bitmap that tracks which targets already have a
1628 * lun 0 assigned.
1629 * Returns 1 if an enclosure was added, 0 if not.
1631 static int add_msa2xxx_enclosure_device(struct ctlr_info *h,
1632 struct hpsa_scsi_dev_t *tmpdevice,
1633 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
1634 int bus, int target, int lun, unsigned long lunzerobits[],
1635 int *nmsa2xxx_enclosures)
1637 unsigned char scsi3addr[8];
1639 if (test_bit(target, lunzerobits))
1640 return 0; /* There is already a lun 0 on this target. */
1642 if (!is_logical_dev_addr_mode(lunaddrbytes))
1643 return 0; /* It's the logical targets that may lack lun 0. */
1645 if (!is_msa2xxx(h, tmpdevice))
1646 return 0; /* It's only the MSA2xxx that have this problem. */
1648 if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */
1649 return 0;
1651 if (is_hba_lunid(scsi3addr))
1652 return 0; /* Don't add the RAID controller here. */
1654 if (is_scsi_rev_5(h))
1655 return 0; /* p1210m doesn't need to do this. */
1657 #define MAX_MSA2XXX_ENCLOSURES 32
1658 if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) {
1659 dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX "
1660 "enclosures exceeded. Check your hardware "
1661 "configuration.");
1662 return 0;
1665 memset(scsi3addr, 0, 8);
1666 scsi3addr[3] = target;
1667 if (hpsa_update_device_info(h, scsi3addr, this_device))
1668 return 0;
1669 (*nmsa2xxx_enclosures)++;
1670 hpsa_set_bus_target_lun(this_device, bus, target, 0);
1671 set_bit(target, lunzerobits);
1672 return 1;
1676 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
1677 * logdev. The number of luns in physdev and logdev are returned in
1678 * *nphysicals and *nlogicals, respectively.
1679 * Returns 0 on success, -1 otherwise.
1681 static int hpsa_gather_lun_info(struct ctlr_info *h,
1682 int reportlunsize,
1683 struct ReportLUNdata *physdev, u32 *nphysicals,
1684 struct ReportLUNdata *logdev, u32 *nlogicals)
1686 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
1687 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
1688 return -1;
1690 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8;
1691 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
1692 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
1693 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1694 *nphysicals - HPSA_MAX_PHYS_LUN);
1695 *nphysicals = HPSA_MAX_PHYS_LUN;
1697 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
1698 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
1699 return -1;
1701 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
1702 /* Reject Logicals in excess of our max capability. */
1703 if (*nlogicals > HPSA_MAX_LUN) {
1704 dev_warn(&h->pdev->dev,
1705 "maximum logical LUNs (%d) exceeded. "
1706 "%d LUNs ignored.\n", HPSA_MAX_LUN,
1707 *nlogicals - HPSA_MAX_LUN);
1708 *nlogicals = HPSA_MAX_LUN;
1710 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
1711 dev_warn(&h->pdev->dev,
1712 "maximum logical + physical LUNs (%d) exceeded. "
1713 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1714 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
1715 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
1717 return 0;
1720 u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
1721 int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list,
1722 struct ReportLUNdata *logdev_list)
1724 /* Helper function, figure out where the LUN ID info is coming from
1725 * given index i, lists of physical and logical devices, where in
1726 * the list the raid controller is supposed to appear (first or last)
1729 int logicals_start = nphysicals + (raid_ctlr_position == 0);
1730 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
1732 if (i == raid_ctlr_position)
1733 return RAID_CTLR_LUNID;
1735 if (i < logicals_start)
1736 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
1738 if (i < last_device)
1739 return &logdev_list->LUN[i - nphysicals -
1740 (raid_ctlr_position == 0)][0];
1741 BUG();
1742 return NULL;
1745 static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
1747 /* the idea here is we could get notified
1748 * that some devices have changed, so we do a report
1749 * physical luns and report logical luns cmd, and adjust
1750 * our list of devices accordingly.
1752 * The scsi3addr's of devices won't change so long as the
1753 * adapter is not reset. That means we can rescan and
1754 * tell which devices we already know about, vs. new
1755 * devices, vs. disappearing devices.
1757 struct ReportLUNdata *physdev_list = NULL;
1758 struct ReportLUNdata *logdev_list = NULL;
1759 unsigned char *inq_buff = NULL;
1760 u32 nphysicals = 0;
1761 u32 nlogicals = 0;
1762 u32 ndev_allocated = 0;
1763 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
1764 int ncurrent = 0;
1765 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
1766 int i, nmsa2xxx_enclosures, ndevs_to_allocate;
1767 int bus, target, lun;
1768 int raid_ctlr_position;
1769 DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR);
1771 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA,
1772 GFP_KERNEL);
1773 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1774 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1775 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1776 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
1778 if (!currentsd || !physdev_list || !logdev_list ||
1779 !inq_buff || !tmpdevice) {
1780 dev_err(&h->pdev->dev, "out of memory\n");
1781 goto out;
1783 memset(lunzerobits, 0, sizeof(lunzerobits));
1785 if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
1786 logdev_list, &nlogicals))
1787 goto out;
1789 /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
1790 * but each of them 4 times through different paths. The plus 1
1791 * is for the RAID controller.
1793 ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1;
1795 /* Allocate the per device structures */
1796 for (i = 0; i < ndevs_to_allocate; i++) {
1797 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
1798 if (!currentsd[i]) {
1799 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
1800 __FILE__, __LINE__);
1801 goto out;
1803 ndev_allocated++;
1806 if (unlikely(is_scsi_rev_5(h)))
1807 raid_ctlr_position = 0;
1808 else
1809 raid_ctlr_position = nphysicals + nlogicals;
1811 /* adjust our table of devices */
1812 nmsa2xxx_enclosures = 0;
1813 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
1814 u8 *lunaddrbytes;
1816 /* Figure out where the LUN ID info is coming from */
1817 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
1818 i, nphysicals, nlogicals, physdev_list, logdev_list);
1819 /* skip masked physical devices. */
1820 if (lunaddrbytes[3] & 0xC0 &&
1821 i < nphysicals + (raid_ctlr_position == 0))
1822 continue;
1824 /* Get device type, vendor, model, device id */
1825 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice))
1826 continue; /* skip it if we can't talk to it. */
1827 figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun,
1828 tmpdevice);
1829 this_device = currentsd[ncurrent];
1832 * For the msa2xxx boxes, we have to insert a LUN 0 which
1833 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
1834 * is nonetheless an enclosure device there. We have to
1835 * present that otherwise linux won't find anything if
1836 * there is no lun 0.
1838 if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device,
1839 lunaddrbytes, bus, target, lun, lunzerobits,
1840 &nmsa2xxx_enclosures)) {
1841 ncurrent++;
1842 this_device = currentsd[ncurrent];
1845 *this_device = *tmpdevice;
1846 hpsa_set_bus_target_lun(this_device, bus, target, lun);
1848 switch (this_device->devtype) {
1849 case TYPE_ROM: {
1850 /* We don't *really* support actual CD-ROM devices,
1851 * just "One Button Disaster Recovery" tape drive
1852 * which temporarily pretends to be a CD-ROM drive.
1853 * So we check that the device is really an OBDR tape
1854 * device by checking for "$DR-10" in bytes 43-48 of
1855 * the inquiry data.
1857 char obdr_sig[7];
1858 #define OBDR_TAPE_SIG "$DR-10"
1859 strncpy(obdr_sig, &inq_buff[43], 6);
1860 obdr_sig[6] = '\0';
1861 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1862 /* Not OBDR device, ignore it. */
1863 break;
1865 ncurrent++;
1866 break;
1867 case TYPE_DISK:
1868 if (i < nphysicals)
1869 break;
1870 ncurrent++;
1871 break;
1872 case TYPE_TAPE:
1873 case TYPE_MEDIUM_CHANGER:
1874 ncurrent++;
1875 break;
1876 case TYPE_RAID:
1877 /* Only present the Smartarray HBA as a RAID controller.
1878 * If it's a RAID controller other than the HBA itself
1879 * (an external RAID controller, MSA500 or similar)
1880 * don't present it.
1882 if (!is_hba_lunid(lunaddrbytes))
1883 break;
1884 ncurrent++;
1885 break;
1886 default:
1887 break;
1889 if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA)
1890 break;
1892 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
1893 out:
1894 kfree(tmpdevice);
1895 for (i = 0; i < ndev_allocated; i++)
1896 kfree(currentsd[i]);
1897 kfree(currentsd);
1898 kfree(inq_buff);
1899 kfree(physdev_list);
1900 kfree(logdev_list);
1903 /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1904 * dma mapping and fills in the scatter gather entries of the
1905 * hpsa command, cp.
1907 static int hpsa_scatter_gather(struct pci_dev *pdev,
1908 struct CommandList *cp,
1909 struct scsi_cmnd *cmd)
1911 unsigned int len;
1912 struct scatterlist *sg;
1913 u64 addr64;
1914 int use_sg, i;
1916 BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
1918 use_sg = scsi_dma_map(cmd);
1919 if (use_sg < 0)
1920 return use_sg;
1922 if (!use_sg)
1923 goto sglist_finished;
1925 scsi_for_each_sg(cmd, sg, use_sg, i) {
1926 addr64 = (u64) sg_dma_address(sg);
1927 len = sg_dma_len(sg);
1928 cp->SG[i].Addr.lower =
1929 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
1930 cp->SG[i].Addr.upper =
1931 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
1932 cp->SG[i].Len = len;
1933 cp->SG[i].Ext = 0; /* we are not chaining */
1936 sglist_finished:
1938 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
1939 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
1940 return 0;
1944 static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
1945 void (*done)(struct scsi_cmnd *))
1947 struct ctlr_info *h;
1948 struct hpsa_scsi_dev_t *dev;
1949 unsigned char scsi3addr[8];
1950 struct CommandList *c;
1951 unsigned long flags;
1953 /* Get the ptr to our adapter structure out of cmd->host. */
1954 h = sdev_to_hba(cmd->device);
1955 dev = cmd->device->hostdata;
1956 if (!dev) {
1957 cmd->result = DID_NO_CONNECT << 16;
1958 done(cmd);
1959 return 0;
1961 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
1963 /* Need a lock as this is being allocated from the pool */
1964 spin_lock_irqsave(&h->lock, flags);
1965 c = cmd_alloc(h);
1966 spin_unlock_irqrestore(&h->lock, flags);
1967 if (c == NULL) { /* trouble... */
1968 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
1969 return SCSI_MLQUEUE_HOST_BUSY;
1972 /* Fill in the command list header */
1974 cmd->scsi_done = done; /* save this for use by completion code */
1976 /* save c in case we have to abort it */
1977 cmd->host_scribble = (unsigned char *) c;
1979 c->cmd_type = CMD_SCSI;
1980 c->scsi_cmd = cmd;
1981 c->Header.ReplyQueue = 0; /* unused in simple mode */
1982 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1983 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
1984 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
1986 /* Fill in the request block... */
1988 c->Request.Timeout = 0;
1989 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
1990 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
1991 c->Request.CDBLen = cmd->cmd_len;
1992 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
1993 c->Request.Type.Type = TYPE_CMD;
1994 c->Request.Type.Attribute = ATTR_SIMPLE;
1995 switch (cmd->sc_data_direction) {
1996 case DMA_TO_DEVICE:
1997 c->Request.Type.Direction = XFER_WRITE;
1998 break;
1999 case DMA_FROM_DEVICE:
2000 c->Request.Type.Direction = XFER_READ;
2001 break;
2002 case DMA_NONE:
2003 c->Request.Type.Direction = XFER_NONE;
2004 break;
2005 case DMA_BIDIRECTIONAL:
2006 /* This can happen if a buggy application does a scsi passthru
2007 * and sets both inlen and outlen to non-zero. ( see
2008 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
2011 c->Request.Type.Direction = XFER_RSVD;
2012 /* This is technically wrong, and hpsa controllers should
2013 * reject it with CMD_INVALID, which is the most correct
2014 * response, but non-fibre backends appear to let it
2015 * slide by, and give the same results as if this field
2016 * were set correctly. Either way is acceptable for
2017 * our purposes here.
2020 break;
2022 default:
2023 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2024 cmd->sc_data_direction);
2025 BUG();
2026 break;
2029 if (hpsa_scatter_gather(h->pdev, c, cmd) < 0) { /* Fill SG list */
2030 cmd_free(h, c);
2031 return SCSI_MLQUEUE_HOST_BUSY;
2033 enqueue_cmd_and_start_io(h, c);
2034 /* the cmd'll come back via intr handler in complete_scsi_command() */
2035 return 0;
2038 static void hpsa_scan_start(struct Scsi_Host *sh)
2040 struct ctlr_info *h = shost_to_hba(sh);
2041 unsigned long flags;
2043 /* wait until any scan already in progress is finished. */
2044 while (1) {
2045 spin_lock_irqsave(&h->scan_lock, flags);
2046 if (h->scan_finished)
2047 break;
2048 spin_unlock_irqrestore(&h->scan_lock, flags);
2049 wait_event(h->scan_wait_queue, h->scan_finished);
2050 /* Note: We don't need to worry about a race between this
2051 * thread and driver unload because the midlayer will
2052 * have incremented the reference count, so unload won't
2053 * happen if we're in here.
2056 h->scan_finished = 0; /* mark scan as in progress */
2057 spin_unlock_irqrestore(&h->scan_lock, flags);
2059 hpsa_update_scsi_devices(h, h->scsi_host->host_no);
2061 spin_lock_irqsave(&h->scan_lock, flags);
2062 h->scan_finished = 1; /* mark scan as finished. */
2063 wake_up_all(&h->scan_wait_queue);
2064 spin_unlock_irqrestore(&h->scan_lock, flags);
2067 static int hpsa_scan_finished(struct Scsi_Host *sh,
2068 unsigned long elapsed_time)
2070 struct ctlr_info *h = shost_to_hba(sh);
2071 unsigned long flags;
2072 int finished;
2074 spin_lock_irqsave(&h->scan_lock, flags);
2075 finished = h->scan_finished;
2076 spin_unlock_irqrestore(&h->scan_lock, flags);
2077 return finished;
2080 static void hpsa_unregister_scsi(struct ctlr_info *h)
2082 /* we are being forcibly unloaded, and may not refuse. */
2083 scsi_remove_host(h->scsi_host);
2084 scsi_host_put(h->scsi_host);
2085 h->scsi_host = NULL;
2088 static int hpsa_register_scsi(struct ctlr_info *h)
2090 int rc;
2092 rc = hpsa_scsi_detect(h);
2093 if (rc != 0)
2094 dev_err(&h->pdev->dev, "hpsa_register_scsi: failed"
2095 " hpsa_scsi_detect(), rc is %d\n", rc);
2096 return rc;
2099 static int wait_for_device_to_become_ready(struct ctlr_info *h,
2100 unsigned char lunaddr[])
2102 int rc = 0;
2103 int count = 0;
2104 int waittime = 1; /* seconds */
2105 struct CommandList *c;
2107 c = cmd_special_alloc(h);
2108 if (!c) {
2109 dev_warn(&h->pdev->dev, "out of memory in "
2110 "wait_for_device_to_become_ready.\n");
2111 return IO_ERROR;
2114 /* Send test unit ready until device ready, or give up. */
2115 while (count < HPSA_TUR_RETRY_LIMIT) {
2117 /* Wait for a bit. do this first, because if we send
2118 * the TUR right away, the reset will just abort it.
2120 msleep(1000 * waittime);
2121 count++;
2123 /* Increase wait time with each try, up to a point. */
2124 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
2125 waittime = waittime * 2;
2127 /* Send the Test Unit Ready */
2128 fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD);
2129 hpsa_scsi_do_simple_cmd_core(h, c);
2130 /* no unmap needed here because no data xfer. */
2132 if (c->err_info->CommandStatus == CMD_SUCCESS)
2133 break;
2135 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2136 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
2137 (c->err_info->SenseInfo[2] == NO_SENSE ||
2138 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
2139 break;
2141 dev_warn(&h->pdev->dev, "waiting %d secs "
2142 "for device to become ready.\n", waittime);
2143 rc = 1; /* device not ready. */
2146 if (rc)
2147 dev_warn(&h->pdev->dev, "giving up on device.\n");
2148 else
2149 dev_warn(&h->pdev->dev, "device is ready.\n");
2151 cmd_special_free(h, c);
2152 return rc;
2155 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
2156 * complaining. Doing a host- or bus-reset can't do anything good here.
2158 static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
2160 int rc;
2161 struct ctlr_info *h;
2162 struct hpsa_scsi_dev_t *dev;
2164 /* find the controller to which the command to be aborted was sent */
2165 h = sdev_to_hba(scsicmd->device);
2166 if (h == NULL) /* paranoia */
2167 return FAILED;
2168 dev = scsicmd->device->hostdata;
2169 if (!dev) {
2170 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
2171 "device lookup failed.\n");
2172 return FAILED;
2174 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
2175 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
2176 /* send a reset to the SCSI LUN which the command was sent to */
2177 rc = hpsa_send_reset(h, dev->scsi3addr);
2178 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
2179 return SUCCESS;
2181 dev_warn(&h->pdev->dev, "resetting device failed.\n");
2182 return FAILED;
2186 * For operations that cannot sleep, a command block is allocated at init,
2187 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
2188 * which ones are free or in use. Lock must be held when calling this.
2189 * cmd_free() is the complement.
2191 static struct CommandList *cmd_alloc(struct ctlr_info *h)
2193 struct CommandList *c;
2194 int i;
2195 union u64bit temp64;
2196 dma_addr_t cmd_dma_handle, err_dma_handle;
2198 do {
2199 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
2200 if (i == h->nr_cmds)
2201 return NULL;
2202 } while (test_and_set_bit
2203 (i & (BITS_PER_LONG - 1),
2204 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
2205 c = h->cmd_pool + i;
2206 memset(c, 0, sizeof(*c));
2207 cmd_dma_handle = h->cmd_pool_dhandle
2208 + i * sizeof(*c);
2209 c->err_info = h->errinfo_pool + i;
2210 memset(c->err_info, 0, sizeof(*c->err_info));
2211 err_dma_handle = h->errinfo_pool_dhandle
2212 + i * sizeof(*c->err_info);
2213 h->nr_allocs++;
2215 c->cmdindex = i;
2217 INIT_HLIST_NODE(&c->list);
2218 c->busaddr = (u32) cmd_dma_handle;
2219 temp64.val = (u64) err_dma_handle;
2220 c->ErrDesc.Addr.lower = temp64.val32.lower;
2221 c->ErrDesc.Addr.upper = temp64.val32.upper;
2222 c->ErrDesc.Len = sizeof(*c->err_info);
2224 c->h = h;
2225 return c;
2228 /* For operations that can wait for kmalloc to possibly sleep,
2229 * this routine can be called. Lock need not be held to call
2230 * cmd_special_alloc. cmd_special_free() is the complement.
2232 static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
2234 struct CommandList *c;
2235 union u64bit temp64;
2236 dma_addr_t cmd_dma_handle, err_dma_handle;
2238 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
2239 if (c == NULL)
2240 return NULL;
2241 memset(c, 0, sizeof(*c));
2243 c->cmdindex = -1;
2245 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
2246 &err_dma_handle);
2248 if (c->err_info == NULL) {
2249 pci_free_consistent(h->pdev,
2250 sizeof(*c), c, cmd_dma_handle);
2251 return NULL;
2253 memset(c->err_info, 0, sizeof(*c->err_info));
2255 INIT_HLIST_NODE(&c->list);
2256 c->busaddr = (u32) cmd_dma_handle;
2257 temp64.val = (u64) err_dma_handle;
2258 c->ErrDesc.Addr.lower = temp64.val32.lower;
2259 c->ErrDesc.Addr.upper = temp64.val32.upper;
2260 c->ErrDesc.Len = sizeof(*c->err_info);
2262 c->h = h;
2263 return c;
2266 static void cmd_free(struct ctlr_info *h, struct CommandList *c)
2268 int i;
2270 i = c - h->cmd_pool;
2271 clear_bit(i & (BITS_PER_LONG - 1),
2272 h->cmd_pool_bits + (i / BITS_PER_LONG));
2273 h->nr_frees++;
2276 static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
2278 union u64bit temp64;
2280 temp64.val32.lower = c->ErrDesc.Addr.lower;
2281 temp64.val32.upper = c->ErrDesc.Addr.upper;
2282 pci_free_consistent(h->pdev, sizeof(*c->err_info),
2283 c->err_info, (dma_addr_t) temp64.val);
2284 pci_free_consistent(h->pdev, sizeof(*c),
2285 c, (dma_addr_t) c->busaddr);
2288 #ifdef CONFIG_COMPAT
2290 static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg);
2291 static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
2292 int cmd, void *arg);
2294 static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
2296 switch (cmd) {
2297 case CCISS_GETPCIINFO:
2298 case CCISS_GETINTINFO:
2299 case CCISS_SETINTINFO:
2300 case CCISS_GETNODENAME:
2301 case CCISS_SETNODENAME:
2302 case CCISS_GETHEARTBEAT:
2303 case CCISS_GETBUSTYPES:
2304 case CCISS_GETFIRMVER:
2305 case CCISS_GETDRIVVER:
2306 case CCISS_REVALIDVOLS:
2307 case CCISS_DEREGDISK:
2308 case CCISS_REGNEWDISK:
2309 case CCISS_REGNEWD:
2310 case CCISS_RESCANDISK:
2311 case CCISS_GETLUNINFO:
2312 return hpsa_ioctl(dev, cmd, arg);
2314 case CCISS_PASSTHRU32:
2315 return hpsa_ioctl32_passthru(dev, cmd, arg);
2316 case CCISS_BIG_PASSTHRU32:
2317 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
2319 default:
2320 return -ENOIOCTLCMD;
2324 static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
2326 IOCTL32_Command_struct __user *arg32 =
2327 (IOCTL32_Command_struct __user *) arg;
2328 IOCTL_Command_struct arg64;
2329 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
2330 int err;
2331 u32 cp;
2333 err = 0;
2334 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2335 sizeof(arg64.LUN_info));
2336 err |= copy_from_user(&arg64.Request, &arg32->Request,
2337 sizeof(arg64.Request));
2338 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2339 sizeof(arg64.error_info));
2340 err |= get_user(arg64.buf_size, &arg32->buf_size);
2341 err |= get_user(cp, &arg32->buf);
2342 arg64.buf = compat_ptr(cp);
2343 err |= copy_to_user(p, &arg64, sizeof(arg64));
2345 if (err)
2346 return -EFAULT;
2348 err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
2349 if (err)
2350 return err;
2351 err |= copy_in_user(&arg32->error_info, &p->error_info,
2352 sizeof(arg32->error_info));
2353 if (err)
2354 return -EFAULT;
2355 return err;
2358 static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
2359 int cmd, void *arg)
2361 BIG_IOCTL32_Command_struct __user *arg32 =
2362 (BIG_IOCTL32_Command_struct __user *) arg;
2363 BIG_IOCTL_Command_struct arg64;
2364 BIG_IOCTL_Command_struct __user *p =
2365 compat_alloc_user_space(sizeof(arg64));
2366 int err;
2367 u32 cp;
2369 err = 0;
2370 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2371 sizeof(arg64.LUN_info));
2372 err |= copy_from_user(&arg64.Request, &arg32->Request,
2373 sizeof(arg64.Request));
2374 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2375 sizeof(arg64.error_info));
2376 err |= get_user(arg64.buf_size, &arg32->buf_size);
2377 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
2378 err |= get_user(cp, &arg32->buf);
2379 arg64.buf = compat_ptr(cp);
2380 err |= copy_to_user(p, &arg64, sizeof(arg64));
2382 if (err)
2383 return -EFAULT;
2385 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
2386 if (err)
2387 return err;
2388 err |= copy_in_user(&arg32->error_info, &p->error_info,
2389 sizeof(arg32->error_info));
2390 if (err)
2391 return -EFAULT;
2392 return err;
2394 #endif
2396 static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
2398 struct hpsa_pci_info pciinfo;
2400 if (!argp)
2401 return -EINVAL;
2402 pciinfo.domain = pci_domain_nr(h->pdev->bus);
2403 pciinfo.bus = h->pdev->bus->number;
2404 pciinfo.dev_fn = h->pdev->devfn;
2405 pciinfo.board_id = h->board_id;
2406 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
2407 return -EFAULT;
2408 return 0;
2411 static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
2413 DriverVer_type DriverVer;
2414 unsigned char vmaj, vmin, vsubmin;
2415 int rc;
2417 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
2418 &vmaj, &vmin, &vsubmin);
2419 if (rc != 3) {
2420 dev_info(&h->pdev->dev, "driver version string '%s' "
2421 "unrecognized.", HPSA_DRIVER_VERSION);
2422 vmaj = 0;
2423 vmin = 0;
2424 vsubmin = 0;
2426 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
2427 if (!argp)
2428 return -EINVAL;
2429 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
2430 return -EFAULT;
2431 return 0;
2434 static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2436 IOCTL_Command_struct iocommand;
2437 struct CommandList *c;
2438 char *buff = NULL;
2439 union u64bit temp64;
2441 if (!argp)
2442 return -EINVAL;
2443 if (!capable(CAP_SYS_RAWIO))
2444 return -EPERM;
2445 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
2446 return -EFAULT;
2447 if ((iocommand.buf_size < 1) &&
2448 (iocommand.Request.Type.Direction != XFER_NONE)) {
2449 return -EINVAL;
2451 if (iocommand.buf_size > 0) {
2452 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
2453 if (buff == NULL)
2454 return -EFAULT;
2456 if (iocommand.Request.Type.Direction == XFER_WRITE) {
2457 /* Copy the data into the buffer we created */
2458 if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) {
2459 kfree(buff);
2460 return -EFAULT;
2462 } else
2463 memset(buff, 0, iocommand.buf_size);
2464 c = cmd_special_alloc(h);
2465 if (c == NULL) {
2466 kfree(buff);
2467 return -ENOMEM;
2469 /* Fill in the command type */
2470 c->cmd_type = CMD_IOCTL_PEND;
2471 /* Fill in Command Header */
2472 c->Header.ReplyQueue = 0; /* unused in simple mode */
2473 if (iocommand.buf_size > 0) { /* buffer to fill */
2474 c->Header.SGList = 1;
2475 c->Header.SGTotal = 1;
2476 } else { /* no buffers to fill */
2477 c->Header.SGList = 0;
2478 c->Header.SGTotal = 0;
2480 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
2481 /* use the kernel address the cmd block for tag */
2482 c->Header.Tag.lower = c->busaddr;
2484 /* Fill in Request block */
2485 memcpy(&c->Request, &iocommand.Request,
2486 sizeof(c->Request));
2488 /* Fill in the scatter gather information */
2489 if (iocommand.buf_size > 0) {
2490 temp64.val = pci_map_single(h->pdev, buff,
2491 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
2492 c->SG[0].Addr.lower = temp64.val32.lower;
2493 c->SG[0].Addr.upper = temp64.val32.upper;
2494 c->SG[0].Len = iocommand.buf_size;
2495 c->SG[0].Ext = 0; /* we are not chaining*/
2497 hpsa_scsi_do_simple_cmd_core(h, c);
2498 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
2499 check_ioctl_unit_attention(h, c);
2501 /* Copy the error information out */
2502 memcpy(&iocommand.error_info, c->err_info,
2503 sizeof(iocommand.error_info));
2504 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
2505 kfree(buff);
2506 cmd_special_free(h, c);
2507 return -EFAULT;
2510 if (iocommand.Request.Type.Direction == XFER_READ) {
2511 /* Copy the data out of the buffer we created */
2512 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
2513 kfree(buff);
2514 cmd_special_free(h, c);
2515 return -EFAULT;
2518 kfree(buff);
2519 cmd_special_free(h, c);
2520 return 0;
2523 static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2525 BIG_IOCTL_Command_struct *ioc;
2526 struct CommandList *c;
2527 unsigned char **buff = NULL;
2528 int *buff_size = NULL;
2529 union u64bit temp64;
2530 BYTE sg_used = 0;
2531 int status = 0;
2532 int i;
2533 u32 left;
2534 u32 sz;
2535 BYTE __user *data_ptr;
2537 if (!argp)
2538 return -EINVAL;
2539 if (!capable(CAP_SYS_RAWIO))
2540 return -EPERM;
2541 ioc = (BIG_IOCTL_Command_struct *)
2542 kmalloc(sizeof(*ioc), GFP_KERNEL);
2543 if (!ioc) {
2544 status = -ENOMEM;
2545 goto cleanup1;
2547 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
2548 status = -EFAULT;
2549 goto cleanup1;
2551 if ((ioc->buf_size < 1) &&
2552 (ioc->Request.Type.Direction != XFER_NONE)) {
2553 status = -EINVAL;
2554 goto cleanup1;
2556 /* Check kmalloc limits using all SGs */
2557 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
2558 status = -EINVAL;
2559 goto cleanup1;
2561 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
2562 status = -EINVAL;
2563 goto cleanup1;
2565 buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
2566 if (!buff) {
2567 status = -ENOMEM;
2568 goto cleanup1;
2570 buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL);
2571 if (!buff_size) {
2572 status = -ENOMEM;
2573 goto cleanup1;
2575 left = ioc->buf_size;
2576 data_ptr = ioc->buf;
2577 while (left) {
2578 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
2579 buff_size[sg_used] = sz;
2580 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
2581 if (buff[sg_used] == NULL) {
2582 status = -ENOMEM;
2583 goto cleanup1;
2585 if (ioc->Request.Type.Direction == XFER_WRITE) {
2586 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
2587 status = -ENOMEM;
2588 goto cleanup1;
2590 } else
2591 memset(buff[sg_used], 0, sz);
2592 left -= sz;
2593 data_ptr += sz;
2594 sg_used++;
2596 c = cmd_special_alloc(h);
2597 if (c == NULL) {
2598 status = -ENOMEM;
2599 goto cleanup1;
2601 c->cmd_type = CMD_IOCTL_PEND;
2602 c->Header.ReplyQueue = 0;
2604 if (ioc->buf_size > 0) {
2605 c->Header.SGList = sg_used;
2606 c->Header.SGTotal = sg_used;
2607 } else {
2608 c->Header.SGList = 0;
2609 c->Header.SGTotal = 0;
2611 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
2612 c->Header.Tag.lower = c->busaddr;
2613 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
2614 if (ioc->buf_size > 0) {
2615 int i;
2616 for (i = 0; i < sg_used; i++) {
2617 temp64.val = pci_map_single(h->pdev, buff[i],
2618 buff_size[i], PCI_DMA_BIDIRECTIONAL);
2619 c->SG[i].Addr.lower = temp64.val32.lower;
2620 c->SG[i].Addr.upper = temp64.val32.upper;
2621 c->SG[i].Len = buff_size[i];
2622 /* we are not chaining */
2623 c->SG[i].Ext = 0;
2626 hpsa_scsi_do_simple_cmd_core(h, c);
2627 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
2628 check_ioctl_unit_attention(h, c);
2629 /* Copy the error information out */
2630 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
2631 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
2632 cmd_special_free(h, c);
2633 status = -EFAULT;
2634 goto cleanup1;
2636 if (ioc->Request.Type.Direction == XFER_READ) {
2637 /* Copy the data out of the buffer we created */
2638 BYTE __user *ptr = ioc->buf;
2639 for (i = 0; i < sg_used; i++) {
2640 if (copy_to_user(ptr, buff[i], buff_size[i])) {
2641 cmd_special_free(h, c);
2642 status = -EFAULT;
2643 goto cleanup1;
2645 ptr += buff_size[i];
2648 cmd_special_free(h, c);
2649 status = 0;
2650 cleanup1:
2651 if (buff) {
2652 for (i = 0; i < sg_used; i++)
2653 kfree(buff[i]);
2654 kfree(buff);
2656 kfree(buff_size);
2657 kfree(ioc);
2658 return status;
2661 static void check_ioctl_unit_attention(struct ctlr_info *h,
2662 struct CommandList *c)
2664 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2665 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
2666 (void) check_for_unit_attention(h, c);
2669 * ioctl
2671 static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
2673 struct ctlr_info *h;
2674 void __user *argp = (void __user *)arg;
2676 h = sdev_to_hba(dev);
2678 switch (cmd) {
2679 case CCISS_DEREGDISK:
2680 case CCISS_REGNEWDISK:
2681 case CCISS_REGNEWD:
2682 hpsa_scan_start(h->scsi_host);
2683 return 0;
2684 case CCISS_GETPCIINFO:
2685 return hpsa_getpciinfo_ioctl(h, argp);
2686 case CCISS_GETDRIVVER:
2687 return hpsa_getdrivver_ioctl(h, argp);
2688 case CCISS_PASSTHRU:
2689 return hpsa_passthru_ioctl(h, argp);
2690 case CCISS_BIG_PASSTHRU:
2691 return hpsa_big_passthru_ioctl(h, argp);
2692 default:
2693 return -ENOTTY;
2697 static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
2698 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
2699 int cmd_type)
2701 int pci_dir = XFER_NONE;
2703 c->cmd_type = CMD_IOCTL_PEND;
2704 c->Header.ReplyQueue = 0;
2705 if (buff != NULL && size > 0) {
2706 c->Header.SGList = 1;
2707 c->Header.SGTotal = 1;
2708 } else {
2709 c->Header.SGList = 0;
2710 c->Header.SGTotal = 0;
2712 c->Header.Tag.lower = c->busaddr;
2713 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
2715 c->Request.Type.Type = cmd_type;
2716 if (cmd_type == TYPE_CMD) {
2717 switch (cmd) {
2718 case HPSA_INQUIRY:
2719 /* are we trying to read a vital product page */
2720 if (page_code != 0) {
2721 c->Request.CDB[1] = 0x01;
2722 c->Request.CDB[2] = page_code;
2724 c->Request.CDBLen = 6;
2725 c->Request.Type.Attribute = ATTR_SIMPLE;
2726 c->Request.Type.Direction = XFER_READ;
2727 c->Request.Timeout = 0;
2728 c->Request.CDB[0] = HPSA_INQUIRY;
2729 c->Request.CDB[4] = size & 0xFF;
2730 break;
2731 case HPSA_REPORT_LOG:
2732 case HPSA_REPORT_PHYS:
2733 /* Talking to controller so It's a physical command
2734 mode = 00 target = 0. Nothing to write.
2736 c->Request.CDBLen = 12;
2737 c->Request.Type.Attribute = ATTR_SIMPLE;
2738 c->Request.Type.Direction = XFER_READ;
2739 c->Request.Timeout = 0;
2740 c->Request.CDB[0] = cmd;
2741 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
2742 c->Request.CDB[7] = (size >> 16) & 0xFF;
2743 c->Request.CDB[8] = (size >> 8) & 0xFF;
2744 c->Request.CDB[9] = size & 0xFF;
2745 break;
2747 case HPSA_READ_CAPACITY:
2748 c->Request.CDBLen = 10;
2749 c->Request.Type.Attribute = ATTR_SIMPLE;
2750 c->Request.Type.Direction = XFER_READ;
2751 c->Request.Timeout = 0;
2752 c->Request.CDB[0] = cmd;
2753 break;
2754 case HPSA_CACHE_FLUSH:
2755 c->Request.CDBLen = 12;
2756 c->Request.Type.Attribute = ATTR_SIMPLE;
2757 c->Request.Type.Direction = XFER_WRITE;
2758 c->Request.Timeout = 0;
2759 c->Request.CDB[0] = BMIC_WRITE;
2760 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
2761 break;
2762 case TEST_UNIT_READY:
2763 c->Request.CDBLen = 6;
2764 c->Request.Type.Attribute = ATTR_SIMPLE;
2765 c->Request.Type.Direction = XFER_NONE;
2766 c->Request.Timeout = 0;
2767 break;
2768 default:
2769 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
2770 BUG();
2771 return;
2773 } else if (cmd_type == TYPE_MSG) {
2774 switch (cmd) {
2776 case HPSA_DEVICE_RESET_MSG:
2777 c->Request.CDBLen = 16;
2778 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
2779 c->Request.Type.Attribute = ATTR_SIMPLE;
2780 c->Request.Type.Direction = XFER_NONE;
2781 c->Request.Timeout = 0; /* Don't time out */
2782 c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */
2783 c->Request.CDB[1] = 0x03; /* Reset target above */
2784 /* If bytes 4-7 are zero, it means reset the */
2785 /* LunID device */
2786 c->Request.CDB[4] = 0x00;
2787 c->Request.CDB[5] = 0x00;
2788 c->Request.CDB[6] = 0x00;
2789 c->Request.CDB[7] = 0x00;
2790 break;
2792 default:
2793 dev_warn(&h->pdev->dev, "unknown message type %d\n",
2794 cmd);
2795 BUG();
2797 } else {
2798 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
2799 BUG();
2802 switch (c->Request.Type.Direction) {
2803 case XFER_READ:
2804 pci_dir = PCI_DMA_FROMDEVICE;
2805 break;
2806 case XFER_WRITE:
2807 pci_dir = PCI_DMA_TODEVICE;
2808 break;
2809 case XFER_NONE:
2810 pci_dir = PCI_DMA_NONE;
2811 break;
2812 default:
2813 pci_dir = PCI_DMA_BIDIRECTIONAL;
2816 hpsa_map_one(h->pdev, c, buff, size, pci_dir);
2818 return;
2822 * Map (physical) PCI mem into (virtual) kernel space
2824 static void __iomem *remap_pci_mem(ulong base, ulong size)
2826 ulong page_base = ((ulong) base) & PAGE_MASK;
2827 ulong page_offs = ((ulong) base) - page_base;
2828 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
2830 return page_remapped ? (page_remapped + page_offs) : NULL;
2833 /* Takes cmds off the submission queue and sends them to the hardware,
2834 * then puts them on the queue of cmds waiting for completion.
2836 static void start_io(struct ctlr_info *h)
2838 struct CommandList *c;
2840 while (!hlist_empty(&h->reqQ)) {
2841 c = hlist_entry(h->reqQ.first, struct CommandList, list);
2842 /* can't do anything if fifo is full */
2843 if ((h->access.fifo_full(h))) {
2844 dev_warn(&h->pdev->dev, "fifo full\n");
2845 break;
2848 /* Get the first entry from the Request Q */
2849 removeQ(c);
2850 h->Qdepth--;
2852 /* Tell the controller execute command */
2853 h->access.submit_command(h, c);
2855 /* Put job onto the completed Q */
2856 addQ(&h->cmpQ, c);
2860 static inline unsigned long get_next_completion(struct ctlr_info *h)
2862 return h->access.command_completed(h);
2865 static inline bool interrupt_pending(struct ctlr_info *h)
2867 return h->access.intr_pending(h);
2870 static inline long interrupt_not_for_us(struct ctlr_info *h)
2872 return !(h->msi_vector || h->msix_vector) &&
2873 ((h->access.intr_pending(h) == 0) ||
2874 (h->interrupts_enabled == 0));
2877 static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
2878 u32 raw_tag)
2880 if (unlikely(tag_index >= h->nr_cmds)) {
2881 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
2882 return 1;
2884 return 0;
2887 static inline void finish_cmd(struct CommandList *c, u32 raw_tag)
2889 removeQ(c);
2890 if (likely(c->cmd_type == CMD_SCSI))
2891 complete_scsi_command(c, 0, raw_tag);
2892 else if (c->cmd_type == CMD_IOCTL_PEND)
2893 complete(c->waiting);
2896 static inline u32 hpsa_tag_contains_index(u32 tag)
2898 #define DIRECT_LOOKUP_BIT 0x10
2899 return tag & DIRECT_LOOKUP_BIT;
2902 static inline u32 hpsa_tag_to_index(u32 tag)
2904 #define DIRECT_LOOKUP_SHIFT 5
2905 return tag >> DIRECT_LOOKUP_SHIFT;
2908 static inline u32 hpsa_tag_discard_error_bits(u32 tag)
2910 #define HPSA_ERROR_BITS 0x03
2911 return tag & ~HPSA_ERROR_BITS;
2914 /* process completion of an indexed ("direct lookup") command */
2915 static inline u32 process_indexed_cmd(struct ctlr_info *h,
2916 u32 raw_tag)
2918 u32 tag_index;
2919 struct CommandList *c;
2921 tag_index = hpsa_tag_to_index(raw_tag);
2922 if (bad_tag(h, tag_index, raw_tag))
2923 return next_command(h);
2924 c = h->cmd_pool + tag_index;
2925 finish_cmd(c, raw_tag);
2926 return next_command(h);
2929 /* process completion of a non-indexed command */
2930 static inline u32 process_nonindexed_cmd(struct ctlr_info *h,
2931 u32 raw_tag)
2933 u32 tag;
2934 struct CommandList *c = NULL;
2935 struct hlist_node *tmp;
2937 tag = hpsa_tag_discard_error_bits(raw_tag);
2938 hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
2939 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
2940 finish_cmd(c, raw_tag);
2941 return next_command(h);
2944 bad_tag(h, h->nr_cmds + 1, raw_tag);
2945 return next_command(h);
2948 static irqreturn_t do_hpsa_intr(int irq, void *dev_id)
2950 struct ctlr_info *h = dev_id;
2951 unsigned long flags;
2952 u32 raw_tag;
2954 if (interrupt_not_for_us(h))
2955 return IRQ_NONE;
2956 spin_lock_irqsave(&h->lock, flags);
2957 raw_tag = get_next_completion(h);
2958 while (raw_tag != FIFO_EMPTY) {
2959 if (hpsa_tag_contains_index(raw_tag))
2960 raw_tag = process_indexed_cmd(h, raw_tag);
2961 else
2962 raw_tag = process_nonindexed_cmd(h, raw_tag);
2964 spin_unlock_irqrestore(&h->lock, flags);
2965 return IRQ_HANDLED;
2968 /* Send a message CDB to the firmwart. */
2969 static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
2970 unsigned char type)
2972 struct Command {
2973 struct CommandListHeader CommandHeader;
2974 struct RequestBlock Request;
2975 struct ErrDescriptor ErrorDescriptor;
2977 struct Command *cmd;
2978 static const size_t cmd_sz = sizeof(*cmd) +
2979 sizeof(cmd->ErrorDescriptor);
2980 dma_addr_t paddr64;
2981 uint32_t paddr32, tag;
2982 void __iomem *vaddr;
2983 int i, err;
2985 vaddr = pci_ioremap_bar(pdev, 0);
2986 if (vaddr == NULL)
2987 return -ENOMEM;
2989 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
2990 * CCISS commands, so they must be allocated from the lower 4GiB of
2991 * memory.
2993 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2994 if (err) {
2995 iounmap(vaddr);
2996 return -ENOMEM;
2999 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
3000 if (cmd == NULL) {
3001 iounmap(vaddr);
3002 return -ENOMEM;
3005 /* This must fit, because of the 32-bit consistent DMA mask. Also,
3006 * although there's no guarantee, we assume that the address is at
3007 * least 4-byte aligned (most likely, it's page-aligned).
3009 paddr32 = paddr64;
3011 cmd->CommandHeader.ReplyQueue = 0;
3012 cmd->CommandHeader.SGList = 0;
3013 cmd->CommandHeader.SGTotal = 0;
3014 cmd->CommandHeader.Tag.lower = paddr32;
3015 cmd->CommandHeader.Tag.upper = 0;
3016 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
3018 cmd->Request.CDBLen = 16;
3019 cmd->Request.Type.Type = TYPE_MSG;
3020 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
3021 cmd->Request.Type.Direction = XFER_NONE;
3022 cmd->Request.Timeout = 0; /* Don't time out */
3023 cmd->Request.CDB[0] = opcode;
3024 cmd->Request.CDB[1] = type;
3025 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
3026 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
3027 cmd->ErrorDescriptor.Addr.upper = 0;
3028 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
3030 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
3032 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
3033 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
3034 if (hpsa_tag_discard_error_bits(tag) == paddr32)
3035 break;
3036 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
3039 iounmap(vaddr);
3041 /* we leak the DMA buffer here ... no choice since the controller could
3042 * still complete the command.
3044 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
3045 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
3046 opcode, type);
3047 return -ETIMEDOUT;
3050 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
3052 if (tag & HPSA_ERROR_BIT) {
3053 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
3054 opcode, type);
3055 return -EIO;
3058 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
3059 opcode, type);
3060 return 0;
3063 #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
3064 #define hpsa_noop(p) hpsa_message(p, 3, 0)
3066 static __devinit int hpsa_reset_msi(struct pci_dev *pdev)
3068 /* the #defines are stolen from drivers/pci/msi.h. */
3069 #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
3070 #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
3072 int pos;
3073 u16 control = 0;
3075 pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
3076 if (pos) {
3077 pci_read_config_word(pdev, msi_control_reg(pos), &control);
3078 if (control & PCI_MSI_FLAGS_ENABLE) {
3079 dev_info(&pdev->dev, "resetting MSI\n");
3080 pci_write_config_word(pdev, msi_control_reg(pos),
3081 control & ~PCI_MSI_FLAGS_ENABLE);
3085 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3086 if (pos) {
3087 pci_read_config_word(pdev, msi_control_reg(pos), &control);
3088 if (control & PCI_MSIX_FLAGS_ENABLE) {
3089 dev_info(&pdev->dev, "resetting MSI-X\n");
3090 pci_write_config_word(pdev, msi_control_reg(pos),
3091 control & ~PCI_MSIX_FLAGS_ENABLE);
3095 return 0;
3098 /* This does a hard reset of the controller using PCI power management
3099 * states.
3101 static __devinit int hpsa_hard_reset_controller(struct pci_dev *pdev)
3103 u16 pmcsr, saved_config_space[32];
3104 int i, pos;
3106 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
3108 /* This is very nearly the same thing as
3110 * pci_save_state(pci_dev);
3111 * pci_set_power_state(pci_dev, PCI_D3hot);
3112 * pci_set_power_state(pci_dev, PCI_D0);
3113 * pci_restore_state(pci_dev);
3115 * but we can't use these nice canned kernel routines on
3116 * kexec, because they also check the MSI/MSI-X state in PCI
3117 * configuration space and do the wrong thing when it is
3118 * set/cleared. Also, the pci_save/restore_state functions
3119 * violate the ordering requirements for restoring the
3120 * configuration space from the CCISS document (see the
3121 * comment below). So we roll our own ....
3124 for (i = 0; i < 32; i++)
3125 pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
3127 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
3128 if (pos == 0) {
3129 dev_err(&pdev->dev,
3130 "hpsa_reset_controller: PCI PM not supported\n");
3131 return -ENODEV;
3134 /* Quoting from the Open CISS Specification: "The Power
3135 * Management Control/Status Register (CSR) controls the power
3136 * state of the device. The normal operating state is D0,
3137 * CSR=00h. The software off state is D3, CSR=03h. To reset
3138 * the controller, place the interface device in D3 then to
3139 * D0, this causes a secondary PCI reset which will reset the
3140 * controller."
3143 /* enter the D3hot power management state */
3144 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
3145 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3146 pmcsr |= PCI_D3hot;
3147 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3149 msleep(500);
3151 /* enter the D0 power management state */
3152 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3153 pmcsr |= PCI_D0;
3154 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3156 msleep(500);
3158 /* Restore the PCI configuration space. The Open CISS
3159 * Specification says, "Restore the PCI Configuration
3160 * Registers, offsets 00h through 60h. It is important to
3161 * restore the command register, 16-bits at offset 04h,
3162 * last. Do not restore the configuration status register,
3163 * 16-bits at offset 06h." Note that the offset is 2*i.
3165 for (i = 0; i < 32; i++) {
3166 if (i == 2 || i == 3)
3167 continue;
3168 pci_write_config_word(pdev, 2*i, saved_config_space[i]);
3170 wmb();
3171 pci_write_config_word(pdev, 4, saved_config_space[2]);
3173 return 0;
3177 * We cannot read the structure directly, for portability we must use
3178 * the io functions.
3179 * This is for debug only.
3181 #ifdef HPSA_DEBUG
3182 static void print_cfg_table(struct device *dev, struct CfgTable *tb)
3184 int i;
3185 char temp_name[17];
3187 dev_info(dev, "Controller Configuration information\n");
3188 dev_info(dev, "------------------------------------\n");
3189 for (i = 0; i < 4; i++)
3190 temp_name[i] = readb(&(tb->Signature[i]));
3191 temp_name[4] = '\0';
3192 dev_info(dev, " Signature = %s\n", temp_name);
3193 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
3194 dev_info(dev, " Transport methods supported = 0x%x\n",
3195 readl(&(tb->TransportSupport)));
3196 dev_info(dev, " Transport methods active = 0x%x\n",
3197 readl(&(tb->TransportActive)));
3198 dev_info(dev, " Requested transport Method = 0x%x\n",
3199 readl(&(tb->HostWrite.TransportRequest)));
3200 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
3201 readl(&(tb->HostWrite.CoalIntDelay)));
3202 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
3203 readl(&(tb->HostWrite.CoalIntCount)));
3204 dev_info(dev, " Max outstanding commands = 0x%d\n",
3205 readl(&(tb->CmdsOutMax)));
3206 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
3207 for (i = 0; i < 16; i++)
3208 temp_name[i] = readb(&(tb->ServerName[i]));
3209 temp_name[16] = '\0';
3210 dev_info(dev, " Server Name = %s\n", temp_name);
3211 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
3212 readl(&(tb->HeartBeat)));
3214 #endif /* HPSA_DEBUG */
3216 static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
3218 int i, offset, mem_type, bar_type;
3220 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
3221 return 0;
3222 offset = 0;
3223 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3224 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
3225 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
3226 offset += 4;
3227 else {
3228 mem_type = pci_resource_flags(pdev, i) &
3229 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
3230 switch (mem_type) {
3231 case PCI_BASE_ADDRESS_MEM_TYPE_32:
3232 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
3233 offset += 4; /* 32 bit */
3234 break;
3235 case PCI_BASE_ADDRESS_MEM_TYPE_64:
3236 offset += 8;
3237 break;
3238 default: /* reserved in PCI 2.2 */
3239 dev_warn(&pdev->dev,
3240 "base address is invalid\n");
3241 return -1;
3242 break;
3245 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
3246 return i + 1;
3248 return -1;
3251 /* If MSI/MSI-X is supported by the kernel we will try to enable it on
3252 * controllers that are capable. If not, we use IO-APIC mode.
3255 static void __devinit hpsa_interrupt_mode(struct ctlr_info *h,
3256 struct pci_dev *pdev, u32 board_id)
3258 #ifdef CONFIG_PCI_MSI
3259 int err;
3260 struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1},
3261 {0, 2}, {0, 3}
3264 /* Some boards advertise MSI but don't really support it */
3265 if ((board_id == 0x40700E11) ||
3266 (board_id == 0x40800E11) ||
3267 (board_id == 0x40820E11) || (board_id == 0x40830E11))
3268 goto default_int_mode;
3269 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
3270 dev_info(&pdev->dev, "MSIX\n");
3271 err = pci_enable_msix(pdev, hpsa_msix_entries, 4);
3272 if (!err) {
3273 h->intr[0] = hpsa_msix_entries[0].vector;
3274 h->intr[1] = hpsa_msix_entries[1].vector;
3275 h->intr[2] = hpsa_msix_entries[2].vector;
3276 h->intr[3] = hpsa_msix_entries[3].vector;
3277 h->msix_vector = 1;
3278 return;
3280 if (err > 0) {
3281 dev_warn(&pdev->dev, "only %d MSI-X vectors "
3282 "available\n", err);
3283 goto default_int_mode;
3284 } else {
3285 dev_warn(&pdev->dev, "MSI-X init failed %d\n",
3286 err);
3287 goto default_int_mode;
3290 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
3291 dev_info(&pdev->dev, "MSI\n");
3292 if (!pci_enable_msi(pdev))
3293 h->msi_vector = 1;
3294 else
3295 dev_warn(&pdev->dev, "MSI init failed\n");
3297 default_int_mode:
3298 #endif /* CONFIG_PCI_MSI */
3299 /* if we get here we're going to use the default interrupt mode */
3300 h->intr[PERF_MODE_INT] = pdev->irq;
3303 static int hpsa_pci_init(struct ctlr_info *h, struct pci_dev *pdev)
3305 ushort subsystem_vendor_id, subsystem_device_id, command;
3306 u32 board_id, scratchpad = 0;
3307 u64 cfg_offset;
3308 u32 cfg_base_addr;
3309 u64 cfg_base_addr_index;
3310 u32 trans_offset;
3311 int i, prod_index, err;
3313 subsystem_vendor_id = pdev->subsystem_vendor;
3314 subsystem_device_id = pdev->subsystem_device;
3315 board_id = (((u32) (subsystem_device_id << 16) & 0xffff0000) |
3316 subsystem_vendor_id);
3318 for (i = 0; i < ARRAY_SIZE(products); i++)
3319 if (board_id == products[i].board_id)
3320 break;
3322 prod_index = i;
3324 if (prod_index == ARRAY_SIZE(products)) {
3325 prod_index--;
3326 if (subsystem_vendor_id != PCI_VENDOR_ID_HP ||
3327 !hpsa_allow_any) {
3328 dev_warn(&pdev->dev, "unrecognized board ID:"
3329 " 0x%08lx, ignoring.\n",
3330 (unsigned long) board_id);
3331 return -ENODEV;
3334 /* check to see if controller has been disabled
3335 * BEFORE trying to enable it
3337 (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
3338 if (!(command & 0x02)) {
3339 dev_warn(&pdev->dev, "controller appears to be disabled\n");
3340 return -ENODEV;
3343 err = pci_enable_device(pdev);
3344 if (err) {
3345 dev_warn(&pdev->dev, "unable to enable PCI device\n");
3346 return err;
3349 err = pci_request_regions(pdev, "hpsa");
3350 if (err) {
3351 dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
3352 return err;
3355 /* If the kernel supports MSI/MSI-X we will try to enable that,
3356 * else we use the IO-APIC interrupt assigned to us by system ROM.
3358 hpsa_interrupt_mode(h, pdev, board_id);
3360 /* find the memory BAR */
3361 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3362 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
3363 break;
3365 if (i == DEVICE_COUNT_RESOURCE) {
3366 dev_warn(&pdev->dev, "no memory BAR found\n");
3367 err = -ENODEV;
3368 goto err_out_free_res;
3371 h->paddr = pci_resource_start(pdev, i); /* addressing mode bits
3372 * already removed
3375 h->vaddr = remap_pci_mem(h->paddr, 0x250);
3377 /* Wait for the board to become ready. */
3378 for (i = 0; i < HPSA_BOARD_READY_ITERATIONS; i++) {
3379 scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
3380 if (scratchpad == HPSA_FIRMWARE_READY)
3381 break;
3382 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
3384 if (scratchpad != HPSA_FIRMWARE_READY) {
3385 dev_warn(&pdev->dev, "board not ready, timed out.\n");
3386 err = -ENODEV;
3387 goto err_out_free_res;
3390 /* get the address index number */
3391 cfg_base_addr = readl(h->vaddr + SA5_CTCFG_OFFSET);
3392 cfg_base_addr &= (u32) 0x0000ffff;
3393 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
3394 if (cfg_base_addr_index == -1) {
3395 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
3396 err = -ENODEV;
3397 goto err_out_free_res;
3400 cfg_offset = readl(h->vaddr + SA5_CTMEM_OFFSET);
3401 h->cfgtable = remap_pci_mem(pci_resource_start(pdev,
3402 cfg_base_addr_index) + cfg_offset,
3403 sizeof(h->cfgtable));
3404 /* Find performant mode table. */
3405 trans_offset = readl(&(h->cfgtable->TransMethodOffset));
3406 h->transtable = remap_pci_mem(pci_resource_start(pdev,
3407 cfg_base_addr_index)+cfg_offset+trans_offset,
3408 sizeof(*h->transtable));
3410 h->board_id = board_id;
3411 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
3412 h->product_name = products[prod_index].product_name;
3413 h->access = *(products[prod_index].access);
3414 /* Allow room for some ioctls */
3415 h->nr_cmds = h->max_commands - 4;
3417 if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
3418 (readb(&h->cfgtable->Signature[1]) != 'I') ||
3419 (readb(&h->cfgtable->Signature[2]) != 'S') ||
3420 (readb(&h->cfgtable->Signature[3]) != 'S')) {
3421 dev_warn(&pdev->dev, "not a valid CISS config table\n");
3422 err = -ENODEV;
3423 goto err_out_free_res;
3425 #ifdef CONFIG_X86
3427 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3428 u32 prefetch;
3429 prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
3430 prefetch |= 0x100;
3431 writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
3433 #endif
3435 /* Disabling DMA prefetch for the P600
3436 * An ASIC bug may result in a prefetch beyond
3437 * physical memory.
3439 if (board_id == 0x3225103C) {
3440 u32 dma_prefetch;
3441 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
3442 dma_prefetch |= 0x8000;
3443 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
3446 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
3447 /* Update the field, and then ring the doorbell */
3448 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
3449 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
3451 /* under certain very rare conditions, this can take awhile.
3452 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3453 * as we enter this code.)
3455 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
3456 if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3457 break;
3458 /* delay and try again */
3459 msleep(10);
3462 #ifdef HPSA_DEBUG
3463 print_cfg_table(&pdev->dev, h->cfgtable);
3464 #endif /* HPSA_DEBUG */
3466 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
3467 dev_warn(&pdev->dev, "unable to get board into simple mode\n");
3468 err = -ENODEV;
3469 goto err_out_free_res;
3471 return 0;
3473 err_out_free_res:
3475 * Deliberately omit pci_disable_device(): it does something nasty to
3476 * Smart Array controllers that pci_enable_device does not undo
3478 pci_release_regions(pdev);
3479 return err;
3482 static void __devinit hpsa_hba_inquiry(struct ctlr_info *h)
3484 int rc;
3486 #define HBA_INQUIRY_BYTE_COUNT 64
3487 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
3488 if (!h->hba_inquiry_data)
3489 return;
3490 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
3491 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
3492 if (rc != 0) {
3493 kfree(h->hba_inquiry_data);
3494 h->hba_inquiry_data = NULL;
3498 static int __devinit hpsa_init_one(struct pci_dev *pdev,
3499 const struct pci_device_id *ent)
3501 int i, rc;
3502 int dac;
3503 struct ctlr_info *h;
3505 if (number_of_controllers == 0)
3506 printk(KERN_INFO DRIVER_NAME "\n");
3507 if (reset_devices) {
3508 /* Reset the controller with a PCI power-cycle */
3509 if (hpsa_hard_reset_controller(pdev) || hpsa_reset_msi(pdev))
3510 return -ENODEV;
3512 /* Some devices (notably the HP Smart Array 5i Controller)
3513 need a little pause here */
3514 msleep(HPSA_POST_RESET_PAUSE_MSECS);
3516 /* Now try to get the controller to respond to a no-op */
3517 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
3518 if (hpsa_noop(pdev) == 0)
3519 break;
3520 else
3521 dev_warn(&pdev->dev, "no-op failed%s\n",
3522 (i < 11 ? "; re-trying" : ""));
3526 /* Command structures must be aligned on a 32-byte boundary because
3527 * the 5 lower bits of the address are used by the hardware. and by
3528 * the driver. See comments in hpsa.h for more info.
3530 #define COMMANDLIST_ALIGNMENT 32
3531 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
3532 h = kzalloc(sizeof(*h), GFP_KERNEL);
3533 if (!h)
3534 return -ENOMEM;
3536 h->busy_initializing = 1;
3537 INIT_HLIST_HEAD(&h->cmpQ);
3538 INIT_HLIST_HEAD(&h->reqQ);
3539 mutex_init(&h->busy_shutting_down);
3540 init_completion(&h->scan_wait);
3541 rc = hpsa_pci_init(h, pdev);
3542 if (rc != 0)
3543 goto clean1;
3545 sprintf(h->devname, "hpsa%d", number_of_controllers);
3546 h->ctlr = number_of_controllers;
3547 number_of_controllers++;
3548 h->pdev = pdev;
3550 /* configure PCI DMA stuff */
3551 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3552 if (rc == 0) {
3553 dac = 1;
3554 } else {
3555 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3556 if (rc == 0) {
3557 dac = 0;
3558 } else {
3559 dev_err(&pdev->dev, "no suitable DMA available\n");
3560 goto clean1;
3564 /* make sure the board interrupts are off */
3565 h->access.set_intr_mask(h, HPSA_INTR_OFF);
3566 rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr,
3567 IRQF_DISABLED, h->devname, h);
3568 if (rc) {
3569 dev_err(&pdev->dev, "unable to get irq %d for %s\n",
3570 h->intr[PERF_MODE_INT], h->devname);
3571 goto clean2;
3574 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
3575 h->devname, pdev->device,
3576 h->intr[PERF_MODE_INT], dac ? "" : " not");
3578 h->cmd_pool_bits =
3579 kmalloc(((h->nr_cmds + BITS_PER_LONG -
3580 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
3581 h->cmd_pool = pci_alloc_consistent(h->pdev,
3582 h->nr_cmds * sizeof(*h->cmd_pool),
3583 &(h->cmd_pool_dhandle));
3584 h->errinfo_pool = pci_alloc_consistent(h->pdev,
3585 h->nr_cmds * sizeof(*h->errinfo_pool),
3586 &(h->errinfo_pool_dhandle));
3587 if ((h->cmd_pool_bits == NULL)
3588 || (h->cmd_pool == NULL)
3589 || (h->errinfo_pool == NULL)) {
3590 dev_err(&pdev->dev, "out of memory");
3591 rc = -ENOMEM;
3592 goto clean4;
3594 spin_lock_init(&h->lock);
3595 spin_lock_init(&h->scan_lock);
3596 init_waitqueue_head(&h->scan_wait_queue);
3597 h->scan_finished = 1; /* no scan currently in progress */
3599 pci_set_drvdata(pdev, h);
3600 memset(h->cmd_pool_bits, 0,
3601 ((h->nr_cmds + BITS_PER_LONG -
3602 1) / BITS_PER_LONG) * sizeof(unsigned long));
3604 hpsa_scsi_setup(h);
3606 /* Turn the interrupts on so we can service requests */
3607 h->access.set_intr_mask(h, HPSA_INTR_ON);
3609 hpsa_put_ctlr_into_performant_mode(h);
3610 hpsa_hba_inquiry(h);
3611 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
3612 h->busy_initializing = 0;
3613 return 1;
3615 clean4:
3616 kfree(h->cmd_pool_bits);
3617 if (h->cmd_pool)
3618 pci_free_consistent(h->pdev,
3619 h->nr_cmds * sizeof(struct CommandList),
3620 h->cmd_pool, h->cmd_pool_dhandle);
3621 if (h->errinfo_pool)
3622 pci_free_consistent(h->pdev,
3623 h->nr_cmds * sizeof(struct ErrorInfo),
3624 h->errinfo_pool,
3625 h->errinfo_pool_dhandle);
3626 free_irq(h->intr[PERF_MODE_INT], h);
3627 clean2:
3628 clean1:
3629 h->busy_initializing = 0;
3630 kfree(h);
3631 return rc;
3634 static void hpsa_flush_cache(struct ctlr_info *h)
3636 char *flush_buf;
3637 struct CommandList *c;
3639 flush_buf = kzalloc(4, GFP_KERNEL);
3640 if (!flush_buf)
3641 return;
3643 c = cmd_special_alloc(h);
3644 if (!c) {
3645 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
3646 goto out_of_memory;
3648 fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
3649 RAID_CTLR_LUNID, TYPE_CMD);
3650 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
3651 if (c->err_info->CommandStatus != 0)
3652 dev_warn(&h->pdev->dev,
3653 "error flushing cache on controller\n");
3654 cmd_special_free(h, c);
3655 out_of_memory:
3656 kfree(flush_buf);
3659 static void hpsa_shutdown(struct pci_dev *pdev)
3661 struct ctlr_info *h;
3663 h = pci_get_drvdata(pdev);
3664 /* Turn board interrupts off and send the flush cache command
3665 * sendcmd will turn off interrupt, and send the flush...
3666 * To write all data in the battery backed cache to disks
3668 hpsa_flush_cache(h);
3669 h->access.set_intr_mask(h, HPSA_INTR_OFF);
3670 free_irq(h->intr[PERF_MODE_INT], h);
3671 #ifdef CONFIG_PCI_MSI
3672 if (h->msix_vector)
3673 pci_disable_msix(h->pdev);
3674 else if (h->msi_vector)
3675 pci_disable_msi(h->pdev);
3676 #endif /* CONFIG_PCI_MSI */
3679 static void __devexit hpsa_remove_one(struct pci_dev *pdev)
3681 struct ctlr_info *h;
3683 if (pci_get_drvdata(pdev) == NULL) {
3684 dev_err(&pdev->dev, "unable to remove device \n");
3685 return;
3687 h = pci_get_drvdata(pdev);
3688 mutex_lock(&h->busy_shutting_down);
3689 remove_from_scan_list(h);
3690 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
3691 hpsa_shutdown(pdev);
3692 iounmap(h->vaddr);
3693 pci_free_consistent(h->pdev,
3694 h->nr_cmds * sizeof(struct CommandList),
3695 h->cmd_pool, h->cmd_pool_dhandle);
3696 pci_free_consistent(h->pdev,
3697 h->nr_cmds * sizeof(struct ErrorInfo),
3698 h->errinfo_pool, h->errinfo_pool_dhandle);
3699 pci_free_consistent(h->pdev, h->reply_pool_size,
3700 h->reply_pool, h->reply_pool_dhandle);
3701 kfree(h->cmd_pool_bits);
3702 kfree(h->blockFetchTable);
3703 kfree(h->hba_inquiry_data);
3705 * Deliberately omit pci_disable_device(): it does something nasty to
3706 * Smart Array controllers that pci_enable_device does not undo
3708 pci_release_regions(pdev);
3709 pci_set_drvdata(pdev, NULL);
3710 mutex_unlock(&h->busy_shutting_down);
3711 kfree(h);
3714 static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
3715 __attribute__((unused)) pm_message_t state)
3717 return -ENOSYS;
3720 static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
3722 return -ENOSYS;
3725 static struct pci_driver hpsa_pci_driver = {
3726 .name = "hpsa",
3727 .probe = hpsa_init_one,
3728 .remove = __devexit_p(hpsa_remove_one),
3729 .id_table = hpsa_pci_device_id, /* id_table */
3730 .shutdown = hpsa_shutdown,
3731 .suspend = hpsa_suspend,
3732 .resume = hpsa_resume,
3735 /* Fill in bucket_map[], given nsgs (the max number of
3736 * scatter gather elements supported) and bucket[],
3737 * which is an array of 8 integers. The bucket[] array
3738 * contains 8 different DMA transfer sizes (in 16
3739 * byte increments) which the controller uses to fetch
3740 * commands. This function fills in bucket_map[], which
3741 * maps a given number of scatter gather elements to one of
3742 * the 8 DMA transfer sizes. The point of it is to allow the
3743 * controller to only do as much DMA as needed to fetch the
3744 * command, with the DMA transfer size encoded in the lower
3745 * bits of the command address.
3747 static void calc_bucket_map(int bucket[], int num_buckets,
3748 int nsgs, int *bucket_map)
3750 int i, j, b, size;
3752 /* even a command with 0 SGs requires 4 blocks */
3753 #define MINIMUM_TRANSFER_BLOCKS 4
3754 #define NUM_BUCKETS 8
3755 /* Note, bucket_map must have nsgs+1 entries. */
3756 for (i = 0; i <= nsgs; i++) {
3757 /* Compute size of a command with i SG entries */
3758 size = i + MINIMUM_TRANSFER_BLOCKS;
3759 b = num_buckets; /* Assume the biggest bucket */
3760 /* Find the bucket that is just big enough */
3761 for (j = 0; j < 8; j++) {
3762 if (bucket[j] >= size) {
3763 b = j;
3764 break;
3767 /* for a command with i SG entries, use bucket b. */
3768 bucket_map[i] = b;
3772 static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
3774 u32 trans_support;
3775 u64 trans_offset;
3776 /* 5 = 1 s/g entry or 4k
3777 * 6 = 2 s/g entry or 8k
3778 * 8 = 4 s/g entry or 16k
3779 * 10 = 6 s/g entry or 24k
3781 int bft[8] = {5, 6, 8, 10, 12, 20, 28, 35}; /* for scatter/gathers */
3782 int i = 0;
3783 int l = 0;
3784 unsigned long register_value;
3786 trans_support = readl(&(h->cfgtable->TransportSupport));
3787 if (!(trans_support & PERFORMANT_MODE))
3788 return;
3790 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
3791 h->max_sg_entries = 32;
3792 /* Performant mode ring buffer and supporting data structures */
3793 h->reply_pool_size = h->max_commands * sizeof(u64);
3794 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
3795 &(h->reply_pool_dhandle));
3797 /* Need a block fetch table for performant mode */
3798 h->blockFetchTable = kmalloc(((h->max_sg_entries+1) *
3799 sizeof(u32)), GFP_KERNEL);
3801 if ((h->reply_pool == NULL)
3802 || (h->blockFetchTable == NULL))
3803 goto clean_up;
3805 h->reply_pool_wraparound = 1; /* spec: init to 1 */
3807 /* Controller spec: zero out this buffer. */
3808 memset(h->reply_pool, 0, h->reply_pool_size);
3809 h->reply_pool_head = h->reply_pool;
3811 trans_offset = readl(&(h->cfgtable->TransMethodOffset));
3812 bft[7] = h->max_sg_entries + 4;
3813 calc_bucket_map(bft, ARRAY_SIZE(bft), 32, h->blockFetchTable);
3814 for (i = 0; i < 8; i++)
3815 writel(bft[i], &h->transtable->BlockFetch[i]);
3817 /* size of controller ring buffer */
3818 writel(h->max_commands, &h->transtable->RepQSize);
3819 writel(1, &h->transtable->RepQCount);
3820 writel(0, &h->transtable->RepQCtrAddrLow32);
3821 writel(0, &h->transtable->RepQCtrAddrHigh32);
3822 writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32);
3823 writel(0, &h->transtable->RepQAddr0High32);
3824 writel(CFGTBL_Trans_Performant,
3825 &(h->cfgtable->HostWrite.TransportRequest));
3826 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
3827 /* under certain very rare conditions, this can take awhile.
3828 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3829 * as we enter this code.) */
3830 for (l = 0; l < MAX_CONFIG_WAIT; l++) {
3831 register_value = readl(h->vaddr + SA5_DOORBELL);
3832 if (!(register_value & CFGTBL_ChangeReq))
3833 break;
3834 /* delay and try again */
3835 set_current_state(TASK_INTERRUPTIBLE);
3836 schedule_timeout(10);
3838 register_value = readl(&(h->cfgtable->TransportActive));
3839 if (!(register_value & CFGTBL_Trans_Performant)) {
3840 dev_warn(&h->pdev->dev, "unable to get board into"
3841 " performant mode\n");
3842 return;
3845 /* Change the access methods to the performant access methods */
3846 h->access = SA5_performant_access;
3847 h->transMethod = CFGTBL_Trans_Performant;
3849 return;
3851 clean_up:
3852 if (h->reply_pool)
3853 pci_free_consistent(h->pdev, h->reply_pool_size,
3854 h->reply_pool, h->reply_pool_dhandle);
3855 kfree(h->blockFetchTable);
3859 * This is it. Register the PCI driver information for the cards we control
3860 * the OS will call our registered routines when it finds one of our cards.
3862 static int __init hpsa_init(void)
3864 int err;
3865 /* Start the scan thread */
3866 hpsa_scan_thread = kthread_run(hpsa_scan_func, NULL, "hpsa_scan");
3867 if (IS_ERR(hpsa_scan_thread)) {
3868 err = PTR_ERR(hpsa_scan_thread);
3869 return -ENODEV;
3871 err = pci_register_driver(&hpsa_pci_driver);
3872 if (err)
3873 kthread_stop(hpsa_scan_thread);
3874 return err;
3877 static void __exit hpsa_cleanup(void)
3879 pci_unregister_driver(&hpsa_pci_driver);
3880 kthread_stop(hpsa_scan_thread);
3883 module_init(hpsa_init);
3884 module_exit(hpsa_cleanup);