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/compat.h> 35edd16368SStephen M. Cameron #include <linux/blktrace_api.h> 36edd16368SStephen M. Cameron #include <linux/uaccess.h> 37edd16368SStephen M. Cameron #include <linux/io.h> 38edd16368SStephen M. Cameron #include <linux/dma-mapping.h> 39edd16368SStephen M. Cameron #include <linux/completion.h> 40edd16368SStephen M. Cameron #include <linux/moduleparam.h> 41edd16368SStephen M. Cameron #include <scsi/scsi.h> 42edd16368SStephen M. Cameron #include <scsi/scsi_cmnd.h> 43edd16368SStephen M. Cameron #include <scsi/scsi_device.h> 44edd16368SStephen M. Cameron #include <scsi/scsi_host.h> 45667e23d4SStephen M. Cameron #include <scsi/scsi_tcq.h> 46edd16368SStephen M. Cameron #include <linux/cciss_ioctl.h> 47edd16368SStephen M. Cameron #include <linux/string.h> 48edd16368SStephen M. Cameron #include <linux/bitmap.h> 49edd16368SStephen M. Cameron #include <asm/atomic.h> 50edd16368SStephen M. Cameron #include <linux/kthread.h> 51edd16368SStephen M. Cameron #include "hpsa_cmd.h" 52edd16368SStephen M. Cameron #include "hpsa.h" 53edd16368SStephen M. Cameron 54edd16368SStephen M. Cameron /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */ 5531468401SMike Miller #define HPSA_DRIVER_VERSION "2.0.2-1" 56edd16368SStephen M. Cameron #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")" 57edd16368SStephen M. Cameron 58edd16368SStephen M. Cameron /* How long to wait (in milliseconds) for board to go into simple mode */ 59edd16368SStephen M. Cameron #define MAX_CONFIG_WAIT 30000 60edd16368SStephen M. Cameron #define MAX_IOCTL_CONFIG_WAIT 1000 61edd16368SStephen M. Cameron 62edd16368SStephen M. Cameron /*define how many times we will try a command because of bus resets */ 63edd16368SStephen M. Cameron #define MAX_CMD_RETRIES 3 64edd16368SStephen M. Cameron 65edd16368SStephen M. Cameron /* Embedded module documentation macros - see modules.h */ 66edd16368SStephen M. Cameron MODULE_AUTHOR("Hewlett-Packard Company"); 67edd16368SStephen M. Cameron MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \ 68edd16368SStephen M. Cameron HPSA_DRIVER_VERSION); 69edd16368SStephen M. Cameron MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers"); 70edd16368SStephen M. Cameron MODULE_VERSION(HPSA_DRIVER_VERSION); 71edd16368SStephen M. Cameron MODULE_LICENSE("GPL"); 72edd16368SStephen M. Cameron 73edd16368SStephen M. Cameron static int hpsa_allow_any; 74edd16368SStephen M. Cameron module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR); 75edd16368SStephen M. Cameron MODULE_PARM_DESC(hpsa_allow_any, 76edd16368SStephen M. Cameron "Allow hpsa driver to access unknown HP Smart Array hardware"); 77edd16368SStephen M. Cameron 78edd16368SStephen M. Cameron /* define the PCI info for the cards we can control */ 79edd16368SStephen M. Cameron static const struct pci_device_id hpsa_pci_device_id[] = { 80edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241}, 81edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243}, 82edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245}, 83edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247}, 84edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249}, 85edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a}, 86edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b}, 87f8b01eb9SMike Miller {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233}, 882e931f31SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3250}, 892e931f31SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3251}, 902e931f31SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3252}, 912e931f31SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3253}, 922e931f31SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3254}, 93edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 94edd16368SStephen M. Cameron PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0}, 95edd16368SStephen M. Cameron {0,} 96edd16368SStephen M. Cameron }; 97edd16368SStephen M. Cameron 98edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id); 99edd16368SStephen M. Cameron 100edd16368SStephen M. Cameron /* board_id = Subsystem Device ID & Vendor ID 101edd16368SStephen M. Cameron * product = Marketing Name for the board 102edd16368SStephen M. Cameron * access = Address of the struct of function pointers 103edd16368SStephen M. Cameron */ 104edd16368SStephen M. Cameron static struct board_type products[] = { 105edd16368SStephen M. Cameron {0x3241103C, "Smart Array P212", &SA5_access}, 106edd16368SStephen M. Cameron {0x3243103C, "Smart Array P410", &SA5_access}, 107edd16368SStephen M. Cameron {0x3245103C, "Smart Array P410i", &SA5_access}, 108edd16368SStephen M. Cameron {0x3247103C, "Smart Array P411", &SA5_access}, 109edd16368SStephen M. Cameron {0x3249103C, "Smart Array P812", &SA5_access}, 110edd16368SStephen M. Cameron {0x324a103C, "Smart Array P712m", &SA5_access}, 111edd16368SStephen M. Cameron {0x324b103C, "Smart Array P711m", &SA5_access}, 1122e931f31SStephen M. Cameron {0x3250103C, "Smart Array", &SA5_access}, 1132e931f31SStephen M. Cameron {0x3250113C, "Smart Array", &SA5_access}, 1142e931f31SStephen M. Cameron {0x3250123C, "Smart Array", &SA5_access}, 1152e931f31SStephen M. Cameron {0x3250133C, "Smart Array", &SA5_access}, 1162e931f31SStephen M. Cameron {0x3250143C, "Smart Array", &SA5_access}, 117edd16368SStephen M. Cameron {0xFFFF103C, "Unknown Smart Array", &SA5_access}, 118edd16368SStephen M. Cameron }; 119edd16368SStephen M. Cameron 120edd16368SStephen M. Cameron static int number_of_controllers; 121edd16368SStephen M. Cameron 12210f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id); 12310f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id); 124edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg); 125edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h); 126edd16368SStephen M. Cameron 127edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT 128edd16368SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg); 129edd16368SStephen M. Cameron #endif 130edd16368SStephen M. Cameron 131edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c); 132edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c); 133edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h); 134edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h); 13501a02ffcSStephen M. Cameron static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h, 13601a02ffcSStephen M. Cameron void *buff, size_t size, u8 page_code, unsigned char *scsi3addr, 137edd16368SStephen M. Cameron int cmd_type); 138edd16368SStephen M. Cameron 139f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd); 140a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *); 141a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh, 142a08a8471SStephen M. Cameron unsigned long elapsed_time); 143667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev, 144667e23d4SStephen M. Cameron int qdepth, int reason); 145edd16368SStephen M. Cameron 146edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd); 147edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev); 148edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev); 149edd16368SStephen M. Cameron 150edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev, 151edd16368SStephen M. Cameron struct device_attribute *attr, char *buf); 152edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev, 153edd16368SStephen M. Cameron struct device_attribute *attr, char *buf); 154edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev, 155edd16368SStephen M. Cameron struct device_attribute *attr, char *buf); 156d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev, 157d28ce020SStephen M. Cameron struct device_attribute *attr, char *buf); 158edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno); 159edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev, 160edd16368SStephen M. Cameron struct device_attribute *attr, const char *buf, size_t count); 161edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h, 162edd16368SStephen M. Cameron struct CommandList *c); 163edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h, 164edd16368SStephen M. Cameron struct CommandList *c); 165303932fdSDon Brace /* performant mode helper functions */ 166303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets, 167303932fdSDon Brace int nsgs, int *bucket_map); 1687136f9a7SStephen M. Cameron static __devinit void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h); 169303932fdSDon Brace static inline u32 next_command(struct ctlr_info *h); 1701df8552aSStephen M. Cameron static int __devinit hpsa_find_cfg_addrs(struct pci_dev *pdev, 1711df8552aSStephen M. Cameron void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index, 1721df8552aSStephen M. Cameron u64 *cfg_offset); 1731df8552aSStephen M. Cameron static int __devinit hpsa_pci_find_memory_BAR(struct pci_dev *pdev, 1741df8552aSStephen M. Cameron unsigned long *memory_bar); 17518867659SStephen M. Cameron static int __devinit hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id); 176*fe5389c8SStephen M. Cameron static int __devinit hpsa_wait_for_board_state(struct pci_dev *pdev, 177*fe5389c8SStephen M. Cameron void __iomem *vaddr, int wait_for_ready); 178*fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0 179*fe5389c8SStephen M. Cameron #define BOARD_READY 1 180edd16368SStephen M. Cameron 181edd16368SStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL); 182edd16368SStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL); 183edd16368SStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL); 184edd16368SStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan); 185d28ce020SStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO, 186d28ce020SStephen M. Cameron host_show_firmware_revision, NULL); 187edd16368SStephen M. Cameron 188edd16368SStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = { 189edd16368SStephen M. Cameron &dev_attr_raid_level, 190edd16368SStephen M. Cameron &dev_attr_lunid, 191edd16368SStephen M. Cameron &dev_attr_unique_id, 192edd16368SStephen M. Cameron NULL, 193edd16368SStephen M. Cameron }; 194edd16368SStephen M. Cameron 195edd16368SStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = { 196edd16368SStephen M. Cameron &dev_attr_rescan, 197d28ce020SStephen M. Cameron &dev_attr_firmware_revision, 198edd16368SStephen M. Cameron NULL, 199edd16368SStephen M. Cameron }; 200edd16368SStephen M. Cameron 201edd16368SStephen M. Cameron static struct scsi_host_template hpsa_driver_template = { 202edd16368SStephen M. Cameron .module = THIS_MODULE, 203edd16368SStephen M. Cameron .name = "hpsa", 204edd16368SStephen M. Cameron .proc_name = "hpsa", 205edd16368SStephen M. Cameron .queuecommand = hpsa_scsi_queue_command, 206a08a8471SStephen M. Cameron .scan_start = hpsa_scan_start, 207a08a8471SStephen M. Cameron .scan_finished = hpsa_scan_finished, 208667e23d4SStephen M. Cameron .change_queue_depth = hpsa_change_queue_depth, 209edd16368SStephen M. Cameron .this_id = -1, 210edd16368SStephen M. Cameron .use_clustering = ENABLE_CLUSTERING, 211edd16368SStephen M. Cameron .eh_device_reset_handler = hpsa_eh_device_reset_handler, 212edd16368SStephen M. Cameron .ioctl = hpsa_ioctl, 213edd16368SStephen M. Cameron .slave_alloc = hpsa_slave_alloc, 214edd16368SStephen M. Cameron .slave_destroy = hpsa_slave_destroy, 215edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT 216edd16368SStephen M. Cameron .compat_ioctl = hpsa_compat_ioctl, 217edd16368SStephen M. Cameron #endif 218edd16368SStephen M. Cameron .sdev_attrs = hpsa_sdev_attrs, 219edd16368SStephen M. Cameron .shost_attrs = hpsa_shost_attrs, 220edd16368SStephen M. Cameron }; 221edd16368SStephen M. Cameron 222edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev) 223edd16368SStephen M. Cameron { 224edd16368SStephen M. Cameron unsigned long *priv = shost_priv(sdev->host); 225edd16368SStephen M. Cameron return (struct ctlr_info *) *priv; 226edd16368SStephen M. Cameron } 227edd16368SStephen M. Cameron 228a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh) 229a23513e8SStephen M. Cameron { 230a23513e8SStephen M. Cameron unsigned long *priv = shost_priv(sh); 231a23513e8SStephen M. Cameron return (struct ctlr_info *) *priv; 232a23513e8SStephen M. Cameron } 233a23513e8SStephen M. Cameron 234edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h, 235edd16368SStephen M. Cameron struct CommandList *c) 236edd16368SStephen M. Cameron { 237edd16368SStephen M. Cameron if (c->err_info->SenseInfo[2] != UNIT_ATTENTION) 238edd16368SStephen M. Cameron return 0; 239edd16368SStephen M. Cameron 240edd16368SStephen M. Cameron switch (c->err_info->SenseInfo[12]) { 241edd16368SStephen M. Cameron case STATE_CHANGED: 242edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: a state change " 243edd16368SStephen M. Cameron "detected, command retried\n", h->ctlr); 244edd16368SStephen M. Cameron break; 245edd16368SStephen M. Cameron case LUN_FAILED: 246edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: LUN failure " 247edd16368SStephen M. Cameron "detected, action required\n", h->ctlr); 248edd16368SStephen M. Cameron break; 249edd16368SStephen M. Cameron case REPORT_LUNS_CHANGED: 250edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: report LUN data " 25131468401SMike Miller "changed, action required\n", h->ctlr); 252edd16368SStephen M. Cameron /* 253edd16368SStephen M. Cameron * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012. 254edd16368SStephen M. Cameron */ 255edd16368SStephen M. Cameron break; 256edd16368SStephen M. Cameron case POWER_OR_RESET: 257edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: a power on " 258edd16368SStephen M. Cameron "or device reset detected\n", h->ctlr); 259edd16368SStephen M. Cameron break; 260edd16368SStephen M. Cameron case UNIT_ATTENTION_CLEARED: 261edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: unit attention " 262edd16368SStephen M. Cameron "cleared by another initiator\n", h->ctlr); 263edd16368SStephen M. Cameron break; 264edd16368SStephen M. Cameron default: 265edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: unknown " 266edd16368SStephen M. Cameron "unit attention detected\n", h->ctlr); 267edd16368SStephen M. Cameron break; 268edd16368SStephen M. Cameron } 269edd16368SStephen M. Cameron return 1; 270edd16368SStephen M. Cameron } 271edd16368SStephen M. Cameron 272edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev, 273edd16368SStephen M. Cameron struct device_attribute *attr, 274edd16368SStephen M. Cameron const char *buf, size_t count) 275edd16368SStephen M. Cameron { 276edd16368SStephen M. Cameron struct ctlr_info *h; 277edd16368SStephen M. Cameron struct Scsi_Host *shost = class_to_shost(dev); 278a23513e8SStephen M. Cameron h = shost_to_hba(shost); 27931468401SMike Miller hpsa_scan_start(h->scsi_host); 280edd16368SStephen M. Cameron return count; 281edd16368SStephen M. Cameron } 282edd16368SStephen M. Cameron 283d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev, 284d28ce020SStephen M. Cameron struct device_attribute *attr, char *buf) 285d28ce020SStephen M. Cameron { 286d28ce020SStephen M. Cameron struct ctlr_info *h; 287d28ce020SStephen M. Cameron struct Scsi_Host *shost = class_to_shost(dev); 288d28ce020SStephen M. Cameron unsigned char *fwrev; 289d28ce020SStephen M. Cameron 290d28ce020SStephen M. Cameron h = shost_to_hba(shost); 291d28ce020SStephen M. Cameron if (!h->hba_inquiry_data) 292d28ce020SStephen M. Cameron return 0; 293d28ce020SStephen M. Cameron fwrev = &h->hba_inquiry_data[32]; 294d28ce020SStephen M. Cameron return snprintf(buf, 20, "%c%c%c%c\n", 295d28ce020SStephen M. Cameron fwrev[0], fwrev[1], fwrev[2], fwrev[3]); 296d28ce020SStephen M. Cameron } 297d28ce020SStephen M. Cameron 298edd16368SStephen M. Cameron /* Enqueuing and dequeuing functions for cmdlists. */ 299edd16368SStephen M. Cameron static inline void addQ(struct hlist_head *list, struct CommandList *c) 300edd16368SStephen M. Cameron { 301edd16368SStephen M. Cameron hlist_add_head(&c->list, list); 302edd16368SStephen M. Cameron } 303edd16368SStephen M. Cameron 304303932fdSDon Brace static inline u32 next_command(struct ctlr_info *h) 305303932fdSDon Brace { 306303932fdSDon Brace u32 a; 307303932fdSDon Brace 308303932fdSDon Brace if (unlikely(h->transMethod != CFGTBL_Trans_Performant)) 309303932fdSDon Brace return h->access.command_completed(h); 310303932fdSDon Brace 311303932fdSDon Brace if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) { 312303932fdSDon Brace a = *(h->reply_pool_head); /* Next cmd in ring buffer */ 313303932fdSDon Brace (h->reply_pool_head)++; 314303932fdSDon Brace h->commands_outstanding--; 315303932fdSDon Brace } else { 316303932fdSDon Brace a = FIFO_EMPTY; 317303932fdSDon Brace } 318303932fdSDon Brace /* Check for wraparound */ 319303932fdSDon Brace if (h->reply_pool_head == (h->reply_pool + h->max_commands)) { 320303932fdSDon Brace h->reply_pool_head = h->reply_pool; 321303932fdSDon Brace h->reply_pool_wraparound ^= 1; 322303932fdSDon Brace } 323303932fdSDon Brace return a; 324303932fdSDon Brace } 325303932fdSDon Brace 326303932fdSDon Brace /* set_performant_mode: Modify the tag for cciss performant 327303932fdSDon Brace * set bit 0 for pull model, bits 3-1 for block fetch 328303932fdSDon Brace * register number 329303932fdSDon Brace */ 330303932fdSDon Brace static void set_performant_mode(struct ctlr_info *h, struct CommandList *c) 331303932fdSDon Brace { 332303932fdSDon Brace if (likely(h->transMethod == CFGTBL_Trans_Performant)) 333303932fdSDon Brace c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1); 334303932fdSDon Brace } 335303932fdSDon Brace 336edd16368SStephen M. Cameron static void enqueue_cmd_and_start_io(struct ctlr_info *h, 337edd16368SStephen M. Cameron struct CommandList *c) 338edd16368SStephen M. Cameron { 339edd16368SStephen M. Cameron unsigned long flags; 340303932fdSDon Brace 341303932fdSDon Brace set_performant_mode(h, c); 342edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 343edd16368SStephen M. Cameron addQ(&h->reqQ, c); 344edd16368SStephen M. Cameron h->Qdepth++; 345edd16368SStephen M. Cameron start_io(h); 346edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 347edd16368SStephen M. Cameron } 348edd16368SStephen M. Cameron 349edd16368SStephen M. Cameron static inline void removeQ(struct CommandList *c) 350edd16368SStephen M. Cameron { 351edd16368SStephen M. Cameron if (WARN_ON(hlist_unhashed(&c->list))) 352edd16368SStephen M. Cameron return; 353edd16368SStephen M. Cameron hlist_del_init(&c->list); 354edd16368SStephen M. Cameron } 355edd16368SStephen M. Cameron 356edd16368SStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[]) 357edd16368SStephen M. Cameron { 358edd16368SStephen M. Cameron return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0; 359edd16368SStephen M. Cameron } 360edd16368SStephen M. Cameron 361edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[]) 362edd16368SStephen M. Cameron { 363edd16368SStephen M. Cameron return (scsi3addr[3] & 0xC0) == 0x40; 364edd16368SStephen M. Cameron } 365edd16368SStephen M. Cameron 366339b2b14SStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h) 367339b2b14SStephen M. Cameron { 368339b2b14SStephen M. Cameron if (!h->hba_inquiry_data) 369339b2b14SStephen M. Cameron return 0; 370339b2b14SStephen M. Cameron if ((h->hba_inquiry_data[2] & 0x07) == 5) 371339b2b14SStephen M. Cameron return 1; 372339b2b14SStephen M. Cameron return 0; 373339b2b14SStephen M. Cameron } 374339b2b14SStephen M. Cameron 375edd16368SStephen M. Cameron static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG", 376edd16368SStephen M. Cameron "UNKNOWN" 377edd16368SStephen M. Cameron }; 378edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1) 379edd16368SStephen M. Cameron 380edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev, 381edd16368SStephen M. Cameron struct device_attribute *attr, char *buf) 382edd16368SStephen M. Cameron { 383edd16368SStephen M. Cameron ssize_t l = 0; 38482a72c0aSStephen M. Cameron unsigned char rlevel; 385edd16368SStephen M. Cameron struct ctlr_info *h; 386edd16368SStephen M. Cameron struct scsi_device *sdev; 387edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *hdev; 388edd16368SStephen M. Cameron unsigned long flags; 389edd16368SStephen M. Cameron 390edd16368SStephen M. Cameron sdev = to_scsi_device(dev); 391edd16368SStephen M. Cameron h = sdev_to_hba(sdev); 392edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 393edd16368SStephen M. Cameron hdev = sdev->hostdata; 394edd16368SStephen M. Cameron if (!hdev) { 395edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 396edd16368SStephen M. Cameron return -ENODEV; 397edd16368SStephen M. Cameron } 398edd16368SStephen M. Cameron 399edd16368SStephen M. Cameron /* Is this even a logical drive? */ 400edd16368SStephen M. Cameron if (!is_logical_dev_addr_mode(hdev->scsi3addr)) { 401edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 402edd16368SStephen M. Cameron l = snprintf(buf, PAGE_SIZE, "N/A\n"); 403edd16368SStephen M. Cameron return l; 404edd16368SStephen M. Cameron } 405edd16368SStephen M. Cameron 406edd16368SStephen M. Cameron rlevel = hdev->raid_level; 407edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 40882a72c0aSStephen M. Cameron if (rlevel > RAID_UNKNOWN) 409edd16368SStephen M. Cameron rlevel = RAID_UNKNOWN; 410edd16368SStephen M. Cameron l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]); 411edd16368SStephen M. Cameron return l; 412edd16368SStephen M. Cameron } 413edd16368SStephen M. Cameron 414edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev, 415edd16368SStephen M. Cameron struct device_attribute *attr, char *buf) 416edd16368SStephen M. Cameron { 417edd16368SStephen M. Cameron struct ctlr_info *h; 418edd16368SStephen M. Cameron struct scsi_device *sdev; 419edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *hdev; 420edd16368SStephen M. Cameron unsigned long flags; 421edd16368SStephen M. Cameron unsigned char lunid[8]; 422edd16368SStephen M. Cameron 423edd16368SStephen M. Cameron sdev = to_scsi_device(dev); 424edd16368SStephen M. Cameron h = sdev_to_hba(sdev); 425edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 426edd16368SStephen M. Cameron hdev = sdev->hostdata; 427edd16368SStephen M. Cameron if (!hdev) { 428edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 429edd16368SStephen M. Cameron return -ENODEV; 430edd16368SStephen M. Cameron } 431edd16368SStephen M. Cameron memcpy(lunid, hdev->scsi3addr, sizeof(lunid)); 432edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 433edd16368SStephen M. Cameron return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n", 434edd16368SStephen M. Cameron lunid[0], lunid[1], lunid[2], lunid[3], 435edd16368SStephen M. Cameron lunid[4], lunid[5], lunid[6], lunid[7]); 436edd16368SStephen M. Cameron } 437edd16368SStephen M. Cameron 438edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev, 439edd16368SStephen M. Cameron struct device_attribute *attr, char *buf) 440edd16368SStephen M. Cameron { 441edd16368SStephen M. Cameron struct ctlr_info *h; 442edd16368SStephen M. Cameron struct scsi_device *sdev; 443edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *hdev; 444edd16368SStephen M. Cameron unsigned long flags; 445edd16368SStephen M. Cameron unsigned char sn[16]; 446edd16368SStephen M. Cameron 447edd16368SStephen M. Cameron sdev = to_scsi_device(dev); 448edd16368SStephen M. Cameron h = sdev_to_hba(sdev); 449edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 450edd16368SStephen M. Cameron hdev = sdev->hostdata; 451edd16368SStephen M. Cameron if (!hdev) { 452edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 453edd16368SStephen M. Cameron return -ENODEV; 454edd16368SStephen M. Cameron } 455edd16368SStephen M. Cameron memcpy(sn, hdev->device_id, sizeof(sn)); 456edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 457edd16368SStephen M. Cameron return snprintf(buf, 16 * 2 + 2, 458edd16368SStephen M. Cameron "%02X%02X%02X%02X%02X%02X%02X%02X" 459edd16368SStephen M. Cameron "%02X%02X%02X%02X%02X%02X%02X%02X\n", 460edd16368SStephen M. Cameron sn[0], sn[1], sn[2], sn[3], 461edd16368SStephen M. Cameron sn[4], sn[5], sn[6], sn[7], 462edd16368SStephen M. Cameron sn[8], sn[9], sn[10], sn[11], 463edd16368SStephen M. Cameron sn[12], sn[13], sn[14], sn[15]); 464edd16368SStephen M. Cameron } 465edd16368SStephen M. Cameron 466edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h, 467edd16368SStephen M. Cameron unsigned char scsi3addr[], int bus, int *target, int *lun) 468edd16368SStephen M. Cameron { 469edd16368SStephen M. Cameron /* finds an unused bus, target, lun for a new physical device 470edd16368SStephen M. Cameron * assumes h->devlock is held 471edd16368SStephen M. Cameron */ 472edd16368SStephen M. Cameron int i, found = 0; 473edd16368SStephen M. Cameron DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA); 474edd16368SStephen M. Cameron 475edd16368SStephen M. Cameron memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3); 476edd16368SStephen M. Cameron 477edd16368SStephen M. Cameron for (i = 0; i < h->ndevices; i++) { 478edd16368SStephen M. Cameron if (h->dev[i]->bus == bus && h->dev[i]->target != -1) 479edd16368SStephen M. Cameron set_bit(h->dev[i]->target, lun_taken); 480edd16368SStephen M. Cameron } 481edd16368SStephen M. Cameron 482edd16368SStephen M. Cameron for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) { 483edd16368SStephen M. Cameron if (!test_bit(i, lun_taken)) { 484edd16368SStephen M. Cameron /* *bus = 1; */ 485edd16368SStephen M. Cameron *target = i; 486edd16368SStephen M. Cameron *lun = 0; 487edd16368SStephen M. Cameron found = 1; 488edd16368SStephen M. Cameron break; 489edd16368SStephen M. Cameron } 490edd16368SStephen M. Cameron } 491edd16368SStephen M. Cameron return !found; 492edd16368SStephen M. Cameron } 493edd16368SStephen M. Cameron 494edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */ 495edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno, 496edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *device, 497edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *added[], int *nadded) 498edd16368SStephen M. Cameron { 499edd16368SStephen M. Cameron /* assumes h->devlock is held */ 500edd16368SStephen M. Cameron int n = h->ndevices; 501edd16368SStephen M. Cameron int i; 502edd16368SStephen M. Cameron unsigned char addr1[8], addr2[8]; 503edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd; 504edd16368SStephen M. Cameron 505edd16368SStephen M. Cameron if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) { 506edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "too many devices, some will be " 507edd16368SStephen M. Cameron "inaccessible.\n"); 508edd16368SStephen M. Cameron return -1; 509edd16368SStephen M. Cameron } 510edd16368SStephen M. Cameron 511edd16368SStephen M. Cameron /* physical devices do not have lun or target assigned until now. */ 512edd16368SStephen M. Cameron if (device->lun != -1) 513edd16368SStephen M. Cameron /* Logical device, lun is already assigned. */ 514edd16368SStephen M. Cameron goto lun_assigned; 515edd16368SStephen M. Cameron 516edd16368SStephen M. Cameron /* If this device a non-zero lun of a multi-lun device 517edd16368SStephen M. Cameron * byte 4 of the 8-byte LUN addr will contain the logical 518edd16368SStephen M. Cameron * unit no, zero otherise. 519edd16368SStephen M. Cameron */ 520edd16368SStephen M. Cameron if (device->scsi3addr[4] == 0) { 521edd16368SStephen M. Cameron /* This is not a non-zero lun of a multi-lun device */ 522edd16368SStephen M. Cameron if (hpsa_find_target_lun(h, device->scsi3addr, 523edd16368SStephen M. Cameron device->bus, &device->target, &device->lun) != 0) 524edd16368SStephen M. Cameron return -1; 525edd16368SStephen M. Cameron goto lun_assigned; 526edd16368SStephen M. Cameron } 527edd16368SStephen M. Cameron 528edd16368SStephen M. Cameron /* This is a non-zero lun of a multi-lun device. 529edd16368SStephen M. Cameron * Search through our list and find the device which 530edd16368SStephen M. Cameron * has the same 8 byte LUN address, excepting byte 4. 531edd16368SStephen M. Cameron * Assign the same bus and target for this new LUN. 532edd16368SStephen M. Cameron * Use the logical unit number from the firmware. 533edd16368SStephen M. Cameron */ 534edd16368SStephen M. Cameron memcpy(addr1, device->scsi3addr, 8); 535edd16368SStephen M. Cameron addr1[4] = 0; 536edd16368SStephen M. Cameron for (i = 0; i < n; i++) { 537edd16368SStephen M. Cameron sd = h->dev[i]; 538edd16368SStephen M. Cameron memcpy(addr2, sd->scsi3addr, 8); 539edd16368SStephen M. Cameron addr2[4] = 0; 540edd16368SStephen M. Cameron /* differ only in byte 4? */ 541edd16368SStephen M. Cameron if (memcmp(addr1, addr2, 8) == 0) { 542edd16368SStephen M. Cameron device->bus = sd->bus; 543edd16368SStephen M. Cameron device->target = sd->target; 544edd16368SStephen M. Cameron device->lun = device->scsi3addr[4]; 545edd16368SStephen M. Cameron break; 546edd16368SStephen M. Cameron } 547edd16368SStephen M. Cameron } 548edd16368SStephen M. Cameron if (device->lun == -1) { 549edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "physical device with no LUN=0," 550edd16368SStephen M. Cameron " suspect firmware bug or unsupported hardware " 551edd16368SStephen M. Cameron "configuration.\n"); 552edd16368SStephen M. Cameron return -1; 553edd16368SStephen M. Cameron } 554edd16368SStephen M. Cameron 555edd16368SStephen M. Cameron lun_assigned: 556edd16368SStephen M. Cameron 557edd16368SStephen M. Cameron h->dev[n] = device; 558edd16368SStephen M. Cameron h->ndevices++; 559edd16368SStephen M. Cameron added[*nadded] = device; 560edd16368SStephen M. Cameron (*nadded)++; 561edd16368SStephen M. Cameron 562edd16368SStephen M. Cameron /* initially, (before registering with scsi layer) we don't 563edd16368SStephen M. Cameron * know our hostno and we don't want to print anything first 564edd16368SStephen M. Cameron * time anyway (the scsi layer's inquiries will show that info) 565edd16368SStephen M. Cameron */ 566edd16368SStephen M. Cameron /* if (hostno != -1) */ 567edd16368SStephen M. Cameron dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n", 568edd16368SStephen M. Cameron scsi_device_type(device->devtype), hostno, 569edd16368SStephen M. Cameron device->bus, device->target, device->lun); 570edd16368SStephen M. Cameron return 0; 571edd16368SStephen M. Cameron } 572edd16368SStephen M. Cameron 5732a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */ 5742a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno, 5752a8ccf31SStephen M. Cameron int entry, struct hpsa_scsi_dev_t *new_entry, 5762a8ccf31SStephen M. Cameron struct hpsa_scsi_dev_t *added[], int *nadded, 5772a8ccf31SStephen M. Cameron struct hpsa_scsi_dev_t *removed[], int *nremoved) 5782a8ccf31SStephen M. Cameron { 5792a8ccf31SStephen M. Cameron /* assumes h->devlock is held */ 5802a8ccf31SStephen M. Cameron BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA); 5812a8ccf31SStephen M. Cameron removed[*nremoved] = h->dev[entry]; 5822a8ccf31SStephen M. Cameron (*nremoved)++; 5832a8ccf31SStephen M. Cameron h->dev[entry] = new_entry; 5842a8ccf31SStephen M. Cameron added[*nadded] = new_entry; 5852a8ccf31SStephen M. Cameron (*nadded)++; 5862a8ccf31SStephen M. Cameron dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n", 5872a8ccf31SStephen M. Cameron scsi_device_type(new_entry->devtype), hostno, new_entry->bus, 5882a8ccf31SStephen M. Cameron new_entry->target, new_entry->lun); 5892a8ccf31SStephen M. Cameron } 5902a8ccf31SStephen M. Cameron 591edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */ 592edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry, 593edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *removed[], int *nremoved) 594edd16368SStephen M. Cameron { 595edd16368SStephen M. Cameron /* assumes h->devlock is held */ 596edd16368SStephen M. Cameron int i; 597edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd; 598edd16368SStephen M. Cameron 599b2ed4f79SStephen M. Cameron BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA); 600edd16368SStephen M. Cameron 601edd16368SStephen M. Cameron sd = h->dev[entry]; 602edd16368SStephen M. Cameron removed[*nremoved] = h->dev[entry]; 603edd16368SStephen M. Cameron (*nremoved)++; 604edd16368SStephen M. Cameron 605edd16368SStephen M. Cameron for (i = entry; i < h->ndevices-1; i++) 606edd16368SStephen M. Cameron h->dev[i] = h->dev[i+1]; 607edd16368SStephen M. Cameron h->ndevices--; 608edd16368SStephen M. Cameron dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n", 609edd16368SStephen M. Cameron scsi_device_type(sd->devtype), hostno, sd->bus, sd->target, 610edd16368SStephen M. Cameron sd->lun); 611edd16368SStephen M. Cameron } 612edd16368SStephen M. Cameron 613edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \ 614edd16368SStephen M. Cameron (a)[7] == (b)[7] && \ 615edd16368SStephen M. Cameron (a)[6] == (b)[6] && \ 616edd16368SStephen M. Cameron (a)[5] == (b)[5] && \ 617edd16368SStephen M. Cameron (a)[4] == (b)[4] && \ 618edd16368SStephen M. Cameron (a)[3] == (b)[3] && \ 619edd16368SStephen M. Cameron (a)[2] == (b)[2] && \ 620edd16368SStephen M. Cameron (a)[1] == (b)[1] && \ 621edd16368SStephen M. Cameron (a)[0] == (b)[0]) 622edd16368SStephen M. Cameron 623edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h, 624edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *added) 625edd16368SStephen M. Cameron { 626edd16368SStephen M. Cameron /* called when scsi_add_device fails in order to re-adjust 627edd16368SStephen M. Cameron * h->dev[] to match the mid layer's view. 628edd16368SStephen M. Cameron */ 629edd16368SStephen M. Cameron unsigned long flags; 630edd16368SStephen M. Cameron int i, j; 631edd16368SStephen M. Cameron 632edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 633edd16368SStephen M. Cameron for (i = 0; i < h->ndevices; i++) { 634edd16368SStephen M. Cameron if (h->dev[i] == added) { 635edd16368SStephen M. Cameron for (j = i; j < h->ndevices-1; j++) 636edd16368SStephen M. Cameron h->dev[j] = h->dev[j+1]; 637edd16368SStephen M. Cameron h->ndevices--; 638edd16368SStephen M. Cameron break; 639edd16368SStephen M. Cameron } 640edd16368SStephen M. Cameron } 641edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 642edd16368SStephen M. Cameron kfree(added); 643edd16368SStephen M. Cameron } 644edd16368SStephen M. Cameron 645edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1, 646edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *dev2) 647edd16368SStephen M. Cameron { 648edd16368SStephen M. Cameron /* we compare everything except lun and target as these 649edd16368SStephen M. Cameron * are not yet assigned. Compare parts likely 650edd16368SStephen M. Cameron * to differ first 651edd16368SStephen M. Cameron */ 652edd16368SStephen M. Cameron if (memcmp(dev1->scsi3addr, dev2->scsi3addr, 653edd16368SStephen M. Cameron sizeof(dev1->scsi3addr)) != 0) 654edd16368SStephen M. Cameron return 0; 655edd16368SStephen M. Cameron if (memcmp(dev1->device_id, dev2->device_id, 656edd16368SStephen M. Cameron sizeof(dev1->device_id)) != 0) 657edd16368SStephen M. Cameron return 0; 658edd16368SStephen M. Cameron if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0) 659edd16368SStephen M. Cameron return 0; 660edd16368SStephen M. Cameron if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0) 661edd16368SStephen M. Cameron return 0; 662edd16368SStephen M. Cameron if (dev1->devtype != dev2->devtype) 663edd16368SStephen M. Cameron return 0; 664edd16368SStephen M. Cameron if (dev1->bus != dev2->bus) 665edd16368SStephen M. Cameron return 0; 666edd16368SStephen M. Cameron return 1; 667edd16368SStephen M. Cameron } 668edd16368SStephen M. Cameron 669edd16368SStephen M. Cameron /* Find needle in haystack. If exact match found, return DEVICE_SAME, 670edd16368SStephen M. Cameron * and return needle location in *index. If scsi3addr matches, but not 671edd16368SStephen M. Cameron * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle 672edd16368SStephen M. Cameron * location in *index. If needle not found, return DEVICE_NOT_FOUND. 673edd16368SStephen M. Cameron */ 674edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle, 675edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *haystack[], int haystack_size, 676edd16368SStephen M. Cameron int *index) 677edd16368SStephen M. Cameron { 678edd16368SStephen M. Cameron int i; 679edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0 680edd16368SStephen M. Cameron #define DEVICE_CHANGED 1 681edd16368SStephen M. Cameron #define DEVICE_SAME 2 682edd16368SStephen M. Cameron for (i = 0; i < haystack_size; i++) { 68323231048SStephen M. Cameron if (haystack[i] == NULL) /* previously removed. */ 68423231048SStephen M. Cameron continue; 685edd16368SStephen M. Cameron if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) { 686edd16368SStephen M. Cameron *index = i; 687edd16368SStephen M. Cameron if (device_is_the_same(needle, haystack[i])) 688edd16368SStephen M. Cameron return DEVICE_SAME; 689edd16368SStephen M. Cameron else 690edd16368SStephen M. Cameron return DEVICE_CHANGED; 691edd16368SStephen M. Cameron } 692edd16368SStephen M. Cameron } 693edd16368SStephen M. Cameron *index = -1; 694edd16368SStephen M. Cameron return DEVICE_NOT_FOUND; 695edd16368SStephen M. Cameron } 696edd16368SStephen M. Cameron 6974967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno, 698edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd[], int nsds) 699edd16368SStephen M. Cameron { 700edd16368SStephen M. Cameron /* sd contains scsi3 addresses and devtypes, and inquiry 701edd16368SStephen M. Cameron * data. This function takes what's in sd to be the current 702edd16368SStephen M. Cameron * reality and updates h->dev[] to reflect that reality. 703edd16368SStephen M. Cameron */ 704edd16368SStephen M. Cameron int i, entry, device_change, changes = 0; 705edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *csd; 706edd16368SStephen M. Cameron unsigned long flags; 707edd16368SStephen M. Cameron struct hpsa_scsi_dev_t **added, **removed; 708edd16368SStephen M. Cameron int nadded, nremoved; 709edd16368SStephen M. Cameron struct Scsi_Host *sh = NULL; 710edd16368SStephen M. Cameron 711edd16368SStephen M. Cameron added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA, 712edd16368SStephen M. Cameron GFP_KERNEL); 713edd16368SStephen M. Cameron removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA, 714edd16368SStephen M. Cameron GFP_KERNEL); 715edd16368SStephen M. Cameron 716edd16368SStephen M. Cameron if (!added || !removed) { 717edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "out of memory in " 718edd16368SStephen M. Cameron "adjust_hpsa_scsi_table\n"); 719edd16368SStephen M. Cameron goto free_and_out; 720edd16368SStephen M. Cameron } 721edd16368SStephen M. Cameron 722edd16368SStephen M. Cameron spin_lock_irqsave(&h->devlock, flags); 723edd16368SStephen M. Cameron 724edd16368SStephen M. Cameron /* find any devices in h->dev[] that are not in 725edd16368SStephen M. Cameron * sd[] and remove them from h->dev[], and for any 726edd16368SStephen M. Cameron * devices which have changed, remove the old device 727edd16368SStephen M. Cameron * info and add the new device info. 728edd16368SStephen M. Cameron */ 729edd16368SStephen M. Cameron i = 0; 730edd16368SStephen M. Cameron nremoved = 0; 731edd16368SStephen M. Cameron nadded = 0; 732edd16368SStephen M. Cameron while (i < h->ndevices) { 733edd16368SStephen M. Cameron csd = h->dev[i]; 734edd16368SStephen M. Cameron device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry); 735edd16368SStephen M. Cameron if (device_change == DEVICE_NOT_FOUND) { 736edd16368SStephen M. Cameron changes++; 737edd16368SStephen M. Cameron hpsa_scsi_remove_entry(h, hostno, i, 738edd16368SStephen M. Cameron removed, &nremoved); 739edd16368SStephen M. Cameron continue; /* remove ^^^, hence i not incremented */ 740edd16368SStephen M. Cameron } else if (device_change == DEVICE_CHANGED) { 741edd16368SStephen M. Cameron changes++; 7422a8ccf31SStephen M. Cameron hpsa_scsi_replace_entry(h, hostno, i, sd[entry], 7432a8ccf31SStephen M. Cameron added, &nadded, removed, &nremoved); 744c7f172dcSStephen M. Cameron /* Set it to NULL to prevent it from being freed 745c7f172dcSStephen M. Cameron * at the bottom of hpsa_update_scsi_devices() 746c7f172dcSStephen M. Cameron */ 747c7f172dcSStephen M. Cameron sd[entry] = NULL; 748edd16368SStephen M. Cameron } 749edd16368SStephen M. Cameron i++; 750edd16368SStephen M. Cameron } 751edd16368SStephen M. Cameron 752edd16368SStephen M. Cameron /* Now, make sure every device listed in sd[] is also 753edd16368SStephen M. Cameron * listed in h->dev[], adding them if they aren't found 754edd16368SStephen M. Cameron */ 755edd16368SStephen M. Cameron 756edd16368SStephen M. Cameron for (i = 0; i < nsds; i++) { 757edd16368SStephen M. Cameron if (!sd[i]) /* if already added above. */ 758edd16368SStephen M. Cameron continue; 759edd16368SStephen M. Cameron device_change = hpsa_scsi_find_entry(sd[i], h->dev, 760edd16368SStephen M. Cameron h->ndevices, &entry); 761edd16368SStephen M. Cameron if (device_change == DEVICE_NOT_FOUND) { 762edd16368SStephen M. Cameron changes++; 763edd16368SStephen M. Cameron if (hpsa_scsi_add_entry(h, hostno, sd[i], 764edd16368SStephen M. Cameron added, &nadded) != 0) 765edd16368SStephen M. Cameron break; 766edd16368SStephen M. Cameron sd[i] = NULL; /* prevent from being freed later. */ 767edd16368SStephen M. Cameron } else if (device_change == DEVICE_CHANGED) { 768edd16368SStephen M. Cameron /* should never happen... */ 769edd16368SStephen M. Cameron changes++; 770edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, 771edd16368SStephen M. Cameron "device unexpectedly changed.\n"); 772edd16368SStephen M. Cameron /* but if it does happen, we just ignore that device */ 773edd16368SStephen M. Cameron } 774edd16368SStephen M. Cameron } 775edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->devlock, flags); 776edd16368SStephen M. Cameron 777edd16368SStephen M. Cameron /* Don't notify scsi mid layer of any changes the first time through 778edd16368SStephen M. Cameron * (or if there are no changes) scsi_scan_host will do it later the 779edd16368SStephen M. Cameron * first time through. 780edd16368SStephen M. Cameron */ 781edd16368SStephen M. Cameron if (hostno == -1 || !changes) 782edd16368SStephen M. Cameron goto free_and_out; 783edd16368SStephen M. Cameron 784edd16368SStephen M. Cameron sh = h->scsi_host; 785edd16368SStephen M. Cameron /* Notify scsi mid layer of any removed devices */ 786edd16368SStephen M. Cameron for (i = 0; i < nremoved; i++) { 787edd16368SStephen M. Cameron struct scsi_device *sdev = 788edd16368SStephen M. Cameron scsi_device_lookup(sh, removed[i]->bus, 789edd16368SStephen M. Cameron removed[i]->target, removed[i]->lun); 790edd16368SStephen M. Cameron if (sdev != NULL) { 791edd16368SStephen M. Cameron scsi_remove_device(sdev); 792edd16368SStephen M. Cameron scsi_device_put(sdev); 793edd16368SStephen M. Cameron } else { 794edd16368SStephen M. Cameron /* We don't expect to get here. 795edd16368SStephen M. Cameron * future cmds to this device will get selection 796edd16368SStephen M. Cameron * timeout as if the device was gone. 797edd16368SStephen M. Cameron */ 798edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d " 799edd16368SStephen M. Cameron " for removal.", hostno, removed[i]->bus, 800edd16368SStephen M. Cameron removed[i]->target, removed[i]->lun); 801edd16368SStephen M. Cameron } 802edd16368SStephen M. Cameron kfree(removed[i]); 803edd16368SStephen M. Cameron removed[i] = NULL; 804edd16368SStephen M. Cameron } 805edd16368SStephen M. Cameron 806edd16368SStephen M. Cameron /* Notify scsi mid layer of any added devices */ 807edd16368SStephen M. Cameron for (i = 0; i < nadded; i++) { 808edd16368SStephen M. Cameron if (scsi_add_device(sh, added[i]->bus, 809edd16368SStephen M. Cameron added[i]->target, added[i]->lun) == 0) 810edd16368SStephen M. Cameron continue; 811edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, " 812edd16368SStephen M. Cameron "device not added.\n", hostno, added[i]->bus, 813edd16368SStephen M. Cameron added[i]->target, added[i]->lun); 814edd16368SStephen M. Cameron /* now we have to remove it from h->dev, 815edd16368SStephen M. Cameron * since it didn't get added to scsi mid layer 816edd16368SStephen M. Cameron */ 817edd16368SStephen M. Cameron fixup_botched_add(h, added[i]); 818edd16368SStephen M. Cameron } 819edd16368SStephen M. Cameron 820edd16368SStephen M. Cameron free_and_out: 821edd16368SStephen M. Cameron kfree(added); 822edd16368SStephen M. Cameron kfree(removed); 823edd16368SStephen M. Cameron } 824edd16368SStephen M. Cameron 825edd16368SStephen M. Cameron /* 826edd16368SStephen M. Cameron * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t * 827edd16368SStephen M. Cameron * Assume's h->devlock is held. 828edd16368SStephen M. Cameron */ 829edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h, 830edd16368SStephen M. Cameron int bus, int target, int lun) 831edd16368SStephen M. Cameron { 832edd16368SStephen M. Cameron int i; 833edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd; 834edd16368SStephen M. Cameron 835edd16368SStephen M. Cameron for (i = 0; i < h->ndevices; i++) { 836edd16368SStephen M. Cameron sd = h->dev[i]; 837edd16368SStephen M. Cameron if (sd->bus == bus && sd->target == target && sd->lun == lun) 838edd16368SStephen M. Cameron return sd; 839edd16368SStephen M. Cameron } 840edd16368SStephen M. Cameron return NULL; 841edd16368SStephen M. Cameron } 842edd16368SStephen M. Cameron 843edd16368SStephen M. Cameron /* link sdev->hostdata to our per-device structure. */ 844edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev) 845edd16368SStephen M. Cameron { 846edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd; 847edd16368SStephen M. Cameron unsigned long flags; 848edd16368SStephen M. Cameron struct ctlr_info *h; 849edd16368SStephen M. Cameron 850edd16368SStephen M. Cameron h = sdev_to_hba(sdev); 851edd16368SStephen M. Cameron spin_lock_irqsave(&h->devlock, flags); 852edd16368SStephen M. Cameron sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev), 853edd16368SStephen M. Cameron sdev_id(sdev), sdev->lun); 854edd16368SStephen M. Cameron if (sd != NULL) 855edd16368SStephen M. Cameron sdev->hostdata = sd; 856edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->devlock, flags); 857edd16368SStephen M. Cameron return 0; 858edd16368SStephen M. Cameron } 859edd16368SStephen M. Cameron 860edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev) 861edd16368SStephen M. Cameron { 862bcc44255SStephen M. Cameron /* nothing to do. */ 863edd16368SStephen M. Cameron } 864edd16368SStephen M. Cameron 865edd16368SStephen M. Cameron static void hpsa_scsi_setup(struct ctlr_info *h) 866edd16368SStephen M. Cameron { 867edd16368SStephen M. Cameron h->ndevices = 0; 868edd16368SStephen M. Cameron h->scsi_host = NULL; 869edd16368SStephen M. Cameron spin_lock_init(&h->devlock); 870edd16368SStephen M. Cameron } 871edd16368SStephen M. Cameron 87233a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h) 87333a2ffceSStephen M. Cameron { 87433a2ffceSStephen M. Cameron int i; 87533a2ffceSStephen M. Cameron 87633a2ffceSStephen M. Cameron if (!h->cmd_sg_list) 87733a2ffceSStephen M. Cameron return; 87833a2ffceSStephen M. Cameron for (i = 0; i < h->nr_cmds; i++) { 87933a2ffceSStephen M. Cameron kfree(h->cmd_sg_list[i]); 88033a2ffceSStephen M. Cameron h->cmd_sg_list[i] = NULL; 88133a2ffceSStephen M. Cameron } 88233a2ffceSStephen M. Cameron kfree(h->cmd_sg_list); 88333a2ffceSStephen M. Cameron h->cmd_sg_list = NULL; 88433a2ffceSStephen M. Cameron } 88533a2ffceSStephen M. Cameron 88633a2ffceSStephen M. Cameron static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h) 88733a2ffceSStephen M. Cameron { 88833a2ffceSStephen M. Cameron int i; 88933a2ffceSStephen M. Cameron 89033a2ffceSStephen M. Cameron if (h->chainsize <= 0) 89133a2ffceSStephen M. Cameron return 0; 89233a2ffceSStephen M. Cameron 89333a2ffceSStephen M. Cameron h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds, 89433a2ffceSStephen M. Cameron GFP_KERNEL); 89533a2ffceSStephen M. Cameron if (!h->cmd_sg_list) 89633a2ffceSStephen M. Cameron return -ENOMEM; 89733a2ffceSStephen M. Cameron for (i = 0; i < h->nr_cmds; i++) { 89833a2ffceSStephen M. Cameron h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) * 89933a2ffceSStephen M. Cameron h->chainsize, GFP_KERNEL); 90033a2ffceSStephen M. Cameron if (!h->cmd_sg_list[i]) 90133a2ffceSStephen M. Cameron goto clean; 90233a2ffceSStephen M. Cameron } 90333a2ffceSStephen M. Cameron return 0; 90433a2ffceSStephen M. Cameron 90533a2ffceSStephen M. Cameron clean: 90633a2ffceSStephen M. Cameron hpsa_free_sg_chain_blocks(h); 90733a2ffceSStephen M. Cameron return -ENOMEM; 90833a2ffceSStephen M. Cameron } 90933a2ffceSStephen M. Cameron 91033a2ffceSStephen M. Cameron static void hpsa_map_sg_chain_block(struct ctlr_info *h, 91133a2ffceSStephen M. Cameron struct CommandList *c) 91233a2ffceSStephen M. Cameron { 91333a2ffceSStephen M. Cameron struct SGDescriptor *chain_sg, *chain_block; 91433a2ffceSStephen M. Cameron u64 temp64; 91533a2ffceSStephen M. Cameron 91633a2ffceSStephen M. Cameron chain_sg = &c->SG[h->max_cmd_sg_entries - 1]; 91733a2ffceSStephen M. Cameron chain_block = h->cmd_sg_list[c->cmdindex]; 91833a2ffceSStephen M. Cameron chain_sg->Ext = HPSA_SG_CHAIN; 91933a2ffceSStephen M. Cameron chain_sg->Len = sizeof(*chain_sg) * 92033a2ffceSStephen M. Cameron (c->Header.SGTotal - h->max_cmd_sg_entries); 92133a2ffceSStephen M. Cameron temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len, 92233a2ffceSStephen M. Cameron PCI_DMA_TODEVICE); 92333a2ffceSStephen M. Cameron chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL); 92433a2ffceSStephen M. Cameron chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL); 92533a2ffceSStephen M. Cameron } 92633a2ffceSStephen M. Cameron 92733a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h, 92833a2ffceSStephen M. Cameron struct CommandList *c) 92933a2ffceSStephen M. Cameron { 93033a2ffceSStephen M. Cameron struct SGDescriptor *chain_sg; 93133a2ffceSStephen M. Cameron union u64bit temp64; 93233a2ffceSStephen M. Cameron 93333a2ffceSStephen M. Cameron if (c->Header.SGTotal <= h->max_cmd_sg_entries) 93433a2ffceSStephen M. Cameron return; 93533a2ffceSStephen M. Cameron 93633a2ffceSStephen M. Cameron chain_sg = &c->SG[h->max_cmd_sg_entries - 1]; 93733a2ffceSStephen M. Cameron temp64.val32.lower = chain_sg->Addr.lower; 93833a2ffceSStephen M. Cameron temp64.val32.upper = chain_sg->Addr.upper; 93933a2ffceSStephen M. Cameron pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE); 94033a2ffceSStephen M. Cameron } 94133a2ffceSStephen M. Cameron 942edd16368SStephen M. Cameron static void complete_scsi_command(struct CommandList *cp, 94301a02ffcSStephen M. Cameron int timeout, u32 tag) 944edd16368SStephen M. Cameron { 945edd16368SStephen M. Cameron struct scsi_cmnd *cmd; 946edd16368SStephen M. Cameron struct ctlr_info *h; 947edd16368SStephen M. Cameron struct ErrorInfo *ei; 948edd16368SStephen M. Cameron 949edd16368SStephen M. Cameron unsigned char sense_key; 950edd16368SStephen M. Cameron unsigned char asc; /* additional sense code */ 951edd16368SStephen M. Cameron unsigned char ascq; /* additional sense code qualifier */ 952edd16368SStephen M. Cameron 953edd16368SStephen M. Cameron ei = cp->err_info; 954edd16368SStephen M. Cameron cmd = (struct scsi_cmnd *) cp->scsi_cmd; 955edd16368SStephen M. Cameron h = cp->h; 956edd16368SStephen M. Cameron 957edd16368SStephen M. Cameron scsi_dma_unmap(cmd); /* undo the DMA mappings */ 95833a2ffceSStephen M. Cameron if (cp->Header.SGTotal > h->max_cmd_sg_entries) 95933a2ffceSStephen M. Cameron hpsa_unmap_sg_chain_block(h, cp); 960edd16368SStephen M. Cameron 961edd16368SStephen M. Cameron cmd->result = (DID_OK << 16); /* host byte */ 962edd16368SStephen M. Cameron cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */ 9635512672fSStephen M. Cameron cmd->result |= ei->ScsiStatus; 964edd16368SStephen M. Cameron 965edd16368SStephen M. Cameron /* copy the sense data whether we need to or not. */ 966edd16368SStephen M. Cameron memcpy(cmd->sense_buffer, ei->SenseInfo, 967edd16368SStephen M. Cameron ei->SenseLen > SCSI_SENSE_BUFFERSIZE ? 968edd16368SStephen M. Cameron SCSI_SENSE_BUFFERSIZE : 969edd16368SStephen M. Cameron ei->SenseLen); 970edd16368SStephen M. Cameron scsi_set_resid(cmd, ei->ResidualCnt); 971edd16368SStephen M. Cameron 972edd16368SStephen M. Cameron if (ei->CommandStatus == 0) { 973edd16368SStephen M. Cameron cmd->scsi_done(cmd); 974edd16368SStephen M. Cameron cmd_free(h, cp); 975edd16368SStephen M. Cameron return; 976edd16368SStephen M. Cameron } 977edd16368SStephen M. Cameron 978edd16368SStephen M. Cameron /* an error has occurred */ 979edd16368SStephen M. Cameron switch (ei->CommandStatus) { 980edd16368SStephen M. Cameron 981edd16368SStephen M. Cameron case CMD_TARGET_STATUS: 982edd16368SStephen M. Cameron if (ei->ScsiStatus) { 983edd16368SStephen M. Cameron /* Get sense key */ 984edd16368SStephen M. Cameron sense_key = 0xf & ei->SenseInfo[2]; 985edd16368SStephen M. Cameron /* Get additional sense code */ 986edd16368SStephen M. Cameron asc = ei->SenseInfo[12]; 987edd16368SStephen M. Cameron /* Get addition sense code qualifier */ 988edd16368SStephen M. Cameron ascq = ei->SenseInfo[13]; 989edd16368SStephen M. Cameron } 990edd16368SStephen M. Cameron 991edd16368SStephen M. Cameron if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) { 992edd16368SStephen M. Cameron if (check_for_unit_attention(h, cp)) { 993edd16368SStephen M. Cameron cmd->result = DID_SOFT_ERROR << 16; 994edd16368SStephen M. Cameron break; 995edd16368SStephen M. Cameron } 996edd16368SStephen M. Cameron if (sense_key == ILLEGAL_REQUEST) { 997edd16368SStephen M. Cameron /* 998edd16368SStephen M. Cameron * SCSI REPORT_LUNS is commonly unsupported on 999edd16368SStephen M. Cameron * Smart Array. Suppress noisy complaint. 1000edd16368SStephen M. Cameron */ 1001edd16368SStephen M. Cameron if (cp->Request.CDB[0] == REPORT_LUNS) 1002edd16368SStephen M. Cameron break; 1003edd16368SStephen M. Cameron 1004edd16368SStephen M. Cameron /* If ASC/ASCQ indicate Logical Unit 1005edd16368SStephen M. Cameron * Not Supported condition, 1006edd16368SStephen M. Cameron */ 1007edd16368SStephen M. Cameron if ((asc == 0x25) && (ascq == 0x0)) { 1008edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p " 1009edd16368SStephen M. Cameron "has check condition\n", cp); 1010edd16368SStephen M. Cameron break; 1011edd16368SStephen M. Cameron } 1012edd16368SStephen M. Cameron } 1013edd16368SStephen M. Cameron 1014edd16368SStephen M. Cameron if (sense_key == NOT_READY) { 1015edd16368SStephen M. Cameron /* If Sense is Not Ready, Logical Unit 1016edd16368SStephen M. Cameron * Not ready, Manual Intervention 1017edd16368SStephen M. Cameron * required 1018edd16368SStephen M. Cameron */ 1019edd16368SStephen M. Cameron if ((asc == 0x04) && (ascq == 0x03)) { 1020edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p " 1021edd16368SStephen M. Cameron "has check condition: unit " 1022edd16368SStephen M. Cameron "not ready, manual " 1023edd16368SStephen M. Cameron "intervention required\n", cp); 1024edd16368SStephen M. Cameron break; 1025edd16368SStephen M. Cameron } 1026edd16368SStephen M. Cameron } 10271d3b3609SMatt Gates if (sense_key == ABORTED_COMMAND) { 10281d3b3609SMatt Gates /* Aborted command is retryable */ 10291d3b3609SMatt Gates dev_warn(&h->pdev->dev, "cp %p " 10301d3b3609SMatt Gates "has check condition: aborted command: " 10311d3b3609SMatt Gates "ASC: 0x%x, ASCQ: 0x%x\n", 10321d3b3609SMatt Gates cp, asc, ascq); 10331d3b3609SMatt Gates cmd->result = DID_SOFT_ERROR << 16; 10341d3b3609SMatt Gates break; 10351d3b3609SMatt Gates } 1036edd16368SStephen M. Cameron /* Must be some other type of check condition */ 1037edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p has check condition: " 1038edd16368SStephen M. Cameron "unknown type: " 1039edd16368SStephen M. Cameron "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, " 1040edd16368SStephen M. Cameron "Returning result: 0x%x, " 1041edd16368SStephen M. Cameron "cmd=[%02x %02x %02x %02x %02x " 1042807be732SMike Miller "%02x %02x %02x %02x %02x %02x " 1043edd16368SStephen M. Cameron "%02x %02x %02x %02x %02x]\n", 1044edd16368SStephen M. Cameron cp, sense_key, asc, ascq, 1045edd16368SStephen M. Cameron cmd->result, 1046edd16368SStephen M. Cameron cmd->cmnd[0], cmd->cmnd[1], 1047edd16368SStephen M. Cameron cmd->cmnd[2], cmd->cmnd[3], 1048edd16368SStephen M. Cameron cmd->cmnd[4], cmd->cmnd[5], 1049edd16368SStephen M. Cameron cmd->cmnd[6], cmd->cmnd[7], 1050807be732SMike Miller cmd->cmnd[8], cmd->cmnd[9], 1051807be732SMike Miller cmd->cmnd[10], cmd->cmnd[11], 1052807be732SMike Miller cmd->cmnd[12], cmd->cmnd[13], 1053807be732SMike Miller cmd->cmnd[14], cmd->cmnd[15]); 1054edd16368SStephen M. Cameron break; 1055edd16368SStephen M. Cameron } 1056edd16368SStephen M. Cameron 1057edd16368SStephen M. Cameron 1058edd16368SStephen M. Cameron /* Problem was not a check condition 1059edd16368SStephen M. Cameron * Pass it up to the upper layers... 1060edd16368SStephen M. Cameron */ 1061edd16368SStephen M. Cameron if (ei->ScsiStatus) { 1062edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p has status 0x%x " 1063edd16368SStephen M. Cameron "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, " 1064edd16368SStephen M. Cameron "Returning result: 0x%x\n", 1065edd16368SStephen M. Cameron cp, ei->ScsiStatus, 1066edd16368SStephen M. Cameron sense_key, asc, ascq, 1067edd16368SStephen M. Cameron cmd->result); 1068edd16368SStephen M. Cameron } else { /* scsi status is zero??? How??? */ 1069edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. " 1070edd16368SStephen M. Cameron "Returning no connection.\n", cp), 1071edd16368SStephen M. Cameron 1072edd16368SStephen M. Cameron /* Ordinarily, this case should never happen, 1073edd16368SStephen M. Cameron * but there is a bug in some released firmware 1074edd16368SStephen M. Cameron * revisions that allows it to happen if, for 1075edd16368SStephen M. Cameron * example, a 4100 backplane loses power and 1076edd16368SStephen M. Cameron * the tape drive is in it. We assume that 1077edd16368SStephen M. Cameron * it's a fatal error of some kind because we 1078edd16368SStephen M. Cameron * can't show that it wasn't. We will make it 1079edd16368SStephen M. Cameron * look like selection timeout since that is 1080edd16368SStephen M. Cameron * the most common reason for this to occur, 1081edd16368SStephen M. Cameron * and it's severe enough. 1082edd16368SStephen M. Cameron */ 1083edd16368SStephen M. Cameron 1084edd16368SStephen M. Cameron cmd->result = DID_NO_CONNECT << 16; 1085edd16368SStephen M. Cameron } 1086edd16368SStephen M. Cameron break; 1087edd16368SStephen M. Cameron 1088edd16368SStephen M. Cameron case CMD_DATA_UNDERRUN: /* let mid layer handle it. */ 1089edd16368SStephen M. Cameron break; 1090edd16368SStephen M. Cameron case CMD_DATA_OVERRUN: 1091edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p has" 1092edd16368SStephen M. Cameron " completed with data overrun " 1093edd16368SStephen M. Cameron "reported\n", cp); 1094edd16368SStephen M. Cameron break; 1095edd16368SStephen M. Cameron case CMD_INVALID: { 1096edd16368SStephen M. Cameron /* print_bytes(cp, sizeof(*cp), 1, 0); 1097edd16368SStephen M. Cameron print_cmd(cp); */ 1098edd16368SStephen M. Cameron /* We get CMD_INVALID if you address a non-existent device 1099edd16368SStephen M. Cameron * instead of a selection timeout (no response). You will 1100edd16368SStephen M. Cameron * see this if you yank out a drive, then try to access it. 1101edd16368SStephen M. Cameron * This is kind of a shame because it means that any other 1102edd16368SStephen M. Cameron * CMD_INVALID (e.g. driver bug) will get interpreted as a 1103edd16368SStephen M. Cameron * missing target. */ 1104edd16368SStephen M. Cameron cmd->result = DID_NO_CONNECT << 16; 1105edd16368SStephen M. Cameron } 1106edd16368SStephen M. Cameron break; 1107edd16368SStephen M. Cameron case CMD_PROTOCOL_ERR: 1108edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p has " 1109edd16368SStephen M. Cameron "protocol error \n", cp); 1110edd16368SStephen M. Cameron break; 1111edd16368SStephen M. Cameron case CMD_HARDWARE_ERR: 1112edd16368SStephen M. Cameron cmd->result = DID_ERROR << 16; 1113edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp); 1114edd16368SStephen M. Cameron break; 1115edd16368SStephen M. Cameron case CMD_CONNECTION_LOST: 1116edd16368SStephen M. Cameron cmd->result = DID_ERROR << 16; 1117edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp); 1118edd16368SStephen M. Cameron break; 1119edd16368SStephen M. Cameron case CMD_ABORTED: 1120edd16368SStephen M. Cameron cmd->result = DID_ABORT << 16; 1121edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n", 1122edd16368SStephen M. Cameron cp, ei->ScsiStatus); 1123edd16368SStephen M. Cameron break; 1124edd16368SStephen M. Cameron case CMD_ABORT_FAILED: 1125edd16368SStephen M. Cameron cmd->result = DID_ERROR << 16; 1126edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp); 1127edd16368SStephen M. Cameron break; 1128edd16368SStephen M. Cameron case CMD_UNSOLICITED_ABORT: 11295f0325abSMatt Gates cmd->result = DID_RESET << 16; 1130edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited " 1131edd16368SStephen M. Cameron "abort\n", cp); 1132edd16368SStephen M. Cameron break; 1133edd16368SStephen M. Cameron case CMD_TIMEOUT: 1134edd16368SStephen M. Cameron cmd->result = DID_TIME_OUT << 16; 1135edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p timedout\n", cp); 1136edd16368SStephen M. Cameron break; 1137edd16368SStephen M. Cameron default: 1138edd16368SStephen M. Cameron cmd->result = DID_ERROR << 16; 1139edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n", 1140edd16368SStephen M. Cameron cp, ei->CommandStatus); 1141edd16368SStephen M. Cameron } 1142edd16368SStephen M. Cameron cmd->scsi_done(cmd); 1143edd16368SStephen M. Cameron cmd_free(h, cp); 1144edd16368SStephen M. Cameron } 1145edd16368SStephen M. Cameron 1146edd16368SStephen M. Cameron static int hpsa_scsi_detect(struct ctlr_info *h) 1147edd16368SStephen M. Cameron { 1148edd16368SStephen M. Cameron struct Scsi_Host *sh; 1149edd16368SStephen M. Cameron int error; 1150edd16368SStephen M. Cameron 1151edd16368SStephen M. Cameron sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h)); 1152edd16368SStephen M. Cameron if (sh == NULL) 1153edd16368SStephen M. Cameron goto fail; 1154edd16368SStephen M. Cameron 1155edd16368SStephen M. Cameron sh->io_port = 0; 1156edd16368SStephen M. Cameron sh->n_io_port = 0; 1157edd16368SStephen M. Cameron sh->this_id = -1; 1158edd16368SStephen M. Cameron sh->max_channel = 3; 1159edd16368SStephen M. Cameron sh->max_cmd_len = MAX_COMMAND_SIZE; 1160edd16368SStephen M. Cameron sh->max_lun = HPSA_MAX_LUN; 1161edd16368SStephen M. Cameron sh->max_id = HPSA_MAX_LUN; 1162303932fdSDon Brace sh->can_queue = h->nr_cmds; 1163303932fdSDon Brace sh->cmd_per_lun = h->nr_cmds; 116433a2ffceSStephen M. Cameron sh->sg_tablesize = h->maxsgentries; 1165edd16368SStephen M. Cameron h->scsi_host = sh; 1166edd16368SStephen M. Cameron sh->hostdata[0] = (unsigned long) h; 1167303932fdSDon Brace sh->irq = h->intr[PERF_MODE_INT]; 1168edd16368SStephen M. Cameron sh->unique_id = sh->irq; 1169edd16368SStephen M. Cameron error = scsi_add_host(sh, &h->pdev->dev); 1170edd16368SStephen M. Cameron if (error) 1171edd16368SStephen M. Cameron goto fail_host_put; 1172edd16368SStephen M. Cameron scsi_scan_host(sh); 1173edd16368SStephen M. Cameron return 0; 1174edd16368SStephen M. Cameron 1175edd16368SStephen M. Cameron fail_host_put: 1176edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host" 1177edd16368SStephen M. Cameron " failed for controller %d\n", h->ctlr); 1178edd16368SStephen M. Cameron scsi_host_put(sh); 1179ecd9aad4SStephen M. Cameron return error; 1180edd16368SStephen M. Cameron fail: 1181edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc" 1182edd16368SStephen M. Cameron " failed for controller %d\n", h->ctlr); 1183ecd9aad4SStephen M. Cameron return -ENOMEM; 1184edd16368SStephen M. Cameron } 1185edd16368SStephen M. Cameron 1186edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev, 1187edd16368SStephen M. Cameron struct CommandList *c, int sg_used, int data_direction) 1188edd16368SStephen M. Cameron { 1189edd16368SStephen M. Cameron int i; 1190edd16368SStephen M. Cameron union u64bit addr64; 1191edd16368SStephen M. Cameron 1192edd16368SStephen M. Cameron for (i = 0; i < sg_used; i++) { 1193edd16368SStephen M. Cameron addr64.val32.lower = c->SG[i].Addr.lower; 1194edd16368SStephen M. Cameron addr64.val32.upper = c->SG[i].Addr.upper; 1195edd16368SStephen M. Cameron pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len, 1196edd16368SStephen M. Cameron data_direction); 1197edd16368SStephen M. Cameron } 1198edd16368SStephen M. Cameron } 1199edd16368SStephen M. Cameron 1200edd16368SStephen M. Cameron static void hpsa_map_one(struct pci_dev *pdev, 1201edd16368SStephen M. Cameron struct CommandList *cp, 1202edd16368SStephen M. Cameron unsigned char *buf, 1203edd16368SStephen M. Cameron size_t buflen, 1204edd16368SStephen M. Cameron int data_direction) 1205edd16368SStephen M. Cameron { 120601a02ffcSStephen M. Cameron u64 addr64; 1207edd16368SStephen M. Cameron 1208edd16368SStephen M. Cameron if (buflen == 0 || data_direction == PCI_DMA_NONE) { 1209edd16368SStephen M. Cameron cp->Header.SGList = 0; 1210edd16368SStephen M. Cameron cp->Header.SGTotal = 0; 1211edd16368SStephen M. Cameron return; 1212edd16368SStephen M. Cameron } 1213edd16368SStephen M. Cameron 121401a02ffcSStephen M. Cameron addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction); 1215edd16368SStephen M. Cameron cp->SG[0].Addr.lower = 121601a02ffcSStephen M. Cameron (u32) (addr64 & (u64) 0x00000000FFFFFFFF); 1217edd16368SStephen M. Cameron cp->SG[0].Addr.upper = 121801a02ffcSStephen M. Cameron (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF); 1219edd16368SStephen M. Cameron cp->SG[0].Len = buflen; 122001a02ffcSStephen M. Cameron cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */ 122101a02ffcSStephen M. Cameron cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */ 1222edd16368SStephen M. Cameron } 1223edd16368SStephen M. Cameron 1224edd16368SStephen M. Cameron static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h, 1225edd16368SStephen M. Cameron struct CommandList *c) 1226edd16368SStephen M. Cameron { 1227edd16368SStephen M. Cameron DECLARE_COMPLETION_ONSTACK(wait); 1228edd16368SStephen M. Cameron 1229edd16368SStephen M. Cameron c->waiting = &wait; 1230edd16368SStephen M. Cameron enqueue_cmd_and_start_io(h, c); 1231edd16368SStephen M. Cameron wait_for_completion(&wait); 1232edd16368SStephen M. Cameron } 1233edd16368SStephen M. Cameron 1234edd16368SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h, 1235edd16368SStephen M. Cameron struct CommandList *c, int data_direction) 1236edd16368SStephen M. Cameron { 1237edd16368SStephen M. Cameron int retry_count = 0; 1238edd16368SStephen M. Cameron 1239edd16368SStephen M. Cameron do { 1240edd16368SStephen M. Cameron memset(c->err_info, 0, sizeof(c->err_info)); 1241edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 1242edd16368SStephen M. Cameron retry_count++; 1243edd16368SStephen M. Cameron } while (check_for_unit_attention(h, c) && retry_count <= 3); 1244edd16368SStephen M. Cameron hpsa_pci_unmap(h->pdev, c, 1, data_direction); 1245edd16368SStephen M. Cameron } 1246edd16368SStephen M. Cameron 1247edd16368SStephen M. Cameron static void hpsa_scsi_interpret_error(struct CommandList *cp) 1248edd16368SStephen M. Cameron { 1249edd16368SStephen M. Cameron struct ErrorInfo *ei; 1250edd16368SStephen M. Cameron struct device *d = &cp->h->pdev->dev; 1251edd16368SStephen M. Cameron 1252edd16368SStephen M. Cameron ei = cp->err_info; 1253edd16368SStephen M. Cameron switch (ei->CommandStatus) { 1254edd16368SStephen M. Cameron case CMD_TARGET_STATUS: 1255edd16368SStephen M. Cameron dev_warn(d, "cmd %p has completed with errors\n", cp); 1256edd16368SStephen M. Cameron dev_warn(d, "cmd %p has SCSI Status = %x\n", cp, 1257edd16368SStephen M. Cameron ei->ScsiStatus); 1258edd16368SStephen M. Cameron if (ei->ScsiStatus == 0) 1259edd16368SStephen M. Cameron dev_warn(d, "SCSI status is abnormally zero. " 1260edd16368SStephen M. Cameron "(probably indicates selection timeout " 1261edd16368SStephen M. Cameron "reported incorrectly due to a known " 1262edd16368SStephen M. Cameron "firmware bug, circa July, 2001.)\n"); 1263edd16368SStephen M. Cameron break; 1264edd16368SStephen M. Cameron case CMD_DATA_UNDERRUN: /* let mid layer handle it. */ 1265edd16368SStephen M. Cameron dev_info(d, "UNDERRUN\n"); 1266edd16368SStephen M. Cameron break; 1267edd16368SStephen M. Cameron case CMD_DATA_OVERRUN: 1268edd16368SStephen M. Cameron dev_warn(d, "cp %p has completed with data overrun\n", cp); 1269edd16368SStephen M. Cameron break; 1270edd16368SStephen M. Cameron case CMD_INVALID: { 1271edd16368SStephen M. Cameron /* controller unfortunately reports SCSI passthru's 1272edd16368SStephen M. Cameron * to non-existent targets as invalid commands. 1273edd16368SStephen M. Cameron */ 1274edd16368SStephen M. Cameron dev_warn(d, "cp %p is reported invalid (probably means " 1275edd16368SStephen M. Cameron "target device no longer present)\n", cp); 1276edd16368SStephen M. Cameron /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0); 1277edd16368SStephen M. Cameron print_cmd(cp); */ 1278edd16368SStephen M. Cameron } 1279edd16368SStephen M. Cameron break; 1280edd16368SStephen M. Cameron case CMD_PROTOCOL_ERR: 1281edd16368SStephen M. Cameron dev_warn(d, "cp %p has protocol error \n", cp); 1282edd16368SStephen M. Cameron break; 1283edd16368SStephen M. Cameron case CMD_HARDWARE_ERR: 1284edd16368SStephen M. Cameron /* cmd->result = DID_ERROR << 16; */ 1285edd16368SStephen M. Cameron dev_warn(d, "cp %p had hardware error\n", cp); 1286edd16368SStephen M. Cameron break; 1287edd16368SStephen M. Cameron case CMD_CONNECTION_LOST: 1288edd16368SStephen M. Cameron dev_warn(d, "cp %p had connection lost\n", cp); 1289edd16368SStephen M. Cameron break; 1290edd16368SStephen M. Cameron case CMD_ABORTED: 1291edd16368SStephen M. Cameron dev_warn(d, "cp %p was aborted\n", cp); 1292edd16368SStephen M. Cameron break; 1293edd16368SStephen M. Cameron case CMD_ABORT_FAILED: 1294edd16368SStephen M. Cameron dev_warn(d, "cp %p reports abort failed\n", cp); 1295edd16368SStephen M. Cameron break; 1296edd16368SStephen M. Cameron case CMD_UNSOLICITED_ABORT: 1297edd16368SStephen M. Cameron dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp); 1298edd16368SStephen M. Cameron break; 1299edd16368SStephen M. Cameron case CMD_TIMEOUT: 1300edd16368SStephen M. Cameron dev_warn(d, "cp %p timed out\n", cp); 1301edd16368SStephen M. Cameron break; 1302edd16368SStephen M. Cameron default: 1303edd16368SStephen M. Cameron dev_warn(d, "cp %p returned unknown status %x\n", cp, 1304edd16368SStephen M. Cameron ei->CommandStatus); 1305edd16368SStephen M. Cameron } 1306edd16368SStephen M. Cameron } 1307edd16368SStephen M. Cameron 1308edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr, 1309edd16368SStephen M. Cameron unsigned char page, unsigned char *buf, 1310edd16368SStephen M. Cameron unsigned char bufsize) 1311edd16368SStephen M. Cameron { 1312edd16368SStephen M. Cameron int rc = IO_OK; 1313edd16368SStephen M. Cameron struct CommandList *c; 1314edd16368SStephen M. Cameron struct ErrorInfo *ei; 1315edd16368SStephen M. Cameron 1316edd16368SStephen M. Cameron c = cmd_special_alloc(h); 1317edd16368SStephen M. Cameron 1318edd16368SStephen M. Cameron if (c == NULL) { /* trouble... */ 1319edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); 1320ecd9aad4SStephen M. Cameron return -ENOMEM; 1321edd16368SStephen M. Cameron } 1322edd16368SStephen M. Cameron 1323edd16368SStephen M. Cameron fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD); 1324edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE); 1325edd16368SStephen M. Cameron ei = c->err_info; 1326edd16368SStephen M. Cameron if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) { 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 int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr) 1335edd16368SStephen M. Cameron { 1336edd16368SStephen M. Cameron int rc = IO_OK; 1337edd16368SStephen M. Cameron struct CommandList *c; 1338edd16368SStephen M. Cameron struct ErrorInfo *ei; 1339edd16368SStephen M. Cameron 1340edd16368SStephen M. Cameron c = cmd_special_alloc(h); 1341edd16368SStephen M. Cameron 1342edd16368SStephen M. Cameron if (c == NULL) { /* trouble... */ 1343edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); 1344e9ea04a6SStephen M. Cameron return -ENOMEM; 1345edd16368SStephen M. Cameron } 1346edd16368SStephen M. Cameron 1347edd16368SStephen M. Cameron fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG); 1348edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 1349edd16368SStephen M. Cameron /* no unmap needed here because no data xfer. */ 1350edd16368SStephen M. Cameron 1351edd16368SStephen M. Cameron ei = c->err_info; 1352edd16368SStephen M. Cameron if (ei->CommandStatus != 0) { 1353edd16368SStephen M. Cameron hpsa_scsi_interpret_error(c); 1354edd16368SStephen M. Cameron rc = -1; 1355edd16368SStephen M. Cameron } 1356edd16368SStephen M. Cameron cmd_special_free(h, c); 1357edd16368SStephen M. Cameron return rc; 1358edd16368SStephen M. Cameron } 1359edd16368SStephen M. Cameron 1360edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h, 1361edd16368SStephen M. Cameron unsigned char *scsi3addr, unsigned char *raid_level) 1362edd16368SStephen M. Cameron { 1363edd16368SStephen M. Cameron int rc; 1364edd16368SStephen M. Cameron unsigned char *buf; 1365edd16368SStephen M. Cameron 1366edd16368SStephen M. Cameron *raid_level = RAID_UNKNOWN; 1367edd16368SStephen M. Cameron buf = kzalloc(64, GFP_KERNEL); 1368edd16368SStephen M. Cameron if (!buf) 1369edd16368SStephen M. Cameron return; 1370edd16368SStephen M. Cameron rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64); 1371edd16368SStephen M. Cameron if (rc == 0) 1372edd16368SStephen M. Cameron *raid_level = buf[8]; 1373edd16368SStephen M. Cameron if (*raid_level > RAID_UNKNOWN) 1374edd16368SStephen M. Cameron *raid_level = RAID_UNKNOWN; 1375edd16368SStephen M. Cameron kfree(buf); 1376edd16368SStephen M. Cameron return; 1377edd16368SStephen M. Cameron } 1378edd16368SStephen M. Cameron 1379edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */ 1380edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr, 1381edd16368SStephen M. Cameron unsigned char *device_id, int buflen) 1382edd16368SStephen M. Cameron { 1383edd16368SStephen M. Cameron int rc; 1384edd16368SStephen M. Cameron unsigned char *buf; 1385edd16368SStephen M. Cameron 1386edd16368SStephen M. Cameron if (buflen > 16) 1387edd16368SStephen M. Cameron buflen = 16; 1388edd16368SStephen M. Cameron buf = kzalloc(64, GFP_KERNEL); 1389edd16368SStephen M. Cameron if (!buf) 1390edd16368SStephen M. Cameron return -1; 1391edd16368SStephen M. Cameron rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64); 1392edd16368SStephen M. Cameron if (rc == 0) 1393edd16368SStephen M. Cameron memcpy(device_id, &buf[8], buflen); 1394edd16368SStephen M. Cameron kfree(buf); 1395edd16368SStephen M. Cameron return rc != 0; 1396edd16368SStephen M. Cameron } 1397edd16368SStephen M. Cameron 1398edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical, 1399edd16368SStephen M. Cameron struct ReportLUNdata *buf, int bufsize, 1400edd16368SStephen M. Cameron int extended_response) 1401edd16368SStephen M. Cameron { 1402edd16368SStephen M. Cameron int rc = IO_OK; 1403edd16368SStephen M. Cameron struct CommandList *c; 1404edd16368SStephen M. Cameron unsigned char scsi3addr[8]; 1405edd16368SStephen M. Cameron struct ErrorInfo *ei; 1406edd16368SStephen M. Cameron 1407edd16368SStephen M. Cameron c = cmd_special_alloc(h); 1408edd16368SStephen M. Cameron if (c == NULL) { /* trouble... */ 1409edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); 1410edd16368SStephen M. Cameron return -1; 1411edd16368SStephen M. Cameron } 1412e89c0ae7SStephen M. Cameron /* address the controller */ 1413e89c0ae7SStephen M. Cameron memset(scsi3addr, 0, sizeof(scsi3addr)); 1414edd16368SStephen M. Cameron fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h, 1415edd16368SStephen M. Cameron buf, bufsize, 0, scsi3addr, TYPE_CMD); 1416edd16368SStephen M. Cameron if (extended_response) 1417edd16368SStephen M. Cameron c->Request.CDB[1] = extended_response; 1418edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE); 1419edd16368SStephen M. Cameron ei = c->err_info; 1420edd16368SStephen M. Cameron if (ei->CommandStatus != 0 && 1421edd16368SStephen M. Cameron ei->CommandStatus != CMD_DATA_UNDERRUN) { 1422edd16368SStephen M. Cameron hpsa_scsi_interpret_error(c); 1423edd16368SStephen M. Cameron rc = -1; 1424edd16368SStephen M. Cameron } 1425edd16368SStephen M. Cameron cmd_special_free(h, c); 1426edd16368SStephen M. Cameron return rc; 1427edd16368SStephen M. Cameron } 1428edd16368SStephen M. Cameron 1429edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h, 1430edd16368SStephen M. Cameron struct ReportLUNdata *buf, 1431edd16368SStephen M. Cameron int bufsize, int extended_response) 1432edd16368SStephen M. Cameron { 1433edd16368SStephen M. Cameron return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response); 1434edd16368SStephen M. Cameron } 1435edd16368SStephen M. Cameron 1436edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h, 1437edd16368SStephen M. Cameron struct ReportLUNdata *buf, int bufsize) 1438edd16368SStephen M. Cameron { 1439edd16368SStephen M. Cameron return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0); 1440edd16368SStephen M. Cameron } 1441edd16368SStephen M. Cameron 1442edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device, 1443edd16368SStephen M. Cameron int bus, int target, int lun) 1444edd16368SStephen M. Cameron { 1445edd16368SStephen M. Cameron device->bus = bus; 1446edd16368SStephen M. Cameron device->target = target; 1447edd16368SStephen M. Cameron device->lun = lun; 1448edd16368SStephen M. Cameron } 1449edd16368SStephen M. Cameron 1450edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h, 1451edd16368SStephen M. Cameron unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device) 1452edd16368SStephen M. Cameron { 1453edd16368SStephen M. Cameron #define OBDR_TAPE_INQ_SIZE 49 1454ea6d3bc3SStephen M. Cameron unsigned char *inq_buff; 1455edd16368SStephen M. Cameron 1456ea6d3bc3SStephen M. Cameron inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL); 1457edd16368SStephen M. Cameron if (!inq_buff) 1458edd16368SStephen M. Cameron goto bail_out; 1459edd16368SStephen M. Cameron 1460edd16368SStephen M. Cameron /* Do an inquiry to the device to see what it is. */ 1461edd16368SStephen M. Cameron if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff, 1462edd16368SStephen M. Cameron (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) { 1463edd16368SStephen M. Cameron /* Inquiry failed (msg printed already) */ 1464edd16368SStephen M. Cameron dev_err(&h->pdev->dev, 1465edd16368SStephen M. Cameron "hpsa_update_device_info: inquiry failed\n"); 1466edd16368SStephen M. Cameron goto bail_out; 1467edd16368SStephen M. Cameron } 1468edd16368SStephen M. Cameron 1469edd16368SStephen M. Cameron this_device->devtype = (inq_buff[0] & 0x1f); 1470edd16368SStephen M. Cameron memcpy(this_device->scsi3addr, scsi3addr, 8); 1471edd16368SStephen M. Cameron memcpy(this_device->vendor, &inq_buff[8], 1472edd16368SStephen M. Cameron sizeof(this_device->vendor)); 1473edd16368SStephen M. Cameron memcpy(this_device->model, &inq_buff[16], 1474edd16368SStephen M. Cameron sizeof(this_device->model)); 1475edd16368SStephen M. Cameron memset(this_device->device_id, 0, 1476edd16368SStephen M. Cameron sizeof(this_device->device_id)); 1477edd16368SStephen M. Cameron hpsa_get_device_id(h, scsi3addr, this_device->device_id, 1478edd16368SStephen M. Cameron sizeof(this_device->device_id)); 1479edd16368SStephen M. Cameron 1480edd16368SStephen M. Cameron if (this_device->devtype == TYPE_DISK && 1481edd16368SStephen M. Cameron is_logical_dev_addr_mode(scsi3addr)) 1482edd16368SStephen M. Cameron hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level); 1483edd16368SStephen M. Cameron else 1484edd16368SStephen M. Cameron this_device->raid_level = RAID_UNKNOWN; 1485edd16368SStephen M. Cameron 1486edd16368SStephen M. Cameron kfree(inq_buff); 1487edd16368SStephen M. Cameron return 0; 1488edd16368SStephen M. Cameron 1489edd16368SStephen M. Cameron bail_out: 1490edd16368SStephen M. Cameron kfree(inq_buff); 1491edd16368SStephen M. Cameron return 1; 1492edd16368SStephen M. Cameron } 1493edd16368SStephen M. Cameron 1494edd16368SStephen M. Cameron static unsigned char *msa2xxx_model[] = { 1495edd16368SStephen M. Cameron "MSA2012", 1496edd16368SStephen M. Cameron "MSA2024", 1497edd16368SStephen M. Cameron "MSA2312", 1498edd16368SStephen M. Cameron "MSA2324", 1499edd16368SStephen M. Cameron NULL, 1500edd16368SStephen M. Cameron }; 1501edd16368SStephen M. Cameron 1502edd16368SStephen M. Cameron static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device) 1503edd16368SStephen M. Cameron { 1504edd16368SStephen M. Cameron int i; 1505edd16368SStephen M. Cameron 1506edd16368SStephen M. Cameron for (i = 0; msa2xxx_model[i]; i++) 1507edd16368SStephen M. Cameron if (strncmp(device->model, msa2xxx_model[i], 1508edd16368SStephen M. Cameron strlen(msa2xxx_model[i])) == 0) 1509edd16368SStephen M. Cameron return 1; 1510edd16368SStephen M. Cameron return 0; 1511edd16368SStephen M. Cameron } 1512edd16368SStephen M. Cameron 1513edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices. 1514edd16368SStephen M. Cameron * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical 1515edd16368SStephen M. Cameron * volumes on bus 1, physical devices on bus 2. and the hba on bus 3. 1516edd16368SStephen M. Cameron * Logical drive target and lun are assigned at this time, but 1517edd16368SStephen M. Cameron * physical device lun and target assignment are deferred (assigned 1518edd16368SStephen M. Cameron * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.) 1519edd16368SStephen M. Cameron */ 1520edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h, 152101a02ffcSStephen M. Cameron u8 *lunaddrbytes, int *bus, int *target, int *lun, 1522edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *device) 1523edd16368SStephen M. Cameron { 152401a02ffcSStephen M. Cameron u32 lunid; 1525edd16368SStephen M. Cameron 1526edd16368SStephen M. Cameron if (is_logical_dev_addr_mode(lunaddrbytes)) { 1527edd16368SStephen M. Cameron /* logical device */ 1528339b2b14SStephen M. Cameron if (unlikely(is_scsi_rev_5(h))) { 1529339b2b14SStephen M. Cameron /* p1210m, logical drives lun assignments 1530339b2b14SStephen M. Cameron * match SCSI REPORT LUNS data. 1531339b2b14SStephen M. Cameron */ 1532339b2b14SStephen M. Cameron lunid = le32_to_cpu(*((__le32 *) lunaddrbytes)); 1533339b2b14SStephen M. Cameron *bus = 0; 1534339b2b14SStephen M. Cameron *target = 0; 1535339b2b14SStephen M. Cameron *lun = (lunid & 0x3fff) + 1; 1536339b2b14SStephen M. Cameron } else { 1537339b2b14SStephen M. Cameron /* not p1210m... */ 15386df1e954SStephen M. Cameron lunid = le32_to_cpu(*((__le32 *) lunaddrbytes)); 1539edd16368SStephen M. Cameron if (is_msa2xxx(h, device)) { 1540339b2b14SStephen M. Cameron /* msa2xxx way, put logicals on bus 1 1541339b2b14SStephen M. Cameron * and match target/lun numbers box 1542339b2b14SStephen M. Cameron * reports. 1543339b2b14SStephen M. Cameron */ 1544edd16368SStephen M. Cameron *bus = 1; 1545edd16368SStephen M. Cameron *target = (lunid >> 16) & 0x3fff; 1546edd16368SStephen M. Cameron *lun = lunid & 0x00ff; 1547edd16368SStephen M. Cameron } else { 1548339b2b14SStephen M. Cameron /* Traditional smart array way. */ 1549edd16368SStephen M. Cameron *bus = 0; 1550edd16368SStephen M. Cameron *lun = 0; 1551edd16368SStephen M. Cameron *target = lunid & 0x3fff; 1552edd16368SStephen M. Cameron } 1553339b2b14SStephen M. Cameron } 1554edd16368SStephen M. Cameron } else { 1555edd16368SStephen M. Cameron /* physical device */ 1556edd16368SStephen M. Cameron if (is_hba_lunid(lunaddrbytes)) 1557339b2b14SStephen M. Cameron if (unlikely(is_scsi_rev_5(h))) { 1558339b2b14SStephen M. Cameron *bus = 0; /* put p1210m ctlr at 0,0,0 */ 1559339b2b14SStephen M. Cameron *target = 0; 1560339b2b14SStephen M. Cameron *lun = 0; 1561339b2b14SStephen M. Cameron return; 1562339b2b14SStephen M. Cameron } else 1563339b2b14SStephen M. Cameron *bus = 3; /* traditional smartarray */ 1564edd16368SStephen M. Cameron else 1565339b2b14SStephen M. Cameron *bus = 2; /* physical disk */ 1566edd16368SStephen M. Cameron *target = -1; 1567edd16368SStephen M. Cameron *lun = -1; /* we will fill these in later. */ 1568edd16368SStephen M. Cameron } 1569edd16368SStephen M. Cameron } 1570edd16368SStephen M. Cameron 1571edd16368SStephen M. Cameron /* 1572edd16368SStephen M. Cameron * If there is no lun 0 on a target, linux won't find any devices. 1573edd16368SStephen M. Cameron * For the MSA2xxx boxes, we have to manually detect the enclosure 1574edd16368SStephen M. Cameron * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report 1575edd16368SStephen M. Cameron * it for some reason. *tmpdevice is the target we're adding, 1576edd16368SStephen M. Cameron * this_device is a pointer into the current element of currentsd[] 1577edd16368SStephen M. Cameron * that we're building up in update_scsi_devices(), below. 1578edd16368SStephen M. Cameron * lunzerobits is a bitmap that tracks which targets already have a 1579edd16368SStephen M. Cameron * lun 0 assigned. 1580edd16368SStephen M. Cameron * Returns 1 if an enclosure was added, 0 if not. 1581edd16368SStephen M. Cameron */ 1582edd16368SStephen M. Cameron static int add_msa2xxx_enclosure_device(struct ctlr_info *h, 1583edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *tmpdevice, 158401a02ffcSStephen M. Cameron struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes, 1585edd16368SStephen M. Cameron int bus, int target, int lun, unsigned long lunzerobits[], 1586edd16368SStephen M. Cameron int *nmsa2xxx_enclosures) 1587edd16368SStephen M. Cameron { 1588edd16368SStephen M. Cameron unsigned char scsi3addr[8]; 1589edd16368SStephen M. Cameron 1590edd16368SStephen M. Cameron if (test_bit(target, lunzerobits)) 1591edd16368SStephen M. Cameron return 0; /* There is already a lun 0 on this target. */ 1592edd16368SStephen M. Cameron 1593edd16368SStephen M. Cameron if (!is_logical_dev_addr_mode(lunaddrbytes)) 1594edd16368SStephen M. Cameron return 0; /* It's the logical targets that may lack lun 0. */ 1595edd16368SStephen M. Cameron 1596edd16368SStephen M. Cameron if (!is_msa2xxx(h, tmpdevice)) 1597edd16368SStephen M. Cameron return 0; /* It's only the MSA2xxx that have this problem. */ 1598edd16368SStephen M. Cameron 1599edd16368SStephen M. Cameron if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */ 1600edd16368SStephen M. Cameron return 0; 1601edd16368SStephen M. Cameron 1602edd16368SStephen M. Cameron if (is_hba_lunid(scsi3addr)) 1603edd16368SStephen M. Cameron return 0; /* Don't add the RAID controller here. */ 1604edd16368SStephen M. Cameron 1605339b2b14SStephen M. Cameron if (is_scsi_rev_5(h)) 1606339b2b14SStephen M. Cameron return 0; /* p1210m doesn't need to do this. */ 1607339b2b14SStephen M. Cameron 1608edd16368SStephen M. Cameron #define MAX_MSA2XXX_ENCLOSURES 32 1609edd16368SStephen M. Cameron if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) { 1610edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX " 1611edd16368SStephen M. Cameron "enclosures exceeded. Check your hardware " 1612edd16368SStephen M. Cameron "configuration."); 1613edd16368SStephen M. Cameron return 0; 1614edd16368SStephen M. Cameron } 1615edd16368SStephen M. Cameron 1616edd16368SStephen M. Cameron memset(scsi3addr, 0, 8); 1617edd16368SStephen M. Cameron scsi3addr[3] = target; 1618edd16368SStephen M. Cameron if (hpsa_update_device_info(h, scsi3addr, this_device)) 1619edd16368SStephen M. Cameron return 0; 1620edd16368SStephen M. Cameron (*nmsa2xxx_enclosures)++; 1621edd16368SStephen M. Cameron hpsa_set_bus_target_lun(this_device, bus, target, 0); 1622edd16368SStephen M. Cameron set_bit(target, lunzerobits); 1623edd16368SStephen M. Cameron return 1; 1624edd16368SStephen M. Cameron } 1625edd16368SStephen M. Cameron 1626edd16368SStephen M. Cameron /* 1627edd16368SStephen M. Cameron * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev, 1628edd16368SStephen M. Cameron * logdev. The number of luns in physdev and logdev are returned in 1629edd16368SStephen M. Cameron * *nphysicals and *nlogicals, respectively. 1630edd16368SStephen M. Cameron * Returns 0 on success, -1 otherwise. 1631edd16368SStephen M. Cameron */ 1632edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h, 1633edd16368SStephen M. Cameron int reportlunsize, 163401a02ffcSStephen M. Cameron struct ReportLUNdata *physdev, u32 *nphysicals, 163501a02ffcSStephen M. Cameron struct ReportLUNdata *logdev, u32 *nlogicals) 1636edd16368SStephen M. Cameron { 1637edd16368SStephen M. Cameron if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) { 1638edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "report physical LUNs failed.\n"); 1639edd16368SStephen M. Cameron return -1; 1640edd16368SStephen M. Cameron } 16416df1e954SStephen M. Cameron *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8; 1642edd16368SStephen M. Cameron if (*nphysicals > HPSA_MAX_PHYS_LUN) { 1643edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded." 1644edd16368SStephen M. Cameron " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN, 1645edd16368SStephen M. Cameron *nphysicals - HPSA_MAX_PHYS_LUN); 1646edd16368SStephen M. Cameron *nphysicals = HPSA_MAX_PHYS_LUN; 1647edd16368SStephen M. Cameron } 1648edd16368SStephen M. Cameron if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) { 1649edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "report logical LUNs failed.\n"); 1650edd16368SStephen M. Cameron return -1; 1651edd16368SStephen M. Cameron } 16526df1e954SStephen M. Cameron *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8; 1653edd16368SStephen M. Cameron /* Reject Logicals in excess of our max capability. */ 1654edd16368SStephen M. Cameron if (*nlogicals > HPSA_MAX_LUN) { 1655edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, 1656edd16368SStephen M. Cameron "maximum logical LUNs (%d) exceeded. " 1657edd16368SStephen M. Cameron "%d LUNs ignored.\n", HPSA_MAX_LUN, 1658edd16368SStephen M. Cameron *nlogicals - HPSA_MAX_LUN); 1659edd16368SStephen M. Cameron *nlogicals = HPSA_MAX_LUN; 1660edd16368SStephen M. Cameron } 1661edd16368SStephen M. Cameron if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) { 1662edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, 1663edd16368SStephen M. Cameron "maximum logical + physical LUNs (%d) exceeded. " 1664edd16368SStephen M. Cameron "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN, 1665edd16368SStephen M. Cameron *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN); 1666edd16368SStephen M. Cameron *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals; 1667edd16368SStephen M. Cameron } 1668edd16368SStephen M. Cameron return 0; 1669edd16368SStephen M. Cameron } 1670edd16368SStephen M. Cameron 1671339b2b14SStephen M. Cameron u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i, 1672339b2b14SStephen M. Cameron int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list, 1673339b2b14SStephen M. Cameron struct ReportLUNdata *logdev_list) 1674339b2b14SStephen M. Cameron { 1675339b2b14SStephen M. Cameron /* Helper function, figure out where the LUN ID info is coming from 1676339b2b14SStephen M. Cameron * given index i, lists of physical and logical devices, where in 1677339b2b14SStephen M. Cameron * the list the raid controller is supposed to appear (first or last) 1678339b2b14SStephen M. Cameron */ 1679339b2b14SStephen M. Cameron 1680339b2b14SStephen M. Cameron int logicals_start = nphysicals + (raid_ctlr_position == 0); 1681339b2b14SStephen M. Cameron int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0); 1682339b2b14SStephen M. Cameron 1683339b2b14SStephen M. Cameron if (i == raid_ctlr_position) 1684339b2b14SStephen M. Cameron return RAID_CTLR_LUNID; 1685339b2b14SStephen M. Cameron 1686339b2b14SStephen M. Cameron if (i < logicals_start) 1687339b2b14SStephen M. Cameron return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0]; 1688339b2b14SStephen M. Cameron 1689339b2b14SStephen M. Cameron if (i < last_device) 1690339b2b14SStephen M. Cameron return &logdev_list->LUN[i - nphysicals - 1691339b2b14SStephen M. Cameron (raid_ctlr_position == 0)][0]; 1692339b2b14SStephen M. Cameron BUG(); 1693339b2b14SStephen M. Cameron return NULL; 1694339b2b14SStephen M. Cameron } 1695339b2b14SStephen M. Cameron 1696edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno) 1697edd16368SStephen M. Cameron { 1698edd16368SStephen M. Cameron /* the idea here is we could get notified 1699edd16368SStephen M. Cameron * that some devices have changed, so we do a report 1700edd16368SStephen M. Cameron * physical luns and report logical luns cmd, and adjust 1701edd16368SStephen M. Cameron * our list of devices accordingly. 1702edd16368SStephen M. Cameron * 1703edd16368SStephen M. Cameron * The scsi3addr's of devices won't change so long as the 1704edd16368SStephen M. Cameron * adapter is not reset. That means we can rescan and 1705edd16368SStephen M. Cameron * tell which devices we already know about, vs. new 1706edd16368SStephen M. Cameron * devices, vs. disappearing devices. 1707edd16368SStephen M. Cameron */ 1708edd16368SStephen M. Cameron struct ReportLUNdata *physdev_list = NULL; 1709edd16368SStephen M. Cameron struct ReportLUNdata *logdev_list = NULL; 1710edd16368SStephen M. Cameron unsigned char *inq_buff = NULL; 171101a02ffcSStephen M. Cameron u32 nphysicals = 0; 171201a02ffcSStephen M. Cameron u32 nlogicals = 0; 171301a02ffcSStephen M. Cameron u32 ndev_allocated = 0; 1714edd16368SStephen M. Cameron struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice; 1715edd16368SStephen M. Cameron int ncurrent = 0; 1716edd16368SStephen M. Cameron int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8; 1717edd16368SStephen M. Cameron int i, nmsa2xxx_enclosures, ndevs_to_allocate; 1718edd16368SStephen M. Cameron int bus, target, lun; 1719339b2b14SStephen M. Cameron int raid_ctlr_position; 1720edd16368SStephen M. Cameron DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR); 1721edd16368SStephen M. Cameron 1722edd16368SStephen M. Cameron currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA, 1723edd16368SStephen M. Cameron GFP_KERNEL); 1724edd16368SStephen M. Cameron physdev_list = kzalloc(reportlunsize, GFP_KERNEL); 1725edd16368SStephen M. Cameron logdev_list = kzalloc(reportlunsize, GFP_KERNEL); 1726edd16368SStephen M. Cameron inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL); 1727edd16368SStephen M. Cameron tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL); 1728edd16368SStephen M. Cameron 1729edd16368SStephen M. Cameron if (!currentsd || !physdev_list || !logdev_list || 1730edd16368SStephen M. Cameron !inq_buff || !tmpdevice) { 1731edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "out of memory\n"); 1732edd16368SStephen M. Cameron goto out; 1733edd16368SStephen M. Cameron } 1734edd16368SStephen M. Cameron memset(lunzerobits, 0, sizeof(lunzerobits)); 1735edd16368SStephen M. Cameron 1736edd16368SStephen M. Cameron if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals, 1737edd16368SStephen M. Cameron logdev_list, &nlogicals)) 1738edd16368SStephen M. Cameron goto out; 1739edd16368SStephen M. Cameron 1740edd16368SStephen M. Cameron /* We might see up to 32 MSA2xxx enclosures, actually 8 of them 1741edd16368SStephen M. Cameron * but each of them 4 times through different paths. The plus 1 1742edd16368SStephen M. Cameron * is for the RAID controller. 1743edd16368SStephen M. Cameron */ 1744edd16368SStephen M. Cameron ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1; 1745edd16368SStephen M. Cameron 1746edd16368SStephen M. Cameron /* Allocate the per device structures */ 1747edd16368SStephen M. Cameron for (i = 0; i < ndevs_to_allocate; i++) { 1748edd16368SStephen M. Cameron currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL); 1749edd16368SStephen M. Cameron if (!currentsd[i]) { 1750edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "out of memory at %s:%d\n", 1751edd16368SStephen M. Cameron __FILE__, __LINE__); 1752edd16368SStephen M. Cameron goto out; 1753edd16368SStephen M. Cameron } 1754edd16368SStephen M. Cameron ndev_allocated++; 1755edd16368SStephen M. Cameron } 1756edd16368SStephen M. Cameron 1757339b2b14SStephen M. Cameron if (unlikely(is_scsi_rev_5(h))) 1758339b2b14SStephen M. Cameron raid_ctlr_position = 0; 1759339b2b14SStephen M. Cameron else 1760339b2b14SStephen M. Cameron raid_ctlr_position = nphysicals + nlogicals; 1761339b2b14SStephen M. Cameron 1762edd16368SStephen M. Cameron /* adjust our table of devices */ 1763edd16368SStephen M. Cameron nmsa2xxx_enclosures = 0; 1764edd16368SStephen M. Cameron for (i = 0; i < nphysicals + nlogicals + 1; i++) { 176501a02ffcSStephen M. Cameron u8 *lunaddrbytes; 1766edd16368SStephen M. Cameron 1767edd16368SStephen M. Cameron /* Figure out where the LUN ID info is coming from */ 1768339b2b14SStephen M. Cameron lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position, 1769339b2b14SStephen M. Cameron i, nphysicals, nlogicals, physdev_list, logdev_list); 1770edd16368SStephen M. Cameron /* skip masked physical devices. */ 1771339b2b14SStephen M. Cameron if (lunaddrbytes[3] & 0xC0 && 1772339b2b14SStephen M. Cameron i < nphysicals + (raid_ctlr_position == 0)) 1773edd16368SStephen M. Cameron continue; 1774edd16368SStephen M. Cameron 1775edd16368SStephen M. Cameron /* Get device type, vendor, model, device id */ 1776edd16368SStephen M. Cameron if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice)) 1777edd16368SStephen M. Cameron continue; /* skip it if we can't talk to it. */ 1778edd16368SStephen M. Cameron figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun, 1779edd16368SStephen M. Cameron tmpdevice); 1780edd16368SStephen M. Cameron this_device = currentsd[ncurrent]; 1781edd16368SStephen M. Cameron 1782edd16368SStephen M. Cameron /* 1783edd16368SStephen M. Cameron * For the msa2xxx boxes, we have to insert a LUN 0 which 1784edd16368SStephen M. Cameron * doesn't show up in CCISS_REPORT_PHYSICAL data, but there 1785edd16368SStephen M. Cameron * is nonetheless an enclosure device there. We have to 1786edd16368SStephen M. Cameron * present that otherwise linux won't find anything if 1787edd16368SStephen M. Cameron * there is no lun 0. 1788edd16368SStephen M. Cameron */ 1789edd16368SStephen M. Cameron if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device, 1790edd16368SStephen M. Cameron lunaddrbytes, bus, target, lun, lunzerobits, 1791edd16368SStephen M. Cameron &nmsa2xxx_enclosures)) { 1792edd16368SStephen M. Cameron ncurrent++; 1793edd16368SStephen M. Cameron this_device = currentsd[ncurrent]; 1794edd16368SStephen M. Cameron } 1795edd16368SStephen M. Cameron 1796edd16368SStephen M. Cameron *this_device = *tmpdevice; 1797edd16368SStephen M. Cameron hpsa_set_bus_target_lun(this_device, bus, target, lun); 1798edd16368SStephen M. Cameron 1799edd16368SStephen M. Cameron switch (this_device->devtype) { 1800edd16368SStephen M. Cameron case TYPE_ROM: { 1801edd16368SStephen M. Cameron /* We don't *really* support actual CD-ROM devices, 1802edd16368SStephen M. Cameron * just "One Button Disaster Recovery" tape drive 1803edd16368SStephen M. Cameron * which temporarily pretends to be a CD-ROM drive. 1804edd16368SStephen M. Cameron * So we check that the device is really an OBDR tape 1805edd16368SStephen M. Cameron * device by checking for "$DR-10" in bytes 43-48 of 1806edd16368SStephen M. Cameron * the inquiry data. 1807edd16368SStephen M. Cameron */ 1808edd16368SStephen M. Cameron char obdr_sig[7]; 1809edd16368SStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10" 1810edd16368SStephen M. Cameron strncpy(obdr_sig, &inq_buff[43], 6); 1811edd16368SStephen M. Cameron obdr_sig[6] = '\0'; 1812edd16368SStephen M. Cameron if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0) 1813edd16368SStephen M. Cameron /* Not OBDR device, ignore it. */ 1814edd16368SStephen M. Cameron break; 1815edd16368SStephen M. Cameron } 1816edd16368SStephen M. Cameron ncurrent++; 1817edd16368SStephen M. Cameron break; 1818edd16368SStephen M. Cameron case TYPE_DISK: 1819edd16368SStephen M. Cameron if (i < nphysicals) 1820edd16368SStephen M. Cameron break; 1821edd16368SStephen M. Cameron ncurrent++; 1822edd16368SStephen M. Cameron break; 1823edd16368SStephen M. Cameron case TYPE_TAPE: 1824edd16368SStephen M. Cameron case TYPE_MEDIUM_CHANGER: 1825edd16368SStephen M. Cameron ncurrent++; 1826edd16368SStephen M. Cameron break; 1827edd16368SStephen M. Cameron case TYPE_RAID: 1828edd16368SStephen M. Cameron /* Only present the Smartarray HBA as a RAID controller. 1829edd16368SStephen M. Cameron * If it's a RAID controller other than the HBA itself 1830edd16368SStephen M. Cameron * (an external RAID controller, MSA500 or similar) 1831edd16368SStephen M. Cameron * don't present it. 1832edd16368SStephen M. Cameron */ 1833edd16368SStephen M. Cameron if (!is_hba_lunid(lunaddrbytes)) 1834edd16368SStephen M. Cameron break; 1835edd16368SStephen M. Cameron ncurrent++; 1836edd16368SStephen M. Cameron break; 1837edd16368SStephen M. Cameron default: 1838edd16368SStephen M. Cameron break; 1839edd16368SStephen M. Cameron } 1840edd16368SStephen M. Cameron if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA) 1841edd16368SStephen M. Cameron break; 1842edd16368SStephen M. Cameron } 1843edd16368SStephen M. Cameron adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent); 1844edd16368SStephen M. Cameron out: 1845edd16368SStephen M. Cameron kfree(tmpdevice); 1846edd16368SStephen M. Cameron for (i = 0; i < ndev_allocated; i++) 1847edd16368SStephen M. Cameron kfree(currentsd[i]); 1848edd16368SStephen M. Cameron kfree(currentsd); 1849edd16368SStephen M. Cameron kfree(inq_buff); 1850edd16368SStephen M. Cameron kfree(physdev_list); 1851edd16368SStephen M. Cameron kfree(logdev_list); 1852edd16368SStephen M. Cameron } 1853edd16368SStephen M. Cameron 1854edd16368SStephen M. Cameron /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci 1855edd16368SStephen M. Cameron * dma mapping and fills in the scatter gather entries of the 1856edd16368SStephen M. Cameron * hpsa command, cp. 1857edd16368SStephen M. Cameron */ 185833a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h, 1859edd16368SStephen M. Cameron struct CommandList *cp, 1860edd16368SStephen M. Cameron struct scsi_cmnd *cmd) 1861edd16368SStephen M. Cameron { 1862edd16368SStephen M. Cameron unsigned int len; 1863edd16368SStephen M. Cameron struct scatterlist *sg; 186401a02ffcSStephen M. Cameron u64 addr64; 186533a2ffceSStephen M. Cameron int use_sg, i, sg_index, chained; 186633a2ffceSStephen M. Cameron struct SGDescriptor *curr_sg; 1867edd16368SStephen M. Cameron 186833a2ffceSStephen M. Cameron BUG_ON(scsi_sg_count(cmd) > h->maxsgentries); 1869edd16368SStephen M. Cameron 1870edd16368SStephen M. Cameron use_sg = scsi_dma_map(cmd); 1871edd16368SStephen M. Cameron if (use_sg < 0) 1872edd16368SStephen M. Cameron return use_sg; 1873edd16368SStephen M. Cameron 1874edd16368SStephen M. Cameron if (!use_sg) 1875edd16368SStephen M. Cameron goto sglist_finished; 1876edd16368SStephen M. Cameron 187733a2ffceSStephen M. Cameron curr_sg = cp->SG; 187833a2ffceSStephen M. Cameron chained = 0; 187933a2ffceSStephen M. Cameron sg_index = 0; 1880edd16368SStephen M. Cameron scsi_for_each_sg(cmd, sg, use_sg, i) { 188133a2ffceSStephen M. Cameron if (i == h->max_cmd_sg_entries - 1 && 188233a2ffceSStephen M. Cameron use_sg > h->max_cmd_sg_entries) { 188333a2ffceSStephen M. Cameron chained = 1; 188433a2ffceSStephen M. Cameron curr_sg = h->cmd_sg_list[cp->cmdindex]; 188533a2ffceSStephen M. Cameron sg_index = 0; 188633a2ffceSStephen M. Cameron } 188701a02ffcSStephen M. Cameron addr64 = (u64) sg_dma_address(sg); 1888edd16368SStephen M. Cameron len = sg_dma_len(sg); 188933a2ffceSStephen M. Cameron curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL); 189033a2ffceSStephen M. Cameron curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL); 189133a2ffceSStephen M. Cameron curr_sg->Len = len; 189233a2ffceSStephen M. Cameron curr_sg->Ext = 0; /* we are not chaining */ 189333a2ffceSStephen M. Cameron curr_sg++; 189433a2ffceSStephen M. Cameron } 189533a2ffceSStephen M. Cameron 189633a2ffceSStephen M. Cameron if (use_sg + chained > h->maxSG) 189733a2ffceSStephen M. Cameron h->maxSG = use_sg + chained; 189833a2ffceSStephen M. Cameron 189933a2ffceSStephen M. Cameron if (chained) { 190033a2ffceSStephen M. Cameron cp->Header.SGList = h->max_cmd_sg_entries; 190133a2ffceSStephen M. Cameron cp->Header.SGTotal = (u16) (use_sg + 1); 190233a2ffceSStephen M. Cameron hpsa_map_sg_chain_block(h, cp); 190333a2ffceSStephen M. Cameron return 0; 1904edd16368SStephen M. Cameron } 1905edd16368SStephen M. Cameron 1906edd16368SStephen M. Cameron sglist_finished: 1907edd16368SStephen M. Cameron 190801a02ffcSStephen M. Cameron cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */ 190901a02ffcSStephen M. Cameron cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */ 1910edd16368SStephen M. Cameron return 0; 1911edd16368SStephen M. Cameron } 1912edd16368SStephen M. Cameron 1913edd16368SStephen M. Cameron 1914f281233dSJeff Garzik static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd, 1915edd16368SStephen M. Cameron void (*done)(struct scsi_cmnd *)) 1916edd16368SStephen M. Cameron { 1917edd16368SStephen M. Cameron struct ctlr_info *h; 1918edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *dev; 1919edd16368SStephen M. Cameron unsigned char scsi3addr[8]; 1920edd16368SStephen M. Cameron struct CommandList *c; 1921edd16368SStephen M. Cameron unsigned long flags; 1922edd16368SStephen M. Cameron 1923edd16368SStephen M. Cameron /* Get the ptr to our adapter structure out of cmd->host. */ 1924edd16368SStephen M. Cameron h = sdev_to_hba(cmd->device); 1925edd16368SStephen M. Cameron dev = cmd->device->hostdata; 1926edd16368SStephen M. Cameron if (!dev) { 1927edd16368SStephen M. Cameron cmd->result = DID_NO_CONNECT << 16; 1928edd16368SStephen M. Cameron done(cmd); 1929edd16368SStephen M. Cameron return 0; 1930edd16368SStephen M. Cameron } 1931edd16368SStephen M. Cameron memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr)); 1932edd16368SStephen M. Cameron 1933edd16368SStephen M. Cameron /* Need a lock as this is being allocated from the pool */ 1934edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 1935edd16368SStephen M. Cameron c = cmd_alloc(h); 1936edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 1937edd16368SStephen M. Cameron if (c == NULL) { /* trouble... */ 1938edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n"); 1939edd16368SStephen M. Cameron return SCSI_MLQUEUE_HOST_BUSY; 1940edd16368SStephen M. Cameron } 1941edd16368SStephen M. Cameron 1942edd16368SStephen M. Cameron /* Fill in the command list header */ 1943edd16368SStephen M. Cameron 1944edd16368SStephen M. Cameron cmd->scsi_done = done; /* save this for use by completion code */ 1945edd16368SStephen M. Cameron 1946edd16368SStephen M. Cameron /* save c in case we have to abort it */ 1947edd16368SStephen M. Cameron cmd->host_scribble = (unsigned char *) c; 1948edd16368SStephen M. Cameron 1949edd16368SStephen M. Cameron c->cmd_type = CMD_SCSI; 1950edd16368SStephen M. Cameron c->scsi_cmd = cmd; 1951edd16368SStephen M. Cameron c->Header.ReplyQueue = 0; /* unused in simple mode */ 1952edd16368SStephen M. Cameron memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8); 1953303932fdSDon Brace c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT); 1954303932fdSDon Brace c->Header.Tag.lower |= DIRECT_LOOKUP_BIT; 1955edd16368SStephen M. Cameron 1956edd16368SStephen M. Cameron /* Fill in the request block... */ 1957edd16368SStephen M. Cameron 1958edd16368SStephen M. Cameron c->Request.Timeout = 0; 1959edd16368SStephen M. Cameron memset(c->Request.CDB, 0, sizeof(c->Request.CDB)); 1960edd16368SStephen M. Cameron BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB)); 1961edd16368SStephen M. Cameron c->Request.CDBLen = cmd->cmd_len; 1962edd16368SStephen M. Cameron memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len); 1963edd16368SStephen M. Cameron c->Request.Type.Type = TYPE_CMD; 1964edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 1965edd16368SStephen M. Cameron switch (cmd->sc_data_direction) { 1966edd16368SStephen M. Cameron case DMA_TO_DEVICE: 1967edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_WRITE; 1968edd16368SStephen M. Cameron break; 1969edd16368SStephen M. Cameron case DMA_FROM_DEVICE: 1970edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_READ; 1971edd16368SStephen M. Cameron break; 1972edd16368SStephen M. Cameron case DMA_NONE: 1973edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_NONE; 1974edd16368SStephen M. Cameron break; 1975edd16368SStephen M. Cameron case DMA_BIDIRECTIONAL: 1976edd16368SStephen M. Cameron /* This can happen if a buggy application does a scsi passthru 1977edd16368SStephen M. Cameron * and sets both inlen and outlen to non-zero. ( see 1978edd16368SStephen M. Cameron * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() ) 1979edd16368SStephen M. Cameron */ 1980edd16368SStephen M. Cameron 1981edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_RSVD; 1982edd16368SStephen M. Cameron /* This is technically wrong, and hpsa controllers should 1983edd16368SStephen M. Cameron * reject it with CMD_INVALID, which is the most correct 1984edd16368SStephen M. Cameron * response, but non-fibre backends appear to let it 1985edd16368SStephen M. Cameron * slide by, and give the same results as if this field 1986edd16368SStephen M. Cameron * were set correctly. Either way is acceptable for 1987edd16368SStephen M. Cameron * our purposes here. 1988edd16368SStephen M. Cameron */ 1989edd16368SStephen M. Cameron 1990edd16368SStephen M. Cameron break; 1991edd16368SStephen M. Cameron 1992edd16368SStephen M. Cameron default: 1993edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "unknown data direction: %d\n", 1994edd16368SStephen M. Cameron cmd->sc_data_direction); 1995edd16368SStephen M. Cameron BUG(); 1996edd16368SStephen M. Cameron break; 1997edd16368SStephen M. Cameron } 1998edd16368SStephen M. Cameron 199933a2ffceSStephen M. Cameron if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */ 2000edd16368SStephen M. Cameron cmd_free(h, c); 2001edd16368SStephen M. Cameron return SCSI_MLQUEUE_HOST_BUSY; 2002edd16368SStephen M. Cameron } 2003edd16368SStephen M. Cameron enqueue_cmd_and_start_io(h, c); 2004edd16368SStephen M. Cameron /* the cmd'll come back via intr handler in complete_scsi_command() */ 2005edd16368SStephen M. Cameron return 0; 2006edd16368SStephen M. Cameron } 2007edd16368SStephen M. Cameron 2008f281233dSJeff Garzik static DEF_SCSI_QCMD(hpsa_scsi_queue_command) 2009f281233dSJeff Garzik 2010a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh) 2011a08a8471SStephen M. Cameron { 2012a08a8471SStephen M. Cameron struct ctlr_info *h = shost_to_hba(sh); 2013a08a8471SStephen M. Cameron unsigned long flags; 2014a08a8471SStephen M. Cameron 2015a08a8471SStephen M. Cameron /* wait until any scan already in progress is finished. */ 2016a08a8471SStephen M. Cameron while (1) { 2017a08a8471SStephen M. Cameron spin_lock_irqsave(&h->scan_lock, flags); 2018a08a8471SStephen M. Cameron if (h->scan_finished) 2019a08a8471SStephen M. Cameron break; 2020a08a8471SStephen M. Cameron spin_unlock_irqrestore(&h->scan_lock, flags); 2021a08a8471SStephen M. Cameron wait_event(h->scan_wait_queue, h->scan_finished); 2022a08a8471SStephen M. Cameron /* Note: We don't need to worry about a race between this 2023a08a8471SStephen M. Cameron * thread and driver unload because the midlayer will 2024a08a8471SStephen M. Cameron * have incremented the reference count, so unload won't 2025a08a8471SStephen M. Cameron * happen if we're in here. 2026a08a8471SStephen M. Cameron */ 2027a08a8471SStephen M. Cameron } 2028a08a8471SStephen M. Cameron h->scan_finished = 0; /* mark scan as in progress */ 2029a08a8471SStephen M. Cameron spin_unlock_irqrestore(&h->scan_lock, flags); 2030a08a8471SStephen M. Cameron 2031a08a8471SStephen M. Cameron hpsa_update_scsi_devices(h, h->scsi_host->host_no); 2032a08a8471SStephen M. Cameron 2033a08a8471SStephen M. Cameron spin_lock_irqsave(&h->scan_lock, flags); 2034a08a8471SStephen M. Cameron h->scan_finished = 1; /* mark scan as finished. */ 2035a08a8471SStephen M. Cameron wake_up_all(&h->scan_wait_queue); 2036a08a8471SStephen M. Cameron spin_unlock_irqrestore(&h->scan_lock, flags); 2037a08a8471SStephen M. Cameron } 2038a08a8471SStephen M. Cameron 2039a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh, 2040a08a8471SStephen M. Cameron unsigned long elapsed_time) 2041a08a8471SStephen M. Cameron { 2042a08a8471SStephen M. Cameron struct ctlr_info *h = shost_to_hba(sh); 2043a08a8471SStephen M. Cameron unsigned long flags; 2044a08a8471SStephen M. Cameron int finished; 2045a08a8471SStephen M. Cameron 2046a08a8471SStephen M. Cameron spin_lock_irqsave(&h->scan_lock, flags); 2047a08a8471SStephen M. Cameron finished = h->scan_finished; 2048a08a8471SStephen M. Cameron spin_unlock_irqrestore(&h->scan_lock, flags); 2049a08a8471SStephen M. Cameron return finished; 2050a08a8471SStephen M. Cameron } 2051a08a8471SStephen M. Cameron 2052667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev, 2053667e23d4SStephen M. Cameron int qdepth, int reason) 2054667e23d4SStephen M. Cameron { 2055667e23d4SStephen M. Cameron struct ctlr_info *h = sdev_to_hba(sdev); 2056667e23d4SStephen M. Cameron 2057667e23d4SStephen M. Cameron if (reason != SCSI_QDEPTH_DEFAULT) 2058667e23d4SStephen M. Cameron return -ENOTSUPP; 2059667e23d4SStephen M. Cameron 2060667e23d4SStephen M. Cameron if (qdepth < 1) 2061667e23d4SStephen M. Cameron qdepth = 1; 2062667e23d4SStephen M. Cameron else 2063667e23d4SStephen M. Cameron if (qdepth > h->nr_cmds) 2064667e23d4SStephen M. Cameron qdepth = h->nr_cmds; 2065667e23d4SStephen M. Cameron scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth); 2066667e23d4SStephen M. Cameron return sdev->queue_depth; 2067667e23d4SStephen M. Cameron } 2068667e23d4SStephen M. Cameron 2069edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h) 2070edd16368SStephen M. Cameron { 2071edd16368SStephen M. Cameron /* we are being forcibly unloaded, and may not refuse. */ 2072edd16368SStephen M. Cameron scsi_remove_host(h->scsi_host); 2073edd16368SStephen M. Cameron scsi_host_put(h->scsi_host); 2074edd16368SStephen M. Cameron h->scsi_host = NULL; 2075edd16368SStephen M. Cameron } 2076edd16368SStephen M. Cameron 2077edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h) 2078edd16368SStephen M. Cameron { 2079edd16368SStephen M. Cameron int rc; 2080edd16368SStephen M. Cameron 2081edd16368SStephen M. Cameron rc = hpsa_scsi_detect(h); 2082edd16368SStephen M. Cameron if (rc != 0) 2083edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "hpsa_register_scsi: failed" 2084edd16368SStephen M. Cameron " hpsa_scsi_detect(), rc is %d\n", rc); 2085edd16368SStephen M. Cameron return rc; 2086edd16368SStephen M. Cameron } 2087edd16368SStephen M. Cameron 2088edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h, 2089edd16368SStephen M. Cameron unsigned char lunaddr[]) 2090edd16368SStephen M. Cameron { 2091edd16368SStephen M. Cameron int rc = 0; 2092edd16368SStephen M. Cameron int count = 0; 2093edd16368SStephen M. Cameron int waittime = 1; /* seconds */ 2094edd16368SStephen M. Cameron struct CommandList *c; 2095edd16368SStephen M. Cameron 2096edd16368SStephen M. Cameron c = cmd_special_alloc(h); 2097edd16368SStephen M. Cameron if (!c) { 2098edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "out of memory in " 2099edd16368SStephen M. Cameron "wait_for_device_to_become_ready.\n"); 2100edd16368SStephen M. Cameron return IO_ERROR; 2101edd16368SStephen M. Cameron } 2102edd16368SStephen M. Cameron 2103edd16368SStephen M. Cameron /* Send test unit ready until device ready, or give up. */ 2104edd16368SStephen M. Cameron while (count < HPSA_TUR_RETRY_LIMIT) { 2105edd16368SStephen M. Cameron 2106edd16368SStephen M. Cameron /* Wait for a bit. do this first, because if we send 2107edd16368SStephen M. Cameron * the TUR right away, the reset will just abort it. 2108edd16368SStephen M. Cameron */ 2109edd16368SStephen M. Cameron msleep(1000 * waittime); 2110edd16368SStephen M. Cameron count++; 2111edd16368SStephen M. Cameron 2112edd16368SStephen M. Cameron /* Increase wait time with each try, up to a point. */ 2113edd16368SStephen M. Cameron if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS) 2114edd16368SStephen M. Cameron waittime = waittime * 2; 2115edd16368SStephen M. Cameron 2116edd16368SStephen M. Cameron /* Send the Test Unit Ready */ 2117edd16368SStephen M. Cameron fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD); 2118edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 2119edd16368SStephen M. Cameron /* no unmap needed here because no data xfer. */ 2120edd16368SStephen M. Cameron 2121edd16368SStephen M. Cameron if (c->err_info->CommandStatus == CMD_SUCCESS) 2122edd16368SStephen M. Cameron break; 2123edd16368SStephen M. Cameron 2124edd16368SStephen M. Cameron if (c->err_info->CommandStatus == CMD_TARGET_STATUS && 2125edd16368SStephen M. Cameron c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION && 2126edd16368SStephen M. Cameron (c->err_info->SenseInfo[2] == NO_SENSE || 2127edd16368SStephen M. Cameron c->err_info->SenseInfo[2] == UNIT_ATTENTION)) 2128edd16368SStephen M. Cameron break; 2129edd16368SStephen M. Cameron 2130edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "waiting %d secs " 2131edd16368SStephen M. Cameron "for device to become ready.\n", waittime); 2132edd16368SStephen M. Cameron rc = 1; /* device not ready. */ 2133edd16368SStephen M. Cameron } 2134edd16368SStephen M. Cameron 2135edd16368SStephen M. Cameron if (rc) 2136edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "giving up on device.\n"); 2137edd16368SStephen M. Cameron else 2138edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "device is ready.\n"); 2139edd16368SStephen M. Cameron 2140edd16368SStephen M. Cameron cmd_special_free(h, c); 2141edd16368SStephen M. Cameron return rc; 2142edd16368SStephen M. Cameron } 2143edd16368SStephen M. Cameron 2144edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from 2145edd16368SStephen M. Cameron * complaining. Doing a host- or bus-reset can't do anything good here. 2146edd16368SStephen M. Cameron */ 2147edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd) 2148edd16368SStephen M. Cameron { 2149edd16368SStephen M. Cameron int rc; 2150edd16368SStephen M. Cameron struct ctlr_info *h; 2151edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *dev; 2152edd16368SStephen M. Cameron 2153edd16368SStephen M. Cameron /* find the controller to which the command to be aborted was sent */ 2154edd16368SStephen M. Cameron h = sdev_to_hba(scsicmd->device); 2155edd16368SStephen M. Cameron if (h == NULL) /* paranoia */ 2156edd16368SStephen M. Cameron return FAILED; 2157edd16368SStephen M. Cameron dev = scsicmd->device->hostdata; 2158edd16368SStephen M. Cameron if (!dev) { 2159edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: " 2160edd16368SStephen M. Cameron "device lookup failed.\n"); 2161edd16368SStephen M. Cameron return FAILED; 2162edd16368SStephen M. Cameron } 2163d416b0c7SStephen M. Cameron dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n", 2164d416b0c7SStephen M. Cameron h->scsi_host->host_no, dev->bus, dev->target, dev->lun); 2165edd16368SStephen M. Cameron /* send a reset to the SCSI LUN which the command was sent to */ 2166edd16368SStephen M. Cameron rc = hpsa_send_reset(h, dev->scsi3addr); 2167edd16368SStephen M. Cameron if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0) 2168edd16368SStephen M. Cameron return SUCCESS; 2169edd16368SStephen M. Cameron 2170edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "resetting device failed.\n"); 2171edd16368SStephen M. Cameron return FAILED; 2172edd16368SStephen M. Cameron } 2173edd16368SStephen M. Cameron 2174edd16368SStephen M. Cameron /* 2175edd16368SStephen M. Cameron * For operations that cannot sleep, a command block is allocated at init, 2176edd16368SStephen M. Cameron * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track 2177edd16368SStephen M. Cameron * which ones are free or in use. Lock must be held when calling this. 2178edd16368SStephen M. Cameron * cmd_free() is the complement. 2179edd16368SStephen M. Cameron */ 2180edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h) 2181edd16368SStephen M. Cameron { 2182edd16368SStephen M. Cameron struct CommandList *c; 2183edd16368SStephen M. Cameron int i; 2184edd16368SStephen M. Cameron union u64bit temp64; 2185edd16368SStephen M. Cameron dma_addr_t cmd_dma_handle, err_dma_handle; 2186edd16368SStephen M. Cameron 2187edd16368SStephen M. Cameron do { 2188edd16368SStephen M. Cameron i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds); 2189edd16368SStephen M. Cameron if (i == h->nr_cmds) 2190edd16368SStephen M. Cameron return NULL; 2191edd16368SStephen M. Cameron } while (test_and_set_bit 2192edd16368SStephen M. Cameron (i & (BITS_PER_LONG - 1), 2193edd16368SStephen M. Cameron h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0); 2194edd16368SStephen M. Cameron c = h->cmd_pool + i; 2195edd16368SStephen M. Cameron memset(c, 0, sizeof(*c)); 2196edd16368SStephen M. Cameron cmd_dma_handle = h->cmd_pool_dhandle 2197edd16368SStephen M. Cameron + i * sizeof(*c); 2198edd16368SStephen M. Cameron c->err_info = h->errinfo_pool + i; 2199edd16368SStephen M. Cameron memset(c->err_info, 0, sizeof(*c->err_info)); 2200edd16368SStephen M. Cameron err_dma_handle = h->errinfo_pool_dhandle 2201edd16368SStephen M. Cameron + i * sizeof(*c->err_info); 2202edd16368SStephen M. Cameron h->nr_allocs++; 2203edd16368SStephen M. Cameron 2204edd16368SStephen M. Cameron c->cmdindex = i; 2205edd16368SStephen M. Cameron 2206edd16368SStephen M. Cameron INIT_HLIST_NODE(&c->list); 220701a02ffcSStephen M. Cameron c->busaddr = (u32) cmd_dma_handle; 220801a02ffcSStephen M. Cameron temp64.val = (u64) err_dma_handle; 2209edd16368SStephen M. Cameron c->ErrDesc.Addr.lower = temp64.val32.lower; 2210edd16368SStephen M. Cameron c->ErrDesc.Addr.upper = temp64.val32.upper; 2211edd16368SStephen M. Cameron c->ErrDesc.Len = sizeof(*c->err_info); 2212edd16368SStephen M. Cameron 2213edd16368SStephen M. Cameron c->h = h; 2214edd16368SStephen M. Cameron return c; 2215edd16368SStephen M. Cameron } 2216edd16368SStephen M. Cameron 2217edd16368SStephen M. Cameron /* For operations that can wait for kmalloc to possibly sleep, 2218edd16368SStephen M. Cameron * this routine can be called. Lock need not be held to call 2219edd16368SStephen M. Cameron * cmd_special_alloc. cmd_special_free() is the complement. 2220edd16368SStephen M. Cameron */ 2221edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h) 2222edd16368SStephen M. Cameron { 2223edd16368SStephen M. Cameron struct CommandList *c; 2224edd16368SStephen M. Cameron union u64bit temp64; 2225edd16368SStephen M. Cameron dma_addr_t cmd_dma_handle, err_dma_handle; 2226edd16368SStephen M. Cameron 2227edd16368SStephen M. Cameron c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle); 2228edd16368SStephen M. Cameron if (c == NULL) 2229edd16368SStephen M. Cameron return NULL; 2230edd16368SStephen M. Cameron memset(c, 0, sizeof(*c)); 2231edd16368SStephen M. Cameron 2232edd16368SStephen M. Cameron c->cmdindex = -1; 2233edd16368SStephen M. Cameron 2234edd16368SStephen M. Cameron c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info), 2235edd16368SStephen M. Cameron &err_dma_handle); 2236edd16368SStephen M. Cameron 2237edd16368SStephen M. Cameron if (c->err_info == NULL) { 2238edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 2239edd16368SStephen M. Cameron sizeof(*c), c, cmd_dma_handle); 2240edd16368SStephen M. Cameron return NULL; 2241edd16368SStephen M. Cameron } 2242edd16368SStephen M. Cameron memset(c->err_info, 0, sizeof(*c->err_info)); 2243edd16368SStephen M. Cameron 2244edd16368SStephen M. Cameron INIT_HLIST_NODE(&c->list); 224501a02ffcSStephen M. Cameron c->busaddr = (u32) cmd_dma_handle; 224601a02ffcSStephen M. Cameron temp64.val = (u64) err_dma_handle; 2247edd16368SStephen M. Cameron c->ErrDesc.Addr.lower = temp64.val32.lower; 2248edd16368SStephen M. Cameron c->ErrDesc.Addr.upper = temp64.val32.upper; 2249edd16368SStephen M. Cameron c->ErrDesc.Len = sizeof(*c->err_info); 2250edd16368SStephen M. Cameron 2251edd16368SStephen M. Cameron c->h = h; 2252edd16368SStephen M. Cameron return c; 2253edd16368SStephen M. Cameron } 2254edd16368SStephen M. Cameron 2255edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c) 2256edd16368SStephen M. Cameron { 2257edd16368SStephen M. Cameron int i; 2258edd16368SStephen M. Cameron 2259edd16368SStephen M. Cameron i = c - h->cmd_pool; 2260edd16368SStephen M. Cameron clear_bit(i & (BITS_PER_LONG - 1), 2261edd16368SStephen M. Cameron h->cmd_pool_bits + (i / BITS_PER_LONG)); 2262edd16368SStephen M. Cameron h->nr_frees++; 2263edd16368SStephen M. Cameron } 2264edd16368SStephen M. Cameron 2265edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c) 2266edd16368SStephen M. Cameron { 2267edd16368SStephen M. Cameron union u64bit temp64; 2268edd16368SStephen M. Cameron 2269edd16368SStephen M. Cameron temp64.val32.lower = c->ErrDesc.Addr.lower; 2270edd16368SStephen M. Cameron temp64.val32.upper = c->ErrDesc.Addr.upper; 2271edd16368SStephen M. Cameron pci_free_consistent(h->pdev, sizeof(*c->err_info), 2272edd16368SStephen M. Cameron c->err_info, (dma_addr_t) temp64.val); 2273edd16368SStephen M. Cameron pci_free_consistent(h->pdev, sizeof(*c), 2274d896f3f3SStephen M. Cameron c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK)); 2275edd16368SStephen M. Cameron } 2276edd16368SStephen M. Cameron 2277edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT 2278edd16368SStephen M. Cameron 2279edd16368SStephen M. Cameron static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg) 2280edd16368SStephen M. Cameron { 2281edd16368SStephen M. Cameron IOCTL32_Command_struct __user *arg32 = 2282edd16368SStephen M. Cameron (IOCTL32_Command_struct __user *) arg; 2283edd16368SStephen M. Cameron IOCTL_Command_struct arg64; 2284edd16368SStephen M. Cameron IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64)); 2285edd16368SStephen M. Cameron int err; 2286edd16368SStephen M. Cameron u32 cp; 2287edd16368SStephen M. Cameron 2288edd16368SStephen M. Cameron err = 0; 2289edd16368SStephen M. Cameron err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, 2290edd16368SStephen M. Cameron sizeof(arg64.LUN_info)); 2291edd16368SStephen M. Cameron err |= copy_from_user(&arg64.Request, &arg32->Request, 2292edd16368SStephen M. Cameron sizeof(arg64.Request)); 2293edd16368SStephen M. Cameron err |= copy_from_user(&arg64.error_info, &arg32->error_info, 2294edd16368SStephen M. Cameron sizeof(arg64.error_info)); 2295edd16368SStephen M. Cameron err |= get_user(arg64.buf_size, &arg32->buf_size); 2296edd16368SStephen M. Cameron err |= get_user(cp, &arg32->buf); 2297edd16368SStephen M. Cameron arg64.buf = compat_ptr(cp); 2298edd16368SStephen M. Cameron err |= copy_to_user(p, &arg64, sizeof(arg64)); 2299edd16368SStephen M. Cameron 2300edd16368SStephen M. Cameron if (err) 2301edd16368SStephen M. Cameron return -EFAULT; 2302edd16368SStephen M. Cameron 2303e39eeaedSStephen M. Cameron err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p); 2304edd16368SStephen M. Cameron if (err) 2305edd16368SStephen M. Cameron return err; 2306edd16368SStephen M. Cameron err |= copy_in_user(&arg32->error_info, &p->error_info, 2307edd16368SStephen M. Cameron sizeof(arg32->error_info)); 2308edd16368SStephen M. Cameron if (err) 2309edd16368SStephen M. Cameron return -EFAULT; 2310edd16368SStephen M. Cameron return err; 2311edd16368SStephen M. Cameron } 2312edd16368SStephen M. Cameron 2313edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev, 2314edd16368SStephen M. Cameron int cmd, void *arg) 2315edd16368SStephen M. Cameron { 2316edd16368SStephen M. Cameron BIG_IOCTL32_Command_struct __user *arg32 = 2317edd16368SStephen M. Cameron (BIG_IOCTL32_Command_struct __user *) arg; 2318edd16368SStephen M. Cameron BIG_IOCTL_Command_struct arg64; 2319edd16368SStephen M. Cameron BIG_IOCTL_Command_struct __user *p = 2320edd16368SStephen M. Cameron compat_alloc_user_space(sizeof(arg64)); 2321edd16368SStephen M. Cameron int err; 2322edd16368SStephen M. Cameron u32 cp; 2323edd16368SStephen M. Cameron 2324edd16368SStephen M. Cameron err = 0; 2325edd16368SStephen M. Cameron err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, 2326edd16368SStephen M. Cameron sizeof(arg64.LUN_info)); 2327edd16368SStephen M. Cameron err |= copy_from_user(&arg64.Request, &arg32->Request, 2328edd16368SStephen M. Cameron sizeof(arg64.Request)); 2329edd16368SStephen M. Cameron err |= copy_from_user(&arg64.error_info, &arg32->error_info, 2330edd16368SStephen M. Cameron sizeof(arg64.error_info)); 2331edd16368SStephen M. Cameron err |= get_user(arg64.buf_size, &arg32->buf_size); 2332edd16368SStephen M. Cameron err |= get_user(arg64.malloc_size, &arg32->malloc_size); 2333edd16368SStephen M. Cameron err |= get_user(cp, &arg32->buf); 2334edd16368SStephen M. Cameron arg64.buf = compat_ptr(cp); 2335edd16368SStephen M. Cameron err |= copy_to_user(p, &arg64, sizeof(arg64)); 2336edd16368SStephen M. Cameron 2337edd16368SStephen M. Cameron if (err) 2338edd16368SStephen M. Cameron return -EFAULT; 2339edd16368SStephen M. Cameron 2340e39eeaedSStephen M. Cameron err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p); 2341edd16368SStephen M. Cameron if (err) 2342edd16368SStephen M. Cameron return err; 2343edd16368SStephen M. Cameron err |= copy_in_user(&arg32->error_info, &p->error_info, 2344edd16368SStephen M. Cameron sizeof(arg32->error_info)); 2345edd16368SStephen M. Cameron if (err) 2346edd16368SStephen M. Cameron return -EFAULT; 2347edd16368SStephen M. Cameron return err; 2348edd16368SStephen M. Cameron } 234971fe75a7SStephen M. Cameron 235071fe75a7SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg) 235171fe75a7SStephen M. Cameron { 235271fe75a7SStephen M. Cameron switch (cmd) { 235371fe75a7SStephen M. Cameron case CCISS_GETPCIINFO: 235471fe75a7SStephen M. Cameron case CCISS_GETINTINFO: 235571fe75a7SStephen M. Cameron case CCISS_SETINTINFO: 235671fe75a7SStephen M. Cameron case CCISS_GETNODENAME: 235771fe75a7SStephen M. Cameron case CCISS_SETNODENAME: 235871fe75a7SStephen M. Cameron case CCISS_GETHEARTBEAT: 235971fe75a7SStephen M. Cameron case CCISS_GETBUSTYPES: 236071fe75a7SStephen M. Cameron case CCISS_GETFIRMVER: 236171fe75a7SStephen M. Cameron case CCISS_GETDRIVVER: 236271fe75a7SStephen M. Cameron case CCISS_REVALIDVOLS: 236371fe75a7SStephen M. Cameron case CCISS_DEREGDISK: 236471fe75a7SStephen M. Cameron case CCISS_REGNEWDISK: 236571fe75a7SStephen M. Cameron case CCISS_REGNEWD: 236671fe75a7SStephen M. Cameron case CCISS_RESCANDISK: 236771fe75a7SStephen M. Cameron case CCISS_GETLUNINFO: 236871fe75a7SStephen M. Cameron return hpsa_ioctl(dev, cmd, arg); 236971fe75a7SStephen M. Cameron 237071fe75a7SStephen M. Cameron case CCISS_PASSTHRU32: 237171fe75a7SStephen M. Cameron return hpsa_ioctl32_passthru(dev, cmd, arg); 237271fe75a7SStephen M. Cameron case CCISS_BIG_PASSTHRU32: 237371fe75a7SStephen M. Cameron return hpsa_ioctl32_big_passthru(dev, cmd, arg); 237471fe75a7SStephen M. Cameron 237571fe75a7SStephen M. Cameron default: 237671fe75a7SStephen M. Cameron return -ENOIOCTLCMD; 237771fe75a7SStephen M. Cameron } 237871fe75a7SStephen M. Cameron } 2379edd16368SStephen M. Cameron #endif 2380edd16368SStephen M. Cameron 2381edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp) 2382edd16368SStephen M. Cameron { 2383edd16368SStephen M. Cameron struct hpsa_pci_info pciinfo; 2384edd16368SStephen M. Cameron 2385edd16368SStephen M. Cameron if (!argp) 2386edd16368SStephen M. Cameron return -EINVAL; 2387edd16368SStephen M. Cameron pciinfo.domain = pci_domain_nr(h->pdev->bus); 2388edd16368SStephen M. Cameron pciinfo.bus = h->pdev->bus->number; 2389edd16368SStephen M. Cameron pciinfo.dev_fn = h->pdev->devfn; 2390edd16368SStephen M. Cameron pciinfo.board_id = h->board_id; 2391edd16368SStephen M. Cameron if (copy_to_user(argp, &pciinfo, sizeof(pciinfo))) 2392edd16368SStephen M. Cameron return -EFAULT; 2393edd16368SStephen M. Cameron return 0; 2394edd16368SStephen M. Cameron } 2395edd16368SStephen M. Cameron 2396edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp) 2397edd16368SStephen M. Cameron { 2398edd16368SStephen M. Cameron DriverVer_type DriverVer; 2399edd16368SStephen M. Cameron unsigned char vmaj, vmin, vsubmin; 2400edd16368SStephen M. Cameron int rc; 2401edd16368SStephen M. Cameron 2402edd16368SStephen M. Cameron rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu", 2403edd16368SStephen M. Cameron &vmaj, &vmin, &vsubmin); 2404edd16368SStephen M. Cameron if (rc != 3) { 2405edd16368SStephen M. Cameron dev_info(&h->pdev->dev, "driver version string '%s' " 2406edd16368SStephen M. Cameron "unrecognized.", HPSA_DRIVER_VERSION); 2407edd16368SStephen M. Cameron vmaj = 0; 2408edd16368SStephen M. Cameron vmin = 0; 2409edd16368SStephen M. Cameron vsubmin = 0; 2410edd16368SStephen M. Cameron } 2411edd16368SStephen M. Cameron DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin; 2412edd16368SStephen M. Cameron if (!argp) 2413edd16368SStephen M. Cameron return -EINVAL; 2414edd16368SStephen M. Cameron if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type))) 2415edd16368SStephen M. Cameron return -EFAULT; 2416edd16368SStephen M. Cameron return 0; 2417edd16368SStephen M. Cameron } 2418edd16368SStephen M. Cameron 2419edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp) 2420edd16368SStephen M. Cameron { 2421edd16368SStephen M. Cameron IOCTL_Command_struct iocommand; 2422edd16368SStephen M. Cameron struct CommandList *c; 2423edd16368SStephen M. Cameron char *buff = NULL; 2424edd16368SStephen M. Cameron union u64bit temp64; 2425edd16368SStephen M. Cameron 2426edd16368SStephen M. Cameron if (!argp) 2427edd16368SStephen M. Cameron return -EINVAL; 2428edd16368SStephen M. Cameron if (!capable(CAP_SYS_RAWIO)) 2429edd16368SStephen M. Cameron return -EPERM; 2430edd16368SStephen M. Cameron if (copy_from_user(&iocommand, argp, sizeof(iocommand))) 2431edd16368SStephen M. Cameron return -EFAULT; 2432edd16368SStephen M. Cameron if ((iocommand.buf_size < 1) && 2433edd16368SStephen M. Cameron (iocommand.Request.Type.Direction != XFER_NONE)) { 2434edd16368SStephen M. Cameron return -EINVAL; 2435edd16368SStephen M. Cameron } 2436edd16368SStephen M. Cameron if (iocommand.buf_size > 0) { 2437edd16368SStephen M. Cameron buff = kmalloc(iocommand.buf_size, GFP_KERNEL); 2438edd16368SStephen M. Cameron if (buff == NULL) 2439edd16368SStephen M. Cameron return -EFAULT; 2440edd16368SStephen M. Cameron if (iocommand.Request.Type.Direction == XFER_WRITE) { 2441edd16368SStephen M. Cameron /* Copy the data into the buffer we created */ 2442b03a7771SStephen M. Cameron if (copy_from_user(buff, iocommand.buf, 2443b03a7771SStephen M. Cameron iocommand.buf_size)) { 2444edd16368SStephen M. Cameron kfree(buff); 2445edd16368SStephen M. Cameron return -EFAULT; 2446edd16368SStephen M. Cameron } 2447b03a7771SStephen M. Cameron } else { 2448edd16368SStephen M. Cameron memset(buff, 0, iocommand.buf_size); 2449b03a7771SStephen M. Cameron } 2450b03a7771SStephen M. Cameron } 2451edd16368SStephen M. Cameron c = cmd_special_alloc(h); 2452edd16368SStephen M. Cameron if (c == NULL) { 2453edd16368SStephen M. Cameron kfree(buff); 2454edd16368SStephen M. Cameron return -ENOMEM; 2455edd16368SStephen M. Cameron } 2456edd16368SStephen M. Cameron /* Fill in the command type */ 2457edd16368SStephen M. Cameron c->cmd_type = CMD_IOCTL_PEND; 2458edd16368SStephen M. Cameron /* Fill in Command Header */ 2459edd16368SStephen M. Cameron c->Header.ReplyQueue = 0; /* unused in simple mode */ 2460edd16368SStephen M. Cameron if (iocommand.buf_size > 0) { /* buffer to fill */ 2461edd16368SStephen M. Cameron c->Header.SGList = 1; 2462edd16368SStephen M. Cameron c->Header.SGTotal = 1; 2463edd16368SStephen M. Cameron } else { /* no buffers to fill */ 2464edd16368SStephen M. Cameron c->Header.SGList = 0; 2465edd16368SStephen M. Cameron c->Header.SGTotal = 0; 2466edd16368SStephen M. Cameron } 2467edd16368SStephen M. Cameron memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN)); 2468edd16368SStephen M. Cameron /* use the kernel address the cmd block for tag */ 2469edd16368SStephen M. Cameron c->Header.Tag.lower = c->busaddr; 2470edd16368SStephen M. Cameron 2471edd16368SStephen M. Cameron /* Fill in Request block */ 2472edd16368SStephen M. Cameron memcpy(&c->Request, &iocommand.Request, 2473edd16368SStephen M. Cameron sizeof(c->Request)); 2474edd16368SStephen M. Cameron 2475edd16368SStephen M. Cameron /* Fill in the scatter gather information */ 2476edd16368SStephen M. Cameron if (iocommand.buf_size > 0) { 2477edd16368SStephen M. Cameron temp64.val = pci_map_single(h->pdev, buff, 2478edd16368SStephen M. Cameron iocommand.buf_size, PCI_DMA_BIDIRECTIONAL); 2479edd16368SStephen M. Cameron c->SG[0].Addr.lower = temp64.val32.lower; 2480edd16368SStephen M. Cameron c->SG[0].Addr.upper = temp64.val32.upper; 2481edd16368SStephen M. Cameron c->SG[0].Len = iocommand.buf_size; 2482edd16368SStephen M. Cameron c->SG[0].Ext = 0; /* we are not chaining*/ 2483edd16368SStephen M. Cameron } 2484edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 2485edd16368SStephen M. Cameron hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL); 2486edd16368SStephen M. Cameron check_ioctl_unit_attention(h, c); 2487edd16368SStephen M. Cameron 2488edd16368SStephen M. Cameron /* Copy the error information out */ 2489edd16368SStephen M. Cameron memcpy(&iocommand.error_info, c->err_info, 2490edd16368SStephen M. Cameron sizeof(iocommand.error_info)); 2491edd16368SStephen M. Cameron if (copy_to_user(argp, &iocommand, sizeof(iocommand))) { 2492edd16368SStephen M. Cameron kfree(buff); 2493edd16368SStephen M. Cameron cmd_special_free(h, c); 2494edd16368SStephen M. Cameron return -EFAULT; 2495edd16368SStephen M. Cameron } 2496b03a7771SStephen M. Cameron if (iocommand.Request.Type.Direction == XFER_READ && 2497b03a7771SStephen M. Cameron iocommand.buf_size > 0) { 2498edd16368SStephen M. Cameron /* Copy the data out of the buffer we created */ 2499edd16368SStephen M. Cameron if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) { 2500edd16368SStephen M. Cameron kfree(buff); 2501edd16368SStephen M. Cameron cmd_special_free(h, c); 2502edd16368SStephen M. Cameron return -EFAULT; 2503edd16368SStephen M. Cameron } 2504edd16368SStephen M. Cameron } 2505edd16368SStephen M. Cameron kfree(buff); 2506edd16368SStephen M. Cameron cmd_special_free(h, c); 2507edd16368SStephen M. Cameron return 0; 2508edd16368SStephen M. Cameron } 2509edd16368SStephen M. Cameron 2510edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp) 2511edd16368SStephen M. Cameron { 2512edd16368SStephen M. Cameron BIG_IOCTL_Command_struct *ioc; 2513edd16368SStephen M. Cameron struct CommandList *c; 2514edd16368SStephen M. Cameron unsigned char **buff = NULL; 2515edd16368SStephen M. Cameron int *buff_size = NULL; 2516edd16368SStephen M. Cameron union u64bit temp64; 2517edd16368SStephen M. Cameron BYTE sg_used = 0; 2518edd16368SStephen M. Cameron int status = 0; 2519edd16368SStephen M. Cameron int i; 252001a02ffcSStephen M. Cameron u32 left; 252101a02ffcSStephen M. Cameron u32 sz; 2522edd16368SStephen M. Cameron BYTE __user *data_ptr; 2523edd16368SStephen M. Cameron 2524edd16368SStephen M. Cameron if (!argp) 2525edd16368SStephen M. Cameron return -EINVAL; 2526edd16368SStephen M. Cameron if (!capable(CAP_SYS_RAWIO)) 2527edd16368SStephen M. Cameron return -EPERM; 2528edd16368SStephen M. Cameron ioc = (BIG_IOCTL_Command_struct *) 2529edd16368SStephen M. Cameron kmalloc(sizeof(*ioc), GFP_KERNEL); 2530edd16368SStephen M. Cameron if (!ioc) { 2531edd16368SStephen M. Cameron status = -ENOMEM; 2532edd16368SStephen M. Cameron goto cleanup1; 2533edd16368SStephen M. Cameron } 2534edd16368SStephen M. Cameron if (copy_from_user(ioc, argp, sizeof(*ioc))) { 2535edd16368SStephen M. Cameron status = -EFAULT; 2536edd16368SStephen M. Cameron goto cleanup1; 2537edd16368SStephen M. Cameron } 2538edd16368SStephen M. Cameron if ((ioc->buf_size < 1) && 2539edd16368SStephen M. Cameron (ioc->Request.Type.Direction != XFER_NONE)) { 2540edd16368SStephen M. Cameron status = -EINVAL; 2541edd16368SStephen M. Cameron goto cleanup1; 2542edd16368SStephen M. Cameron } 2543edd16368SStephen M. Cameron /* Check kmalloc limits using all SGs */ 2544edd16368SStephen M. Cameron if (ioc->malloc_size > MAX_KMALLOC_SIZE) { 2545edd16368SStephen M. Cameron status = -EINVAL; 2546edd16368SStephen M. Cameron goto cleanup1; 2547edd16368SStephen M. Cameron } 2548edd16368SStephen M. Cameron if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) { 2549edd16368SStephen M. Cameron status = -EINVAL; 2550edd16368SStephen M. Cameron goto cleanup1; 2551edd16368SStephen M. Cameron } 2552edd16368SStephen M. Cameron buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL); 2553edd16368SStephen M. Cameron if (!buff) { 2554edd16368SStephen M. Cameron status = -ENOMEM; 2555edd16368SStephen M. Cameron goto cleanup1; 2556edd16368SStephen M. Cameron } 2557edd16368SStephen M. Cameron buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL); 2558edd16368SStephen M. Cameron if (!buff_size) { 2559edd16368SStephen M. Cameron status = -ENOMEM; 2560edd16368SStephen M. Cameron goto cleanup1; 2561edd16368SStephen M. Cameron } 2562edd16368SStephen M. Cameron left = ioc->buf_size; 2563edd16368SStephen M. Cameron data_ptr = ioc->buf; 2564edd16368SStephen M. Cameron while (left) { 2565edd16368SStephen M. Cameron sz = (left > ioc->malloc_size) ? ioc->malloc_size : left; 2566edd16368SStephen M. Cameron buff_size[sg_used] = sz; 2567edd16368SStephen M. Cameron buff[sg_used] = kmalloc(sz, GFP_KERNEL); 2568edd16368SStephen M. Cameron if (buff[sg_used] == NULL) { 2569edd16368SStephen M. Cameron status = -ENOMEM; 2570edd16368SStephen M. Cameron goto cleanup1; 2571edd16368SStephen M. Cameron } 2572edd16368SStephen M. Cameron if (ioc->Request.Type.Direction == XFER_WRITE) { 2573edd16368SStephen M. Cameron if (copy_from_user(buff[sg_used], data_ptr, sz)) { 2574edd16368SStephen M. Cameron status = -ENOMEM; 2575edd16368SStephen M. Cameron goto cleanup1; 2576edd16368SStephen M. Cameron } 2577edd16368SStephen M. Cameron } else 2578edd16368SStephen M. Cameron memset(buff[sg_used], 0, sz); 2579edd16368SStephen M. Cameron left -= sz; 2580edd16368SStephen M. Cameron data_ptr += sz; 2581edd16368SStephen M. Cameron sg_used++; 2582edd16368SStephen M. Cameron } 2583edd16368SStephen M. Cameron c = cmd_special_alloc(h); 2584edd16368SStephen M. Cameron if (c == NULL) { 2585edd16368SStephen M. Cameron status = -ENOMEM; 2586edd16368SStephen M. Cameron goto cleanup1; 2587edd16368SStephen M. Cameron } 2588edd16368SStephen M. Cameron c->cmd_type = CMD_IOCTL_PEND; 2589edd16368SStephen M. Cameron c->Header.ReplyQueue = 0; 2590b03a7771SStephen M. Cameron c->Header.SGList = c->Header.SGTotal = sg_used; 2591edd16368SStephen M. Cameron memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN)); 2592edd16368SStephen M. Cameron c->Header.Tag.lower = c->busaddr; 2593edd16368SStephen M. Cameron memcpy(&c->Request, &ioc->Request, sizeof(c->Request)); 2594edd16368SStephen M. Cameron if (ioc->buf_size > 0) { 2595edd16368SStephen M. Cameron int i; 2596edd16368SStephen M. Cameron for (i = 0; i < sg_used; i++) { 2597edd16368SStephen M. Cameron temp64.val = pci_map_single(h->pdev, buff[i], 2598edd16368SStephen M. Cameron buff_size[i], PCI_DMA_BIDIRECTIONAL); 2599edd16368SStephen M. Cameron c->SG[i].Addr.lower = temp64.val32.lower; 2600edd16368SStephen M. Cameron c->SG[i].Addr.upper = temp64.val32.upper; 2601edd16368SStephen M. Cameron c->SG[i].Len = buff_size[i]; 2602edd16368SStephen M. Cameron /* we are not chaining */ 2603edd16368SStephen M. Cameron c->SG[i].Ext = 0; 2604edd16368SStephen M. Cameron } 2605edd16368SStephen M. Cameron } 2606edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 2607b03a7771SStephen M. Cameron if (sg_used) 2608edd16368SStephen M. Cameron hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL); 2609edd16368SStephen M. Cameron check_ioctl_unit_attention(h, c); 2610edd16368SStephen M. Cameron /* Copy the error information out */ 2611edd16368SStephen M. Cameron memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info)); 2612edd16368SStephen M. Cameron if (copy_to_user(argp, ioc, sizeof(*ioc))) { 2613edd16368SStephen M. Cameron cmd_special_free(h, c); 2614edd16368SStephen M. Cameron status = -EFAULT; 2615edd16368SStephen M. Cameron goto cleanup1; 2616edd16368SStephen M. Cameron } 2617b03a7771SStephen M. Cameron if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) { 2618edd16368SStephen M. Cameron /* Copy the data out of the buffer we created */ 2619edd16368SStephen M. Cameron BYTE __user *ptr = ioc->buf; 2620edd16368SStephen M. Cameron for (i = 0; i < sg_used; i++) { 2621edd16368SStephen M. Cameron if (copy_to_user(ptr, buff[i], buff_size[i])) { 2622edd16368SStephen M. Cameron cmd_special_free(h, c); 2623edd16368SStephen M. Cameron status = -EFAULT; 2624edd16368SStephen M. Cameron goto cleanup1; 2625edd16368SStephen M. Cameron } 2626edd16368SStephen M. Cameron ptr += buff_size[i]; 2627edd16368SStephen M. Cameron } 2628edd16368SStephen M. Cameron } 2629edd16368SStephen M. Cameron cmd_special_free(h, c); 2630edd16368SStephen M. Cameron status = 0; 2631edd16368SStephen M. Cameron cleanup1: 2632edd16368SStephen M. Cameron if (buff) { 2633edd16368SStephen M. Cameron for (i = 0; i < sg_used; i++) 2634edd16368SStephen M. Cameron kfree(buff[i]); 2635edd16368SStephen M. Cameron kfree(buff); 2636edd16368SStephen M. Cameron } 2637edd16368SStephen M. Cameron kfree(buff_size); 2638edd16368SStephen M. Cameron kfree(ioc); 2639edd16368SStephen M. Cameron return status; 2640edd16368SStephen M. Cameron } 2641edd16368SStephen M. Cameron 2642edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h, 2643edd16368SStephen M. Cameron struct CommandList *c) 2644edd16368SStephen M. Cameron { 2645edd16368SStephen M. Cameron if (c->err_info->CommandStatus == CMD_TARGET_STATUS && 2646edd16368SStephen M. Cameron c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) 2647edd16368SStephen M. Cameron (void) check_for_unit_attention(h, c); 2648edd16368SStephen M. Cameron } 2649edd16368SStephen M. Cameron /* 2650edd16368SStephen M. Cameron * ioctl 2651edd16368SStephen M. Cameron */ 2652edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg) 2653edd16368SStephen M. Cameron { 2654edd16368SStephen M. Cameron struct ctlr_info *h; 2655edd16368SStephen M. Cameron void __user *argp = (void __user *)arg; 2656edd16368SStephen M. Cameron 2657edd16368SStephen M. Cameron h = sdev_to_hba(dev); 2658edd16368SStephen M. Cameron 2659edd16368SStephen M. Cameron switch (cmd) { 2660edd16368SStephen M. Cameron case CCISS_DEREGDISK: 2661edd16368SStephen M. Cameron case CCISS_REGNEWDISK: 2662edd16368SStephen M. Cameron case CCISS_REGNEWD: 2663a08a8471SStephen M. Cameron hpsa_scan_start(h->scsi_host); 2664edd16368SStephen M. Cameron return 0; 2665edd16368SStephen M. Cameron case CCISS_GETPCIINFO: 2666edd16368SStephen M. Cameron return hpsa_getpciinfo_ioctl(h, argp); 2667edd16368SStephen M. Cameron case CCISS_GETDRIVVER: 2668edd16368SStephen M. Cameron return hpsa_getdrivver_ioctl(h, argp); 2669edd16368SStephen M. Cameron case CCISS_PASSTHRU: 2670edd16368SStephen M. Cameron return hpsa_passthru_ioctl(h, argp); 2671edd16368SStephen M. Cameron case CCISS_BIG_PASSTHRU: 2672edd16368SStephen M. Cameron return hpsa_big_passthru_ioctl(h, argp); 2673edd16368SStephen M. Cameron default: 2674edd16368SStephen M. Cameron return -ENOTTY; 2675edd16368SStephen M. Cameron } 2676edd16368SStephen M. Cameron } 2677edd16368SStephen M. Cameron 267801a02ffcSStephen M. Cameron static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h, 267901a02ffcSStephen M. Cameron void *buff, size_t size, u8 page_code, unsigned char *scsi3addr, 2680edd16368SStephen M. Cameron int cmd_type) 2681edd16368SStephen M. Cameron { 2682edd16368SStephen M. Cameron int pci_dir = XFER_NONE; 2683edd16368SStephen M. Cameron 2684edd16368SStephen M. Cameron c->cmd_type = CMD_IOCTL_PEND; 2685edd16368SStephen M. Cameron c->Header.ReplyQueue = 0; 2686edd16368SStephen M. Cameron if (buff != NULL && size > 0) { 2687edd16368SStephen M. Cameron c->Header.SGList = 1; 2688edd16368SStephen M. Cameron c->Header.SGTotal = 1; 2689edd16368SStephen M. Cameron } else { 2690edd16368SStephen M. Cameron c->Header.SGList = 0; 2691edd16368SStephen M. Cameron c->Header.SGTotal = 0; 2692edd16368SStephen M. Cameron } 2693edd16368SStephen M. Cameron c->Header.Tag.lower = c->busaddr; 2694edd16368SStephen M. Cameron memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8); 2695edd16368SStephen M. Cameron 2696edd16368SStephen M. Cameron c->Request.Type.Type = cmd_type; 2697edd16368SStephen M. Cameron if (cmd_type == TYPE_CMD) { 2698edd16368SStephen M. Cameron switch (cmd) { 2699edd16368SStephen M. Cameron case HPSA_INQUIRY: 2700edd16368SStephen M. Cameron /* are we trying to read a vital product page */ 2701edd16368SStephen M. Cameron if (page_code != 0) { 2702edd16368SStephen M. Cameron c->Request.CDB[1] = 0x01; 2703edd16368SStephen M. Cameron c->Request.CDB[2] = page_code; 2704edd16368SStephen M. Cameron } 2705edd16368SStephen M. Cameron c->Request.CDBLen = 6; 2706edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2707edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_READ; 2708edd16368SStephen M. Cameron c->Request.Timeout = 0; 2709edd16368SStephen M. Cameron c->Request.CDB[0] = HPSA_INQUIRY; 2710edd16368SStephen M. Cameron c->Request.CDB[4] = size & 0xFF; 2711edd16368SStephen M. Cameron break; 2712edd16368SStephen M. Cameron case HPSA_REPORT_LOG: 2713edd16368SStephen M. Cameron case HPSA_REPORT_PHYS: 2714edd16368SStephen M. Cameron /* Talking to controller so It's a physical command 2715edd16368SStephen M. Cameron mode = 00 target = 0. Nothing to write. 2716edd16368SStephen M. Cameron */ 2717edd16368SStephen M. Cameron c->Request.CDBLen = 12; 2718edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2719edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_READ; 2720edd16368SStephen M. Cameron c->Request.Timeout = 0; 2721edd16368SStephen M. Cameron c->Request.CDB[0] = cmd; 2722edd16368SStephen M. Cameron c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */ 2723edd16368SStephen M. Cameron c->Request.CDB[7] = (size >> 16) & 0xFF; 2724edd16368SStephen M. Cameron c->Request.CDB[8] = (size >> 8) & 0xFF; 2725edd16368SStephen M. Cameron c->Request.CDB[9] = size & 0xFF; 2726edd16368SStephen M. Cameron break; 2727edd16368SStephen M. Cameron case HPSA_CACHE_FLUSH: 2728edd16368SStephen M. Cameron c->Request.CDBLen = 12; 2729edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2730edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_WRITE; 2731edd16368SStephen M. Cameron c->Request.Timeout = 0; 2732edd16368SStephen M. Cameron c->Request.CDB[0] = BMIC_WRITE; 2733edd16368SStephen M. Cameron c->Request.CDB[6] = BMIC_CACHE_FLUSH; 2734edd16368SStephen M. Cameron break; 2735edd16368SStephen M. Cameron case TEST_UNIT_READY: 2736edd16368SStephen M. Cameron c->Request.CDBLen = 6; 2737edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2738edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_NONE; 2739edd16368SStephen M. Cameron c->Request.Timeout = 0; 2740edd16368SStephen M. Cameron break; 2741edd16368SStephen M. Cameron default: 2742edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd); 2743edd16368SStephen M. Cameron BUG(); 2744edd16368SStephen M. Cameron return; 2745edd16368SStephen M. Cameron } 2746edd16368SStephen M. Cameron } else if (cmd_type == TYPE_MSG) { 2747edd16368SStephen M. Cameron switch (cmd) { 2748edd16368SStephen M. Cameron 2749edd16368SStephen M. Cameron case HPSA_DEVICE_RESET_MSG: 2750edd16368SStephen M. Cameron c->Request.CDBLen = 16; 2751edd16368SStephen M. Cameron c->Request.Type.Type = 1; /* It is a MSG not a CMD */ 2752edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2753edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_NONE; 2754edd16368SStephen M. Cameron c->Request.Timeout = 0; /* Don't time out */ 2755edd16368SStephen M. Cameron c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */ 2756edd16368SStephen M. Cameron c->Request.CDB[1] = 0x03; /* Reset target above */ 2757edd16368SStephen M. Cameron /* If bytes 4-7 are zero, it means reset the */ 2758edd16368SStephen M. Cameron /* LunID device */ 2759edd16368SStephen M. Cameron c->Request.CDB[4] = 0x00; 2760edd16368SStephen M. Cameron c->Request.CDB[5] = 0x00; 2761edd16368SStephen M. Cameron c->Request.CDB[6] = 0x00; 2762edd16368SStephen M. Cameron c->Request.CDB[7] = 0x00; 2763edd16368SStephen M. Cameron break; 2764edd16368SStephen M. Cameron 2765edd16368SStephen M. Cameron default: 2766edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "unknown message type %d\n", 2767edd16368SStephen M. Cameron cmd); 2768edd16368SStephen M. Cameron BUG(); 2769edd16368SStephen M. Cameron } 2770edd16368SStephen M. Cameron } else { 2771edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type); 2772edd16368SStephen M. Cameron BUG(); 2773edd16368SStephen M. Cameron } 2774edd16368SStephen M. Cameron 2775edd16368SStephen M. Cameron switch (c->Request.Type.Direction) { 2776edd16368SStephen M. Cameron case XFER_READ: 2777edd16368SStephen M. Cameron pci_dir = PCI_DMA_FROMDEVICE; 2778edd16368SStephen M. Cameron break; 2779edd16368SStephen M. Cameron case XFER_WRITE: 2780edd16368SStephen M. Cameron pci_dir = PCI_DMA_TODEVICE; 2781edd16368SStephen M. Cameron break; 2782edd16368SStephen M. Cameron case XFER_NONE: 2783edd16368SStephen M. Cameron pci_dir = PCI_DMA_NONE; 2784edd16368SStephen M. Cameron break; 2785edd16368SStephen M. Cameron default: 2786edd16368SStephen M. Cameron pci_dir = PCI_DMA_BIDIRECTIONAL; 2787edd16368SStephen M. Cameron } 2788edd16368SStephen M. Cameron 2789edd16368SStephen M. Cameron hpsa_map_one(h->pdev, c, buff, size, pci_dir); 2790edd16368SStephen M. Cameron 2791edd16368SStephen M. Cameron return; 2792edd16368SStephen M. Cameron } 2793edd16368SStephen M. Cameron 2794edd16368SStephen M. Cameron /* 2795edd16368SStephen M. Cameron * Map (physical) PCI mem into (virtual) kernel space 2796edd16368SStephen M. Cameron */ 2797edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size) 2798edd16368SStephen M. Cameron { 2799edd16368SStephen M. Cameron ulong page_base = ((ulong) base) & PAGE_MASK; 2800edd16368SStephen M. Cameron ulong page_offs = ((ulong) base) - page_base; 2801edd16368SStephen M. Cameron void __iomem *page_remapped = ioremap(page_base, page_offs + size); 2802edd16368SStephen M. Cameron 2803edd16368SStephen M. Cameron return page_remapped ? (page_remapped + page_offs) : NULL; 2804edd16368SStephen M. Cameron } 2805edd16368SStephen M. Cameron 2806edd16368SStephen M. Cameron /* Takes cmds off the submission queue and sends them to the hardware, 2807edd16368SStephen M. Cameron * then puts them on the queue of cmds waiting for completion. 2808edd16368SStephen M. Cameron */ 2809edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h) 2810edd16368SStephen M. Cameron { 2811edd16368SStephen M. Cameron struct CommandList *c; 2812edd16368SStephen M. Cameron 2813edd16368SStephen M. Cameron while (!hlist_empty(&h->reqQ)) { 2814edd16368SStephen M. Cameron c = hlist_entry(h->reqQ.first, struct CommandList, list); 2815edd16368SStephen M. Cameron /* can't do anything if fifo is full */ 2816edd16368SStephen M. Cameron if ((h->access.fifo_full(h))) { 2817edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "fifo full\n"); 2818edd16368SStephen M. Cameron break; 2819edd16368SStephen M. Cameron } 2820edd16368SStephen M. Cameron 2821edd16368SStephen M. Cameron /* Get the first entry from the Request Q */ 2822edd16368SStephen M. Cameron removeQ(c); 2823edd16368SStephen M. Cameron h->Qdepth--; 2824edd16368SStephen M. Cameron 2825edd16368SStephen M. Cameron /* Tell the controller execute command */ 2826edd16368SStephen M. Cameron h->access.submit_command(h, c); 2827edd16368SStephen M. Cameron 2828edd16368SStephen M. Cameron /* Put job onto the completed Q */ 2829edd16368SStephen M. Cameron addQ(&h->cmpQ, c); 2830edd16368SStephen M. Cameron } 2831edd16368SStephen M. Cameron } 2832edd16368SStephen M. Cameron 2833edd16368SStephen M. Cameron static inline unsigned long get_next_completion(struct ctlr_info *h) 2834edd16368SStephen M. Cameron { 2835edd16368SStephen M. Cameron return h->access.command_completed(h); 2836edd16368SStephen M. Cameron } 2837edd16368SStephen M. Cameron 2838900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h) 2839edd16368SStephen M. Cameron { 2840edd16368SStephen M. Cameron return h->access.intr_pending(h); 2841edd16368SStephen M. Cameron } 2842edd16368SStephen M. Cameron 2843edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h) 2844edd16368SStephen M. Cameron { 284510f66018SStephen M. Cameron return (h->access.intr_pending(h) == 0) || 284610f66018SStephen M. Cameron (h->interrupts_enabled == 0); 2847edd16368SStephen M. Cameron } 2848edd16368SStephen M. Cameron 284901a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index, 285001a02ffcSStephen M. Cameron u32 raw_tag) 2851edd16368SStephen M. Cameron { 2852edd16368SStephen M. Cameron if (unlikely(tag_index >= h->nr_cmds)) { 2853edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag); 2854edd16368SStephen M. Cameron return 1; 2855edd16368SStephen M. Cameron } 2856edd16368SStephen M. Cameron return 0; 2857edd16368SStephen M. Cameron } 2858edd16368SStephen M. Cameron 285901a02ffcSStephen M. Cameron static inline void finish_cmd(struct CommandList *c, u32 raw_tag) 2860edd16368SStephen M. Cameron { 2861edd16368SStephen M. Cameron removeQ(c); 2862edd16368SStephen M. Cameron if (likely(c->cmd_type == CMD_SCSI)) 2863edd16368SStephen M. Cameron complete_scsi_command(c, 0, raw_tag); 2864edd16368SStephen M. Cameron else if (c->cmd_type == CMD_IOCTL_PEND) 2865edd16368SStephen M. Cameron complete(c->waiting); 2866edd16368SStephen M. Cameron } 2867edd16368SStephen M. Cameron 2868a104c99fSStephen M. Cameron static inline u32 hpsa_tag_contains_index(u32 tag) 2869a104c99fSStephen M. Cameron { 2870a104c99fSStephen M. Cameron return tag & DIRECT_LOOKUP_BIT; 2871a104c99fSStephen M. Cameron } 2872a104c99fSStephen M. Cameron 2873a104c99fSStephen M. Cameron static inline u32 hpsa_tag_to_index(u32 tag) 2874a104c99fSStephen M. Cameron { 2875a104c99fSStephen M. Cameron return tag >> DIRECT_LOOKUP_SHIFT; 2876a104c99fSStephen M. Cameron } 2877a104c99fSStephen M. Cameron 2878a104c99fSStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(u32 tag) 2879a104c99fSStephen M. Cameron { 2880a104c99fSStephen M. Cameron #define HPSA_ERROR_BITS 0x03 2881a104c99fSStephen M. Cameron return tag & ~HPSA_ERROR_BITS; 2882a104c99fSStephen M. Cameron } 2883a104c99fSStephen M. Cameron 2884303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */ 2885303932fdSDon Brace static inline u32 process_indexed_cmd(struct ctlr_info *h, 2886303932fdSDon Brace u32 raw_tag) 2887303932fdSDon Brace { 2888303932fdSDon Brace u32 tag_index; 2889303932fdSDon Brace struct CommandList *c; 2890303932fdSDon Brace 2891303932fdSDon Brace tag_index = hpsa_tag_to_index(raw_tag); 2892303932fdSDon Brace if (bad_tag(h, tag_index, raw_tag)) 2893303932fdSDon Brace return next_command(h); 2894303932fdSDon Brace c = h->cmd_pool + tag_index; 2895303932fdSDon Brace finish_cmd(c, raw_tag); 2896303932fdSDon Brace return next_command(h); 2897303932fdSDon Brace } 2898303932fdSDon Brace 2899303932fdSDon Brace /* process completion of a non-indexed command */ 2900303932fdSDon Brace static inline u32 process_nonindexed_cmd(struct ctlr_info *h, 2901303932fdSDon Brace u32 raw_tag) 2902303932fdSDon Brace { 2903303932fdSDon Brace u32 tag; 2904303932fdSDon Brace struct CommandList *c = NULL; 2905303932fdSDon Brace struct hlist_node *tmp; 2906303932fdSDon Brace 2907303932fdSDon Brace tag = hpsa_tag_discard_error_bits(raw_tag); 2908303932fdSDon Brace hlist_for_each_entry(c, tmp, &h->cmpQ, list) { 2909303932fdSDon Brace if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) { 2910303932fdSDon Brace finish_cmd(c, raw_tag); 2911303932fdSDon Brace return next_command(h); 2912303932fdSDon Brace } 2913303932fdSDon Brace } 2914303932fdSDon Brace bad_tag(h, h->nr_cmds + 1, raw_tag); 2915303932fdSDon Brace return next_command(h); 2916303932fdSDon Brace } 2917303932fdSDon Brace 291810f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id) 2919edd16368SStephen M. Cameron { 2920edd16368SStephen M. Cameron struct ctlr_info *h = dev_id; 2921edd16368SStephen M. Cameron unsigned long flags; 2922303932fdSDon Brace u32 raw_tag; 2923edd16368SStephen M. Cameron 2924edd16368SStephen M. Cameron if (interrupt_not_for_us(h)) 2925edd16368SStephen M. Cameron return IRQ_NONE; 2926edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 292710f66018SStephen M. Cameron while (interrupt_pending(h)) { 292810f66018SStephen M. Cameron raw_tag = get_next_completion(h); 292910f66018SStephen M. Cameron while (raw_tag != FIFO_EMPTY) { 293010f66018SStephen M. Cameron if (hpsa_tag_contains_index(raw_tag)) 293110f66018SStephen M. Cameron raw_tag = process_indexed_cmd(h, raw_tag); 293210f66018SStephen M. Cameron else 293310f66018SStephen M. Cameron raw_tag = process_nonindexed_cmd(h, raw_tag); 293410f66018SStephen M. Cameron } 293510f66018SStephen M. Cameron } 293610f66018SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 293710f66018SStephen M. Cameron return IRQ_HANDLED; 293810f66018SStephen M. Cameron } 293910f66018SStephen M. Cameron 294010f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id) 294110f66018SStephen M. Cameron { 294210f66018SStephen M. Cameron struct ctlr_info *h = dev_id; 294310f66018SStephen M. Cameron unsigned long flags; 294410f66018SStephen M. Cameron u32 raw_tag; 294510f66018SStephen M. Cameron 294610f66018SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 2947303932fdSDon Brace raw_tag = get_next_completion(h); 2948303932fdSDon Brace while (raw_tag != FIFO_EMPTY) { 2949303932fdSDon Brace if (hpsa_tag_contains_index(raw_tag)) 2950303932fdSDon Brace raw_tag = process_indexed_cmd(h, raw_tag); 2951303932fdSDon Brace else 2952303932fdSDon Brace raw_tag = process_nonindexed_cmd(h, raw_tag); 2953edd16368SStephen M. Cameron } 2954edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 2955edd16368SStephen M. Cameron return IRQ_HANDLED; 2956edd16368SStephen M. Cameron } 2957edd16368SStephen M. Cameron 2958f0edafc6SStephen M. Cameron /* Send a message CDB to the firmware. */ 2959edd16368SStephen M. Cameron static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode, 2960edd16368SStephen M. Cameron unsigned char type) 2961edd16368SStephen M. Cameron { 2962edd16368SStephen M. Cameron struct Command { 2963edd16368SStephen M. Cameron struct CommandListHeader CommandHeader; 2964edd16368SStephen M. Cameron struct RequestBlock Request; 2965edd16368SStephen M. Cameron struct ErrDescriptor ErrorDescriptor; 2966edd16368SStephen M. Cameron }; 2967edd16368SStephen M. Cameron struct Command *cmd; 2968edd16368SStephen M. Cameron static const size_t cmd_sz = sizeof(*cmd) + 2969edd16368SStephen M. Cameron sizeof(cmd->ErrorDescriptor); 2970edd16368SStephen M. Cameron dma_addr_t paddr64; 2971edd16368SStephen M. Cameron uint32_t paddr32, tag; 2972edd16368SStephen M. Cameron void __iomem *vaddr; 2973edd16368SStephen M. Cameron int i, err; 2974edd16368SStephen M. Cameron 2975edd16368SStephen M. Cameron vaddr = pci_ioremap_bar(pdev, 0); 2976edd16368SStephen M. Cameron if (vaddr == NULL) 2977edd16368SStephen M. Cameron return -ENOMEM; 2978edd16368SStephen M. Cameron 2979edd16368SStephen M. Cameron /* The Inbound Post Queue only accepts 32-bit physical addresses for the 2980edd16368SStephen M. Cameron * CCISS commands, so they must be allocated from the lower 4GiB of 2981edd16368SStephen M. Cameron * memory. 2982edd16368SStephen M. Cameron */ 2983edd16368SStephen M. Cameron err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 2984edd16368SStephen M. Cameron if (err) { 2985edd16368SStephen M. Cameron iounmap(vaddr); 2986edd16368SStephen M. Cameron return -ENOMEM; 2987edd16368SStephen M. Cameron } 2988edd16368SStephen M. Cameron 2989edd16368SStephen M. Cameron cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64); 2990edd16368SStephen M. Cameron if (cmd == NULL) { 2991edd16368SStephen M. Cameron iounmap(vaddr); 2992edd16368SStephen M. Cameron return -ENOMEM; 2993edd16368SStephen M. Cameron } 2994edd16368SStephen M. Cameron 2995edd16368SStephen M. Cameron /* This must fit, because of the 32-bit consistent DMA mask. Also, 2996edd16368SStephen M. Cameron * although there's no guarantee, we assume that the address is at 2997edd16368SStephen M. Cameron * least 4-byte aligned (most likely, it's page-aligned). 2998edd16368SStephen M. Cameron */ 2999edd16368SStephen M. Cameron paddr32 = paddr64; 3000edd16368SStephen M. Cameron 3001edd16368SStephen M. Cameron cmd->CommandHeader.ReplyQueue = 0; 3002edd16368SStephen M. Cameron cmd->CommandHeader.SGList = 0; 3003edd16368SStephen M. Cameron cmd->CommandHeader.SGTotal = 0; 3004edd16368SStephen M. Cameron cmd->CommandHeader.Tag.lower = paddr32; 3005edd16368SStephen M. Cameron cmd->CommandHeader.Tag.upper = 0; 3006edd16368SStephen M. Cameron memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8); 3007edd16368SStephen M. Cameron 3008edd16368SStephen M. Cameron cmd->Request.CDBLen = 16; 3009edd16368SStephen M. Cameron cmd->Request.Type.Type = TYPE_MSG; 3010edd16368SStephen M. Cameron cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE; 3011edd16368SStephen M. Cameron cmd->Request.Type.Direction = XFER_NONE; 3012edd16368SStephen M. Cameron cmd->Request.Timeout = 0; /* Don't time out */ 3013edd16368SStephen M. Cameron cmd->Request.CDB[0] = opcode; 3014edd16368SStephen M. Cameron cmd->Request.CDB[1] = type; 3015edd16368SStephen M. Cameron memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */ 3016edd16368SStephen M. Cameron cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd); 3017edd16368SStephen M. Cameron cmd->ErrorDescriptor.Addr.upper = 0; 3018edd16368SStephen M. Cameron cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo); 3019edd16368SStephen M. Cameron 3020edd16368SStephen M. Cameron writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET); 3021edd16368SStephen M. Cameron 3022edd16368SStephen M. Cameron for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) { 3023edd16368SStephen M. Cameron tag = readl(vaddr + SA5_REPLY_PORT_OFFSET); 3024a104c99fSStephen M. Cameron if (hpsa_tag_discard_error_bits(tag) == paddr32) 3025edd16368SStephen M. Cameron break; 3026edd16368SStephen M. Cameron msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS); 3027edd16368SStephen M. Cameron } 3028edd16368SStephen M. Cameron 3029edd16368SStephen M. Cameron iounmap(vaddr); 3030edd16368SStephen M. Cameron 3031edd16368SStephen M. Cameron /* we leak the DMA buffer here ... no choice since the controller could 3032edd16368SStephen M. Cameron * still complete the command. 3033edd16368SStephen M. Cameron */ 3034edd16368SStephen M. Cameron if (i == HPSA_MSG_SEND_RETRY_LIMIT) { 3035edd16368SStephen M. Cameron dev_err(&pdev->dev, "controller message %02x:%02x timed out\n", 3036edd16368SStephen M. Cameron opcode, type); 3037edd16368SStephen M. Cameron return -ETIMEDOUT; 3038edd16368SStephen M. Cameron } 3039edd16368SStephen M. Cameron 3040edd16368SStephen M. Cameron pci_free_consistent(pdev, cmd_sz, cmd, paddr64); 3041edd16368SStephen M. Cameron 3042edd16368SStephen M. Cameron if (tag & HPSA_ERROR_BIT) { 3043edd16368SStephen M. Cameron dev_err(&pdev->dev, "controller message %02x:%02x failed\n", 3044edd16368SStephen M. Cameron opcode, type); 3045edd16368SStephen M. Cameron return -EIO; 3046edd16368SStephen M. Cameron } 3047edd16368SStephen M. Cameron 3048edd16368SStephen M. Cameron dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n", 3049edd16368SStephen M. Cameron opcode, type); 3050edd16368SStephen M. Cameron return 0; 3051edd16368SStephen M. Cameron } 3052edd16368SStephen M. Cameron 3053edd16368SStephen M. Cameron #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0) 3054edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0) 3055edd16368SStephen M. Cameron 3056edd16368SStephen M. Cameron static __devinit int hpsa_reset_msi(struct pci_dev *pdev) 3057edd16368SStephen M. Cameron { 3058edd16368SStephen M. Cameron /* the #defines are stolen from drivers/pci/msi.h. */ 3059edd16368SStephen M. Cameron #define msi_control_reg(base) (base + PCI_MSI_FLAGS) 3060edd16368SStephen M. Cameron #define PCI_MSIX_FLAGS_ENABLE (1 << 15) 3061edd16368SStephen M. Cameron 3062edd16368SStephen M. Cameron int pos; 3063edd16368SStephen M. Cameron u16 control = 0; 3064edd16368SStephen M. Cameron 3065edd16368SStephen M. Cameron pos = pci_find_capability(pdev, PCI_CAP_ID_MSI); 3066edd16368SStephen M. Cameron if (pos) { 3067edd16368SStephen M. Cameron pci_read_config_word(pdev, msi_control_reg(pos), &control); 3068edd16368SStephen M. Cameron if (control & PCI_MSI_FLAGS_ENABLE) { 3069edd16368SStephen M. Cameron dev_info(&pdev->dev, "resetting MSI\n"); 3070edd16368SStephen M. Cameron pci_write_config_word(pdev, msi_control_reg(pos), 3071edd16368SStephen M. Cameron control & ~PCI_MSI_FLAGS_ENABLE); 3072edd16368SStephen M. Cameron } 3073edd16368SStephen M. Cameron } 3074edd16368SStephen M. Cameron 3075edd16368SStephen M. Cameron pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX); 3076edd16368SStephen M. Cameron if (pos) { 3077edd16368SStephen M. Cameron pci_read_config_word(pdev, msi_control_reg(pos), &control); 3078edd16368SStephen M. Cameron if (control & PCI_MSIX_FLAGS_ENABLE) { 3079edd16368SStephen M. Cameron dev_info(&pdev->dev, "resetting MSI-X\n"); 3080edd16368SStephen M. Cameron pci_write_config_word(pdev, msi_control_reg(pos), 3081edd16368SStephen M. Cameron control & ~PCI_MSIX_FLAGS_ENABLE); 3082edd16368SStephen M. Cameron } 3083edd16368SStephen M. Cameron } 3084edd16368SStephen M. Cameron 3085edd16368SStephen M. Cameron return 0; 3086edd16368SStephen M. Cameron } 3087edd16368SStephen M. Cameron 30881df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev, 30891df8552aSStephen M. Cameron void * __iomem vaddr, bool use_doorbell) 3090edd16368SStephen M. Cameron { 30911df8552aSStephen M. Cameron u16 pmcsr; 30921df8552aSStephen M. Cameron int pos; 3093edd16368SStephen M. Cameron 30941df8552aSStephen M. Cameron if (use_doorbell) { 30951df8552aSStephen M. Cameron /* For everything after the P600, the PCI power state method 30961df8552aSStephen M. Cameron * of resetting the controller doesn't work, so we have this 30971df8552aSStephen M. Cameron * other way using the doorbell register. 3098edd16368SStephen M. Cameron */ 30991df8552aSStephen M. Cameron dev_info(&pdev->dev, "using doorbell to reset controller\n"); 31001df8552aSStephen M. Cameron writel(DOORBELL_CTLR_RESET, vaddr + SA5_DOORBELL); 31011df8552aSStephen M. Cameron msleep(1000); 31021df8552aSStephen M. Cameron } else { /* Try to do it the PCI power state way */ 3103edd16368SStephen M. Cameron 3104edd16368SStephen M. Cameron /* Quoting from the Open CISS Specification: "The Power 3105edd16368SStephen M. Cameron * Management Control/Status Register (CSR) controls the power 3106edd16368SStephen M. Cameron * state of the device. The normal operating state is D0, 3107edd16368SStephen M. Cameron * CSR=00h. The software off state is D3, CSR=03h. To reset 31081df8552aSStephen M. Cameron * the controller, place the interface device in D3 then to D0, 31091df8552aSStephen M. Cameron * this causes a secondary PCI reset which will reset the 31101df8552aSStephen M. Cameron * controller." */ 3111edd16368SStephen M. Cameron 31121df8552aSStephen M. Cameron pos = pci_find_capability(pdev, PCI_CAP_ID_PM); 31131df8552aSStephen M. Cameron if (pos == 0) { 31141df8552aSStephen M. Cameron dev_err(&pdev->dev, 31151df8552aSStephen M. Cameron "hpsa_reset_controller: " 31161df8552aSStephen M. Cameron "PCI PM not supported\n"); 31171df8552aSStephen M. Cameron return -ENODEV; 31181df8552aSStephen M. Cameron } 31191df8552aSStephen M. Cameron dev_info(&pdev->dev, "using PCI PM to reset controller\n"); 3120edd16368SStephen M. Cameron /* enter the D3hot power management state */ 3121edd16368SStephen M. Cameron pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr); 3122edd16368SStephen M. Cameron pmcsr &= ~PCI_PM_CTRL_STATE_MASK; 3123edd16368SStephen M. Cameron pmcsr |= PCI_D3hot; 3124edd16368SStephen M. Cameron pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr); 3125edd16368SStephen M. Cameron 3126edd16368SStephen M. Cameron msleep(500); 3127edd16368SStephen M. Cameron 3128edd16368SStephen M. Cameron /* enter the D0 power management state */ 3129edd16368SStephen M. Cameron pmcsr &= ~PCI_PM_CTRL_STATE_MASK; 3130edd16368SStephen M. Cameron pmcsr |= PCI_D0; 3131edd16368SStephen M. Cameron pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr); 3132edd16368SStephen M. Cameron 3133edd16368SStephen M. Cameron msleep(500); 31341df8552aSStephen M. Cameron } 31351df8552aSStephen M. Cameron return 0; 31361df8552aSStephen M. Cameron } 31371df8552aSStephen M. Cameron 31381df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management 31391df8552aSStephen M. Cameron * states or the using the doorbell register. 31401df8552aSStephen M. Cameron */ 31411df8552aSStephen M. Cameron static __devinit int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev) 31421df8552aSStephen M. Cameron { 31431df8552aSStephen M. Cameron u16 saved_config_space[32]; 31441df8552aSStephen M. Cameron u64 cfg_offset; 31451df8552aSStephen M. Cameron u32 cfg_base_addr; 31461df8552aSStephen M. Cameron u64 cfg_base_addr_index; 31471df8552aSStephen M. Cameron void __iomem *vaddr; 31481df8552aSStephen M. Cameron unsigned long paddr; 31491df8552aSStephen M. Cameron u32 misc_fw_support, active_transport; 31501df8552aSStephen M. Cameron int rc, i; 31511df8552aSStephen M. Cameron struct CfgTable __iomem *cfgtable; 31521df8552aSStephen M. Cameron bool use_doorbell; 315318867659SStephen M. Cameron u32 board_id; 31541df8552aSStephen M. Cameron 31551df8552aSStephen M. Cameron /* For controllers as old as the P600, this is very nearly 31561df8552aSStephen M. Cameron * the same thing as 31571df8552aSStephen M. Cameron * 31581df8552aSStephen M. Cameron * pci_save_state(pci_dev); 31591df8552aSStephen M. Cameron * pci_set_power_state(pci_dev, PCI_D3hot); 31601df8552aSStephen M. Cameron * pci_set_power_state(pci_dev, PCI_D0); 31611df8552aSStephen M. Cameron * pci_restore_state(pci_dev); 31621df8552aSStephen M. Cameron * 31631df8552aSStephen M. Cameron * but we can't use these nice canned kernel routines on 31641df8552aSStephen M. Cameron * kexec, because they also check the MSI/MSI-X state in PCI 31651df8552aSStephen M. Cameron * configuration space and do the wrong thing when it is 31661df8552aSStephen M. Cameron * set/cleared. Also, the pci_save/restore_state functions 31671df8552aSStephen M. Cameron * violate the ordering requirements for restoring the 31681df8552aSStephen M. Cameron * configuration space from the CCISS document (see the 31691df8552aSStephen M. Cameron * comment below). So we roll our own .... 31701df8552aSStephen M. Cameron * 31711df8552aSStephen M. Cameron * For controllers newer than the P600, the pci power state 31721df8552aSStephen M. Cameron * method of resetting doesn't work so we have another way 31731df8552aSStephen M. Cameron * using the doorbell register. 31741df8552aSStephen M. Cameron */ 317518867659SStephen M. Cameron 317618867659SStephen M. Cameron /* Exclude 640x boards. These are two pci devices in one slot 317718867659SStephen M. Cameron * which share a battery backed cache module. One controls the 317818867659SStephen M. Cameron * cache, the other accesses the cache through the one that controls 317918867659SStephen M. Cameron * it. If we reset the one controlling the cache, the other will 318018867659SStephen M. Cameron * likely not be happy. Just forbid resetting this conjoined mess. 318118867659SStephen M. Cameron * The 640x isn't really supported by hpsa anyway. 318218867659SStephen M. Cameron */ 318318867659SStephen M. Cameron hpsa_lookup_board_id(pdev, &board_id); 318418867659SStephen M. Cameron if (board_id == 0x409C0E11 || board_id == 0x409D0E11) 318518867659SStephen M. Cameron return -ENOTSUPP; 318618867659SStephen M. Cameron 31871df8552aSStephen M. Cameron for (i = 0; i < 32; i++) 31881df8552aSStephen M. Cameron pci_read_config_word(pdev, 2*i, &saved_config_space[i]); 31891df8552aSStephen M. Cameron 31901df8552aSStephen M. Cameron 31911df8552aSStephen M. Cameron /* find the first memory BAR, so we can find the cfg table */ 31921df8552aSStephen M. Cameron rc = hpsa_pci_find_memory_BAR(pdev, &paddr); 31931df8552aSStephen M. Cameron if (rc) 31941df8552aSStephen M. Cameron return rc; 31951df8552aSStephen M. Cameron vaddr = remap_pci_mem(paddr, 0x250); 31961df8552aSStephen M. Cameron if (!vaddr) 31971df8552aSStephen M. Cameron return -ENOMEM; 31981df8552aSStephen M. Cameron 31991df8552aSStephen M. Cameron /* find cfgtable in order to check if reset via doorbell is supported */ 32001df8552aSStephen M. Cameron rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr, 32011df8552aSStephen M. Cameron &cfg_base_addr_index, &cfg_offset); 32021df8552aSStephen M. Cameron if (rc) 32031df8552aSStephen M. Cameron goto unmap_vaddr; 32041df8552aSStephen M. Cameron cfgtable = remap_pci_mem(pci_resource_start(pdev, 32051df8552aSStephen M. Cameron cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable)); 32061df8552aSStephen M. Cameron if (!cfgtable) { 32071df8552aSStephen M. Cameron rc = -ENOMEM; 32081df8552aSStephen M. Cameron goto unmap_vaddr; 32091df8552aSStephen M. Cameron } 32101df8552aSStephen M. Cameron 32111df8552aSStephen M. Cameron /* If reset via doorbell register is supported, use that. */ 32121df8552aSStephen M. Cameron misc_fw_support = readl(&cfgtable->misc_fw_support); 32131df8552aSStephen M. Cameron use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET; 32141df8552aSStephen M. Cameron 321536ed2176SStephen M. Cameron /* The doorbell reset seems to cause lockups on some Smart 321636ed2176SStephen M. Cameron * Arrays (e.g. P410, P410i, maybe others). Until this is 321736ed2176SStephen M. Cameron * fixed or at least isolated, avoid the doorbell reset. 321836ed2176SStephen M. Cameron */ 321936ed2176SStephen M. Cameron use_doorbell = 0; 322036ed2176SStephen M. Cameron 32211df8552aSStephen M. Cameron rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell); 32221df8552aSStephen M. Cameron if (rc) 32231df8552aSStephen M. Cameron goto unmap_cfgtable; 3224edd16368SStephen M. Cameron 3225edd16368SStephen M. Cameron /* Restore the PCI configuration space. The Open CISS 3226edd16368SStephen M. Cameron * Specification says, "Restore the PCI Configuration 3227edd16368SStephen M. Cameron * Registers, offsets 00h through 60h. It is important to 3228edd16368SStephen M. Cameron * restore the command register, 16-bits at offset 04h, 3229edd16368SStephen M. Cameron * last. Do not restore the configuration status register, 3230edd16368SStephen M. Cameron * 16-bits at offset 06h." Note that the offset is 2*i. 3231edd16368SStephen M. Cameron */ 3232edd16368SStephen M. Cameron for (i = 0; i < 32; i++) { 3233edd16368SStephen M. Cameron if (i == 2 || i == 3) 3234edd16368SStephen M. Cameron continue; 3235edd16368SStephen M. Cameron pci_write_config_word(pdev, 2*i, saved_config_space[i]); 3236edd16368SStephen M. Cameron } 3237edd16368SStephen M. Cameron wmb(); 3238edd16368SStephen M. Cameron pci_write_config_word(pdev, 4, saved_config_space[2]); 3239edd16368SStephen M. Cameron 32401df8552aSStephen M. Cameron /* Some devices (notably the HP Smart Array 5i Controller) 32411df8552aSStephen M. Cameron need a little pause here */ 32421df8552aSStephen M. Cameron msleep(HPSA_POST_RESET_PAUSE_MSECS); 32431df8552aSStephen M. Cameron 3244*fe5389c8SStephen M. Cameron /* Wait for board to become not ready, then ready. */ 3245*fe5389c8SStephen M. Cameron dev_info(&pdev->dev, "Waiting for board to become ready.\n"); 3246*fe5389c8SStephen M. Cameron rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_NOT_READY); 3247*fe5389c8SStephen M. Cameron if (rc) 3248*fe5389c8SStephen M. Cameron dev_warn(&pdev->dev, 3249*fe5389c8SStephen M. Cameron "failed waiting for board to become not ready\n"); 3250*fe5389c8SStephen M. Cameron rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY); 3251*fe5389c8SStephen M. Cameron if (rc) { 3252*fe5389c8SStephen M. Cameron dev_warn(&pdev->dev, 3253*fe5389c8SStephen M. Cameron "failed waiting for board to become ready\n"); 3254*fe5389c8SStephen M. Cameron goto unmap_cfgtable; 3255*fe5389c8SStephen M. Cameron } 3256*fe5389c8SStephen M. Cameron dev_info(&pdev->dev, "board ready.\n"); 3257*fe5389c8SStephen M. Cameron 32581df8552aSStephen M. Cameron /* Controller should be in simple mode at this point. If it's not, 32591df8552aSStephen M. Cameron * It means we're on one of those controllers which doesn't support 32601df8552aSStephen M. Cameron * the doorbell reset method and on which the PCI power management reset 32611df8552aSStephen M. Cameron * method doesn't work (P800, for example.) 32621df8552aSStephen M. Cameron * In those cases, pretend the reset worked and hope for the best. 32631df8552aSStephen M. Cameron */ 32641df8552aSStephen M. Cameron active_transport = readl(&cfgtable->TransportActive); 32651df8552aSStephen M. Cameron if (active_transport & PERFORMANT_MODE) { 32661df8552aSStephen M. Cameron dev_warn(&pdev->dev, "Unable to successfully reset controller," 32671df8552aSStephen M. Cameron " proceeding anyway.\n"); 32681df8552aSStephen M. Cameron rc = -ENOTSUPP; 32691df8552aSStephen M. Cameron } 32701df8552aSStephen M. Cameron 32711df8552aSStephen M. Cameron unmap_cfgtable: 32721df8552aSStephen M. Cameron iounmap(cfgtable); 32731df8552aSStephen M. Cameron 32741df8552aSStephen M. Cameron unmap_vaddr: 32751df8552aSStephen M. Cameron iounmap(vaddr); 32761df8552aSStephen M. Cameron return rc; 3277edd16368SStephen M. Cameron } 3278edd16368SStephen M. Cameron 3279edd16368SStephen M. Cameron /* 3280edd16368SStephen M. Cameron * We cannot read the structure directly, for portability we must use 3281edd16368SStephen M. Cameron * the io functions. 3282edd16368SStephen M. Cameron * This is for debug only. 3283edd16368SStephen M. Cameron */ 3284edd16368SStephen M. Cameron static void print_cfg_table(struct device *dev, struct CfgTable *tb) 3285edd16368SStephen M. Cameron { 328658f8665cSStephen M. Cameron #ifdef HPSA_DEBUG 3287edd16368SStephen M. Cameron int i; 3288edd16368SStephen M. Cameron char temp_name[17]; 3289edd16368SStephen M. Cameron 3290edd16368SStephen M. Cameron dev_info(dev, "Controller Configuration information\n"); 3291edd16368SStephen M. Cameron dev_info(dev, "------------------------------------\n"); 3292edd16368SStephen M. Cameron for (i = 0; i < 4; i++) 3293edd16368SStephen M. Cameron temp_name[i] = readb(&(tb->Signature[i])); 3294edd16368SStephen M. Cameron temp_name[4] = '\0'; 3295edd16368SStephen M. Cameron dev_info(dev, " Signature = %s\n", temp_name); 3296edd16368SStephen M. Cameron dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence))); 3297edd16368SStephen M. Cameron dev_info(dev, " Transport methods supported = 0x%x\n", 3298edd16368SStephen M. Cameron readl(&(tb->TransportSupport))); 3299edd16368SStephen M. Cameron dev_info(dev, " Transport methods active = 0x%x\n", 3300edd16368SStephen M. Cameron readl(&(tb->TransportActive))); 3301edd16368SStephen M. Cameron dev_info(dev, " Requested transport Method = 0x%x\n", 3302edd16368SStephen M. Cameron readl(&(tb->HostWrite.TransportRequest))); 3303edd16368SStephen M. Cameron dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n", 3304edd16368SStephen M. Cameron readl(&(tb->HostWrite.CoalIntDelay))); 3305edd16368SStephen M. Cameron dev_info(dev, " Coalesce Interrupt Count = 0x%x\n", 3306edd16368SStephen M. Cameron readl(&(tb->HostWrite.CoalIntCount))); 3307edd16368SStephen M. Cameron dev_info(dev, " Max outstanding commands = 0x%d\n", 3308edd16368SStephen M. Cameron readl(&(tb->CmdsOutMax))); 3309edd16368SStephen M. Cameron dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes))); 3310edd16368SStephen M. Cameron for (i = 0; i < 16; i++) 3311edd16368SStephen M. Cameron temp_name[i] = readb(&(tb->ServerName[i])); 3312edd16368SStephen M. Cameron temp_name[16] = '\0'; 3313edd16368SStephen M. Cameron dev_info(dev, " Server Name = %s\n", temp_name); 3314edd16368SStephen M. Cameron dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n", 3315edd16368SStephen M. Cameron readl(&(tb->HeartBeat))); 3316edd16368SStephen M. Cameron #endif /* HPSA_DEBUG */ 331758f8665cSStephen M. Cameron } 3318edd16368SStephen M. Cameron 3319edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr) 3320edd16368SStephen M. Cameron { 3321edd16368SStephen M. Cameron int i, offset, mem_type, bar_type; 3322edd16368SStephen M. Cameron 3323edd16368SStephen M. Cameron if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */ 3324edd16368SStephen M. Cameron return 0; 3325edd16368SStephen M. Cameron offset = 0; 3326edd16368SStephen M. Cameron for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 3327edd16368SStephen M. Cameron bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE; 3328edd16368SStephen M. Cameron if (bar_type == PCI_BASE_ADDRESS_SPACE_IO) 3329edd16368SStephen M. Cameron offset += 4; 3330edd16368SStephen M. Cameron else { 3331edd16368SStephen M. Cameron mem_type = pci_resource_flags(pdev, i) & 3332edd16368SStephen M. Cameron PCI_BASE_ADDRESS_MEM_TYPE_MASK; 3333edd16368SStephen M. Cameron switch (mem_type) { 3334edd16368SStephen M. Cameron case PCI_BASE_ADDRESS_MEM_TYPE_32: 3335edd16368SStephen M. Cameron case PCI_BASE_ADDRESS_MEM_TYPE_1M: 3336edd16368SStephen M. Cameron offset += 4; /* 32 bit */ 3337edd16368SStephen M. Cameron break; 3338edd16368SStephen M. Cameron case PCI_BASE_ADDRESS_MEM_TYPE_64: 3339edd16368SStephen M. Cameron offset += 8; 3340edd16368SStephen M. Cameron break; 3341edd16368SStephen M. Cameron default: /* reserved in PCI 2.2 */ 3342edd16368SStephen M. Cameron dev_warn(&pdev->dev, 3343edd16368SStephen M. Cameron "base address is invalid\n"); 3344edd16368SStephen M. Cameron return -1; 3345edd16368SStephen M. Cameron break; 3346edd16368SStephen M. Cameron } 3347edd16368SStephen M. Cameron } 3348edd16368SStephen M. Cameron if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0) 3349edd16368SStephen M. Cameron return i + 1; 3350edd16368SStephen M. Cameron } 3351edd16368SStephen M. Cameron return -1; 3352edd16368SStephen M. Cameron } 3353edd16368SStephen M. Cameron 3354edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on 3355edd16368SStephen M. Cameron * controllers that are capable. If not, we use IO-APIC mode. 3356edd16368SStephen M. Cameron */ 3357edd16368SStephen M. Cameron 33586b3f4c52SStephen M. Cameron static void __devinit hpsa_interrupt_mode(struct ctlr_info *h) 3359edd16368SStephen M. Cameron { 3360edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI 3361edd16368SStephen M. Cameron int err; 3362edd16368SStephen M. Cameron struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1}, 3363edd16368SStephen M. Cameron {0, 2}, {0, 3} 3364edd16368SStephen M. Cameron }; 3365edd16368SStephen M. Cameron 3366edd16368SStephen M. Cameron /* Some boards advertise MSI but don't really support it */ 33676b3f4c52SStephen M. Cameron if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) || 33686b3f4c52SStephen M. Cameron (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11)) 3369edd16368SStephen M. Cameron goto default_int_mode; 337055c06c71SStephen M. Cameron if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) { 337155c06c71SStephen M. Cameron dev_info(&h->pdev->dev, "MSIX\n"); 337255c06c71SStephen M. Cameron err = pci_enable_msix(h->pdev, hpsa_msix_entries, 4); 3373edd16368SStephen M. Cameron if (!err) { 3374edd16368SStephen M. Cameron h->intr[0] = hpsa_msix_entries[0].vector; 3375edd16368SStephen M. Cameron h->intr[1] = hpsa_msix_entries[1].vector; 3376edd16368SStephen M. Cameron h->intr[2] = hpsa_msix_entries[2].vector; 3377edd16368SStephen M. Cameron h->intr[3] = hpsa_msix_entries[3].vector; 3378edd16368SStephen M. Cameron h->msix_vector = 1; 3379edd16368SStephen M. Cameron return; 3380edd16368SStephen M. Cameron } 3381edd16368SStephen M. Cameron if (err > 0) { 338255c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "only %d MSI-X vectors " 3383edd16368SStephen M. Cameron "available\n", err); 3384edd16368SStephen M. Cameron goto default_int_mode; 3385edd16368SStephen M. Cameron } else { 338655c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", 3387edd16368SStephen M. Cameron err); 3388edd16368SStephen M. Cameron goto default_int_mode; 3389edd16368SStephen M. Cameron } 3390edd16368SStephen M. Cameron } 339155c06c71SStephen M. Cameron if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) { 339255c06c71SStephen M. Cameron dev_info(&h->pdev->dev, "MSI\n"); 339355c06c71SStephen M. Cameron if (!pci_enable_msi(h->pdev)) 3394edd16368SStephen M. Cameron h->msi_vector = 1; 3395edd16368SStephen M. Cameron else 339655c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "MSI init failed\n"); 3397edd16368SStephen M. Cameron } 3398edd16368SStephen M. Cameron default_int_mode: 3399edd16368SStephen M. Cameron #endif /* CONFIG_PCI_MSI */ 3400edd16368SStephen M. Cameron /* if we get here we're going to use the default interrupt mode */ 340155c06c71SStephen M. Cameron h->intr[PERF_MODE_INT] = h->pdev->irq; 3402edd16368SStephen M. Cameron } 3403edd16368SStephen M. Cameron 3404e5c880d1SStephen M. Cameron static int __devinit hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id) 3405e5c880d1SStephen M. Cameron { 3406e5c880d1SStephen M. Cameron int i; 3407e5c880d1SStephen M. Cameron u32 subsystem_vendor_id, subsystem_device_id; 3408e5c880d1SStephen M. Cameron 3409e5c880d1SStephen M. Cameron subsystem_vendor_id = pdev->subsystem_vendor; 3410e5c880d1SStephen M. Cameron subsystem_device_id = pdev->subsystem_device; 3411e5c880d1SStephen M. Cameron *board_id = ((subsystem_device_id << 16) & 0xffff0000) | 3412e5c880d1SStephen M. Cameron subsystem_vendor_id; 3413e5c880d1SStephen M. Cameron 3414e5c880d1SStephen M. Cameron for (i = 0; i < ARRAY_SIZE(products); i++) 3415e5c880d1SStephen M. Cameron if (*board_id == products[i].board_id) 3416e5c880d1SStephen M. Cameron return i; 3417e5c880d1SStephen M. Cameron 34186798cc0aSStephen M. Cameron if ((subsystem_vendor_id != PCI_VENDOR_ID_HP && 34196798cc0aSStephen M. Cameron subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) || 34206798cc0aSStephen M. Cameron !hpsa_allow_any) { 3421e5c880d1SStephen M. Cameron dev_warn(&pdev->dev, "unrecognized board ID: " 3422e5c880d1SStephen M. Cameron "0x%08x, ignoring.\n", *board_id); 3423e5c880d1SStephen M. Cameron return -ENODEV; 3424e5c880d1SStephen M. Cameron } 3425e5c880d1SStephen M. Cameron return ARRAY_SIZE(products) - 1; /* generic unknown smart array */ 3426e5c880d1SStephen M. Cameron } 3427e5c880d1SStephen M. Cameron 342885bdbabbSStephen M. Cameron static inline bool hpsa_board_disabled(struct pci_dev *pdev) 342985bdbabbSStephen M. Cameron { 343085bdbabbSStephen M. Cameron u16 command; 343185bdbabbSStephen M. Cameron 343285bdbabbSStephen M. Cameron (void) pci_read_config_word(pdev, PCI_COMMAND, &command); 343385bdbabbSStephen M. Cameron return ((command & PCI_COMMAND_MEMORY) == 0); 343485bdbabbSStephen M. Cameron } 343585bdbabbSStephen M. Cameron 343612d2cd47SStephen M. Cameron static int __devinit hpsa_pci_find_memory_BAR(struct pci_dev *pdev, 34373a7774ceSStephen M. Cameron unsigned long *memory_bar) 34383a7774ceSStephen M. Cameron { 34393a7774ceSStephen M. Cameron int i; 34403a7774ceSStephen M. Cameron 34413a7774ceSStephen M. Cameron for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) 344212d2cd47SStephen M. Cameron if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) { 34433a7774ceSStephen M. Cameron /* addressing mode bits already removed */ 344412d2cd47SStephen M. Cameron *memory_bar = pci_resource_start(pdev, i); 344512d2cd47SStephen M. Cameron dev_dbg(&pdev->dev, "memory BAR = %lx\n", 34463a7774ceSStephen M. Cameron *memory_bar); 34473a7774ceSStephen M. Cameron return 0; 34483a7774ceSStephen M. Cameron } 344912d2cd47SStephen M. Cameron dev_warn(&pdev->dev, "no memory BAR found\n"); 34503a7774ceSStephen M. Cameron return -ENODEV; 34513a7774ceSStephen M. Cameron } 34523a7774ceSStephen M. Cameron 3453*fe5389c8SStephen M. Cameron static int __devinit hpsa_wait_for_board_state(struct pci_dev *pdev, 3454*fe5389c8SStephen M. Cameron void __iomem *vaddr, int wait_for_ready) 34552c4c8c8bSStephen M. Cameron { 3456*fe5389c8SStephen M. Cameron int i, iterations; 34572c4c8c8bSStephen M. Cameron u32 scratchpad; 3458*fe5389c8SStephen M. Cameron if (wait_for_ready) 3459*fe5389c8SStephen M. Cameron iterations = HPSA_BOARD_READY_ITERATIONS; 3460*fe5389c8SStephen M. Cameron else 3461*fe5389c8SStephen M. Cameron iterations = HPSA_BOARD_NOT_READY_ITERATIONS; 34622c4c8c8bSStephen M. Cameron 3463*fe5389c8SStephen M. Cameron for (i = 0; i < iterations; i++) { 3464*fe5389c8SStephen M. Cameron scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET); 3465*fe5389c8SStephen M. Cameron if (wait_for_ready) { 34662c4c8c8bSStephen M. Cameron if (scratchpad == HPSA_FIRMWARE_READY) 34672c4c8c8bSStephen M. Cameron return 0; 3468*fe5389c8SStephen M. Cameron } else { 3469*fe5389c8SStephen M. Cameron if (scratchpad != HPSA_FIRMWARE_READY) 3470*fe5389c8SStephen M. Cameron return 0; 3471*fe5389c8SStephen M. Cameron } 34722c4c8c8bSStephen M. Cameron msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS); 34732c4c8c8bSStephen M. Cameron } 3474*fe5389c8SStephen M. Cameron dev_warn(&pdev->dev, "board not ready, timed out.\n"); 34752c4c8c8bSStephen M. Cameron return -ENODEV; 34762c4c8c8bSStephen M. Cameron } 34772c4c8c8bSStephen M. Cameron 3478a51fd47fSStephen M. Cameron static int __devinit hpsa_find_cfg_addrs(struct pci_dev *pdev, 3479a51fd47fSStephen M. Cameron void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index, 3480a51fd47fSStephen M. Cameron u64 *cfg_offset) 3481a51fd47fSStephen M. Cameron { 3482a51fd47fSStephen M. Cameron *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET); 3483a51fd47fSStephen M. Cameron *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET); 3484a51fd47fSStephen M. Cameron *cfg_base_addr &= (u32) 0x0000ffff; 3485a51fd47fSStephen M. Cameron *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr); 3486a51fd47fSStephen M. Cameron if (*cfg_base_addr_index == -1) { 3487a51fd47fSStephen M. Cameron dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n"); 3488a51fd47fSStephen M. Cameron return -ENODEV; 3489a51fd47fSStephen M. Cameron } 3490a51fd47fSStephen M. Cameron return 0; 3491a51fd47fSStephen M. Cameron } 3492a51fd47fSStephen M. Cameron 349377c4495cSStephen M. Cameron static int __devinit hpsa_find_cfgtables(struct ctlr_info *h) 3494edd16368SStephen M. Cameron { 349501a02ffcSStephen M. Cameron u64 cfg_offset; 349601a02ffcSStephen M. Cameron u32 cfg_base_addr; 349701a02ffcSStephen M. Cameron u64 cfg_base_addr_index; 3498303932fdSDon Brace u32 trans_offset; 3499a51fd47fSStephen M. Cameron int rc; 350077c4495cSStephen M. Cameron 3501a51fd47fSStephen M. Cameron rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr, 3502a51fd47fSStephen M. Cameron &cfg_base_addr_index, &cfg_offset); 3503a51fd47fSStephen M. Cameron if (rc) 3504a51fd47fSStephen M. Cameron return rc; 350577c4495cSStephen M. Cameron h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev, 3506a51fd47fSStephen M. Cameron cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable)); 350777c4495cSStephen M. Cameron if (!h->cfgtable) 350877c4495cSStephen M. Cameron return -ENOMEM; 350977c4495cSStephen M. Cameron /* Find performant mode table. */ 3510a51fd47fSStephen M. Cameron trans_offset = readl(&h->cfgtable->TransMethodOffset); 351177c4495cSStephen M. Cameron h->transtable = remap_pci_mem(pci_resource_start(h->pdev, 351277c4495cSStephen M. Cameron cfg_base_addr_index)+cfg_offset+trans_offset, 351377c4495cSStephen M. Cameron sizeof(*h->transtable)); 351477c4495cSStephen M. Cameron if (!h->transtable) 351577c4495cSStephen M. Cameron return -ENOMEM; 351677c4495cSStephen M. Cameron return 0; 351777c4495cSStephen M. Cameron } 351877c4495cSStephen M. Cameron 3519cba3d38bSStephen M. Cameron static void __devinit hpsa_get_max_perf_mode_cmds(struct ctlr_info *h) 3520cba3d38bSStephen M. Cameron { 3521cba3d38bSStephen M. Cameron h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands)); 3522cba3d38bSStephen M. Cameron if (h->max_commands < 16) { 3523cba3d38bSStephen M. Cameron dev_warn(&h->pdev->dev, "Controller reports " 3524cba3d38bSStephen M. Cameron "max supported commands of %d, an obvious lie. " 3525cba3d38bSStephen M. Cameron "Using 16. Ensure that firmware is up to date.\n", 3526cba3d38bSStephen M. Cameron h->max_commands); 3527cba3d38bSStephen M. Cameron h->max_commands = 16; 3528cba3d38bSStephen M. Cameron } 3529cba3d38bSStephen M. Cameron } 3530cba3d38bSStephen M. Cameron 3531b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits: 3532b93d7536SStephen M. Cameron * max commands, max SG elements without chaining, and with chaining, 3533b93d7536SStephen M. Cameron * SG chain block size, etc. 3534b93d7536SStephen M. Cameron */ 3535b93d7536SStephen M. Cameron static void __devinit hpsa_find_board_params(struct ctlr_info *h) 3536b93d7536SStephen M. Cameron { 3537cba3d38bSStephen M. Cameron hpsa_get_max_perf_mode_cmds(h); 3538b93d7536SStephen M. Cameron h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */ 3539b93d7536SStephen M. Cameron h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements)); 3540b93d7536SStephen M. Cameron /* 3541b93d7536SStephen M. Cameron * Limit in-command s/g elements to 32 save dma'able memory. 3542b93d7536SStephen M. Cameron * Howvever spec says if 0, use 31 3543b93d7536SStephen M. Cameron */ 3544b93d7536SStephen M. Cameron h->max_cmd_sg_entries = 31; 3545b93d7536SStephen M. Cameron if (h->maxsgentries > 512) { 3546b93d7536SStephen M. Cameron h->max_cmd_sg_entries = 32; 3547b93d7536SStephen M. Cameron h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1; 3548b93d7536SStephen M. Cameron h->maxsgentries--; /* save one for chain pointer */ 3549b93d7536SStephen M. Cameron } else { 3550b93d7536SStephen M. Cameron h->maxsgentries = 31; /* default to traditional values */ 3551b93d7536SStephen M. Cameron h->chainsize = 0; 3552b93d7536SStephen M. Cameron } 3553b93d7536SStephen M. Cameron } 3554b93d7536SStephen M. Cameron 355576c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h) 355676c46e49SStephen M. Cameron { 355776c46e49SStephen M. Cameron if ((readb(&h->cfgtable->Signature[0]) != 'C') || 355876c46e49SStephen M. Cameron (readb(&h->cfgtable->Signature[1]) != 'I') || 355976c46e49SStephen M. Cameron (readb(&h->cfgtable->Signature[2]) != 'S') || 356076c46e49SStephen M. Cameron (readb(&h->cfgtable->Signature[3]) != 'S')) { 356176c46e49SStephen M. Cameron dev_warn(&h->pdev->dev, "not a valid CISS config table\n"); 356276c46e49SStephen M. Cameron return false; 356376c46e49SStephen M. Cameron } 356476c46e49SStephen M. Cameron return true; 356576c46e49SStephen M. Cameron } 356676c46e49SStephen M. Cameron 3567f7c39101SStephen M. Cameron /* Need to enable prefetch in the SCSI core for 6400 in x86 */ 3568f7c39101SStephen M. Cameron static inline void hpsa_enable_scsi_prefetch(struct ctlr_info *h) 3569f7c39101SStephen M. Cameron { 3570f7c39101SStephen M. Cameron #ifdef CONFIG_X86 3571f7c39101SStephen M. Cameron u32 prefetch; 3572f7c39101SStephen M. Cameron 3573f7c39101SStephen M. Cameron prefetch = readl(&(h->cfgtable->SCSI_Prefetch)); 3574f7c39101SStephen M. Cameron prefetch |= 0x100; 3575f7c39101SStephen M. Cameron writel(prefetch, &(h->cfgtable->SCSI_Prefetch)); 3576f7c39101SStephen M. Cameron #endif 3577f7c39101SStephen M. Cameron } 3578f7c39101SStephen M. Cameron 35793d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result 35803d0eab67SStephen M. Cameron * in a prefetch beyond physical memory. 35813d0eab67SStephen M. Cameron */ 35823d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h) 35833d0eab67SStephen M. Cameron { 35843d0eab67SStephen M. Cameron u32 dma_prefetch; 35853d0eab67SStephen M. Cameron 35863d0eab67SStephen M. Cameron if (h->board_id != 0x3225103C) 35873d0eab67SStephen M. Cameron return; 35883d0eab67SStephen M. Cameron dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG); 35893d0eab67SStephen M. Cameron dma_prefetch |= 0x8000; 35903d0eab67SStephen M. Cameron writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG); 35913d0eab67SStephen M. Cameron } 35923d0eab67SStephen M. Cameron 35933f4336f3SStephen M. Cameron static void __devinit hpsa_wait_for_mode_change_ack(struct ctlr_info *h) 3594eb6b2ae9SStephen M. Cameron { 3595eb6b2ae9SStephen M. Cameron int i; 3596eb6b2ae9SStephen M. Cameron 3597eb6b2ae9SStephen M. Cameron /* under certain very rare conditions, this can take awhile. 3598eb6b2ae9SStephen M. Cameron * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right 3599eb6b2ae9SStephen M. Cameron * as we enter this code.) 3600eb6b2ae9SStephen M. Cameron */ 3601eb6b2ae9SStephen M. Cameron for (i = 0; i < MAX_CONFIG_WAIT; i++) { 3602eb6b2ae9SStephen M. Cameron if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq)) 3603eb6b2ae9SStephen M. Cameron break; 3604eb6b2ae9SStephen M. Cameron /* delay and try again */ 3605eb6b2ae9SStephen M. Cameron msleep(10); 3606eb6b2ae9SStephen M. Cameron } 36073f4336f3SStephen M. Cameron } 36083f4336f3SStephen M. Cameron 36093f4336f3SStephen M. Cameron static int __devinit hpsa_enter_simple_mode(struct ctlr_info *h) 36103f4336f3SStephen M. Cameron { 36113f4336f3SStephen M. Cameron u32 trans_support; 36123f4336f3SStephen M. Cameron 36133f4336f3SStephen M. Cameron trans_support = readl(&(h->cfgtable->TransportSupport)); 36143f4336f3SStephen M. Cameron if (!(trans_support & SIMPLE_MODE)) 36153f4336f3SStephen M. Cameron return -ENOTSUPP; 36163f4336f3SStephen M. Cameron 36173f4336f3SStephen M. Cameron h->max_commands = readl(&(h->cfgtable->CmdsOutMax)); 36183f4336f3SStephen M. Cameron /* Update the field, and then ring the doorbell */ 36193f4336f3SStephen M. Cameron writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest)); 36203f4336f3SStephen M. Cameron writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL); 36213f4336f3SStephen M. Cameron hpsa_wait_for_mode_change_ack(h); 3622eb6b2ae9SStephen M. Cameron print_cfg_table(&h->pdev->dev, h->cfgtable); 3623eb6b2ae9SStephen M. Cameron if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) { 3624eb6b2ae9SStephen M. Cameron dev_warn(&h->pdev->dev, 3625eb6b2ae9SStephen M. Cameron "unable to get board into simple mode\n"); 3626eb6b2ae9SStephen M. Cameron return -ENODEV; 3627eb6b2ae9SStephen M. Cameron } 3628eb6b2ae9SStephen M. Cameron return 0; 3629eb6b2ae9SStephen M. Cameron } 3630eb6b2ae9SStephen M. Cameron 363177c4495cSStephen M. Cameron static int __devinit hpsa_pci_init(struct ctlr_info *h) 363277c4495cSStephen M. Cameron { 3633eb6b2ae9SStephen M. Cameron int prod_index, err; 3634edd16368SStephen M. Cameron 3635e5c880d1SStephen M. Cameron prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id); 3636e5c880d1SStephen M. Cameron if (prod_index < 0) 3637edd16368SStephen M. Cameron return -ENODEV; 3638e5c880d1SStephen M. Cameron h->product_name = products[prod_index].product_name; 3639e5c880d1SStephen M. Cameron h->access = *(products[prod_index].access); 3640e5c880d1SStephen M. Cameron 364185bdbabbSStephen M. Cameron if (hpsa_board_disabled(h->pdev)) { 364255c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "controller appears to be disabled\n"); 3643edd16368SStephen M. Cameron return -ENODEV; 3644edd16368SStephen M. Cameron } 364555c06c71SStephen M. Cameron err = pci_enable_device(h->pdev); 3646edd16368SStephen M. Cameron if (err) { 364755c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "unable to enable PCI device\n"); 3648edd16368SStephen M. Cameron return err; 3649edd16368SStephen M. Cameron } 3650edd16368SStephen M. Cameron 365155c06c71SStephen M. Cameron err = pci_request_regions(h->pdev, "hpsa"); 3652edd16368SStephen M. Cameron if (err) { 365355c06c71SStephen M. Cameron dev_err(&h->pdev->dev, 365455c06c71SStephen M. Cameron "cannot obtain PCI resources, aborting\n"); 3655edd16368SStephen M. Cameron return err; 3656edd16368SStephen M. Cameron } 36576b3f4c52SStephen M. Cameron hpsa_interrupt_mode(h); 365812d2cd47SStephen M. Cameron err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr); 36593a7774ceSStephen M. Cameron if (err) 3660edd16368SStephen M. Cameron goto err_out_free_res; 3661edd16368SStephen M. Cameron h->vaddr = remap_pci_mem(h->paddr, 0x250); 3662204892e9SStephen M. Cameron if (!h->vaddr) { 3663204892e9SStephen M. Cameron err = -ENOMEM; 3664204892e9SStephen M. Cameron goto err_out_free_res; 3665204892e9SStephen M. Cameron } 3666*fe5389c8SStephen M. Cameron err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY); 36672c4c8c8bSStephen M. Cameron if (err) 3668edd16368SStephen M. Cameron goto err_out_free_res; 366977c4495cSStephen M. Cameron err = hpsa_find_cfgtables(h); 367077c4495cSStephen M. Cameron if (err) 3671edd16368SStephen M. Cameron goto err_out_free_res; 3672b93d7536SStephen M. Cameron hpsa_find_board_params(h); 3673edd16368SStephen M. Cameron 367476c46e49SStephen M. Cameron if (!hpsa_CISS_signature_present(h)) { 3675edd16368SStephen M. Cameron err = -ENODEV; 3676edd16368SStephen M. Cameron goto err_out_free_res; 3677edd16368SStephen M. Cameron } 3678f7c39101SStephen M. Cameron hpsa_enable_scsi_prefetch(h); 36793d0eab67SStephen M. Cameron hpsa_p600_dma_prefetch_quirk(h); 3680eb6b2ae9SStephen M. Cameron err = hpsa_enter_simple_mode(h); 3681eb6b2ae9SStephen M. Cameron if (err) 3682edd16368SStephen M. Cameron goto err_out_free_res; 3683edd16368SStephen M. Cameron return 0; 3684edd16368SStephen M. Cameron 3685edd16368SStephen M. Cameron err_out_free_res: 3686204892e9SStephen M. Cameron if (h->transtable) 3687204892e9SStephen M. Cameron iounmap(h->transtable); 3688204892e9SStephen M. Cameron if (h->cfgtable) 3689204892e9SStephen M. Cameron iounmap(h->cfgtable); 3690204892e9SStephen M. Cameron if (h->vaddr) 3691204892e9SStephen M. Cameron iounmap(h->vaddr); 3692edd16368SStephen M. Cameron /* 3693edd16368SStephen M. Cameron * Deliberately omit pci_disable_device(): it does something nasty to 3694edd16368SStephen M. Cameron * Smart Array controllers that pci_enable_device does not undo 3695edd16368SStephen M. Cameron */ 369655c06c71SStephen M. Cameron pci_release_regions(h->pdev); 3697edd16368SStephen M. Cameron return err; 3698edd16368SStephen M. Cameron } 3699edd16368SStephen M. Cameron 3700339b2b14SStephen M. Cameron static void __devinit hpsa_hba_inquiry(struct ctlr_info *h) 3701339b2b14SStephen M. Cameron { 3702339b2b14SStephen M. Cameron int rc; 3703339b2b14SStephen M. Cameron 3704339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64 3705339b2b14SStephen M. Cameron h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL); 3706339b2b14SStephen M. Cameron if (!h->hba_inquiry_data) 3707339b2b14SStephen M. Cameron return; 3708339b2b14SStephen M. Cameron rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0, 3709339b2b14SStephen M. Cameron h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT); 3710339b2b14SStephen M. Cameron if (rc != 0) { 3711339b2b14SStephen M. Cameron kfree(h->hba_inquiry_data); 3712339b2b14SStephen M. Cameron h->hba_inquiry_data = NULL; 3713339b2b14SStephen M. Cameron } 3714339b2b14SStephen M. Cameron } 3715339b2b14SStephen M. Cameron 37164c2a8c40SStephen M. Cameron static __devinit int hpsa_init_reset_devices(struct pci_dev *pdev) 3717edd16368SStephen M. Cameron { 37181df8552aSStephen M. Cameron int rc, i; 3719edd16368SStephen M. Cameron 37204c2a8c40SStephen M. Cameron if (!reset_devices) 37214c2a8c40SStephen M. Cameron return 0; 37224c2a8c40SStephen M. Cameron 37231df8552aSStephen M. Cameron /* Reset the controller with a PCI power-cycle or via doorbell */ 37241df8552aSStephen M. Cameron rc = hpsa_kdump_hard_reset_controller(pdev); 3725edd16368SStephen M. Cameron 37261df8552aSStephen M. Cameron /* -ENOTSUPP here means we cannot reset the controller 37271df8552aSStephen M. Cameron * but it's already (and still) up and running in 372818867659SStephen M. Cameron * "performant mode". Or, it might be 640x, which can't reset 372918867659SStephen M. Cameron * due to concerns about shared bbwc between 6402/6404 pair. 37301df8552aSStephen M. Cameron */ 37311df8552aSStephen M. Cameron if (rc == -ENOTSUPP) 37321df8552aSStephen M. Cameron return 0; /* just try to do the kdump anyhow. */ 37331df8552aSStephen M. Cameron if (rc) 37341df8552aSStephen M. Cameron return -ENODEV; 37351df8552aSStephen M. Cameron if (hpsa_reset_msi(pdev)) 37361df8552aSStephen M. Cameron return -ENODEV; 3737edd16368SStephen M. Cameron 3738edd16368SStephen M. Cameron /* Now try to get the controller to respond to a no-op */ 3739edd16368SStephen M. Cameron for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) { 3740edd16368SStephen M. Cameron if (hpsa_noop(pdev) == 0) 3741edd16368SStephen M. Cameron break; 3742edd16368SStephen M. Cameron else 3743edd16368SStephen M. Cameron dev_warn(&pdev->dev, "no-op failed%s\n", 3744edd16368SStephen M. Cameron (i < 11 ? "; re-trying" : "")); 3745edd16368SStephen M. Cameron } 37464c2a8c40SStephen M. Cameron return 0; 3747edd16368SStephen M. Cameron } 3748edd16368SStephen M. Cameron 37494c2a8c40SStephen M. Cameron static int __devinit hpsa_init_one(struct pci_dev *pdev, 37504c2a8c40SStephen M. Cameron const struct pci_device_id *ent) 37514c2a8c40SStephen M. Cameron { 37524c2a8c40SStephen M. Cameron int dac, rc; 37534c2a8c40SStephen M. Cameron struct ctlr_info *h; 37544c2a8c40SStephen M. Cameron 37554c2a8c40SStephen M. Cameron if (number_of_controllers == 0) 37564c2a8c40SStephen M. Cameron printk(KERN_INFO DRIVER_NAME "\n"); 37574c2a8c40SStephen M. Cameron 37584c2a8c40SStephen M. Cameron rc = hpsa_init_reset_devices(pdev); 37594c2a8c40SStephen M. Cameron if (rc) 37604c2a8c40SStephen M. Cameron return rc; 37614c2a8c40SStephen M. Cameron 3762303932fdSDon Brace /* Command structures must be aligned on a 32-byte boundary because 3763303932fdSDon Brace * the 5 lower bits of the address are used by the hardware. and by 3764303932fdSDon Brace * the driver. See comments in hpsa.h for more info. 3765303932fdSDon Brace */ 3766303932fdSDon Brace #define COMMANDLIST_ALIGNMENT 32 3767303932fdSDon Brace BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT); 3768edd16368SStephen M. Cameron h = kzalloc(sizeof(*h), GFP_KERNEL); 3769edd16368SStephen M. Cameron if (!h) 3770ecd9aad4SStephen M. Cameron return -ENOMEM; 3771edd16368SStephen M. Cameron 377255c06c71SStephen M. Cameron h->pdev = pdev; 3773edd16368SStephen M. Cameron h->busy_initializing = 1; 3774edd16368SStephen M. Cameron INIT_HLIST_HEAD(&h->cmpQ); 3775edd16368SStephen M. Cameron INIT_HLIST_HEAD(&h->reqQ); 377655c06c71SStephen M. Cameron rc = hpsa_pci_init(h); 3777ecd9aad4SStephen M. Cameron if (rc != 0) 3778edd16368SStephen M. Cameron goto clean1; 3779edd16368SStephen M. Cameron 3780edd16368SStephen M. Cameron sprintf(h->devname, "hpsa%d", number_of_controllers); 3781edd16368SStephen M. Cameron h->ctlr = number_of_controllers; 3782edd16368SStephen M. Cameron number_of_controllers++; 3783edd16368SStephen M. Cameron 3784edd16368SStephen M. Cameron /* configure PCI DMA stuff */ 3785ecd9aad4SStephen M. Cameron rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 3786ecd9aad4SStephen M. Cameron if (rc == 0) { 3787edd16368SStephen M. Cameron dac = 1; 3788ecd9aad4SStephen M. Cameron } else { 3789ecd9aad4SStephen M. Cameron rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 3790ecd9aad4SStephen M. Cameron if (rc == 0) { 3791edd16368SStephen M. Cameron dac = 0; 3792ecd9aad4SStephen M. Cameron } else { 3793edd16368SStephen M. Cameron dev_err(&pdev->dev, "no suitable DMA available\n"); 3794edd16368SStephen M. Cameron goto clean1; 3795edd16368SStephen M. Cameron } 3796ecd9aad4SStephen M. Cameron } 3797edd16368SStephen M. Cameron 3798edd16368SStephen M. Cameron /* make sure the board interrupts are off */ 3799edd16368SStephen M. Cameron h->access.set_intr_mask(h, HPSA_INTR_OFF); 380010f66018SStephen M. Cameron 380110f66018SStephen M. Cameron if (h->msix_vector || h->msi_vector) 380210f66018SStephen M. Cameron rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr_msi, 380310f66018SStephen M. Cameron IRQF_DISABLED, h->devname, h); 380410f66018SStephen M. Cameron else 380510f66018SStephen M. Cameron rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr_intx, 3806303932fdSDon Brace IRQF_DISABLED, h->devname, h); 3807ecd9aad4SStephen M. Cameron if (rc) { 3808edd16368SStephen M. Cameron dev_err(&pdev->dev, "unable to get irq %d for %s\n", 3809303932fdSDon Brace h->intr[PERF_MODE_INT], h->devname); 3810edd16368SStephen M. Cameron goto clean2; 3811edd16368SStephen M. Cameron } 3812edd16368SStephen M. Cameron 3813303932fdSDon Brace dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n", 3814303932fdSDon Brace h->devname, pdev->device, 3815303932fdSDon Brace h->intr[PERF_MODE_INT], dac ? "" : " not"); 3816edd16368SStephen M. Cameron 3817edd16368SStephen M. Cameron h->cmd_pool_bits = 3818edd16368SStephen M. Cameron kmalloc(((h->nr_cmds + BITS_PER_LONG - 3819edd16368SStephen M. Cameron 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL); 3820edd16368SStephen M. Cameron h->cmd_pool = pci_alloc_consistent(h->pdev, 3821edd16368SStephen M. Cameron h->nr_cmds * sizeof(*h->cmd_pool), 3822edd16368SStephen M. Cameron &(h->cmd_pool_dhandle)); 3823edd16368SStephen M. Cameron h->errinfo_pool = pci_alloc_consistent(h->pdev, 3824edd16368SStephen M. Cameron h->nr_cmds * sizeof(*h->errinfo_pool), 3825edd16368SStephen M. Cameron &(h->errinfo_pool_dhandle)); 3826edd16368SStephen M. Cameron if ((h->cmd_pool_bits == NULL) 3827edd16368SStephen M. Cameron || (h->cmd_pool == NULL) 3828edd16368SStephen M. Cameron || (h->errinfo_pool == NULL)) { 3829edd16368SStephen M. Cameron dev_err(&pdev->dev, "out of memory"); 3830ecd9aad4SStephen M. Cameron rc = -ENOMEM; 3831edd16368SStephen M. Cameron goto clean4; 3832edd16368SStephen M. Cameron } 383333a2ffceSStephen M. Cameron if (hpsa_allocate_sg_chain_blocks(h)) 383433a2ffceSStephen M. Cameron goto clean4; 3835edd16368SStephen M. Cameron spin_lock_init(&h->lock); 3836a08a8471SStephen M. Cameron spin_lock_init(&h->scan_lock); 3837a08a8471SStephen M. Cameron init_waitqueue_head(&h->scan_wait_queue); 3838a08a8471SStephen M. Cameron h->scan_finished = 1; /* no scan currently in progress */ 3839edd16368SStephen M. Cameron 3840edd16368SStephen M. Cameron pci_set_drvdata(pdev, h); 3841edd16368SStephen M. Cameron memset(h->cmd_pool_bits, 0, 3842edd16368SStephen M. Cameron ((h->nr_cmds + BITS_PER_LONG - 3843edd16368SStephen M. Cameron 1) / BITS_PER_LONG) * sizeof(unsigned long)); 3844edd16368SStephen M. Cameron 3845edd16368SStephen M. Cameron hpsa_scsi_setup(h); 3846edd16368SStephen M. Cameron 3847edd16368SStephen M. Cameron /* Turn the interrupts on so we can service requests */ 3848edd16368SStephen M. Cameron h->access.set_intr_mask(h, HPSA_INTR_ON); 3849edd16368SStephen M. Cameron 3850303932fdSDon Brace hpsa_put_ctlr_into_performant_mode(h); 3851339b2b14SStephen M. Cameron hpsa_hba_inquiry(h); 3852edd16368SStephen M. Cameron hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */ 3853edd16368SStephen M. Cameron h->busy_initializing = 0; 3854edd16368SStephen M. Cameron return 1; 3855edd16368SStephen M. Cameron 3856edd16368SStephen M. Cameron clean4: 385733a2ffceSStephen M. Cameron hpsa_free_sg_chain_blocks(h); 3858edd16368SStephen M. Cameron kfree(h->cmd_pool_bits); 3859edd16368SStephen M. Cameron if (h->cmd_pool) 3860edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 3861edd16368SStephen M. Cameron h->nr_cmds * sizeof(struct CommandList), 3862edd16368SStephen M. Cameron h->cmd_pool, h->cmd_pool_dhandle); 3863edd16368SStephen M. Cameron if (h->errinfo_pool) 3864edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 3865edd16368SStephen M. Cameron h->nr_cmds * sizeof(struct ErrorInfo), 3866edd16368SStephen M. Cameron h->errinfo_pool, 3867edd16368SStephen M. Cameron h->errinfo_pool_dhandle); 3868303932fdSDon Brace free_irq(h->intr[PERF_MODE_INT], h); 3869edd16368SStephen M. Cameron clean2: 3870edd16368SStephen M. Cameron clean1: 3871edd16368SStephen M. Cameron h->busy_initializing = 0; 3872edd16368SStephen M. Cameron kfree(h); 3873ecd9aad4SStephen M. Cameron return rc; 3874edd16368SStephen M. Cameron } 3875edd16368SStephen M. Cameron 3876edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h) 3877edd16368SStephen M. Cameron { 3878edd16368SStephen M. Cameron char *flush_buf; 3879edd16368SStephen M. Cameron struct CommandList *c; 3880edd16368SStephen M. Cameron 3881edd16368SStephen M. Cameron flush_buf = kzalloc(4, GFP_KERNEL); 3882edd16368SStephen M. Cameron if (!flush_buf) 3883edd16368SStephen M. Cameron return; 3884edd16368SStephen M. Cameron 3885edd16368SStephen M. Cameron c = cmd_special_alloc(h); 3886edd16368SStephen M. Cameron if (!c) { 3887edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); 3888edd16368SStephen M. Cameron goto out_of_memory; 3889edd16368SStephen M. Cameron } 3890edd16368SStephen M. Cameron fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0, 3891edd16368SStephen M. Cameron RAID_CTLR_LUNID, TYPE_CMD); 3892edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE); 3893edd16368SStephen M. Cameron if (c->err_info->CommandStatus != 0) 3894edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, 3895edd16368SStephen M. Cameron "error flushing cache on controller\n"); 3896edd16368SStephen M. Cameron cmd_special_free(h, c); 3897edd16368SStephen M. Cameron out_of_memory: 3898edd16368SStephen M. Cameron kfree(flush_buf); 3899edd16368SStephen M. Cameron } 3900edd16368SStephen M. Cameron 3901edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev) 3902edd16368SStephen M. Cameron { 3903edd16368SStephen M. Cameron struct ctlr_info *h; 3904edd16368SStephen M. Cameron 3905edd16368SStephen M. Cameron h = pci_get_drvdata(pdev); 3906edd16368SStephen M. Cameron /* Turn board interrupts off and send the flush cache command 3907edd16368SStephen M. Cameron * sendcmd will turn off interrupt, and send the flush... 3908edd16368SStephen M. Cameron * To write all data in the battery backed cache to disks 3909edd16368SStephen M. Cameron */ 3910edd16368SStephen M. Cameron hpsa_flush_cache(h); 3911edd16368SStephen M. Cameron h->access.set_intr_mask(h, HPSA_INTR_OFF); 3912303932fdSDon Brace free_irq(h->intr[PERF_MODE_INT], h); 3913edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI 3914edd16368SStephen M. Cameron if (h->msix_vector) 3915edd16368SStephen M. Cameron pci_disable_msix(h->pdev); 3916edd16368SStephen M. Cameron else if (h->msi_vector) 3917edd16368SStephen M. Cameron pci_disable_msi(h->pdev); 3918edd16368SStephen M. Cameron #endif /* CONFIG_PCI_MSI */ 3919edd16368SStephen M. Cameron } 3920edd16368SStephen M. Cameron 3921edd16368SStephen M. Cameron static void __devexit hpsa_remove_one(struct pci_dev *pdev) 3922edd16368SStephen M. Cameron { 3923edd16368SStephen M. Cameron struct ctlr_info *h; 3924edd16368SStephen M. Cameron 3925edd16368SStephen M. Cameron if (pci_get_drvdata(pdev) == NULL) { 3926edd16368SStephen M. Cameron dev_err(&pdev->dev, "unable to remove device \n"); 3927edd16368SStephen M. Cameron return; 3928edd16368SStephen M. Cameron } 3929edd16368SStephen M. Cameron h = pci_get_drvdata(pdev); 3930edd16368SStephen M. Cameron hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */ 3931edd16368SStephen M. Cameron hpsa_shutdown(pdev); 3932edd16368SStephen M. Cameron iounmap(h->vaddr); 3933204892e9SStephen M. Cameron iounmap(h->transtable); 3934204892e9SStephen M. Cameron iounmap(h->cfgtable); 393533a2ffceSStephen M. Cameron hpsa_free_sg_chain_blocks(h); 3936edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 3937edd16368SStephen M. Cameron h->nr_cmds * sizeof(struct CommandList), 3938edd16368SStephen M. Cameron h->cmd_pool, h->cmd_pool_dhandle); 3939edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 3940edd16368SStephen M. Cameron h->nr_cmds * sizeof(struct ErrorInfo), 3941edd16368SStephen M. Cameron h->errinfo_pool, h->errinfo_pool_dhandle); 3942303932fdSDon Brace pci_free_consistent(h->pdev, h->reply_pool_size, 3943303932fdSDon Brace h->reply_pool, h->reply_pool_dhandle); 3944edd16368SStephen M. Cameron kfree(h->cmd_pool_bits); 3945303932fdSDon Brace kfree(h->blockFetchTable); 3946339b2b14SStephen M. Cameron kfree(h->hba_inquiry_data); 3947edd16368SStephen M. Cameron /* 3948edd16368SStephen M. Cameron * Deliberately omit pci_disable_device(): it does something nasty to 3949edd16368SStephen M. Cameron * Smart Array controllers that pci_enable_device does not undo 3950edd16368SStephen M. Cameron */ 3951edd16368SStephen M. Cameron pci_release_regions(pdev); 3952edd16368SStephen M. Cameron pci_set_drvdata(pdev, NULL); 3953edd16368SStephen M. Cameron kfree(h); 3954edd16368SStephen M. Cameron } 3955edd16368SStephen M. Cameron 3956edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev, 3957edd16368SStephen M. Cameron __attribute__((unused)) pm_message_t state) 3958edd16368SStephen M. Cameron { 3959edd16368SStephen M. Cameron return -ENOSYS; 3960edd16368SStephen M. Cameron } 3961edd16368SStephen M. Cameron 3962edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev) 3963edd16368SStephen M. Cameron { 3964edd16368SStephen M. Cameron return -ENOSYS; 3965edd16368SStephen M. Cameron } 3966edd16368SStephen M. Cameron 3967edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = { 3968edd16368SStephen M. Cameron .name = "hpsa", 3969edd16368SStephen M. Cameron .probe = hpsa_init_one, 3970edd16368SStephen M. Cameron .remove = __devexit_p(hpsa_remove_one), 3971edd16368SStephen M. Cameron .id_table = hpsa_pci_device_id, /* id_table */ 3972edd16368SStephen M. Cameron .shutdown = hpsa_shutdown, 3973edd16368SStephen M. Cameron .suspend = hpsa_suspend, 3974edd16368SStephen M. Cameron .resume = hpsa_resume, 3975edd16368SStephen M. Cameron }; 3976edd16368SStephen M. Cameron 3977303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of 3978303932fdSDon Brace * scatter gather elements supported) and bucket[], 3979303932fdSDon Brace * which is an array of 8 integers. The bucket[] array 3980303932fdSDon Brace * contains 8 different DMA transfer sizes (in 16 3981303932fdSDon Brace * byte increments) which the controller uses to fetch 3982303932fdSDon Brace * commands. This function fills in bucket_map[], which 3983303932fdSDon Brace * maps a given number of scatter gather elements to one of 3984303932fdSDon Brace * the 8 DMA transfer sizes. The point of it is to allow the 3985303932fdSDon Brace * controller to only do as much DMA as needed to fetch the 3986303932fdSDon Brace * command, with the DMA transfer size encoded in the lower 3987303932fdSDon Brace * bits of the command address. 3988303932fdSDon Brace */ 3989303932fdSDon Brace static void calc_bucket_map(int bucket[], int num_buckets, 3990303932fdSDon Brace int nsgs, int *bucket_map) 3991303932fdSDon Brace { 3992303932fdSDon Brace int i, j, b, size; 3993303932fdSDon Brace 3994303932fdSDon Brace /* even a command with 0 SGs requires 4 blocks */ 3995303932fdSDon Brace #define MINIMUM_TRANSFER_BLOCKS 4 3996303932fdSDon Brace #define NUM_BUCKETS 8 3997303932fdSDon Brace /* Note, bucket_map must have nsgs+1 entries. */ 3998303932fdSDon Brace for (i = 0; i <= nsgs; i++) { 3999303932fdSDon Brace /* Compute size of a command with i SG entries */ 4000303932fdSDon Brace size = i + MINIMUM_TRANSFER_BLOCKS; 4001303932fdSDon Brace b = num_buckets; /* Assume the biggest bucket */ 4002303932fdSDon Brace /* Find the bucket that is just big enough */ 4003303932fdSDon Brace for (j = 0; j < 8; j++) { 4004303932fdSDon Brace if (bucket[j] >= size) { 4005303932fdSDon Brace b = j; 4006303932fdSDon Brace break; 4007303932fdSDon Brace } 4008303932fdSDon Brace } 4009303932fdSDon Brace /* for a command with i SG entries, use bucket b. */ 4010303932fdSDon Brace bucket_map[i] = b; 4011303932fdSDon Brace } 4012303932fdSDon Brace } 4013303932fdSDon Brace 40146c311b57SStephen M. Cameron static __devinit void hpsa_enter_performant_mode(struct ctlr_info *h) 4015303932fdSDon Brace { 40166c311b57SStephen M. Cameron int i; 40176c311b57SStephen M. Cameron unsigned long register_value; 4018def342bdSStephen M. Cameron 4019def342bdSStephen M. Cameron /* This is a bit complicated. There are 8 registers on 4020def342bdSStephen M. Cameron * the controller which we write to to tell it 8 different 4021def342bdSStephen M. Cameron * sizes of commands which there may be. It's a way of 4022def342bdSStephen M. Cameron * reducing the DMA done to fetch each command. Encoded into 4023def342bdSStephen M. Cameron * each command's tag are 3 bits which communicate to the controller 4024def342bdSStephen M. Cameron * which of the eight sizes that command fits within. The size of 4025def342bdSStephen M. Cameron * each command depends on how many scatter gather entries there are. 4026def342bdSStephen M. Cameron * Each SG entry requires 16 bytes. The eight registers are programmed 4027def342bdSStephen M. Cameron * with the number of 16-byte blocks a command of that size requires. 4028def342bdSStephen M. Cameron * The smallest command possible requires 5 such 16 byte blocks. 4029def342bdSStephen M. Cameron * the largest command possible requires MAXSGENTRIES + 4 16-byte 4030def342bdSStephen M. Cameron * blocks. Note, this only extends to the SG entries contained 4031def342bdSStephen M. Cameron * within the command block, and does not extend to chained blocks 4032def342bdSStephen M. Cameron * of SG elements. bft[] contains the eight values we write to 4033def342bdSStephen M. Cameron * the registers. They are not evenly distributed, but have more 4034def342bdSStephen M. Cameron * sizes for small commands, and fewer sizes for larger commands. 4035def342bdSStephen M. Cameron */ 4036def342bdSStephen M. Cameron int bft[8] = {5, 6, 8, 10, 12, 20, 28, MAXSGENTRIES + 4}; 4037def342bdSStephen M. Cameron BUILD_BUG_ON(28 > MAXSGENTRIES + 4); 4038303932fdSDon Brace /* 5 = 1 s/g entry or 4k 4039303932fdSDon Brace * 6 = 2 s/g entry or 8k 4040303932fdSDon Brace * 8 = 4 s/g entry or 16k 4041303932fdSDon Brace * 10 = 6 s/g entry or 24k 4042303932fdSDon Brace */ 4043303932fdSDon Brace 4044303932fdSDon Brace h->reply_pool_wraparound = 1; /* spec: init to 1 */ 4045303932fdSDon Brace 4046303932fdSDon Brace /* Controller spec: zero out this buffer. */ 4047303932fdSDon Brace memset(h->reply_pool, 0, h->reply_pool_size); 4048303932fdSDon Brace h->reply_pool_head = h->reply_pool; 4049303932fdSDon Brace 4050303932fdSDon Brace bft[7] = h->max_sg_entries + 4; 4051303932fdSDon Brace calc_bucket_map(bft, ARRAY_SIZE(bft), 32, h->blockFetchTable); 4052303932fdSDon Brace for (i = 0; i < 8; i++) 4053303932fdSDon Brace writel(bft[i], &h->transtable->BlockFetch[i]); 4054303932fdSDon Brace 4055303932fdSDon Brace /* size of controller ring buffer */ 4056303932fdSDon Brace writel(h->max_commands, &h->transtable->RepQSize); 4057303932fdSDon Brace writel(1, &h->transtable->RepQCount); 4058303932fdSDon Brace writel(0, &h->transtable->RepQCtrAddrLow32); 4059303932fdSDon Brace writel(0, &h->transtable->RepQCtrAddrHigh32); 4060303932fdSDon Brace writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32); 4061303932fdSDon Brace writel(0, &h->transtable->RepQAddr0High32); 4062303932fdSDon Brace writel(CFGTBL_Trans_Performant, 4063303932fdSDon Brace &(h->cfgtable->HostWrite.TransportRequest)); 4064303932fdSDon Brace writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL); 40653f4336f3SStephen M. Cameron hpsa_wait_for_mode_change_ack(h); 4066303932fdSDon Brace register_value = readl(&(h->cfgtable->TransportActive)); 4067303932fdSDon Brace if (!(register_value & CFGTBL_Trans_Performant)) { 4068303932fdSDon Brace dev_warn(&h->pdev->dev, "unable to get board into" 4069303932fdSDon Brace " performant mode\n"); 4070303932fdSDon Brace return; 4071303932fdSDon Brace } 40726c311b57SStephen M. Cameron } 40736c311b57SStephen M. Cameron 40746c311b57SStephen M. Cameron static __devinit void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h) 40756c311b57SStephen M. Cameron { 40766c311b57SStephen M. Cameron u32 trans_support; 40776c311b57SStephen M. Cameron 40786c311b57SStephen M. Cameron trans_support = readl(&(h->cfgtable->TransportSupport)); 40796c311b57SStephen M. Cameron if (!(trans_support & PERFORMANT_MODE)) 40806c311b57SStephen M. Cameron return; 40816c311b57SStephen M. Cameron 4082cba3d38bSStephen M. Cameron hpsa_get_max_perf_mode_cmds(h); 40836c311b57SStephen M. Cameron h->max_sg_entries = 32; 40846c311b57SStephen M. Cameron /* Performant mode ring buffer and supporting data structures */ 40856c311b57SStephen M. Cameron h->reply_pool_size = h->max_commands * sizeof(u64); 40866c311b57SStephen M. Cameron h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size, 40876c311b57SStephen M. Cameron &(h->reply_pool_dhandle)); 40886c311b57SStephen M. Cameron 40896c311b57SStephen M. Cameron /* Need a block fetch table for performant mode */ 40906c311b57SStephen M. Cameron h->blockFetchTable = kmalloc(((h->max_sg_entries+1) * 40916c311b57SStephen M. Cameron sizeof(u32)), GFP_KERNEL); 40926c311b57SStephen M. Cameron 40936c311b57SStephen M. Cameron if ((h->reply_pool == NULL) 40946c311b57SStephen M. Cameron || (h->blockFetchTable == NULL)) 40956c311b57SStephen M. Cameron goto clean_up; 40966c311b57SStephen M. Cameron 40976c311b57SStephen M. Cameron hpsa_enter_performant_mode(h); 4098303932fdSDon Brace 4099303932fdSDon Brace /* Change the access methods to the performant access methods */ 4100303932fdSDon Brace h->access = SA5_performant_access; 4101303932fdSDon Brace h->transMethod = CFGTBL_Trans_Performant; 4102303932fdSDon Brace 4103303932fdSDon Brace return; 4104303932fdSDon Brace 4105303932fdSDon Brace clean_up: 4106303932fdSDon Brace if (h->reply_pool) 4107303932fdSDon Brace pci_free_consistent(h->pdev, h->reply_pool_size, 4108303932fdSDon Brace h->reply_pool, h->reply_pool_dhandle); 4109303932fdSDon Brace kfree(h->blockFetchTable); 4110303932fdSDon Brace } 4111303932fdSDon Brace 4112edd16368SStephen M. Cameron /* 4113edd16368SStephen M. Cameron * This is it. Register the PCI driver information for the cards we control 4114edd16368SStephen M. Cameron * the OS will call our registered routines when it finds one of our cards. 4115edd16368SStephen M. Cameron */ 4116edd16368SStephen M. Cameron static int __init hpsa_init(void) 4117edd16368SStephen M. Cameron { 411831468401SMike Miller return pci_register_driver(&hpsa_pci_driver); 4119edd16368SStephen M. Cameron } 4120edd16368SStephen M. Cameron 4121edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void) 4122edd16368SStephen M. Cameron { 4123edd16368SStephen M. Cameron pci_unregister_driver(&hpsa_pci_driver); 4124edd16368SStephen M. Cameron } 4125edd16368SStephen M. Cameron 4126edd16368SStephen M. Cameron module_init(hpsa_init); 4127edd16368SStephen M. Cameron module_exit(hpsa_cleanup); 4128