2 * Disk Array driver for HP Smart Array SAS controllers
3 * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12 * NON INFRINGEMENT. See the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * Questions/Comments/Bugfixes to iss_storagedev@hp.com
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/types.h>
25 #include <linux/pci.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
30 #include <linux/timer.h>
31 #include <linux/seq_file.h>
32 #include <linux/init.h>
33 #include <linux/spinlock.h>
34 #include <linux/smp_lock.h>
35 #include <linux/compat.h>
36 #include <linux/blktrace_api.h>
37 #include <linux/uaccess.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/completion.h>
41 #include <linux/moduleparam.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_cmnd.h>
44 #include <scsi/scsi_device.h>
45 #include <scsi/scsi_host.h>
46 #include <linux/cciss_ioctl.h>
47 #include <linux/string.h>
48 #include <linux/bitmap.h>
49 #include <asm/atomic.h>
50 #include <linux/kthread.h>
54 /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
55 #define HPSA_DRIVER_VERSION "1.0.0"
56 #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
58 /* How long to wait (in milliseconds) for board to go into simple mode */
59 #define MAX_CONFIG_WAIT 30000
60 #define MAX_IOCTL_CONFIG_WAIT 1000
62 /*define how many times we will try a command because of bus resets */
63 #define MAX_CMD_RETRIES 3
65 /* Embedded module documentation macros - see modules.h */
66 MODULE_AUTHOR("Hewlett-Packard Company");
67 MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
69 MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
70 MODULE_VERSION(HPSA_DRIVER_VERSION);
71 MODULE_LICENSE("GPL");
73 static int hpsa_allow_any;
74 module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
75 MODULE_PARM_DESC(hpsa_allow_any,
76 "Allow hpsa driver to access unknown HP Smart Array hardware");
78 /* define the PCI info for the cards we can control */
79 static const struct pci_device_id hpsa_pci_device_id[] = {
80 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
81 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
82 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
83 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
84 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
85 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a},
86 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b},
87 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
88 #define PCI_DEVICE_ID_HP_CISSF 0x333f
89 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x333F},
90 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
91 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
95 MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
97 /* board_id = Subsystem Device ID & Vendor ID
98 * product = Marketing Name for the board
99 * access = Address of the struct of function pointers
101 static struct board_type products[] = {
102 {0x3241103C, "Smart Array P212", &SA5_access},
103 {0x3243103C, "Smart Array P410", &SA5_access},
104 {0x3245103C, "Smart Array P410i", &SA5_access},
105 {0x3247103C, "Smart Array P411", &SA5_access},
106 {0x3249103C, "Smart Array P812", &SA5_access},
107 {0x324a103C, "Smart Array P712m", &SA5_access},
108 {0x324b103C, "Smart Array P711m", &SA5_access},
109 {0x3233103C, "StorageWorks P1210m", &SA5_access},
110 {0x333F103C, "StorageWorks P1210m", &SA5_access},
111 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
114 static int number_of_controllers;
116 static irqreturn_t do_hpsa_intr(int irq, void *dev_id);
117 static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
118 static void start_io(struct ctlr_info *h);
121 static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
124 static void cmd_free(struct ctlr_info *h, struct CommandList *c);
125 static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
126 static struct CommandList *cmd_alloc(struct ctlr_info *h);
127 static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
128 static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
129 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
132 static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
133 void (*done)(struct scsi_cmnd *));
135 static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
136 static int hpsa_slave_alloc(struct scsi_device *sdev);
137 static void hpsa_slave_destroy(struct scsi_device *sdev);
139 static ssize_t raid_level_show(struct device *dev,
140 struct device_attribute *attr, char *buf);
141 static ssize_t lunid_show(struct device *dev,
142 struct device_attribute *attr, char *buf);
143 static ssize_t unique_id_show(struct device *dev,
144 struct device_attribute *attr, char *buf);
145 static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
146 static ssize_t host_store_rescan(struct device *dev,
147 struct device_attribute *attr, const char *buf, size_t count);
148 static int check_for_unit_attention(struct ctlr_info *h,
149 struct CommandList *c);
150 static void check_ioctl_unit_attention(struct ctlr_info *h,
151 struct CommandList *c);
152 /* performant mode helper functions */
153 static void calc_bucket_map(int *bucket, int num_buckets,
154 int nsgs, int *bucket_map);
155 static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
156 static inline u32 next_command(struct ctlr_info *h);
158 static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
159 static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
160 static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
161 static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
163 static struct device_attribute *hpsa_sdev_attrs[] = {
164 &dev_attr_raid_level,
170 static struct device_attribute *hpsa_shost_attrs[] = {
175 static struct scsi_host_template hpsa_driver_template = {
176 .module = THIS_MODULE,
179 .queuecommand = hpsa_scsi_queue_command,
181 .sg_tablesize = MAXSGENTRIES,
182 .use_clustering = ENABLE_CLUSTERING,
183 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
185 .slave_alloc = hpsa_slave_alloc,
186 .slave_destroy = hpsa_slave_destroy,
188 .compat_ioctl = hpsa_compat_ioctl,
190 .sdev_attrs = hpsa_sdev_attrs,
191 .shost_attrs = hpsa_shost_attrs,
194 static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
196 unsigned long *priv = shost_priv(sdev->host);
197 return (struct ctlr_info *) *priv;
200 static struct task_struct *hpsa_scan_thread;
201 static DEFINE_MUTEX(hpsa_scan_mutex);
202 static LIST_HEAD(hpsa_scan_q);
203 static int hpsa_scan_func(void *data);
206 * add_to_scan_list() - add controller to rescan queue
207 * @h: Pointer to the controller.
209 * Adds the controller to the rescan queue if not already on the queue.
211 * returns 1 if added to the queue, 0 if skipped (could be on the
212 * queue already, or the controller could be initializing or shutting
215 static int add_to_scan_list(struct ctlr_info *h)
217 struct ctlr_info *test_h;
221 if (h->busy_initializing)
225 * If we don't get the lock, it means the driver is unloading
226 * and there's no point in scheduling a new scan.
228 if (!mutex_trylock(&h->busy_shutting_down))
231 mutex_lock(&hpsa_scan_mutex);
232 list_for_each_entry(test_h, &hpsa_scan_q, scan_list) {
238 if (!found && !h->busy_scanning) {
239 INIT_COMPLETION(h->scan_wait);
240 list_add_tail(&h->scan_list, &hpsa_scan_q);
243 mutex_unlock(&hpsa_scan_mutex);
244 mutex_unlock(&h->busy_shutting_down);
250 * remove_from_scan_list() - remove controller from rescan queue
251 * @h: Pointer to the controller.
253 * Removes the controller from the rescan queue if present. Blocks if
254 * the controller is currently conducting a rescan. The controller
255 * can be in one of three states:
256 * 1. Doesn't need a scan
257 * 2. On the scan list, but not scanning yet (we remove it)
258 * 3. Busy scanning (and not on the list). In this case we want to wait for
259 * the scan to complete to make sure the scanning thread for this
260 * controller is completely idle.
262 static void remove_from_scan_list(struct ctlr_info *h)
264 struct ctlr_info *test_h, *tmp_h;
266 mutex_lock(&hpsa_scan_mutex);
267 list_for_each_entry_safe(test_h, tmp_h, &hpsa_scan_q, scan_list) {
268 if (test_h == h) { /* state 2. */
269 list_del(&h->scan_list);
270 complete_all(&h->scan_wait);
271 mutex_unlock(&hpsa_scan_mutex);
275 if (h->busy_scanning) { /* state 3. */
276 mutex_unlock(&hpsa_scan_mutex);
277 wait_for_completion(&h->scan_wait);
278 } else { /* state 1, nothing to do. */
279 mutex_unlock(&hpsa_scan_mutex);
283 /* hpsa_scan_func() - kernel thread used to rescan controllers
286 * A kernel thread used scan for drive topology changes on
287 * controllers. The thread processes only one controller at a time
288 * using a queue. Controllers are added to the queue using
289 * add_to_scan_list() and removed from the queue either after done
290 * processing or using remove_from_scan_list().
294 static int hpsa_scan_func(__attribute__((unused)) void *data)
300 set_current_state(TASK_INTERRUPTIBLE);
302 if (kthread_should_stop())
306 mutex_lock(&hpsa_scan_mutex);
307 if (list_empty(&hpsa_scan_q)) {
308 mutex_unlock(&hpsa_scan_mutex);
311 h = list_entry(hpsa_scan_q.next, struct ctlr_info,
313 list_del(&h->scan_list);
314 h->busy_scanning = 1;
315 mutex_unlock(&hpsa_scan_mutex);
316 host_no = h->scsi_host ? h->scsi_host->host_no : -1;
317 hpsa_update_scsi_devices(h, host_no);
318 complete_all(&h->scan_wait);
319 mutex_lock(&hpsa_scan_mutex);
320 h->busy_scanning = 0;
321 mutex_unlock(&hpsa_scan_mutex);
327 static int check_for_unit_attention(struct ctlr_info *h,
328 struct CommandList *c)
330 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
333 switch (c->err_info->SenseInfo[12]) {
335 dev_warn(&h->pdev->dev, "hpsa%d: a state change "
336 "detected, command retried\n", h->ctlr);
339 dev_warn(&h->pdev->dev, "hpsa%d: LUN failure "
340 "detected, action required\n", h->ctlr);
342 case REPORT_LUNS_CHANGED:
343 dev_warn(&h->pdev->dev, "hpsa%d: report LUN data "
344 "changed\n", h->ctlr);
346 * Here, we could call add_to_scan_list and wake up the scan thread,
347 * except that it's quite likely that we will get more than one
348 * REPORT_LUNS_CHANGED condition in quick succession, which means
349 * that those which occur after the first one will likely happen
350 * *during* the hpsa_scan_thread's rescan. And the rescan code is not
351 * robust enough to restart in the middle, undoing what it has already
352 * done, and it's not clear that it's even possible to do this, since
353 * part of what it does is notify the SCSI mid layer, which starts
354 * doing it's own i/o to read partition tables and so on, and the
355 * driver doesn't have visibility to know what might need undoing.
356 * In any event, if possible, it is horribly complicated to get right
357 * so we just don't do it for now.
359 * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
363 dev_warn(&h->pdev->dev, "hpsa%d: a power on "
364 "or device reset detected\n", h->ctlr);
366 case UNIT_ATTENTION_CLEARED:
367 dev_warn(&h->pdev->dev, "hpsa%d: unit attention "
368 "cleared by another initiator\n", h->ctlr);
371 dev_warn(&h->pdev->dev, "hpsa%d: unknown "
372 "unit attention detected\n", h->ctlr);
378 static ssize_t host_store_rescan(struct device *dev,
379 struct device_attribute *attr,
380 const char *buf, size_t count)
383 struct Scsi_Host *shost = class_to_shost(dev);
384 unsigned long *priv = shost_priv(shost);
385 h = (struct ctlr_info *) *priv;
386 if (add_to_scan_list(h)) {
387 wake_up_process(hpsa_scan_thread);
388 wait_for_completion_interruptible(&h->scan_wait);
393 /* Enqueuing and dequeuing functions for cmdlists. */
394 static inline void addQ(struct hlist_head *list, struct CommandList *c)
396 hlist_add_head(&c->list, list);
399 static inline u32 next_command(struct ctlr_info *h)
403 if (unlikely(h->transMethod != CFGTBL_Trans_Performant))
404 return h->access.command_completed(h);
406 if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) {
407 a = *(h->reply_pool_head); /* Next cmd in ring buffer */
408 (h->reply_pool_head)++;
409 h->commands_outstanding--;
413 /* Check for wraparound */
414 if (h->reply_pool_head == (h->reply_pool + h->max_commands)) {
415 h->reply_pool_head = h->reply_pool;
416 h->reply_pool_wraparound ^= 1;
421 /* set_performant_mode: Modify the tag for cciss performant
422 * set bit 0 for pull model, bits 3-1 for block fetch
425 static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
427 if (likely(h->transMethod == CFGTBL_Trans_Performant))
428 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
431 static void enqueue_cmd_and_start_io(struct ctlr_info *h,
432 struct CommandList *c)
436 set_performant_mode(h, c);
437 spin_lock_irqsave(&h->lock, flags);
441 spin_unlock_irqrestore(&h->lock, flags);
444 static inline void removeQ(struct CommandList *c)
446 if (WARN_ON(hlist_unhashed(&c->list)))
448 hlist_del_init(&c->list);
451 static inline int is_hba_lunid(unsigned char scsi3addr[])
453 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
456 static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
458 return (scsi3addr[3] & 0xC0) == 0x40;
461 static inline int is_scsi_rev_5(struct ctlr_info *h)
463 if (!h->hba_inquiry_data)
465 if ((h->hba_inquiry_data[2] & 0x07) == 5)
470 static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
473 #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
475 static ssize_t raid_level_show(struct device *dev,
476 struct device_attribute *attr, char *buf)
479 unsigned char rlevel;
481 struct scsi_device *sdev;
482 struct hpsa_scsi_dev_t *hdev;
485 sdev = to_scsi_device(dev);
486 h = sdev_to_hba(sdev);
487 spin_lock_irqsave(&h->lock, flags);
488 hdev = sdev->hostdata;
490 spin_unlock_irqrestore(&h->lock, flags);
494 /* Is this even a logical drive? */
495 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
496 spin_unlock_irqrestore(&h->lock, flags);
497 l = snprintf(buf, PAGE_SIZE, "N/A\n");
501 rlevel = hdev->raid_level;
502 spin_unlock_irqrestore(&h->lock, flags);
503 if (rlevel > RAID_UNKNOWN)
504 rlevel = RAID_UNKNOWN;
505 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
509 static ssize_t lunid_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
513 struct scsi_device *sdev;
514 struct hpsa_scsi_dev_t *hdev;
516 unsigned char lunid[8];
518 sdev = to_scsi_device(dev);
519 h = sdev_to_hba(sdev);
520 spin_lock_irqsave(&h->lock, flags);
521 hdev = sdev->hostdata;
523 spin_unlock_irqrestore(&h->lock, flags);
526 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
527 spin_unlock_irqrestore(&h->lock, flags);
528 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
529 lunid[0], lunid[1], lunid[2], lunid[3],
530 lunid[4], lunid[5], lunid[6], lunid[7]);
533 static ssize_t unique_id_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
537 struct scsi_device *sdev;
538 struct hpsa_scsi_dev_t *hdev;
540 unsigned char sn[16];
542 sdev = to_scsi_device(dev);
543 h = sdev_to_hba(sdev);
544 spin_lock_irqsave(&h->lock, flags);
545 hdev = sdev->hostdata;
547 spin_unlock_irqrestore(&h->lock, flags);
550 memcpy(sn, hdev->device_id, sizeof(sn));
551 spin_unlock_irqrestore(&h->lock, flags);
552 return snprintf(buf, 16 * 2 + 2,
553 "%02X%02X%02X%02X%02X%02X%02X%02X"
554 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
555 sn[0], sn[1], sn[2], sn[3],
556 sn[4], sn[5], sn[6], sn[7],
557 sn[8], sn[9], sn[10], sn[11],
558 sn[12], sn[13], sn[14], sn[15]);
561 static int hpsa_find_target_lun(struct ctlr_info *h,
562 unsigned char scsi3addr[], int bus, int *target, int *lun)
564 /* finds an unused bus, target, lun for a new physical device
565 * assumes h->devlock is held
568 DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA);
570 memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3);
572 for (i = 0; i < h->ndevices; i++) {
573 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
574 set_bit(h->dev[i]->target, lun_taken);
577 for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) {
578 if (!test_bit(i, lun_taken)) {
589 /* Add an entry into h->dev[] array. */
590 static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
591 struct hpsa_scsi_dev_t *device,
592 struct hpsa_scsi_dev_t *added[], int *nadded)
594 /* assumes h->devlock is held */
597 unsigned char addr1[8], addr2[8];
598 struct hpsa_scsi_dev_t *sd;
600 if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) {
601 dev_err(&h->pdev->dev, "too many devices, some will be "
606 /* physical devices do not have lun or target assigned until now. */
607 if (device->lun != -1)
608 /* Logical device, lun is already assigned. */
611 /* If this device a non-zero lun of a multi-lun device
612 * byte 4 of the 8-byte LUN addr will contain the logical
613 * unit no, zero otherise.
615 if (device->scsi3addr[4] == 0) {
616 /* This is not a non-zero lun of a multi-lun device */
617 if (hpsa_find_target_lun(h, device->scsi3addr,
618 device->bus, &device->target, &device->lun) != 0)
623 /* This is a non-zero lun of a multi-lun device.
624 * Search through our list and find the device which
625 * has the same 8 byte LUN address, excepting byte 4.
626 * Assign the same bus and target for this new LUN.
627 * Use the logical unit number from the firmware.
629 memcpy(addr1, device->scsi3addr, 8);
631 for (i = 0; i < n; i++) {
633 memcpy(addr2, sd->scsi3addr, 8);
635 /* differ only in byte 4? */
636 if (memcmp(addr1, addr2, 8) == 0) {
637 device->bus = sd->bus;
638 device->target = sd->target;
639 device->lun = device->scsi3addr[4];
643 if (device->lun == -1) {
644 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
645 " suspect firmware bug or unsupported hardware "
654 added[*nadded] = device;
657 /* initially, (before registering with scsi layer) we don't
658 * know our hostno and we don't want to print anything first
659 * time anyway (the scsi layer's inquiries will show that info)
661 /* if (hostno != -1) */
662 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
663 scsi_device_type(device->devtype), hostno,
664 device->bus, device->target, device->lun);
668 /* Remove an entry from h->dev[] array. */
669 static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
670 struct hpsa_scsi_dev_t *removed[], int *nremoved)
672 /* assumes h->devlock is held */
674 struct hpsa_scsi_dev_t *sd;
676 BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
679 removed[*nremoved] = h->dev[entry];
682 for (i = entry; i < h->ndevices-1; i++)
683 h->dev[i] = h->dev[i+1];
685 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
686 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
690 #define SCSI3ADDR_EQ(a, b) ( \
691 (a)[7] == (b)[7] && \
692 (a)[6] == (b)[6] && \
693 (a)[5] == (b)[5] && \
694 (a)[4] == (b)[4] && \
695 (a)[3] == (b)[3] && \
696 (a)[2] == (b)[2] && \
697 (a)[1] == (b)[1] && \
700 static void fixup_botched_add(struct ctlr_info *h,
701 struct hpsa_scsi_dev_t *added)
703 /* called when scsi_add_device fails in order to re-adjust
704 * h->dev[] to match the mid layer's view.
709 spin_lock_irqsave(&h->lock, flags);
710 for (i = 0; i < h->ndevices; i++) {
711 if (h->dev[i] == added) {
712 for (j = i; j < h->ndevices-1; j++)
713 h->dev[j] = h->dev[j+1];
718 spin_unlock_irqrestore(&h->lock, flags);
722 static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
723 struct hpsa_scsi_dev_t *dev2)
725 if ((is_logical_dev_addr_mode(dev1->scsi3addr) ||
726 (dev1->lun != -1 && dev2->lun != -1)) &&
727 dev1->devtype != 0x0C)
728 return (memcmp(dev1, dev2, sizeof(*dev1)) == 0);
730 /* we compare everything except lun and target as these
731 * are not yet assigned. Compare parts likely
734 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
735 sizeof(dev1->scsi3addr)) != 0)
737 if (memcmp(dev1->device_id, dev2->device_id,
738 sizeof(dev1->device_id)) != 0)
740 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
742 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
744 if (memcmp(dev1->revision, dev2->revision, sizeof(dev1->revision)) != 0)
746 if (dev1->devtype != dev2->devtype)
748 if (dev1->raid_level != dev2->raid_level)
750 if (dev1->bus != dev2->bus)
755 /* Find needle in haystack. If exact match found, return DEVICE_SAME,
756 * and return needle location in *index. If scsi3addr matches, but not
757 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
758 * location in *index. If needle not found, return DEVICE_NOT_FOUND.
760 static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
761 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
765 #define DEVICE_NOT_FOUND 0
766 #define DEVICE_CHANGED 1
767 #define DEVICE_SAME 2
768 for (i = 0; i < haystack_size; i++) {
769 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
771 if (device_is_the_same(needle, haystack[i]))
774 return DEVICE_CHANGED;
778 return DEVICE_NOT_FOUND;
781 static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
782 struct hpsa_scsi_dev_t *sd[], int nsds)
784 /* sd contains scsi3 addresses and devtypes, and inquiry
785 * data. This function takes what's in sd to be the current
786 * reality and updates h->dev[] to reflect that reality.
788 int i, entry, device_change, changes = 0;
789 struct hpsa_scsi_dev_t *csd;
791 struct hpsa_scsi_dev_t **added, **removed;
792 int nadded, nremoved;
793 struct Scsi_Host *sh = NULL;
795 added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA,
797 removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA,
800 if (!added || !removed) {
801 dev_warn(&h->pdev->dev, "out of memory in "
802 "adjust_hpsa_scsi_table\n");
806 spin_lock_irqsave(&h->devlock, flags);
808 /* find any devices in h->dev[] that are not in
809 * sd[] and remove them from h->dev[], and for any
810 * devices which have changed, remove the old device
811 * info and add the new device info.
816 while (i < h->ndevices) {
818 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
819 if (device_change == DEVICE_NOT_FOUND) {
821 hpsa_scsi_remove_entry(h, hostno, i,
823 continue; /* remove ^^^, hence i not incremented */
824 } else if (device_change == DEVICE_CHANGED) {
826 hpsa_scsi_remove_entry(h, hostno, i,
828 (void) hpsa_scsi_add_entry(h, hostno, sd[entry],
830 /* add can't fail, we just removed one. */
831 sd[entry] = NULL; /* prevent it from being freed */
836 /* Now, make sure every device listed in sd[] is also
837 * listed in h->dev[], adding them if they aren't found
840 for (i = 0; i < nsds; i++) {
841 if (!sd[i]) /* if already added above. */
843 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
844 h->ndevices, &entry);
845 if (device_change == DEVICE_NOT_FOUND) {
847 if (hpsa_scsi_add_entry(h, hostno, sd[i],
848 added, &nadded) != 0)
850 sd[i] = NULL; /* prevent from being freed later. */
851 } else if (device_change == DEVICE_CHANGED) {
852 /* should never happen... */
854 dev_warn(&h->pdev->dev,
855 "device unexpectedly changed.\n");
856 /* but if it does happen, we just ignore that device */
859 spin_unlock_irqrestore(&h->devlock, flags);
861 /* Don't notify scsi mid layer of any changes the first time through
862 * (or if there are no changes) scsi_scan_host will do it later the
863 * first time through.
865 if (hostno == -1 || !changes)
869 /* Notify scsi mid layer of any removed devices */
870 for (i = 0; i < nremoved; i++) {
871 struct scsi_device *sdev =
872 scsi_device_lookup(sh, removed[i]->bus,
873 removed[i]->target, removed[i]->lun);
875 scsi_remove_device(sdev);
876 scsi_device_put(sdev);
878 /* We don't expect to get here.
879 * future cmds to this device will get selection
880 * timeout as if the device was gone.
882 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
883 " for removal.", hostno, removed[i]->bus,
884 removed[i]->target, removed[i]->lun);
890 /* Notify scsi mid layer of any added devices */
891 for (i = 0; i < nadded; i++) {
892 if (scsi_add_device(sh, added[i]->bus,
893 added[i]->target, added[i]->lun) == 0)
895 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
896 "device not added.\n", hostno, added[i]->bus,
897 added[i]->target, added[i]->lun);
898 /* now we have to remove it from h->dev,
899 * since it didn't get added to scsi mid layer
901 fixup_botched_add(h, added[i]);
910 * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
911 * Assume's h->devlock is held.
913 static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
914 int bus, int target, int lun)
917 struct hpsa_scsi_dev_t *sd;
919 for (i = 0; i < h->ndevices; i++) {
921 if (sd->bus == bus && sd->target == target && sd->lun == lun)
927 /* link sdev->hostdata to our per-device structure. */
928 static int hpsa_slave_alloc(struct scsi_device *sdev)
930 struct hpsa_scsi_dev_t *sd;
934 h = sdev_to_hba(sdev);
935 spin_lock_irqsave(&h->devlock, flags);
936 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
937 sdev_id(sdev), sdev->lun);
940 spin_unlock_irqrestore(&h->devlock, flags);
944 static void hpsa_slave_destroy(struct scsi_device *sdev)
949 static void hpsa_scsi_setup(struct ctlr_info *h)
953 spin_lock_init(&h->devlock);
956 static void complete_scsi_command(struct CommandList *cp,
957 int timeout, u32 tag)
959 struct scsi_cmnd *cmd;
961 struct ErrorInfo *ei;
963 unsigned char sense_key;
964 unsigned char asc; /* additional sense code */
965 unsigned char ascq; /* additional sense code qualifier */
968 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
971 scsi_dma_unmap(cmd); /* undo the DMA mappings */
973 cmd->result = (DID_OK << 16); /* host byte */
974 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
975 cmd->result |= (ei->ScsiStatus << 1);
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 cmd->result = DID_NO_CONNECT << 16;
1033 dev_warn(&h->pdev->dev, "cp %p "
1034 "has check condition: unit "
1035 "not ready, manual "
1036 "intervention required\n", cp);
1042 /* Must be some other type of check condition */
1043 dev_warn(&h->pdev->dev, "cp %p has check condition: "
1045 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1046 "Returning result: 0x%x, "
1047 "cmd=[%02x %02x %02x %02x %02x "
1048 "%02x %02x %02x %02x %02x]\n",
1049 cp, sense_key, asc, ascq,
1051 cmd->cmnd[0], cmd->cmnd[1],
1052 cmd->cmnd[2], cmd->cmnd[3],
1053 cmd->cmnd[4], cmd->cmnd[5],
1054 cmd->cmnd[6], cmd->cmnd[7],
1055 cmd->cmnd[8], cmd->cmnd[9]);
1060 /* Problem was not a check condition
1061 * Pass it up to the upper layers...
1063 if (ei->ScsiStatus) {
1064 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1065 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1066 "Returning result: 0x%x\n",
1068 sense_key, asc, ascq,
1070 } else { /* scsi status is zero??? How??? */
1071 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1072 "Returning no connection.\n", cp),
1074 /* Ordinarily, this case should never happen,
1075 * but there is a bug in some released firmware
1076 * revisions that allows it to happen if, for
1077 * example, a 4100 backplane loses power and
1078 * the tape drive is in it. We assume that
1079 * it's a fatal error of some kind because we
1080 * can't show that it wasn't. We will make it
1081 * look like selection timeout since that is
1082 * the most common reason for this to occur,
1083 * and it's severe enough.
1086 cmd->result = DID_NO_CONNECT << 16;
1090 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1092 case CMD_DATA_OVERRUN:
1093 dev_warn(&h->pdev->dev, "cp %p has"
1094 " completed with data overrun "
1098 /* print_bytes(cp, sizeof(*cp), 1, 0);
1100 /* We get CMD_INVALID if you address a non-existent device
1101 * instead of a selection timeout (no response). You will
1102 * see this if you yank out a drive, then try to access it.
1103 * This is kind of a shame because it means that any other
1104 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1105 * missing target. */
1106 cmd->result = DID_NO_CONNECT << 16;
1109 case CMD_PROTOCOL_ERR:
1110 dev_warn(&h->pdev->dev, "cp %p has "
1111 "protocol error \n", cp);
1113 case CMD_HARDWARE_ERR:
1114 cmd->result = DID_ERROR << 16;
1115 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1117 case CMD_CONNECTION_LOST:
1118 cmd->result = DID_ERROR << 16;
1119 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1122 cmd->result = DID_ABORT << 16;
1123 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1124 cp, ei->ScsiStatus);
1126 case CMD_ABORT_FAILED:
1127 cmd->result = DID_ERROR << 16;
1128 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1130 case CMD_UNSOLICITED_ABORT:
1131 cmd->result = DID_RESET << 16;
1132 dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited "
1136 cmd->result = DID_TIME_OUT << 16;
1137 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1140 cmd->result = DID_ERROR << 16;
1141 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1142 cp, ei->CommandStatus);
1144 cmd->scsi_done(cmd);
1148 static int hpsa_scsi_detect(struct ctlr_info *h)
1150 struct Scsi_Host *sh;
1153 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
1160 sh->max_channel = 3;
1161 sh->max_cmd_len = MAX_COMMAND_SIZE;
1162 sh->max_lun = HPSA_MAX_LUN;
1163 sh->max_id = HPSA_MAX_LUN;
1164 sh->can_queue = h->nr_cmds;
1165 sh->cmd_per_lun = h->nr_cmds;
1167 sh->hostdata[0] = (unsigned long) h;
1168 sh->irq = h->intr[PERF_MODE_INT];
1169 sh->unique_id = sh->irq;
1170 error = scsi_add_host(sh, &h->pdev->dev);
1177 dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host"
1178 " failed for controller %d\n", h->ctlr);
1182 dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc"
1183 " failed for controller %d\n", h->ctlr);
1187 static void hpsa_pci_unmap(struct pci_dev *pdev,
1188 struct CommandList *c, int sg_used, int data_direction)
1191 union u64bit addr64;
1193 for (i = 0; i < sg_used; i++) {
1194 addr64.val32.lower = c->SG[i].Addr.lower;
1195 addr64.val32.upper = c->SG[i].Addr.upper;
1196 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1201 static void hpsa_map_one(struct pci_dev *pdev,
1202 struct CommandList *cp,
1209 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1210 cp->Header.SGList = 0;
1211 cp->Header.SGTotal = 0;
1215 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
1216 cp->SG[0].Addr.lower =
1217 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
1218 cp->SG[0].Addr.upper =
1219 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
1220 cp->SG[0].Len = buflen;
1221 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1222 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
1225 static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1226 struct CommandList *c)
1228 DECLARE_COMPLETION_ONSTACK(wait);
1231 enqueue_cmd_and_start_io(h, c);
1232 wait_for_completion(&wait);
1235 static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1236 struct CommandList *c, int data_direction)
1238 int retry_count = 0;
1241 memset(c->err_info, 0, sizeof(c->err_info));
1242 hpsa_scsi_do_simple_cmd_core(h, c);
1244 } while (check_for_unit_attention(h, c) && retry_count <= 3);
1245 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1248 static void hpsa_scsi_interpret_error(struct CommandList *cp)
1250 struct ErrorInfo *ei;
1251 struct device *d = &cp->h->pdev->dev;
1254 switch (ei->CommandStatus) {
1255 case CMD_TARGET_STATUS:
1256 dev_warn(d, "cmd %p has completed with errors\n", cp);
1257 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1259 if (ei->ScsiStatus == 0)
1260 dev_warn(d, "SCSI status is abnormally zero. "
1261 "(probably indicates selection timeout "
1262 "reported incorrectly due to a known "
1263 "firmware bug, circa July, 2001.)\n");
1265 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1266 dev_info(d, "UNDERRUN\n");
1268 case CMD_DATA_OVERRUN:
1269 dev_warn(d, "cp %p has completed with data overrun\n", cp);
1272 /* controller unfortunately reports SCSI passthru's
1273 * to non-existent targets as invalid commands.
1275 dev_warn(d, "cp %p is reported invalid (probably means "
1276 "target device no longer present)\n", cp);
1277 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1281 case CMD_PROTOCOL_ERR:
1282 dev_warn(d, "cp %p has protocol error \n", cp);
1284 case CMD_HARDWARE_ERR:
1285 /* cmd->result = DID_ERROR << 16; */
1286 dev_warn(d, "cp %p had hardware error\n", cp);
1288 case CMD_CONNECTION_LOST:
1289 dev_warn(d, "cp %p had connection lost\n", cp);
1292 dev_warn(d, "cp %p was aborted\n", cp);
1294 case CMD_ABORT_FAILED:
1295 dev_warn(d, "cp %p reports abort failed\n", cp);
1297 case CMD_UNSOLICITED_ABORT:
1298 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1301 dev_warn(d, "cp %p timed out\n", cp);
1304 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1309 static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1310 unsigned char page, unsigned char *buf,
1311 unsigned char bufsize)
1314 struct CommandList *c;
1315 struct ErrorInfo *ei;
1317 c = cmd_special_alloc(h);
1319 if (c == NULL) { /* trouble... */
1320 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1324 fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD);
1325 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1327 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1328 hpsa_scsi_interpret_error(c);
1331 cmd_special_free(h, c);
1335 static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
1338 struct CommandList *c;
1339 struct ErrorInfo *ei;
1341 c = cmd_special_alloc(h);
1343 if (c == NULL) { /* trouble... */
1344 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1348 fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG);
1349 hpsa_scsi_do_simple_cmd_core(h, c);
1350 /* no unmap needed here because no data xfer. */
1353 if (ei->CommandStatus != 0) {
1354 hpsa_scsi_interpret_error(c);
1357 cmd_special_free(h, c);
1361 static void hpsa_get_raid_level(struct ctlr_info *h,
1362 unsigned char *scsi3addr, unsigned char *raid_level)
1367 *raid_level = RAID_UNKNOWN;
1368 buf = kzalloc(64, GFP_KERNEL);
1371 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1373 *raid_level = buf[8];
1374 if (*raid_level > RAID_UNKNOWN)
1375 *raid_level = RAID_UNKNOWN;
1380 /* Get the device id from inquiry page 0x83 */
1381 static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
1382 unsigned char *device_id, int buflen)
1389 buf = kzalloc(64, GFP_KERNEL);
1392 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1394 memcpy(device_id, &buf[8], buflen);
1399 static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
1400 struct ReportLUNdata *buf, int bufsize,
1401 int extended_response)
1404 struct CommandList *c;
1405 unsigned char scsi3addr[8];
1406 struct ErrorInfo *ei;
1408 c = cmd_special_alloc(h);
1409 if (c == NULL) { /* trouble... */
1410 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1413 /* address the controller */
1414 memset(scsi3addr, 0, sizeof(scsi3addr));
1415 fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
1416 buf, bufsize, 0, scsi3addr, TYPE_CMD);
1417 if (extended_response)
1418 c->Request.CDB[1] = extended_response;
1419 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1421 if (ei->CommandStatus != 0 &&
1422 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1423 hpsa_scsi_interpret_error(c);
1426 cmd_special_free(h, c);
1430 static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
1431 struct ReportLUNdata *buf,
1432 int bufsize, int extended_response)
1434 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
1437 static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
1438 struct ReportLUNdata *buf, int bufsize)
1440 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
1443 static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
1444 int bus, int target, int lun)
1447 device->target = target;
1451 static int hpsa_update_device_info(struct ctlr_info *h,
1452 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device)
1454 #define OBDR_TAPE_INQ_SIZE 49
1455 unsigned char *inq_buff;
1457 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1461 /* Do an inquiry to the device to see what it is. */
1462 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1463 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1464 /* Inquiry failed (msg printed already) */
1465 dev_err(&h->pdev->dev,
1466 "hpsa_update_device_info: inquiry failed\n");
1470 /* As a side effect, record the firmware version number
1471 * if we happen to be talking to the RAID controller.
1473 if (is_hba_lunid(scsi3addr))
1474 memcpy(h->firm_ver, &inq_buff[32], 4);
1476 this_device->devtype = (inq_buff[0] & 0x1f);
1477 memcpy(this_device->scsi3addr, scsi3addr, 8);
1478 memcpy(this_device->vendor, &inq_buff[8],
1479 sizeof(this_device->vendor));
1480 memcpy(this_device->model, &inq_buff[16],
1481 sizeof(this_device->model));
1482 memcpy(this_device->revision, &inq_buff[32],
1483 sizeof(this_device->revision));
1484 memset(this_device->device_id, 0,
1485 sizeof(this_device->device_id));
1486 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
1487 sizeof(this_device->device_id));
1489 if (this_device->devtype == TYPE_DISK &&
1490 is_logical_dev_addr_mode(scsi3addr))
1491 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
1493 this_device->raid_level = RAID_UNKNOWN;
1503 static unsigned char *msa2xxx_model[] = {
1511 static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1515 for (i = 0; msa2xxx_model[i]; i++)
1516 if (strncmp(device->model, msa2xxx_model[i],
1517 strlen(msa2xxx_model[i])) == 0)
1522 /* Helper function to assign bus, target, lun mapping of devices.
1523 * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
1524 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
1525 * Logical drive target and lun are assigned at this time, but
1526 * physical device lun and target assignment are deferred (assigned
1527 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
1529 static void figure_bus_target_lun(struct ctlr_info *h,
1530 u8 *lunaddrbytes, int *bus, int *target, int *lun,
1531 struct hpsa_scsi_dev_t *device)
1535 if (is_logical_dev_addr_mode(lunaddrbytes)) {
1536 /* logical device */
1537 if (unlikely(is_scsi_rev_5(h))) {
1538 /* p1210m, logical drives lun assignments
1539 * match SCSI REPORT LUNS data.
1541 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
1544 *lun = (lunid & 0x3fff) + 1;
1547 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
1548 if (is_msa2xxx(h, device)) {
1549 /* msa2xxx way, put logicals on bus 1
1550 * and match target/lun numbers box
1554 *target = (lunid >> 16) & 0x3fff;
1555 *lun = lunid & 0x00ff;
1557 /* Traditional smart array way. */
1560 *target = lunid & 0x3fff;
1564 /* physical device */
1565 if (is_hba_lunid(lunaddrbytes))
1566 if (unlikely(is_scsi_rev_5(h))) {
1567 *bus = 0; /* put p1210m ctlr at 0,0,0 */
1572 *bus = 3; /* traditional smartarray */
1574 *bus = 2; /* physical disk */
1576 *lun = -1; /* we will fill these in later. */
1581 * If there is no lun 0 on a target, linux won't find any devices.
1582 * For the MSA2xxx boxes, we have to manually detect the enclosure
1583 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
1584 * it for some reason. *tmpdevice is the target we're adding,
1585 * this_device is a pointer into the current element of currentsd[]
1586 * that we're building up in update_scsi_devices(), below.
1587 * lunzerobits is a bitmap that tracks which targets already have a
1589 * Returns 1 if an enclosure was added, 0 if not.
1591 static int add_msa2xxx_enclosure_device(struct ctlr_info *h,
1592 struct hpsa_scsi_dev_t *tmpdevice,
1593 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
1594 int bus, int target, int lun, unsigned long lunzerobits[],
1595 int *nmsa2xxx_enclosures)
1597 unsigned char scsi3addr[8];
1599 if (test_bit(target, lunzerobits))
1600 return 0; /* There is already a lun 0 on this target. */
1602 if (!is_logical_dev_addr_mode(lunaddrbytes))
1603 return 0; /* It's the logical targets that may lack lun 0. */
1605 if (!is_msa2xxx(h, tmpdevice))
1606 return 0; /* It's only the MSA2xxx that have this problem. */
1608 if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */
1611 if (is_hba_lunid(scsi3addr))
1612 return 0; /* Don't add the RAID controller here. */
1614 if (is_scsi_rev_5(h))
1615 return 0; /* p1210m doesn't need to do this. */
1617 #define MAX_MSA2XXX_ENCLOSURES 32
1618 if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) {
1619 dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX "
1620 "enclosures exceeded. Check your hardware "
1625 memset(scsi3addr, 0, 8);
1626 scsi3addr[3] = target;
1627 if (hpsa_update_device_info(h, scsi3addr, this_device))
1629 (*nmsa2xxx_enclosures)++;
1630 hpsa_set_bus_target_lun(this_device, bus, target, 0);
1631 set_bit(target, lunzerobits);
1636 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
1637 * logdev. The number of luns in physdev and logdev are returned in
1638 * *nphysicals and *nlogicals, respectively.
1639 * Returns 0 on success, -1 otherwise.
1641 static int hpsa_gather_lun_info(struct ctlr_info *h,
1643 struct ReportLUNdata *physdev, u32 *nphysicals,
1644 struct ReportLUNdata *logdev, u32 *nlogicals)
1646 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
1647 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
1650 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8;
1651 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
1652 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
1653 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1654 *nphysicals - HPSA_MAX_PHYS_LUN);
1655 *nphysicals = HPSA_MAX_PHYS_LUN;
1657 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
1658 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
1661 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
1662 /* Reject Logicals in excess of our max capability. */
1663 if (*nlogicals > HPSA_MAX_LUN) {
1664 dev_warn(&h->pdev->dev,
1665 "maximum logical LUNs (%d) exceeded. "
1666 "%d LUNs ignored.\n", HPSA_MAX_LUN,
1667 *nlogicals - HPSA_MAX_LUN);
1668 *nlogicals = HPSA_MAX_LUN;
1670 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
1671 dev_warn(&h->pdev->dev,
1672 "maximum logical + physical LUNs (%d) exceeded. "
1673 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1674 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
1675 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
1680 u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
1681 int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list,
1682 struct ReportLUNdata *logdev_list)
1684 /* Helper function, figure out where the LUN ID info is coming from
1685 * given index i, lists of physical and logical devices, where in
1686 * the list the raid controller is supposed to appear (first or last)
1689 int logicals_start = nphysicals + (raid_ctlr_position == 0);
1690 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
1692 if (i == raid_ctlr_position)
1693 return RAID_CTLR_LUNID;
1695 if (i < logicals_start)
1696 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
1698 if (i < last_device)
1699 return &logdev_list->LUN[i - nphysicals -
1700 (raid_ctlr_position == 0)][0];
1705 static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
1707 /* the idea here is we could get notified
1708 * that some devices have changed, so we do a report
1709 * physical luns and report logical luns cmd, and adjust
1710 * our list of devices accordingly.
1712 * The scsi3addr's of devices won't change so long as the
1713 * adapter is not reset. That means we can rescan and
1714 * tell which devices we already know about, vs. new
1715 * devices, vs. disappearing devices.
1717 struct ReportLUNdata *physdev_list = NULL;
1718 struct ReportLUNdata *logdev_list = NULL;
1719 unsigned char *inq_buff = NULL;
1722 u32 ndev_allocated = 0;
1723 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
1725 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
1726 int i, nmsa2xxx_enclosures, ndevs_to_allocate;
1727 int bus, target, lun;
1728 int raid_ctlr_position;
1729 DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR);
1731 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA,
1733 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1734 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1735 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1736 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
1738 if (!currentsd || !physdev_list || !logdev_list ||
1739 !inq_buff || !tmpdevice) {
1740 dev_err(&h->pdev->dev, "out of memory\n");
1743 memset(lunzerobits, 0, sizeof(lunzerobits));
1745 if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
1746 logdev_list, &nlogicals))
1749 /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
1750 * but each of them 4 times through different paths. The plus 1
1751 * is for the RAID controller.
1753 ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1;
1755 /* Allocate the per device structures */
1756 for (i = 0; i < ndevs_to_allocate; i++) {
1757 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
1758 if (!currentsd[i]) {
1759 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
1760 __FILE__, __LINE__);
1766 if (unlikely(is_scsi_rev_5(h)))
1767 raid_ctlr_position = 0;
1769 raid_ctlr_position = nphysicals + nlogicals;
1771 /* adjust our table of devices */
1772 nmsa2xxx_enclosures = 0;
1773 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
1776 /* Figure out where the LUN ID info is coming from */
1777 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
1778 i, nphysicals, nlogicals, physdev_list, logdev_list);
1779 /* skip masked physical devices. */
1780 if (lunaddrbytes[3] & 0xC0 &&
1781 i < nphysicals + (raid_ctlr_position == 0))
1784 /* Get device type, vendor, model, device id */
1785 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice))
1786 continue; /* skip it if we can't talk to it. */
1787 figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun,
1789 this_device = currentsd[ncurrent];
1792 * For the msa2xxx boxes, we have to insert a LUN 0 which
1793 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
1794 * is nonetheless an enclosure device there. We have to
1795 * present that otherwise linux won't find anything if
1796 * there is no lun 0.
1798 if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device,
1799 lunaddrbytes, bus, target, lun, lunzerobits,
1800 &nmsa2xxx_enclosures)) {
1802 this_device = currentsd[ncurrent];
1805 *this_device = *tmpdevice;
1806 hpsa_set_bus_target_lun(this_device, bus, target, lun);
1808 switch (this_device->devtype) {
1810 /* We don't *really* support actual CD-ROM devices,
1811 * just "One Button Disaster Recovery" tape drive
1812 * which temporarily pretends to be a CD-ROM drive.
1813 * So we check that the device is really an OBDR tape
1814 * device by checking for "$DR-10" in bytes 43-48 of
1818 #define OBDR_TAPE_SIG "$DR-10"
1819 strncpy(obdr_sig, &inq_buff[43], 6);
1821 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1822 /* Not OBDR device, ignore it. */
1833 case TYPE_MEDIUM_CHANGER:
1837 /* Only present the Smartarray HBA as a RAID controller.
1838 * If it's a RAID controller other than the HBA itself
1839 * (an external RAID controller, MSA500 or similar)
1842 if (!is_hba_lunid(lunaddrbytes))
1849 if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA)
1852 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
1855 for (i = 0; i < ndev_allocated; i++)
1856 kfree(currentsd[i]);
1859 kfree(physdev_list);
1863 /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1864 * dma mapping and fills in the scatter gather entries of the
1867 static int hpsa_scatter_gather(struct pci_dev *pdev,
1868 struct CommandList *cp,
1869 struct scsi_cmnd *cmd)
1872 struct scatterlist *sg;
1876 BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
1878 use_sg = scsi_dma_map(cmd);
1883 goto sglist_finished;
1885 scsi_for_each_sg(cmd, sg, use_sg, i) {
1886 addr64 = (u64) sg_dma_address(sg);
1887 len = sg_dma_len(sg);
1888 cp->SG[i].Addr.lower =
1889 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
1890 cp->SG[i].Addr.upper =
1891 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
1892 cp->SG[i].Len = len;
1893 cp->SG[i].Ext = 0; /* we are not chaining */
1898 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
1899 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
1904 static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
1905 void (*done)(struct scsi_cmnd *))
1907 struct ctlr_info *h;
1908 struct hpsa_scsi_dev_t *dev;
1909 unsigned char scsi3addr[8];
1910 struct CommandList *c;
1911 unsigned long flags;
1913 /* Get the ptr to our adapter structure out of cmd->host. */
1914 h = sdev_to_hba(cmd->device);
1915 dev = cmd->device->hostdata;
1917 cmd->result = DID_NO_CONNECT << 16;
1921 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
1923 /* Need a lock as this is being allocated from the pool */
1924 spin_lock_irqsave(&h->lock, flags);
1926 spin_unlock_irqrestore(&h->lock, flags);
1927 if (c == NULL) { /* trouble... */
1928 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
1929 return SCSI_MLQUEUE_HOST_BUSY;
1932 /* Fill in the command list header */
1934 cmd->scsi_done = done; /* save this for use by completion code */
1936 /* save c in case we have to abort it */
1937 cmd->host_scribble = (unsigned char *) c;
1939 c->cmd_type = CMD_SCSI;
1941 c->Header.ReplyQueue = 0; /* unused in simple mode */
1942 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1943 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
1944 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
1946 /* Fill in the request block... */
1948 c->Request.Timeout = 0;
1949 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
1950 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
1951 c->Request.CDBLen = cmd->cmd_len;
1952 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
1953 c->Request.Type.Type = TYPE_CMD;
1954 c->Request.Type.Attribute = ATTR_SIMPLE;
1955 switch (cmd->sc_data_direction) {
1957 c->Request.Type.Direction = XFER_WRITE;
1959 case DMA_FROM_DEVICE:
1960 c->Request.Type.Direction = XFER_READ;
1963 c->Request.Type.Direction = XFER_NONE;
1965 case DMA_BIDIRECTIONAL:
1966 /* This can happen if a buggy application does a scsi passthru
1967 * and sets both inlen and outlen to non-zero. ( see
1968 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1971 c->Request.Type.Direction = XFER_RSVD;
1972 /* This is technically wrong, and hpsa controllers should
1973 * reject it with CMD_INVALID, which is the most correct
1974 * response, but non-fibre backends appear to let it
1975 * slide by, and give the same results as if this field
1976 * were set correctly. Either way is acceptable for
1977 * our purposes here.
1983 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
1984 cmd->sc_data_direction);
1989 if (hpsa_scatter_gather(h->pdev, c, cmd) < 0) { /* Fill SG list */
1991 return SCSI_MLQUEUE_HOST_BUSY;
1993 enqueue_cmd_and_start_io(h, c);
1994 /* the cmd'll come back via intr handler in complete_scsi_command() */
1998 static void hpsa_unregister_scsi(struct ctlr_info *h)
2000 /* we are being forcibly unloaded, and may not refuse. */
2001 scsi_remove_host(h->scsi_host);
2002 scsi_host_put(h->scsi_host);
2003 h->scsi_host = NULL;
2006 static int hpsa_register_scsi(struct ctlr_info *h)
2010 hpsa_update_scsi_devices(h, -1);
2011 rc = hpsa_scsi_detect(h);
2013 dev_err(&h->pdev->dev, "hpsa_register_scsi: failed"
2014 " hpsa_scsi_detect(), rc is %d\n", rc);
2018 static int wait_for_device_to_become_ready(struct ctlr_info *h,
2019 unsigned char lunaddr[])
2023 int waittime = 1; /* seconds */
2024 struct CommandList *c;
2026 c = cmd_special_alloc(h);
2028 dev_warn(&h->pdev->dev, "out of memory in "
2029 "wait_for_device_to_become_ready.\n");
2033 /* Send test unit ready until device ready, or give up. */
2034 while (count < HPSA_TUR_RETRY_LIMIT) {
2036 /* Wait for a bit. do this first, because if we send
2037 * the TUR right away, the reset will just abort it.
2039 msleep(1000 * waittime);
2042 /* Increase wait time with each try, up to a point. */
2043 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
2044 waittime = waittime * 2;
2046 /* Send the Test Unit Ready */
2047 fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD);
2048 hpsa_scsi_do_simple_cmd_core(h, c);
2049 /* no unmap needed here because no data xfer. */
2051 if (c->err_info->CommandStatus == CMD_SUCCESS)
2054 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2055 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
2056 (c->err_info->SenseInfo[2] == NO_SENSE ||
2057 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
2060 dev_warn(&h->pdev->dev, "waiting %d secs "
2061 "for device to become ready.\n", waittime);
2062 rc = 1; /* device not ready. */
2066 dev_warn(&h->pdev->dev, "giving up on device.\n");
2068 dev_warn(&h->pdev->dev, "device is ready.\n");
2070 cmd_special_free(h, c);
2074 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
2075 * complaining. Doing a host- or bus-reset can't do anything good here.
2077 static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
2080 struct ctlr_info *h;
2081 struct hpsa_scsi_dev_t *dev;
2083 /* find the controller to which the command to be aborted was sent */
2084 h = sdev_to_hba(scsicmd->device);
2085 if (h == NULL) /* paranoia */
2087 dev_warn(&h->pdev->dev, "resetting drive\n");
2089 dev = scsicmd->device->hostdata;
2091 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
2092 "device lookup failed.\n");
2095 /* send a reset to the SCSI LUN which the command was sent to */
2096 rc = hpsa_send_reset(h, dev->scsi3addr);
2097 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
2100 dev_warn(&h->pdev->dev, "resetting device failed.\n");
2105 * For operations that cannot sleep, a command block is allocated at init,
2106 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
2107 * which ones are free or in use. Lock must be held when calling this.
2108 * cmd_free() is the complement.
2110 static struct CommandList *cmd_alloc(struct ctlr_info *h)
2112 struct CommandList *c;
2114 union u64bit temp64;
2115 dma_addr_t cmd_dma_handle, err_dma_handle;
2118 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
2119 if (i == h->nr_cmds)
2121 } while (test_and_set_bit
2122 (i & (BITS_PER_LONG - 1),
2123 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
2124 c = h->cmd_pool + i;
2125 memset(c, 0, sizeof(*c));
2126 cmd_dma_handle = h->cmd_pool_dhandle
2128 c->err_info = h->errinfo_pool + i;
2129 memset(c->err_info, 0, sizeof(*c->err_info));
2130 err_dma_handle = h->errinfo_pool_dhandle
2131 + i * sizeof(*c->err_info);
2136 INIT_HLIST_NODE(&c->list);
2137 c->busaddr = (u32) cmd_dma_handle;
2138 temp64.val = (u64) err_dma_handle;
2139 c->ErrDesc.Addr.lower = temp64.val32.lower;
2140 c->ErrDesc.Addr.upper = temp64.val32.upper;
2141 c->ErrDesc.Len = sizeof(*c->err_info);
2147 /* For operations that can wait for kmalloc to possibly sleep,
2148 * this routine can be called. Lock need not be held to call
2149 * cmd_special_alloc. cmd_special_free() is the complement.
2151 static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
2153 struct CommandList *c;
2154 union u64bit temp64;
2155 dma_addr_t cmd_dma_handle, err_dma_handle;
2157 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
2160 memset(c, 0, sizeof(*c));
2164 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
2167 if (c->err_info == NULL) {
2168 pci_free_consistent(h->pdev,
2169 sizeof(*c), c, cmd_dma_handle);
2172 memset(c->err_info, 0, sizeof(*c->err_info));
2174 INIT_HLIST_NODE(&c->list);
2175 c->busaddr = (u32) cmd_dma_handle;
2176 temp64.val = (u64) err_dma_handle;
2177 c->ErrDesc.Addr.lower = temp64.val32.lower;
2178 c->ErrDesc.Addr.upper = temp64.val32.upper;
2179 c->ErrDesc.Len = sizeof(*c->err_info);
2185 static void cmd_free(struct ctlr_info *h, struct CommandList *c)
2189 i = c - h->cmd_pool;
2190 clear_bit(i & (BITS_PER_LONG - 1),
2191 h->cmd_pool_bits + (i / BITS_PER_LONG));
2195 static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
2197 union u64bit temp64;
2199 temp64.val32.lower = c->ErrDesc.Addr.lower;
2200 temp64.val32.upper = c->ErrDesc.Addr.upper;
2201 pci_free_consistent(h->pdev, sizeof(*c->err_info),
2202 c->err_info, (dma_addr_t) temp64.val);
2203 pci_free_consistent(h->pdev, sizeof(*c),
2204 c, (dma_addr_t) c->busaddr);
2207 #ifdef CONFIG_COMPAT
2209 static int do_ioctl(struct scsi_device *dev, int cmd, void *arg)
2214 ret = hpsa_ioctl(dev, cmd, arg);
2219 static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg);
2220 static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
2221 int cmd, void *arg);
2223 static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
2226 case CCISS_GETPCIINFO:
2227 case CCISS_GETINTINFO:
2228 case CCISS_SETINTINFO:
2229 case CCISS_GETNODENAME:
2230 case CCISS_SETNODENAME:
2231 case CCISS_GETHEARTBEAT:
2232 case CCISS_GETBUSTYPES:
2233 case CCISS_GETFIRMVER:
2234 case CCISS_GETDRIVVER:
2235 case CCISS_REVALIDVOLS:
2236 case CCISS_DEREGDISK:
2237 case CCISS_REGNEWDISK:
2239 case CCISS_RESCANDISK:
2240 case CCISS_GETLUNINFO:
2241 return do_ioctl(dev, cmd, arg);
2243 case CCISS_PASSTHRU32:
2244 return hpsa_ioctl32_passthru(dev, cmd, arg);
2245 case CCISS_BIG_PASSTHRU32:
2246 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
2249 return -ENOIOCTLCMD;
2253 static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
2255 IOCTL32_Command_struct __user *arg32 =
2256 (IOCTL32_Command_struct __user *) arg;
2257 IOCTL_Command_struct arg64;
2258 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
2263 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2264 sizeof(arg64.LUN_info));
2265 err |= copy_from_user(&arg64.Request, &arg32->Request,
2266 sizeof(arg64.Request));
2267 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2268 sizeof(arg64.error_info));
2269 err |= get_user(arg64.buf_size, &arg32->buf_size);
2270 err |= get_user(cp, &arg32->buf);
2271 arg64.buf = compat_ptr(cp);
2272 err |= copy_to_user(p, &arg64, sizeof(arg64));
2277 err = do_ioctl(dev, CCISS_PASSTHRU, (void *)p);
2280 err |= copy_in_user(&arg32->error_info, &p->error_info,
2281 sizeof(arg32->error_info));
2287 static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
2290 BIG_IOCTL32_Command_struct __user *arg32 =
2291 (BIG_IOCTL32_Command_struct __user *) arg;
2292 BIG_IOCTL_Command_struct arg64;
2293 BIG_IOCTL_Command_struct __user *p =
2294 compat_alloc_user_space(sizeof(arg64));
2299 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2300 sizeof(arg64.LUN_info));
2301 err |= copy_from_user(&arg64.Request, &arg32->Request,
2302 sizeof(arg64.Request));
2303 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2304 sizeof(arg64.error_info));
2305 err |= get_user(arg64.buf_size, &arg32->buf_size);
2306 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
2307 err |= get_user(cp, &arg32->buf);
2308 arg64.buf = compat_ptr(cp);
2309 err |= copy_to_user(p, &arg64, sizeof(arg64));
2314 err = do_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
2317 err |= copy_in_user(&arg32->error_info, &p->error_info,
2318 sizeof(arg32->error_info));
2325 static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
2327 struct hpsa_pci_info pciinfo;
2331 pciinfo.domain = pci_domain_nr(h->pdev->bus);
2332 pciinfo.bus = h->pdev->bus->number;
2333 pciinfo.dev_fn = h->pdev->devfn;
2334 pciinfo.board_id = h->board_id;
2335 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
2340 static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
2342 DriverVer_type DriverVer;
2343 unsigned char vmaj, vmin, vsubmin;
2346 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
2347 &vmaj, &vmin, &vsubmin);
2349 dev_info(&h->pdev->dev, "driver version string '%s' "
2350 "unrecognized.", HPSA_DRIVER_VERSION);
2355 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
2358 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
2363 static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2365 IOCTL_Command_struct iocommand;
2366 struct CommandList *c;
2368 union u64bit temp64;
2372 if (!capable(CAP_SYS_RAWIO))
2374 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
2376 if ((iocommand.buf_size < 1) &&
2377 (iocommand.Request.Type.Direction != XFER_NONE)) {
2380 if (iocommand.buf_size > 0) {
2381 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
2385 if (iocommand.Request.Type.Direction == XFER_WRITE) {
2386 /* Copy the data into the buffer we created */
2387 if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) {
2392 memset(buff, 0, iocommand.buf_size);
2393 c = cmd_special_alloc(h);
2398 /* Fill in the command type */
2399 c->cmd_type = CMD_IOCTL_PEND;
2400 /* Fill in Command Header */
2401 c->Header.ReplyQueue = 0; /* unused in simple mode */
2402 if (iocommand.buf_size > 0) { /* buffer to fill */
2403 c->Header.SGList = 1;
2404 c->Header.SGTotal = 1;
2405 } else { /* no buffers to fill */
2406 c->Header.SGList = 0;
2407 c->Header.SGTotal = 0;
2409 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
2410 /* use the kernel address the cmd block for tag */
2411 c->Header.Tag.lower = c->busaddr;
2413 /* Fill in Request block */
2414 memcpy(&c->Request, &iocommand.Request,
2415 sizeof(c->Request));
2417 /* Fill in the scatter gather information */
2418 if (iocommand.buf_size > 0) {
2419 temp64.val = pci_map_single(h->pdev, buff,
2420 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
2421 c->SG[0].Addr.lower = temp64.val32.lower;
2422 c->SG[0].Addr.upper = temp64.val32.upper;
2423 c->SG[0].Len = iocommand.buf_size;
2424 c->SG[0].Ext = 0; /* we are not chaining*/
2426 hpsa_scsi_do_simple_cmd_core(h, c);
2427 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
2428 check_ioctl_unit_attention(h, c);
2430 /* Copy the error information out */
2431 memcpy(&iocommand.error_info, c->err_info,
2432 sizeof(iocommand.error_info));
2433 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
2435 cmd_special_free(h, c);
2439 if (iocommand.Request.Type.Direction == XFER_READ) {
2440 /* Copy the data out of the buffer we created */
2441 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
2443 cmd_special_free(h, c);
2448 cmd_special_free(h, c);
2452 static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2454 BIG_IOCTL_Command_struct *ioc;
2455 struct CommandList *c;
2456 unsigned char **buff = NULL;
2457 int *buff_size = NULL;
2458 union u64bit temp64;
2464 BYTE __user *data_ptr;
2468 if (!capable(CAP_SYS_RAWIO))
2470 ioc = (BIG_IOCTL_Command_struct *)
2471 kmalloc(sizeof(*ioc), GFP_KERNEL);
2476 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
2480 if ((ioc->buf_size < 1) &&
2481 (ioc->Request.Type.Direction != XFER_NONE)) {
2485 /* Check kmalloc limits using all SGs */
2486 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
2490 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
2494 buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
2499 buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL);
2504 left = ioc->buf_size;
2505 data_ptr = ioc->buf;
2507 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
2508 buff_size[sg_used] = sz;
2509 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
2510 if (buff[sg_used] == NULL) {
2514 if (ioc->Request.Type.Direction == XFER_WRITE) {
2515 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
2520 memset(buff[sg_used], 0, sz);
2525 c = cmd_special_alloc(h);
2530 c->cmd_type = CMD_IOCTL_PEND;
2531 c->Header.ReplyQueue = 0;
2533 if (ioc->buf_size > 0) {
2534 c->Header.SGList = sg_used;
2535 c->Header.SGTotal = sg_used;
2537 c->Header.SGList = 0;
2538 c->Header.SGTotal = 0;
2540 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
2541 c->Header.Tag.lower = c->busaddr;
2542 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
2543 if (ioc->buf_size > 0) {
2545 for (i = 0; i < sg_used; i++) {
2546 temp64.val = pci_map_single(h->pdev, buff[i],
2547 buff_size[i], PCI_DMA_BIDIRECTIONAL);
2548 c->SG[i].Addr.lower = temp64.val32.lower;
2549 c->SG[i].Addr.upper = temp64.val32.upper;
2550 c->SG[i].Len = buff_size[i];
2551 /* we are not chaining */
2555 hpsa_scsi_do_simple_cmd_core(h, c);
2556 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
2557 check_ioctl_unit_attention(h, c);
2558 /* Copy the error information out */
2559 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
2560 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
2561 cmd_special_free(h, c);
2565 if (ioc->Request.Type.Direction == XFER_READ) {
2566 /* Copy the data out of the buffer we created */
2567 BYTE __user *ptr = ioc->buf;
2568 for (i = 0; i < sg_used; i++) {
2569 if (copy_to_user(ptr, buff[i], buff_size[i])) {
2570 cmd_special_free(h, c);
2574 ptr += buff_size[i];
2577 cmd_special_free(h, c);
2581 for (i = 0; i < sg_used; i++)
2590 static void check_ioctl_unit_attention(struct ctlr_info *h,
2591 struct CommandList *c)
2593 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2594 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
2595 (void) check_for_unit_attention(h, c);
2600 static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
2602 struct ctlr_info *h;
2603 void __user *argp = (void __user *)arg;
2605 h = sdev_to_hba(dev);
2608 case CCISS_DEREGDISK:
2609 case CCISS_REGNEWDISK:
2611 hpsa_update_scsi_devices(h, dev->host->host_no);
2613 case CCISS_GETPCIINFO:
2614 return hpsa_getpciinfo_ioctl(h, argp);
2615 case CCISS_GETDRIVVER:
2616 return hpsa_getdrivver_ioctl(h, argp);
2617 case CCISS_PASSTHRU:
2618 return hpsa_passthru_ioctl(h, argp);
2619 case CCISS_BIG_PASSTHRU:
2620 return hpsa_big_passthru_ioctl(h, argp);
2626 static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
2627 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
2630 int pci_dir = XFER_NONE;
2632 c->cmd_type = CMD_IOCTL_PEND;
2633 c->Header.ReplyQueue = 0;
2634 if (buff != NULL && size > 0) {
2635 c->Header.SGList = 1;
2636 c->Header.SGTotal = 1;
2638 c->Header.SGList = 0;
2639 c->Header.SGTotal = 0;
2641 c->Header.Tag.lower = c->busaddr;
2642 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
2644 c->Request.Type.Type = cmd_type;
2645 if (cmd_type == TYPE_CMD) {
2648 /* are we trying to read a vital product page */
2649 if (page_code != 0) {
2650 c->Request.CDB[1] = 0x01;
2651 c->Request.CDB[2] = page_code;
2653 c->Request.CDBLen = 6;
2654 c->Request.Type.Attribute = ATTR_SIMPLE;
2655 c->Request.Type.Direction = XFER_READ;
2656 c->Request.Timeout = 0;
2657 c->Request.CDB[0] = HPSA_INQUIRY;
2658 c->Request.CDB[4] = size & 0xFF;
2660 case HPSA_REPORT_LOG:
2661 case HPSA_REPORT_PHYS:
2662 /* Talking to controller so It's a physical command
2663 mode = 00 target = 0. Nothing to write.
2665 c->Request.CDBLen = 12;
2666 c->Request.Type.Attribute = ATTR_SIMPLE;
2667 c->Request.Type.Direction = XFER_READ;
2668 c->Request.Timeout = 0;
2669 c->Request.CDB[0] = cmd;
2670 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
2671 c->Request.CDB[7] = (size >> 16) & 0xFF;
2672 c->Request.CDB[8] = (size >> 8) & 0xFF;
2673 c->Request.CDB[9] = size & 0xFF;
2676 case HPSA_READ_CAPACITY:
2677 c->Request.CDBLen = 10;
2678 c->Request.Type.Attribute = ATTR_SIMPLE;
2679 c->Request.Type.Direction = XFER_READ;
2680 c->Request.Timeout = 0;
2681 c->Request.CDB[0] = cmd;
2683 case HPSA_CACHE_FLUSH:
2684 c->Request.CDBLen = 12;
2685 c->Request.Type.Attribute = ATTR_SIMPLE;
2686 c->Request.Type.Direction = XFER_WRITE;
2687 c->Request.Timeout = 0;
2688 c->Request.CDB[0] = BMIC_WRITE;
2689 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
2691 case TEST_UNIT_READY:
2692 c->Request.CDBLen = 6;
2693 c->Request.Type.Attribute = ATTR_SIMPLE;
2694 c->Request.Type.Direction = XFER_NONE;
2695 c->Request.Timeout = 0;
2698 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
2702 } else if (cmd_type == TYPE_MSG) {
2705 case HPSA_DEVICE_RESET_MSG:
2706 c->Request.CDBLen = 16;
2707 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
2708 c->Request.Type.Attribute = ATTR_SIMPLE;
2709 c->Request.Type.Direction = XFER_NONE;
2710 c->Request.Timeout = 0; /* Don't time out */
2711 c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */
2712 c->Request.CDB[1] = 0x03; /* Reset target above */
2713 /* If bytes 4-7 are zero, it means reset the */
2715 c->Request.CDB[4] = 0x00;
2716 c->Request.CDB[5] = 0x00;
2717 c->Request.CDB[6] = 0x00;
2718 c->Request.CDB[7] = 0x00;
2722 dev_warn(&h->pdev->dev, "unknown message type %d\n",
2727 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
2731 switch (c->Request.Type.Direction) {
2733 pci_dir = PCI_DMA_FROMDEVICE;
2736 pci_dir = PCI_DMA_TODEVICE;
2739 pci_dir = PCI_DMA_NONE;
2742 pci_dir = PCI_DMA_BIDIRECTIONAL;
2745 hpsa_map_one(h->pdev, c, buff, size, pci_dir);
2751 * Map (physical) PCI mem into (virtual) kernel space
2753 static void __iomem *remap_pci_mem(ulong base, ulong size)
2755 ulong page_base = ((ulong) base) & PAGE_MASK;
2756 ulong page_offs = ((ulong) base) - page_base;
2757 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
2759 return page_remapped ? (page_remapped + page_offs) : NULL;
2762 /* Takes cmds off the submission queue and sends them to the hardware,
2763 * then puts them on the queue of cmds waiting for completion.
2765 static void start_io(struct ctlr_info *h)
2767 struct CommandList *c;
2769 while (!hlist_empty(&h->reqQ)) {
2770 c = hlist_entry(h->reqQ.first, struct CommandList, list);
2771 /* can't do anything if fifo is full */
2772 if ((h->access.fifo_full(h))) {
2773 dev_warn(&h->pdev->dev, "fifo full\n");
2777 /* Get the first entry from the Request Q */
2781 /* Tell the controller execute command */
2782 h->access.submit_command(h, c);
2784 /* Put job onto the completed Q */
2789 static inline unsigned long get_next_completion(struct ctlr_info *h)
2791 return h->access.command_completed(h);
2794 static inline bool interrupt_pending(struct ctlr_info *h)
2796 return h->access.intr_pending(h);
2799 static inline long interrupt_not_for_us(struct ctlr_info *h)
2801 return !(h->msi_vector || h->msix_vector) &&
2802 ((h->access.intr_pending(h) == 0) ||
2803 (h->interrupts_enabled == 0));
2806 static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
2809 if (unlikely(tag_index >= h->nr_cmds)) {
2810 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
2816 static inline void finish_cmd(struct CommandList *c, u32 raw_tag)
2819 if (likely(c->cmd_type == CMD_SCSI))
2820 complete_scsi_command(c, 0, raw_tag);
2821 else if (c->cmd_type == CMD_IOCTL_PEND)
2822 complete(c->waiting);
2825 static inline u32 hpsa_tag_contains_index(u32 tag)
2827 #define DIRECT_LOOKUP_BIT 0x10
2828 return tag & DIRECT_LOOKUP_BIT;
2831 static inline u32 hpsa_tag_to_index(u32 tag)
2833 #define DIRECT_LOOKUP_SHIFT 5
2834 return tag >> DIRECT_LOOKUP_SHIFT;
2837 static inline u32 hpsa_tag_discard_error_bits(u32 tag)
2839 #define HPSA_ERROR_BITS 0x03
2840 return tag & ~HPSA_ERROR_BITS;
2843 /* process completion of an indexed ("direct lookup") command */
2844 static inline u32 process_indexed_cmd(struct ctlr_info *h,
2848 struct CommandList *c;
2850 tag_index = hpsa_tag_to_index(raw_tag);
2851 if (bad_tag(h, tag_index, raw_tag))
2852 return next_command(h);
2853 c = h->cmd_pool + tag_index;
2854 finish_cmd(c, raw_tag);
2855 return next_command(h);
2858 /* process completion of a non-indexed command */
2859 static inline u32 process_nonindexed_cmd(struct ctlr_info *h,
2863 struct CommandList *c = NULL;
2864 struct hlist_node *tmp;
2866 tag = hpsa_tag_discard_error_bits(raw_tag);
2867 hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
2868 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
2869 finish_cmd(c, raw_tag);
2870 return next_command(h);
2873 bad_tag(h, h->nr_cmds + 1, raw_tag);
2874 return next_command(h);
2877 static irqreturn_t do_hpsa_intr(int irq, void *dev_id)
2879 struct ctlr_info *h = dev_id;
2880 unsigned long flags;
2883 if (interrupt_not_for_us(h))
2885 spin_lock_irqsave(&h->lock, flags);
2886 raw_tag = get_next_completion(h);
2887 while (raw_tag != FIFO_EMPTY) {
2888 if (hpsa_tag_contains_index(raw_tag))
2889 raw_tag = process_indexed_cmd(h, raw_tag);
2891 raw_tag = process_nonindexed_cmd(h, raw_tag);
2893 spin_unlock_irqrestore(&h->lock, flags);
2897 /* Send a message CDB to the firmwart. */
2898 static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
2902 struct CommandListHeader CommandHeader;
2903 struct RequestBlock Request;
2904 struct ErrDescriptor ErrorDescriptor;
2906 struct Command *cmd;
2907 static const size_t cmd_sz = sizeof(*cmd) +
2908 sizeof(cmd->ErrorDescriptor);
2910 uint32_t paddr32, tag;
2911 void __iomem *vaddr;
2914 vaddr = pci_ioremap_bar(pdev, 0);
2918 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
2919 * CCISS commands, so they must be allocated from the lower 4GiB of
2922 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2928 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
2934 /* This must fit, because of the 32-bit consistent DMA mask. Also,
2935 * although there's no guarantee, we assume that the address is at
2936 * least 4-byte aligned (most likely, it's page-aligned).
2940 cmd->CommandHeader.ReplyQueue = 0;
2941 cmd->CommandHeader.SGList = 0;
2942 cmd->CommandHeader.SGTotal = 0;
2943 cmd->CommandHeader.Tag.lower = paddr32;
2944 cmd->CommandHeader.Tag.upper = 0;
2945 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
2947 cmd->Request.CDBLen = 16;
2948 cmd->Request.Type.Type = TYPE_MSG;
2949 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
2950 cmd->Request.Type.Direction = XFER_NONE;
2951 cmd->Request.Timeout = 0; /* Don't time out */
2952 cmd->Request.CDB[0] = opcode;
2953 cmd->Request.CDB[1] = type;
2954 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
2955 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
2956 cmd->ErrorDescriptor.Addr.upper = 0;
2957 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
2959 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
2961 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
2962 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
2963 if (hpsa_tag_discard_error_bits(tag) == paddr32)
2965 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
2970 /* we leak the DMA buffer here ... no choice since the controller could
2971 * still complete the command.
2973 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
2974 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
2979 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
2981 if (tag & HPSA_ERROR_BIT) {
2982 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
2987 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
2992 #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
2993 #define hpsa_noop(p) hpsa_message(p, 3, 0)
2995 static __devinit int hpsa_reset_msi(struct pci_dev *pdev)
2997 /* the #defines are stolen from drivers/pci/msi.h. */
2998 #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
2999 #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
3004 pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
3006 pci_read_config_word(pdev, msi_control_reg(pos), &control);
3007 if (control & PCI_MSI_FLAGS_ENABLE) {
3008 dev_info(&pdev->dev, "resetting MSI\n");
3009 pci_write_config_word(pdev, msi_control_reg(pos),
3010 control & ~PCI_MSI_FLAGS_ENABLE);
3014 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3016 pci_read_config_word(pdev, msi_control_reg(pos), &control);
3017 if (control & PCI_MSIX_FLAGS_ENABLE) {
3018 dev_info(&pdev->dev, "resetting MSI-X\n");
3019 pci_write_config_word(pdev, msi_control_reg(pos),
3020 control & ~PCI_MSIX_FLAGS_ENABLE);
3027 /* This does a hard reset of the controller using PCI power management
3030 static __devinit int hpsa_hard_reset_controller(struct pci_dev *pdev)
3032 u16 pmcsr, saved_config_space[32];
3035 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
3037 /* This is very nearly the same thing as
3039 * pci_save_state(pci_dev);
3040 * pci_set_power_state(pci_dev, PCI_D3hot);
3041 * pci_set_power_state(pci_dev, PCI_D0);
3042 * pci_restore_state(pci_dev);
3044 * but we can't use these nice canned kernel routines on
3045 * kexec, because they also check the MSI/MSI-X state in PCI
3046 * configuration space and do the wrong thing when it is
3047 * set/cleared. Also, the pci_save/restore_state functions
3048 * violate the ordering requirements for restoring the
3049 * configuration space from the CCISS document (see the
3050 * comment below). So we roll our own ....
3053 for (i = 0; i < 32; i++)
3054 pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
3056 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
3059 "hpsa_reset_controller: PCI PM not supported\n");
3063 /* Quoting from the Open CISS Specification: "The Power
3064 * Management Control/Status Register (CSR) controls the power
3065 * state of the device. The normal operating state is D0,
3066 * CSR=00h. The software off state is D3, CSR=03h. To reset
3067 * the controller, place the interface device in D3 then to
3068 * D0, this causes a secondary PCI reset which will reset the
3072 /* enter the D3hot power management state */
3073 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
3074 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3076 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3080 /* enter the D0 power management state */
3081 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3083 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3087 /* Restore the PCI configuration space. The Open CISS
3088 * Specification says, "Restore the PCI Configuration
3089 * Registers, offsets 00h through 60h. It is important to
3090 * restore the command register, 16-bits at offset 04h,
3091 * last. Do not restore the configuration status register,
3092 * 16-bits at offset 06h." Note that the offset is 2*i.
3094 for (i = 0; i < 32; i++) {
3095 if (i == 2 || i == 3)
3097 pci_write_config_word(pdev, 2*i, saved_config_space[i]);
3100 pci_write_config_word(pdev, 4, saved_config_space[2]);
3106 * We cannot read the structure directly, for portability we must use
3108 * This is for debug only.
3111 static void print_cfg_table(struct device *dev, struct CfgTable *tb)
3116 dev_info(dev, "Controller Configuration information\n");
3117 dev_info(dev, "------------------------------------\n");
3118 for (i = 0; i < 4; i++)
3119 temp_name[i] = readb(&(tb->Signature[i]));
3120 temp_name[4] = '\0';
3121 dev_info(dev, " Signature = %s\n", temp_name);
3122 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
3123 dev_info(dev, " Transport methods supported = 0x%x\n",
3124 readl(&(tb->TransportSupport)));
3125 dev_info(dev, " Transport methods active = 0x%x\n",
3126 readl(&(tb->TransportActive)));
3127 dev_info(dev, " Requested transport Method = 0x%x\n",
3128 readl(&(tb->HostWrite.TransportRequest)));
3129 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
3130 readl(&(tb->HostWrite.CoalIntDelay)));
3131 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
3132 readl(&(tb->HostWrite.CoalIntCount)));
3133 dev_info(dev, " Max outstanding commands = 0x%d\n",
3134 readl(&(tb->CmdsOutMax)));
3135 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
3136 for (i = 0; i < 16; i++)
3137 temp_name[i] = readb(&(tb->ServerName[i]));
3138 temp_name[16] = '\0';
3139 dev_info(dev, " Server Name = %s\n", temp_name);
3140 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
3141 readl(&(tb->HeartBeat)));
3143 #endif /* HPSA_DEBUG */
3145 static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
3147 int i, offset, mem_type, bar_type;
3149 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
3152 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3153 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
3154 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
3157 mem_type = pci_resource_flags(pdev, i) &
3158 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
3160 case PCI_BASE_ADDRESS_MEM_TYPE_32:
3161 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
3162 offset += 4; /* 32 bit */
3164 case PCI_BASE_ADDRESS_MEM_TYPE_64:
3167 default: /* reserved in PCI 2.2 */
3168 dev_warn(&pdev->dev,
3169 "base address is invalid\n");
3174 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
3180 /* If MSI/MSI-X is supported by the kernel we will try to enable it on
3181 * controllers that are capable. If not, we use IO-APIC mode.
3184 static void __devinit hpsa_interrupt_mode(struct ctlr_info *h,
3185 struct pci_dev *pdev, u32 board_id)
3187 #ifdef CONFIG_PCI_MSI
3189 struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1},
3193 /* Some boards advertise MSI but don't really support it */
3194 if ((board_id == 0x40700E11) ||
3195 (board_id == 0x40800E11) ||
3196 (board_id == 0x40820E11) || (board_id == 0x40830E11))
3197 goto default_int_mode;
3198 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
3199 dev_info(&pdev->dev, "MSIX\n");
3200 err = pci_enable_msix(pdev, hpsa_msix_entries, 4);
3202 h->intr[0] = hpsa_msix_entries[0].vector;
3203 h->intr[1] = hpsa_msix_entries[1].vector;
3204 h->intr[2] = hpsa_msix_entries[2].vector;
3205 h->intr[3] = hpsa_msix_entries[3].vector;
3210 dev_warn(&pdev->dev, "only %d MSI-X vectors "
3211 "available\n", err);
3212 goto default_int_mode;
3214 dev_warn(&pdev->dev, "MSI-X init failed %d\n",
3216 goto default_int_mode;
3219 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
3220 dev_info(&pdev->dev, "MSI\n");
3221 if (!pci_enable_msi(pdev))
3224 dev_warn(&pdev->dev, "MSI init failed\n");
3227 #endif /* CONFIG_PCI_MSI */
3228 /* if we get here we're going to use the default interrupt mode */
3229 h->intr[PERF_MODE_INT] = pdev->irq;
3232 static int hpsa_pci_init(struct ctlr_info *h, struct pci_dev *pdev)
3234 ushort subsystem_vendor_id, subsystem_device_id, command;
3235 u32 board_id, scratchpad = 0;
3238 u64 cfg_base_addr_index;
3240 int i, prod_index, err;
3242 subsystem_vendor_id = pdev->subsystem_vendor;
3243 subsystem_device_id = pdev->subsystem_device;
3244 board_id = (((u32) (subsystem_device_id << 16) & 0xffff0000) |
3245 subsystem_vendor_id);
3247 for (i = 0; i < ARRAY_SIZE(products); i++)
3248 if (board_id == products[i].board_id)
3253 if (prod_index == ARRAY_SIZE(products)) {
3255 if (subsystem_vendor_id != PCI_VENDOR_ID_HP ||
3257 dev_warn(&pdev->dev, "unrecognized board ID:"
3258 " 0x%08lx, ignoring.\n",
3259 (unsigned long) board_id);
3263 /* check to see if controller has been disabled
3264 * BEFORE trying to enable it
3266 (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
3267 if (!(command & 0x02)) {
3268 dev_warn(&pdev->dev, "controller appears to be disabled\n");
3272 err = pci_enable_device(pdev);
3274 dev_warn(&pdev->dev, "unable to enable PCI device\n");
3278 err = pci_request_regions(pdev, "hpsa");
3280 dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
3284 /* If the kernel supports MSI/MSI-X we will try to enable that,
3285 * else we use the IO-APIC interrupt assigned to us by system ROM.
3287 hpsa_interrupt_mode(h, pdev, board_id);
3289 /* find the memory BAR */
3290 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3291 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
3294 if (i == DEVICE_COUNT_RESOURCE) {
3295 dev_warn(&pdev->dev, "no memory BAR found\n");
3297 goto err_out_free_res;
3300 h->paddr = pci_resource_start(pdev, i); /* addressing mode bits
3304 h->vaddr = remap_pci_mem(h->paddr, 0x250);
3306 /* Wait for the board to become ready. */
3307 for (i = 0; i < HPSA_BOARD_READY_ITERATIONS; i++) {
3308 scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
3309 if (scratchpad == HPSA_FIRMWARE_READY)
3311 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
3313 if (scratchpad != HPSA_FIRMWARE_READY) {
3314 dev_warn(&pdev->dev, "board not ready, timed out.\n");
3316 goto err_out_free_res;
3319 /* get the address index number */
3320 cfg_base_addr = readl(h->vaddr + SA5_CTCFG_OFFSET);
3321 cfg_base_addr &= (u32) 0x0000ffff;
3322 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
3323 if (cfg_base_addr_index == -1) {
3324 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
3326 goto err_out_free_res;
3329 cfg_offset = readl(h->vaddr + SA5_CTMEM_OFFSET);
3330 h->cfgtable = remap_pci_mem(pci_resource_start(pdev,
3331 cfg_base_addr_index) + cfg_offset,
3332 sizeof(h->cfgtable));
3333 /* Find performant mode table. */
3334 trans_offset = readl(&(h->cfgtable->TransMethodOffset));
3335 h->transtable = remap_pci_mem(pci_resource_start(pdev,
3336 cfg_base_addr_index)+cfg_offset+trans_offset,
3337 sizeof(*h->transtable));
3339 h->board_id = board_id;
3340 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
3341 h->product_name = products[prod_index].product_name;
3342 h->access = *(products[prod_index].access);
3343 /* Allow room for some ioctls */
3344 h->nr_cmds = h->max_commands - 4;
3346 if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
3347 (readb(&h->cfgtable->Signature[1]) != 'I') ||
3348 (readb(&h->cfgtable->Signature[2]) != 'S') ||
3349 (readb(&h->cfgtable->Signature[3]) != 'S')) {
3350 dev_warn(&pdev->dev, "not a valid CISS config table\n");
3352 goto err_out_free_res;
3356 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3358 prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
3360 writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
3364 /* Disabling DMA prefetch for the P600
3365 * An ASIC bug may result in a prefetch beyond
3368 if (board_id == 0x3225103C) {
3370 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
3371 dma_prefetch |= 0x8000;
3372 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
3375 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
3376 /* Update the field, and then ring the doorbell */
3377 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
3378 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
3380 /* under certain very rare conditions, this can take awhile.
3381 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3382 * as we enter this code.)
3384 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
3385 if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3387 /* delay and try again */
3392 print_cfg_table(&pdev->dev, h->cfgtable);
3393 #endif /* HPSA_DEBUG */
3395 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
3396 dev_warn(&pdev->dev, "unable to get board into simple mode\n");
3398 goto err_out_free_res;
3404 * Deliberately omit pci_disable_device(): it does something nasty to
3405 * Smart Array controllers that pci_enable_device does not undo
3407 pci_release_regions(pdev);
3411 static void __devinit hpsa_hba_inquiry(struct ctlr_info *h)
3415 #define HBA_INQUIRY_BYTE_COUNT 64
3416 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
3417 if (!h->hba_inquiry_data)
3419 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
3420 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
3422 kfree(h->hba_inquiry_data);
3423 h->hba_inquiry_data = NULL;
3427 static int __devinit hpsa_init_one(struct pci_dev *pdev,
3428 const struct pci_device_id *ent)
3432 struct ctlr_info *h;
3434 if (number_of_controllers == 0)
3435 printk(KERN_INFO DRIVER_NAME "\n");
3436 if (reset_devices) {
3437 /* Reset the controller with a PCI power-cycle */
3438 if (hpsa_hard_reset_controller(pdev) || hpsa_reset_msi(pdev))
3441 /* Some devices (notably the HP Smart Array 5i Controller)
3442 need a little pause here */
3443 msleep(HPSA_POST_RESET_PAUSE_MSECS);
3445 /* Now try to get the controller to respond to a no-op */
3446 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
3447 if (hpsa_noop(pdev) == 0)
3450 dev_warn(&pdev->dev, "no-op failed%s\n",
3451 (i < 11 ? "; re-trying" : ""));
3455 /* Command structures must be aligned on a 32-byte boundary because
3456 * the 5 lower bits of the address are used by the hardware. and by
3457 * the driver. See comments in hpsa.h for more info.
3459 #define COMMANDLIST_ALIGNMENT 32
3460 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
3461 h = kzalloc(sizeof(*h), GFP_KERNEL);
3465 h->busy_initializing = 1;
3466 INIT_HLIST_HEAD(&h->cmpQ);
3467 INIT_HLIST_HEAD(&h->reqQ);
3468 mutex_init(&h->busy_shutting_down);
3469 init_completion(&h->scan_wait);
3470 rc = hpsa_pci_init(h, pdev);
3474 sprintf(h->devname, "hpsa%d", number_of_controllers);
3475 h->ctlr = number_of_controllers;
3476 number_of_controllers++;
3479 /* configure PCI DMA stuff */
3480 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3484 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3488 dev_err(&pdev->dev, "no suitable DMA available\n");
3493 /* make sure the board interrupts are off */
3494 h->access.set_intr_mask(h, HPSA_INTR_OFF);
3495 rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr,
3496 IRQF_DISABLED, h->devname, h);
3498 dev_err(&pdev->dev, "unable to get irq %d for %s\n",
3499 h->intr[PERF_MODE_INT], h->devname);
3503 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
3504 h->devname, pdev->device,
3505 h->intr[PERF_MODE_INT], dac ? "" : " not");
3508 kmalloc(((h->nr_cmds + BITS_PER_LONG -
3509 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
3510 h->cmd_pool = pci_alloc_consistent(h->pdev,
3511 h->nr_cmds * sizeof(*h->cmd_pool),
3512 &(h->cmd_pool_dhandle));
3513 h->errinfo_pool = pci_alloc_consistent(h->pdev,
3514 h->nr_cmds * sizeof(*h->errinfo_pool),
3515 &(h->errinfo_pool_dhandle));
3516 if ((h->cmd_pool_bits == NULL)
3517 || (h->cmd_pool == NULL)
3518 || (h->errinfo_pool == NULL)) {
3519 dev_err(&pdev->dev, "out of memory");
3523 spin_lock_init(&h->lock);
3525 pci_set_drvdata(pdev, h);
3526 memset(h->cmd_pool_bits, 0,
3527 ((h->nr_cmds + BITS_PER_LONG -
3528 1) / BITS_PER_LONG) * sizeof(unsigned long));
3532 /* Turn the interrupts on so we can service requests */
3533 h->access.set_intr_mask(h, HPSA_INTR_ON);
3535 hpsa_put_ctlr_into_performant_mode(h);
3536 hpsa_hba_inquiry(h);
3537 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
3538 h->busy_initializing = 0;
3542 kfree(h->cmd_pool_bits);
3544 pci_free_consistent(h->pdev,
3545 h->nr_cmds * sizeof(struct CommandList),
3546 h->cmd_pool, h->cmd_pool_dhandle);
3547 if (h->errinfo_pool)
3548 pci_free_consistent(h->pdev,
3549 h->nr_cmds * sizeof(struct ErrorInfo),
3551 h->errinfo_pool_dhandle);
3552 free_irq(h->intr[PERF_MODE_INT], h);
3555 h->busy_initializing = 0;
3560 static void hpsa_flush_cache(struct ctlr_info *h)
3563 struct CommandList *c;
3565 flush_buf = kzalloc(4, GFP_KERNEL);
3569 c = cmd_special_alloc(h);
3571 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
3574 fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
3575 RAID_CTLR_LUNID, TYPE_CMD);
3576 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
3577 if (c->err_info->CommandStatus != 0)
3578 dev_warn(&h->pdev->dev,
3579 "error flushing cache on controller\n");
3580 cmd_special_free(h, c);
3585 static void hpsa_shutdown(struct pci_dev *pdev)
3587 struct ctlr_info *h;
3589 h = pci_get_drvdata(pdev);
3590 /* Turn board interrupts off and send the flush cache command
3591 * sendcmd will turn off interrupt, and send the flush...
3592 * To write all data in the battery backed cache to disks
3594 hpsa_flush_cache(h);
3595 h->access.set_intr_mask(h, HPSA_INTR_OFF);
3596 free_irq(h->intr[PERF_MODE_INT], h);
3597 #ifdef CONFIG_PCI_MSI
3599 pci_disable_msix(h->pdev);
3600 else if (h->msi_vector)
3601 pci_disable_msi(h->pdev);
3602 #endif /* CONFIG_PCI_MSI */
3605 static void __devexit hpsa_remove_one(struct pci_dev *pdev)
3607 struct ctlr_info *h;
3609 if (pci_get_drvdata(pdev) == NULL) {
3610 dev_err(&pdev->dev, "unable to remove device \n");
3613 h = pci_get_drvdata(pdev);
3614 mutex_lock(&h->busy_shutting_down);
3615 remove_from_scan_list(h);
3616 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
3617 hpsa_shutdown(pdev);
3619 pci_free_consistent(h->pdev,
3620 h->nr_cmds * sizeof(struct CommandList),
3621 h->cmd_pool, h->cmd_pool_dhandle);
3622 pci_free_consistent(h->pdev,
3623 h->nr_cmds * sizeof(struct ErrorInfo),
3624 h->errinfo_pool, h->errinfo_pool_dhandle);
3625 pci_free_consistent(h->pdev, h->reply_pool_size,
3626 h->reply_pool, h->reply_pool_dhandle);
3627 kfree(h->cmd_pool_bits);
3628 kfree(h->blockFetchTable);
3629 kfree(h->hba_inquiry_data);
3631 * Deliberately omit pci_disable_device(): it does something nasty to
3632 * Smart Array controllers that pci_enable_device does not undo
3634 pci_release_regions(pdev);
3635 pci_set_drvdata(pdev, NULL);
3636 mutex_unlock(&h->busy_shutting_down);
3640 static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
3641 __attribute__((unused)) pm_message_t state)
3646 static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
3651 static struct pci_driver hpsa_pci_driver = {
3653 .probe = hpsa_init_one,
3654 .remove = __devexit_p(hpsa_remove_one),
3655 .id_table = hpsa_pci_device_id, /* id_table */
3656 .shutdown = hpsa_shutdown,
3657 .suspend = hpsa_suspend,
3658 .resume = hpsa_resume,
3661 /* Fill in bucket_map[], given nsgs (the max number of
3662 * scatter gather elements supported) and bucket[],
3663 * which is an array of 8 integers. The bucket[] array
3664 * contains 8 different DMA transfer sizes (in 16
3665 * byte increments) which the controller uses to fetch
3666 * commands. This function fills in bucket_map[], which
3667 * maps a given number of scatter gather elements to one of
3668 * the 8 DMA transfer sizes. The point of it is to allow the
3669 * controller to only do as much DMA as needed to fetch the
3670 * command, with the DMA transfer size encoded in the lower
3671 * bits of the command address.
3673 static void calc_bucket_map(int bucket[], int num_buckets,
3674 int nsgs, int *bucket_map)
3678 /* even a command with 0 SGs requires 4 blocks */
3679 #define MINIMUM_TRANSFER_BLOCKS 4
3680 #define NUM_BUCKETS 8
3681 /* Note, bucket_map must have nsgs+1 entries. */
3682 for (i = 0; i <= nsgs; i++) {
3683 /* Compute size of a command with i SG entries */
3684 size = i + MINIMUM_TRANSFER_BLOCKS;
3685 b = num_buckets; /* Assume the biggest bucket */
3686 /* Find the bucket that is just big enough */
3687 for (j = 0; j < 8; j++) {
3688 if (bucket[j] >= size) {
3693 /* for a command with i SG entries, use bucket b. */
3698 static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
3702 /* 5 = 1 s/g entry or 4k
3703 * 6 = 2 s/g entry or 8k
3704 * 8 = 4 s/g entry or 16k
3705 * 10 = 6 s/g entry or 24k
3707 int bft[8] = {5, 6, 8, 10, 12, 20, 28, 35}; /* for scatter/gathers */
3710 unsigned long register_value;
3712 trans_support = readl(&(h->cfgtable->TransportSupport));
3713 if (!(trans_support & PERFORMANT_MODE))
3716 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
3717 h->max_sg_entries = 32;
3718 /* Performant mode ring buffer and supporting data structures */
3719 h->reply_pool_size = h->max_commands * sizeof(u64);
3720 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
3721 &(h->reply_pool_dhandle));
3723 /* Need a block fetch table for performant mode */
3724 h->blockFetchTable = kmalloc(((h->max_sg_entries+1) *
3725 sizeof(u32)), GFP_KERNEL);
3727 if ((h->reply_pool == NULL)
3728 || (h->blockFetchTable == NULL))
3731 h->reply_pool_wraparound = 1; /* spec: init to 1 */
3733 /* Controller spec: zero out this buffer. */
3734 memset(h->reply_pool, 0, h->reply_pool_size);
3735 h->reply_pool_head = h->reply_pool;
3737 trans_offset = readl(&(h->cfgtable->TransMethodOffset));
3738 bft[7] = h->max_sg_entries + 4;
3739 calc_bucket_map(bft, ARRAY_SIZE(bft), 32, h->blockFetchTable);
3740 for (i = 0; i < 8; i++)
3741 writel(bft[i], &h->transtable->BlockFetch[i]);
3743 /* size of controller ring buffer */
3744 writel(h->max_commands, &h->transtable->RepQSize);
3745 writel(1, &h->transtable->RepQCount);
3746 writel(0, &h->transtable->RepQCtrAddrLow32);
3747 writel(0, &h->transtable->RepQCtrAddrHigh32);
3748 writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32);
3749 writel(0, &h->transtable->RepQAddr0High32);
3750 writel(CFGTBL_Trans_Performant,
3751 &(h->cfgtable->HostWrite.TransportRequest));
3752 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
3753 /* under certain very rare conditions, this can take awhile.
3754 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3755 * as we enter this code.) */
3756 for (l = 0; l < MAX_CONFIG_WAIT; l++) {
3757 register_value = readl(h->vaddr + SA5_DOORBELL);
3758 if (!(register_value & CFGTBL_ChangeReq))
3760 /* delay and try again */
3761 set_current_state(TASK_INTERRUPTIBLE);
3762 schedule_timeout(10);
3764 register_value = readl(&(h->cfgtable->TransportActive));
3765 if (!(register_value & CFGTBL_Trans_Performant)) {
3766 dev_warn(&h->pdev->dev, "unable to get board into"
3767 " performant mode\n");
3771 /* Change the access methods to the performant access methods */
3772 h->access = SA5_performant_access;
3773 h->transMethod = CFGTBL_Trans_Performant;
3779 pci_free_consistent(h->pdev, h->reply_pool_size,
3780 h->reply_pool, h->reply_pool_dhandle);
3781 kfree(h->blockFetchTable);
3785 * This is it. Register the PCI driver information for the cards we control
3786 * the OS will call our registered routines when it finds one of our cards.
3788 static int __init hpsa_init(void)
3791 /* Start the scan thread */
3792 hpsa_scan_thread = kthread_run(hpsa_scan_func, NULL, "hpsa_scan");
3793 if (IS_ERR(hpsa_scan_thread)) {
3794 err = PTR_ERR(hpsa_scan_thread);
3797 err = pci_register_driver(&hpsa_pci_driver);
3799 kthread_stop(hpsa_scan_thread);
3803 static void __exit hpsa_cleanup(void)
3805 pci_unregister_driver(&hpsa_pci_driver);
3806 kthread_stop(hpsa_scan_thread);
3809 module_init(hpsa_init);
3810 module_exit(hpsa_cleanup);