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