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