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