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 <scsi/scsi_tcq.h>
47 #include <linux/cciss_ioctl.h>
48 #include <linux/string.h>
49 #include <linux/bitmap.h>
50 #include <asm/atomic.h>
51 #include <linux/kthread.h>
55 /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
56 #define HPSA_DRIVER_VERSION "2.0.2-1"
57 #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
59 /* How long to wait (in milliseconds) for board to go into simple mode */
60 #define MAX_CONFIG_WAIT 30000
61 #define MAX_IOCTL_CONFIG_WAIT 1000
63 /*define how many times we will try a command because of bus resets */
64 #define MAX_CMD_RETRIES 3
66 /* Embedded module documentation macros - see modules.h */
67 MODULE_AUTHOR("Hewlett-Packard Company");
68 MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
70 MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
71 MODULE_VERSION(HPSA_DRIVER_VERSION);
72 MODULE_LICENSE("GPL");
74 static int hpsa_allow_any;
75 module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
76 MODULE_PARM_DESC(hpsa_allow_any,
77 "Allow hpsa driver to access unknown HP Smart Array hardware");
79 /* define the PCI info for the cards we can control */
80 static const struct pci_device_id hpsa_pci_device_id[] = {
81 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
82 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
83 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
84 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
85 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
86 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a},
87 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b},
88 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
89 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3250},
90 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3251},
91 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3252},
92 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3253},
93 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3254},
94 #define PCI_DEVICE_ID_HP_CISSF 0x333f
95 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x333F},
96 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
97 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
98 {PCI_VENDOR_ID_COMPAQ, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
99 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
103 MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
105 /* board_id = Subsystem Device ID & Vendor ID
106 * product = Marketing Name for the board
107 * access = Address of the struct of function pointers
109 static struct board_type products[] = {
110 {0x3241103C, "Smart Array P212", &SA5_access},
111 {0x3243103C, "Smart Array P410", &SA5_access},
112 {0x3245103C, "Smart Array P410i", &SA5_access},
113 {0x3247103C, "Smart Array P411", &SA5_access},
114 {0x3249103C, "Smart Array P812", &SA5_access},
115 {0x324a103C, "Smart Array P712m", &SA5_access},
116 {0x324b103C, "Smart Array P711m", &SA5_access},
117 {0x3233103C, "StorageWorks P1210m", &SA5_access},
118 {0x333F103C, "StorageWorks P1210m", &SA5_access},
119 {0x3250103C, "Smart Array", &SA5_access},
120 {0x3250113C, "Smart Array", &SA5_access},
121 {0x3250123C, "Smart Array", &SA5_access},
122 {0x3250133C, "Smart Array", &SA5_access},
123 {0x3250143C, "Smart Array", &SA5_access},
124 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
127 static int number_of_controllers;
129 static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
130 static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
131 static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
132 static void start_io(struct ctlr_info *h);
135 static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
138 static void cmd_free(struct ctlr_info *h, struct CommandList *c);
139 static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
140 static struct CommandList *cmd_alloc(struct ctlr_info *h);
141 static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
142 static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
143 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
146 static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
147 static void hpsa_scan_start(struct Scsi_Host *);
148 static int hpsa_scan_finished(struct Scsi_Host *sh,
149 unsigned long elapsed_time);
150 static int hpsa_change_queue_depth(struct scsi_device *sdev,
151 int qdepth, int reason);
153 static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
154 static int hpsa_slave_alloc(struct scsi_device *sdev);
155 static void hpsa_slave_destroy(struct scsi_device *sdev);
157 static ssize_t raid_level_show(struct device *dev,
158 struct device_attribute *attr, char *buf);
159 static ssize_t lunid_show(struct device *dev,
160 struct device_attribute *attr, char *buf);
161 static ssize_t unique_id_show(struct device *dev,
162 struct device_attribute *attr, char *buf);
163 static ssize_t host_show_firmware_revision(struct device *dev,
164 struct device_attribute *attr, char *buf);
165 static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
166 static ssize_t host_store_rescan(struct device *dev,
167 struct device_attribute *attr, const char *buf, size_t count);
168 static int check_for_unit_attention(struct ctlr_info *h,
169 struct CommandList *c);
170 static void check_ioctl_unit_attention(struct ctlr_info *h,
171 struct CommandList *c);
172 /* performant mode helper functions */
173 static void calc_bucket_map(int *bucket, int num_buckets,
174 int nsgs, int *bucket_map);
175 static __devinit void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
176 static inline u32 next_command(struct ctlr_info *h);
177 static int __devinit hpsa_find_cfg_addrs(struct pci_dev *pdev,
178 void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index,
180 static int __devinit hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
181 unsigned long *memory_bar);
182 static int __devinit hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
184 static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
185 static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
186 static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
187 static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
188 static DEVICE_ATTR(firmware_revision, S_IRUGO,
189 host_show_firmware_revision, NULL);
191 static struct device_attribute *hpsa_sdev_attrs[] = {
192 &dev_attr_raid_level,
198 static struct device_attribute *hpsa_shost_attrs[] = {
200 &dev_attr_firmware_revision,
204 static struct scsi_host_template hpsa_driver_template = {
205 .module = THIS_MODULE,
208 .queuecommand = hpsa_scsi_queue_command,
209 .scan_start = hpsa_scan_start,
210 .scan_finished = hpsa_scan_finished,
211 .change_queue_depth = hpsa_change_queue_depth,
213 .use_clustering = ENABLE_CLUSTERING,
214 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
216 .slave_alloc = hpsa_slave_alloc,
217 .slave_destroy = hpsa_slave_destroy,
219 .compat_ioctl = hpsa_compat_ioctl,
221 .sdev_attrs = hpsa_sdev_attrs,
222 .shost_attrs = hpsa_shost_attrs,
225 static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
227 unsigned long *priv = shost_priv(sdev->host);
228 return (struct ctlr_info *) *priv;
231 static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
233 unsigned long *priv = shost_priv(sh);
234 return (struct ctlr_info *) *priv;
237 static int check_for_unit_attention(struct ctlr_info *h,
238 struct CommandList *c)
240 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
243 switch (c->err_info->SenseInfo[12]) {
245 dev_warn(&h->pdev->dev, "hpsa%d: a state change "
246 "detected, command retried\n", h->ctlr);
249 dev_warn(&h->pdev->dev, "hpsa%d: LUN failure "
250 "detected, action required\n", h->ctlr);
252 case REPORT_LUNS_CHANGED:
253 dev_warn(&h->pdev->dev, "hpsa%d: report LUN data "
254 "changed, action required\n", h->ctlr);
256 * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
260 dev_warn(&h->pdev->dev, "hpsa%d: a power on "
261 "or device reset detected\n", h->ctlr);
263 case UNIT_ATTENTION_CLEARED:
264 dev_warn(&h->pdev->dev, "hpsa%d: unit attention "
265 "cleared by another initiator\n", h->ctlr);
268 dev_warn(&h->pdev->dev, "hpsa%d: unknown "
269 "unit attention detected\n", h->ctlr);
275 static ssize_t host_store_rescan(struct device *dev,
276 struct device_attribute *attr,
277 const char *buf, size_t count)
280 struct Scsi_Host *shost = class_to_shost(dev);
281 h = shost_to_hba(shost);
282 hpsa_scan_start(h->scsi_host);
286 static ssize_t host_show_firmware_revision(struct device *dev,
287 struct device_attribute *attr, char *buf)
290 struct Scsi_Host *shost = class_to_shost(dev);
291 unsigned char *fwrev;
293 h = shost_to_hba(shost);
294 if (!h->hba_inquiry_data)
296 fwrev = &h->hba_inquiry_data[32];
297 return snprintf(buf, 20, "%c%c%c%c\n",
298 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
301 /* Enqueuing and dequeuing functions for cmdlists. */
302 static inline void addQ(struct hlist_head *list, struct CommandList *c)
304 hlist_add_head(&c->list, list);
307 static inline u32 next_command(struct ctlr_info *h)
311 if (unlikely(h->transMethod != CFGTBL_Trans_Performant))
312 return h->access.command_completed(h);
314 if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) {
315 a = *(h->reply_pool_head); /* Next cmd in ring buffer */
316 (h->reply_pool_head)++;
317 h->commands_outstanding--;
321 /* Check for wraparound */
322 if (h->reply_pool_head == (h->reply_pool + h->max_commands)) {
323 h->reply_pool_head = h->reply_pool;
324 h->reply_pool_wraparound ^= 1;
329 /* set_performant_mode: Modify the tag for cciss performant
330 * set bit 0 for pull model, bits 3-1 for block fetch
333 static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
335 if (likely(h->transMethod == CFGTBL_Trans_Performant))
336 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
339 static void enqueue_cmd_and_start_io(struct ctlr_info *h,
340 struct CommandList *c)
344 set_performant_mode(h, c);
345 spin_lock_irqsave(&h->lock, flags);
349 spin_unlock_irqrestore(&h->lock, flags);
352 static inline void removeQ(struct CommandList *c)
354 if (WARN_ON(hlist_unhashed(&c->list)))
356 hlist_del_init(&c->list);
359 static inline int is_hba_lunid(unsigned char scsi3addr[])
361 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
364 static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
366 return (scsi3addr[3] & 0xC0) == 0x40;
369 static inline int is_scsi_rev_5(struct ctlr_info *h)
371 if (!h->hba_inquiry_data)
373 if ((h->hba_inquiry_data[2] & 0x07) == 5)
378 static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
381 #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
383 static ssize_t raid_level_show(struct device *dev,
384 struct device_attribute *attr, char *buf)
387 unsigned char rlevel;
389 struct scsi_device *sdev;
390 struct hpsa_scsi_dev_t *hdev;
393 sdev = to_scsi_device(dev);
394 h = sdev_to_hba(sdev);
395 spin_lock_irqsave(&h->lock, flags);
396 hdev = sdev->hostdata;
398 spin_unlock_irqrestore(&h->lock, flags);
402 /* Is this even a logical drive? */
403 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
404 spin_unlock_irqrestore(&h->lock, flags);
405 l = snprintf(buf, PAGE_SIZE, "N/A\n");
409 rlevel = hdev->raid_level;
410 spin_unlock_irqrestore(&h->lock, flags);
411 if (rlevel > RAID_UNKNOWN)
412 rlevel = RAID_UNKNOWN;
413 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
417 static ssize_t lunid_show(struct device *dev,
418 struct device_attribute *attr, char *buf)
421 struct scsi_device *sdev;
422 struct hpsa_scsi_dev_t *hdev;
424 unsigned char lunid[8];
426 sdev = to_scsi_device(dev);
427 h = sdev_to_hba(sdev);
428 spin_lock_irqsave(&h->lock, flags);
429 hdev = sdev->hostdata;
431 spin_unlock_irqrestore(&h->lock, flags);
434 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
435 spin_unlock_irqrestore(&h->lock, flags);
436 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
437 lunid[0], lunid[1], lunid[2], lunid[3],
438 lunid[4], lunid[5], lunid[6], lunid[7]);
441 static ssize_t unique_id_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
445 struct scsi_device *sdev;
446 struct hpsa_scsi_dev_t *hdev;
448 unsigned char sn[16];
450 sdev = to_scsi_device(dev);
451 h = sdev_to_hba(sdev);
452 spin_lock_irqsave(&h->lock, flags);
453 hdev = sdev->hostdata;
455 spin_unlock_irqrestore(&h->lock, flags);
458 memcpy(sn, hdev->device_id, sizeof(sn));
459 spin_unlock_irqrestore(&h->lock, flags);
460 return snprintf(buf, 16 * 2 + 2,
461 "%02X%02X%02X%02X%02X%02X%02X%02X"
462 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
463 sn[0], sn[1], sn[2], sn[3],
464 sn[4], sn[5], sn[6], sn[7],
465 sn[8], sn[9], sn[10], sn[11],
466 sn[12], sn[13], sn[14], sn[15]);
469 static int hpsa_find_target_lun(struct ctlr_info *h,
470 unsigned char scsi3addr[], int bus, int *target, int *lun)
472 /* finds an unused bus, target, lun for a new physical device
473 * assumes h->devlock is held
476 DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA);
478 memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3);
480 for (i = 0; i < h->ndevices; i++) {
481 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
482 set_bit(h->dev[i]->target, lun_taken);
485 for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) {
486 if (!test_bit(i, lun_taken)) {
497 /* Add an entry into h->dev[] array. */
498 static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
499 struct hpsa_scsi_dev_t *device,
500 struct hpsa_scsi_dev_t *added[], int *nadded)
502 /* assumes h->devlock is held */
505 unsigned char addr1[8], addr2[8];
506 struct hpsa_scsi_dev_t *sd;
508 if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) {
509 dev_err(&h->pdev->dev, "too many devices, some will be "
514 /* physical devices do not have lun or target assigned until now. */
515 if (device->lun != -1)
516 /* Logical device, lun is already assigned. */
519 /* If this device a non-zero lun of a multi-lun device
520 * byte 4 of the 8-byte LUN addr will contain the logical
521 * unit no, zero otherise.
523 if (device->scsi3addr[4] == 0) {
524 /* This is not a non-zero lun of a multi-lun device */
525 if (hpsa_find_target_lun(h, device->scsi3addr,
526 device->bus, &device->target, &device->lun) != 0)
531 /* This is a non-zero lun of a multi-lun device.
532 * Search through our list and find the device which
533 * has the same 8 byte LUN address, excepting byte 4.
534 * Assign the same bus and target for this new LUN.
535 * Use the logical unit number from the firmware.
537 memcpy(addr1, device->scsi3addr, 8);
539 for (i = 0; i < n; i++) {
541 memcpy(addr2, sd->scsi3addr, 8);
543 /* differ only in byte 4? */
544 if (memcmp(addr1, addr2, 8) == 0) {
545 device->bus = sd->bus;
546 device->target = sd->target;
547 device->lun = device->scsi3addr[4];
551 if (device->lun == -1) {
552 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
553 " suspect firmware bug or unsupported hardware "
562 added[*nadded] = device;
565 /* initially, (before registering with scsi layer) we don't
566 * know our hostno and we don't want to print anything first
567 * time anyway (the scsi layer's inquiries will show that info)
569 /* if (hostno != -1) */
570 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
571 scsi_device_type(device->devtype), hostno,
572 device->bus, device->target, device->lun);
576 /* Replace an entry from h->dev[] array. */
577 static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
578 int entry, struct hpsa_scsi_dev_t *new_entry,
579 struct hpsa_scsi_dev_t *added[], int *nadded,
580 struct hpsa_scsi_dev_t *removed[], int *nremoved)
582 /* assumes h->devlock is held */
583 BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
584 removed[*nremoved] = h->dev[entry];
586 h->dev[entry] = new_entry;
587 added[*nadded] = new_entry;
589 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
590 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
591 new_entry->target, new_entry->lun);
594 /* Remove an entry from h->dev[] array. */
595 static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
596 struct hpsa_scsi_dev_t *removed[], int *nremoved)
598 /* assumes h->devlock is held */
600 struct hpsa_scsi_dev_t *sd;
602 BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
605 removed[*nremoved] = h->dev[entry];
608 for (i = entry; i < h->ndevices-1; i++)
609 h->dev[i] = h->dev[i+1];
611 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
612 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
616 #define SCSI3ADDR_EQ(a, b) ( \
617 (a)[7] == (b)[7] && \
618 (a)[6] == (b)[6] && \
619 (a)[5] == (b)[5] && \
620 (a)[4] == (b)[4] && \
621 (a)[3] == (b)[3] && \
622 (a)[2] == (b)[2] && \
623 (a)[1] == (b)[1] && \
626 static void fixup_botched_add(struct ctlr_info *h,
627 struct hpsa_scsi_dev_t *added)
629 /* called when scsi_add_device fails in order to re-adjust
630 * h->dev[] to match the mid layer's view.
635 spin_lock_irqsave(&h->lock, flags);
636 for (i = 0; i < h->ndevices; i++) {
637 if (h->dev[i] == added) {
638 for (j = i; j < h->ndevices-1; j++)
639 h->dev[j] = h->dev[j+1];
644 spin_unlock_irqrestore(&h->lock, flags);
648 static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
649 struct hpsa_scsi_dev_t *dev2)
651 if ((is_logical_dev_addr_mode(dev1->scsi3addr) ||
652 (dev1->lun != -1 && dev2->lun != -1)) &&
653 dev1->devtype != 0x0C)
654 return (memcmp(dev1, dev2, sizeof(*dev1)) == 0);
656 /* we compare everything except lun and target as these
657 * are not yet assigned. Compare parts likely
660 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
661 sizeof(dev1->scsi3addr)) != 0)
663 if (memcmp(dev1->device_id, dev2->device_id,
664 sizeof(dev1->device_id)) != 0)
666 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
668 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
670 if (memcmp(dev1->revision, dev2->revision, sizeof(dev1->revision)) != 0)
672 if (dev1->devtype != dev2->devtype)
674 if (dev1->raid_level != dev2->raid_level)
676 if (dev1->bus != dev2->bus)
681 /* Find needle in haystack. If exact match found, return DEVICE_SAME,
682 * and return needle location in *index. If scsi3addr matches, but not
683 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
684 * location in *index. If needle not found, return DEVICE_NOT_FOUND.
686 static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
687 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
691 #define DEVICE_NOT_FOUND 0
692 #define DEVICE_CHANGED 1
693 #define DEVICE_SAME 2
694 for (i = 0; i < haystack_size; i++) {
695 if (haystack[i] == NULL) /* previously removed. */
697 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
699 if (device_is_the_same(needle, haystack[i]))
702 return DEVICE_CHANGED;
706 return DEVICE_NOT_FOUND;
709 static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
710 struct hpsa_scsi_dev_t *sd[], int nsds)
712 /* sd contains scsi3 addresses and devtypes, and inquiry
713 * data. This function takes what's in sd to be the current
714 * reality and updates h->dev[] to reflect that reality.
716 int i, entry, device_change, changes = 0;
717 struct hpsa_scsi_dev_t *csd;
719 struct hpsa_scsi_dev_t **added, **removed;
720 int nadded, nremoved;
721 struct Scsi_Host *sh = NULL;
723 added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA,
725 removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA,
728 if (!added || !removed) {
729 dev_warn(&h->pdev->dev, "out of memory in "
730 "adjust_hpsa_scsi_table\n");
734 spin_lock_irqsave(&h->devlock, flags);
736 /* find any devices in h->dev[] that are not in
737 * sd[] and remove them from h->dev[], and for any
738 * devices which have changed, remove the old device
739 * info and add the new device info.
744 while (i < h->ndevices) {
746 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
747 if (device_change == DEVICE_NOT_FOUND) {
749 hpsa_scsi_remove_entry(h, hostno, i,
751 continue; /* remove ^^^, hence i not incremented */
752 } else if (device_change == DEVICE_CHANGED) {
754 hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
755 added, &nadded, removed, &nremoved);
756 /* Set it to NULL to prevent it from being freed
757 * at the bottom of hpsa_update_scsi_devices()
764 /* Now, make sure every device listed in sd[] is also
765 * listed in h->dev[], adding them if they aren't found
768 for (i = 0; i < nsds; i++) {
769 if (!sd[i]) /* if already added above. */
771 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
772 h->ndevices, &entry);
773 if (device_change == DEVICE_NOT_FOUND) {
775 if (hpsa_scsi_add_entry(h, hostno, sd[i],
776 added, &nadded) != 0)
778 sd[i] = NULL; /* prevent from being freed later. */
779 } else if (device_change == DEVICE_CHANGED) {
780 /* should never happen... */
782 dev_warn(&h->pdev->dev,
783 "device unexpectedly changed.\n");
784 /* but if it does happen, we just ignore that device */
787 spin_unlock_irqrestore(&h->devlock, flags);
789 /* Don't notify scsi mid layer of any changes the first time through
790 * (or if there are no changes) scsi_scan_host will do it later the
791 * first time through.
793 if (hostno == -1 || !changes)
797 /* Notify scsi mid layer of any removed devices */
798 for (i = 0; i < nremoved; i++) {
799 struct scsi_device *sdev =
800 scsi_device_lookup(sh, removed[i]->bus,
801 removed[i]->target, removed[i]->lun);
803 scsi_remove_device(sdev);
804 scsi_device_put(sdev);
806 /* We don't expect to get here.
807 * future cmds to this device will get selection
808 * timeout as if the device was gone.
810 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
811 " for removal.", hostno, removed[i]->bus,
812 removed[i]->target, removed[i]->lun);
818 /* Notify scsi mid layer of any added devices */
819 for (i = 0; i < nadded; i++) {
820 if (scsi_add_device(sh, added[i]->bus,
821 added[i]->target, added[i]->lun) == 0)
823 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
824 "device not added.\n", hostno, added[i]->bus,
825 added[i]->target, added[i]->lun);
826 /* now we have to remove it from h->dev,
827 * since it didn't get added to scsi mid layer
829 fixup_botched_add(h, added[i]);
838 * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
839 * Assume's h->devlock is held.
841 static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
842 int bus, int target, int lun)
845 struct hpsa_scsi_dev_t *sd;
847 for (i = 0; i < h->ndevices; i++) {
849 if (sd->bus == bus && sd->target == target && sd->lun == lun)
855 /* link sdev->hostdata to our per-device structure. */
856 static int hpsa_slave_alloc(struct scsi_device *sdev)
858 struct hpsa_scsi_dev_t *sd;
862 h = sdev_to_hba(sdev);
863 spin_lock_irqsave(&h->devlock, flags);
864 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
865 sdev_id(sdev), sdev->lun);
868 spin_unlock_irqrestore(&h->devlock, flags);
872 static void hpsa_slave_destroy(struct scsi_device *sdev)
877 static void hpsa_scsi_setup(struct ctlr_info *h)
881 spin_lock_init(&h->devlock);
884 static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
890 for (i = 0; i < h->nr_cmds; i++) {
891 kfree(h->cmd_sg_list[i]);
892 h->cmd_sg_list[i] = NULL;
894 kfree(h->cmd_sg_list);
895 h->cmd_sg_list = NULL;
898 static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
902 if (h->chainsize <= 0)
905 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
909 for (i = 0; i < h->nr_cmds; i++) {
910 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
911 h->chainsize, GFP_KERNEL);
912 if (!h->cmd_sg_list[i])
918 hpsa_free_sg_chain_blocks(h);
922 static void hpsa_map_sg_chain_block(struct ctlr_info *h,
923 struct CommandList *c)
925 struct SGDescriptor *chain_sg, *chain_block;
928 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
929 chain_block = h->cmd_sg_list[c->cmdindex];
930 chain_sg->Ext = HPSA_SG_CHAIN;
931 chain_sg->Len = sizeof(*chain_sg) *
932 (c->Header.SGTotal - h->max_cmd_sg_entries);
933 temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
935 chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
936 chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
939 static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
940 struct CommandList *c)
942 struct SGDescriptor *chain_sg;
945 if (c->Header.SGTotal <= h->max_cmd_sg_entries)
948 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
949 temp64.val32.lower = chain_sg->Addr.lower;
950 temp64.val32.upper = chain_sg->Addr.upper;
951 pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
954 static void complete_scsi_command(struct CommandList *cp,
955 int timeout, u32 tag)
957 struct scsi_cmnd *cmd;
959 struct ErrorInfo *ei;
961 unsigned char sense_key;
962 unsigned char asc; /* additional sense code */
963 unsigned char ascq; /* additional sense code qualifier */
966 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
969 scsi_dma_unmap(cmd); /* undo the DMA mappings */
970 if (cp->Header.SGTotal > h->max_cmd_sg_entries)
971 hpsa_unmap_sg_chain_block(h, cp);
973 cmd->result = (DID_OK << 16); /* host byte */
974 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
975 cmd->result |= ei->ScsiStatus;
977 /* copy the sense data whether we need to or not. */
978 memcpy(cmd->sense_buffer, ei->SenseInfo,
979 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
980 SCSI_SENSE_BUFFERSIZE :
982 scsi_set_resid(cmd, ei->ResidualCnt);
984 if (ei->CommandStatus == 0) {
990 /* an error has occurred */
991 switch (ei->CommandStatus) {
993 case CMD_TARGET_STATUS:
994 if (ei->ScsiStatus) {
996 sense_key = 0xf & ei->SenseInfo[2];
997 /* Get additional sense code */
998 asc = ei->SenseInfo[12];
999 /* Get addition sense code qualifier */
1000 ascq = ei->SenseInfo[13];
1003 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
1004 if (check_for_unit_attention(h, cp)) {
1005 cmd->result = DID_SOFT_ERROR << 16;
1008 if (sense_key == ILLEGAL_REQUEST) {
1010 * SCSI REPORT_LUNS is commonly unsupported on
1011 * Smart Array. Suppress noisy complaint.
1013 if (cp->Request.CDB[0] == REPORT_LUNS)
1016 /* If ASC/ASCQ indicate Logical Unit
1017 * Not Supported condition,
1019 if ((asc == 0x25) && (ascq == 0x0)) {
1020 dev_warn(&h->pdev->dev, "cp %p "
1021 "has check condition\n", cp);
1026 if (sense_key == NOT_READY) {
1027 /* If Sense is Not Ready, Logical Unit
1028 * Not ready, Manual Intervention
1031 if ((asc == 0x04) && (ascq == 0x03)) {
1032 dev_warn(&h->pdev->dev, "cp %p "
1033 "has check condition: unit "
1034 "not ready, manual "
1035 "intervention required\n", cp);
1039 if (sense_key == ABORTED_COMMAND) {
1040 /* Aborted command is retryable */
1041 dev_warn(&h->pdev->dev, "cp %p "
1042 "has check condition: aborted command: "
1043 "ASC: 0x%x, ASCQ: 0x%x\n",
1045 cmd->result = DID_SOFT_ERROR << 16;
1048 /* Must be some other type of check condition */
1049 dev_warn(&h->pdev->dev, "cp %p has check condition: "
1051 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1052 "Returning result: 0x%x, "
1053 "cmd=[%02x %02x %02x %02x %02x "
1054 "%02x %02x %02x %02x %02x %02x "
1055 "%02x %02x %02x %02x %02x]\n",
1056 cp, sense_key, asc, ascq,
1058 cmd->cmnd[0], cmd->cmnd[1],
1059 cmd->cmnd[2], cmd->cmnd[3],
1060 cmd->cmnd[4], cmd->cmnd[5],
1061 cmd->cmnd[6], cmd->cmnd[7],
1062 cmd->cmnd[8], cmd->cmnd[9],
1063 cmd->cmnd[10], cmd->cmnd[11],
1064 cmd->cmnd[12], cmd->cmnd[13],
1065 cmd->cmnd[14], cmd->cmnd[15]);
1070 /* Problem was not a check condition
1071 * Pass it up to the upper layers...
1073 if (ei->ScsiStatus) {
1074 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1075 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1076 "Returning result: 0x%x\n",
1078 sense_key, asc, ascq,
1080 } else { /* scsi status is zero??? How??? */
1081 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1082 "Returning no connection.\n", cp),
1084 /* Ordinarily, this case should never happen,
1085 * but there is a bug in some released firmware
1086 * revisions that allows it to happen if, for
1087 * example, a 4100 backplane loses power and
1088 * the tape drive is in it. We assume that
1089 * it's a fatal error of some kind because we
1090 * can't show that it wasn't. We will make it
1091 * look like selection timeout since that is
1092 * the most common reason for this to occur,
1093 * and it's severe enough.
1096 cmd->result = DID_NO_CONNECT << 16;
1100 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1102 case CMD_DATA_OVERRUN:
1103 dev_warn(&h->pdev->dev, "cp %p has"
1104 " completed with data overrun "
1108 /* print_bytes(cp, sizeof(*cp), 1, 0);
1110 /* We get CMD_INVALID if you address a non-existent device
1111 * instead of a selection timeout (no response). You will
1112 * see this if you yank out a drive, then try to access it.
1113 * This is kind of a shame because it means that any other
1114 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1115 * missing target. */
1116 cmd->result = DID_NO_CONNECT << 16;
1119 case CMD_PROTOCOL_ERR:
1120 dev_warn(&h->pdev->dev, "cp %p has "
1121 "protocol error \n", cp);
1123 case CMD_HARDWARE_ERR:
1124 cmd->result = DID_ERROR << 16;
1125 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1127 case CMD_CONNECTION_LOST:
1128 cmd->result = DID_ERROR << 16;
1129 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1132 cmd->result = DID_ABORT << 16;
1133 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1134 cp, ei->ScsiStatus);
1136 case CMD_ABORT_FAILED:
1137 cmd->result = DID_ERROR << 16;
1138 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1140 case CMD_UNSOLICITED_ABORT:
1141 cmd->result = DID_RESET << 16;
1142 dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited "
1146 cmd->result = DID_TIME_OUT << 16;
1147 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1150 cmd->result = DID_ERROR << 16;
1151 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1152 cp, ei->CommandStatus);
1154 cmd->scsi_done(cmd);
1158 static int hpsa_scsi_detect(struct ctlr_info *h)
1160 struct Scsi_Host *sh;
1163 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
1170 sh->max_channel = 3;
1171 sh->max_cmd_len = MAX_COMMAND_SIZE;
1172 sh->max_lun = HPSA_MAX_LUN;
1173 sh->max_id = HPSA_MAX_LUN;
1174 sh->can_queue = h->nr_cmds;
1175 sh->cmd_per_lun = h->nr_cmds;
1176 sh->sg_tablesize = h->maxsgentries;
1178 sh->hostdata[0] = (unsigned long) h;
1179 sh->irq = h->intr[PERF_MODE_INT];
1180 sh->unique_id = sh->irq;
1181 error = scsi_add_host(sh, &h->pdev->dev);
1188 dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host"
1189 " failed for controller %d\n", h->ctlr);
1193 dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc"
1194 " failed for controller %d\n", h->ctlr);
1198 static void hpsa_pci_unmap(struct pci_dev *pdev,
1199 struct CommandList *c, int sg_used, int data_direction)
1202 union u64bit addr64;
1204 for (i = 0; i < sg_used; i++) {
1205 addr64.val32.lower = c->SG[i].Addr.lower;
1206 addr64.val32.upper = c->SG[i].Addr.upper;
1207 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1212 static void hpsa_map_one(struct pci_dev *pdev,
1213 struct CommandList *cp,
1220 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1221 cp->Header.SGList = 0;
1222 cp->Header.SGTotal = 0;
1226 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
1227 cp->SG[0].Addr.lower =
1228 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
1229 cp->SG[0].Addr.upper =
1230 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
1231 cp->SG[0].Len = buflen;
1232 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1233 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
1236 static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1237 struct CommandList *c)
1239 DECLARE_COMPLETION_ONSTACK(wait);
1242 enqueue_cmd_and_start_io(h, c);
1243 wait_for_completion(&wait);
1246 static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1247 struct CommandList *c, int data_direction)
1249 int retry_count = 0;
1252 memset(c->err_info, 0, sizeof(c->err_info));
1253 hpsa_scsi_do_simple_cmd_core(h, c);
1255 } while (check_for_unit_attention(h, c) && retry_count <= 3);
1256 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1259 static void hpsa_scsi_interpret_error(struct CommandList *cp)
1261 struct ErrorInfo *ei;
1262 struct device *d = &cp->h->pdev->dev;
1265 switch (ei->CommandStatus) {
1266 case CMD_TARGET_STATUS:
1267 dev_warn(d, "cmd %p has completed with errors\n", cp);
1268 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1270 if (ei->ScsiStatus == 0)
1271 dev_warn(d, "SCSI status is abnormally zero. "
1272 "(probably indicates selection timeout "
1273 "reported incorrectly due to a known "
1274 "firmware bug, circa July, 2001.)\n");
1276 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1277 dev_info(d, "UNDERRUN\n");
1279 case CMD_DATA_OVERRUN:
1280 dev_warn(d, "cp %p has completed with data overrun\n", cp);
1283 /* controller unfortunately reports SCSI passthru's
1284 * to non-existent targets as invalid commands.
1286 dev_warn(d, "cp %p is reported invalid (probably means "
1287 "target device no longer present)\n", cp);
1288 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1292 case CMD_PROTOCOL_ERR:
1293 dev_warn(d, "cp %p has protocol error \n", cp);
1295 case CMD_HARDWARE_ERR:
1296 /* cmd->result = DID_ERROR << 16; */
1297 dev_warn(d, "cp %p had hardware error\n", cp);
1299 case CMD_CONNECTION_LOST:
1300 dev_warn(d, "cp %p had connection lost\n", cp);
1303 dev_warn(d, "cp %p was aborted\n", cp);
1305 case CMD_ABORT_FAILED:
1306 dev_warn(d, "cp %p reports abort failed\n", cp);
1308 case CMD_UNSOLICITED_ABORT:
1309 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1312 dev_warn(d, "cp %p timed out\n", cp);
1315 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1320 static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1321 unsigned char page, unsigned char *buf,
1322 unsigned char bufsize)
1325 struct CommandList *c;
1326 struct ErrorInfo *ei;
1328 c = cmd_special_alloc(h);
1330 if (c == NULL) { /* trouble... */
1331 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1335 fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD);
1336 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1338 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1339 hpsa_scsi_interpret_error(c);
1342 cmd_special_free(h, c);
1346 static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
1349 struct CommandList *c;
1350 struct ErrorInfo *ei;
1352 c = cmd_special_alloc(h);
1354 if (c == NULL) { /* trouble... */
1355 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1359 fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG);
1360 hpsa_scsi_do_simple_cmd_core(h, c);
1361 /* no unmap needed here because no data xfer. */
1364 if (ei->CommandStatus != 0) {
1365 hpsa_scsi_interpret_error(c);
1368 cmd_special_free(h, c);
1372 static void hpsa_get_raid_level(struct ctlr_info *h,
1373 unsigned char *scsi3addr, unsigned char *raid_level)
1378 *raid_level = RAID_UNKNOWN;
1379 buf = kzalloc(64, GFP_KERNEL);
1382 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1384 *raid_level = buf[8];
1385 if (*raid_level > RAID_UNKNOWN)
1386 *raid_level = RAID_UNKNOWN;
1391 /* Get the device id from inquiry page 0x83 */
1392 static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
1393 unsigned char *device_id, int buflen)
1400 buf = kzalloc(64, GFP_KERNEL);
1403 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1405 memcpy(device_id, &buf[8], buflen);
1410 static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
1411 struct ReportLUNdata *buf, int bufsize,
1412 int extended_response)
1415 struct CommandList *c;
1416 unsigned char scsi3addr[8];
1417 struct ErrorInfo *ei;
1419 c = cmd_special_alloc(h);
1420 if (c == NULL) { /* trouble... */
1421 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1424 /* address the controller */
1425 memset(scsi3addr, 0, sizeof(scsi3addr));
1426 fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
1427 buf, bufsize, 0, scsi3addr, TYPE_CMD);
1428 if (extended_response)
1429 c->Request.CDB[1] = extended_response;
1430 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1432 if (ei->CommandStatus != 0 &&
1433 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1434 hpsa_scsi_interpret_error(c);
1437 cmd_special_free(h, c);
1441 static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
1442 struct ReportLUNdata *buf,
1443 int bufsize, int extended_response)
1445 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
1448 static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
1449 struct ReportLUNdata *buf, int bufsize)
1451 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
1454 static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
1455 int bus, int target, int lun)
1458 device->target = target;
1462 static int hpsa_update_device_info(struct ctlr_info *h,
1463 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device)
1465 #define OBDR_TAPE_INQ_SIZE 49
1466 unsigned char *inq_buff;
1468 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1472 /* Do an inquiry to the device to see what it is. */
1473 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1474 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1475 /* Inquiry failed (msg printed already) */
1476 dev_err(&h->pdev->dev,
1477 "hpsa_update_device_info: inquiry failed\n");
1481 this_device->devtype = (inq_buff[0] & 0x1f);
1482 memcpy(this_device->scsi3addr, scsi3addr, 8);
1483 memcpy(this_device->vendor, &inq_buff[8],
1484 sizeof(this_device->vendor));
1485 memcpy(this_device->model, &inq_buff[16],
1486 sizeof(this_device->model));
1487 memcpy(this_device->revision, &inq_buff[32],
1488 sizeof(this_device->revision));
1489 memset(this_device->device_id, 0,
1490 sizeof(this_device->device_id));
1491 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
1492 sizeof(this_device->device_id));
1494 if (this_device->devtype == TYPE_DISK &&
1495 is_logical_dev_addr_mode(scsi3addr))
1496 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
1498 this_device->raid_level = RAID_UNKNOWN;
1508 static unsigned char *msa2xxx_model[] = {
1516 static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1520 for (i = 0; msa2xxx_model[i]; i++)
1521 if (strncmp(device->model, msa2xxx_model[i],
1522 strlen(msa2xxx_model[i])) == 0)
1527 /* Helper function to assign bus, target, lun mapping of devices.
1528 * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
1529 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
1530 * Logical drive target and lun are assigned at this time, but
1531 * physical device lun and target assignment are deferred (assigned
1532 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
1534 static void figure_bus_target_lun(struct ctlr_info *h,
1535 u8 *lunaddrbytes, int *bus, int *target, int *lun,
1536 struct hpsa_scsi_dev_t *device)
1540 if (is_logical_dev_addr_mode(lunaddrbytes)) {
1541 /* logical device */
1542 if (unlikely(is_scsi_rev_5(h))) {
1543 /* p1210m, logical drives lun assignments
1544 * match SCSI REPORT LUNS data.
1546 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
1549 *lun = (lunid & 0x3fff) + 1;
1552 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
1553 if (is_msa2xxx(h, device)) {
1554 /* msa2xxx way, put logicals on bus 1
1555 * and match target/lun numbers box
1559 *target = (lunid >> 16) & 0x3fff;
1560 *lun = lunid & 0x00ff;
1562 /* Traditional smart array way. */
1565 *target = lunid & 0x3fff;
1569 /* physical device */
1570 if (is_hba_lunid(lunaddrbytes))
1571 if (unlikely(is_scsi_rev_5(h))) {
1572 *bus = 0; /* put p1210m ctlr at 0,0,0 */
1577 *bus = 3; /* traditional smartarray */
1579 *bus = 2; /* physical disk */
1581 *lun = -1; /* we will fill these in later. */
1586 * If there is no lun 0 on a target, linux won't find any devices.
1587 * For the MSA2xxx boxes, we have to manually detect the enclosure
1588 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
1589 * it for some reason. *tmpdevice is the target we're adding,
1590 * this_device is a pointer into the current element of currentsd[]
1591 * that we're building up in update_scsi_devices(), below.
1592 * lunzerobits is a bitmap that tracks which targets already have a
1594 * Returns 1 if an enclosure was added, 0 if not.
1596 static int add_msa2xxx_enclosure_device(struct ctlr_info *h,
1597 struct hpsa_scsi_dev_t *tmpdevice,
1598 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
1599 int bus, int target, int lun, unsigned long lunzerobits[],
1600 int *nmsa2xxx_enclosures)
1602 unsigned char scsi3addr[8];
1604 if (test_bit(target, lunzerobits))
1605 return 0; /* There is already a lun 0 on this target. */
1607 if (!is_logical_dev_addr_mode(lunaddrbytes))
1608 return 0; /* It's the logical targets that may lack lun 0. */
1610 if (!is_msa2xxx(h, tmpdevice))
1611 return 0; /* It's only the MSA2xxx that have this problem. */
1613 if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */
1616 if (is_hba_lunid(scsi3addr))
1617 return 0; /* Don't add the RAID controller here. */
1619 if (is_scsi_rev_5(h))
1620 return 0; /* p1210m doesn't need to do this. */
1622 #define MAX_MSA2XXX_ENCLOSURES 32
1623 if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) {
1624 dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX "
1625 "enclosures exceeded. Check your hardware "
1630 memset(scsi3addr, 0, 8);
1631 scsi3addr[3] = target;
1632 if (hpsa_update_device_info(h, scsi3addr, this_device))
1634 (*nmsa2xxx_enclosures)++;
1635 hpsa_set_bus_target_lun(this_device, bus, target, 0);
1636 set_bit(target, lunzerobits);
1641 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
1642 * logdev. The number of luns in physdev and logdev are returned in
1643 * *nphysicals and *nlogicals, respectively.
1644 * Returns 0 on success, -1 otherwise.
1646 static int hpsa_gather_lun_info(struct ctlr_info *h,
1648 struct ReportLUNdata *physdev, u32 *nphysicals,
1649 struct ReportLUNdata *logdev, u32 *nlogicals)
1651 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
1652 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
1655 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8;
1656 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
1657 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
1658 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1659 *nphysicals - HPSA_MAX_PHYS_LUN);
1660 *nphysicals = HPSA_MAX_PHYS_LUN;
1662 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
1663 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
1666 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
1667 /* Reject Logicals in excess of our max capability. */
1668 if (*nlogicals > HPSA_MAX_LUN) {
1669 dev_warn(&h->pdev->dev,
1670 "maximum logical LUNs (%d) exceeded. "
1671 "%d LUNs ignored.\n", HPSA_MAX_LUN,
1672 *nlogicals - HPSA_MAX_LUN);
1673 *nlogicals = HPSA_MAX_LUN;
1675 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
1676 dev_warn(&h->pdev->dev,
1677 "maximum logical + physical LUNs (%d) exceeded. "
1678 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1679 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
1680 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
1685 u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
1686 int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list,
1687 struct ReportLUNdata *logdev_list)
1689 /* Helper function, figure out where the LUN ID info is coming from
1690 * given index i, lists of physical and logical devices, where in
1691 * the list the raid controller is supposed to appear (first or last)
1694 int logicals_start = nphysicals + (raid_ctlr_position == 0);
1695 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
1697 if (i == raid_ctlr_position)
1698 return RAID_CTLR_LUNID;
1700 if (i < logicals_start)
1701 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
1703 if (i < last_device)
1704 return &logdev_list->LUN[i - nphysicals -
1705 (raid_ctlr_position == 0)][0];
1710 static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
1712 /* the idea here is we could get notified
1713 * that some devices have changed, so we do a report
1714 * physical luns and report logical luns cmd, and adjust
1715 * our list of devices accordingly.
1717 * The scsi3addr's of devices won't change so long as the
1718 * adapter is not reset. That means we can rescan and
1719 * tell which devices we already know about, vs. new
1720 * devices, vs. disappearing devices.
1722 struct ReportLUNdata *physdev_list = NULL;
1723 struct ReportLUNdata *logdev_list = NULL;
1724 unsigned char *inq_buff = NULL;
1727 u32 ndev_allocated = 0;
1728 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
1730 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
1731 int i, nmsa2xxx_enclosures, ndevs_to_allocate;
1732 int bus, target, lun;
1733 int raid_ctlr_position;
1734 DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR);
1736 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA,
1738 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1739 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1740 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1741 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
1743 if (!currentsd || !physdev_list || !logdev_list ||
1744 !inq_buff || !tmpdevice) {
1745 dev_err(&h->pdev->dev, "out of memory\n");
1748 memset(lunzerobits, 0, sizeof(lunzerobits));
1750 if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
1751 logdev_list, &nlogicals))
1754 /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
1755 * but each of them 4 times through different paths. The plus 1
1756 * is for the RAID controller.
1758 ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1;
1760 /* Allocate the per device structures */
1761 for (i = 0; i < ndevs_to_allocate; i++) {
1762 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
1763 if (!currentsd[i]) {
1764 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
1765 __FILE__, __LINE__);
1771 if (unlikely(is_scsi_rev_5(h)))
1772 raid_ctlr_position = 0;
1774 raid_ctlr_position = nphysicals + nlogicals;
1776 /* adjust our table of devices */
1777 nmsa2xxx_enclosures = 0;
1778 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
1781 /* Figure out where the LUN ID info is coming from */
1782 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
1783 i, nphysicals, nlogicals, physdev_list, logdev_list);
1784 /* skip masked physical devices. */
1785 if (lunaddrbytes[3] & 0xC0 &&
1786 i < nphysicals + (raid_ctlr_position == 0))
1789 /* Get device type, vendor, model, device id */
1790 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice))
1791 continue; /* skip it if we can't talk to it. */
1792 figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun,
1794 this_device = currentsd[ncurrent];
1797 * For the msa2xxx boxes, we have to insert a LUN 0 which
1798 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
1799 * is nonetheless an enclosure device there. We have to
1800 * present that otherwise linux won't find anything if
1801 * there is no lun 0.
1803 if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device,
1804 lunaddrbytes, bus, target, lun, lunzerobits,
1805 &nmsa2xxx_enclosures)) {
1807 this_device = currentsd[ncurrent];
1810 *this_device = *tmpdevice;
1811 hpsa_set_bus_target_lun(this_device, bus, target, lun);
1813 switch (this_device->devtype) {
1815 /* We don't *really* support actual CD-ROM devices,
1816 * just "One Button Disaster Recovery" tape drive
1817 * which temporarily pretends to be a CD-ROM drive.
1818 * So we check that the device is really an OBDR tape
1819 * device by checking for "$DR-10" in bytes 43-48 of
1823 #define OBDR_TAPE_SIG "$DR-10"
1824 strncpy(obdr_sig, &inq_buff[43], 6);
1826 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1827 /* Not OBDR device, ignore it. */
1838 case TYPE_MEDIUM_CHANGER:
1842 /* Only present the Smartarray HBA as a RAID controller.
1843 * If it's a RAID controller other than the HBA itself
1844 * (an external RAID controller, MSA500 or similar)
1847 if (!is_hba_lunid(lunaddrbytes))
1854 if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA)
1857 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
1860 for (i = 0; i < ndev_allocated; i++)
1861 kfree(currentsd[i]);
1864 kfree(physdev_list);
1868 /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1869 * dma mapping and fills in the scatter gather entries of the
1872 static int hpsa_scatter_gather(struct ctlr_info *h,
1873 struct CommandList *cp,
1874 struct scsi_cmnd *cmd)
1877 struct scatterlist *sg;
1879 int use_sg, i, sg_index, chained;
1880 struct SGDescriptor *curr_sg;
1882 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
1884 use_sg = scsi_dma_map(cmd);
1889 goto sglist_finished;
1894 scsi_for_each_sg(cmd, sg, use_sg, i) {
1895 if (i == h->max_cmd_sg_entries - 1 &&
1896 use_sg > h->max_cmd_sg_entries) {
1898 curr_sg = h->cmd_sg_list[cp->cmdindex];
1901 addr64 = (u64) sg_dma_address(sg);
1902 len = sg_dma_len(sg);
1903 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
1904 curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
1906 curr_sg->Ext = 0; /* we are not chaining */
1910 if (use_sg + chained > h->maxSG)
1911 h->maxSG = use_sg + chained;
1914 cp->Header.SGList = h->max_cmd_sg_entries;
1915 cp->Header.SGTotal = (u16) (use_sg + 1);
1916 hpsa_map_sg_chain_block(h, cp);
1922 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
1923 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
1928 static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
1929 void (*done)(struct scsi_cmnd *))
1931 struct ctlr_info *h;
1932 struct hpsa_scsi_dev_t *dev;
1933 unsigned char scsi3addr[8];
1934 struct CommandList *c;
1935 unsigned long flags;
1937 /* Get the ptr to our adapter structure out of cmd->host. */
1938 h = sdev_to_hba(cmd->device);
1939 dev = cmd->device->hostdata;
1941 cmd->result = DID_NO_CONNECT << 16;
1945 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
1947 /* Need a lock as this is being allocated from the pool */
1948 spin_lock_irqsave(&h->lock, flags);
1950 spin_unlock_irqrestore(&h->lock, flags);
1951 if (c == NULL) { /* trouble... */
1952 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
1953 return SCSI_MLQUEUE_HOST_BUSY;
1956 /* Fill in the command list header */
1958 cmd->scsi_done = done; /* save this for use by completion code */
1960 /* save c in case we have to abort it */
1961 cmd->host_scribble = (unsigned char *) c;
1963 c->cmd_type = CMD_SCSI;
1965 c->Header.ReplyQueue = 0; /* unused in simple mode */
1966 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1967 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
1968 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
1970 /* Fill in the request block... */
1972 c->Request.Timeout = 0;
1973 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
1974 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
1975 c->Request.CDBLen = cmd->cmd_len;
1976 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
1977 c->Request.Type.Type = TYPE_CMD;
1978 c->Request.Type.Attribute = ATTR_SIMPLE;
1979 switch (cmd->sc_data_direction) {
1981 c->Request.Type.Direction = XFER_WRITE;
1983 case DMA_FROM_DEVICE:
1984 c->Request.Type.Direction = XFER_READ;
1987 c->Request.Type.Direction = XFER_NONE;
1989 case DMA_BIDIRECTIONAL:
1990 /* This can happen if a buggy application does a scsi passthru
1991 * and sets both inlen and outlen to non-zero. ( see
1992 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1995 c->Request.Type.Direction = XFER_RSVD;
1996 /* This is technically wrong, and hpsa controllers should
1997 * reject it with CMD_INVALID, which is the most correct
1998 * response, but non-fibre backends appear to let it
1999 * slide by, and give the same results as if this field
2000 * were set correctly. Either way is acceptable for
2001 * our purposes here.
2007 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2008 cmd->sc_data_direction);
2013 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
2015 return SCSI_MLQUEUE_HOST_BUSY;
2017 enqueue_cmd_and_start_io(h, c);
2018 /* the cmd'll come back via intr handler in complete_scsi_command() */
2022 static DEF_SCSI_QCMD(hpsa_scsi_queue_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 int hpsa_change_queue_depth(struct scsi_device *sdev,
2067 int qdepth, int reason)
2069 struct ctlr_info *h = sdev_to_hba(sdev);
2071 if (reason != SCSI_QDEPTH_DEFAULT)
2077 if (qdepth > h->nr_cmds)
2078 qdepth = h->nr_cmds;
2079 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2080 return sdev->queue_depth;
2083 static void hpsa_unregister_scsi(struct ctlr_info *h)
2085 /* we are being forcibly unloaded, and may not refuse. */
2086 scsi_remove_host(h->scsi_host);
2087 scsi_host_put(h->scsi_host);
2088 h->scsi_host = NULL;
2091 static int hpsa_register_scsi(struct ctlr_info *h)
2095 rc = hpsa_scsi_detect(h);
2097 dev_err(&h->pdev->dev, "hpsa_register_scsi: failed"
2098 " hpsa_scsi_detect(), rc is %d\n", rc);
2102 static int wait_for_device_to_become_ready(struct ctlr_info *h,
2103 unsigned char lunaddr[])
2107 int waittime = 1; /* seconds */
2108 struct CommandList *c;
2110 c = cmd_special_alloc(h);
2112 dev_warn(&h->pdev->dev, "out of memory in "
2113 "wait_for_device_to_become_ready.\n");
2117 /* Send test unit ready until device ready, or give up. */
2118 while (count < HPSA_TUR_RETRY_LIMIT) {
2120 /* Wait for a bit. do this first, because if we send
2121 * the TUR right away, the reset will just abort it.
2123 msleep(1000 * waittime);
2126 /* Increase wait time with each try, up to a point. */
2127 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
2128 waittime = waittime * 2;
2130 /* Send the Test Unit Ready */
2131 fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD);
2132 hpsa_scsi_do_simple_cmd_core(h, c);
2133 /* no unmap needed here because no data xfer. */
2135 if (c->err_info->CommandStatus == CMD_SUCCESS)
2138 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2139 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
2140 (c->err_info->SenseInfo[2] == NO_SENSE ||
2141 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
2144 dev_warn(&h->pdev->dev, "waiting %d secs "
2145 "for device to become ready.\n", waittime);
2146 rc = 1; /* device not ready. */
2150 dev_warn(&h->pdev->dev, "giving up on device.\n");
2152 dev_warn(&h->pdev->dev, "device is ready.\n");
2154 cmd_special_free(h, c);
2158 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
2159 * complaining. Doing a host- or bus-reset can't do anything good here.
2161 static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
2164 struct ctlr_info *h;
2165 struct hpsa_scsi_dev_t *dev;
2167 /* find the controller to which the command to be aborted was sent */
2168 h = sdev_to_hba(scsicmd->device);
2169 if (h == NULL) /* paranoia */
2171 dev = scsicmd->device->hostdata;
2173 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
2174 "device lookup failed.\n");
2177 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
2178 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
2179 /* send a reset to the SCSI LUN which the command was sent to */
2180 rc = hpsa_send_reset(h, dev->scsi3addr);
2181 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
2184 dev_warn(&h->pdev->dev, "resetting device failed.\n");
2189 * For operations that cannot sleep, a command block is allocated at init,
2190 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
2191 * which ones are free or in use. Lock must be held when calling this.
2192 * cmd_free() is the complement.
2194 static struct CommandList *cmd_alloc(struct ctlr_info *h)
2196 struct CommandList *c;
2198 union u64bit temp64;
2199 dma_addr_t cmd_dma_handle, err_dma_handle;
2202 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
2203 if (i == h->nr_cmds)
2205 } while (test_and_set_bit
2206 (i & (BITS_PER_LONG - 1),
2207 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
2208 c = h->cmd_pool + i;
2209 memset(c, 0, sizeof(*c));
2210 cmd_dma_handle = h->cmd_pool_dhandle
2212 c->err_info = h->errinfo_pool + i;
2213 memset(c->err_info, 0, sizeof(*c->err_info));
2214 err_dma_handle = h->errinfo_pool_dhandle
2215 + i * sizeof(*c->err_info);
2220 INIT_HLIST_NODE(&c->list);
2221 c->busaddr = (u32) cmd_dma_handle;
2222 temp64.val = (u64) err_dma_handle;
2223 c->ErrDesc.Addr.lower = temp64.val32.lower;
2224 c->ErrDesc.Addr.upper = temp64.val32.upper;
2225 c->ErrDesc.Len = sizeof(*c->err_info);
2231 /* For operations that can wait for kmalloc to possibly sleep,
2232 * this routine can be called. Lock need not be held to call
2233 * cmd_special_alloc. cmd_special_free() is the complement.
2235 static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
2237 struct CommandList *c;
2238 union u64bit temp64;
2239 dma_addr_t cmd_dma_handle, err_dma_handle;
2241 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
2244 memset(c, 0, sizeof(*c));
2248 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
2251 if (c->err_info == NULL) {
2252 pci_free_consistent(h->pdev,
2253 sizeof(*c), c, cmd_dma_handle);
2256 memset(c->err_info, 0, sizeof(*c->err_info));
2258 INIT_HLIST_NODE(&c->list);
2259 c->busaddr = (u32) cmd_dma_handle;
2260 temp64.val = (u64) err_dma_handle;
2261 c->ErrDesc.Addr.lower = temp64.val32.lower;
2262 c->ErrDesc.Addr.upper = temp64.val32.upper;
2263 c->ErrDesc.Len = sizeof(*c->err_info);
2269 static void cmd_free(struct ctlr_info *h, struct CommandList *c)
2273 i = c - h->cmd_pool;
2274 clear_bit(i & (BITS_PER_LONG - 1),
2275 h->cmd_pool_bits + (i / BITS_PER_LONG));
2279 static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
2281 union u64bit temp64;
2283 temp64.val32.lower = c->ErrDesc.Addr.lower;
2284 temp64.val32.upper = c->ErrDesc.Addr.upper;
2285 pci_free_consistent(h->pdev, sizeof(*c->err_info),
2286 c->err_info, (dma_addr_t) temp64.val);
2287 pci_free_consistent(h->pdev, sizeof(*c),
2288 c, (dma_addr_t) c->busaddr);
2291 #ifdef CONFIG_COMPAT
2293 static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
2295 IOCTL32_Command_struct __user *arg32 =
2296 (IOCTL32_Command_struct __user *) arg;
2297 IOCTL_Command_struct arg64;
2298 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
2303 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2304 sizeof(arg64.LUN_info));
2305 err |= copy_from_user(&arg64.Request, &arg32->Request,
2306 sizeof(arg64.Request));
2307 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2308 sizeof(arg64.error_info));
2309 err |= get_user(arg64.buf_size, &arg32->buf_size);
2310 err |= get_user(cp, &arg32->buf);
2311 arg64.buf = compat_ptr(cp);
2312 err |= copy_to_user(p, &arg64, sizeof(arg64));
2317 err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
2320 err |= copy_in_user(&arg32->error_info, &p->error_info,
2321 sizeof(arg32->error_info));
2327 static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
2330 BIG_IOCTL32_Command_struct __user *arg32 =
2331 (BIG_IOCTL32_Command_struct __user *) arg;
2332 BIG_IOCTL_Command_struct arg64;
2333 BIG_IOCTL_Command_struct __user *p =
2334 compat_alloc_user_space(sizeof(arg64));
2339 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2340 sizeof(arg64.LUN_info));
2341 err |= copy_from_user(&arg64.Request, &arg32->Request,
2342 sizeof(arg64.Request));
2343 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2344 sizeof(arg64.error_info));
2345 err |= get_user(arg64.buf_size, &arg32->buf_size);
2346 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
2347 err |= get_user(cp, &arg32->buf);
2348 arg64.buf = compat_ptr(cp);
2349 err |= copy_to_user(p, &arg64, sizeof(arg64));
2354 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
2357 err |= copy_in_user(&arg32->error_info, &p->error_info,
2358 sizeof(arg32->error_info));
2364 static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
2367 case CCISS_GETPCIINFO:
2368 case CCISS_GETINTINFO:
2369 case CCISS_SETINTINFO:
2370 case CCISS_GETNODENAME:
2371 case CCISS_SETNODENAME:
2372 case CCISS_GETHEARTBEAT:
2373 case CCISS_GETBUSTYPES:
2374 case CCISS_GETFIRMVER:
2375 case CCISS_GETDRIVVER:
2376 case CCISS_REVALIDVOLS:
2377 case CCISS_DEREGDISK:
2378 case CCISS_REGNEWDISK:
2380 case CCISS_RESCANDISK:
2381 case CCISS_GETLUNINFO:
2382 return hpsa_ioctl(dev, cmd, arg);
2384 case CCISS_PASSTHRU32:
2385 return hpsa_ioctl32_passthru(dev, cmd, arg);
2386 case CCISS_BIG_PASSTHRU32:
2387 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
2390 return -ENOIOCTLCMD;
2395 static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
2397 struct hpsa_pci_info pciinfo;
2401 pciinfo.domain = pci_domain_nr(h->pdev->bus);
2402 pciinfo.bus = h->pdev->bus->number;
2403 pciinfo.dev_fn = h->pdev->devfn;
2404 pciinfo.board_id = h->board_id;
2405 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
2410 static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
2412 DriverVer_type DriverVer;
2413 unsigned char vmaj, vmin, vsubmin;
2416 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
2417 &vmaj, &vmin, &vsubmin);
2419 dev_info(&h->pdev->dev, "driver version string '%s' "
2420 "unrecognized.", HPSA_DRIVER_VERSION);
2425 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
2428 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
2433 static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2435 IOCTL_Command_struct iocommand;
2436 struct CommandList *c;
2438 union u64bit temp64;
2442 if (!capable(CAP_SYS_RAWIO))
2444 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
2446 if ((iocommand.buf_size < 1) &&
2447 (iocommand.Request.Type.Direction != XFER_NONE)) {
2450 if (iocommand.buf_size > 0) {
2451 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
2455 if (iocommand.Request.Type.Direction == XFER_WRITE) {
2456 /* Copy the data into the buffer we created */
2457 if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) {
2462 memset(buff, 0, iocommand.buf_size);
2463 c = cmd_special_alloc(h);
2468 /* Fill in the command type */
2469 c->cmd_type = CMD_IOCTL_PEND;
2470 /* Fill in Command Header */
2471 c->Header.ReplyQueue = 0; /* unused in simple mode */
2472 if (iocommand.buf_size > 0) { /* buffer to fill */
2473 c->Header.SGList = 1;
2474 c->Header.SGTotal = 1;
2475 } else { /* no buffers to fill */
2476 c->Header.SGList = 0;
2477 c->Header.SGTotal = 0;
2479 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
2480 /* use the kernel address the cmd block for tag */
2481 c->Header.Tag.lower = c->busaddr;
2483 /* Fill in Request block */
2484 memcpy(&c->Request, &iocommand.Request,
2485 sizeof(c->Request));
2487 /* Fill in the scatter gather information */
2488 if (iocommand.buf_size > 0) {
2489 temp64.val = pci_map_single(h->pdev, buff,
2490 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
2491 c->SG[0].Addr.lower = temp64.val32.lower;
2492 c->SG[0].Addr.upper = temp64.val32.upper;
2493 c->SG[0].Len = iocommand.buf_size;
2494 c->SG[0].Ext = 0; /* we are not chaining*/
2496 hpsa_scsi_do_simple_cmd_core(h, c);
2497 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
2498 check_ioctl_unit_attention(h, c);
2500 /* Copy the error information out */
2501 memcpy(&iocommand.error_info, c->err_info,
2502 sizeof(iocommand.error_info));
2503 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
2505 cmd_special_free(h, c);
2509 if (iocommand.Request.Type.Direction == XFER_READ) {
2510 /* Copy the data out of the buffer we created */
2511 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
2513 cmd_special_free(h, c);
2518 cmd_special_free(h, c);
2522 static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2524 BIG_IOCTL_Command_struct *ioc;
2525 struct CommandList *c;
2526 unsigned char **buff = NULL;
2527 int *buff_size = NULL;
2528 union u64bit temp64;
2534 BYTE __user *data_ptr;
2538 if (!capable(CAP_SYS_RAWIO))
2540 ioc = (BIG_IOCTL_Command_struct *)
2541 kmalloc(sizeof(*ioc), GFP_KERNEL);
2546 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
2550 if ((ioc->buf_size < 1) &&
2551 (ioc->Request.Type.Direction != XFER_NONE)) {
2555 /* Check kmalloc limits using all SGs */
2556 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
2560 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
2564 buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
2569 buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL);
2574 left = ioc->buf_size;
2575 data_ptr = ioc->buf;
2577 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
2578 buff_size[sg_used] = sz;
2579 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
2580 if (buff[sg_used] == NULL) {
2584 if (ioc->Request.Type.Direction == XFER_WRITE) {
2585 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
2590 memset(buff[sg_used], 0, sz);
2595 c = cmd_special_alloc(h);
2600 c->cmd_type = CMD_IOCTL_PEND;
2601 c->Header.ReplyQueue = 0;
2603 if (ioc->buf_size > 0) {
2604 c->Header.SGList = sg_used;
2605 c->Header.SGTotal = sg_used;
2607 c->Header.SGList = 0;
2608 c->Header.SGTotal = 0;
2610 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
2611 c->Header.Tag.lower = c->busaddr;
2612 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
2613 if (ioc->buf_size > 0) {
2615 for (i = 0; i < sg_used; i++) {
2616 temp64.val = pci_map_single(h->pdev, buff[i],
2617 buff_size[i], PCI_DMA_BIDIRECTIONAL);
2618 c->SG[i].Addr.lower = temp64.val32.lower;
2619 c->SG[i].Addr.upper = temp64.val32.upper;
2620 c->SG[i].Len = buff_size[i];
2621 /* we are not chaining */
2625 hpsa_scsi_do_simple_cmd_core(h, c);
2626 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
2627 check_ioctl_unit_attention(h, c);
2628 /* Copy the error information out */
2629 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
2630 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
2631 cmd_special_free(h, c);
2635 if (ioc->Request.Type.Direction == XFER_READ) {
2636 /* Copy the data out of the buffer we created */
2637 BYTE __user *ptr = ioc->buf;
2638 for (i = 0; i < sg_used; i++) {
2639 if (copy_to_user(ptr, buff[i], buff_size[i])) {
2640 cmd_special_free(h, c);
2644 ptr += buff_size[i];
2647 cmd_special_free(h, c);
2651 for (i = 0; i < sg_used; i++)
2660 static void check_ioctl_unit_attention(struct ctlr_info *h,
2661 struct CommandList *c)
2663 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2664 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
2665 (void) check_for_unit_attention(h, c);
2670 static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
2672 struct ctlr_info *h;
2673 void __user *argp = (void __user *)arg;
2675 h = sdev_to_hba(dev);
2678 case CCISS_DEREGDISK:
2679 case CCISS_REGNEWDISK:
2681 hpsa_scan_start(h->scsi_host);
2683 case CCISS_GETPCIINFO:
2684 return hpsa_getpciinfo_ioctl(h, argp);
2685 case CCISS_GETDRIVVER:
2686 return hpsa_getdrivver_ioctl(h, argp);
2687 case CCISS_PASSTHRU:
2688 return hpsa_passthru_ioctl(h, argp);
2689 case CCISS_BIG_PASSTHRU:
2690 return hpsa_big_passthru_ioctl(h, argp);
2696 static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
2697 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
2700 int pci_dir = XFER_NONE;
2702 c->cmd_type = CMD_IOCTL_PEND;
2703 c->Header.ReplyQueue = 0;
2704 if (buff != NULL && size > 0) {
2705 c->Header.SGList = 1;
2706 c->Header.SGTotal = 1;
2708 c->Header.SGList = 0;
2709 c->Header.SGTotal = 0;
2711 c->Header.Tag.lower = c->busaddr;
2712 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
2714 c->Request.Type.Type = cmd_type;
2715 if (cmd_type == TYPE_CMD) {
2718 /* are we trying to read a vital product page */
2719 if (page_code != 0) {
2720 c->Request.CDB[1] = 0x01;
2721 c->Request.CDB[2] = page_code;
2723 c->Request.CDBLen = 6;
2724 c->Request.Type.Attribute = ATTR_SIMPLE;
2725 c->Request.Type.Direction = XFER_READ;
2726 c->Request.Timeout = 0;
2727 c->Request.CDB[0] = HPSA_INQUIRY;
2728 c->Request.CDB[4] = size & 0xFF;
2730 case HPSA_REPORT_LOG:
2731 case HPSA_REPORT_PHYS:
2732 /* Talking to controller so It's a physical command
2733 mode = 00 target = 0. Nothing to write.
2735 c->Request.CDBLen = 12;
2736 c->Request.Type.Attribute = ATTR_SIMPLE;
2737 c->Request.Type.Direction = XFER_READ;
2738 c->Request.Timeout = 0;
2739 c->Request.CDB[0] = cmd;
2740 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
2741 c->Request.CDB[7] = (size >> 16) & 0xFF;
2742 c->Request.CDB[8] = (size >> 8) & 0xFF;
2743 c->Request.CDB[9] = size & 0xFF;
2745 case HPSA_CACHE_FLUSH:
2746 c->Request.CDBLen = 12;
2747 c->Request.Type.Attribute = ATTR_SIMPLE;
2748 c->Request.Type.Direction = XFER_WRITE;
2749 c->Request.Timeout = 0;
2750 c->Request.CDB[0] = BMIC_WRITE;
2751 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
2753 case TEST_UNIT_READY:
2754 c->Request.CDBLen = 6;
2755 c->Request.Type.Attribute = ATTR_SIMPLE;
2756 c->Request.Type.Direction = XFER_NONE;
2757 c->Request.Timeout = 0;
2760 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
2764 } else if (cmd_type == TYPE_MSG) {
2767 case HPSA_DEVICE_RESET_MSG:
2768 c->Request.CDBLen = 16;
2769 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
2770 c->Request.Type.Attribute = ATTR_SIMPLE;
2771 c->Request.Type.Direction = XFER_NONE;
2772 c->Request.Timeout = 0; /* Don't time out */
2773 c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */
2774 c->Request.CDB[1] = 0x03; /* Reset target above */
2775 /* If bytes 4-7 are zero, it means reset the */
2777 c->Request.CDB[4] = 0x00;
2778 c->Request.CDB[5] = 0x00;
2779 c->Request.CDB[6] = 0x00;
2780 c->Request.CDB[7] = 0x00;
2784 dev_warn(&h->pdev->dev, "unknown message type %d\n",
2789 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
2793 switch (c->Request.Type.Direction) {
2795 pci_dir = PCI_DMA_FROMDEVICE;
2798 pci_dir = PCI_DMA_TODEVICE;
2801 pci_dir = PCI_DMA_NONE;
2804 pci_dir = PCI_DMA_BIDIRECTIONAL;
2807 hpsa_map_one(h->pdev, c, buff, size, pci_dir);
2813 * Map (physical) PCI mem into (virtual) kernel space
2815 static void __iomem *remap_pci_mem(ulong base, ulong size)
2817 ulong page_base = ((ulong) base) & PAGE_MASK;
2818 ulong page_offs = ((ulong) base) - page_base;
2819 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
2821 return page_remapped ? (page_remapped + page_offs) : NULL;
2824 /* Takes cmds off the submission queue and sends them to the hardware,
2825 * then puts them on the queue of cmds waiting for completion.
2827 static void start_io(struct ctlr_info *h)
2829 struct CommandList *c;
2831 while (!hlist_empty(&h->reqQ)) {
2832 c = hlist_entry(h->reqQ.first, struct CommandList, list);
2833 /* can't do anything if fifo is full */
2834 if ((h->access.fifo_full(h))) {
2835 dev_warn(&h->pdev->dev, "fifo full\n");
2839 /* Get the first entry from the Request Q */
2843 /* Tell the controller execute command */
2844 h->access.submit_command(h, c);
2846 /* Put job onto the completed Q */
2851 static inline unsigned long get_next_completion(struct ctlr_info *h)
2853 return h->access.command_completed(h);
2856 static inline bool interrupt_pending(struct ctlr_info *h)
2858 return h->access.intr_pending(h);
2861 static inline long interrupt_not_for_us(struct ctlr_info *h)
2863 return (h->access.intr_pending(h) == 0) ||
2864 (h->interrupts_enabled == 0);
2867 static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
2870 if (unlikely(tag_index >= h->nr_cmds)) {
2871 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
2877 static inline void finish_cmd(struct CommandList *c, u32 raw_tag)
2880 if (likely(c->cmd_type == CMD_SCSI))
2881 complete_scsi_command(c, 0, raw_tag);
2882 else if (c->cmd_type == CMD_IOCTL_PEND)
2883 complete(c->waiting);
2886 static inline u32 hpsa_tag_contains_index(u32 tag)
2888 #define DIRECT_LOOKUP_BIT 0x10
2889 return tag & DIRECT_LOOKUP_BIT;
2892 static inline u32 hpsa_tag_to_index(u32 tag)
2894 #define DIRECT_LOOKUP_SHIFT 5
2895 return tag >> DIRECT_LOOKUP_SHIFT;
2898 static inline u32 hpsa_tag_discard_error_bits(u32 tag)
2900 #define HPSA_ERROR_BITS 0x03
2901 return tag & ~HPSA_ERROR_BITS;
2904 /* process completion of an indexed ("direct lookup") command */
2905 static inline u32 process_indexed_cmd(struct ctlr_info *h,
2909 struct CommandList *c;
2911 tag_index = hpsa_tag_to_index(raw_tag);
2912 if (bad_tag(h, tag_index, raw_tag))
2913 return next_command(h);
2914 c = h->cmd_pool + tag_index;
2915 finish_cmd(c, raw_tag);
2916 return next_command(h);
2919 /* process completion of a non-indexed command */
2920 static inline u32 process_nonindexed_cmd(struct ctlr_info *h,
2924 struct CommandList *c = NULL;
2925 struct hlist_node *tmp;
2927 tag = hpsa_tag_discard_error_bits(raw_tag);
2928 hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
2929 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
2930 finish_cmd(c, raw_tag);
2931 return next_command(h);
2934 bad_tag(h, h->nr_cmds + 1, raw_tag);
2935 return next_command(h);
2938 static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id)
2940 struct ctlr_info *h = dev_id;
2941 unsigned long flags;
2944 if (interrupt_not_for_us(h))
2946 spin_lock_irqsave(&h->lock, flags);
2947 while (interrupt_pending(h)) {
2948 raw_tag = get_next_completion(h);
2949 while (raw_tag != FIFO_EMPTY) {
2950 if (hpsa_tag_contains_index(raw_tag))
2951 raw_tag = process_indexed_cmd(h, raw_tag);
2953 raw_tag = process_nonindexed_cmd(h, raw_tag);
2956 spin_unlock_irqrestore(&h->lock, flags);
2960 static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id)
2962 struct ctlr_info *h = dev_id;
2963 unsigned long flags;
2966 spin_lock_irqsave(&h->lock, flags);
2967 raw_tag = get_next_completion(h);
2968 while (raw_tag != FIFO_EMPTY) {
2969 if (hpsa_tag_contains_index(raw_tag))
2970 raw_tag = process_indexed_cmd(h, raw_tag);
2972 raw_tag = process_nonindexed_cmd(h, raw_tag);
2974 spin_unlock_irqrestore(&h->lock, flags);
2978 /* Send a message CDB to the firmware. */
2979 static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
2983 struct CommandListHeader CommandHeader;
2984 struct RequestBlock Request;
2985 struct ErrDescriptor ErrorDescriptor;
2987 struct Command *cmd;
2988 static const size_t cmd_sz = sizeof(*cmd) +
2989 sizeof(cmd->ErrorDescriptor);
2991 uint32_t paddr32, tag;
2992 void __iomem *vaddr;
2995 vaddr = pci_ioremap_bar(pdev, 0);
2999 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
3000 * CCISS commands, so they must be allocated from the lower 4GiB of
3003 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
3009 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
3015 /* This must fit, because of the 32-bit consistent DMA mask. Also,
3016 * although there's no guarantee, we assume that the address is at
3017 * least 4-byte aligned (most likely, it's page-aligned).
3021 cmd->CommandHeader.ReplyQueue = 0;
3022 cmd->CommandHeader.SGList = 0;
3023 cmd->CommandHeader.SGTotal = 0;
3024 cmd->CommandHeader.Tag.lower = paddr32;
3025 cmd->CommandHeader.Tag.upper = 0;
3026 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
3028 cmd->Request.CDBLen = 16;
3029 cmd->Request.Type.Type = TYPE_MSG;
3030 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
3031 cmd->Request.Type.Direction = XFER_NONE;
3032 cmd->Request.Timeout = 0; /* Don't time out */
3033 cmd->Request.CDB[0] = opcode;
3034 cmd->Request.CDB[1] = type;
3035 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
3036 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
3037 cmd->ErrorDescriptor.Addr.upper = 0;
3038 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
3040 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
3042 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
3043 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
3044 if (hpsa_tag_discard_error_bits(tag) == paddr32)
3046 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
3051 /* we leak the DMA buffer here ... no choice since the controller could
3052 * still complete the command.
3054 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
3055 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
3060 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
3062 if (tag & HPSA_ERROR_BIT) {
3063 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
3068 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
3073 #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
3074 #define hpsa_noop(p) hpsa_message(p, 3, 0)
3076 static __devinit int hpsa_reset_msi(struct pci_dev *pdev)
3078 /* the #defines are stolen from drivers/pci/msi.h. */
3079 #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
3080 #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
3085 pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
3087 pci_read_config_word(pdev, msi_control_reg(pos), &control);
3088 if (control & PCI_MSI_FLAGS_ENABLE) {
3089 dev_info(&pdev->dev, "resetting MSI\n");
3090 pci_write_config_word(pdev, msi_control_reg(pos),
3091 control & ~PCI_MSI_FLAGS_ENABLE);
3095 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3097 pci_read_config_word(pdev, msi_control_reg(pos), &control);
3098 if (control & PCI_MSIX_FLAGS_ENABLE) {
3099 dev_info(&pdev->dev, "resetting MSI-X\n");
3100 pci_write_config_word(pdev, msi_control_reg(pos),
3101 control & ~PCI_MSIX_FLAGS_ENABLE);
3108 static int hpsa_controller_hard_reset(struct pci_dev *pdev,
3109 void * __iomem vaddr, bool use_doorbell)
3115 /* For everything after the P600, the PCI power state method
3116 * of resetting the controller doesn't work, so we have this
3117 * other way using the doorbell register.
3119 dev_info(&pdev->dev, "using doorbell to reset controller\n");
3120 writel(DOORBELL_CTLR_RESET, vaddr + SA5_DOORBELL);
3122 } else { /* Try to do it the PCI power state way */
3124 /* Quoting from the Open CISS Specification: "The Power
3125 * Management Control/Status Register (CSR) controls the power
3126 * state of the device. The normal operating state is D0,
3127 * CSR=00h. The software off state is D3, CSR=03h. To reset
3128 * the controller, place the interface device in D3 then to D0,
3129 * this causes a secondary PCI reset which will reset the
3132 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
3135 "hpsa_reset_controller: "
3136 "PCI PM not supported\n");
3139 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
3140 /* enter the D3hot power management state */
3141 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
3142 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3144 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3148 /* enter the D0 power management state */
3149 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3151 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3158 /* This does a hard reset of the controller using PCI power management
3159 * states or the using the doorbell register.
3161 static __devinit int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
3163 u16 saved_config_space[32];
3166 u64 cfg_base_addr_index;
3167 void __iomem *vaddr;
3168 unsigned long paddr;
3169 u32 misc_fw_support, active_transport;
3171 struct CfgTable __iomem *cfgtable;
3175 /* For controllers as old as the P600, this is very nearly
3178 * pci_save_state(pci_dev);
3179 * pci_set_power_state(pci_dev, PCI_D3hot);
3180 * pci_set_power_state(pci_dev, PCI_D0);
3181 * pci_restore_state(pci_dev);
3183 * but we can't use these nice canned kernel routines on
3184 * kexec, because they also check the MSI/MSI-X state in PCI
3185 * configuration space and do the wrong thing when it is
3186 * set/cleared. Also, the pci_save/restore_state functions
3187 * violate the ordering requirements for restoring the
3188 * configuration space from the CCISS document (see the
3189 * comment below). So we roll our own ....
3191 * For controllers newer than the P600, the pci power state
3192 * method of resetting doesn't work so we have another way
3193 * using the doorbell register.
3196 /* Exclude 640x boards. These are two pci devices in one slot
3197 * which share a battery backed cache module. One controls the
3198 * cache, the other accesses the cache through the one that controls
3199 * it. If we reset the one controlling the cache, the other will
3200 * likely not be happy. Just forbid resetting this conjoined mess.
3201 * The 640x isn't really supported by hpsa anyway.
3203 hpsa_lookup_board_id(pdev, &board_id);
3204 if (board_id == 0x409C0E11 || board_id == 0x409D0E11)
3207 for (i = 0; i < 32; i++)
3208 pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
3211 /* find the first memory BAR, so we can find the cfg table */
3212 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
3215 vaddr = remap_pci_mem(paddr, 0x250);
3219 /* find cfgtable in order to check if reset via doorbell is supported */
3220 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
3221 &cfg_base_addr_index, &cfg_offset);
3224 cfgtable = remap_pci_mem(pci_resource_start(pdev,
3225 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
3231 /* If reset via doorbell register is supported, use that. */
3232 misc_fw_support = readl(&cfgtable->misc_fw_support);
3233 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
3235 /* The doorbell reset seems to cause lockups on some Smart
3236 * Arrays (e.g. P410, P410i, maybe others). Until this is
3237 * fixed or at least isolated, avoid the doorbell reset.
3241 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
3243 goto unmap_cfgtable;
3245 /* Restore the PCI configuration space. The Open CISS
3246 * Specification says, "Restore the PCI Configuration
3247 * Registers, offsets 00h through 60h. It is important to
3248 * restore the command register, 16-bits at offset 04h,
3249 * last. Do not restore the configuration status register,
3250 * 16-bits at offset 06h." Note that the offset is 2*i.
3252 for (i = 0; i < 32; i++) {
3253 if (i == 2 || i == 3)
3255 pci_write_config_word(pdev, 2*i, saved_config_space[i]);
3258 pci_write_config_word(pdev, 4, saved_config_space[2]);
3260 /* Some devices (notably the HP Smart Array 5i Controller)
3261 need a little pause here */
3262 msleep(HPSA_POST_RESET_PAUSE_MSECS);
3264 /* Controller should be in simple mode at this point. If it's not,
3265 * It means we're on one of those controllers which doesn't support
3266 * the doorbell reset method and on which the PCI power management reset
3267 * method doesn't work (P800, for example.)
3268 * In those cases, pretend the reset worked and hope for the best.
3270 active_transport = readl(&cfgtable->TransportActive);
3271 if (active_transport & PERFORMANT_MODE) {
3272 dev_warn(&pdev->dev, "Unable to successfully reset controller,"
3273 " proceeding anyway.\n");
3286 * We cannot read the structure directly, for portability we must use
3288 * This is for debug only.
3290 static void print_cfg_table(struct device *dev, struct CfgTable *tb)
3296 dev_info(dev, "Controller Configuration information\n");
3297 dev_info(dev, "------------------------------------\n");
3298 for (i = 0; i < 4; i++)
3299 temp_name[i] = readb(&(tb->Signature[i]));
3300 temp_name[4] = '\0';
3301 dev_info(dev, " Signature = %s\n", temp_name);
3302 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
3303 dev_info(dev, " Transport methods supported = 0x%x\n",
3304 readl(&(tb->TransportSupport)));
3305 dev_info(dev, " Transport methods active = 0x%x\n",
3306 readl(&(tb->TransportActive)));
3307 dev_info(dev, " Requested transport Method = 0x%x\n",
3308 readl(&(tb->HostWrite.TransportRequest)));
3309 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
3310 readl(&(tb->HostWrite.CoalIntDelay)));
3311 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
3312 readl(&(tb->HostWrite.CoalIntCount)));
3313 dev_info(dev, " Max outstanding commands = 0x%d\n",
3314 readl(&(tb->CmdsOutMax)));
3315 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
3316 for (i = 0; i < 16; i++)
3317 temp_name[i] = readb(&(tb->ServerName[i]));
3318 temp_name[16] = '\0';
3319 dev_info(dev, " Server Name = %s\n", temp_name);
3320 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
3321 readl(&(tb->HeartBeat)));
3322 #endif /* HPSA_DEBUG */
3325 static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
3327 int i, offset, mem_type, bar_type;
3329 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
3332 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3333 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
3334 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
3337 mem_type = pci_resource_flags(pdev, i) &
3338 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
3340 case PCI_BASE_ADDRESS_MEM_TYPE_32:
3341 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
3342 offset += 4; /* 32 bit */
3344 case PCI_BASE_ADDRESS_MEM_TYPE_64:
3347 default: /* reserved in PCI 2.2 */
3348 dev_warn(&pdev->dev,
3349 "base address is invalid\n");
3354 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
3360 /* If MSI/MSI-X is supported by the kernel we will try to enable it on
3361 * controllers that are capable. If not, we use IO-APIC mode.
3364 static void __devinit hpsa_interrupt_mode(struct ctlr_info *h)
3366 #ifdef CONFIG_PCI_MSI
3368 struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1},
3372 /* Some boards advertise MSI but don't really support it */
3373 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
3374 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
3375 goto default_int_mode;
3376 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
3377 dev_info(&h->pdev->dev, "MSIX\n");
3378 err = pci_enable_msix(h->pdev, hpsa_msix_entries, 4);
3380 h->intr[0] = hpsa_msix_entries[0].vector;
3381 h->intr[1] = hpsa_msix_entries[1].vector;
3382 h->intr[2] = hpsa_msix_entries[2].vector;
3383 h->intr[3] = hpsa_msix_entries[3].vector;
3388 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
3389 "available\n", err);
3390 goto default_int_mode;
3392 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
3394 goto default_int_mode;
3397 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
3398 dev_info(&h->pdev->dev, "MSI\n");
3399 if (!pci_enable_msi(h->pdev))
3402 dev_warn(&h->pdev->dev, "MSI init failed\n");
3405 #endif /* CONFIG_PCI_MSI */
3406 /* if we get here we're going to use the default interrupt mode */
3407 h->intr[PERF_MODE_INT] = h->pdev->irq;
3410 static int __devinit hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
3413 u32 subsystem_vendor_id, subsystem_device_id;
3415 subsystem_vendor_id = pdev->subsystem_vendor;
3416 subsystem_device_id = pdev->subsystem_device;
3417 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
3418 subsystem_vendor_id;
3420 for (i = 0; i < ARRAY_SIZE(products); i++)
3421 if (*board_id == products[i].board_id)
3424 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
3425 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
3427 dev_warn(&pdev->dev, "unrecognized board ID: "
3428 "0x%08x, ignoring.\n", *board_id);
3431 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
3434 static inline bool hpsa_board_disabled(struct pci_dev *pdev)
3438 (void) pci_read_config_word(pdev, PCI_COMMAND, &command);
3439 return ((command & PCI_COMMAND_MEMORY) == 0);
3442 static int __devinit hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
3443 unsigned long *memory_bar)
3447 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
3448 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
3449 /* addressing mode bits already removed */
3450 *memory_bar = pci_resource_start(pdev, i);
3451 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
3455 dev_warn(&pdev->dev, "no memory BAR found\n");
3459 static int __devinit hpsa_wait_for_board_ready(struct ctlr_info *h)
3464 for (i = 0; i < HPSA_BOARD_READY_ITERATIONS; i++) {
3465 scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
3466 if (scratchpad == HPSA_FIRMWARE_READY)
3468 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
3470 dev_warn(&h->pdev->dev, "board not ready, timed out.\n");
3474 static int __devinit hpsa_find_cfg_addrs(struct pci_dev *pdev,
3475 void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index,
3478 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
3479 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
3480 *cfg_base_addr &= (u32) 0x0000ffff;
3481 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
3482 if (*cfg_base_addr_index == -1) {
3483 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
3489 static int __devinit hpsa_find_cfgtables(struct ctlr_info *h)
3493 u64 cfg_base_addr_index;
3497 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
3498 &cfg_base_addr_index, &cfg_offset);
3501 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
3502 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
3505 /* Find performant mode table. */
3506 trans_offset = readl(&h->cfgtable->TransMethodOffset);
3507 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
3508 cfg_base_addr_index)+cfg_offset+trans_offset,
3509 sizeof(*h->transtable));
3515 static void __devinit hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
3517 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
3518 if (h->max_commands < 16) {
3519 dev_warn(&h->pdev->dev, "Controller reports "
3520 "max supported commands of %d, an obvious lie. "
3521 "Using 16. Ensure that firmware is up to date.\n",
3523 h->max_commands = 16;
3527 /* Interrogate the hardware for some limits:
3528 * max commands, max SG elements without chaining, and with chaining,
3529 * SG chain block size, etc.
3531 static void __devinit hpsa_find_board_params(struct ctlr_info *h)
3533 hpsa_get_max_perf_mode_cmds(h);
3534 h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
3535 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
3537 * Limit in-command s/g elements to 32 save dma'able memory.
3538 * Howvever spec says if 0, use 31
3540 h->max_cmd_sg_entries = 31;
3541 if (h->maxsgentries > 512) {
3542 h->max_cmd_sg_entries = 32;
3543 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
3544 h->maxsgentries--; /* save one for chain pointer */
3546 h->maxsgentries = 31; /* default to traditional values */
3551 static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
3553 if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
3554 (readb(&h->cfgtable->Signature[1]) != 'I') ||
3555 (readb(&h->cfgtable->Signature[2]) != 'S') ||
3556 (readb(&h->cfgtable->Signature[3]) != 'S')) {
3557 dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
3563 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3564 static inline void hpsa_enable_scsi_prefetch(struct ctlr_info *h)
3569 prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
3571 writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
3575 /* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
3576 * in a prefetch beyond physical memory.
3578 static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
3582 if (h->board_id != 0x3225103C)
3584 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
3585 dma_prefetch |= 0x8000;
3586 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
3589 static void __devinit hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
3593 /* under certain very rare conditions, this can take awhile.
3594 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3595 * as we enter this code.)
3597 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
3598 if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3600 /* delay and try again */
3605 static int __devinit hpsa_enter_simple_mode(struct ctlr_info *h)
3609 trans_support = readl(&(h->cfgtable->TransportSupport));
3610 if (!(trans_support & SIMPLE_MODE))
3613 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
3614 /* Update the field, and then ring the doorbell */
3615 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
3616 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
3617 hpsa_wait_for_mode_change_ack(h);
3618 print_cfg_table(&h->pdev->dev, h->cfgtable);
3619 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
3620 dev_warn(&h->pdev->dev,
3621 "unable to get board into simple mode\n");
3627 static int __devinit hpsa_pci_init(struct ctlr_info *h)
3629 int prod_index, err;
3631 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
3634 h->product_name = products[prod_index].product_name;
3635 h->access = *(products[prod_index].access);
3637 if (hpsa_board_disabled(h->pdev)) {
3638 dev_warn(&h->pdev->dev, "controller appears to be disabled\n");
3641 err = pci_enable_device(h->pdev);
3643 dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
3647 err = pci_request_regions(h->pdev, "hpsa");
3649 dev_err(&h->pdev->dev,
3650 "cannot obtain PCI resources, aborting\n");
3653 hpsa_interrupt_mode(h);
3654 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
3656 goto err_out_free_res;
3657 h->vaddr = remap_pci_mem(h->paddr, 0x250);
3660 goto err_out_free_res;
3662 err = hpsa_wait_for_board_ready(h);
3664 goto err_out_free_res;
3665 err = hpsa_find_cfgtables(h);
3667 goto err_out_free_res;
3668 hpsa_find_board_params(h);
3670 if (!hpsa_CISS_signature_present(h)) {
3672 goto err_out_free_res;
3674 hpsa_enable_scsi_prefetch(h);
3675 hpsa_p600_dma_prefetch_quirk(h);
3676 err = hpsa_enter_simple_mode(h);
3678 goto err_out_free_res;
3683 iounmap(h->transtable);
3685 iounmap(h->cfgtable);
3689 * Deliberately omit pci_disable_device(): it does something nasty to
3690 * Smart Array controllers that pci_enable_device does not undo
3692 pci_release_regions(h->pdev);
3696 static void __devinit hpsa_hba_inquiry(struct ctlr_info *h)
3700 #define HBA_INQUIRY_BYTE_COUNT 64
3701 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
3702 if (!h->hba_inquiry_data)
3704 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
3705 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
3707 kfree(h->hba_inquiry_data);
3708 h->hba_inquiry_data = NULL;
3712 static __devinit int hpsa_init_reset_devices(struct pci_dev *pdev)
3719 /* Reset the controller with a PCI power-cycle or via doorbell */
3720 rc = hpsa_kdump_hard_reset_controller(pdev);
3722 /* -ENOTSUPP here means we cannot reset the controller
3723 * but it's already (and still) up and running in
3724 * "performant mode". Or, it might be 640x, which can't reset
3725 * due to concerns about shared bbwc between 6402/6404 pair.
3727 if (rc == -ENOTSUPP)
3728 return 0; /* just try to do the kdump anyhow. */
3731 if (hpsa_reset_msi(pdev))
3734 /* Now try to get the controller to respond to a no-op */
3735 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
3736 if (hpsa_noop(pdev) == 0)
3739 dev_warn(&pdev->dev, "no-op failed%s\n",
3740 (i < 11 ? "; re-trying" : ""));
3745 static int __devinit hpsa_init_one(struct pci_dev *pdev,
3746 const struct pci_device_id *ent)
3749 struct ctlr_info *h;
3751 if (number_of_controllers == 0)
3752 printk(KERN_INFO DRIVER_NAME "\n");
3754 rc = hpsa_init_reset_devices(pdev);
3758 /* Command structures must be aligned on a 32-byte boundary because
3759 * the 5 lower bits of the address are used by the hardware. and by
3760 * the driver. See comments in hpsa.h for more info.
3762 #define COMMANDLIST_ALIGNMENT 32
3763 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
3764 h = kzalloc(sizeof(*h), GFP_KERNEL);
3769 h->busy_initializing = 1;
3770 INIT_HLIST_HEAD(&h->cmpQ);
3771 INIT_HLIST_HEAD(&h->reqQ);
3772 rc = hpsa_pci_init(h);
3776 sprintf(h->devname, "hpsa%d", number_of_controllers);
3777 h->ctlr = number_of_controllers;
3778 number_of_controllers++;
3780 /* configure PCI DMA stuff */
3781 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3785 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3789 dev_err(&pdev->dev, "no suitable DMA available\n");
3794 /* make sure the board interrupts are off */
3795 h->access.set_intr_mask(h, HPSA_INTR_OFF);
3797 if (h->msix_vector || h->msi_vector)
3798 rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr_msi,
3799 IRQF_DISABLED, h->devname, h);
3801 rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr_intx,
3802 IRQF_DISABLED, h->devname, h);
3804 dev_err(&pdev->dev, "unable to get irq %d for %s\n",
3805 h->intr[PERF_MODE_INT], h->devname);
3809 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
3810 h->devname, pdev->device,
3811 h->intr[PERF_MODE_INT], dac ? "" : " not");
3814 kmalloc(((h->nr_cmds + BITS_PER_LONG -
3815 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
3816 h->cmd_pool = pci_alloc_consistent(h->pdev,
3817 h->nr_cmds * sizeof(*h->cmd_pool),
3818 &(h->cmd_pool_dhandle));
3819 h->errinfo_pool = pci_alloc_consistent(h->pdev,
3820 h->nr_cmds * sizeof(*h->errinfo_pool),
3821 &(h->errinfo_pool_dhandle));
3822 if ((h->cmd_pool_bits == NULL)
3823 || (h->cmd_pool == NULL)
3824 || (h->errinfo_pool == NULL)) {
3825 dev_err(&pdev->dev, "out of memory");
3829 if (hpsa_allocate_sg_chain_blocks(h))
3831 spin_lock_init(&h->lock);
3832 spin_lock_init(&h->scan_lock);
3833 init_waitqueue_head(&h->scan_wait_queue);
3834 h->scan_finished = 1; /* no scan currently in progress */
3836 pci_set_drvdata(pdev, h);
3837 memset(h->cmd_pool_bits, 0,
3838 ((h->nr_cmds + BITS_PER_LONG -
3839 1) / BITS_PER_LONG) * sizeof(unsigned long));
3843 /* Turn the interrupts on so we can service requests */
3844 h->access.set_intr_mask(h, HPSA_INTR_ON);
3846 hpsa_put_ctlr_into_performant_mode(h);
3847 hpsa_hba_inquiry(h);
3848 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
3849 h->busy_initializing = 0;
3853 hpsa_free_sg_chain_blocks(h);
3854 kfree(h->cmd_pool_bits);
3856 pci_free_consistent(h->pdev,
3857 h->nr_cmds * sizeof(struct CommandList),
3858 h->cmd_pool, h->cmd_pool_dhandle);
3859 if (h->errinfo_pool)
3860 pci_free_consistent(h->pdev,
3861 h->nr_cmds * sizeof(struct ErrorInfo),
3863 h->errinfo_pool_dhandle);
3864 free_irq(h->intr[PERF_MODE_INT], h);
3867 h->busy_initializing = 0;
3872 static void hpsa_flush_cache(struct ctlr_info *h)
3875 struct CommandList *c;
3877 flush_buf = kzalloc(4, GFP_KERNEL);
3881 c = cmd_special_alloc(h);
3883 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
3886 fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
3887 RAID_CTLR_LUNID, TYPE_CMD);
3888 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
3889 if (c->err_info->CommandStatus != 0)
3890 dev_warn(&h->pdev->dev,
3891 "error flushing cache on controller\n");
3892 cmd_special_free(h, c);
3897 static void hpsa_shutdown(struct pci_dev *pdev)
3899 struct ctlr_info *h;
3901 h = pci_get_drvdata(pdev);
3902 /* Turn board interrupts off and send the flush cache command
3903 * sendcmd will turn off interrupt, and send the flush...
3904 * To write all data in the battery backed cache to disks
3906 hpsa_flush_cache(h);
3907 h->access.set_intr_mask(h, HPSA_INTR_OFF);
3908 free_irq(h->intr[PERF_MODE_INT], h);
3909 #ifdef CONFIG_PCI_MSI
3911 pci_disable_msix(h->pdev);
3912 else if (h->msi_vector)
3913 pci_disable_msi(h->pdev);
3914 #endif /* CONFIG_PCI_MSI */
3917 static void __devexit hpsa_remove_one(struct pci_dev *pdev)
3919 struct ctlr_info *h;
3921 if (pci_get_drvdata(pdev) == NULL) {
3922 dev_err(&pdev->dev, "unable to remove device \n");
3925 h = pci_get_drvdata(pdev);
3926 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
3927 hpsa_shutdown(pdev);
3929 iounmap(h->transtable);
3930 iounmap(h->cfgtable);
3931 hpsa_free_sg_chain_blocks(h);
3932 pci_free_consistent(h->pdev,
3933 h->nr_cmds * sizeof(struct CommandList),
3934 h->cmd_pool, h->cmd_pool_dhandle);
3935 pci_free_consistent(h->pdev,
3936 h->nr_cmds * sizeof(struct ErrorInfo),
3937 h->errinfo_pool, h->errinfo_pool_dhandle);
3938 pci_free_consistent(h->pdev, h->reply_pool_size,
3939 h->reply_pool, h->reply_pool_dhandle);
3940 kfree(h->cmd_pool_bits);
3941 kfree(h->blockFetchTable);
3942 kfree(h->hba_inquiry_data);
3944 * Deliberately omit pci_disable_device(): it does something nasty to
3945 * Smart Array controllers that pci_enable_device does not undo
3947 pci_release_regions(pdev);
3948 pci_set_drvdata(pdev, NULL);
3952 static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
3953 __attribute__((unused)) pm_message_t state)
3958 static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
3963 static struct pci_driver hpsa_pci_driver = {
3965 .probe = hpsa_init_one,
3966 .remove = __devexit_p(hpsa_remove_one),
3967 .id_table = hpsa_pci_device_id, /* id_table */
3968 .shutdown = hpsa_shutdown,
3969 .suspend = hpsa_suspend,
3970 .resume = hpsa_resume,
3973 /* Fill in bucket_map[], given nsgs (the max number of
3974 * scatter gather elements supported) and bucket[],
3975 * which is an array of 8 integers. The bucket[] array
3976 * contains 8 different DMA transfer sizes (in 16
3977 * byte increments) which the controller uses to fetch
3978 * commands. This function fills in bucket_map[], which
3979 * maps a given number of scatter gather elements to one of
3980 * the 8 DMA transfer sizes. The point of it is to allow the
3981 * controller to only do as much DMA as needed to fetch the
3982 * command, with the DMA transfer size encoded in the lower
3983 * bits of the command address.
3985 static void calc_bucket_map(int bucket[], int num_buckets,
3986 int nsgs, int *bucket_map)
3990 /* even a command with 0 SGs requires 4 blocks */
3991 #define MINIMUM_TRANSFER_BLOCKS 4
3992 #define NUM_BUCKETS 8
3993 /* Note, bucket_map must have nsgs+1 entries. */
3994 for (i = 0; i <= nsgs; i++) {
3995 /* Compute size of a command with i SG entries */
3996 size = i + MINIMUM_TRANSFER_BLOCKS;
3997 b = num_buckets; /* Assume the biggest bucket */
3998 /* Find the bucket that is just big enough */
3999 for (j = 0; j < 8; j++) {
4000 if (bucket[j] >= size) {
4005 /* for a command with i SG entries, use bucket b. */
4010 static __devinit void hpsa_enter_performant_mode(struct ctlr_info *h)
4013 unsigned long register_value;
4015 /* This is a bit complicated. There are 8 registers on
4016 * the controller which we write to to tell it 8 different
4017 * sizes of commands which there may be. It's a way of
4018 * reducing the DMA done to fetch each command. Encoded into
4019 * each command's tag are 3 bits which communicate to the controller
4020 * which of the eight sizes that command fits within. The size of
4021 * each command depends on how many scatter gather entries there are.
4022 * Each SG entry requires 16 bytes. The eight registers are programmed
4023 * with the number of 16-byte blocks a command of that size requires.
4024 * The smallest command possible requires 5 such 16 byte blocks.
4025 * the largest command possible requires MAXSGENTRIES + 4 16-byte
4026 * blocks. Note, this only extends to the SG entries contained
4027 * within the command block, and does not extend to chained blocks
4028 * of SG elements. bft[] contains the eight values we write to
4029 * the registers. They are not evenly distributed, but have more
4030 * sizes for small commands, and fewer sizes for larger commands.
4032 int bft[8] = {5, 6, 8, 10, 12, 20, 28, MAXSGENTRIES + 4};
4033 BUILD_BUG_ON(28 > MAXSGENTRIES + 4);
4034 /* 5 = 1 s/g entry or 4k
4035 * 6 = 2 s/g entry or 8k
4036 * 8 = 4 s/g entry or 16k
4037 * 10 = 6 s/g entry or 24k
4040 h->reply_pool_wraparound = 1; /* spec: init to 1 */
4042 /* Controller spec: zero out this buffer. */
4043 memset(h->reply_pool, 0, h->reply_pool_size);
4044 h->reply_pool_head = h->reply_pool;
4046 bft[7] = h->max_sg_entries + 4;
4047 calc_bucket_map(bft, ARRAY_SIZE(bft), 32, h->blockFetchTable);
4048 for (i = 0; i < 8; i++)
4049 writel(bft[i], &h->transtable->BlockFetch[i]);
4051 /* size of controller ring buffer */
4052 writel(h->max_commands, &h->transtable->RepQSize);
4053 writel(1, &h->transtable->RepQCount);
4054 writel(0, &h->transtable->RepQCtrAddrLow32);
4055 writel(0, &h->transtable->RepQCtrAddrHigh32);
4056 writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32);
4057 writel(0, &h->transtable->RepQAddr0High32);
4058 writel(CFGTBL_Trans_Performant,
4059 &(h->cfgtable->HostWrite.TransportRequest));
4060 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
4061 hpsa_wait_for_mode_change_ack(h);
4062 register_value = readl(&(h->cfgtable->TransportActive));
4063 if (!(register_value & CFGTBL_Trans_Performant)) {
4064 dev_warn(&h->pdev->dev, "unable to get board into"
4065 " performant mode\n");
4070 static __devinit void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
4074 trans_support = readl(&(h->cfgtable->TransportSupport));
4075 if (!(trans_support & PERFORMANT_MODE))
4078 hpsa_get_max_perf_mode_cmds(h);
4079 h->max_sg_entries = 32;
4080 /* Performant mode ring buffer and supporting data structures */
4081 h->reply_pool_size = h->max_commands * sizeof(u64);
4082 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
4083 &(h->reply_pool_dhandle));
4085 /* Need a block fetch table for performant mode */
4086 h->blockFetchTable = kmalloc(((h->max_sg_entries+1) *
4087 sizeof(u32)), GFP_KERNEL);
4089 if ((h->reply_pool == NULL)
4090 || (h->blockFetchTable == NULL))
4093 hpsa_enter_performant_mode(h);
4095 /* Change the access methods to the performant access methods */
4096 h->access = SA5_performant_access;
4097 h->transMethod = CFGTBL_Trans_Performant;
4103 pci_free_consistent(h->pdev, h->reply_pool_size,
4104 h->reply_pool, h->reply_pool_dhandle);
4105 kfree(h->blockFetchTable);
4109 * This is it. Register the PCI driver information for the cards we control
4110 * the OS will call our registered routines when it finds one of our cards.
4112 static int __init hpsa_init(void)
4114 return pci_register_driver(&hpsa_pci_driver);
4117 static void __exit hpsa_cleanup(void)
4119 pci_unregister_driver(&hpsa_pci_driver);
4122 module_init(hpsa_init);
4123 module_exit(hpsa_cleanup);