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>
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>
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>
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 " \
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},
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);
121 static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
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,
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,
173 static struct device_attribute *hpsa_shost_attrs[] = {
178 static struct scsi_host_template hpsa_driver_template = {
179 .module = THIS_MODULE,
182 .queuecommand = hpsa_scsi_queue_command,
183 .scan_start = hpsa_scan_start,
184 .scan_finished = hpsa_scan_finished,
186 .sg_tablesize = MAXSGENTRIES,
187 .use_clustering = ENABLE_CLUSTERING,
188 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
190 .slave_alloc = hpsa_slave_alloc,
191 .slave_destroy = hpsa_slave_destroy,
193 .compat_ioctl = hpsa_compat_ioctl,
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
226 static int add_to_scan_list(struct ctlr_info *h)
228 struct ctlr_info *test_h;
232 if (h->busy_initializing)
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))
242 mutex_lock(&hpsa_scan_mutex);
243 list_for_each_entry(test_h, &hpsa_scan_q, scan_list) {
249 if (!found && !h->busy_scanning) {
250 INIT_COMPLETION(h->scan_wait);
251 list_add_tail(&h->scan_list, &hpsa_scan_q);
254 mutex_unlock(&hpsa_scan_mutex);
255 mutex_unlock(&h->busy_shutting_down);
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);
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
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().
305 static int hpsa_scan_func(__attribute__((unused)) void *data)
311 set_current_state(TASK_INTERRUPTIBLE);
313 if (kthread_should_stop())
317 mutex_lock(&hpsa_scan_mutex);
318 if (list_empty(&hpsa_scan_q)) {
319 mutex_unlock(&hpsa_scan_mutex);
322 h = list_entry(hpsa_scan_q.next, struct ctlr_info,
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);
338 static int check_for_unit_attention(struct ctlr_info *h,
339 struct CommandList *c)
341 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
344 switch (c->err_info->SenseInfo[12]) {
346 dev_warn(&h->pdev->dev, "hpsa%d: a state change "
347 "detected, command retried\n", h->ctlr);
350 dev_warn(&h->pdev->dev, "hpsa%d: LUN failure "
351 "detected, action required\n", h->ctlr);
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.
374 dev_warn(&h->pdev->dev, "hpsa%d: a power on "
375 "or device reset detected\n", h->ctlr);
377 case UNIT_ATTENTION_CLEARED:
378 dev_warn(&h->pdev->dev, "hpsa%d: unit attention "
379 "cleared by another initiator\n", h->ctlr);
382 dev_warn(&h->pdev->dev, "hpsa%d: unknown "
383 "unit attention detected\n", h->ctlr);
389 static ssize_t host_store_rescan(struct device *dev,
390 struct device_attribute *attr,
391 const char *buf, size_t count)
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);
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)
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--;
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;
431 /* set_performant_mode: Modify the tag for cciss performant
432 * set bit 0 for pull model, bits 3-1 for block fetch
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)
446 set_performant_mode(h, c);
447 spin_lock_irqsave(&h->lock, flags);
451 spin_unlock_irqrestore(&h->lock, flags);
454 static inline void removeQ(struct CommandList *c)
456 if (WARN_ON(hlist_unhashed(&c->list)))
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)
475 if ((h->hba_inquiry_data[2] & 0x07) == 5)
480 static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
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)
489 unsigned char rlevel;
491 struct scsi_device *sdev;
492 struct hpsa_scsi_dev_t *hdev;
495 sdev = to_scsi_device(dev);
496 h = sdev_to_hba(sdev);
497 spin_lock_irqsave(&h->lock, flags);
498 hdev = sdev->hostdata;
500 spin_unlock_irqrestore(&h->lock, flags);
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");
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]);
519 static ssize_t lunid_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
523 struct scsi_device *sdev;
524 struct hpsa_scsi_dev_t *hdev;
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;
533 spin_unlock_irqrestore(&h->lock, flags);
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)
547 struct scsi_device *sdev;
548 struct hpsa_scsi_dev_t *hdev;
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;
557 spin_unlock_irqrestore(&h->lock, flags);
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
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)) {
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 */
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 "
616 /* physical devices do not have lun or target assigned until now. */
617 if (device->lun != -1)
618 /* Logical device, lun is already 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)
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);
641 for (i = 0; i < n; i++) {
643 memcpy(addr2, sd->scsi3addr, 8);
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];
653 if (device->lun == -1) {
654 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
655 " suspect firmware bug or unsupported hardware "
664 added[*nadded] = device;
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);
678 /* Remove an entry from h->dev[] array. */
679 static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
680 struct hpsa_scsi_dev_t *removed[], int *nremoved)
682 /* assumes h->devlock is held */
684 struct hpsa_scsi_dev_t *sd;
686 BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
689 removed[*nremoved] = h->dev[entry];
692 for (i = entry; i < h->ndevices-1; i++)
693 h->dev[i] = h->dev[i+1];
695 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
696 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
700 #define SCSI3ADDR_EQ(a, b) ( \
701 (a)[7] == (b)[7] && \
702 (a)[6] == (b)[6] && \
703 (a)[5] == (b)[5] && \
704 (a)[4] == (b)[4] && \
705 (a)[3] == (b)[3] && \
706 (a)[2] == (b)[2] && \
707 (a)[1] == (b)[1] && \
710 static void fixup_botched_add(struct ctlr_info *h,
711 struct hpsa_scsi_dev_t *added)
713 /* called when scsi_add_device fails in order to re-adjust
714 * h->dev[] to match the mid layer's view.
719 spin_lock_irqsave(&h->lock, flags);
720 for (i = 0; i < h->ndevices; i++) {
721 if (h->dev[i] == added) {
722 for (j = i; j < h->ndevices-1; j++)
723 h->dev[j] = h->dev[j+1];
728 spin_unlock_irqrestore(&h->lock, flags);
732 static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
733 struct hpsa_scsi_dev_t *dev2)
735 if ((is_logical_dev_addr_mode(dev1->scsi3addr) ||
736 (dev1->lun != -1 && dev2->lun != -1)) &&
737 dev1->devtype != 0x0C)
738 return (memcmp(dev1, dev2, sizeof(*dev1)) == 0);
740 /* we compare everything except lun and target as these
741 * are not yet assigned. Compare parts likely
744 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
745 sizeof(dev1->scsi3addr)) != 0)
747 if (memcmp(dev1->device_id, dev2->device_id,
748 sizeof(dev1->device_id)) != 0)
750 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
752 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
754 if (memcmp(dev1->revision, dev2->revision, sizeof(dev1->revision)) != 0)
756 if (dev1->devtype != dev2->devtype)
758 if (dev1->raid_level != dev2->raid_level)
760 if (dev1->bus != dev2->bus)
765 /* Find needle in haystack. If exact match found, return DEVICE_SAME,
766 * and return needle location in *index. If scsi3addr matches, but not
767 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
768 * location in *index. If needle not found, return DEVICE_NOT_FOUND.
770 static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
771 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
775 #define DEVICE_NOT_FOUND 0
776 #define DEVICE_CHANGED 1
777 #define DEVICE_SAME 2
778 for (i = 0; i < haystack_size; i++) {
779 if (haystack[i] == NULL) /* previously removed. */
781 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
783 if (device_is_the_same(needle, haystack[i]))
786 return DEVICE_CHANGED;
790 return DEVICE_NOT_FOUND;
793 static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
794 struct hpsa_scsi_dev_t *sd[], int nsds)
796 /* sd contains scsi3 addresses and devtypes, and inquiry
797 * data. This function takes what's in sd to be the current
798 * reality and updates h->dev[] to reflect that reality.
800 int i, entry, device_change, changes = 0;
801 struct hpsa_scsi_dev_t *csd;
803 struct hpsa_scsi_dev_t **added, **removed;
804 int nadded, nremoved;
805 struct Scsi_Host *sh = NULL;
807 added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA,
809 removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA,
812 if (!added || !removed) {
813 dev_warn(&h->pdev->dev, "out of memory in "
814 "adjust_hpsa_scsi_table\n");
818 spin_lock_irqsave(&h->devlock, flags);
820 /* find any devices in h->dev[] that are not in
821 * sd[] and remove them from h->dev[], and for any
822 * devices which have changed, remove the old device
823 * info and add the new device info.
828 while (i < h->ndevices) {
830 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
831 if (device_change == DEVICE_NOT_FOUND) {
833 hpsa_scsi_remove_entry(h, hostno, i,
835 continue; /* remove ^^^, hence i not incremented */
836 } else if (device_change == DEVICE_CHANGED) {
838 hpsa_scsi_remove_entry(h, hostno, i,
840 (void) hpsa_scsi_add_entry(h, hostno, sd[entry],
842 /* add can't fail, we just removed one. */
844 /* Set it to NULL to prevent it from being freed
845 * at the bottom of hpsa_update_scsi_devices()
852 /* Now, make sure every device listed in sd[] is also
853 * listed in h->dev[], adding them if they aren't found
856 for (i = 0; i < nsds; i++) {
857 if (!sd[i]) /* if already added above. */
859 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
860 h->ndevices, &entry);
861 if (device_change == DEVICE_NOT_FOUND) {
863 if (hpsa_scsi_add_entry(h, hostno, sd[i],
864 added, &nadded) != 0)
866 sd[i] = NULL; /* prevent from being freed later. */
867 } else if (device_change == DEVICE_CHANGED) {
868 /* should never happen... */
870 dev_warn(&h->pdev->dev,
871 "device unexpectedly changed.\n");
872 /* but if it does happen, we just ignore that device */
875 spin_unlock_irqrestore(&h->devlock, flags);
877 /* Don't notify scsi mid layer of any changes the first time through
878 * (or if there are no changes) scsi_scan_host will do it later the
879 * first time through.
881 if (hostno == -1 || !changes)
885 /* Notify scsi mid layer of any removed devices */
886 for (i = 0; i < nremoved; i++) {
887 struct scsi_device *sdev =
888 scsi_device_lookup(sh, removed[i]->bus,
889 removed[i]->target, removed[i]->lun);
891 scsi_remove_device(sdev);
892 scsi_device_put(sdev);
894 /* We don't expect to get here.
895 * future cmds to this device will get selection
896 * timeout as if the device was gone.
898 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
899 " for removal.", hostno, removed[i]->bus,
900 removed[i]->target, removed[i]->lun);
906 /* Notify scsi mid layer of any added devices */
907 for (i = 0; i < nadded; i++) {
908 if (scsi_add_device(sh, added[i]->bus,
909 added[i]->target, added[i]->lun) == 0)
911 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
912 "device not added.\n", hostno, added[i]->bus,
913 added[i]->target, added[i]->lun);
914 /* now we have to remove it from h->dev,
915 * since it didn't get added to scsi mid layer
917 fixup_botched_add(h, added[i]);
926 * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
927 * Assume's h->devlock is held.
929 static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
930 int bus, int target, int lun)
933 struct hpsa_scsi_dev_t *sd;
935 for (i = 0; i < h->ndevices; i++) {
937 if (sd->bus == bus && sd->target == target && sd->lun == lun)
943 /* link sdev->hostdata to our per-device structure. */
944 static int hpsa_slave_alloc(struct scsi_device *sdev)
946 struct hpsa_scsi_dev_t *sd;
950 h = sdev_to_hba(sdev);
951 spin_lock_irqsave(&h->devlock, flags);
952 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
953 sdev_id(sdev), sdev->lun);
956 spin_unlock_irqrestore(&h->devlock, flags);
960 static void hpsa_slave_destroy(struct scsi_device *sdev)
965 static void hpsa_scsi_setup(struct ctlr_info *h)
969 spin_lock_init(&h->devlock);
972 static void complete_scsi_command(struct CommandList *cp,
973 int timeout, u32 tag)
975 struct scsi_cmnd *cmd;
977 struct ErrorInfo *ei;
979 unsigned char sense_key;
980 unsigned char asc; /* additional sense code */
981 unsigned char ascq; /* additional sense code qualifier */
984 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
987 scsi_dma_unmap(cmd); /* undo the DMA mappings */
989 cmd->result = (DID_OK << 16); /* host byte */
990 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
991 cmd->result |= (ei->ScsiStatus << 1);
993 /* copy the sense data whether we need to or not. */
994 memcpy(cmd->sense_buffer, ei->SenseInfo,
995 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
996 SCSI_SENSE_BUFFERSIZE :
998 scsi_set_resid(cmd, ei->ResidualCnt);
1000 if (ei->CommandStatus == 0) {
1001 cmd->scsi_done(cmd);
1006 /* an error has occurred */
1007 switch (ei->CommandStatus) {
1009 case CMD_TARGET_STATUS:
1010 if (ei->ScsiStatus) {
1012 sense_key = 0xf & ei->SenseInfo[2];
1013 /* Get additional sense code */
1014 asc = ei->SenseInfo[12];
1015 /* Get addition sense code qualifier */
1016 ascq = ei->SenseInfo[13];
1019 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
1020 if (check_for_unit_attention(h, cp)) {
1021 cmd->result = DID_SOFT_ERROR << 16;
1024 if (sense_key == ILLEGAL_REQUEST) {
1026 * SCSI REPORT_LUNS is commonly unsupported on
1027 * Smart Array. Suppress noisy complaint.
1029 if (cp->Request.CDB[0] == REPORT_LUNS)
1032 /* If ASC/ASCQ indicate Logical Unit
1033 * Not Supported condition,
1035 if ((asc == 0x25) && (ascq == 0x0)) {
1036 dev_warn(&h->pdev->dev, "cp %p "
1037 "has check condition\n", cp);
1042 if (sense_key == NOT_READY) {
1043 /* If Sense is Not Ready, Logical Unit
1044 * Not ready, Manual Intervention
1047 if ((asc == 0x04) && (ascq == 0x03)) {
1048 dev_warn(&h->pdev->dev, "cp %p "
1049 "has check condition: unit "
1050 "not ready, manual "
1051 "intervention required\n", cp);
1055 if (sense_key == ABORTED_COMMAND) {
1056 /* Aborted command is retryable */
1057 dev_warn(&h->pdev->dev, "cp %p "
1058 "has check condition: aborted command: "
1059 "ASC: 0x%x, ASCQ: 0x%x\n",
1061 cmd->result = DID_SOFT_ERROR << 16;
1064 /* Must be some other type of check condition */
1065 dev_warn(&h->pdev->dev, "cp %p has check condition: "
1067 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1068 "Returning result: 0x%x, "
1069 "cmd=[%02x %02x %02x %02x %02x "
1070 "%02x %02x %02x %02x %02x %02x "
1071 "%02x %02x %02x %02x %02x]\n",
1072 cp, sense_key, asc, ascq,
1074 cmd->cmnd[0], cmd->cmnd[1],
1075 cmd->cmnd[2], cmd->cmnd[3],
1076 cmd->cmnd[4], cmd->cmnd[5],
1077 cmd->cmnd[6], cmd->cmnd[7],
1078 cmd->cmnd[8], cmd->cmnd[9],
1079 cmd->cmnd[10], cmd->cmnd[11],
1080 cmd->cmnd[12], cmd->cmnd[13],
1081 cmd->cmnd[14], cmd->cmnd[15]);
1086 /* Problem was not a check condition
1087 * Pass it up to the upper layers...
1089 if (ei->ScsiStatus) {
1090 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1091 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1092 "Returning result: 0x%x\n",
1094 sense_key, asc, ascq,
1096 } else { /* scsi status is zero??? How??? */
1097 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1098 "Returning no connection.\n", cp),
1100 /* Ordinarily, this case should never happen,
1101 * but there is a bug in some released firmware
1102 * revisions that allows it to happen if, for
1103 * example, a 4100 backplane loses power and
1104 * the tape drive is in it. We assume that
1105 * it's a fatal error of some kind because we
1106 * can't show that it wasn't. We will make it
1107 * look like selection timeout since that is
1108 * the most common reason for this to occur,
1109 * and it's severe enough.
1112 cmd->result = DID_NO_CONNECT << 16;
1116 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1118 case CMD_DATA_OVERRUN:
1119 dev_warn(&h->pdev->dev, "cp %p has"
1120 " completed with data overrun "
1124 /* print_bytes(cp, sizeof(*cp), 1, 0);
1126 /* We get CMD_INVALID if you address a non-existent device
1127 * instead of a selection timeout (no response). You will
1128 * see this if you yank out a drive, then try to access it.
1129 * This is kind of a shame because it means that any other
1130 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1131 * missing target. */
1132 cmd->result = DID_NO_CONNECT << 16;
1135 case CMD_PROTOCOL_ERR:
1136 dev_warn(&h->pdev->dev, "cp %p has "
1137 "protocol error \n", cp);
1139 case CMD_HARDWARE_ERR:
1140 cmd->result = DID_ERROR << 16;
1141 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1143 case CMD_CONNECTION_LOST:
1144 cmd->result = DID_ERROR << 16;
1145 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1148 cmd->result = DID_ABORT << 16;
1149 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1150 cp, ei->ScsiStatus);
1152 case CMD_ABORT_FAILED:
1153 cmd->result = DID_ERROR << 16;
1154 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1156 case CMD_UNSOLICITED_ABORT:
1157 cmd->result = DID_RESET << 16;
1158 dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited "
1162 cmd->result = DID_TIME_OUT << 16;
1163 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1166 cmd->result = DID_ERROR << 16;
1167 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1168 cp, ei->CommandStatus);
1170 cmd->scsi_done(cmd);
1174 static int hpsa_scsi_detect(struct ctlr_info *h)
1176 struct Scsi_Host *sh;
1179 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
1186 sh->max_channel = 3;
1187 sh->max_cmd_len = MAX_COMMAND_SIZE;
1188 sh->max_lun = HPSA_MAX_LUN;
1189 sh->max_id = HPSA_MAX_LUN;
1190 sh->can_queue = h->nr_cmds;
1191 sh->cmd_per_lun = h->nr_cmds;
1193 sh->hostdata[0] = (unsigned long) h;
1194 sh->irq = h->intr[PERF_MODE_INT];
1195 sh->unique_id = sh->irq;
1196 error = scsi_add_host(sh, &h->pdev->dev);
1203 dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host"
1204 " failed for controller %d\n", h->ctlr);
1208 dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc"
1209 " failed for controller %d\n", h->ctlr);
1213 static void hpsa_pci_unmap(struct pci_dev *pdev,
1214 struct CommandList *c, int sg_used, int data_direction)
1217 union u64bit addr64;
1219 for (i = 0; i < sg_used; i++) {
1220 addr64.val32.lower = c->SG[i].Addr.lower;
1221 addr64.val32.upper = c->SG[i].Addr.upper;
1222 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1227 static void hpsa_map_one(struct pci_dev *pdev,
1228 struct CommandList *cp,
1235 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1236 cp->Header.SGList = 0;
1237 cp->Header.SGTotal = 0;
1241 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
1242 cp->SG[0].Addr.lower =
1243 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
1244 cp->SG[0].Addr.upper =
1245 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
1246 cp->SG[0].Len = buflen;
1247 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1248 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
1251 static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1252 struct CommandList *c)
1254 DECLARE_COMPLETION_ONSTACK(wait);
1257 enqueue_cmd_and_start_io(h, c);
1258 wait_for_completion(&wait);
1261 static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1262 struct CommandList *c, int data_direction)
1264 int retry_count = 0;
1267 memset(c->err_info, 0, sizeof(c->err_info));
1268 hpsa_scsi_do_simple_cmd_core(h, c);
1270 } while (check_for_unit_attention(h, c) && retry_count <= 3);
1271 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1274 static void hpsa_scsi_interpret_error(struct CommandList *cp)
1276 struct ErrorInfo *ei;
1277 struct device *d = &cp->h->pdev->dev;
1280 switch (ei->CommandStatus) {
1281 case CMD_TARGET_STATUS:
1282 dev_warn(d, "cmd %p has completed with errors\n", cp);
1283 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1285 if (ei->ScsiStatus == 0)
1286 dev_warn(d, "SCSI status is abnormally zero. "
1287 "(probably indicates selection timeout "
1288 "reported incorrectly due to a known "
1289 "firmware bug, circa July, 2001.)\n");
1291 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1292 dev_info(d, "UNDERRUN\n");
1294 case CMD_DATA_OVERRUN:
1295 dev_warn(d, "cp %p has completed with data overrun\n", cp);
1298 /* controller unfortunately reports SCSI passthru's
1299 * to non-existent targets as invalid commands.
1301 dev_warn(d, "cp %p is reported invalid (probably means "
1302 "target device no longer present)\n", cp);
1303 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1307 case CMD_PROTOCOL_ERR:
1308 dev_warn(d, "cp %p has protocol error \n", cp);
1310 case CMD_HARDWARE_ERR:
1311 /* cmd->result = DID_ERROR << 16; */
1312 dev_warn(d, "cp %p had hardware error\n", cp);
1314 case CMD_CONNECTION_LOST:
1315 dev_warn(d, "cp %p had connection lost\n", cp);
1318 dev_warn(d, "cp %p was aborted\n", cp);
1320 case CMD_ABORT_FAILED:
1321 dev_warn(d, "cp %p reports abort failed\n", cp);
1323 case CMD_UNSOLICITED_ABORT:
1324 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1327 dev_warn(d, "cp %p timed out\n", cp);
1330 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1335 static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1336 unsigned char page, unsigned char *buf,
1337 unsigned char bufsize)
1340 struct CommandList *c;
1341 struct ErrorInfo *ei;
1343 c = cmd_special_alloc(h);
1345 if (c == NULL) { /* trouble... */
1346 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1350 fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD);
1351 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1353 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1354 hpsa_scsi_interpret_error(c);
1357 cmd_special_free(h, c);
1361 static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
1364 struct CommandList *c;
1365 struct ErrorInfo *ei;
1367 c = cmd_special_alloc(h);
1369 if (c == NULL) { /* trouble... */
1370 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1374 fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG);
1375 hpsa_scsi_do_simple_cmd_core(h, c);
1376 /* no unmap needed here because no data xfer. */
1379 if (ei->CommandStatus != 0) {
1380 hpsa_scsi_interpret_error(c);
1383 cmd_special_free(h, c);
1387 static void hpsa_get_raid_level(struct ctlr_info *h,
1388 unsigned char *scsi3addr, unsigned char *raid_level)
1393 *raid_level = RAID_UNKNOWN;
1394 buf = kzalloc(64, GFP_KERNEL);
1397 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1399 *raid_level = buf[8];
1400 if (*raid_level > RAID_UNKNOWN)
1401 *raid_level = RAID_UNKNOWN;
1406 /* Get the device id from inquiry page 0x83 */
1407 static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
1408 unsigned char *device_id, int buflen)
1415 buf = kzalloc(64, GFP_KERNEL);
1418 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1420 memcpy(device_id, &buf[8], buflen);
1425 static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
1426 struct ReportLUNdata *buf, int bufsize,
1427 int extended_response)
1430 struct CommandList *c;
1431 unsigned char scsi3addr[8];
1432 struct ErrorInfo *ei;
1434 c = cmd_special_alloc(h);
1435 if (c == NULL) { /* trouble... */
1436 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1439 /* address the controller */
1440 memset(scsi3addr, 0, sizeof(scsi3addr));
1441 fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
1442 buf, bufsize, 0, scsi3addr, TYPE_CMD);
1443 if (extended_response)
1444 c->Request.CDB[1] = extended_response;
1445 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1447 if (ei->CommandStatus != 0 &&
1448 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1449 hpsa_scsi_interpret_error(c);
1452 cmd_special_free(h, c);
1456 static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
1457 struct ReportLUNdata *buf,
1458 int bufsize, int extended_response)
1460 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
1463 static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
1464 struct ReportLUNdata *buf, int bufsize)
1466 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
1469 static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
1470 int bus, int target, int lun)
1473 device->target = target;
1477 static int hpsa_update_device_info(struct ctlr_info *h,
1478 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device)
1480 #define OBDR_TAPE_INQ_SIZE 49
1481 unsigned char *inq_buff;
1483 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1487 /* Do an inquiry to the device to see what it is. */
1488 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1489 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1490 /* Inquiry failed (msg printed already) */
1491 dev_err(&h->pdev->dev,
1492 "hpsa_update_device_info: inquiry failed\n");
1496 /* As a side effect, record the firmware version number
1497 * if we happen to be talking to the RAID controller.
1499 if (is_hba_lunid(scsi3addr))
1500 memcpy(h->firm_ver, &inq_buff[32], 4);
1502 this_device->devtype = (inq_buff[0] & 0x1f);
1503 memcpy(this_device->scsi3addr, scsi3addr, 8);
1504 memcpy(this_device->vendor, &inq_buff[8],
1505 sizeof(this_device->vendor));
1506 memcpy(this_device->model, &inq_buff[16],
1507 sizeof(this_device->model));
1508 memcpy(this_device->revision, &inq_buff[32],
1509 sizeof(this_device->revision));
1510 memset(this_device->device_id, 0,
1511 sizeof(this_device->device_id));
1512 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
1513 sizeof(this_device->device_id));
1515 if (this_device->devtype == TYPE_DISK &&
1516 is_logical_dev_addr_mode(scsi3addr))
1517 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
1519 this_device->raid_level = RAID_UNKNOWN;
1529 static unsigned char *msa2xxx_model[] = {
1537 static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1541 for (i = 0; msa2xxx_model[i]; i++)
1542 if (strncmp(device->model, msa2xxx_model[i],
1543 strlen(msa2xxx_model[i])) == 0)
1548 /* Helper function to assign bus, target, lun mapping of devices.
1549 * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
1550 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
1551 * Logical drive target and lun are assigned at this time, but
1552 * physical device lun and target assignment are deferred (assigned
1553 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
1555 static void figure_bus_target_lun(struct ctlr_info *h,
1556 u8 *lunaddrbytes, int *bus, int *target, int *lun,
1557 struct hpsa_scsi_dev_t *device)
1561 if (is_logical_dev_addr_mode(lunaddrbytes)) {
1562 /* logical device */
1563 if (unlikely(is_scsi_rev_5(h))) {
1564 /* p1210m, logical drives lun assignments
1565 * match SCSI REPORT LUNS data.
1567 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
1570 *lun = (lunid & 0x3fff) + 1;
1573 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
1574 if (is_msa2xxx(h, device)) {
1575 /* msa2xxx way, put logicals on bus 1
1576 * and match target/lun numbers box
1580 *target = (lunid >> 16) & 0x3fff;
1581 *lun = lunid & 0x00ff;
1583 /* Traditional smart array way. */
1586 *target = lunid & 0x3fff;
1590 /* physical device */
1591 if (is_hba_lunid(lunaddrbytes))
1592 if (unlikely(is_scsi_rev_5(h))) {
1593 *bus = 0; /* put p1210m ctlr at 0,0,0 */
1598 *bus = 3; /* traditional smartarray */
1600 *bus = 2; /* physical disk */
1602 *lun = -1; /* we will fill these in later. */
1607 * If there is no lun 0 on a target, linux won't find any devices.
1608 * For the MSA2xxx boxes, we have to manually detect the enclosure
1609 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
1610 * it for some reason. *tmpdevice is the target we're adding,
1611 * this_device is a pointer into the current element of currentsd[]
1612 * that we're building up in update_scsi_devices(), below.
1613 * lunzerobits is a bitmap that tracks which targets already have a
1615 * Returns 1 if an enclosure was added, 0 if not.
1617 static int add_msa2xxx_enclosure_device(struct ctlr_info *h,
1618 struct hpsa_scsi_dev_t *tmpdevice,
1619 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
1620 int bus, int target, int lun, unsigned long lunzerobits[],
1621 int *nmsa2xxx_enclosures)
1623 unsigned char scsi3addr[8];
1625 if (test_bit(target, lunzerobits))
1626 return 0; /* There is already a lun 0 on this target. */
1628 if (!is_logical_dev_addr_mode(lunaddrbytes))
1629 return 0; /* It's the logical targets that may lack lun 0. */
1631 if (!is_msa2xxx(h, tmpdevice))
1632 return 0; /* It's only the MSA2xxx that have this problem. */
1634 if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */
1637 if (is_hba_lunid(scsi3addr))
1638 return 0; /* Don't add the RAID controller here. */
1640 if (is_scsi_rev_5(h))
1641 return 0; /* p1210m doesn't need to do this. */
1643 #define MAX_MSA2XXX_ENCLOSURES 32
1644 if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) {
1645 dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX "
1646 "enclosures exceeded. Check your hardware "
1651 memset(scsi3addr, 0, 8);
1652 scsi3addr[3] = target;
1653 if (hpsa_update_device_info(h, scsi3addr, this_device))
1655 (*nmsa2xxx_enclosures)++;
1656 hpsa_set_bus_target_lun(this_device, bus, target, 0);
1657 set_bit(target, lunzerobits);
1662 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
1663 * logdev. The number of luns in physdev and logdev are returned in
1664 * *nphysicals and *nlogicals, respectively.
1665 * Returns 0 on success, -1 otherwise.
1667 static int hpsa_gather_lun_info(struct ctlr_info *h,
1669 struct ReportLUNdata *physdev, u32 *nphysicals,
1670 struct ReportLUNdata *logdev, u32 *nlogicals)
1672 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
1673 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
1676 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8;
1677 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
1678 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
1679 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1680 *nphysicals - HPSA_MAX_PHYS_LUN);
1681 *nphysicals = HPSA_MAX_PHYS_LUN;
1683 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
1684 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
1687 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
1688 /* Reject Logicals in excess of our max capability. */
1689 if (*nlogicals > HPSA_MAX_LUN) {
1690 dev_warn(&h->pdev->dev,
1691 "maximum logical LUNs (%d) exceeded. "
1692 "%d LUNs ignored.\n", HPSA_MAX_LUN,
1693 *nlogicals - HPSA_MAX_LUN);
1694 *nlogicals = HPSA_MAX_LUN;
1696 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
1697 dev_warn(&h->pdev->dev,
1698 "maximum logical + physical LUNs (%d) exceeded. "
1699 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1700 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
1701 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
1706 u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
1707 int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list,
1708 struct ReportLUNdata *logdev_list)
1710 /* Helper function, figure out where the LUN ID info is coming from
1711 * given index i, lists of physical and logical devices, where in
1712 * the list the raid controller is supposed to appear (first or last)
1715 int logicals_start = nphysicals + (raid_ctlr_position == 0);
1716 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
1718 if (i == raid_ctlr_position)
1719 return RAID_CTLR_LUNID;
1721 if (i < logicals_start)
1722 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
1724 if (i < last_device)
1725 return &logdev_list->LUN[i - nphysicals -
1726 (raid_ctlr_position == 0)][0];
1731 static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
1733 /* the idea here is we could get notified
1734 * that some devices have changed, so we do a report
1735 * physical luns and report logical luns cmd, and adjust
1736 * our list of devices accordingly.
1738 * The scsi3addr's of devices won't change so long as the
1739 * adapter is not reset. That means we can rescan and
1740 * tell which devices we already know about, vs. new
1741 * devices, vs. disappearing devices.
1743 struct ReportLUNdata *physdev_list = NULL;
1744 struct ReportLUNdata *logdev_list = NULL;
1745 unsigned char *inq_buff = NULL;
1748 u32 ndev_allocated = 0;
1749 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
1751 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
1752 int i, nmsa2xxx_enclosures, ndevs_to_allocate;
1753 int bus, target, lun;
1754 int raid_ctlr_position;
1755 DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR);
1757 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA,
1759 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1760 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1761 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1762 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
1764 if (!currentsd || !physdev_list || !logdev_list ||
1765 !inq_buff || !tmpdevice) {
1766 dev_err(&h->pdev->dev, "out of memory\n");
1769 memset(lunzerobits, 0, sizeof(lunzerobits));
1771 if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
1772 logdev_list, &nlogicals))
1775 /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
1776 * but each of them 4 times through different paths. The plus 1
1777 * is for the RAID controller.
1779 ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1;
1781 /* Allocate the per device structures */
1782 for (i = 0; i < ndevs_to_allocate; i++) {
1783 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
1784 if (!currentsd[i]) {
1785 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
1786 __FILE__, __LINE__);
1792 if (unlikely(is_scsi_rev_5(h)))
1793 raid_ctlr_position = 0;
1795 raid_ctlr_position = nphysicals + nlogicals;
1797 /* adjust our table of devices */
1798 nmsa2xxx_enclosures = 0;
1799 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
1802 /* Figure out where the LUN ID info is coming from */
1803 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
1804 i, nphysicals, nlogicals, physdev_list, logdev_list);
1805 /* skip masked physical devices. */
1806 if (lunaddrbytes[3] & 0xC0 &&
1807 i < nphysicals + (raid_ctlr_position == 0))
1810 /* Get device type, vendor, model, device id */
1811 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice))
1812 continue; /* skip it if we can't talk to it. */
1813 figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun,
1815 this_device = currentsd[ncurrent];
1818 * For the msa2xxx boxes, we have to insert a LUN 0 which
1819 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
1820 * is nonetheless an enclosure device there. We have to
1821 * present that otherwise linux won't find anything if
1822 * there is no lun 0.
1824 if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device,
1825 lunaddrbytes, bus, target, lun, lunzerobits,
1826 &nmsa2xxx_enclosures)) {
1828 this_device = currentsd[ncurrent];
1831 *this_device = *tmpdevice;
1832 hpsa_set_bus_target_lun(this_device, bus, target, lun);
1834 switch (this_device->devtype) {
1836 /* We don't *really* support actual CD-ROM devices,
1837 * just "One Button Disaster Recovery" tape drive
1838 * which temporarily pretends to be a CD-ROM drive.
1839 * So we check that the device is really an OBDR tape
1840 * device by checking for "$DR-10" in bytes 43-48 of
1844 #define OBDR_TAPE_SIG "$DR-10"
1845 strncpy(obdr_sig, &inq_buff[43], 6);
1847 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1848 /* Not OBDR device, ignore it. */
1859 case TYPE_MEDIUM_CHANGER:
1863 /* Only present the Smartarray HBA as a RAID controller.
1864 * If it's a RAID controller other than the HBA itself
1865 * (an external RAID controller, MSA500 or similar)
1868 if (!is_hba_lunid(lunaddrbytes))
1875 if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA)
1878 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
1881 for (i = 0; i < ndev_allocated; i++)
1882 kfree(currentsd[i]);
1885 kfree(physdev_list);
1889 /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1890 * dma mapping and fills in the scatter gather entries of the
1893 static int hpsa_scatter_gather(struct pci_dev *pdev,
1894 struct CommandList *cp,
1895 struct scsi_cmnd *cmd)
1898 struct scatterlist *sg;
1902 BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
1904 use_sg = scsi_dma_map(cmd);
1909 goto sglist_finished;
1911 scsi_for_each_sg(cmd, sg, use_sg, i) {
1912 addr64 = (u64) sg_dma_address(sg);
1913 len = sg_dma_len(sg);
1914 cp->SG[i].Addr.lower =
1915 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
1916 cp->SG[i].Addr.upper =
1917 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
1918 cp->SG[i].Len = len;
1919 cp->SG[i].Ext = 0; /* we are not chaining */
1924 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
1925 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
1930 static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
1931 void (*done)(struct scsi_cmnd *))
1933 struct ctlr_info *h;
1934 struct hpsa_scsi_dev_t *dev;
1935 unsigned char scsi3addr[8];
1936 struct CommandList *c;
1937 unsigned long flags;
1939 /* Get the ptr to our adapter structure out of cmd->host. */
1940 h = sdev_to_hba(cmd->device);
1941 dev = cmd->device->hostdata;
1943 cmd->result = DID_NO_CONNECT << 16;
1947 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
1949 /* Need a lock as this is being allocated from the pool */
1950 spin_lock_irqsave(&h->lock, flags);
1952 spin_unlock_irqrestore(&h->lock, flags);
1953 if (c == NULL) { /* trouble... */
1954 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
1955 return SCSI_MLQUEUE_HOST_BUSY;
1958 /* Fill in the command list header */
1960 cmd->scsi_done = done; /* save this for use by completion code */
1962 /* save c in case we have to abort it */
1963 cmd->host_scribble = (unsigned char *) c;
1965 c->cmd_type = CMD_SCSI;
1967 c->Header.ReplyQueue = 0; /* unused in simple mode */
1968 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1969 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
1970 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
1972 /* Fill in the request block... */
1974 c->Request.Timeout = 0;
1975 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
1976 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
1977 c->Request.CDBLen = cmd->cmd_len;
1978 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
1979 c->Request.Type.Type = TYPE_CMD;
1980 c->Request.Type.Attribute = ATTR_SIMPLE;
1981 switch (cmd->sc_data_direction) {
1983 c->Request.Type.Direction = XFER_WRITE;
1985 case DMA_FROM_DEVICE:
1986 c->Request.Type.Direction = XFER_READ;
1989 c->Request.Type.Direction = XFER_NONE;
1991 case DMA_BIDIRECTIONAL:
1992 /* This can happen if a buggy application does a scsi passthru
1993 * and sets both inlen and outlen to non-zero. ( see
1994 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1997 c->Request.Type.Direction = XFER_RSVD;
1998 /* This is technically wrong, and hpsa controllers should
1999 * reject it with CMD_INVALID, which is the most correct
2000 * response, but non-fibre backends appear to let it
2001 * slide by, and give the same results as if this field
2002 * were set correctly. Either way is acceptable for
2003 * our purposes here.
2009 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2010 cmd->sc_data_direction);
2015 if (hpsa_scatter_gather(h->pdev, c, cmd) < 0) { /* Fill SG list */
2017 return SCSI_MLQUEUE_HOST_BUSY;
2019 enqueue_cmd_and_start_io(h, c);
2020 /* the cmd'll come back via intr handler in complete_scsi_command() */
2024 static void hpsa_scan_start(struct Scsi_Host *sh)
2026 struct ctlr_info *h = shost_to_hba(sh);
2027 unsigned long flags;
2029 /* wait until any scan already in progress is finished. */
2031 spin_lock_irqsave(&h->scan_lock, flags);
2032 if (h->scan_finished)
2034 spin_unlock_irqrestore(&h->scan_lock, flags);
2035 wait_event(h->scan_wait_queue, h->scan_finished);
2036 /* Note: We don't need to worry about a race between this
2037 * thread and driver unload because the midlayer will
2038 * have incremented the reference count, so unload won't
2039 * happen if we're in here.
2042 h->scan_finished = 0; /* mark scan as in progress */
2043 spin_unlock_irqrestore(&h->scan_lock, flags);
2045 hpsa_update_scsi_devices(h, h->scsi_host->host_no);
2047 spin_lock_irqsave(&h->scan_lock, flags);
2048 h->scan_finished = 1; /* mark scan as finished. */
2049 wake_up_all(&h->scan_wait_queue);
2050 spin_unlock_irqrestore(&h->scan_lock, flags);
2053 static int hpsa_scan_finished(struct Scsi_Host *sh,
2054 unsigned long elapsed_time)
2056 struct ctlr_info *h = shost_to_hba(sh);
2057 unsigned long flags;
2060 spin_lock_irqsave(&h->scan_lock, flags);
2061 finished = h->scan_finished;
2062 spin_unlock_irqrestore(&h->scan_lock, flags);
2066 static void hpsa_unregister_scsi(struct ctlr_info *h)
2068 /* we are being forcibly unloaded, and may not refuse. */
2069 scsi_remove_host(h->scsi_host);
2070 scsi_host_put(h->scsi_host);
2071 h->scsi_host = NULL;
2074 static int hpsa_register_scsi(struct ctlr_info *h)
2078 rc = hpsa_scsi_detect(h);
2080 dev_err(&h->pdev->dev, "hpsa_register_scsi: failed"
2081 " hpsa_scsi_detect(), rc is %d\n", rc);
2085 static int wait_for_device_to_become_ready(struct ctlr_info *h,
2086 unsigned char lunaddr[])
2090 int waittime = 1; /* seconds */
2091 struct CommandList *c;
2093 c = cmd_special_alloc(h);
2095 dev_warn(&h->pdev->dev, "out of memory in "
2096 "wait_for_device_to_become_ready.\n");
2100 /* Send test unit ready until device ready, or give up. */
2101 while (count < HPSA_TUR_RETRY_LIMIT) {
2103 /* Wait for a bit. do this first, because if we send
2104 * the TUR right away, the reset will just abort it.
2106 msleep(1000 * waittime);
2109 /* Increase wait time with each try, up to a point. */
2110 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
2111 waittime = waittime * 2;
2113 /* Send the Test Unit Ready */
2114 fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD);
2115 hpsa_scsi_do_simple_cmd_core(h, c);
2116 /* no unmap needed here because no data xfer. */
2118 if (c->err_info->CommandStatus == CMD_SUCCESS)
2121 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2122 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
2123 (c->err_info->SenseInfo[2] == NO_SENSE ||
2124 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
2127 dev_warn(&h->pdev->dev, "waiting %d secs "
2128 "for device to become ready.\n", waittime);
2129 rc = 1; /* device not ready. */
2133 dev_warn(&h->pdev->dev, "giving up on device.\n");
2135 dev_warn(&h->pdev->dev, "device is ready.\n");
2137 cmd_special_free(h, c);
2141 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
2142 * complaining. Doing a host- or bus-reset can't do anything good here.
2144 static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
2147 struct ctlr_info *h;
2148 struct hpsa_scsi_dev_t *dev;
2150 /* find the controller to which the command to be aborted was sent */
2151 h = sdev_to_hba(scsicmd->device);
2152 if (h == NULL) /* paranoia */
2154 dev = scsicmd->device->hostdata;
2156 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
2157 "device lookup failed.\n");
2160 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
2161 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
2162 /* send a reset to the SCSI LUN which the command was sent to */
2163 rc = hpsa_send_reset(h, dev->scsi3addr);
2164 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
2167 dev_warn(&h->pdev->dev, "resetting device failed.\n");
2172 * For operations that cannot sleep, a command block is allocated at init,
2173 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
2174 * which ones are free or in use. Lock must be held when calling this.
2175 * cmd_free() is the complement.
2177 static struct CommandList *cmd_alloc(struct ctlr_info *h)
2179 struct CommandList *c;
2181 union u64bit temp64;
2182 dma_addr_t cmd_dma_handle, err_dma_handle;
2185 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
2186 if (i == h->nr_cmds)
2188 } while (test_and_set_bit
2189 (i & (BITS_PER_LONG - 1),
2190 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
2191 c = h->cmd_pool + i;
2192 memset(c, 0, sizeof(*c));
2193 cmd_dma_handle = h->cmd_pool_dhandle
2195 c->err_info = h->errinfo_pool + i;
2196 memset(c->err_info, 0, sizeof(*c->err_info));
2197 err_dma_handle = h->errinfo_pool_dhandle
2198 + i * sizeof(*c->err_info);
2203 INIT_HLIST_NODE(&c->list);
2204 c->busaddr = (u32) cmd_dma_handle;
2205 temp64.val = (u64) err_dma_handle;
2206 c->ErrDesc.Addr.lower = temp64.val32.lower;
2207 c->ErrDesc.Addr.upper = temp64.val32.upper;
2208 c->ErrDesc.Len = sizeof(*c->err_info);
2214 /* For operations that can wait for kmalloc to possibly sleep,
2215 * this routine can be called. Lock need not be held to call
2216 * cmd_special_alloc. cmd_special_free() is the complement.
2218 static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
2220 struct CommandList *c;
2221 union u64bit temp64;
2222 dma_addr_t cmd_dma_handle, err_dma_handle;
2224 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
2227 memset(c, 0, sizeof(*c));
2231 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
2234 if (c->err_info == NULL) {
2235 pci_free_consistent(h->pdev,
2236 sizeof(*c), c, cmd_dma_handle);
2239 memset(c->err_info, 0, sizeof(*c->err_info));
2241 INIT_HLIST_NODE(&c->list);
2242 c->busaddr = (u32) cmd_dma_handle;
2243 temp64.val = (u64) err_dma_handle;
2244 c->ErrDesc.Addr.lower = temp64.val32.lower;
2245 c->ErrDesc.Addr.upper = temp64.val32.upper;
2246 c->ErrDesc.Len = sizeof(*c->err_info);
2252 static void cmd_free(struct ctlr_info *h, struct CommandList *c)
2256 i = c - h->cmd_pool;
2257 clear_bit(i & (BITS_PER_LONG - 1),
2258 h->cmd_pool_bits + (i / BITS_PER_LONG));
2262 static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
2264 union u64bit temp64;
2266 temp64.val32.lower = c->ErrDesc.Addr.lower;
2267 temp64.val32.upper = c->ErrDesc.Addr.upper;
2268 pci_free_consistent(h->pdev, sizeof(*c->err_info),
2269 c->err_info, (dma_addr_t) temp64.val);
2270 pci_free_consistent(h->pdev, sizeof(*c),
2271 c, (dma_addr_t) c->busaddr);
2274 #ifdef CONFIG_COMPAT
2276 static int do_ioctl(struct scsi_device *dev, int cmd, void *arg)
2281 ret = hpsa_ioctl(dev, cmd, arg);
2286 static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg);
2287 static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
2288 int cmd, void *arg);
2290 static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
2293 case CCISS_GETPCIINFO:
2294 case CCISS_GETINTINFO:
2295 case CCISS_SETINTINFO:
2296 case CCISS_GETNODENAME:
2297 case CCISS_SETNODENAME:
2298 case CCISS_GETHEARTBEAT:
2299 case CCISS_GETBUSTYPES:
2300 case CCISS_GETFIRMVER:
2301 case CCISS_GETDRIVVER:
2302 case CCISS_REVALIDVOLS:
2303 case CCISS_DEREGDISK:
2304 case CCISS_REGNEWDISK:
2306 case CCISS_RESCANDISK:
2307 case CCISS_GETLUNINFO:
2308 return do_ioctl(dev, cmd, arg);
2310 case CCISS_PASSTHRU32:
2311 return hpsa_ioctl32_passthru(dev, cmd, arg);
2312 case CCISS_BIG_PASSTHRU32:
2313 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
2316 return -ENOIOCTLCMD;
2320 static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
2322 IOCTL32_Command_struct __user *arg32 =
2323 (IOCTL32_Command_struct __user *) arg;
2324 IOCTL_Command_struct arg64;
2325 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
2330 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2331 sizeof(arg64.LUN_info));
2332 err |= copy_from_user(&arg64.Request, &arg32->Request,
2333 sizeof(arg64.Request));
2334 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2335 sizeof(arg64.error_info));
2336 err |= get_user(arg64.buf_size, &arg32->buf_size);
2337 err |= get_user(cp, &arg32->buf);
2338 arg64.buf = compat_ptr(cp);
2339 err |= copy_to_user(p, &arg64, sizeof(arg64));
2344 err = do_ioctl(dev, CCISS_PASSTHRU, (void *)p);
2347 err |= copy_in_user(&arg32->error_info, &p->error_info,
2348 sizeof(arg32->error_info));
2354 static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
2357 BIG_IOCTL32_Command_struct __user *arg32 =
2358 (BIG_IOCTL32_Command_struct __user *) arg;
2359 BIG_IOCTL_Command_struct arg64;
2360 BIG_IOCTL_Command_struct __user *p =
2361 compat_alloc_user_space(sizeof(arg64));
2366 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2367 sizeof(arg64.LUN_info));
2368 err |= copy_from_user(&arg64.Request, &arg32->Request,
2369 sizeof(arg64.Request));
2370 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2371 sizeof(arg64.error_info));
2372 err |= get_user(arg64.buf_size, &arg32->buf_size);
2373 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
2374 err |= get_user(cp, &arg32->buf);
2375 arg64.buf = compat_ptr(cp);
2376 err |= copy_to_user(p, &arg64, sizeof(arg64));
2381 err = do_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
2384 err |= copy_in_user(&arg32->error_info, &p->error_info,
2385 sizeof(arg32->error_info));
2392 static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
2394 struct hpsa_pci_info pciinfo;
2398 pciinfo.domain = pci_domain_nr(h->pdev->bus);
2399 pciinfo.bus = h->pdev->bus->number;
2400 pciinfo.dev_fn = h->pdev->devfn;
2401 pciinfo.board_id = h->board_id;
2402 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
2407 static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
2409 DriverVer_type DriverVer;
2410 unsigned char vmaj, vmin, vsubmin;
2413 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
2414 &vmaj, &vmin, &vsubmin);
2416 dev_info(&h->pdev->dev, "driver version string '%s' "
2417 "unrecognized.", HPSA_DRIVER_VERSION);
2422 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
2425 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
2430 static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2432 IOCTL_Command_struct iocommand;
2433 struct CommandList *c;
2435 union u64bit temp64;
2439 if (!capable(CAP_SYS_RAWIO))
2441 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
2443 if ((iocommand.buf_size < 1) &&
2444 (iocommand.Request.Type.Direction != XFER_NONE)) {
2447 if (iocommand.buf_size > 0) {
2448 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
2452 if (iocommand.Request.Type.Direction == XFER_WRITE) {
2453 /* Copy the data into the buffer we created */
2454 if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) {
2459 memset(buff, 0, iocommand.buf_size);
2460 c = cmd_special_alloc(h);
2465 /* Fill in the command type */
2466 c->cmd_type = CMD_IOCTL_PEND;
2467 /* Fill in Command Header */
2468 c->Header.ReplyQueue = 0; /* unused in simple mode */
2469 if (iocommand.buf_size > 0) { /* buffer to fill */
2470 c->Header.SGList = 1;
2471 c->Header.SGTotal = 1;
2472 } else { /* no buffers to fill */
2473 c->Header.SGList = 0;
2474 c->Header.SGTotal = 0;
2476 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
2477 /* use the kernel address the cmd block for tag */
2478 c->Header.Tag.lower = c->busaddr;
2480 /* Fill in Request block */
2481 memcpy(&c->Request, &iocommand.Request,
2482 sizeof(c->Request));
2484 /* Fill in the scatter gather information */
2485 if (iocommand.buf_size > 0) {
2486 temp64.val = pci_map_single(h->pdev, buff,
2487 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
2488 c->SG[0].Addr.lower = temp64.val32.lower;
2489 c->SG[0].Addr.upper = temp64.val32.upper;
2490 c->SG[0].Len = iocommand.buf_size;
2491 c->SG[0].Ext = 0; /* we are not chaining*/
2493 hpsa_scsi_do_simple_cmd_core(h, c);
2494 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
2495 check_ioctl_unit_attention(h, c);
2497 /* Copy the error information out */
2498 memcpy(&iocommand.error_info, c->err_info,
2499 sizeof(iocommand.error_info));
2500 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
2502 cmd_special_free(h, c);
2506 if (iocommand.Request.Type.Direction == XFER_READ) {
2507 /* Copy the data out of the buffer we created */
2508 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
2510 cmd_special_free(h, c);
2515 cmd_special_free(h, c);
2519 static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2521 BIG_IOCTL_Command_struct *ioc;
2522 struct CommandList *c;
2523 unsigned char **buff = NULL;
2524 int *buff_size = NULL;
2525 union u64bit temp64;
2531 BYTE __user *data_ptr;
2535 if (!capable(CAP_SYS_RAWIO))
2537 ioc = (BIG_IOCTL_Command_struct *)
2538 kmalloc(sizeof(*ioc), GFP_KERNEL);
2543 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
2547 if ((ioc->buf_size < 1) &&
2548 (ioc->Request.Type.Direction != XFER_NONE)) {
2552 /* Check kmalloc limits using all SGs */
2553 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
2557 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
2561 buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
2566 buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL);
2571 left = ioc->buf_size;
2572 data_ptr = ioc->buf;
2574 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
2575 buff_size[sg_used] = sz;
2576 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
2577 if (buff[sg_used] == NULL) {
2581 if (ioc->Request.Type.Direction == XFER_WRITE) {
2582 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
2587 memset(buff[sg_used], 0, sz);
2592 c = cmd_special_alloc(h);
2597 c->cmd_type = CMD_IOCTL_PEND;
2598 c->Header.ReplyQueue = 0;
2600 if (ioc->buf_size > 0) {
2601 c->Header.SGList = sg_used;
2602 c->Header.SGTotal = sg_used;
2604 c->Header.SGList = 0;
2605 c->Header.SGTotal = 0;
2607 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
2608 c->Header.Tag.lower = c->busaddr;
2609 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
2610 if (ioc->buf_size > 0) {
2612 for (i = 0; i < sg_used; i++) {
2613 temp64.val = pci_map_single(h->pdev, buff[i],
2614 buff_size[i], PCI_DMA_BIDIRECTIONAL);
2615 c->SG[i].Addr.lower = temp64.val32.lower;
2616 c->SG[i].Addr.upper = temp64.val32.upper;
2617 c->SG[i].Len = buff_size[i];
2618 /* we are not chaining */
2622 hpsa_scsi_do_simple_cmd_core(h, c);
2623 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
2624 check_ioctl_unit_attention(h, c);
2625 /* Copy the error information out */
2626 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
2627 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
2628 cmd_special_free(h, c);
2632 if (ioc->Request.Type.Direction == XFER_READ) {
2633 /* Copy the data out of the buffer we created */
2634 BYTE __user *ptr = ioc->buf;
2635 for (i = 0; i < sg_used; i++) {
2636 if (copy_to_user(ptr, buff[i], buff_size[i])) {
2637 cmd_special_free(h, c);
2641 ptr += buff_size[i];
2644 cmd_special_free(h, c);
2648 for (i = 0; i < sg_used; i++)
2657 static void check_ioctl_unit_attention(struct ctlr_info *h,
2658 struct CommandList *c)
2660 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2661 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
2662 (void) check_for_unit_attention(h, c);
2667 static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
2669 struct ctlr_info *h;
2670 void __user *argp = (void __user *)arg;
2672 h = sdev_to_hba(dev);
2675 case CCISS_DEREGDISK:
2676 case CCISS_REGNEWDISK:
2678 hpsa_scan_start(h->scsi_host);
2680 case CCISS_GETPCIINFO:
2681 return hpsa_getpciinfo_ioctl(h, argp);
2682 case CCISS_GETDRIVVER:
2683 return hpsa_getdrivver_ioctl(h, argp);
2684 case CCISS_PASSTHRU:
2685 return hpsa_passthru_ioctl(h, argp);
2686 case CCISS_BIG_PASSTHRU:
2687 return hpsa_big_passthru_ioctl(h, argp);
2693 static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
2694 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
2697 int pci_dir = XFER_NONE;
2699 c->cmd_type = CMD_IOCTL_PEND;
2700 c->Header.ReplyQueue = 0;
2701 if (buff != NULL && size > 0) {
2702 c->Header.SGList = 1;
2703 c->Header.SGTotal = 1;
2705 c->Header.SGList = 0;
2706 c->Header.SGTotal = 0;
2708 c->Header.Tag.lower = c->busaddr;
2709 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
2711 c->Request.Type.Type = cmd_type;
2712 if (cmd_type == TYPE_CMD) {
2715 /* are we trying to read a vital product page */
2716 if (page_code != 0) {
2717 c->Request.CDB[1] = 0x01;
2718 c->Request.CDB[2] = page_code;
2720 c->Request.CDBLen = 6;
2721 c->Request.Type.Attribute = ATTR_SIMPLE;
2722 c->Request.Type.Direction = XFER_READ;
2723 c->Request.Timeout = 0;
2724 c->Request.CDB[0] = HPSA_INQUIRY;
2725 c->Request.CDB[4] = size & 0xFF;
2727 case HPSA_REPORT_LOG:
2728 case HPSA_REPORT_PHYS:
2729 /* Talking to controller so It's a physical command
2730 mode = 00 target = 0. Nothing to write.
2732 c->Request.CDBLen = 12;
2733 c->Request.Type.Attribute = ATTR_SIMPLE;
2734 c->Request.Type.Direction = XFER_READ;
2735 c->Request.Timeout = 0;
2736 c->Request.CDB[0] = cmd;
2737 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
2738 c->Request.CDB[7] = (size >> 16) & 0xFF;
2739 c->Request.CDB[8] = (size >> 8) & 0xFF;
2740 c->Request.CDB[9] = size & 0xFF;
2743 case HPSA_READ_CAPACITY:
2744 c->Request.CDBLen = 10;
2745 c->Request.Type.Attribute = ATTR_SIMPLE;
2746 c->Request.Type.Direction = XFER_READ;
2747 c->Request.Timeout = 0;
2748 c->Request.CDB[0] = cmd;
2750 case HPSA_CACHE_FLUSH:
2751 c->Request.CDBLen = 12;
2752 c->Request.Type.Attribute = ATTR_SIMPLE;
2753 c->Request.Type.Direction = XFER_WRITE;
2754 c->Request.Timeout = 0;
2755 c->Request.CDB[0] = BMIC_WRITE;
2756 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
2758 case TEST_UNIT_READY:
2759 c->Request.CDBLen = 6;
2760 c->Request.Type.Attribute = ATTR_SIMPLE;
2761 c->Request.Type.Direction = XFER_NONE;
2762 c->Request.Timeout = 0;
2765 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
2769 } else if (cmd_type == TYPE_MSG) {
2772 case HPSA_DEVICE_RESET_MSG:
2773 c->Request.CDBLen = 16;
2774 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
2775 c->Request.Type.Attribute = ATTR_SIMPLE;
2776 c->Request.Type.Direction = XFER_NONE;
2777 c->Request.Timeout = 0; /* Don't time out */
2778 c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */
2779 c->Request.CDB[1] = 0x03; /* Reset target above */
2780 /* If bytes 4-7 are zero, it means reset the */
2782 c->Request.CDB[4] = 0x00;
2783 c->Request.CDB[5] = 0x00;
2784 c->Request.CDB[6] = 0x00;
2785 c->Request.CDB[7] = 0x00;
2789 dev_warn(&h->pdev->dev, "unknown message type %d\n",
2794 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
2798 switch (c->Request.Type.Direction) {
2800 pci_dir = PCI_DMA_FROMDEVICE;
2803 pci_dir = PCI_DMA_TODEVICE;
2806 pci_dir = PCI_DMA_NONE;
2809 pci_dir = PCI_DMA_BIDIRECTIONAL;
2812 hpsa_map_one(h->pdev, c, buff, size, pci_dir);
2818 * Map (physical) PCI mem into (virtual) kernel space
2820 static void __iomem *remap_pci_mem(ulong base, ulong size)
2822 ulong page_base = ((ulong) base) & PAGE_MASK;
2823 ulong page_offs = ((ulong) base) - page_base;
2824 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
2826 return page_remapped ? (page_remapped + page_offs) : NULL;
2829 /* Takes cmds off the submission queue and sends them to the hardware,
2830 * then puts them on the queue of cmds waiting for completion.
2832 static void start_io(struct ctlr_info *h)
2834 struct CommandList *c;
2836 while (!hlist_empty(&h->reqQ)) {
2837 c = hlist_entry(h->reqQ.first, struct CommandList, list);
2838 /* can't do anything if fifo is full */
2839 if ((h->access.fifo_full(h))) {
2840 dev_warn(&h->pdev->dev, "fifo full\n");
2844 /* Get the first entry from the Request Q */
2848 /* Tell the controller execute command */
2849 h->access.submit_command(h, c);
2851 /* Put job onto the completed Q */
2856 static inline unsigned long get_next_completion(struct ctlr_info *h)
2858 return h->access.command_completed(h);
2861 static inline bool interrupt_pending(struct ctlr_info *h)
2863 return h->access.intr_pending(h);
2866 static inline long interrupt_not_for_us(struct ctlr_info *h)
2868 return !(h->msi_vector || h->msix_vector) &&
2869 ((h->access.intr_pending(h) == 0) ||
2870 (h->interrupts_enabled == 0));
2873 static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
2876 if (unlikely(tag_index >= h->nr_cmds)) {
2877 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
2883 static inline void finish_cmd(struct CommandList *c, u32 raw_tag)
2886 if (likely(c->cmd_type == CMD_SCSI))
2887 complete_scsi_command(c, 0, raw_tag);
2888 else if (c->cmd_type == CMD_IOCTL_PEND)
2889 complete(c->waiting);
2892 static inline u32 hpsa_tag_contains_index(u32 tag)
2894 #define DIRECT_LOOKUP_BIT 0x10
2895 return tag & DIRECT_LOOKUP_BIT;
2898 static inline u32 hpsa_tag_to_index(u32 tag)
2900 #define DIRECT_LOOKUP_SHIFT 5
2901 return tag >> DIRECT_LOOKUP_SHIFT;
2904 static inline u32 hpsa_tag_discard_error_bits(u32 tag)
2906 #define HPSA_ERROR_BITS 0x03
2907 return tag & ~HPSA_ERROR_BITS;
2910 /* process completion of an indexed ("direct lookup") command */
2911 static inline u32 process_indexed_cmd(struct ctlr_info *h,
2915 struct CommandList *c;
2917 tag_index = hpsa_tag_to_index(raw_tag);
2918 if (bad_tag(h, tag_index, raw_tag))
2919 return next_command(h);
2920 c = h->cmd_pool + tag_index;
2921 finish_cmd(c, raw_tag);
2922 return next_command(h);
2925 /* process completion of a non-indexed command */
2926 static inline u32 process_nonindexed_cmd(struct ctlr_info *h,
2930 struct CommandList *c = NULL;
2931 struct hlist_node *tmp;
2933 tag = hpsa_tag_discard_error_bits(raw_tag);
2934 hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
2935 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
2936 finish_cmd(c, raw_tag);
2937 return next_command(h);
2940 bad_tag(h, h->nr_cmds + 1, raw_tag);
2941 return next_command(h);
2944 static irqreturn_t do_hpsa_intr(int irq, void *dev_id)
2946 struct ctlr_info *h = dev_id;
2947 unsigned long flags;
2950 if (interrupt_not_for_us(h))
2952 spin_lock_irqsave(&h->lock, flags);
2953 raw_tag = get_next_completion(h);
2954 while (raw_tag != FIFO_EMPTY) {
2955 if (hpsa_tag_contains_index(raw_tag))
2956 raw_tag = process_indexed_cmd(h, raw_tag);
2958 raw_tag = process_nonindexed_cmd(h, raw_tag);
2960 spin_unlock_irqrestore(&h->lock, flags);
2964 /* Send a message CDB to the firmwart. */
2965 static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
2969 struct CommandListHeader CommandHeader;
2970 struct RequestBlock Request;
2971 struct ErrDescriptor ErrorDescriptor;
2973 struct Command *cmd;
2974 static const size_t cmd_sz = sizeof(*cmd) +
2975 sizeof(cmd->ErrorDescriptor);
2977 uint32_t paddr32, tag;
2978 void __iomem *vaddr;
2981 vaddr = pci_ioremap_bar(pdev, 0);
2985 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
2986 * CCISS commands, so they must be allocated from the lower 4GiB of
2989 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2995 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
3001 /* This must fit, because of the 32-bit consistent DMA mask. Also,
3002 * although there's no guarantee, we assume that the address is at
3003 * least 4-byte aligned (most likely, it's page-aligned).
3007 cmd->CommandHeader.ReplyQueue = 0;
3008 cmd->CommandHeader.SGList = 0;
3009 cmd->CommandHeader.SGTotal = 0;
3010 cmd->CommandHeader.Tag.lower = paddr32;
3011 cmd->CommandHeader.Tag.upper = 0;
3012 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
3014 cmd->Request.CDBLen = 16;
3015 cmd->Request.Type.Type = TYPE_MSG;
3016 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
3017 cmd->Request.Type.Direction = XFER_NONE;
3018 cmd->Request.Timeout = 0; /* Don't time out */
3019 cmd->Request.CDB[0] = opcode;
3020 cmd->Request.CDB[1] = type;
3021 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
3022 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
3023 cmd->ErrorDescriptor.Addr.upper = 0;
3024 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
3026 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
3028 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
3029 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
3030 if (hpsa_tag_discard_error_bits(tag) == paddr32)
3032 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
3037 /* we leak the DMA buffer here ... no choice since the controller could
3038 * still complete the command.
3040 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
3041 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
3046 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
3048 if (tag & HPSA_ERROR_BIT) {
3049 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
3054 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
3059 #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
3060 #define hpsa_noop(p) hpsa_message(p, 3, 0)
3062 static __devinit int hpsa_reset_msi(struct pci_dev *pdev)
3064 /* the #defines are stolen from drivers/pci/msi.h. */
3065 #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
3066 #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
3071 pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
3073 pci_read_config_word(pdev, msi_control_reg(pos), &control);
3074 if (control & PCI_MSI_FLAGS_ENABLE) {
3075 dev_info(&pdev->dev, "resetting MSI\n");
3076 pci_write_config_word(pdev, msi_control_reg(pos),
3077 control & ~PCI_MSI_FLAGS_ENABLE);
3081 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3083 pci_read_config_word(pdev, msi_control_reg(pos), &control);
3084 if (control & PCI_MSIX_FLAGS_ENABLE) {
3085 dev_info(&pdev->dev, "resetting MSI-X\n");
3086 pci_write_config_word(pdev, msi_control_reg(pos),
3087 control & ~PCI_MSIX_FLAGS_ENABLE);
3094 /* This does a hard reset of the controller using PCI power management
3097 static __devinit int hpsa_hard_reset_controller(struct pci_dev *pdev)
3099 u16 pmcsr, saved_config_space[32];
3102 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
3104 /* This is very nearly the same thing as
3106 * pci_save_state(pci_dev);
3107 * pci_set_power_state(pci_dev, PCI_D3hot);
3108 * pci_set_power_state(pci_dev, PCI_D0);
3109 * pci_restore_state(pci_dev);
3111 * but we can't use these nice canned kernel routines on
3112 * kexec, because they also check the MSI/MSI-X state in PCI
3113 * configuration space and do the wrong thing when it is
3114 * set/cleared. Also, the pci_save/restore_state functions
3115 * violate the ordering requirements for restoring the
3116 * configuration space from the CCISS document (see the
3117 * comment below). So we roll our own ....
3120 for (i = 0; i < 32; i++)
3121 pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
3123 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
3126 "hpsa_reset_controller: PCI PM not supported\n");
3130 /* Quoting from the Open CISS Specification: "The Power
3131 * Management Control/Status Register (CSR) controls the power
3132 * state of the device. The normal operating state is D0,
3133 * CSR=00h. The software off state is D3, CSR=03h. To reset
3134 * the controller, place the interface device in D3 then to
3135 * D0, this causes a secondary PCI reset which will reset the
3139 /* enter the D3hot power management state */
3140 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
3141 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3143 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3147 /* enter the D0 power management state */
3148 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3150 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3154 /* Restore the PCI configuration space. The Open CISS
3155 * Specification says, "Restore the PCI Configuration
3156 * Registers, offsets 00h through 60h. It is important to
3157 * restore the command register, 16-bits at offset 04h,
3158 * last. Do not restore the configuration status register,
3159 * 16-bits at offset 06h." Note that the offset is 2*i.
3161 for (i = 0; i < 32; i++) {
3162 if (i == 2 || i == 3)
3164 pci_write_config_word(pdev, 2*i, saved_config_space[i]);
3167 pci_write_config_word(pdev, 4, saved_config_space[2]);
3173 * We cannot read the structure directly, for portability we must use
3175 * This is for debug only.
3178 static void print_cfg_table(struct device *dev, struct CfgTable *tb)
3183 dev_info(dev, "Controller Configuration information\n");
3184 dev_info(dev, "------------------------------------\n");
3185 for (i = 0; i < 4; i++)
3186 temp_name[i] = readb(&(tb->Signature[i]));
3187 temp_name[4] = '\0';
3188 dev_info(dev, " Signature = %s\n", temp_name);
3189 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
3190 dev_info(dev, " Transport methods supported = 0x%x\n",
3191 readl(&(tb->TransportSupport)));
3192 dev_info(dev, " Transport methods active = 0x%x\n",
3193 readl(&(tb->TransportActive)));
3194 dev_info(dev, " Requested transport Method = 0x%x\n",
3195 readl(&(tb->HostWrite.TransportRequest)));
3196 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
3197 readl(&(tb->HostWrite.CoalIntDelay)));
3198 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
3199 readl(&(tb->HostWrite.CoalIntCount)));
3200 dev_info(dev, " Max outstanding commands = 0x%d\n",
3201 readl(&(tb->CmdsOutMax)));
3202 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
3203 for (i = 0; i < 16; i++)
3204 temp_name[i] = readb(&(tb->ServerName[i]));
3205 temp_name[16] = '\0';
3206 dev_info(dev, " Server Name = %s\n", temp_name);
3207 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
3208 readl(&(tb->HeartBeat)));
3210 #endif /* HPSA_DEBUG */
3212 static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
3214 int i, offset, mem_type, bar_type;
3216 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
3219 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3220 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
3221 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
3224 mem_type = pci_resource_flags(pdev, i) &
3225 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
3227 case PCI_BASE_ADDRESS_MEM_TYPE_32:
3228 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
3229 offset += 4; /* 32 bit */
3231 case PCI_BASE_ADDRESS_MEM_TYPE_64:
3234 default: /* reserved in PCI 2.2 */
3235 dev_warn(&pdev->dev,
3236 "base address is invalid\n");
3241 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
3247 /* If MSI/MSI-X is supported by the kernel we will try to enable it on
3248 * controllers that are capable. If not, we use IO-APIC mode.
3251 static void __devinit hpsa_interrupt_mode(struct ctlr_info *h,
3252 struct pci_dev *pdev, u32 board_id)
3254 #ifdef CONFIG_PCI_MSI
3256 struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1},
3260 /* Some boards advertise MSI but don't really support it */
3261 if ((board_id == 0x40700E11) ||
3262 (board_id == 0x40800E11) ||
3263 (board_id == 0x40820E11) || (board_id == 0x40830E11))
3264 goto default_int_mode;
3265 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
3266 dev_info(&pdev->dev, "MSIX\n");
3267 err = pci_enable_msix(pdev, hpsa_msix_entries, 4);
3269 h->intr[0] = hpsa_msix_entries[0].vector;
3270 h->intr[1] = hpsa_msix_entries[1].vector;
3271 h->intr[2] = hpsa_msix_entries[2].vector;
3272 h->intr[3] = hpsa_msix_entries[3].vector;
3277 dev_warn(&pdev->dev, "only %d MSI-X vectors "
3278 "available\n", err);
3279 goto default_int_mode;
3281 dev_warn(&pdev->dev, "MSI-X init failed %d\n",
3283 goto default_int_mode;
3286 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
3287 dev_info(&pdev->dev, "MSI\n");
3288 if (!pci_enable_msi(pdev))
3291 dev_warn(&pdev->dev, "MSI init failed\n");
3294 #endif /* CONFIG_PCI_MSI */
3295 /* if we get here we're going to use the default interrupt mode */
3296 h->intr[PERF_MODE_INT] = pdev->irq;
3299 static int hpsa_pci_init(struct ctlr_info *h, struct pci_dev *pdev)
3301 ushort subsystem_vendor_id, subsystem_device_id, command;
3302 u32 board_id, scratchpad = 0;
3305 u64 cfg_base_addr_index;
3307 int i, prod_index, err;
3309 subsystem_vendor_id = pdev->subsystem_vendor;
3310 subsystem_device_id = pdev->subsystem_device;
3311 board_id = (((u32) (subsystem_device_id << 16) & 0xffff0000) |
3312 subsystem_vendor_id);
3314 for (i = 0; i < ARRAY_SIZE(products); i++)
3315 if (board_id == products[i].board_id)
3320 if (prod_index == ARRAY_SIZE(products)) {
3322 if (subsystem_vendor_id != PCI_VENDOR_ID_HP ||
3324 dev_warn(&pdev->dev, "unrecognized board ID:"
3325 " 0x%08lx, ignoring.\n",
3326 (unsigned long) board_id);
3330 /* check to see if controller has been disabled
3331 * BEFORE trying to enable it
3333 (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
3334 if (!(command & 0x02)) {
3335 dev_warn(&pdev->dev, "controller appears to be disabled\n");
3339 err = pci_enable_device(pdev);
3341 dev_warn(&pdev->dev, "unable to enable PCI device\n");
3345 err = pci_request_regions(pdev, "hpsa");
3347 dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
3351 /* If the kernel supports MSI/MSI-X we will try to enable that,
3352 * else we use the IO-APIC interrupt assigned to us by system ROM.
3354 hpsa_interrupt_mode(h, pdev, board_id);
3356 /* find the memory BAR */
3357 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3358 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
3361 if (i == DEVICE_COUNT_RESOURCE) {
3362 dev_warn(&pdev->dev, "no memory BAR found\n");
3364 goto err_out_free_res;
3367 h->paddr = pci_resource_start(pdev, i); /* addressing mode bits
3371 h->vaddr = remap_pci_mem(h->paddr, 0x250);
3373 /* Wait for the board to become ready. */
3374 for (i = 0; i < HPSA_BOARD_READY_ITERATIONS; i++) {
3375 scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
3376 if (scratchpad == HPSA_FIRMWARE_READY)
3378 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
3380 if (scratchpad != HPSA_FIRMWARE_READY) {
3381 dev_warn(&pdev->dev, "board not ready, timed out.\n");
3383 goto err_out_free_res;
3386 /* get the address index number */
3387 cfg_base_addr = readl(h->vaddr + SA5_CTCFG_OFFSET);
3388 cfg_base_addr &= (u32) 0x0000ffff;
3389 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
3390 if (cfg_base_addr_index == -1) {
3391 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
3393 goto err_out_free_res;
3396 cfg_offset = readl(h->vaddr + SA5_CTMEM_OFFSET);
3397 h->cfgtable = remap_pci_mem(pci_resource_start(pdev,
3398 cfg_base_addr_index) + cfg_offset,
3399 sizeof(h->cfgtable));
3400 /* Find performant mode table. */
3401 trans_offset = readl(&(h->cfgtable->TransMethodOffset));
3402 h->transtable = remap_pci_mem(pci_resource_start(pdev,
3403 cfg_base_addr_index)+cfg_offset+trans_offset,
3404 sizeof(*h->transtable));
3406 h->board_id = board_id;
3407 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
3408 h->product_name = products[prod_index].product_name;
3409 h->access = *(products[prod_index].access);
3410 /* Allow room for some ioctls */
3411 h->nr_cmds = h->max_commands - 4;
3413 if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
3414 (readb(&h->cfgtable->Signature[1]) != 'I') ||
3415 (readb(&h->cfgtable->Signature[2]) != 'S') ||
3416 (readb(&h->cfgtable->Signature[3]) != 'S')) {
3417 dev_warn(&pdev->dev, "not a valid CISS config table\n");
3419 goto err_out_free_res;
3423 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3425 prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
3427 writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
3431 /* Disabling DMA prefetch for the P600
3432 * An ASIC bug may result in a prefetch beyond
3435 if (board_id == 0x3225103C) {
3437 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
3438 dma_prefetch |= 0x8000;
3439 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
3442 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
3443 /* Update the field, and then ring the doorbell */
3444 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
3445 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
3447 /* under certain very rare conditions, this can take awhile.
3448 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3449 * as we enter this code.)
3451 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
3452 if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3454 /* delay and try again */
3459 print_cfg_table(&pdev->dev, h->cfgtable);
3460 #endif /* HPSA_DEBUG */
3462 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
3463 dev_warn(&pdev->dev, "unable to get board into simple mode\n");
3465 goto err_out_free_res;
3471 * Deliberately omit pci_disable_device(): it does something nasty to
3472 * Smart Array controllers that pci_enable_device does not undo
3474 pci_release_regions(pdev);
3478 static void __devinit hpsa_hba_inquiry(struct ctlr_info *h)
3482 #define HBA_INQUIRY_BYTE_COUNT 64
3483 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
3484 if (!h->hba_inquiry_data)
3486 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
3487 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
3489 kfree(h->hba_inquiry_data);
3490 h->hba_inquiry_data = NULL;
3494 static int __devinit hpsa_init_one(struct pci_dev *pdev,
3495 const struct pci_device_id *ent)
3499 struct ctlr_info *h;
3501 if (number_of_controllers == 0)
3502 printk(KERN_INFO DRIVER_NAME "\n");
3503 if (reset_devices) {
3504 /* Reset the controller with a PCI power-cycle */
3505 if (hpsa_hard_reset_controller(pdev) || hpsa_reset_msi(pdev))
3508 /* Some devices (notably the HP Smart Array 5i Controller)
3509 need a little pause here */
3510 msleep(HPSA_POST_RESET_PAUSE_MSECS);
3512 /* Now try to get the controller to respond to a no-op */
3513 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
3514 if (hpsa_noop(pdev) == 0)
3517 dev_warn(&pdev->dev, "no-op failed%s\n",
3518 (i < 11 ? "; re-trying" : ""));
3522 /* Command structures must be aligned on a 32-byte boundary because
3523 * the 5 lower bits of the address are used by the hardware. and by
3524 * the driver. See comments in hpsa.h for more info.
3526 #define COMMANDLIST_ALIGNMENT 32
3527 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
3528 h = kzalloc(sizeof(*h), GFP_KERNEL);
3532 h->busy_initializing = 1;
3533 INIT_HLIST_HEAD(&h->cmpQ);
3534 INIT_HLIST_HEAD(&h->reqQ);
3535 mutex_init(&h->busy_shutting_down);
3536 init_completion(&h->scan_wait);
3537 rc = hpsa_pci_init(h, pdev);
3541 sprintf(h->devname, "hpsa%d", number_of_controllers);
3542 h->ctlr = number_of_controllers;
3543 number_of_controllers++;
3546 /* configure PCI DMA stuff */
3547 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3551 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3555 dev_err(&pdev->dev, "no suitable DMA available\n");
3560 /* make sure the board interrupts are off */
3561 h->access.set_intr_mask(h, HPSA_INTR_OFF);
3562 rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr,
3563 IRQF_DISABLED, h->devname, h);
3565 dev_err(&pdev->dev, "unable to get irq %d for %s\n",
3566 h->intr[PERF_MODE_INT], h->devname);
3570 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
3571 h->devname, pdev->device,
3572 h->intr[PERF_MODE_INT], dac ? "" : " not");
3575 kmalloc(((h->nr_cmds + BITS_PER_LONG -
3576 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
3577 h->cmd_pool = pci_alloc_consistent(h->pdev,
3578 h->nr_cmds * sizeof(*h->cmd_pool),
3579 &(h->cmd_pool_dhandle));
3580 h->errinfo_pool = pci_alloc_consistent(h->pdev,
3581 h->nr_cmds * sizeof(*h->errinfo_pool),
3582 &(h->errinfo_pool_dhandle));
3583 if ((h->cmd_pool_bits == NULL)
3584 || (h->cmd_pool == NULL)
3585 || (h->errinfo_pool == NULL)) {
3586 dev_err(&pdev->dev, "out of memory");
3590 spin_lock_init(&h->lock);
3591 spin_lock_init(&h->scan_lock);
3592 init_waitqueue_head(&h->scan_wait_queue);
3593 h->scan_finished = 1; /* no scan currently in progress */
3595 pci_set_drvdata(pdev, h);
3596 memset(h->cmd_pool_bits, 0,
3597 ((h->nr_cmds + BITS_PER_LONG -
3598 1) / BITS_PER_LONG) * sizeof(unsigned long));
3602 /* Turn the interrupts on so we can service requests */
3603 h->access.set_intr_mask(h, HPSA_INTR_ON);
3605 hpsa_put_ctlr_into_performant_mode(h);
3606 hpsa_hba_inquiry(h);
3607 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
3608 h->busy_initializing = 0;
3612 kfree(h->cmd_pool_bits);
3614 pci_free_consistent(h->pdev,
3615 h->nr_cmds * sizeof(struct CommandList),
3616 h->cmd_pool, h->cmd_pool_dhandle);
3617 if (h->errinfo_pool)
3618 pci_free_consistent(h->pdev,
3619 h->nr_cmds * sizeof(struct ErrorInfo),
3621 h->errinfo_pool_dhandle);
3622 free_irq(h->intr[PERF_MODE_INT], h);
3625 h->busy_initializing = 0;
3630 static void hpsa_flush_cache(struct ctlr_info *h)
3633 struct CommandList *c;
3635 flush_buf = kzalloc(4, GFP_KERNEL);
3639 c = cmd_special_alloc(h);
3641 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
3644 fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
3645 RAID_CTLR_LUNID, TYPE_CMD);
3646 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
3647 if (c->err_info->CommandStatus != 0)
3648 dev_warn(&h->pdev->dev,
3649 "error flushing cache on controller\n");
3650 cmd_special_free(h, c);
3655 static void hpsa_shutdown(struct pci_dev *pdev)
3657 struct ctlr_info *h;
3659 h = pci_get_drvdata(pdev);
3660 /* Turn board interrupts off and send the flush cache command
3661 * sendcmd will turn off interrupt, and send the flush...
3662 * To write all data in the battery backed cache to disks
3664 hpsa_flush_cache(h);
3665 h->access.set_intr_mask(h, HPSA_INTR_OFF);
3666 free_irq(h->intr[PERF_MODE_INT], h);
3667 #ifdef CONFIG_PCI_MSI
3669 pci_disable_msix(h->pdev);
3670 else if (h->msi_vector)
3671 pci_disable_msi(h->pdev);
3672 #endif /* CONFIG_PCI_MSI */
3675 static void __devexit hpsa_remove_one(struct pci_dev *pdev)
3677 struct ctlr_info *h;
3679 if (pci_get_drvdata(pdev) == NULL) {
3680 dev_err(&pdev->dev, "unable to remove device \n");
3683 h = pci_get_drvdata(pdev);
3684 mutex_lock(&h->busy_shutting_down);
3685 remove_from_scan_list(h);
3686 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
3687 hpsa_shutdown(pdev);
3689 pci_free_consistent(h->pdev,
3690 h->nr_cmds * sizeof(struct CommandList),
3691 h->cmd_pool, h->cmd_pool_dhandle);
3692 pci_free_consistent(h->pdev,
3693 h->nr_cmds * sizeof(struct ErrorInfo),
3694 h->errinfo_pool, h->errinfo_pool_dhandle);
3695 pci_free_consistent(h->pdev, h->reply_pool_size,
3696 h->reply_pool, h->reply_pool_dhandle);
3697 kfree(h->cmd_pool_bits);
3698 kfree(h->blockFetchTable);
3699 kfree(h->hba_inquiry_data);
3701 * Deliberately omit pci_disable_device(): it does something nasty to
3702 * Smart Array controllers that pci_enable_device does not undo
3704 pci_release_regions(pdev);
3705 pci_set_drvdata(pdev, NULL);
3706 mutex_unlock(&h->busy_shutting_down);
3710 static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
3711 __attribute__((unused)) pm_message_t state)
3716 static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
3721 static struct pci_driver hpsa_pci_driver = {
3723 .probe = hpsa_init_one,
3724 .remove = __devexit_p(hpsa_remove_one),
3725 .id_table = hpsa_pci_device_id, /* id_table */
3726 .shutdown = hpsa_shutdown,
3727 .suspend = hpsa_suspend,
3728 .resume = hpsa_resume,
3731 /* Fill in bucket_map[], given nsgs (the max number of
3732 * scatter gather elements supported) and bucket[],
3733 * which is an array of 8 integers. The bucket[] array
3734 * contains 8 different DMA transfer sizes (in 16
3735 * byte increments) which the controller uses to fetch
3736 * commands. This function fills in bucket_map[], which
3737 * maps a given number of scatter gather elements to one of
3738 * the 8 DMA transfer sizes. The point of it is to allow the
3739 * controller to only do as much DMA as needed to fetch the
3740 * command, with the DMA transfer size encoded in the lower
3741 * bits of the command address.
3743 static void calc_bucket_map(int bucket[], int num_buckets,
3744 int nsgs, int *bucket_map)
3748 /* even a command with 0 SGs requires 4 blocks */
3749 #define MINIMUM_TRANSFER_BLOCKS 4
3750 #define NUM_BUCKETS 8
3751 /* Note, bucket_map must have nsgs+1 entries. */
3752 for (i = 0; i <= nsgs; i++) {
3753 /* Compute size of a command with i SG entries */
3754 size = i + MINIMUM_TRANSFER_BLOCKS;
3755 b = num_buckets; /* Assume the biggest bucket */
3756 /* Find the bucket that is just big enough */
3757 for (j = 0; j < 8; j++) {
3758 if (bucket[j] >= size) {
3763 /* for a command with i SG entries, use bucket b. */
3768 static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
3772 /* 5 = 1 s/g entry or 4k
3773 * 6 = 2 s/g entry or 8k
3774 * 8 = 4 s/g entry or 16k
3775 * 10 = 6 s/g entry or 24k
3777 int bft[8] = {5, 6, 8, 10, 12, 20, 28, 35}; /* for scatter/gathers */
3780 unsigned long register_value;
3782 trans_support = readl(&(h->cfgtable->TransportSupport));
3783 if (!(trans_support & PERFORMANT_MODE))
3786 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
3787 h->max_sg_entries = 32;
3788 /* Performant mode ring buffer and supporting data structures */
3789 h->reply_pool_size = h->max_commands * sizeof(u64);
3790 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
3791 &(h->reply_pool_dhandle));
3793 /* Need a block fetch table for performant mode */
3794 h->blockFetchTable = kmalloc(((h->max_sg_entries+1) *
3795 sizeof(u32)), GFP_KERNEL);
3797 if ((h->reply_pool == NULL)
3798 || (h->blockFetchTable == NULL))
3801 h->reply_pool_wraparound = 1; /* spec: init to 1 */
3803 /* Controller spec: zero out this buffer. */
3804 memset(h->reply_pool, 0, h->reply_pool_size);
3805 h->reply_pool_head = h->reply_pool;
3807 trans_offset = readl(&(h->cfgtable->TransMethodOffset));
3808 bft[7] = h->max_sg_entries + 4;
3809 calc_bucket_map(bft, ARRAY_SIZE(bft), 32, h->blockFetchTable);
3810 for (i = 0; i < 8; i++)
3811 writel(bft[i], &h->transtable->BlockFetch[i]);
3813 /* size of controller ring buffer */
3814 writel(h->max_commands, &h->transtable->RepQSize);
3815 writel(1, &h->transtable->RepQCount);
3816 writel(0, &h->transtable->RepQCtrAddrLow32);
3817 writel(0, &h->transtable->RepQCtrAddrHigh32);
3818 writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32);
3819 writel(0, &h->transtable->RepQAddr0High32);
3820 writel(CFGTBL_Trans_Performant,
3821 &(h->cfgtable->HostWrite.TransportRequest));
3822 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
3823 /* under certain very rare conditions, this can take awhile.
3824 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3825 * as we enter this code.) */
3826 for (l = 0; l < MAX_CONFIG_WAIT; l++) {
3827 register_value = readl(h->vaddr + SA5_DOORBELL);
3828 if (!(register_value & CFGTBL_ChangeReq))
3830 /* delay and try again */
3831 set_current_state(TASK_INTERRUPTIBLE);
3832 schedule_timeout(10);
3834 register_value = readl(&(h->cfgtable->TransportActive));
3835 if (!(register_value & CFGTBL_Trans_Performant)) {
3836 dev_warn(&h->pdev->dev, "unable to get board into"
3837 " performant mode\n");
3841 /* Change the access methods to the performant access methods */
3842 h->access = SA5_performant_access;
3843 h->transMethod = CFGTBL_Trans_Performant;
3849 pci_free_consistent(h->pdev, h->reply_pool_size,
3850 h->reply_pool, h->reply_pool_dhandle);
3851 kfree(h->blockFetchTable);
3855 * This is it. Register the PCI driver information for the cards we control
3856 * the OS will call our registered routines when it finds one of our cards.
3858 static int __init hpsa_init(void)
3861 /* Start the scan thread */
3862 hpsa_scan_thread = kthread_run(hpsa_scan_func, NULL, "hpsa_scan");
3863 if (IS_ERR(hpsa_scan_thread)) {
3864 err = PTR_ERR(hpsa_scan_thread);
3867 err = pci_register_driver(&hpsa_pci_driver);
3869 kthread_stop(hpsa_scan_thread);
3873 static void __exit hpsa_cleanup(void)
3875 pci_unregister_driver(&hpsa_pci_driver);
3876 kthread_stop(hpsa_scan_thread);
3879 module_init(hpsa_init);
3880 module_exit(hpsa_cleanup);