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_CISSC
, 0x103C, 0x3223},
81 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSC
, 0x103C, 0x3234},
82 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSC
, 0x103C, 0x323D},
83 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x3241},
84 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x3243},
85 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x3245},
86 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x3247},
87 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x3249},
88 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x324a},
89 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x324b},
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 {0x3223103C, "Smart Array P800", &SA5_access
},
103 {0x3234103C, "Smart Array P400", &SA5_access
},
104 {0x323d103c, "Smart Array P700M", &SA5_access
},
105 {0x3241103C, "Smart Array P212", &SA5_access
},
106 {0x3243103C, "Smart Array P410", &SA5_access
},
107 {0x3245103C, "Smart Array P410i", &SA5_access
},
108 {0x3247103C, "Smart Array P411", &SA5_access
},
109 {0x3249103C, "Smart Array P812", &SA5_access
},
110 {0x324a103C, "Smart Array P712m", &SA5_access
},
111 {0x324b103C, "Smart Array P711m", &SA5_access
},
112 {0xFFFF103C, "Unknown Smart Array", &SA5_access
},
115 static int number_of_controllers
;
117 static irqreturn_t
do_hpsa_intr(int irq
, void *dev_id
);
118 static int hpsa_ioctl(struct scsi_device
*dev
, int cmd
, void *arg
);
119 static void start_io(struct ctlr_info
*h
);
122 static int hpsa_compat_ioctl(struct scsi_device
*dev
, int cmd
, void *arg
);
125 static void cmd_free(struct ctlr_info
*h
, struct CommandList
*c
);
126 static void cmd_special_free(struct ctlr_info
*h
, struct CommandList
*c
);
127 static struct CommandList
*cmd_alloc(struct ctlr_info
*h
);
128 static struct CommandList
*cmd_special_alloc(struct ctlr_info
*h
);
129 static void fill_cmd(struct CommandList
*c
, __u8 cmd
, struct ctlr_info
*h
,
130 void *buff
, size_t size
, __u8 page_code
, unsigned char *scsi3addr
,
133 static int hpsa_scsi_queue_command(struct scsi_cmnd
*cmd
,
134 void (*done
)(struct scsi_cmnd
*));
136 static int hpsa_eh_device_reset_handler(struct scsi_cmnd
*scsicmd
);
137 static int hpsa_slave_alloc(struct scsi_device
*sdev
);
138 static void hpsa_slave_destroy(struct scsi_device
*sdev
);
140 static ssize_t
raid_level_show(struct device
*dev
,
141 struct device_attribute
*attr
, char *buf
);
142 static ssize_t
lunid_show(struct device
*dev
,
143 struct device_attribute
*attr
, char *buf
);
144 static ssize_t
unique_id_show(struct device
*dev
,
145 struct device_attribute
*attr
, char *buf
);
146 static void hpsa_update_scsi_devices(struct ctlr_info
*h
, int hostno
);
147 static ssize_t
host_store_rescan(struct device
*dev
,
148 struct device_attribute
*attr
, const char *buf
, size_t count
);
149 static int check_for_unit_attention(struct ctlr_info
*h
,
150 struct CommandList
*c
);
151 static void check_ioctl_unit_attention(struct ctlr_info
*h
,
152 struct CommandList
*c
);
154 static DEVICE_ATTR(raid_level
, S_IRUGO
, raid_level_show
, NULL
);
155 static DEVICE_ATTR(lunid
, S_IRUGO
, lunid_show
, NULL
);
156 static DEVICE_ATTR(unique_id
, S_IRUGO
, unique_id_show
, NULL
);
157 static DEVICE_ATTR(rescan
, S_IWUSR
, NULL
, host_store_rescan
);
159 static struct device_attribute
*hpsa_sdev_attrs
[] = {
160 &dev_attr_raid_level
,
166 static struct device_attribute
*hpsa_shost_attrs
[] = {
171 static struct scsi_host_template hpsa_driver_template
= {
172 .module
= THIS_MODULE
,
175 .queuecommand
= hpsa_scsi_queue_command
,
178 .sg_tablesize
= MAXSGENTRIES
,
180 .use_clustering
= ENABLE_CLUSTERING
,
181 .eh_device_reset_handler
= hpsa_eh_device_reset_handler
,
183 .slave_alloc
= hpsa_slave_alloc
,
184 .slave_destroy
= hpsa_slave_destroy
,
186 .compat_ioctl
= hpsa_compat_ioctl
,
188 .sdev_attrs
= hpsa_sdev_attrs
,
189 .shost_attrs
= hpsa_shost_attrs
,
192 static inline struct ctlr_info
*sdev_to_hba(struct scsi_device
*sdev
)
194 unsigned long *priv
= shost_priv(sdev
->host
);
195 return (struct ctlr_info
*) *priv
;
198 static struct task_struct
*hpsa_scan_thread
;
199 static DEFINE_MUTEX(hpsa_scan_mutex
);
200 static LIST_HEAD(hpsa_scan_q
);
201 static int hpsa_scan_func(void *data
);
204 * add_to_scan_list() - add controller to rescan queue
205 * @h: Pointer to the controller.
207 * Adds the controller to the rescan queue if not already on the queue.
209 * returns 1 if added to the queue, 0 if skipped (could be on the
210 * queue already, or the controller could be initializing or shutting
213 static int add_to_scan_list(struct ctlr_info
*h
)
215 struct ctlr_info
*test_h
;
219 if (h
->busy_initializing
)
223 * If we don't get the lock, it means the driver is unloading
224 * and there's no point in scheduling a new scan.
226 if (!mutex_trylock(&h
->busy_shutting_down
))
229 mutex_lock(&hpsa_scan_mutex
);
230 list_for_each_entry(test_h
, &hpsa_scan_q
, scan_list
) {
236 if (!found
&& !h
->busy_scanning
) {
237 INIT_COMPLETION(h
->scan_wait
);
238 list_add_tail(&h
->scan_list
, &hpsa_scan_q
);
241 mutex_unlock(&hpsa_scan_mutex
);
242 mutex_unlock(&h
->busy_shutting_down
);
248 * remove_from_scan_list() - remove controller from rescan queue
249 * @h: Pointer to the controller.
251 * Removes the controller from the rescan queue if present. Blocks if
252 * the controller is currently conducting a rescan. The controller
253 * can be in one of three states:
254 * 1. Doesn't need a scan
255 * 2. On the scan list, but not scanning yet (we remove it)
256 * 3. Busy scanning (and not on the list). In this case we want to wait for
257 * the scan to complete to make sure the scanning thread for this
258 * controller is completely idle.
260 static void remove_from_scan_list(struct ctlr_info
*h
)
262 struct ctlr_info
*test_h
, *tmp_h
;
264 mutex_lock(&hpsa_scan_mutex
);
265 list_for_each_entry_safe(test_h
, tmp_h
, &hpsa_scan_q
, scan_list
) {
266 if (test_h
== h
) { /* state 2. */
267 list_del(&h
->scan_list
);
268 complete_all(&h
->scan_wait
);
269 mutex_unlock(&hpsa_scan_mutex
);
273 if (h
->busy_scanning
) { /* state 3. */
274 mutex_unlock(&hpsa_scan_mutex
);
275 wait_for_completion(&h
->scan_wait
);
276 } else { /* state 1, nothing to do. */
277 mutex_unlock(&hpsa_scan_mutex
);
281 /* hpsa_scan_func() - kernel thread used to rescan controllers
284 * A kernel thread used scan for drive topology changes on
285 * controllers. The thread processes only one controller at a time
286 * using a queue. Controllers are added to the queue using
287 * add_to_scan_list() and removed from the queue either after done
288 * processing or using remove_from_scan_list().
292 static int hpsa_scan_func(__attribute__((unused
)) void *data
)
298 set_current_state(TASK_INTERRUPTIBLE
);
300 if (kthread_should_stop())
304 mutex_lock(&hpsa_scan_mutex
);
305 if (list_empty(&hpsa_scan_q
)) {
306 mutex_unlock(&hpsa_scan_mutex
);
309 h
= list_entry(hpsa_scan_q
.next
, struct ctlr_info
,
311 list_del(&h
->scan_list
);
312 h
->busy_scanning
= 1;
313 mutex_unlock(&hpsa_scan_mutex
);
314 host_no
= h
->scsi_host
? h
->scsi_host
->host_no
: -1;
315 hpsa_update_scsi_devices(h
, host_no
);
316 complete_all(&h
->scan_wait
);
317 mutex_lock(&hpsa_scan_mutex
);
318 h
->busy_scanning
= 0;
319 mutex_unlock(&hpsa_scan_mutex
);
325 static int check_for_unit_attention(struct ctlr_info
*h
,
326 struct CommandList
*c
)
328 if (c
->err_info
->SenseInfo
[2] != UNIT_ATTENTION
)
331 switch (c
->err_info
->SenseInfo
[12]) {
333 dev_warn(&h
->pdev
->dev
, "hpsa%d: a state change "
334 "detected, command retried\n", h
->ctlr
);
337 dev_warn(&h
->pdev
->dev
, "hpsa%d: LUN failure "
338 "detected, action required\n", h
->ctlr
);
340 case REPORT_LUNS_CHANGED
:
341 dev_warn(&h
->pdev
->dev
, "hpsa%d: report LUN data "
342 "changed\n", h
->ctlr
);
344 * Here, we could call add_to_scan_list and wake up the scan thread,
345 * except that it's quite likely that we will get more than one
346 * REPORT_LUNS_CHANGED condition in quick succession, which means
347 * that those which occur after the first one will likely happen
348 * *during* the hpsa_scan_thread's rescan. And the rescan code is not
349 * robust enough to restart in the middle, undoing what it has already
350 * done, and it's not clear that it's even possible to do this, since
351 * part of what it does is notify the SCSI mid layer, which starts
352 * doing it's own i/o to read partition tables and so on, and the
353 * driver doesn't have visibility to know what might need undoing.
354 * In any event, if possible, it is horribly complicated to get right
355 * so we just don't do it for now.
357 * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
361 dev_warn(&h
->pdev
->dev
, "hpsa%d: a power on "
362 "or device reset detected\n", h
->ctlr
);
364 case UNIT_ATTENTION_CLEARED
:
365 dev_warn(&h
->pdev
->dev
, "hpsa%d: unit attention "
366 "cleared by another initiator\n", h
->ctlr
);
369 dev_warn(&h
->pdev
->dev
, "hpsa%d: unknown "
370 "unit attention detected\n", h
->ctlr
);
376 static ssize_t
host_store_rescan(struct device
*dev
,
377 struct device_attribute
*attr
,
378 const char *buf
, size_t count
)
381 struct Scsi_Host
*shost
= class_to_shost(dev
);
382 unsigned long *priv
= shost_priv(shost
);
383 h
= (struct ctlr_info
*) *priv
;
384 if (add_to_scan_list(h
)) {
385 wake_up_process(hpsa_scan_thread
);
386 wait_for_completion_interruptible(&h
->scan_wait
);
391 /* Enqueuing and dequeuing functions for cmdlists. */
392 static inline void addQ(struct hlist_head
*list
, struct CommandList
*c
)
394 hlist_add_head(&c
->list
, list
);
397 static void enqueue_cmd_and_start_io(struct ctlr_info
*h
,
398 struct CommandList
*c
)
401 spin_lock_irqsave(&h
->lock
, flags
);
405 spin_unlock_irqrestore(&h
->lock
, flags
);
408 static inline void removeQ(struct CommandList
*c
)
410 if (WARN_ON(hlist_unhashed(&c
->list
)))
412 hlist_del_init(&c
->list
);
415 static inline int is_hba_lunid(unsigned char scsi3addr
[])
417 return memcmp(scsi3addr
, RAID_CTLR_LUNID
, 8) == 0;
420 static inline int is_logical_dev_addr_mode(unsigned char scsi3addr
[])
422 return (scsi3addr
[3] & 0xC0) == 0x40;
425 static const char *raid_label
[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
428 #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
430 static ssize_t
raid_level_show(struct device
*dev
,
431 struct device_attribute
*attr
, char *buf
)
436 struct scsi_device
*sdev
;
437 struct hpsa_scsi_dev_t
*hdev
;
440 sdev
= to_scsi_device(dev
);
441 h
= sdev_to_hba(sdev
);
442 spin_lock_irqsave(&h
->lock
, flags
);
443 hdev
= sdev
->hostdata
;
445 spin_unlock_irqrestore(&h
->lock
, flags
);
449 /* Is this even a logical drive? */
450 if (!is_logical_dev_addr_mode(hdev
->scsi3addr
)) {
451 spin_unlock_irqrestore(&h
->lock
, flags
);
452 l
= snprintf(buf
, PAGE_SIZE
, "N/A\n");
456 rlevel
= hdev
->raid_level
;
457 spin_unlock_irqrestore(&h
->lock
, flags
);
458 if (rlevel
< 0 || rlevel
> RAID_UNKNOWN
)
459 rlevel
= RAID_UNKNOWN
;
460 l
= snprintf(buf
, PAGE_SIZE
, "RAID %s\n", raid_label
[rlevel
]);
464 static ssize_t
lunid_show(struct device
*dev
,
465 struct device_attribute
*attr
, char *buf
)
468 struct scsi_device
*sdev
;
469 struct hpsa_scsi_dev_t
*hdev
;
471 unsigned char lunid
[8];
473 sdev
= to_scsi_device(dev
);
474 h
= sdev_to_hba(sdev
);
475 spin_lock_irqsave(&h
->lock
, flags
);
476 hdev
= sdev
->hostdata
;
478 spin_unlock_irqrestore(&h
->lock
, flags
);
481 memcpy(lunid
, hdev
->scsi3addr
, sizeof(lunid
));
482 spin_unlock_irqrestore(&h
->lock
, flags
);
483 return snprintf(buf
, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
484 lunid
[0], lunid
[1], lunid
[2], lunid
[3],
485 lunid
[4], lunid
[5], lunid
[6], lunid
[7]);
488 static ssize_t
unique_id_show(struct device
*dev
,
489 struct device_attribute
*attr
, char *buf
)
492 struct scsi_device
*sdev
;
493 struct hpsa_scsi_dev_t
*hdev
;
495 unsigned char sn
[16];
497 sdev
= to_scsi_device(dev
);
498 h
= sdev_to_hba(sdev
);
499 spin_lock_irqsave(&h
->lock
, flags
);
500 hdev
= sdev
->hostdata
;
502 spin_unlock_irqrestore(&h
->lock
, flags
);
505 memcpy(sn
, hdev
->device_id
, sizeof(sn
));
506 spin_unlock_irqrestore(&h
->lock
, flags
);
507 return snprintf(buf
, 16 * 2 + 2,
508 "%02X%02X%02X%02X%02X%02X%02X%02X"
509 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
510 sn
[0], sn
[1], sn
[2], sn
[3],
511 sn
[4], sn
[5], sn
[6], sn
[7],
512 sn
[8], sn
[9], sn
[10], sn
[11],
513 sn
[12], sn
[13], sn
[14], sn
[15]);
516 static int hpsa_find_target_lun(struct ctlr_info
*h
,
517 unsigned char scsi3addr
[], int bus
, int *target
, int *lun
)
519 /* finds an unused bus, target, lun for a new physical device
520 * assumes h->devlock is held
523 DECLARE_BITMAP(lun_taken
, HPSA_MAX_SCSI_DEVS_PER_HBA
);
525 memset(&lun_taken
[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA
>> 3);
527 for (i
= 0; i
< h
->ndevices
; i
++) {
528 if (h
->dev
[i
]->bus
== bus
&& h
->dev
[i
]->target
!= -1)
529 set_bit(h
->dev
[i
]->target
, lun_taken
);
532 for (i
= 0; i
< HPSA_MAX_SCSI_DEVS_PER_HBA
; i
++) {
533 if (!test_bit(i
, lun_taken
)) {
544 /* Add an entry into h->dev[] array. */
545 static int hpsa_scsi_add_entry(struct ctlr_info
*h
, int hostno
,
546 struct hpsa_scsi_dev_t
*device
,
547 struct hpsa_scsi_dev_t
*added
[], int *nadded
)
549 /* assumes h->devlock is held */
552 unsigned char addr1
[8], addr2
[8];
553 struct hpsa_scsi_dev_t
*sd
;
555 if (n
>= HPSA_MAX_SCSI_DEVS_PER_HBA
) {
556 dev_err(&h
->pdev
->dev
, "too many devices, some will be "
561 /* physical devices do not have lun or target assigned until now. */
562 if (device
->lun
!= -1)
563 /* Logical device, lun is already assigned. */
566 /* If this device a non-zero lun of a multi-lun device
567 * byte 4 of the 8-byte LUN addr will contain the logical
568 * unit no, zero otherise.
570 if (device
->scsi3addr
[4] == 0) {
571 /* This is not a non-zero lun of a multi-lun device */
572 if (hpsa_find_target_lun(h
, device
->scsi3addr
,
573 device
->bus
, &device
->target
, &device
->lun
) != 0)
578 /* This is a non-zero lun of a multi-lun device.
579 * Search through our list and find the device which
580 * has the same 8 byte LUN address, excepting byte 4.
581 * Assign the same bus and target for this new LUN.
582 * Use the logical unit number from the firmware.
584 memcpy(addr1
, device
->scsi3addr
, 8);
586 for (i
= 0; i
< n
; i
++) {
588 memcpy(addr2
, sd
->scsi3addr
, 8);
590 /* differ only in byte 4? */
591 if (memcmp(addr1
, addr2
, 8) == 0) {
592 device
->bus
= sd
->bus
;
593 device
->target
= sd
->target
;
594 device
->lun
= device
->scsi3addr
[4];
598 if (device
->lun
== -1) {
599 dev_warn(&h
->pdev
->dev
, "physical device with no LUN=0,"
600 " suspect firmware bug or unsupported hardware "
609 added
[*nadded
] = device
;
612 /* initially, (before registering with scsi layer) we don't
613 * know our hostno and we don't want to print anything first
614 * time anyway (the scsi layer's inquiries will show that info)
616 /* if (hostno != -1) */
617 dev_info(&h
->pdev
->dev
, "%s device c%db%dt%dl%d added.\n",
618 scsi_device_type(device
->devtype
), hostno
,
619 device
->bus
, device
->target
, device
->lun
);
623 /* Remove an entry from h->dev[] array. */
624 static void hpsa_scsi_remove_entry(struct ctlr_info
*h
, int hostno
, int entry
,
625 struct hpsa_scsi_dev_t
*removed
[], int *nremoved
)
627 /* assumes h->devlock is held */
629 struct hpsa_scsi_dev_t
*sd
;
631 if (entry
< 0 || entry
>= HPSA_MAX_SCSI_DEVS_PER_HBA
)
635 removed
[*nremoved
] = h
->dev
[entry
];
638 for (i
= entry
; i
< h
->ndevices
-1; i
++)
639 h
->dev
[i
] = h
->dev
[i
+1];
641 dev_info(&h
->pdev
->dev
, "%s device c%db%dt%dl%d removed.\n",
642 scsi_device_type(sd
->devtype
), hostno
, sd
->bus
, sd
->target
,
646 #define SCSI3ADDR_EQ(a, b) ( \
647 (a)[7] == (b)[7] && \
648 (a)[6] == (b)[6] && \
649 (a)[5] == (b)[5] && \
650 (a)[4] == (b)[4] && \
651 (a)[3] == (b)[3] && \
652 (a)[2] == (b)[2] && \
653 (a)[1] == (b)[1] && \
656 static void fixup_botched_add(struct ctlr_info
*h
,
657 struct hpsa_scsi_dev_t
*added
)
659 /* called when scsi_add_device fails in order to re-adjust
660 * h->dev[] to match the mid layer's view.
665 spin_lock_irqsave(&h
->lock
, flags
);
666 for (i
= 0; i
< h
->ndevices
; i
++) {
667 if (h
->dev
[i
] == added
) {
668 for (j
= i
; j
< h
->ndevices
-1; j
++)
669 h
->dev
[j
] = h
->dev
[j
+1];
674 spin_unlock_irqrestore(&h
->lock
, flags
);
678 static inline int device_is_the_same(struct hpsa_scsi_dev_t
*dev1
,
679 struct hpsa_scsi_dev_t
*dev2
)
681 if ((is_logical_dev_addr_mode(dev1
->scsi3addr
) ||
682 (dev1
->lun
!= -1 && dev2
->lun
!= -1)) &&
683 dev1
->devtype
!= 0x0C)
684 return (memcmp(dev1
, dev2
, sizeof(*dev1
)) == 0);
686 /* we compare everything except lun and target as these
687 * are not yet assigned. Compare parts likely
690 if (memcmp(dev1
->scsi3addr
, dev2
->scsi3addr
,
691 sizeof(dev1
->scsi3addr
)) != 0)
693 if (memcmp(dev1
->device_id
, dev2
->device_id
,
694 sizeof(dev1
->device_id
)) != 0)
696 if (memcmp(dev1
->model
, dev2
->model
, sizeof(dev1
->model
)) != 0)
698 if (memcmp(dev1
->vendor
, dev2
->vendor
, sizeof(dev1
->vendor
)) != 0)
700 if (memcmp(dev1
->revision
, dev2
->revision
, sizeof(dev1
->revision
)) != 0)
702 if (dev1
->devtype
!= dev2
->devtype
)
704 if (dev1
->raid_level
!= dev2
->raid_level
)
706 if (dev1
->bus
!= dev2
->bus
)
711 /* Find needle in haystack. If exact match found, return DEVICE_SAME,
712 * and return needle location in *index. If scsi3addr matches, but not
713 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
714 * location in *index. If needle not found, return DEVICE_NOT_FOUND.
716 static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t
*needle
,
717 struct hpsa_scsi_dev_t
*haystack
[], int haystack_size
,
721 #define DEVICE_NOT_FOUND 0
722 #define DEVICE_CHANGED 1
723 #define DEVICE_SAME 2
724 for (i
= 0; i
< haystack_size
; i
++) {
725 if (SCSI3ADDR_EQ(needle
->scsi3addr
, haystack
[i
]->scsi3addr
)) {
727 if (device_is_the_same(needle
, haystack
[i
]))
730 return DEVICE_CHANGED
;
734 return DEVICE_NOT_FOUND
;
737 static int adjust_hpsa_scsi_table(struct ctlr_info
*h
, int hostno
,
738 struct hpsa_scsi_dev_t
*sd
[], int nsds
)
740 /* sd contains scsi3 addresses and devtypes, and inquiry
741 * data. This function takes what's in sd to be the current
742 * reality and updates h->dev[] to reflect that reality.
744 int i
, entry
, device_change
, changes
= 0;
745 struct hpsa_scsi_dev_t
*csd
;
747 struct hpsa_scsi_dev_t
**added
, **removed
;
748 int nadded
, nremoved
;
749 struct Scsi_Host
*sh
= NULL
;
751 added
= kzalloc(sizeof(*added
) * HPSA_MAX_SCSI_DEVS_PER_HBA
,
753 removed
= kzalloc(sizeof(*removed
) * HPSA_MAX_SCSI_DEVS_PER_HBA
,
756 if (!added
|| !removed
) {
757 dev_warn(&h
->pdev
->dev
, "out of memory in "
758 "adjust_hpsa_scsi_table\n");
762 spin_lock_irqsave(&h
->devlock
, flags
);
764 /* find any devices in h->dev[] that are not in
765 * sd[] and remove them from h->dev[], and for any
766 * devices which have changed, remove the old device
767 * info and add the new device info.
772 while (i
< h
->ndevices
) {
774 device_change
= hpsa_scsi_find_entry(csd
, sd
, nsds
, &entry
);
775 if (device_change
== DEVICE_NOT_FOUND
) {
777 hpsa_scsi_remove_entry(h
, hostno
, i
,
779 continue; /* remove ^^^, hence i not incremented */
780 } else if (device_change
== DEVICE_CHANGED
) {
782 hpsa_scsi_remove_entry(h
, hostno
, i
,
784 (void) hpsa_scsi_add_entry(h
, hostno
, sd
[entry
],
786 /* add can't fail, we just removed one. */
787 sd
[entry
] = NULL
; /* prevent it from being freed */
792 /* Now, make sure every device listed in sd[] is also
793 * listed in h->dev[], adding them if they aren't found
796 for (i
= 0; i
< nsds
; i
++) {
797 if (!sd
[i
]) /* if already added above. */
799 device_change
= hpsa_scsi_find_entry(sd
[i
], h
->dev
,
800 h
->ndevices
, &entry
);
801 if (device_change
== DEVICE_NOT_FOUND
) {
803 if (hpsa_scsi_add_entry(h
, hostno
, sd
[i
],
804 added
, &nadded
) != 0)
806 sd
[i
] = NULL
; /* prevent from being freed later. */
807 } else if (device_change
== DEVICE_CHANGED
) {
808 /* should never happen... */
810 dev_warn(&h
->pdev
->dev
,
811 "device unexpectedly changed.\n");
812 /* but if it does happen, we just ignore that device */
815 spin_unlock_irqrestore(&h
->devlock
, flags
);
817 /* Don't notify scsi mid layer of any changes the first time through
818 * (or if there are no changes) scsi_scan_host will do it later the
819 * first time through.
821 if (hostno
== -1 || !changes
)
825 /* Notify scsi mid layer of any removed devices */
826 for (i
= 0; i
< nremoved
; i
++) {
827 struct scsi_device
*sdev
=
828 scsi_device_lookup(sh
, removed
[i
]->bus
,
829 removed
[i
]->target
, removed
[i
]->lun
);
831 scsi_remove_device(sdev
);
832 scsi_device_put(sdev
);
834 /* We don't expect to get here.
835 * future cmds to this device will get selection
836 * timeout as if the device was gone.
838 dev_warn(&h
->pdev
->dev
, "didn't find c%db%dt%dl%d "
839 " for removal.", hostno
, removed
[i
]->bus
,
840 removed
[i
]->target
, removed
[i
]->lun
);
846 /* Notify scsi mid layer of any added devices */
847 for (i
= 0; i
< nadded
; i
++) {
848 if (scsi_add_device(sh
, added
[i
]->bus
,
849 added
[i
]->target
, added
[i
]->lun
) == 0)
851 dev_warn(&h
->pdev
->dev
, "scsi_add_device c%db%dt%dl%d failed, "
852 "device not added.\n", hostno
, added
[i
]->bus
,
853 added
[i
]->target
, added
[i
]->lun
);
854 /* now we have to remove it from h->dev,
855 * since it didn't get added to scsi mid layer
857 fixup_botched_add(h
, added
[i
]);
867 * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
868 * Assume's h->devlock is held.
870 static struct hpsa_scsi_dev_t
*lookup_hpsa_scsi_dev(struct ctlr_info
*h
,
871 int bus
, int target
, int lun
)
874 struct hpsa_scsi_dev_t
*sd
;
876 for (i
= 0; i
< h
->ndevices
; i
++) {
878 if (sd
->bus
== bus
&& sd
->target
== target
&& sd
->lun
== lun
)
884 /* link sdev->hostdata to our per-device structure. */
885 static int hpsa_slave_alloc(struct scsi_device
*sdev
)
887 struct hpsa_scsi_dev_t
*sd
;
891 h
= sdev_to_hba(sdev
);
892 spin_lock_irqsave(&h
->devlock
, flags
);
893 sd
= lookup_hpsa_scsi_dev(h
, sdev_channel(sdev
),
894 sdev_id(sdev
), sdev
->lun
);
897 spin_unlock_irqrestore(&h
->devlock
, flags
);
901 static void hpsa_slave_destroy(struct scsi_device
*sdev
)
903 return; /* nothing to do. */
906 static void hpsa_scsi_setup(struct ctlr_info
*h
)
910 spin_lock_init(&h
->devlock
);
914 static void complete_scsi_command(struct CommandList
*cp
,
915 int timeout
, __u32 tag
)
917 struct scsi_cmnd
*cmd
;
919 struct ErrorInfo
*ei
;
921 unsigned char sense_key
;
922 unsigned char asc
; /* additional sense code */
923 unsigned char ascq
; /* additional sense code qualifier */
926 cmd
= (struct scsi_cmnd
*) cp
->scsi_cmd
;
929 scsi_dma_unmap(cmd
); /* undo the DMA mappings */
931 cmd
->result
= (DID_OK
<< 16); /* host byte */
932 cmd
->result
|= (COMMAND_COMPLETE
<< 8); /* msg byte */
933 cmd
->result
|= (ei
->ScsiStatus
<< 1);
935 /* copy the sense data whether we need to or not. */
936 memcpy(cmd
->sense_buffer
, ei
->SenseInfo
,
937 ei
->SenseLen
> SCSI_SENSE_BUFFERSIZE
?
938 SCSI_SENSE_BUFFERSIZE
:
940 scsi_set_resid(cmd
, ei
->ResidualCnt
);
942 if (ei
->CommandStatus
== 0) {
948 /* an error has occurred */
949 switch (ei
->CommandStatus
) {
951 case CMD_TARGET_STATUS
:
952 if (ei
->ScsiStatus
) {
954 sense_key
= 0xf & ei
->SenseInfo
[2];
955 /* Get additional sense code */
956 asc
= ei
->SenseInfo
[12];
957 /* Get addition sense code qualifier */
958 ascq
= ei
->SenseInfo
[13];
961 if (ei
->ScsiStatus
== SAM_STAT_CHECK_CONDITION
) {
962 if (check_for_unit_attention(h
, cp
)) {
963 cmd
->result
= DID_SOFT_ERROR
<< 16;
966 if (sense_key
== ILLEGAL_REQUEST
) {
968 * SCSI REPORT_LUNS is commonly unsupported on
969 * Smart Array. Suppress noisy complaint.
971 if (cp
->Request
.CDB
[0] == REPORT_LUNS
)
974 /* If ASC/ASCQ indicate Logical Unit
975 * Not Supported condition,
977 if ((asc
== 0x25) && (ascq
== 0x0)) {
978 dev_warn(&h
->pdev
->dev
, "cp %p "
979 "has check condition\n", cp
);
984 if (sense_key
== NOT_READY
) {
985 /* If Sense is Not Ready, Logical Unit
986 * Not ready, Manual Intervention
989 if ((asc
== 0x04) && (ascq
== 0x03)) {
990 cmd
->result
= DID_NO_CONNECT
<< 16;
991 dev_warn(&h
->pdev
->dev
, "cp %p "
992 "has check condition: unit "
994 "intervention required\n", cp
);
1000 /* Must be some other type of check condition */
1001 dev_warn(&h
->pdev
->dev
, "cp %p has check condition: "
1003 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1004 "Returning result: 0x%x, "
1005 "cmd=[%02x %02x %02x %02x %02x "
1006 "%02x %02x %02x %02x %02x]\n",
1007 cp
, sense_key
, asc
, ascq
,
1009 cmd
->cmnd
[0], cmd
->cmnd
[1],
1010 cmd
->cmnd
[2], cmd
->cmnd
[3],
1011 cmd
->cmnd
[4], cmd
->cmnd
[5],
1012 cmd
->cmnd
[6], cmd
->cmnd
[7],
1013 cmd
->cmnd
[8], cmd
->cmnd
[9]);
1018 /* Problem was not a check condition
1019 * Pass it up to the upper layers...
1021 if (ei
->ScsiStatus
) {
1022 dev_warn(&h
->pdev
->dev
, "cp %p has status 0x%x "
1023 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1024 "Returning result: 0x%x\n",
1026 sense_key
, asc
, ascq
,
1028 } else { /* scsi status is zero??? How??? */
1029 dev_warn(&h
->pdev
->dev
, "cp %p SCSI status was 0. "
1030 "Returning no connection.\n", cp
),
1032 /* Ordinarily, this case should never happen,
1033 * but there is a bug in some released firmware
1034 * revisions that allows it to happen if, for
1035 * example, a 4100 backplane loses power and
1036 * the tape drive is in it. We assume that
1037 * it's a fatal error of some kind because we
1038 * can't show that it wasn't. We will make it
1039 * look like selection timeout since that is
1040 * the most common reason for this to occur,
1041 * and it's severe enough.
1044 cmd
->result
= DID_NO_CONNECT
<< 16;
1048 case CMD_DATA_UNDERRUN
: /* let mid layer handle it. */
1050 case CMD_DATA_OVERRUN
:
1051 dev_warn(&h
->pdev
->dev
, "cp %p has"
1052 " completed with data overrun "
1056 /* print_bytes(cp, sizeof(*cp), 1, 0);
1058 /* We get CMD_INVALID if you address a non-existent device
1059 * instead of a selection timeout (no response). You will
1060 * see this if you yank out a drive, then try to access it.
1061 * This is kind of a shame because it means that any other
1062 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1063 * missing target. */
1064 cmd
->result
= DID_NO_CONNECT
<< 16;
1067 case CMD_PROTOCOL_ERR
:
1068 dev_warn(&h
->pdev
->dev
, "cp %p has "
1069 "protocol error \n", cp
);
1071 case CMD_HARDWARE_ERR
:
1072 cmd
->result
= DID_ERROR
<< 16;
1073 dev_warn(&h
->pdev
->dev
, "cp %p had hardware error\n", cp
);
1075 case CMD_CONNECTION_LOST
:
1076 cmd
->result
= DID_ERROR
<< 16;
1077 dev_warn(&h
->pdev
->dev
, "cp %p had connection lost\n", cp
);
1080 cmd
->result
= DID_ABORT
<< 16;
1081 dev_warn(&h
->pdev
->dev
, "cp %p was aborted with status 0x%x\n",
1082 cp
, ei
->ScsiStatus
);
1084 case CMD_ABORT_FAILED
:
1085 cmd
->result
= DID_ERROR
<< 16;
1086 dev_warn(&h
->pdev
->dev
, "cp %p reports abort failed\n", cp
);
1088 case CMD_UNSOLICITED_ABORT
:
1089 cmd
->result
= DID_ABORT
<< 16;
1090 dev_warn(&h
->pdev
->dev
, "cp %p aborted do to an unsolicited "
1094 cmd
->result
= DID_TIME_OUT
<< 16;
1095 dev_warn(&h
->pdev
->dev
, "cp %p timedout\n", cp
);
1098 cmd
->result
= DID_ERROR
<< 16;
1099 dev_warn(&h
->pdev
->dev
, "cp %p returned unknown status %x\n",
1100 cp
, ei
->CommandStatus
);
1102 cmd
->scsi_done(cmd
);
1106 static int hpsa_scsi_detect(struct ctlr_info
*h
)
1108 struct Scsi_Host
*sh
;
1111 sh
= scsi_host_alloc(&hpsa_driver_template
, sizeof(h
));
1118 sh
->max_channel
= 3;
1119 sh
->max_cmd_len
= MAX_COMMAND_SIZE
;
1120 sh
->max_lun
= HPSA_MAX_LUN
;
1121 sh
->max_id
= HPSA_MAX_LUN
;
1123 sh
->hostdata
[0] = (unsigned long) h
;
1124 sh
->irq
= h
->intr
[SIMPLE_MODE_INT
];
1125 sh
->unique_id
= sh
->irq
;
1126 error
= scsi_add_host(sh
, &h
->pdev
->dev
);
1133 dev_err(&h
->pdev
->dev
, "hpsa_scsi_detect: scsi_add_host"
1134 " failed for controller %d\n", h
->ctlr
);
1138 dev_err(&h
->pdev
->dev
, "hpsa_scsi_detect: scsi_host_alloc"
1139 " failed for controller %d\n", h
->ctlr
);
1143 static void hpsa_pci_unmap(struct pci_dev
*pdev
,
1144 struct CommandList
*c
, int sg_used
, int data_direction
)
1147 union u64bit addr64
;
1149 for (i
= 0; i
< sg_used
; i
++) {
1150 addr64
.val32
.lower
= c
->SG
[i
].Addr
.lower
;
1151 addr64
.val32
.upper
= c
->SG
[i
].Addr
.upper
;
1152 pci_unmap_single(pdev
, (dma_addr_t
) addr64
.val
, c
->SG
[i
].Len
,
1157 static void hpsa_map_one(struct pci_dev
*pdev
,
1158 struct CommandList
*cp
,
1165 if (buflen
== 0 || data_direction
== PCI_DMA_NONE
) {
1166 cp
->Header
.SGList
= 0;
1167 cp
->Header
.SGTotal
= 0;
1171 addr64
= (__u64
) pci_map_single(pdev
, buf
, buflen
, data_direction
);
1172 cp
->SG
[0].Addr
.lower
=
1173 (__u32
) (addr64
& (__u64
) 0x00000000FFFFFFFF);
1174 cp
->SG
[0].Addr
.upper
=
1175 (__u32
) ((addr64
>> 32) & (__u64
) 0x00000000FFFFFFFF);
1176 cp
->SG
[0].Len
= buflen
;
1177 cp
->Header
.SGList
= (__u8
) 1; /* no. SGs contig in this cmd */
1178 cp
->Header
.SGTotal
= (__u16
) 1; /* total sgs in this cmd list */
1181 static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info
*h
,
1182 struct CommandList
*c
)
1184 DECLARE_COMPLETION_ONSTACK(wait
);
1187 enqueue_cmd_and_start_io(h
, c
);
1188 wait_for_completion(&wait
);
1191 static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info
*h
,
1192 struct CommandList
*c
, int data_direction
)
1194 int retry_count
= 0;
1197 memset(c
->err_info
, 0, sizeof(c
->err_info
));
1198 hpsa_scsi_do_simple_cmd_core(h
, c
);
1200 } while (check_for_unit_attention(h
, c
) && retry_count
<= 3);
1201 hpsa_pci_unmap(h
->pdev
, c
, 1, data_direction
);
1204 static void hpsa_scsi_interpret_error(struct CommandList
*cp
)
1206 struct ErrorInfo
*ei
;
1207 struct device
*d
= &cp
->h
->pdev
->dev
;
1210 switch (ei
->CommandStatus
) {
1211 case CMD_TARGET_STATUS
:
1212 dev_warn(d
, "cmd %p has completed with errors\n", cp
);
1213 dev_warn(d
, "cmd %p has SCSI Status = %x\n", cp
,
1215 if (ei
->ScsiStatus
== 0)
1216 dev_warn(d
, "SCSI status is abnormally zero. "
1217 "(probably indicates selection timeout "
1218 "reported incorrectly due to a known "
1219 "firmware bug, circa July, 2001.)\n");
1221 case CMD_DATA_UNDERRUN
: /* let mid layer handle it. */
1222 dev_info(d
, "UNDERRUN\n");
1224 case CMD_DATA_OVERRUN
:
1225 dev_warn(d
, "cp %p has completed with data overrun\n", cp
);
1228 /* controller unfortunately reports SCSI passthru's
1229 * to non-existent targets as invalid commands.
1231 dev_warn(d
, "cp %p is reported invalid (probably means "
1232 "target device no longer present)\n", cp
);
1233 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1237 case CMD_PROTOCOL_ERR
:
1238 dev_warn(d
, "cp %p has protocol error \n", cp
);
1240 case CMD_HARDWARE_ERR
:
1241 /* cmd->result = DID_ERROR << 16; */
1242 dev_warn(d
, "cp %p had hardware error\n", cp
);
1244 case CMD_CONNECTION_LOST
:
1245 dev_warn(d
, "cp %p had connection lost\n", cp
);
1248 dev_warn(d
, "cp %p was aborted\n", cp
);
1250 case CMD_ABORT_FAILED
:
1251 dev_warn(d
, "cp %p reports abort failed\n", cp
);
1253 case CMD_UNSOLICITED_ABORT
:
1254 dev_warn(d
, "cp %p aborted due to an unsolicited abort\n", cp
);
1257 dev_warn(d
, "cp %p timed out\n", cp
);
1260 dev_warn(d
, "cp %p returned unknown status %x\n", cp
,
1265 static int hpsa_scsi_do_inquiry(struct ctlr_info
*h
, unsigned char *scsi3addr
,
1266 unsigned char page
, unsigned char *buf
,
1267 unsigned char bufsize
)
1270 struct CommandList
*c
;
1271 struct ErrorInfo
*ei
;
1273 c
= cmd_special_alloc(h
);
1275 if (c
== NULL
) { /* trouble... */
1276 dev_warn(&h
->pdev
->dev
, "cmd_special_alloc returned NULL!\n");
1280 fill_cmd(c
, HPSA_INQUIRY
, h
, buf
, bufsize
, page
, scsi3addr
, TYPE_CMD
);
1281 hpsa_scsi_do_simple_cmd_with_retry(h
, c
, PCI_DMA_FROMDEVICE
);
1283 if (ei
->CommandStatus
!= 0 && ei
->CommandStatus
!= CMD_DATA_UNDERRUN
) {
1284 hpsa_scsi_interpret_error(c
);
1287 cmd_special_free(h
, c
);
1291 static int hpsa_send_reset(struct ctlr_info
*h
, unsigned char *scsi3addr
)
1294 struct CommandList
*c
;
1295 struct ErrorInfo
*ei
;
1297 c
= cmd_special_alloc(h
);
1299 if (c
== NULL
) { /* trouble... */
1300 dev_warn(&h
->pdev
->dev
, "cmd_special_alloc returned NULL!\n");
1304 fill_cmd(c
, HPSA_DEVICE_RESET_MSG
, h
, NULL
, 0, 0, scsi3addr
, TYPE_MSG
);
1305 hpsa_scsi_do_simple_cmd_core(h
, c
);
1306 /* no unmap needed here because no data xfer. */
1309 if (ei
->CommandStatus
!= 0) {
1310 hpsa_scsi_interpret_error(c
);
1313 cmd_special_free(h
, c
);
1317 static void hpsa_get_raid_level(struct ctlr_info
*h
,
1318 unsigned char *scsi3addr
, unsigned char *raid_level
)
1323 *raid_level
= RAID_UNKNOWN
;
1324 buf
= kzalloc(64, GFP_KERNEL
);
1327 rc
= hpsa_scsi_do_inquiry(h
, scsi3addr
, 0xC1, buf
, 64);
1329 *raid_level
= buf
[8];
1330 if (*raid_level
> RAID_UNKNOWN
)
1331 *raid_level
= RAID_UNKNOWN
;
1336 /* Get the device id from inquiry page 0x83 */
1337 static int hpsa_get_device_id(struct ctlr_info
*h
, unsigned char *scsi3addr
,
1338 unsigned char *device_id
, int buflen
)
1345 buf
= kzalloc(64, GFP_KERNEL
);
1348 rc
= hpsa_scsi_do_inquiry(h
, scsi3addr
, 0x83, buf
, 64);
1350 memcpy(device_id
, &buf
[8], buflen
);
1355 static int hpsa_scsi_do_report_luns(struct ctlr_info
*h
, int logical
,
1356 struct ReportLUNdata
*buf
, int bufsize
,
1357 int extended_response
)
1360 struct CommandList
*c
;
1361 unsigned char scsi3addr
[8];
1362 struct ErrorInfo
*ei
;
1364 c
= cmd_special_alloc(h
);
1365 if (c
== NULL
) { /* trouble... */
1366 dev_err(&h
->pdev
->dev
, "cmd_special_alloc returned NULL!\n");
1370 memset(&scsi3addr
[0], 0, 8); /* address the controller */
1372 fill_cmd(c
, logical
? HPSA_REPORT_LOG
: HPSA_REPORT_PHYS
, h
,
1373 buf
, bufsize
, 0, scsi3addr
, TYPE_CMD
);
1374 if (extended_response
)
1375 c
->Request
.CDB
[1] = extended_response
;
1376 hpsa_scsi_do_simple_cmd_with_retry(h
, c
, PCI_DMA_FROMDEVICE
);
1378 if (ei
->CommandStatus
!= 0 &&
1379 ei
->CommandStatus
!= CMD_DATA_UNDERRUN
) {
1380 hpsa_scsi_interpret_error(c
);
1383 cmd_special_free(h
, c
);
1387 static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info
*h
,
1388 struct ReportLUNdata
*buf
,
1389 int bufsize
, int extended_response
)
1391 return hpsa_scsi_do_report_luns(h
, 0, buf
, bufsize
, extended_response
);
1394 static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info
*h
,
1395 struct ReportLUNdata
*buf
, int bufsize
)
1397 return hpsa_scsi_do_report_luns(h
, 1, buf
, bufsize
, 0);
1400 static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t
*device
,
1401 int bus
, int target
, int lun
)
1404 device
->target
= target
;
1408 static int hpsa_update_device_info(struct ctlr_info
*h
,
1409 unsigned char scsi3addr
[], struct hpsa_scsi_dev_t
*this_device
)
1411 #define OBDR_TAPE_INQ_SIZE 49
1412 unsigned char *inq_buff
= NULL
;
1414 inq_buff
= kmalloc(OBDR_TAPE_INQ_SIZE
, GFP_KERNEL
);
1418 memset(inq_buff
, 0, OBDR_TAPE_INQ_SIZE
);
1419 /* Do an inquiry to the device to see what it is. */
1420 if (hpsa_scsi_do_inquiry(h
, scsi3addr
, 0, inq_buff
,
1421 (unsigned char) OBDR_TAPE_INQ_SIZE
) != 0) {
1422 /* Inquiry failed (msg printed already) */
1423 dev_err(&h
->pdev
->dev
,
1424 "hpsa_update_device_info: inquiry failed\n");
1428 /* As a side effect, record the firmware version number
1429 * if we happen to be talking to the RAID controller.
1431 if (is_hba_lunid(scsi3addr
))
1432 memcpy(h
->firm_ver
, &inq_buff
[32], 4);
1434 this_device
->devtype
= (inq_buff
[0] & 0x1f);
1435 memcpy(this_device
->scsi3addr
, scsi3addr
, 8);
1436 memcpy(this_device
->vendor
, &inq_buff
[8],
1437 sizeof(this_device
->vendor
));
1438 memcpy(this_device
->model
, &inq_buff
[16],
1439 sizeof(this_device
->model
));
1440 memcpy(this_device
->revision
, &inq_buff
[32],
1441 sizeof(this_device
->revision
));
1442 memset(this_device
->device_id
, 0,
1443 sizeof(this_device
->device_id
));
1444 hpsa_get_device_id(h
, scsi3addr
, this_device
->device_id
,
1445 sizeof(this_device
->device_id
));
1447 if (this_device
->devtype
== TYPE_DISK
&&
1448 is_logical_dev_addr_mode(scsi3addr
))
1449 hpsa_get_raid_level(h
, scsi3addr
, &this_device
->raid_level
);
1451 this_device
->raid_level
= RAID_UNKNOWN
;
1461 static unsigned char *msa2xxx_model
[] = {
1469 static int is_msa2xxx(struct ctlr_info
*h
, struct hpsa_scsi_dev_t
*device
)
1473 for (i
= 0; msa2xxx_model
[i
]; i
++)
1474 if (strncmp(device
->model
, msa2xxx_model
[i
],
1475 strlen(msa2xxx_model
[i
])) == 0)
1480 /* Helper function to assign bus, target, lun mapping of devices.
1481 * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
1482 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
1483 * Logical drive target and lun are assigned at this time, but
1484 * physical device lun and target assignment are deferred (assigned
1485 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
1487 static void figure_bus_target_lun(struct ctlr_info
*h
,
1488 __u8
*lunaddrbytes
, int *bus
, int *target
, int *lun
,
1489 struct hpsa_scsi_dev_t
*device
)
1494 if (is_logical_dev_addr_mode(lunaddrbytes
)) {
1495 /* logical device */
1496 memcpy(&lunid
, lunaddrbytes
, sizeof(lunid
));
1497 lunid
= le32_to_cpu(lunid
);
1499 if (is_msa2xxx(h
, device
)) {
1501 *target
= (lunid
>> 16) & 0x3fff;
1502 *lun
= lunid
& 0x00ff;
1506 *target
= lunid
& 0x3fff;
1509 /* physical device */
1510 if (is_hba_lunid(lunaddrbytes
))
1515 *lun
= -1; /* we will fill these in later. */
1520 * If there is no lun 0 on a target, linux won't find any devices.
1521 * For the MSA2xxx boxes, we have to manually detect the enclosure
1522 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
1523 * it for some reason. *tmpdevice is the target we're adding,
1524 * this_device is a pointer into the current element of currentsd[]
1525 * that we're building up in update_scsi_devices(), below.
1526 * lunzerobits is a bitmap that tracks which targets already have a
1528 * Returns 1 if an enclosure was added, 0 if not.
1530 static int add_msa2xxx_enclosure_device(struct ctlr_info
*h
,
1531 struct hpsa_scsi_dev_t
*tmpdevice
,
1532 struct hpsa_scsi_dev_t
*this_device
, __u8
*lunaddrbytes
,
1533 int bus
, int target
, int lun
, unsigned long lunzerobits
[],
1534 int *nmsa2xxx_enclosures
)
1536 unsigned char scsi3addr
[8];
1538 if (test_bit(target
, lunzerobits
))
1539 return 0; /* There is already a lun 0 on this target. */
1541 if (!is_logical_dev_addr_mode(lunaddrbytes
))
1542 return 0; /* It's the logical targets that may lack lun 0. */
1544 if (!is_msa2xxx(h
, tmpdevice
))
1545 return 0; /* It's only the MSA2xxx that have this problem. */
1547 if (lun
== 0) /* if lun is 0, then obviously we have a lun 0. */
1550 if (is_hba_lunid(scsi3addr
))
1551 return 0; /* Don't add the RAID controller here. */
1553 #define MAX_MSA2XXX_ENCLOSURES 32
1554 if (*nmsa2xxx_enclosures
>= MAX_MSA2XXX_ENCLOSURES
) {
1555 dev_warn(&h
->pdev
->dev
, "Maximum number of MSA2XXX "
1556 "enclosures exceeded. Check your hardware "
1561 memset(scsi3addr
, 0, 8);
1562 scsi3addr
[3] = target
;
1563 if (hpsa_update_device_info(h
, scsi3addr
, this_device
))
1565 (*nmsa2xxx_enclosures
)++;
1566 hpsa_set_bus_target_lun(this_device
, bus
, target
, 0);
1567 set_bit(target
, lunzerobits
);
1572 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
1573 * logdev. The number of luns in physdev and logdev are returned in
1574 * *nphysicals and *nlogicals, respectively.
1575 * Returns 0 on success, -1 otherwise.
1577 static int hpsa_gather_lun_info(struct ctlr_info
*h
,
1579 struct ReportLUNdata
*physdev
, __u32
*nphysicals
,
1580 struct ReportLUNdata
*logdev
, __u32
*nlogicals
)
1582 if (hpsa_scsi_do_report_phys_luns(h
, physdev
, reportlunsize
, 0)) {
1583 dev_err(&h
->pdev
->dev
, "report physical LUNs failed.\n");
1586 memcpy(nphysicals
, &physdev
->LUNListLength
[0], sizeof(*nphysicals
));
1587 *nphysicals
= be32_to_cpu(*nphysicals
) / 8;
1589 dev_info(&h
->pdev
->dev
, "number of physical luns is %d\n", *nphysicals
);
1591 if (*nphysicals
> HPSA_MAX_PHYS_LUN
) {
1592 dev_warn(&h
->pdev
->dev
, "maximum physical LUNs (%d) exceeded."
1593 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN
,
1594 *nphysicals
- HPSA_MAX_PHYS_LUN
);
1595 *nphysicals
= HPSA_MAX_PHYS_LUN
;
1597 if (hpsa_scsi_do_report_log_luns(h
, logdev
, reportlunsize
)) {
1598 dev_err(&h
->pdev
->dev
, "report logical LUNs failed.\n");
1601 memcpy(nlogicals
, &logdev
->LUNListLength
[0], sizeof(*nlogicals
));
1602 *nlogicals
= be32_to_cpu(*nlogicals
) / 8;
1604 dev_info(&h
->pdev
->dev
, "number of logical luns is %d\n", *nlogicals
);
1606 /* Reject Logicals in excess of our max capability. */
1607 if (*nlogicals
> HPSA_MAX_LUN
) {
1608 dev_warn(&h
->pdev
->dev
,
1609 "maximum logical LUNs (%d) exceeded. "
1610 "%d LUNs ignored.\n", HPSA_MAX_LUN
,
1611 *nlogicals
- HPSA_MAX_LUN
);
1612 *nlogicals
= HPSA_MAX_LUN
;
1614 if (*nlogicals
+ *nphysicals
> HPSA_MAX_PHYS_LUN
) {
1615 dev_warn(&h
->pdev
->dev
,
1616 "maximum logical + physical LUNs (%d) exceeded. "
1617 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN
,
1618 *nphysicals
+ *nlogicals
- HPSA_MAX_PHYS_LUN
);
1619 *nlogicals
= HPSA_MAX_PHYS_LUN
- *nphysicals
;
1624 static void hpsa_update_scsi_devices(struct ctlr_info
*h
, int hostno
)
1626 /* the idea here is we could get notified
1627 * that some devices have changed, so we do a report
1628 * physical luns and report logical luns cmd, and adjust
1629 * our list of devices accordingly.
1631 * The scsi3addr's of devices won't change so long as the
1632 * adapter is not reset. That means we can rescan and
1633 * tell which devices we already know about, vs. new
1634 * devices, vs. disappearing devices.
1636 struct ReportLUNdata
*physdev_list
= NULL
;
1637 struct ReportLUNdata
*logdev_list
= NULL
;
1638 unsigned char *inq_buff
= NULL
;
1639 __u32 nphysicals
= 0;
1640 __u32 nlogicals
= 0;
1641 __u32 ndev_allocated
= 0;
1642 struct hpsa_scsi_dev_t
**currentsd
, *this_device
, *tmpdevice
;
1644 int reportlunsize
= sizeof(*physdev_list
) + HPSA_MAX_PHYS_LUN
* 8;
1645 int i
, nmsa2xxx_enclosures
, ndevs_to_allocate
;
1646 int bus
, target
, lun
;
1647 DECLARE_BITMAP(lunzerobits
, HPSA_MAX_TARGETS_PER_CTLR
);
1649 currentsd
= kzalloc(sizeof(*currentsd
) * HPSA_MAX_SCSI_DEVS_PER_HBA
,
1651 physdev_list
= kzalloc(reportlunsize
, GFP_KERNEL
);
1652 logdev_list
= kzalloc(reportlunsize
, GFP_KERNEL
);
1653 inq_buff
= kmalloc(OBDR_TAPE_INQ_SIZE
, GFP_KERNEL
);
1654 tmpdevice
= kzalloc(sizeof(*tmpdevice
), GFP_KERNEL
);
1656 if (!currentsd
|| !physdev_list
|| !logdev_list
||
1657 !inq_buff
|| !tmpdevice
) {
1658 dev_err(&h
->pdev
->dev
, "out of memory\n");
1661 memset(lunzerobits
, 0, sizeof(lunzerobits
));
1663 if (hpsa_gather_lun_info(h
, reportlunsize
, physdev_list
, &nphysicals
,
1664 logdev_list
, &nlogicals
))
1667 /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
1668 * but each of them 4 times through different paths. The plus 1
1669 * is for the RAID controller.
1671 ndevs_to_allocate
= nphysicals
+ nlogicals
+ MAX_MSA2XXX_ENCLOSURES
+ 1;
1673 /* Allocate the per device structures */
1674 for (i
= 0; i
< ndevs_to_allocate
; i
++) {
1675 currentsd
[i
] = kzalloc(sizeof(*currentsd
[i
]), GFP_KERNEL
);
1676 if (!currentsd
[i
]) {
1677 dev_warn(&h
->pdev
->dev
, "out of memory at %s:%d\n",
1678 __FILE__
, __LINE__
);
1684 /* adjust our table of devices */
1685 nmsa2xxx_enclosures
= 0;
1686 for (i
= 0; i
< nphysicals
+ nlogicals
+ 1; i
++) {
1689 /* Figure out where the LUN ID info is coming from */
1691 lunaddrbytes
= &physdev_list
->LUN
[i
][0];
1693 if (i
< nphysicals
+ nlogicals
)
1695 &logdev_list
->LUN
[i
-nphysicals
][0];
1696 else /* jam in the RAID controller at the end */
1697 lunaddrbytes
= RAID_CTLR_LUNID
;
1699 /* skip masked physical devices. */
1700 if (lunaddrbytes
[3] & 0xC0 && i
< nphysicals
)
1703 /* Get device type, vendor, model, device id */
1704 if (hpsa_update_device_info(h
, lunaddrbytes
, tmpdevice
))
1705 continue; /* skip it if we can't talk to it. */
1706 figure_bus_target_lun(h
, lunaddrbytes
, &bus
, &target
, &lun
,
1708 this_device
= currentsd
[ncurrent
];
1711 * For the msa2xxx boxes, we have to insert a LUN 0 which
1712 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
1713 * is nonetheless an enclosure device there. We have to
1714 * present that otherwise linux won't find anything if
1715 * there is no lun 0.
1717 if (add_msa2xxx_enclosure_device(h
, tmpdevice
, this_device
,
1718 lunaddrbytes
, bus
, target
, lun
, lunzerobits
,
1719 &nmsa2xxx_enclosures
)) {
1721 this_device
= currentsd
[ncurrent
];
1724 *this_device
= *tmpdevice
;
1725 hpsa_set_bus_target_lun(this_device
, bus
, target
, lun
);
1727 switch (this_device
->devtype
) {
1729 /* We don't *really* support actual CD-ROM devices,
1730 * just "One Button Disaster Recovery" tape drive
1731 * which temporarily pretends to be a CD-ROM drive.
1732 * So we check that the device is really an OBDR tape
1733 * device by checking for "$DR-10" in bytes 43-48 of
1737 #define OBDR_TAPE_SIG "$DR-10"
1738 strncpy(obdr_sig
, &inq_buff
[43], 6);
1740 if (strncmp(obdr_sig
, OBDR_TAPE_SIG
, 6) != 0)
1741 /* Not OBDR device, ignore it. */
1752 case TYPE_MEDIUM_CHANGER
:
1756 /* Only present the Smartarray HBA as a RAID controller.
1757 * If it's a RAID controller other than the HBA itself
1758 * (an external RAID controller, MSA500 or similar)
1761 if (!is_hba_lunid(lunaddrbytes
))
1768 if (ncurrent
>= HPSA_MAX_SCSI_DEVS_PER_HBA
)
1771 adjust_hpsa_scsi_table(h
, hostno
, currentsd
, ncurrent
);
1774 for (i
= 0; i
< ndev_allocated
; i
++)
1775 kfree(currentsd
[i
]);
1778 kfree(physdev_list
);
1783 /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1784 * dma mapping and fills in the scatter gather entries of the
1787 static int hpsa_scatter_gather(struct pci_dev
*pdev
,
1788 struct CommandList
*cp
,
1789 struct scsi_cmnd
*cmd
)
1792 struct scatterlist
*sg
;
1796 BUG_ON(scsi_sg_count(cmd
) > MAXSGENTRIES
);
1798 use_sg
= scsi_dma_map(cmd
);
1803 goto sglist_finished
;
1805 scsi_for_each_sg(cmd
, sg
, use_sg
, i
) {
1806 addr64
= (__u64
) sg_dma_address(sg
);
1807 len
= sg_dma_len(sg
);
1808 cp
->SG
[i
].Addr
.lower
=
1809 (__u32
) (addr64
& (__u64
) 0x00000000FFFFFFFF);
1810 cp
->SG
[i
].Addr
.upper
=
1811 (__u32
) ((addr64
>> 32) & (__u64
) 0x00000000FFFFFFFF);
1812 cp
->SG
[i
].Len
= len
;
1813 cp
->SG
[i
].Ext
= 0; /* we are not chaining */
1818 cp
->Header
.SGList
= (__u8
) use_sg
; /* no. SGs contig in this cmd */
1819 cp
->Header
.SGTotal
= (__u16
) use_sg
; /* total sgs in this cmd list */
1824 static int hpsa_scsi_queue_command(struct scsi_cmnd
*cmd
,
1825 void (*done
)(struct scsi_cmnd
*))
1827 struct ctlr_info
*h
;
1828 struct hpsa_scsi_dev_t
*dev
;
1829 unsigned char scsi3addr
[8];
1830 struct CommandList
*c
;
1831 unsigned long flags
;
1833 /* Get the ptr to our adapter structure out of cmd->host. */
1834 h
= sdev_to_hba(cmd
->device
);
1835 dev
= cmd
->device
->hostdata
;
1837 cmd
->result
= DID_NO_CONNECT
<< 16;
1841 memcpy(scsi3addr
, dev
->scsi3addr
, sizeof(scsi3addr
));
1843 /* Need a lock as this is being allocated from the pool */
1844 spin_lock_irqsave(&h
->lock
, flags
);
1846 spin_unlock_irqrestore(&h
->lock
, flags
);
1847 if (c
== NULL
) { /* trouble... */
1848 dev_err(&h
->pdev
->dev
, "cmd_alloc returned NULL!\n");
1849 return SCSI_MLQUEUE_HOST_BUSY
;
1852 /* Fill in the command list header */
1854 cmd
->scsi_done
= done
; /* save this for use by completion code */
1856 /* save c in case we have to abort it */
1857 cmd
->host_scribble
= (unsigned char *) c
;
1859 c
->cmd_type
= CMD_SCSI
;
1861 c
->Header
.ReplyQueue
= 0; /* unused in simple mode */
1862 memcpy(&c
->Header
.LUN
.LunAddrBytes
[0], &scsi3addr
[0], 8);
1863 c
->Header
.Tag
.lower
= c
->busaddr
; /* Use k. address of cmd as tag */
1865 /* Fill in the request block... */
1867 c
->Request
.Timeout
= 0;
1868 memset(c
->Request
.CDB
, 0, sizeof(c
->Request
.CDB
));
1869 BUG_ON(cmd
->cmd_len
> sizeof(c
->Request
.CDB
));
1870 c
->Request
.CDBLen
= cmd
->cmd_len
;
1871 memcpy(c
->Request
.CDB
, cmd
->cmnd
, cmd
->cmd_len
);
1872 c
->Request
.Type
.Type
= TYPE_CMD
;
1873 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
1874 switch (cmd
->sc_data_direction
) {
1876 c
->Request
.Type
.Direction
= XFER_WRITE
;
1878 case DMA_FROM_DEVICE
:
1879 c
->Request
.Type
.Direction
= XFER_READ
;
1882 c
->Request
.Type
.Direction
= XFER_NONE
;
1884 case DMA_BIDIRECTIONAL
:
1885 /* This can happen if a buggy application does a scsi passthru
1886 * and sets both inlen and outlen to non-zero. ( see
1887 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1890 c
->Request
.Type
.Direction
= XFER_RSVD
;
1891 /* This is technically wrong, and hpsa controllers should
1892 * reject it with CMD_INVALID, which is the most correct
1893 * response, but non-fibre backends appear to let it
1894 * slide by, and give the same results as if this field
1895 * were set correctly. Either way is acceptable for
1896 * our purposes here.
1902 dev_err(&h
->pdev
->dev
, "unknown data direction: %d\n",
1903 cmd
->sc_data_direction
);
1908 if (hpsa_scatter_gather(h
->pdev
, c
, cmd
) < 0) { /* Fill SG list */
1910 return SCSI_MLQUEUE_HOST_BUSY
;
1912 enqueue_cmd_and_start_io(h
, c
);
1913 /* the cmd'll come back via intr handler in complete_scsi_command() */
1917 static void hpsa_unregister_scsi(struct ctlr_info
*h
)
1919 /* we are being forcibly unloaded, and may not refuse. */
1920 scsi_remove_host(h
->scsi_host
);
1921 scsi_host_put(h
->scsi_host
);
1922 h
->scsi_host
= NULL
;
1925 static int hpsa_register_scsi(struct ctlr_info
*h
)
1929 hpsa_update_scsi_devices(h
, -1);
1930 rc
= hpsa_scsi_detect(h
);
1932 dev_err(&h
->pdev
->dev
, "hpsa_register_scsi: failed"
1933 " hpsa_scsi_detect(), rc is %d\n", rc
);
1937 static int wait_for_device_to_become_ready(struct ctlr_info
*h
,
1938 unsigned char lunaddr
[])
1942 int waittime
= 1; /* seconds */
1943 struct CommandList
*c
;
1945 c
= cmd_special_alloc(h
);
1947 dev_warn(&h
->pdev
->dev
, "out of memory in "
1948 "wait_for_device_to_become_ready.\n");
1952 /* Send test unit ready until device ready, or give up. */
1953 while (count
< HPSA_TUR_RETRY_LIMIT
) {
1955 /* Wait for a bit. do this first, because if we send
1956 * the TUR right away, the reset will just abort it.
1958 msleep(1000 * waittime
);
1961 /* Increase wait time with each try, up to a point. */
1962 if (waittime
< HPSA_MAX_WAIT_INTERVAL_SECS
)
1963 waittime
= waittime
* 2;
1965 /* Send the Test Unit Ready */
1966 fill_cmd(c
, TEST_UNIT_READY
, h
, NULL
, 0, 0, lunaddr
, TYPE_CMD
);
1967 hpsa_scsi_do_simple_cmd_core(h
, c
);
1968 /* no unmap needed here because no data xfer. */
1970 if (c
->err_info
->CommandStatus
== CMD_SUCCESS
)
1973 if (c
->err_info
->CommandStatus
== CMD_TARGET_STATUS
&&
1974 c
->err_info
->ScsiStatus
== SAM_STAT_CHECK_CONDITION
&&
1975 (c
->err_info
->SenseInfo
[2] == NO_SENSE
||
1976 c
->err_info
->SenseInfo
[2] == UNIT_ATTENTION
))
1979 dev_warn(&h
->pdev
->dev
, "waiting %d secs "
1980 "for device to become ready.\n", waittime
);
1981 rc
= 1; /* device not ready. */
1985 dev_warn(&h
->pdev
->dev
, "giving up on device.\n");
1987 dev_warn(&h
->pdev
->dev
, "device is ready.\n");
1989 cmd_special_free(h
, c
);
1993 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
1994 * complaining. Doing a host- or bus-reset can't do anything good here.
1996 static int hpsa_eh_device_reset_handler(struct scsi_cmnd
*scsicmd
)
1999 struct ctlr_info
*h
;
2000 struct hpsa_scsi_dev_t
*dev
;
2002 /* find the controller to which the command to be aborted was sent */
2003 h
= sdev_to_hba(scsicmd
->device
);
2004 if (h
== NULL
) /* paranoia */
2006 dev_warn(&h
->pdev
->dev
, "resetting drive\n");
2008 dev
= scsicmd
->device
->hostdata
;
2010 dev_err(&h
->pdev
->dev
, "hpsa_eh_device_reset_handler: "
2011 "device lookup failed.\n");
2014 /* send a reset to the SCSI LUN which the command was sent to */
2015 rc
= hpsa_send_reset(h
, dev
->scsi3addr
);
2016 if (rc
== 0 && wait_for_device_to_become_ready(h
, dev
->scsi3addr
) == 0)
2019 dev_warn(&h
->pdev
->dev
, "resetting device failed.\n");
2024 * For operations that cannot sleep, a command block is allocated at init,
2025 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
2026 * which ones are free or in use. Lock must be held when calling this.
2027 * cmd_free() is the complement.
2029 static struct CommandList
*cmd_alloc(struct ctlr_info
*h
)
2031 struct CommandList
*c
;
2033 union u64bit temp64
;
2034 dma_addr_t cmd_dma_handle
, err_dma_handle
;
2037 i
= find_first_zero_bit(h
->cmd_pool_bits
, h
->nr_cmds
);
2038 if (i
== h
->nr_cmds
)
2040 } while (test_and_set_bit
2041 (i
& (BITS_PER_LONG
- 1),
2042 h
->cmd_pool_bits
+ (i
/ BITS_PER_LONG
)) != 0);
2043 c
= h
->cmd_pool
+ i
;
2044 memset(c
, 0, sizeof(*c
));
2045 cmd_dma_handle
= h
->cmd_pool_dhandle
2047 c
->err_info
= h
->errinfo_pool
+ i
;
2048 memset(c
->err_info
, 0, sizeof(*c
->err_info
));
2049 err_dma_handle
= h
->errinfo_pool_dhandle
2050 + i
* sizeof(*c
->err_info
);
2055 INIT_HLIST_NODE(&c
->list
);
2056 c
->busaddr
= (__u32
) cmd_dma_handle
;
2057 temp64
.val
= (__u64
) err_dma_handle
;
2058 c
->ErrDesc
.Addr
.lower
= temp64
.val32
.lower
;
2059 c
->ErrDesc
.Addr
.upper
= temp64
.val32
.upper
;
2060 c
->ErrDesc
.Len
= sizeof(*c
->err_info
);
2066 /* For operations that can wait for kmalloc to possibly sleep,
2067 * this routine can be called. Lock need not be held to call
2068 * cmd_special_alloc. cmd_special_free() is the complement.
2070 static struct CommandList
*cmd_special_alloc(struct ctlr_info
*h
)
2072 struct CommandList
*c
;
2073 union u64bit temp64
;
2074 dma_addr_t cmd_dma_handle
, err_dma_handle
;
2076 c
= pci_alloc_consistent(h
->pdev
, sizeof(*c
), &cmd_dma_handle
);
2079 memset(c
, 0, sizeof(*c
));
2083 c
->err_info
= pci_alloc_consistent(h
->pdev
, sizeof(*c
->err_info
),
2086 if (c
->err_info
== NULL
) {
2087 pci_free_consistent(h
->pdev
,
2088 sizeof(*c
), c
, cmd_dma_handle
);
2091 memset(c
->err_info
, 0, sizeof(*c
->err_info
));
2093 INIT_HLIST_NODE(&c
->list
);
2094 c
->busaddr
= (__u32
) cmd_dma_handle
;
2095 temp64
.val
= (__u64
) err_dma_handle
;
2096 c
->ErrDesc
.Addr
.lower
= temp64
.val32
.lower
;
2097 c
->ErrDesc
.Addr
.upper
= temp64
.val32
.upper
;
2098 c
->ErrDesc
.Len
= sizeof(*c
->err_info
);
2104 static void cmd_free(struct ctlr_info
*h
, struct CommandList
*c
)
2108 i
= c
- h
->cmd_pool
;
2109 clear_bit(i
& (BITS_PER_LONG
- 1),
2110 h
->cmd_pool_bits
+ (i
/ BITS_PER_LONG
));
2114 static void cmd_special_free(struct ctlr_info
*h
, struct CommandList
*c
)
2116 union u64bit temp64
;
2118 temp64
.val32
.lower
= c
->ErrDesc
.Addr
.lower
;
2119 temp64
.val32
.upper
= c
->ErrDesc
.Addr
.upper
;
2120 pci_free_consistent(h
->pdev
, sizeof(*c
->err_info
),
2121 c
->err_info
, (dma_addr_t
) temp64
.val
);
2122 pci_free_consistent(h
->pdev
, sizeof(*c
),
2123 c
, (dma_addr_t
) c
->busaddr
);
2126 #ifdef CONFIG_COMPAT
2128 static int do_ioctl(struct scsi_device
*dev
, int cmd
, void *arg
)
2133 ret
= hpsa_ioctl(dev
, cmd
, arg
);
2138 static int hpsa_ioctl32_passthru(struct scsi_device
*dev
, int cmd
, void *arg
);
2139 static int hpsa_ioctl32_big_passthru(struct scsi_device
*dev
,
2140 int cmd
, void *arg
);
2142 static int hpsa_compat_ioctl(struct scsi_device
*dev
, int cmd
, void *arg
)
2145 case CCISS_GETPCIINFO
:
2146 case CCISS_GETINTINFO
:
2147 case CCISS_SETINTINFO
:
2148 case CCISS_GETNODENAME
:
2149 case CCISS_SETNODENAME
:
2150 case CCISS_GETHEARTBEAT
:
2151 case CCISS_GETBUSTYPES
:
2152 case CCISS_GETFIRMVER
:
2153 case CCISS_GETDRIVVER
:
2154 case CCISS_REVALIDVOLS
:
2155 case CCISS_DEREGDISK
:
2156 case CCISS_REGNEWDISK
:
2158 case CCISS_RESCANDISK
:
2159 case CCISS_GETLUNINFO
:
2160 return do_ioctl(dev
, cmd
, arg
);
2162 case CCISS_PASSTHRU32
:
2163 return hpsa_ioctl32_passthru(dev
, cmd
, arg
);
2164 case CCISS_BIG_PASSTHRU32
:
2165 return hpsa_ioctl32_big_passthru(dev
, cmd
, arg
);
2168 return -ENOIOCTLCMD
;
2172 static int hpsa_ioctl32_passthru(struct scsi_device
*dev
, int cmd
, void *arg
)
2174 IOCTL32_Command_struct __user
*arg32
=
2175 (IOCTL32_Command_struct __user
*) arg
;
2176 IOCTL_Command_struct arg64
;
2177 IOCTL_Command_struct __user
*p
= compat_alloc_user_space(sizeof(arg64
));
2182 err
|= copy_from_user(&arg64
.LUN_info
, &arg32
->LUN_info
,
2183 sizeof(arg64
.LUN_info
));
2184 err
|= copy_from_user(&arg64
.Request
, &arg32
->Request
,
2185 sizeof(arg64
.Request
));
2186 err
|= copy_from_user(&arg64
.error_info
, &arg32
->error_info
,
2187 sizeof(arg64
.error_info
));
2188 err
|= get_user(arg64
.buf_size
, &arg32
->buf_size
);
2189 err
|= get_user(cp
, &arg32
->buf
);
2190 arg64
.buf
= compat_ptr(cp
);
2191 err
|= copy_to_user(p
, &arg64
, sizeof(arg64
));
2196 err
= do_ioctl(dev
, CCISS_PASSTHRU
, (void *)p
);
2199 err
|= copy_in_user(&arg32
->error_info
, &p
->error_info
,
2200 sizeof(arg32
->error_info
));
2206 static int hpsa_ioctl32_big_passthru(struct scsi_device
*dev
,
2209 BIG_IOCTL32_Command_struct __user
*arg32
=
2210 (BIG_IOCTL32_Command_struct __user
*) arg
;
2211 BIG_IOCTL_Command_struct arg64
;
2212 BIG_IOCTL_Command_struct __user
*p
=
2213 compat_alloc_user_space(sizeof(arg64
));
2218 err
|= copy_from_user(&arg64
.LUN_info
, &arg32
->LUN_info
,
2219 sizeof(arg64
.LUN_info
));
2220 err
|= copy_from_user(&arg64
.Request
, &arg32
->Request
,
2221 sizeof(arg64
.Request
));
2222 err
|= copy_from_user(&arg64
.error_info
, &arg32
->error_info
,
2223 sizeof(arg64
.error_info
));
2224 err
|= get_user(arg64
.buf_size
, &arg32
->buf_size
);
2225 err
|= get_user(arg64
.malloc_size
, &arg32
->malloc_size
);
2226 err
|= get_user(cp
, &arg32
->buf
);
2227 arg64
.buf
= compat_ptr(cp
);
2228 err
|= copy_to_user(p
, &arg64
, sizeof(arg64
));
2233 err
= do_ioctl(dev
, CCISS_BIG_PASSTHRU
, (void *)p
);
2236 err
|= copy_in_user(&arg32
->error_info
, &p
->error_info
,
2237 sizeof(arg32
->error_info
));
2244 static int hpsa_getpciinfo_ioctl(struct ctlr_info
*h
, void __user
*argp
)
2246 struct hpsa_pci_info pciinfo
;
2250 pciinfo
.domain
= pci_domain_nr(h
->pdev
->bus
);
2251 pciinfo
.bus
= h
->pdev
->bus
->number
;
2252 pciinfo
.dev_fn
= h
->pdev
->devfn
;
2253 pciinfo
.board_id
= h
->board_id
;
2254 if (copy_to_user(argp
, &pciinfo
, sizeof(pciinfo
)))
2259 static int hpsa_getdrivver_ioctl(struct ctlr_info
*h
, void __user
*argp
)
2261 DriverVer_type DriverVer
;
2262 unsigned char vmaj
, vmin
, vsubmin
;
2265 rc
= sscanf(HPSA_DRIVER_VERSION
, "%hhu.%hhu.%hhu",
2266 &vmaj
, &vmin
, &vsubmin
);
2268 dev_info(&h
->pdev
->dev
, "driver version string '%s' "
2269 "unrecognized.", HPSA_DRIVER_VERSION
);
2274 DriverVer
= (vmaj
<< 16) | (vmin
<< 8) | vsubmin
;
2277 if (copy_to_user(argp
, &DriverVer
, sizeof(DriverVer_type
)))
2282 static int hpsa_passthru_ioctl(struct ctlr_info
*h
, void __user
*argp
)
2284 IOCTL_Command_struct iocommand
;
2285 struct CommandList
*c
;
2287 union u64bit temp64
;
2291 if (!capable(CAP_SYS_RAWIO
))
2293 if (copy_from_user(&iocommand
, argp
, sizeof(iocommand
)))
2295 if ((iocommand
.buf_size
< 1) &&
2296 (iocommand
.Request
.Type
.Direction
!= XFER_NONE
)) {
2299 if (iocommand
.buf_size
> 0) {
2300 buff
= kmalloc(iocommand
.buf_size
, GFP_KERNEL
);
2304 if (iocommand
.Request
.Type
.Direction
== XFER_WRITE
) {
2305 /* Copy the data into the buffer we created */
2306 if (copy_from_user(buff
, iocommand
.buf
, iocommand
.buf_size
)) {
2311 memset(buff
, 0, iocommand
.buf_size
);
2312 c
= cmd_special_alloc(h
);
2317 /* Fill in the command type */
2318 c
->cmd_type
= CMD_IOCTL_PEND
;
2319 /* Fill in Command Header */
2320 c
->Header
.ReplyQueue
= 0; /* unused in simple mode */
2321 if (iocommand
.buf_size
> 0) { /* buffer to fill */
2322 c
->Header
.SGList
= 1;
2323 c
->Header
.SGTotal
= 1;
2324 } else { /* no buffers to fill */
2325 c
->Header
.SGList
= 0;
2326 c
->Header
.SGTotal
= 0;
2328 memcpy(&c
->Header
.LUN
, &iocommand
.LUN_info
, sizeof(c
->Header
.LUN
));
2329 /* use the kernel address the cmd block for tag */
2330 c
->Header
.Tag
.lower
= c
->busaddr
;
2332 /* Fill in Request block */
2333 memcpy(&c
->Request
, &iocommand
.Request
,
2334 sizeof(c
->Request
));
2336 /* Fill in the scatter gather information */
2337 if (iocommand
.buf_size
> 0) {
2338 temp64
.val
= pci_map_single(h
->pdev
, buff
,
2339 iocommand
.buf_size
, PCI_DMA_BIDIRECTIONAL
);
2340 c
->SG
[0].Addr
.lower
= temp64
.val32
.lower
;
2341 c
->SG
[0].Addr
.upper
= temp64
.val32
.upper
;
2342 c
->SG
[0].Len
= iocommand
.buf_size
;
2343 c
->SG
[0].Ext
= 0; /* we are not chaining*/
2345 hpsa_scsi_do_simple_cmd_core(h
, c
);
2346 hpsa_pci_unmap(h
->pdev
, c
, 1, PCI_DMA_BIDIRECTIONAL
);
2347 check_ioctl_unit_attention(h
, c
);
2349 /* Copy the error information out */
2350 memcpy(&iocommand
.error_info
, c
->err_info
,
2351 sizeof(iocommand
.error_info
));
2352 if (copy_to_user(argp
, &iocommand
, sizeof(iocommand
))) {
2354 cmd_special_free(h
, c
);
2358 if (iocommand
.Request
.Type
.Direction
== XFER_READ
) {
2359 /* Copy the data out of the buffer we created */
2360 if (copy_to_user(iocommand
.buf
, buff
, iocommand
.buf_size
)) {
2362 cmd_special_free(h
, c
);
2367 cmd_special_free(h
, c
);
2371 static int hpsa_big_passthru_ioctl(struct ctlr_info
*h
, void __user
*argp
)
2373 BIG_IOCTL_Command_struct
*ioc
;
2374 struct CommandList
*c
;
2375 unsigned char **buff
= NULL
;
2376 int *buff_size
= NULL
;
2377 union u64bit temp64
;
2383 BYTE __user
*data_ptr
;
2387 if (!capable(CAP_SYS_RAWIO
))
2389 ioc
= (BIG_IOCTL_Command_struct
*)
2390 kmalloc(sizeof(*ioc
), GFP_KERNEL
);
2395 if (copy_from_user(ioc
, argp
, sizeof(*ioc
))) {
2399 if ((ioc
->buf_size
< 1) &&
2400 (ioc
->Request
.Type
.Direction
!= XFER_NONE
)) {
2404 /* Check kmalloc limits using all SGs */
2405 if (ioc
->malloc_size
> MAX_KMALLOC_SIZE
) {
2409 if (ioc
->buf_size
> ioc
->malloc_size
* MAXSGENTRIES
) {
2413 buff
= kzalloc(MAXSGENTRIES
* sizeof(char *), GFP_KERNEL
);
2418 buff_size
= kmalloc(MAXSGENTRIES
* sizeof(int), GFP_KERNEL
);
2423 left
= ioc
->buf_size
;
2424 data_ptr
= ioc
->buf
;
2426 sz
= (left
> ioc
->malloc_size
) ? ioc
->malloc_size
: left
;
2427 buff_size
[sg_used
] = sz
;
2428 buff
[sg_used
] = kmalloc(sz
, GFP_KERNEL
);
2429 if (buff
[sg_used
] == NULL
) {
2433 if (ioc
->Request
.Type
.Direction
== XFER_WRITE
) {
2434 if (copy_from_user(buff
[sg_used
], data_ptr
, sz
)) {
2439 memset(buff
[sg_used
], 0, sz
);
2444 c
= cmd_special_alloc(h
);
2449 c
->cmd_type
= CMD_IOCTL_PEND
;
2450 c
->Header
.ReplyQueue
= 0;
2452 if (ioc
->buf_size
> 0) {
2453 c
->Header
.SGList
= sg_used
;
2454 c
->Header
.SGTotal
= sg_used
;
2456 c
->Header
.SGList
= 0;
2457 c
->Header
.SGTotal
= 0;
2459 memcpy(&c
->Header
.LUN
, &ioc
->LUN_info
, sizeof(c
->Header
.LUN
));
2460 c
->Header
.Tag
.lower
= c
->busaddr
;
2461 memcpy(&c
->Request
, &ioc
->Request
, sizeof(c
->Request
));
2462 if (ioc
->buf_size
> 0) {
2464 for (i
= 0; i
< sg_used
; i
++) {
2465 temp64
.val
= pci_map_single(h
->pdev
, buff
[i
],
2466 buff_size
[i
], PCI_DMA_BIDIRECTIONAL
);
2467 c
->SG
[i
].Addr
.lower
= temp64
.val32
.lower
;
2468 c
->SG
[i
].Addr
.upper
= temp64
.val32
.upper
;
2469 c
->SG
[i
].Len
= buff_size
[i
];
2470 /* we are not chaining */
2474 hpsa_scsi_do_simple_cmd_core(h
, c
);
2475 hpsa_pci_unmap(h
->pdev
, c
, sg_used
, PCI_DMA_BIDIRECTIONAL
);
2476 check_ioctl_unit_attention(h
, c
);
2477 /* Copy the error information out */
2478 memcpy(&ioc
->error_info
, c
->err_info
, sizeof(ioc
->error_info
));
2479 if (copy_to_user(argp
, ioc
, sizeof(*ioc
))) {
2480 cmd_special_free(h
, c
);
2484 if (ioc
->Request
.Type
.Direction
== XFER_READ
) {
2485 /* Copy the data out of the buffer we created */
2486 BYTE __user
*ptr
= ioc
->buf
;
2487 for (i
= 0; i
< sg_used
; i
++) {
2488 if (copy_to_user(ptr
, buff
[i
], buff_size
[i
])) {
2489 cmd_special_free(h
, c
);
2493 ptr
+= buff_size
[i
];
2496 cmd_special_free(h
, c
);
2500 for (i
= 0; i
< sg_used
; i
++)
2509 static void check_ioctl_unit_attention(struct ctlr_info
*h
,
2510 struct CommandList
*c
)
2512 if (c
->err_info
->CommandStatus
== CMD_TARGET_STATUS
&&
2513 c
->err_info
->ScsiStatus
!= SAM_STAT_CHECK_CONDITION
)
2514 (void) check_for_unit_attention(h
, c
);
2519 static int hpsa_ioctl(struct scsi_device
*dev
, int cmd
, void *arg
)
2521 struct ctlr_info
*h
;
2522 void __user
*argp
= (void __user
*)arg
;
2524 h
= sdev_to_hba(dev
);
2527 case CCISS_DEREGDISK
:
2528 case CCISS_REGNEWDISK
:
2530 hpsa_update_scsi_devices(h
, dev
->host
->host_no
);
2532 case CCISS_GETPCIINFO
:
2533 return hpsa_getpciinfo_ioctl(h
, argp
);
2534 case CCISS_GETDRIVVER
:
2535 return hpsa_getdrivver_ioctl(h
, argp
);
2536 case CCISS_PASSTHRU
:
2537 return hpsa_passthru_ioctl(h
, argp
);
2538 case CCISS_BIG_PASSTHRU
:
2539 return hpsa_big_passthru_ioctl(h
, argp
);
2545 static void fill_cmd(struct CommandList
*c
, __u8 cmd
, struct ctlr_info
*h
,
2546 void *buff
, size_t size
, __u8 page_code
, unsigned char *scsi3addr
,
2549 int pci_dir
= XFER_NONE
;
2551 c
->cmd_type
= CMD_IOCTL_PEND
;
2552 c
->Header
.ReplyQueue
= 0;
2553 if (buff
!= NULL
&& size
> 0) {
2554 c
->Header
.SGList
= 1;
2555 c
->Header
.SGTotal
= 1;
2557 c
->Header
.SGList
= 0;
2558 c
->Header
.SGTotal
= 0;
2560 c
->Header
.Tag
.lower
= c
->busaddr
;
2561 memcpy(c
->Header
.LUN
.LunAddrBytes
, scsi3addr
, 8);
2563 c
->Request
.Type
.Type
= cmd_type
;
2564 if (cmd_type
== TYPE_CMD
) {
2567 /* are we trying to read a vital product page */
2568 if (page_code
!= 0) {
2569 c
->Request
.CDB
[1] = 0x01;
2570 c
->Request
.CDB
[2] = page_code
;
2572 c
->Request
.CDBLen
= 6;
2573 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
2574 c
->Request
.Type
.Direction
= XFER_READ
;
2575 c
->Request
.Timeout
= 0;
2576 c
->Request
.CDB
[0] = HPSA_INQUIRY
;
2577 c
->Request
.CDB
[4] = size
& 0xFF;
2579 case HPSA_REPORT_LOG
:
2580 case HPSA_REPORT_PHYS
:
2581 /* Talking to controller so It's a physical command
2582 mode = 00 target = 0. Nothing to write.
2584 c
->Request
.CDBLen
= 12;
2585 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
2586 c
->Request
.Type
.Direction
= XFER_READ
;
2587 c
->Request
.Timeout
= 0;
2588 c
->Request
.CDB
[0] = cmd
;
2589 c
->Request
.CDB
[6] = (size
>> 24) & 0xFF; /* MSB */
2590 c
->Request
.CDB
[7] = (size
>> 16) & 0xFF;
2591 c
->Request
.CDB
[8] = (size
>> 8) & 0xFF;
2592 c
->Request
.CDB
[9] = size
& 0xFF;
2595 case HPSA_READ_CAPACITY
:
2596 c
->Request
.CDBLen
= 10;
2597 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
2598 c
->Request
.Type
.Direction
= XFER_READ
;
2599 c
->Request
.Timeout
= 0;
2600 c
->Request
.CDB
[0] = cmd
;
2602 case HPSA_CACHE_FLUSH
:
2603 c
->Request
.CDBLen
= 12;
2604 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
2605 c
->Request
.Type
.Direction
= XFER_WRITE
;
2606 c
->Request
.Timeout
= 0;
2607 c
->Request
.CDB
[0] = BMIC_WRITE
;
2608 c
->Request
.CDB
[6] = BMIC_CACHE_FLUSH
;
2610 case TEST_UNIT_READY
:
2611 c
->Request
.CDBLen
= 6;
2612 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
2613 c
->Request
.Type
.Direction
= XFER_NONE
;
2614 c
->Request
.Timeout
= 0;
2617 dev_warn(&h
->pdev
->dev
, "unknown command 0x%c\n", cmd
);
2621 } else if (cmd_type
== TYPE_MSG
) {
2624 case HPSA_DEVICE_RESET_MSG
:
2625 c
->Request
.CDBLen
= 16;
2626 c
->Request
.Type
.Type
= 1; /* It is a MSG not a CMD */
2627 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
2628 c
->Request
.Type
.Direction
= XFER_NONE
;
2629 c
->Request
.Timeout
= 0; /* Don't time out */
2630 c
->Request
.CDB
[0] = 0x01; /* RESET_MSG is 0x01 */
2631 c
->Request
.CDB
[1] = 0x03; /* Reset target above */
2632 /* If bytes 4-7 are zero, it means reset the */
2634 c
->Request
.CDB
[4] = 0x00;
2635 c
->Request
.CDB
[5] = 0x00;
2636 c
->Request
.CDB
[6] = 0x00;
2637 c
->Request
.CDB
[7] = 0x00;
2641 dev_warn(&h
->pdev
->dev
, "unknown message type %d\n",
2646 dev_warn(&h
->pdev
->dev
, "unknown command type %d\n", cmd_type
);
2650 switch (c
->Request
.Type
.Direction
) {
2652 pci_dir
= PCI_DMA_FROMDEVICE
;
2655 pci_dir
= PCI_DMA_TODEVICE
;
2658 pci_dir
= PCI_DMA_NONE
;
2661 pci_dir
= PCI_DMA_BIDIRECTIONAL
;
2664 hpsa_map_one(h
->pdev
, c
, buff
, size
, pci_dir
);
2670 * Map (physical) PCI mem into (virtual) kernel space
2672 static void __iomem
*remap_pci_mem(ulong base
, ulong size
)
2674 ulong page_base
= ((ulong
) base
) & PAGE_MASK
;
2675 ulong page_offs
= ((ulong
) base
) - page_base
;
2676 void __iomem
*page_remapped
= ioremap(page_base
, page_offs
+ size
);
2678 return page_remapped
? (page_remapped
+ page_offs
) : NULL
;
2681 /* Takes cmds off the submission queue and sends them to the hardware,
2682 * then puts them on the queue of cmds waiting for completion.
2684 static void start_io(struct ctlr_info
*h
)
2686 struct CommandList
*c
;
2688 while (!hlist_empty(&h
->reqQ
)) {
2689 c
= hlist_entry(h
->reqQ
.first
, struct CommandList
, list
);
2690 /* can't do anything if fifo is full */
2691 if ((h
->access
.fifo_full(h
))) {
2692 dev_warn(&h
->pdev
->dev
, "fifo full\n");
2696 /* Get the first entry from the Request Q */
2700 /* Tell the controller execute command */
2701 h
->access
.submit_command(h
, c
);
2703 /* Put job onto the completed Q */
2708 static inline unsigned long get_next_completion(struct ctlr_info
*h
)
2710 return h
->access
.command_completed(h
);
2713 static inline int interrupt_pending(struct ctlr_info
*h
)
2715 return h
->access
.intr_pending(h
);
2718 static inline long interrupt_not_for_us(struct ctlr_info
*h
)
2720 return ((h
->access
.intr_pending(h
) == 0) ||
2721 (h
->interrupts_enabled
== 0));
2724 static inline int bad_tag(struct ctlr_info
*h
, __u32 tag_index
,
2727 if (unlikely(tag_index
>= h
->nr_cmds
)) {
2728 dev_warn(&h
->pdev
->dev
, "bad tag 0x%08x ignored.\n", raw_tag
);
2734 static inline void finish_cmd(struct CommandList
*c
, __u32 raw_tag
)
2737 if (likely(c
->cmd_type
== CMD_SCSI
))
2738 complete_scsi_command(c
, 0, raw_tag
);
2739 else if (c
->cmd_type
== CMD_IOCTL_PEND
)
2740 complete(c
->waiting
);
2743 static irqreturn_t
do_hpsa_intr(int irq
, void *dev_id
)
2745 struct ctlr_info
*h
= dev_id
;
2746 struct CommandList
*c
;
2747 unsigned long flags
;
2748 __u32 raw_tag
, tag
, tag_index
;
2749 struct hlist_node
*tmp
;
2751 if (interrupt_not_for_us(h
))
2753 spin_lock_irqsave(&h
->lock
, flags
);
2754 while (interrupt_pending(h
)) {
2755 while ((raw_tag
= get_next_completion(h
)) != FIFO_EMPTY
) {
2756 if (likely(HPSA_TAG_CONTAINS_INDEX(raw_tag
))) {
2757 tag_index
= HPSA_TAG_TO_INDEX(raw_tag
);
2758 if (bad_tag(h
, tag_index
, raw_tag
))
2760 c
= h
->cmd_pool
+ tag_index
;
2761 finish_cmd(c
, raw_tag
);
2764 tag
= HPSA_TAG_DISCARD_ERROR_BITS(raw_tag
);
2766 hlist_for_each_entry(c
, tmp
, &h
->cmpQ
, list
) {
2767 if (c
->busaddr
== tag
) {
2768 finish_cmd(c
, raw_tag
);
2774 spin_unlock_irqrestore(&h
->lock
, flags
);
2778 /* Send a message CDB to the firmware. */
2779 static __devinit
int hpsa_message(struct pci_dev
*pdev
, unsigned char opcode
,
2783 struct CommandListHeader CommandHeader
;
2784 struct RequestBlock Request
;
2785 struct ErrDescriptor ErrorDescriptor
;
2787 struct Command
*cmd
;
2788 static const size_t cmd_sz
= sizeof(*cmd
) +
2789 sizeof(cmd
->ErrorDescriptor
);
2791 uint32_t paddr32
, tag
;
2792 void __iomem
*vaddr
;
2795 vaddr
= pci_ioremap_bar(pdev
, 0);
2799 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
2800 * CCISS commands, so they must be allocated from the lower 4GiB of
2803 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
2809 cmd
= pci_alloc_consistent(pdev
, cmd_sz
, &paddr64
);
2815 /* This must fit, because of the 32-bit consistent DMA mask. Also,
2816 * although there's no guarantee, we assume that the address is at
2817 * least 4-byte aligned (most likely, it's page-aligned).
2821 cmd
->CommandHeader
.ReplyQueue
= 0;
2822 cmd
->CommandHeader
.SGList
= 0;
2823 cmd
->CommandHeader
.SGTotal
= 0;
2824 cmd
->CommandHeader
.Tag
.lower
= paddr32
;
2825 cmd
->CommandHeader
.Tag
.upper
= 0;
2826 memset(&cmd
->CommandHeader
.LUN
.LunAddrBytes
, 0, 8);
2828 cmd
->Request
.CDBLen
= 16;
2829 cmd
->Request
.Type
.Type
= TYPE_MSG
;
2830 cmd
->Request
.Type
.Attribute
= ATTR_HEADOFQUEUE
;
2831 cmd
->Request
.Type
.Direction
= XFER_NONE
;
2832 cmd
->Request
.Timeout
= 0; /* Don't time out */
2833 cmd
->Request
.CDB
[0] = opcode
;
2834 cmd
->Request
.CDB
[1] = type
;
2835 memset(&cmd
->Request
.CDB
[2], 0, 14); /* rest of the CDB is reserved */
2836 cmd
->ErrorDescriptor
.Addr
.lower
= paddr32
+ sizeof(*cmd
);
2837 cmd
->ErrorDescriptor
.Addr
.upper
= 0;
2838 cmd
->ErrorDescriptor
.Len
= sizeof(struct ErrorInfo
);
2840 writel(paddr32
, vaddr
+ SA5_REQUEST_PORT_OFFSET
);
2842 for (i
= 0; i
< HPSA_MSG_SEND_RETRY_LIMIT
; i
++) {
2843 tag
= readl(vaddr
+ SA5_REPLY_PORT_OFFSET
);
2844 if (HPSA_TAG_DISCARD_ERROR_BITS(tag
) == paddr32
)
2846 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS
);
2851 /* we leak the DMA buffer here ... no choice since the controller could
2852 * still complete the command.
2854 if (i
== HPSA_MSG_SEND_RETRY_LIMIT
) {
2855 dev_err(&pdev
->dev
, "controller message %02x:%02x timed out\n",
2860 pci_free_consistent(pdev
, cmd_sz
, cmd
, paddr64
);
2862 if (tag
& HPSA_ERROR_BIT
) {
2863 dev_err(&pdev
->dev
, "controller message %02x:%02x failed\n",
2868 dev_info(&pdev
->dev
, "controller message %02x:%02x succeeded\n",
2873 #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
2874 #define hpsa_noop(p) hpsa_message(p, 3, 0)
2876 static __devinit
int hpsa_reset_msi(struct pci_dev
*pdev
)
2878 /* the #defines are stolen from drivers/pci/msi.h. */
2879 #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
2880 #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
2885 pos
= pci_find_capability(pdev
, PCI_CAP_ID_MSI
);
2887 pci_read_config_word(pdev
, msi_control_reg(pos
), &control
);
2888 if (control
& PCI_MSI_FLAGS_ENABLE
) {
2889 dev_info(&pdev
->dev
, "resetting MSI\n");
2890 pci_write_config_word(pdev
, msi_control_reg(pos
),
2891 control
& ~PCI_MSI_FLAGS_ENABLE
);
2895 pos
= pci_find_capability(pdev
, PCI_CAP_ID_MSIX
);
2897 pci_read_config_word(pdev
, msi_control_reg(pos
), &control
);
2898 if (control
& PCI_MSIX_FLAGS_ENABLE
) {
2899 dev_info(&pdev
->dev
, "resetting MSI-X\n");
2900 pci_write_config_word(pdev
, msi_control_reg(pos
),
2901 control
& ~PCI_MSIX_FLAGS_ENABLE
);
2908 /* This does a hard reset of the controller using PCI power management
2911 static __devinit
int hpsa_hard_reset_controller(struct pci_dev
*pdev
)
2913 u16 pmcsr
, saved_config_space
[32];
2916 dev_info(&pdev
->dev
, "using PCI PM to reset controller\n");
2918 /* This is very nearly the same thing as
2920 * pci_save_state(pci_dev);
2921 * pci_set_power_state(pci_dev, PCI_D3hot);
2922 * pci_set_power_state(pci_dev, PCI_D0);
2923 * pci_restore_state(pci_dev);
2925 * but we can't use these nice canned kernel routines on
2926 * kexec, because they also check the MSI/MSI-X state in PCI
2927 * configuration space and do the wrong thing when it is
2928 * set/cleared. Also, the pci_save/restore_state functions
2929 * violate the ordering requirements for restoring the
2930 * configuration space from the CCISS document (see the
2931 * comment below). So we roll our own ....
2934 for (i
= 0; i
< 32; i
++)
2935 pci_read_config_word(pdev
, 2*i
, &saved_config_space
[i
]);
2937 pos
= pci_find_capability(pdev
, PCI_CAP_ID_PM
);
2940 "hpsa_reset_controller: PCI PM not supported\n");
2944 /* Quoting from the Open CISS Specification: "The Power
2945 * Management Control/Status Register (CSR) controls the power
2946 * state of the device. The normal operating state is D0,
2947 * CSR=00h. The software off state is D3, CSR=03h. To reset
2948 * the controller, place the interface device in D3 then to
2949 * D0, this causes a secondary PCI reset which will reset the
2953 /* enter the D3hot power management state */
2954 pci_read_config_word(pdev
, pos
+ PCI_PM_CTRL
, &pmcsr
);
2955 pmcsr
&= ~PCI_PM_CTRL_STATE_MASK
;
2957 pci_write_config_word(pdev
, pos
+ PCI_PM_CTRL
, pmcsr
);
2961 /* enter the D0 power management state */
2962 pmcsr
&= ~PCI_PM_CTRL_STATE_MASK
;
2964 pci_write_config_word(pdev
, pos
+ PCI_PM_CTRL
, pmcsr
);
2968 /* Restore the PCI configuration space. The Open CISS
2969 * Specification says, "Restore the PCI Configuration
2970 * Registers, offsets 00h through 60h. It is important to
2971 * restore the command register, 16-bits at offset 04h,
2972 * last. Do not restore the configuration status register,
2973 * 16-bits at offset 06h." Note that the offset is 2*i.
2975 for (i
= 0; i
< 32; i
++) {
2976 if (i
== 2 || i
== 3)
2978 pci_write_config_word(pdev
, 2*i
, saved_config_space
[i
]);
2981 pci_write_config_word(pdev
, 4, saved_config_space
[2]);
2987 * We cannot read the structure directly, for portability we must use
2989 * This is for debug only.
2992 static void print_cfg_table(struct device
*dev
, struct CfgTable
*tb
)
2997 dev_info(dev
, "Controller Configuration information\n");
2998 dev_info(dev
, "------------------------------------\n");
2999 for (i
= 0; i
< 4; i
++)
3000 temp_name
[i
] = readb(&(tb
->Signature
[i
]));
3001 temp_name
[4] = '\0';
3002 dev_info(dev
, " Signature = %s\n", temp_name
);
3003 dev_info(dev
, " Spec Number = %d\n", readl(&(tb
->SpecValence
)));
3004 dev_info(dev
, " Transport methods supported = 0x%x\n",
3005 readl(&(tb
->TransportSupport
)));
3006 dev_info(dev
, " Transport methods active = 0x%x\n",
3007 readl(&(tb
->TransportActive
)));
3008 dev_info(dev
, " Requested transport Method = 0x%x\n",
3009 readl(&(tb
->HostWrite
.TransportRequest
)));
3010 dev_info(dev
, " Coalesce Interrupt Delay = 0x%x\n",
3011 readl(&(tb
->HostWrite
.CoalIntDelay
)));
3012 dev_info(dev
, " Coalesce Interrupt Count = 0x%x\n",
3013 readl(&(tb
->HostWrite
.CoalIntCount
)));
3014 dev_info(dev
, " Max outstanding commands = 0x%d\n",
3015 readl(&(tb
->CmdsOutMax
)));
3016 dev_info(dev
, " Bus Types = 0x%x\n", readl(&(tb
->BusTypes
)));
3017 for (i
= 0; i
< 16; i
++)
3018 temp_name
[i
] = readb(&(tb
->ServerName
[i
]));
3019 temp_name
[16] = '\0';
3020 dev_info(dev
, " Server Name = %s\n", temp_name
);
3021 dev_info(dev
, " Heartbeat Counter = 0x%x\n\n\n",
3022 readl(&(tb
->HeartBeat
)));
3024 #endif /* HPSA_DEBUG */
3026 static int find_PCI_BAR_index(struct pci_dev
*pdev
, unsigned long pci_bar_addr
)
3028 int i
, offset
, mem_type
, bar_type
;
3030 if (pci_bar_addr
== PCI_BASE_ADDRESS_0
) /* looking for BAR zero? */
3033 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
3034 bar_type
= pci_resource_flags(pdev
, i
) & PCI_BASE_ADDRESS_SPACE
;
3035 if (bar_type
== PCI_BASE_ADDRESS_SPACE_IO
)
3038 mem_type
= pci_resource_flags(pdev
, i
) &
3039 PCI_BASE_ADDRESS_MEM_TYPE_MASK
;
3041 case PCI_BASE_ADDRESS_MEM_TYPE_32
:
3042 case PCI_BASE_ADDRESS_MEM_TYPE_1M
:
3043 offset
+= 4; /* 32 bit */
3045 case PCI_BASE_ADDRESS_MEM_TYPE_64
:
3048 default: /* reserved in PCI 2.2 */
3049 dev_warn(&pdev
->dev
,
3050 "base address is invalid\n");
3055 if (offset
== pci_bar_addr
- PCI_BASE_ADDRESS_0
)
3061 /* If MSI/MSI-X is supported by the kernel we will try to enable it on
3062 * controllers that are capable. If not, we use IO-APIC mode.
3065 static void __devinit
hpsa_interrupt_mode(struct ctlr_info
*h
,
3066 struct pci_dev
*pdev
, __u32 board_id
)
3068 #ifdef CONFIG_PCI_MSI
3070 struct msix_entry hpsa_msix_entries
[4] = { {0, 0}, {0, 1},
3074 /* Some boards advertise MSI but don't really support it */
3075 if ((board_id
== 0x40700E11) ||
3076 (board_id
== 0x40800E11) ||
3077 (board_id
== 0x40820E11) || (board_id
== 0x40830E11))
3078 goto default_int_mode
;
3079 if (pci_find_capability(pdev
, PCI_CAP_ID_MSIX
)) {
3080 dev_info(&pdev
->dev
, "MSIX\n");
3081 err
= pci_enable_msix(pdev
, hpsa_msix_entries
, 4);
3083 h
->intr
[0] = hpsa_msix_entries
[0].vector
;
3084 h
->intr
[1] = hpsa_msix_entries
[1].vector
;
3085 h
->intr
[2] = hpsa_msix_entries
[2].vector
;
3086 h
->intr
[3] = hpsa_msix_entries
[3].vector
;
3091 dev_warn(&pdev
->dev
, "only %d MSI-X vectors "
3092 "available\n", err
);
3093 goto default_int_mode
;
3095 dev_warn(&pdev
->dev
, "MSI-X init failed %d\n",
3097 goto default_int_mode
;
3100 if (pci_find_capability(pdev
, PCI_CAP_ID_MSI
)) {
3101 dev_info(&pdev
->dev
, "MSI\n");
3102 if (!pci_enable_msi(pdev
))
3105 dev_warn(&pdev
->dev
, "MSI init failed\n");
3108 #endif /* CONFIG_PCI_MSI */
3109 /* if we get here we're going to use the default interrupt mode */
3110 h
->intr
[SIMPLE_MODE_INT
] = pdev
->irq
;
3114 static int hpsa_pci_init(struct ctlr_info
*h
, struct pci_dev
*pdev
)
3116 ushort subsystem_vendor_id
, subsystem_device_id
, command
;
3117 __u32 board_id
, scratchpad
= 0;
3119 __u32 cfg_base_addr
;
3120 __u64 cfg_base_addr_index
;
3121 int i
, prod_index
, err
;
3123 subsystem_vendor_id
= pdev
->subsystem_vendor
;
3124 subsystem_device_id
= pdev
->subsystem_device
;
3125 board_id
= (((__u32
) (subsystem_device_id
<< 16) & 0xffff0000) |
3126 subsystem_vendor_id
);
3128 for (i
= 0; i
< ARRAY_SIZE(products
); i
++)
3129 if (board_id
== products
[i
].board_id
)
3134 if (prod_index
== ARRAY_SIZE(products
)) {
3136 if (subsystem_vendor_id
!= PCI_VENDOR_ID_HP
||
3138 dev_warn(&pdev
->dev
, "unrecognized board ID:"
3139 " 0x%08lx, ignoring.\n",
3140 (unsigned long) board_id
);
3144 /* check to see if controller has been disabled
3145 * BEFORE trying to enable it
3147 (void)pci_read_config_word(pdev
, PCI_COMMAND
, &command
);
3148 if (!(command
& 0x02)) {
3149 dev_warn(&pdev
->dev
, "controller appears to be disabled\n");
3153 err
= pci_enable_device(pdev
);
3155 dev_warn(&pdev
->dev
, "unable to enable PCI device\n");
3159 err
= pci_request_regions(pdev
, "hpsa");
3161 dev_err(&pdev
->dev
, "cannot obtain PCI resources, aborting\n");
3165 /* If the kernel supports MSI/MSI-X we will try to enable that,
3166 * else we use the IO-APIC interrupt assigned to us by system ROM.
3168 hpsa_interrupt_mode(h
, pdev
, board_id
);
3170 /* find the memory BAR */
3171 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
3172 if (pci_resource_flags(pdev
, i
) & IORESOURCE_MEM
)
3175 if (i
== DEVICE_COUNT_RESOURCE
) {
3176 dev_warn(&pdev
->dev
, "no memory BAR found\n");
3178 goto err_out_free_res
;
3181 h
->paddr
= pci_resource_start(pdev
, i
); /* addressing mode bits
3185 h
->vaddr
= remap_pci_mem(h
->paddr
, 0x250);
3187 /* Wait for the board to become ready. */
3188 for (i
= 0; i
< HPSA_BOARD_READY_ITERATIONS
; i
++) {
3189 scratchpad
= readl(h
->vaddr
+ SA5_SCRATCHPAD_OFFSET
);
3190 if (scratchpad
== HPSA_FIRMWARE_READY
)
3192 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS
);
3194 if (scratchpad
!= HPSA_FIRMWARE_READY
) {
3195 dev_warn(&pdev
->dev
, "board not ready, timed out.\n");
3197 goto err_out_free_res
;
3200 /* get the address index number */
3201 cfg_base_addr
= readl(h
->vaddr
+ SA5_CTCFG_OFFSET
);
3202 cfg_base_addr
&= (__u32
) 0x0000ffff;
3203 cfg_base_addr_index
= find_PCI_BAR_index(pdev
, cfg_base_addr
);
3204 if (cfg_base_addr_index
== -1) {
3205 dev_warn(&pdev
->dev
, "cannot find cfg_base_addr_index\n");
3207 goto err_out_free_res
;
3210 cfg_offset
= readl(h
->vaddr
+ SA5_CTMEM_OFFSET
);
3211 h
->cfgtable
= remap_pci_mem(pci_resource_start(pdev
,
3212 cfg_base_addr_index
) + cfg_offset
,
3213 sizeof(h
->cfgtable
));
3214 h
->board_id
= board_id
;
3216 /* Query controller for max supported commands: */
3217 h
->max_commands
= readl(&(h
->cfgtable
->CmdsOutMax
));
3219 h
->product_name
= products
[prod_index
].product_name
;
3220 h
->access
= *(products
[prod_index
].access
);
3221 /* Allow room for some ioctls */
3222 h
->nr_cmds
= h
->max_commands
- 4;
3224 if ((readb(&h
->cfgtable
->Signature
[0]) != 'C') ||
3225 (readb(&h
->cfgtable
->Signature
[1]) != 'I') ||
3226 (readb(&h
->cfgtable
->Signature
[2]) != 'S') ||
3227 (readb(&h
->cfgtable
->Signature
[3]) != 'S')) {
3228 dev_warn(&pdev
->dev
, "not a valid CISS config table\n");
3230 goto err_out_free_res
;
3234 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3236 prefetch
= readl(&(h
->cfgtable
->SCSI_Prefetch
));
3238 writel(prefetch
, &(h
->cfgtable
->SCSI_Prefetch
));
3242 /* Disabling DMA prefetch for the P600
3243 * An ASIC bug may result in a prefetch beyond
3246 if (board_id
== 0x3225103C) {
3248 dma_prefetch
= readl(h
->vaddr
+ I2O_DMA1_CFG
);
3249 dma_prefetch
|= 0x8000;
3250 writel(dma_prefetch
, h
->vaddr
+ I2O_DMA1_CFG
);
3253 h
->max_commands
= readl(&(h
->cfgtable
->CmdsOutMax
));
3254 /* Update the field, and then ring the doorbell */
3255 writel(CFGTBL_Trans_Simple
, &(h
->cfgtable
->HostWrite
.TransportRequest
));
3256 writel(CFGTBL_ChangeReq
, h
->vaddr
+ SA5_DOORBELL
);
3258 /* under certain very rare conditions, this can take awhile.
3259 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3260 * as we enter this code.)
3262 for (i
= 0; i
< MAX_CONFIG_WAIT
; i
++) {
3263 if (!(readl(h
->vaddr
+ SA5_DOORBELL
) & CFGTBL_ChangeReq
))
3265 /* delay and try again */
3270 print_cfg_table(&pdev
->dev
, h
->cfgtable
);
3271 #endif /* HPSA_DEBUG */
3273 if (!(readl(&(h
->cfgtable
->TransportActive
)) & CFGTBL_Trans_Simple
)) {
3274 dev_warn(&pdev
->dev
, "unable to get board into simple mode\n");
3276 goto err_out_free_res
;
3282 * Deliberately omit pci_disable_device(): it does something nasty to
3283 * Smart Array controllers that pci_enable_device does not undo
3285 pci_release_regions(pdev
);
3289 static int __devinit
hpsa_init_one(struct pci_dev
*pdev
,
3290 const struct pci_device_id
*ent
)
3294 struct ctlr_info
*h
;
3296 if (number_of_controllers
== 0)
3297 printk(KERN_INFO DRIVER_NAME
"\n");
3298 if (reset_devices
) {
3299 /* Reset the controller with a PCI power-cycle */
3300 if (hpsa_hard_reset_controller(pdev
) || hpsa_reset_msi(pdev
))
3303 /* Some devices (notably the HP Smart Array 5i Controller)
3304 need a little pause here */
3305 msleep(HPSA_POST_RESET_PAUSE_MSECS
);
3307 /* Now try to get the controller to respond to a no-op */
3308 for (i
= 0; i
< HPSA_POST_RESET_NOOP_RETRIES
; i
++) {
3309 if (hpsa_noop(pdev
) == 0)
3312 dev_warn(&pdev
->dev
, "no-op failed%s\n",
3313 (i
< 11 ? "; re-trying" : ""));
3317 BUILD_BUG_ON(sizeof(struct CommandList
) % 8);
3318 h
= kzalloc(sizeof(*h
), GFP_KERNEL
);
3322 h
->busy_initializing
= 1;
3323 INIT_HLIST_HEAD(&h
->cmpQ
);
3324 INIT_HLIST_HEAD(&h
->reqQ
);
3325 mutex_init(&h
->busy_shutting_down
);
3326 init_completion(&h
->scan_wait
);
3327 if (hpsa_pci_init(h
, pdev
) != 0)
3330 sprintf(h
->devname
, "hpsa%d", number_of_controllers
);
3331 h
->ctlr
= number_of_controllers
;
3332 number_of_controllers
++;
3335 /* configure PCI DMA stuff */
3336 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)))
3338 else if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)))
3341 dev_err(&pdev
->dev
, "no suitable DMA available\n");
3345 /* make sure the board interrupts are off */
3346 h
->access
.set_intr_mask(h
, HPSA_INTR_OFF
);
3347 if (request_irq(h
->intr
[SIMPLE_MODE_INT
], do_hpsa_intr
,
3348 IRQF_DISABLED
| IRQF_SHARED
, h
->devname
, h
)) {
3349 dev_err(&pdev
->dev
, "unable to get irq %d for %s\n",
3350 h
->intr
[SIMPLE_MODE_INT
], h
->devname
);
3354 dev_info(&pdev
->dev
, "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
3355 h
->devname
, pdev
->device
, pci_name(pdev
),
3356 h
->intr
[SIMPLE_MODE_INT
], dac
? "" : " not");
3359 kmalloc(((h
->nr_cmds
+ BITS_PER_LONG
-
3360 1) / BITS_PER_LONG
) * sizeof(unsigned long), GFP_KERNEL
);
3361 h
->cmd_pool
= pci_alloc_consistent(h
->pdev
,
3362 h
->nr_cmds
* sizeof(*h
->cmd_pool
),
3363 &(h
->cmd_pool_dhandle
));
3364 h
->errinfo_pool
= pci_alloc_consistent(h
->pdev
,
3365 h
->nr_cmds
* sizeof(*h
->errinfo_pool
),
3366 &(h
->errinfo_pool_dhandle
));
3367 if ((h
->cmd_pool_bits
== NULL
)
3368 || (h
->cmd_pool
== NULL
)
3369 || (h
->errinfo_pool
== NULL
)) {
3370 dev_err(&pdev
->dev
, "out of memory");
3373 spin_lock_init(&h
->lock
);
3375 pci_set_drvdata(pdev
, h
);
3376 memset(h
->cmd_pool_bits
, 0,
3377 ((h
->nr_cmds
+ BITS_PER_LONG
-
3378 1) / BITS_PER_LONG
) * sizeof(unsigned long));
3382 /* Turn the interrupts on so we can service requests */
3383 h
->access
.set_intr_mask(h
, HPSA_INTR_ON
);
3385 hpsa_register_scsi(h
); /* hook ourselves into SCSI subsystem */
3386 h
->busy_initializing
= 0;
3390 kfree(h
->cmd_pool_bits
);
3392 pci_free_consistent(h
->pdev
,
3393 h
->nr_cmds
* sizeof(struct CommandList
),
3394 h
->cmd_pool
, h
->cmd_pool_dhandle
);
3395 if (h
->errinfo_pool
)
3396 pci_free_consistent(h
->pdev
,
3397 h
->nr_cmds
* sizeof(struct ErrorInfo
),
3399 h
->errinfo_pool_dhandle
);
3400 free_irq(h
->intr
[SIMPLE_MODE_INT
], h
);
3403 h
->busy_initializing
= 0;
3408 static void hpsa_flush_cache(struct ctlr_info
*h
)
3411 struct CommandList
*c
;
3413 flush_buf
= kzalloc(4, GFP_KERNEL
);
3417 c
= cmd_special_alloc(h
);
3419 dev_warn(&h
->pdev
->dev
, "cmd_special_alloc returned NULL!\n");
3422 fill_cmd(c
, HPSA_CACHE_FLUSH
, h
, flush_buf
, 4, 0,
3423 RAID_CTLR_LUNID
, TYPE_CMD
);
3424 hpsa_scsi_do_simple_cmd_with_retry(h
, c
, PCI_DMA_TODEVICE
);
3425 if (c
->err_info
->CommandStatus
!= 0)
3426 dev_warn(&h
->pdev
->dev
,
3427 "error flushing cache on controller\n");
3428 cmd_special_free(h
, c
);
3433 static void hpsa_shutdown(struct pci_dev
*pdev
)
3435 struct ctlr_info
*h
;
3437 h
= pci_get_drvdata(pdev
);
3438 /* Turn board interrupts off and send the flush cache command
3439 * sendcmd will turn off interrupt, and send the flush...
3440 * To write all data in the battery backed cache to disks
3442 hpsa_flush_cache(h
);
3443 h
->access
.set_intr_mask(h
, HPSA_INTR_OFF
);
3444 free_irq(h
->intr
[2], h
);
3445 #ifdef CONFIG_PCI_MSI
3447 pci_disable_msix(h
->pdev
);
3448 else if (h
->msi_vector
)
3449 pci_disable_msi(h
->pdev
);
3450 #endif /* CONFIG_PCI_MSI */
3453 static void __devexit
hpsa_remove_one(struct pci_dev
*pdev
)
3455 struct ctlr_info
*h
;
3457 if (pci_get_drvdata(pdev
) == NULL
) {
3458 dev_err(&pdev
->dev
, "unable to remove device \n");
3461 h
= pci_get_drvdata(pdev
);
3462 mutex_lock(&h
->busy_shutting_down
);
3463 remove_from_scan_list(h
);
3464 hpsa_unregister_scsi(h
); /* unhook from SCSI subsystem */
3465 hpsa_shutdown(pdev
);
3467 pci_free_consistent(h
->pdev
,
3468 h
->nr_cmds
* sizeof(struct CommandList
),
3469 h
->cmd_pool
, h
->cmd_pool_dhandle
);
3470 pci_free_consistent(h
->pdev
,
3471 h
->nr_cmds
* sizeof(struct ErrorInfo
),
3472 h
->errinfo_pool
, h
->errinfo_pool_dhandle
);
3473 kfree(h
->cmd_pool_bits
);
3475 * Deliberately omit pci_disable_device(): it does something nasty to
3476 * Smart Array controllers that pci_enable_device does not undo
3478 pci_release_regions(pdev
);
3479 pci_set_drvdata(pdev
, NULL
);
3480 mutex_unlock(&h
->busy_shutting_down
);
3484 static int hpsa_suspend(__attribute__((unused
)) struct pci_dev
*pdev
,
3485 __attribute__((unused
)) pm_message_t state
)
3490 static int hpsa_resume(__attribute__((unused
)) struct pci_dev
*pdev
)
3495 static struct pci_driver hpsa_pci_driver
= {
3497 .probe
= hpsa_init_one
,
3498 .remove
= __devexit_p(hpsa_remove_one
),
3499 .id_table
= hpsa_pci_device_id
, /* id_table */
3500 .shutdown
= hpsa_shutdown
,
3501 .suspend
= hpsa_suspend
,
3502 .resume
= hpsa_resume
,
3506 * This is it. Register the PCI driver information for the cards we control
3507 * the OS will call our registered routines when it finds one of our cards.
3509 static int __init
hpsa_init(void)
3512 /* Start the scan thread */
3513 hpsa_scan_thread
= kthread_run(hpsa_scan_func
, NULL
, "hpsa_scan");
3514 if (IS_ERR(hpsa_scan_thread
)) {
3515 err
= PTR_ERR(hpsa_scan_thread
);
3518 err
= pci_register_driver(&hpsa_pci_driver
);
3520 kthread_stop(hpsa_scan_thread
);
3524 static void __exit
hpsa_cleanup(void)
3526 pci_unregister_driver(&hpsa_pci_driver
);
3527 kthread_stop(hpsa_scan_thread
);
3530 module_init(hpsa_init
);
3531 module_exit(hpsa_cleanup
);