mm-only debug patch...
[mmotm.git] / drivers / scsi / hpsa.c
blobe402155d1efe4acab2c675b033c6f9576d69fb2a
1 /*
2 * Disk Array driver for HP Smart Array SAS controllers
3 * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12 * NON INFRINGEMENT. See the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * Questions/Comments/Bugfixes to iss_storagedev@hp.com
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/types.h>
25 #include <linux/pci.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
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 "hpsa_cmd.h"
51 #include "hpsa.h"
53 /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
54 #define HPSA_DRIVER_VERSION "1.0.0"
55 #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
57 /* How long to wait (in milliseconds) for board to go into simple mode */
58 #define MAX_CONFIG_WAIT 30000
59 #define MAX_IOCTL_CONFIG_WAIT 1000
61 /*define how many times we will try a command because of bus resets */
62 #define MAX_CMD_RETRIES 3
64 /* Embedded module documentation macros - see modules.h */
65 MODULE_AUTHOR("Hewlett-Packard Company");
66 MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
67 HPSA_DRIVER_VERSION);
68 MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
69 MODULE_VERSION(HPSA_DRIVER_VERSION);
70 MODULE_LICENSE("GPL");
72 static int hpsa_allow_any;
73 module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
74 MODULE_PARM_DESC(hpsa_allow_any,
75 "Allow hpsa driver to access unknown HP Smart Array hardware");
77 /* define the PCI info for the cards we can control */
78 static const struct pci_device_id hpsa_pci_device_id[] = {
79 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3223},
80 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3234},
81 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x323D},
82 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
83 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
84 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
85 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
86 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
87 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a},
88 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b},
89 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
90 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
91 {0,}
94 MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
96 /* board_id = Subsystem Device ID & Vendor ID
97 * product = Marketing Name for the board
98 * access = Address of the struct of function pointers
100 static struct board_type products[] = {
101 {0x3223103C, "Smart Array P800", &SA5_access},
102 {0x3234103C, "Smart Array P400", &SA5_access},
103 {0x323d103c, "Smart Array P700M", &SA5_access},
104 {0x3241103C, "Smart Array P212", &SA5_access},
105 {0x3243103C, "Smart Array P410", &SA5_access},
106 {0x3245103C, "Smart Array P410i", &SA5_access},
107 {0x3247103C, "Smart Array P411", &SA5_access},
108 {0x3249103C, "Smart Array P812", &SA5_access},
109 {0x324a103C, "Smart Array P712m", &SA5_access},
110 {0x324b103C, "Smart Array P711m", &SA5_access},
111 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
114 static int number_of_controllers;
116 static irqreturn_t do_hpsa_intr(int irq, void *dev_id);
117 static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
118 static void start_io(struct ctlr_info *h);
119 static int sendcmd(__u8 cmd, struct ctlr_info *h, void *buff, size_t size,
120 __u8 page_code, unsigned char *scsi3addr, int cmd_type);
122 #ifdef CONFIG_COMPAT
123 static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
124 #endif
126 static void cmd_free(struct ctlr_info *h, struct CommandList *c);
127 static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
128 static struct CommandList *cmd_alloc(struct ctlr_info *h);
129 static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
130 static int fill_cmd(struct CommandList *c, __u8 cmd, struct ctlr_info *h,
131 void *buff, size_t size, __u8 page_code, unsigned char *scsi3addr,
132 int cmd_type);
133 static int sendcmd_core(struct ctlr_info *h, struct CommandList *c);
135 static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
136 void (*done)(struct scsi_cmnd *));
138 static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
139 static int hpsa_slave_alloc(struct scsi_device *sdev);
140 static void hpsa_slave_destroy(struct scsi_device *sdev);
142 static ssize_t raid_level_show(struct device *dev,
143 struct device_attribute *attr, char *buf);
144 static ssize_t lunid_show(struct device *dev,
145 struct device_attribute *attr, char *buf);
146 static ssize_t unique_id_show(struct device *dev,
147 struct device_attribute *attr, char *buf);
149 DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
150 DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
151 DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
153 struct device_attribute *hpsa_sdev_attrs[] = {
154 &dev_attr_raid_level,
155 &dev_attr_lunid,
156 &dev_attr_unique_id,
157 NULL,
160 static struct scsi_host_template hpsa_driver_template = {
161 .module = THIS_MODULE,
162 .name = "hpsa",
163 .proc_name = "hpsa",
164 .queuecommand = hpsa_scsi_queue_command,
165 .can_queue = 512,
166 .this_id = -1,
167 .sg_tablesize = MAXSGENTRIES,
168 .cmd_per_lun = 512,
169 .use_clustering = ENABLE_CLUSTERING,
170 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
171 .ioctl = hpsa_ioctl,
172 .slave_alloc = hpsa_slave_alloc,
173 .slave_destroy = hpsa_slave_destroy,
174 #ifdef CONFIG_COMPAT
175 .compat_ioctl = hpsa_compat_ioctl,
176 #endif
177 .sdev_attrs = hpsa_sdev_attrs,
180 /* Enqueuing and dequeuing functions for cmdlists. */
181 static inline void addQ(struct hlist_head *list, struct CommandList *c)
183 hlist_add_head(&c->list, list);
186 static void enqueue_cmd_and_start_io(struct ctlr_info *h,
187 struct CommandList *c)
189 unsigned long flags;
190 spin_lock_irqsave(&h->lock, flags);
191 addQ(&h->reqQ, c);
192 h->Qdepth++;
193 start_io(h);
194 spin_unlock_irqrestore(&h->lock, flags);
197 static inline void removeQ(struct CommandList *c)
199 if (WARN_ON(hlist_unhashed(&c->list)))
200 return;
201 hlist_del_init(&c->list);
204 static inline int is_hba_lunid(unsigned char scsi3addr[])
206 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
209 static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
211 return (scsi3addr[3] & 0xC0) == 0x40;
214 static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
215 "UNKNOWN"
217 #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
219 static ssize_t raid_level_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
222 ssize_t l = 0;
223 int rlevel;
224 struct ctlr_info *h;
225 struct scsi_device *sdev;
226 struct hpsa_scsi_dev_t *hdev;
227 unsigned long flags;
229 sdev = to_scsi_device(dev);
230 h = (struct ctlr_info *) sdev->host->hostdata[0];
231 spin_lock_irqsave(&h->lock, flags);
232 hdev = sdev->hostdata;
233 if (!hdev) {
234 spin_unlock_irqrestore(&h->lock, flags);
235 return -ENODEV;
238 /* Is this even a logical drive? */
239 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
240 spin_unlock_irqrestore(&h->lock, flags);
241 l = snprintf(buf, PAGE_SIZE, "N/A\n");
242 return l;
245 rlevel = hdev->raid_level;
246 spin_unlock_irqrestore(&h->lock, flags);
247 if (rlevel < 0 || rlevel > RAID_UNKNOWN)
248 rlevel = RAID_UNKNOWN;
249 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
250 return l;
253 static ssize_t lunid_show(struct device *dev,
254 struct device_attribute *attr, char *buf)
256 struct ctlr_info *h;
257 struct scsi_device *sdev;
258 struct hpsa_scsi_dev_t *hdev;
259 unsigned long flags;
260 unsigned char lunid[8];
262 sdev = to_scsi_device(dev);
263 h = (struct ctlr_info *) sdev->host->hostdata[0];
264 spin_lock_irqsave(&h->lock, flags);
265 hdev = sdev->hostdata;
266 if (!hdev) {
267 spin_unlock_irqrestore(&h->lock, flags);
268 return -ENODEV;
270 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
271 spin_unlock_irqrestore(&h->lock, flags);
272 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
273 lunid[0], lunid[1], lunid[2], lunid[3],
274 lunid[4], lunid[5], lunid[6], lunid[7]);
277 static ssize_t unique_id_show(struct device *dev,
278 struct device_attribute *attr, char *buf)
280 struct ctlr_info *h;
281 struct scsi_device *sdev;
282 struct hpsa_scsi_dev_t *hdev;
283 unsigned long flags;
284 unsigned char sn[16];
286 sdev = to_scsi_device(dev);
287 h = (struct ctlr_info *) sdev->host->hostdata[0];
288 spin_lock_irqsave(&h->lock, flags);
289 hdev = sdev->hostdata;
290 if (!hdev) {
291 spin_unlock_irqrestore(&h->lock, flags);
292 return -ENODEV;
294 memcpy(sn, hdev->device_id, sizeof(sn));
295 spin_unlock_irqrestore(&h->lock, flags);
296 return snprintf(buf, 16 * 2 + 2,
297 "%02X%02X%02X%02X%02X%02X%02X%02X"
298 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
299 sn[0], sn[1], sn[2], sn[3],
300 sn[4], sn[5], sn[6], sn[7],
301 sn[8], sn[9], sn[10], sn[11],
302 sn[12], sn[13], sn[14], sn[15]);
305 static int hpsa_find_target_lun(struct ctlr_info *h,
306 unsigned char scsi3addr[], int bus, int *target, int *lun)
308 /* finds an unused bus, target, lun for a new physical device
309 * assumes h->devlock is held
311 int i, found = 0;
312 DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA);
314 memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3);
316 for (i = 0; i < h->ndevices; i++) {
317 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
318 set_bit(h->dev[i]->target, lun_taken);
321 for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) {
322 if (!test_bit(i, lun_taken)) {
323 /* *bus = 1; */
324 *target = i;
325 *lun = 0;
326 found = 1;
327 break;
330 return !found;
333 /* Add an entry into h->dev[] array. */
334 static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
335 struct hpsa_scsi_dev_t *device,
336 struct hpsa_scsi_dev_t *added[], int *nadded)
338 /* assumes h->devlock is held */
339 int n = h->ndevices;
340 int i;
341 unsigned char addr1[8], addr2[8];
342 struct hpsa_scsi_dev_t *sd;
344 if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) {
345 dev_err(&h->pdev->dev, "too many devices, some will be "
346 "inaccessible.\n");
347 return -1;
350 /* physical devices do not have lun or target assigned until now. */
351 if (device->lun != -1)
352 /* Logical device, lun is already assigned. */
353 goto lun_assigned;
355 /* If this device a non-zero lun of a multi-lun device
356 * byte 4 of the 8-byte LUN addr will contain the logical
357 * unit no, zero otherise.
359 if (device->scsi3addr[4] == 0) {
360 /* This is not a non-zero lun of a multi-lun device */
361 if (hpsa_find_target_lun(h, device->scsi3addr,
362 device->bus, &device->target, &device->lun) != 0)
363 return -1;
364 goto lun_assigned;
367 /* This is a non-zero lun of a multi-lun device.
368 * Search through our list and find the device which
369 * has the same 8 byte LUN address, excepting byte 4.
370 * Assign the same bus and target for this new LUN.
371 * Use the logical unit number from the firmware.
373 memcpy(addr1, device->scsi3addr, 8);
374 addr1[4] = 0;
375 for (i = 0; i < n; i++) {
376 sd = h->dev[i];
377 memcpy(addr2, sd->scsi3addr, 8);
378 addr2[4] = 0;
379 /* differ only in byte 4? */
380 if (memcmp(addr1, addr2, 8) == 0) {
381 device->bus = sd->bus;
382 device->target = sd->target;
383 device->lun = device->scsi3addr[4];
384 break;
387 if (device->lun == -1) {
388 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
389 " suspect firmware bug or unsupported hardware "
390 "configuration.\n");
391 return -1;
394 lun_assigned:
396 h->dev[n] = device;
397 h->ndevices++;
398 added[*nadded] = device;
399 (*nadded)++;
401 /* initially, (before registering with scsi layer) we don't
402 * know our hostno and we don't want to print anything first
403 * time anyway (the scsi layer's inquiries will show that info)
405 /* if (hostno != -1) */
406 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
407 scsi_device_type(device->devtype), hostno,
408 device->bus, device->target, device->lun);
409 return 0;
412 /* Remove an entry from h->dev[] array. */
413 static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
414 struct hpsa_scsi_dev_t *removed[], int *nremoved)
416 /* assumes h->devlock is held */
417 int i;
418 struct hpsa_scsi_dev_t *sd;
420 if (entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA)
421 BUG();
423 sd = h->dev[entry];
424 removed[*nremoved] = h->dev[entry];
425 (*nremoved)++;
427 for (i = entry; i < h->ndevices-1; i++)
428 h->dev[i] = h->dev[i+1];
429 h->ndevices--;
430 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
431 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
432 sd->lun);
435 #define SCSI3ADDR_EQ(a, b) ( \
436 (a)[7] == (b)[7] && \
437 (a)[6] == (b)[6] && \
438 (a)[5] == (b)[5] && \
439 (a)[4] == (b)[4] && \
440 (a)[3] == (b)[3] && \
441 (a)[2] == (b)[2] && \
442 (a)[1] == (b)[1] && \
443 (a)[0] == (b)[0])
445 static void fixup_botched_add(struct ctlr_info *h,
446 struct hpsa_scsi_dev_t *added)
448 /* called when scsi_add_device fails in order to re-adjust
449 * h->dev[] to match the mid layer's view.
451 unsigned long flags;
452 int i, j;
454 spin_lock_irqsave(&h->lock, flags);
455 for (i = 0; i < h->ndevices; i++) {
456 if (h->dev[i] == added) {
457 for (j = i; j < h->ndevices-1; j++)
458 h->dev[j] = h->dev[j+1];
459 h->ndevices--;
460 break;
463 spin_unlock_irqrestore(&h->lock, flags);
464 kfree(added);
467 static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
468 struct hpsa_scsi_dev_t *dev2)
470 if ((is_logical_dev_addr_mode(dev1->scsi3addr) ||
471 (dev1->lun != -1 && dev2->lun != -1)) &&
472 dev1->devtype != 0x0C)
473 return (memcmp(dev1, dev2, sizeof(*dev1)) == 0);
475 /* we compare everything except lun and target as these
476 * are not yet assigned. Compare parts likely
477 * to differ first
479 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
480 sizeof(dev1->scsi3addr)) != 0)
481 return 0;
482 if (memcmp(dev1->device_id, dev2->device_id,
483 sizeof(dev1->device_id)) != 0)
484 return 0;
485 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
486 return 0;
487 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
488 return 0;
489 if (memcmp(dev1->revision, dev2->revision, sizeof(dev1->revision)) != 0)
490 return 0;
491 if (dev1->devtype != dev2->devtype)
492 return 0;
493 if (dev1->raid_level != dev2->raid_level)
494 return 0;
495 if (dev1->bus != dev2->bus)
496 return 0;
497 return 1;
500 /* Find needle in haystack. If exact match found, return DEVICE_SAME,
501 * and return needle location in *index. If scsi3addr matches, but not
502 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
503 * location in *index. If needle not found, return DEVICE_NOT_FOUND.
505 static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
506 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
507 int *index)
509 int i;
510 #define DEVICE_NOT_FOUND 0
511 #define DEVICE_CHANGED 1
512 #define DEVICE_SAME 2
513 for (i = 0; i < haystack_size; i++) {
514 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
515 *index = i;
516 if (device_is_the_same(needle, haystack[i]))
517 return DEVICE_SAME;
518 else
519 return DEVICE_CHANGED;
522 *index = -1;
523 return DEVICE_NOT_FOUND;
526 static int adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
527 struct hpsa_scsi_dev_t *sd[], int nsds)
529 /* sd contains scsi3 addresses and devtypes, and inquiry
530 * data. This function takes what's in sd to be the current
531 * reality and updates h->dev[] to reflect that reality.
533 int i, entry, device_change, changes = 0;
534 struct hpsa_scsi_dev_t *csd;
535 unsigned long flags;
536 struct hpsa_scsi_dev_t **added, **removed;
537 int nadded, nremoved;
538 struct Scsi_Host *sh = NULL;
540 added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA,
541 GFP_KERNEL);
542 removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA,
543 GFP_KERNEL);
545 if (!added || !removed) {
546 dev_warn(&h->pdev->dev, "out of memory in "
547 "adjust_hpsa_scsi_table\n");
548 goto free_and_out;
551 spin_lock_irqsave(&h->devlock, flags);
553 /* find any devices in h->dev[] that are not in
554 * sd[] and remove them from h->dev[], and for any
555 * devices which have changed, remove the old device
556 * info and add the new device info.
558 i = 0;
559 nremoved = 0;
560 nadded = 0;
561 while (i < h->ndevices) {
562 csd = h->dev[i];
563 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
564 if (device_change == DEVICE_NOT_FOUND) {
565 changes++;
566 hpsa_scsi_remove_entry(h, hostno, i,
567 removed, &nremoved);
568 continue; /* remove ^^^, hence i not incremented */
569 } else if (device_change == DEVICE_CHANGED) {
570 changes++;
571 hpsa_scsi_remove_entry(h, hostno, i,
572 removed, &nremoved);
573 (void) hpsa_scsi_add_entry(h, hostno, sd[entry],
574 added, &nadded);
575 /* add can't fail, we just removed one. */
576 sd[entry] = NULL; /* prevent it from being freed */
578 i++;
581 /* Now, make sure every device listed in sd[] is also
582 * listed in h->dev[], adding them if they aren't found
585 for (i = 0; i < nsds; i++) {
586 if (!sd[i]) /* if already added above. */
587 continue;
588 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
589 h->ndevices, &entry);
590 if (device_change == DEVICE_NOT_FOUND) {
591 changes++;
592 if (hpsa_scsi_add_entry(h, hostno, sd[i],
593 added, &nadded) != 0)
594 break;
595 sd[i] = NULL; /* prevent from being freed later. */
596 } else if (device_change == DEVICE_CHANGED) {
597 /* should never happen... */
598 changes++;
599 dev_warn(&h->pdev->dev,
600 "device unexpectedly changed.\n");
601 /* but if it does happen, we just ignore that device */
604 spin_unlock_irqrestore(&h->devlock, flags);
606 /* Don't notify scsi mid layer of any changes the first time through
607 * (or if there are no changes) scsi_scan_host will do it later the
608 * first time through.
610 if (hostno == -1 || !changes)
611 goto free_and_out;
613 sh = h->scsi_host;
614 /* Notify scsi mid layer of any removed devices */
615 for (i = 0; i < nremoved; i++) {
616 struct scsi_device *sdev =
617 scsi_device_lookup(sh, removed[i]->bus,
618 removed[i]->target, removed[i]->lun);
619 if (sdev != NULL) {
620 scsi_remove_device(sdev);
621 scsi_device_put(sdev);
622 } else {
623 /* We don't expect to get here.
624 * future cmds to this device will get selection
625 * timeout as if the device was gone.
627 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
628 " for removal.", hostno, removed[i]->bus,
629 removed[i]->target, removed[i]->lun);
631 kfree(removed[i]);
632 removed[i] = NULL;
635 /* Notify scsi mid layer of any added devices */
636 for (i = 0; i < nadded; i++) {
637 if (scsi_add_device(sh, added[i]->bus,
638 added[i]->target, added[i]->lun) == 0)
639 continue;
640 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
641 "device not added.\n", hostno, added[i]->bus,
642 added[i]->target, added[i]->lun);
643 /* now we have to remove it from h->dev,
644 * since it didn't get added to scsi mid layer
646 fixup_botched_add(h, added[i]);
649 free_and_out:
650 kfree(added);
651 kfree(removed);
652 return 0;
656 * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
657 * Assume's h->devlock is held.
659 static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
660 int bus, int target, int lun)
662 int i;
663 struct hpsa_scsi_dev_t *sd;
665 for (i = 0; i < h->ndevices; i++) {
666 sd = h->dev[i];
667 if (sd->bus == bus && sd->target == target && sd->lun == lun)
668 return sd;
670 return NULL;
673 /* link sdev->hostdata to our per-device structure. */
674 static int hpsa_slave_alloc(struct scsi_device *sdev)
676 struct hpsa_scsi_dev_t *sd;
677 unsigned long flags;
678 struct ctlr_info *h;
680 h = (struct ctlr_info *) sdev->host->hostdata[0];
681 spin_lock_irqsave(&h->devlock, flags);
682 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
683 sdev_id(sdev), sdev->lun);
684 if (sd != NULL)
685 sdev->hostdata = sd;
686 spin_unlock_irqrestore(&h->devlock, flags);
687 return 0;
690 static void hpsa_slave_destroy(struct scsi_device *sdev)
692 return; /* nothing to do. */
695 static void hpsa_scsi_setup(struct ctlr_info *h)
697 h->ndevices = 0;
698 h->scsi_host = NULL;
699 spin_lock_init(&h->devlock);
700 return;
703 static void complete_scsi_command(struct CommandList *cp,
704 int timeout, __u32 tag)
706 struct scsi_cmnd *cmd;
707 struct ctlr_info *h;
708 struct ErrorInfo *ei;
710 unsigned char sense_key;
711 unsigned char asc; /* additional sense code */
712 unsigned char ascq; /* additional sense code qualifier */
714 ei = cp->err_info;
715 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
716 h = cp->h;
718 scsi_dma_unmap(cmd); /* undo the DMA mappings */
720 cmd->result = (DID_OK << 16); /* host byte */
721 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
722 cmd->result |= (ei->ScsiStatus);
724 /* copy the sense data whether we need to or not. */
725 memcpy(cmd->sense_buffer, ei->SenseInfo,
726 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
727 SCSI_SENSE_BUFFERSIZE :
728 ei->SenseLen);
729 scsi_set_resid(cmd, ei->ResidualCnt);
731 if (ei->CommandStatus == 0) {
732 cmd->scsi_done(cmd);
733 cmd_free(h, cp);
734 return;
737 /* an error has occurred */
738 switch (ei->CommandStatus) {
740 case CMD_TARGET_STATUS:
741 if (ei->ScsiStatus) {
742 /* Get sense key */
743 sense_key = 0xf & ei->SenseInfo[2];
744 /* Get additional sense code */
745 asc = ei->SenseInfo[12];
746 /* Get addition sense code qualifier */
747 ascq = ei->SenseInfo[13];
750 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
752 if (sense_key == ILLEGAL_REQUEST) {
753 /* If ASC/ASCQ indicate Logical Unit
754 * Not Supported condition,
756 if ((asc == 0x25) && (ascq == 0x0)) {
757 dev_warn(&h->pdev->dev, "cp %p "
758 "has check condition\n", cp);
759 break;
763 if (sense_key == NOT_READY) {
764 /* If Sense is Not Ready, Logical Unit
765 * Not ready, Manual Intervention
766 * required
768 if ((asc == 0x04) && (ascq == 0x03)) {
769 cmd->result = DID_NO_CONNECT << 16;
770 dev_warn(&h->pdev->dev, "cp %p "
771 "has check condition: unit "
772 "not ready, manual "
773 "intervention required\n", cp);
774 break;
779 /* Must be some other type of check condition */
780 cmd->result |= (ei->ScsiStatus < 1);
781 dev_warn(&h->pdev->dev, "cp %p has check condition: "
782 "unknown type: "
783 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
784 "Returning result: 0x%x, "
785 "cmd=[%02x %02x %02x %02x %02x "
786 "%02x %02x %02x %02x %02x]\n",
787 cp, sense_key, asc, ascq,
788 cmd->result,
789 cmd->cmnd[0], cmd->cmnd[1],
790 cmd->cmnd[2], cmd->cmnd[3],
791 cmd->cmnd[4], cmd->cmnd[5],
792 cmd->cmnd[6], cmd->cmnd[7],
793 cmd->cmnd[8], cmd->cmnd[9]);
794 break;
798 /* Problem was not a check condition
799 * Pass it up to the upper layers...
801 if (ei->ScsiStatus) {
803 cmd->result |= (ei->ScsiStatus < 1);
804 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
805 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
806 "Returning result: 0x%x\n",
807 cp, ei->ScsiStatus,
808 sense_key, asc, ascq,
809 cmd->result);
810 } else { /* scsi status is zero??? How??? */
811 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
812 "Returning no connection.\n", cp),
814 /* Ordinarily, this case should never happen,
815 * but there is a bug in some released firmware
816 * revisions that allows it to happen if, for
817 * example, a 4100 backplane loses power and
818 * the tape drive is in it. We assume that
819 * it's a fatal error of some kind because we
820 * can't show that it wasn't. We will make it
821 * look like selection timeout since that is
822 * the most common reason for this to occur,
823 * and it's severe enough.
826 cmd->result = DID_NO_CONNECT << 16;
828 break;
830 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
831 break;
832 case CMD_DATA_OVERRUN:
833 dev_warn(&h->pdev->dev, "cp %p has"
834 " completed with data overrun "
835 "reported\n", cp);
836 break;
837 case CMD_INVALID: {
838 /* print_bytes(cp, sizeof(*cp), 1, 0);
839 print_cmd(cp); */
840 /* We get CMD_INVALID if you address a non-existent device
841 * instead of a selection timeout (no response). You will
842 * see this if you yank out a drive, then try to access it.
843 * This is kind of a shame because it means that any other
844 * CMD_INVALID (e.g. driver bug) will get interpreted as a
845 * missing target. */
846 cmd->result = DID_NO_CONNECT << 16;
848 break;
849 case CMD_PROTOCOL_ERR:
850 dev_warn(&h->pdev->dev, "cp %p has "
851 "protocol error \n", cp);
852 break;
853 case CMD_HARDWARE_ERR:
854 cmd->result = DID_ERROR << 16;
855 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
856 break;
857 case CMD_CONNECTION_LOST:
858 cmd->result = DID_ERROR << 16;
859 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
860 break;
861 case CMD_ABORTED:
862 cmd->result = DID_ABORT << 16;
863 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
864 cp, ei->ScsiStatus);
865 break;
866 case CMD_ABORT_FAILED:
867 cmd->result = DID_ERROR << 16;
868 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
869 break;
870 case CMD_UNSOLICITED_ABORT:
871 cmd->result = DID_ABORT << 16;
872 dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited "
873 "abort\n", cp);
874 break;
875 case CMD_TIMEOUT:
876 cmd->result = DID_TIME_OUT << 16;
877 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
878 break;
879 default:
880 cmd->result = DID_ERROR << 16;
881 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
882 cp, ei->CommandStatus);
884 cmd->scsi_done(cmd);
885 cmd_free(h, cp);
888 static int hpsa_scsi_detect(struct ctlr_info *h)
890 struct Scsi_Host *sh;
891 int error;
893 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(*h));
894 if (sh == NULL)
895 goto fail;
897 sh->io_port = 0;
898 sh->n_io_port = 0;
899 sh->this_id = -1;
900 sh->max_channel = 3;
901 sh->max_cmd_len = MAX_COMMAND_SIZE;
902 sh->max_lun = HPSA_MAX_LUN;
903 sh->max_id = HPSA_MAX_LUN;
904 h->scsi_host = sh;
905 sh->hostdata[0] = (unsigned long) h;
906 sh->irq = h->intr[SIMPLE_MODE_INT];
907 sh->unique_id = sh->irq;
908 error = scsi_add_host(sh, &h->pdev->dev);
909 if (error)
910 goto fail_host_put;
911 scsi_scan_host(sh);
912 return 0;
914 fail_host_put:
915 dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host"
916 " failed for controller %d\n", h->ctlr);
917 scsi_host_put(sh);
918 return -1;
919 fail:
920 dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc"
921 " failed for controller %d\n", h->ctlr);
922 return -1;
925 static void hpsa_unmap_one(struct pci_dev *pdev,
926 struct CommandList *cp,
927 size_t buflen,
928 int data_direction)
930 union u64bit addr64;
932 addr64.val32.lower = cp->SG[0].Addr.lower;
933 addr64.val32.upper = cp->SG[0].Addr.upper;
934 pci_unmap_single(pdev, (dma_addr_t) addr64.val,
935 buflen, data_direction);
938 static void hpsa_map_one(struct pci_dev *pdev,
939 struct CommandList *cp,
940 unsigned char *buf,
941 size_t buflen,
942 int data_direction)
944 __u64 addr64;
946 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
947 cp->Header.SGList = 0;
948 cp->Header.SGTotal = 0;
949 return;
952 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
953 cp->SG[0].Addr.lower =
954 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
955 cp->SG[0].Addr.upper =
956 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
957 cp->SG[0].Len = buflen;
958 cp->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */
959 cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
962 static void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
963 struct CommandList *c)
965 DECLARE_COMPLETION_ONSTACK(wait);
967 c->waiting = &wait;
968 enqueue_cmd_and_start_io(h, c);
969 wait_for_completion(&wait);
972 static void hpsa_scsi_interpret_error(struct CommandList *cp)
974 struct ErrorInfo *ei;
975 struct device *d = &cp->h->pdev->dev;
977 ei = cp->err_info;
978 switch (ei->CommandStatus) {
979 case CMD_TARGET_STATUS:
980 dev_warn(d, "cmd %p has completed with errors\n", cp);
981 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
982 ei->ScsiStatus);
983 if (ei->ScsiStatus == 0)
984 dev_warn(d, "SCSI status is abnormally zero. "
985 "(probably indicates selection timeout "
986 "reported incorrectly due to a known "
987 "firmware bug, circa July, 2001.)\n");
988 break;
989 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
990 dev_info(d, "UNDERRUN\n");
991 break;
992 case CMD_DATA_OVERRUN:
993 dev_warn(d, "cp %p has completed with data overrun\n", cp);
994 break;
995 case CMD_INVALID: {
996 /* controller unfortunately reports SCSI passthru's
997 * to non-existent targets as invalid commands.
999 dev_warn(d, "cp %p is reported invalid (probably means "
1000 "target device no longer present)\n", cp);
1001 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1002 print_cmd(cp); */
1004 break;
1005 case CMD_PROTOCOL_ERR:
1006 dev_warn(d, "cp %p has protocol error \n", cp);
1007 break;
1008 case CMD_HARDWARE_ERR:
1009 /* cmd->result = DID_ERROR << 16; */
1010 dev_warn(d, "cp %p had hardware error\n", cp);
1011 break;
1012 case CMD_CONNECTION_LOST:
1013 dev_warn(d, "cp %p had connection lost\n", cp);
1014 break;
1015 case CMD_ABORTED:
1016 dev_warn(d, "cp %p was aborted\n", cp);
1017 break;
1018 case CMD_ABORT_FAILED:
1019 dev_warn(d, "cp %p reports abort failed\n", cp);
1020 break;
1021 case CMD_UNSOLICITED_ABORT:
1022 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1023 break;
1024 case CMD_TIMEOUT:
1025 dev_warn(d, "cp %p timed out\n", cp);
1026 break;
1027 default:
1028 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1029 ei->CommandStatus);
1033 static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1034 unsigned char page, unsigned char *buf,
1035 unsigned char bufsize)
1037 int rc;
1038 struct CommandList *c;
1039 struct ErrorInfo *ei;
1041 c = cmd_special_alloc(h);
1043 if (c == NULL) { /* trouble... */
1044 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1045 return -1;
1048 rc = fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr,
1049 TYPE_CMD);
1050 if (rc == 0) {
1051 hpsa_scsi_do_simple_cmd_core(h, c);
1052 hpsa_unmap_one(h->pdev, c, bufsize, PCI_DMA_FROMDEVICE);
1054 ei = c->err_info;
1055 if (ei->CommandStatus != 0 &&
1056 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1057 hpsa_scsi_interpret_error(c);
1058 rc = -1;
1061 cmd_special_free(h, c);
1062 return rc;
1065 static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
1067 int rc;
1068 struct CommandList *c;
1069 struct ErrorInfo *ei;
1071 c = cmd_special_alloc(h);
1073 if (c == NULL) { /* trouble... */
1074 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1075 return -1;
1078 rc = fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr,
1079 TYPE_MSG);
1080 if (rc != 0)
1081 goto out;
1083 hpsa_scsi_do_simple_cmd_core(h, c);
1084 /* no unmap needed here because no data xfer. */
1086 ei = c->err_info;
1087 if (ei->CommandStatus != 0) {
1088 hpsa_scsi_interpret_error(c);
1089 rc = -1;
1091 out:
1092 cmd_special_free(h, c);
1093 return rc;
1096 static void hpsa_get_raid_level(struct ctlr_info *h,
1097 unsigned char *scsi3addr, unsigned char *raid_level)
1099 int rc;
1100 unsigned char *buf;
1102 *raid_level = RAID_UNKNOWN;
1103 buf = kzalloc(64, GFP_KERNEL);
1104 if (!buf)
1105 return;
1106 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1107 if (rc == 0)
1108 *raid_level = buf[8];
1109 if (*raid_level > RAID_UNKNOWN)
1110 *raid_level = RAID_UNKNOWN;
1111 kfree(buf);
1112 return;
1115 /* Get the device id from inquiry page 0x83 */
1116 static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
1117 unsigned char *device_id, int buflen)
1119 int rc;
1120 unsigned char *buf;
1122 if (buflen > 16)
1123 buflen = 16;
1124 buf = kzalloc(64, GFP_KERNEL);
1125 if (!buf)
1126 return -1;
1127 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1128 if (rc == 0)
1129 memcpy(device_id, &buf[8], buflen);
1130 kfree(buf);
1131 return rc != 0;
1134 static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
1135 struct ReportLUNdata *buf, int bufsize,
1136 int extended_response)
1138 int rc;
1139 struct CommandList *c;
1140 unsigned char scsi3addr[8];
1141 struct ErrorInfo *ei;
1143 c = cmd_special_alloc(h);
1144 if (c == NULL) { /* trouble... */
1145 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1146 return -1;
1149 memset(&scsi3addr[0], 0, 8); /* address the controller */
1151 rc = fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
1152 buf, bufsize, 0, scsi3addr, TYPE_CMD);
1153 if (rc != 0)
1154 goto out;
1156 if (extended_response)
1157 c->Request.CDB[1] = extended_response;
1158 hpsa_scsi_do_simple_cmd_core(h, c);
1159 hpsa_unmap_one(h->pdev, c, bufsize, PCI_DMA_FROMDEVICE);
1160 ei = c->err_info;
1161 if (ei->CommandStatus != 0 &&
1162 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1163 hpsa_scsi_interpret_error(c);
1164 rc = -1;
1166 out:
1167 cmd_special_free(h, c);
1168 return rc;
1171 static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
1172 struct ReportLUNdata *buf,
1173 int bufsize, int extended_response)
1175 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
1178 static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
1179 struct ReportLUNdata *buf, int bufsize)
1181 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
1184 static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
1185 int bus, int target, int lun)
1187 device->bus = bus;
1188 device->target = target;
1189 device->lun = lun;
1192 static int hpsa_update_device_info(struct ctlr_info *h,
1193 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device)
1195 #define OBDR_TAPE_INQ_SIZE 49
1196 unsigned char *inq_buff = NULL;
1198 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1199 if (!inq_buff)
1200 goto bail_out;
1202 memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE);
1203 /* Do an inquiry to the device to see what it is. */
1204 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1205 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1206 /* Inquiry failed (msg printed already) */
1207 dev_err(&h->pdev->dev,
1208 "hpsa_update_device_info: inquiry failed\n");
1209 goto bail_out;
1212 /* As a side effect, record the firmware version number
1213 * if we happen to be talking to the RAID controller.
1215 if (is_hba_lunid(scsi3addr))
1216 memcpy(h->firm_ver, &inq_buff[32], 4);
1218 this_device->devtype = (inq_buff[0] & 0x1f);
1219 memcpy(this_device->scsi3addr, scsi3addr, 8);
1220 memcpy(this_device->vendor, &inq_buff[8],
1221 sizeof(this_device->vendor));
1222 memcpy(this_device->model, &inq_buff[16],
1223 sizeof(this_device->model));
1224 memcpy(this_device->revision, &inq_buff[32],
1225 sizeof(this_device->revision));
1226 memset(this_device->device_id, 0,
1227 sizeof(this_device->device_id));
1228 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
1229 sizeof(this_device->device_id));
1231 if (this_device->devtype == TYPE_DISK &&
1232 is_logical_dev_addr_mode(scsi3addr))
1233 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
1234 else
1235 this_device->raid_level = RAID_UNKNOWN;
1237 kfree(inq_buff);
1238 return 0;
1240 bail_out:
1241 kfree(inq_buff);
1242 return 1;
1245 static unsigned char *msa2xxx_model[] = {
1246 "MSA2012",
1247 "MSA2024",
1248 "MSA2312",
1249 "MSA2324",
1250 NULL,
1253 static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1255 int i;
1257 for (i = 0; msa2xxx_model[i]; i++)
1258 if (strncmp(device->model, msa2xxx_model[i],
1259 strlen(msa2xxx_model[i])) == 0)
1260 return 1;
1261 return 0;
1264 /* Helper function to assign bus, target, lun mapping of devices.
1265 * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
1266 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
1267 * Logical drive target and lun are assigned at this time, but
1268 * physical device lun and target assignment are deferred (assigned
1269 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
1271 static void figure_bus_target_lun(struct ctlr_info *h,
1272 __u8 *lunaddrbytes, int *bus, int *target, int *lun,
1273 struct hpsa_scsi_dev_t *device)
1276 __u32 lunid;
1278 if (is_logical_dev_addr_mode(lunaddrbytes)) {
1279 /* logical device */
1280 memcpy(&lunid, lunaddrbytes, sizeof(lunid));
1281 lunid = le32_to_cpu(lunid);
1283 if (is_msa2xxx(h, device)) {
1284 *bus = 1;
1285 *target = (lunid >> 16) & 0x3fff;
1286 *lun = lunid & 0x00ff;
1287 } else {
1288 *bus = 0;
1289 *lun = 0;
1290 *target = lunid & 0x3fff;
1292 } else {
1293 /* physical device */
1294 if (is_hba_lunid(lunaddrbytes))
1295 *bus = 3;
1296 else
1297 *bus = 2;
1298 *target = -1;
1299 *lun = -1; /* we will fill these in later. */
1304 * If there is no lun 0 on a target, linux won't find any devices.
1305 * For the MSA2xxx boxes, we have to manually detect the enclosure
1306 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
1307 * it for some reason. *tmpdevice is the target we're adding,
1308 * this_device is a pointer into the current element of currentsd[]
1309 * that we're building up in update_scsi_devices(), below.
1310 * lunzerobits is a bitmap that tracks which targets already have a
1311 * lun 0 assigned.
1312 * Returns 1 if an enclosure was added, 0 if not.
1314 static int add_msa2xxx_enclosure_device(struct ctlr_info *h,
1315 struct hpsa_scsi_dev_t *tmpdevice,
1316 struct hpsa_scsi_dev_t *this_device, __u8 *lunaddrbytes,
1317 int bus, int target, int lun, unsigned long lunzerobits[],
1318 int *nmsa2xxx_enclosures)
1320 unsigned char scsi3addr[8];
1322 if (test_bit(target, lunzerobits))
1323 return 0; /* There is already a lun 0 on this target. */
1325 if (!is_logical_dev_addr_mode(lunaddrbytes))
1326 return 0; /* It's the logical targets that may lack lun 0. */
1328 if (!is_msa2xxx(h, tmpdevice))
1329 return 0; /* It's only the MSA2xxx that have this problem. */
1331 if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */
1332 return 0;
1334 if (is_hba_lunid(scsi3addr))
1335 return 0; /* Don't add the RAID controller here. */
1337 #define MAX_MSA2XXX_ENCLOSURES 32
1338 if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) {
1339 dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX "
1340 "enclosures exceeded. Check your hardware "
1341 "configuration.");
1342 return 0;
1345 memset(scsi3addr, 0, 8);
1346 scsi3addr[3] = target;
1347 if (hpsa_update_device_info(h, scsi3addr, this_device))
1348 return 0;
1349 (*nmsa2xxx_enclosures)++;
1350 hpsa_set_bus_target_lun(this_device, bus, target, 0);
1351 set_bit(target, lunzerobits);
1352 return 1;
1356 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
1357 * logdev. The number of luns in physdev and logdev are returned in
1358 * *nphysicals and *nlogicals, respectively.
1359 * Returns 0 on success, -1 otherwise.
1361 static int hpsa_gather_lun_info(struct ctlr_info *h,
1362 int reportlunsize,
1363 struct ReportLUNdata *physdev, __u32 *nphysicals,
1364 struct ReportLUNdata *logdev, __u32 *nlogicals)
1366 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
1367 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
1368 return -1;
1370 memcpy(nphysicals, &physdev->LUNListLength[0], sizeof(*nphysicals));
1371 *nphysicals = be32_to_cpu(*nphysicals) / 8;
1372 #ifdef DEBUG
1373 dev_info(&h->pdev->dev, "number of physical luns is %d\n", *nphysicals);
1374 #endif
1375 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
1376 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
1377 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1378 *nphysicals - HPSA_MAX_PHYS_LUN);
1379 *nphysicals = HPSA_MAX_PHYS_LUN;
1381 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
1382 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
1383 return -1;
1385 memcpy(nlogicals, &logdev->LUNListLength[0], sizeof(*nlogicals));
1386 *nlogicals = be32_to_cpu(*nlogicals) / 8;
1387 #ifdef DEBUG
1388 dev_info(&h->pdev->dev, "number of logical luns is %d\n", *nlogicals);
1389 #endif
1390 /* Reject Logicals in excess of our max capability. */
1391 if (*nlogicals > HPSA_MAX_LUN) {
1392 dev_warn(&h->pdev->dev,
1393 "maximum logical LUNs (%d) exceeded. "
1394 "%d LUNs ignored.\n", HPSA_MAX_LUN,
1395 *nlogicals - HPSA_MAX_LUN);
1396 *nlogicals = HPSA_MAX_LUN;
1398 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
1399 dev_warn(&h->pdev->dev,
1400 "maximum logical + physical LUNs (%d) exceeded. "
1401 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1402 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
1403 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
1405 return 0;
1408 static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
1410 /* the idea here is we could get notified
1411 * that some devices have changed, so we do a report
1412 * physical luns and report logical luns cmd, and adjust
1413 * our list of devices accordingly.
1415 * The scsi3addr's of devices won't change so long as the
1416 * adapter is not reset. That means we can rescan and
1417 * tell which devices we already know about, vs. new
1418 * devices, vs. disappearing devices.
1420 struct ReportLUNdata *physdev_list = NULL;
1421 struct ReportLUNdata *logdev_list = NULL;
1422 unsigned char *inq_buff = NULL;
1423 __u32 nphysicals = 0;
1424 __u32 nlogicals = 0;
1425 __u32 ndev_allocated = 0;
1426 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
1427 int ncurrent = 0;
1428 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
1429 int i, nmsa2xxx_enclosures, ndevs_to_allocate;
1430 int bus, target, lun;
1431 DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR);
1433 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA,
1434 GFP_KERNEL);
1435 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1436 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1437 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1438 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
1440 if (!currentsd || !physdev_list || !logdev_list ||
1441 !inq_buff || !tmpdevice) {
1442 dev_err(&h->pdev->dev, "out of memory\n");
1443 goto out;
1445 memset(lunzerobits, 0, sizeof(lunzerobits));
1447 if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
1448 logdev_list, &nlogicals))
1449 goto out;
1451 /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
1452 * but each of them 4 times through different paths. The plus 1
1453 * is for the RAID controller.
1455 ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1;
1457 /* Allocate the per device structures */
1458 for (i = 0; i < ndevs_to_allocate; i++) {
1459 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
1460 if (!currentsd[i]) {
1461 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
1462 __FILE__, __LINE__);
1463 goto out;
1465 ndev_allocated++;
1468 /* adjust our table of devices */
1469 nmsa2xxx_enclosures = 0;
1470 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
1471 __u8 *lunaddrbytes;
1473 /* Figure out where the LUN ID info is coming from */
1474 if (i < nphysicals)
1475 lunaddrbytes = &physdev_list->LUN[i][0];
1476 else
1477 if (i < nphysicals + nlogicals)
1478 lunaddrbytes =
1479 &logdev_list->LUN[i-nphysicals][0];
1480 else /* jam in the RAID controller at the end */
1481 lunaddrbytes = RAID_CTLR_LUNID;
1483 /* skip masked physical devices. */
1484 if (lunaddrbytes[3] & 0xC0 && i < nphysicals)
1485 continue;
1487 /* Get device type, vendor, model, device id */
1488 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice))
1489 continue; /* skip it if we can't talk to it. */
1490 figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun,
1491 tmpdevice);
1492 this_device = currentsd[ncurrent];
1495 * For the msa2xxx boxes, we have to insert a LUN 0 which
1496 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
1497 * is nonetheless an enclosure device there. We have to
1498 * present that otherwise linux won't find anything if
1499 * there is no lun 0.
1501 if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device,
1502 lunaddrbytes, bus, target, lun, lunzerobits,
1503 &nmsa2xxx_enclosures)) {
1504 ncurrent++;
1505 this_device = currentsd[ncurrent];
1508 *this_device = *tmpdevice;
1509 hpsa_set_bus_target_lun(this_device, bus, target, lun);
1511 switch (this_device->devtype) {
1512 case TYPE_ROM: {
1513 /* We don't *really* support actual CD-ROM devices,
1514 * just "One Button Disaster Recovery" tape drive
1515 * which temporarily pretends to be a CD-ROM drive.
1516 * So we check that the device is really an OBDR tape
1517 * device by checking for "$DR-10" in bytes 43-48 of
1518 * the inquiry data.
1520 char obdr_sig[7];
1521 #define OBDR_TAPE_SIG "$DR-10"
1522 strncpy(obdr_sig, &inq_buff[43], 6);
1523 obdr_sig[6] = '\0';
1524 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1525 /* Not OBDR device, ignore it. */
1526 break;
1528 ncurrent++;
1529 break;
1530 case TYPE_DISK:
1531 if (i < nphysicals)
1532 break;
1533 ncurrent++;
1534 break;
1535 case TYPE_TAPE:
1536 case TYPE_MEDIUM_CHANGER:
1537 ncurrent++;
1538 break;
1539 case TYPE_RAID:
1540 /* Only present the Smartarray HBA as a RAID controller.
1541 * If it's a RAID controller other than the HBA itself
1542 * (an external RAID controller, MSA500 or similar)
1543 * don't present it.
1545 if (!is_hba_lunid(lunaddrbytes))
1546 break;
1547 ncurrent++;
1548 break;
1549 default:
1550 break;
1552 if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA)
1553 break;
1555 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
1556 out:
1557 kfree(tmpdevice);
1558 for (i = 0; i < ndev_allocated; i++)
1559 kfree(currentsd[i]);
1560 kfree(currentsd);
1561 kfree(inq_buff);
1562 kfree(physdev_list);
1563 kfree(logdev_list);
1564 return;
1567 /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1568 * dma mapping and fills in the scatter gather entries of the
1569 * hpsa command, cp.
1571 static int hpsa_scatter_gather(struct pci_dev *pdev,
1572 struct CommandList *cp,
1573 struct scsi_cmnd *cmd)
1575 unsigned int len;
1576 struct scatterlist *sg;
1577 __u64 addr64;
1578 int use_sg, i;
1580 BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
1582 use_sg = scsi_dma_map(cmd);
1583 if (use_sg < 0)
1584 return use_sg;
1586 if (!use_sg)
1587 goto sglist_finished;
1589 scsi_for_each_sg(cmd, sg, use_sg, i) {
1590 addr64 = (__u64) sg_dma_address(sg);
1591 len = sg_dma_len(sg);
1592 cp->SG[i].Addr.lower =
1593 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1594 cp->SG[i].Addr.upper =
1595 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1596 cp->SG[i].Len = len;
1597 cp->SG[i].Ext = 0; /* we are not chaining */
1600 sglist_finished:
1602 cp->Header.SGList = (__u8) use_sg; /* no. SGs contig in this cmd */
1603 cp->Header.SGTotal = (__u16) use_sg; /* total sgs in this cmd list */
1604 return 0;
1608 static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
1609 void (*done)(struct scsi_cmnd *))
1611 struct ctlr_info *h;
1612 struct hpsa_scsi_dev_t *dev;
1613 unsigned char scsi3addr[8];
1614 struct CommandList *c;
1615 unsigned long flags;
1617 /* Get the ptr to our adapter structure out of cmd->host. */
1618 h = (struct ctlr_info *) cmd->device->host->hostdata[0];
1619 dev = cmd->device->hostdata;
1620 if (!dev) {
1621 cmd->result = DID_NO_CONNECT << 16;
1622 done(cmd);
1623 return 0;
1625 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
1627 /* Need a lock as this is being allocated from the pool */
1628 spin_lock_irqsave(&h->lock, flags);
1629 c = cmd_alloc(h);
1630 spin_unlock_irqrestore(&h->lock, flags);
1631 if (c == NULL) { /* trouble... */
1632 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
1633 cmd->result = DID_NO_CONNECT << 16;
1634 done(cmd);
1635 return 0;
1638 /* Fill in the command list header */
1640 cmd->scsi_done = done; /* save this for use by completion code */
1642 /* save c in case we have to abort it */
1643 cmd->host_scribble = (unsigned char *) c;
1645 c->cmd_type = CMD_SCSI;
1646 c->scsi_cmd = cmd;
1647 c->Header.ReplyQueue = 0; /* unused in simple mode */
1648 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1649 c->Header.Tag.lower = c->busaddr; /* Use k. address of cmd as tag */
1651 /* Fill in the request block... */
1653 c->Request.Timeout = 0;
1654 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
1655 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
1656 c->Request.CDBLen = cmd->cmd_len;
1657 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
1658 c->Request.Type.Type = TYPE_CMD;
1659 c->Request.Type.Attribute = ATTR_SIMPLE;
1660 switch (cmd->sc_data_direction) {
1661 case DMA_TO_DEVICE:
1662 c->Request.Type.Direction = XFER_WRITE;
1663 break;
1664 case DMA_FROM_DEVICE:
1665 c->Request.Type.Direction = XFER_READ;
1666 break;
1667 case DMA_NONE:
1668 c->Request.Type.Direction = XFER_NONE;
1669 break;
1670 case DMA_BIDIRECTIONAL:
1671 /* This can happen if a buggy application does a scsi passthru
1672 * and sets both inlen and outlen to non-zero. ( see
1673 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1676 c->Request.Type.Direction = XFER_RSVD;
1677 /* This is technically wrong, and hpsa controllers should
1678 * reject it with CMD_INVALID, which is the most correct
1679 * response, but non-fibre backends appear to let it
1680 * slide by, and give the same results as if this field
1681 * were set correctly. Either way is acceptable for
1682 * our purposes here.
1685 break;
1687 default:
1688 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
1689 cmd->sc_data_direction);
1690 BUG();
1691 break;
1694 if (hpsa_scatter_gather(h->pdev, c, cmd) < 0) { /* Fill SG list */
1695 cmd_free(h, c);
1696 return SCSI_MLQUEUE_HOST_BUSY;
1698 enqueue_cmd_and_start_io(h, c);
1699 /* the cmd'll come back via intr handler in complete_scsi_command() */
1700 return 0;
1703 static void hpsa_unregister_scsi(struct ctlr_info *h)
1705 /* we are being forcibly unloaded, and may not refuse. */
1706 scsi_remove_host(h->scsi_host);
1707 scsi_host_put(h->scsi_host);
1708 h->scsi_host = NULL;
1711 static int hpsa_register_scsi(struct ctlr_info *h)
1713 int rc;
1715 hpsa_update_scsi_devices(h, -1);
1716 rc = hpsa_scsi_detect(h);
1717 if (rc != 0)
1718 dev_err(&h->pdev->dev, "hpsa_register_scsi: failed"
1719 " hpsa_scsi_detect(), rc is %d\n", rc);
1720 return rc;
1723 static int wait_for_device_to_become_ready(struct ctlr_info *h,
1724 unsigned char lunaddr[])
1726 int rc;
1727 int count = 0;
1728 int waittime = HZ;
1729 struct CommandList *c;
1731 c = cmd_special_alloc(h);
1732 if (!c) {
1733 dev_warn(&h->pdev->dev, "out of memory in "
1734 "wait_for_device_to_become_ready.\n");
1735 return IO_ERROR;
1738 /* Send test unit ready until device ready, or give up. */
1739 while (count < HPSA_TUR_RETRY_LIMIT) {
1741 /* Wait for a bit. do this first, because if we send
1742 * the TUR right away, the reset will just abort it.
1744 set_current_state(TASK_UNINTERRUPTIBLE);
1745 schedule_timeout(waittime);
1746 count++;
1748 /* Increase wait time with each try, up to a point. */
1749 if (waittime < (HZ * HPSA_MAX_WAIT_INTERVAL_SECS))
1750 waittime = waittime * 2;
1752 /* Send the Test Unit Ready */
1753 rc = fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0,
1754 lunaddr, TYPE_CMD);
1755 if (rc != 0) {
1756 /* We don't expect to get in here */
1757 dev_warn(&h->pdev->dev, "fill_cmd failed at %s:%d\n",
1758 __FILE__, __LINE__);
1759 break;
1762 hpsa_scsi_do_simple_cmd_core(h, c);
1763 /* no unmap needed here because no data xfer. */
1765 if (c->err_info->CommandStatus == CMD_SUCCESS)
1766 break;
1768 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
1769 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
1770 (c->err_info->SenseInfo[2] == NO_SENSE ||
1771 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
1772 break;
1774 dev_warn(&h->pdev->dev, "waiting %d secs "
1775 "for device to become ready.\n", waittime / HZ);
1776 rc = 1; /* device not ready. */
1779 if (rc)
1780 dev_warn(&h->pdev->dev, "giving up on device.\n");
1781 else
1782 dev_warn(&h->pdev->dev, "device is ready.\n");
1784 cmd_special_free(h, c);
1785 return rc;
1788 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
1789 * complaining. Doing a host- or bus-reset can't do anything good here.
1791 static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
1793 int rc;
1794 struct ctlr_info *h;
1795 struct hpsa_scsi_dev_t *dev;
1797 /* find the controller to which the command to be aborted was sent */
1798 h = (struct ctlr_info *) scsicmd->device->host->hostdata[0];
1799 if (h == NULL) /* paranoia */
1800 return FAILED;
1801 dev_warn(&h->pdev->dev, "resetting drive\n");
1803 dev = scsicmd->device->hostdata;
1804 if (!dev) {
1805 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
1806 "device lookup failed.\n");
1807 return FAILED;
1809 /* send a reset to the SCSI LUN which the command was sent to */
1810 rc = hpsa_send_reset(h, dev->scsi3addr);
1811 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
1812 return SUCCESS;
1814 dev_warn(&h->pdev->dev, "resetting device failed.\n");
1815 return FAILED;
1819 * For operations that cannot sleep, a command block is allocated at init,
1820 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
1821 * which ones are free or in use. Lock must be held when calling this.
1822 * cmd_free() is the complement.
1824 static struct CommandList *cmd_alloc(struct ctlr_info *h)
1826 struct CommandList *c;
1827 int i;
1828 union u64bit temp64;
1829 dma_addr_t cmd_dma_handle, err_dma_handle;
1831 do {
1832 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
1833 if (i == h->nr_cmds)
1834 return NULL;
1835 } while (test_and_set_bit
1836 (i & (BITS_PER_LONG - 1),
1837 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
1838 c = h->cmd_pool + i;
1839 memset(c, 0, sizeof(*c));
1840 cmd_dma_handle = h->cmd_pool_dhandle
1841 + i * sizeof(*c);
1842 c->err_info = h->errinfo_pool + i;
1843 memset(c->err_info, 0, sizeof(*c->err_info));
1844 err_dma_handle = h->errinfo_pool_dhandle
1845 + i * sizeof(*c->err_info);
1846 h->nr_allocs++;
1848 c->cmdindex = i;
1850 INIT_HLIST_NODE(&c->list);
1851 c->busaddr = (__u32) cmd_dma_handle;
1852 temp64.val = (__u64) err_dma_handle;
1853 c->ErrDesc.Addr.lower = temp64.val32.lower;
1854 c->ErrDesc.Addr.upper = temp64.val32.upper;
1855 c->ErrDesc.Len = sizeof(*c->err_info);
1857 c->h = h;
1858 return c;
1861 /* For operations that can wait for kmalloc to possibly sleep,
1862 * this routine can be called. Lock need not be held to call
1863 * cmd_special_alloc. cmd_special_free() is the complement.
1865 static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
1867 struct CommandList *c;
1868 union u64bit temp64;
1869 dma_addr_t cmd_dma_handle, err_dma_handle;
1871 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
1872 if (c == NULL)
1873 return NULL;
1874 memset(c, 0, sizeof(*c));
1876 c->cmdindex = -1;
1878 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
1879 &err_dma_handle);
1881 if (c->err_info == NULL) {
1882 pci_free_consistent(h->pdev,
1883 sizeof(*c), c, cmd_dma_handle);
1884 return NULL;
1886 memset(c->err_info, 0, sizeof(*c->err_info));
1888 INIT_HLIST_NODE(&c->list);
1889 c->busaddr = (__u32) cmd_dma_handle;
1890 temp64.val = (__u64) err_dma_handle;
1891 c->ErrDesc.Addr.lower = temp64.val32.lower;
1892 c->ErrDesc.Addr.upper = temp64.val32.upper;
1893 c->ErrDesc.Len = sizeof(*c->err_info);
1895 c->h = h;
1896 return c;
1899 static void cmd_free(struct ctlr_info *h, struct CommandList *c)
1901 int i;
1903 i = c - h->cmd_pool;
1904 clear_bit(i & (BITS_PER_LONG - 1),
1905 h->cmd_pool_bits + (i / BITS_PER_LONG));
1906 h->nr_frees++;
1909 static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
1911 union u64bit temp64;
1913 temp64.val32.lower = c->ErrDesc.Addr.lower;
1914 temp64.val32.upper = c->ErrDesc.Addr.upper;
1915 pci_free_consistent(h->pdev, sizeof(*c->err_info),
1916 c->err_info, (dma_addr_t) temp64.val);
1917 pci_free_consistent(h->pdev, sizeof(*c),
1918 c, (dma_addr_t) c->busaddr);
1921 #ifdef CONFIG_COMPAT
1923 static int do_ioctl(struct scsi_device *dev, int cmd, void *arg)
1925 int ret;
1927 lock_kernel();
1928 ret = hpsa_ioctl(dev, cmd, arg);
1929 unlock_kernel();
1930 return ret;
1933 static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg);
1934 static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
1935 int cmd, void *arg);
1937 static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
1939 switch (cmd) {
1940 case CCISS_GETPCIINFO:
1941 case CCISS_GETINTINFO:
1942 case CCISS_SETINTINFO:
1943 case CCISS_GETNODENAME:
1944 case CCISS_SETNODENAME:
1945 case CCISS_GETHEARTBEAT:
1946 case CCISS_GETBUSTYPES:
1947 case CCISS_GETFIRMVER:
1948 case CCISS_GETDRIVVER:
1949 case CCISS_REVALIDVOLS:
1950 case CCISS_DEREGDISK:
1951 case CCISS_REGNEWDISK:
1952 case CCISS_REGNEWD:
1953 case CCISS_RESCANDISK:
1954 case CCISS_GETLUNINFO:
1955 return do_ioctl(dev, cmd, arg);
1957 case CCISS_PASSTHRU32:
1958 return hpsa_ioctl32_passthru(dev, cmd, arg);
1959 case CCISS_BIG_PASSTHRU32:
1960 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
1962 default:
1963 return -ENOIOCTLCMD;
1967 static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
1969 IOCTL32_Command_struct __user *arg32 =
1970 (IOCTL32_Command_struct __user *) arg;
1971 IOCTL_Command_struct arg64;
1972 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
1973 int err;
1974 u32 cp;
1976 err = 0;
1977 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
1978 sizeof(arg64.LUN_info));
1979 err |= copy_from_user(&arg64.Request, &arg32->Request,
1980 sizeof(arg64.Request));
1981 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
1982 sizeof(arg64.error_info));
1983 err |= get_user(arg64.buf_size, &arg32->buf_size);
1984 err |= get_user(cp, &arg32->buf);
1985 arg64.buf = compat_ptr(cp);
1986 err |= copy_to_user(p, &arg64, sizeof(arg64));
1988 if (err)
1989 return -EFAULT;
1991 err = do_ioctl(dev, CCISS_PASSTHRU, (void *)p);
1992 if (err)
1993 return err;
1994 err |= copy_in_user(&arg32->error_info, &p->error_info,
1995 sizeof(arg32->error_info));
1996 if (err)
1997 return -EFAULT;
1998 return err;
2001 static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
2002 int cmd, void *arg)
2004 BIG_IOCTL32_Command_struct __user *arg32 =
2005 (BIG_IOCTL32_Command_struct __user *) arg;
2006 BIG_IOCTL_Command_struct arg64;
2007 BIG_IOCTL_Command_struct __user *p =
2008 compat_alloc_user_space(sizeof(arg64));
2009 int err;
2010 u32 cp;
2012 err = 0;
2013 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2014 sizeof(arg64.LUN_info));
2015 err |= copy_from_user(&arg64.Request, &arg32->Request,
2016 sizeof(arg64.Request));
2017 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2018 sizeof(arg64.error_info));
2019 err |= get_user(arg64.buf_size, &arg32->buf_size);
2020 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
2021 err |= get_user(cp, &arg32->buf);
2022 arg64.buf = compat_ptr(cp);
2023 err |= copy_to_user(p, &arg64, sizeof(arg64));
2025 if (err)
2026 return -EFAULT;
2028 err = do_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
2029 if (err)
2030 return err;
2031 err |= copy_in_user(&arg32->error_info, &p->error_info,
2032 sizeof(arg32->error_info));
2033 if (err)
2034 return -EFAULT;
2035 return err;
2037 #endif
2039 static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
2041 struct hpsa_pci_info pciinfo;
2043 if (!argp)
2044 return -EINVAL;
2045 pciinfo.domain = pci_domain_nr(h->pdev->bus);
2046 pciinfo.bus = h->pdev->bus->number;
2047 pciinfo.dev_fn = h->pdev->devfn;
2048 pciinfo.board_id = h->board_id;
2049 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
2050 return -EFAULT;
2051 return 0;
2054 static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
2056 DriverVer_type DriverVer;
2057 unsigned char vmaj, vmin, vsubmin;
2058 int rc;
2060 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
2061 &vmaj, &vmin, &vsubmin);
2062 if (rc != 3) {
2063 dev_info(&h->pdev->dev, "driver version string '%s' "
2064 "unrecognized.", HPSA_DRIVER_VERSION);
2065 vmaj = 0;
2066 vmin = 0;
2067 vsubmin = 0;
2069 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
2070 if (!argp)
2071 return -EINVAL;
2072 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
2073 return -EFAULT;
2074 return 0;
2077 static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2079 IOCTL_Command_struct iocommand;
2080 struct CommandList *c;
2081 char *buff = NULL;
2082 union u64bit temp64;
2083 DECLARE_COMPLETION_ONSTACK(wait);
2085 if (!argp)
2086 return -EINVAL;
2087 if (!capable(CAP_SYS_RAWIO))
2088 return -EPERM;
2089 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
2090 return -EFAULT;
2091 if ((iocommand.buf_size < 1) &&
2092 (iocommand.Request.Type.Direction != XFER_NONE)) {
2093 return -EINVAL;
2095 if (iocommand.buf_size > 0) {
2096 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
2097 if (buff == NULL)
2098 return -EFAULT;
2100 if (iocommand.Request.Type.Direction == XFER_WRITE) {
2101 /* Copy the data into the buffer we created */
2102 if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) {
2103 kfree(buff);
2104 return -EFAULT;
2106 } else
2107 memset(buff, 0, iocommand.buf_size);
2108 c = cmd_special_alloc(h);
2109 if (c == NULL) {
2110 kfree(buff);
2111 return -ENOMEM;
2113 /* Fill in the command type */
2114 c->cmd_type = CMD_IOCTL_PEND;
2115 /* Fill in Command Header */
2116 c->Header.ReplyQueue = 0; /* unused in simple mode */
2117 if (iocommand.buf_size > 0) { /* buffer to fill */
2118 c->Header.SGList = 1;
2119 c->Header.SGTotal = 1;
2120 } else { /* no buffers to fill */
2121 c->Header.SGList = 0;
2122 c->Header.SGTotal = 0;
2124 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
2125 /* use the kernel address the cmd block for tag */
2126 c->Header.Tag.lower = c->busaddr;
2128 /* Fill in Request block */
2129 memcpy(&c->Request, &iocommand.Request,
2130 sizeof(c->Request));
2132 /* Fill in the scatter gather information */
2133 if (iocommand.buf_size > 0) {
2134 temp64.val = pci_map_single(h->pdev, buff,
2135 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
2136 c->SG[0].Addr.lower = temp64.val32.lower;
2137 c->SG[0].Addr.upper = temp64.val32.upper;
2138 c->SG[0].Len = iocommand.buf_size;
2139 c->SG[0].Ext = 0; /* we are not chaining*/
2141 c->waiting = &wait;
2142 enqueue_cmd_and_start_io(h, c);
2143 wait_for_completion(&wait);
2145 /* unlock the buffers from DMA */
2146 temp64.val32.lower = c->SG[0].Addr.lower;
2147 temp64.val32.upper = c->SG[0].Addr.upper;
2148 pci_unmap_single(h->pdev, (dma_addr_t) temp64.val, iocommand.buf_size,
2149 PCI_DMA_BIDIRECTIONAL);
2151 /* Copy the error information out */
2152 memcpy(&iocommand.error_info, c->err_info,
2153 sizeof(iocommand.error_info));
2154 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
2155 kfree(buff);
2156 cmd_special_free(h, c);
2157 return -EFAULT;
2160 if (iocommand.Request.Type.Direction == XFER_READ) {
2161 /* Copy the data out of the buffer we created */
2162 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
2163 kfree(buff);
2164 cmd_special_free(h, c);
2165 return -EFAULT;
2168 kfree(buff);
2169 cmd_special_free(h, c);
2170 return 0;
2173 static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2175 BIG_IOCTL_Command_struct *ioc;
2176 struct CommandList *c;
2177 unsigned char **buff = NULL;
2178 int *buff_size = NULL;
2179 union u64bit temp64;
2180 BYTE sg_used = 0;
2181 int status = 0;
2182 int i;
2183 DECLARE_COMPLETION_ONSTACK(wait);
2184 __u32 left;
2185 __u32 sz;
2186 BYTE __user *data_ptr;
2188 if (!argp)
2189 return -EINVAL;
2190 if (!capable(CAP_SYS_RAWIO))
2191 return -EPERM;
2192 ioc = (BIG_IOCTL_Command_struct *)
2193 kmalloc(sizeof(*ioc), GFP_KERNEL);
2194 if (!ioc) {
2195 status = -ENOMEM;
2196 goto cleanup1;
2198 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
2199 status = -EFAULT;
2200 goto cleanup1;
2202 if ((ioc->buf_size < 1) &&
2203 (ioc->Request.Type.Direction != XFER_NONE)) {
2204 status = -EINVAL;
2205 goto cleanup1;
2207 /* Check kmalloc limits using all SGs */
2208 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
2209 status = -EINVAL;
2210 goto cleanup1;
2212 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
2213 status = -EINVAL;
2214 goto cleanup1;
2216 buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
2217 if (!buff) {
2218 status = -ENOMEM;
2219 goto cleanup1;
2221 buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL);
2222 if (!buff_size) {
2223 status = -ENOMEM;
2224 goto cleanup1;
2226 left = ioc->buf_size;
2227 data_ptr = ioc->buf;
2228 while (left) {
2229 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
2230 buff_size[sg_used] = sz;
2231 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
2232 if (buff[sg_used] == NULL) {
2233 status = -ENOMEM;
2234 goto cleanup1;
2236 if (ioc->Request.Type.Direction == XFER_WRITE) {
2237 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
2238 status = -ENOMEM;
2239 goto cleanup1;
2241 } else
2242 memset(buff[sg_used], 0, sz);
2243 left -= sz;
2244 data_ptr += sz;
2245 sg_used++;
2247 c = cmd_special_alloc(h);
2248 if (c == NULL) {
2249 status = -ENOMEM;
2250 goto cleanup1;
2252 c->cmd_type = CMD_IOCTL_PEND;
2253 c->Header.ReplyQueue = 0;
2255 if (ioc->buf_size > 0) {
2256 c->Header.SGList = sg_used;
2257 c->Header.SGTotal = sg_used;
2258 } else {
2259 c->Header.SGList = 0;
2260 c->Header.SGTotal = 0;
2262 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
2263 c->Header.Tag.lower = c->busaddr;
2264 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
2265 if (ioc->buf_size > 0) {
2266 int i;
2267 for (i = 0; i < sg_used; i++) {
2268 temp64.val = pci_map_single(h->pdev, buff[i],
2269 buff_size[i], PCI_DMA_BIDIRECTIONAL);
2270 c->SG[i].Addr.lower = temp64.val32.lower;
2271 c->SG[i].Addr.upper = temp64.val32.upper;
2272 c->SG[i].Len = buff_size[i];
2273 /* we are not chaining */
2274 c->SG[i].Ext = 0;
2277 c->waiting = &wait;
2278 enqueue_cmd_and_start_io(h, c);
2279 wait_for_completion(&wait);
2280 /* unlock the buffers from DMA */
2281 for (i = 0; i < sg_used; i++) {
2282 temp64.val32.lower = c->SG[i].Addr.lower;
2283 temp64.val32.upper = c->SG[i].Addr.upper;
2284 pci_unmap_single(h->pdev,
2285 (dma_addr_t) temp64.val, buff_size[i],
2286 PCI_DMA_BIDIRECTIONAL);
2288 /* Copy the error information out */
2289 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
2290 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
2291 cmd_special_free(h, c);
2292 status = -EFAULT;
2293 goto cleanup1;
2295 if (ioc->Request.Type.Direction == XFER_READ) {
2296 /* Copy the data out of the buffer we created */
2297 BYTE __user *ptr = ioc->buf;
2298 for (i = 0; i < sg_used; i++) {
2299 if (copy_to_user(ptr, buff[i], buff_size[i])) {
2300 cmd_special_free(h, c);
2301 status = -EFAULT;
2302 goto cleanup1;
2304 ptr += buff_size[i];
2307 cmd_special_free(h, c);
2308 status = 0;
2309 cleanup1:
2310 if (buff) {
2311 for (i = 0; i < sg_used; i++)
2312 kfree(buff[i]);
2313 kfree(buff);
2315 kfree(buff_size);
2316 kfree(ioc);
2317 return status;
2321 * ioctl
2323 static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
2325 struct ctlr_info *h;
2326 void __user *argp = (void __user *)arg;
2328 h = (struct ctlr_info *) dev->host->hostdata[0];
2330 switch (cmd) {
2331 case CCISS_DEREGDISK:
2332 case CCISS_REGNEWDISK:
2333 case CCISS_REGNEWD:
2334 hpsa_update_scsi_devices(h, dev->host->host_no);
2335 return 0;
2336 case CCISS_GETPCIINFO:
2337 return hpsa_getpciinfo_ioctl(h, argp);
2338 case CCISS_GETDRIVVER:
2339 return hpsa_getdrivver_ioctl(h, argp);
2340 case CCISS_PASSTHRU:
2341 return hpsa_passthru_ioctl(h, argp);
2342 case CCISS_BIG_PASSTHRU:
2343 return hpsa_big_passthru_ioctl(h, argp);
2344 default:
2345 return -ENOTTY;
2349 static int fill_cmd(struct CommandList *c, __u8 cmd, struct ctlr_info *h,
2350 void *buff, size_t size, __u8 page_code, unsigned char *scsi3addr,
2351 int cmd_type)
2353 int pci_dir = XFER_NONE;
2355 c->cmd_type = CMD_IOCTL_PEND;
2356 c->Header.ReplyQueue = 0;
2357 if (buff != NULL && size > 0) {
2358 c->Header.SGList = 1;
2359 c->Header.SGTotal = 1;
2360 } else {
2361 c->Header.SGList = 0;
2362 c->Header.SGTotal = 0;
2364 c->Header.Tag.lower = c->busaddr;
2365 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
2367 c->Request.Type.Type = cmd_type;
2368 if (cmd_type == TYPE_CMD) {
2369 switch (cmd) {
2370 case HPSA_INQUIRY:
2371 /* are we trying to read a vital product page */
2372 if (page_code != 0) {
2373 c->Request.CDB[1] = 0x01;
2374 c->Request.CDB[2] = page_code;
2376 c->Request.CDBLen = 6;
2377 c->Request.Type.Attribute = ATTR_SIMPLE;
2378 c->Request.Type.Direction = XFER_READ;
2379 c->Request.Timeout = 0;
2380 c->Request.CDB[0] = HPSA_INQUIRY;
2381 c->Request.CDB[4] = size & 0xFF;
2382 break;
2383 case HPSA_REPORT_LOG:
2384 case HPSA_REPORT_PHYS:
2385 /* Talking to controller so It's a physical command
2386 mode = 00 target = 0. Nothing to write.
2388 c->Request.CDBLen = 12;
2389 c->Request.Type.Attribute = ATTR_SIMPLE;
2390 c->Request.Type.Direction = XFER_READ;
2391 c->Request.Timeout = 0;
2392 c->Request.CDB[0] = cmd;
2393 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
2394 c->Request.CDB[7] = (size >> 16) & 0xFF;
2395 c->Request.CDB[8] = (size >> 8) & 0xFF;
2396 c->Request.CDB[9] = size & 0xFF;
2397 break;
2399 case HPSA_READ_CAPACITY:
2400 c->Request.CDBLen = 10;
2401 c->Request.Type.Attribute = ATTR_SIMPLE;
2402 c->Request.Type.Direction = XFER_READ;
2403 c->Request.Timeout = 0;
2404 c->Request.CDB[0] = cmd;
2405 break;
2406 case HPSA_CACHE_FLUSH:
2407 c->Request.CDBLen = 12;
2408 c->Request.Type.Attribute = ATTR_SIMPLE;
2409 c->Request.Type.Direction = XFER_WRITE;
2410 c->Request.Timeout = 0;
2411 c->Request.CDB[0] = BMIC_WRITE;
2412 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
2413 break;
2414 case TEST_UNIT_READY:
2415 c->Request.CDBLen = 6;
2416 c->Request.Type.Attribute = ATTR_SIMPLE;
2417 c->Request.Type.Direction = XFER_NONE;
2418 c->Request.Timeout = 0;
2419 break;
2420 default:
2421 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
2422 return IO_ERROR;
2424 } else if (cmd_type == TYPE_MSG) {
2425 switch (cmd) {
2427 case HPSA_DEVICE_RESET_MSG:
2428 c->Request.CDBLen = 16;
2429 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
2430 c->Request.Type.Attribute = ATTR_SIMPLE;
2431 c->Request.Type.Direction = XFER_NONE;
2432 c->Request.Timeout = 0; /* Don't time out */
2433 c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */
2434 c->Request.CDB[1] = 0x03; /* Reset target above */
2435 /* If bytes 4-7 are zero, it means reset the */
2436 /* LunID device */
2437 c->Request.CDB[4] = 0x00;
2438 c->Request.CDB[5] = 0x00;
2439 c->Request.CDB[6] = 0x00;
2440 c->Request.CDB[7] = 0x00;
2441 break;
2443 default:
2444 dev_warn(&h->pdev->dev, "unknown message type %d\n",
2445 cmd);
2446 return IO_ERROR;
2448 } else {
2449 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
2450 return IO_ERROR;
2453 switch (c->Request.Type.Direction) {
2454 case XFER_READ:
2455 pci_dir = PCI_DMA_FROMDEVICE;
2456 break;
2457 case XFER_WRITE:
2458 pci_dir = PCI_DMA_TODEVICE;
2459 break;
2460 case XFER_NONE:
2461 pci_dir = PCI_DMA_NONE;
2462 break;
2463 default:
2464 pci_dir = PCI_DMA_BIDIRECTIONAL;
2467 hpsa_map_one(h->pdev, c, buff, size, pci_dir);
2469 return IO_OK;
2473 * Wait polling for a command to complete.
2474 * The memory mapped FIFO is polled for the completion.
2475 * Used only at init time, interrupts from the HBA are disabled.
2477 static unsigned long pollcomplete(struct ctlr_info *h)
2479 unsigned long done;
2480 int i;
2482 /* Wait (up to HPSA_MAX_POLL_TIME_SECS) for a command to complete */
2483 for (i = HPSA_MAX_POLL_TIME_SECS * HZ; i > 0; i--) {
2484 done = h->access.command_completed(h);
2485 if (done == FIFO_EMPTY)
2486 schedule_timeout_uninterruptible(1);
2487 else
2488 return done;
2490 /* Invalid address to tell caller we ran out of time */
2491 dev_warn(&h->pdev->dev, "pollcomplete(): returning 1\n");
2492 return 1;
2495 /* Send command c to controller h and poll for it to complete.
2496 * Turns interrupts off on the board. Used at driver init time
2497 * and during SCSI error recovery.
2499 static int sendcmd_core(struct ctlr_info *h, struct CommandList *c)
2501 int i;
2502 unsigned long complete;
2503 int status = IO_ERROR;
2504 union u64bit buff_dma_handle;
2506 resend_cmd1:
2508 * Disable interrupt
2510 h->access.set_intr_mask(h, HPSA_INTR_OFF);
2512 /* Make sure there is room in the command FIFO
2513 * Actually it should be completely empty at this time
2514 * unless we are in here doing error handling for the scsi
2515 * side of the driver.
2517 for (i = 200000; i > 0; i--) {
2518 /* if fifo isn't full go */
2519 if (!(h->access.fifo_full(h)))
2520 break;
2521 udelay(10);
2522 dev_warn(&h->pdev->dev, "sendcmd FIFO full, waiting!\n");
2524 h->access.submit_command(h, c); /* Send the cmd */
2525 do {
2526 complete = pollcomplete(h);
2528 if (complete == 1) {
2529 dev_warn(&h->pdev->dev,
2530 "sendcmd timeout, no command list address "
2531 "returned!\n");
2532 status = IO_ERROR;
2533 break;
2536 /* If it's not the cmd we're looking for, save it for later */
2537 if ((complete & ~HPSA_ERROR_BIT) != c->busaddr) {
2538 dev_warn(&h->pdev->dev, "unexpected command "
2539 "completion.\n");
2540 continue;
2543 /* It is our command. If no error, we're done. */
2544 if (!(complete & HPSA_ERROR_BIT)) {
2545 status = IO_OK;
2546 break;
2549 /* There is an error... */
2551 /* if data overrun or underun on Report command ignore it */
2552 if (((c->Request.CDB[0] == HPSA_REPORT_LOG) ||
2553 (c->Request.CDB[0] == HPSA_REPORT_PHYS) ||
2554 (c->Request.CDB[0] == HPSA_INQUIRY)) &&
2555 ((c->err_info->CommandStatus == CMD_DATA_OVERRUN) ||
2556 (c->err_info->CommandStatus == CMD_DATA_UNDERRUN))) {
2557 complete = c->busaddr;
2558 status = IO_OK;
2559 break;
2561 if (c->err_info->CommandStatus == CMD_UNSOLICITED_ABORT) {
2562 dev_warn(&h->pdev->dev, "unsolicited abort %p\n", c);
2563 if (c->retry_count < MAX_CMD_RETRIES) {
2564 dev_warn(&h->pdev->dev, "retrying %p\n", c);
2565 c->retry_count++;
2566 /* erase the old error information */
2567 memset(c->err_info, 0, sizeof(c->err_info));
2568 goto resend_cmd1;
2570 dev_warn(&h->pdev->dev,
2571 "retried %p too many times\n", c);
2572 status = IO_ERROR;
2573 goto cleanup1;
2575 if (c->err_info->CommandStatus == CMD_UNABORTABLE) {
2576 dev_warn(&h->pdev->dev,
2577 "command could not be aborted.\n");
2578 status = IO_ERROR;
2579 goto cleanup1;
2581 dev_warn(&h->pdev->dev, "sendcmd error\n");
2582 dev_warn(&h->pdev->dev,
2583 "cmd = 0x%02x, CommandStatus = 0x%02x\n",
2584 c->Request.CDB[0], c->err_info->CommandStatus);
2585 if (c->err_info->CommandStatus == CMD_TARGET_STATUS) {
2586 dev_warn(&h->pdev->dev, "target status = 0x%02x\n",
2587 c->err_info->ScsiStatus);
2588 if (c->err_info->ScsiStatus == 2) /* chk cond */
2589 dev_warn(&h->pdev->dev, "sense key = 0x%02x\n",
2590 0xf & c->err_info->SenseInfo[2]);
2593 status = IO_ERROR;
2594 goto cleanup1;
2596 } while (1);
2598 cleanup1:
2600 /* unlock the data buffer from DMA */
2601 buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
2602 buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
2603 pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
2604 c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
2605 return status;
2609 * Send a command to the controller, and wait for it to complete.
2610 * Used at init time, and during SCSI error recovery.
2612 static int sendcmd(__u8 cmd, struct ctlr_info *h, void *buff, size_t size,
2613 __u8 page_code, unsigned char *scsi3addr, int cmd_type)
2615 struct CommandList *c;
2616 int status;
2618 c = cmd_alloc(h);
2619 if (c == NULL) {
2620 dev_warn(&h->pdev->dev, "unable to get memory");
2621 return IO_ERROR;
2623 status = fill_cmd(c, cmd, h, buff, size, page_code,
2624 scsi3addr, cmd_type);
2625 if (status == IO_OK)
2626 status = sendcmd_core(h, c);
2627 cmd_free(h, c);
2628 return status;
2632 * Map (physical) PCI mem into (virtual) kernel space
2634 static void __iomem *remap_pci_mem(ulong base, ulong size)
2636 ulong page_base = ((ulong) base) & PAGE_MASK;
2637 ulong page_offs = ((ulong) base) - page_base;
2638 void __iomem *page_remapped = ioremap(page_base, page_offs + size);
2640 return page_remapped ? (page_remapped + page_offs) : NULL;
2643 /* Takes cmds off the submission queue and sends them to the hardware,
2644 * then puts them on the queue of cmds waiting for completion.
2646 static void start_io(struct ctlr_info *h)
2648 struct CommandList *c;
2650 while (!hlist_empty(&h->reqQ)) {
2651 c = hlist_entry(h->reqQ.first, struct CommandList, list);
2652 /* can't do anything if fifo is full */
2653 if ((h->access.fifo_full(h))) {
2654 dev_warn(&h->pdev->dev, "fifo full\n");
2655 break;
2658 /* Get the first entry from the Request Q */
2659 removeQ(c);
2660 h->Qdepth--;
2662 /* Tell the controller execute command */
2663 h->access.submit_command(h, c);
2665 /* Put job onto the completed Q */
2666 addQ(&h->cmpQ, c);
2670 static inline unsigned long get_next_completion(struct ctlr_info *h)
2672 return h->access.command_completed(h);
2675 static inline int interrupt_pending(struct ctlr_info *h)
2677 return h->access.intr_pending(h);
2680 static inline long interrupt_not_for_us(struct ctlr_info *h)
2682 return ((h->access.intr_pending(h) == 0) ||
2683 (h->interrupts_enabled == 0));
2686 static inline int bad_tag(struct ctlr_info *h, __u32 tag_index,
2687 __u32 raw_tag)
2689 if (unlikely(tag_index >= h->nr_cmds)) {
2690 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
2691 return 1;
2693 return 0;
2696 static inline void finish_cmd(struct CommandList *c, __u32 raw_tag)
2698 removeQ(c);
2699 if (likely(c->cmd_type == CMD_SCSI))
2700 complete_scsi_command(c, 0, raw_tag);
2701 else if (c->cmd_type == CMD_IOCTL_PEND)
2702 complete(c->waiting);
2705 static irqreturn_t do_hpsa_intr(int irq, void *dev_id)
2707 struct ctlr_info *h = dev_id;
2708 struct CommandList *c;
2709 unsigned long flags;
2710 __u32 raw_tag, tag, tag_index;
2711 struct hlist_node *tmp;
2713 if (interrupt_not_for_us(h))
2714 return IRQ_NONE;
2715 spin_lock_irqsave(&h->lock, flags);
2716 while (interrupt_pending(h)) {
2717 while ((raw_tag = get_next_completion(h)) != FIFO_EMPTY) {
2718 if (likely(HPSA_TAG_CONTAINS_INDEX(raw_tag))) {
2719 tag_index = HPSA_TAG_TO_INDEX(raw_tag);
2720 if (bad_tag(h, tag_index, raw_tag))
2721 return IRQ_HANDLED;
2722 c = h->cmd_pool + tag_index;
2723 finish_cmd(c, raw_tag);
2724 continue;
2726 tag = HPSA_TAG_DISCARD_ERROR_BITS(raw_tag);
2727 c = NULL;
2728 hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
2729 if (c->busaddr == tag) {
2730 finish_cmd(c, raw_tag);
2731 break;
2736 spin_unlock_irqrestore(&h->lock, flags);
2737 return IRQ_HANDLED;
2740 /* Send a message CDB to the firmware. */
2741 static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
2742 unsigned char type)
2744 struct Command {
2745 struct CommandListHeader CommandHeader;
2746 struct RequestBlock Request;
2747 struct ErrDescriptor ErrorDescriptor;
2749 struct Command *cmd;
2750 static const size_t cmd_sz = sizeof(*cmd) +
2751 sizeof(cmd->ErrorDescriptor);
2752 dma_addr_t paddr64;
2753 uint32_t paddr32, tag;
2754 void __iomem *vaddr;
2755 int i, err;
2757 vaddr = pci_ioremap_bar(pdev, 0);
2758 if (vaddr == NULL)
2759 return -ENOMEM;
2761 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
2762 * CCISS commands, so they must be allocated from the lower 4GiB of
2763 * memory.
2765 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2766 if (err) {
2767 iounmap(vaddr);
2768 return -ENOMEM;
2771 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
2772 if (cmd == NULL) {
2773 iounmap(vaddr);
2774 return -ENOMEM;
2777 /* This must fit, because of the 32-bit consistent DMA mask. Also,
2778 * although there's no guarantee, we assume that the address is at
2779 * least 4-byte aligned (most likely, it's page-aligned).
2781 paddr32 = paddr64;
2783 cmd->CommandHeader.ReplyQueue = 0;
2784 cmd->CommandHeader.SGList = 0;
2785 cmd->CommandHeader.SGTotal = 0;
2786 cmd->CommandHeader.Tag.lower = paddr32;
2787 cmd->CommandHeader.Tag.upper = 0;
2788 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
2790 cmd->Request.CDBLen = 16;
2791 cmd->Request.Type.Type = TYPE_MSG;
2792 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
2793 cmd->Request.Type.Direction = XFER_NONE;
2794 cmd->Request.Timeout = 0; /* Don't time out */
2795 cmd->Request.CDB[0] = opcode;
2796 cmd->Request.CDB[1] = type;
2797 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
2798 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
2799 cmd->ErrorDescriptor.Addr.upper = 0;
2800 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
2802 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
2804 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
2805 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
2806 if (HPSA_TAG_DISCARD_ERROR_BITS(tag) == paddr32)
2807 break;
2808 schedule_timeout_uninterruptible(
2809 HPSA_MSG_SEND_RETRY_INTERVAL_SECS * HZ);
2812 iounmap(vaddr);
2814 /* we leak the DMA buffer here ... no choice since the controller could
2815 * still complete the command.
2817 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
2818 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
2819 opcode, type);
2820 return -ETIMEDOUT;
2823 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
2825 if (tag & HPSA_ERROR_BIT) {
2826 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
2827 opcode, type);
2828 return -EIO;
2831 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
2832 opcode, type);
2833 return 0;
2836 #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
2837 #define hpsa_noop(p) hpsa_message(p, 3, 0)
2839 static __devinit int hpsa_reset_msi(struct pci_dev *pdev)
2841 /* the #defines are stolen from drivers/pci/msi.h. */
2842 #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
2843 #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
2845 int pos;
2846 u16 control = 0;
2848 pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
2849 if (pos) {
2850 pci_read_config_word(pdev, msi_control_reg(pos), &control);
2851 if (control & PCI_MSI_FLAGS_ENABLE) {
2852 dev_info(&pdev->dev, "resetting MSI\n");
2853 pci_write_config_word(pdev, msi_control_reg(pos),
2854 control & ~PCI_MSI_FLAGS_ENABLE);
2858 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
2859 if (pos) {
2860 pci_read_config_word(pdev, msi_control_reg(pos), &control);
2861 if (control & PCI_MSIX_FLAGS_ENABLE) {
2862 dev_info(&pdev->dev, "resetting MSI-X\n");
2863 pci_write_config_word(pdev, msi_control_reg(pos),
2864 control & ~PCI_MSIX_FLAGS_ENABLE);
2868 return 0;
2871 /* This does a hard reset of the controller using PCI power management
2872 * states.
2874 static __devinit int hpsa_hard_reset_controller(struct pci_dev *pdev)
2876 u16 pmcsr, saved_config_space[32];
2877 int i, pos;
2879 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
2881 /* This is very nearly the same thing as
2883 * pci_save_state(pci_dev);
2884 * pci_set_power_state(pci_dev, PCI_D3hot);
2885 * pci_set_power_state(pci_dev, PCI_D0);
2886 * pci_restore_state(pci_dev);
2888 * but we can't use these nice canned kernel routines on
2889 * kexec, because they also check the MSI/MSI-X state in PCI
2890 * configuration space and do the wrong thing when it is
2891 * set/cleared. Also, the pci_save/restore_state functions
2892 * violate the ordering requirements for restoring the
2893 * configuration space from the CCISS document (see the
2894 * comment below). So we roll our own ....
2897 for (i = 0; i < 32; i++)
2898 pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
2900 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
2901 if (pos == 0) {
2902 dev_err(&pdev->dev,
2903 "hpsa_reset_controller: PCI PM not supported\n");
2904 return -ENODEV;
2907 /* Quoting from the Open CISS Specification: "The Power
2908 * Management Control/Status Register (CSR) controls the power
2909 * state of the device. The normal operating state is D0,
2910 * CSR=00h. The software off state is D3, CSR=03h. To reset
2911 * the controller, place the interface device in D3 then to
2912 * D0, this causes a secondary PCI reset which will reset the
2913 * controller."
2916 /* enter the D3hot power management state */
2917 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
2918 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
2919 pmcsr |= PCI_D3hot;
2920 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
2922 set_current_state(TASK_UNINTERRUPTIBLE);
2923 schedule_timeout(HZ >> 1);
2925 /* enter the D0 power management state */
2926 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
2927 pmcsr |= PCI_D0;
2928 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
2930 set_current_state(TASK_UNINTERRUPTIBLE);
2931 schedule_timeout(HZ >> 1);
2933 /* Restore the PCI configuration space. The Open CISS
2934 * Specification says, "Restore the PCI Configuration
2935 * Registers, offsets 00h through 60h. It is important to
2936 * restore the command register, 16-bits at offset 04h,
2937 * last. Do not restore the configuration status register,
2938 * 16-bits at offset 06h." Note that the offset is 2*i.
2940 for (i = 0; i < 32; i++) {
2941 if (i == 2 || i == 3)
2942 continue;
2943 pci_write_config_word(pdev, 2*i, saved_config_space[i]);
2945 wmb();
2946 pci_write_config_word(pdev, 4, saved_config_space[2]);
2948 return 0;
2952 * We cannot read the structure directly, for portability we must use
2953 * the io functions.
2954 * This is for debug only.
2956 #ifdef HPSA_DEBUG
2957 static void print_cfg_table(struct device *dev, struct CfgTable *tb)
2959 int i;
2960 char temp_name[17];
2962 dev_info(dev, "Controller Configuration information\n");
2963 dev_info(dev, "------------------------------------\n");
2964 for (i = 0; i < 4; i++)
2965 temp_name[i] = readb(&(tb->Signature[i]));
2966 temp_name[4] = '\0';
2967 dev_info(dev, " Signature = %s\n", temp_name);
2968 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
2969 dev_info(dev, " Transport methods supported = 0x%x\n",
2970 readl(&(tb->TransportSupport)));
2971 dev_info(dev, " Transport methods active = 0x%x\n",
2972 readl(&(tb->TransportActive)));
2973 dev_info(dev, " Requested transport Method = 0x%x\n",
2974 readl(&(tb->HostWrite.TransportRequest)));
2975 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
2976 readl(&(tb->HostWrite.CoalIntDelay)));
2977 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
2978 readl(&(tb->HostWrite.CoalIntCount)));
2979 dev_info(dev, " Max outstanding commands = 0x%d\n",
2980 readl(&(tb->CmdsOutMax)));
2981 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
2982 for (i = 0; i < 16; i++)
2983 temp_name[i] = readb(&(tb->ServerName[i]));
2984 temp_name[16] = '\0';
2985 dev_info(dev, " Server Name = %s\n", temp_name);
2986 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
2987 readl(&(tb->HeartBeat)));
2989 #endif /* HPSA_DEBUG */
2991 static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
2993 int i, offset, mem_type, bar_type;
2995 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
2996 return 0;
2997 offset = 0;
2998 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2999 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
3000 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
3001 offset += 4;
3002 else {
3003 mem_type = pci_resource_flags(pdev, i) &
3004 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
3005 switch (mem_type) {
3006 case PCI_BASE_ADDRESS_MEM_TYPE_32:
3007 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
3008 offset += 4; /* 32 bit */
3009 break;
3010 case PCI_BASE_ADDRESS_MEM_TYPE_64:
3011 offset += 8;
3012 break;
3013 default: /* reserved in PCI 2.2 */
3014 dev_warn(&pdev->dev,
3015 "base address is invalid\n");
3016 return -1;
3017 break;
3020 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
3021 return i + 1;
3023 return -1;
3026 /* If MSI/MSI-X is supported by the kernel we will try to enable it on
3027 * controllers that are capable. If not, we use IO-APIC mode.
3030 static void __devinit hpsa_interrupt_mode(struct ctlr_info *h,
3031 struct pci_dev *pdev, __u32 board_id)
3033 #ifdef CONFIG_PCI_MSI
3034 int err;
3035 struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1},
3036 {0, 2}, {0, 3}
3039 /* Some boards advertise MSI but don't really support it */
3040 if ((board_id == 0x40700E11) ||
3041 (board_id == 0x40800E11) ||
3042 (board_id == 0x40820E11) || (board_id == 0x40830E11))
3043 goto default_int_mode;
3044 if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
3045 dev_info(&pdev->dev, "MSIX\n");
3046 err = pci_enable_msix(pdev, hpsa_msix_entries, 4);
3047 if (!err) {
3048 h->intr[0] = hpsa_msix_entries[0].vector;
3049 h->intr[1] = hpsa_msix_entries[1].vector;
3050 h->intr[2] = hpsa_msix_entries[2].vector;
3051 h->intr[3] = hpsa_msix_entries[3].vector;
3052 h->msix_vector = 1;
3053 return;
3055 if (err > 0) {
3056 dev_warn(&pdev->dev, "only %d MSI-X vectors "
3057 "available\n", err);
3058 goto default_int_mode;
3059 } else {
3060 dev_warn(&pdev->dev, "MSI-X init failed %d\n",
3061 err);
3062 goto default_int_mode;
3065 if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
3066 dev_info(&pdev->dev, "MSI\n");
3067 if (!pci_enable_msi(pdev))
3068 h->msi_vector = 1;
3069 else
3070 dev_warn(&pdev->dev, "MSI init failed\n");
3072 default_int_mode:
3073 #endif /* CONFIG_PCI_MSI */
3074 /* if we get here we're going to use the default interrupt mode */
3075 h->intr[SIMPLE_MODE_INT] = pdev->irq;
3076 return;
3079 static int hpsa_pci_init(struct ctlr_info *h, struct pci_dev *pdev)
3081 ushort subsystem_vendor_id, subsystem_device_id, command;
3082 __u32 board_id, scratchpad = 0;
3083 __u64 cfg_offset;
3084 __u32 cfg_base_addr;
3085 __u64 cfg_base_addr_index;
3086 int i, prod_index, err;
3088 subsystem_vendor_id = pdev->subsystem_vendor;
3089 subsystem_device_id = pdev->subsystem_device;
3090 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
3091 subsystem_vendor_id);
3093 for (i = 0; i < ARRAY_SIZE(products); i++)
3094 if (board_id == products[i].board_id)
3095 break;
3097 prod_index = i;
3099 if (prod_index == ARRAY_SIZE(products)) {
3100 prod_index--;
3101 if (subsystem_vendor_id != PCI_VENDOR_ID_HP ||
3102 !hpsa_allow_any) {
3103 dev_warn(&pdev->dev, "unrecognized board ID:"
3104 " 0x%08lx, ignoring.\n",
3105 (unsigned long) board_id);
3106 return -ENODEV;
3109 /* check to see if controller has been disabled
3110 * BEFORE trying to enable it
3112 (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
3113 if (!(command & 0x02)) {
3114 dev_warn(&pdev->dev, "controller appears to be disabled\n");
3115 return -ENODEV;
3118 err = pci_enable_device(pdev);
3119 if (err) {
3120 dev_warn(&pdev->dev, "unable to enable PCI device\n");
3121 return err;
3124 err = pci_request_regions(pdev, "hpsa");
3125 if (err) {
3126 dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
3127 return err;
3130 /* If the kernel supports MSI/MSI-X we will try to enable that,
3131 * else we use the IO-APIC interrupt assigned to us by system ROM.
3133 hpsa_interrupt_mode(h, pdev, board_id);
3135 /* find the memory BAR */
3136 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3137 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
3138 break;
3140 if (i == DEVICE_COUNT_RESOURCE) {
3141 dev_warn(&pdev->dev, "no memory BAR found\n");
3142 err = -ENODEV;
3143 goto err_out_free_res;
3146 h->paddr = pci_resource_start(pdev, i); /* addressing mode bits
3147 * already removed
3150 h->vaddr = remap_pci_mem(h->paddr, 0x250);
3152 /* Wait for the board to become ready. */
3153 for (i = 0; i < HPSA_BOARD_READY_ITERATIONS; i++) {
3154 scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
3155 if (scratchpad == HPSA_FIRMWARE_READY)
3156 break;
3157 set_current_state(TASK_INTERRUPTIBLE);
3158 schedule_timeout(HPSA_BOARD_READY_POLL_INTERVAL);
3160 if (scratchpad != HPSA_FIRMWARE_READY) {
3161 dev_warn(&pdev->dev, "board not ready, timed out.\n");
3162 err = -ENODEV;
3163 goto err_out_free_res;
3166 /* get the address index number */
3167 cfg_base_addr = readl(h->vaddr + SA5_CTCFG_OFFSET);
3168 cfg_base_addr &= (__u32) 0x0000ffff;
3169 cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
3170 if (cfg_base_addr_index == -1) {
3171 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
3172 err = -ENODEV;
3173 goto err_out_free_res;
3176 cfg_offset = readl(h->vaddr + SA5_CTMEM_OFFSET);
3177 h->cfgtable = remap_pci_mem(pci_resource_start(pdev,
3178 cfg_base_addr_index) + cfg_offset,
3179 sizeof(h->cfgtable));
3180 h->board_id = board_id;
3182 /* Query controller for max supported commands: */
3183 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
3185 h->product_name = products[prod_index].product_name;
3186 h->access = *(products[prod_index].access);
3187 /* Allow room for some ioctls */
3188 h->nr_cmds = h->max_commands - 4;
3190 if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
3191 (readb(&h->cfgtable->Signature[1]) != 'I') ||
3192 (readb(&h->cfgtable->Signature[2]) != 'S') ||
3193 (readb(&h->cfgtable->Signature[3]) != 'S')) {
3194 dev_warn(&pdev->dev, "not a valid CISS config table\n");
3195 err = -ENODEV;
3196 goto err_out_free_res;
3198 #ifdef CONFIG_X86
3200 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3201 __u32 prefetch;
3202 prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
3203 prefetch |= 0x100;
3204 writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
3206 #endif
3208 /* Disabling DMA prefetch for the P600
3209 * An ASIC bug may result in a prefetch beyond
3210 * physical memory.
3212 if (board_id == 0x3225103C) {
3213 __u32 dma_prefetch;
3214 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
3215 dma_prefetch |= 0x8000;
3216 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
3219 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
3220 /* Update the field, and then ring the doorbell */
3221 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
3222 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
3224 /* under certain very rare conditions, this can take awhile.
3225 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3226 * as we enter this code.)
3228 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
3229 if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
3230 break;
3231 /* delay and try again */
3232 set_current_state(TASK_INTERRUPTIBLE);
3233 schedule_timeout(10);
3236 #ifdef HPSA_DEBUG
3237 print_cfg_table(&pdev->dev, h->cfgtable);
3238 #endif /* HPSA_DEBUG */
3240 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
3241 dev_warn(&pdev->dev, "unable to get board into simple mode\n");
3242 err = -ENODEV;
3243 goto err_out_free_res;
3245 return 0;
3247 err_out_free_res:
3249 * Deliberately omit pci_disable_device(): it does something nasty to
3250 * Smart Array controllers that pci_enable_device does not undo
3252 pci_release_regions(pdev);
3253 return err;
3256 static int __devinit hpsa_init_one(struct pci_dev *pdev,
3257 const struct pci_device_id *ent)
3259 int i;
3260 int dac;
3261 struct ctlr_info *h;
3263 if (number_of_controllers == 0)
3264 printk(KERN_INFO DRIVER_NAME "\n");
3265 if (reset_devices) {
3266 /* Reset the controller with a PCI power-cycle */
3267 if (hpsa_hard_reset_controller(pdev) || hpsa_reset_msi(pdev))
3268 return -ENODEV;
3270 /* Some devices (notably the HP Smart Array 5i Controller)
3271 need a little pause here */
3272 schedule_timeout_uninterruptible(HPSA_POST_RESET_PAUSE);
3274 /* Now try to get the controller to respond to a no-op */
3275 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
3276 if (hpsa_noop(pdev) == 0)
3277 break;
3278 else
3279 dev_warn(&pdev->dev, "no-op failed%s\n",
3280 (i < 11 ? "; re-trying" : ""));
3284 BUILD_BUG_ON(sizeof(struct CommandList) % 8);
3285 h = kzalloc(sizeof(*h), GFP_KERNEL);
3286 if (!h)
3287 return -1;
3289 INIT_HLIST_HEAD(&h->cmpQ);
3290 INIT_HLIST_HEAD(&h->reqQ);
3291 if (hpsa_pci_init(h, pdev) != 0)
3292 goto clean1;
3294 sprintf(h->devname, "hpsa%d", number_of_controllers);
3295 h->ctlr = number_of_controllers;
3296 number_of_controllers++;
3297 h->pdev = pdev;
3299 /* configure PCI DMA stuff */
3300 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
3301 dac = 1;
3302 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))
3303 dac = 0;
3304 else {
3305 dev_err(&pdev->dev, "no suitable DMA available\n");
3306 goto clean1;
3309 /* make sure the board interrupts are off */
3310 h->access.set_intr_mask(h, HPSA_INTR_OFF);
3311 if (request_irq(h->intr[SIMPLE_MODE_INT], do_hpsa_intr,
3312 IRQF_DISABLED | IRQF_SHARED, h->devname, h)) {
3313 dev_err(&pdev->dev, "unable to get irq %d for %s\n",
3314 h->intr[SIMPLE_MODE_INT], h->devname);
3315 goto clean2;
3318 dev_info(&pdev->dev, "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
3319 h->devname, pdev->device, pci_name(pdev),
3320 h->intr[SIMPLE_MODE_INT], dac ? "" : " not");
3322 h->cmd_pool_bits =
3323 kmalloc(((h->nr_cmds + BITS_PER_LONG -
3324 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
3325 h->cmd_pool = pci_alloc_consistent(h->pdev,
3326 h->nr_cmds * sizeof(*h->cmd_pool),
3327 &(h->cmd_pool_dhandle));
3328 h->errinfo_pool = pci_alloc_consistent(h->pdev,
3329 h->nr_cmds * sizeof(*h->errinfo_pool),
3330 &(h->errinfo_pool_dhandle));
3331 if ((h->cmd_pool_bits == NULL)
3332 || (h->cmd_pool == NULL)
3333 || (h->errinfo_pool == NULL)) {
3334 dev_err(&pdev->dev, "out of memory");
3335 goto clean4;
3337 spin_lock_init(&h->lock);
3339 pci_set_drvdata(pdev, h);
3340 memset(h->cmd_pool_bits, 0,
3341 ((h->nr_cmds + BITS_PER_LONG -
3342 1) / BITS_PER_LONG) * sizeof(unsigned long));
3344 hpsa_scsi_setup(h);
3346 /* Turn the interrupts on so we can service requests */
3347 h->access.set_intr_mask(h, HPSA_INTR_ON);
3349 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
3351 return 1;
3353 clean4:
3354 kfree(h->cmd_pool_bits);
3355 if (h->cmd_pool)
3356 pci_free_consistent(h->pdev,
3357 h->nr_cmds * sizeof(struct CommandList),
3358 h->cmd_pool, h->cmd_pool_dhandle);
3359 if (h->errinfo_pool)
3360 pci_free_consistent(h->pdev,
3361 h->nr_cmds * sizeof(struct ErrorInfo),
3362 h->errinfo_pool,
3363 h->errinfo_pool_dhandle);
3364 free_irq(h->intr[SIMPLE_MODE_INT], h);
3365 clean2:
3366 clean1:
3367 kfree(h);
3368 return -1;
3371 static void hpsa_shutdown(struct pci_dev *pdev)
3373 struct ctlr_info *h;
3374 char flush_buf[4];
3375 int return_code;
3377 h = pci_get_drvdata(pdev);
3378 /* Turn board interrupts off and send the flush cache command
3379 * sendcmd will turn off interrupt, and send the flush...
3380 * To write all data in the battery backed cache to disks
3382 memset(flush_buf, 0, 4);
3383 return_code = sendcmd(HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
3384 RAID_CTLR_LUNID, TYPE_CMD);
3385 if (return_code != IO_OK) {
3386 dev_warn(&pdev->dev, "error flushing cache on controller %d\n",
3387 h->ctlr);
3389 free_irq(h->intr[2], h);
3390 #ifdef CONFIG_PCI_MSI
3391 if (h->msix_vector)
3392 pci_disable_msix(h->pdev);
3393 else if (h->msi_vector)
3394 pci_disable_msi(h->pdev);
3395 #endif /* CONFIG_PCI_MSI */
3398 static void __devexit hpsa_remove_one(struct pci_dev *pdev)
3400 struct ctlr_info *h;
3402 if (pci_get_drvdata(pdev) == NULL) {
3403 dev_err(&pdev->dev, "unable to remove device \n");
3404 return;
3406 h = pci_get_drvdata(pdev);
3407 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
3408 hpsa_shutdown(pdev);
3409 iounmap(h->vaddr);
3410 pci_free_consistent(h->pdev,
3411 h->nr_cmds * sizeof(struct CommandList),
3412 h->cmd_pool, h->cmd_pool_dhandle);
3413 pci_free_consistent(h->pdev,
3414 h->nr_cmds * sizeof(struct ErrorInfo),
3415 h->errinfo_pool, h->errinfo_pool_dhandle);
3416 kfree(h->cmd_pool_bits);
3418 * Deliberately omit pci_disable_device(): it does something nasty to
3419 * Smart Array controllers that pci_enable_device does not undo
3421 pci_release_regions(pdev);
3422 pci_set_drvdata(pdev, NULL);
3423 kfree(h);
3426 static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
3427 __attribute__((unused)) pm_message_t state)
3429 return -ENOSYS;
3432 static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
3434 return -ENOSYS;
3437 static struct pci_driver hpsa_pci_driver = {
3438 .name = "hpsa",
3439 .probe = hpsa_init_one,
3440 .remove = __devexit_p(hpsa_remove_one),
3441 .id_table = hpsa_pci_device_id, /* id_table */
3442 .shutdown = hpsa_shutdown,
3443 .suspend = hpsa_suspend,
3444 .resume = hpsa_resume,
3448 * This is it. Register the PCI driver information for the cards we control
3449 * the OS will call our registered routines when it finds one of our cards.
3451 static int __init hpsa_init(void)
3453 return pci_register_driver(&hpsa_pci_driver);
3456 static void __exit hpsa_cleanup(void)
3458 pci_unregister_driver(&hpsa_pci_driver);
3461 module_init(hpsa_init);
3462 module_exit(hpsa_cleanup);