1edd16368SStephen M. Cameron /* 2edd16368SStephen M. Cameron * Disk Array driver for HP Smart Array SAS controllers 3edd16368SStephen M. Cameron * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P. 4edd16368SStephen M. Cameron * 5edd16368SStephen M. Cameron * This program is free software; you can redistribute it and/or modify 6edd16368SStephen M. Cameron * it under the terms of the GNU General Public License as published by 7edd16368SStephen M. Cameron * the Free Software Foundation; version 2 of the License. 8edd16368SStephen M. Cameron * 9edd16368SStephen M. Cameron * This program is distributed in the hope that it will be useful, 10edd16368SStephen M. Cameron * but WITHOUT ANY WARRANTY; without even the implied warranty of 11edd16368SStephen M. Cameron * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or 12edd16368SStephen M. Cameron * NON INFRINGEMENT. See the GNU General Public License for more details. 13edd16368SStephen M. Cameron * 14edd16368SStephen M. Cameron * You should have received a copy of the GNU General Public License 15edd16368SStephen M. Cameron * along with this program; if not, write to the Free Software 16edd16368SStephen M. Cameron * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17edd16368SStephen M. Cameron * 18edd16368SStephen M. Cameron * Questions/Comments/Bugfixes to iss_storagedev@hp.com 19edd16368SStephen M. Cameron * 20edd16368SStephen M. Cameron */ 21edd16368SStephen M. Cameron 22edd16368SStephen M. Cameron #include <linux/module.h> 23edd16368SStephen M. Cameron #include <linux/interrupt.h> 24edd16368SStephen M. Cameron #include <linux/types.h> 25edd16368SStephen M. Cameron #include <linux/pci.h> 26edd16368SStephen M. Cameron #include <linux/kernel.h> 27edd16368SStephen M. Cameron #include <linux/slab.h> 28edd16368SStephen M. Cameron #include <linux/delay.h> 29edd16368SStephen M. Cameron #include <linux/fs.h> 30edd16368SStephen M. Cameron #include <linux/timer.h> 31edd16368SStephen M. Cameron #include <linux/seq_file.h> 32edd16368SStephen M. Cameron #include <linux/init.h> 33edd16368SStephen M. Cameron #include <linux/spinlock.h> 34edd16368SStephen M. Cameron #include <linux/smp_lock.h> 35edd16368SStephen M. Cameron #include <linux/compat.h> 36edd16368SStephen M. Cameron #include <linux/blktrace_api.h> 37edd16368SStephen M. Cameron #include <linux/uaccess.h> 38edd16368SStephen M. Cameron #include <linux/io.h> 39edd16368SStephen M. Cameron #include <linux/dma-mapping.h> 40edd16368SStephen M. Cameron #include <linux/completion.h> 41edd16368SStephen M. Cameron #include <linux/moduleparam.h> 42edd16368SStephen M. Cameron #include <scsi/scsi.h> 43edd16368SStephen M. Cameron #include <scsi/scsi_cmnd.h> 44edd16368SStephen M. Cameron #include <scsi/scsi_device.h> 45edd16368SStephen M. Cameron #include <scsi/scsi_host.h> 46667e23d4SStephen M. Cameron #include <scsi/scsi_tcq.h> 47edd16368SStephen M. Cameron #include <linux/cciss_ioctl.h> 48edd16368SStephen M. Cameron #include <linux/string.h> 49edd16368SStephen M. Cameron #include <linux/bitmap.h> 50edd16368SStephen M. Cameron #include <asm/atomic.h> 51edd16368SStephen M. Cameron #include <linux/kthread.h> 52edd16368SStephen M. Cameron #include "hpsa_cmd.h" 53edd16368SStephen M. Cameron #include "hpsa.h" 54edd16368SStephen M. Cameron 55edd16368SStephen M. Cameron /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */ 5631468401SMike Miller #define HPSA_DRIVER_VERSION "2.0.2-1" 57edd16368SStephen M. Cameron #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")" 58edd16368SStephen M. Cameron 59edd16368SStephen M. Cameron /* How long to wait (in milliseconds) for board to go into simple mode */ 60edd16368SStephen M. Cameron #define MAX_CONFIG_WAIT 30000 61edd16368SStephen M. Cameron #define MAX_IOCTL_CONFIG_WAIT 1000 62edd16368SStephen M. Cameron 63edd16368SStephen M. Cameron /*define how many times we will try a command because of bus resets */ 64edd16368SStephen M. Cameron #define MAX_CMD_RETRIES 3 65edd16368SStephen M. Cameron 66edd16368SStephen M. Cameron /* Embedded module documentation macros - see modules.h */ 67edd16368SStephen M. Cameron MODULE_AUTHOR("Hewlett-Packard Company"); 68edd16368SStephen M. Cameron MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \ 69edd16368SStephen M. Cameron HPSA_DRIVER_VERSION); 70edd16368SStephen M. Cameron MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers"); 71edd16368SStephen M. Cameron MODULE_VERSION(HPSA_DRIVER_VERSION); 72edd16368SStephen M. Cameron MODULE_LICENSE("GPL"); 73edd16368SStephen M. Cameron 74edd16368SStephen M. Cameron static int hpsa_allow_any; 75edd16368SStephen M. Cameron module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR); 76edd16368SStephen M. Cameron MODULE_PARM_DESC(hpsa_allow_any, 77edd16368SStephen M. Cameron "Allow hpsa driver to access unknown HP Smart Array hardware"); 78edd16368SStephen M. Cameron 79edd16368SStephen M. Cameron /* define the PCI info for the cards we can control */ 80edd16368SStephen M. Cameron static const struct pci_device_id hpsa_pci_device_id[] = { 81edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241}, 82edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243}, 83edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245}, 84edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247}, 85edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249}, 86edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a}, 87edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b}, 88f8b01eb9SMike Miller {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233}, 89f8b01eb9SMike Miller #define PCI_DEVICE_ID_HP_CISSF 0x333f 90f8b01eb9SMike Miller {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x333F}, 91edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 92edd16368SStephen M. Cameron PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0}, 93edd16368SStephen M. Cameron {0,} 94edd16368SStephen M. Cameron }; 95edd16368SStephen M. Cameron 96edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id); 97edd16368SStephen M. Cameron 98edd16368SStephen M. Cameron /* board_id = Subsystem Device ID & Vendor ID 99edd16368SStephen M. Cameron * product = Marketing Name for the board 100edd16368SStephen M. Cameron * access = Address of the struct of function pointers 101edd16368SStephen M. Cameron */ 102edd16368SStephen M. Cameron static struct board_type products[] = { 103edd16368SStephen M. Cameron {0x3241103C, "Smart Array P212", &SA5_access}, 104edd16368SStephen M. Cameron {0x3243103C, "Smart Array P410", &SA5_access}, 105edd16368SStephen M. Cameron {0x3245103C, "Smart Array P410i", &SA5_access}, 106edd16368SStephen M. Cameron {0x3247103C, "Smart Array P411", &SA5_access}, 107edd16368SStephen M. Cameron {0x3249103C, "Smart Array P812", &SA5_access}, 108edd16368SStephen M. Cameron {0x324a103C, "Smart Array P712m", &SA5_access}, 109edd16368SStephen M. Cameron {0x324b103C, "Smart Array P711m", &SA5_access}, 110f8b01eb9SMike Miller {0x3233103C, "StorageWorks P1210m", &SA5_access}, 111f8b01eb9SMike Miller {0x333F103C, "StorageWorks P1210m", &SA5_access}, 112edd16368SStephen M. Cameron {0xFFFF103C, "Unknown Smart Array", &SA5_access}, 113edd16368SStephen M. Cameron }; 114edd16368SStephen M. Cameron 115edd16368SStephen M. Cameron static int number_of_controllers; 116edd16368SStephen M. Cameron 117edd16368SStephen M. Cameron static irqreturn_t do_hpsa_intr(int irq, void *dev_id); 118edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg); 119edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h); 120edd16368SStephen M. Cameron 121edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT 122edd16368SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg); 123edd16368SStephen M. Cameron #endif 124edd16368SStephen M. Cameron 125edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c); 126edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c); 127edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h); 128edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h); 12901a02ffcSStephen M. Cameron static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h, 13001a02ffcSStephen M. Cameron void *buff, size_t size, u8 page_code, unsigned char *scsi3addr, 131edd16368SStephen M. Cameron int cmd_type); 132edd16368SStephen M. Cameron 133edd16368SStephen M. Cameron static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd, 134edd16368SStephen M. Cameron void (*done)(struct scsi_cmnd *)); 135a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *); 136a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh, 137a08a8471SStephen M. Cameron unsigned long elapsed_time); 138667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev, 139667e23d4SStephen M. Cameron int qdepth, int reason); 140edd16368SStephen M. Cameron 141edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd); 142edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev); 143edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev); 144edd16368SStephen M. Cameron 145edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev, 146edd16368SStephen M. Cameron struct device_attribute *attr, char *buf); 147edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev, 148edd16368SStephen M. Cameron struct device_attribute *attr, char *buf); 149edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev, 150edd16368SStephen M. Cameron struct device_attribute *attr, char *buf); 151edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno); 152edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev, 153edd16368SStephen M. Cameron struct device_attribute *attr, const char *buf, size_t count); 154edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h, 155edd16368SStephen M. Cameron struct CommandList *c); 156edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h, 157edd16368SStephen M. Cameron struct CommandList *c); 158303932fdSDon Brace /* performant mode helper functions */ 159303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets, 160303932fdSDon Brace int nsgs, int *bucket_map); 161303932fdSDon Brace static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h); 162303932fdSDon Brace static inline u32 next_command(struct ctlr_info *h); 163edd16368SStephen M. Cameron 164edd16368SStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL); 165edd16368SStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL); 166edd16368SStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL); 167edd16368SStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan); 168edd16368SStephen M. Cameron 169edd16368SStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = { 170edd16368SStephen M. Cameron &dev_attr_raid_level, 171edd16368SStephen M. Cameron &dev_attr_lunid, 172edd16368SStephen M. Cameron &dev_attr_unique_id, 173edd16368SStephen M. Cameron NULL, 174edd16368SStephen M. Cameron }; 175edd16368SStephen M. Cameron 176edd16368SStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = { 177edd16368SStephen M. Cameron &dev_attr_rescan, 178edd16368SStephen M. Cameron NULL, 179edd16368SStephen M. Cameron }; 180edd16368SStephen M. Cameron 181edd16368SStephen M. Cameron static struct scsi_host_template hpsa_driver_template = { 182edd16368SStephen M. Cameron .module = THIS_MODULE, 183edd16368SStephen M. Cameron .name = "hpsa", 184edd16368SStephen M. Cameron .proc_name = "hpsa", 185edd16368SStephen M. Cameron .queuecommand = hpsa_scsi_queue_command, 186a08a8471SStephen M. Cameron .scan_start = hpsa_scan_start, 187a08a8471SStephen M. Cameron .scan_finished = hpsa_scan_finished, 188667e23d4SStephen M. Cameron .change_queue_depth = hpsa_change_queue_depth, 189edd16368SStephen M. Cameron .this_id = -1, 190edd16368SStephen M. Cameron .use_clustering = ENABLE_CLUSTERING, 191edd16368SStephen M. Cameron .eh_device_reset_handler = hpsa_eh_device_reset_handler, 192edd16368SStephen M. Cameron .ioctl = hpsa_ioctl, 193edd16368SStephen M. Cameron .slave_alloc = hpsa_slave_alloc, 194edd16368SStephen M. Cameron .slave_destroy = hpsa_slave_destroy, 195edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT 196edd16368SStephen M. Cameron .compat_ioctl = hpsa_compat_ioctl, 197edd16368SStephen M. Cameron #endif 198edd16368SStephen M. Cameron .sdev_attrs = hpsa_sdev_attrs, 199edd16368SStephen M. Cameron .shost_attrs = hpsa_shost_attrs, 200edd16368SStephen M. Cameron }; 201edd16368SStephen M. Cameron 202edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev) 203edd16368SStephen M. Cameron { 204edd16368SStephen M. Cameron unsigned long *priv = shost_priv(sdev->host); 205edd16368SStephen M. Cameron return (struct ctlr_info *) *priv; 206edd16368SStephen M. Cameron } 207edd16368SStephen M. Cameron 208a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh) 209a23513e8SStephen M. Cameron { 210a23513e8SStephen M. Cameron unsigned long *priv = shost_priv(sh); 211a23513e8SStephen M. Cameron return (struct ctlr_info *) *priv; 212a23513e8SStephen M. Cameron } 213a23513e8SStephen M. Cameron 214edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h, 215edd16368SStephen M. Cameron struct CommandList *c) 216edd16368SStephen M. Cameron { 217edd16368SStephen M. Cameron if (c->err_info->SenseInfo[2] != UNIT_ATTENTION) 218edd16368SStephen M. Cameron return 0; 219edd16368SStephen M. Cameron 220edd16368SStephen M. Cameron switch (c->err_info->SenseInfo[12]) { 221edd16368SStephen M. Cameron case STATE_CHANGED: 222edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: a state change " 223edd16368SStephen M. Cameron "detected, command retried\n", h->ctlr); 224edd16368SStephen M. Cameron break; 225edd16368SStephen M. Cameron case LUN_FAILED: 226edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: LUN failure " 227edd16368SStephen M. Cameron "detected, action required\n", h->ctlr); 228edd16368SStephen M. Cameron break; 229edd16368SStephen M. Cameron case REPORT_LUNS_CHANGED: 230edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: report LUN data " 23131468401SMike Miller "changed, action required\n", h->ctlr); 232edd16368SStephen M. Cameron /* 233edd16368SStephen M. Cameron * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012. 234edd16368SStephen M. Cameron */ 235edd16368SStephen M. Cameron break; 236edd16368SStephen M. Cameron case POWER_OR_RESET: 237edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: a power on " 238edd16368SStephen M. Cameron "or device reset detected\n", h->ctlr); 239edd16368SStephen M. Cameron break; 240edd16368SStephen M. Cameron case UNIT_ATTENTION_CLEARED: 241edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: unit attention " 242edd16368SStephen M. Cameron "cleared by another initiator\n", h->ctlr); 243edd16368SStephen M. Cameron break; 244edd16368SStephen M. Cameron default: 245edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: unknown " 246edd16368SStephen M. Cameron "unit attention detected\n", h->ctlr); 247edd16368SStephen M. Cameron break; 248edd16368SStephen M. Cameron } 249edd16368SStephen M. Cameron return 1; 250edd16368SStephen M. Cameron } 251edd16368SStephen M. Cameron 252edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev, 253edd16368SStephen M. Cameron struct device_attribute *attr, 254edd16368SStephen M. Cameron const char *buf, size_t count) 255edd16368SStephen M. Cameron { 256edd16368SStephen M. Cameron struct ctlr_info *h; 257edd16368SStephen M. Cameron struct Scsi_Host *shost = class_to_shost(dev); 258a23513e8SStephen M. Cameron h = shost_to_hba(shost); 25931468401SMike Miller hpsa_scan_start(h->scsi_host); 260edd16368SStephen M. Cameron return count; 261edd16368SStephen M. Cameron } 262edd16368SStephen M. Cameron 263edd16368SStephen M. Cameron /* Enqueuing and dequeuing functions for cmdlists. */ 264edd16368SStephen M. Cameron static inline void addQ(struct hlist_head *list, struct CommandList *c) 265edd16368SStephen M. Cameron { 266edd16368SStephen M. Cameron hlist_add_head(&c->list, list); 267edd16368SStephen M. Cameron } 268edd16368SStephen M. Cameron 269303932fdSDon Brace static inline u32 next_command(struct ctlr_info *h) 270303932fdSDon Brace { 271303932fdSDon Brace u32 a; 272303932fdSDon Brace 273303932fdSDon Brace if (unlikely(h->transMethod != CFGTBL_Trans_Performant)) 274303932fdSDon Brace return h->access.command_completed(h); 275303932fdSDon Brace 276303932fdSDon Brace if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) { 277303932fdSDon Brace a = *(h->reply_pool_head); /* Next cmd in ring buffer */ 278303932fdSDon Brace (h->reply_pool_head)++; 279303932fdSDon Brace h->commands_outstanding--; 280303932fdSDon Brace } else { 281303932fdSDon Brace a = FIFO_EMPTY; 282303932fdSDon Brace } 283303932fdSDon Brace /* Check for wraparound */ 284303932fdSDon Brace if (h->reply_pool_head == (h->reply_pool + h->max_commands)) { 285303932fdSDon Brace h->reply_pool_head = h->reply_pool; 286303932fdSDon Brace h->reply_pool_wraparound ^= 1; 287303932fdSDon Brace } 288303932fdSDon Brace return a; 289303932fdSDon Brace } 290303932fdSDon Brace 291303932fdSDon Brace /* set_performant_mode: Modify the tag for cciss performant 292303932fdSDon Brace * set bit 0 for pull model, bits 3-1 for block fetch 293303932fdSDon Brace * register number 294303932fdSDon Brace */ 295303932fdSDon Brace static void set_performant_mode(struct ctlr_info *h, struct CommandList *c) 296303932fdSDon Brace { 297303932fdSDon Brace if (likely(h->transMethod == CFGTBL_Trans_Performant)) 298303932fdSDon Brace c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1); 299303932fdSDon Brace } 300303932fdSDon Brace 301edd16368SStephen M. Cameron static void enqueue_cmd_and_start_io(struct ctlr_info *h, 302edd16368SStephen M. Cameron struct CommandList *c) 303edd16368SStephen M. Cameron { 304edd16368SStephen M. Cameron unsigned long flags; 305303932fdSDon Brace 306303932fdSDon Brace set_performant_mode(h, c); 307edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 308edd16368SStephen M. Cameron addQ(&h->reqQ, c); 309edd16368SStephen M. Cameron h->Qdepth++; 310edd16368SStephen M. Cameron start_io(h); 311edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 312edd16368SStephen M. Cameron } 313edd16368SStephen M. Cameron 314edd16368SStephen M. Cameron static inline void removeQ(struct CommandList *c) 315edd16368SStephen M. Cameron { 316edd16368SStephen M. Cameron if (WARN_ON(hlist_unhashed(&c->list))) 317edd16368SStephen M. Cameron return; 318edd16368SStephen M. Cameron hlist_del_init(&c->list); 319edd16368SStephen M. Cameron } 320edd16368SStephen M. Cameron 321edd16368SStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[]) 322edd16368SStephen M. Cameron { 323edd16368SStephen M. Cameron return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0; 324edd16368SStephen M. Cameron } 325edd16368SStephen M. Cameron 326edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[]) 327edd16368SStephen M. Cameron { 328edd16368SStephen M. Cameron return (scsi3addr[3] & 0xC0) == 0x40; 329edd16368SStephen M. Cameron } 330edd16368SStephen M. Cameron 331339b2b14SStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h) 332339b2b14SStephen M. Cameron { 333339b2b14SStephen M. Cameron if (!h->hba_inquiry_data) 334339b2b14SStephen M. Cameron return 0; 335339b2b14SStephen M. Cameron if ((h->hba_inquiry_data[2] & 0x07) == 5) 336339b2b14SStephen M. Cameron return 1; 337339b2b14SStephen M. Cameron return 0; 338339b2b14SStephen M. Cameron } 339339b2b14SStephen M. Cameron 340edd16368SStephen M. Cameron static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG", 341edd16368SStephen M. Cameron "UNKNOWN" 342edd16368SStephen M. Cameron }; 343edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1) 344edd16368SStephen M. Cameron 345edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev, 346edd16368SStephen M. Cameron struct device_attribute *attr, char *buf) 347edd16368SStephen M. Cameron { 348edd16368SStephen M. Cameron ssize_t l = 0; 34982a72c0aSStephen M. Cameron unsigned char rlevel; 350edd16368SStephen M. Cameron struct ctlr_info *h; 351edd16368SStephen M. Cameron struct scsi_device *sdev; 352edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *hdev; 353edd16368SStephen M. Cameron unsigned long flags; 354edd16368SStephen M. Cameron 355edd16368SStephen M. Cameron sdev = to_scsi_device(dev); 356edd16368SStephen M. Cameron h = sdev_to_hba(sdev); 357edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 358edd16368SStephen M. Cameron hdev = sdev->hostdata; 359edd16368SStephen M. Cameron if (!hdev) { 360edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 361edd16368SStephen M. Cameron return -ENODEV; 362edd16368SStephen M. Cameron } 363edd16368SStephen M. Cameron 364edd16368SStephen M. Cameron /* Is this even a logical drive? */ 365edd16368SStephen M. Cameron if (!is_logical_dev_addr_mode(hdev->scsi3addr)) { 366edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 367edd16368SStephen M. Cameron l = snprintf(buf, PAGE_SIZE, "N/A\n"); 368edd16368SStephen M. Cameron return l; 369edd16368SStephen M. Cameron } 370edd16368SStephen M. Cameron 371edd16368SStephen M. Cameron rlevel = hdev->raid_level; 372edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 37382a72c0aSStephen M. Cameron if (rlevel > RAID_UNKNOWN) 374edd16368SStephen M. Cameron rlevel = RAID_UNKNOWN; 375edd16368SStephen M. Cameron l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]); 376edd16368SStephen M. Cameron return l; 377edd16368SStephen M. Cameron } 378edd16368SStephen M. Cameron 379edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev, 380edd16368SStephen M. Cameron struct device_attribute *attr, char *buf) 381edd16368SStephen M. Cameron { 382edd16368SStephen M. Cameron struct ctlr_info *h; 383edd16368SStephen M. Cameron struct scsi_device *sdev; 384edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *hdev; 385edd16368SStephen M. Cameron unsigned long flags; 386edd16368SStephen M. Cameron unsigned char lunid[8]; 387edd16368SStephen M. Cameron 388edd16368SStephen M. Cameron sdev = to_scsi_device(dev); 389edd16368SStephen M. Cameron h = sdev_to_hba(sdev); 390edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 391edd16368SStephen M. Cameron hdev = sdev->hostdata; 392edd16368SStephen M. Cameron if (!hdev) { 393edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 394edd16368SStephen M. Cameron return -ENODEV; 395edd16368SStephen M. Cameron } 396edd16368SStephen M. Cameron memcpy(lunid, hdev->scsi3addr, sizeof(lunid)); 397edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 398edd16368SStephen M. Cameron return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n", 399edd16368SStephen M. Cameron lunid[0], lunid[1], lunid[2], lunid[3], 400edd16368SStephen M. Cameron lunid[4], lunid[5], lunid[6], lunid[7]); 401edd16368SStephen M. Cameron } 402edd16368SStephen M. Cameron 403edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev, 404edd16368SStephen M. Cameron struct device_attribute *attr, char *buf) 405edd16368SStephen M. Cameron { 406edd16368SStephen M. Cameron struct ctlr_info *h; 407edd16368SStephen M. Cameron struct scsi_device *sdev; 408edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *hdev; 409edd16368SStephen M. Cameron unsigned long flags; 410edd16368SStephen M. Cameron unsigned char sn[16]; 411edd16368SStephen M. Cameron 412edd16368SStephen M. Cameron sdev = to_scsi_device(dev); 413edd16368SStephen M. Cameron h = sdev_to_hba(sdev); 414edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 415edd16368SStephen M. Cameron hdev = sdev->hostdata; 416edd16368SStephen M. Cameron if (!hdev) { 417edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 418edd16368SStephen M. Cameron return -ENODEV; 419edd16368SStephen M. Cameron } 420edd16368SStephen M. Cameron memcpy(sn, hdev->device_id, sizeof(sn)); 421edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 422edd16368SStephen M. Cameron return snprintf(buf, 16 * 2 + 2, 423edd16368SStephen M. Cameron "%02X%02X%02X%02X%02X%02X%02X%02X" 424edd16368SStephen M. Cameron "%02X%02X%02X%02X%02X%02X%02X%02X\n", 425edd16368SStephen M. Cameron sn[0], sn[1], sn[2], sn[3], 426edd16368SStephen M. Cameron sn[4], sn[5], sn[6], sn[7], 427edd16368SStephen M. Cameron sn[8], sn[9], sn[10], sn[11], 428edd16368SStephen M. Cameron sn[12], sn[13], sn[14], sn[15]); 429edd16368SStephen M. Cameron } 430edd16368SStephen M. Cameron 431edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h, 432edd16368SStephen M. Cameron unsigned char scsi3addr[], int bus, int *target, int *lun) 433edd16368SStephen M. Cameron { 434edd16368SStephen M. Cameron /* finds an unused bus, target, lun for a new physical device 435edd16368SStephen M. Cameron * assumes h->devlock is held 436edd16368SStephen M. Cameron */ 437edd16368SStephen M. Cameron int i, found = 0; 438edd16368SStephen M. Cameron DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA); 439edd16368SStephen M. Cameron 440edd16368SStephen M. Cameron memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3); 441edd16368SStephen M. Cameron 442edd16368SStephen M. Cameron for (i = 0; i < h->ndevices; i++) { 443edd16368SStephen M. Cameron if (h->dev[i]->bus == bus && h->dev[i]->target != -1) 444edd16368SStephen M. Cameron set_bit(h->dev[i]->target, lun_taken); 445edd16368SStephen M. Cameron } 446edd16368SStephen M. Cameron 447edd16368SStephen M. Cameron for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) { 448edd16368SStephen M. Cameron if (!test_bit(i, lun_taken)) { 449edd16368SStephen M. Cameron /* *bus = 1; */ 450edd16368SStephen M. Cameron *target = i; 451edd16368SStephen M. Cameron *lun = 0; 452edd16368SStephen M. Cameron found = 1; 453edd16368SStephen M. Cameron break; 454edd16368SStephen M. Cameron } 455edd16368SStephen M. Cameron } 456edd16368SStephen M. Cameron return !found; 457edd16368SStephen M. Cameron } 458edd16368SStephen M. Cameron 459edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */ 460edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno, 461edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *device, 462edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *added[], int *nadded) 463edd16368SStephen M. Cameron { 464edd16368SStephen M. Cameron /* assumes h->devlock is held */ 465edd16368SStephen M. Cameron int n = h->ndevices; 466edd16368SStephen M. Cameron int i; 467edd16368SStephen M. Cameron unsigned char addr1[8], addr2[8]; 468edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd; 469edd16368SStephen M. Cameron 470edd16368SStephen M. Cameron if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) { 471edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "too many devices, some will be " 472edd16368SStephen M. Cameron "inaccessible.\n"); 473edd16368SStephen M. Cameron return -1; 474edd16368SStephen M. Cameron } 475edd16368SStephen M. Cameron 476edd16368SStephen M. Cameron /* physical devices do not have lun or target assigned until now. */ 477edd16368SStephen M. Cameron if (device->lun != -1) 478edd16368SStephen M. Cameron /* Logical device, lun is already assigned. */ 479edd16368SStephen M. Cameron goto lun_assigned; 480edd16368SStephen M. Cameron 481edd16368SStephen M. Cameron /* If this device a non-zero lun of a multi-lun device 482edd16368SStephen M. Cameron * byte 4 of the 8-byte LUN addr will contain the logical 483edd16368SStephen M. Cameron * unit no, zero otherise. 484edd16368SStephen M. Cameron */ 485edd16368SStephen M. Cameron if (device->scsi3addr[4] == 0) { 486edd16368SStephen M. Cameron /* This is not a non-zero lun of a multi-lun device */ 487edd16368SStephen M. Cameron if (hpsa_find_target_lun(h, device->scsi3addr, 488edd16368SStephen M. Cameron device->bus, &device->target, &device->lun) != 0) 489edd16368SStephen M. Cameron return -1; 490edd16368SStephen M. Cameron goto lun_assigned; 491edd16368SStephen M. Cameron } 492edd16368SStephen M. Cameron 493edd16368SStephen M. Cameron /* This is a non-zero lun of a multi-lun device. 494edd16368SStephen M. Cameron * Search through our list and find the device which 495edd16368SStephen M. Cameron * has the same 8 byte LUN address, excepting byte 4. 496edd16368SStephen M. Cameron * Assign the same bus and target for this new LUN. 497edd16368SStephen M. Cameron * Use the logical unit number from the firmware. 498edd16368SStephen M. Cameron */ 499edd16368SStephen M. Cameron memcpy(addr1, device->scsi3addr, 8); 500edd16368SStephen M. Cameron addr1[4] = 0; 501edd16368SStephen M. Cameron for (i = 0; i < n; i++) { 502edd16368SStephen M. Cameron sd = h->dev[i]; 503edd16368SStephen M. Cameron memcpy(addr2, sd->scsi3addr, 8); 504edd16368SStephen M. Cameron addr2[4] = 0; 505edd16368SStephen M. Cameron /* differ only in byte 4? */ 506edd16368SStephen M. Cameron if (memcmp(addr1, addr2, 8) == 0) { 507edd16368SStephen M. Cameron device->bus = sd->bus; 508edd16368SStephen M. Cameron device->target = sd->target; 509edd16368SStephen M. Cameron device->lun = device->scsi3addr[4]; 510edd16368SStephen M. Cameron break; 511edd16368SStephen M. Cameron } 512edd16368SStephen M. Cameron } 513edd16368SStephen M. Cameron if (device->lun == -1) { 514edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "physical device with no LUN=0," 515edd16368SStephen M. Cameron " suspect firmware bug or unsupported hardware " 516edd16368SStephen M. Cameron "configuration.\n"); 517edd16368SStephen M. Cameron return -1; 518edd16368SStephen M. Cameron } 519edd16368SStephen M. Cameron 520edd16368SStephen M. Cameron lun_assigned: 521edd16368SStephen M. Cameron 522edd16368SStephen M. Cameron h->dev[n] = device; 523edd16368SStephen M. Cameron h->ndevices++; 524edd16368SStephen M. Cameron added[*nadded] = device; 525edd16368SStephen M. Cameron (*nadded)++; 526edd16368SStephen M. Cameron 527edd16368SStephen M. Cameron /* initially, (before registering with scsi layer) we don't 528edd16368SStephen M. Cameron * know our hostno and we don't want to print anything first 529edd16368SStephen M. Cameron * time anyway (the scsi layer's inquiries will show that info) 530edd16368SStephen M. Cameron */ 531edd16368SStephen M. Cameron /* if (hostno != -1) */ 532edd16368SStephen M. Cameron dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n", 533edd16368SStephen M. Cameron scsi_device_type(device->devtype), hostno, 534edd16368SStephen M. Cameron device->bus, device->target, device->lun); 535edd16368SStephen M. Cameron return 0; 536edd16368SStephen M. Cameron } 537edd16368SStephen M. Cameron 5382a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */ 5392a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno, 5402a8ccf31SStephen M. Cameron int entry, struct hpsa_scsi_dev_t *new_entry, 5412a8ccf31SStephen M. Cameron struct hpsa_scsi_dev_t *added[], int *nadded, 5422a8ccf31SStephen M. Cameron struct hpsa_scsi_dev_t *removed[], int *nremoved) 5432a8ccf31SStephen M. Cameron { 5442a8ccf31SStephen M. Cameron /* assumes h->devlock is held */ 5452a8ccf31SStephen M. Cameron BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA); 5462a8ccf31SStephen M. Cameron removed[*nremoved] = h->dev[entry]; 5472a8ccf31SStephen M. Cameron (*nremoved)++; 5482a8ccf31SStephen M. Cameron h->dev[entry] = new_entry; 5492a8ccf31SStephen M. Cameron added[*nadded] = new_entry; 5502a8ccf31SStephen M. Cameron (*nadded)++; 5512a8ccf31SStephen M. Cameron dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n", 5522a8ccf31SStephen M. Cameron scsi_device_type(new_entry->devtype), hostno, new_entry->bus, 5532a8ccf31SStephen M. Cameron new_entry->target, new_entry->lun); 5542a8ccf31SStephen M. Cameron } 5552a8ccf31SStephen M. Cameron 556edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */ 557edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry, 558edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *removed[], int *nremoved) 559edd16368SStephen M. Cameron { 560edd16368SStephen M. Cameron /* assumes h->devlock is held */ 561edd16368SStephen M. Cameron int i; 562edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd; 563edd16368SStephen M. Cameron 564b2ed4f79SStephen M. Cameron BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA); 565edd16368SStephen M. Cameron 566edd16368SStephen M. Cameron sd = h->dev[entry]; 567edd16368SStephen M. Cameron removed[*nremoved] = h->dev[entry]; 568edd16368SStephen M. Cameron (*nremoved)++; 569edd16368SStephen M. Cameron 570edd16368SStephen M. Cameron for (i = entry; i < h->ndevices-1; i++) 571edd16368SStephen M. Cameron h->dev[i] = h->dev[i+1]; 572edd16368SStephen M. Cameron h->ndevices--; 573edd16368SStephen M. Cameron dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n", 574edd16368SStephen M. Cameron scsi_device_type(sd->devtype), hostno, sd->bus, sd->target, 575edd16368SStephen M. Cameron sd->lun); 576edd16368SStephen M. Cameron } 577edd16368SStephen M. Cameron 578edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \ 579edd16368SStephen M. Cameron (a)[7] == (b)[7] && \ 580edd16368SStephen M. Cameron (a)[6] == (b)[6] && \ 581edd16368SStephen M. Cameron (a)[5] == (b)[5] && \ 582edd16368SStephen M. Cameron (a)[4] == (b)[4] && \ 583edd16368SStephen M. Cameron (a)[3] == (b)[3] && \ 584edd16368SStephen M. Cameron (a)[2] == (b)[2] && \ 585edd16368SStephen M. Cameron (a)[1] == (b)[1] && \ 586edd16368SStephen M. Cameron (a)[0] == (b)[0]) 587edd16368SStephen M. Cameron 588edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h, 589edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *added) 590edd16368SStephen M. Cameron { 591edd16368SStephen M. Cameron /* called when scsi_add_device fails in order to re-adjust 592edd16368SStephen M. Cameron * h->dev[] to match the mid layer's view. 593edd16368SStephen M. Cameron */ 594edd16368SStephen M. Cameron unsigned long flags; 595edd16368SStephen M. Cameron int i, j; 596edd16368SStephen M. Cameron 597edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 598edd16368SStephen M. Cameron for (i = 0; i < h->ndevices; i++) { 599edd16368SStephen M. Cameron if (h->dev[i] == added) { 600edd16368SStephen M. Cameron for (j = i; j < h->ndevices-1; j++) 601edd16368SStephen M. Cameron h->dev[j] = h->dev[j+1]; 602edd16368SStephen M. Cameron h->ndevices--; 603edd16368SStephen M. Cameron break; 604edd16368SStephen M. Cameron } 605edd16368SStephen M. Cameron } 606edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 607edd16368SStephen M. Cameron kfree(added); 608edd16368SStephen M. Cameron } 609edd16368SStephen M. Cameron 610edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1, 611edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *dev2) 612edd16368SStephen M. Cameron { 613edd16368SStephen M. Cameron if ((is_logical_dev_addr_mode(dev1->scsi3addr) || 614edd16368SStephen M. Cameron (dev1->lun != -1 && dev2->lun != -1)) && 615edd16368SStephen M. Cameron dev1->devtype != 0x0C) 616edd16368SStephen M. Cameron return (memcmp(dev1, dev2, sizeof(*dev1)) == 0); 617edd16368SStephen M. Cameron 618edd16368SStephen M. Cameron /* we compare everything except lun and target as these 619edd16368SStephen M. Cameron * are not yet assigned. Compare parts likely 620edd16368SStephen M. Cameron * to differ first 621edd16368SStephen M. Cameron */ 622edd16368SStephen M. Cameron if (memcmp(dev1->scsi3addr, dev2->scsi3addr, 623edd16368SStephen M. Cameron sizeof(dev1->scsi3addr)) != 0) 624edd16368SStephen M. Cameron return 0; 625edd16368SStephen M. Cameron if (memcmp(dev1->device_id, dev2->device_id, 626edd16368SStephen M. Cameron sizeof(dev1->device_id)) != 0) 627edd16368SStephen M. Cameron return 0; 628edd16368SStephen M. Cameron if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0) 629edd16368SStephen M. Cameron return 0; 630edd16368SStephen M. Cameron if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0) 631edd16368SStephen M. Cameron return 0; 632edd16368SStephen M. Cameron if (memcmp(dev1->revision, dev2->revision, sizeof(dev1->revision)) != 0) 633edd16368SStephen M. Cameron return 0; 634edd16368SStephen M. Cameron if (dev1->devtype != dev2->devtype) 635edd16368SStephen M. Cameron return 0; 636edd16368SStephen M. Cameron if (dev1->raid_level != dev2->raid_level) 637edd16368SStephen M. Cameron return 0; 638edd16368SStephen M. Cameron if (dev1->bus != dev2->bus) 639edd16368SStephen M. Cameron return 0; 640edd16368SStephen M. Cameron return 1; 641edd16368SStephen M. Cameron } 642edd16368SStephen M. Cameron 643edd16368SStephen M. Cameron /* Find needle in haystack. If exact match found, return DEVICE_SAME, 644edd16368SStephen M. Cameron * and return needle location in *index. If scsi3addr matches, but not 645edd16368SStephen M. Cameron * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle 646edd16368SStephen M. Cameron * location in *index. If needle not found, return DEVICE_NOT_FOUND. 647edd16368SStephen M. Cameron */ 648edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle, 649edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *haystack[], int haystack_size, 650edd16368SStephen M. Cameron int *index) 651edd16368SStephen M. Cameron { 652edd16368SStephen M. Cameron int i; 653edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0 654edd16368SStephen M. Cameron #define DEVICE_CHANGED 1 655edd16368SStephen M. Cameron #define DEVICE_SAME 2 656edd16368SStephen M. Cameron for (i = 0; i < haystack_size; i++) { 65723231048SStephen M. Cameron if (haystack[i] == NULL) /* previously removed. */ 65823231048SStephen M. Cameron continue; 659edd16368SStephen M. Cameron if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) { 660edd16368SStephen M. Cameron *index = i; 661edd16368SStephen M. Cameron if (device_is_the_same(needle, haystack[i])) 662edd16368SStephen M. Cameron return DEVICE_SAME; 663edd16368SStephen M. Cameron else 664edd16368SStephen M. Cameron return DEVICE_CHANGED; 665edd16368SStephen M. Cameron } 666edd16368SStephen M. Cameron } 667edd16368SStephen M. Cameron *index = -1; 668edd16368SStephen M. Cameron return DEVICE_NOT_FOUND; 669edd16368SStephen M. Cameron } 670edd16368SStephen M. Cameron 6714967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno, 672edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd[], int nsds) 673edd16368SStephen M. Cameron { 674edd16368SStephen M. Cameron /* sd contains scsi3 addresses and devtypes, and inquiry 675edd16368SStephen M. Cameron * data. This function takes what's in sd to be the current 676edd16368SStephen M. Cameron * reality and updates h->dev[] to reflect that reality. 677edd16368SStephen M. Cameron */ 678edd16368SStephen M. Cameron int i, entry, device_change, changes = 0; 679edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *csd; 680edd16368SStephen M. Cameron unsigned long flags; 681edd16368SStephen M. Cameron struct hpsa_scsi_dev_t **added, **removed; 682edd16368SStephen M. Cameron int nadded, nremoved; 683edd16368SStephen M. Cameron struct Scsi_Host *sh = NULL; 684edd16368SStephen M. Cameron 685edd16368SStephen M. Cameron added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA, 686edd16368SStephen M. Cameron GFP_KERNEL); 687edd16368SStephen M. Cameron removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA, 688edd16368SStephen M. Cameron GFP_KERNEL); 689edd16368SStephen M. Cameron 690edd16368SStephen M. Cameron if (!added || !removed) { 691edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "out of memory in " 692edd16368SStephen M. Cameron "adjust_hpsa_scsi_table\n"); 693edd16368SStephen M. Cameron goto free_and_out; 694edd16368SStephen M. Cameron } 695edd16368SStephen M. Cameron 696edd16368SStephen M. Cameron spin_lock_irqsave(&h->devlock, flags); 697edd16368SStephen M. Cameron 698edd16368SStephen M. Cameron /* find any devices in h->dev[] that are not in 699edd16368SStephen M. Cameron * sd[] and remove them from h->dev[], and for any 700edd16368SStephen M. Cameron * devices which have changed, remove the old device 701edd16368SStephen M. Cameron * info and add the new device info. 702edd16368SStephen M. Cameron */ 703edd16368SStephen M. Cameron i = 0; 704edd16368SStephen M. Cameron nremoved = 0; 705edd16368SStephen M. Cameron nadded = 0; 706edd16368SStephen M. Cameron while (i < h->ndevices) { 707edd16368SStephen M. Cameron csd = h->dev[i]; 708edd16368SStephen M. Cameron device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry); 709edd16368SStephen M. Cameron if (device_change == DEVICE_NOT_FOUND) { 710edd16368SStephen M. Cameron changes++; 711edd16368SStephen M. Cameron hpsa_scsi_remove_entry(h, hostno, i, 712edd16368SStephen M. Cameron removed, &nremoved); 713edd16368SStephen M. Cameron continue; /* remove ^^^, hence i not incremented */ 714edd16368SStephen M. Cameron } else if (device_change == DEVICE_CHANGED) { 715edd16368SStephen M. Cameron changes++; 7162a8ccf31SStephen M. Cameron hpsa_scsi_replace_entry(h, hostno, i, sd[entry], 7172a8ccf31SStephen M. Cameron added, &nadded, removed, &nremoved); 718c7f172dcSStephen M. Cameron /* Set it to NULL to prevent it from being freed 719c7f172dcSStephen M. Cameron * at the bottom of hpsa_update_scsi_devices() 720c7f172dcSStephen M. Cameron */ 721c7f172dcSStephen M. Cameron sd[entry] = NULL; 722edd16368SStephen M. Cameron } 723edd16368SStephen M. Cameron i++; 724edd16368SStephen M. Cameron } 725edd16368SStephen M. Cameron 726edd16368SStephen M. Cameron /* Now, make sure every device listed in sd[] is also 727edd16368SStephen M. Cameron * listed in h->dev[], adding them if they aren't found 728edd16368SStephen M. Cameron */ 729edd16368SStephen M. Cameron 730edd16368SStephen M. Cameron for (i = 0; i < nsds; i++) { 731edd16368SStephen M. Cameron if (!sd[i]) /* if already added above. */ 732edd16368SStephen M. Cameron continue; 733edd16368SStephen M. Cameron device_change = hpsa_scsi_find_entry(sd[i], h->dev, 734edd16368SStephen M. Cameron h->ndevices, &entry); 735edd16368SStephen M. Cameron if (device_change == DEVICE_NOT_FOUND) { 736edd16368SStephen M. Cameron changes++; 737edd16368SStephen M. Cameron if (hpsa_scsi_add_entry(h, hostno, sd[i], 738edd16368SStephen M. Cameron added, &nadded) != 0) 739edd16368SStephen M. Cameron break; 740edd16368SStephen M. Cameron sd[i] = NULL; /* prevent from being freed later. */ 741edd16368SStephen M. Cameron } else if (device_change == DEVICE_CHANGED) { 742edd16368SStephen M. Cameron /* should never happen... */ 743edd16368SStephen M. Cameron changes++; 744edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, 745edd16368SStephen M. Cameron "device unexpectedly changed.\n"); 746edd16368SStephen M. Cameron /* but if it does happen, we just ignore that device */ 747edd16368SStephen M. Cameron } 748edd16368SStephen M. Cameron } 749edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->devlock, flags); 750edd16368SStephen M. Cameron 751edd16368SStephen M. Cameron /* Don't notify scsi mid layer of any changes the first time through 752edd16368SStephen M. Cameron * (or if there are no changes) scsi_scan_host will do it later the 753edd16368SStephen M. Cameron * first time through. 754edd16368SStephen M. Cameron */ 755edd16368SStephen M. Cameron if (hostno == -1 || !changes) 756edd16368SStephen M. Cameron goto free_and_out; 757edd16368SStephen M. Cameron 758edd16368SStephen M. Cameron sh = h->scsi_host; 759edd16368SStephen M. Cameron /* Notify scsi mid layer of any removed devices */ 760edd16368SStephen M. Cameron for (i = 0; i < nremoved; i++) { 761edd16368SStephen M. Cameron struct scsi_device *sdev = 762edd16368SStephen M. Cameron scsi_device_lookup(sh, removed[i]->bus, 763edd16368SStephen M. Cameron removed[i]->target, removed[i]->lun); 764edd16368SStephen M. Cameron if (sdev != NULL) { 765edd16368SStephen M. Cameron scsi_remove_device(sdev); 766edd16368SStephen M. Cameron scsi_device_put(sdev); 767edd16368SStephen M. Cameron } else { 768edd16368SStephen M. Cameron /* We don't expect to get here. 769edd16368SStephen M. Cameron * future cmds to this device will get selection 770edd16368SStephen M. Cameron * timeout as if the device was gone. 771edd16368SStephen M. Cameron */ 772edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d " 773edd16368SStephen M. Cameron " for removal.", hostno, removed[i]->bus, 774edd16368SStephen M. Cameron removed[i]->target, removed[i]->lun); 775edd16368SStephen M. Cameron } 776edd16368SStephen M. Cameron kfree(removed[i]); 777edd16368SStephen M. Cameron removed[i] = NULL; 778edd16368SStephen M. Cameron } 779edd16368SStephen M. Cameron 780edd16368SStephen M. Cameron /* Notify scsi mid layer of any added devices */ 781edd16368SStephen M. Cameron for (i = 0; i < nadded; i++) { 782edd16368SStephen M. Cameron if (scsi_add_device(sh, added[i]->bus, 783edd16368SStephen M. Cameron added[i]->target, added[i]->lun) == 0) 784edd16368SStephen M. Cameron continue; 785edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, " 786edd16368SStephen M. Cameron "device not added.\n", hostno, added[i]->bus, 787edd16368SStephen M. Cameron added[i]->target, added[i]->lun); 788edd16368SStephen M. Cameron /* now we have to remove it from h->dev, 789edd16368SStephen M. Cameron * since it didn't get added to scsi mid layer 790edd16368SStephen M. Cameron */ 791edd16368SStephen M. Cameron fixup_botched_add(h, added[i]); 792edd16368SStephen M. Cameron } 793edd16368SStephen M. Cameron 794edd16368SStephen M. Cameron free_and_out: 795edd16368SStephen M. Cameron kfree(added); 796edd16368SStephen M. Cameron kfree(removed); 797edd16368SStephen M. Cameron } 798edd16368SStephen M. Cameron 799edd16368SStephen M. Cameron /* 800edd16368SStephen M. Cameron * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t * 801edd16368SStephen M. Cameron * Assume's h->devlock is held. 802edd16368SStephen M. Cameron */ 803edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h, 804edd16368SStephen M. Cameron int bus, int target, int lun) 805edd16368SStephen M. Cameron { 806edd16368SStephen M. Cameron int i; 807edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd; 808edd16368SStephen M. Cameron 809edd16368SStephen M. Cameron for (i = 0; i < h->ndevices; i++) { 810edd16368SStephen M. Cameron sd = h->dev[i]; 811edd16368SStephen M. Cameron if (sd->bus == bus && sd->target == target && sd->lun == lun) 812edd16368SStephen M. Cameron return sd; 813edd16368SStephen M. Cameron } 814edd16368SStephen M. Cameron return NULL; 815edd16368SStephen M. Cameron } 816edd16368SStephen M. Cameron 817edd16368SStephen M. Cameron /* link sdev->hostdata to our per-device structure. */ 818edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev) 819edd16368SStephen M. Cameron { 820edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd; 821edd16368SStephen M. Cameron unsigned long flags; 822edd16368SStephen M. Cameron struct ctlr_info *h; 823edd16368SStephen M. Cameron 824edd16368SStephen M. Cameron h = sdev_to_hba(sdev); 825edd16368SStephen M. Cameron spin_lock_irqsave(&h->devlock, flags); 826edd16368SStephen M. Cameron sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev), 827edd16368SStephen M. Cameron sdev_id(sdev), sdev->lun); 828edd16368SStephen M. Cameron if (sd != NULL) 829edd16368SStephen M. Cameron sdev->hostdata = sd; 830edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->devlock, flags); 831edd16368SStephen M. Cameron return 0; 832edd16368SStephen M. Cameron } 833edd16368SStephen M. Cameron 834edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev) 835edd16368SStephen M. Cameron { 836bcc44255SStephen M. Cameron /* nothing to do. */ 837edd16368SStephen M. Cameron } 838edd16368SStephen M. Cameron 839edd16368SStephen M. Cameron static void hpsa_scsi_setup(struct ctlr_info *h) 840edd16368SStephen M. Cameron { 841edd16368SStephen M. Cameron h->ndevices = 0; 842edd16368SStephen M. Cameron h->scsi_host = NULL; 843edd16368SStephen M. Cameron spin_lock_init(&h->devlock); 844edd16368SStephen M. Cameron } 845edd16368SStephen M. Cameron 84633a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h) 84733a2ffceSStephen M. Cameron { 84833a2ffceSStephen M. Cameron int i; 84933a2ffceSStephen M. Cameron 85033a2ffceSStephen M. Cameron if (!h->cmd_sg_list) 85133a2ffceSStephen M. Cameron return; 85233a2ffceSStephen M. Cameron for (i = 0; i < h->nr_cmds; i++) { 85333a2ffceSStephen M. Cameron kfree(h->cmd_sg_list[i]); 85433a2ffceSStephen M. Cameron h->cmd_sg_list[i] = NULL; 85533a2ffceSStephen M. Cameron } 85633a2ffceSStephen M. Cameron kfree(h->cmd_sg_list); 85733a2ffceSStephen M. Cameron h->cmd_sg_list = NULL; 85833a2ffceSStephen M. Cameron } 85933a2ffceSStephen M. Cameron 86033a2ffceSStephen M. Cameron static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h) 86133a2ffceSStephen M. Cameron { 86233a2ffceSStephen M. Cameron int i; 86333a2ffceSStephen M. Cameron 86433a2ffceSStephen M. Cameron if (h->chainsize <= 0) 86533a2ffceSStephen M. Cameron return 0; 86633a2ffceSStephen M. Cameron 86733a2ffceSStephen M. Cameron h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds, 86833a2ffceSStephen M. Cameron GFP_KERNEL); 86933a2ffceSStephen M. Cameron if (!h->cmd_sg_list) 87033a2ffceSStephen M. Cameron return -ENOMEM; 87133a2ffceSStephen M. Cameron for (i = 0; i < h->nr_cmds; i++) { 87233a2ffceSStephen M. Cameron h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) * 87333a2ffceSStephen M. Cameron h->chainsize, GFP_KERNEL); 87433a2ffceSStephen M. Cameron if (!h->cmd_sg_list[i]) 87533a2ffceSStephen M. Cameron goto clean; 87633a2ffceSStephen M. Cameron } 87733a2ffceSStephen M. Cameron return 0; 87833a2ffceSStephen M. Cameron 87933a2ffceSStephen M. Cameron clean: 88033a2ffceSStephen M. Cameron hpsa_free_sg_chain_blocks(h); 88133a2ffceSStephen M. Cameron return -ENOMEM; 88233a2ffceSStephen M. Cameron } 88333a2ffceSStephen M. Cameron 88433a2ffceSStephen M. Cameron static void hpsa_map_sg_chain_block(struct ctlr_info *h, 88533a2ffceSStephen M. Cameron struct CommandList *c) 88633a2ffceSStephen M. Cameron { 88733a2ffceSStephen M. Cameron struct SGDescriptor *chain_sg, *chain_block; 88833a2ffceSStephen M. Cameron u64 temp64; 88933a2ffceSStephen M. Cameron 89033a2ffceSStephen M. Cameron chain_sg = &c->SG[h->max_cmd_sg_entries - 1]; 89133a2ffceSStephen M. Cameron chain_block = h->cmd_sg_list[c->cmdindex]; 89233a2ffceSStephen M. Cameron chain_sg->Ext = HPSA_SG_CHAIN; 89333a2ffceSStephen M. Cameron chain_sg->Len = sizeof(*chain_sg) * 89433a2ffceSStephen M. Cameron (c->Header.SGTotal - h->max_cmd_sg_entries); 89533a2ffceSStephen M. Cameron temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len, 89633a2ffceSStephen M. Cameron PCI_DMA_TODEVICE); 89733a2ffceSStephen M. Cameron chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL); 89833a2ffceSStephen M. Cameron chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL); 89933a2ffceSStephen M. Cameron } 90033a2ffceSStephen M. Cameron 90133a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h, 90233a2ffceSStephen M. Cameron struct CommandList *c) 90333a2ffceSStephen M. Cameron { 90433a2ffceSStephen M. Cameron struct SGDescriptor *chain_sg; 90533a2ffceSStephen M. Cameron union u64bit temp64; 90633a2ffceSStephen M. Cameron 90733a2ffceSStephen M. Cameron if (c->Header.SGTotal <= h->max_cmd_sg_entries) 90833a2ffceSStephen M. Cameron return; 90933a2ffceSStephen M. Cameron 91033a2ffceSStephen M. Cameron chain_sg = &c->SG[h->max_cmd_sg_entries - 1]; 91133a2ffceSStephen M. Cameron temp64.val32.lower = chain_sg->Addr.lower; 91233a2ffceSStephen M. Cameron temp64.val32.upper = chain_sg->Addr.upper; 91333a2ffceSStephen M. Cameron pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE); 91433a2ffceSStephen M. Cameron } 91533a2ffceSStephen M. Cameron 916edd16368SStephen M. Cameron static void complete_scsi_command(struct CommandList *cp, 91701a02ffcSStephen M. Cameron int timeout, u32 tag) 918edd16368SStephen M. Cameron { 919edd16368SStephen M. Cameron struct scsi_cmnd *cmd; 920edd16368SStephen M. Cameron struct ctlr_info *h; 921edd16368SStephen M. Cameron struct ErrorInfo *ei; 922edd16368SStephen M. Cameron 923edd16368SStephen M. Cameron unsigned char sense_key; 924edd16368SStephen M. Cameron unsigned char asc; /* additional sense code */ 925edd16368SStephen M. Cameron unsigned char ascq; /* additional sense code qualifier */ 926edd16368SStephen M. Cameron 927edd16368SStephen M. Cameron ei = cp->err_info; 928edd16368SStephen M. Cameron cmd = (struct scsi_cmnd *) cp->scsi_cmd; 929edd16368SStephen M. Cameron h = cp->h; 930edd16368SStephen M. Cameron 931edd16368SStephen M. Cameron scsi_dma_unmap(cmd); /* undo the DMA mappings */ 93233a2ffceSStephen M. Cameron if (cp->Header.SGTotal > h->max_cmd_sg_entries) 93333a2ffceSStephen M. Cameron hpsa_unmap_sg_chain_block(h, cp); 934edd16368SStephen M. Cameron 935edd16368SStephen M. Cameron cmd->result = (DID_OK << 16); /* host byte */ 936edd16368SStephen M. Cameron cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */ 9375512672fSStephen M. Cameron cmd->result |= ei->ScsiStatus; 938edd16368SStephen M. Cameron 939edd16368SStephen M. Cameron /* copy the sense data whether we need to or not. */ 940edd16368SStephen M. Cameron memcpy(cmd->sense_buffer, ei->SenseInfo, 941edd16368SStephen M. Cameron ei->SenseLen > SCSI_SENSE_BUFFERSIZE ? 942edd16368SStephen M. Cameron SCSI_SENSE_BUFFERSIZE : 943edd16368SStephen M. Cameron ei->SenseLen); 944edd16368SStephen M. Cameron scsi_set_resid(cmd, ei->ResidualCnt); 945edd16368SStephen M. Cameron 946edd16368SStephen M. Cameron if (ei->CommandStatus == 0) { 947edd16368SStephen M. Cameron cmd->scsi_done(cmd); 948edd16368SStephen M. Cameron cmd_free(h, cp); 949edd16368SStephen M. Cameron return; 950edd16368SStephen M. Cameron } 951edd16368SStephen M. Cameron 952edd16368SStephen M. Cameron /* an error has occurred */ 953edd16368SStephen M. Cameron switch (ei->CommandStatus) { 954edd16368SStephen M. Cameron 955edd16368SStephen M. Cameron case CMD_TARGET_STATUS: 956edd16368SStephen M. Cameron if (ei->ScsiStatus) { 957edd16368SStephen M. Cameron /* Get sense key */ 958edd16368SStephen M. Cameron sense_key = 0xf & ei->SenseInfo[2]; 959edd16368SStephen M. Cameron /* Get additional sense code */ 960edd16368SStephen M. Cameron asc = ei->SenseInfo[12]; 961edd16368SStephen M. Cameron /* Get addition sense code qualifier */ 962edd16368SStephen M. Cameron ascq = ei->SenseInfo[13]; 963edd16368SStephen M. Cameron } 964edd16368SStephen M. Cameron 965edd16368SStephen M. Cameron if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) { 966edd16368SStephen M. Cameron if (check_for_unit_attention(h, cp)) { 967edd16368SStephen M. Cameron cmd->result = DID_SOFT_ERROR << 16; 968edd16368SStephen M. Cameron break; 969edd16368SStephen M. Cameron } 970edd16368SStephen M. Cameron if (sense_key == ILLEGAL_REQUEST) { 971edd16368SStephen M. Cameron /* 972edd16368SStephen M. Cameron * SCSI REPORT_LUNS is commonly unsupported on 973edd16368SStephen M. Cameron * Smart Array. Suppress noisy complaint. 974edd16368SStephen M. Cameron */ 975edd16368SStephen M. Cameron if (cp->Request.CDB[0] == REPORT_LUNS) 976edd16368SStephen M. Cameron break; 977edd16368SStephen M. Cameron 978edd16368SStephen M. Cameron /* If ASC/ASCQ indicate Logical Unit 979edd16368SStephen M. Cameron * Not Supported condition, 980edd16368SStephen M. Cameron */ 981edd16368SStephen M. Cameron if ((asc == 0x25) && (ascq == 0x0)) { 982edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p " 983edd16368SStephen M. Cameron "has check condition\n", cp); 984edd16368SStephen M. Cameron break; 985edd16368SStephen M. Cameron } 986edd16368SStephen M. Cameron } 987edd16368SStephen M. Cameron 988edd16368SStephen M. Cameron if (sense_key == NOT_READY) { 989edd16368SStephen M. Cameron /* If Sense is Not Ready, Logical Unit 990edd16368SStephen M. Cameron * Not ready, Manual Intervention 991edd16368SStephen M. Cameron * required 992edd16368SStephen M. Cameron */ 993edd16368SStephen M. Cameron if ((asc == 0x04) && (ascq == 0x03)) { 994edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p " 995edd16368SStephen M. Cameron "has check condition: unit " 996edd16368SStephen M. Cameron "not ready, manual " 997edd16368SStephen M. Cameron "intervention required\n", cp); 998edd16368SStephen M. Cameron break; 999edd16368SStephen M. Cameron } 1000edd16368SStephen M. Cameron } 10011d3b3609SMatt Gates if (sense_key == ABORTED_COMMAND) { 10021d3b3609SMatt Gates /* Aborted command is retryable */ 10031d3b3609SMatt Gates dev_warn(&h->pdev->dev, "cp %p " 10041d3b3609SMatt Gates "has check condition: aborted command: " 10051d3b3609SMatt Gates "ASC: 0x%x, ASCQ: 0x%x\n", 10061d3b3609SMatt Gates cp, asc, ascq); 10071d3b3609SMatt Gates cmd->result = DID_SOFT_ERROR << 16; 10081d3b3609SMatt Gates break; 10091d3b3609SMatt Gates } 1010edd16368SStephen M. Cameron /* Must be some other type of check condition */ 1011edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p has check condition: " 1012edd16368SStephen M. Cameron "unknown type: " 1013edd16368SStephen M. Cameron "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, " 1014edd16368SStephen M. Cameron "Returning result: 0x%x, " 1015edd16368SStephen M. Cameron "cmd=[%02x %02x %02x %02x %02x " 1016807be732SMike Miller "%02x %02x %02x %02x %02x %02x " 1017edd16368SStephen M. Cameron "%02x %02x %02x %02x %02x]\n", 1018edd16368SStephen M. Cameron cp, sense_key, asc, ascq, 1019edd16368SStephen M. Cameron cmd->result, 1020edd16368SStephen M. Cameron cmd->cmnd[0], cmd->cmnd[1], 1021edd16368SStephen M. Cameron cmd->cmnd[2], cmd->cmnd[3], 1022edd16368SStephen M. Cameron cmd->cmnd[4], cmd->cmnd[5], 1023edd16368SStephen M. Cameron cmd->cmnd[6], cmd->cmnd[7], 1024807be732SMike Miller cmd->cmnd[8], cmd->cmnd[9], 1025807be732SMike Miller cmd->cmnd[10], cmd->cmnd[11], 1026807be732SMike Miller cmd->cmnd[12], cmd->cmnd[13], 1027807be732SMike Miller cmd->cmnd[14], cmd->cmnd[15]); 1028edd16368SStephen M. Cameron break; 1029edd16368SStephen M. Cameron } 1030edd16368SStephen M. Cameron 1031edd16368SStephen M. Cameron 1032edd16368SStephen M. Cameron /* Problem was not a check condition 1033edd16368SStephen M. Cameron * Pass it up to the upper layers... 1034edd16368SStephen M. Cameron */ 1035edd16368SStephen M. Cameron if (ei->ScsiStatus) { 1036edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p has status 0x%x " 1037edd16368SStephen M. Cameron "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, " 1038edd16368SStephen M. Cameron "Returning result: 0x%x\n", 1039edd16368SStephen M. Cameron cp, ei->ScsiStatus, 1040edd16368SStephen M. Cameron sense_key, asc, ascq, 1041edd16368SStephen M. Cameron cmd->result); 1042edd16368SStephen M. Cameron } else { /* scsi status is zero??? How??? */ 1043edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. " 1044edd16368SStephen M. Cameron "Returning no connection.\n", cp), 1045edd16368SStephen M. Cameron 1046edd16368SStephen M. Cameron /* Ordinarily, this case should never happen, 1047edd16368SStephen M. Cameron * but there is a bug in some released firmware 1048edd16368SStephen M. Cameron * revisions that allows it to happen if, for 1049edd16368SStephen M. Cameron * example, a 4100 backplane loses power and 1050edd16368SStephen M. Cameron * the tape drive is in it. We assume that 1051edd16368SStephen M. Cameron * it's a fatal error of some kind because we 1052edd16368SStephen M. Cameron * can't show that it wasn't. We will make it 1053edd16368SStephen M. Cameron * look like selection timeout since that is 1054edd16368SStephen M. Cameron * the most common reason for this to occur, 1055edd16368SStephen M. Cameron * and it's severe enough. 1056edd16368SStephen M. Cameron */ 1057edd16368SStephen M. Cameron 1058edd16368SStephen M. Cameron cmd->result = DID_NO_CONNECT << 16; 1059edd16368SStephen M. Cameron } 1060edd16368SStephen M. Cameron break; 1061edd16368SStephen M. Cameron 1062edd16368SStephen M. Cameron case CMD_DATA_UNDERRUN: /* let mid layer handle it. */ 1063edd16368SStephen M. Cameron break; 1064edd16368SStephen M. Cameron case CMD_DATA_OVERRUN: 1065edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p has" 1066edd16368SStephen M. Cameron " completed with data overrun " 1067edd16368SStephen M. Cameron "reported\n", cp); 1068edd16368SStephen M. Cameron break; 1069edd16368SStephen M. Cameron case CMD_INVALID: { 1070edd16368SStephen M. Cameron /* print_bytes(cp, sizeof(*cp), 1, 0); 1071edd16368SStephen M. Cameron print_cmd(cp); */ 1072edd16368SStephen M. Cameron /* We get CMD_INVALID if you address a non-existent device 1073edd16368SStephen M. Cameron * instead of a selection timeout (no response). You will 1074edd16368SStephen M. Cameron * see this if you yank out a drive, then try to access it. 1075edd16368SStephen M. Cameron * This is kind of a shame because it means that any other 1076edd16368SStephen M. Cameron * CMD_INVALID (e.g. driver bug) will get interpreted as a 1077edd16368SStephen M. Cameron * missing target. */ 1078edd16368SStephen M. Cameron cmd->result = DID_NO_CONNECT << 16; 1079edd16368SStephen M. Cameron } 1080edd16368SStephen M. Cameron break; 1081edd16368SStephen M. Cameron case CMD_PROTOCOL_ERR: 1082edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p has " 1083edd16368SStephen M. Cameron "protocol error \n", cp); 1084edd16368SStephen M. Cameron break; 1085edd16368SStephen M. Cameron case CMD_HARDWARE_ERR: 1086edd16368SStephen M. Cameron cmd->result = DID_ERROR << 16; 1087edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp); 1088edd16368SStephen M. Cameron break; 1089edd16368SStephen M. Cameron case CMD_CONNECTION_LOST: 1090edd16368SStephen M. Cameron cmd->result = DID_ERROR << 16; 1091edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp); 1092edd16368SStephen M. Cameron break; 1093edd16368SStephen M. Cameron case CMD_ABORTED: 1094edd16368SStephen M. Cameron cmd->result = DID_ABORT << 16; 1095edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n", 1096edd16368SStephen M. Cameron cp, ei->ScsiStatus); 1097edd16368SStephen M. Cameron break; 1098edd16368SStephen M. Cameron case CMD_ABORT_FAILED: 1099edd16368SStephen M. Cameron cmd->result = DID_ERROR << 16; 1100edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp); 1101edd16368SStephen M. Cameron break; 1102edd16368SStephen M. Cameron case CMD_UNSOLICITED_ABORT: 11035f0325abSMatt Gates cmd->result = DID_RESET << 16; 1104edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited " 1105edd16368SStephen M. Cameron "abort\n", cp); 1106edd16368SStephen M. Cameron break; 1107edd16368SStephen M. Cameron case CMD_TIMEOUT: 1108edd16368SStephen M. Cameron cmd->result = DID_TIME_OUT << 16; 1109edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p timedout\n", cp); 1110edd16368SStephen M. Cameron break; 1111edd16368SStephen M. Cameron default: 1112edd16368SStephen M. Cameron cmd->result = DID_ERROR << 16; 1113edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n", 1114edd16368SStephen M. Cameron cp, ei->CommandStatus); 1115edd16368SStephen M. Cameron } 1116edd16368SStephen M. Cameron cmd->scsi_done(cmd); 1117edd16368SStephen M. Cameron cmd_free(h, cp); 1118edd16368SStephen M. Cameron } 1119edd16368SStephen M. Cameron 1120edd16368SStephen M. Cameron static int hpsa_scsi_detect(struct ctlr_info *h) 1121edd16368SStephen M. Cameron { 1122edd16368SStephen M. Cameron struct Scsi_Host *sh; 1123edd16368SStephen M. Cameron int error; 1124edd16368SStephen M. Cameron 1125edd16368SStephen M. Cameron sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h)); 1126edd16368SStephen M. Cameron if (sh == NULL) 1127edd16368SStephen M. Cameron goto fail; 1128edd16368SStephen M. Cameron 1129edd16368SStephen M. Cameron sh->io_port = 0; 1130edd16368SStephen M. Cameron sh->n_io_port = 0; 1131edd16368SStephen M. Cameron sh->this_id = -1; 1132edd16368SStephen M. Cameron sh->max_channel = 3; 1133edd16368SStephen M. Cameron sh->max_cmd_len = MAX_COMMAND_SIZE; 1134edd16368SStephen M. Cameron sh->max_lun = HPSA_MAX_LUN; 1135edd16368SStephen M. Cameron sh->max_id = HPSA_MAX_LUN; 1136303932fdSDon Brace sh->can_queue = h->nr_cmds; 1137303932fdSDon Brace sh->cmd_per_lun = h->nr_cmds; 113833a2ffceSStephen M. Cameron sh->sg_tablesize = h->maxsgentries; 1139edd16368SStephen M. Cameron h->scsi_host = sh; 1140edd16368SStephen M. Cameron sh->hostdata[0] = (unsigned long) h; 1141303932fdSDon Brace sh->irq = h->intr[PERF_MODE_INT]; 1142edd16368SStephen M. Cameron sh->unique_id = sh->irq; 1143edd16368SStephen M. Cameron error = scsi_add_host(sh, &h->pdev->dev); 1144edd16368SStephen M. Cameron if (error) 1145edd16368SStephen M. Cameron goto fail_host_put; 1146edd16368SStephen M. Cameron scsi_scan_host(sh); 1147edd16368SStephen M. Cameron return 0; 1148edd16368SStephen M. Cameron 1149edd16368SStephen M. Cameron fail_host_put: 1150edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host" 1151edd16368SStephen M. Cameron " failed for controller %d\n", h->ctlr); 1152edd16368SStephen M. Cameron scsi_host_put(sh); 1153ecd9aad4SStephen M. Cameron return error; 1154edd16368SStephen M. Cameron fail: 1155edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc" 1156edd16368SStephen M. Cameron " failed for controller %d\n", h->ctlr); 1157ecd9aad4SStephen M. Cameron return -ENOMEM; 1158edd16368SStephen M. Cameron } 1159edd16368SStephen M. Cameron 1160edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev, 1161edd16368SStephen M. Cameron struct CommandList *c, int sg_used, int data_direction) 1162edd16368SStephen M. Cameron { 1163edd16368SStephen M. Cameron int i; 1164edd16368SStephen M. Cameron union u64bit addr64; 1165edd16368SStephen M. Cameron 1166edd16368SStephen M. Cameron for (i = 0; i < sg_used; i++) { 1167edd16368SStephen M. Cameron addr64.val32.lower = c->SG[i].Addr.lower; 1168edd16368SStephen M. Cameron addr64.val32.upper = c->SG[i].Addr.upper; 1169edd16368SStephen M. Cameron pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len, 1170edd16368SStephen M. Cameron data_direction); 1171edd16368SStephen M. Cameron } 1172edd16368SStephen M. Cameron } 1173edd16368SStephen M. Cameron 1174edd16368SStephen M. Cameron static void hpsa_map_one(struct pci_dev *pdev, 1175edd16368SStephen M. Cameron struct CommandList *cp, 1176edd16368SStephen M. Cameron unsigned char *buf, 1177edd16368SStephen M. Cameron size_t buflen, 1178edd16368SStephen M. Cameron int data_direction) 1179edd16368SStephen M. Cameron { 118001a02ffcSStephen M. Cameron u64 addr64; 1181edd16368SStephen M. Cameron 1182edd16368SStephen M. Cameron if (buflen == 0 || data_direction == PCI_DMA_NONE) { 1183edd16368SStephen M. Cameron cp->Header.SGList = 0; 1184edd16368SStephen M. Cameron cp->Header.SGTotal = 0; 1185edd16368SStephen M. Cameron return; 1186edd16368SStephen M. Cameron } 1187edd16368SStephen M. Cameron 118801a02ffcSStephen M. Cameron addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction); 1189edd16368SStephen M. Cameron cp->SG[0].Addr.lower = 119001a02ffcSStephen M. Cameron (u32) (addr64 & (u64) 0x00000000FFFFFFFF); 1191edd16368SStephen M. Cameron cp->SG[0].Addr.upper = 119201a02ffcSStephen M. Cameron (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF); 1193edd16368SStephen M. Cameron cp->SG[0].Len = buflen; 119401a02ffcSStephen M. Cameron cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */ 119501a02ffcSStephen M. Cameron cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */ 1196edd16368SStephen M. Cameron } 1197edd16368SStephen M. Cameron 1198edd16368SStephen M. Cameron static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h, 1199edd16368SStephen M. Cameron struct CommandList *c) 1200edd16368SStephen M. Cameron { 1201edd16368SStephen M. Cameron DECLARE_COMPLETION_ONSTACK(wait); 1202edd16368SStephen M. Cameron 1203edd16368SStephen M. Cameron c->waiting = &wait; 1204edd16368SStephen M. Cameron enqueue_cmd_and_start_io(h, c); 1205edd16368SStephen M. Cameron wait_for_completion(&wait); 1206edd16368SStephen M. Cameron } 1207edd16368SStephen M. Cameron 1208edd16368SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h, 1209edd16368SStephen M. Cameron struct CommandList *c, int data_direction) 1210edd16368SStephen M. Cameron { 1211edd16368SStephen M. Cameron int retry_count = 0; 1212edd16368SStephen M. Cameron 1213edd16368SStephen M. Cameron do { 1214edd16368SStephen M. Cameron memset(c->err_info, 0, sizeof(c->err_info)); 1215edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 1216edd16368SStephen M. Cameron retry_count++; 1217edd16368SStephen M. Cameron } while (check_for_unit_attention(h, c) && retry_count <= 3); 1218edd16368SStephen M. Cameron hpsa_pci_unmap(h->pdev, c, 1, data_direction); 1219edd16368SStephen M. Cameron } 1220edd16368SStephen M. Cameron 1221edd16368SStephen M. Cameron static void hpsa_scsi_interpret_error(struct CommandList *cp) 1222edd16368SStephen M. Cameron { 1223edd16368SStephen M. Cameron struct ErrorInfo *ei; 1224edd16368SStephen M. Cameron struct device *d = &cp->h->pdev->dev; 1225edd16368SStephen M. Cameron 1226edd16368SStephen M. Cameron ei = cp->err_info; 1227edd16368SStephen M. Cameron switch (ei->CommandStatus) { 1228edd16368SStephen M. Cameron case CMD_TARGET_STATUS: 1229edd16368SStephen M. Cameron dev_warn(d, "cmd %p has completed with errors\n", cp); 1230edd16368SStephen M. Cameron dev_warn(d, "cmd %p has SCSI Status = %x\n", cp, 1231edd16368SStephen M. Cameron ei->ScsiStatus); 1232edd16368SStephen M. Cameron if (ei->ScsiStatus == 0) 1233edd16368SStephen M. Cameron dev_warn(d, "SCSI status is abnormally zero. " 1234edd16368SStephen M. Cameron "(probably indicates selection timeout " 1235edd16368SStephen M. Cameron "reported incorrectly due to a known " 1236edd16368SStephen M. Cameron "firmware bug, circa July, 2001.)\n"); 1237edd16368SStephen M. Cameron break; 1238edd16368SStephen M. Cameron case CMD_DATA_UNDERRUN: /* let mid layer handle it. */ 1239edd16368SStephen M. Cameron dev_info(d, "UNDERRUN\n"); 1240edd16368SStephen M. Cameron break; 1241edd16368SStephen M. Cameron case CMD_DATA_OVERRUN: 1242edd16368SStephen M. Cameron dev_warn(d, "cp %p has completed with data overrun\n", cp); 1243edd16368SStephen M. Cameron break; 1244edd16368SStephen M. Cameron case CMD_INVALID: { 1245edd16368SStephen M. Cameron /* controller unfortunately reports SCSI passthru's 1246edd16368SStephen M. Cameron * to non-existent targets as invalid commands. 1247edd16368SStephen M. Cameron */ 1248edd16368SStephen M. Cameron dev_warn(d, "cp %p is reported invalid (probably means " 1249edd16368SStephen M. Cameron "target device no longer present)\n", cp); 1250edd16368SStephen M. Cameron /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0); 1251edd16368SStephen M. Cameron print_cmd(cp); */ 1252edd16368SStephen M. Cameron } 1253edd16368SStephen M. Cameron break; 1254edd16368SStephen M. Cameron case CMD_PROTOCOL_ERR: 1255edd16368SStephen M. Cameron dev_warn(d, "cp %p has protocol error \n", cp); 1256edd16368SStephen M. Cameron break; 1257edd16368SStephen M. Cameron case CMD_HARDWARE_ERR: 1258edd16368SStephen M. Cameron /* cmd->result = DID_ERROR << 16; */ 1259edd16368SStephen M. Cameron dev_warn(d, "cp %p had hardware error\n", cp); 1260edd16368SStephen M. Cameron break; 1261edd16368SStephen M. Cameron case CMD_CONNECTION_LOST: 1262edd16368SStephen M. Cameron dev_warn(d, "cp %p had connection lost\n", cp); 1263edd16368SStephen M. Cameron break; 1264edd16368SStephen M. Cameron case CMD_ABORTED: 1265edd16368SStephen M. Cameron dev_warn(d, "cp %p was aborted\n", cp); 1266edd16368SStephen M. Cameron break; 1267edd16368SStephen M. Cameron case CMD_ABORT_FAILED: 1268edd16368SStephen M. Cameron dev_warn(d, "cp %p reports abort failed\n", cp); 1269edd16368SStephen M. Cameron break; 1270edd16368SStephen M. Cameron case CMD_UNSOLICITED_ABORT: 1271edd16368SStephen M. Cameron dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp); 1272edd16368SStephen M. Cameron break; 1273edd16368SStephen M. Cameron case CMD_TIMEOUT: 1274edd16368SStephen M. Cameron dev_warn(d, "cp %p timed out\n", cp); 1275edd16368SStephen M. Cameron break; 1276edd16368SStephen M. Cameron default: 1277edd16368SStephen M. Cameron dev_warn(d, "cp %p returned unknown status %x\n", cp, 1278edd16368SStephen M. Cameron ei->CommandStatus); 1279edd16368SStephen M. Cameron } 1280edd16368SStephen M. Cameron } 1281edd16368SStephen M. Cameron 1282edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr, 1283edd16368SStephen M. Cameron unsigned char page, unsigned char *buf, 1284edd16368SStephen M. Cameron unsigned char bufsize) 1285edd16368SStephen M. Cameron { 1286edd16368SStephen M. Cameron int rc = IO_OK; 1287edd16368SStephen M. Cameron struct CommandList *c; 1288edd16368SStephen M. Cameron struct ErrorInfo *ei; 1289edd16368SStephen M. Cameron 1290edd16368SStephen M. Cameron c = cmd_special_alloc(h); 1291edd16368SStephen M. Cameron 1292edd16368SStephen M. Cameron if (c == NULL) { /* trouble... */ 1293edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); 1294ecd9aad4SStephen M. Cameron return -ENOMEM; 1295edd16368SStephen M. Cameron } 1296edd16368SStephen M. Cameron 1297edd16368SStephen M. Cameron fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD); 1298edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE); 1299edd16368SStephen M. Cameron ei = c->err_info; 1300edd16368SStephen M. Cameron if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) { 1301edd16368SStephen M. Cameron hpsa_scsi_interpret_error(c); 1302edd16368SStephen M. Cameron rc = -1; 1303edd16368SStephen M. Cameron } 1304edd16368SStephen M. Cameron cmd_special_free(h, c); 1305edd16368SStephen M. Cameron return rc; 1306edd16368SStephen M. Cameron } 1307edd16368SStephen M. Cameron 1308edd16368SStephen M. Cameron static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr) 1309edd16368SStephen M. Cameron { 1310edd16368SStephen M. Cameron int rc = IO_OK; 1311edd16368SStephen M. Cameron struct CommandList *c; 1312edd16368SStephen M. Cameron struct ErrorInfo *ei; 1313edd16368SStephen M. Cameron 1314edd16368SStephen M. Cameron c = cmd_special_alloc(h); 1315edd16368SStephen M. Cameron 1316edd16368SStephen M. Cameron if (c == NULL) { /* trouble... */ 1317edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); 1318e9ea04a6SStephen M. Cameron return -ENOMEM; 1319edd16368SStephen M. Cameron } 1320edd16368SStephen M. Cameron 1321edd16368SStephen M. Cameron fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG); 1322edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 1323edd16368SStephen M. Cameron /* no unmap needed here because no data xfer. */ 1324edd16368SStephen M. Cameron 1325edd16368SStephen M. Cameron ei = c->err_info; 1326edd16368SStephen M. Cameron if (ei->CommandStatus != 0) { 1327edd16368SStephen M. Cameron hpsa_scsi_interpret_error(c); 1328edd16368SStephen M. Cameron rc = -1; 1329edd16368SStephen M. Cameron } 1330edd16368SStephen M. Cameron cmd_special_free(h, c); 1331edd16368SStephen M. Cameron return rc; 1332edd16368SStephen M. Cameron } 1333edd16368SStephen M. Cameron 1334edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h, 1335edd16368SStephen M. Cameron unsigned char *scsi3addr, unsigned char *raid_level) 1336edd16368SStephen M. Cameron { 1337edd16368SStephen M. Cameron int rc; 1338edd16368SStephen M. Cameron unsigned char *buf; 1339edd16368SStephen M. Cameron 1340edd16368SStephen M. Cameron *raid_level = RAID_UNKNOWN; 1341edd16368SStephen M. Cameron buf = kzalloc(64, GFP_KERNEL); 1342edd16368SStephen M. Cameron if (!buf) 1343edd16368SStephen M. Cameron return; 1344edd16368SStephen M. Cameron rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64); 1345edd16368SStephen M. Cameron if (rc == 0) 1346edd16368SStephen M. Cameron *raid_level = buf[8]; 1347edd16368SStephen M. Cameron if (*raid_level > RAID_UNKNOWN) 1348edd16368SStephen M. Cameron *raid_level = RAID_UNKNOWN; 1349edd16368SStephen M. Cameron kfree(buf); 1350edd16368SStephen M. Cameron return; 1351edd16368SStephen M. Cameron } 1352edd16368SStephen M. Cameron 1353edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */ 1354edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr, 1355edd16368SStephen M. Cameron unsigned char *device_id, int buflen) 1356edd16368SStephen M. Cameron { 1357edd16368SStephen M. Cameron int rc; 1358edd16368SStephen M. Cameron unsigned char *buf; 1359edd16368SStephen M. Cameron 1360edd16368SStephen M. Cameron if (buflen > 16) 1361edd16368SStephen M. Cameron buflen = 16; 1362edd16368SStephen M. Cameron buf = kzalloc(64, GFP_KERNEL); 1363edd16368SStephen M. Cameron if (!buf) 1364edd16368SStephen M. Cameron return -1; 1365edd16368SStephen M. Cameron rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64); 1366edd16368SStephen M. Cameron if (rc == 0) 1367edd16368SStephen M. Cameron memcpy(device_id, &buf[8], buflen); 1368edd16368SStephen M. Cameron kfree(buf); 1369edd16368SStephen M. Cameron return rc != 0; 1370edd16368SStephen M. Cameron } 1371edd16368SStephen M. Cameron 1372edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical, 1373edd16368SStephen M. Cameron struct ReportLUNdata *buf, int bufsize, 1374edd16368SStephen M. Cameron int extended_response) 1375edd16368SStephen M. Cameron { 1376edd16368SStephen M. Cameron int rc = IO_OK; 1377edd16368SStephen M. Cameron struct CommandList *c; 1378edd16368SStephen M. Cameron unsigned char scsi3addr[8]; 1379edd16368SStephen M. Cameron struct ErrorInfo *ei; 1380edd16368SStephen M. Cameron 1381edd16368SStephen M. Cameron c = cmd_special_alloc(h); 1382edd16368SStephen M. Cameron if (c == NULL) { /* trouble... */ 1383edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); 1384edd16368SStephen M. Cameron return -1; 1385edd16368SStephen M. Cameron } 1386e89c0ae7SStephen M. Cameron /* address the controller */ 1387e89c0ae7SStephen M. Cameron memset(scsi3addr, 0, sizeof(scsi3addr)); 1388edd16368SStephen M. Cameron fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h, 1389edd16368SStephen M. Cameron buf, bufsize, 0, scsi3addr, TYPE_CMD); 1390edd16368SStephen M. Cameron if (extended_response) 1391edd16368SStephen M. Cameron c->Request.CDB[1] = extended_response; 1392edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE); 1393edd16368SStephen M. Cameron ei = c->err_info; 1394edd16368SStephen M. Cameron if (ei->CommandStatus != 0 && 1395edd16368SStephen M. Cameron ei->CommandStatus != CMD_DATA_UNDERRUN) { 1396edd16368SStephen M. Cameron hpsa_scsi_interpret_error(c); 1397edd16368SStephen M. Cameron rc = -1; 1398edd16368SStephen M. Cameron } 1399edd16368SStephen M. Cameron cmd_special_free(h, c); 1400edd16368SStephen M. Cameron return rc; 1401edd16368SStephen M. Cameron } 1402edd16368SStephen M. Cameron 1403edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h, 1404edd16368SStephen M. Cameron struct ReportLUNdata *buf, 1405edd16368SStephen M. Cameron int bufsize, int extended_response) 1406edd16368SStephen M. Cameron { 1407edd16368SStephen M. Cameron return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response); 1408edd16368SStephen M. Cameron } 1409edd16368SStephen M. Cameron 1410edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h, 1411edd16368SStephen M. Cameron struct ReportLUNdata *buf, int bufsize) 1412edd16368SStephen M. Cameron { 1413edd16368SStephen M. Cameron return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0); 1414edd16368SStephen M. Cameron } 1415edd16368SStephen M. Cameron 1416edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device, 1417edd16368SStephen M. Cameron int bus, int target, int lun) 1418edd16368SStephen M. Cameron { 1419edd16368SStephen M. Cameron device->bus = bus; 1420edd16368SStephen M. Cameron device->target = target; 1421edd16368SStephen M. Cameron device->lun = lun; 1422edd16368SStephen M. Cameron } 1423edd16368SStephen M. Cameron 1424edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h, 1425edd16368SStephen M. Cameron unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device) 1426edd16368SStephen M. Cameron { 1427edd16368SStephen M. Cameron #define OBDR_TAPE_INQ_SIZE 49 1428ea6d3bc3SStephen M. Cameron unsigned char *inq_buff; 1429edd16368SStephen M. Cameron 1430ea6d3bc3SStephen M. Cameron inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL); 1431edd16368SStephen M. Cameron if (!inq_buff) 1432edd16368SStephen M. Cameron goto bail_out; 1433edd16368SStephen M. Cameron 1434edd16368SStephen M. Cameron /* Do an inquiry to the device to see what it is. */ 1435edd16368SStephen M. Cameron if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff, 1436edd16368SStephen M. Cameron (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) { 1437edd16368SStephen M. Cameron /* Inquiry failed (msg printed already) */ 1438edd16368SStephen M. Cameron dev_err(&h->pdev->dev, 1439edd16368SStephen M. Cameron "hpsa_update_device_info: inquiry failed\n"); 1440edd16368SStephen M. Cameron goto bail_out; 1441edd16368SStephen M. Cameron } 1442edd16368SStephen M. Cameron 1443edd16368SStephen M. Cameron /* As a side effect, record the firmware version number 1444edd16368SStephen M. Cameron * if we happen to be talking to the RAID controller. 1445edd16368SStephen M. Cameron */ 1446edd16368SStephen M. Cameron if (is_hba_lunid(scsi3addr)) 1447edd16368SStephen M. Cameron memcpy(h->firm_ver, &inq_buff[32], 4); 1448edd16368SStephen M. Cameron 1449edd16368SStephen M. Cameron this_device->devtype = (inq_buff[0] & 0x1f); 1450edd16368SStephen M. Cameron memcpy(this_device->scsi3addr, scsi3addr, 8); 1451edd16368SStephen M. Cameron memcpy(this_device->vendor, &inq_buff[8], 1452edd16368SStephen M. Cameron sizeof(this_device->vendor)); 1453edd16368SStephen M. Cameron memcpy(this_device->model, &inq_buff[16], 1454edd16368SStephen M. Cameron sizeof(this_device->model)); 1455edd16368SStephen M. Cameron memcpy(this_device->revision, &inq_buff[32], 1456edd16368SStephen M. Cameron sizeof(this_device->revision)); 1457edd16368SStephen M. Cameron memset(this_device->device_id, 0, 1458edd16368SStephen M. Cameron sizeof(this_device->device_id)); 1459edd16368SStephen M. Cameron hpsa_get_device_id(h, scsi3addr, this_device->device_id, 1460edd16368SStephen M. Cameron sizeof(this_device->device_id)); 1461edd16368SStephen M. Cameron 1462edd16368SStephen M. Cameron if (this_device->devtype == TYPE_DISK && 1463edd16368SStephen M. Cameron is_logical_dev_addr_mode(scsi3addr)) 1464edd16368SStephen M. Cameron hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level); 1465edd16368SStephen M. Cameron else 1466edd16368SStephen M. Cameron this_device->raid_level = RAID_UNKNOWN; 1467edd16368SStephen M. Cameron 1468edd16368SStephen M. Cameron kfree(inq_buff); 1469edd16368SStephen M. Cameron return 0; 1470edd16368SStephen M. Cameron 1471edd16368SStephen M. Cameron bail_out: 1472edd16368SStephen M. Cameron kfree(inq_buff); 1473edd16368SStephen M. Cameron return 1; 1474edd16368SStephen M. Cameron } 1475edd16368SStephen M. Cameron 1476edd16368SStephen M. Cameron static unsigned char *msa2xxx_model[] = { 1477edd16368SStephen M. Cameron "MSA2012", 1478edd16368SStephen M. Cameron "MSA2024", 1479edd16368SStephen M. Cameron "MSA2312", 1480edd16368SStephen M. Cameron "MSA2324", 1481edd16368SStephen M. Cameron NULL, 1482edd16368SStephen M. Cameron }; 1483edd16368SStephen M. Cameron 1484edd16368SStephen M. Cameron static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device) 1485edd16368SStephen M. Cameron { 1486edd16368SStephen M. Cameron int i; 1487edd16368SStephen M. Cameron 1488edd16368SStephen M. Cameron for (i = 0; msa2xxx_model[i]; i++) 1489edd16368SStephen M. Cameron if (strncmp(device->model, msa2xxx_model[i], 1490edd16368SStephen M. Cameron strlen(msa2xxx_model[i])) == 0) 1491edd16368SStephen M. Cameron return 1; 1492edd16368SStephen M. Cameron return 0; 1493edd16368SStephen M. Cameron } 1494edd16368SStephen M. Cameron 1495edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices. 1496edd16368SStephen M. Cameron * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical 1497edd16368SStephen M. Cameron * volumes on bus 1, physical devices on bus 2. and the hba on bus 3. 1498edd16368SStephen M. Cameron * Logical drive target and lun are assigned at this time, but 1499edd16368SStephen M. Cameron * physical device lun and target assignment are deferred (assigned 1500edd16368SStephen M. Cameron * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.) 1501edd16368SStephen M. Cameron */ 1502edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h, 150301a02ffcSStephen M. Cameron u8 *lunaddrbytes, int *bus, int *target, int *lun, 1504edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *device) 1505edd16368SStephen M. Cameron { 150601a02ffcSStephen M. Cameron u32 lunid; 1507edd16368SStephen M. Cameron 1508edd16368SStephen M. Cameron if (is_logical_dev_addr_mode(lunaddrbytes)) { 1509edd16368SStephen M. Cameron /* logical device */ 1510339b2b14SStephen M. Cameron if (unlikely(is_scsi_rev_5(h))) { 1511339b2b14SStephen M. Cameron /* p1210m, logical drives lun assignments 1512339b2b14SStephen M. Cameron * match SCSI REPORT LUNS data. 1513339b2b14SStephen M. Cameron */ 1514339b2b14SStephen M. Cameron lunid = le32_to_cpu(*((__le32 *) lunaddrbytes)); 1515339b2b14SStephen M. Cameron *bus = 0; 1516339b2b14SStephen M. Cameron *target = 0; 1517339b2b14SStephen M. Cameron *lun = (lunid & 0x3fff) + 1; 1518339b2b14SStephen M. Cameron } else { 1519339b2b14SStephen M. Cameron /* not p1210m... */ 15206df1e954SStephen M. Cameron lunid = le32_to_cpu(*((__le32 *) lunaddrbytes)); 1521edd16368SStephen M. Cameron if (is_msa2xxx(h, device)) { 1522339b2b14SStephen M. Cameron /* msa2xxx way, put logicals on bus 1 1523339b2b14SStephen M. Cameron * and match target/lun numbers box 1524339b2b14SStephen M. Cameron * reports. 1525339b2b14SStephen M. Cameron */ 1526edd16368SStephen M. Cameron *bus = 1; 1527edd16368SStephen M. Cameron *target = (lunid >> 16) & 0x3fff; 1528edd16368SStephen M. Cameron *lun = lunid & 0x00ff; 1529edd16368SStephen M. Cameron } else { 1530339b2b14SStephen M. Cameron /* Traditional smart array way. */ 1531edd16368SStephen M. Cameron *bus = 0; 1532edd16368SStephen M. Cameron *lun = 0; 1533edd16368SStephen M. Cameron *target = lunid & 0x3fff; 1534edd16368SStephen M. Cameron } 1535339b2b14SStephen M. Cameron } 1536edd16368SStephen M. Cameron } else { 1537edd16368SStephen M. Cameron /* physical device */ 1538edd16368SStephen M. Cameron if (is_hba_lunid(lunaddrbytes)) 1539339b2b14SStephen M. Cameron if (unlikely(is_scsi_rev_5(h))) { 1540339b2b14SStephen M. Cameron *bus = 0; /* put p1210m ctlr at 0,0,0 */ 1541339b2b14SStephen M. Cameron *target = 0; 1542339b2b14SStephen M. Cameron *lun = 0; 1543339b2b14SStephen M. Cameron return; 1544339b2b14SStephen M. Cameron } else 1545339b2b14SStephen M. Cameron *bus = 3; /* traditional smartarray */ 1546edd16368SStephen M. Cameron else 1547339b2b14SStephen M. Cameron *bus = 2; /* physical disk */ 1548edd16368SStephen M. Cameron *target = -1; 1549edd16368SStephen M. Cameron *lun = -1; /* we will fill these in later. */ 1550edd16368SStephen M. Cameron } 1551edd16368SStephen M. Cameron } 1552edd16368SStephen M. Cameron 1553edd16368SStephen M. Cameron /* 1554edd16368SStephen M. Cameron * If there is no lun 0 on a target, linux won't find any devices. 1555edd16368SStephen M. Cameron * For the MSA2xxx boxes, we have to manually detect the enclosure 1556edd16368SStephen M. Cameron * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report 1557edd16368SStephen M. Cameron * it for some reason. *tmpdevice is the target we're adding, 1558edd16368SStephen M. Cameron * this_device is a pointer into the current element of currentsd[] 1559edd16368SStephen M. Cameron * that we're building up in update_scsi_devices(), below. 1560edd16368SStephen M. Cameron * lunzerobits is a bitmap that tracks which targets already have a 1561edd16368SStephen M. Cameron * lun 0 assigned. 1562edd16368SStephen M. Cameron * Returns 1 if an enclosure was added, 0 if not. 1563edd16368SStephen M. Cameron */ 1564edd16368SStephen M. Cameron static int add_msa2xxx_enclosure_device(struct ctlr_info *h, 1565edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *tmpdevice, 156601a02ffcSStephen M. Cameron struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes, 1567edd16368SStephen M. Cameron int bus, int target, int lun, unsigned long lunzerobits[], 1568edd16368SStephen M. Cameron int *nmsa2xxx_enclosures) 1569edd16368SStephen M. Cameron { 1570edd16368SStephen M. Cameron unsigned char scsi3addr[8]; 1571edd16368SStephen M. Cameron 1572edd16368SStephen M. Cameron if (test_bit(target, lunzerobits)) 1573edd16368SStephen M. Cameron return 0; /* There is already a lun 0 on this target. */ 1574edd16368SStephen M. Cameron 1575edd16368SStephen M. Cameron if (!is_logical_dev_addr_mode(lunaddrbytes)) 1576edd16368SStephen M. Cameron return 0; /* It's the logical targets that may lack lun 0. */ 1577edd16368SStephen M. Cameron 1578edd16368SStephen M. Cameron if (!is_msa2xxx(h, tmpdevice)) 1579edd16368SStephen M. Cameron return 0; /* It's only the MSA2xxx that have this problem. */ 1580edd16368SStephen M. Cameron 1581edd16368SStephen M. Cameron if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */ 1582edd16368SStephen M. Cameron return 0; 1583edd16368SStephen M. Cameron 1584edd16368SStephen M. Cameron if (is_hba_lunid(scsi3addr)) 1585edd16368SStephen M. Cameron return 0; /* Don't add the RAID controller here. */ 1586edd16368SStephen M. Cameron 1587339b2b14SStephen M. Cameron if (is_scsi_rev_5(h)) 1588339b2b14SStephen M. Cameron return 0; /* p1210m doesn't need to do this. */ 1589339b2b14SStephen M. Cameron 1590edd16368SStephen M. Cameron #define MAX_MSA2XXX_ENCLOSURES 32 1591edd16368SStephen M. Cameron if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) { 1592edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX " 1593edd16368SStephen M. Cameron "enclosures exceeded. Check your hardware " 1594edd16368SStephen M. Cameron "configuration."); 1595edd16368SStephen M. Cameron return 0; 1596edd16368SStephen M. Cameron } 1597edd16368SStephen M. Cameron 1598edd16368SStephen M. Cameron memset(scsi3addr, 0, 8); 1599edd16368SStephen M. Cameron scsi3addr[3] = target; 1600edd16368SStephen M. Cameron if (hpsa_update_device_info(h, scsi3addr, this_device)) 1601edd16368SStephen M. Cameron return 0; 1602edd16368SStephen M. Cameron (*nmsa2xxx_enclosures)++; 1603edd16368SStephen M. Cameron hpsa_set_bus_target_lun(this_device, bus, target, 0); 1604edd16368SStephen M. Cameron set_bit(target, lunzerobits); 1605edd16368SStephen M. Cameron return 1; 1606edd16368SStephen M. Cameron } 1607edd16368SStephen M. Cameron 1608edd16368SStephen M. Cameron /* 1609edd16368SStephen M. Cameron * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev, 1610edd16368SStephen M. Cameron * logdev. The number of luns in physdev and logdev are returned in 1611edd16368SStephen M. Cameron * *nphysicals and *nlogicals, respectively. 1612edd16368SStephen M. Cameron * Returns 0 on success, -1 otherwise. 1613edd16368SStephen M. Cameron */ 1614edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h, 1615edd16368SStephen M. Cameron int reportlunsize, 161601a02ffcSStephen M. Cameron struct ReportLUNdata *physdev, u32 *nphysicals, 161701a02ffcSStephen M. Cameron struct ReportLUNdata *logdev, u32 *nlogicals) 1618edd16368SStephen M. Cameron { 1619edd16368SStephen M. Cameron if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) { 1620edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "report physical LUNs failed.\n"); 1621edd16368SStephen M. Cameron return -1; 1622edd16368SStephen M. Cameron } 16236df1e954SStephen M. Cameron *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8; 1624edd16368SStephen M. Cameron if (*nphysicals > HPSA_MAX_PHYS_LUN) { 1625edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded." 1626edd16368SStephen M. Cameron " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN, 1627edd16368SStephen M. Cameron *nphysicals - HPSA_MAX_PHYS_LUN); 1628edd16368SStephen M. Cameron *nphysicals = HPSA_MAX_PHYS_LUN; 1629edd16368SStephen M. Cameron } 1630edd16368SStephen M. Cameron if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) { 1631edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "report logical LUNs failed.\n"); 1632edd16368SStephen M. Cameron return -1; 1633edd16368SStephen M. Cameron } 16346df1e954SStephen M. Cameron *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8; 1635edd16368SStephen M. Cameron /* Reject Logicals in excess of our max capability. */ 1636edd16368SStephen M. Cameron if (*nlogicals > HPSA_MAX_LUN) { 1637edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, 1638edd16368SStephen M. Cameron "maximum logical LUNs (%d) exceeded. " 1639edd16368SStephen M. Cameron "%d LUNs ignored.\n", HPSA_MAX_LUN, 1640edd16368SStephen M. Cameron *nlogicals - HPSA_MAX_LUN); 1641edd16368SStephen M. Cameron *nlogicals = HPSA_MAX_LUN; 1642edd16368SStephen M. Cameron } 1643edd16368SStephen M. Cameron if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) { 1644edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, 1645edd16368SStephen M. Cameron "maximum logical + physical LUNs (%d) exceeded. " 1646edd16368SStephen M. Cameron "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN, 1647edd16368SStephen M. Cameron *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN); 1648edd16368SStephen M. Cameron *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals; 1649edd16368SStephen M. Cameron } 1650edd16368SStephen M. Cameron return 0; 1651edd16368SStephen M. Cameron } 1652edd16368SStephen M. Cameron 1653339b2b14SStephen M. Cameron u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i, 1654339b2b14SStephen M. Cameron int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list, 1655339b2b14SStephen M. Cameron struct ReportLUNdata *logdev_list) 1656339b2b14SStephen M. Cameron { 1657339b2b14SStephen M. Cameron /* Helper function, figure out where the LUN ID info is coming from 1658339b2b14SStephen M. Cameron * given index i, lists of physical and logical devices, where in 1659339b2b14SStephen M. Cameron * the list the raid controller is supposed to appear (first or last) 1660339b2b14SStephen M. Cameron */ 1661339b2b14SStephen M. Cameron 1662339b2b14SStephen M. Cameron int logicals_start = nphysicals + (raid_ctlr_position == 0); 1663339b2b14SStephen M. Cameron int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0); 1664339b2b14SStephen M. Cameron 1665339b2b14SStephen M. Cameron if (i == raid_ctlr_position) 1666339b2b14SStephen M. Cameron return RAID_CTLR_LUNID; 1667339b2b14SStephen M. Cameron 1668339b2b14SStephen M. Cameron if (i < logicals_start) 1669339b2b14SStephen M. Cameron return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0]; 1670339b2b14SStephen M. Cameron 1671339b2b14SStephen M. Cameron if (i < last_device) 1672339b2b14SStephen M. Cameron return &logdev_list->LUN[i - nphysicals - 1673339b2b14SStephen M. Cameron (raid_ctlr_position == 0)][0]; 1674339b2b14SStephen M. Cameron BUG(); 1675339b2b14SStephen M. Cameron return NULL; 1676339b2b14SStephen M. Cameron } 1677339b2b14SStephen M. Cameron 1678edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno) 1679edd16368SStephen M. Cameron { 1680edd16368SStephen M. Cameron /* the idea here is we could get notified 1681edd16368SStephen M. Cameron * that some devices have changed, so we do a report 1682edd16368SStephen M. Cameron * physical luns and report logical luns cmd, and adjust 1683edd16368SStephen M. Cameron * our list of devices accordingly. 1684edd16368SStephen M. Cameron * 1685edd16368SStephen M. Cameron * The scsi3addr's of devices won't change so long as the 1686edd16368SStephen M. Cameron * adapter is not reset. That means we can rescan and 1687edd16368SStephen M. Cameron * tell which devices we already know about, vs. new 1688edd16368SStephen M. Cameron * devices, vs. disappearing devices. 1689edd16368SStephen M. Cameron */ 1690edd16368SStephen M. Cameron struct ReportLUNdata *physdev_list = NULL; 1691edd16368SStephen M. Cameron struct ReportLUNdata *logdev_list = NULL; 1692edd16368SStephen M. Cameron unsigned char *inq_buff = NULL; 169301a02ffcSStephen M. Cameron u32 nphysicals = 0; 169401a02ffcSStephen M. Cameron u32 nlogicals = 0; 169501a02ffcSStephen M. Cameron u32 ndev_allocated = 0; 1696edd16368SStephen M. Cameron struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice; 1697edd16368SStephen M. Cameron int ncurrent = 0; 1698edd16368SStephen M. Cameron int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8; 1699edd16368SStephen M. Cameron int i, nmsa2xxx_enclosures, ndevs_to_allocate; 1700edd16368SStephen M. Cameron int bus, target, lun; 1701339b2b14SStephen M. Cameron int raid_ctlr_position; 1702edd16368SStephen M. Cameron DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR); 1703edd16368SStephen M. Cameron 1704edd16368SStephen M. Cameron currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA, 1705edd16368SStephen M. Cameron GFP_KERNEL); 1706edd16368SStephen M. Cameron physdev_list = kzalloc(reportlunsize, GFP_KERNEL); 1707edd16368SStephen M. Cameron logdev_list = kzalloc(reportlunsize, GFP_KERNEL); 1708edd16368SStephen M. Cameron inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL); 1709edd16368SStephen M. Cameron tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL); 1710edd16368SStephen M. Cameron 1711edd16368SStephen M. Cameron if (!currentsd || !physdev_list || !logdev_list || 1712edd16368SStephen M. Cameron !inq_buff || !tmpdevice) { 1713edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "out of memory\n"); 1714edd16368SStephen M. Cameron goto out; 1715edd16368SStephen M. Cameron } 1716edd16368SStephen M. Cameron memset(lunzerobits, 0, sizeof(lunzerobits)); 1717edd16368SStephen M. Cameron 1718edd16368SStephen M. Cameron if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals, 1719edd16368SStephen M. Cameron logdev_list, &nlogicals)) 1720edd16368SStephen M. Cameron goto out; 1721edd16368SStephen M. Cameron 1722edd16368SStephen M. Cameron /* We might see up to 32 MSA2xxx enclosures, actually 8 of them 1723edd16368SStephen M. Cameron * but each of them 4 times through different paths. The plus 1 1724edd16368SStephen M. Cameron * is for the RAID controller. 1725edd16368SStephen M. Cameron */ 1726edd16368SStephen M. Cameron ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1; 1727edd16368SStephen M. Cameron 1728edd16368SStephen M. Cameron /* Allocate the per device structures */ 1729edd16368SStephen M. Cameron for (i = 0; i < ndevs_to_allocate; i++) { 1730edd16368SStephen M. Cameron currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL); 1731edd16368SStephen M. Cameron if (!currentsd[i]) { 1732edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "out of memory at %s:%d\n", 1733edd16368SStephen M. Cameron __FILE__, __LINE__); 1734edd16368SStephen M. Cameron goto out; 1735edd16368SStephen M. Cameron } 1736edd16368SStephen M. Cameron ndev_allocated++; 1737edd16368SStephen M. Cameron } 1738edd16368SStephen M. Cameron 1739339b2b14SStephen M. Cameron if (unlikely(is_scsi_rev_5(h))) 1740339b2b14SStephen M. Cameron raid_ctlr_position = 0; 1741339b2b14SStephen M. Cameron else 1742339b2b14SStephen M. Cameron raid_ctlr_position = nphysicals + nlogicals; 1743339b2b14SStephen M. Cameron 1744edd16368SStephen M. Cameron /* adjust our table of devices */ 1745edd16368SStephen M. Cameron nmsa2xxx_enclosures = 0; 1746edd16368SStephen M. Cameron for (i = 0; i < nphysicals + nlogicals + 1; i++) { 174701a02ffcSStephen M. Cameron u8 *lunaddrbytes; 1748edd16368SStephen M. Cameron 1749edd16368SStephen M. Cameron /* Figure out where the LUN ID info is coming from */ 1750339b2b14SStephen M. Cameron lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position, 1751339b2b14SStephen M. Cameron i, nphysicals, nlogicals, physdev_list, logdev_list); 1752edd16368SStephen M. Cameron /* skip masked physical devices. */ 1753339b2b14SStephen M. Cameron if (lunaddrbytes[3] & 0xC0 && 1754339b2b14SStephen M. Cameron i < nphysicals + (raid_ctlr_position == 0)) 1755edd16368SStephen M. Cameron continue; 1756edd16368SStephen M. Cameron 1757edd16368SStephen M. Cameron /* Get device type, vendor, model, device id */ 1758edd16368SStephen M. Cameron if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice)) 1759edd16368SStephen M. Cameron continue; /* skip it if we can't talk to it. */ 1760edd16368SStephen M. Cameron figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun, 1761edd16368SStephen M. Cameron tmpdevice); 1762edd16368SStephen M. Cameron this_device = currentsd[ncurrent]; 1763edd16368SStephen M. Cameron 1764edd16368SStephen M. Cameron /* 1765edd16368SStephen M. Cameron * For the msa2xxx boxes, we have to insert a LUN 0 which 1766edd16368SStephen M. Cameron * doesn't show up in CCISS_REPORT_PHYSICAL data, but there 1767edd16368SStephen M. Cameron * is nonetheless an enclosure device there. We have to 1768edd16368SStephen M. Cameron * present that otherwise linux won't find anything if 1769edd16368SStephen M. Cameron * there is no lun 0. 1770edd16368SStephen M. Cameron */ 1771edd16368SStephen M. Cameron if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device, 1772edd16368SStephen M. Cameron lunaddrbytes, bus, target, lun, lunzerobits, 1773edd16368SStephen M. Cameron &nmsa2xxx_enclosures)) { 1774edd16368SStephen M. Cameron ncurrent++; 1775edd16368SStephen M. Cameron this_device = currentsd[ncurrent]; 1776edd16368SStephen M. Cameron } 1777edd16368SStephen M. Cameron 1778edd16368SStephen M. Cameron *this_device = *tmpdevice; 1779edd16368SStephen M. Cameron hpsa_set_bus_target_lun(this_device, bus, target, lun); 1780edd16368SStephen M. Cameron 1781edd16368SStephen M. Cameron switch (this_device->devtype) { 1782edd16368SStephen M. Cameron case TYPE_ROM: { 1783edd16368SStephen M. Cameron /* We don't *really* support actual CD-ROM devices, 1784edd16368SStephen M. Cameron * just "One Button Disaster Recovery" tape drive 1785edd16368SStephen M. Cameron * which temporarily pretends to be a CD-ROM drive. 1786edd16368SStephen M. Cameron * So we check that the device is really an OBDR tape 1787edd16368SStephen M. Cameron * device by checking for "$DR-10" in bytes 43-48 of 1788edd16368SStephen M. Cameron * the inquiry data. 1789edd16368SStephen M. Cameron */ 1790edd16368SStephen M. Cameron char obdr_sig[7]; 1791edd16368SStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10" 1792edd16368SStephen M. Cameron strncpy(obdr_sig, &inq_buff[43], 6); 1793edd16368SStephen M. Cameron obdr_sig[6] = '\0'; 1794edd16368SStephen M. Cameron if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0) 1795edd16368SStephen M. Cameron /* Not OBDR device, ignore it. */ 1796edd16368SStephen M. Cameron break; 1797edd16368SStephen M. Cameron } 1798edd16368SStephen M. Cameron ncurrent++; 1799edd16368SStephen M. Cameron break; 1800edd16368SStephen M. Cameron case TYPE_DISK: 1801edd16368SStephen M. Cameron if (i < nphysicals) 1802edd16368SStephen M. Cameron break; 1803edd16368SStephen M. Cameron ncurrent++; 1804edd16368SStephen M. Cameron break; 1805edd16368SStephen M. Cameron case TYPE_TAPE: 1806edd16368SStephen M. Cameron case TYPE_MEDIUM_CHANGER: 1807edd16368SStephen M. Cameron ncurrent++; 1808edd16368SStephen M. Cameron break; 1809edd16368SStephen M. Cameron case TYPE_RAID: 1810edd16368SStephen M. Cameron /* Only present the Smartarray HBA as a RAID controller. 1811edd16368SStephen M. Cameron * If it's a RAID controller other than the HBA itself 1812edd16368SStephen M. Cameron * (an external RAID controller, MSA500 or similar) 1813edd16368SStephen M. Cameron * don't present it. 1814edd16368SStephen M. Cameron */ 1815edd16368SStephen M. Cameron if (!is_hba_lunid(lunaddrbytes)) 1816edd16368SStephen M. Cameron break; 1817edd16368SStephen M. Cameron ncurrent++; 1818edd16368SStephen M. Cameron break; 1819edd16368SStephen M. Cameron default: 1820edd16368SStephen M. Cameron break; 1821edd16368SStephen M. Cameron } 1822edd16368SStephen M. Cameron if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA) 1823edd16368SStephen M. Cameron break; 1824edd16368SStephen M. Cameron } 1825edd16368SStephen M. Cameron adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent); 1826edd16368SStephen M. Cameron out: 1827edd16368SStephen M. Cameron kfree(tmpdevice); 1828edd16368SStephen M. Cameron for (i = 0; i < ndev_allocated; i++) 1829edd16368SStephen M. Cameron kfree(currentsd[i]); 1830edd16368SStephen M. Cameron kfree(currentsd); 1831edd16368SStephen M. Cameron kfree(inq_buff); 1832edd16368SStephen M. Cameron kfree(physdev_list); 1833edd16368SStephen M. Cameron kfree(logdev_list); 1834edd16368SStephen M. Cameron } 1835edd16368SStephen M. Cameron 1836edd16368SStephen M. Cameron /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci 1837edd16368SStephen M. Cameron * dma mapping and fills in the scatter gather entries of the 1838edd16368SStephen M. Cameron * hpsa command, cp. 1839edd16368SStephen M. Cameron */ 184033a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h, 1841edd16368SStephen M. Cameron struct CommandList *cp, 1842edd16368SStephen M. Cameron struct scsi_cmnd *cmd) 1843edd16368SStephen M. Cameron { 1844edd16368SStephen M. Cameron unsigned int len; 1845edd16368SStephen M. Cameron struct scatterlist *sg; 184601a02ffcSStephen M. Cameron u64 addr64; 184733a2ffceSStephen M. Cameron int use_sg, i, sg_index, chained; 184833a2ffceSStephen M. Cameron struct SGDescriptor *curr_sg; 1849edd16368SStephen M. Cameron 185033a2ffceSStephen M. Cameron BUG_ON(scsi_sg_count(cmd) > h->maxsgentries); 1851edd16368SStephen M. Cameron 1852edd16368SStephen M. Cameron use_sg = scsi_dma_map(cmd); 1853edd16368SStephen M. Cameron if (use_sg < 0) 1854edd16368SStephen M. Cameron return use_sg; 1855edd16368SStephen M. Cameron 1856edd16368SStephen M. Cameron if (!use_sg) 1857edd16368SStephen M. Cameron goto sglist_finished; 1858edd16368SStephen M. Cameron 185933a2ffceSStephen M. Cameron curr_sg = cp->SG; 186033a2ffceSStephen M. Cameron chained = 0; 186133a2ffceSStephen M. Cameron sg_index = 0; 1862edd16368SStephen M. Cameron scsi_for_each_sg(cmd, sg, use_sg, i) { 186333a2ffceSStephen M. Cameron if (i == h->max_cmd_sg_entries - 1 && 186433a2ffceSStephen M. Cameron use_sg > h->max_cmd_sg_entries) { 186533a2ffceSStephen M. Cameron chained = 1; 186633a2ffceSStephen M. Cameron curr_sg = h->cmd_sg_list[cp->cmdindex]; 186733a2ffceSStephen M. Cameron sg_index = 0; 186833a2ffceSStephen M. Cameron } 186901a02ffcSStephen M. Cameron addr64 = (u64) sg_dma_address(sg); 1870edd16368SStephen M. Cameron len = sg_dma_len(sg); 187133a2ffceSStephen M. Cameron curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL); 187233a2ffceSStephen M. Cameron curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL); 187333a2ffceSStephen M. Cameron curr_sg->Len = len; 187433a2ffceSStephen M. Cameron curr_sg->Ext = 0; /* we are not chaining */ 187533a2ffceSStephen M. Cameron curr_sg++; 187633a2ffceSStephen M. Cameron } 187733a2ffceSStephen M. Cameron 187833a2ffceSStephen M. Cameron if (use_sg + chained > h->maxSG) 187933a2ffceSStephen M. Cameron h->maxSG = use_sg + chained; 188033a2ffceSStephen M. Cameron 188133a2ffceSStephen M. Cameron if (chained) { 188233a2ffceSStephen M. Cameron cp->Header.SGList = h->max_cmd_sg_entries; 188333a2ffceSStephen M. Cameron cp->Header.SGTotal = (u16) (use_sg + 1); 188433a2ffceSStephen M. Cameron hpsa_map_sg_chain_block(h, cp); 188533a2ffceSStephen M. Cameron return 0; 1886edd16368SStephen M. Cameron } 1887edd16368SStephen M. Cameron 1888edd16368SStephen M. Cameron sglist_finished: 1889edd16368SStephen M. Cameron 189001a02ffcSStephen M. Cameron cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */ 189101a02ffcSStephen M. Cameron cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */ 1892edd16368SStephen M. Cameron return 0; 1893edd16368SStephen M. Cameron } 1894edd16368SStephen M. Cameron 1895edd16368SStephen M. Cameron 1896edd16368SStephen M. Cameron static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd, 1897edd16368SStephen M. Cameron void (*done)(struct scsi_cmnd *)) 1898edd16368SStephen M. Cameron { 1899edd16368SStephen M. Cameron struct ctlr_info *h; 1900edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *dev; 1901edd16368SStephen M. Cameron unsigned char scsi3addr[8]; 1902edd16368SStephen M. Cameron struct CommandList *c; 1903edd16368SStephen M. Cameron unsigned long flags; 1904edd16368SStephen M. Cameron 1905edd16368SStephen M. Cameron /* Get the ptr to our adapter structure out of cmd->host. */ 1906edd16368SStephen M. Cameron h = sdev_to_hba(cmd->device); 1907edd16368SStephen M. Cameron dev = cmd->device->hostdata; 1908edd16368SStephen M. Cameron if (!dev) { 1909edd16368SStephen M. Cameron cmd->result = DID_NO_CONNECT << 16; 1910edd16368SStephen M. Cameron done(cmd); 1911edd16368SStephen M. Cameron return 0; 1912edd16368SStephen M. Cameron } 1913edd16368SStephen M. Cameron memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr)); 1914edd16368SStephen M. Cameron 1915edd16368SStephen M. Cameron /* Need a lock as this is being allocated from the pool */ 1916edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 1917edd16368SStephen M. Cameron c = cmd_alloc(h); 1918edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 1919edd16368SStephen M. Cameron if (c == NULL) { /* trouble... */ 1920edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n"); 1921edd16368SStephen M. Cameron return SCSI_MLQUEUE_HOST_BUSY; 1922edd16368SStephen M. Cameron } 1923edd16368SStephen M. Cameron 1924edd16368SStephen M. Cameron /* Fill in the command list header */ 1925edd16368SStephen M. Cameron 1926edd16368SStephen M. Cameron cmd->scsi_done = done; /* save this for use by completion code */ 1927edd16368SStephen M. Cameron 1928edd16368SStephen M. Cameron /* save c in case we have to abort it */ 1929edd16368SStephen M. Cameron cmd->host_scribble = (unsigned char *) c; 1930edd16368SStephen M. Cameron 1931edd16368SStephen M. Cameron c->cmd_type = CMD_SCSI; 1932edd16368SStephen M. Cameron c->scsi_cmd = cmd; 1933edd16368SStephen M. Cameron c->Header.ReplyQueue = 0; /* unused in simple mode */ 1934edd16368SStephen M. Cameron memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8); 1935303932fdSDon Brace c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT); 1936303932fdSDon Brace c->Header.Tag.lower |= DIRECT_LOOKUP_BIT; 1937edd16368SStephen M. Cameron 1938edd16368SStephen M. Cameron /* Fill in the request block... */ 1939edd16368SStephen M. Cameron 1940edd16368SStephen M. Cameron c->Request.Timeout = 0; 1941edd16368SStephen M. Cameron memset(c->Request.CDB, 0, sizeof(c->Request.CDB)); 1942edd16368SStephen M. Cameron BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB)); 1943edd16368SStephen M. Cameron c->Request.CDBLen = cmd->cmd_len; 1944edd16368SStephen M. Cameron memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len); 1945edd16368SStephen M. Cameron c->Request.Type.Type = TYPE_CMD; 1946edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 1947edd16368SStephen M. Cameron switch (cmd->sc_data_direction) { 1948edd16368SStephen M. Cameron case DMA_TO_DEVICE: 1949edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_WRITE; 1950edd16368SStephen M. Cameron break; 1951edd16368SStephen M. Cameron case DMA_FROM_DEVICE: 1952edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_READ; 1953edd16368SStephen M. Cameron break; 1954edd16368SStephen M. Cameron case DMA_NONE: 1955edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_NONE; 1956edd16368SStephen M. Cameron break; 1957edd16368SStephen M. Cameron case DMA_BIDIRECTIONAL: 1958edd16368SStephen M. Cameron /* This can happen if a buggy application does a scsi passthru 1959edd16368SStephen M. Cameron * and sets both inlen and outlen to non-zero. ( see 1960edd16368SStephen M. Cameron * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() ) 1961edd16368SStephen M. Cameron */ 1962edd16368SStephen M. Cameron 1963edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_RSVD; 1964edd16368SStephen M. Cameron /* This is technically wrong, and hpsa controllers should 1965edd16368SStephen M. Cameron * reject it with CMD_INVALID, which is the most correct 1966edd16368SStephen M. Cameron * response, but non-fibre backends appear to let it 1967edd16368SStephen M. Cameron * slide by, and give the same results as if this field 1968edd16368SStephen M. Cameron * were set correctly. Either way is acceptable for 1969edd16368SStephen M. Cameron * our purposes here. 1970edd16368SStephen M. Cameron */ 1971edd16368SStephen M. Cameron 1972edd16368SStephen M. Cameron break; 1973edd16368SStephen M. Cameron 1974edd16368SStephen M. Cameron default: 1975edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "unknown data direction: %d\n", 1976edd16368SStephen M. Cameron cmd->sc_data_direction); 1977edd16368SStephen M. Cameron BUG(); 1978edd16368SStephen M. Cameron break; 1979edd16368SStephen M. Cameron } 1980edd16368SStephen M. Cameron 198133a2ffceSStephen M. Cameron if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */ 1982edd16368SStephen M. Cameron cmd_free(h, c); 1983edd16368SStephen M. Cameron return SCSI_MLQUEUE_HOST_BUSY; 1984edd16368SStephen M. Cameron } 1985edd16368SStephen M. Cameron enqueue_cmd_and_start_io(h, c); 1986edd16368SStephen M. Cameron /* the cmd'll come back via intr handler in complete_scsi_command() */ 1987edd16368SStephen M. Cameron return 0; 1988edd16368SStephen M. Cameron } 1989edd16368SStephen M. Cameron 1990a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh) 1991a08a8471SStephen M. Cameron { 1992a08a8471SStephen M. Cameron struct ctlr_info *h = shost_to_hba(sh); 1993a08a8471SStephen M. Cameron unsigned long flags; 1994a08a8471SStephen M. Cameron 1995a08a8471SStephen M. Cameron /* wait until any scan already in progress is finished. */ 1996a08a8471SStephen M. Cameron while (1) { 1997a08a8471SStephen M. Cameron spin_lock_irqsave(&h->scan_lock, flags); 1998a08a8471SStephen M. Cameron if (h->scan_finished) 1999a08a8471SStephen M. Cameron break; 2000a08a8471SStephen M. Cameron spin_unlock_irqrestore(&h->scan_lock, flags); 2001a08a8471SStephen M. Cameron wait_event(h->scan_wait_queue, h->scan_finished); 2002a08a8471SStephen M. Cameron /* Note: We don't need to worry about a race between this 2003a08a8471SStephen M. Cameron * thread and driver unload because the midlayer will 2004a08a8471SStephen M. Cameron * have incremented the reference count, so unload won't 2005a08a8471SStephen M. Cameron * happen if we're in here. 2006a08a8471SStephen M. Cameron */ 2007a08a8471SStephen M. Cameron } 2008a08a8471SStephen M. Cameron h->scan_finished = 0; /* mark scan as in progress */ 2009a08a8471SStephen M. Cameron spin_unlock_irqrestore(&h->scan_lock, flags); 2010a08a8471SStephen M. Cameron 2011a08a8471SStephen M. Cameron hpsa_update_scsi_devices(h, h->scsi_host->host_no); 2012a08a8471SStephen M. Cameron 2013a08a8471SStephen M. Cameron spin_lock_irqsave(&h->scan_lock, flags); 2014a08a8471SStephen M. Cameron h->scan_finished = 1; /* mark scan as finished. */ 2015a08a8471SStephen M. Cameron wake_up_all(&h->scan_wait_queue); 2016a08a8471SStephen M. Cameron spin_unlock_irqrestore(&h->scan_lock, flags); 2017a08a8471SStephen M. Cameron } 2018a08a8471SStephen M. Cameron 2019a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh, 2020a08a8471SStephen M. Cameron unsigned long elapsed_time) 2021a08a8471SStephen M. Cameron { 2022a08a8471SStephen M. Cameron struct ctlr_info *h = shost_to_hba(sh); 2023a08a8471SStephen M. Cameron unsigned long flags; 2024a08a8471SStephen M. Cameron int finished; 2025a08a8471SStephen M. Cameron 2026a08a8471SStephen M. Cameron spin_lock_irqsave(&h->scan_lock, flags); 2027a08a8471SStephen M. Cameron finished = h->scan_finished; 2028a08a8471SStephen M. Cameron spin_unlock_irqrestore(&h->scan_lock, flags); 2029a08a8471SStephen M. Cameron return finished; 2030a08a8471SStephen M. Cameron } 2031a08a8471SStephen M. Cameron 2032667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev, 2033667e23d4SStephen M. Cameron int qdepth, int reason) 2034667e23d4SStephen M. Cameron { 2035667e23d4SStephen M. Cameron struct ctlr_info *h = sdev_to_hba(sdev); 2036667e23d4SStephen M. Cameron 2037667e23d4SStephen M. Cameron if (reason != SCSI_QDEPTH_DEFAULT) 2038667e23d4SStephen M. Cameron return -ENOTSUPP; 2039667e23d4SStephen M. Cameron 2040667e23d4SStephen M. Cameron if (qdepth < 1) 2041667e23d4SStephen M. Cameron qdepth = 1; 2042667e23d4SStephen M. Cameron else 2043667e23d4SStephen M. Cameron if (qdepth > h->nr_cmds) 2044667e23d4SStephen M. Cameron qdepth = h->nr_cmds; 2045667e23d4SStephen M. Cameron scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth); 2046667e23d4SStephen M. Cameron return sdev->queue_depth; 2047667e23d4SStephen M. Cameron } 2048667e23d4SStephen M. Cameron 2049edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h) 2050edd16368SStephen M. Cameron { 2051edd16368SStephen M. Cameron /* we are being forcibly unloaded, and may not refuse. */ 2052edd16368SStephen M. Cameron scsi_remove_host(h->scsi_host); 2053edd16368SStephen M. Cameron scsi_host_put(h->scsi_host); 2054edd16368SStephen M. Cameron h->scsi_host = NULL; 2055edd16368SStephen M. Cameron } 2056edd16368SStephen M. Cameron 2057edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h) 2058edd16368SStephen M. Cameron { 2059edd16368SStephen M. Cameron int rc; 2060edd16368SStephen M. Cameron 2061edd16368SStephen M. Cameron rc = hpsa_scsi_detect(h); 2062edd16368SStephen M. Cameron if (rc != 0) 2063edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "hpsa_register_scsi: failed" 2064edd16368SStephen M. Cameron " hpsa_scsi_detect(), rc is %d\n", rc); 2065edd16368SStephen M. Cameron return rc; 2066edd16368SStephen M. Cameron } 2067edd16368SStephen M. Cameron 2068edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h, 2069edd16368SStephen M. Cameron unsigned char lunaddr[]) 2070edd16368SStephen M. Cameron { 2071edd16368SStephen M. Cameron int rc = 0; 2072edd16368SStephen M. Cameron int count = 0; 2073edd16368SStephen M. Cameron int waittime = 1; /* seconds */ 2074edd16368SStephen M. Cameron struct CommandList *c; 2075edd16368SStephen M. Cameron 2076edd16368SStephen M. Cameron c = cmd_special_alloc(h); 2077edd16368SStephen M. Cameron if (!c) { 2078edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "out of memory in " 2079edd16368SStephen M. Cameron "wait_for_device_to_become_ready.\n"); 2080edd16368SStephen M. Cameron return IO_ERROR; 2081edd16368SStephen M. Cameron } 2082edd16368SStephen M. Cameron 2083edd16368SStephen M. Cameron /* Send test unit ready until device ready, or give up. */ 2084edd16368SStephen M. Cameron while (count < HPSA_TUR_RETRY_LIMIT) { 2085edd16368SStephen M. Cameron 2086edd16368SStephen M. Cameron /* Wait for a bit. do this first, because if we send 2087edd16368SStephen M. Cameron * the TUR right away, the reset will just abort it. 2088edd16368SStephen M. Cameron */ 2089edd16368SStephen M. Cameron msleep(1000 * waittime); 2090edd16368SStephen M. Cameron count++; 2091edd16368SStephen M. Cameron 2092edd16368SStephen M. Cameron /* Increase wait time with each try, up to a point. */ 2093edd16368SStephen M. Cameron if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS) 2094edd16368SStephen M. Cameron waittime = waittime * 2; 2095edd16368SStephen M. Cameron 2096edd16368SStephen M. Cameron /* Send the Test Unit Ready */ 2097edd16368SStephen M. Cameron fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD); 2098edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 2099edd16368SStephen M. Cameron /* no unmap needed here because no data xfer. */ 2100edd16368SStephen M. Cameron 2101edd16368SStephen M. Cameron if (c->err_info->CommandStatus == CMD_SUCCESS) 2102edd16368SStephen M. Cameron break; 2103edd16368SStephen M. Cameron 2104edd16368SStephen M. Cameron if (c->err_info->CommandStatus == CMD_TARGET_STATUS && 2105edd16368SStephen M. Cameron c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION && 2106edd16368SStephen M. Cameron (c->err_info->SenseInfo[2] == NO_SENSE || 2107edd16368SStephen M. Cameron c->err_info->SenseInfo[2] == UNIT_ATTENTION)) 2108edd16368SStephen M. Cameron break; 2109edd16368SStephen M. Cameron 2110edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "waiting %d secs " 2111edd16368SStephen M. Cameron "for device to become ready.\n", waittime); 2112edd16368SStephen M. Cameron rc = 1; /* device not ready. */ 2113edd16368SStephen M. Cameron } 2114edd16368SStephen M. Cameron 2115edd16368SStephen M. Cameron if (rc) 2116edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "giving up on device.\n"); 2117edd16368SStephen M. Cameron else 2118edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "device is ready.\n"); 2119edd16368SStephen M. Cameron 2120edd16368SStephen M. Cameron cmd_special_free(h, c); 2121edd16368SStephen M. Cameron return rc; 2122edd16368SStephen M. Cameron } 2123edd16368SStephen M. Cameron 2124edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from 2125edd16368SStephen M. Cameron * complaining. Doing a host- or bus-reset can't do anything good here. 2126edd16368SStephen M. Cameron */ 2127edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd) 2128edd16368SStephen M. Cameron { 2129edd16368SStephen M. Cameron int rc; 2130edd16368SStephen M. Cameron struct ctlr_info *h; 2131edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *dev; 2132edd16368SStephen M. Cameron 2133edd16368SStephen M. Cameron /* find the controller to which the command to be aborted was sent */ 2134edd16368SStephen M. Cameron h = sdev_to_hba(scsicmd->device); 2135edd16368SStephen M. Cameron if (h == NULL) /* paranoia */ 2136edd16368SStephen M. Cameron return FAILED; 2137edd16368SStephen M. Cameron dev = scsicmd->device->hostdata; 2138edd16368SStephen M. Cameron if (!dev) { 2139edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: " 2140edd16368SStephen M. Cameron "device lookup failed.\n"); 2141edd16368SStephen M. Cameron return FAILED; 2142edd16368SStephen M. Cameron } 2143d416b0c7SStephen M. Cameron dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n", 2144d416b0c7SStephen M. Cameron h->scsi_host->host_no, dev->bus, dev->target, dev->lun); 2145edd16368SStephen M. Cameron /* send a reset to the SCSI LUN which the command was sent to */ 2146edd16368SStephen M. Cameron rc = hpsa_send_reset(h, dev->scsi3addr); 2147edd16368SStephen M. Cameron if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0) 2148edd16368SStephen M. Cameron return SUCCESS; 2149edd16368SStephen M. Cameron 2150edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "resetting device failed.\n"); 2151edd16368SStephen M. Cameron return FAILED; 2152edd16368SStephen M. Cameron } 2153edd16368SStephen M. Cameron 2154edd16368SStephen M. Cameron /* 2155edd16368SStephen M. Cameron * For operations that cannot sleep, a command block is allocated at init, 2156edd16368SStephen M. Cameron * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track 2157edd16368SStephen M. Cameron * which ones are free or in use. Lock must be held when calling this. 2158edd16368SStephen M. Cameron * cmd_free() is the complement. 2159edd16368SStephen M. Cameron */ 2160edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h) 2161edd16368SStephen M. Cameron { 2162edd16368SStephen M. Cameron struct CommandList *c; 2163edd16368SStephen M. Cameron int i; 2164edd16368SStephen M. Cameron union u64bit temp64; 2165edd16368SStephen M. Cameron dma_addr_t cmd_dma_handle, err_dma_handle; 2166edd16368SStephen M. Cameron 2167edd16368SStephen M. Cameron do { 2168edd16368SStephen M. Cameron i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds); 2169edd16368SStephen M. Cameron if (i == h->nr_cmds) 2170edd16368SStephen M. Cameron return NULL; 2171edd16368SStephen M. Cameron } while (test_and_set_bit 2172edd16368SStephen M. Cameron (i & (BITS_PER_LONG - 1), 2173edd16368SStephen M. Cameron h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0); 2174edd16368SStephen M. Cameron c = h->cmd_pool + i; 2175edd16368SStephen M. Cameron memset(c, 0, sizeof(*c)); 2176edd16368SStephen M. Cameron cmd_dma_handle = h->cmd_pool_dhandle 2177edd16368SStephen M. Cameron + i * sizeof(*c); 2178edd16368SStephen M. Cameron c->err_info = h->errinfo_pool + i; 2179edd16368SStephen M. Cameron memset(c->err_info, 0, sizeof(*c->err_info)); 2180edd16368SStephen M. Cameron err_dma_handle = h->errinfo_pool_dhandle 2181edd16368SStephen M. Cameron + i * sizeof(*c->err_info); 2182edd16368SStephen M. Cameron h->nr_allocs++; 2183edd16368SStephen M. Cameron 2184edd16368SStephen M. Cameron c->cmdindex = i; 2185edd16368SStephen M. Cameron 2186edd16368SStephen M. Cameron INIT_HLIST_NODE(&c->list); 218701a02ffcSStephen M. Cameron c->busaddr = (u32) cmd_dma_handle; 218801a02ffcSStephen M. Cameron temp64.val = (u64) err_dma_handle; 2189edd16368SStephen M. Cameron c->ErrDesc.Addr.lower = temp64.val32.lower; 2190edd16368SStephen M. Cameron c->ErrDesc.Addr.upper = temp64.val32.upper; 2191edd16368SStephen M. Cameron c->ErrDesc.Len = sizeof(*c->err_info); 2192edd16368SStephen M. Cameron 2193edd16368SStephen M. Cameron c->h = h; 2194edd16368SStephen M. Cameron return c; 2195edd16368SStephen M. Cameron } 2196edd16368SStephen M. Cameron 2197edd16368SStephen M. Cameron /* For operations that can wait for kmalloc to possibly sleep, 2198edd16368SStephen M. Cameron * this routine can be called. Lock need not be held to call 2199edd16368SStephen M. Cameron * cmd_special_alloc. cmd_special_free() is the complement. 2200edd16368SStephen M. Cameron */ 2201edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h) 2202edd16368SStephen M. Cameron { 2203edd16368SStephen M. Cameron struct CommandList *c; 2204edd16368SStephen M. Cameron union u64bit temp64; 2205edd16368SStephen M. Cameron dma_addr_t cmd_dma_handle, err_dma_handle; 2206edd16368SStephen M. Cameron 2207edd16368SStephen M. Cameron c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle); 2208edd16368SStephen M. Cameron if (c == NULL) 2209edd16368SStephen M. Cameron return NULL; 2210edd16368SStephen M. Cameron memset(c, 0, sizeof(*c)); 2211edd16368SStephen M. Cameron 2212edd16368SStephen M. Cameron c->cmdindex = -1; 2213edd16368SStephen M. Cameron 2214edd16368SStephen M. Cameron c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info), 2215edd16368SStephen M. Cameron &err_dma_handle); 2216edd16368SStephen M. Cameron 2217edd16368SStephen M. Cameron if (c->err_info == NULL) { 2218edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 2219edd16368SStephen M. Cameron sizeof(*c), c, cmd_dma_handle); 2220edd16368SStephen M. Cameron return NULL; 2221edd16368SStephen M. Cameron } 2222edd16368SStephen M. Cameron memset(c->err_info, 0, sizeof(*c->err_info)); 2223edd16368SStephen M. Cameron 2224edd16368SStephen M. Cameron INIT_HLIST_NODE(&c->list); 222501a02ffcSStephen M. Cameron c->busaddr = (u32) cmd_dma_handle; 222601a02ffcSStephen M. Cameron temp64.val = (u64) err_dma_handle; 2227edd16368SStephen M. Cameron c->ErrDesc.Addr.lower = temp64.val32.lower; 2228edd16368SStephen M. Cameron c->ErrDesc.Addr.upper = temp64.val32.upper; 2229edd16368SStephen M. Cameron c->ErrDesc.Len = sizeof(*c->err_info); 2230edd16368SStephen M. Cameron 2231edd16368SStephen M. Cameron c->h = h; 2232edd16368SStephen M. Cameron return c; 2233edd16368SStephen M. Cameron } 2234edd16368SStephen M. Cameron 2235edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c) 2236edd16368SStephen M. Cameron { 2237edd16368SStephen M. Cameron int i; 2238edd16368SStephen M. Cameron 2239edd16368SStephen M. Cameron i = c - h->cmd_pool; 2240edd16368SStephen M. Cameron clear_bit(i & (BITS_PER_LONG - 1), 2241edd16368SStephen M. Cameron h->cmd_pool_bits + (i / BITS_PER_LONG)); 2242edd16368SStephen M. Cameron h->nr_frees++; 2243edd16368SStephen M. Cameron } 2244edd16368SStephen M. Cameron 2245edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c) 2246edd16368SStephen M. Cameron { 2247edd16368SStephen M. Cameron union u64bit temp64; 2248edd16368SStephen M. Cameron 2249edd16368SStephen M. Cameron temp64.val32.lower = c->ErrDesc.Addr.lower; 2250edd16368SStephen M. Cameron temp64.val32.upper = c->ErrDesc.Addr.upper; 2251edd16368SStephen M. Cameron pci_free_consistent(h->pdev, sizeof(*c->err_info), 2252edd16368SStephen M. Cameron c->err_info, (dma_addr_t) temp64.val); 2253edd16368SStephen M. Cameron pci_free_consistent(h->pdev, sizeof(*c), 2254edd16368SStephen M. Cameron c, (dma_addr_t) c->busaddr); 2255edd16368SStephen M. Cameron } 2256edd16368SStephen M. Cameron 2257edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT 2258edd16368SStephen M. Cameron 2259edd16368SStephen M. Cameron static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg) 2260edd16368SStephen M. Cameron { 2261edd16368SStephen M. Cameron IOCTL32_Command_struct __user *arg32 = 2262edd16368SStephen M. Cameron (IOCTL32_Command_struct __user *) arg; 2263edd16368SStephen M. Cameron IOCTL_Command_struct arg64; 2264edd16368SStephen M. Cameron IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64)); 2265edd16368SStephen M. Cameron int err; 2266edd16368SStephen M. Cameron u32 cp; 2267edd16368SStephen M. Cameron 2268edd16368SStephen M. Cameron err = 0; 2269edd16368SStephen M. Cameron err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, 2270edd16368SStephen M. Cameron sizeof(arg64.LUN_info)); 2271edd16368SStephen M. Cameron err |= copy_from_user(&arg64.Request, &arg32->Request, 2272edd16368SStephen M. Cameron sizeof(arg64.Request)); 2273edd16368SStephen M. Cameron err |= copy_from_user(&arg64.error_info, &arg32->error_info, 2274edd16368SStephen M. Cameron sizeof(arg64.error_info)); 2275edd16368SStephen M. Cameron err |= get_user(arg64.buf_size, &arg32->buf_size); 2276edd16368SStephen M. Cameron err |= get_user(cp, &arg32->buf); 2277edd16368SStephen M. Cameron arg64.buf = compat_ptr(cp); 2278edd16368SStephen M. Cameron err |= copy_to_user(p, &arg64, sizeof(arg64)); 2279edd16368SStephen M. Cameron 2280edd16368SStephen M. Cameron if (err) 2281edd16368SStephen M. Cameron return -EFAULT; 2282edd16368SStephen M. Cameron 2283e39eeaedSStephen M. Cameron err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p); 2284edd16368SStephen M. Cameron if (err) 2285edd16368SStephen M. Cameron return err; 2286edd16368SStephen M. Cameron err |= copy_in_user(&arg32->error_info, &p->error_info, 2287edd16368SStephen M. Cameron sizeof(arg32->error_info)); 2288edd16368SStephen M. Cameron if (err) 2289edd16368SStephen M. Cameron return -EFAULT; 2290edd16368SStephen M. Cameron return err; 2291edd16368SStephen M. Cameron } 2292edd16368SStephen M. Cameron 2293edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev, 2294edd16368SStephen M. Cameron int cmd, void *arg) 2295edd16368SStephen M. Cameron { 2296edd16368SStephen M. Cameron BIG_IOCTL32_Command_struct __user *arg32 = 2297edd16368SStephen M. Cameron (BIG_IOCTL32_Command_struct __user *) arg; 2298edd16368SStephen M. Cameron BIG_IOCTL_Command_struct arg64; 2299edd16368SStephen M. Cameron BIG_IOCTL_Command_struct __user *p = 2300edd16368SStephen M. Cameron compat_alloc_user_space(sizeof(arg64)); 2301edd16368SStephen M. Cameron int err; 2302edd16368SStephen M. Cameron u32 cp; 2303edd16368SStephen M. Cameron 2304edd16368SStephen M. Cameron err = 0; 2305edd16368SStephen M. Cameron err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, 2306edd16368SStephen M. Cameron sizeof(arg64.LUN_info)); 2307edd16368SStephen M. Cameron err |= copy_from_user(&arg64.Request, &arg32->Request, 2308edd16368SStephen M. Cameron sizeof(arg64.Request)); 2309edd16368SStephen M. Cameron err |= copy_from_user(&arg64.error_info, &arg32->error_info, 2310edd16368SStephen M. Cameron sizeof(arg64.error_info)); 2311edd16368SStephen M. Cameron err |= get_user(arg64.buf_size, &arg32->buf_size); 2312edd16368SStephen M. Cameron err |= get_user(arg64.malloc_size, &arg32->malloc_size); 2313edd16368SStephen M. Cameron err |= get_user(cp, &arg32->buf); 2314edd16368SStephen M. Cameron arg64.buf = compat_ptr(cp); 2315edd16368SStephen M. Cameron err |= copy_to_user(p, &arg64, sizeof(arg64)); 2316edd16368SStephen M. Cameron 2317edd16368SStephen M. Cameron if (err) 2318edd16368SStephen M. Cameron return -EFAULT; 2319edd16368SStephen M. Cameron 2320e39eeaedSStephen M. Cameron err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p); 2321edd16368SStephen M. Cameron if (err) 2322edd16368SStephen M. Cameron return err; 2323edd16368SStephen M. Cameron err |= copy_in_user(&arg32->error_info, &p->error_info, 2324edd16368SStephen M. Cameron sizeof(arg32->error_info)); 2325edd16368SStephen M. Cameron if (err) 2326edd16368SStephen M. Cameron return -EFAULT; 2327edd16368SStephen M. Cameron return err; 2328edd16368SStephen M. Cameron } 232971fe75a7SStephen M. Cameron 233071fe75a7SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg) 233171fe75a7SStephen M. Cameron { 233271fe75a7SStephen M. Cameron switch (cmd) { 233371fe75a7SStephen M. Cameron case CCISS_GETPCIINFO: 233471fe75a7SStephen M. Cameron case CCISS_GETINTINFO: 233571fe75a7SStephen M. Cameron case CCISS_SETINTINFO: 233671fe75a7SStephen M. Cameron case CCISS_GETNODENAME: 233771fe75a7SStephen M. Cameron case CCISS_SETNODENAME: 233871fe75a7SStephen M. Cameron case CCISS_GETHEARTBEAT: 233971fe75a7SStephen M. Cameron case CCISS_GETBUSTYPES: 234071fe75a7SStephen M. Cameron case CCISS_GETFIRMVER: 234171fe75a7SStephen M. Cameron case CCISS_GETDRIVVER: 234271fe75a7SStephen M. Cameron case CCISS_REVALIDVOLS: 234371fe75a7SStephen M. Cameron case CCISS_DEREGDISK: 234471fe75a7SStephen M. Cameron case CCISS_REGNEWDISK: 234571fe75a7SStephen M. Cameron case CCISS_REGNEWD: 234671fe75a7SStephen M. Cameron case CCISS_RESCANDISK: 234771fe75a7SStephen M. Cameron case CCISS_GETLUNINFO: 234871fe75a7SStephen M. Cameron return hpsa_ioctl(dev, cmd, arg); 234971fe75a7SStephen M. Cameron 235071fe75a7SStephen M. Cameron case CCISS_PASSTHRU32: 235171fe75a7SStephen M. Cameron return hpsa_ioctl32_passthru(dev, cmd, arg); 235271fe75a7SStephen M. Cameron case CCISS_BIG_PASSTHRU32: 235371fe75a7SStephen M. Cameron return hpsa_ioctl32_big_passthru(dev, cmd, arg); 235471fe75a7SStephen M. Cameron 235571fe75a7SStephen M. Cameron default: 235671fe75a7SStephen M. Cameron return -ENOIOCTLCMD; 235771fe75a7SStephen M. Cameron } 235871fe75a7SStephen M. Cameron } 2359edd16368SStephen M. Cameron #endif 2360edd16368SStephen M. Cameron 2361edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp) 2362edd16368SStephen M. Cameron { 2363edd16368SStephen M. Cameron struct hpsa_pci_info pciinfo; 2364edd16368SStephen M. Cameron 2365edd16368SStephen M. Cameron if (!argp) 2366edd16368SStephen M. Cameron return -EINVAL; 2367edd16368SStephen M. Cameron pciinfo.domain = pci_domain_nr(h->pdev->bus); 2368edd16368SStephen M. Cameron pciinfo.bus = h->pdev->bus->number; 2369edd16368SStephen M. Cameron pciinfo.dev_fn = h->pdev->devfn; 2370edd16368SStephen M. Cameron pciinfo.board_id = h->board_id; 2371edd16368SStephen M. Cameron if (copy_to_user(argp, &pciinfo, sizeof(pciinfo))) 2372edd16368SStephen M. Cameron return -EFAULT; 2373edd16368SStephen M. Cameron return 0; 2374edd16368SStephen M. Cameron } 2375edd16368SStephen M. Cameron 2376edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp) 2377edd16368SStephen M. Cameron { 2378edd16368SStephen M. Cameron DriverVer_type DriverVer; 2379edd16368SStephen M. Cameron unsigned char vmaj, vmin, vsubmin; 2380edd16368SStephen M. Cameron int rc; 2381edd16368SStephen M. Cameron 2382edd16368SStephen M. Cameron rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu", 2383edd16368SStephen M. Cameron &vmaj, &vmin, &vsubmin); 2384edd16368SStephen M. Cameron if (rc != 3) { 2385edd16368SStephen M. Cameron dev_info(&h->pdev->dev, "driver version string '%s' " 2386edd16368SStephen M. Cameron "unrecognized.", HPSA_DRIVER_VERSION); 2387edd16368SStephen M. Cameron vmaj = 0; 2388edd16368SStephen M. Cameron vmin = 0; 2389edd16368SStephen M. Cameron vsubmin = 0; 2390edd16368SStephen M. Cameron } 2391edd16368SStephen M. Cameron DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin; 2392edd16368SStephen M. Cameron if (!argp) 2393edd16368SStephen M. Cameron return -EINVAL; 2394edd16368SStephen M. Cameron if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type))) 2395edd16368SStephen M. Cameron return -EFAULT; 2396edd16368SStephen M. Cameron return 0; 2397edd16368SStephen M. Cameron } 2398edd16368SStephen M. Cameron 2399edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp) 2400edd16368SStephen M. Cameron { 2401edd16368SStephen M. Cameron IOCTL_Command_struct iocommand; 2402edd16368SStephen M. Cameron struct CommandList *c; 2403edd16368SStephen M. Cameron char *buff = NULL; 2404edd16368SStephen M. Cameron union u64bit temp64; 2405edd16368SStephen M. Cameron 2406edd16368SStephen M. Cameron if (!argp) 2407edd16368SStephen M. Cameron return -EINVAL; 2408edd16368SStephen M. Cameron if (!capable(CAP_SYS_RAWIO)) 2409edd16368SStephen M. Cameron return -EPERM; 2410edd16368SStephen M. Cameron if (copy_from_user(&iocommand, argp, sizeof(iocommand))) 2411edd16368SStephen M. Cameron return -EFAULT; 2412edd16368SStephen M. Cameron if ((iocommand.buf_size < 1) && 2413edd16368SStephen M. Cameron (iocommand.Request.Type.Direction != XFER_NONE)) { 2414edd16368SStephen M. Cameron return -EINVAL; 2415edd16368SStephen M. Cameron } 2416edd16368SStephen M. Cameron if (iocommand.buf_size > 0) { 2417edd16368SStephen M. Cameron buff = kmalloc(iocommand.buf_size, GFP_KERNEL); 2418edd16368SStephen M. Cameron if (buff == NULL) 2419edd16368SStephen M. Cameron return -EFAULT; 2420edd16368SStephen M. Cameron } 2421edd16368SStephen M. Cameron if (iocommand.Request.Type.Direction == XFER_WRITE) { 2422edd16368SStephen M. Cameron /* Copy the data into the buffer we created */ 2423edd16368SStephen M. Cameron if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) { 2424edd16368SStephen M. Cameron kfree(buff); 2425edd16368SStephen M. Cameron return -EFAULT; 2426edd16368SStephen M. Cameron } 2427edd16368SStephen M. Cameron } else 2428edd16368SStephen M. Cameron memset(buff, 0, iocommand.buf_size); 2429edd16368SStephen M. Cameron c = cmd_special_alloc(h); 2430edd16368SStephen M. Cameron if (c == NULL) { 2431edd16368SStephen M. Cameron kfree(buff); 2432edd16368SStephen M. Cameron return -ENOMEM; 2433edd16368SStephen M. Cameron } 2434edd16368SStephen M. Cameron /* Fill in the command type */ 2435edd16368SStephen M. Cameron c->cmd_type = CMD_IOCTL_PEND; 2436edd16368SStephen M. Cameron /* Fill in Command Header */ 2437edd16368SStephen M. Cameron c->Header.ReplyQueue = 0; /* unused in simple mode */ 2438edd16368SStephen M. Cameron if (iocommand.buf_size > 0) { /* buffer to fill */ 2439edd16368SStephen M. Cameron c->Header.SGList = 1; 2440edd16368SStephen M. Cameron c->Header.SGTotal = 1; 2441edd16368SStephen M. Cameron } else { /* no buffers to fill */ 2442edd16368SStephen M. Cameron c->Header.SGList = 0; 2443edd16368SStephen M. Cameron c->Header.SGTotal = 0; 2444edd16368SStephen M. Cameron } 2445edd16368SStephen M. Cameron memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN)); 2446edd16368SStephen M. Cameron /* use the kernel address the cmd block for tag */ 2447edd16368SStephen M. Cameron c->Header.Tag.lower = c->busaddr; 2448edd16368SStephen M. Cameron 2449edd16368SStephen M. Cameron /* Fill in Request block */ 2450edd16368SStephen M. Cameron memcpy(&c->Request, &iocommand.Request, 2451edd16368SStephen M. Cameron sizeof(c->Request)); 2452edd16368SStephen M. Cameron 2453edd16368SStephen M. Cameron /* Fill in the scatter gather information */ 2454edd16368SStephen M. Cameron if (iocommand.buf_size > 0) { 2455edd16368SStephen M. Cameron temp64.val = pci_map_single(h->pdev, buff, 2456edd16368SStephen M. Cameron iocommand.buf_size, PCI_DMA_BIDIRECTIONAL); 2457edd16368SStephen M. Cameron c->SG[0].Addr.lower = temp64.val32.lower; 2458edd16368SStephen M. Cameron c->SG[0].Addr.upper = temp64.val32.upper; 2459edd16368SStephen M. Cameron c->SG[0].Len = iocommand.buf_size; 2460edd16368SStephen M. Cameron c->SG[0].Ext = 0; /* we are not chaining*/ 2461edd16368SStephen M. Cameron } 2462edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 2463edd16368SStephen M. Cameron hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL); 2464edd16368SStephen M. Cameron check_ioctl_unit_attention(h, c); 2465edd16368SStephen M. Cameron 2466edd16368SStephen M. Cameron /* Copy the error information out */ 2467edd16368SStephen M. Cameron memcpy(&iocommand.error_info, c->err_info, 2468edd16368SStephen M. Cameron sizeof(iocommand.error_info)); 2469edd16368SStephen M. Cameron if (copy_to_user(argp, &iocommand, sizeof(iocommand))) { 2470edd16368SStephen M. Cameron kfree(buff); 2471edd16368SStephen M. Cameron cmd_special_free(h, c); 2472edd16368SStephen M. Cameron return -EFAULT; 2473edd16368SStephen M. Cameron } 2474edd16368SStephen M. Cameron 2475edd16368SStephen M. Cameron if (iocommand.Request.Type.Direction == XFER_READ) { 2476edd16368SStephen M. Cameron /* Copy the data out of the buffer we created */ 2477edd16368SStephen M. Cameron if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) { 2478edd16368SStephen M. Cameron kfree(buff); 2479edd16368SStephen M. Cameron cmd_special_free(h, c); 2480edd16368SStephen M. Cameron return -EFAULT; 2481edd16368SStephen M. Cameron } 2482edd16368SStephen M. Cameron } 2483edd16368SStephen M. Cameron kfree(buff); 2484edd16368SStephen M. Cameron cmd_special_free(h, c); 2485edd16368SStephen M. Cameron return 0; 2486edd16368SStephen M. Cameron } 2487edd16368SStephen M. Cameron 2488edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp) 2489edd16368SStephen M. Cameron { 2490edd16368SStephen M. Cameron BIG_IOCTL_Command_struct *ioc; 2491edd16368SStephen M. Cameron struct CommandList *c; 2492edd16368SStephen M. Cameron unsigned char **buff = NULL; 2493edd16368SStephen M. Cameron int *buff_size = NULL; 2494edd16368SStephen M. Cameron union u64bit temp64; 2495edd16368SStephen M. Cameron BYTE sg_used = 0; 2496edd16368SStephen M. Cameron int status = 0; 2497edd16368SStephen M. Cameron int i; 249801a02ffcSStephen M. Cameron u32 left; 249901a02ffcSStephen M. Cameron u32 sz; 2500edd16368SStephen M. Cameron BYTE __user *data_ptr; 2501edd16368SStephen M. Cameron 2502edd16368SStephen M. Cameron if (!argp) 2503edd16368SStephen M. Cameron return -EINVAL; 2504edd16368SStephen M. Cameron if (!capable(CAP_SYS_RAWIO)) 2505edd16368SStephen M. Cameron return -EPERM; 2506edd16368SStephen M. Cameron ioc = (BIG_IOCTL_Command_struct *) 2507edd16368SStephen M. Cameron kmalloc(sizeof(*ioc), GFP_KERNEL); 2508edd16368SStephen M. Cameron if (!ioc) { 2509edd16368SStephen M. Cameron status = -ENOMEM; 2510edd16368SStephen M. Cameron goto cleanup1; 2511edd16368SStephen M. Cameron } 2512edd16368SStephen M. Cameron if (copy_from_user(ioc, argp, sizeof(*ioc))) { 2513edd16368SStephen M. Cameron status = -EFAULT; 2514edd16368SStephen M. Cameron goto cleanup1; 2515edd16368SStephen M. Cameron } 2516edd16368SStephen M. Cameron if ((ioc->buf_size < 1) && 2517edd16368SStephen M. Cameron (ioc->Request.Type.Direction != XFER_NONE)) { 2518edd16368SStephen M. Cameron status = -EINVAL; 2519edd16368SStephen M. Cameron goto cleanup1; 2520edd16368SStephen M. Cameron } 2521edd16368SStephen M. Cameron /* Check kmalloc limits using all SGs */ 2522edd16368SStephen M. Cameron if (ioc->malloc_size > MAX_KMALLOC_SIZE) { 2523edd16368SStephen M. Cameron status = -EINVAL; 2524edd16368SStephen M. Cameron goto cleanup1; 2525edd16368SStephen M. Cameron } 2526edd16368SStephen M. Cameron if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) { 2527edd16368SStephen M. Cameron status = -EINVAL; 2528edd16368SStephen M. Cameron goto cleanup1; 2529edd16368SStephen M. Cameron } 2530edd16368SStephen M. Cameron buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL); 2531edd16368SStephen M. Cameron if (!buff) { 2532edd16368SStephen M. Cameron status = -ENOMEM; 2533edd16368SStephen M. Cameron goto cleanup1; 2534edd16368SStephen M. Cameron } 2535edd16368SStephen M. Cameron buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL); 2536edd16368SStephen M. Cameron if (!buff_size) { 2537edd16368SStephen M. Cameron status = -ENOMEM; 2538edd16368SStephen M. Cameron goto cleanup1; 2539edd16368SStephen M. Cameron } 2540edd16368SStephen M. Cameron left = ioc->buf_size; 2541edd16368SStephen M. Cameron data_ptr = ioc->buf; 2542edd16368SStephen M. Cameron while (left) { 2543edd16368SStephen M. Cameron sz = (left > ioc->malloc_size) ? ioc->malloc_size : left; 2544edd16368SStephen M. Cameron buff_size[sg_used] = sz; 2545edd16368SStephen M. Cameron buff[sg_used] = kmalloc(sz, GFP_KERNEL); 2546edd16368SStephen M. Cameron if (buff[sg_used] == NULL) { 2547edd16368SStephen M. Cameron status = -ENOMEM; 2548edd16368SStephen M. Cameron goto cleanup1; 2549edd16368SStephen M. Cameron } 2550edd16368SStephen M. Cameron if (ioc->Request.Type.Direction == XFER_WRITE) { 2551edd16368SStephen M. Cameron if (copy_from_user(buff[sg_used], data_ptr, sz)) { 2552edd16368SStephen M. Cameron status = -ENOMEM; 2553edd16368SStephen M. Cameron goto cleanup1; 2554edd16368SStephen M. Cameron } 2555edd16368SStephen M. Cameron } else 2556edd16368SStephen M. Cameron memset(buff[sg_used], 0, sz); 2557edd16368SStephen M. Cameron left -= sz; 2558edd16368SStephen M. Cameron data_ptr += sz; 2559edd16368SStephen M. Cameron sg_used++; 2560edd16368SStephen M. Cameron } 2561edd16368SStephen M. Cameron c = cmd_special_alloc(h); 2562edd16368SStephen M. Cameron if (c == NULL) { 2563edd16368SStephen M. Cameron status = -ENOMEM; 2564edd16368SStephen M. Cameron goto cleanup1; 2565edd16368SStephen M. Cameron } 2566edd16368SStephen M. Cameron c->cmd_type = CMD_IOCTL_PEND; 2567edd16368SStephen M. Cameron c->Header.ReplyQueue = 0; 2568edd16368SStephen M. Cameron 2569edd16368SStephen M. Cameron if (ioc->buf_size > 0) { 2570edd16368SStephen M. Cameron c->Header.SGList = sg_used; 2571edd16368SStephen M. Cameron c->Header.SGTotal = sg_used; 2572edd16368SStephen M. Cameron } else { 2573edd16368SStephen M. Cameron c->Header.SGList = 0; 2574edd16368SStephen M. Cameron c->Header.SGTotal = 0; 2575edd16368SStephen M. Cameron } 2576edd16368SStephen M. Cameron memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN)); 2577edd16368SStephen M. Cameron c->Header.Tag.lower = c->busaddr; 2578edd16368SStephen M. Cameron memcpy(&c->Request, &ioc->Request, sizeof(c->Request)); 2579edd16368SStephen M. Cameron if (ioc->buf_size > 0) { 2580edd16368SStephen M. Cameron int i; 2581edd16368SStephen M. Cameron for (i = 0; i < sg_used; i++) { 2582edd16368SStephen M. Cameron temp64.val = pci_map_single(h->pdev, buff[i], 2583edd16368SStephen M. Cameron buff_size[i], PCI_DMA_BIDIRECTIONAL); 2584edd16368SStephen M. Cameron c->SG[i].Addr.lower = temp64.val32.lower; 2585edd16368SStephen M. Cameron c->SG[i].Addr.upper = temp64.val32.upper; 2586edd16368SStephen M. Cameron c->SG[i].Len = buff_size[i]; 2587edd16368SStephen M. Cameron /* we are not chaining */ 2588edd16368SStephen M. Cameron c->SG[i].Ext = 0; 2589edd16368SStephen M. Cameron } 2590edd16368SStephen M. Cameron } 2591edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 2592edd16368SStephen M. Cameron hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL); 2593edd16368SStephen M. Cameron check_ioctl_unit_attention(h, c); 2594edd16368SStephen M. Cameron /* Copy the error information out */ 2595edd16368SStephen M. Cameron memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info)); 2596edd16368SStephen M. Cameron if (copy_to_user(argp, ioc, sizeof(*ioc))) { 2597edd16368SStephen M. Cameron cmd_special_free(h, c); 2598edd16368SStephen M. Cameron status = -EFAULT; 2599edd16368SStephen M. Cameron goto cleanup1; 2600edd16368SStephen M. Cameron } 2601edd16368SStephen M. Cameron if (ioc->Request.Type.Direction == XFER_READ) { 2602edd16368SStephen M. Cameron /* Copy the data out of the buffer we created */ 2603edd16368SStephen M. Cameron BYTE __user *ptr = ioc->buf; 2604edd16368SStephen M. Cameron for (i = 0; i < sg_used; i++) { 2605edd16368SStephen M. Cameron if (copy_to_user(ptr, buff[i], buff_size[i])) { 2606edd16368SStephen M. Cameron cmd_special_free(h, c); 2607edd16368SStephen M. Cameron status = -EFAULT; 2608edd16368SStephen M. Cameron goto cleanup1; 2609edd16368SStephen M. Cameron } 2610edd16368SStephen M. Cameron ptr += buff_size[i]; 2611edd16368SStephen M. Cameron } 2612edd16368SStephen M. Cameron } 2613edd16368SStephen M. Cameron cmd_special_free(h, c); 2614edd16368SStephen M. Cameron status = 0; 2615edd16368SStephen M. Cameron cleanup1: 2616edd16368SStephen M. Cameron if (buff) { 2617edd16368SStephen M. Cameron for (i = 0; i < sg_used; i++) 2618edd16368SStephen M. Cameron kfree(buff[i]); 2619edd16368SStephen M. Cameron kfree(buff); 2620edd16368SStephen M. Cameron } 2621edd16368SStephen M. Cameron kfree(buff_size); 2622edd16368SStephen M. Cameron kfree(ioc); 2623edd16368SStephen M. Cameron return status; 2624edd16368SStephen M. Cameron } 2625edd16368SStephen M. Cameron 2626edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h, 2627edd16368SStephen M. Cameron struct CommandList *c) 2628edd16368SStephen M. Cameron { 2629edd16368SStephen M. Cameron if (c->err_info->CommandStatus == CMD_TARGET_STATUS && 2630edd16368SStephen M. Cameron c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) 2631edd16368SStephen M. Cameron (void) check_for_unit_attention(h, c); 2632edd16368SStephen M. Cameron } 2633edd16368SStephen M. Cameron /* 2634edd16368SStephen M. Cameron * ioctl 2635edd16368SStephen M. Cameron */ 2636edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg) 2637edd16368SStephen M. Cameron { 2638edd16368SStephen M. Cameron struct ctlr_info *h; 2639edd16368SStephen M. Cameron void __user *argp = (void __user *)arg; 2640edd16368SStephen M. Cameron 2641edd16368SStephen M. Cameron h = sdev_to_hba(dev); 2642edd16368SStephen M. Cameron 2643edd16368SStephen M. Cameron switch (cmd) { 2644edd16368SStephen M. Cameron case CCISS_DEREGDISK: 2645edd16368SStephen M. Cameron case CCISS_REGNEWDISK: 2646edd16368SStephen M. Cameron case CCISS_REGNEWD: 2647a08a8471SStephen M. Cameron hpsa_scan_start(h->scsi_host); 2648edd16368SStephen M. Cameron return 0; 2649edd16368SStephen M. Cameron case CCISS_GETPCIINFO: 2650edd16368SStephen M. Cameron return hpsa_getpciinfo_ioctl(h, argp); 2651edd16368SStephen M. Cameron case CCISS_GETDRIVVER: 2652edd16368SStephen M. Cameron return hpsa_getdrivver_ioctl(h, argp); 2653edd16368SStephen M. Cameron case CCISS_PASSTHRU: 2654edd16368SStephen M. Cameron return hpsa_passthru_ioctl(h, argp); 2655edd16368SStephen M. Cameron case CCISS_BIG_PASSTHRU: 2656edd16368SStephen M. Cameron return hpsa_big_passthru_ioctl(h, argp); 2657edd16368SStephen M. Cameron default: 2658edd16368SStephen M. Cameron return -ENOTTY; 2659edd16368SStephen M. Cameron } 2660edd16368SStephen M. Cameron } 2661edd16368SStephen M. Cameron 266201a02ffcSStephen M. Cameron static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h, 266301a02ffcSStephen M. Cameron void *buff, size_t size, u8 page_code, unsigned char *scsi3addr, 2664edd16368SStephen M. Cameron int cmd_type) 2665edd16368SStephen M. Cameron { 2666edd16368SStephen M. Cameron int pci_dir = XFER_NONE; 2667edd16368SStephen M. Cameron 2668edd16368SStephen M. Cameron c->cmd_type = CMD_IOCTL_PEND; 2669edd16368SStephen M. Cameron c->Header.ReplyQueue = 0; 2670edd16368SStephen M. Cameron if (buff != NULL && size > 0) { 2671edd16368SStephen M. Cameron c->Header.SGList = 1; 2672edd16368SStephen M. Cameron c->Header.SGTotal = 1; 2673edd16368SStephen M. Cameron } else { 2674edd16368SStephen M. Cameron c->Header.SGList = 0; 2675edd16368SStephen M. Cameron c->Header.SGTotal = 0; 2676edd16368SStephen M. Cameron } 2677edd16368SStephen M. Cameron c->Header.Tag.lower = c->busaddr; 2678edd16368SStephen M. Cameron memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8); 2679edd16368SStephen M. Cameron 2680edd16368SStephen M. Cameron c->Request.Type.Type = cmd_type; 2681edd16368SStephen M. Cameron if (cmd_type == TYPE_CMD) { 2682edd16368SStephen M. Cameron switch (cmd) { 2683edd16368SStephen M. Cameron case HPSA_INQUIRY: 2684edd16368SStephen M. Cameron /* are we trying to read a vital product page */ 2685edd16368SStephen M. Cameron if (page_code != 0) { 2686edd16368SStephen M. Cameron c->Request.CDB[1] = 0x01; 2687edd16368SStephen M. Cameron c->Request.CDB[2] = page_code; 2688edd16368SStephen M. Cameron } 2689edd16368SStephen M. Cameron c->Request.CDBLen = 6; 2690edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2691edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_READ; 2692edd16368SStephen M. Cameron c->Request.Timeout = 0; 2693edd16368SStephen M. Cameron c->Request.CDB[0] = HPSA_INQUIRY; 2694edd16368SStephen M. Cameron c->Request.CDB[4] = size & 0xFF; 2695edd16368SStephen M. Cameron break; 2696edd16368SStephen M. Cameron case HPSA_REPORT_LOG: 2697edd16368SStephen M. Cameron case HPSA_REPORT_PHYS: 2698edd16368SStephen M. Cameron /* Talking to controller so It's a physical command 2699edd16368SStephen M. Cameron mode = 00 target = 0. Nothing to write. 2700edd16368SStephen M. Cameron */ 2701edd16368SStephen M. Cameron c->Request.CDBLen = 12; 2702edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2703edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_READ; 2704edd16368SStephen M. Cameron c->Request.Timeout = 0; 2705edd16368SStephen M. Cameron c->Request.CDB[0] = cmd; 2706edd16368SStephen M. Cameron c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */ 2707edd16368SStephen M. Cameron c->Request.CDB[7] = (size >> 16) & 0xFF; 2708edd16368SStephen M. Cameron c->Request.CDB[8] = (size >> 8) & 0xFF; 2709edd16368SStephen M. Cameron c->Request.CDB[9] = size & 0xFF; 2710edd16368SStephen M. Cameron break; 2711edd16368SStephen M. Cameron case HPSA_CACHE_FLUSH: 2712edd16368SStephen M. Cameron c->Request.CDBLen = 12; 2713edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2714edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_WRITE; 2715edd16368SStephen M. Cameron c->Request.Timeout = 0; 2716edd16368SStephen M. Cameron c->Request.CDB[0] = BMIC_WRITE; 2717edd16368SStephen M. Cameron c->Request.CDB[6] = BMIC_CACHE_FLUSH; 2718edd16368SStephen M. Cameron break; 2719edd16368SStephen M. Cameron case TEST_UNIT_READY: 2720edd16368SStephen M. Cameron c->Request.CDBLen = 6; 2721edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2722edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_NONE; 2723edd16368SStephen M. Cameron c->Request.Timeout = 0; 2724edd16368SStephen M. Cameron break; 2725edd16368SStephen M. Cameron default: 2726edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd); 2727edd16368SStephen M. Cameron BUG(); 2728edd16368SStephen M. Cameron return; 2729edd16368SStephen M. Cameron } 2730edd16368SStephen M. Cameron } else if (cmd_type == TYPE_MSG) { 2731edd16368SStephen M. Cameron switch (cmd) { 2732edd16368SStephen M. Cameron 2733edd16368SStephen M. Cameron case HPSA_DEVICE_RESET_MSG: 2734edd16368SStephen M. Cameron c->Request.CDBLen = 16; 2735edd16368SStephen M. Cameron c->Request.Type.Type = 1; /* It is a MSG not a CMD */ 2736edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2737edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_NONE; 2738edd16368SStephen M. Cameron c->Request.Timeout = 0; /* Don't time out */ 2739edd16368SStephen M. Cameron c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */ 2740edd16368SStephen M. Cameron c->Request.CDB[1] = 0x03; /* Reset target above */ 2741edd16368SStephen M. Cameron /* If bytes 4-7 are zero, it means reset the */ 2742edd16368SStephen M. Cameron /* LunID device */ 2743edd16368SStephen M. Cameron c->Request.CDB[4] = 0x00; 2744edd16368SStephen M. Cameron c->Request.CDB[5] = 0x00; 2745edd16368SStephen M. Cameron c->Request.CDB[6] = 0x00; 2746edd16368SStephen M. Cameron c->Request.CDB[7] = 0x00; 2747edd16368SStephen M. Cameron break; 2748edd16368SStephen M. Cameron 2749edd16368SStephen M. Cameron default: 2750edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "unknown message type %d\n", 2751edd16368SStephen M. Cameron cmd); 2752edd16368SStephen M. Cameron BUG(); 2753edd16368SStephen M. Cameron } 2754edd16368SStephen M. Cameron } else { 2755edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type); 2756edd16368SStephen M. Cameron BUG(); 2757edd16368SStephen M. Cameron } 2758edd16368SStephen M. Cameron 2759edd16368SStephen M. Cameron switch (c->Request.Type.Direction) { 2760edd16368SStephen M. Cameron case XFER_READ: 2761edd16368SStephen M. Cameron pci_dir = PCI_DMA_FROMDEVICE; 2762edd16368SStephen M. Cameron break; 2763edd16368SStephen M. Cameron case XFER_WRITE: 2764edd16368SStephen M. Cameron pci_dir = PCI_DMA_TODEVICE; 2765edd16368SStephen M. Cameron break; 2766edd16368SStephen M. Cameron case XFER_NONE: 2767edd16368SStephen M. Cameron pci_dir = PCI_DMA_NONE; 2768edd16368SStephen M. Cameron break; 2769edd16368SStephen M. Cameron default: 2770edd16368SStephen M. Cameron pci_dir = PCI_DMA_BIDIRECTIONAL; 2771edd16368SStephen M. Cameron } 2772edd16368SStephen M. Cameron 2773edd16368SStephen M. Cameron hpsa_map_one(h->pdev, c, buff, size, pci_dir); 2774edd16368SStephen M. Cameron 2775edd16368SStephen M. Cameron return; 2776edd16368SStephen M. Cameron } 2777edd16368SStephen M. Cameron 2778edd16368SStephen M. Cameron /* 2779edd16368SStephen M. Cameron * Map (physical) PCI mem into (virtual) kernel space 2780edd16368SStephen M. Cameron */ 2781edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size) 2782edd16368SStephen M. Cameron { 2783edd16368SStephen M. Cameron ulong page_base = ((ulong) base) & PAGE_MASK; 2784edd16368SStephen M. Cameron ulong page_offs = ((ulong) base) - page_base; 2785edd16368SStephen M. Cameron void __iomem *page_remapped = ioremap(page_base, page_offs + size); 2786edd16368SStephen M. Cameron 2787edd16368SStephen M. Cameron return page_remapped ? (page_remapped + page_offs) : NULL; 2788edd16368SStephen M. Cameron } 2789edd16368SStephen M. Cameron 2790edd16368SStephen M. Cameron /* Takes cmds off the submission queue and sends them to the hardware, 2791edd16368SStephen M. Cameron * then puts them on the queue of cmds waiting for completion. 2792edd16368SStephen M. Cameron */ 2793edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h) 2794edd16368SStephen M. Cameron { 2795edd16368SStephen M. Cameron struct CommandList *c; 2796edd16368SStephen M. Cameron 2797edd16368SStephen M. Cameron while (!hlist_empty(&h->reqQ)) { 2798edd16368SStephen M. Cameron c = hlist_entry(h->reqQ.first, struct CommandList, list); 2799edd16368SStephen M. Cameron /* can't do anything if fifo is full */ 2800edd16368SStephen M. Cameron if ((h->access.fifo_full(h))) { 2801edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "fifo full\n"); 2802edd16368SStephen M. Cameron break; 2803edd16368SStephen M. Cameron } 2804edd16368SStephen M. Cameron 2805edd16368SStephen M. Cameron /* Get the first entry from the Request Q */ 2806edd16368SStephen M. Cameron removeQ(c); 2807edd16368SStephen M. Cameron h->Qdepth--; 2808edd16368SStephen M. Cameron 2809edd16368SStephen M. Cameron /* Tell the controller execute command */ 2810edd16368SStephen M. Cameron h->access.submit_command(h, c); 2811edd16368SStephen M. Cameron 2812edd16368SStephen M. Cameron /* Put job onto the completed Q */ 2813edd16368SStephen M. Cameron addQ(&h->cmpQ, c); 2814edd16368SStephen M. Cameron } 2815edd16368SStephen M. Cameron } 2816edd16368SStephen M. Cameron 2817edd16368SStephen M. Cameron static inline unsigned long get_next_completion(struct ctlr_info *h) 2818edd16368SStephen M. Cameron { 2819edd16368SStephen M. Cameron return h->access.command_completed(h); 2820edd16368SStephen M. Cameron } 2821edd16368SStephen M. Cameron 2822900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h) 2823edd16368SStephen M. Cameron { 2824edd16368SStephen M. Cameron return h->access.intr_pending(h); 2825edd16368SStephen M. Cameron } 2826edd16368SStephen M. Cameron 2827edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h) 2828edd16368SStephen M. Cameron { 2829303932fdSDon Brace return !(h->msi_vector || h->msix_vector) && 2830303932fdSDon Brace ((h->access.intr_pending(h) == 0) || 2831edd16368SStephen M. Cameron (h->interrupts_enabled == 0)); 2832edd16368SStephen M. Cameron } 2833edd16368SStephen M. Cameron 283401a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index, 283501a02ffcSStephen M. Cameron u32 raw_tag) 2836edd16368SStephen M. Cameron { 2837edd16368SStephen M. Cameron if (unlikely(tag_index >= h->nr_cmds)) { 2838edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag); 2839edd16368SStephen M. Cameron return 1; 2840edd16368SStephen M. Cameron } 2841edd16368SStephen M. Cameron return 0; 2842edd16368SStephen M. Cameron } 2843edd16368SStephen M. Cameron 284401a02ffcSStephen M. Cameron static inline void finish_cmd(struct CommandList *c, u32 raw_tag) 2845edd16368SStephen M. Cameron { 2846edd16368SStephen M. Cameron removeQ(c); 2847edd16368SStephen M. Cameron if (likely(c->cmd_type == CMD_SCSI)) 2848edd16368SStephen M. Cameron complete_scsi_command(c, 0, raw_tag); 2849edd16368SStephen M. Cameron else if (c->cmd_type == CMD_IOCTL_PEND) 2850edd16368SStephen M. Cameron complete(c->waiting); 2851edd16368SStephen M. Cameron } 2852edd16368SStephen M. Cameron 2853a104c99fSStephen M. Cameron static inline u32 hpsa_tag_contains_index(u32 tag) 2854a104c99fSStephen M. Cameron { 2855303932fdSDon Brace #define DIRECT_LOOKUP_BIT 0x10 2856a104c99fSStephen M. Cameron return tag & DIRECT_LOOKUP_BIT; 2857a104c99fSStephen M. Cameron } 2858a104c99fSStephen M. Cameron 2859a104c99fSStephen M. Cameron static inline u32 hpsa_tag_to_index(u32 tag) 2860a104c99fSStephen M. Cameron { 2861303932fdSDon Brace #define DIRECT_LOOKUP_SHIFT 5 2862a104c99fSStephen M. Cameron return tag >> DIRECT_LOOKUP_SHIFT; 2863a104c99fSStephen M. Cameron } 2864a104c99fSStephen M. Cameron 2865a104c99fSStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(u32 tag) 2866a104c99fSStephen M. Cameron { 2867a104c99fSStephen M. Cameron #define HPSA_ERROR_BITS 0x03 2868a104c99fSStephen M. Cameron return tag & ~HPSA_ERROR_BITS; 2869a104c99fSStephen M. Cameron } 2870a104c99fSStephen M. Cameron 2871303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */ 2872303932fdSDon Brace static inline u32 process_indexed_cmd(struct ctlr_info *h, 2873303932fdSDon Brace u32 raw_tag) 2874303932fdSDon Brace { 2875303932fdSDon Brace u32 tag_index; 2876303932fdSDon Brace struct CommandList *c; 2877303932fdSDon Brace 2878303932fdSDon Brace tag_index = hpsa_tag_to_index(raw_tag); 2879303932fdSDon Brace if (bad_tag(h, tag_index, raw_tag)) 2880303932fdSDon Brace return next_command(h); 2881303932fdSDon Brace c = h->cmd_pool + tag_index; 2882303932fdSDon Brace finish_cmd(c, raw_tag); 2883303932fdSDon Brace return next_command(h); 2884303932fdSDon Brace } 2885303932fdSDon Brace 2886303932fdSDon Brace /* process completion of a non-indexed command */ 2887303932fdSDon Brace static inline u32 process_nonindexed_cmd(struct ctlr_info *h, 2888303932fdSDon Brace u32 raw_tag) 2889303932fdSDon Brace { 2890303932fdSDon Brace u32 tag; 2891303932fdSDon Brace struct CommandList *c = NULL; 2892303932fdSDon Brace struct hlist_node *tmp; 2893303932fdSDon Brace 2894303932fdSDon Brace tag = hpsa_tag_discard_error_bits(raw_tag); 2895303932fdSDon Brace hlist_for_each_entry(c, tmp, &h->cmpQ, list) { 2896303932fdSDon Brace if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) { 2897303932fdSDon Brace finish_cmd(c, raw_tag); 2898303932fdSDon Brace return next_command(h); 2899303932fdSDon Brace } 2900303932fdSDon Brace } 2901303932fdSDon Brace bad_tag(h, h->nr_cmds + 1, raw_tag); 2902303932fdSDon Brace return next_command(h); 2903303932fdSDon Brace } 2904303932fdSDon Brace 2905edd16368SStephen M. Cameron static irqreturn_t do_hpsa_intr(int irq, void *dev_id) 2906edd16368SStephen M. Cameron { 2907edd16368SStephen M. Cameron struct ctlr_info *h = dev_id; 2908edd16368SStephen M. Cameron unsigned long flags; 2909303932fdSDon Brace u32 raw_tag; 2910edd16368SStephen M. Cameron 2911edd16368SStephen M. Cameron if (interrupt_not_for_us(h)) 2912edd16368SStephen M. Cameron return IRQ_NONE; 2913edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 2914303932fdSDon Brace raw_tag = get_next_completion(h); 2915303932fdSDon Brace while (raw_tag != FIFO_EMPTY) { 2916303932fdSDon Brace if (hpsa_tag_contains_index(raw_tag)) 2917303932fdSDon Brace raw_tag = process_indexed_cmd(h, raw_tag); 2918303932fdSDon Brace else 2919303932fdSDon Brace raw_tag = process_nonindexed_cmd(h, raw_tag); 2920edd16368SStephen M. Cameron } 2921edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 2922edd16368SStephen M. Cameron return IRQ_HANDLED; 2923edd16368SStephen M. Cameron } 2924edd16368SStephen M. Cameron 2925f0edafc6SStephen M. Cameron /* Send a message CDB to the firmware. */ 2926edd16368SStephen M. Cameron static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode, 2927edd16368SStephen M. Cameron unsigned char type) 2928edd16368SStephen M. Cameron { 2929edd16368SStephen M. Cameron struct Command { 2930edd16368SStephen M. Cameron struct CommandListHeader CommandHeader; 2931edd16368SStephen M. Cameron struct RequestBlock Request; 2932edd16368SStephen M. Cameron struct ErrDescriptor ErrorDescriptor; 2933edd16368SStephen M. Cameron }; 2934edd16368SStephen M. Cameron struct Command *cmd; 2935edd16368SStephen M. Cameron static const size_t cmd_sz = sizeof(*cmd) + 2936edd16368SStephen M. Cameron sizeof(cmd->ErrorDescriptor); 2937edd16368SStephen M. Cameron dma_addr_t paddr64; 2938edd16368SStephen M. Cameron uint32_t paddr32, tag; 2939edd16368SStephen M. Cameron void __iomem *vaddr; 2940edd16368SStephen M. Cameron int i, err; 2941edd16368SStephen M. Cameron 2942edd16368SStephen M. Cameron vaddr = pci_ioremap_bar(pdev, 0); 2943edd16368SStephen M. Cameron if (vaddr == NULL) 2944edd16368SStephen M. Cameron return -ENOMEM; 2945edd16368SStephen M. Cameron 2946edd16368SStephen M. Cameron /* The Inbound Post Queue only accepts 32-bit physical addresses for the 2947edd16368SStephen M. Cameron * CCISS commands, so they must be allocated from the lower 4GiB of 2948edd16368SStephen M. Cameron * memory. 2949edd16368SStephen M. Cameron */ 2950edd16368SStephen M. Cameron err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 2951edd16368SStephen M. Cameron if (err) { 2952edd16368SStephen M. Cameron iounmap(vaddr); 2953edd16368SStephen M. Cameron return -ENOMEM; 2954edd16368SStephen M. Cameron } 2955edd16368SStephen M. Cameron 2956edd16368SStephen M. Cameron cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64); 2957edd16368SStephen M. Cameron if (cmd == NULL) { 2958edd16368SStephen M. Cameron iounmap(vaddr); 2959edd16368SStephen M. Cameron return -ENOMEM; 2960edd16368SStephen M. Cameron } 2961edd16368SStephen M. Cameron 2962edd16368SStephen M. Cameron /* This must fit, because of the 32-bit consistent DMA mask. Also, 2963edd16368SStephen M. Cameron * although there's no guarantee, we assume that the address is at 2964edd16368SStephen M. Cameron * least 4-byte aligned (most likely, it's page-aligned). 2965edd16368SStephen M. Cameron */ 2966edd16368SStephen M. Cameron paddr32 = paddr64; 2967edd16368SStephen M. Cameron 2968edd16368SStephen M. Cameron cmd->CommandHeader.ReplyQueue = 0; 2969edd16368SStephen M. Cameron cmd->CommandHeader.SGList = 0; 2970edd16368SStephen M. Cameron cmd->CommandHeader.SGTotal = 0; 2971edd16368SStephen M. Cameron cmd->CommandHeader.Tag.lower = paddr32; 2972edd16368SStephen M. Cameron cmd->CommandHeader.Tag.upper = 0; 2973edd16368SStephen M. Cameron memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8); 2974edd16368SStephen M. Cameron 2975edd16368SStephen M. Cameron cmd->Request.CDBLen = 16; 2976edd16368SStephen M. Cameron cmd->Request.Type.Type = TYPE_MSG; 2977edd16368SStephen M. Cameron cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE; 2978edd16368SStephen M. Cameron cmd->Request.Type.Direction = XFER_NONE; 2979edd16368SStephen M. Cameron cmd->Request.Timeout = 0; /* Don't time out */ 2980edd16368SStephen M. Cameron cmd->Request.CDB[0] = opcode; 2981edd16368SStephen M. Cameron cmd->Request.CDB[1] = type; 2982edd16368SStephen M. Cameron memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */ 2983edd16368SStephen M. Cameron cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd); 2984edd16368SStephen M. Cameron cmd->ErrorDescriptor.Addr.upper = 0; 2985edd16368SStephen M. Cameron cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo); 2986edd16368SStephen M. Cameron 2987edd16368SStephen M. Cameron writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET); 2988edd16368SStephen M. Cameron 2989edd16368SStephen M. Cameron for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) { 2990edd16368SStephen M. Cameron tag = readl(vaddr + SA5_REPLY_PORT_OFFSET); 2991a104c99fSStephen M. Cameron if (hpsa_tag_discard_error_bits(tag) == paddr32) 2992edd16368SStephen M. Cameron break; 2993edd16368SStephen M. Cameron msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS); 2994edd16368SStephen M. Cameron } 2995edd16368SStephen M. Cameron 2996edd16368SStephen M. Cameron iounmap(vaddr); 2997edd16368SStephen M. Cameron 2998edd16368SStephen M. Cameron /* we leak the DMA buffer here ... no choice since the controller could 2999edd16368SStephen M. Cameron * still complete the command. 3000edd16368SStephen M. Cameron */ 3001edd16368SStephen M. Cameron if (i == HPSA_MSG_SEND_RETRY_LIMIT) { 3002edd16368SStephen M. Cameron dev_err(&pdev->dev, "controller message %02x:%02x timed out\n", 3003edd16368SStephen M. Cameron opcode, type); 3004edd16368SStephen M. Cameron return -ETIMEDOUT; 3005edd16368SStephen M. Cameron } 3006edd16368SStephen M. Cameron 3007edd16368SStephen M. Cameron pci_free_consistent(pdev, cmd_sz, cmd, paddr64); 3008edd16368SStephen M. Cameron 3009edd16368SStephen M. Cameron if (tag & HPSA_ERROR_BIT) { 3010edd16368SStephen M. Cameron dev_err(&pdev->dev, "controller message %02x:%02x failed\n", 3011edd16368SStephen M. Cameron opcode, type); 3012edd16368SStephen M. Cameron return -EIO; 3013edd16368SStephen M. Cameron } 3014edd16368SStephen M. Cameron 3015edd16368SStephen M. Cameron dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n", 3016edd16368SStephen M. Cameron opcode, type); 3017edd16368SStephen M. Cameron return 0; 3018edd16368SStephen M. Cameron } 3019edd16368SStephen M. Cameron 3020edd16368SStephen M. Cameron #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0) 3021edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0) 3022edd16368SStephen M. Cameron 3023edd16368SStephen M. Cameron static __devinit int hpsa_reset_msi(struct pci_dev *pdev) 3024edd16368SStephen M. Cameron { 3025edd16368SStephen M. Cameron /* the #defines are stolen from drivers/pci/msi.h. */ 3026edd16368SStephen M. Cameron #define msi_control_reg(base) (base + PCI_MSI_FLAGS) 3027edd16368SStephen M. Cameron #define PCI_MSIX_FLAGS_ENABLE (1 << 15) 3028edd16368SStephen M. Cameron 3029edd16368SStephen M. Cameron int pos; 3030edd16368SStephen M. Cameron u16 control = 0; 3031edd16368SStephen M. Cameron 3032edd16368SStephen M. Cameron pos = pci_find_capability(pdev, PCI_CAP_ID_MSI); 3033edd16368SStephen M. Cameron if (pos) { 3034edd16368SStephen M. Cameron pci_read_config_word(pdev, msi_control_reg(pos), &control); 3035edd16368SStephen M. Cameron if (control & PCI_MSI_FLAGS_ENABLE) { 3036edd16368SStephen M. Cameron dev_info(&pdev->dev, "resetting MSI\n"); 3037edd16368SStephen M. Cameron pci_write_config_word(pdev, msi_control_reg(pos), 3038edd16368SStephen M. Cameron control & ~PCI_MSI_FLAGS_ENABLE); 3039edd16368SStephen M. Cameron } 3040edd16368SStephen M. Cameron } 3041edd16368SStephen M. Cameron 3042edd16368SStephen M. Cameron pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX); 3043edd16368SStephen M. Cameron if (pos) { 3044edd16368SStephen M. Cameron pci_read_config_word(pdev, msi_control_reg(pos), &control); 3045edd16368SStephen M. Cameron if (control & PCI_MSIX_FLAGS_ENABLE) { 3046edd16368SStephen M. Cameron dev_info(&pdev->dev, "resetting MSI-X\n"); 3047edd16368SStephen M. Cameron pci_write_config_word(pdev, msi_control_reg(pos), 3048edd16368SStephen M. Cameron control & ~PCI_MSIX_FLAGS_ENABLE); 3049edd16368SStephen M. Cameron } 3050edd16368SStephen M. Cameron } 3051edd16368SStephen M. Cameron 3052edd16368SStephen M. Cameron return 0; 3053edd16368SStephen M. Cameron } 3054edd16368SStephen M. Cameron 3055edd16368SStephen M. Cameron /* This does a hard reset of the controller using PCI power management 3056edd16368SStephen M. Cameron * states. 3057edd16368SStephen M. Cameron */ 3058edd16368SStephen M. Cameron static __devinit int hpsa_hard_reset_controller(struct pci_dev *pdev) 3059edd16368SStephen M. Cameron { 3060edd16368SStephen M. Cameron u16 pmcsr, saved_config_space[32]; 3061edd16368SStephen M. Cameron int i, pos; 3062edd16368SStephen M. Cameron 3063edd16368SStephen M. Cameron dev_info(&pdev->dev, "using PCI PM to reset controller\n"); 3064edd16368SStephen M. Cameron 3065edd16368SStephen M. Cameron /* This is very nearly the same thing as 3066edd16368SStephen M. Cameron * 3067edd16368SStephen M. Cameron * pci_save_state(pci_dev); 3068edd16368SStephen M. Cameron * pci_set_power_state(pci_dev, PCI_D3hot); 3069edd16368SStephen M. Cameron * pci_set_power_state(pci_dev, PCI_D0); 3070edd16368SStephen M. Cameron * pci_restore_state(pci_dev); 3071edd16368SStephen M. Cameron * 3072edd16368SStephen M. Cameron * but we can't use these nice canned kernel routines on 3073edd16368SStephen M. Cameron * kexec, because they also check the MSI/MSI-X state in PCI 3074edd16368SStephen M. Cameron * configuration space and do the wrong thing when it is 3075edd16368SStephen M. Cameron * set/cleared. Also, the pci_save/restore_state functions 3076edd16368SStephen M. Cameron * violate the ordering requirements for restoring the 3077edd16368SStephen M. Cameron * configuration space from the CCISS document (see the 3078edd16368SStephen M. Cameron * comment below). So we roll our own .... 3079edd16368SStephen M. Cameron */ 3080edd16368SStephen M. Cameron 3081edd16368SStephen M. Cameron for (i = 0; i < 32; i++) 3082edd16368SStephen M. Cameron pci_read_config_word(pdev, 2*i, &saved_config_space[i]); 3083edd16368SStephen M. Cameron 3084edd16368SStephen M. Cameron pos = pci_find_capability(pdev, PCI_CAP_ID_PM); 3085edd16368SStephen M. Cameron if (pos == 0) { 3086edd16368SStephen M. Cameron dev_err(&pdev->dev, 3087edd16368SStephen M. Cameron "hpsa_reset_controller: PCI PM not supported\n"); 3088edd16368SStephen M. Cameron return -ENODEV; 3089edd16368SStephen M. Cameron } 3090edd16368SStephen M. Cameron 3091edd16368SStephen M. Cameron /* Quoting from the Open CISS Specification: "The Power 3092edd16368SStephen M. Cameron * Management Control/Status Register (CSR) controls the power 3093edd16368SStephen M. Cameron * state of the device. The normal operating state is D0, 3094edd16368SStephen M. Cameron * CSR=00h. The software off state is D3, CSR=03h. To reset 3095edd16368SStephen M. Cameron * the controller, place the interface device in D3 then to 3096edd16368SStephen M. Cameron * D0, this causes a secondary PCI reset which will reset the 3097edd16368SStephen M. Cameron * controller." 3098edd16368SStephen M. Cameron */ 3099edd16368SStephen M. Cameron 3100edd16368SStephen M. Cameron /* enter the D3hot power management state */ 3101edd16368SStephen M. Cameron pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr); 3102edd16368SStephen M. Cameron pmcsr &= ~PCI_PM_CTRL_STATE_MASK; 3103edd16368SStephen M. Cameron pmcsr |= PCI_D3hot; 3104edd16368SStephen M. Cameron pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr); 3105edd16368SStephen M. Cameron 3106edd16368SStephen M. Cameron msleep(500); 3107edd16368SStephen M. Cameron 3108edd16368SStephen M. Cameron /* enter the D0 power management state */ 3109edd16368SStephen M. Cameron pmcsr &= ~PCI_PM_CTRL_STATE_MASK; 3110edd16368SStephen M. Cameron pmcsr |= PCI_D0; 3111edd16368SStephen M. Cameron pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr); 3112edd16368SStephen M. Cameron 3113edd16368SStephen M. Cameron msleep(500); 3114edd16368SStephen M. Cameron 3115edd16368SStephen M. Cameron /* Restore the PCI configuration space. The Open CISS 3116edd16368SStephen M. Cameron * Specification says, "Restore the PCI Configuration 3117edd16368SStephen M. Cameron * Registers, offsets 00h through 60h. It is important to 3118edd16368SStephen M. Cameron * restore the command register, 16-bits at offset 04h, 3119edd16368SStephen M. Cameron * last. Do not restore the configuration status register, 3120edd16368SStephen M. Cameron * 16-bits at offset 06h." Note that the offset is 2*i. 3121edd16368SStephen M. Cameron */ 3122edd16368SStephen M. Cameron for (i = 0; i < 32; i++) { 3123edd16368SStephen M. Cameron if (i == 2 || i == 3) 3124edd16368SStephen M. Cameron continue; 3125edd16368SStephen M. Cameron pci_write_config_word(pdev, 2*i, saved_config_space[i]); 3126edd16368SStephen M. Cameron } 3127edd16368SStephen M. Cameron wmb(); 3128edd16368SStephen M. Cameron pci_write_config_word(pdev, 4, saved_config_space[2]); 3129edd16368SStephen M. Cameron 3130edd16368SStephen M. Cameron return 0; 3131edd16368SStephen M. Cameron } 3132edd16368SStephen M. Cameron 3133edd16368SStephen M. Cameron /* 3134edd16368SStephen M. Cameron * We cannot read the structure directly, for portability we must use 3135edd16368SStephen M. Cameron * the io functions. 3136edd16368SStephen M. Cameron * This is for debug only. 3137edd16368SStephen M. Cameron */ 3138edd16368SStephen M. Cameron #ifdef HPSA_DEBUG 3139edd16368SStephen M. Cameron static void print_cfg_table(struct device *dev, struct CfgTable *tb) 3140edd16368SStephen M. Cameron { 3141edd16368SStephen M. Cameron int i; 3142edd16368SStephen M. Cameron char temp_name[17]; 3143edd16368SStephen M. Cameron 3144edd16368SStephen M. Cameron dev_info(dev, "Controller Configuration information\n"); 3145edd16368SStephen M. Cameron dev_info(dev, "------------------------------------\n"); 3146edd16368SStephen M. Cameron for (i = 0; i < 4; i++) 3147edd16368SStephen M. Cameron temp_name[i] = readb(&(tb->Signature[i])); 3148edd16368SStephen M. Cameron temp_name[4] = '\0'; 3149edd16368SStephen M. Cameron dev_info(dev, " Signature = %s\n", temp_name); 3150edd16368SStephen M. Cameron dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence))); 3151edd16368SStephen M. Cameron dev_info(dev, " Transport methods supported = 0x%x\n", 3152edd16368SStephen M. Cameron readl(&(tb->TransportSupport))); 3153edd16368SStephen M. Cameron dev_info(dev, " Transport methods active = 0x%x\n", 3154edd16368SStephen M. Cameron readl(&(tb->TransportActive))); 3155edd16368SStephen M. Cameron dev_info(dev, " Requested transport Method = 0x%x\n", 3156edd16368SStephen M. Cameron readl(&(tb->HostWrite.TransportRequest))); 3157edd16368SStephen M. Cameron dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n", 3158edd16368SStephen M. Cameron readl(&(tb->HostWrite.CoalIntDelay))); 3159edd16368SStephen M. Cameron dev_info(dev, " Coalesce Interrupt Count = 0x%x\n", 3160edd16368SStephen M. Cameron readl(&(tb->HostWrite.CoalIntCount))); 3161edd16368SStephen M. Cameron dev_info(dev, " Max outstanding commands = 0x%d\n", 3162edd16368SStephen M. Cameron readl(&(tb->CmdsOutMax))); 3163edd16368SStephen M. Cameron dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes))); 3164edd16368SStephen M. Cameron for (i = 0; i < 16; i++) 3165edd16368SStephen M. Cameron temp_name[i] = readb(&(tb->ServerName[i])); 3166edd16368SStephen M. Cameron temp_name[16] = '\0'; 3167edd16368SStephen M. Cameron dev_info(dev, " Server Name = %s\n", temp_name); 3168edd16368SStephen M. Cameron dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n", 3169edd16368SStephen M. Cameron readl(&(tb->HeartBeat))); 3170edd16368SStephen M. Cameron } 3171edd16368SStephen M. Cameron #endif /* HPSA_DEBUG */ 3172edd16368SStephen M. Cameron 3173edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr) 3174edd16368SStephen M. Cameron { 3175edd16368SStephen M. Cameron int i, offset, mem_type, bar_type; 3176edd16368SStephen M. Cameron 3177edd16368SStephen M. Cameron if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */ 3178edd16368SStephen M. Cameron return 0; 3179edd16368SStephen M. Cameron offset = 0; 3180edd16368SStephen M. Cameron for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 3181edd16368SStephen M. Cameron bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE; 3182edd16368SStephen M. Cameron if (bar_type == PCI_BASE_ADDRESS_SPACE_IO) 3183edd16368SStephen M. Cameron offset += 4; 3184edd16368SStephen M. Cameron else { 3185edd16368SStephen M. Cameron mem_type = pci_resource_flags(pdev, i) & 3186edd16368SStephen M. Cameron PCI_BASE_ADDRESS_MEM_TYPE_MASK; 3187edd16368SStephen M. Cameron switch (mem_type) { 3188edd16368SStephen M. Cameron case PCI_BASE_ADDRESS_MEM_TYPE_32: 3189edd16368SStephen M. Cameron case PCI_BASE_ADDRESS_MEM_TYPE_1M: 3190edd16368SStephen M. Cameron offset += 4; /* 32 bit */ 3191edd16368SStephen M. Cameron break; 3192edd16368SStephen M. Cameron case PCI_BASE_ADDRESS_MEM_TYPE_64: 3193edd16368SStephen M. Cameron offset += 8; 3194edd16368SStephen M. Cameron break; 3195edd16368SStephen M. Cameron default: /* reserved in PCI 2.2 */ 3196edd16368SStephen M. Cameron dev_warn(&pdev->dev, 3197edd16368SStephen M. Cameron "base address is invalid\n"); 3198edd16368SStephen M. Cameron return -1; 3199edd16368SStephen M. Cameron break; 3200edd16368SStephen M. Cameron } 3201edd16368SStephen M. Cameron } 3202edd16368SStephen M. Cameron if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0) 3203edd16368SStephen M. Cameron return i + 1; 3204edd16368SStephen M. Cameron } 3205edd16368SStephen M. Cameron return -1; 3206edd16368SStephen M. Cameron } 3207edd16368SStephen M. Cameron 3208edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on 3209edd16368SStephen M. Cameron * controllers that are capable. If not, we use IO-APIC mode. 3210edd16368SStephen M. Cameron */ 3211edd16368SStephen M. Cameron 3212*55c06c71SStephen M. Cameron static void __devinit hpsa_interrupt_mode(struct ctlr_info *h, u32 board_id) 3213edd16368SStephen M. Cameron { 3214edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI 3215edd16368SStephen M. Cameron int err; 3216edd16368SStephen M. Cameron struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1}, 3217edd16368SStephen M. Cameron {0, 2}, {0, 3} 3218edd16368SStephen M. Cameron }; 3219edd16368SStephen M. Cameron 3220edd16368SStephen M. Cameron /* Some boards advertise MSI but don't really support it */ 3221edd16368SStephen M. Cameron if ((board_id == 0x40700E11) || 3222edd16368SStephen M. Cameron (board_id == 0x40800E11) || 3223edd16368SStephen M. Cameron (board_id == 0x40820E11) || (board_id == 0x40830E11)) 3224edd16368SStephen M. Cameron goto default_int_mode; 3225*55c06c71SStephen M. Cameron if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) { 3226*55c06c71SStephen M. Cameron dev_info(&h->pdev->dev, "MSIX\n"); 3227*55c06c71SStephen M. Cameron err = pci_enable_msix(h->pdev, hpsa_msix_entries, 4); 3228edd16368SStephen M. Cameron if (!err) { 3229edd16368SStephen M. Cameron h->intr[0] = hpsa_msix_entries[0].vector; 3230edd16368SStephen M. Cameron h->intr[1] = hpsa_msix_entries[1].vector; 3231edd16368SStephen M. Cameron h->intr[2] = hpsa_msix_entries[2].vector; 3232edd16368SStephen M. Cameron h->intr[3] = hpsa_msix_entries[3].vector; 3233edd16368SStephen M. Cameron h->msix_vector = 1; 3234edd16368SStephen M. Cameron return; 3235edd16368SStephen M. Cameron } 3236edd16368SStephen M. Cameron if (err > 0) { 3237*55c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "only %d MSI-X vectors " 3238edd16368SStephen M. Cameron "available\n", err); 3239edd16368SStephen M. Cameron goto default_int_mode; 3240edd16368SStephen M. Cameron } else { 3241*55c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", 3242edd16368SStephen M. Cameron err); 3243edd16368SStephen M. Cameron goto default_int_mode; 3244edd16368SStephen M. Cameron } 3245edd16368SStephen M. Cameron } 3246*55c06c71SStephen M. Cameron if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) { 3247*55c06c71SStephen M. Cameron dev_info(&h->pdev->dev, "MSI\n"); 3248*55c06c71SStephen M. Cameron if (!pci_enable_msi(h->pdev)) 3249edd16368SStephen M. Cameron h->msi_vector = 1; 3250edd16368SStephen M. Cameron else 3251*55c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "MSI init failed\n"); 3252edd16368SStephen M. Cameron } 3253edd16368SStephen M. Cameron default_int_mode: 3254edd16368SStephen M. Cameron #endif /* CONFIG_PCI_MSI */ 3255edd16368SStephen M. Cameron /* if we get here we're going to use the default interrupt mode */ 3256*55c06c71SStephen M. Cameron h->intr[PERF_MODE_INT] = h->pdev->irq; 3257edd16368SStephen M. Cameron } 3258edd16368SStephen M. Cameron 3259*55c06c71SStephen M. Cameron static int __devinit hpsa_pci_init(struct ctlr_info *h) 3260edd16368SStephen M. Cameron { 3261edd16368SStephen M. Cameron ushort subsystem_vendor_id, subsystem_device_id, command; 326201a02ffcSStephen M. Cameron u32 board_id, scratchpad = 0; 326301a02ffcSStephen M. Cameron u64 cfg_offset; 326401a02ffcSStephen M. Cameron u32 cfg_base_addr; 326501a02ffcSStephen M. Cameron u64 cfg_base_addr_index; 3266303932fdSDon Brace u32 trans_offset; 3267edd16368SStephen M. Cameron int i, prod_index, err; 3268edd16368SStephen M. Cameron 3269*55c06c71SStephen M. Cameron subsystem_vendor_id = h->pdev->subsystem_vendor; 3270*55c06c71SStephen M. Cameron subsystem_device_id = h->pdev->subsystem_device; 327101a02ffcSStephen M. Cameron board_id = (((u32) (subsystem_device_id << 16) & 0xffff0000) | 3272edd16368SStephen M. Cameron subsystem_vendor_id); 3273edd16368SStephen M. Cameron 3274edd16368SStephen M. Cameron for (i = 0; i < ARRAY_SIZE(products); i++) 3275edd16368SStephen M. Cameron if (board_id == products[i].board_id) 3276edd16368SStephen M. Cameron break; 3277edd16368SStephen M. Cameron 3278edd16368SStephen M. Cameron prod_index = i; 3279edd16368SStephen M. Cameron 3280edd16368SStephen M. Cameron if (prod_index == ARRAY_SIZE(products)) { 3281edd16368SStephen M. Cameron prod_index--; 3282edd16368SStephen M. Cameron if (subsystem_vendor_id != PCI_VENDOR_ID_HP || 3283edd16368SStephen M. Cameron !hpsa_allow_any) { 3284*55c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "unrecognized board ID:" 3285edd16368SStephen M. Cameron " 0x%08lx, ignoring.\n", 3286edd16368SStephen M. Cameron (unsigned long) board_id); 3287edd16368SStephen M. Cameron return -ENODEV; 3288edd16368SStephen M. Cameron } 3289edd16368SStephen M. Cameron } 3290edd16368SStephen M. Cameron /* check to see if controller has been disabled 3291edd16368SStephen M. Cameron * BEFORE trying to enable it 3292edd16368SStephen M. Cameron */ 3293*55c06c71SStephen M. Cameron (void)pci_read_config_word(h->pdev, PCI_COMMAND, &command); 3294edd16368SStephen M. Cameron if (!(command & 0x02)) { 3295*55c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "controller appears to be disabled\n"); 3296edd16368SStephen M. Cameron return -ENODEV; 3297edd16368SStephen M. Cameron } 3298edd16368SStephen M. Cameron 3299*55c06c71SStephen M. Cameron err = pci_enable_device(h->pdev); 3300edd16368SStephen M. Cameron if (err) { 3301*55c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "unable to enable PCI device\n"); 3302edd16368SStephen M. Cameron return err; 3303edd16368SStephen M. Cameron } 3304edd16368SStephen M. Cameron 3305*55c06c71SStephen M. Cameron err = pci_request_regions(h->pdev, "hpsa"); 3306edd16368SStephen M. Cameron if (err) { 3307*55c06c71SStephen M. Cameron dev_err(&h->pdev->dev, 3308*55c06c71SStephen M. Cameron "cannot obtain PCI resources, aborting\n"); 3309edd16368SStephen M. Cameron return err; 3310edd16368SStephen M. Cameron } 3311edd16368SStephen M. Cameron 3312edd16368SStephen M. Cameron /* If the kernel supports MSI/MSI-X we will try to enable that, 3313edd16368SStephen M. Cameron * else we use the IO-APIC interrupt assigned to us by system ROM. 3314edd16368SStephen M. Cameron */ 3315*55c06c71SStephen M. Cameron hpsa_interrupt_mode(h, board_id); 3316edd16368SStephen M. Cameron 3317edd16368SStephen M. Cameron /* find the memory BAR */ 3318edd16368SStephen M. Cameron for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 3319*55c06c71SStephen M. Cameron if (pci_resource_flags(h->pdev, i) & IORESOURCE_MEM) 3320edd16368SStephen M. Cameron break; 3321edd16368SStephen M. Cameron } 3322edd16368SStephen M. Cameron if (i == DEVICE_COUNT_RESOURCE) { 3323*55c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "no memory BAR found\n"); 3324edd16368SStephen M. Cameron err = -ENODEV; 3325edd16368SStephen M. Cameron goto err_out_free_res; 3326edd16368SStephen M. Cameron } 3327edd16368SStephen M. Cameron 3328*55c06c71SStephen M. Cameron h->paddr = pci_resource_start(h->pdev, i); /* addressing mode bits 3329edd16368SStephen M. Cameron * already removed 3330edd16368SStephen M. Cameron */ 3331edd16368SStephen M. Cameron 3332edd16368SStephen M. Cameron h->vaddr = remap_pci_mem(h->paddr, 0x250); 3333edd16368SStephen M. Cameron 3334edd16368SStephen M. Cameron /* Wait for the board to become ready. */ 3335edd16368SStephen M. Cameron for (i = 0; i < HPSA_BOARD_READY_ITERATIONS; i++) { 3336edd16368SStephen M. Cameron scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET); 3337edd16368SStephen M. Cameron if (scratchpad == HPSA_FIRMWARE_READY) 3338edd16368SStephen M. Cameron break; 3339edd16368SStephen M. Cameron msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS); 3340edd16368SStephen M. Cameron } 3341edd16368SStephen M. Cameron if (scratchpad != HPSA_FIRMWARE_READY) { 3342*55c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "board not ready, timed out.\n"); 3343edd16368SStephen M. Cameron err = -ENODEV; 3344edd16368SStephen M. Cameron goto err_out_free_res; 3345edd16368SStephen M. Cameron } 3346edd16368SStephen M. Cameron 3347edd16368SStephen M. Cameron /* get the address index number */ 3348edd16368SStephen M. Cameron cfg_base_addr = readl(h->vaddr + SA5_CTCFG_OFFSET); 334901a02ffcSStephen M. Cameron cfg_base_addr &= (u32) 0x0000ffff; 3350*55c06c71SStephen M. Cameron cfg_base_addr_index = find_PCI_BAR_index(h->pdev, cfg_base_addr); 3351edd16368SStephen M. Cameron if (cfg_base_addr_index == -1) { 3352*55c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "cannot find cfg_base_addr_index\n"); 3353edd16368SStephen M. Cameron err = -ENODEV; 3354edd16368SStephen M. Cameron goto err_out_free_res; 3355edd16368SStephen M. Cameron } 3356edd16368SStephen M. Cameron 3357edd16368SStephen M. Cameron cfg_offset = readl(h->vaddr + SA5_CTMEM_OFFSET); 3358*55c06c71SStephen M. Cameron h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev, 3359edd16368SStephen M. Cameron cfg_base_addr_index) + cfg_offset, 3360edd16368SStephen M. Cameron sizeof(h->cfgtable)); 3361303932fdSDon Brace /* Find performant mode table. */ 3362303932fdSDon Brace trans_offset = readl(&(h->cfgtable->TransMethodOffset)); 3363*55c06c71SStephen M. Cameron h->transtable = remap_pci_mem(pci_resource_start(h->pdev, 3364303932fdSDon Brace cfg_base_addr_index)+cfg_offset+trans_offset, 3365303932fdSDon Brace sizeof(*h->transtable)); 3366303932fdSDon Brace 3367edd16368SStephen M. Cameron h->board_id = board_id; 3368303932fdSDon Brace h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands)); 336933a2ffceSStephen M. Cameron h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements)); 337033a2ffceSStephen M. Cameron 337133a2ffceSStephen M. Cameron /* 337233a2ffceSStephen M. Cameron * Limit in-command s/g elements to 32 save dma'able memory. 337333a2ffceSStephen M. Cameron * Howvever spec says if 0, use 31 337433a2ffceSStephen M. Cameron */ 337533a2ffceSStephen M. Cameron 337633a2ffceSStephen M. Cameron h->max_cmd_sg_entries = 31; 337733a2ffceSStephen M. Cameron if (h->maxsgentries > 512) { 337833a2ffceSStephen M. Cameron h->max_cmd_sg_entries = 32; 337933a2ffceSStephen M. Cameron h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1; 338033a2ffceSStephen M. Cameron h->maxsgentries--; /* save one for chain pointer */ 338133a2ffceSStephen M. Cameron } else { 338233a2ffceSStephen M. Cameron h->maxsgentries = 31; /* default to traditional values */ 338333a2ffceSStephen M. Cameron h->chainsize = 0; 338433a2ffceSStephen M. Cameron } 338533a2ffceSStephen M. Cameron 3386edd16368SStephen M. Cameron h->product_name = products[prod_index].product_name; 3387edd16368SStephen M. Cameron h->access = *(products[prod_index].access); 3388edd16368SStephen M. Cameron /* Allow room for some ioctls */ 3389edd16368SStephen M. Cameron h->nr_cmds = h->max_commands - 4; 3390edd16368SStephen M. Cameron 3391edd16368SStephen M. Cameron if ((readb(&h->cfgtable->Signature[0]) != 'C') || 3392edd16368SStephen M. Cameron (readb(&h->cfgtable->Signature[1]) != 'I') || 3393edd16368SStephen M. Cameron (readb(&h->cfgtable->Signature[2]) != 'S') || 3394edd16368SStephen M. Cameron (readb(&h->cfgtable->Signature[3]) != 'S')) { 3395*55c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "not a valid CISS config table\n"); 3396edd16368SStephen M. Cameron err = -ENODEV; 3397edd16368SStephen M. Cameron goto err_out_free_res; 3398edd16368SStephen M. Cameron } 3399edd16368SStephen M. Cameron #ifdef CONFIG_X86 3400edd16368SStephen M. Cameron { 3401edd16368SStephen M. Cameron /* Need to enable prefetch in the SCSI core for 6400 in x86 */ 340201a02ffcSStephen M. Cameron u32 prefetch; 3403edd16368SStephen M. Cameron prefetch = readl(&(h->cfgtable->SCSI_Prefetch)); 3404edd16368SStephen M. Cameron prefetch |= 0x100; 3405edd16368SStephen M. Cameron writel(prefetch, &(h->cfgtable->SCSI_Prefetch)); 3406edd16368SStephen M. Cameron } 3407edd16368SStephen M. Cameron #endif 3408edd16368SStephen M. Cameron 3409edd16368SStephen M. Cameron /* Disabling DMA prefetch for the P600 3410edd16368SStephen M. Cameron * An ASIC bug may result in a prefetch beyond 3411edd16368SStephen M. Cameron * physical memory. 3412edd16368SStephen M. Cameron */ 3413edd16368SStephen M. Cameron if (board_id == 0x3225103C) { 341401a02ffcSStephen M. Cameron u32 dma_prefetch; 3415edd16368SStephen M. Cameron dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG); 3416edd16368SStephen M. Cameron dma_prefetch |= 0x8000; 3417edd16368SStephen M. Cameron writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG); 3418edd16368SStephen M. Cameron } 3419edd16368SStephen M. Cameron 3420edd16368SStephen M. Cameron h->max_commands = readl(&(h->cfgtable->CmdsOutMax)); 3421edd16368SStephen M. Cameron /* Update the field, and then ring the doorbell */ 3422edd16368SStephen M. Cameron writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest)); 3423edd16368SStephen M. Cameron writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL); 3424edd16368SStephen M. Cameron 3425edd16368SStephen M. Cameron /* under certain very rare conditions, this can take awhile. 3426edd16368SStephen M. Cameron * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right 3427edd16368SStephen M. Cameron * as we enter this code.) 3428edd16368SStephen M. Cameron */ 3429edd16368SStephen M. Cameron for (i = 0; i < MAX_CONFIG_WAIT; i++) { 3430edd16368SStephen M. Cameron if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq)) 3431edd16368SStephen M. Cameron break; 3432edd16368SStephen M. Cameron /* delay and try again */ 3433edd16368SStephen M. Cameron msleep(10); 3434edd16368SStephen M. Cameron } 3435edd16368SStephen M. Cameron 3436edd16368SStephen M. Cameron #ifdef HPSA_DEBUG 3437*55c06c71SStephen M. Cameron print_cfg_table(&h->pdev->dev, h->cfgtable); 3438edd16368SStephen M. Cameron #endif /* HPSA_DEBUG */ 3439edd16368SStephen M. Cameron 3440edd16368SStephen M. Cameron if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) { 3441*55c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, 3442*55c06c71SStephen M. Cameron "unable to get board into simple mode\n"); 3443edd16368SStephen M. Cameron err = -ENODEV; 3444edd16368SStephen M. Cameron goto err_out_free_res; 3445edd16368SStephen M. Cameron } 3446edd16368SStephen M. Cameron return 0; 3447edd16368SStephen M. Cameron 3448edd16368SStephen M. Cameron err_out_free_res: 3449edd16368SStephen M. Cameron /* 3450edd16368SStephen M. Cameron * Deliberately omit pci_disable_device(): it does something nasty to 3451edd16368SStephen M. Cameron * Smart Array controllers that pci_enable_device does not undo 3452edd16368SStephen M. Cameron */ 3453*55c06c71SStephen M. Cameron pci_release_regions(h->pdev); 3454edd16368SStephen M. Cameron return err; 3455edd16368SStephen M. Cameron } 3456edd16368SStephen M. Cameron 3457339b2b14SStephen M. Cameron static void __devinit hpsa_hba_inquiry(struct ctlr_info *h) 3458339b2b14SStephen M. Cameron { 3459339b2b14SStephen M. Cameron int rc; 3460339b2b14SStephen M. Cameron 3461339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64 3462339b2b14SStephen M. Cameron h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL); 3463339b2b14SStephen M. Cameron if (!h->hba_inquiry_data) 3464339b2b14SStephen M. Cameron return; 3465339b2b14SStephen M. Cameron rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0, 3466339b2b14SStephen M. Cameron h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT); 3467339b2b14SStephen M. Cameron if (rc != 0) { 3468339b2b14SStephen M. Cameron kfree(h->hba_inquiry_data); 3469339b2b14SStephen M. Cameron h->hba_inquiry_data = NULL; 3470339b2b14SStephen M. Cameron } 3471339b2b14SStephen M. Cameron } 3472339b2b14SStephen M. Cameron 3473edd16368SStephen M. Cameron static int __devinit hpsa_init_one(struct pci_dev *pdev, 3474edd16368SStephen M. Cameron const struct pci_device_id *ent) 3475edd16368SStephen M. Cameron { 3476ecd9aad4SStephen M. Cameron int i, rc; 3477edd16368SStephen M. Cameron int dac; 3478edd16368SStephen M. Cameron struct ctlr_info *h; 3479edd16368SStephen M. Cameron 3480edd16368SStephen M. Cameron if (number_of_controllers == 0) 3481edd16368SStephen M. Cameron printk(KERN_INFO DRIVER_NAME "\n"); 3482edd16368SStephen M. Cameron if (reset_devices) { 3483edd16368SStephen M. Cameron /* Reset the controller with a PCI power-cycle */ 3484edd16368SStephen M. Cameron if (hpsa_hard_reset_controller(pdev) || hpsa_reset_msi(pdev)) 3485edd16368SStephen M. Cameron return -ENODEV; 3486edd16368SStephen M. Cameron 3487edd16368SStephen M. Cameron /* Some devices (notably the HP Smart Array 5i Controller) 3488edd16368SStephen M. Cameron need a little pause here */ 3489edd16368SStephen M. Cameron msleep(HPSA_POST_RESET_PAUSE_MSECS); 3490edd16368SStephen M. Cameron 3491edd16368SStephen M. Cameron /* Now try to get the controller to respond to a no-op */ 3492edd16368SStephen M. Cameron for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) { 3493edd16368SStephen M. Cameron if (hpsa_noop(pdev) == 0) 3494edd16368SStephen M. Cameron break; 3495edd16368SStephen M. Cameron else 3496edd16368SStephen M. Cameron dev_warn(&pdev->dev, "no-op failed%s\n", 3497edd16368SStephen M. Cameron (i < 11 ? "; re-trying" : "")); 3498edd16368SStephen M. Cameron } 3499edd16368SStephen M. Cameron } 3500edd16368SStephen M. Cameron 3501303932fdSDon Brace /* Command structures must be aligned on a 32-byte boundary because 3502303932fdSDon Brace * the 5 lower bits of the address are used by the hardware. and by 3503303932fdSDon Brace * the driver. See comments in hpsa.h for more info. 3504303932fdSDon Brace */ 3505303932fdSDon Brace #define COMMANDLIST_ALIGNMENT 32 3506303932fdSDon Brace BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT); 3507edd16368SStephen M. Cameron h = kzalloc(sizeof(*h), GFP_KERNEL); 3508edd16368SStephen M. Cameron if (!h) 3509ecd9aad4SStephen M. Cameron return -ENOMEM; 3510edd16368SStephen M. Cameron 3511*55c06c71SStephen M. Cameron h->pdev = pdev; 3512edd16368SStephen M. Cameron h->busy_initializing = 1; 3513edd16368SStephen M. Cameron INIT_HLIST_HEAD(&h->cmpQ); 3514edd16368SStephen M. Cameron INIT_HLIST_HEAD(&h->reqQ); 3515*55c06c71SStephen M. Cameron rc = hpsa_pci_init(h); 3516ecd9aad4SStephen M. Cameron if (rc != 0) 3517edd16368SStephen M. Cameron goto clean1; 3518edd16368SStephen M. Cameron 3519edd16368SStephen M. Cameron sprintf(h->devname, "hpsa%d", number_of_controllers); 3520edd16368SStephen M. Cameron h->ctlr = number_of_controllers; 3521edd16368SStephen M. Cameron number_of_controllers++; 3522edd16368SStephen M. Cameron 3523edd16368SStephen M. Cameron /* configure PCI DMA stuff */ 3524ecd9aad4SStephen M. Cameron rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 3525ecd9aad4SStephen M. Cameron if (rc == 0) { 3526edd16368SStephen M. Cameron dac = 1; 3527ecd9aad4SStephen M. Cameron } else { 3528ecd9aad4SStephen M. Cameron rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 3529ecd9aad4SStephen M. Cameron if (rc == 0) { 3530edd16368SStephen M. Cameron dac = 0; 3531ecd9aad4SStephen M. Cameron } else { 3532edd16368SStephen M. Cameron dev_err(&pdev->dev, "no suitable DMA available\n"); 3533edd16368SStephen M. Cameron goto clean1; 3534edd16368SStephen M. Cameron } 3535ecd9aad4SStephen M. Cameron } 3536edd16368SStephen M. Cameron 3537edd16368SStephen M. Cameron /* make sure the board interrupts are off */ 3538edd16368SStephen M. Cameron h->access.set_intr_mask(h, HPSA_INTR_OFF); 3539303932fdSDon Brace rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr, 3540303932fdSDon Brace IRQF_DISABLED, h->devname, h); 3541ecd9aad4SStephen M. Cameron if (rc) { 3542edd16368SStephen M. Cameron dev_err(&pdev->dev, "unable to get irq %d for %s\n", 3543303932fdSDon Brace h->intr[PERF_MODE_INT], h->devname); 3544edd16368SStephen M. Cameron goto clean2; 3545edd16368SStephen M. Cameron } 3546edd16368SStephen M. Cameron 3547303932fdSDon Brace dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n", 3548303932fdSDon Brace h->devname, pdev->device, 3549303932fdSDon Brace h->intr[PERF_MODE_INT], dac ? "" : " not"); 3550edd16368SStephen M. Cameron 3551edd16368SStephen M. Cameron h->cmd_pool_bits = 3552edd16368SStephen M. Cameron kmalloc(((h->nr_cmds + BITS_PER_LONG - 3553edd16368SStephen M. Cameron 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL); 3554edd16368SStephen M. Cameron h->cmd_pool = pci_alloc_consistent(h->pdev, 3555edd16368SStephen M. Cameron h->nr_cmds * sizeof(*h->cmd_pool), 3556edd16368SStephen M. Cameron &(h->cmd_pool_dhandle)); 3557edd16368SStephen M. Cameron h->errinfo_pool = pci_alloc_consistent(h->pdev, 3558edd16368SStephen M. Cameron h->nr_cmds * sizeof(*h->errinfo_pool), 3559edd16368SStephen M. Cameron &(h->errinfo_pool_dhandle)); 3560edd16368SStephen M. Cameron if ((h->cmd_pool_bits == NULL) 3561edd16368SStephen M. Cameron || (h->cmd_pool == NULL) 3562edd16368SStephen M. Cameron || (h->errinfo_pool == NULL)) { 3563edd16368SStephen M. Cameron dev_err(&pdev->dev, "out of memory"); 3564ecd9aad4SStephen M. Cameron rc = -ENOMEM; 3565edd16368SStephen M. Cameron goto clean4; 3566edd16368SStephen M. Cameron } 356733a2ffceSStephen M. Cameron if (hpsa_allocate_sg_chain_blocks(h)) 356833a2ffceSStephen M. Cameron goto clean4; 3569edd16368SStephen M. Cameron spin_lock_init(&h->lock); 3570a08a8471SStephen M. Cameron spin_lock_init(&h->scan_lock); 3571a08a8471SStephen M. Cameron init_waitqueue_head(&h->scan_wait_queue); 3572a08a8471SStephen M. Cameron h->scan_finished = 1; /* no scan currently in progress */ 3573edd16368SStephen M. Cameron 3574edd16368SStephen M. Cameron pci_set_drvdata(pdev, h); 3575edd16368SStephen M. Cameron memset(h->cmd_pool_bits, 0, 3576edd16368SStephen M. Cameron ((h->nr_cmds + BITS_PER_LONG - 3577edd16368SStephen M. Cameron 1) / BITS_PER_LONG) * sizeof(unsigned long)); 3578edd16368SStephen M. Cameron 3579edd16368SStephen M. Cameron hpsa_scsi_setup(h); 3580edd16368SStephen M. Cameron 3581edd16368SStephen M. Cameron /* Turn the interrupts on so we can service requests */ 3582edd16368SStephen M. Cameron h->access.set_intr_mask(h, HPSA_INTR_ON); 3583edd16368SStephen M. Cameron 3584303932fdSDon Brace hpsa_put_ctlr_into_performant_mode(h); 3585339b2b14SStephen M. Cameron hpsa_hba_inquiry(h); 3586edd16368SStephen M. Cameron hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */ 3587edd16368SStephen M. Cameron h->busy_initializing = 0; 3588edd16368SStephen M. Cameron return 1; 3589edd16368SStephen M. Cameron 3590edd16368SStephen M. Cameron clean4: 359133a2ffceSStephen M. Cameron hpsa_free_sg_chain_blocks(h); 3592edd16368SStephen M. Cameron kfree(h->cmd_pool_bits); 3593edd16368SStephen M. Cameron if (h->cmd_pool) 3594edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 3595edd16368SStephen M. Cameron h->nr_cmds * sizeof(struct CommandList), 3596edd16368SStephen M. Cameron h->cmd_pool, h->cmd_pool_dhandle); 3597edd16368SStephen M. Cameron if (h->errinfo_pool) 3598edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 3599edd16368SStephen M. Cameron h->nr_cmds * sizeof(struct ErrorInfo), 3600edd16368SStephen M. Cameron h->errinfo_pool, 3601edd16368SStephen M. Cameron h->errinfo_pool_dhandle); 3602303932fdSDon Brace free_irq(h->intr[PERF_MODE_INT], h); 3603edd16368SStephen M. Cameron clean2: 3604edd16368SStephen M. Cameron clean1: 3605edd16368SStephen M. Cameron h->busy_initializing = 0; 3606edd16368SStephen M. Cameron kfree(h); 3607ecd9aad4SStephen M. Cameron return rc; 3608edd16368SStephen M. Cameron } 3609edd16368SStephen M. Cameron 3610edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h) 3611edd16368SStephen M. Cameron { 3612edd16368SStephen M. Cameron char *flush_buf; 3613edd16368SStephen M. Cameron struct CommandList *c; 3614edd16368SStephen M. Cameron 3615edd16368SStephen M. Cameron flush_buf = kzalloc(4, GFP_KERNEL); 3616edd16368SStephen M. Cameron if (!flush_buf) 3617edd16368SStephen M. Cameron return; 3618edd16368SStephen M. Cameron 3619edd16368SStephen M. Cameron c = cmd_special_alloc(h); 3620edd16368SStephen M. Cameron if (!c) { 3621edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); 3622edd16368SStephen M. Cameron goto out_of_memory; 3623edd16368SStephen M. Cameron } 3624edd16368SStephen M. Cameron fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0, 3625edd16368SStephen M. Cameron RAID_CTLR_LUNID, TYPE_CMD); 3626edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE); 3627edd16368SStephen M. Cameron if (c->err_info->CommandStatus != 0) 3628edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, 3629edd16368SStephen M. Cameron "error flushing cache on controller\n"); 3630edd16368SStephen M. Cameron cmd_special_free(h, c); 3631edd16368SStephen M. Cameron out_of_memory: 3632edd16368SStephen M. Cameron kfree(flush_buf); 3633edd16368SStephen M. Cameron } 3634edd16368SStephen M. Cameron 3635edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev) 3636edd16368SStephen M. Cameron { 3637edd16368SStephen M. Cameron struct ctlr_info *h; 3638edd16368SStephen M. Cameron 3639edd16368SStephen M. Cameron h = pci_get_drvdata(pdev); 3640edd16368SStephen M. Cameron /* Turn board interrupts off and send the flush cache command 3641edd16368SStephen M. Cameron * sendcmd will turn off interrupt, and send the flush... 3642edd16368SStephen M. Cameron * To write all data in the battery backed cache to disks 3643edd16368SStephen M. Cameron */ 3644edd16368SStephen M. Cameron hpsa_flush_cache(h); 3645edd16368SStephen M. Cameron h->access.set_intr_mask(h, HPSA_INTR_OFF); 3646303932fdSDon Brace free_irq(h->intr[PERF_MODE_INT], h); 3647edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI 3648edd16368SStephen M. Cameron if (h->msix_vector) 3649edd16368SStephen M. Cameron pci_disable_msix(h->pdev); 3650edd16368SStephen M. Cameron else if (h->msi_vector) 3651edd16368SStephen M. Cameron pci_disable_msi(h->pdev); 3652edd16368SStephen M. Cameron #endif /* CONFIG_PCI_MSI */ 3653edd16368SStephen M. Cameron } 3654edd16368SStephen M. Cameron 3655edd16368SStephen M. Cameron static void __devexit hpsa_remove_one(struct pci_dev *pdev) 3656edd16368SStephen M. Cameron { 3657edd16368SStephen M. Cameron struct ctlr_info *h; 3658edd16368SStephen M. Cameron 3659edd16368SStephen M. Cameron if (pci_get_drvdata(pdev) == NULL) { 3660edd16368SStephen M. Cameron dev_err(&pdev->dev, "unable to remove device \n"); 3661edd16368SStephen M. Cameron return; 3662edd16368SStephen M. Cameron } 3663edd16368SStephen M. Cameron h = pci_get_drvdata(pdev); 3664edd16368SStephen M. Cameron hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */ 3665edd16368SStephen M. Cameron hpsa_shutdown(pdev); 3666edd16368SStephen M. Cameron iounmap(h->vaddr); 366733a2ffceSStephen M. Cameron hpsa_free_sg_chain_blocks(h); 3668edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 3669edd16368SStephen M. Cameron h->nr_cmds * sizeof(struct CommandList), 3670edd16368SStephen M. Cameron h->cmd_pool, h->cmd_pool_dhandle); 3671edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 3672edd16368SStephen M. Cameron h->nr_cmds * sizeof(struct ErrorInfo), 3673edd16368SStephen M. Cameron h->errinfo_pool, h->errinfo_pool_dhandle); 3674303932fdSDon Brace pci_free_consistent(h->pdev, h->reply_pool_size, 3675303932fdSDon Brace h->reply_pool, h->reply_pool_dhandle); 3676edd16368SStephen M. Cameron kfree(h->cmd_pool_bits); 3677303932fdSDon Brace kfree(h->blockFetchTable); 3678339b2b14SStephen M. Cameron kfree(h->hba_inquiry_data); 3679edd16368SStephen M. Cameron /* 3680edd16368SStephen M. Cameron * Deliberately omit pci_disable_device(): it does something nasty to 3681edd16368SStephen M. Cameron * Smart Array controllers that pci_enable_device does not undo 3682edd16368SStephen M. Cameron */ 3683edd16368SStephen M. Cameron pci_release_regions(pdev); 3684edd16368SStephen M. Cameron pci_set_drvdata(pdev, NULL); 3685edd16368SStephen M. Cameron kfree(h); 3686edd16368SStephen M. Cameron } 3687edd16368SStephen M. Cameron 3688edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev, 3689edd16368SStephen M. Cameron __attribute__((unused)) pm_message_t state) 3690edd16368SStephen M. Cameron { 3691edd16368SStephen M. Cameron return -ENOSYS; 3692edd16368SStephen M. Cameron } 3693edd16368SStephen M. Cameron 3694edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev) 3695edd16368SStephen M. Cameron { 3696edd16368SStephen M. Cameron return -ENOSYS; 3697edd16368SStephen M. Cameron } 3698edd16368SStephen M. Cameron 3699edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = { 3700edd16368SStephen M. Cameron .name = "hpsa", 3701edd16368SStephen M. Cameron .probe = hpsa_init_one, 3702edd16368SStephen M. Cameron .remove = __devexit_p(hpsa_remove_one), 3703edd16368SStephen M. Cameron .id_table = hpsa_pci_device_id, /* id_table */ 3704edd16368SStephen M. Cameron .shutdown = hpsa_shutdown, 3705edd16368SStephen M. Cameron .suspend = hpsa_suspend, 3706edd16368SStephen M. Cameron .resume = hpsa_resume, 3707edd16368SStephen M. Cameron }; 3708edd16368SStephen M. Cameron 3709303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of 3710303932fdSDon Brace * scatter gather elements supported) and bucket[], 3711303932fdSDon Brace * which is an array of 8 integers. The bucket[] array 3712303932fdSDon Brace * contains 8 different DMA transfer sizes (in 16 3713303932fdSDon Brace * byte increments) which the controller uses to fetch 3714303932fdSDon Brace * commands. This function fills in bucket_map[], which 3715303932fdSDon Brace * maps a given number of scatter gather elements to one of 3716303932fdSDon Brace * the 8 DMA transfer sizes. The point of it is to allow the 3717303932fdSDon Brace * controller to only do as much DMA as needed to fetch the 3718303932fdSDon Brace * command, with the DMA transfer size encoded in the lower 3719303932fdSDon Brace * bits of the command address. 3720303932fdSDon Brace */ 3721303932fdSDon Brace static void calc_bucket_map(int bucket[], int num_buckets, 3722303932fdSDon Brace int nsgs, int *bucket_map) 3723303932fdSDon Brace { 3724303932fdSDon Brace int i, j, b, size; 3725303932fdSDon Brace 3726303932fdSDon Brace /* even a command with 0 SGs requires 4 blocks */ 3727303932fdSDon Brace #define MINIMUM_TRANSFER_BLOCKS 4 3728303932fdSDon Brace #define NUM_BUCKETS 8 3729303932fdSDon Brace /* Note, bucket_map must have nsgs+1 entries. */ 3730303932fdSDon Brace for (i = 0; i <= nsgs; i++) { 3731303932fdSDon Brace /* Compute size of a command with i SG entries */ 3732303932fdSDon Brace size = i + MINIMUM_TRANSFER_BLOCKS; 3733303932fdSDon Brace b = num_buckets; /* Assume the biggest bucket */ 3734303932fdSDon Brace /* Find the bucket that is just big enough */ 3735303932fdSDon Brace for (j = 0; j < 8; j++) { 3736303932fdSDon Brace if (bucket[j] >= size) { 3737303932fdSDon Brace b = j; 3738303932fdSDon Brace break; 3739303932fdSDon Brace } 3740303932fdSDon Brace } 3741303932fdSDon Brace /* for a command with i SG entries, use bucket b. */ 3742303932fdSDon Brace bucket_map[i] = b; 3743303932fdSDon Brace } 3744303932fdSDon Brace } 3745303932fdSDon Brace 3746303932fdSDon Brace static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h) 3747303932fdSDon Brace { 3748303932fdSDon Brace u32 trans_support; 3749303932fdSDon Brace u64 trans_offset; 3750303932fdSDon Brace /* 5 = 1 s/g entry or 4k 3751303932fdSDon Brace * 6 = 2 s/g entry or 8k 3752303932fdSDon Brace * 8 = 4 s/g entry or 16k 3753303932fdSDon Brace * 10 = 6 s/g entry or 24k 3754303932fdSDon Brace */ 3755303932fdSDon Brace int bft[8] = {5, 6, 8, 10, 12, 20, 28, 35}; /* for scatter/gathers */ 3756303932fdSDon Brace int i = 0; 3757303932fdSDon Brace int l = 0; 3758303932fdSDon Brace unsigned long register_value; 3759303932fdSDon Brace 3760303932fdSDon Brace trans_support = readl(&(h->cfgtable->TransportSupport)); 3761303932fdSDon Brace if (!(trans_support & PERFORMANT_MODE)) 3762303932fdSDon Brace return; 3763303932fdSDon Brace 3764303932fdSDon Brace h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands)); 3765303932fdSDon Brace h->max_sg_entries = 32; 3766303932fdSDon Brace /* Performant mode ring buffer and supporting data structures */ 3767303932fdSDon Brace h->reply_pool_size = h->max_commands * sizeof(u64); 3768303932fdSDon Brace h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size, 3769303932fdSDon Brace &(h->reply_pool_dhandle)); 3770303932fdSDon Brace 3771303932fdSDon Brace /* Need a block fetch table for performant mode */ 3772303932fdSDon Brace h->blockFetchTable = kmalloc(((h->max_sg_entries+1) * 3773303932fdSDon Brace sizeof(u32)), GFP_KERNEL); 3774303932fdSDon Brace 3775303932fdSDon Brace if ((h->reply_pool == NULL) 3776303932fdSDon Brace || (h->blockFetchTable == NULL)) 3777303932fdSDon Brace goto clean_up; 3778303932fdSDon Brace 3779303932fdSDon Brace h->reply_pool_wraparound = 1; /* spec: init to 1 */ 3780303932fdSDon Brace 3781303932fdSDon Brace /* Controller spec: zero out this buffer. */ 3782303932fdSDon Brace memset(h->reply_pool, 0, h->reply_pool_size); 3783303932fdSDon Brace h->reply_pool_head = h->reply_pool; 3784303932fdSDon Brace 3785303932fdSDon Brace trans_offset = readl(&(h->cfgtable->TransMethodOffset)); 3786303932fdSDon Brace bft[7] = h->max_sg_entries + 4; 3787303932fdSDon Brace calc_bucket_map(bft, ARRAY_SIZE(bft), 32, h->blockFetchTable); 3788303932fdSDon Brace for (i = 0; i < 8; i++) 3789303932fdSDon Brace writel(bft[i], &h->transtable->BlockFetch[i]); 3790303932fdSDon Brace 3791303932fdSDon Brace /* size of controller ring buffer */ 3792303932fdSDon Brace writel(h->max_commands, &h->transtable->RepQSize); 3793303932fdSDon Brace writel(1, &h->transtable->RepQCount); 3794303932fdSDon Brace writel(0, &h->transtable->RepQCtrAddrLow32); 3795303932fdSDon Brace writel(0, &h->transtable->RepQCtrAddrHigh32); 3796303932fdSDon Brace writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32); 3797303932fdSDon Brace writel(0, &h->transtable->RepQAddr0High32); 3798303932fdSDon Brace writel(CFGTBL_Trans_Performant, 3799303932fdSDon Brace &(h->cfgtable->HostWrite.TransportRequest)); 3800303932fdSDon Brace writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL); 3801303932fdSDon Brace /* under certain very rare conditions, this can take awhile. 3802303932fdSDon Brace * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right 3803303932fdSDon Brace * as we enter this code.) */ 3804303932fdSDon Brace for (l = 0; l < MAX_CONFIG_WAIT; l++) { 3805303932fdSDon Brace register_value = readl(h->vaddr + SA5_DOORBELL); 3806303932fdSDon Brace if (!(register_value & CFGTBL_ChangeReq)) 3807303932fdSDon Brace break; 3808303932fdSDon Brace /* delay and try again */ 3809303932fdSDon Brace set_current_state(TASK_INTERRUPTIBLE); 3810303932fdSDon Brace schedule_timeout(10); 3811303932fdSDon Brace } 3812303932fdSDon Brace register_value = readl(&(h->cfgtable->TransportActive)); 3813303932fdSDon Brace if (!(register_value & CFGTBL_Trans_Performant)) { 3814303932fdSDon Brace dev_warn(&h->pdev->dev, "unable to get board into" 3815303932fdSDon Brace " performant mode\n"); 3816303932fdSDon Brace return; 3817303932fdSDon Brace } 3818303932fdSDon Brace 3819303932fdSDon Brace /* Change the access methods to the performant access methods */ 3820303932fdSDon Brace h->access = SA5_performant_access; 3821303932fdSDon Brace h->transMethod = CFGTBL_Trans_Performant; 3822303932fdSDon Brace 3823303932fdSDon Brace return; 3824303932fdSDon Brace 3825303932fdSDon Brace clean_up: 3826303932fdSDon Brace if (h->reply_pool) 3827303932fdSDon Brace pci_free_consistent(h->pdev, h->reply_pool_size, 3828303932fdSDon Brace h->reply_pool, h->reply_pool_dhandle); 3829303932fdSDon Brace kfree(h->blockFetchTable); 3830303932fdSDon Brace } 3831303932fdSDon Brace 3832edd16368SStephen M. Cameron /* 3833edd16368SStephen M. Cameron * This is it. Register the PCI driver information for the cards we control 3834edd16368SStephen M. Cameron * the OS will call our registered routines when it finds one of our cards. 3835edd16368SStephen M. Cameron */ 3836edd16368SStephen M. Cameron static int __init hpsa_init(void) 3837edd16368SStephen M. Cameron { 383831468401SMike Miller return pci_register_driver(&hpsa_pci_driver); 3839edd16368SStephen M. Cameron } 3840edd16368SStephen M. Cameron 3841edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void) 3842edd16368SStephen M. Cameron { 3843edd16368SStephen M. Cameron pci_unregister_driver(&hpsa_pci_driver); 3844edd16368SStephen M. Cameron } 3845edd16368SStephen M. Cameron 3846edd16368SStephen M. Cameron module_init(hpsa_init); 3847edd16368SStephen M. Cameron module_exit(hpsa_cleanup); 3848