2 * Disk Array driver for HP Smart Array SAS controllers
3 * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12 * NON INFRINGEMENT. See the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * Questions/Comments/Bugfixes to iss_storagedev@hp.com
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/types.h>
25 #include <linux/pci.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
30 #include <linux/timer.h>
31 #include <linux/seq_file.h>
32 #include <linux/init.h>
33 #include <linux/spinlock.h>
34 #include <linux/smp_lock.h>
35 #include <linux/compat.h>
36 #include <linux/blktrace_api.h>
37 #include <linux/uaccess.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/completion.h>
41 #include <linux/moduleparam.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_cmnd.h>
44 #include <scsi/scsi_device.h>
45 #include <scsi/scsi_host.h>
46 #include <scsi/scsi_tcq.h>
47 #include <linux/cciss_ioctl.h>
48 #include <linux/string.h>
49 #include <linux/bitmap.h>
50 #include <asm/atomic.h>
51 #include <linux/kthread.h>
55 /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
56 #define HPSA_DRIVER_VERSION "2.0.1-3"
57 #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
59 /* How long to wait (in milliseconds) for board to go into simple mode */
60 #define MAX_CONFIG_WAIT 30000
61 #define MAX_IOCTL_CONFIG_WAIT 1000
63 /*define how many times we will try a command because of bus resets */
64 #define MAX_CMD_RETRIES 3
66 /* Embedded module documentation macros - see modules.h */
67 MODULE_AUTHOR("Hewlett-Packard Company");
68 MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
70 MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
71 MODULE_VERSION(HPSA_DRIVER_VERSION
);
72 MODULE_LICENSE("GPL");
74 static int hpsa_allow_any
;
75 module_param(hpsa_allow_any
, int, S_IRUGO
|S_IWUSR
);
76 MODULE_PARM_DESC(hpsa_allow_any
,
77 "Allow hpsa driver to access unknown HP Smart Array hardware");
79 /* define the PCI info for the cards we can control */
80 static const struct pci_device_id hpsa_pci_device_id
[] = {
81 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x3241},
82 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x3243},
83 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x3245},
84 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x3247},
85 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x3249},
86 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x324a},
87 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x324b},
88 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSE
, 0x103C, 0x3233},
89 #define PCI_DEVICE_ID_HP_CISSF 0x333f
90 {PCI_VENDOR_ID_HP
, PCI_DEVICE_ID_HP_CISSF
, 0x103C, 0x333F},
91 {PCI_VENDOR_ID_HP
, PCI_ANY_ID
, PCI_ANY_ID
, PCI_ANY_ID
,
92 PCI_CLASS_STORAGE_RAID
<< 8, 0xffff << 8, 0},
96 MODULE_DEVICE_TABLE(pci
, hpsa_pci_device_id
);
98 /* board_id = Subsystem Device ID & Vendor ID
99 * product = Marketing Name for the board
100 * access = Address of the struct of function pointers
102 static struct board_type products
[] = {
103 {0x3241103C, "Smart Array P212", &SA5_access
},
104 {0x3243103C, "Smart Array P410", &SA5_access
},
105 {0x3245103C, "Smart Array P410i", &SA5_access
},
106 {0x3247103C, "Smart Array P411", &SA5_access
},
107 {0x3249103C, "Smart Array P812", &SA5_access
},
108 {0x324a103C, "Smart Array P712m", &SA5_access
},
109 {0x324b103C, "Smart Array P711m", &SA5_access
},
110 {0x3233103C, "StorageWorks P1210m", &SA5_access
},
111 {0x333F103C, "StorageWorks P1210m", &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
*));
135 static void hpsa_scan_start(struct Scsi_Host
*);
136 static int hpsa_scan_finished(struct Scsi_Host
*sh
,
137 unsigned long elapsed_time
);
138 static int hpsa_change_queue_depth(struct scsi_device
*sdev
,
139 int qdepth
, int reason
);
141 static int hpsa_eh_device_reset_handler(struct scsi_cmnd
*scsicmd
);
142 static int hpsa_slave_alloc(struct scsi_device
*sdev
);
143 static void hpsa_slave_destroy(struct scsi_device
*sdev
);
145 static ssize_t
raid_level_show(struct device
*dev
,
146 struct device_attribute
*attr
, char *buf
);
147 static ssize_t
lunid_show(struct device
*dev
,
148 struct device_attribute
*attr
, char *buf
);
149 static ssize_t
unique_id_show(struct device
*dev
,
150 struct device_attribute
*attr
, char *buf
);
151 static void hpsa_update_scsi_devices(struct ctlr_info
*h
, int hostno
);
152 static ssize_t
host_store_rescan(struct device
*dev
,
153 struct device_attribute
*attr
, const char *buf
, size_t count
);
154 static int check_for_unit_attention(struct ctlr_info
*h
,
155 struct CommandList
*c
);
156 static void check_ioctl_unit_attention(struct ctlr_info
*h
,
157 struct CommandList
*c
);
158 /* performant mode helper functions */
159 static void calc_bucket_map(int *bucket
, int num_buckets
,
160 int nsgs
, int *bucket_map
);
161 static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info
*h
);
162 static inline u32
next_command(struct ctlr_info
*h
);
164 static DEVICE_ATTR(raid_level
, S_IRUGO
, raid_level_show
, NULL
);
165 static DEVICE_ATTR(lunid
, S_IRUGO
, lunid_show
, NULL
);
166 static DEVICE_ATTR(unique_id
, S_IRUGO
, unique_id_show
, NULL
);
167 static DEVICE_ATTR(rescan
, S_IWUSR
, NULL
, host_store_rescan
);
169 static struct device_attribute
*hpsa_sdev_attrs
[] = {
170 &dev_attr_raid_level
,
176 static struct device_attribute
*hpsa_shost_attrs
[] = {
181 static struct scsi_host_template hpsa_driver_template
= {
182 .module
= THIS_MODULE
,
185 .queuecommand
= hpsa_scsi_queue_command
,
186 .scan_start
= hpsa_scan_start
,
187 .scan_finished
= hpsa_scan_finished
,
188 .change_queue_depth
= hpsa_change_queue_depth
,
190 .sg_tablesize
= MAXSGENTRIES
,
191 .use_clustering
= ENABLE_CLUSTERING
,
192 .eh_device_reset_handler
= hpsa_eh_device_reset_handler
,
194 .slave_alloc
= hpsa_slave_alloc
,
195 .slave_destroy
= hpsa_slave_destroy
,
197 .compat_ioctl
= hpsa_compat_ioctl
,
199 .sdev_attrs
= hpsa_sdev_attrs
,
200 .shost_attrs
= hpsa_shost_attrs
,
203 static inline struct ctlr_info
*sdev_to_hba(struct scsi_device
*sdev
)
205 unsigned long *priv
= shost_priv(sdev
->host
);
206 return (struct ctlr_info
*) *priv
;
209 static inline struct ctlr_info
*shost_to_hba(struct Scsi_Host
*sh
)
211 unsigned long *priv
= shost_priv(sh
);
212 return (struct ctlr_info
*) *priv
;
215 static struct task_struct
*hpsa_scan_thread
;
216 static DEFINE_MUTEX(hpsa_scan_mutex
);
217 static LIST_HEAD(hpsa_scan_q
);
218 static int hpsa_scan_func(void *data
);
221 * add_to_scan_list() - add controller to rescan queue
222 * @h: Pointer to the controller.
224 * Adds the controller to the rescan queue if not already on the queue.
226 * returns 1 if added to the queue, 0 if skipped (could be on the
227 * queue already, or the controller could be initializing or shutting
230 static int add_to_scan_list(struct ctlr_info
*h
)
232 struct ctlr_info
*test_h
;
236 if (h
->busy_initializing
)
240 * If we don't get the lock, it means the driver is unloading
241 * and there's no point in scheduling a new scan.
243 if (!mutex_trylock(&h
->busy_shutting_down
))
246 mutex_lock(&hpsa_scan_mutex
);
247 list_for_each_entry(test_h
, &hpsa_scan_q
, scan_list
) {
253 if (!found
&& !h
->busy_scanning
) {
254 INIT_COMPLETION(h
->scan_wait
);
255 list_add_tail(&h
->scan_list
, &hpsa_scan_q
);
258 mutex_unlock(&hpsa_scan_mutex
);
259 mutex_unlock(&h
->busy_shutting_down
);
265 * remove_from_scan_list() - remove controller from rescan queue
266 * @h: Pointer to the controller.
268 * Removes the controller from the rescan queue if present. Blocks if
269 * the controller is currently conducting a rescan. The controller
270 * can be in one of three states:
271 * 1. Doesn't need a scan
272 * 2. On the scan list, but not scanning yet (we remove it)
273 * 3. Busy scanning (and not on the list). In this case we want to wait for
274 * the scan to complete to make sure the scanning thread for this
275 * controller is completely idle.
277 static void remove_from_scan_list(struct ctlr_info
*h
)
279 struct ctlr_info
*test_h
, *tmp_h
;
281 mutex_lock(&hpsa_scan_mutex
);
282 list_for_each_entry_safe(test_h
, tmp_h
, &hpsa_scan_q
, scan_list
) {
283 if (test_h
== h
) { /* state 2. */
284 list_del(&h
->scan_list
);
285 complete_all(&h
->scan_wait
);
286 mutex_unlock(&hpsa_scan_mutex
);
290 if (h
->busy_scanning
) { /* state 3. */
291 mutex_unlock(&hpsa_scan_mutex
);
292 wait_for_completion(&h
->scan_wait
);
293 } else { /* state 1, nothing to do. */
294 mutex_unlock(&hpsa_scan_mutex
);
298 /* hpsa_scan_func() - kernel thread used to rescan controllers
301 * A kernel thread used scan for drive topology changes on
302 * controllers. The thread processes only one controller at a time
303 * using a queue. Controllers are added to the queue using
304 * add_to_scan_list() and removed from the queue either after done
305 * processing or using remove_from_scan_list().
309 static int hpsa_scan_func(__attribute__((unused
)) void *data
)
315 set_current_state(TASK_INTERRUPTIBLE
);
317 if (kthread_should_stop())
321 mutex_lock(&hpsa_scan_mutex
);
322 if (list_empty(&hpsa_scan_q
)) {
323 mutex_unlock(&hpsa_scan_mutex
);
326 h
= list_entry(hpsa_scan_q
.next
, struct ctlr_info
,
328 list_del(&h
->scan_list
);
329 h
->busy_scanning
= 1;
330 mutex_unlock(&hpsa_scan_mutex
);
331 host_no
= h
->scsi_host
? h
->scsi_host
->host_no
: -1;
332 hpsa_scan_start(h
->scsi_host
);
333 complete_all(&h
->scan_wait
);
334 mutex_lock(&hpsa_scan_mutex
);
335 h
->busy_scanning
= 0;
336 mutex_unlock(&hpsa_scan_mutex
);
342 static int check_for_unit_attention(struct ctlr_info
*h
,
343 struct CommandList
*c
)
345 if (c
->err_info
->SenseInfo
[2] != UNIT_ATTENTION
)
348 switch (c
->err_info
->SenseInfo
[12]) {
350 dev_warn(&h
->pdev
->dev
, "hpsa%d: a state change "
351 "detected, command retried\n", h
->ctlr
);
354 dev_warn(&h
->pdev
->dev
, "hpsa%d: LUN failure "
355 "detected, action required\n", h
->ctlr
);
357 case REPORT_LUNS_CHANGED
:
358 dev_warn(&h
->pdev
->dev
, "hpsa%d: report LUN data "
359 "changed\n", h
->ctlr
);
361 * Here, we could call add_to_scan_list and wake up the scan thread,
362 * except that it's quite likely that we will get more than one
363 * REPORT_LUNS_CHANGED condition in quick succession, which means
364 * that those which occur after the first one will likely happen
365 * *during* the hpsa_scan_thread's rescan. And the rescan code is not
366 * robust enough to restart in the middle, undoing what it has already
367 * done, and it's not clear that it's even possible to do this, since
368 * part of what it does is notify the SCSI mid layer, which starts
369 * doing it's own i/o to read partition tables and so on, and the
370 * driver doesn't have visibility to know what might need undoing.
371 * In any event, if possible, it is horribly complicated to get right
372 * so we just don't do it for now.
374 * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
378 dev_warn(&h
->pdev
->dev
, "hpsa%d: a power on "
379 "or device reset detected\n", h
->ctlr
);
381 case UNIT_ATTENTION_CLEARED
:
382 dev_warn(&h
->pdev
->dev
, "hpsa%d: unit attention "
383 "cleared by another initiator\n", h
->ctlr
);
386 dev_warn(&h
->pdev
->dev
, "hpsa%d: unknown "
387 "unit attention detected\n", h
->ctlr
);
393 static ssize_t
host_store_rescan(struct device
*dev
,
394 struct device_attribute
*attr
,
395 const char *buf
, size_t count
)
398 struct Scsi_Host
*shost
= class_to_shost(dev
);
399 h
= shost_to_hba(shost
);
400 if (add_to_scan_list(h
)) {
401 wake_up_process(hpsa_scan_thread
);
402 wait_for_completion_interruptible(&h
->scan_wait
);
407 /* Enqueuing and dequeuing functions for cmdlists. */
408 static inline void addQ(struct hlist_head
*list
, struct CommandList
*c
)
410 hlist_add_head(&c
->list
, list
);
413 static inline u32
next_command(struct ctlr_info
*h
)
417 if (unlikely(h
->transMethod
!= CFGTBL_Trans_Performant
))
418 return h
->access
.command_completed(h
);
420 if ((*(h
->reply_pool_head
) & 1) == (h
->reply_pool_wraparound
)) {
421 a
= *(h
->reply_pool_head
); /* Next cmd in ring buffer */
422 (h
->reply_pool_head
)++;
423 h
->commands_outstanding
--;
427 /* Check for wraparound */
428 if (h
->reply_pool_head
== (h
->reply_pool
+ h
->max_commands
)) {
429 h
->reply_pool_head
= h
->reply_pool
;
430 h
->reply_pool_wraparound
^= 1;
435 /* set_performant_mode: Modify the tag for cciss performant
436 * set bit 0 for pull model, bits 3-1 for block fetch
439 static void set_performant_mode(struct ctlr_info
*h
, struct CommandList
*c
)
441 if (likely(h
->transMethod
== CFGTBL_Trans_Performant
))
442 c
->busaddr
|= 1 | (h
->blockFetchTable
[c
->Header
.SGList
] << 1);
445 static void enqueue_cmd_and_start_io(struct ctlr_info
*h
,
446 struct CommandList
*c
)
450 set_performant_mode(h
, c
);
451 spin_lock_irqsave(&h
->lock
, flags
);
455 spin_unlock_irqrestore(&h
->lock
, flags
);
458 static inline void removeQ(struct CommandList
*c
)
460 if (WARN_ON(hlist_unhashed(&c
->list
)))
462 hlist_del_init(&c
->list
);
465 static inline int is_hba_lunid(unsigned char scsi3addr
[])
467 return memcmp(scsi3addr
, RAID_CTLR_LUNID
, 8) == 0;
470 static inline int is_logical_dev_addr_mode(unsigned char scsi3addr
[])
472 return (scsi3addr
[3] & 0xC0) == 0x40;
475 static inline int is_scsi_rev_5(struct ctlr_info
*h
)
477 if (!h
->hba_inquiry_data
)
479 if ((h
->hba_inquiry_data
[2] & 0x07) == 5)
484 static const char *raid_label
[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
487 #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
489 static ssize_t
raid_level_show(struct device
*dev
,
490 struct device_attribute
*attr
, char *buf
)
493 unsigned char rlevel
;
495 struct scsi_device
*sdev
;
496 struct hpsa_scsi_dev_t
*hdev
;
499 sdev
= to_scsi_device(dev
);
500 h
= sdev_to_hba(sdev
);
501 spin_lock_irqsave(&h
->lock
, flags
);
502 hdev
= sdev
->hostdata
;
504 spin_unlock_irqrestore(&h
->lock
, flags
);
508 /* Is this even a logical drive? */
509 if (!is_logical_dev_addr_mode(hdev
->scsi3addr
)) {
510 spin_unlock_irqrestore(&h
->lock
, flags
);
511 l
= snprintf(buf
, PAGE_SIZE
, "N/A\n");
515 rlevel
= hdev
->raid_level
;
516 spin_unlock_irqrestore(&h
->lock
, flags
);
517 if (rlevel
> RAID_UNKNOWN
)
518 rlevel
= RAID_UNKNOWN
;
519 l
= snprintf(buf
, PAGE_SIZE
, "RAID %s\n", raid_label
[rlevel
]);
523 static ssize_t
lunid_show(struct device
*dev
,
524 struct device_attribute
*attr
, char *buf
)
527 struct scsi_device
*sdev
;
528 struct hpsa_scsi_dev_t
*hdev
;
530 unsigned char lunid
[8];
532 sdev
= to_scsi_device(dev
);
533 h
= sdev_to_hba(sdev
);
534 spin_lock_irqsave(&h
->lock
, flags
);
535 hdev
= sdev
->hostdata
;
537 spin_unlock_irqrestore(&h
->lock
, flags
);
540 memcpy(lunid
, hdev
->scsi3addr
, sizeof(lunid
));
541 spin_unlock_irqrestore(&h
->lock
, flags
);
542 return snprintf(buf
, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
543 lunid
[0], lunid
[1], lunid
[2], lunid
[3],
544 lunid
[4], lunid
[5], lunid
[6], lunid
[7]);
547 static ssize_t
unique_id_show(struct device
*dev
,
548 struct device_attribute
*attr
, char *buf
)
551 struct scsi_device
*sdev
;
552 struct hpsa_scsi_dev_t
*hdev
;
554 unsigned char sn
[16];
556 sdev
= to_scsi_device(dev
);
557 h
= sdev_to_hba(sdev
);
558 spin_lock_irqsave(&h
->lock
, flags
);
559 hdev
= sdev
->hostdata
;
561 spin_unlock_irqrestore(&h
->lock
, flags
);
564 memcpy(sn
, hdev
->device_id
, sizeof(sn
));
565 spin_unlock_irqrestore(&h
->lock
, flags
);
566 return snprintf(buf
, 16 * 2 + 2,
567 "%02X%02X%02X%02X%02X%02X%02X%02X"
568 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
569 sn
[0], sn
[1], sn
[2], sn
[3],
570 sn
[4], sn
[5], sn
[6], sn
[7],
571 sn
[8], sn
[9], sn
[10], sn
[11],
572 sn
[12], sn
[13], sn
[14], sn
[15]);
575 static int hpsa_find_target_lun(struct ctlr_info
*h
,
576 unsigned char scsi3addr
[], int bus
, int *target
, int *lun
)
578 /* finds an unused bus, target, lun for a new physical device
579 * assumes h->devlock is held
582 DECLARE_BITMAP(lun_taken
, HPSA_MAX_SCSI_DEVS_PER_HBA
);
584 memset(&lun_taken
[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA
>> 3);
586 for (i
= 0; i
< h
->ndevices
; i
++) {
587 if (h
->dev
[i
]->bus
== bus
&& h
->dev
[i
]->target
!= -1)
588 set_bit(h
->dev
[i
]->target
, lun_taken
);
591 for (i
= 0; i
< HPSA_MAX_SCSI_DEVS_PER_HBA
; i
++) {
592 if (!test_bit(i
, lun_taken
)) {
603 /* Add an entry into h->dev[] array. */
604 static int hpsa_scsi_add_entry(struct ctlr_info
*h
, int hostno
,
605 struct hpsa_scsi_dev_t
*device
,
606 struct hpsa_scsi_dev_t
*added
[], int *nadded
)
608 /* assumes h->devlock is held */
611 unsigned char addr1
[8], addr2
[8];
612 struct hpsa_scsi_dev_t
*sd
;
614 if (n
>= HPSA_MAX_SCSI_DEVS_PER_HBA
) {
615 dev_err(&h
->pdev
->dev
, "too many devices, some will be "
620 /* physical devices do not have lun or target assigned until now. */
621 if (device
->lun
!= -1)
622 /* Logical device, lun is already assigned. */
625 /* If this device a non-zero lun of a multi-lun device
626 * byte 4 of the 8-byte LUN addr will contain the logical
627 * unit no, zero otherise.
629 if (device
->scsi3addr
[4] == 0) {
630 /* This is not a non-zero lun of a multi-lun device */
631 if (hpsa_find_target_lun(h
, device
->scsi3addr
,
632 device
->bus
, &device
->target
, &device
->lun
) != 0)
637 /* This is a non-zero lun of a multi-lun device.
638 * Search through our list and find the device which
639 * has the same 8 byte LUN address, excepting byte 4.
640 * Assign the same bus and target for this new LUN.
641 * Use the logical unit number from the firmware.
643 memcpy(addr1
, device
->scsi3addr
, 8);
645 for (i
= 0; i
< n
; i
++) {
647 memcpy(addr2
, sd
->scsi3addr
, 8);
649 /* differ only in byte 4? */
650 if (memcmp(addr1
, addr2
, 8) == 0) {
651 device
->bus
= sd
->bus
;
652 device
->target
= sd
->target
;
653 device
->lun
= device
->scsi3addr
[4];
657 if (device
->lun
== -1) {
658 dev_warn(&h
->pdev
->dev
, "physical device with no LUN=0,"
659 " suspect firmware bug or unsupported hardware "
668 added
[*nadded
] = device
;
671 /* initially, (before registering with scsi layer) we don't
672 * know our hostno and we don't want to print anything first
673 * time anyway (the scsi layer's inquiries will show that info)
675 /* if (hostno != -1) */
676 dev_info(&h
->pdev
->dev
, "%s device c%db%dt%dl%d added.\n",
677 scsi_device_type(device
->devtype
), hostno
,
678 device
->bus
, device
->target
, device
->lun
);
682 /* Replace an entry from h->dev[] array. */
683 static void hpsa_scsi_replace_entry(struct ctlr_info
*h
, int hostno
,
684 int entry
, struct hpsa_scsi_dev_t
*new_entry
,
685 struct hpsa_scsi_dev_t
*added
[], int *nadded
,
686 struct hpsa_scsi_dev_t
*removed
[], int *nremoved
)
688 /* assumes h->devlock is held */
689 BUG_ON(entry
< 0 || entry
>= HPSA_MAX_SCSI_DEVS_PER_HBA
);
690 removed
[*nremoved
] = h
->dev
[entry
];
692 h
->dev
[entry
] = new_entry
;
693 added
[*nadded
] = new_entry
;
695 dev_info(&h
->pdev
->dev
, "%s device c%db%dt%dl%d changed.\n",
696 scsi_device_type(new_entry
->devtype
), hostno
, new_entry
->bus
,
697 new_entry
->target
, new_entry
->lun
);
700 /* Remove an entry from h->dev[] array. */
701 static void hpsa_scsi_remove_entry(struct ctlr_info
*h
, int hostno
, int entry
,
702 struct hpsa_scsi_dev_t
*removed
[], int *nremoved
)
704 /* assumes h->devlock is held */
706 struct hpsa_scsi_dev_t
*sd
;
708 BUG_ON(entry
< 0 || entry
>= HPSA_MAX_SCSI_DEVS_PER_HBA
);
711 removed
[*nremoved
] = h
->dev
[entry
];
714 for (i
= entry
; i
< h
->ndevices
-1; i
++)
715 h
->dev
[i
] = h
->dev
[i
+1];
717 dev_info(&h
->pdev
->dev
, "%s device c%db%dt%dl%d removed.\n",
718 scsi_device_type(sd
->devtype
), hostno
, sd
->bus
, sd
->target
,
722 #define SCSI3ADDR_EQ(a, b) ( \
723 (a)[7] == (b)[7] && \
724 (a)[6] == (b)[6] && \
725 (a)[5] == (b)[5] && \
726 (a)[4] == (b)[4] && \
727 (a)[3] == (b)[3] && \
728 (a)[2] == (b)[2] && \
729 (a)[1] == (b)[1] && \
732 static void fixup_botched_add(struct ctlr_info
*h
,
733 struct hpsa_scsi_dev_t
*added
)
735 /* called when scsi_add_device fails in order to re-adjust
736 * h->dev[] to match the mid layer's view.
741 spin_lock_irqsave(&h
->lock
, flags
);
742 for (i
= 0; i
< h
->ndevices
; i
++) {
743 if (h
->dev
[i
] == added
) {
744 for (j
= i
; j
< h
->ndevices
-1; j
++)
745 h
->dev
[j
] = h
->dev
[j
+1];
750 spin_unlock_irqrestore(&h
->lock
, flags
);
754 static inline int device_is_the_same(struct hpsa_scsi_dev_t
*dev1
,
755 struct hpsa_scsi_dev_t
*dev2
)
757 if ((is_logical_dev_addr_mode(dev1
->scsi3addr
) ||
758 (dev1
->lun
!= -1 && dev2
->lun
!= -1)) &&
759 dev1
->devtype
!= 0x0C)
760 return (memcmp(dev1
, dev2
, sizeof(*dev1
)) == 0);
762 /* we compare everything except lun and target as these
763 * are not yet assigned. Compare parts likely
766 if (memcmp(dev1
->scsi3addr
, dev2
->scsi3addr
,
767 sizeof(dev1
->scsi3addr
)) != 0)
769 if (memcmp(dev1
->device_id
, dev2
->device_id
,
770 sizeof(dev1
->device_id
)) != 0)
772 if (memcmp(dev1
->model
, dev2
->model
, sizeof(dev1
->model
)) != 0)
774 if (memcmp(dev1
->vendor
, dev2
->vendor
, sizeof(dev1
->vendor
)) != 0)
776 if (memcmp(dev1
->revision
, dev2
->revision
, sizeof(dev1
->revision
)) != 0)
778 if (dev1
->devtype
!= dev2
->devtype
)
780 if (dev1
->raid_level
!= dev2
->raid_level
)
782 if (dev1
->bus
!= dev2
->bus
)
787 /* Find needle in haystack. If exact match found, return DEVICE_SAME,
788 * and return needle location in *index. If scsi3addr matches, but not
789 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
790 * location in *index. If needle not found, return DEVICE_NOT_FOUND.
792 static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t
*needle
,
793 struct hpsa_scsi_dev_t
*haystack
[], int haystack_size
,
797 #define DEVICE_NOT_FOUND 0
798 #define DEVICE_CHANGED 1
799 #define DEVICE_SAME 2
800 for (i
= 0; i
< haystack_size
; i
++) {
801 if (haystack
[i
] == NULL
) /* previously removed. */
803 if (SCSI3ADDR_EQ(needle
->scsi3addr
, haystack
[i
]->scsi3addr
)) {
805 if (device_is_the_same(needle
, haystack
[i
]))
808 return DEVICE_CHANGED
;
812 return DEVICE_NOT_FOUND
;
815 static void adjust_hpsa_scsi_table(struct ctlr_info
*h
, int hostno
,
816 struct hpsa_scsi_dev_t
*sd
[], int nsds
)
818 /* sd contains scsi3 addresses and devtypes, and inquiry
819 * data. This function takes what's in sd to be the current
820 * reality and updates h->dev[] to reflect that reality.
822 int i
, entry
, device_change
, changes
= 0;
823 struct hpsa_scsi_dev_t
*csd
;
825 struct hpsa_scsi_dev_t
**added
, **removed
;
826 int nadded
, nremoved
;
827 struct Scsi_Host
*sh
= NULL
;
829 added
= kzalloc(sizeof(*added
) * HPSA_MAX_SCSI_DEVS_PER_HBA
,
831 removed
= kzalloc(sizeof(*removed
) * HPSA_MAX_SCSI_DEVS_PER_HBA
,
834 if (!added
|| !removed
) {
835 dev_warn(&h
->pdev
->dev
, "out of memory in "
836 "adjust_hpsa_scsi_table\n");
840 spin_lock_irqsave(&h
->devlock
, flags
);
842 /* find any devices in h->dev[] that are not in
843 * sd[] and remove them from h->dev[], and for any
844 * devices which have changed, remove the old device
845 * info and add the new device info.
850 while (i
< h
->ndevices
) {
852 device_change
= hpsa_scsi_find_entry(csd
, sd
, nsds
, &entry
);
853 if (device_change
== DEVICE_NOT_FOUND
) {
855 hpsa_scsi_remove_entry(h
, hostno
, i
,
857 continue; /* remove ^^^, hence i not incremented */
858 } else if (device_change
== DEVICE_CHANGED
) {
860 hpsa_scsi_replace_entry(h
, hostno
, i
, sd
[entry
],
861 added
, &nadded
, removed
, &nremoved
);
862 /* Set it to NULL to prevent it from being freed
863 * at the bottom of hpsa_update_scsi_devices()
870 /* Now, make sure every device listed in sd[] is also
871 * listed in h->dev[], adding them if they aren't found
874 for (i
= 0; i
< nsds
; i
++) {
875 if (!sd
[i
]) /* if already added above. */
877 device_change
= hpsa_scsi_find_entry(sd
[i
], h
->dev
,
878 h
->ndevices
, &entry
);
879 if (device_change
== DEVICE_NOT_FOUND
) {
881 if (hpsa_scsi_add_entry(h
, hostno
, sd
[i
],
882 added
, &nadded
) != 0)
884 sd
[i
] = NULL
; /* prevent from being freed later. */
885 } else if (device_change
== DEVICE_CHANGED
) {
886 /* should never happen... */
888 dev_warn(&h
->pdev
->dev
,
889 "device unexpectedly changed.\n");
890 /* but if it does happen, we just ignore that device */
893 spin_unlock_irqrestore(&h
->devlock
, flags
);
895 /* Don't notify scsi mid layer of any changes the first time through
896 * (or if there are no changes) scsi_scan_host will do it later the
897 * first time through.
899 if (hostno
== -1 || !changes
)
903 /* Notify scsi mid layer of any removed devices */
904 for (i
= 0; i
< nremoved
; i
++) {
905 struct scsi_device
*sdev
=
906 scsi_device_lookup(sh
, removed
[i
]->bus
,
907 removed
[i
]->target
, removed
[i
]->lun
);
909 scsi_remove_device(sdev
);
910 scsi_device_put(sdev
);
912 /* We don't expect to get here.
913 * future cmds to this device will get selection
914 * timeout as if the device was gone.
916 dev_warn(&h
->pdev
->dev
, "didn't find c%db%dt%dl%d "
917 " for removal.", hostno
, removed
[i
]->bus
,
918 removed
[i
]->target
, removed
[i
]->lun
);
924 /* Notify scsi mid layer of any added devices */
925 for (i
= 0; i
< nadded
; i
++) {
926 if (scsi_add_device(sh
, added
[i
]->bus
,
927 added
[i
]->target
, added
[i
]->lun
) == 0)
929 dev_warn(&h
->pdev
->dev
, "scsi_add_device c%db%dt%dl%d failed, "
930 "device not added.\n", hostno
, added
[i
]->bus
,
931 added
[i
]->target
, added
[i
]->lun
);
932 /* now we have to remove it from h->dev,
933 * since it didn't get added to scsi mid layer
935 fixup_botched_add(h
, added
[i
]);
944 * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
945 * Assume's h->devlock is held.
947 static struct hpsa_scsi_dev_t
*lookup_hpsa_scsi_dev(struct ctlr_info
*h
,
948 int bus
, int target
, int lun
)
951 struct hpsa_scsi_dev_t
*sd
;
953 for (i
= 0; i
< h
->ndevices
; i
++) {
955 if (sd
->bus
== bus
&& sd
->target
== target
&& sd
->lun
== lun
)
961 /* link sdev->hostdata to our per-device structure. */
962 static int hpsa_slave_alloc(struct scsi_device
*sdev
)
964 struct hpsa_scsi_dev_t
*sd
;
968 h
= sdev_to_hba(sdev
);
969 spin_lock_irqsave(&h
->devlock
, flags
);
970 sd
= lookup_hpsa_scsi_dev(h
, sdev_channel(sdev
),
971 sdev_id(sdev
), sdev
->lun
);
974 spin_unlock_irqrestore(&h
->devlock
, flags
);
978 static void hpsa_slave_destroy(struct scsi_device
*sdev
)
983 static void hpsa_scsi_setup(struct ctlr_info
*h
)
987 spin_lock_init(&h
->devlock
);
990 static void complete_scsi_command(struct CommandList
*cp
,
991 int timeout
, u32 tag
)
993 struct scsi_cmnd
*cmd
;
995 struct ErrorInfo
*ei
;
997 unsigned char sense_key
;
998 unsigned char asc
; /* additional sense code */
999 unsigned char ascq
; /* additional sense code qualifier */
1002 cmd
= (struct scsi_cmnd
*) cp
->scsi_cmd
;
1005 scsi_dma_unmap(cmd
); /* undo the DMA mappings */
1007 cmd
->result
= (DID_OK
<< 16); /* host byte */
1008 cmd
->result
|= (COMMAND_COMPLETE
<< 8); /* msg byte */
1009 cmd
->result
|= (ei
->ScsiStatus
<< 1);
1011 /* copy the sense data whether we need to or not. */
1012 memcpy(cmd
->sense_buffer
, ei
->SenseInfo
,
1013 ei
->SenseLen
> SCSI_SENSE_BUFFERSIZE
?
1014 SCSI_SENSE_BUFFERSIZE
:
1016 scsi_set_resid(cmd
, ei
->ResidualCnt
);
1018 if (ei
->CommandStatus
== 0) {
1019 cmd
->scsi_done(cmd
);
1024 /* an error has occurred */
1025 switch (ei
->CommandStatus
) {
1027 case CMD_TARGET_STATUS
:
1028 if (ei
->ScsiStatus
) {
1030 sense_key
= 0xf & ei
->SenseInfo
[2];
1031 /* Get additional sense code */
1032 asc
= ei
->SenseInfo
[12];
1033 /* Get addition sense code qualifier */
1034 ascq
= ei
->SenseInfo
[13];
1037 if (ei
->ScsiStatus
== SAM_STAT_CHECK_CONDITION
) {
1038 if (check_for_unit_attention(h
, cp
)) {
1039 cmd
->result
= DID_SOFT_ERROR
<< 16;
1042 if (sense_key
== ILLEGAL_REQUEST
) {
1044 * SCSI REPORT_LUNS is commonly unsupported on
1045 * Smart Array. Suppress noisy complaint.
1047 if (cp
->Request
.CDB
[0] == REPORT_LUNS
)
1050 /* If ASC/ASCQ indicate Logical Unit
1051 * Not Supported condition,
1053 if ((asc
== 0x25) && (ascq
== 0x0)) {
1054 dev_warn(&h
->pdev
->dev
, "cp %p "
1055 "has check condition\n", cp
);
1060 if (sense_key
== NOT_READY
) {
1061 /* If Sense is Not Ready, Logical Unit
1062 * Not ready, Manual Intervention
1065 if ((asc
== 0x04) && (ascq
== 0x03)) {
1066 dev_warn(&h
->pdev
->dev
, "cp %p "
1067 "has check condition: unit "
1068 "not ready, manual "
1069 "intervention required\n", cp
);
1073 if (sense_key
== ABORTED_COMMAND
) {
1074 /* Aborted command is retryable */
1075 dev_warn(&h
->pdev
->dev
, "cp %p "
1076 "has check condition: aborted command: "
1077 "ASC: 0x%x, ASCQ: 0x%x\n",
1079 cmd
->result
= DID_SOFT_ERROR
<< 16;
1082 /* Must be some other type of check condition */
1083 dev_warn(&h
->pdev
->dev
, "cp %p has check condition: "
1085 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1086 "Returning result: 0x%x, "
1087 "cmd=[%02x %02x %02x %02x %02x "
1088 "%02x %02x %02x %02x %02x %02x "
1089 "%02x %02x %02x %02x %02x]\n",
1090 cp
, sense_key
, asc
, ascq
,
1092 cmd
->cmnd
[0], cmd
->cmnd
[1],
1093 cmd
->cmnd
[2], cmd
->cmnd
[3],
1094 cmd
->cmnd
[4], cmd
->cmnd
[5],
1095 cmd
->cmnd
[6], cmd
->cmnd
[7],
1096 cmd
->cmnd
[8], cmd
->cmnd
[9],
1097 cmd
->cmnd
[10], cmd
->cmnd
[11],
1098 cmd
->cmnd
[12], cmd
->cmnd
[13],
1099 cmd
->cmnd
[14], cmd
->cmnd
[15]);
1104 /* Problem was not a check condition
1105 * Pass it up to the upper layers...
1107 if (ei
->ScsiStatus
) {
1108 dev_warn(&h
->pdev
->dev
, "cp %p has status 0x%x "
1109 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1110 "Returning result: 0x%x\n",
1112 sense_key
, asc
, ascq
,
1114 } else { /* scsi status is zero??? How??? */
1115 dev_warn(&h
->pdev
->dev
, "cp %p SCSI status was 0. "
1116 "Returning no connection.\n", cp
),
1118 /* Ordinarily, this case should never happen,
1119 * but there is a bug in some released firmware
1120 * revisions that allows it to happen if, for
1121 * example, a 4100 backplane loses power and
1122 * the tape drive is in it. We assume that
1123 * it's a fatal error of some kind because we
1124 * can't show that it wasn't. We will make it
1125 * look like selection timeout since that is
1126 * the most common reason for this to occur,
1127 * and it's severe enough.
1130 cmd
->result
= DID_NO_CONNECT
<< 16;
1134 case CMD_DATA_UNDERRUN
: /* let mid layer handle it. */
1136 case CMD_DATA_OVERRUN
:
1137 dev_warn(&h
->pdev
->dev
, "cp %p has"
1138 " completed with data overrun "
1142 /* print_bytes(cp, sizeof(*cp), 1, 0);
1144 /* We get CMD_INVALID if you address a non-existent device
1145 * instead of a selection timeout (no response). You will
1146 * see this if you yank out a drive, then try to access it.
1147 * This is kind of a shame because it means that any other
1148 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1149 * missing target. */
1150 cmd
->result
= DID_NO_CONNECT
<< 16;
1153 case CMD_PROTOCOL_ERR
:
1154 dev_warn(&h
->pdev
->dev
, "cp %p has "
1155 "protocol error \n", cp
);
1157 case CMD_HARDWARE_ERR
:
1158 cmd
->result
= DID_ERROR
<< 16;
1159 dev_warn(&h
->pdev
->dev
, "cp %p had hardware error\n", cp
);
1161 case CMD_CONNECTION_LOST
:
1162 cmd
->result
= DID_ERROR
<< 16;
1163 dev_warn(&h
->pdev
->dev
, "cp %p had connection lost\n", cp
);
1166 cmd
->result
= DID_ABORT
<< 16;
1167 dev_warn(&h
->pdev
->dev
, "cp %p was aborted with status 0x%x\n",
1168 cp
, ei
->ScsiStatus
);
1170 case CMD_ABORT_FAILED
:
1171 cmd
->result
= DID_ERROR
<< 16;
1172 dev_warn(&h
->pdev
->dev
, "cp %p reports abort failed\n", cp
);
1174 case CMD_UNSOLICITED_ABORT
:
1175 cmd
->result
= DID_RESET
<< 16;
1176 dev_warn(&h
->pdev
->dev
, "cp %p aborted do to an unsolicited "
1180 cmd
->result
= DID_TIME_OUT
<< 16;
1181 dev_warn(&h
->pdev
->dev
, "cp %p timedout\n", cp
);
1184 cmd
->result
= DID_ERROR
<< 16;
1185 dev_warn(&h
->pdev
->dev
, "cp %p returned unknown status %x\n",
1186 cp
, ei
->CommandStatus
);
1188 cmd
->scsi_done(cmd
);
1192 static int hpsa_scsi_detect(struct ctlr_info
*h
)
1194 struct Scsi_Host
*sh
;
1197 sh
= scsi_host_alloc(&hpsa_driver_template
, sizeof(h
));
1204 sh
->max_channel
= 3;
1205 sh
->max_cmd_len
= MAX_COMMAND_SIZE
;
1206 sh
->max_lun
= HPSA_MAX_LUN
;
1207 sh
->max_id
= HPSA_MAX_LUN
;
1208 sh
->can_queue
= h
->nr_cmds
;
1209 sh
->cmd_per_lun
= h
->nr_cmds
;
1211 sh
->hostdata
[0] = (unsigned long) h
;
1212 sh
->irq
= h
->intr
[PERF_MODE_INT
];
1213 sh
->unique_id
= sh
->irq
;
1214 error
= scsi_add_host(sh
, &h
->pdev
->dev
);
1221 dev_err(&h
->pdev
->dev
, "hpsa_scsi_detect: scsi_add_host"
1222 " failed for controller %d\n", h
->ctlr
);
1226 dev_err(&h
->pdev
->dev
, "hpsa_scsi_detect: scsi_host_alloc"
1227 " failed for controller %d\n", h
->ctlr
);
1231 static void hpsa_pci_unmap(struct pci_dev
*pdev
,
1232 struct CommandList
*c
, int sg_used
, int data_direction
)
1235 union u64bit addr64
;
1237 for (i
= 0; i
< sg_used
; i
++) {
1238 addr64
.val32
.lower
= c
->SG
[i
].Addr
.lower
;
1239 addr64
.val32
.upper
= c
->SG
[i
].Addr
.upper
;
1240 pci_unmap_single(pdev
, (dma_addr_t
) addr64
.val
, c
->SG
[i
].Len
,
1245 static void hpsa_map_one(struct pci_dev
*pdev
,
1246 struct CommandList
*cp
,
1253 if (buflen
== 0 || data_direction
== PCI_DMA_NONE
) {
1254 cp
->Header
.SGList
= 0;
1255 cp
->Header
.SGTotal
= 0;
1259 addr64
= (u64
) pci_map_single(pdev
, buf
, buflen
, data_direction
);
1260 cp
->SG
[0].Addr
.lower
=
1261 (u32
) (addr64
& (u64
) 0x00000000FFFFFFFF);
1262 cp
->SG
[0].Addr
.upper
=
1263 (u32
) ((addr64
>> 32) & (u64
) 0x00000000FFFFFFFF);
1264 cp
->SG
[0].Len
= buflen
;
1265 cp
->Header
.SGList
= (u8
) 1; /* no. SGs contig in this cmd */
1266 cp
->Header
.SGTotal
= (u16
) 1; /* total sgs in this cmd list */
1269 static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info
*h
,
1270 struct CommandList
*c
)
1272 DECLARE_COMPLETION_ONSTACK(wait
);
1275 enqueue_cmd_and_start_io(h
, c
);
1276 wait_for_completion(&wait
);
1279 static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info
*h
,
1280 struct CommandList
*c
, int data_direction
)
1282 int retry_count
= 0;
1285 memset(c
->err_info
, 0, sizeof(c
->err_info
));
1286 hpsa_scsi_do_simple_cmd_core(h
, c
);
1288 } while (check_for_unit_attention(h
, c
) && retry_count
<= 3);
1289 hpsa_pci_unmap(h
->pdev
, c
, 1, data_direction
);
1292 static void hpsa_scsi_interpret_error(struct CommandList
*cp
)
1294 struct ErrorInfo
*ei
;
1295 struct device
*d
= &cp
->h
->pdev
->dev
;
1298 switch (ei
->CommandStatus
) {
1299 case CMD_TARGET_STATUS
:
1300 dev_warn(d
, "cmd %p has completed with errors\n", cp
);
1301 dev_warn(d
, "cmd %p has SCSI Status = %x\n", cp
,
1303 if (ei
->ScsiStatus
== 0)
1304 dev_warn(d
, "SCSI status is abnormally zero. "
1305 "(probably indicates selection timeout "
1306 "reported incorrectly due to a known "
1307 "firmware bug, circa July, 2001.)\n");
1309 case CMD_DATA_UNDERRUN
: /* let mid layer handle it. */
1310 dev_info(d
, "UNDERRUN\n");
1312 case CMD_DATA_OVERRUN
:
1313 dev_warn(d
, "cp %p has completed with data overrun\n", cp
);
1316 /* controller unfortunately reports SCSI passthru's
1317 * to non-existent targets as invalid commands.
1319 dev_warn(d
, "cp %p is reported invalid (probably means "
1320 "target device no longer present)\n", cp
);
1321 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1325 case CMD_PROTOCOL_ERR
:
1326 dev_warn(d
, "cp %p has protocol error \n", cp
);
1328 case CMD_HARDWARE_ERR
:
1329 /* cmd->result = DID_ERROR << 16; */
1330 dev_warn(d
, "cp %p had hardware error\n", cp
);
1332 case CMD_CONNECTION_LOST
:
1333 dev_warn(d
, "cp %p had connection lost\n", cp
);
1336 dev_warn(d
, "cp %p was aborted\n", cp
);
1338 case CMD_ABORT_FAILED
:
1339 dev_warn(d
, "cp %p reports abort failed\n", cp
);
1341 case CMD_UNSOLICITED_ABORT
:
1342 dev_warn(d
, "cp %p aborted due to an unsolicited abort\n", cp
);
1345 dev_warn(d
, "cp %p timed out\n", cp
);
1348 dev_warn(d
, "cp %p returned unknown status %x\n", cp
,
1353 static int hpsa_scsi_do_inquiry(struct ctlr_info
*h
, unsigned char *scsi3addr
,
1354 unsigned char page
, unsigned char *buf
,
1355 unsigned char bufsize
)
1358 struct CommandList
*c
;
1359 struct ErrorInfo
*ei
;
1361 c
= cmd_special_alloc(h
);
1363 if (c
== NULL
) { /* trouble... */
1364 dev_warn(&h
->pdev
->dev
, "cmd_special_alloc returned NULL!\n");
1368 fill_cmd(c
, HPSA_INQUIRY
, h
, buf
, bufsize
, page
, scsi3addr
, TYPE_CMD
);
1369 hpsa_scsi_do_simple_cmd_with_retry(h
, c
, PCI_DMA_FROMDEVICE
);
1371 if (ei
->CommandStatus
!= 0 && ei
->CommandStatus
!= CMD_DATA_UNDERRUN
) {
1372 hpsa_scsi_interpret_error(c
);
1375 cmd_special_free(h
, c
);
1379 static int hpsa_send_reset(struct ctlr_info
*h
, unsigned char *scsi3addr
)
1382 struct CommandList
*c
;
1383 struct ErrorInfo
*ei
;
1385 c
= cmd_special_alloc(h
);
1387 if (c
== NULL
) { /* trouble... */
1388 dev_warn(&h
->pdev
->dev
, "cmd_special_alloc returned NULL!\n");
1392 fill_cmd(c
, HPSA_DEVICE_RESET_MSG
, h
, NULL
, 0, 0, scsi3addr
, TYPE_MSG
);
1393 hpsa_scsi_do_simple_cmd_core(h
, c
);
1394 /* no unmap needed here because no data xfer. */
1397 if (ei
->CommandStatus
!= 0) {
1398 hpsa_scsi_interpret_error(c
);
1401 cmd_special_free(h
, c
);
1405 static void hpsa_get_raid_level(struct ctlr_info
*h
,
1406 unsigned char *scsi3addr
, unsigned char *raid_level
)
1411 *raid_level
= RAID_UNKNOWN
;
1412 buf
= kzalloc(64, GFP_KERNEL
);
1415 rc
= hpsa_scsi_do_inquiry(h
, scsi3addr
, 0xC1, buf
, 64);
1417 *raid_level
= buf
[8];
1418 if (*raid_level
> RAID_UNKNOWN
)
1419 *raid_level
= RAID_UNKNOWN
;
1424 /* Get the device id from inquiry page 0x83 */
1425 static int hpsa_get_device_id(struct ctlr_info
*h
, unsigned char *scsi3addr
,
1426 unsigned char *device_id
, int buflen
)
1433 buf
= kzalloc(64, GFP_KERNEL
);
1436 rc
= hpsa_scsi_do_inquiry(h
, scsi3addr
, 0x83, buf
, 64);
1438 memcpy(device_id
, &buf
[8], buflen
);
1443 static int hpsa_scsi_do_report_luns(struct ctlr_info
*h
, int logical
,
1444 struct ReportLUNdata
*buf
, int bufsize
,
1445 int extended_response
)
1448 struct CommandList
*c
;
1449 unsigned char scsi3addr
[8];
1450 struct ErrorInfo
*ei
;
1452 c
= cmd_special_alloc(h
);
1453 if (c
== NULL
) { /* trouble... */
1454 dev_err(&h
->pdev
->dev
, "cmd_special_alloc returned NULL!\n");
1457 /* address the controller */
1458 memset(scsi3addr
, 0, sizeof(scsi3addr
));
1459 fill_cmd(c
, logical
? HPSA_REPORT_LOG
: HPSA_REPORT_PHYS
, h
,
1460 buf
, bufsize
, 0, scsi3addr
, TYPE_CMD
);
1461 if (extended_response
)
1462 c
->Request
.CDB
[1] = extended_response
;
1463 hpsa_scsi_do_simple_cmd_with_retry(h
, c
, PCI_DMA_FROMDEVICE
);
1465 if (ei
->CommandStatus
!= 0 &&
1466 ei
->CommandStatus
!= CMD_DATA_UNDERRUN
) {
1467 hpsa_scsi_interpret_error(c
);
1470 cmd_special_free(h
, c
);
1474 static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info
*h
,
1475 struct ReportLUNdata
*buf
,
1476 int bufsize
, int extended_response
)
1478 return hpsa_scsi_do_report_luns(h
, 0, buf
, bufsize
, extended_response
);
1481 static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info
*h
,
1482 struct ReportLUNdata
*buf
, int bufsize
)
1484 return hpsa_scsi_do_report_luns(h
, 1, buf
, bufsize
, 0);
1487 static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t
*device
,
1488 int bus
, int target
, int lun
)
1491 device
->target
= target
;
1495 static int hpsa_update_device_info(struct ctlr_info
*h
,
1496 unsigned char scsi3addr
[], struct hpsa_scsi_dev_t
*this_device
)
1498 #define OBDR_TAPE_INQ_SIZE 49
1499 unsigned char *inq_buff
;
1501 inq_buff
= kzalloc(OBDR_TAPE_INQ_SIZE
, GFP_KERNEL
);
1505 /* Do an inquiry to the device to see what it is. */
1506 if (hpsa_scsi_do_inquiry(h
, scsi3addr
, 0, inq_buff
,
1507 (unsigned char) OBDR_TAPE_INQ_SIZE
) != 0) {
1508 /* Inquiry failed (msg printed already) */
1509 dev_err(&h
->pdev
->dev
,
1510 "hpsa_update_device_info: inquiry failed\n");
1514 /* As a side effect, record the firmware version number
1515 * if we happen to be talking to the RAID controller.
1517 if (is_hba_lunid(scsi3addr
))
1518 memcpy(h
->firm_ver
, &inq_buff
[32], 4);
1520 this_device
->devtype
= (inq_buff
[0] & 0x1f);
1521 memcpy(this_device
->scsi3addr
, scsi3addr
, 8);
1522 memcpy(this_device
->vendor
, &inq_buff
[8],
1523 sizeof(this_device
->vendor
));
1524 memcpy(this_device
->model
, &inq_buff
[16],
1525 sizeof(this_device
->model
));
1526 memcpy(this_device
->revision
, &inq_buff
[32],
1527 sizeof(this_device
->revision
));
1528 memset(this_device
->device_id
, 0,
1529 sizeof(this_device
->device_id
));
1530 hpsa_get_device_id(h
, scsi3addr
, this_device
->device_id
,
1531 sizeof(this_device
->device_id
));
1533 if (this_device
->devtype
== TYPE_DISK
&&
1534 is_logical_dev_addr_mode(scsi3addr
))
1535 hpsa_get_raid_level(h
, scsi3addr
, &this_device
->raid_level
);
1537 this_device
->raid_level
= RAID_UNKNOWN
;
1547 static unsigned char *msa2xxx_model
[] = {
1555 static int is_msa2xxx(struct ctlr_info
*h
, struct hpsa_scsi_dev_t
*device
)
1559 for (i
= 0; msa2xxx_model
[i
]; i
++)
1560 if (strncmp(device
->model
, msa2xxx_model
[i
],
1561 strlen(msa2xxx_model
[i
])) == 0)
1566 /* Helper function to assign bus, target, lun mapping of devices.
1567 * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
1568 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
1569 * Logical drive target and lun are assigned at this time, but
1570 * physical device lun and target assignment are deferred (assigned
1571 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
1573 static void figure_bus_target_lun(struct ctlr_info
*h
,
1574 u8
*lunaddrbytes
, int *bus
, int *target
, int *lun
,
1575 struct hpsa_scsi_dev_t
*device
)
1579 if (is_logical_dev_addr_mode(lunaddrbytes
)) {
1580 /* logical device */
1581 if (unlikely(is_scsi_rev_5(h
))) {
1582 /* p1210m, logical drives lun assignments
1583 * match SCSI REPORT LUNS data.
1585 lunid
= le32_to_cpu(*((__le32
*) lunaddrbytes
));
1588 *lun
= (lunid
& 0x3fff) + 1;
1591 lunid
= le32_to_cpu(*((__le32
*) lunaddrbytes
));
1592 if (is_msa2xxx(h
, device
)) {
1593 /* msa2xxx way, put logicals on bus 1
1594 * and match target/lun numbers box
1598 *target
= (lunid
>> 16) & 0x3fff;
1599 *lun
= lunid
& 0x00ff;
1601 /* Traditional smart array way. */
1604 *target
= lunid
& 0x3fff;
1608 /* physical device */
1609 if (is_hba_lunid(lunaddrbytes
))
1610 if (unlikely(is_scsi_rev_5(h
))) {
1611 *bus
= 0; /* put p1210m ctlr at 0,0,0 */
1616 *bus
= 3; /* traditional smartarray */
1618 *bus
= 2; /* physical disk */
1620 *lun
= -1; /* we will fill these in later. */
1625 * If there is no lun 0 on a target, linux won't find any devices.
1626 * For the MSA2xxx boxes, we have to manually detect the enclosure
1627 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
1628 * it for some reason. *tmpdevice is the target we're adding,
1629 * this_device is a pointer into the current element of currentsd[]
1630 * that we're building up in update_scsi_devices(), below.
1631 * lunzerobits is a bitmap that tracks which targets already have a
1633 * Returns 1 if an enclosure was added, 0 if not.
1635 static int add_msa2xxx_enclosure_device(struct ctlr_info
*h
,
1636 struct hpsa_scsi_dev_t
*tmpdevice
,
1637 struct hpsa_scsi_dev_t
*this_device
, u8
*lunaddrbytes
,
1638 int bus
, int target
, int lun
, unsigned long lunzerobits
[],
1639 int *nmsa2xxx_enclosures
)
1641 unsigned char scsi3addr
[8];
1643 if (test_bit(target
, lunzerobits
))
1644 return 0; /* There is already a lun 0 on this target. */
1646 if (!is_logical_dev_addr_mode(lunaddrbytes
))
1647 return 0; /* It's the logical targets that may lack lun 0. */
1649 if (!is_msa2xxx(h
, tmpdevice
))
1650 return 0; /* It's only the MSA2xxx that have this problem. */
1652 if (lun
== 0) /* if lun is 0, then obviously we have a lun 0. */
1655 if (is_hba_lunid(scsi3addr
))
1656 return 0; /* Don't add the RAID controller here. */
1658 if (is_scsi_rev_5(h
))
1659 return 0; /* p1210m doesn't need to do this. */
1661 #define MAX_MSA2XXX_ENCLOSURES 32
1662 if (*nmsa2xxx_enclosures
>= MAX_MSA2XXX_ENCLOSURES
) {
1663 dev_warn(&h
->pdev
->dev
, "Maximum number of MSA2XXX "
1664 "enclosures exceeded. Check your hardware "
1669 memset(scsi3addr
, 0, 8);
1670 scsi3addr
[3] = target
;
1671 if (hpsa_update_device_info(h
, scsi3addr
, this_device
))
1673 (*nmsa2xxx_enclosures
)++;
1674 hpsa_set_bus_target_lun(this_device
, bus
, target
, 0);
1675 set_bit(target
, lunzerobits
);
1680 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
1681 * logdev. The number of luns in physdev and logdev are returned in
1682 * *nphysicals and *nlogicals, respectively.
1683 * Returns 0 on success, -1 otherwise.
1685 static int hpsa_gather_lun_info(struct ctlr_info
*h
,
1687 struct ReportLUNdata
*physdev
, u32
*nphysicals
,
1688 struct ReportLUNdata
*logdev
, u32
*nlogicals
)
1690 if (hpsa_scsi_do_report_phys_luns(h
, physdev
, reportlunsize
, 0)) {
1691 dev_err(&h
->pdev
->dev
, "report physical LUNs failed.\n");
1694 *nphysicals
= be32_to_cpu(*((__be32
*)physdev
->LUNListLength
)) / 8;
1695 if (*nphysicals
> HPSA_MAX_PHYS_LUN
) {
1696 dev_warn(&h
->pdev
->dev
, "maximum physical LUNs (%d) exceeded."
1697 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN
,
1698 *nphysicals
- HPSA_MAX_PHYS_LUN
);
1699 *nphysicals
= HPSA_MAX_PHYS_LUN
;
1701 if (hpsa_scsi_do_report_log_luns(h
, logdev
, reportlunsize
)) {
1702 dev_err(&h
->pdev
->dev
, "report logical LUNs failed.\n");
1705 *nlogicals
= be32_to_cpu(*((__be32
*) logdev
->LUNListLength
)) / 8;
1706 /* Reject Logicals in excess of our max capability. */
1707 if (*nlogicals
> HPSA_MAX_LUN
) {
1708 dev_warn(&h
->pdev
->dev
,
1709 "maximum logical LUNs (%d) exceeded. "
1710 "%d LUNs ignored.\n", HPSA_MAX_LUN
,
1711 *nlogicals
- HPSA_MAX_LUN
);
1712 *nlogicals
= HPSA_MAX_LUN
;
1714 if (*nlogicals
+ *nphysicals
> HPSA_MAX_PHYS_LUN
) {
1715 dev_warn(&h
->pdev
->dev
,
1716 "maximum logical + physical LUNs (%d) exceeded. "
1717 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN
,
1718 *nphysicals
+ *nlogicals
- HPSA_MAX_PHYS_LUN
);
1719 *nlogicals
= HPSA_MAX_PHYS_LUN
- *nphysicals
;
1724 u8
*figure_lunaddrbytes(struct ctlr_info
*h
, int raid_ctlr_position
, int i
,
1725 int nphysicals
, int nlogicals
, struct ReportLUNdata
*physdev_list
,
1726 struct ReportLUNdata
*logdev_list
)
1728 /* Helper function, figure out where the LUN ID info is coming from
1729 * given index i, lists of physical and logical devices, where in
1730 * the list the raid controller is supposed to appear (first or last)
1733 int logicals_start
= nphysicals
+ (raid_ctlr_position
== 0);
1734 int last_device
= nphysicals
+ nlogicals
+ (raid_ctlr_position
== 0);
1736 if (i
== raid_ctlr_position
)
1737 return RAID_CTLR_LUNID
;
1739 if (i
< logicals_start
)
1740 return &physdev_list
->LUN
[i
- (raid_ctlr_position
== 0)][0];
1742 if (i
< last_device
)
1743 return &logdev_list
->LUN
[i
- nphysicals
-
1744 (raid_ctlr_position
== 0)][0];
1749 static void hpsa_update_scsi_devices(struct ctlr_info
*h
, int hostno
)
1751 /* the idea here is we could get notified
1752 * that some devices have changed, so we do a report
1753 * physical luns and report logical luns cmd, and adjust
1754 * our list of devices accordingly.
1756 * The scsi3addr's of devices won't change so long as the
1757 * adapter is not reset. That means we can rescan and
1758 * tell which devices we already know about, vs. new
1759 * devices, vs. disappearing devices.
1761 struct ReportLUNdata
*physdev_list
= NULL
;
1762 struct ReportLUNdata
*logdev_list
= NULL
;
1763 unsigned char *inq_buff
= NULL
;
1766 u32 ndev_allocated
= 0;
1767 struct hpsa_scsi_dev_t
**currentsd
, *this_device
, *tmpdevice
;
1769 int reportlunsize
= sizeof(*physdev_list
) + HPSA_MAX_PHYS_LUN
* 8;
1770 int i
, nmsa2xxx_enclosures
, ndevs_to_allocate
;
1771 int bus
, target
, lun
;
1772 int raid_ctlr_position
;
1773 DECLARE_BITMAP(lunzerobits
, HPSA_MAX_TARGETS_PER_CTLR
);
1775 currentsd
= kzalloc(sizeof(*currentsd
) * HPSA_MAX_SCSI_DEVS_PER_HBA
,
1777 physdev_list
= kzalloc(reportlunsize
, GFP_KERNEL
);
1778 logdev_list
= kzalloc(reportlunsize
, GFP_KERNEL
);
1779 inq_buff
= kmalloc(OBDR_TAPE_INQ_SIZE
, GFP_KERNEL
);
1780 tmpdevice
= kzalloc(sizeof(*tmpdevice
), GFP_KERNEL
);
1782 if (!currentsd
|| !physdev_list
|| !logdev_list
||
1783 !inq_buff
|| !tmpdevice
) {
1784 dev_err(&h
->pdev
->dev
, "out of memory\n");
1787 memset(lunzerobits
, 0, sizeof(lunzerobits
));
1789 if (hpsa_gather_lun_info(h
, reportlunsize
, physdev_list
, &nphysicals
,
1790 logdev_list
, &nlogicals
))
1793 /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
1794 * but each of them 4 times through different paths. The plus 1
1795 * is for the RAID controller.
1797 ndevs_to_allocate
= nphysicals
+ nlogicals
+ MAX_MSA2XXX_ENCLOSURES
+ 1;
1799 /* Allocate the per device structures */
1800 for (i
= 0; i
< ndevs_to_allocate
; i
++) {
1801 currentsd
[i
] = kzalloc(sizeof(*currentsd
[i
]), GFP_KERNEL
);
1802 if (!currentsd
[i
]) {
1803 dev_warn(&h
->pdev
->dev
, "out of memory at %s:%d\n",
1804 __FILE__
, __LINE__
);
1810 if (unlikely(is_scsi_rev_5(h
)))
1811 raid_ctlr_position
= 0;
1813 raid_ctlr_position
= nphysicals
+ nlogicals
;
1815 /* adjust our table of devices */
1816 nmsa2xxx_enclosures
= 0;
1817 for (i
= 0; i
< nphysicals
+ nlogicals
+ 1; i
++) {
1820 /* Figure out where the LUN ID info is coming from */
1821 lunaddrbytes
= figure_lunaddrbytes(h
, raid_ctlr_position
,
1822 i
, nphysicals
, nlogicals
, physdev_list
, logdev_list
);
1823 /* skip masked physical devices. */
1824 if (lunaddrbytes
[3] & 0xC0 &&
1825 i
< nphysicals
+ (raid_ctlr_position
== 0))
1828 /* Get device type, vendor, model, device id */
1829 if (hpsa_update_device_info(h
, lunaddrbytes
, tmpdevice
))
1830 continue; /* skip it if we can't talk to it. */
1831 figure_bus_target_lun(h
, lunaddrbytes
, &bus
, &target
, &lun
,
1833 this_device
= currentsd
[ncurrent
];
1836 * For the msa2xxx boxes, we have to insert a LUN 0 which
1837 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
1838 * is nonetheless an enclosure device there. We have to
1839 * present that otherwise linux won't find anything if
1840 * there is no lun 0.
1842 if (add_msa2xxx_enclosure_device(h
, tmpdevice
, this_device
,
1843 lunaddrbytes
, bus
, target
, lun
, lunzerobits
,
1844 &nmsa2xxx_enclosures
)) {
1846 this_device
= currentsd
[ncurrent
];
1849 *this_device
= *tmpdevice
;
1850 hpsa_set_bus_target_lun(this_device
, bus
, target
, lun
);
1852 switch (this_device
->devtype
) {
1854 /* We don't *really* support actual CD-ROM devices,
1855 * just "One Button Disaster Recovery" tape drive
1856 * which temporarily pretends to be a CD-ROM drive.
1857 * So we check that the device is really an OBDR tape
1858 * device by checking for "$DR-10" in bytes 43-48 of
1862 #define OBDR_TAPE_SIG "$DR-10"
1863 strncpy(obdr_sig
, &inq_buff
[43], 6);
1865 if (strncmp(obdr_sig
, OBDR_TAPE_SIG
, 6) != 0)
1866 /* Not OBDR device, ignore it. */
1877 case TYPE_MEDIUM_CHANGER
:
1881 /* Only present the Smartarray HBA as a RAID controller.
1882 * If it's a RAID controller other than the HBA itself
1883 * (an external RAID controller, MSA500 or similar)
1886 if (!is_hba_lunid(lunaddrbytes
))
1893 if (ncurrent
>= HPSA_MAX_SCSI_DEVS_PER_HBA
)
1896 adjust_hpsa_scsi_table(h
, hostno
, currentsd
, ncurrent
);
1899 for (i
= 0; i
< ndev_allocated
; i
++)
1900 kfree(currentsd
[i
]);
1903 kfree(physdev_list
);
1907 /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1908 * dma mapping and fills in the scatter gather entries of the
1911 static int hpsa_scatter_gather(struct pci_dev
*pdev
,
1912 struct CommandList
*cp
,
1913 struct scsi_cmnd
*cmd
)
1916 struct scatterlist
*sg
;
1920 BUG_ON(scsi_sg_count(cmd
) > MAXSGENTRIES
);
1922 use_sg
= scsi_dma_map(cmd
);
1927 goto sglist_finished
;
1929 scsi_for_each_sg(cmd
, sg
, use_sg
, i
) {
1930 addr64
= (u64
) sg_dma_address(sg
);
1931 len
= sg_dma_len(sg
);
1932 cp
->SG
[i
].Addr
.lower
=
1933 (u32
) (addr64
& (u64
) 0x00000000FFFFFFFF);
1934 cp
->SG
[i
].Addr
.upper
=
1935 (u32
) ((addr64
>> 32) & (u64
) 0x00000000FFFFFFFF);
1936 cp
->SG
[i
].Len
= len
;
1937 cp
->SG
[i
].Ext
= 0; /* we are not chaining */
1942 cp
->Header
.SGList
= (u8
) use_sg
; /* no. SGs contig in this cmd */
1943 cp
->Header
.SGTotal
= (u16
) use_sg
; /* total sgs in this cmd list */
1948 static int hpsa_scsi_queue_command(struct scsi_cmnd
*cmd
,
1949 void (*done
)(struct scsi_cmnd
*))
1951 struct ctlr_info
*h
;
1952 struct hpsa_scsi_dev_t
*dev
;
1953 unsigned char scsi3addr
[8];
1954 struct CommandList
*c
;
1955 unsigned long flags
;
1957 /* Get the ptr to our adapter structure out of cmd->host. */
1958 h
= sdev_to_hba(cmd
->device
);
1959 dev
= cmd
->device
->hostdata
;
1961 cmd
->result
= DID_NO_CONNECT
<< 16;
1965 memcpy(scsi3addr
, dev
->scsi3addr
, sizeof(scsi3addr
));
1967 /* Need a lock as this is being allocated from the pool */
1968 spin_lock_irqsave(&h
->lock
, flags
);
1970 spin_unlock_irqrestore(&h
->lock
, flags
);
1971 if (c
== NULL
) { /* trouble... */
1972 dev_err(&h
->pdev
->dev
, "cmd_alloc returned NULL!\n");
1973 return SCSI_MLQUEUE_HOST_BUSY
;
1976 /* Fill in the command list header */
1978 cmd
->scsi_done
= done
; /* save this for use by completion code */
1980 /* save c in case we have to abort it */
1981 cmd
->host_scribble
= (unsigned char *) c
;
1983 c
->cmd_type
= CMD_SCSI
;
1985 c
->Header
.ReplyQueue
= 0; /* unused in simple mode */
1986 memcpy(&c
->Header
.LUN
.LunAddrBytes
[0], &scsi3addr
[0], 8);
1987 c
->Header
.Tag
.lower
= (c
->cmdindex
<< DIRECT_LOOKUP_SHIFT
);
1988 c
->Header
.Tag
.lower
|= DIRECT_LOOKUP_BIT
;
1990 /* Fill in the request block... */
1992 c
->Request
.Timeout
= 0;
1993 memset(c
->Request
.CDB
, 0, sizeof(c
->Request
.CDB
));
1994 BUG_ON(cmd
->cmd_len
> sizeof(c
->Request
.CDB
));
1995 c
->Request
.CDBLen
= cmd
->cmd_len
;
1996 memcpy(c
->Request
.CDB
, cmd
->cmnd
, cmd
->cmd_len
);
1997 c
->Request
.Type
.Type
= TYPE_CMD
;
1998 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
1999 switch (cmd
->sc_data_direction
) {
2001 c
->Request
.Type
.Direction
= XFER_WRITE
;
2003 case DMA_FROM_DEVICE
:
2004 c
->Request
.Type
.Direction
= XFER_READ
;
2007 c
->Request
.Type
.Direction
= XFER_NONE
;
2009 case DMA_BIDIRECTIONAL
:
2010 /* This can happen if a buggy application does a scsi passthru
2011 * and sets both inlen and outlen to non-zero. ( see
2012 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
2015 c
->Request
.Type
.Direction
= XFER_RSVD
;
2016 /* This is technically wrong, and hpsa controllers should
2017 * reject it with CMD_INVALID, which is the most correct
2018 * response, but non-fibre backends appear to let it
2019 * slide by, and give the same results as if this field
2020 * were set correctly. Either way is acceptable for
2021 * our purposes here.
2027 dev_err(&h
->pdev
->dev
, "unknown data direction: %d\n",
2028 cmd
->sc_data_direction
);
2033 if (hpsa_scatter_gather(h
->pdev
, c
, cmd
) < 0) { /* Fill SG list */
2035 return SCSI_MLQUEUE_HOST_BUSY
;
2037 enqueue_cmd_and_start_io(h
, c
);
2038 /* the cmd'll come back via intr handler in complete_scsi_command() */
2042 static void hpsa_scan_start(struct Scsi_Host
*sh
)
2044 struct ctlr_info
*h
= shost_to_hba(sh
);
2045 unsigned long flags
;
2047 /* wait until any scan already in progress is finished. */
2049 spin_lock_irqsave(&h
->scan_lock
, flags
);
2050 if (h
->scan_finished
)
2052 spin_unlock_irqrestore(&h
->scan_lock
, flags
);
2053 wait_event(h
->scan_wait_queue
, h
->scan_finished
);
2054 /* Note: We don't need to worry about a race between this
2055 * thread and driver unload because the midlayer will
2056 * have incremented the reference count, so unload won't
2057 * happen if we're in here.
2060 h
->scan_finished
= 0; /* mark scan as in progress */
2061 spin_unlock_irqrestore(&h
->scan_lock
, flags
);
2063 hpsa_update_scsi_devices(h
, h
->scsi_host
->host_no
);
2065 spin_lock_irqsave(&h
->scan_lock
, flags
);
2066 h
->scan_finished
= 1; /* mark scan as finished. */
2067 wake_up_all(&h
->scan_wait_queue
);
2068 spin_unlock_irqrestore(&h
->scan_lock
, flags
);
2071 static int hpsa_scan_finished(struct Scsi_Host
*sh
,
2072 unsigned long elapsed_time
)
2074 struct ctlr_info
*h
= shost_to_hba(sh
);
2075 unsigned long flags
;
2078 spin_lock_irqsave(&h
->scan_lock
, flags
);
2079 finished
= h
->scan_finished
;
2080 spin_unlock_irqrestore(&h
->scan_lock
, flags
);
2084 static int hpsa_change_queue_depth(struct scsi_device
*sdev
,
2085 int qdepth
, int reason
)
2087 struct ctlr_info
*h
= sdev_to_hba(sdev
);
2089 if (reason
!= SCSI_QDEPTH_DEFAULT
)
2095 if (qdepth
> h
->nr_cmds
)
2096 qdepth
= h
->nr_cmds
;
2097 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), qdepth
);
2098 return sdev
->queue_depth
;
2101 static void hpsa_unregister_scsi(struct ctlr_info
*h
)
2103 /* we are being forcibly unloaded, and may not refuse. */
2104 scsi_remove_host(h
->scsi_host
);
2105 scsi_host_put(h
->scsi_host
);
2106 h
->scsi_host
= NULL
;
2109 static int hpsa_register_scsi(struct ctlr_info
*h
)
2113 rc
= hpsa_scsi_detect(h
);
2115 dev_err(&h
->pdev
->dev
, "hpsa_register_scsi: failed"
2116 " hpsa_scsi_detect(), rc is %d\n", rc
);
2120 static int wait_for_device_to_become_ready(struct ctlr_info
*h
,
2121 unsigned char lunaddr
[])
2125 int waittime
= 1; /* seconds */
2126 struct CommandList
*c
;
2128 c
= cmd_special_alloc(h
);
2130 dev_warn(&h
->pdev
->dev
, "out of memory in "
2131 "wait_for_device_to_become_ready.\n");
2135 /* Send test unit ready until device ready, or give up. */
2136 while (count
< HPSA_TUR_RETRY_LIMIT
) {
2138 /* Wait for a bit. do this first, because if we send
2139 * the TUR right away, the reset will just abort it.
2141 msleep(1000 * waittime
);
2144 /* Increase wait time with each try, up to a point. */
2145 if (waittime
< HPSA_MAX_WAIT_INTERVAL_SECS
)
2146 waittime
= waittime
* 2;
2148 /* Send the Test Unit Ready */
2149 fill_cmd(c
, TEST_UNIT_READY
, h
, NULL
, 0, 0, lunaddr
, TYPE_CMD
);
2150 hpsa_scsi_do_simple_cmd_core(h
, c
);
2151 /* no unmap needed here because no data xfer. */
2153 if (c
->err_info
->CommandStatus
== CMD_SUCCESS
)
2156 if (c
->err_info
->CommandStatus
== CMD_TARGET_STATUS
&&
2157 c
->err_info
->ScsiStatus
== SAM_STAT_CHECK_CONDITION
&&
2158 (c
->err_info
->SenseInfo
[2] == NO_SENSE
||
2159 c
->err_info
->SenseInfo
[2] == UNIT_ATTENTION
))
2162 dev_warn(&h
->pdev
->dev
, "waiting %d secs "
2163 "for device to become ready.\n", waittime
);
2164 rc
= 1; /* device not ready. */
2168 dev_warn(&h
->pdev
->dev
, "giving up on device.\n");
2170 dev_warn(&h
->pdev
->dev
, "device is ready.\n");
2172 cmd_special_free(h
, c
);
2176 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
2177 * complaining. Doing a host- or bus-reset can't do anything good here.
2179 static int hpsa_eh_device_reset_handler(struct scsi_cmnd
*scsicmd
)
2182 struct ctlr_info
*h
;
2183 struct hpsa_scsi_dev_t
*dev
;
2185 /* find the controller to which the command to be aborted was sent */
2186 h
= sdev_to_hba(scsicmd
->device
);
2187 if (h
== NULL
) /* paranoia */
2189 dev
= scsicmd
->device
->hostdata
;
2191 dev_err(&h
->pdev
->dev
, "hpsa_eh_device_reset_handler: "
2192 "device lookup failed.\n");
2195 dev_warn(&h
->pdev
->dev
, "resetting device %d:%d:%d:%d\n",
2196 h
->scsi_host
->host_no
, dev
->bus
, dev
->target
, dev
->lun
);
2197 /* send a reset to the SCSI LUN which the command was sent to */
2198 rc
= hpsa_send_reset(h
, dev
->scsi3addr
);
2199 if (rc
== 0 && wait_for_device_to_become_ready(h
, dev
->scsi3addr
) == 0)
2202 dev_warn(&h
->pdev
->dev
, "resetting device failed.\n");
2207 * For operations that cannot sleep, a command block is allocated at init,
2208 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
2209 * which ones are free or in use. Lock must be held when calling this.
2210 * cmd_free() is the complement.
2212 static struct CommandList
*cmd_alloc(struct ctlr_info
*h
)
2214 struct CommandList
*c
;
2216 union u64bit temp64
;
2217 dma_addr_t cmd_dma_handle
, err_dma_handle
;
2220 i
= find_first_zero_bit(h
->cmd_pool_bits
, h
->nr_cmds
);
2221 if (i
== h
->nr_cmds
)
2223 } while (test_and_set_bit
2224 (i
& (BITS_PER_LONG
- 1),
2225 h
->cmd_pool_bits
+ (i
/ BITS_PER_LONG
)) != 0);
2226 c
= h
->cmd_pool
+ i
;
2227 memset(c
, 0, sizeof(*c
));
2228 cmd_dma_handle
= h
->cmd_pool_dhandle
2230 c
->err_info
= h
->errinfo_pool
+ i
;
2231 memset(c
->err_info
, 0, sizeof(*c
->err_info
));
2232 err_dma_handle
= h
->errinfo_pool_dhandle
2233 + i
* sizeof(*c
->err_info
);
2238 INIT_HLIST_NODE(&c
->list
);
2239 c
->busaddr
= (u32
) cmd_dma_handle
;
2240 temp64
.val
= (u64
) err_dma_handle
;
2241 c
->ErrDesc
.Addr
.lower
= temp64
.val32
.lower
;
2242 c
->ErrDesc
.Addr
.upper
= temp64
.val32
.upper
;
2243 c
->ErrDesc
.Len
= sizeof(*c
->err_info
);
2249 /* For operations that can wait for kmalloc to possibly sleep,
2250 * this routine can be called. Lock need not be held to call
2251 * cmd_special_alloc. cmd_special_free() is the complement.
2253 static struct CommandList
*cmd_special_alloc(struct ctlr_info
*h
)
2255 struct CommandList
*c
;
2256 union u64bit temp64
;
2257 dma_addr_t cmd_dma_handle
, err_dma_handle
;
2259 c
= pci_alloc_consistent(h
->pdev
, sizeof(*c
), &cmd_dma_handle
);
2262 memset(c
, 0, sizeof(*c
));
2266 c
->err_info
= pci_alloc_consistent(h
->pdev
, sizeof(*c
->err_info
),
2269 if (c
->err_info
== NULL
) {
2270 pci_free_consistent(h
->pdev
,
2271 sizeof(*c
), c
, cmd_dma_handle
);
2274 memset(c
->err_info
, 0, sizeof(*c
->err_info
));
2276 INIT_HLIST_NODE(&c
->list
);
2277 c
->busaddr
= (u32
) cmd_dma_handle
;
2278 temp64
.val
= (u64
) err_dma_handle
;
2279 c
->ErrDesc
.Addr
.lower
= temp64
.val32
.lower
;
2280 c
->ErrDesc
.Addr
.upper
= temp64
.val32
.upper
;
2281 c
->ErrDesc
.Len
= sizeof(*c
->err_info
);
2287 static void cmd_free(struct ctlr_info
*h
, struct CommandList
*c
)
2291 i
= c
- h
->cmd_pool
;
2292 clear_bit(i
& (BITS_PER_LONG
- 1),
2293 h
->cmd_pool_bits
+ (i
/ BITS_PER_LONG
));
2297 static void cmd_special_free(struct ctlr_info
*h
, struct CommandList
*c
)
2299 union u64bit temp64
;
2301 temp64
.val32
.lower
= c
->ErrDesc
.Addr
.lower
;
2302 temp64
.val32
.upper
= c
->ErrDesc
.Addr
.upper
;
2303 pci_free_consistent(h
->pdev
, sizeof(*c
->err_info
),
2304 c
->err_info
, (dma_addr_t
) temp64
.val
);
2305 pci_free_consistent(h
->pdev
, sizeof(*c
),
2306 c
, (dma_addr_t
) c
->busaddr
);
2309 #ifdef CONFIG_COMPAT
2311 static int hpsa_ioctl32_passthru(struct scsi_device
*dev
, int cmd
, void *arg
)
2313 IOCTL32_Command_struct __user
*arg32
=
2314 (IOCTL32_Command_struct __user
*) arg
;
2315 IOCTL_Command_struct arg64
;
2316 IOCTL_Command_struct __user
*p
= compat_alloc_user_space(sizeof(arg64
));
2321 err
|= copy_from_user(&arg64
.LUN_info
, &arg32
->LUN_info
,
2322 sizeof(arg64
.LUN_info
));
2323 err
|= copy_from_user(&arg64
.Request
, &arg32
->Request
,
2324 sizeof(arg64
.Request
));
2325 err
|= copy_from_user(&arg64
.error_info
, &arg32
->error_info
,
2326 sizeof(arg64
.error_info
));
2327 err
|= get_user(arg64
.buf_size
, &arg32
->buf_size
);
2328 err
|= get_user(cp
, &arg32
->buf
);
2329 arg64
.buf
= compat_ptr(cp
);
2330 err
|= copy_to_user(p
, &arg64
, sizeof(arg64
));
2335 err
= hpsa_ioctl(dev
, CCISS_PASSTHRU
, (void *)p
);
2338 err
|= copy_in_user(&arg32
->error_info
, &p
->error_info
,
2339 sizeof(arg32
->error_info
));
2345 static int hpsa_ioctl32_big_passthru(struct scsi_device
*dev
,
2348 BIG_IOCTL32_Command_struct __user
*arg32
=
2349 (BIG_IOCTL32_Command_struct __user
*) arg
;
2350 BIG_IOCTL_Command_struct arg64
;
2351 BIG_IOCTL_Command_struct __user
*p
=
2352 compat_alloc_user_space(sizeof(arg64
));
2357 err
|= copy_from_user(&arg64
.LUN_info
, &arg32
->LUN_info
,
2358 sizeof(arg64
.LUN_info
));
2359 err
|= copy_from_user(&arg64
.Request
, &arg32
->Request
,
2360 sizeof(arg64
.Request
));
2361 err
|= copy_from_user(&arg64
.error_info
, &arg32
->error_info
,
2362 sizeof(arg64
.error_info
));
2363 err
|= get_user(arg64
.buf_size
, &arg32
->buf_size
);
2364 err
|= get_user(arg64
.malloc_size
, &arg32
->malloc_size
);
2365 err
|= get_user(cp
, &arg32
->buf
);
2366 arg64
.buf
= compat_ptr(cp
);
2367 err
|= copy_to_user(p
, &arg64
, sizeof(arg64
));
2372 err
= hpsa_ioctl(dev
, CCISS_BIG_PASSTHRU
, (void *)p
);
2375 err
|= copy_in_user(&arg32
->error_info
, &p
->error_info
,
2376 sizeof(arg32
->error_info
));
2382 static int hpsa_compat_ioctl(struct scsi_device
*dev
, int cmd
, void *arg
)
2385 case CCISS_GETPCIINFO
:
2386 case CCISS_GETINTINFO
:
2387 case CCISS_SETINTINFO
:
2388 case CCISS_GETNODENAME
:
2389 case CCISS_SETNODENAME
:
2390 case CCISS_GETHEARTBEAT
:
2391 case CCISS_GETBUSTYPES
:
2392 case CCISS_GETFIRMVER
:
2393 case CCISS_GETDRIVVER
:
2394 case CCISS_REVALIDVOLS
:
2395 case CCISS_DEREGDISK
:
2396 case CCISS_REGNEWDISK
:
2398 case CCISS_RESCANDISK
:
2399 case CCISS_GETLUNINFO
:
2400 return hpsa_ioctl(dev
, cmd
, arg
);
2402 case CCISS_PASSTHRU32
:
2403 return hpsa_ioctl32_passthru(dev
, cmd
, arg
);
2404 case CCISS_BIG_PASSTHRU32
:
2405 return hpsa_ioctl32_big_passthru(dev
, cmd
, arg
);
2408 return -ENOIOCTLCMD
;
2413 static int hpsa_getpciinfo_ioctl(struct ctlr_info
*h
, void __user
*argp
)
2415 struct hpsa_pci_info pciinfo
;
2419 pciinfo
.domain
= pci_domain_nr(h
->pdev
->bus
);
2420 pciinfo
.bus
= h
->pdev
->bus
->number
;
2421 pciinfo
.dev_fn
= h
->pdev
->devfn
;
2422 pciinfo
.board_id
= h
->board_id
;
2423 if (copy_to_user(argp
, &pciinfo
, sizeof(pciinfo
)))
2428 static int hpsa_getdrivver_ioctl(struct ctlr_info
*h
, void __user
*argp
)
2430 DriverVer_type DriverVer
;
2431 unsigned char vmaj
, vmin
, vsubmin
;
2434 rc
= sscanf(HPSA_DRIVER_VERSION
, "%hhu.%hhu.%hhu",
2435 &vmaj
, &vmin
, &vsubmin
);
2437 dev_info(&h
->pdev
->dev
, "driver version string '%s' "
2438 "unrecognized.", HPSA_DRIVER_VERSION
);
2443 DriverVer
= (vmaj
<< 16) | (vmin
<< 8) | vsubmin
;
2446 if (copy_to_user(argp
, &DriverVer
, sizeof(DriverVer_type
)))
2451 static int hpsa_passthru_ioctl(struct ctlr_info
*h
, void __user
*argp
)
2453 IOCTL_Command_struct iocommand
;
2454 struct CommandList
*c
;
2456 union u64bit temp64
;
2460 if (!capable(CAP_SYS_RAWIO
))
2462 if (copy_from_user(&iocommand
, argp
, sizeof(iocommand
)))
2464 if ((iocommand
.buf_size
< 1) &&
2465 (iocommand
.Request
.Type
.Direction
!= XFER_NONE
)) {
2468 if (iocommand
.buf_size
> 0) {
2469 buff
= kmalloc(iocommand
.buf_size
, GFP_KERNEL
);
2473 if (iocommand
.Request
.Type
.Direction
== XFER_WRITE
) {
2474 /* Copy the data into the buffer we created */
2475 if (copy_from_user(buff
, iocommand
.buf
, iocommand
.buf_size
)) {
2480 memset(buff
, 0, iocommand
.buf_size
);
2481 c
= cmd_special_alloc(h
);
2486 /* Fill in the command type */
2487 c
->cmd_type
= CMD_IOCTL_PEND
;
2488 /* Fill in Command Header */
2489 c
->Header
.ReplyQueue
= 0; /* unused in simple mode */
2490 if (iocommand
.buf_size
> 0) { /* buffer to fill */
2491 c
->Header
.SGList
= 1;
2492 c
->Header
.SGTotal
= 1;
2493 } else { /* no buffers to fill */
2494 c
->Header
.SGList
= 0;
2495 c
->Header
.SGTotal
= 0;
2497 memcpy(&c
->Header
.LUN
, &iocommand
.LUN_info
, sizeof(c
->Header
.LUN
));
2498 /* use the kernel address the cmd block for tag */
2499 c
->Header
.Tag
.lower
= c
->busaddr
;
2501 /* Fill in Request block */
2502 memcpy(&c
->Request
, &iocommand
.Request
,
2503 sizeof(c
->Request
));
2505 /* Fill in the scatter gather information */
2506 if (iocommand
.buf_size
> 0) {
2507 temp64
.val
= pci_map_single(h
->pdev
, buff
,
2508 iocommand
.buf_size
, PCI_DMA_BIDIRECTIONAL
);
2509 c
->SG
[0].Addr
.lower
= temp64
.val32
.lower
;
2510 c
->SG
[0].Addr
.upper
= temp64
.val32
.upper
;
2511 c
->SG
[0].Len
= iocommand
.buf_size
;
2512 c
->SG
[0].Ext
= 0; /* we are not chaining*/
2514 hpsa_scsi_do_simple_cmd_core(h
, c
);
2515 hpsa_pci_unmap(h
->pdev
, c
, 1, PCI_DMA_BIDIRECTIONAL
);
2516 check_ioctl_unit_attention(h
, c
);
2518 /* Copy the error information out */
2519 memcpy(&iocommand
.error_info
, c
->err_info
,
2520 sizeof(iocommand
.error_info
));
2521 if (copy_to_user(argp
, &iocommand
, sizeof(iocommand
))) {
2523 cmd_special_free(h
, c
);
2527 if (iocommand
.Request
.Type
.Direction
== XFER_READ
) {
2528 /* Copy the data out of the buffer we created */
2529 if (copy_to_user(iocommand
.buf
, buff
, iocommand
.buf_size
)) {
2531 cmd_special_free(h
, c
);
2536 cmd_special_free(h
, c
);
2540 static int hpsa_big_passthru_ioctl(struct ctlr_info
*h
, void __user
*argp
)
2542 BIG_IOCTL_Command_struct
*ioc
;
2543 struct CommandList
*c
;
2544 unsigned char **buff
= NULL
;
2545 int *buff_size
= NULL
;
2546 union u64bit temp64
;
2552 BYTE __user
*data_ptr
;
2556 if (!capable(CAP_SYS_RAWIO
))
2558 ioc
= (BIG_IOCTL_Command_struct
*)
2559 kmalloc(sizeof(*ioc
), GFP_KERNEL
);
2564 if (copy_from_user(ioc
, argp
, sizeof(*ioc
))) {
2568 if ((ioc
->buf_size
< 1) &&
2569 (ioc
->Request
.Type
.Direction
!= XFER_NONE
)) {
2573 /* Check kmalloc limits using all SGs */
2574 if (ioc
->malloc_size
> MAX_KMALLOC_SIZE
) {
2578 if (ioc
->buf_size
> ioc
->malloc_size
* MAXSGENTRIES
) {
2582 buff
= kzalloc(MAXSGENTRIES
* sizeof(char *), GFP_KERNEL
);
2587 buff_size
= kmalloc(MAXSGENTRIES
* sizeof(int), GFP_KERNEL
);
2592 left
= ioc
->buf_size
;
2593 data_ptr
= ioc
->buf
;
2595 sz
= (left
> ioc
->malloc_size
) ? ioc
->malloc_size
: left
;
2596 buff_size
[sg_used
] = sz
;
2597 buff
[sg_used
] = kmalloc(sz
, GFP_KERNEL
);
2598 if (buff
[sg_used
] == NULL
) {
2602 if (ioc
->Request
.Type
.Direction
== XFER_WRITE
) {
2603 if (copy_from_user(buff
[sg_used
], data_ptr
, sz
)) {
2608 memset(buff
[sg_used
], 0, sz
);
2613 c
= cmd_special_alloc(h
);
2618 c
->cmd_type
= CMD_IOCTL_PEND
;
2619 c
->Header
.ReplyQueue
= 0;
2621 if (ioc
->buf_size
> 0) {
2622 c
->Header
.SGList
= sg_used
;
2623 c
->Header
.SGTotal
= sg_used
;
2625 c
->Header
.SGList
= 0;
2626 c
->Header
.SGTotal
= 0;
2628 memcpy(&c
->Header
.LUN
, &ioc
->LUN_info
, sizeof(c
->Header
.LUN
));
2629 c
->Header
.Tag
.lower
= c
->busaddr
;
2630 memcpy(&c
->Request
, &ioc
->Request
, sizeof(c
->Request
));
2631 if (ioc
->buf_size
> 0) {
2633 for (i
= 0; i
< sg_used
; i
++) {
2634 temp64
.val
= pci_map_single(h
->pdev
, buff
[i
],
2635 buff_size
[i
], PCI_DMA_BIDIRECTIONAL
);
2636 c
->SG
[i
].Addr
.lower
= temp64
.val32
.lower
;
2637 c
->SG
[i
].Addr
.upper
= temp64
.val32
.upper
;
2638 c
->SG
[i
].Len
= buff_size
[i
];
2639 /* we are not chaining */
2643 hpsa_scsi_do_simple_cmd_core(h
, c
);
2644 hpsa_pci_unmap(h
->pdev
, c
, sg_used
, PCI_DMA_BIDIRECTIONAL
);
2645 check_ioctl_unit_attention(h
, c
);
2646 /* Copy the error information out */
2647 memcpy(&ioc
->error_info
, c
->err_info
, sizeof(ioc
->error_info
));
2648 if (copy_to_user(argp
, ioc
, sizeof(*ioc
))) {
2649 cmd_special_free(h
, c
);
2653 if (ioc
->Request
.Type
.Direction
== XFER_READ
) {
2654 /* Copy the data out of the buffer we created */
2655 BYTE __user
*ptr
= ioc
->buf
;
2656 for (i
= 0; i
< sg_used
; i
++) {
2657 if (copy_to_user(ptr
, buff
[i
], buff_size
[i
])) {
2658 cmd_special_free(h
, c
);
2662 ptr
+= buff_size
[i
];
2665 cmd_special_free(h
, c
);
2669 for (i
= 0; i
< sg_used
; i
++)
2678 static void check_ioctl_unit_attention(struct ctlr_info
*h
,
2679 struct CommandList
*c
)
2681 if (c
->err_info
->CommandStatus
== CMD_TARGET_STATUS
&&
2682 c
->err_info
->ScsiStatus
!= SAM_STAT_CHECK_CONDITION
)
2683 (void) check_for_unit_attention(h
, c
);
2688 static int hpsa_ioctl(struct scsi_device
*dev
, int cmd
, void *arg
)
2690 struct ctlr_info
*h
;
2691 void __user
*argp
= (void __user
*)arg
;
2693 h
= sdev_to_hba(dev
);
2696 case CCISS_DEREGDISK
:
2697 case CCISS_REGNEWDISK
:
2699 hpsa_scan_start(h
->scsi_host
);
2701 case CCISS_GETPCIINFO
:
2702 return hpsa_getpciinfo_ioctl(h
, argp
);
2703 case CCISS_GETDRIVVER
:
2704 return hpsa_getdrivver_ioctl(h
, argp
);
2705 case CCISS_PASSTHRU
:
2706 return hpsa_passthru_ioctl(h
, argp
);
2707 case CCISS_BIG_PASSTHRU
:
2708 return hpsa_big_passthru_ioctl(h
, argp
);
2714 static void fill_cmd(struct CommandList
*c
, u8 cmd
, struct ctlr_info
*h
,
2715 void *buff
, size_t size
, u8 page_code
, unsigned char *scsi3addr
,
2718 int pci_dir
= XFER_NONE
;
2720 c
->cmd_type
= CMD_IOCTL_PEND
;
2721 c
->Header
.ReplyQueue
= 0;
2722 if (buff
!= NULL
&& size
> 0) {
2723 c
->Header
.SGList
= 1;
2724 c
->Header
.SGTotal
= 1;
2726 c
->Header
.SGList
= 0;
2727 c
->Header
.SGTotal
= 0;
2729 c
->Header
.Tag
.lower
= c
->busaddr
;
2730 memcpy(c
->Header
.LUN
.LunAddrBytes
, scsi3addr
, 8);
2732 c
->Request
.Type
.Type
= cmd_type
;
2733 if (cmd_type
== TYPE_CMD
) {
2736 /* are we trying to read a vital product page */
2737 if (page_code
!= 0) {
2738 c
->Request
.CDB
[1] = 0x01;
2739 c
->Request
.CDB
[2] = page_code
;
2741 c
->Request
.CDBLen
= 6;
2742 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
2743 c
->Request
.Type
.Direction
= XFER_READ
;
2744 c
->Request
.Timeout
= 0;
2745 c
->Request
.CDB
[0] = HPSA_INQUIRY
;
2746 c
->Request
.CDB
[4] = size
& 0xFF;
2748 case HPSA_REPORT_LOG
:
2749 case HPSA_REPORT_PHYS
:
2750 /* Talking to controller so It's a physical command
2751 mode = 00 target = 0. Nothing to write.
2753 c
->Request
.CDBLen
= 12;
2754 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
2755 c
->Request
.Type
.Direction
= XFER_READ
;
2756 c
->Request
.Timeout
= 0;
2757 c
->Request
.CDB
[0] = cmd
;
2758 c
->Request
.CDB
[6] = (size
>> 24) & 0xFF; /* MSB */
2759 c
->Request
.CDB
[7] = (size
>> 16) & 0xFF;
2760 c
->Request
.CDB
[8] = (size
>> 8) & 0xFF;
2761 c
->Request
.CDB
[9] = size
& 0xFF;
2764 case HPSA_READ_CAPACITY
:
2765 c
->Request
.CDBLen
= 10;
2766 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
2767 c
->Request
.Type
.Direction
= XFER_READ
;
2768 c
->Request
.Timeout
= 0;
2769 c
->Request
.CDB
[0] = cmd
;
2771 case HPSA_CACHE_FLUSH
:
2772 c
->Request
.CDBLen
= 12;
2773 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
2774 c
->Request
.Type
.Direction
= XFER_WRITE
;
2775 c
->Request
.Timeout
= 0;
2776 c
->Request
.CDB
[0] = BMIC_WRITE
;
2777 c
->Request
.CDB
[6] = BMIC_CACHE_FLUSH
;
2779 case TEST_UNIT_READY
:
2780 c
->Request
.CDBLen
= 6;
2781 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
2782 c
->Request
.Type
.Direction
= XFER_NONE
;
2783 c
->Request
.Timeout
= 0;
2786 dev_warn(&h
->pdev
->dev
, "unknown command 0x%c\n", cmd
);
2790 } else if (cmd_type
== TYPE_MSG
) {
2793 case HPSA_DEVICE_RESET_MSG
:
2794 c
->Request
.CDBLen
= 16;
2795 c
->Request
.Type
.Type
= 1; /* It is a MSG not a CMD */
2796 c
->Request
.Type
.Attribute
= ATTR_SIMPLE
;
2797 c
->Request
.Type
.Direction
= XFER_NONE
;
2798 c
->Request
.Timeout
= 0; /* Don't time out */
2799 c
->Request
.CDB
[0] = 0x01; /* RESET_MSG is 0x01 */
2800 c
->Request
.CDB
[1] = 0x03; /* Reset target above */
2801 /* If bytes 4-7 are zero, it means reset the */
2803 c
->Request
.CDB
[4] = 0x00;
2804 c
->Request
.CDB
[5] = 0x00;
2805 c
->Request
.CDB
[6] = 0x00;
2806 c
->Request
.CDB
[7] = 0x00;
2810 dev_warn(&h
->pdev
->dev
, "unknown message type %d\n",
2815 dev_warn(&h
->pdev
->dev
, "unknown command type %d\n", cmd_type
);
2819 switch (c
->Request
.Type
.Direction
) {
2821 pci_dir
= PCI_DMA_FROMDEVICE
;
2824 pci_dir
= PCI_DMA_TODEVICE
;
2827 pci_dir
= PCI_DMA_NONE
;
2830 pci_dir
= PCI_DMA_BIDIRECTIONAL
;
2833 hpsa_map_one(h
->pdev
, c
, buff
, size
, pci_dir
);
2839 * Map (physical) PCI mem into (virtual) kernel space
2841 static void __iomem
*remap_pci_mem(ulong base
, ulong size
)
2843 ulong page_base
= ((ulong
) base
) & PAGE_MASK
;
2844 ulong page_offs
= ((ulong
) base
) - page_base
;
2845 void __iomem
*page_remapped
= ioremap(page_base
, page_offs
+ size
);
2847 return page_remapped
? (page_remapped
+ page_offs
) : NULL
;
2850 /* Takes cmds off the submission queue and sends them to the hardware,
2851 * then puts them on the queue of cmds waiting for completion.
2853 static void start_io(struct ctlr_info
*h
)
2855 struct CommandList
*c
;
2857 while (!hlist_empty(&h
->reqQ
)) {
2858 c
= hlist_entry(h
->reqQ
.first
, struct CommandList
, list
);
2859 /* can't do anything if fifo is full */
2860 if ((h
->access
.fifo_full(h
))) {
2861 dev_warn(&h
->pdev
->dev
, "fifo full\n");
2865 /* Get the first entry from the Request Q */
2869 /* Tell the controller execute command */
2870 h
->access
.submit_command(h
, c
);
2872 /* Put job onto the completed Q */
2877 static inline unsigned long get_next_completion(struct ctlr_info
*h
)
2879 return h
->access
.command_completed(h
);
2882 static inline bool interrupt_pending(struct ctlr_info
*h
)
2884 return h
->access
.intr_pending(h
);
2887 static inline long interrupt_not_for_us(struct ctlr_info
*h
)
2889 return !(h
->msi_vector
|| h
->msix_vector
) &&
2890 ((h
->access
.intr_pending(h
) == 0) ||
2891 (h
->interrupts_enabled
== 0));
2894 static inline int bad_tag(struct ctlr_info
*h
, u32 tag_index
,
2897 if (unlikely(tag_index
>= h
->nr_cmds
)) {
2898 dev_warn(&h
->pdev
->dev
, "bad tag 0x%08x ignored.\n", raw_tag
);
2904 static inline void finish_cmd(struct CommandList
*c
, u32 raw_tag
)
2907 if (likely(c
->cmd_type
== CMD_SCSI
))
2908 complete_scsi_command(c
, 0, raw_tag
);
2909 else if (c
->cmd_type
== CMD_IOCTL_PEND
)
2910 complete(c
->waiting
);
2913 static inline u32
hpsa_tag_contains_index(u32 tag
)
2915 #define DIRECT_LOOKUP_BIT 0x10
2916 return tag
& DIRECT_LOOKUP_BIT
;
2919 static inline u32
hpsa_tag_to_index(u32 tag
)
2921 #define DIRECT_LOOKUP_SHIFT 5
2922 return tag
>> DIRECT_LOOKUP_SHIFT
;
2925 static inline u32
hpsa_tag_discard_error_bits(u32 tag
)
2927 #define HPSA_ERROR_BITS 0x03
2928 return tag
& ~HPSA_ERROR_BITS
;
2931 /* process completion of an indexed ("direct lookup") command */
2932 static inline u32
process_indexed_cmd(struct ctlr_info
*h
,
2936 struct CommandList
*c
;
2938 tag_index
= hpsa_tag_to_index(raw_tag
);
2939 if (bad_tag(h
, tag_index
, raw_tag
))
2940 return next_command(h
);
2941 c
= h
->cmd_pool
+ tag_index
;
2942 finish_cmd(c
, raw_tag
);
2943 return next_command(h
);
2946 /* process completion of a non-indexed command */
2947 static inline u32
process_nonindexed_cmd(struct ctlr_info
*h
,
2951 struct CommandList
*c
= NULL
;
2952 struct hlist_node
*tmp
;
2954 tag
= hpsa_tag_discard_error_bits(raw_tag
);
2955 hlist_for_each_entry(c
, tmp
, &h
->cmpQ
, list
) {
2956 if ((c
->busaddr
& 0xFFFFFFE0) == (tag
& 0xFFFFFFE0)) {
2957 finish_cmd(c
, raw_tag
);
2958 return next_command(h
);
2961 bad_tag(h
, h
->nr_cmds
+ 1, raw_tag
);
2962 return next_command(h
);
2965 static irqreturn_t
do_hpsa_intr(int irq
, void *dev_id
)
2967 struct ctlr_info
*h
= dev_id
;
2968 unsigned long flags
;
2971 if (interrupt_not_for_us(h
))
2973 spin_lock_irqsave(&h
->lock
, flags
);
2974 raw_tag
= get_next_completion(h
);
2975 while (raw_tag
!= FIFO_EMPTY
) {
2976 if (hpsa_tag_contains_index(raw_tag
))
2977 raw_tag
= process_indexed_cmd(h
, raw_tag
);
2979 raw_tag
= process_nonindexed_cmd(h
, raw_tag
);
2981 spin_unlock_irqrestore(&h
->lock
, flags
);
2985 /* Send a message CDB to the firmware. */
2986 static __devinit
int hpsa_message(struct pci_dev
*pdev
, unsigned char opcode
,
2990 struct CommandListHeader CommandHeader
;
2991 struct RequestBlock Request
;
2992 struct ErrDescriptor ErrorDescriptor
;
2994 struct Command
*cmd
;
2995 static const size_t cmd_sz
= sizeof(*cmd
) +
2996 sizeof(cmd
->ErrorDescriptor
);
2998 uint32_t paddr32
, tag
;
2999 void __iomem
*vaddr
;
3002 vaddr
= pci_ioremap_bar(pdev
, 0);
3006 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
3007 * CCISS commands, so they must be allocated from the lower 4GiB of
3010 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
3016 cmd
= pci_alloc_consistent(pdev
, cmd_sz
, &paddr64
);
3022 /* This must fit, because of the 32-bit consistent DMA mask. Also,
3023 * although there's no guarantee, we assume that the address is at
3024 * least 4-byte aligned (most likely, it's page-aligned).
3028 cmd
->CommandHeader
.ReplyQueue
= 0;
3029 cmd
->CommandHeader
.SGList
= 0;
3030 cmd
->CommandHeader
.SGTotal
= 0;
3031 cmd
->CommandHeader
.Tag
.lower
= paddr32
;
3032 cmd
->CommandHeader
.Tag
.upper
= 0;
3033 memset(&cmd
->CommandHeader
.LUN
.LunAddrBytes
, 0, 8);
3035 cmd
->Request
.CDBLen
= 16;
3036 cmd
->Request
.Type
.Type
= TYPE_MSG
;
3037 cmd
->Request
.Type
.Attribute
= ATTR_HEADOFQUEUE
;
3038 cmd
->Request
.Type
.Direction
= XFER_NONE
;
3039 cmd
->Request
.Timeout
= 0; /* Don't time out */
3040 cmd
->Request
.CDB
[0] = opcode
;
3041 cmd
->Request
.CDB
[1] = type
;
3042 memset(&cmd
->Request
.CDB
[2], 0, 14); /* rest of the CDB is reserved */
3043 cmd
->ErrorDescriptor
.Addr
.lower
= paddr32
+ sizeof(*cmd
);
3044 cmd
->ErrorDescriptor
.Addr
.upper
= 0;
3045 cmd
->ErrorDescriptor
.Len
= sizeof(struct ErrorInfo
);
3047 writel(paddr32
, vaddr
+ SA5_REQUEST_PORT_OFFSET
);
3049 for (i
= 0; i
< HPSA_MSG_SEND_RETRY_LIMIT
; i
++) {
3050 tag
= readl(vaddr
+ SA5_REPLY_PORT_OFFSET
);
3051 if (hpsa_tag_discard_error_bits(tag
) == paddr32
)
3053 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS
);
3058 /* we leak the DMA buffer here ... no choice since the controller could
3059 * still complete the command.
3061 if (i
== HPSA_MSG_SEND_RETRY_LIMIT
) {
3062 dev_err(&pdev
->dev
, "controller message %02x:%02x timed out\n",
3067 pci_free_consistent(pdev
, cmd_sz
, cmd
, paddr64
);
3069 if (tag
& HPSA_ERROR_BIT
) {
3070 dev_err(&pdev
->dev
, "controller message %02x:%02x failed\n",
3075 dev_info(&pdev
->dev
, "controller message %02x:%02x succeeded\n",
3080 #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
3081 #define hpsa_noop(p) hpsa_message(p, 3, 0)
3083 static __devinit
int hpsa_reset_msi(struct pci_dev
*pdev
)
3085 /* the #defines are stolen from drivers/pci/msi.h. */
3086 #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
3087 #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
3092 pos
= pci_find_capability(pdev
, PCI_CAP_ID_MSI
);
3094 pci_read_config_word(pdev
, msi_control_reg(pos
), &control
);
3095 if (control
& PCI_MSI_FLAGS_ENABLE
) {
3096 dev_info(&pdev
->dev
, "resetting MSI\n");
3097 pci_write_config_word(pdev
, msi_control_reg(pos
),
3098 control
& ~PCI_MSI_FLAGS_ENABLE
);
3102 pos
= pci_find_capability(pdev
, PCI_CAP_ID_MSIX
);
3104 pci_read_config_word(pdev
, msi_control_reg(pos
), &control
);
3105 if (control
& PCI_MSIX_FLAGS_ENABLE
) {
3106 dev_info(&pdev
->dev
, "resetting MSI-X\n");
3107 pci_write_config_word(pdev
, msi_control_reg(pos
),
3108 control
& ~PCI_MSIX_FLAGS_ENABLE
);
3115 /* This does a hard reset of the controller using PCI power management
3118 static __devinit
int hpsa_hard_reset_controller(struct pci_dev
*pdev
)
3120 u16 pmcsr
, saved_config_space
[32];
3123 dev_info(&pdev
->dev
, "using PCI PM to reset controller\n");
3125 /* This is very nearly the same thing as
3127 * pci_save_state(pci_dev);
3128 * pci_set_power_state(pci_dev, PCI_D3hot);
3129 * pci_set_power_state(pci_dev, PCI_D0);
3130 * pci_restore_state(pci_dev);
3132 * but we can't use these nice canned kernel routines on
3133 * kexec, because they also check the MSI/MSI-X state in PCI
3134 * configuration space and do the wrong thing when it is
3135 * set/cleared. Also, the pci_save/restore_state functions
3136 * violate the ordering requirements for restoring the
3137 * configuration space from the CCISS document (see the
3138 * comment below). So we roll our own ....
3141 for (i
= 0; i
< 32; i
++)
3142 pci_read_config_word(pdev
, 2*i
, &saved_config_space
[i
]);
3144 pos
= pci_find_capability(pdev
, PCI_CAP_ID_PM
);
3147 "hpsa_reset_controller: PCI PM not supported\n");
3151 /* Quoting from the Open CISS Specification: "The Power
3152 * Management Control/Status Register (CSR) controls the power
3153 * state of the device. The normal operating state is D0,
3154 * CSR=00h. The software off state is D3, CSR=03h. To reset
3155 * the controller, place the interface device in D3 then to
3156 * D0, this causes a secondary PCI reset which will reset the
3160 /* enter the D3hot power management state */
3161 pci_read_config_word(pdev
, pos
+ PCI_PM_CTRL
, &pmcsr
);
3162 pmcsr
&= ~PCI_PM_CTRL_STATE_MASK
;
3164 pci_write_config_word(pdev
, pos
+ PCI_PM_CTRL
, pmcsr
);
3168 /* enter the D0 power management state */
3169 pmcsr
&= ~PCI_PM_CTRL_STATE_MASK
;
3171 pci_write_config_word(pdev
, pos
+ PCI_PM_CTRL
, pmcsr
);
3175 /* Restore the PCI configuration space. The Open CISS
3176 * Specification says, "Restore the PCI Configuration
3177 * Registers, offsets 00h through 60h. It is important to
3178 * restore the command register, 16-bits at offset 04h,
3179 * last. Do not restore the configuration status register,
3180 * 16-bits at offset 06h." Note that the offset is 2*i.
3182 for (i
= 0; i
< 32; i
++) {
3183 if (i
== 2 || i
== 3)
3185 pci_write_config_word(pdev
, 2*i
, saved_config_space
[i
]);
3188 pci_write_config_word(pdev
, 4, saved_config_space
[2]);
3194 * We cannot read the structure directly, for portability we must use
3196 * This is for debug only.
3199 static void print_cfg_table(struct device
*dev
, struct CfgTable
*tb
)
3204 dev_info(dev
, "Controller Configuration information\n");
3205 dev_info(dev
, "------------------------------------\n");
3206 for (i
= 0; i
< 4; i
++)
3207 temp_name
[i
] = readb(&(tb
->Signature
[i
]));
3208 temp_name
[4] = '\0';
3209 dev_info(dev
, " Signature = %s\n", temp_name
);
3210 dev_info(dev
, " Spec Number = %d\n", readl(&(tb
->SpecValence
)));
3211 dev_info(dev
, " Transport methods supported = 0x%x\n",
3212 readl(&(tb
->TransportSupport
)));
3213 dev_info(dev
, " Transport methods active = 0x%x\n",
3214 readl(&(tb
->TransportActive
)));
3215 dev_info(dev
, " Requested transport Method = 0x%x\n",
3216 readl(&(tb
->HostWrite
.TransportRequest
)));
3217 dev_info(dev
, " Coalesce Interrupt Delay = 0x%x\n",
3218 readl(&(tb
->HostWrite
.CoalIntDelay
)));
3219 dev_info(dev
, " Coalesce Interrupt Count = 0x%x\n",
3220 readl(&(tb
->HostWrite
.CoalIntCount
)));
3221 dev_info(dev
, " Max outstanding commands = 0x%d\n",
3222 readl(&(tb
->CmdsOutMax
)));
3223 dev_info(dev
, " Bus Types = 0x%x\n", readl(&(tb
->BusTypes
)));
3224 for (i
= 0; i
< 16; i
++)
3225 temp_name
[i
] = readb(&(tb
->ServerName
[i
]));
3226 temp_name
[16] = '\0';
3227 dev_info(dev
, " Server Name = %s\n", temp_name
);
3228 dev_info(dev
, " Heartbeat Counter = 0x%x\n\n\n",
3229 readl(&(tb
->HeartBeat
)));
3231 #endif /* HPSA_DEBUG */
3233 static int find_PCI_BAR_index(struct pci_dev
*pdev
, unsigned long pci_bar_addr
)
3235 int i
, offset
, mem_type
, bar_type
;
3237 if (pci_bar_addr
== PCI_BASE_ADDRESS_0
) /* looking for BAR zero? */
3240 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
3241 bar_type
= pci_resource_flags(pdev
, i
) & PCI_BASE_ADDRESS_SPACE
;
3242 if (bar_type
== PCI_BASE_ADDRESS_SPACE_IO
)
3245 mem_type
= pci_resource_flags(pdev
, i
) &
3246 PCI_BASE_ADDRESS_MEM_TYPE_MASK
;
3248 case PCI_BASE_ADDRESS_MEM_TYPE_32
:
3249 case PCI_BASE_ADDRESS_MEM_TYPE_1M
:
3250 offset
+= 4; /* 32 bit */
3252 case PCI_BASE_ADDRESS_MEM_TYPE_64
:
3255 default: /* reserved in PCI 2.2 */
3256 dev_warn(&pdev
->dev
,
3257 "base address is invalid\n");
3262 if (offset
== pci_bar_addr
- PCI_BASE_ADDRESS_0
)
3268 /* If MSI/MSI-X is supported by the kernel we will try to enable it on
3269 * controllers that are capable. If not, we use IO-APIC mode.
3272 static void __devinit
hpsa_interrupt_mode(struct ctlr_info
*h
,
3273 struct pci_dev
*pdev
, u32 board_id
)
3275 #ifdef CONFIG_PCI_MSI
3277 struct msix_entry hpsa_msix_entries
[4] = { {0, 0}, {0, 1},
3281 /* Some boards advertise MSI but don't really support it */
3282 if ((board_id
== 0x40700E11) ||
3283 (board_id
== 0x40800E11) ||
3284 (board_id
== 0x40820E11) || (board_id
== 0x40830E11))
3285 goto default_int_mode
;
3286 if (pci_find_capability(pdev
, PCI_CAP_ID_MSIX
)) {
3287 dev_info(&pdev
->dev
, "MSIX\n");
3288 err
= pci_enable_msix(pdev
, hpsa_msix_entries
, 4);
3290 h
->intr
[0] = hpsa_msix_entries
[0].vector
;
3291 h
->intr
[1] = hpsa_msix_entries
[1].vector
;
3292 h
->intr
[2] = hpsa_msix_entries
[2].vector
;
3293 h
->intr
[3] = hpsa_msix_entries
[3].vector
;
3298 dev_warn(&pdev
->dev
, "only %d MSI-X vectors "
3299 "available\n", err
);
3300 goto default_int_mode
;
3302 dev_warn(&pdev
->dev
, "MSI-X init failed %d\n",
3304 goto default_int_mode
;
3307 if (pci_find_capability(pdev
, PCI_CAP_ID_MSI
)) {
3308 dev_info(&pdev
->dev
, "MSI\n");
3309 if (!pci_enable_msi(pdev
))
3312 dev_warn(&pdev
->dev
, "MSI init failed\n");
3315 #endif /* CONFIG_PCI_MSI */
3316 /* if we get here we're going to use the default interrupt mode */
3317 h
->intr
[PERF_MODE_INT
] = pdev
->irq
;
3320 static int hpsa_pci_init(struct ctlr_info
*h
, struct pci_dev
*pdev
)
3322 ushort subsystem_vendor_id
, subsystem_device_id
, command
;
3323 u32 board_id
, scratchpad
= 0;
3326 u64 cfg_base_addr_index
;
3328 int i
, prod_index
, err
;
3330 subsystem_vendor_id
= pdev
->subsystem_vendor
;
3331 subsystem_device_id
= pdev
->subsystem_device
;
3332 board_id
= (((u32
) (subsystem_device_id
<< 16) & 0xffff0000) |
3333 subsystem_vendor_id
);
3335 for (i
= 0; i
< ARRAY_SIZE(products
); i
++)
3336 if (board_id
== products
[i
].board_id
)
3341 if (prod_index
== ARRAY_SIZE(products
)) {
3343 if (subsystem_vendor_id
!= PCI_VENDOR_ID_HP
||
3345 dev_warn(&pdev
->dev
, "unrecognized board ID:"
3346 " 0x%08lx, ignoring.\n",
3347 (unsigned long) board_id
);
3351 /* check to see if controller has been disabled
3352 * BEFORE trying to enable it
3354 (void)pci_read_config_word(pdev
, PCI_COMMAND
, &command
);
3355 if (!(command
& 0x02)) {
3356 dev_warn(&pdev
->dev
, "controller appears to be disabled\n");
3360 err
= pci_enable_device(pdev
);
3362 dev_warn(&pdev
->dev
, "unable to enable PCI device\n");
3366 err
= pci_request_regions(pdev
, "hpsa");
3368 dev_err(&pdev
->dev
, "cannot obtain PCI resources, aborting\n");
3372 /* If the kernel supports MSI/MSI-X we will try to enable that,
3373 * else we use the IO-APIC interrupt assigned to us by system ROM.
3375 hpsa_interrupt_mode(h
, pdev
, board_id
);
3377 /* find the memory BAR */
3378 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
3379 if (pci_resource_flags(pdev
, i
) & IORESOURCE_MEM
)
3382 if (i
== DEVICE_COUNT_RESOURCE
) {
3383 dev_warn(&pdev
->dev
, "no memory BAR found\n");
3385 goto err_out_free_res
;
3388 h
->paddr
= pci_resource_start(pdev
, i
); /* addressing mode bits
3392 h
->vaddr
= remap_pci_mem(h
->paddr
, 0x250);
3394 /* Wait for the board to become ready. */
3395 for (i
= 0; i
< HPSA_BOARD_READY_ITERATIONS
; i
++) {
3396 scratchpad
= readl(h
->vaddr
+ SA5_SCRATCHPAD_OFFSET
);
3397 if (scratchpad
== HPSA_FIRMWARE_READY
)
3399 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS
);
3401 if (scratchpad
!= HPSA_FIRMWARE_READY
) {
3402 dev_warn(&pdev
->dev
, "board not ready, timed out.\n");
3404 goto err_out_free_res
;
3407 /* get the address index number */
3408 cfg_base_addr
= readl(h
->vaddr
+ SA5_CTCFG_OFFSET
);
3409 cfg_base_addr
&= (u32
) 0x0000ffff;
3410 cfg_base_addr_index
= find_PCI_BAR_index(pdev
, cfg_base_addr
);
3411 if (cfg_base_addr_index
== -1) {
3412 dev_warn(&pdev
->dev
, "cannot find cfg_base_addr_index\n");
3414 goto err_out_free_res
;
3417 cfg_offset
= readl(h
->vaddr
+ SA5_CTMEM_OFFSET
);
3418 h
->cfgtable
= remap_pci_mem(pci_resource_start(pdev
,
3419 cfg_base_addr_index
) + cfg_offset
,
3420 sizeof(h
->cfgtable
));
3421 /* Find performant mode table. */
3422 trans_offset
= readl(&(h
->cfgtable
->TransMethodOffset
));
3423 h
->transtable
= remap_pci_mem(pci_resource_start(pdev
,
3424 cfg_base_addr_index
)+cfg_offset
+trans_offset
,
3425 sizeof(*h
->transtable
));
3427 h
->board_id
= board_id
;
3428 h
->max_commands
= readl(&(h
->cfgtable
->MaxPerformantModeCommands
));
3429 h
->product_name
= products
[prod_index
].product_name
;
3430 h
->access
= *(products
[prod_index
].access
);
3431 /* Allow room for some ioctls */
3432 h
->nr_cmds
= h
->max_commands
- 4;
3434 if ((readb(&h
->cfgtable
->Signature
[0]) != 'C') ||
3435 (readb(&h
->cfgtable
->Signature
[1]) != 'I') ||
3436 (readb(&h
->cfgtable
->Signature
[2]) != 'S') ||
3437 (readb(&h
->cfgtable
->Signature
[3]) != 'S')) {
3438 dev_warn(&pdev
->dev
, "not a valid CISS config table\n");
3440 goto err_out_free_res
;
3444 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3446 prefetch
= readl(&(h
->cfgtable
->SCSI_Prefetch
));
3448 writel(prefetch
, &(h
->cfgtable
->SCSI_Prefetch
));
3452 /* Disabling DMA prefetch for the P600
3453 * An ASIC bug may result in a prefetch beyond
3456 if (board_id
== 0x3225103C) {
3458 dma_prefetch
= readl(h
->vaddr
+ I2O_DMA1_CFG
);
3459 dma_prefetch
|= 0x8000;
3460 writel(dma_prefetch
, h
->vaddr
+ I2O_DMA1_CFG
);
3463 h
->max_commands
= readl(&(h
->cfgtable
->CmdsOutMax
));
3464 /* Update the field, and then ring the doorbell */
3465 writel(CFGTBL_Trans_Simple
, &(h
->cfgtable
->HostWrite
.TransportRequest
));
3466 writel(CFGTBL_ChangeReq
, h
->vaddr
+ SA5_DOORBELL
);
3468 /* under certain very rare conditions, this can take awhile.
3469 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3470 * as we enter this code.)
3472 for (i
= 0; i
< MAX_CONFIG_WAIT
; i
++) {
3473 if (!(readl(h
->vaddr
+ SA5_DOORBELL
) & CFGTBL_ChangeReq
))
3475 /* delay and try again */
3480 print_cfg_table(&pdev
->dev
, h
->cfgtable
);
3481 #endif /* HPSA_DEBUG */
3483 if (!(readl(&(h
->cfgtable
->TransportActive
)) & CFGTBL_Trans_Simple
)) {
3484 dev_warn(&pdev
->dev
, "unable to get board into simple mode\n");
3486 goto err_out_free_res
;
3492 * Deliberately omit pci_disable_device(): it does something nasty to
3493 * Smart Array controllers that pci_enable_device does not undo
3495 pci_release_regions(pdev
);
3499 static void __devinit
hpsa_hba_inquiry(struct ctlr_info
*h
)
3503 #define HBA_INQUIRY_BYTE_COUNT 64
3504 h
->hba_inquiry_data
= kmalloc(HBA_INQUIRY_BYTE_COUNT
, GFP_KERNEL
);
3505 if (!h
->hba_inquiry_data
)
3507 rc
= hpsa_scsi_do_inquiry(h
, RAID_CTLR_LUNID
, 0,
3508 h
->hba_inquiry_data
, HBA_INQUIRY_BYTE_COUNT
);
3510 kfree(h
->hba_inquiry_data
);
3511 h
->hba_inquiry_data
= NULL
;
3515 static int __devinit
hpsa_init_one(struct pci_dev
*pdev
,
3516 const struct pci_device_id
*ent
)
3520 struct ctlr_info
*h
;
3522 if (number_of_controllers
== 0)
3523 printk(KERN_INFO DRIVER_NAME
"\n");
3524 if (reset_devices
) {
3525 /* Reset the controller with a PCI power-cycle */
3526 if (hpsa_hard_reset_controller(pdev
) || hpsa_reset_msi(pdev
))
3529 /* Some devices (notably the HP Smart Array 5i Controller)
3530 need a little pause here */
3531 msleep(HPSA_POST_RESET_PAUSE_MSECS
);
3533 /* Now try to get the controller to respond to a no-op */
3534 for (i
= 0; i
< HPSA_POST_RESET_NOOP_RETRIES
; i
++) {
3535 if (hpsa_noop(pdev
) == 0)
3538 dev_warn(&pdev
->dev
, "no-op failed%s\n",
3539 (i
< 11 ? "; re-trying" : ""));
3543 /* Command structures must be aligned on a 32-byte boundary because
3544 * the 5 lower bits of the address are used by the hardware. and by
3545 * the driver. See comments in hpsa.h for more info.
3547 #define COMMANDLIST_ALIGNMENT 32
3548 BUILD_BUG_ON(sizeof(struct CommandList
) % COMMANDLIST_ALIGNMENT
);
3549 h
= kzalloc(sizeof(*h
), GFP_KERNEL
);
3553 h
->busy_initializing
= 1;
3554 INIT_HLIST_HEAD(&h
->cmpQ
);
3555 INIT_HLIST_HEAD(&h
->reqQ
);
3556 mutex_init(&h
->busy_shutting_down
);
3557 init_completion(&h
->scan_wait
);
3558 rc
= hpsa_pci_init(h
, pdev
);
3562 sprintf(h
->devname
, "hpsa%d", number_of_controllers
);
3563 h
->ctlr
= number_of_controllers
;
3564 number_of_controllers
++;
3567 /* configure PCI DMA stuff */
3568 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
3572 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
3576 dev_err(&pdev
->dev
, "no suitable DMA available\n");
3581 /* make sure the board interrupts are off */
3582 h
->access
.set_intr_mask(h
, HPSA_INTR_OFF
);
3583 rc
= request_irq(h
->intr
[PERF_MODE_INT
], do_hpsa_intr
,
3584 IRQF_DISABLED
, h
->devname
, h
);
3586 dev_err(&pdev
->dev
, "unable to get irq %d for %s\n",
3587 h
->intr
[PERF_MODE_INT
], h
->devname
);
3591 dev_info(&pdev
->dev
, "%s: <0x%x> at IRQ %d%s using DAC\n",
3592 h
->devname
, pdev
->device
,
3593 h
->intr
[PERF_MODE_INT
], dac
? "" : " not");
3596 kmalloc(((h
->nr_cmds
+ BITS_PER_LONG
-
3597 1) / BITS_PER_LONG
) * sizeof(unsigned long), GFP_KERNEL
);
3598 h
->cmd_pool
= pci_alloc_consistent(h
->pdev
,
3599 h
->nr_cmds
* sizeof(*h
->cmd_pool
),
3600 &(h
->cmd_pool_dhandle
));
3601 h
->errinfo_pool
= pci_alloc_consistent(h
->pdev
,
3602 h
->nr_cmds
* sizeof(*h
->errinfo_pool
),
3603 &(h
->errinfo_pool_dhandle
));
3604 if ((h
->cmd_pool_bits
== NULL
)
3605 || (h
->cmd_pool
== NULL
)
3606 || (h
->errinfo_pool
== NULL
)) {
3607 dev_err(&pdev
->dev
, "out of memory");
3611 spin_lock_init(&h
->lock
);
3612 spin_lock_init(&h
->scan_lock
);
3613 init_waitqueue_head(&h
->scan_wait_queue
);
3614 h
->scan_finished
= 1; /* no scan currently in progress */
3616 pci_set_drvdata(pdev
, h
);
3617 memset(h
->cmd_pool_bits
, 0,
3618 ((h
->nr_cmds
+ BITS_PER_LONG
-
3619 1) / BITS_PER_LONG
) * sizeof(unsigned long));
3623 /* Turn the interrupts on so we can service requests */
3624 h
->access
.set_intr_mask(h
, HPSA_INTR_ON
);
3626 hpsa_put_ctlr_into_performant_mode(h
);
3627 hpsa_hba_inquiry(h
);
3628 hpsa_register_scsi(h
); /* hook ourselves into SCSI subsystem */
3629 h
->busy_initializing
= 0;
3633 kfree(h
->cmd_pool_bits
);
3635 pci_free_consistent(h
->pdev
,
3636 h
->nr_cmds
* sizeof(struct CommandList
),
3637 h
->cmd_pool
, h
->cmd_pool_dhandle
);
3638 if (h
->errinfo_pool
)
3639 pci_free_consistent(h
->pdev
,
3640 h
->nr_cmds
* sizeof(struct ErrorInfo
),
3642 h
->errinfo_pool_dhandle
);
3643 free_irq(h
->intr
[PERF_MODE_INT
], h
);
3646 h
->busy_initializing
= 0;
3651 static void hpsa_flush_cache(struct ctlr_info
*h
)
3654 struct CommandList
*c
;
3656 flush_buf
= kzalloc(4, GFP_KERNEL
);
3660 c
= cmd_special_alloc(h
);
3662 dev_warn(&h
->pdev
->dev
, "cmd_special_alloc returned NULL!\n");
3665 fill_cmd(c
, HPSA_CACHE_FLUSH
, h
, flush_buf
, 4, 0,
3666 RAID_CTLR_LUNID
, TYPE_CMD
);
3667 hpsa_scsi_do_simple_cmd_with_retry(h
, c
, PCI_DMA_TODEVICE
);
3668 if (c
->err_info
->CommandStatus
!= 0)
3669 dev_warn(&h
->pdev
->dev
,
3670 "error flushing cache on controller\n");
3671 cmd_special_free(h
, c
);
3676 static void hpsa_shutdown(struct pci_dev
*pdev
)
3678 struct ctlr_info
*h
;
3680 h
= pci_get_drvdata(pdev
);
3681 /* Turn board interrupts off and send the flush cache command
3682 * sendcmd will turn off interrupt, and send the flush...
3683 * To write all data in the battery backed cache to disks
3685 hpsa_flush_cache(h
);
3686 h
->access
.set_intr_mask(h
, HPSA_INTR_OFF
);
3687 free_irq(h
->intr
[PERF_MODE_INT
], h
);
3688 #ifdef CONFIG_PCI_MSI
3690 pci_disable_msix(h
->pdev
);
3691 else if (h
->msi_vector
)
3692 pci_disable_msi(h
->pdev
);
3693 #endif /* CONFIG_PCI_MSI */
3696 static void __devexit
hpsa_remove_one(struct pci_dev
*pdev
)
3698 struct ctlr_info
*h
;
3700 if (pci_get_drvdata(pdev
) == NULL
) {
3701 dev_err(&pdev
->dev
, "unable to remove device \n");
3704 h
= pci_get_drvdata(pdev
);
3705 mutex_lock(&h
->busy_shutting_down
);
3706 remove_from_scan_list(h
);
3707 hpsa_unregister_scsi(h
); /* unhook from SCSI subsystem */
3708 hpsa_shutdown(pdev
);
3710 pci_free_consistent(h
->pdev
,
3711 h
->nr_cmds
* sizeof(struct CommandList
),
3712 h
->cmd_pool
, h
->cmd_pool_dhandle
);
3713 pci_free_consistent(h
->pdev
,
3714 h
->nr_cmds
* sizeof(struct ErrorInfo
),
3715 h
->errinfo_pool
, h
->errinfo_pool_dhandle
);
3716 pci_free_consistent(h
->pdev
, h
->reply_pool_size
,
3717 h
->reply_pool
, h
->reply_pool_dhandle
);
3718 kfree(h
->cmd_pool_bits
);
3719 kfree(h
->blockFetchTable
);
3720 kfree(h
->hba_inquiry_data
);
3722 * Deliberately omit pci_disable_device(): it does something nasty to
3723 * Smart Array controllers that pci_enable_device does not undo
3725 pci_release_regions(pdev
);
3726 pci_set_drvdata(pdev
, NULL
);
3727 mutex_unlock(&h
->busy_shutting_down
);
3731 static int hpsa_suspend(__attribute__((unused
)) struct pci_dev
*pdev
,
3732 __attribute__((unused
)) pm_message_t state
)
3737 static int hpsa_resume(__attribute__((unused
)) struct pci_dev
*pdev
)
3742 static struct pci_driver hpsa_pci_driver
= {
3744 .probe
= hpsa_init_one
,
3745 .remove
= __devexit_p(hpsa_remove_one
),
3746 .id_table
= hpsa_pci_device_id
, /* id_table */
3747 .shutdown
= hpsa_shutdown
,
3748 .suspend
= hpsa_suspend
,
3749 .resume
= hpsa_resume
,
3752 /* Fill in bucket_map[], given nsgs (the max number of
3753 * scatter gather elements supported) and bucket[],
3754 * which is an array of 8 integers. The bucket[] array
3755 * contains 8 different DMA transfer sizes (in 16
3756 * byte increments) which the controller uses to fetch
3757 * commands. This function fills in bucket_map[], which
3758 * maps a given number of scatter gather elements to one of
3759 * the 8 DMA transfer sizes. The point of it is to allow the
3760 * controller to only do as much DMA as needed to fetch the
3761 * command, with the DMA transfer size encoded in the lower
3762 * bits of the command address.
3764 static void calc_bucket_map(int bucket
[], int num_buckets
,
3765 int nsgs
, int *bucket_map
)
3769 /* even a command with 0 SGs requires 4 blocks */
3770 #define MINIMUM_TRANSFER_BLOCKS 4
3771 #define NUM_BUCKETS 8
3772 /* Note, bucket_map must have nsgs+1 entries. */
3773 for (i
= 0; i
<= nsgs
; i
++) {
3774 /* Compute size of a command with i SG entries */
3775 size
= i
+ MINIMUM_TRANSFER_BLOCKS
;
3776 b
= num_buckets
; /* Assume the biggest bucket */
3777 /* Find the bucket that is just big enough */
3778 for (j
= 0; j
< 8; j
++) {
3779 if (bucket
[j
] >= size
) {
3784 /* for a command with i SG entries, use bucket b. */
3789 static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info
*h
)
3793 /* 5 = 1 s/g entry or 4k
3794 * 6 = 2 s/g entry or 8k
3795 * 8 = 4 s/g entry or 16k
3796 * 10 = 6 s/g entry or 24k
3798 int bft
[8] = {5, 6, 8, 10, 12, 20, 28, 35}; /* for scatter/gathers */
3801 unsigned long register_value
;
3803 trans_support
= readl(&(h
->cfgtable
->TransportSupport
));
3804 if (!(trans_support
& PERFORMANT_MODE
))
3807 h
->max_commands
= readl(&(h
->cfgtable
->MaxPerformantModeCommands
));
3808 h
->max_sg_entries
= 32;
3809 /* Performant mode ring buffer and supporting data structures */
3810 h
->reply_pool_size
= h
->max_commands
* sizeof(u64
);
3811 h
->reply_pool
= pci_alloc_consistent(h
->pdev
, h
->reply_pool_size
,
3812 &(h
->reply_pool_dhandle
));
3814 /* Need a block fetch table for performant mode */
3815 h
->blockFetchTable
= kmalloc(((h
->max_sg_entries
+1) *
3816 sizeof(u32
)), GFP_KERNEL
);
3818 if ((h
->reply_pool
== NULL
)
3819 || (h
->blockFetchTable
== NULL
))
3822 h
->reply_pool_wraparound
= 1; /* spec: init to 1 */
3824 /* Controller spec: zero out this buffer. */
3825 memset(h
->reply_pool
, 0, h
->reply_pool_size
);
3826 h
->reply_pool_head
= h
->reply_pool
;
3828 trans_offset
= readl(&(h
->cfgtable
->TransMethodOffset
));
3829 bft
[7] = h
->max_sg_entries
+ 4;
3830 calc_bucket_map(bft
, ARRAY_SIZE(bft
), 32, h
->blockFetchTable
);
3831 for (i
= 0; i
< 8; i
++)
3832 writel(bft
[i
], &h
->transtable
->BlockFetch
[i
]);
3834 /* size of controller ring buffer */
3835 writel(h
->max_commands
, &h
->transtable
->RepQSize
);
3836 writel(1, &h
->transtable
->RepQCount
);
3837 writel(0, &h
->transtable
->RepQCtrAddrLow32
);
3838 writel(0, &h
->transtable
->RepQCtrAddrHigh32
);
3839 writel(h
->reply_pool_dhandle
, &h
->transtable
->RepQAddr0Low32
);
3840 writel(0, &h
->transtable
->RepQAddr0High32
);
3841 writel(CFGTBL_Trans_Performant
,
3842 &(h
->cfgtable
->HostWrite
.TransportRequest
));
3843 writel(CFGTBL_ChangeReq
, h
->vaddr
+ SA5_DOORBELL
);
3844 /* under certain very rare conditions, this can take awhile.
3845 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3846 * as we enter this code.) */
3847 for (l
= 0; l
< MAX_CONFIG_WAIT
; l
++) {
3848 register_value
= readl(h
->vaddr
+ SA5_DOORBELL
);
3849 if (!(register_value
& CFGTBL_ChangeReq
))
3851 /* delay and try again */
3852 set_current_state(TASK_INTERRUPTIBLE
);
3853 schedule_timeout(10);
3855 register_value
= readl(&(h
->cfgtable
->TransportActive
));
3856 if (!(register_value
& CFGTBL_Trans_Performant
)) {
3857 dev_warn(&h
->pdev
->dev
, "unable to get board into"
3858 " performant mode\n");
3862 /* Change the access methods to the performant access methods */
3863 h
->access
= SA5_performant_access
;
3864 h
->transMethod
= CFGTBL_Trans_Performant
;
3870 pci_free_consistent(h
->pdev
, h
->reply_pool_size
,
3871 h
->reply_pool
, h
->reply_pool_dhandle
);
3872 kfree(h
->blockFetchTable
);
3876 * This is it. Register the PCI driver information for the cards we control
3877 * the OS will call our registered routines when it finds one of our cards.
3879 static int __init
hpsa_init(void)
3882 /* Start the scan thread */
3883 hpsa_scan_thread
= kthread_run(hpsa_scan_func
, NULL
, "hpsa_scan");
3884 if (IS_ERR(hpsa_scan_thread
)) {
3885 err
= PTR_ERR(hpsa_scan_thread
);
3888 err
= pci_register_driver(&hpsa_pci_driver
);
3890 kthread_stop(hpsa_scan_thread
);
3894 static void __exit
hpsa_cleanup(void)
3896 pci_unregister_driver(&hpsa_pci_driver
);
3897 kthread_stop(hpsa_scan_thread
);
3900 module_init(hpsa_init
);
3901 module_exit(hpsa_cleanup
);