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