]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/hpsa.c
[SCSI] hpsa: Return DID_RESET for commands which complete with status of UNSOLICITED...
[mirror_ubuntu-eoan-kernel.git] / drivers / scsi / hpsa.c
CommitLineData
edd16368
SC
1/*
2 * Disk Array driver for HP Smart Array SAS controllers
3 * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P.
4 *
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.
8 *
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.
13 *
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.
17 *
18 * Questions/Comments/Bugfixes to iss_storagedev@hp.com
19 *
20 */
21
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>
29#include <linux/fs.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>
38#include <linux/io.h>
39#include <linux/dma-mapping.h>
40#include <linux/completion.h>
41#include <linux/moduleparam.h>
42#include <scsi/scsi.h>
43#include <scsi/scsi_cmnd.h>
44#include <scsi/scsi_device.h>
45#include <scsi/scsi_host.h>
46#include <linux/cciss_ioctl.h>
47#include <linux/string.h>
48#include <linux/bitmap.h>
49#include <asm/atomic.h>
50#include <linux/kthread.h>
51#include "hpsa_cmd.h"
52#include "hpsa.h"
53
54/* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
55#define HPSA_DRIVER_VERSION "1.0.0"
56#define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
57
58/* How long to wait (in milliseconds) for board to go into simple mode */
59#define MAX_CONFIG_WAIT 30000
60#define MAX_IOCTL_CONFIG_WAIT 1000
61
62/*define how many times we will try a command because of bus resets */
63#define MAX_CMD_RETRIES 3
64
65/* Embedded module documentation macros - see modules.h */
66MODULE_AUTHOR("Hewlett-Packard Company");
67MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
68 HPSA_DRIVER_VERSION);
69MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
70MODULE_VERSION(HPSA_DRIVER_VERSION);
71MODULE_LICENSE("GPL");
72
73static int hpsa_allow_any;
74module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
75MODULE_PARM_DESC(hpsa_allow_any,
76 "Allow hpsa driver to access unknown HP Smart Array hardware");
77
78/* define the PCI info for the cards we can control */
79static const struct pci_device_id hpsa_pci_device_id[] = {
edd16368
SC
80 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
81 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
82 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
83 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
84 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
85 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a},
86 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b},
f8b01eb9
MM
87 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
88#define PCI_DEVICE_ID_HP_CISSF 0x333f
89 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x333F},
edd16368
SC
90 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
91 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
92 {0,}
93};
94
95MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
96
97/* board_id = Subsystem Device ID & Vendor ID
98 * product = Marketing Name for the board
99 * access = Address of the struct of function pointers
100 */
101static struct board_type products[] = {
edd16368
SC
102 {0x3241103C, "Smart Array P212", &SA5_access},
103 {0x3243103C, "Smart Array P410", &SA5_access},
104 {0x3245103C, "Smart Array P410i", &SA5_access},
105 {0x3247103C, "Smart Array P411", &SA5_access},
106 {0x3249103C, "Smart Array P812", &SA5_access},
107 {0x324a103C, "Smart Array P712m", &SA5_access},
108 {0x324b103C, "Smart Array P711m", &SA5_access},
f8b01eb9
MM
109 {0x3233103C, "StorageWorks P1210m", &SA5_access},
110 {0x333F103C, "StorageWorks P1210m", &SA5_access},
edd16368
SC
111 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
112};
113
114static int number_of_controllers;
115
116static irqreturn_t do_hpsa_intr(int irq, void *dev_id);
117static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
118static void start_io(struct ctlr_info *h);
119
120#ifdef CONFIG_COMPAT
121static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
122#endif
123
124static void cmd_free(struct ctlr_info *h, struct CommandList *c);
125static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
126static struct CommandList *cmd_alloc(struct ctlr_info *h);
127static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
01a02ffc
SC
128static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
129 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
edd16368
SC
130 int cmd_type);
131
132static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
133 void (*done)(struct scsi_cmnd *));
134
135static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
136static int hpsa_slave_alloc(struct scsi_device *sdev);
137static void hpsa_slave_destroy(struct scsi_device *sdev);
138
139static ssize_t raid_level_show(struct device *dev,
140 struct device_attribute *attr, char *buf);
141static ssize_t lunid_show(struct device *dev,
142 struct device_attribute *attr, char *buf);
143static ssize_t unique_id_show(struct device *dev,
144 struct device_attribute *attr, char *buf);
145static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
146static ssize_t host_store_rescan(struct device *dev,
147 struct device_attribute *attr, const char *buf, size_t count);
148static int check_for_unit_attention(struct ctlr_info *h,
149 struct CommandList *c);
150static void check_ioctl_unit_attention(struct ctlr_info *h,
151 struct CommandList *c);
303932fd
DB
152/* performant mode helper functions */
153static void calc_bucket_map(int *bucket, int num_buckets,
154 int nsgs, int *bucket_map);
155static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
156static inline u32 next_command(struct ctlr_info *h);
edd16368
SC
157
158static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
159static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
160static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
161static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
162
163static struct device_attribute *hpsa_sdev_attrs[] = {
164 &dev_attr_raid_level,
165 &dev_attr_lunid,
166 &dev_attr_unique_id,
167 NULL,
168};
169
170static struct device_attribute *hpsa_shost_attrs[] = {
171 &dev_attr_rescan,
172 NULL,
173};
174
175static struct scsi_host_template hpsa_driver_template = {
176 .module = THIS_MODULE,
177 .name = "hpsa",
178 .proc_name = "hpsa",
179 .queuecommand = hpsa_scsi_queue_command,
edd16368
SC
180 .this_id = -1,
181 .sg_tablesize = MAXSGENTRIES,
edd16368
SC
182 .use_clustering = ENABLE_CLUSTERING,
183 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
184 .ioctl = hpsa_ioctl,
185 .slave_alloc = hpsa_slave_alloc,
186 .slave_destroy = hpsa_slave_destroy,
187#ifdef CONFIG_COMPAT
188 .compat_ioctl = hpsa_compat_ioctl,
189#endif
190 .sdev_attrs = hpsa_sdev_attrs,
191 .shost_attrs = hpsa_shost_attrs,
192};
193
194static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
195{
196 unsigned long *priv = shost_priv(sdev->host);
197 return (struct ctlr_info *) *priv;
198}
199
200static struct task_struct *hpsa_scan_thread;
201static DEFINE_MUTEX(hpsa_scan_mutex);
202static LIST_HEAD(hpsa_scan_q);
203static int hpsa_scan_func(void *data);
204
205/**
206 * add_to_scan_list() - add controller to rescan queue
207 * @h: Pointer to the controller.
208 *
209 * Adds the controller to the rescan queue if not already on the queue.
210 *
211 * returns 1 if added to the queue, 0 if skipped (could be on the
212 * queue already, or the controller could be initializing or shutting
213 * down).
214 **/
215static int add_to_scan_list(struct ctlr_info *h)
216{
217 struct ctlr_info *test_h;
218 int found = 0;
219 int ret = 0;
220
221 if (h->busy_initializing)
222 return 0;
223
224 /*
225 * If we don't get the lock, it means the driver is unloading
226 * and there's no point in scheduling a new scan.
227 */
228 if (!mutex_trylock(&h->busy_shutting_down))
229 return 0;
230
231 mutex_lock(&hpsa_scan_mutex);
232 list_for_each_entry(test_h, &hpsa_scan_q, scan_list) {
233 if (test_h == h) {
234 found = 1;
235 break;
236 }
237 }
238 if (!found && !h->busy_scanning) {
239 INIT_COMPLETION(h->scan_wait);
240 list_add_tail(&h->scan_list, &hpsa_scan_q);
241 ret = 1;
242 }
243 mutex_unlock(&hpsa_scan_mutex);
244 mutex_unlock(&h->busy_shutting_down);
245
246 return ret;
247}
248
249/**
250 * remove_from_scan_list() - remove controller from rescan queue
251 * @h: Pointer to the controller.
252 *
253 * Removes the controller from the rescan queue if present. Blocks if
254 * the controller is currently conducting a rescan. The controller
255 * can be in one of three states:
256 * 1. Doesn't need a scan
257 * 2. On the scan list, but not scanning yet (we remove it)
258 * 3. Busy scanning (and not on the list). In this case we want to wait for
259 * the scan to complete to make sure the scanning thread for this
260 * controller is completely idle.
261 **/
262static void remove_from_scan_list(struct ctlr_info *h)
263{
264 struct ctlr_info *test_h, *tmp_h;
265
266 mutex_lock(&hpsa_scan_mutex);
267 list_for_each_entry_safe(test_h, tmp_h, &hpsa_scan_q, scan_list) {
268 if (test_h == h) { /* state 2. */
269 list_del(&h->scan_list);
270 complete_all(&h->scan_wait);
271 mutex_unlock(&hpsa_scan_mutex);
272 return;
273 }
274 }
275 if (h->busy_scanning) { /* state 3. */
276 mutex_unlock(&hpsa_scan_mutex);
277 wait_for_completion(&h->scan_wait);
278 } else { /* state 1, nothing to do. */
279 mutex_unlock(&hpsa_scan_mutex);
280 }
281}
282
283/* hpsa_scan_func() - kernel thread used to rescan controllers
284 * @data: Ignored.
285 *
286 * A kernel thread used scan for drive topology changes on
287 * controllers. The thread processes only one controller at a time
288 * using a queue. Controllers are added to the queue using
289 * add_to_scan_list() and removed from the queue either after done
290 * processing or using remove_from_scan_list().
291 *
292 * returns 0.
293 **/
294static int hpsa_scan_func(__attribute__((unused)) void *data)
295{
296 struct ctlr_info *h;
297 int host_no;
298
299 while (1) {
300 set_current_state(TASK_INTERRUPTIBLE);
301 schedule();
302 if (kthread_should_stop())
303 break;
304
305 while (1) {
306 mutex_lock(&hpsa_scan_mutex);
307 if (list_empty(&hpsa_scan_q)) {
308 mutex_unlock(&hpsa_scan_mutex);
309 break;
310 }
311 h = list_entry(hpsa_scan_q.next, struct ctlr_info,
312 scan_list);
313 list_del(&h->scan_list);
314 h->busy_scanning = 1;
315 mutex_unlock(&hpsa_scan_mutex);
316 host_no = h->scsi_host ? h->scsi_host->host_no : -1;
317 hpsa_update_scsi_devices(h, host_no);
318 complete_all(&h->scan_wait);
319 mutex_lock(&hpsa_scan_mutex);
320 h->busy_scanning = 0;
321 mutex_unlock(&hpsa_scan_mutex);
322 }
323 }
324 return 0;
325}
326
327static int check_for_unit_attention(struct ctlr_info *h,
328 struct CommandList *c)
329{
330 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
331 return 0;
332
333 switch (c->err_info->SenseInfo[12]) {
334 case STATE_CHANGED:
335 dev_warn(&h->pdev->dev, "hpsa%d: a state change "
336 "detected, command retried\n", h->ctlr);
337 break;
338 case LUN_FAILED:
339 dev_warn(&h->pdev->dev, "hpsa%d: LUN failure "
340 "detected, action required\n", h->ctlr);
341 break;
342 case REPORT_LUNS_CHANGED:
343 dev_warn(&h->pdev->dev, "hpsa%d: report LUN data "
344 "changed\n", h->ctlr);
345 /*
346 * Here, we could call add_to_scan_list and wake up the scan thread,
347 * except that it's quite likely that we will get more than one
348 * REPORT_LUNS_CHANGED condition in quick succession, which means
349 * that those which occur after the first one will likely happen
350 * *during* the hpsa_scan_thread's rescan. And the rescan code is not
351 * robust enough to restart in the middle, undoing what it has already
352 * done, and it's not clear that it's even possible to do this, since
353 * part of what it does is notify the SCSI mid layer, which starts
354 * doing it's own i/o to read partition tables and so on, and the
355 * driver doesn't have visibility to know what might need undoing.
356 * In any event, if possible, it is horribly complicated to get right
357 * so we just don't do it for now.
358 *
359 * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
360 */
361 break;
362 case POWER_OR_RESET:
363 dev_warn(&h->pdev->dev, "hpsa%d: a power on "
364 "or device reset detected\n", h->ctlr);
365 break;
366 case UNIT_ATTENTION_CLEARED:
367 dev_warn(&h->pdev->dev, "hpsa%d: unit attention "
368 "cleared by another initiator\n", h->ctlr);
369 break;
370 default:
371 dev_warn(&h->pdev->dev, "hpsa%d: unknown "
372 "unit attention detected\n", h->ctlr);
373 break;
374 }
375 return 1;
376}
377
378static ssize_t host_store_rescan(struct device *dev,
379 struct device_attribute *attr,
380 const char *buf, size_t count)
381{
382 struct ctlr_info *h;
383 struct Scsi_Host *shost = class_to_shost(dev);
384 unsigned long *priv = shost_priv(shost);
385 h = (struct ctlr_info *) *priv;
386 if (add_to_scan_list(h)) {
387 wake_up_process(hpsa_scan_thread);
388 wait_for_completion_interruptible(&h->scan_wait);
389 }
390 return count;
391}
392
393/* Enqueuing and dequeuing functions for cmdlists. */
394static inline void addQ(struct hlist_head *list, struct CommandList *c)
395{
396 hlist_add_head(&c->list, list);
397}
398
303932fd
DB
399static inline u32 next_command(struct ctlr_info *h)
400{
401 u32 a;
402
403 if (unlikely(h->transMethod != CFGTBL_Trans_Performant))
404 return h->access.command_completed(h);
405
406 if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) {
407 a = *(h->reply_pool_head); /* Next cmd in ring buffer */
408 (h->reply_pool_head)++;
409 h->commands_outstanding--;
410 } else {
411 a = FIFO_EMPTY;
412 }
413 /* Check for wraparound */
414 if (h->reply_pool_head == (h->reply_pool + h->max_commands)) {
415 h->reply_pool_head = h->reply_pool;
416 h->reply_pool_wraparound ^= 1;
417 }
418 return a;
419}
420
421/* set_performant_mode: Modify the tag for cciss performant
422 * set bit 0 for pull model, bits 3-1 for block fetch
423 * register number
424 */
425static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
426{
427 if (likely(h->transMethod == CFGTBL_Trans_Performant))
428 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
429}
430
edd16368
SC
431static void enqueue_cmd_and_start_io(struct ctlr_info *h,
432 struct CommandList *c)
433{
434 unsigned long flags;
303932fd
DB
435
436 set_performant_mode(h, c);
edd16368
SC
437 spin_lock_irqsave(&h->lock, flags);
438 addQ(&h->reqQ, c);
439 h->Qdepth++;
440 start_io(h);
441 spin_unlock_irqrestore(&h->lock, flags);
442}
443
444static inline void removeQ(struct CommandList *c)
445{
446 if (WARN_ON(hlist_unhashed(&c->list)))
447 return;
448 hlist_del_init(&c->list);
449}
450
451static inline int is_hba_lunid(unsigned char scsi3addr[])
452{
453 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
454}
455
456static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
457{
458 return (scsi3addr[3] & 0xC0) == 0x40;
459}
460
339b2b14
SC
461static inline int is_scsi_rev_5(struct ctlr_info *h)
462{
463 if (!h->hba_inquiry_data)
464 return 0;
465 if ((h->hba_inquiry_data[2] & 0x07) == 5)
466 return 1;
467 return 0;
468}
469
edd16368
SC
470static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
471 "UNKNOWN"
472};
473#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
474
475static ssize_t raid_level_show(struct device *dev,
476 struct device_attribute *attr, char *buf)
477{
478 ssize_t l = 0;
82a72c0a 479 unsigned char rlevel;
edd16368
SC
480 struct ctlr_info *h;
481 struct scsi_device *sdev;
482 struct hpsa_scsi_dev_t *hdev;
483 unsigned long flags;
484
485 sdev = to_scsi_device(dev);
486 h = sdev_to_hba(sdev);
487 spin_lock_irqsave(&h->lock, flags);
488 hdev = sdev->hostdata;
489 if (!hdev) {
490 spin_unlock_irqrestore(&h->lock, flags);
491 return -ENODEV;
492 }
493
494 /* Is this even a logical drive? */
495 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
496 spin_unlock_irqrestore(&h->lock, flags);
497 l = snprintf(buf, PAGE_SIZE, "N/A\n");
498 return l;
499 }
500
501 rlevel = hdev->raid_level;
502 spin_unlock_irqrestore(&h->lock, flags);
82a72c0a 503 if (rlevel > RAID_UNKNOWN)
edd16368
SC
504 rlevel = RAID_UNKNOWN;
505 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
506 return l;
507}
508
509static ssize_t lunid_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
512 struct ctlr_info *h;
513 struct scsi_device *sdev;
514 struct hpsa_scsi_dev_t *hdev;
515 unsigned long flags;
516 unsigned char lunid[8];
517
518 sdev = to_scsi_device(dev);
519 h = sdev_to_hba(sdev);
520 spin_lock_irqsave(&h->lock, flags);
521 hdev = sdev->hostdata;
522 if (!hdev) {
523 spin_unlock_irqrestore(&h->lock, flags);
524 return -ENODEV;
525 }
526 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
527 spin_unlock_irqrestore(&h->lock, flags);
528 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
529 lunid[0], lunid[1], lunid[2], lunid[3],
530 lunid[4], lunid[5], lunid[6], lunid[7]);
531}
532
533static ssize_t unique_id_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
535{
536 struct ctlr_info *h;
537 struct scsi_device *sdev;
538 struct hpsa_scsi_dev_t *hdev;
539 unsigned long flags;
540 unsigned char sn[16];
541
542 sdev = to_scsi_device(dev);
543 h = sdev_to_hba(sdev);
544 spin_lock_irqsave(&h->lock, flags);
545 hdev = sdev->hostdata;
546 if (!hdev) {
547 spin_unlock_irqrestore(&h->lock, flags);
548 return -ENODEV;
549 }
550 memcpy(sn, hdev->device_id, sizeof(sn));
551 spin_unlock_irqrestore(&h->lock, flags);
552 return snprintf(buf, 16 * 2 + 2,
553 "%02X%02X%02X%02X%02X%02X%02X%02X"
554 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
555 sn[0], sn[1], sn[2], sn[3],
556 sn[4], sn[5], sn[6], sn[7],
557 sn[8], sn[9], sn[10], sn[11],
558 sn[12], sn[13], sn[14], sn[15]);
559}
560
561static int hpsa_find_target_lun(struct ctlr_info *h,
562 unsigned char scsi3addr[], int bus, int *target, int *lun)
563{
564 /* finds an unused bus, target, lun for a new physical device
565 * assumes h->devlock is held
566 */
567 int i, found = 0;
568 DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA);
569
570 memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3);
571
572 for (i = 0; i < h->ndevices; i++) {
573 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
574 set_bit(h->dev[i]->target, lun_taken);
575 }
576
577 for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) {
578 if (!test_bit(i, lun_taken)) {
579 /* *bus = 1; */
580 *target = i;
581 *lun = 0;
582 found = 1;
583 break;
584 }
585 }
586 return !found;
587}
588
589/* Add an entry into h->dev[] array. */
590static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
591 struct hpsa_scsi_dev_t *device,
592 struct hpsa_scsi_dev_t *added[], int *nadded)
593{
594 /* assumes h->devlock is held */
595 int n = h->ndevices;
596 int i;
597 unsigned char addr1[8], addr2[8];
598 struct hpsa_scsi_dev_t *sd;
599
600 if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) {
601 dev_err(&h->pdev->dev, "too many devices, some will be "
602 "inaccessible.\n");
603 return -1;
604 }
605
606 /* physical devices do not have lun or target assigned until now. */
607 if (device->lun != -1)
608 /* Logical device, lun is already assigned. */
609 goto lun_assigned;
610
611 /* If this device a non-zero lun of a multi-lun device
612 * byte 4 of the 8-byte LUN addr will contain the logical
613 * unit no, zero otherise.
614 */
615 if (device->scsi3addr[4] == 0) {
616 /* This is not a non-zero lun of a multi-lun device */
617 if (hpsa_find_target_lun(h, device->scsi3addr,
618 device->bus, &device->target, &device->lun) != 0)
619 return -1;
620 goto lun_assigned;
621 }
622
623 /* This is a non-zero lun of a multi-lun device.
624 * Search through our list and find the device which
625 * has the same 8 byte LUN address, excepting byte 4.
626 * Assign the same bus and target for this new LUN.
627 * Use the logical unit number from the firmware.
628 */
629 memcpy(addr1, device->scsi3addr, 8);
630 addr1[4] = 0;
631 for (i = 0; i < n; i++) {
632 sd = h->dev[i];
633 memcpy(addr2, sd->scsi3addr, 8);
634 addr2[4] = 0;
635 /* differ only in byte 4? */
636 if (memcmp(addr1, addr2, 8) == 0) {
637 device->bus = sd->bus;
638 device->target = sd->target;
639 device->lun = device->scsi3addr[4];
640 break;
641 }
642 }
643 if (device->lun == -1) {
644 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
645 " suspect firmware bug or unsupported hardware "
646 "configuration.\n");
647 return -1;
648 }
649
650lun_assigned:
651
652 h->dev[n] = device;
653 h->ndevices++;
654 added[*nadded] = device;
655 (*nadded)++;
656
657 /* initially, (before registering with scsi layer) we don't
658 * know our hostno and we don't want to print anything first
659 * time anyway (the scsi layer's inquiries will show that info)
660 */
661 /* if (hostno != -1) */
662 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
663 scsi_device_type(device->devtype), hostno,
664 device->bus, device->target, device->lun);
665 return 0;
666}
667
668/* Remove an entry from h->dev[] array. */
669static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
670 struct hpsa_scsi_dev_t *removed[], int *nremoved)
671{
672 /* assumes h->devlock is held */
673 int i;
674 struct hpsa_scsi_dev_t *sd;
675
b2ed4f79 676 BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
edd16368
SC
677
678 sd = h->dev[entry];
679 removed[*nremoved] = h->dev[entry];
680 (*nremoved)++;
681
682 for (i = entry; i < h->ndevices-1; i++)
683 h->dev[i] = h->dev[i+1];
684 h->ndevices--;
685 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
686 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
687 sd->lun);
688}
689
690#define SCSI3ADDR_EQ(a, b) ( \
691 (a)[7] == (b)[7] && \
692 (a)[6] == (b)[6] && \
693 (a)[5] == (b)[5] && \
694 (a)[4] == (b)[4] && \
695 (a)[3] == (b)[3] && \
696 (a)[2] == (b)[2] && \
697 (a)[1] == (b)[1] && \
698 (a)[0] == (b)[0])
699
700static void fixup_botched_add(struct ctlr_info *h,
701 struct hpsa_scsi_dev_t *added)
702{
703 /* called when scsi_add_device fails in order to re-adjust
704 * h->dev[] to match the mid layer's view.
705 */
706 unsigned long flags;
707 int i, j;
708
709 spin_lock_irqsave(&h->lock, flags);
710 for (i = 0; i < h->ndevices; i++) {
711 if (h->dev[i] == added) {
712 for (j = i; j < h->ndevices-1; j++)
713 h->dev[j] = h->dev[j+1];
714 h->ndevices--;
715 break;
716 }
717 }
718 spin_unlock_irqrestore(&h->lock, flags);
719 kfree(added);
720}
721
722static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
723 struct hpsa_scsi_dev_t *dev2)
724{
725 if ((is_logical_dev_addr_mode(dev1->scsi3addr) ||
726 (dev1->lun != -1 && dev2->lun != -1)) &&
727 dev1->devtype != 0x0C)
728 return (memcmp(dev1, dev2, sizeof(*dev1)) == 0);
729
730 /* we compare everything except lun and target as these
731 * are not yet assigned. Compare parts likely
732 * to differ first
733 */
734 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
735 sizeof(dev1->scsi3addr)) != 0)
736 return 0;
737 if (memcmp(dev1->device_id, dev2->device_id,
738 sizeof(dev1->device_id)) != 0)
739 return 0;
740 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
741 return 0;
742 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
743 return 0;
744 if (memcmp(dev1->revision, dev2->revision, sizeof(dev1->revision)) != 0)
745 return 0;
746 if (dev1->devtype != dev2->devtype)
747 return 0;
748 if (dev1->raid_level != dev2->raid_level)
749 return 0;
750 if (dev1->bus != dev2->bus)
751 return 0;
752 return 1;
753}
754
755/* Find needle in haystack. If exact match found, return DEVICE_SAME,
756 * and return needle location in *index. If scsi3addr matches, but not
757 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
758 * location in *index. If needle not found, return DEVICE_NOT_FOUND.
759 */
760static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
761 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
762 int *index)
763{
764 int i;
765#define DEVICE_NOT_FOUND 0
766#define DEVICE_CHANGED 1
767#define DEVICE_SAME 2
768 for (i = 0; i < haystack_size; i++) {
769 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
770 *index = i;
771 if (device_is_the_same(needle, haystack[i]))
772 return DEVICE_SAME;
773 else
774 return DEVICE_CHANGED;
775 }
776 }
777 *index = -1;
778 return DEVICE_NOT_FOUND;
779}
780
4967bd3e 781static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
edd16368
SC
782 struct hpsa_scsi_dev_t *sd[], int nsds)
783{
784 /* sd contains scsi3 addresses and devtypes, and inquiry
785 * data. This function takes what's in sd to be the current
786 * reality and updates h->dev[] to reflect that reality.
787 */
788 int i, entry, device_change, changes = 0;
789 struct hpsa_scsi_dev_t *csd;
790 unsigned long flags;
791 struct hpsa_scsi_dev_t **added, **removed;
792 int nadded, nremoved;
793 struct Scsi_Host *sh = NULL;
794
795 added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA,
796 GFP_KERNEL);
797 removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA,
798 GFP_KERNEL);
799
800 if (!added || !removed) {
801 dev_warn(&h->pdev->dev, "out of memory in "
802 "adjust_hpsa_scsi_table\n");
803 goto free_and_out;
804 }
805
806 spin_lock_irqsave(&h->devlock, flags);
807
808 /* find any devices in h->dev[] that are not in
809 * sd[] and remove them from h->dev[], and for any
810 * devices which have changed, remove the old device
811 * info and add the new device info.
812 */
813 i = 0;
814 nremoved = 0;
815 nadded = 0;
816 while (i < h->ndevices) {
817 csd = h->dev[i];
818 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
819 if (device_change == DEVICE_NOT_FOUND) {
820 changes++;
821 hpsa_scsi_remove_entry(h, hostno, i,
822 removed, &nremoved);
823 continue; /* remove ^^^, hence i not incremented */
824 } else if (device_change == DEVICE_CHANGED) {
825 changes++;
826 hpsa_scsi_remove_entry(h, hostno, i,
827 removed, &nremoved);
828 (void) hpsa_scsi_add_entry(h, hostno, sd[entry],
829 added, &nadded);
830 /* add can't fail, we just removed one. */
831 sd[entry] = NULL; /* prevent it from being freed */
832 }
833 i++;
834 }
835
836 /* Now, make sure every device listed in sd[] is also
837 * listed in h->dev[], adding them if they aren't found
838 */
839
840 for (i = 0; i < nsds; i++) {
841 if (!sd[i]) /* if already added above. */
842 continue;
843 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
844 h->ndevices, &entry);
845 if (device_change == DEVICE_NOT_FOUND) {
846 changes++;
847 if (hpsa_scsi_add_entry(h, hostno, sd[i],
848 added, &nadded) != 0)
849 break;
850 sd[i] = NULL; /* prevent from being freed later. */
851 } else if (device_change == DEVICE_CHANGED) {
852 /* should never happen... */
853 changes++;
854 dev_warn(&h->pdev->dev,
855 "device unexpectedly changed.\n");
856 /* but if it does happen, we just ignore that device */
857 }
858 }
859 spin_unlock_irqrestore(&h->devlock, flags);
860
861 /* Don't notify scsi mid layer of any changes the first time through
862 * (or if there are no changes) scsi_scan_host will do it later the
863 * first time through.
864 */
865 if (hostno == -1 || !changes)
866 goto free_and_out;
867
868 sh = h->scsi_host;
869 /* Notify scsi mid layer of any removed devices */
870 for (i = 0; i < nremoved; i++) {
871 struct scsi_device *sdev =
872 scsi_device_lookup(sh, removed[i]->bus,
873 removed[i]->target, removed[i]->lun);
874 if (sdev != NULL) {
875 scsi_remove_device(sdev);
876 scsi_device_put(sdev);
877 } else {
878 /* We don't expect to get here.
879 * future cmds to this device will get selection
880 * timeout as if the device was gone.
881 */
882 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
883 " for removal.", hostno, removed[i]->bus,
884 removed[i]->target, removed[i]->lun);
885 }
886 kfree(removed[i]);
887 removed[i] = NULL;
888 }
889
890 /* Notify scsi mid layer of any added devices */
891 for (i = 0; i < nadded; i++) {
892 if (scsi_add_device(sh, added[i]->bus,
893 added[i]->target, added[i]->lun) == 0)
894 continue;
895 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
896 "device not added.\n", hostno, added[i]->bus,
897 added[i]->target, added[i]->lun);
898 /* now we have to remove it from h->dev,
899 * since it didn't get added to scsi mid layer
900 */
901 fixup_botched_add(h, added[i]);
902 }
903
904free_and_out:
905 kfree(added);
906 kfree(removed);
edd16368
SC
907}
908
909/*
910 * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
911 * Assume's h->devlock is held.
912 */
913static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
914 int bus, int target, int lun)
915{
916 int i;
917 struct hpsa_scsi_dev_t *sd;
918
919 for (i = 0; i < h->ndevices; i++) {
920 sd = h->dev[i];
921 if (sd->bus == bus && sd->target == target && sd->lun == lun)
922 return sd;
923 }
924 return NULL;
925}
926
927/* link sdev->hostdata to our per-device structure. */
928static int hpsa_slave_alloc(struct scsi_device *sdev)
929{
930 struct hpsa_scsi_dev_t *sd;
931 unsigned long flags;
932 struct ctlr_info *h;
933
934 h = sdev_to_hba(sdev);
935 spin_lock_irqsave(&h->devlock, flags);
936 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
937 sdev_id(sdev), sdev->lun);
938 if (sd != NULL)
939 sdev->hostdata = sd;
940 spin_unlock_irqrestore(&h->devlock, flags);
941 return 0;
942}
943
944static void hpsa_slave_destroy(struct scsi_device *sdev)
945{
bcc44255 946 /* nothing to do. */
edd16368
SC
947}
948
949static void hpsa_scsi_setup(struct ctlr_info *h)
950{
951 h->ndevices = 0;
952 h->scsi_host = NULL;
953 spin_lock_init(&h->devlock);
edd16368
SC
954}
955
956static void complete_scsi_command(struct CommandList *cp,
01a02ffc 957 int timeout, u32 tag)
edd16368
SC
958{
959 struct scsi_cmnd *cmd;
960 struct ctlr_info *h;
961 struct ErrorInfo *ei;
962
963 unsigned char sense_key;
964 unsigned char asc; /* additional sense code */
965 unsigned char ascq; /* additional sense code qualifier */
966
967 ei = cp->err_info;
968 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
969 h = cp->h;
970
971 scsi_dma_unmap(cmd); /* undo the DMA mappings */
972
973 cmd->result = (DID_OK << 16); /* host byte */
974 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
975 cmd->result |= (ei->ScsiStatus << 1);
976
977 /* copy the sense data whether we need to or not. */
978 memcpy(cmd->sense_buffer, ei->SenseInfo,
979 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
980 SCSI_SENSE_BUFFERSIZE :
981 ei->SenseLen);
982 scsi_set_resid(cmd, ei->ResidualCnt);
983
984 if (ei->CommandStatus == 0) {
985 cmd->scsi_done(cmd);
986 cmd_free(h, cp);
987 return;
988 }
989
990 /* an error has occurred */
991 switch (ei->CommandStatus) {
992
993 case CMD_TARGET_STATUS:
994 if (ei->ScsiStatus) {
995 /* Get sense key */
996 sense_key = 0xf & ei->SenseInfo[2];
997 /* Get additional sense code */
998 asc = ei->SenseInfo[12];
999 /* Get addition sense code qualifier */
1000 ascq = ei->SenseInfo[13];
1001 }
1002
1003 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
1004 if (check_for_unit_attention(h, cp)) {
1005 cmd->result = DID_SOFT_ERROR << 16;
1006 break;
1007 }
1008 if (sense_key == ILLEGAL_REQUEST) {
1009 /*
1010 * SCSI REPORT_LUNS is commonly unsupported on
1011 * Smart Array. Suppress noisy complaint.
1012 */
1013 if (cp->Request.CDB[0] == REPORT_LUNS)
1014 break;
1015
1016 /* If ASC/ASCQ indicate Logical Unit
1017 * Not Supported condition,
1018 */
1019 if ((asc == 0x25) && (ascq == 0x0)) {
1020 dev_warn(&h->pdev->dev, "cp %p "
1021 "has check condition\n", cp);
1022 break;
1023 }
1024 }
1025
1026 if (sense_key == NOT_READY) {
1027 /* If Sense is Not Ready, Logical Unit
1028 * Not ready, Manual Intervention
1029 * required
1030 */
1031 if ((asc == 0x04) && (ascq == 0x03)) {
1032 cmd->result = DID_NO_CONNECT << 16;
1033 dev_warn(&h->pdev->dev, "cp %p "
1034 "has check condition: unit "
1035 "not ready, manual "
1036 "intervention required\n", cp);
1037 break;
1038 }
1039 }
1040
1041
1042 /* Must be some other type of check condition */
1043 dev_warn(&h->pdev->dev, "cp %p has check condition: "
1044 "unknown type: "
1045 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1046 "Returning result: 0x%x, "
1047 "cmd=[%02x %02x %02x %02x %02x "
1048 "%02x %02x %02x %02x %02x]\n",
1049 cp, sense_key, asc, ascq,
1050 cmd->result,
1051 cmd->cmnd[0], cmd->cmnd[1],
1052 cmd->cmnd[2], cmd->cmnd[3],
1053 cmd->cmnd[4], cmd->cmnd[5],
1054 cmd->cmnd[6], cmd->cmnd[7],
1055 cmd->cmnd[8], cmd->cmnd[9]);
1056 break;
1057 }
1058
1059
1060 /* Problem was not a check condition
1061 * Pass it up to the upper layers...
1062 */
1063 if (ei->ScsiStatus) {
1064 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1065 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1066 "Returning result: 0x%x\n",
1067 cp, ei->ScsiStatus,
1068 sense_key, asc, ascq,
1069 cmd->result);
1070 } else { /* scsi status is zero??? How??? */
1071 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1072 "Returning no connection.\n", cp),
1073
1074 /* Ordinarily, this case should never happen,
1075 * but there is a bug in some released firmware
1076 * revisions that allows it to happen if, for
1077 * example, a 4100 backplane loses power and
1078 * the tape drive is in it. We assume that
1079 * it's a fatal error of some kind because we
1080 * can't show that it wasn't. We will make it
1081 * look like selection timeout since that is
1082 * the most common reason for this to occur,
1083 * and it's severe enough.
1084 */
1085
1086 cmd->result = DID_NO_CONNECT << 16;
1087 }
1088 break;
1089
1090 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1091 break;
1092 case CMD_DATA_OVERRUN:
1093 dev_warn(&h->pdev->dev, "cp %p has"
1094 " completed with data overrun "
1095 "reported\n", cp);
1096 break;
1097 case CMD_INVALID: {
1098 /* print_bytes(cp, sizeof(*cp), 1, 0);
1099 print_cmd(cp); */
1100 /* We get CMD_INVALID if you address a non-existent device
1101 * instead of a selection timeout (no response). You will
1102 * see this if you yank out a drive, then try to access it.
1103 * This is kind of a shame because it means that any other
1104 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1105 * missing target. */
1106 cmd->result = DID_NO_CONNECT << 16;
1107 }
1108 break;
1109 case CMD_PROTOCOL_ERR:
1110 dev_warn(&h->pdev->dev, "cp %p has "
1111 "protocol error \n", cp);
1112 break;
1113 case CMD_HARDWARE_ERR:
1114 cmd->result = DID_ERROR << 16;
1115 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1116 break;
1117 case CMD_CONNECTION_LOST:
1118 cmd->result = DID_ERROR << 16;
1119 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1120 break;
1121 case CMD_ABORTED:
1122 cmd->result = DID_ABORT << 16;
1123 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1124 cp, ei->ScsiStatus);
1125 break;
1126 case CMD_ABORT_FAILED:
1127 cmd->result = DID_ERROR << 16;
1128 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1129 break;
1130 case CMD_UNSOLICITED_ABORT:
5f0325ab 1131 cmd->result = DID_RESET << 16;
edd16368
SC
1132 dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited "
1133 "abort\n", cp);
1134 break;
1135 case CMD_TIMEOUT:
1136 cmd->result = DID_TIME_OUT << 16;
1137 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1138 break;
1139 default:
1140 cmd->result = DID_ERROR << 16;
1141 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1142 cp, ei->CommandStatus);
1143 }
1144 cmd->scsi_done(cmd);
1145 cmd_free(h, cp);
1146}
1147
1148static int hpsa_scsi_detect(struct ctlr_info *h)
1149{
1150 struct Scsi_Host *sh;
1151 int error;
1152
1153 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
1154 if (sh == NULL)
1155 goto fail;
1156
1157 sh->io_port = 0;
1158 sh->n_io_port = 0;
1159 sh->this_id = -1;
1160 sh->max_channel = 3;
1161 sh->max_cmd_len = MAX_COMMAND_SIZE;
1162 sh->max_lun = HPSA_MAX_LUN;
1163 sh->max_id = HPSA_MAX_LUN;
303932fd
DB
1164 sh->can_queue = h->nr_cmds;
1165 sh->cmd_per_lun = h->nr_cmds;
edd16368
SC
1166 h->scsi_host = sh;
1167 sh->hostdata[0] = (unsigned long) h;
303932fd 1168 sh->irq = h->intr[PERF_MODE_INT];
edd16368
SC
1169 sh->unique_id = sh->irq;
1170 error = scsi_add_host(sh, &h->pdev->dev);
1171 if (error)
1172 goto fail_host_put;
1173 scsi_scan_host(sh);
1174 return 0;
1175
1176 fail_host_put:
1177 dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host"
1178 " failed for controller %d\n", h->ctlr);
1179 scsi_host_put(sh);
ecd9aad4 1180 return error;
edd16368
SC
1181 fail:
1182 dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc"
1183 " failed for controller %d\n", h->ctlr);
ecd9aad4 1184 return -ENOMEM;
edd16368
SC
1185}
1186
1187static void hpsa_pci_unmap(struct pci_dev *pdev,
1188 struct CommandList *c, int sg_used, int data_direction)
1189{
1190 int i;
1191 union u64bit addr64;
1192
1193 for (i = 0; i < sg_used; i++) {
1194 addr64.val32.lower = c->SG[i].Addr.lower;
1195 addr64.val32.upper = c->SG[i].Addr.upper;
1196 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1197 data_direction);
1198 }
1199}
1200
1201static void hpsa_map_one(struct pci_dev *pdev,
1202 struct CommandList *cp,
1203 unsigned char *buf,
1204 size_t buflen,
1205 int data_direction)
1206{
01a02ffc 1207 u64 addr64;
edd16368
SC
1208
1209 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1210 cp->Header.SGList = 0;
1211 cp->Header.SGTotal = 0;
1212 return;
1213 }
1214
01a02ffc 1215 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
edd16368 1216 cp->SG[0].Addr.lower =
01a02ffc 1217 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
edd16368 1218 cp->SG[0].Addr.upper =
01a02ffc 1219 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
edd16368 1220 cp->SG[0].Len = buflen;
01a02ffc
SC
1221 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1222 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
edd16368
SC
1223}
1224
1225static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1226 struct CommandList *c)
1227{
1228 DECLARE_COMPLETION_ONSTACK(wait);
1229
1230 c->waiting = &wait;
1231 enqueue_cmd_and_start_io(h, c);
1232 wait_for_completion(&wait);
1233}
1234
1235static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1236 struct CommandList *c, int data_direction)
1237{
1238 int retry_count = 0;
1239
1240 do {
1241 memset(c->err_info, 0, sizeof(c->err_info));
1242 hpsa_scsi_do_simple_cmd_core(h, c);
1243 retry_count++;
1244 } while (check_for_unit_attention(h, c) && retry_count <= 3);
1245 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1246}
1247
1248static void hpsa_scsi_interpret_error(struct CommandList *cp)
1249{
1250 struct ErrorInfo *ei;
1251 struct device *d = &cp->h->pdev->dev;
1252
1253 ei = cp->err_info;
1254 switch (ei->CommandStatus) {
1255 case CMD_TARGET_STATUS:
1256 dev_warn(d, "cmd %p has completed with errors\n", cp);
1257 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1258 ei->ScsiStatus);
1259 if (ei->ScsiStatus == 0)
1260 dev_warn(d, "SCSI status is abnormally zero. "
1261 "(probably indicates selection timeout "
1262 "reported incorrectly due to a known "
1263 "firmware bug, circa July, 2001.)\n");
1264 break;
1265 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1266 dev_info(d, "UNDERRUN\n");
1267 break;
1268 case CMD_DATA_OVERRUN:
1269 dev_warn(d, "cp %p has completed with data overrun\n", cp);
1270 break;
1271 case CMD_INVALID: {
1272 /* controller unfortunately reports SCSI passthru's
1273 * to non-existent targets as invalid commands.
1274 */
1275 dev_warn(d, "cp %p is reported invalid (probably means "
1276 "target device no longer present)\n", cp);
1277 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1278 print_cmd(cp); */
1279 }
1280 break;
1281 case CMD_PROTOCOL_ERR:
1282 dev_warn(d, "cp %p has protocol error \n", cp);
1283 break;
1284 case CMD_HARDWARE_ERR:
1285 /* cmd->result = DID_ERROR << 16; */
1286 dev_warn(d, "cp %p had hardware error\n", cp);
1287 break;
1288 case CMD_CONNECTION_LOST:
1289 dev_warn(d, "cp %p had connection lost\n", cp);
1290 break;
1291 case CMD_ABORTED:
1292 dev_warn(d, "cp %p was aborted\n", cp);
1293 break;
1294 case CMD_ABORT_FAILED:
1295 dev_warn(d, "cp %p reports abort failed\n", cp);
1296 break;
1297 case CMD_UNSOLICITED_ABORT:
1298 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1299 break;
1300 case CMD_TIMEOUT:
1301 dev_warn(d, "cp %p timed out\n", cp);
1302 break;
1303 default:
1304 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1305 ei->CommandStatus);
1306 }
1307}
1308
1309static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1310 unsigned char page, unsigned char *buf,
1311 unsigned char bufsize)
1312{
1313 int rc = IO_OK;
1314 struct CommandList *c;
1315 struct ErrorInfo *ei;
1316
1317 c = cmd_special_alloc(h);
1318
1319 if (c == NULL) { /* trouble... */
1320 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
ecd9aad4 1321 return -ENOMEM;
edd16368
SC
1322 }
1323
1324 fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD);
1325 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1326 ei = c->err_info;
1327 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1328 hpsa_scsi_interpret_error(c);
1329 rc = -1;
1330 }
1331 cmd_special_free(h, c);
1332 return rc;
1333}
1334
1335static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
1336{
1337 int rc = IO_OK;
1338 struct CommandList *c;
1339 struct ErrorInfo *ei;
1340
1341 c = cmd_special_alloc(h);
1342
1343 if (c == NULL) { /* trouble... */
1344 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1345 return -1;
1346 }
1347
1348 fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG);
1349 hpsa_scsi_do_simple_cmd_core(h, c);
1350 /* no unmap needed here because no data xfer. */
1351
1352 ei = c->err_info;
1353 if (ei->CommandStatus != 0) {
1354 hpsa_scsi_interpret_error(c);
1355 rc = -1;
1356 }
1357 cmd_special_free(h, c);
1358 return rc;
1359}
1360
1361static void hpsa_get_raid_level(struct ctlr_info *h,
1362 unsigned char *scsi3addr, unsigned char *raid_level)
1363{
1364 int rc;
1365 unsigned char *buf;
1366
1367 *raid_level = RAID_UNKNOWN;
1368 buf = kzalloc(64, GFP_KERNEL);
1369 if (!buf)
1370 return;
1371 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1372 if (rc == 0)
1373 *raid_level = buf[8];
1374 if (*raid_level > RAID_UNKNOWN)
1375 *raid_level = RAID_UNKNOWN;
1376 kfree(buf);
1377 return;
1378}
1379
1380/* Get the device id from inquiry page 0x83 */
1381static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
1382 unsigned char *device_id, int buflen)
1383{
1384 int rc;
1385 unsigned char *buf;
1386
1387 if (buflen > 16)
1388 buflen = 16;
1389 buf = kzalloc(64, GFP_KERNEL);
1390 if (!buf)
1391 return -1;
1392 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1393 if (rc == 0)
1394 memcpy(device_id, &buf[8], buflen);
1395 kfree(buf);
1396 return rc != 0;
1397}
1398
1399static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
1400 struct ReportLUNdata *buf, int bufsize,
1401 int extended_response)
1402{
1403 int rc = IO_OK;
1404 struct CommandList *c;
1405 unsigned char scsi3addr[8];
1406 struct ErrorInfo *ei;
1407
1408 c = cmd_special_alloc(h);
1409 if (c == NULL) { /* trouble... */
1410 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1411 return -1;
1412 }
e89c0ae7
SC
1413 /* address the controller */
1414 memset(scsi3addr, 0, sizeof(scsi3addr));
edd16368
SC
1415 fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
1416 buf, bufsize, 0, scsi3addr, TYPE_CMD);
1417 if (extended_response)
1418 c->Request.CDB[1] = extended_response;
1419 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1420 ei = c->err_info;
1421 if (ei->CommandStatus != 0 &&
1422 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1423 hpsa_scsi_interpret_error(c);
1424 rc = -1;
1425 }
1426 cmd_special_free(h, c);
1427 return rc;
1428}
1429
1430static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
1431 struct ReportLUNdata *buf,
1432 int bufsize, int extended_response)
1433{
1434 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
1435}
1436
1437static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
1438 struct ReportLUNdata *buf, int bufsize)
1439{
1440 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
1441}
1442
1443static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
1444 int bus, int target, int lun)
1445{
1446 device->bus = bus;
1447 device->target = target;
1448 device->lun = lun;
1449}
1450
1451static int hpsa_update_device_info(struct ctlr_info *h,
1452 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device)
1453{
1454#define OBDR_TAPE_INQ_SIZE 49
ea6d3bc3 1455 unsigned char *inq_buff;
edd16368 1456
ea6d3bc3 1457 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
edd16368
SC
1458 if (!inq_buff)
1459 goto bail_out;
1460
edd16368
SC
1461 /* Do an inquiry to the device to see what it is. */
1462 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1463 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1464 /* Inquiry failed (msg printed already) */
1465 dev_err(&h->pdev->dev,
1466 "hpsa_update_device_info: inquiry failed\n");
1467 goto bail_out;
1468 }
1469
1470 /* As a side effect, record the firmware version number
1471 * if we happen to be talking to the RAID controller.
1472 */
1473 if (is_hba_lunid(scsi3addr))
1474 memcpy(h->firm_ver, &inq_buff[32], 4);
1475
1476 this_device->devtype = (inq_buff[0] & 0x1f);
1477 memcpy(this_device->scsi3addr, scsi3addr, 8);
1478 memcpy(this_device->vendor, &inq_buff[8],
1479 sizeof(this_device->vendor));
1480 memcpy(this_device->model, &inq_buff[16],
1481 sizeof(this_device->model));
1482 memcpy(this_device->revision, &inq_buff[32],
1483 sizeof(this_device->revision));
1484 memset(this_device->device_id, 0,
1485 sizeof(this_device->device_id));
1486 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
1487 sizeof(this_device->device_id));
1488
1489 if (this_device->devtype == TYPE_DISK &&
1490 is_logical_dev_addr_mode(scsi3addr))
1491 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
1492 else
1493 this_device->raid_level = RAID_UNKNOWN;
1494
1495 kfree(inq_buff);
1496 return 0;
1497
1498bail_out:
1499 kfree(inq_buff);
1500 return 1;
1501}
1502
1503static unsigned char *msa2xxx_model[] = {
1504 "MSA2012",
1505 "MSA2024",
1506 "MSA2312",
1507 "MSA2324",
1508 NULL,
1509};
1510
1511static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1512{
1513 int i;
1514
1515 for (i = 0; msa2xxx_model[i]; i++)
1516 if (strncmp(device->model, msa2xxx_model[i],
1517 strlen(msa2xxx_model[i])) == 0)
1518 return 1;
1519 return 0;
1520}
1521
1522/* Helper function to assign bus, target, lun mapping of devices.
1523 * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
1524 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
1525 * Logical drive target and lun are assigned at this time, but
1526 * physical device lun and target assignment are deferred (assigned
1527 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
1528 */
1529static void figure_bus_target_lun(struct ctlr_info *h,
01a02ffc 1530 u8 *lunaddrbytes, int *bus, int *target, int *lun,
edd16368
SC
1531 struct hpsa_scsi_dev_t *device)
1532{
01a02ffc 1533 u32 lunid;
edd16368
SC
1534
1535 if (is_logical_dev_addr_mode(lunaddrbytes)) {
1536 /* logical device */
339b2b14
SC
1537 if (unlikely(is_scsi_rev_5(h))) {
1538 /* p1210m, logical drives lun assignments
1539 * match SCSI REPORT LUNS data.
1540 */
1541 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
edd16368 1542 *bus = 0;
339b2b14
SC
1543 *target = 0;
1544 *lun = (lunid & 0x3fff) + 1;
1545 } else {
1546 /* not p1210m... */
1547 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
1548 if (is_msa2xxx(h, device)) {
1549 /* msa2xxx way, put logicals on bus 1
1550 * and match target/lun numbers box
1551 * reports.
1552 */
1553 *bus = 1;
1554 *target = (lunid >> 16) & 0x3fff;
1555 *lun = lunid & 0x00ff;
1556 } else {
1557 /* Traditional smart array way. */
1558 *bus = 0;
1559 *lun = 0;
1560 *target = lunid & 0x3fff;
1561 }
edd16368
SC
1562 }
1563 } else {
1564 /* physical device */
1565 if (is_hba_lunid(lunaddrbytes))
339b2b14
SC
1566 if (unlikely(is_scsi_rev_5(h))) {
1567 *bus = 0; /* put p1210m ctlr at 0,0,0 */
1568 *target = 0;
1569 *lun = 0;
1570 return;
1571 } else
1572 *bus = 3; /* traditional smartarray */
edd16368 1573 else
339b2b14 1574 *bus = 2; /* physical disk */
edd16368
SC
1575 *target = -1;
1576 *lun = -1; /* we will fill these in later. */
1577 }
1578}
1579
1580/*
1581 * If there is no lun 0 on a target, linux won't find any devices.
1582 * For the MSA2xxx boxes, we have to manually detect the enclosure
1583 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
1584 * it for some reason. *tmpdevice is the target we're adding,
1585 * this_device is a pointer into the current element of currentsd[]
1586 * that we're building up in update_scsi_devices(), below.
1587 * lunzerobits is a bitmap that tracks which targets already have a
1588 * lun 0 assigned.
1589 * Returns 1 if an enclosure was added, 0 if not.
1590 */
1591static int add_msa2xxx_enclosure_device(struct ctlr_info *h,
1592 struct hpsa_scsi_dev_t *tmpdevice,
01a02ffc 1593 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
edd16368
SC
1594 int bus, int target, int lun, unsigned long lunzerobits[],
1595 int *nmsa2xxx_enclosures)
1596{
1597 unsigned char scsi3addr[8];
1598
1599 if (test_bit(target, lunzerobits))
1600 return 0; /* There is already a lun 0 on this target. */
1601
1602 if (!is_logical_dev_addr_mode(lunaddrbytes))
1603 return 0; /* It's the logical targets that may lack lun 0. */
1604
1605 if (!is_msa2xxx(h, tmpdevice))
1606 return 0; /* It's only the MSA2xxx that have this problem. */
1607
1608 if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */
1609 return 0;
1610
1611 if (is_hba_lunid(scsi3addr))
1612 return 0; /* Don't add the RAID controller here. */
1613
339b2b14
SC
1614 if (is_scsi_rev_5(h))
1615 return 0; /* p1210m doesn't need to do this. */
1616
edd16368
SC
1617#define MAX_MSA2XXX_ENCLOSURES 32
1618 if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) {
1619 dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX "
1620 "enclosures exceeded. Check your hardware "
1621 "configuration.");
1622 return 0;
1623 }
1624
1625 memset(scsi3addr, 0, 8);
1626 scsi3addr[3] = target;
1627 if (hpsa_update_device_info(h, scsi3addr, this_device))
1628 return 0;
1629 (*nmsa2xxx_enclosures)++;
1630 hpsa_set_bus_target_lun(this_device, bus, target, 0);
1631 set_bit(target, lunzerobits);
1632 return 1;
1633}
1634
1635/*
1636 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
1637 * logdev. The number of luns in physdev and logdev are returned in
1638 * *nphysicals and *nlogicals, respectively.
1639 * Returns 0 on success, -1 otherwise.
1640 */
1641static int hpsa_gather_lun_info(struct ctlr_info *h,
1642 int reportlunsize,
01a02ffc
SC
1643 struct ReportLUNdata *physdev, u32 *nphysicals,
1644 struct ReportLUNdata *logdev, u32 *nlogicals)
edd16368
SC
1645{
1646 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
1647 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
1648 return -1;
1649 }
6df1e954 1650 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8;
edd16368
SC
1651 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
1652 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
1653 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1654 *nphysicals - HPSA_MAX_PHYS_LUN);
1655 *nphysicals = HPSA_MAX_PHYS_LUN;
1656 }
1657 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
1658 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
1659 return -1;
1660 }
6df1e954 1661 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
edd16368
SC
1662 /* Reject Logicals in excess of our max capability. */
1663 if (*nlogicals > HPSA_MAX_LUN) {
1664 dev_warn(&h->pdev->dev,
1665 "maximum logical LUNs (%d) exceeded. "
1666 "%d LUNs ignored.\n", HPSA_MAX_LUN,
1667 *nlogicals - HPSA_MAX_LUN);
1668 *nlogicals = HPSA_MAX_LUN;
1669 }
1670 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
1671 dev_warn(&h->pdev->dev,
1672 "maximum logical + physical LUNs (%d) exceeded. "
1673 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1674 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
1675 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
1676 }
1677 return 0;
1678}
1679
339b2b14
SC
1680u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
1681 int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list,
1682 struct ReportLUNdata *logdev_list)
1683{
1684 /* Helper function, figure out where the LUN ID info is coming from
1685 * given index i, lists of physical and logical devices, where in
1686 * the list the raid controller is supposed to appear (first or last)
1687 */
1688
1689 int logicals_start = nphysicals + (raid_ctlr_position == 0);
1690 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
1691
1692 if (i == raid_ctlr_position)
1693 return RAID_CTLR_LUNID;
1694
1695 if (i < logicals_start)
1696 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
1697
1698 if (i < last_device)
1699 return &logdev_list->LUN[i - nphysicals -
1700 (raid_ctlr_position == 0)][0];
1701 BUG();
1702 return NULL;
1703}
1704
edd16368
SC
1705static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
1706{
1707 /* the idea here is we could get notified
1708 * that some devices have changed, so we do a report
1709 * physical luns and report logical luns cmd, and adjust
1710 * our list of devices accordingly.
1711 *
1712 * The scsi3addr's of devices won't change so long as the
1713 * adapter is not reset. That means we can rescan and
1714 * tell which devices we already know about, vs. new
1715 * devices, vs. disappearing devices.
1716 */
1717 struct ReportLUNdata *physdev_list = NULL;
1718 struct ReportLUNdata *logdev_list = NULL;
1719 unsigned char *inq_buff = NULL;
01a02ffc
SC
1720 u32 nphysicals = 0;
1721 u32 nlogicals = 0;
1722 u32 ndev_allocated = 0;
edd16368
SC
1723 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
1724 int ncurrent = 0;
1725 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
1726 int i, nmsa2xxx_enclosures, ndevs_to_allocate;
1727 int bus, target, lun;
339b2b14 1728 int raid_ctlr_position;
edd16368
SC
1729 DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR);
1730
1731 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA,
1732 GFP_KERNEL);
1733 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1734 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1735 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1736 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
1737
1738 if (!currentsd || !physdev_list || !logdev_list ||
1739 !inq_buff || !tmpdevice) {
1740 dev_err(&h->pdev->dev, "out of memory\n");
1741 goto out;
1742 }
1743 memset(lunzerobits, 0, sizeof(lunzerobits));
1744
1745 if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
1746 logdev_list, &nlogicals))
1747 goto out;
1748
1749 /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
1750 * but each of them 4 times through different paths. The plus 1
1751 * is for the RAID controller.
1752 */
1753 ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1;
1754
1755 /* Allocate the per device structures */
1756 for (i = 0; i < ndevs_to_allocate; i++) {
1757 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
1758 if (!currentsd[i]) {
1759 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
1760 __FILE__, __LINE__);
1761 goto out;
1762 }
1763 ndev_allocated++;
1764 }
1765
339b2b14
SC
1766 if (unlikely(is_scsi_rev_5(h)))
1767 raid_ctlr_position = 0;
1768 else
1769 raid_ctlr_position = nphysicals + nlogicals;
1770
edd16368
SC
1771 /* adjust our table of devices */
1772 nmsa2xxx_enclosures = 0;
1773 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
01a02ffc 1774 u8 *lunaddrbytes;
edd16368
SC
1775
1776 /* Figure out where the LUN ID info is coming from */
339b2b14
SC
1777 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
1778 i, nphysicals, nlogicals, physdev_list, logdev_list);
edd16368 1779 /* skip masked physical devices. */
339b2b14
SC
1780 if (lunaddrbytes[3] & 0xC0 &&
1781 i < nphysicals + (raid_ctlr_position == 0))
edd16368
SC
1782 continue;
1783
1784 /* Get device type, vendor, model, device id */
1785 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice))
1786 continue; /* skip it if we can't talk to it. */
1787 figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun,
1788 tmpdevice);
1789 this_device = currentsd[ncurrent];
1790
1791 /*
1792 * For the msa2xxx boxes, we have to insert a LUN 0 which
1793 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
1794 * is nonetheless an enclosure device there. We have to
1795 * present that otherwise linux won't find anything if
1796 * there is no lun 0.
1797 */
1798 if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device,
1799 lunaddrbytes, bus, target, lun, lunzerobits,
1800 &nmsa2xxx_enclosures)) {
1801 ncurrent++;
1802 this_device = currentsd[ncurrent];
1803 }
1804
1805 *this_device = *tmpdevice;
1806 hpsa_set_bus_target_lun(this_device, bus, target, lun);
1807
1808 switch (this_device->devtype) {
1809 case TYPE_ROM: {
1810 /* We don't *really* support actual CD-ROM devices,
1811 * just "One Button Disaster Recovery" tape drive
1812 * which temporarily pretends to be a CD-ROM drive.
1813 * So we check that the device is really an OBDR tape
1814 * device by checking for "$DR-10" in bytes 43-48 of
1815 * the inquiry data.
1816 */
1817 char obdr_sig[7];
1818#define OBDR_TAPE_SIG "$DR-10"
1819 strncpy(obdr_sig, &inq_buff[43], 6);
1820 obdr_sig[6] = '\0';
1821 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1822 /* Not OBDR device, ignore it. */
1823 break;
1824 }
1825 ncurrent++;
1826 break;
1827 case TYPE_DISK:
1828 if (i < nphysicals)
1829 break;
1830 ncurrent++;
1831 break;
1832 case TYPE_TAPE:
1833 case TYPE_MEDIUM_CHANGER:
1834 ncurrent++;
1835 break;
1836 case TYPE_RAID:
1837 /* Only present the Smartarray HBA as a RAID controller.
1838 * If it's a RAID controller other than the HBA itself
1839 * (an external RAID controller, MSA500 or similar)
1840 * don't present it.
1841 */
1842 if (!is_hba_lunid(lunaddrbytes))
1843 break;
1844 ncurrent++;
1845 break;
1846 default:
1847 break;
1848 }
1849 if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA)
1850 break;
1851 }
1852 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
1853out:
1854 kfree(tmpdevice);
1855 for (i = 0; i < ndev_allocated; i++)
1856 kfree(currentsd[i]);
1857 kfree(currentsd);
1858 kfree(inq_buff);
1859 kfree(physdev_list);
1860 kfree(logdev_list);
edd16368
SC
1861}
1862
1863/* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1864 * dma mapping and fills in the scatter gather entries of the
1865 * hpsa command, cp.
1866 */
1867static int hpsa_scatter_gather(struct pci_dev *pdev,
1868 struct CommandList *cp,
1869 struct scsi_cmnd *cmd)
1870{
1871 unsigned int len;
1872 struct scatterlist *sg;
01a02ffc 1873 u64 addr64;
edd16368
SC
1874 int use_sg, i;
1875
1876 BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
1877
1878 use_sg = scsi_dma_map(cmd);
1879 if (use_sg < 0)
1880 return use_sg;
1881
1882 if (!use_sg)
1883 goto sglist_finished;
1884
1885 scsi_for_each_sg(cmd, sg, use_sg, i) {
01a02ffc 1886 addr64 = (u64) sg_dma_address(sg);
edd16368
SC
1887 len = sg_dma_len(sg);
1888 cp->SG[i].Addr.lower =
01a02ffc 1889 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
edd16368 1890 cp->SG[i].Addr.upper =
01a02ffc 1891 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
edd16368
SC
1892 cp->SG[i].Len = len;
1893 cp->SG[i].Ext = 0; /* we are not chaining */
1894 }
1895
1896sglist_finished:
1897
01a02ffc
SC
1898 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
1899 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
edd16368
SC
1900 return 0;
1901}
1902
1903
1904static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
1905 void (*done)(struct scsi_cmnd *))
1906{
1907 struct ctlr_info *h;
1908 struct hpsa_scsi_dev_t *dev;
1909 unsigned char scsi3addr[8];
1910 struct CommandList *c;
1911 unsigned long flags;
1912
1913 /* Get the ptr to our adapter structure out of cmd->host. */
1914 h = sdev_to_hba(cmd->device);
1915 dev = cmd->device->hostdata;
1916 if (!dev) {
1917 cmd->result = DID_NO_CONNECT << 16;
1918 done(cmd);
1919 return 0;
1920 }
1921 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
1922
1923 /* Need a lock as this is being allocated from the pool */
1924 spin_lock_irqsave(&h->lock, flags);
1925 c = cmd_alloc(h);
1926 spin_unlock_irqrestore(&h->lock, flags);
1927 if (c == NULL) { /* trouble... */
1928 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
1929 return SCSI_MLQUEUE_HOST_BUSY;
1930 }
1931
1932 /* Fill in the command list header */
1933
1934 cmd->scsi_done = done; /* save this for use by completion code */
1935
1936 /* save c in case we have to abort it */
1937 cmd->host_scribble = (unsigned char *) c;
1938
1939 c->cmd_type = CMD_SCSI;
1940 c->scsi_cmd = cmd;
1941 c->Header.ReplyQueue = 0; /* unused in simple mode */
1942 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
303932fd
DB
1943 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
1944 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
edd16368
SC
1945
1946 /* Fill in the request block... */
1947
1948 c->Request.Timeout = 0;
1949 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
1950 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
1951 c->Request.CDBLen = cmd->cmd_len;
1952 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
1953 c->Request.Type.Type = TYPE_CMD;
1954 c->Request.Type.Attribute = ATTR_SIMPLE;
1955 switch (cmd->sc_data_direction) {
1956 case DMA_TO_DEVICE:
1957 c->Request.Type.Direction = XFER_WRITE;
1958 break;
1959 case DMA_FROM_DEVICE:
1960 c->Request.Type.Direction = XFER_READ;
1961 break;
1962 case DMA_NONE:
1963 c->Request.Type.Direction = XFER_NONE;
1964 break;
1965 case DMA_BIDIRECTIONAL:
1966 /* This can happen if a buggy application does a scsi passthru
1967 * and sets both inlen and outlen to non-zero. ( see
1968 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1969 */
1970
1971 c->Request.Type.Direction = XFER_RSVD;
1972 /* This is technically wrong, and hpsa controllers should
1973 * reject it with CMD_INVALID, which is the most correct
1974 * response, but non-fibre backends appear to let it
1975 * slide by, and give the same results as if this field
1976 * were set correctly. Either way is acceptable for
1977 * our purposes here.
1978 */
1979
1980 break;
1981
1982 default:
1983 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
1984 cmd->sc_data_direction);
1985 BUG();
1986 break;
1987 }
1988
1989 if (hpsa_scatter_gather(h->pdev, c, cmd) < 0) { /* Fill SG list */
1990 cmd_free(h, c);
1991 return SCSI_MLQUEUE_HOST_BUSY;
1992 }
1993 enqueue_cmd_and_start_io(h, c);
1994 /* the cmd'll come back via intr handler in complete_scsi_command() */
1995 return 0;
1996}
1997
1998static void hpsa_unregister_scsi(struct ctlr_info *h)
1999{
2000 /* we are being forcibly unloaded, and may not refuse. */
2001 scsi_remove_host(h->scsi_host);
2002 scsi_host_put(h->scsi_host);
2003 h->scsi_host = NULL;
2004}
2005
2006static int hpsa_register_scsi(struct ctlr_info *h)
2007{
2008 int rc;
2009
2010 hpsa_update_scsi_devices(h, -1);
2011 rc = hpsa_scsi_detect(h);
2012 if (rc != 0)
2013 dev_err(&h->pdev->dev, "hpsa_register_scsi: failed"
2014 " hpsa_scsi_detect(), rc is %d\n", rc);
2015 return rc;
2016}
2017
2018static int wait_for_device_to_become_ready(struct ctlr_info *h,
2019 unsigned char lunaddr[])
2020{
2021 int rc = 0;
2022 int count = 0;
2023 int waittime = 1; /* seconds */
2024 struct CommandList *c;
2025
2026 c = cmd_special_alloc(h);
2027 if (!c) {
2028 dev_warn(&h->pdev->dev, "out of memory in "
2029 "wait_for_device_to_become_ready.\n");
2030 return IO_ERROR;
2031 }
2032
2033 /* Send test unit ready until device ready, or give up. */
2034 while (count < HPSA_TUR_RETRY_LIMIT) {
2035
2036 /* Wait for a bit. do this first, because if we send
2037 * the TUR right away, the reset will just abort it.
2038 */
2039 msleep(1000 * waittime);
2040 count++;
2041
2042 /* Increase wait time with each try, up to a point. */
2043 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
2044 waittime = waittime * 2;
2045
2046 /* Send the Test Unit Ready */
2047 fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD);
2048 hpsa_scsi_do_simple_cmd_core(h, c);
2049 /* no unmap needed here because no data xfer. */
2050
2051 if (c->err_info->CommandStatus == CMD_SUCCESS)
2052 break;
2053
2054 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2055 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
2056 (c->err_info->SenseInfo[2] == NO_SENSE ||
2057 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
2058 break;
2059
2060 dev_warn(&h->pdev->dev, "waiting %d secs "
2061 "for device to become ready.\n", waittime);
2062 rc = 1; /* device not ready. */
2063 }
2064
2065 if (rc)
2066 dev_warn(&h->pdev->dev, "giving up on device.\n");
2067 else
2068 dev_warn(&h->pdev->dev, "device is ready.\n");
2069
2070 cmd_special_free(h, c);
2071 return rc;
2072}
2073
2074/* Need at least one of these error handlers to keep ../scsi/hosts.c from
2075 * complaining. Doing a host- or bus-reset can't do anything good here.
2076 */
2077static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
2078{
2079 int rc;
2080 struct ctlr_info *h;
2081 struct hpsa_scsi_dev_t *dev;
2082
2083 /* find the controller to which the command to be aborted was sent */
2084 h = sdev_to_hba(scsicmd->device);
2085 if (h == NULL) /* paranoia */
2086 return FAILED;
2087 dev_warn(&h->pdev->dev, "resetting drive\n");
2088
2089 dev = scsicmd->device->hostdata;
2090 if (!dev) {
2091 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
2092 "device lookup failed.\n");
2093 return FAILED;
2094 }
2095 /* send a reset to the SCSI LUN which the command was sent to */
2096 rc = hpsa_send_reset(h, dev->scsi3addr);
2097 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
2098 return SUCCESS;
2099
2100 dev_warn(&h->pdev->dev, "resetting device failed.\n");
2101 return FAILED;
2102}
2103
2104/*
2105 * For operations that cannot sleep, a command block is allocated at init,
2106 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
2107 * which ones are free or in use. Lock must be held when calling this.
2108 * cmd_free() is the complement.
2109 */
2110static struct CommandList *cmd_alloc(struct ctlr_info *h)
2111{
2112 struct CommandList *c;
2113 int i;
2114 union u64bit temp64;
2115 dma_addr_t cmd_dma_handle, err_dma_handle;
2116
2117 do {
2118 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
2119 if (i == h->nr_cmds)
2120 return NULL;
2121 } while (test_and_set_bit
2122 (i & (BITS_PER_LONG - 1),
2123 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
2124 c = h->cmd_pool + i;
2125 memset(c, 0, sizeof(*c));
2126 cmd_dma_handle = h->cmd_pool_dhandle
2127 + i * sizeof(*c);
2128 c->err_info = h->errinfo_pool + i;
2129 memset(c->err_info, 0, sizeof(*c->err_info));
2130 err_dma_handle = h->errinfo_pool_dhandle
2131 + i * sizeof(*c->err_info);
2132 h->nr_allocs++;
2133
2134 c->cmdindex = i;
2135
2136 INIT_HLIST_NODE(&c->list);
01a02ffc
SC
2137 c->busaddr = (u32) cmd_dma_handle;
2138 temp64.val = (u64) err_dma_handle;
edd16368
SC
2139 c->ErrDesc.Addr.lower = temp64.val32.lower;
2140 c->ErrDesc.Addr.upper = temp64.val32.upper;
2141 c->ErrDesc.Len = sizeof(*c->err_info);
2142
2143 c->h = h;
2144 return c;
2145}
2146
2147/* For operations that can wait for kmalloc to possibly sleep,
2148 * this routine can be called. Lock need not be held to call
2149 * cmd_special_alloc. cmd_special_free() is the complement.
2150 */
2151static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
2152{
2153 struct CommandList *c;
2154 union u64bit temp64;
2155 dma_addr_t cmd_dma_handle, err_dma_handle;
2156
2157 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
2158 if (c == NULL)
2159 return NULL;
2160 memset(c, 0, sizeof(*c));
2161
2162 c->cmdindex = -1;
2163
2164 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
2165 &err_dma_handle);
2166
2167 if (c->err_info == NULL) {
2168 pci_free_consistent(h->pdev,
2169 sizeof(*c), c, cmd_dma_handle);
2170 return NULL;
2171 }
2172 memset(c->err_info, 0, sizeof(*c->err_info));
2173
2174 INIT_HLIST_NODE(&c->list);
01a02ffc
SC
2175 c->busaddr = (u32) cmd_dma_handle;
2176 temp64.val = (u64) err_dma_handle;
edd16368
SC
2177 c->ErrDesc.Addr.lower = temp64.val32.lower;
2178 c->ErrDesc.Addr.upper = temp64.val32.upper;
2179 c->ErrDesc.Len = sizeof(*c->err_info);
2180
2181 c->h = h;
2182 return c;
2183}
2184
2185static void cmd_free(struct ctlr_info *h, struct CommandList *c)
2186{
2187 int i;
2188
2189 i = c - h->cmd_pool;
2190 clear_bit(i & (BITS_PER_LONG - 1),
2191 h->cmd_pool_bits + (i / BITS_PER_LONG));
2192 h->nr_frees++;
2193}
2194
2195static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
2196{
2197 union u64bit temp64;
2198
2199 temp64.val32.lower = c->ErrDesc.Addr.lower;
2200 temp64.val32.upper = c->ErrDesc.Addr.upper;
2201 pci_free_consistent(h->pdev, sizeof(*c->err_info),
2202 c->err_info, (dma_addr_t) temp64.val);
2203 pci_free_consistent(h->pdev, sizeof(*c),
2204 c, (dma_addr_t) c->busaddr);
2205}
2206
2207#ifdef CONFIG_COMPAT
2208
2209static int do_ioctl(struct scsi_device *dev, int cmd, void *arg)
2210{
2211 int ret;
2212
2213 lock_kernel();
2214 ret = hpsa_ioctl(dev, cmd, arg);
2215 unlock_kernel();
2216 return ret;
2217}
2218
2219static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg);
2220static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
2221 int cmd, void *arg);
2222
2223static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
2224{
2225 switch (cmd) {
2226 case CCISS_GETPCIINFO:
2227 case CCISS_GETINTINFO:
2228 case CCISS_SETINTINFO:
2229 case CCISS_GETNODENAME:
2230 case CCISS_SETNODENAME:
2231 case CCISS_GETHEARTBEAT:
2232 case CCISS_GETBUSTYPES:
2233 case CCISS_GETFIRMVER:
2234 case CCISS_GETDRIVVER:
2235 case CCISS_REVALIDVOLS:
2236 case CCISS_DEREGDISK:
2237 case CCISS_REGNEWDISK:
2238 case CCISS_REGNEWD:
2239 case CCISS_RESCANDISK:
2240 case CCISS_GETLUNINFO:
2241 return do_ioctl(dev, cmd, arg);
2242
2243 case CCISS_PASSTHRU32:
2244 return hpsa_ioctl32_passthru(dev, cmd, arg);
2245 case CCISS_BIG_PASSTHRU32:
2246 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
2247
2248 default:
2249 return -ENOIOCTLCMD;
2250 }
2251}
2252
2253static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
2254{
2255 IOCTL32_Command_struct __user *arg32 =
2256 (IOCTL32_Command_struct __user *) arg;
2257 IOCTL_Command_struct arg64;
2258 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
2259 int err;
2260 u32 cp;
2261
2262 err = 0;
2263 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2264 sizeof(arg64.LUN_info));
2265 err |= copy_from_user(&arg64.Request, &arg32->Request,
2266 sizeof(arg64.Request));
2267 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2268 sizeof(arg64.error_info));
2269 err |= get_user(arg64.buf_size, &arg32->buf_size);
2270 err |= get_user(cp, &arg32->buf);
2271 arg64.buf = compat_ptr(cp);
2272 err |= copy_to_user(p, &arg64, sizeof(arg64));
2273
2274 if (err)
2275 return -EFAULT;
2276
2277 err = do_ioctl(dev, CCISS_PASSTHRU, (void *)p);
2278 if (err)
2279 return err;
2280 err |= copy_in_user(&arg32->error_info, &p->error_info,
2281 sizeof(arg32->error_info));
2282 if (err)
2283 return -EFAULT;
2284 return err;
2285}
2286
2287static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
2288 int cmd, void *arg)
2289{
2290 BIG_IOCTL32_Command_struct __user *arg32 =
2291 (BIG_IOCTL32_Command_struct __user *) arg;
2292 BIG_IOCTL_Command_struct arg64;
2293 BIG_IOCTL_Command_struct __user *p =
2294 compat_alloc_user_space(sizeof(arg64));
2295 int err;
2296 u32 cp;
2297
2298 err = 0;
2299 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2300 sizeof(arg64.LUN_info));
2301 err |= copy_from_user(&arg64.Request, &arg32->Request,
2302 sizeof(arg64.Request));
2303 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2304 sizeof(arg64.error_info));
2305 err |= get_user(arg64.buf_size, &arg32->buf_size);
2306 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
2307 err |= get_user(cp, &arg32->buf);
2308 arg64.buf = compat_ptr(cp);
2309 err |= copy_to_user(p, &arg64, sizeof(arg64));
2310
2311 if (err)
2312 return -EFAULT;
2313
2314 err = do_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
2315 if (err)
2316 return err;
2317 err |= copy_in_user(&arg32->error_info, &p->error_info,
2318 sizeof(arg32->error_info));
2319 if (err)
2320 return -EFAULT;
2321 return err;
2322}
2323#endif
2324
2325static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
2326{
2327 struct hpsa_pci_info pciinfo;
2328
2329 if (!argp)
2330 return -EINVAL;
2331 pciinfo.domain = pci_domain_nr(h->pdev->bus);
2332 pciinfo.bus = h->pdev->bus->number;
2333 pciinfo.dev_fn = h->pdev->devfn;
2334 pciinfo.board_id = h->board_id;
2335 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
2336 return -EFAULT;
2337 return 0;
2338}
2339
2340static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
2341{
2342 DriverVer_type DriverVer;
2343 unsigned char vmaj, vmin, vsubmin;
2344 int rc;
2345
2346 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
2347 &vmaj, &vmin, &vsubmin);
2348 if (rc != 3) {
2349 dev_info(&h->pdev->dev, "driver version string '%s' "
2350 "unrecognized.", HPSA_DRIVER_VERSION);
2351 vmaj = 0;
2352 vmin = 0;
2353 vsubmin = 0;
2354 }
2355 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
2356 if (!argp)
2357 return -EINVAL;
2358 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
2359 return -EFAULT;
2360 return 0;
2361}
2362
2363static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2364{
2365 IOCTL_Command_struct iocommand;
2366 struct CommandList *c;
2367 char *buff = NULL;
2368 union u64bit temp64;
2369
2370 if (!argp)
2371 return -EINVAL;
2372 if (!capable(CAP_SYS_RAWIO))
2373 return -EPERM;
2374 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
2375 return -EFAULT;
2376 if ((iocommand.buf_size < 1) &&
2377 (iocommand.Request.Type.Direction != XFER_NONE)) {
2378 return -EINVAL;
2379 }
2380 if (iocommand.buf_size > 0) {
2381 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
2382 if (buff == NULL)
2383 return -EFAULT;
2384 }
2385 if (iocommand.Request.Type.Direction == XFER_WRITE) {
2386 /* Copy the data into the buffer we created */
2387 if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) {
2388 kfree(buff);
2389 return -EFAULT;
2390 }
2391 } else
2392 memset(buff, 0, iocommand.buf_size);
2393 c = cmd_special_alloc(h);
2394 if (c == NULL) {
2395 kfree(buff);
2396 return -ENOMEM;
2397 }
2398 /* Fill in the command type */
2399 c->cmd_type = CMD_IOCTL_PEND;
2400 /* Fill in Command Header */
2401 c->Header.ReplyQueue = 0; /* unused in simple mode */
2402 if (iocommand.buf_size > 0) { /* buffer to fill */
2403 c->Header.SGList = 1;
2404 c->Header.SGTotal = 1;
2405 } else { /* no buffers to fill */
2406 c->Header.SGList = 0;
2407 c->Header.SGTotal = 0;
2408 }
2409 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
2410 /* use the kernel address the cmd block for tag */
2411 c->Header.Tag.lower = c->busaddr;
2412
2413 /* Fill in Request block */
2414 memcpy(&c->Request, &iocommand.Request,
2415 sizeof(c->Request));
2416
2417 /* Fill in the scatter gather information */
2418 if (iocommand.buf_size > 0) {
2419 temp64.val = pci_map_single(h->pdev, buff,
2420 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
2421 c->SG[0].Addr.lower = temp64.val32.lower;
2422 c->SG[0].Addr.upper = temp64.val32.upper;
2423 c->SG[0].Len = iocommand.buf_size;
2424 c->SG[0].Ext = 0; /* we are not chaining*/
2425 }
2426 hpsa_scsi_do_simple_cmd_core(h, c);
2427 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
2428 check_ioctl_unit_attention(h, c);
2429
2430 /* Copy the error information out */
2431 memcpy(&iocommand.error_info, c->err_info,
2432 sizeof(iocommand.error_info));
2433 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
2434 kfree(buff);
2435 cmd_special_free(h, c);
2436 return -EFAULT;
2437 }
2438
2439 if (iocommand.Request.Type.Direction == XFER_READ) {
2440 /* Copy the data out of the buffer we created */
2441 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
2442 kfree(buff);
2443 cmd_special_free(h, c);
2444 return -EFAULT;
2445 }
2446 }
2447 kfree(buff);
2448 cmd_special_free(h, c);
2449 return 0;
2450}
2451
2452static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2453{
2454 BIG_IOCTL_Command_struct *ioc;
2455 struct CommandList *c;
2456 unsigned char **buff = NULL;
2457 int *buff_size = NULL;
2458 union u64bit temp64;
2459 BYTE sg_used = 0;
2460 int status = 0;
2461 int i;
01a02ffc
SC
2462 u32 left;
2463 u32 sz;
edd16368
SC
2464 BYTE __user *data_ptr;
2465
2466 if (!argp)
2467 return -EINVAL;
2468 if (!capable(CAP_SYS_RAWIO))
2469 return -EPERM;
2470 ioc = (BIG_IOCTL_Command_struct *)
2471 kmalloc(sizeof(*ioc), GFP_KERNEL);
2472 if (!ioc) {
2473 status = -ENOMEM;
2474 goto cleanup1;
2475 }
2476 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
2477 status = -EFAULT;
2478 goto cleanup1;
2479 }
2480 if ((ioc->buf_size < 1) &&
2481 (ioc->Request.Type.Direction != XFER_NONE)) {
2482 status = -EINVAL;
2483 goto cleanup1;
2484 }
2485 /* Check kmalloc limits using all SGs */
2486 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
2487 status = -EINVAL;
2488 goto cleanup1;
2489 }
2490 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
2491 status = -EINVAL;
2492 goto cleanup1;
2493 }
2494 buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
2495 if (!buff) {
2496 status = -ENOMEM;
2497 goto cleanup1;
2498 }
2499 buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL);
2500 if (!buff_size) {
2501 status = -ENOMEM;
2502 goto cleanup1;
2503 }
2504 left = ioc->buf_size;
2505 data_ptr = ioc->buf;
2506 while (left) {
2507 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
2508 buff_size[sg_used] = sz;
2509 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
2510 if (buff[sg_used] == NULL) {
2511 status = -ENOMEM;
2512 goto cleanup1;
2513 }
2514 if (ioc->Request.Type.Direction == XFER_WRITE) {
2515 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
2516 status = -ENOMEM;
2517 goto cleanup1;
2518 }
2519 } else
2520 memset(buff[sg_used], 0, sz);
2521 left -= sz;
2522 data_ptr += sz;
2523 sg_used++;
2524 }
2525 c = cmd_special_alloc(h);
2526 if (c == NULL) {
2527 status = -ENOMEM;
2528 goto cleanup1;
2529 }
2530 c->cmd_type = CMD_IOCTL_PEND;
2531 c->Header.ReplyQueue = 0;
2532
2533 if (ioc->buf_size > 0) {
2534 c->Header.SGList = sg_used;
2535 c->Header.SGTotal = sg_used;
2536 } else {
2537 c->Header.SGList = 0;
2538 c->Header.SGTotal = 0;
2539 }
2540 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
2541 c->Header.Tag.lower = c->busaddr;
2542 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
2543 if (ioc->buf_size > 0) {
2544 int i;
2545 for (i = 0; i < sg_used; i++) {
2546 temp64.val = pci_map_single(h->pdev, buff[i],
2547 buff_size[i], PCI_DMA_BIDIRECTIONAL);
2548 c->SG[i].Addr.lower = temp64.val32.lower;
2549 c->SG[i].Addr.upper = temp64.val32.upper;
2550 c->SG[i].Len = buff_size[i];
2551 /* we are not chaining */
2552 c->SG[i].Ext = 0;
2553 }
2554 }
2555 hpsa_scsi_do_simple_cmd_core(h, c);
2556 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
2557 check_ioctl_unit_attention(h, c);
2558 /* Copy the error information out */
2559 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
2560 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
2561 cmd_special_free(h, c);
2562 status = -EFAULT;
2563 goto cleanup1;
2564 }
2565 if (ioc->Request.Type.Direction == XFER_READ) {
2566 /* Copy the data out of the buffer we created */
2567 BYTE __user *ptr = ioc->buf;
2568 for (i = 0; i < sg_used; i++) {
2569 if (copy_to_user(ptr, buff[i], buff_size[i])) {
2570 cmd_special_free(h, c);
2571 status = -EFAULT;
2572 goto cleanup1;
2573 }
2574 ptr += buff_size[i];
2575 }
2576 }
2577 cmd_special_free(h, c);
2578 status = 0;
2579cleanup1:
2580 if (buff) {
2581 for (i = 0; i < sg_used; i++)
2582 kfree(buff[i]);
2583 kfree(buff);
2584 }
2585 kfree(buff_size);
2586 kfree(ioc);
2587 return status;
2588}
2589
2590static void check_ioctl_unit_attention(struct ctlr_info *h,
2591 struct CommandList *c)
2592{
2593 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2594 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
2595 (void) check_for_unit_attention(h, c);
2596}
2597/*
2598 * ioctl
2599 */
2600static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
2601{
2602 struct ctlr_info *h;
2603 void __user *argp = (void __user *)arg;
2604
2605 h = sdev_to_hba(dev);
2606
2607 switch (cmd) {
2608 case CCISS_DEREGDISK:
2609 case CCISS_REGNEWDISK:
2610 case CCISS_REGNEWD:
2611 hpsa_update_scsi_devices(h, dev->host->host_no);
2612 return 0;
2613 case CCISS_GETPCIINFO:
2614 return hpsa_getpciinfo_ioctl(h, argp);
2615 case CCISS_GETDRIVVER:
2616 return hpsa_getdrivver_ioctl(h, argp);
2617 case CCISS_PASSTHRU:
2618 return hpsa_passthru_ioctl(h, argp);
2619 case CCISS_BIG_PASSTHRU:
2620 return hpsa_big_passthru_ioctl(h, argp);
2621 default:
2622 return -ENOTTY;
2623 }
2624}
2625
01a02ffc
SC
2626static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
2627 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
edd16368
SC
2628 int cmd_type)
2629{
2630 int pci_dir = XFER_NONE;
2631
2632 c->cmd_type = CMD_IOCTL_PEND;
2633 c->Header.ReplyQueue = 0;
2634 if (buff != NULL && size > 0) {
2635 c->Header.SGList = 1;
2636 c->Header.SGTotal = 1;
2637 } else {
2638 c->Header.SGList = 0;
2639 c->Header.SGTotal = 0;
2640 }
2641 c->Header.Tag.lower = c->busaddr;
2642 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
2643
2644 c->Request.Type.Type = cmd_type;
2645 if (cmd_type == TYPE_CMD) {
2646 switch (cmd) {
2647 case HPSA_INQUIRY:
2648 /* are we trying to read a vital product page */
2649 if (page_code != 0) {
2650 c->Request.CDB[1] = 0x01;
2651 c->Request.CDB[2] = page_code;
2652 }
2653 c->Request.CDBLen = 6;
2654 c->Request.Type.Attribute = ATTR_SIMPLE;
2655 c->Request.Type.Direction = XFER_READ;
2656 c->Request.Timeout = 0;
2657 c->Request.CDB[0] = HPSA_INQUIRY;
2658 c->Request.CDB[4] = size & 0xFF;
2659 break;
2660 case HPSA_REPORT_LOG:
2661 case HPSA_REPORT_PHYS:
2662 /* Talking to controller so It's a physical command
2663 mode = 00 target = 0. Nothing to write.
2664 */
2665 c->Request.CDBLen = 12;
2666 c->Request.Type.Attribute = ATTR_SIMPLE;
2667 c->Request.Type.Direction = XFER_READ;
2668 c->Request.Timeout = 0;
2669 c->Request.CDB[0] = cmd;
2670 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
2671 c->Request.CDB[7] = (size >> 16) & 0xFF;
2672 c->Request.CDB[8] = (size >> 8) & 0xFF;
2673 c->Request.CDB[9] = size & 0xFF;
2674 break;
2675
2676 case HPSA_READ_CAPACITY:
2677 c->Request.CDBLen = 10;
2678 c->Request.Type.Attribute = ATTR_SIMPLE;
2679 c->Request.Type.Direction = XFER_READ;
2680 c->Request.Timeout = 0;
2681 c->Request.CDB[0] = cmd;
2682 break;
2683 case HPSA_CACHE_FLUSH:
2684 c->Request.CDBLen = 12;
2685 c->Request.Type.Attribute = ATTR_SIMPLE;
2686 c->Request.Type.Direction = XFER_WRITE;
2687 c->Request.Timeout = 0;
2688 c->Request.CDB[0] = BMIC_WRITE;
2689 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
2690 break;
2691 case TEST_UNIT_READY:
2692 c->Request.CDBLen = 6;
2693 c->Request.Type.Attribute = ATTR_SIMPLE;
2694 c->Request.Type.Direction = XFER_NONE;
2695 c->Request.Timeout = 0;
2696 break;
2697 default:
2698 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
2699 BUG();
2700 return;
2701 }
2702 } else if (cmd_type == TYPE_MSG) {
2703 switch (cmd) {
2704
2705 case HPSA_DEVICE_RESET_MSG:
2706 c->Request.CDBLen = 16;
2707 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
2708 c->Request.Type.Attribute = ATTR_SIMPLE;
2709 c->Request.Type.Direction = XFER_NONE;
2710 c->Request.Timeout = 0; /* Don't time out */
2711 c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */
2712 c->Request.CDB[1] = 0x03; /* Reset target above */
2713 /* If bytes 4-7 are zero, it means reset the */
2714 /* LunID device */
2715 c->Request.CDB[4] = 0x00;
2716 c->Request.CDB[5] = 0x00;
2717 c->Request.CDB[6] = 0x00;
2718 c->Request.CDB[7] = 0x00;
2719 break;
2720
2721 default:
2722 dev_warn(&h->pdev->dev, "unknown message type %d\n",
2723 cmd);
2724 BUG();
2725 }
2726 } else {
2727 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
2728 BUG();
2729 }
2730
2731 switch (c->Request.Type.Direction) {
2732 case XFER_READ:
2733 pci_dir = PCI_DMA_FROMDEVICE;
2734 break;
2735 case XFER_WRITE:
2736 pci_dir = PCI_DMA_TODEVICE;
2737 break;
2738 case XFER_NONE:
2739 pci_dir = PCI_DMA_NONE;
2740 break;
2741 default:
2742 pci_dir = PCI_DMA_BIDIRECTIONAL;
2743 }
2744
2745 hpsa_map_one(h->pdev, c, buff, size, pci_dir);
2746
2747 return;
2748}
2749
2750/*
2751 * Map (physical) PCI mem into (virtual) kernel space
2752 */
2753static void __iomem *remap_pci_mem(ulong base, ulong size)
2754{
2755 ulong page_base = ((ulong) base) & PAGE_MASK;
2756 ulong page_offs = ((ulong) base) - page_base;
2757 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
2758
2759 return page_remapped ? (page_remapped + page_offs) : NULL;
2760}
2761
2762/* Takes cmds off the submission queue and sends them to the hardware,
2763 * then puts them on the queue of cmds waiting for completion.
2764 */
2765static void start_io(struct ctlr_info *h)
2766{
2767 struct CommandList *c;
2768
2769 while (!hlist_empty(&h->reqQ)) {
2770 c = hlist_entry(h->reqQ.first, struct CommandList, list);
2771 /* can't do anything if fifo is full */
2772 if ((h->access.fifo_full(h))) {
2773 dev_warn(&h->pdev->dev, "fifo full\n");
2774 break;
2775 }
2776
2777 /* Get the first entry from the Request Q */
2778 removeQ(c);
2779 h->Qdepth--;
2780
2781 /* Tell the controller execute command */
2782 h->access.submit_command(h, c);
2783
2784 /* Put job onto the completed Q */
2785 addQ(&h->cmpQ, c);
2786 }
2787}
2788
2789static inline unsigned long get_next_completion(struct ctlr_info *h)
2790{
2791 return h->access.command_completed(h);
2792}
2793
900c5440 2794static inline bool interrupt_pending(struct ctlr_info *h)
edd16368
SC
2795{
2796 return h->access.intr_pending(h);
2797}
2798
2799static inline long interrupt_not_for_us(struct ctlr_info *h)
2800{
303932fd
DB
2801 return !(h->msi_vector || h->msix_vector) &&
2802 ((h->access.intr_pending(h) == 0) ||
2803 (h->interrupts_enabled == 0));
edd16368
SC
2804}
2805
01a02ffc
SC
2806static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
2807 u32 raw_tag)
edd16368
SC
2808{
2809 if (unlikely(tag_index >= h->nr_cmds)) {
2810 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
2811 return 1;
2812 }
2813 return 0;
2814}
2815
01a02ffc 2816static inline void finish_cmd(struct CommandList *c, u32 raw_tag)
edd16368
SC
2817{
2818 removeQ(c);
2819 if (likely(c->cmd_type == CMD_SCSI))
2820 complete_scsi_command(c, 0, raw_tag);
2821 else if (c->cmd_type == CMD_IOCTL_PEND)
2822 complete(c->waiting);
2823}
2824
a104c99f
SC
2825static inline u32 hpsa_tag_contains_index(u32 tag)
2826{
303932fd 2827#define DIRECT_LOOKUP_BIT 0x10
a104c99f
SC
2828 return tag & DIRECT_LOOKUP_BIT;
2829}
2830
2831static inline u32 hpsa_tag_to_index(u32 tag)
2832{
303932fd 2833#define DIRECT_LOOKUP_SHIFT 5
a104c99f
SC
2834 return tag >> DIRECT_LOOKUP_SHIFT;
2835}
2836
2837static inline u32 hpsa_tag_discard_error_bits(u32 tag)
2838{
2839#define HPSA_ERROR_BITS 0x03
2840 return tag & ~HPSA_ERROR_BITS;
2841}
2842
303932fd
DB
2843/* process completion of an indexed ("direct lookup") command */
2844static inline u32 process_indexed_cmd(struct ctlr_info *h,
2845 u32 raw_tag)
2846{
2847 u32 tag_index;
2848 struct CommandList *c;
2849
2850 tag_index = hpsa_tag_to_index(raw_tag);
2851 if (bad_tag(h, tag_index, raw_tag))
2852 return next_command(h);
2853 c = h->cmd_pool + tag_index;
2854 finish_cmd(c, raw_tag);
2855 return next_command(h);
2856}
2857
2858/* process completion of a non-indexed command */
2859static inline u32 process_nonindexed_cmd(struct ctlr_info *h,
2860 u32 raw_tag)
2861{
2862 u32 tag;
2863 struct CommandList *c = NULL;
2864 struct hlist_node *tmp;
2865
2866 tag = hpsa_tag_discard_error_bits(raw_tag);
2867 hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
2868 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
2869 finish_cmd(c, raw_tag);
2870 return next_command(h);
2871 }
2872 }
2873 bad_tag(h, h->nr_cmds + 1, raw_tag);
2874 return next_command(h);
2875}
2876
edd16368
SC
2877static irqreturn_t do_hpsa_intr(int irq, void *dev_id)
2878{
2879 struct ctlr_info *h = dev_id;
edd16368 2880 unsigned long flags;
303932fd 2881 u32 raw_tag;
edd16368
SC
2882
2883 if (interrupt_not_for_us(h))
2884 return IRQ_NONE;
2885 spin_lock_irqsave(&h->lock, flags);
303932fd
DB
2886 raw_tag = get_next_completion(h);
2887 while (raw_tag != FIFO_EMPTY) {
2888 if (hpsa_tag_contains_index(raw_tag))
2889 raw_tag = process_indexed_cmd(h, raw_tag);
2890 else
2891 raw_tag = process_nonindexed_cmd(h, raw_tag);
edd16368
SC
2892 }
2893 spin_unlock_irqrestore(&h->lock, flags);
2894 return IRQ_HANDLED;
2895}
2896
303932fd 2897/* Send a message CDB to the firmwart. */
edd16368
SC
2898static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
2899 unsigned char type)
2900{
2901 struct Command {
2902 struct CommandListHeader CommandHeader;
2903 struct RequestBlock Request;
2904 struct ErrDescriptor ErrorDescriptor;
2905 };
2906 struct Command *cmd;
2907 static const size_t cmd_sz = sizeof(*cmd) +
2908 sizeof(cmd->ErrorDescriptor);
2909 dma_addr_t paddr64;
2910 uint32_t paddr32, tag;
2911 void __iomem *vaddr;
2912 int i, err;
2913
2914 vaddr = pci_ioremap_bar(pdev, 0);
2915 if (vaddr == NULL)
2916 return -ENOMEM;
2917
2918 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
2919 * CCISS commands, so they must be allocated from the lower 4GiB of
2920 * memory.
2921 */
2922 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2923 if (err) {
2924 iounmap(vaddr);
2925 return -ENOMEM;
2926 }
2927
2928 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
2929 if (cmd == NULL) {
2930 iounmap(vaddr);
2931 return -ENOMEM;
2932 }
2933
2934 /* This must fit, because of the 32-bit consistent DMA mask. Also,
2935 * although there's no guarantee, we assume that the address is at
2936 * least 4-byte aligned (most likely, it's page-aligned).
2937 */
2938 paddr32 = paddr64;
2939
2940 cmd->CommandHeader.ReplyQueue = 0;
2941 cmd->CommandHeader.SGList = 0;
2942 cmd->CommandHeader.SGTotal = 0;
2943 cmd->CommandHeader.Tag.lower = paddr32;
2944 cmd->CommandHeader.Tag.upper = 0;
2945 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
2946
2947 cmd->Request.CDBLen = 16;
2948 cmd->Request.Type.Type = TYPE_MSG;
2949 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
2950 cmd->Request.Type.Direction = XFER_NONE;
2951 cmd->Request.Timeout = 0; /* Don't time out */
2952 cmd->Request.CDB[0] = opcode;
2953 cmd->Request.CDB[1] = type;
2954 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
2955 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
2956 cmd->ErrorDescriptor.Addr.upper = 0;
2957 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
2958
2959 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
2960
2961 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
2962 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
a104c99f 2963 if (hpsa_tag_discard_error_bits(tag) == paddr32)
edd16368
SC
2964 break;
2965 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
2966 }
2967
2968 iounmap(vaddr);
2969
2970 /* we leak the DMA buffer here ... no choice since the controller could
2971 * still complete the command.
2972 */
2973 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
2974 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
2975 opcode, type);
2976 return -ETIMEDOUT;
2977 }
2978
2979 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
2980
2981 if (tag & HPSA_ERROR_BIT) {
2982 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
2983 opcode, type);
2984 return -EIO;
2985 }
2986
2987 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
2988 opcode, type);
2989 return 0;
2990}
2991
2992#define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
2993#define hpsa_noop(p) hpsa_message(p, 3, 0)
2994
2995static __devinit int hpsa_reset_msi(struct pci_dev *pdev)
2996{
2997/* the #defines are stolen from drivers/pci/msi.h. */
2998#define msi_control_reg(base) (base + PCI_MSI_FLAGS)
2999#define PCI_MSIX_FLAGS_ENABLE (1 << 15)
3000
3001 int pos;
3002 u16 control = 0;
3003
3004 pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
3005 if (pos) {
3006 pci_read_config_word(pdev, msi_control_reg(pos), &control);
3007 if (control & PCI_MSI_FLAGS_ENABLE) {
3008 dev_info(&pdev->dev, "resetting MSI\n");
3009 pci_write_config_word(pdev, msi_control_reg(pos),
3010 control & ~PCI_MSI_FLAGS_ENABLE);
3011 }
3012 }
3013
3014 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3015 if (pos) {
3016 pci_read_config_word(pdev, msi_control_reg(pos), &control);
3017 if (control & PCI_MSIX_FLAGS_ENABLE) {
3018 dev_info(&pdev->dev, "resetting MSI-X\n");
3019 pci_write_config_word(pdev, msi_control_reg(pos),
3020 control & ~PCI_MSIX_FLAGS_ENABLE);
3021 }
3022 }
3023
3024 return 0;
3025}
3026
3027/* This does a hard reset of the controller using PCI power management
3028 * states.
3029 */
3030static __devinit int hpsa_hard_reset_controller(struct pci_dev *pdev)
3031{
3032 u16 pmcsr, saved_config_space[32];
3033 int i, pos;
3034
3035 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
3036
3037 /* This is very nearly the same thing as
3038 *
3039 * pci_save_state(pci_dev);
3040 * pci_set_power_state(pci_dev, PCI_D3hot);
3041 * pci_set_power_state(pci_dev, PCI_D0);
3042 * pci_restore_state(pci_dev);
3043 *
3044 * but we can't use these nice canned kernel routines on
3045 * kexec, because they also check the MSI/MSI-X state in PCI
3046 * configuration space and do the wrong thing when it is
3047 * set/cleared. Also, the pci_save/restore_state functions
3048 * violate the ordering requirements for restoring the
3049 * configuration space from the CCISS document (see the
3050 * comment below). So we roll our own ....
3051 */
3052
3053 for (i = 0; i < 32; i++)
3054 pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
3055
3056 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
3057 if (pos == 0) {
3058 dev_err(&pdev->dev,
3059 "hpsa_reset_controller: PCI PM not supported\n");
3060 return -ENODEV;
3061 }
3062
3063 /* Quoting from the Open CISS Specification: "The Power
3064 * Management Control/Status Register (CSR) controls the power
3065 * state of the device. The normal operating state is D0,
3066 * CSR=00h. The software off state is D3, CSR=03h. To reset
3067 * the controller, place the interface device in D3 then to
3068 * D0, this causes a secondary PCI reset which will reset the
3069 * controller."
3070 */
3071
3072 /* enter the D3hot power management state */
3073 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
3074 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3075 pmcsr |= PCI_D3hot;
3076 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3077
3078 msleep(500);
3079
3080 /* enter the D0 power management state */
3081 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3082 pmcsr |= PCI_D0;
3083 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3084
3085 msleep(500);
3086
3087 /* Restore the PCI configuration space. The Open CISS
3088 * Specification says, "Restore the PCI Configuration
3089 * Registers, offsets 00h through 60h. It is important to
3090 * restore the command register, 16-bits at offset 04h,
3091 * last. Do not restore the configuration status register,
3092 * 16-bits at offset 06h." Note that the offset is 2*i.
3093 */
3094 for (i = 0; i < 32; i++) {
3095 if (i == 2 || i == 3)
3096 continue;
3097 pci_write_config_word(pdev, 2*i, saved_config_space[i]);
3098 }
3099 wmb();
3100 pci_write_config_word(pdev, 4, saved_config_space[2]);
3101
3102 return 0;
3103}
3104
3105/*
3106 * We cannot read the structure directly, for portability we must use
3107 * the io functions.
3108 * This is for debug only.
3109 */
3110#ifdef HPSA_DEBUG
3111static void print_cfg_table(struct device *dev, struct CfgTable *tb)
3112{
3113 int i;
3114 char temp_name[17];
3115
3116 dev_info(dev, "Controller Configuration information\n");
3117 dev_info(dev, "------------------------------------\n");
3118 for (i = 0; i < 4; i++)
3119 temp_name[i] = readb(&(tb->Signature[i]));
3120 temp_name[4] = '\0';
3121 dev_info(dev, " Signature = %s\n", temp_name);
3122 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
3123 dev_info(dev, " Transport methods supported = 0x%x\n",
3124 readl(&(tb->TransportSupport)));
3125 dev_info(dev, " Transport methods active = 0x%x\n",
3126 readl(&(tb->TransportActive)));
3127 dev_info(dev, " Requested transport Method = 0x%x\n",
3128 readl(&(tb->HostWrite.TransportRequest)));
3129 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
3130 readl(&(tb->HostWrite.CoalIntDelay)));
3131 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
3132 readl(&(tb->HostWrite.CoalIntCount)));
3133 dev_info(dev, " Max outstanding commands = 0x%d\n",
3134 readl(&(tb->CmdsOutMax)));
3135 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
3136 for (i = 0; i < 16; i++)
3137 temp_name[i] = readb(&(tb->ServerName[i]));
3138 temp_name[16] = '\0';
3139 dev_info(dev, " Server Name = %s\n", temp_name);
3140 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
3141 readl(&(tb->HeartBeat)));
3142}
3143#endif /* HPSA_DEBUG */
3144
3145static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
3146{
3147 int i, offset, mem_type, bar_type;
3148
3149 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
3150 return 0;
3151 offset = 0;
3152 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3153 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
3154 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
3155 offset += 4;
3156 else {
3157 mem_type = pci_resource_flags(pdev, i) &
3158 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
3159 switch (mem_type) {
3160 case PCI_BASE_ADDRESS_MEM_TYPE_32:
3161 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
3162 offset += 4; /* 32 bit */
3163 break;
3164 case PCI_BASE_ADDRESS_MEM_TYPE_64:
3165 offset += 8;
3166 break;
3167 default: /* reserved in PCI 2.2 */
3168 dev_warn(&pdev->dev,
3169 "base address is invalid\n");
3170 return -1;
3171 break;
3172 }
3173 }
3174 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
3175 return i + 1;
3176 }
3177 return -1;
3178}
3179
3180/* If MSI/MSI-X is supported by the kernel we will try to enable it on
3181 * controllers that are capable. If not, we use IO-APIC mode.
3182 */
3183
3184static void __devinit hpsa_interrupt_mode(struct ctlr_info *h,
01a02ffc 3185 struct pci_dev *pdev, u32 board_id)
edd16368
SC
3186{
3187#ifdef CONFIG_PCI_MSI
3188 int err;
3189 struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1},
3190 {0, 2}, {0, 3}
3191 };
3192
3193 /* Some boards advertise MSI but don't really support it */
3194 if ((board_id == 0x40700E11) ||
3195 (board_id == 0x40800E11) ||
3196 (board_id == 0x40820E11) || (board_id == 0x40830E11))
3197 goto default_int_mode;
3198 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
3199 dev_info(&pdev->dev, "MSIX\n");
3200 err = pci_enable_msix(pdev, hpsa_msix_entries, 4);
3201 if (!err) {
3202 h->intr[0] = hpsa_msix_entries[0].vector;
3203 h->intr[1] = hpsa_msix_entries[1].vector;
3204 h->intr[2] = hpsa_msix_entries[2].vector;
3205 h->intr[3] = hpsa_msix_entries[3].vector;
3206 h->msix_vector = 1;
3207 return;
3208 }
3209 if (err > 0) {
3210 dev_warn(&pdev->dev, "only %d MSI-X vectors "
3211 "available\n", err);
3212 goto default_int_mode;
3213 } else {
3214 dev_warn(&pdev->dev, "MSI-X init failed %d\n",
3215 err);
3216 goto default_int_mode;
3217 }
3218 }
3219 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
3220 dev_info(&pdev->dev, "MSI\n");
3221 if (!pci_enable_msi(pdev))
3222 h->msi_vector = 1;
3223 else
3224 dev_warn(&pdev->dev, "MSI init failed\n");
3225 }
3226default_int_mode:
3227#endif /* CONFIG_PCI_MSI */
3228 /* if we get here we're going to use the default interrupt mode */
303932fd 3229 h->intr[PERF_MODE_INT] = pdev->irq;
edd16368
SC
3230}
3231
3232static int hpsa_pci_init(struct ctlr_info *h, struct pci_dev *pdev)
3233{
3234 ushort subsystem_vendor_id, subsystem_device_id, command;
01a02ffc
SC
3235 u32 board_id, scratchpad = 0;
3236 u64 cfg_offset;
3237 u32 cfg_base_addr;
3238 u64 cfg_base_addr_index;
303932fd 3239 u32 trans_offset;
edd16368
SC
3240 int i, prod_index, err;
3241
3242 subsystem_vendor_id = pdev->subsystem_vendor;
3243 subsystem_device_id = pdev->subsystem_device;
01a02ffc 3244 board_id = (((u32) (subsystem_device_id << 16) & 0xffff0000) |
edd16368
SC
3245 subsystem_vendor_id);
3246
3247 for (i = 0; i < ARRAY_SIZE(products); i++)
3248 if (board_id == products[i].board_id)
3249 break;
3250
3251 prod_index = i;
3252
3253 if (prod_index == ARRAY_SIZE(products)) {
3254 prod_index--;
3255 if (subsystem_vendor_id != PCI_VENDOR_ID_HP ||
3256 !hpsa_allow_any) {
3257 dev_warn(&pdev->dev, "unrecognized board ID:"
3258 " 0x%08lx, ignoring.\n",
3259 (unsigned long) board_id);
3260 return -ENODEV;
3261 }
3262 }
3263 /* check to see if controller has been disabled
3264 * BEFORE trying to enable it
3265 */
3266 (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
3267 if (!(command & 0x02)) {
3268 dev_warn(&pdev->dev, "controller appears to be disabled\n");
3269 return -ENODEV;
3270 }
3271
3272 err = pci_enable_device(pdev);
3273 if (err) {
3274 dev_warn(&pdev->dev, "unable to enable PCI device\n");
3275 return err;
3276 }
3277
3278 err = pci_request_regions(pdev, "hpsa");
3279 if (err) {
3280 dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
3281 return err;
3282 }
3283
3284 /* If the kernel supports MSI/MSI-X we will try to enable that,
3285 * else we use the IO-APIC interrupt assigned to us by system ROM.
3286 */
3287 hpsa_interrupt_mode(h, pdev, board_id);
3288
3289 /* find the memory BAR */
3290 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3291 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
3292 break;
3293 }
3294 if (i == DEVICE_COUNT_RESOURCE) {
3295 dev_warn(&pdev->dev, "no memory BAR found\n");
3296 err = -ENODEV;
3297 goto err_out_free_res;
3298 }
3299
3300 h->paddr = pci_resource_start(pdev, i); /* addressing mode bits
3301 * already removed
3302 */
3303
3304 h->vaddr = remap_pci_mem(h->paddr, 0x250);
3305
3306 /* Wait for the board to become ready. */
3307 for (i = 0; i < HPSA_BOARD_READY_ITERATIONS; i++) {
3308 scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
3309 if (scratchpad == HPSA_FIRMWARE_READY)
3310 break;
3311 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
3312 }
3313 if (scratchpad != HPSA_FIRMWARE_READY) {
3314 dev_warn(&pdev->dev, "board not ready, timed out.\n");
3315 err = -ENODEV;
3316 goto err_out_free_res;
3317 }
3318
3319 /* get the address index number */
3320 cfg_base_addr = readl(h->vaddr + SA5_CTCFG_OFFSET);
01a02ffc 3321 cfg_base_addr &= (u32) 0x0000ffff;
edd16368
SC
3322 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
3323 if (cfg_base_addr_index == -1) {
3324 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
3325 err = -ENODEV;
3326 goto err_out_free_res;
3327 }
3328
3329 cfg_offset = readl(h->vaddr + SA5_CTMEM_OFFSET);
3330 h->cfgtable = remap_pci_mem(pci_resource_start(pdev,
3331 cfg_base_addr_index) + cfg_offset,
3332 sizeof(h->cfgtable));
303932fd
DB
3333 /* Find performant mode table. */
3334 trans_offset = readl(&(h->cfgtable->TransMethodOffset));
3335 h->transtable = remap_pci_mem(pci_resource_start(pdev,
3336 cfg_base_addr_index)+cfg_offset+trans_offset,
3337 sizeof(*h->transtable));
edd16368 3338
303932fd
DB
3339 h->board_id = board_id;
3340 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
edd16368
SC
3341 h->product_name = products[prod_index].product_name;
3342 h->access = *(products[prod_index].access);
3343 /* Allow room for some ioctls */
3344 h->nr_cmds = h->max_commands - 4;
3345
3346 if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
3347 (readb(&h->cfgtable->Signature[1]) != 'I') ||
3348 (readb(&h->cfgtable->Signature[2]) != 'S') ||
3349 (readb(&h->cfgtable->Signature[3]) != 'S')) {
3350 dev_warn(&pdev->dev, "not a valid CISS config table\n");
3351 err = -ENODEV;
3352 goto err_out_free_res;
3353 }
3354#ifdef CONFIG_X86
3355 {
3356 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
01a02ffc 3357 u32 prefetch;
edd16368
SC
3358 prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
3359 prefetch |= 0x100;
3360 writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
3361 }
3362#endif
3363
3364 /* Disabling DMA prefetch for the P600
3365 * An ASIC bug may result in a prefetch beyond
3366 * physical memory.
3367 */
3368 if (board_id == 0x3225103C) {
01a02ffc 3369 u32 dma_prefetch;
edd16368
SC
3370 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
3371 dma_prefetch |= 0x8000;
3372 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
3373 }
3374
3375 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
3376 /* Update the field, and then ring the doorbell */
3377 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
3378 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
3379
3380 /* under certain very rare conditions, this can take awhile.
3381 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3382 * as we enter this code.)
3383 */
3384 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
3385 if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3386 break;
3387 /* delay and try again */
3388 msleep(10);
3389 }
3390
3391#ifdef HPSA_DEBUG
3392 print_cfg_table(&pdev->dev, h->cfgtable);
3393#endif /* HPSA_DEBUG */
3394
3395 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
3396 dev_warn(&pdev->dev, "unable to get board into simple mode\n");
3397 err = -ENODEV;
3398 goto err_out_free_res;
3399 }
3400 return 0;
3401
3402err_out_free_res:
3403 /*
3404 * Deliberately omit pci_disable_device(): it does something nasty to
3405 * Smart Array controllers that pci_enable_device does not undo
3406 */
3407 pci_release_regions(pdev);
3408 return err;
3409}
3410
339b2b14
SC
3411static void __devinit hpsa_hba_inquiry(struct ctlr_info *h)
3412{
3413 int rc;
3414
3415#define HBA_INQUIRY_BYTE_COUNT 64
3416 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
3417 if (!h->hba_inquiry_data)
3418 return;
3419 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
3420 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
3421 if (rc != 0) {
3422 kfree(h->hba_inquiry_data);
3423 h->hba_inquiry_data = NULL;
3424 }
3425}
3426
edd16368
SC
3427static int __devinit hpsa_init_one(struct pci_dev *pdev,
3428 const struct pci_device_id *ent)
3429{
ecd9aad4 3430 int i, rc;
edd16368
SC
3431 int dac;
3432 struct ctlr_info *h;
3433
3434 if (number_of_controllers == 0)
3435 printk(KERN_INFO DRIVER_NAME "\n");
3436 if (reset_devices) {
3437 /* Reset the controller with a PCI power-cycle */
3438 if (hpsa_hard_reset_controller(pdev) || hpsa_reset_msi(pdev))
3439 return -ENODEV;
3440
3441 /* Some devices (notably the HP Smart Array 5i Controller)
3442 need a little pause here */
3443 msleep(HPSA_POST_RESET_PAUSE_MSECS);
3444
3445 /* Now try to get the controller to respond to a no-op */
3446 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
3447 if (hpsa_noop(pdev) == 0)
3448 break;
3449 else
3450 dev_warn(&pdev->dev, "no-op failed%s\n",
3451 (i < 11 ? "; re-trying" : ""));
3452 }
3453 }
3454
303932fd
DB
3455 /* Command structures must be aligned on a 32-byte boundary because
3456 * the 5 lower bits of the address are used by the hardware. and by
3457 * the driver. See comments in hpsa.h for more info.
3458 */
3459#define COMMANDLIST_ALIGNMENT 32
3460 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
edd16368
SC
3461 h = kzalloc(sizeof(*h), GFP_KERNEL);
3462 if (!h)
ecd9aad4 3463 return -ENOMEM;
edd16368
SC
3464
3465 h->busy_initializing = 1;
3466 INIT_HLIST_HEAD(&h->cmpQ);
3467 INIT_HLIST_HEAD(&h->reqQ);
3468 mutex_init(&h->busy_shutting_down);
3469 init_completion(&h->scan_wait);
ecd9aad4
SC
3470 rc = hpsa_pci_init(h, pdev);
3471 if (rc != 0)
edd16368
SC
3472 goto clean1;
3473
3474 sprintf(h->devname, "hpsa%d", number_of_controllers);
3475 h->ctlr = number_of_controllers;
3476 number_of_controllers++;
3477 h->pdev = pdev;
3478
3479 /* configure PCI DMA stuff */
ecd9aad4
SC
3480 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3481 if (rc == 0) {
edd16368 3482 dac = 1;
ecd9aad4
SC
3483 } else {
3484 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3485 if (rc == 0) {
3486 dac = 0;
3487 } else {
3488 dev_err(&pdev->dev, "no suitable DMA available\n");
3489 goto clean1;
3490 }
edd16368
SC
3491 }
3492
3493 /* make sure the board interrupts are off */
3494 h->access.set_intr_mask(h, HPSA_INTR_OFF);
303932fd
DB
3495 rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr,
3496 IRQF_DISABLED, h->devname, h);
ecd9aad4 3497 if (rc) {
edd16368 3498 dev_err(&pdev->dev, "unable to get irq %d for %s\n",
303932fd 3499 h->intr[PERF_MODE_INT], h->devname);
edd16368
SC
3500 goto clean2;
3501 }
3502
303932fd
DB
3503 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
3504 h->devname, pdev->device,
3505 h->intr[PERF_MODE_INT], dac ? "" : " not");
edd16368
SC
3506
3507 h->cmd_pool_bits =
3508 kmalloc(((h->nr_cmds + BITS_PER_LONG -
3509 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
3510 h->cmd_pool = pci_alloc_consistent(h->pdev,
3511 h->nr_cmds * sizeof(*h->cmd_pool),
3512 &(h->cmd_pool_dhandle));
3513 h->errinfo_pool = pci_alloc_consistent(h->pdev,
3514 h->nr_cmds * sizeof(*h->errinfo_pool),
3515 &(h->errinfo_pool_dhandle));
3516 if ((h->cmd_pool_bits == NULL)
3517 || (h->cmd_pool == NULL)
3518 || (h->errinfo_pool == NULL)) {
3519 dev_err(&pdev->dev, "out of memory");
ecd9aad4 3520 rc = -ENOMEM;
edd16368
SC
3521 goto clean4;
3522 }
3523 spin_lock_init(&h->lock);
3524
3525 pci_set_drvdata(pdev, h);
3526 memset(h->cmd_pool_bits, 0,
3527 ((h->nr_cmds + BITS_PER_LONG -
3528 1) / BITS_PER_LONG) * sizeof(unsigned long));
3529
3530 hpsa_scsi_setup(h);
3531
3532 /* Turn the interrupts on so we can service requests */
3533 h->access.set_intr_mask(h, HPSA_INTR_ON);
3534
303932fd 3535 hpsa_put_ctlr_into_performant_mode(h);
339b2b14 3536 hpsa_hba_inquiry(h);
edd16368
SC
3537 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
3538 h->busy_initializing = 0;
3539 return 1;
3540
3541clean4:
3542 kfree(h->cmd_pool_bits);
3543 if (h->cmd_pool)
3544 pci_free_consistent(h->pdev,
3545 h->nr_cmds * sizeof(struct CommandList),
3546 h->cmd_pool, h->cmd_pool_dhandle);
3547 if (h->errinfo_pool)
3548 pci_free_consistent(h->pdev,
3549 h->nr_cmds * sizeof(struct ErrorInfo),
3550 h->errinfo_pool,
3551 h->errinfo_pool_dhandle);
303932fd 3552 free_irq(h->intr[PERF_MODE_INT], h);
edd16368
SC
3553clean2:
3554clean1:
3555 h->busy_initializing = 0;
3556 kfree(h);
ecd9aad4 3557 return rc;
edd16368
SC
3558}
3559
3560static void hpsa_flush_cache(struct ctlr_info *h)
3561{
3562 char *flush_buf;
3563 struct CommandList *c;
3564
3565 flush_buf = kzalloc(4, GFP_KERNEL);
3566 if (!flush_buf)
3567 return;
3568
3569 c = cmd_special_alloc(h);
3570 if (!c) {
3571 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
3572 goto out_of_memory;
3573 }
3574 fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
3575 RAID_CTLR_LUNID, TYPE_CMD);
3576 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
3577 if (c->err_info->CommandStatus != 0)
3578 dev_warn(&h->pdev->dev,
3579 "error flushing cache on controller\n");
3580 cmd_special_free(h, c);
3581out_of_memory:
3582 kfree(flush_buf);
3583}
3584
3585static void hpsa_shutdown(struct pci_dev *pdev)
3586{
3587 struct ctlr_info *h;
3588
3589 h = pci_get_drvdata(pdev);
3590 /* Turn board interrupts off and send the flush cache command
3591 * sendcmd will turn off interrupt, and send the flush...
3592 * To write all data in the battery backed cache to disks
3593 */
3594 hpsa_flush_cache(h);
3595 h->access.set_intr_mask(h, HPSA_INTR_OFF);
303932fd 3596 free_irq(h->intr[PERF_MODE_INT], h);
edd16368
SC
3597#ifdef CONFIG_PCI_MSI
3598 if (h->msix_vector)
3599 pci_disable_msix(h->pdev);
3600 else if (h->msi_vector)
3601 pci_disable_msi(h->pdev);
3602#endif /* CONFIG_PCI_MSI */
3603}
3604
3605static void __devexit hpsa_remove_one(struct pci_dev *pdev)
3606{
3607 struct ctlr_info *h;
3608
3609 if (pci_get_drvdata(pdev) == NULL) {
3610 dev_err(&pdev->dev, "unable to remove device \n");
3611 return;
3612 }
3613 h = pci_get_drvdata(pdev);
3614 mutex_lock(&h->busy_shutting_down);
3615 remove_from_scan_list(h);
3616 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
3617 hpsa_shutdown(pdev);
3618 iounmap(h->vaddr);
3619 pci_free_consistent(h->pdev,
3620 h->nr_cmds * sizeof(struct CommandList),
3621 h->cmd_pool, h->cmd_pool_dhandle);
3622 pci_free_consistent(h->pdev,
3623 h->nr_cmds * sizeof(struct ErrorInfo),
3624 h->errinfo_pool, h->errinfo_pool_dhandle);
303932fd
DB
3625 pci_free_consistent(h->pdev, h->reply_pool_size,
3626 h->reply_pool, h->reply_pool_dhandle);
edd16368 3627 kfree(h->cmd_pool_bits);
303932fd 3628 kfree(h->blockFetchTable);
339b2b14 3629 kfree(h->hba_inquiry_data);
edd16368
SC
3630 /*
3631 * Deliberately omit pci_disable_device(): it does something nasty to
3632 * Smart Array controllers that pci_enable_device does not undo
3633 */
3634 pci_release_regions(pdev);
3635 pci_set_drvdata(pdev, NULL);
3636 mutex_unlock(&h->busy_shutting_down);
3637 kfree(h);
3638}
3639
3640static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
3641 __attribute__((unused)) pm_message_t state)
3642{
3643 return -ENOSYS;
3644}
3645
3646static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
3647{
3648 return -ENOSYS;
3649}
3650
3651static struct pci_driver hpsa_pci_driver = {
3652 .name = "hpsa",
3653 .probe = hpsa_init_one,
3654 .remove = __devexit_p(hpsa_remove_one),
3655 .id_table = hpsa_pci_device_id, /* id_table */
3656 .shutdown = hpsa_shutdown,
3657 .suspend = hpsa_suspend,
3658 .resume = hpsa_resume,
3659};
3660
303932fd
DB
3661/* Fill in bucket_map[], given nsgs (the max number of
3662 * scatter gather elements supported) and bucket[],
3663 * which is an array of 8 integers. The bucket[] array
3664 * contains 8 different DMA transfer sizes (in 16
3665 * byte increments) which the controller uses to fetch
3666 * commands. This function fills in bucket_map[], which
3667 * maps a given number of scatter gather elements to one of
3668 * the 8 DMA transfer sizes. The point of it is to allow the
3669 * controller to only do as much DMA as needed to fetch the
3670 * command, with the DMA transfer size encoded in the lower
3671 * bits of the command address.
3672 */
3673static void calc_bucket_map(int bucket[], int num_buckets,
3674 int nsgs, int *bucket_map)
3675{
3676 int i, j, b, size;
3677
3678 /* even a command with 0 SGs requires 4 blocks */
3679#define MINIMUM_TRANSFER_BLOCKS 4
3680#define NUM_BUCKETS 8
3681 /* Note, bucket_map must have nsgs+1 entries. */
3682 for (i = 0; i <= nsgs; i++) {
3683 /* Compute size of a command with i SG entries */
3684 size = i + MINIMUM_TRANSFER_BLOCKS;
3685 b = num_buckets; /* Assume the biggest bucket */
3686 /* Find the bucket that is just big enough */
3687 for (j = 0; j < 8; j++) {
3688 if (bucket[j] >= size) {
3689 b = j;
3690 break;
3691 }
3692 }
3693 /* for a command with i SG entries, use bucket b. */
3694 bucket_map[i] = b;
3695 }
3696}
3697
3698static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
3699{
3700 u32 trans_support;
3701 u64 trans_offset;
3702 /* 5 = 1 s/g entry or 4k
3703 * 6 = 2 s/g entry or 8k
3704 * 8 = 4 s/g entry or 16k
3705 * 10 = 6 s/g entry or 24k
3706 */
3707 int bft[8] = {5, 6, 8, 10, 12, 20, 28, 35}; /* for scatter/gathers */
3708 int i = 0;
3709 int l = 0;
3710 unsigned long register_value;
3711
3712 trans_support = readl(&(h->cfgtable->TransportSupport));
3713 if (!(trans_support & PERFORMANT_MODE))
3714 return;
3715
3716 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
3717 h->max_sg_entries = 32;
3718 /* Performant mode ring buffer and supporting data structures */
3719 h->reply_pool_size = h->max_commands * sizeof(u64);
3720 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
3721 &(h->reply_pool_dhandle));
3722
3723 /* Need a block fetch table for performant mode */
3724 h->blockFetchTable = kmalloc(((h->max_sg_entries+1) *
3725 sizeof(u32)), GFP_KERNEL);
3726
3727 if ((h->reply_pool == NULL)
3728 || (h->blockFetchTable == NULL))
3729 goto clean_up;
3730
3731 h->reply_pool_wraparound = 1; /* spec: init to 1 */
3732
3733 /* Controller spec: zero out this buffer. */
3734 memset(h->reply_pool, 0, h->reply_pool_size);
3735 h->reply_pool_head = h->reply_pool;
3736
3737 trans_offset = readl(&(h->cfgtable->TransMethodOffset));
3738 bft[7] = h->max_sg_entries + 4;
3739 calc_bucket_map(bft, ARRAY_SIZE(bft), 32, h->blockFetchTable);
3740 for (i = 0; i < 8; i++)
3741 writel(bft[i], &h->transtable->BlockFetch[i]);
3742
3743 /* size of controller ring buffer */
3744 writel(h->max_commands, &h->transtable->RepQSize);
3745 writel(1, &h->transtable->RepQCount);
3746 writel(0, &h->transtable->RepQCtrAddrLow32);
3747 writel(0, &h->transtable->RepQCtrAddrHigh32);
3748 writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32);
3749 writel(0, &h->transtable->RepQAddr0High32);
3750 writel(CFGTBL_Trans_Performant,
3751 &(h->cfgtable->HostWrite.TransportRequest));
3752 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
3753 /* under certain very rare conditions, this can take awhile.
3754 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3755 * as we enter this code.) */
3756 for (l = 0; l < MAX_CONFIG_WAIT; l++) {
3757 register_value = readl(h->vaddr + SA5_DOORBELL);
3758 if (!(register_value & CFGTBL_ChangeReq))
3759 break;
3760 /* delay and try again */
3761 set_current_state(TASK_INTERRUPTIBLE);
3762 schedule_timeout(10);
3763 }
3764 register_value = readl(&(h->cfgtable->TransportActive));
3765 if (!(register_value & CFGTBL_Trans_Performant)) {
3766 dev_warn(&h->pdev->dev, "unable to get board into"
3767 " performant mode\n");
3768 return;
3769 }
3770
3771 /* Change the access methods to the performant access methods */
3772 h->access = SA5_performant_access;
3773 h->transMethod = CFGTBL_Trans_Performant;
3774
3775 return;
3776
3777clean_up:
3778 if (h->reply_pool)
3779 pci_free_consistent(h->pdev, h->reply_pool_size,
3780 h->reply_pool, h->reply_pool_dhandle);
3781 kfree(h->blockFetchTable);
3782}
3783
edd16368
SC
3784/*
3785 * This is it. Register the PCI driver information for the cards we control
3786 * the OS will call our registered routines when it finds one of our cards.
3787 */
3788static int __init hpsa_init(void)
3789{
3790 int err;
3791 /* Start the scan thread */
3792 hpsa_scan_thread = kthread_run(hpsa_scan_func, NULL, "hpsa_scan");
3793 if (IS_ERR(hpsa_scan_thread)) {
3794 err = PTR_ERR(hpsa_scan_thread);
3795 return -ENODEV;
3796 }
3797 err = pci_register_driver(&hpsa_pci_driver);
3798 if (err)
3799 kthread_stop(hpsa_scan_thread);
3800 return err;
3801}
3802
3803static void __exit hpsa_cleanup(void)
3804{
3805 pci_unregister_driver(&hpsa_pci_driver);
3806 kthread_stop(hpsa_scan_thread);
3807}
3808
3809module_init(hpsa_init);
3810module_exit(hpsa_cleanup);