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