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