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