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> 49*60063497SArun Sharma #include <linux/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"); 7702ec19c8SStephen M. Cameron static int hpsa_simple_mode; 7802ec19c8SStephen M. Cameron module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR); 7902ec19c8SStephen M. Cameron MODULE_PARM_DESC(hpsa_simple_mode, 8002ec19c8SStephen M. Cameron "Use 'simple mode' rather than 'performant mode'"); 81edd16368SStephen M. Cameron 82edd16368SStephen M. Cameron /* define the PCI info for the cards we can control */ 83edd16368SStephen M. Cameron static const struct pci_device_id hpsa_pci_device_id[] = { 84edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241}, 85edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243}, 86edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245}, 87edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247}, 88edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249}, 89edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a}, 90edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b}, 91f8b01eb9SMike Miller {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233}, 929143a961Sscameron@beardog.cce.hp.com {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3350}, 939143a961Sscameron@beardog.cce.hp.com {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3351}, 949143a961Sscameron@beardog.cce.hp.com {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3352}, 959143a961Sscameron@beardog.cce.hp.com {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3353}, 969143a961Sscameron@beardog.cce.hp.com {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3354}, 979143a961Sscameron@beardog.cce.hp.com {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3355}, 989143a961Sscameron@beardog.cce.hp.com {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3356}, 99edd16368SStephen M. Cameron {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 100edd16368SStephen M. Cameron PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0}, 101edd16368SStephen M. Cameron {0,} 102edd16368SStephen M. Cameron }; 103edd16368SStephen M. Cameron 104edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id); 105edd16368SStephen M. Cameron 106edd16368SStephen M. Cameron /* board_id = Subsystem Device ID & Vendor ID 107edd16368SStephen M. Cameron * product = Marketing Name for the board 108edd16368SStephen M. Cameron * access = Address of the struct of function pointers 109edd16368SStephen M. Cameron */ 110edd16368SStephen M. Cameron static struct board_type products[] = { 111edd16368SStephen M. Cameron {0x3241103C, "Smart Array P212", &SA5_access}, 112edd16368SStephen M. Cameron {0x3243103C, "Smart Array P410", &SA5_access}, 113edd16368SStephen M. Cameron {0x3245103C, "Smart Array P410i", &SA5_access}, 114edd16368SStephen M. Cameron {0x3247103C, "Smart Array P411", &SA5_access}, 115edd16368SStephen M. Cameron {0x3249103C, "Smart Array P812", &SA5_access}, 116edd16368SStephen M. Cameron {0x324a103C, "Smart Array P712m", &SA5_access}, 117edd16368SStephen M. Cameron {0x324b103C, "Smart Array P711m", &SA5_access}, 1189143a961Sscameron@beardog.cce.hp.com {0x3350103C, "Smart Array", &SA5_access}, 1199143a961Sscameron@beardog.cce.hp.com {0x3351103C, "Smart Array", &SA5_access}, 1209143a961Sscameron@beardog.cce.hp.com {0x3352103C, "Smart Array", &SA5_access}, 1219143a961Sscameron@beardog.cce.hp.com {0x3353103C, "Smart Array", &SA5_access}, 1229143a961Sscameron@beardog.cce.hp.com {0x3354103C, "Smart Array", &SA5_access}, 1239143a961Sscameron@beardog.cce.hp.com {0x3355103C, "Smart Array", &SA5_access}, 1249143a961Sscameron@beardog.cce.hp.com {0x3356103C, "Smart Array", &SA5_access}, 125edd16368SStephen M. Cameron {0xFFFF103C, "Unknown Smart Array", &SA5_access}, 126edd16368SStephen M. Cameron }; 127edd16368SStephen M. Cameron 128edd16368SStephen M. Cameron static int number_of_controllers; 129edd16368SStephen M. Cameron 13010f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id); 13110f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id); 132edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg); 133edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h); 134edd16368SStephen M. Cameron 135edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT 136edd16368SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg); 137edd16368SStephen M. Cameron #endif 138edd16368SStephen M. Cameron 139edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c); 140edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c); 141edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h); 142edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h); 14301a02ffcSStephen M. Cameron static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h, 14401a02ffcSStephen M. Cameron void *buff, size_t size, u8 page_code, unsigned char *scsi3addr, 145edd16368SStephen M. Cameron int cmd_type); 146edd16368SStephen M. Cameron 147f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd); 148a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *); 149a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh, 150a08a8471SStephen M. Cameron unsigned long elapsed_time); 151667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev, 152667e23d4SStephen M. Cameron int qdepth, int reason); 153edd16368SStephen M. Cameron 154edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd); 155edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev); 156edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev); 157edd16368SStephen M. Cameron 158edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno); 159edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h, 160edd16368SStephen M. Cameron struct CommandList *c); 161edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h, 162edd16368SStephen M. Cameron struct CommandList *c); 163303932fdSDon Brace /* performant mode helper functions */ 164303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets, 165303932fdSDon Brace int nsgs, int *bucket_map); 1667136f9a7SStephen M. Cameron static __devinit void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h); 167303932fdSDon Brace static inline u32 next_command(struct ctlr_info *h); 1681df8552aSStephen M. Cameron static int __devinit hpsa_find_cfg_addrs(struct pci_dev *pdev, 1691df8552aSStephen M. Cameron void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index, 1701df8552aSStephen M. Cameron u64 *cfg_offset); 1711df8552aSStephen M. Cameron static int __devinit hpsa_pci_find_memory_BAR(struct pci_dev *pdev, 1721df8552aSStephen M. Cameron unsigned long *memory_bar); 17318867659SStephen M. Cameron static int __devinit hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id); 174fe5389c8SStephen M. Cameron static int __devinit hpsa_wait_for_board_state(struct pci_dev *pdev, 175fe5389c8SStephen M. Cameron void __iomem *vaddr, int wait_for_ready); 176fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0 177fe5389c8SStephen M. Cameron #define BOARD_READY 1 178edd16368SStephen M. Cameron 179edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev) 180edd16368SStephen M. Cameron { 181edd16368SStephen M. Cameron unsigned long *priv = shost_priv(sdev->host); 182edd16368SStephen M. Cameron return (struct ctlr_info *) *priv; 183edd16368SStephen M. Cameron } 184edd16368SStephen M. Cameron 185a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh) 186a23513e8SStephen M. Cameron { 187a23513e8SStephen M. Cameron unsigned long *priv = shost_priv(sh); 188a23513e8SStephen M. Cameron return (struct ctlr_info *) *priv; 189a23513e8SStephen M. Cameron } 190a23513e8SStephen M. Cameron 191edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h, 192edd16368SStephen M. Cameron struct CommandList *c) 193edd16368SStephen M. Cameron { 194edd16368SStephen M. Cameron if (c->err_info->SenseInfo[2] != UNIT_ATTENTION) 195edd16368SStephen M. Cameron return 0; 196edd16368SStephen M. Cameron 197edd16368SStephen M. Cameron switch (c->err_info->SenseInfo[12]) { 198edd16368SStephen M. Cameron case STATE_CHANGED: 199edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: a state change " 200edd16368SStephen M. Cameron "detected, command retried\n", h->ctlr); 201edd16368SStephen M. Cameron break; 202edd16368SStephen M. Cameron case LUN_FAILED: 203edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: LUN failure " 204edd16368SStephen M. Cameron "detected, action required\n", h->ctlr); 205edd16368SStephen M. Cameron break; 206edd16368SStephen M. Cameron case REPORT_LUNS_CHANGED: 207edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: report LUN data " 20831468401SMike Miller "changed, action required\n", h->ctlr); 209edd16368SStephen M. Cameron /* 210edd16368SStephen M. Cameron * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012. 211edd16368SStephen M. Cameron */ 212edd16368SStephen M. Cameron break; 213edd16368SStephen M. Cameron case POWER_OR_RESET: 214edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: a power on " 215edd16368SStephen M. Cameron "or device reset detected\n", h->ctlr); 216edd16368SStephen M. Cameron break; 217edd16368SStephen M. Cameron case UNIT_ATTENTION_CLEARED: 218edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: unit attention " 219edd16368SStephen M. Cameron "cleared by another initiator\n", h->ctlr); 220edd16368SStephen M. Cameron break; 221edd16368SStephen M. Cameron default: 222edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "hpsa%d: unknown " 223edd16368SStephen M. Cameron "unit attention detected\n", h->ctlr); 224edd16368SStephen M. Cameron break; 225edd16368SStephen M. Cameron } 226edd16368SStephen M. Cameron return 1; 227edd16368SStephen M. Cameron } 228edd16368SStephen M. Cameron 229edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev, 230edd16368SStephen M. Cameron struct device_attribute *attr, 231edd16368SStephen M. Cameron const char *buf, size_t count) 232edd16368SStephen M. Cameron { 233edd16368SStephen M. Cameron struct ctlr_info *h; 234edd16368SStephen M. Cameron struct Scsi_Host *shost = class_to_shost(dev); 235a23513e8SStephen M. Cameron h = shost_to_hba(shost); 23631468401SMike Miller hpsa_scan_start(h->scsi_host); 237edd16368SStephen M. Cameron return count; 238edd16368SStephen M. Cameron } 239edd16368SStephen M. Cameron 240d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev, 241d28ce020SStephen M. Cameron struct device_attribute *attr, char *buf) 242d28ce020SStephen M. Cameron { 243d28ce020SStephen M. Cameron struct ctlr_info *h; 244d28ce020SStephen M. Cameron struct Scsi_Host *shost = class_to_shost(dev); 245d28ce020SStephen M. Cameron unsigned char *fwrev; 246d28ce020SStephen M. Cameron 247d28ce020SStephen M. Cameron h = shost_to_hba(shost); 248d28ce020SStephen M. Cameron if (!h->hba_inquiry_data) 249d28ce020SStephen M. Cameron return 0; 250d28ce020SStephen M. Cameron fwrev = &h->hba_inquiry_data[32]; 251d28ce020SStephen M. Cameron return snprintf(buf, 20, "%c%c%c%c\n", 252d28ce020SStephen M. Cameron fwrev[0], fwrev[1], fwrev[2], fwrev[3]); 253d28ce020SStephen M. Cameron } 254d28ce020SStephen M. Cameron 25594a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev, 25694a13649SStephen M. Cameron struct device_attribute *attr, char *buf) 25794a13649SStephen M. Cameron { 25894a13649SStephen M. Cameron struct Scsi_Host *shost = class_to_shost(dev); 25994a13649SStephen M. Cameron struct ctlr_info *h = shost_to_hba(shost); 26094a13649SStephen M. Cameron 26194a13649SStephen M. Cameron return snprintf(buf, 20, "%d\n", h->commands_outstanding); 26294a13649SStephen M. Cameron } 26394a13649SStephen M. Cameron 264745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev, 265745a7a25SStephen M. Cameron struct device_attribute *attr, char *buf) 266745a7a25SStephen M. Cameron { 267745a7a25SStephen M. Cameron struct ctlr_info *h; 268745a7a25SStephen M. Cameron struct Scsi_Host *shost = class_to_shost(dev); 269745a7a25SStephen M. Cameron 270745a7a25SStephen M. Cameron h = shost_to_hba(shost); 271745a7a25SStephen M. Cameron return snprintf(buf, 20, "%s\n", 272960a30e7SStephen M. Cameron h->transMethod & CFGTBL_Trans_Performant ? 273745a7a25SStephen M. Cameron "performant" : "simple"); 274745a7a25SStephen M. Cameron } 275745a7a25SStephen M. Cameron 27646380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */ 277941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = { 278941b1cdaSStephen M. Cameron 0x324a103C, /* Smart Array P712m */ 279941b1cdaSStephen M. Cameron 0x324b103C, /* SmartArray P711m */ 280941b1cdaSStephen M. Cameron 0x3223103C, /* Smart Array P800 */ 281941b1cdaSStephen M. Cameron 0x3234103C, /* Smart Array P400 */ 282941b1cdaSStephen M. Cameron 0x3235103C, /* Smart Array P400i */ 283941b1cdaSStephen M. Cameron 0x3211103C, /* Smart Array E200i */ 284941b1cdaSStephen M. Cameron 0x3212103C, /* Smart Array E200 */ 285941b1cdaSStephen M. Cameron 0x3213103C, /* Smart Array E200i */ 286941b1cdaSStephen M. Cameron 0x3214103C, /* Smart Array E200i */ 287941b1cdaSStephen M. Cameron 0x3215103C, /* Smart Array E200i */ 288941b1cdaSStephen M. Cameron 0x3237103C, /* Smart Array E500 */ 289941b1cdaSStephen M. Cameron 0x323D103C, /* Smart Array P700m */ 290941b1cdaSStephen M. Cameron 0x409C0E11, /* Smart Array 6400 */ 291941b1cdaSStephen M. Cameron 0x409D0E11, /* Smart Array 6400 EM */ 292941b1cdaSStephen M. Cameron }; 293941b1cdaSStephen M. Cameron 29446380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */ 29546380786SStephen M. Cameron static u32 soft_unresettable_controller[] = { 29646380786SStephen M. Cameron /* Exclude 640x boards. These are two pci devices in one slot 29746380786SStephen M. Cameron * which share a battery backed cache module. One controls the 29846380786SStephen M. Cameron * cache, the other accesses the cache through the one that controls 29946380786SStephen M. Cameron * it. If we reset the one controlling the cache, the other will 30046380786SStephen M. Cameron * likely not be happy. Just forbid resetting this conjoined mess. 30146380786SStephen M. Cameron * The 640x isn't really supported by hpsa anyway. 30246380786SStephen M. Cameron */ 30346380786SStephen M. Cameron 0x409C0E11, /* Smart Array 6400 */ 30446380786SStephen M. Cameron 0x409D0E11, /* Smart Array 6400 EM */ 30546380786SStephen M. Cameron }; 30646380786SStephen M. Cameron 30746380786SStephen M. Cameron static int ctlr_is_hard_resettable(u32 board_id) 308941b1cdaSStephen M. Cameron { 309941b1cdaSStephen M. Cameron int i; 310941b1cdaSStephen M. Cameron 311941b1cdaSStephen M. Cameron for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++) 31246380786SStephen M. Cameron if (unresettable_controller[i] == board_id) 313941b1cdaSStephen M. Cameron return 0; 314941b1cdaSStephen M. Cameron return 1; 315941b1cdaSStephen M. Cameron } 316941b1cdaSStephen M. Cameron 31746380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id) 31846380786SStephen M. Cameron { 31946380786SStephen M. Cameron int i; 32046380786SStephen M. Cameron 32146380786SStephen M. Cameron for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++) 32246380786SStephen M. Cameron if (soft_unresettable_controller[i] == board_id) 32346380786SStephen M. Cameron return 0; 32446380786SStephen M. Cameron return 1; 32546380786SStephen M. Cameron } 32646380786SStephen M. Cameron 32746380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id) 32846380786SStephen M. Cameron { 32946380786SStephen M. Cameron return ctlr_is_hard_resettable(board_id) || 33046380786SStephen M. Cameron ctlr_is_soft_resettable(board_id); 33146380786SStephen M. Cameron } 33246380786SStephen M. Cameron 333941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev, 334941b1cdaSStephen M. Cameron struct device_attribute *attr, char *buf) 335941b1cdaSStephen M. Cameron { 336941b1cdaSStephen M. Cameron struct ctlr_info *h; 337941b1cdaSStephen M. Cameron struct Scsi_Host *shost = class_to_shost(dev); 338941b1cdaSStephen M. Cameron 339941b1cdaSStephen M. Cameron h = shost_to_hba(shost); 34046380786SStephen M. Cameron return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id)); 341941b1cdaSStephen M. Cameron } 342941b1cdaSStephen M. Cameron 343edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[]) 344edd16368SStephen M. Cameron { 345edd16368SStephen M. Cameron return (scsi3addr[3] & 0xC0) == 0x40; 346edd16368SStephen M. Cameron } 347edd16368SStephen M. Cameron 348edd16368SStephen M. Cameron static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG", 349edd16368SStephen M. Cameron "UNKNOWN" 350edd16368SStephen M. Cameron }; 351edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1) 352edd16368SStephen M. Cameron 353edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev, 354edd16368SStephen M. Cameron struct device_attribute *attr, char *buf) 355edd16368SStephen M. Cameron { 356edd16368SStephen M. Cameron ssize_t l = 0; 35782a72c0aSStephen M. Cameron unsigned char rlevel; 358edd16368SStephen M. Cameron struct ctlr_info *h; 359edd16368SStephen M. Cameron struct scsi_device *sdev; 360edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *hdev; 361edd16368SStephen M. Cameron unsigned long flags; 362edd16368SStephen M. Cameron 363edd16368SStephen M. Cameron sdev = to_scsi_device(dev); 364edd16368SStephen M. Cameron h = sdev_to_hba(sdev); 365edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 366edd16368SStephen M. Cameron hdev = sdev->hostdata; 367edd16368SStephen M. Cameron if (!hdev) { 368edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 369edd16368SStephen M. Cameron return -ENODEV; 370edd16368SStephen M. Cameron } 371edd16368SStephen M. Cameron 372edd16368SStephen M. Cameron /* Is this even a logical drive? */ 373edd16368SStephen M. Cameron if (!is_logical_dev_addr_mode(hdev->scsi3addr)) { 374edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 375edd16368SStephen M. Cameron l = snprintf(buf, PAGE_SIZE, "N/A\n"); 376edd16368SStephen M. Cameron return l; 377edd16368SStephen M. Cameron } 378edd16368SStephen M. Cameron 379edd16368SStephen M. Cameron rlevel = hdev->raid_level; 380edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 38182a72c0aSStephen M. Cameron if (rlevel > RAID_UNKNOWN) 382edd16368SStephen M. Cameron rlevel = RAID_UNKNOWN; 383edd16368SStephen M. Cameron l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]); 384edd16368SStephen M. Cameron return l; 385edd16368SStephen M. Cameron } 386edd16368SStephen M. Cameron 387edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev, 388edd16368SStephen M. Cameron struct device_attribute *attr, char *buf) 389edd16368SStephen M. Cameron { 390edd16368SStephen M. Cameron struct ctlr_info *h; 391edd16368SStephen M. Cameron struct scsi_device *sdev; 392edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *hdev; 393edd16368SStephen M. Cameron unsigned long flags; 394edd16368SStephen M. Cameron unsigned char lunid[8]; 395edd16368SStephen M. Cameron 396edd16368SStephen M. Cameron sdev = to_scsi_device(dev); 397edd16368SStephen M. Cameron h = sdev_to_hba(sdev); 398edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 399edd16368SStephen M. Cameron hdev = sdev->hostdata; 400edd16368SStephen M. Cameron if (!hdev) { 401edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 402edd16368SStephen M. Cameron return -ENODEV; 403edd16368SStephen M. Cameron } 404edd16368SStephen M. Cameron memcpy(lunid, hdev->scsi3addr, sizeof(lunid)); 405edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 406edd16368SStephen M. Cameron return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n", 407edd16368SStephen M. Cameron lunid[0], lunid[1], lunid[2], lunid[3], 408edd16368SStephen M. Cameron lunid[4], lunid[5], lunid[6], lunid[7]); 409edd16368SStephen M. Cameron } 410edd16368SStephen M. Cameron 411edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev, 412edd16368SStephen M. Cameron struct device_attribute *attr, char *buf) 413edd16368SStephen M. Cameron { 414edd16368SStephen M. Cameron struct ctlr_info *h; 415edd16368SStephen M. Cameron struct scsi_device *sdev; 416edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *hdev; 417edd16368SStephen M. Cameron unsigned long flags; 418edd16368SStephen M. Cameron unsigned char sn[16]; 419edd16368SStephen M. Cameron 420edd16368SStephen M. Cameron sdev = to_scsi_device(dev); 421edd16368SStephen M. Cameron h = sdev_to_hba(sdev); 422edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 423edd16368SStephen M. Cameron hdev = sdev->hostdata; 424edd16368SStephen M. Cameron if (!hdev) { 425edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 426edd16368SStephen M. Cameron return -ENODEV; 427edd16368SStephen M. Cameron } 428edd16368SStephen M. Cameron memcpy(sn, hdev->device_id, sizeof(sn)); 429edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 430edd16368SStephen M. Cameron return snprintf(buf, 16 * 2 + 2, 431edd16368SStephen M. Cameron "%02X%02X%02X%02X%02X%02X%02X%02X" 432edd16368SStephen M. Cameron "%02X%02X%02X%02X%02X%02X%02X%02X\n", 433edd16368SStephen M. Cameron sn[0], sn[1], sn[2], sn[3], 434edd16368SStephen M. Cameron sn[4], sn[5], sn[6], sn[7], 435edd16368SStephen M. Cameron sn[8], sn[9], sn[10], sn[11], 436edd16368SStephen M. Cameron sn[12], sn[13], sn[14], sn[15]); 437edd16368SStephen M. Cameron } 438edd16368SStephen M. Cameron 4393f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL); 4403f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL); 4413f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL); 4423f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan); 4433f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO, 4443f5eac3aSStephen M. Cameron host_show_firmware_revision, NULL); 4453f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO, 4463f5eac3aSStephen M. Cameron host_show_commands_outstanding, NULL); 4473f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO, 4483f5eac3aSStephen M. Cameron host_show_transport_mode, NULL); 449941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO, 450941b1cdaSStephen M. Cameron host_show_resettable, NULL); 4513f5eac3aSStephen M. Cameron 4523f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = { 4533f5eac3aSStephen M. Cameron &dev_attr_raid_level, 4543f5eac3aSStephen M. Cameron &dev_attr_lunid, 4553f5eac3aSStephen M. Cameron &dev_attr_unique_id, 4563f5eac3aSStephen M. Cameron NULL, 4573f5eac3aSStephen M. Cameron }; 4583f5eac3aSStephen M. Cameron 4593f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = { 4603f5eac3aSStephen M. Cameron &dev_attr_rescan, 4613f5eac3aSStephen M. Cameron &dev_attr_firmware_revision, 4623f5eac3aSStephen M. Cameron &dev_attr_commands_outstanding, 4633f5eac3aSStephen M. Cameron &dev_attr_transport_mode, 464941b1cdaSStephen M. Cameron &dev_attr_resettable, 4653f5eac3aSStephen M. Cameron NULL, 4663f5eac3aSStephen M. Cameron }; 4673f5eac3aSStephen M. Cameron 4683f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = { 4693f5eac3aSStephen M. Cameron .module = THIS_MODULE, 4703f5eac3aSStephen M. Cameron .name = "hpsa", 4713f5eac3aSStephen M. Cameron .proc_name = "hpsa", 4723f5eac3aSStephen M. Cameron .queuecommand = hpsa_scsi_queue_command, 4733f5eac3aSStephen M. Cameron .scan_start = hpsa_scan_start, 4743f5eac3aSStephen M. Cameron .scan_finished = hpsa_scan_finished, 4753f5eac3aSStephen M. Cameron .change_queue_depth = hpsa_change_queue_depth, 4763f5eac3aSStephen M. Cameron .this_id = -1, 4773f5eac3aSStephen M. Cameron .use_clustering = ENABLE_CLUSTERING, 4783f5eac3aSStephen M. Cameron .eh_device_reset_handler = hpsa_eh_device_reset_handler, 4793f5eac3aSStephen M. Cameron .ioctl = hpsa_ioctl, 4803f5eac3aSStephen M. Cameron .slave_alloc = hpsa_slave_alloc, 4813f5eac3aSStephen M. Cameron .slave_destroy = hpsa_slave_destroy, 4823f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT 4833f5eac3aSStephen M. Cameron .compat_ioctl = hpsa_compat_ioctl, 4843f5eac3aSStephen M. Cameron #endif 4853f5eac3aSStephen M. Cameron .sdev_attrs = hpsa_sdev_attrs, 4863f5eac3aSStephen M. Cameron .shost_attrs = hpsa_shost_attrs, 4873f5eac3aSStephen M. Cameron }; 4883f5eac3aSStephen M. Cameron 4893f5eac3aSStephen M. Cameron 4903f5eac3aSStephen M. Cameron /* Enqueuing and dequeuing functions for cmdlists. */ 4913f5eac3aSStephen M. Cameron static inline void addQ(struct list_head *list, struct CommandList *c) 4923f5eac3aSStephen M. Cameron { 4933f5eac3aSStephen M. Cameron list_add_tail(&c->list, list); 4943f5eac3aSStephen M. Cameron } 4953f5eac3aSStephen M. Cameron 4963f5eac3aSStephen M. Cameron static inline u32 next_command(struct ctlr_info *h) 4973f5eac3aSStephen M. Cameron { 4983f5eac3aSStephen M. Cameron u32 a; 4993f5eac3aSStephen M. Cameron 5003f5eac3aSStephen M. Cameron if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant))) 5013f5eac3aSStephen M. Cameron return h->access.command_completed(h); 5023f5eac3aSStephen M. Cameron 5033f5eac3aSStephen M. Cameron if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) { 5043f5eac3aSStephen M. Cameron a = *(h->reply_pool_head); /* Next cmd in ring buffer */ 5053f5eac3aSStephen M. Cameron (h->reply_pool_head)++; 5063f5eac3aSStephen M. Cameron h->commands_outstanding--; 5073f5eac3aSStephen M. Cameron } else { 5083f5eac3aSStephen M. Cameron a = FIFO_EMPTY; 5093f5eac3aSStephen M. Cameron } 5103f5eac3aSStephen M. Cameron /* Check for wraparound */ 5113f5eac3aSStephen M. Cameron if (h->reply_pool_head == (h->reply_pool + h->max_commands)) { 5123f5eac3aSStephen M. Cameron h->reply_pool_head = h->reply_pool; 5133f5eac3aSStephen M. Cameron h->reply_pool_wraparound ^= 1; 5143f5eac3aSStephen M. Cameron } 5153f5eac3aSStephen M. Cameron return a; 5163f5eac3aSStephen M. Cameron } 5173f5eac3aSStephen M. Cameron 5183f5eac3aSStephen M. Cameron /* set_performant_mode: Modify the tag for cciss performant 5193f5eac3aSStephen M. Cameron * set bit 0 for pull model, bits 3-1 for block fetch 5203f5eac3aSStephen M. Cameron * register number 5213f5eac3aSStephen M. Cameron */ 5223f5eac3aSStephen M. Cameron static void set_performant_mode(struct ctlr_info *h, struct CommandList *c) 5233f5eac3aSStephen M. Cameron { 5243f5eac3aSStephen M. Cameron if (likely(h->transMethod & CFGTBL_Trans_Performant)) 5253f5eac3aSStephen M. Cameron c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1); 5263f5eac3aSStephen M. Cameron } 5273f5eac3aSStephen M. Cameron 5283f5eac3aSStephen M. Cameron static void enqueue_cmd_and_start_io(struct ctlr_info *h, 5293f5eac3aSStephen M. Cameron struct CommandList *c) 5303f5eac3aSStephen M. Cameron { 5313f5eac3aSStephen M. Cameron unsigned long flags; 5323f5eac3aSStephen M. Cameron 5333f5eac3aSStephen M. Cameron set_performant_mode(h, c); 5343f5eac3aSStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 5353f5eac3aSStephen M. Cameron addQ(&h->reqQ, c); 5363f5eac3aSStephen M. Cameron h->Qdepth++; 5373f5eac3aSStephen M. Cameron start_io(h); 5383f5eac3aSStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 5393f5eac3aSStephen M. Cameron } 5403f5eac3aSStephen M. Cameron 5413f5eac3aSStephen M. Cameron static inline void removeQ(struct CommandList *c) 5423f5eac3aSStephen M. Cameron { 5433f5eac3aSStephen M. Cameron if (WARN_ON(list_empty(&c->list))) 5443f5eac3aSStephen M. Cameron return; 5453f5eac3aSStephen M. Cameron list_del_init(&c->list); 5463f5eac3aSStephen M. Cameron } 5473f5eac3aSStephen M. Cameron 5483f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[]) 5493f5eac3aSStephen M. Cameron { 5503f5eac3aSStephen M. Cameron return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0; 5513f5eac3aSStephen M. Cameron } 5523f5eac3aSStephen M. Cameron 5533f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h) 5543f5eac3aSStephen M. Cameron { 5553f5eac3aSStephen M. Cameron if (!h->hba_inquiry_data) 5563f5eac3aSStephen M. Cameron return 0; 5573f5eac3aSStephen M. Cameron if ((h->hba_inquiry_data[2] & 0x07) == 5) 5583f5eac3aSStephen M. Cameron return 1; 5593f5eac3aSStephen M. Cameron return 0; 5603f5eac3aSStephen M. Cameron } 5613f5eac3aSStephen M. Cameron 562edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h, 563edd16368SStephen M. Cameron unsigned char scsi3addr[], int bus, int *target, int *lun) 564edd16368SStephen M. Cameron { 565edd16368SStephen M. Cameron /* finds an unused bus, target, lun for a new physical device 566edd16368SStephen M. Cameron * assumes h->devlock is held 567edd16368SStephen M. Cameron */ 568edd16368SStephen M. Cameron int i, found = 0; 569edd16368SStephen M. Cameron DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA); 570edd16368SStephen M. Cameron 571edd16368SStephen M. Cameron memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3); 572edd16368SStephen M. Cameron 573edd16368SStephen M. Cameron for (i = 0; i < h->ndevices; i++) { 574edd16368SStephen M. Cameron if (h->dev[i]->bus == bus && h->dev[i]->target != -1) 575edd16368SStephen M. Cameron set_bit(h->dev[i]->target, lun_taken); 576edd16368SStephen M. Cameron } 577edd16368SStephen M. Cameron 578edd16368SStephen M. Cameron for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) { 579edd16368SStephen M. Cameron if (!test_bit(i, lun_taken)) { 580edd16368SStephen M. Cameron /* *bus = 1; */ 581edd16368SStephen M. Cameron *target = i; 582edd16368SStephen M. Cameron *lun = 0; 583edd16368SStephen M. Cameron found = 1; 584edd16368SStephen M. Cameron break; 585edd16368SStephen M. Cameron } 586edd16368SStephen M. Cameron } 587edd16368SStephen M. Cameron return !found; 588edd16368SStephen M. Cameron } 589edd16368SStephen M. Cameron 590edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */ 591edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno, 592edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *device, 593edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *added[], int *nadded) 594edd16368SStephen M. Cameron { 595edd16368SStephen M. Cameron /* assumes h->devlock is held */ 596edd16368SStephen M. Cameron int n = h->ndevices; 597edd16368SStephen M. Cameron int i; 598edd16368SStephen M. Cameron unsigned char addr1[8], addr2[8]; 599edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd; 600edd16368SStephen M. Cameron 601edd16368SStephen M. Cameron if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) { 602edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "too many devices, some will be " 603edd16368SStephen M. Cameron "inaccessible.\n"); 604edd16368SStephen M. Cameron return -1; 605edd16368SStephen M. Cameron } 606edd16368SStephen M. Cameron 607edd16368SStephen M. Cameron /* physical devices do not have lun or target assigned until now. */ 608edd16368SStephen M. Cameron if (device->lun != -1) 609edd16368SStephen M. Cameron /* Logical device, lun is already assigned. */ 610edd16368SStephen M. Cameron goto lun_assigned; 611edd16368SStephen M. Cameron 612edd16368SStephen M. Cameron /* If this device a non-zero lun of a multi-lun device 613edd16368SStephen M. Cameron * byte 4 of the 8-byte LUN addr will contain the logical 614edd16368SStephen M. Cameron * unit no, zero otherise. 615edd16368SStephen M. Cameron */ 616edd16368SStephen M. Cameron if (device->scsi3addr[4] == 0) { 617edd16368SStephen M. Cameron /* This is not a non-zero lun of a multi-lun device */ 618edd16368SStephen M. Cameron if (hpsa_find_target_lun(h, device->scsi3addr, 619edd16368SStephen M. Cameron device->bus, &device->target, &device->lun) != 0) 620edd16368SStephen M. Cameron return -1; 621edd16368SStephen M. Cameron goto lun_assigned; 622edd16368SStephen M. Cameron } 623edd16368SStephen M. Cameron 624edd16368SStephen M. Cameron /* This is a non-zero lun of a multi-lun device. 625edd16368SStephen M. Cameron * Search through our list and find the device which 626edd16368SStephen M. Cameron * has the same 8 byte LUN address, excepting byte 4. 627edd16368SStephen M. Cameron * Assign the same bus and target for this new LUN. 628edd16368SStephen M. Cameron * Use the logical unit number from the firmware. 629edd16368SStephen M. Cameron */ 630edd16368SStephen M. Cameron memcpy(addr1, device->scsi3addr, 8); 631edd16368SStephen M. Cameron addr1[4] = 0; 632edd16368SStephen M. Cameron for (i = 0; i < n; i++) { 633edd16368SStephen M. Cameron sd = h->dev[i]; 634edd16368SStephen M. Cameron memcpy(addr2, sd->scsi3addr, 8); 635edd16368SStephen M. Cameron addr2[4] = 0; 636edd16368SStephen M. Cameron /* differ only in byte 4? */ 637edd16368SStephen M. Cameron if (memcmp(addr1, addr2, 8) == 0) { 638edd16368SStephen M. Cameron device->bus = sd->bus; 639edd16368SStephen M. Cameron device->target = sd->target; 640edd16368SStephen M. Cameron device->lun = device->scsi3addr[4]; 641edd16368SStephen M. Cameron break; 642edd16368SStephen M. Cameron } 643edd16368SStephen M. Cameron } 644edd16368SStephen M. Cameron if (device->lun == -1) { 645edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "physical device with no LUN=0," 646edd16368SStephen M. Cameron " suspect firmware bug or unsupported hardware " 647edd16368SStephen M. Cameron "configuration.\n"); 648edd16368SStephen M. Cameron return -1; 649edd16368SStephen M. Cameron } 650edd16368SStephen M. Cameron 651edd16368SStephen M. Cameron lun_assigned: 652edd16368SStephen M. Cameron 653edd16368SStephen M. Cameron h->dev[n] = device; 654edd16368SStephen M. Cameron h->ndevices++; 655edd16368SStephen M. Cameron added[*nadded] = device; 656edd16368SStephen M. Cameron (*nadded)++; 657edd16368SStephen M. Cameron 658edd16368SStephen M. Cameron /* initially, (before registering with scsi layer) we don't 659edd16368SStephen M. Cameron * know our hostno and we don't want to print anything first 660edd16368SStephen M. Cameron * time anyway (the scsi layer's inquiries will show that info) 661edd16368SStephen M. Cameron */ 662edd16368SStephen M. Cameron /* if (hostno != -1) */ 663edd16368SStephen M. Cameron dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n", 664edd16368SStephen M. Cameron scsi_device_type(device->devtype), hostno, 665edd16368SStephen M. Cameron device->bus, device->target, device->lun); 666edd16368SStephen M. Cameron return 0; 667edd16368SStephen M. Cameron } 668edd16368SStephen M. Cameron 6692a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */ 6702a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno, 6712a8ccf31SStephen M. Cameron int entry, struct hpsa_scsi_dev_t *new_entry, 6722a8ccf31SStephen M. Cameron struct hpsa_scsi_dev_t *added[], int *nadded, 6732a8ccf31SStephen M. Cameron struct hpsa_scsi_dev_t *removed[], int *nremoved) 6742a8ccf31SStephen M. Cameron { 6752a8ccf31SStephen M. Cameron /* assumes h->devlock is held */ 6762a8ccf31SStephen M. Cameron BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA); 6772a8ccf31SStephen M. Cameron removed[*nremoved] = h->dev[entry]; 6782a8ccf31SStephen M. Cameron (*nremoved)++; 6792a8ccf31SStephen M. Cameron h->dev[entry] = new_entry; 6802a8ccf31SStephen M. Cameron added[*nadded] = new_entry; 6812a8ccf31SStephen M. Cameron (*nadded)++; 6822a8ccf31SStephen M. Cameron dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n", 6832a8ccf31SStephen M. Cameron scsi_device_type(new_entry->devtype), hostno, new_entry->bus, 6842a8ccf31SStephen M. Cameron new_entry->target, new_entry->lun); 6852a8ccf31SStephen M. Cameron } 6862a8ccf31SStephen M. Cameron 687edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */ 688edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry, 689edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *removed[], int *nremoved) 690edd16368SStephen M. Cameron { 691edd16368SStephen M. Cameron /* assumes h->devlock is held */ 692edd16368SStephen M. Cameron int i; 693edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd; 694edd16368SStephen M. Cameron 695b2ed4f79SStephen M. Cameron BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA); 696edd16368SStephen M. Cameron 697edd16368SStephen M. Cameron sd = h->dev[entry]; 698edd16368SStephen M. Cameron removed[*nremoved] = h->dev[entry]; 699edd16368SStephen M. Cameron (*nremoved)++; 700edd16368SStephen M. Cameron 701edd16368SStephen M. Cameron for (i = entry; i < h->ndevices-1; i++) 702edd16368SStephen M. Cameron h->dev[i] = h->dev[i+1]; 703edd16368SStephen M. Cameron h->ndevices--; 704edd16368SStephen M. Cameron dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n", 705edd16368SStephen M. Cameron scsi_device_type(sd->devtype), hostno, sd->bus, sd->target, 706edd16368SStephen M. Cameron sd->lun); 707edd16368SStephen M. Cameron } 708edd16368SStephen M. Cameron 709edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \ 710edd16368SStephen M. Cameron (a)[7] == (b)[7] && \ 711edd16368SStephen M. Cameron (a)[6] == (b)[6] && \ 712edd16368SStephen M. Cameron (a)[5] == (b)[5] && \ 713edd16368SStephen M. Cameron (a)[4] == (b)[4] && \ 714edd16368SStephen M. Cameron (a)[3] == (b)[3] && \ 715edd16368SStephen M. Cameron (a)[2] == (b)[2] && \ 716edd16368SStephen M. Cameron (a)[1] == (b)[1] && \ 717edd16368SStephen M. Cameron (a)[0] == (b)[0]) 718edd16368SStephen M. Cameron 719edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h, 720edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *added) 721edd16368SStephen M. Cameron { 722edd16368SStephen M. Cameron /* called when scsi_add_device fails in order to re-adjust 723edd16368SStephen M. Cameron * h->dev[] to match the mid layer's view. 724edd16368SStephen M. Cameron */ 725edd16368SStephen M. Cameron unsigned long flags; 726edd16368SStephen M. Cameron int i, j; 727edd16368SStephen M. Cameron 728edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 729edd16368SStephen M. Cameron for (i = 0; i < h->ndevices; i++) { 730edd16368SStephen M. Cameron if (h->dev[i] == added) { 731edd16368SStephen M. Cameron for (j = i; j < h->ndevices-1; j++) 732edd16368SStephen M. Cameron h->dev[j] = h->dev[j+1]; 733edd16368SStephen M. Cameron h->ndevices--; 734edd16368SStephen M. Cameron break; 735edd16368SStephen M. Cameron } 736edd16368SStephen M. Cameron } 737edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 738edd16368SStephen M. Cameron kfree(added); 739edd16368SStephen M. Cameron } 740edd16368SStephen M. Cameron 741edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1, 742edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *dev2) 743edd16368SStephen M. Cameron { 744edd16368SStephen M. Cameron /* we compare everything except lun and target as these 745edd16368SStephen M. Cameron * are not yet assigned. Compare parts likely 746edd16368SStephen M. Cameron * to differ first 747edd16368SStephen M. Cameron */ 748edd16368SStephen M. Cameron if (memcmp(dev1->scsi3addr, dev2->scsi3addr, 749edd16368SStephen M. Cameron sizeof(dev1->scsi3addr)) != 0) 750edd16368SStephen M. Cameron return 0; 751edd16368SStephen M. Cameron if (memcmp(dev1->device_id, dev2->device_id, 752edd16368SStephen M. Cameron sizeof(dev1->device_id)) != 0) 753edd16368SStephen M. Cameron return 0; 754edd16368SStephen M. Cameron if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0) 755edd16368SStephen M. Cameron return 0; 756edd16368SStephen M. Cameron if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0) 757edd16368SStephen M. Cameron return 0; 758edd16368SStephen M. Cameron if (dev1->devtype != dev2->devtype) 759edd16368SStephen M. Cameron return 0; 760edd16368SStephen M. Cameron if (dev1->bus != dev2->bus) 761edd16368SStephen M. Cameron return 0; 762edd16368SStephen M. Cameron return 1; 763edd16368SStephen M. Cameron } 764edd16368SStephen M. Cameron 765edd16368SStephen M. Cameron /* Find needle in haystack. If exact match found, return DEVICE_SAME, 766edd16368SStephen M. Cameron * and return needle location in *index. If scsi3addr matches, but not 767edd16368SStephen M. Cameron * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle 768edd16368SStephen M. Cameron * location in *index. If needle not found, return DEVICE_NOT_FOUND. 769edd16368SStephen M. Cameron */ 770edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle, 771edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *haystack[], int haystack_size, 772edd16368SStephen M. Cameron int *index) 773edd16368SStephen M. Cameron { 774edd16368SStephen M. Cameron int i; 775edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0 776edd16368SStephen M. Cameron #define DEVICE_CHANGED 1 777edd16368SStephen M. Cameron #define DEVICE_SAME 2 778edd16368SStephen M. Cameron for (i = 0; i < haystack_size; i++) { 77923231048SStephen M. Cameron if (haystack[i] == NULL) /* previously removed. */ 78023231048SStephen M. Cameron continue; 781edd16368SStephen M. Cameron if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) { 782edd16368SStephen M. Cameron *index = i; 783edd16368SStephen M. Cameron if (device_is_the_same(needle, haystack[i])) 784edd16368SStephen M. Cameron return DEVICE_SAME; 785edd16368SStephen M. Cameron else 786edd16368SStephen M. Cameron return DEVICE_CHANGED; 787edd16368SStephen M. Cameron } 788edd16368SStephen M. Cameron } 789edd16368SStephen M. Cameron *index = -1; 790edd16368SStephen M. Cameron return DEVICE_NOT_FOUND; 791edd16368SStephen M. Cameron } 792edd16368SStephen M. Cameron 7934967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno, 794edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd[], int nsds) 795edd16368SStephen M. Cameron { 796edd16368SStephen M. Cameron /* sd contains scsi3 addresses and devtypes, and inquiry 797edd16368SStephen M. Cameron * data. This function takes what's in sd to be the current 798edd16368SStephen M. Cameron * reality and updates h->dev[] to reflect that reality. 799edd16368SStephen M. Cameron */ 800edd16368SStephen M. Cameron int i, entry, device_change, changes = 0; 801edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *csd; 802edd16368SStephen M. Cameron unsigned long flags; 803edd16368SStephen M. Cameron struct hpsa_scsi_dev_t **added, **removed; 804edd16368SStephen M. Cameron int nadded, nremoved; 805edd16368SStephen M. Cameron struct Scsi_Host *sh = NULL; 806edd16368SStephen M. Cameron 807edd16368SStephen M. Cameron added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA, 808edd16368SStephen M. Cameron GFP_KERNEL); 809edd16368SStephen M. Cameron removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA, 810edd16368SStephen M. Cameron GFP_KERNEL); 811edd16368SStephen M. Cameron 812edd16368SStephen M. Cameron if (!added || !removed) { 813edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "out of memory in " 814edd16368SStephen M. Cameron "adjust_hpsa_scsi_table\n"); 815edd16368SStephen M. Cameron goto free_and_out; 816edd16368SStephen M. Cameron } 817edd16368SStephen M. Cameron 818edd16368SStephen M. Cameron spin_lock_irqsave(&h->devlock, flags); 819edd16368SStephen M. Cameron 820edd16368SStephen M. Cameron /* find any devices in h->dev[] that are not in 821edd16368SStephen M. Cameron * sd[] and remove them from h->dev[], and for any 822edd16368SStephen M. Cameron * devices which have changed, remove the old device 823edd16368SStephen M. Cameron * info and add the new device info. 824edd16368SStephen M. Cameron */ 825edd16368SStephen M. Cameron i = 0; 826edd16368SStephen M. Cameron nremoved = 0; 827edd16368SStephen M. Cameron nadded = 0; 828edd16368SStephen M. Cameron while (i < h->ndevices) { 829edd16368SStephen M. Cameron csd = h->dev[i]; 830edd16368SStephen M. Cameron device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry); 831edd16368SStephen M. Cameron if (device_change == DEVICE_NOT_FOUND) { 832edd16368SStephen M. Cameron changes++; 833edd16368SStephen M. Cameron hpsa_scsi_remove_entry(h, hostno, i, 834edd16368SStephen M. Cameron removed, &nremoved); 835edd16368SStephen M. Cameron continue; /* remove ^^^, hence i not incremented */ 836edd16368SStephen M. Cameron } else if (device_change == DEVICE_CHANGED) { 837edd16368SStephen M. Cameron changes++; 8382a8ccf31SStephen M. Cameron hpsa_scsi_replace_entry(h, hostno, i, sd[entry], 8392a8ccf31SStephen M. Cameron added, &nadded, removed, &nremoved); 840c7f172dcSStephen M. Cameron /* Set it to NULL to prevent it from being freed 841c7f172dcSStephen M. Cameron * at the bottom of hpsa_update_scsi_devices() 842c7f172dcSStephen M. Cameron */ 843c7f172dcSStephen M. Cameron sd[entry] = NULL; 844edd16368SStephen M. Cameron } 845edd16368SStephen M. Cameron i++; 846edd16368SStephen M. Cameron } 847edd16368SStephen M. Cameron 848edd16368SStephen M. Cameron /* Now, make sure every device listed in sd[] is also 849edd16368SStephen M. Cameron * listed in h->dev[], adding them if they aren't found 850edd16368SStephen M. Cameron */ 851edd16368SStephen M. Cameron 852edd16368SStephen M. Cameron for (i = 0; i < nsds; i++) { 853edd16368SStephen M. Cameron if (!sd[i]) /* if already added above. */ 854edd16368SStephen M. Cameron continue; 855edd16368SStephen M. Cameron device_change = hpsa_scsi_find_entry(sd[i], h->dev, 856edd16368SStephen M. Cameron h->ndevices, &entry); 857edd16368SStephen M. Cameron if (device_change == DEVICE_NOT_FOUND) { 858edd16368SStephen M. Cameron changes++; 859edd16368SStephen M. Cameron if (hpsa_scsi_add_entry(h, hostno, sd[i], 860edd16368SStephen M. Cameron added, &nadded) != 0) 861edd16368SStephen M. Cameron break; 862edd16368SStephen M. Cameron sd[i] = NULL; /* prevent from being freed later. */ 863edd16368SStephen M. Cameron } else if (device_change == DEVICE_CHANGED) { 864edd16368SStephen M. Cameron /* should never happen... */ 865edd16368SStephen M. Cameron changes++; 866edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, 867edd16368SStephen M. Cameron "device unexpectedly changed.\n"); 868edd16368SStephen M. Cameron /* but if it does happen, we just ignore that device */ 869edd16368SStephen M. Cameron } 870edd16368SStephen M. Cameron } 871edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->devlock, flags); 872edd16368SStephen M. Cameron 873edd16368SStephen M. Cameron /* Don't notify scsi mid layer of any changes the first time through 874edd16368SStephen M. Cameron * (or if there are no changes) scsi_scan_host will do it later the 875edd16368SStephen M. Cameron * first time through. 876edd16368SStephen M. Cameron */ 877edd16368SStephen M. Cameron if (hostno == -1 || !changes) 878edd16368SStephen M. Cameron goto free_and_out; 879edd16368SStephen M. Cameron 880edd16368SStephen M. Cameron sh = h->scsi_host; 881edd16368SStephen M. Cameron /* Notify scsi mid layer of any removed devices */ 882edd16368SStephen M. Cameron for (i = 0; i < nremoved; i++) { 883edd16368SStephen M. Cameron struct scsi_device *sdev = 884edd16368SStephen M. Cameron scsi_device_lookup(sh, removed[i]->bus, 885edd16368SStephen M. Cameron removed[i]->target, removed[i]->lun); 886edd16368SStephen M. Cameron if (sdev != NULL) { 887edd16368SStephen M. Cameron scsi_remove_device(sdev); 888edd16368SStephen M. Cameron scsi_device_put(sdev); 889edd16368SStephen M. Cameron } else { 890edd16368SStephen M. Cameron /* We don't expect to get here. 891edd16368SStephen M. Cameron * future cmds to this device will get selection 892edd16368SStephen M. Cameron * timeout as if the device was gone. 893edd16368SStephen M. Cameron */ 894edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d " 895edd16368SStephen M. Cameron " for removal.", hostno, removed[i]->bus, 896edd16368SStephen M. Cameron removed[i]->target, removed[i]->lun); 897edd16368SStephen M. Cameron } 898edd16368SStephen M. Cameron kfree(removed[i]); 899edd16368SStephen M. Cameron removed[i] = NULL; 900edd16368SStephen M. Cameron } 901edd16368SStephen M. Cameron 902edd16368SStephen M. Cameron /* Notify scsi mid layer of any added devices */ 903edd16368SStephen M. Cameron for (i = 0; i < nadded; i++) { 904edd16368SStephen M. Cameron if (scsi_add_device(sh, added[i]->bus, 905edd16368SStephen M. Cameron added[i]->target, added[i]->lun) == 0) 906edd16368SStephen M. Cameron continue; 907edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, " 908edd16368SStephen M. Cameron "device not added.\n", hostno, added[i]->bus, 909edd16368SStephen M. Cameron added[i]->target, added[i]->lun); 910edd16368SStephen M. Cameron /* now we have to remove it from h->dev, 911edd16368SStephen M. Cameron * since it didn't get added to scsi mid layer 912edd16368SStephen M. Cameron */ 913edd16368SStephen M. Cameron fixup_botched_add(h, added[i]); 914edd16368SStephen M. Cameron } 915edd16368SStephen M. Cameron 916edd16368SStephen M. Cameron free_and_out: 917edd16368SStephen M. Cameron kfree(added); 918edd16368SStephen M. Cameron kfree(removed); 919edd16368SStephen M. Cameron } 920edd16368SStephen M. Cameron 921edd16368SStephen M. Cameron /* 922edd16368SStephen M. Cameron * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t * 923edd16368SStephen M. Cameron * Assume's h->devlock is held. 924edd16368SStephen M. Cameron */ 925edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h, 926edd16368SStephen M. Cameron int bus, int target, int lun) 927edd16368SStephen M. Cameron { 928edd16368SStephen M. Cameron int i; 929edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd; 930edd16368SStephen M. Cameron 931edd16368SStephen M. Cameron for (i = 0; i < h->ndevices; i++) { 932edd16368SStephen M. Cameron sd = h->dev[i]; 933edd16368SStephen M. Cameron if (sd->bus == bus && sd->target == target && sd->lun == lun) 934edd16368SStephen M. Cameron return sd; 935edd16368SStephen M. Cameron } 936edd16368SStephen M. Cameron return NULL; 937edd16368SStephen M. Cameron } 938edd16368SStephen M. Cameron 939edd16368SStephen M. Cameron /* link sdev->hostdata to our per-device structure. */ 940edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev) 941edd16368SStephen M. Cameron { 942edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *sd; 943edd16368SStephen M. Cameron unsigned long flags; 944edd16368SStephen M. Cameron struct ctlr_info *h; 945edd16368SStephen M. Cameron 946edd16368SStephen M. Cameron h = sdev_to_hba(sdev); 947edd16368SStephen M. Cameron spin_lock_irqsave(&h->devlock, flags); 948edd16368SStephen M. Cameron sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev), 949edd16368SStephen M. Cameron sdev_id(sdev), sdev->lun); 950edd16368SStephen M. Cameron if (sd != NULL) 951edd16368SStephen M. Cameron sdev->hostdata = sd; 952edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->devlock, flags); 953edd16368SStephen M. Cameron return 0; 954edd16368SStephen M. Cameron } 955edd16368SStephen M. Cameron 956edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev) 957edd16368SStephen M. Cameron { 958bcc44255SStephen M. Cameron /* nothing to do. */ 959edd16368SStephen M. Cameron } 960edd16368SStephen M. Cameron 96133a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h) 96233a2ffceSStephen M. Cameron { 96333a2ffceSStephen M. Cameron int i; 96433a2ffceSStephen M. Cameron 96533a2ffceSStephen M. Cameron if (!h->cmd_sg_list) 96633a2ffceSStephen M. Cameron return; 96733a2ffceSStephen M. Cameron for (i = 0; i < h->nr_cmds; i++) { 96833a2ffceSStephen M. Cameron kfree(h->cmd_sg_list[i]); 96933a2ffceSStephen M. Cameron h->cmd_sg_list[i] = NULL; 97033a2ffceSStephen M. Cameron } 97133a2ffceSStephen M. Cameron kfree(h->cmd_sg_list); 97233a2ffceSStephen M. Cameron h->cmd_sg_list = NULL; 97333a2ffceSStephen M. Cameron } 97433a2ffceSStephen M. Cameron 97533a2ffceSStephen M. Cameron static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h) 97633a2ffceSStephen M. Cameron { 97733a2ffceSStephen M. Cameron int i; 97833a2ffceSStephen M. Cameron 97933a2ffceSStephen M. Cameron if (h->chainsize <= 0) 98033a2ffceSStephen M. Cameron return 0; 98133a2ffceSStephen M. Cameron 98233a2ffceSStephen M. Cameron h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds, 98333a2ffceSStephen M. Cameron GFP_KERNEL); 98433a2ffceSStephen M. Cameron if (!h->cmd_sg_list) 98533a2ffceSStephen M. Cameron return -ENOMEM; 98633a2ffceSStephen M. Cameron for (i = 0; i < h->nr_cmds; i++) { 98733a2ffceSStephen M. Cameron h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) * 98833a2ffceSStephen M. Cameron h->chainsize, GFP_KERNEL); 98933a2ffceSStephen M. Cameron if (!h->cmd_sg_list[i]) 99033a2ffceSStephen M. Cameron goto clean; 99133a2ffceSStephen M. Cameron } 99233a2ffceSStephen M. Cameron return 0; 99333a2ffceSStephen M. Cameron 99433a2ffceSStephen M. Cameron clean: 99533a2ffceSStephen M. Cameron hpsa_free_sg_chain_blocks(h); 99633a2ffceSStephen M. Cameron return -ENOMEM; 99733a2ffceSStephen M. Cameron } 99833a2ffceSStephen M. Cameron 99933a2ffceSStephen M. Cameron static void hpsa_map_sg_chain_block(struct ctlr_info *h, 100033a2ffceSStephen M. Cameron struct CommandList *c) 100133a2ffceSStephen M. Cameron { 100233a2ffceSStephen M. Cameron struct SGDescriptor *chain_sg, *chain_block; 100333a2ffceSStephen M. Cameron u64 temp64; 100433a2ffceSStephen M. Cameron 100533a2ffceSStephen M. Cameron chain_sg = &c->SG[h->max_cmd_sg_entries - 1]; 100633a2ffceSStephen M. Cameron chain_block = h->cmd_sg_list[c->cmdindex]; 100733a2ffceSStephen M. Cameron chain_sg->Ext = HPSA_SG_CHAIN; 100833a2ffceSStephen M. Cameron chain_sg->Len = sizeof(*chain_sg) * 100933a2ffceSStephen M. Cameron (c->Header.SGTotal - h->max_cmd_sg_entries); 101033a2ffceSStephen M. Cameron temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len, 101133a2ffceSStephen M. Cameron PCI_DMA_TODEVICE); 101233a2ffceSStephen M. Cameron chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL); 101333a2ffceSStephen M. Cameron chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL); 101433a2ffceSStephen M. Cameron } 101533a2ffceSStephen M. Cameron 101633a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h, 101733a2ffceSStephen M. Cameron struct CommandList *c) 101833a2ffceSStephen M. Cameron { 101933a2ffceSStephen M. Cameron struct SGDescriptor *chain_sg; 102033a2ffceSStephen M. Cameron union u64bit temp64; 102133a2ffceSStephen M. Cameron 102233a2ffceSStephen M. Cameron if (c->Header.SGTotal <= h->max_cmd_sg_entries) 102333a2ffceSStephen M. Cameron return; 102433a2ffceSStephen M. Cameron 102533a2ffceSStephen M. Cameron chain_sg = &c->SG[h->max_cmd_sg_entries - 1]; 102633a2ffceSStephen M. Cameron temp64.val32.lower = chain_sg->Addr.lower; 102733a2ffceSStephen M. Cameron temp64.val32.upper = chain_sg->Addr.upper; 102833a2ffceSStephen M. Cameron pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE); 102933a2ffceSStephen M. Cameron } 103033a2ffceSStephen M. Cameron 10311fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp) 1032edd16368SStephen M. Cameron { 1033edd16368SStephen M. Cameron struct scsi_cmnd *cmd; 1034edd16368SStephen M. Cameron struct ctlr_info *h; 1035edd16368SStephen M. Cameron struct ErrorInfo *ei; 1036edd16368SStephen M. Cameron 1037edd16368SStephen M. Cameron unsigned char sense_key; 1038edd16368SStephen M. Cameron unsigned char asc; /* additional sense code */ 1039edd16368SStephen M. Cameron unsigned char ascq; /* additional sense code qualifier */ 1040db111e18SStephen M. Cameron unsigned long sense_data_size; 1041edd16368SStephen M. Cameron 1042edd16368SStephen M. Cameron ei = cp->err_info; 1043edd16368SStephen M. Cameron cmd = (struct scsi_cmnd *) cp->scsi_cmd; 1044edd16368SStephen M. Cameron h = cp->h; 1045edd16368SStephen M. Cameron 1046edd16368SStephen M. Cameron scsi_dma_unmap(cmd); /* undo the DMA mappings */ 104733a2ffceSStephen M. Cameron if (cp->Header.SGTotal > h->max_cmd_sg_entries) 104833a2ffceSStephen M. Cameron hpsa_unmap_sg_chain_block(h, cp); 1049edd16368SStephen M. Cameron 1050edd16368SStephen M. Cameron cmd->result = (DID_OK << 16); /* host byte */ 1051edd16368SStephen M. Cameron cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */ 10525512672fSStephen M. Cameron cmd->result |= ei->ScsiStatus; 1053edd16368SStephen M. Cameron 1054edd16368SStephen M. Cameron /* copy the sense data whether we need to or not. */ 1055db111e18SStephen M. Cameron if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo)) 1056db111e18SStephen M. Cameron sense_data_size = SCSI_SENSE_BUFFERSIZE; 1057db111e18SStephen M. Cameron else 1058db111e18SStephen M. Cameron sense_data_size = sizeof(ei->SenseInfo); 1059db111e18SStephen M. Cameron if (ei->SenseLen < sense_data_size) 1060db111e18SStephen M. Cameron sense_data_size = ei->SenseLen; 1061db111e18SStephen M. Cameron 1062db111e18SStephen M. Cameron memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size); 1063edd16368SStephen M. Cameron scsi_set_resid(cmd, ei->ResidualCnt); 1064edd16368SStephen M. Cameron 1065edd16368SStephen M. Cameron if (ei->CommandStatus == 0) { 1066edd16368SStephen M. Cameron cmd->scsi_done(cmd); 1067edd16368SStephen M. Cameron cmd_free(h, cp); 1068edd16368SStephen M. Cameron return; 1069edd16368SStephen M. Cameron } 1070edd16368SStephen M. Cameron 1071edd16368SStephen M. Cameron /* an error has occurred */ 1072edd16368SStephen M. Cameron switch (ei->CommandStatus) { 1073edd16368SStephen M. Cameron 1074edd16368SStephen M. Cameron case CMD_TARGET_STATUS: 1075edd16368SStephen M. Cameron if (ei->ScsiStatus) { 1076edd16368SStephen M. Cameron /* Get sense key */ 1077edd16368SStephen M. Cameron sense_key = 0xf & ei->SenseInfo[2]; 1078edd16368SStephen M. Cameron /* Get additional sense code */ 1079edd16368SStephen M. Cameron asc = ei->SenseInfo[12]; 1080edd16368SStephen M. Cameron /* Get addition sense code qualifier */ 1081edd16368SStephen M. Cameron ascq = ei->SenseInfo[13]; 1082edd16368SStephen M. Cameron } 1083edd16368SStephen M. Cameron 1084edd16368SStephen M. Cameron if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) { 1085edd16368SStephen M. Cameron if (check_for_unit_attention(h, cp)) { 1086edd16368SStephen M. Cameron cmd->result = DID_SOFT_ERROR << 16; 1087edd16368SStephen M. Cameron break; 1088edd16368SStephen M. Cameron } 1089edd16368SStephen M. Cameron if (sense_key == ILLEGAL_REQUEST) { 1090edd16368SStephen M. Cameron /* 1091edd16368SStephen M. Cameron * SCSI REPORT_LUNS is commonly unsupported on 1092edd16368SStephen M. Cameron * Smart Array. Suppress noisy complaint. 1093edd16368SStephen M. Cameron */ 1094edd16368SStephen M. Cameron if (cp->Request.CDB[0] == REPORT_LUNS) 1095edd16368SStephen M. Cameron break; 1096edd16368SStephen M. Cameron 1097edd16368SStephen M. Cameron /* If ASC/ASCQ indicate Logical Unit 1098edd16368SStephen M. Cameron * Not Supported condition, 1099edd16368SStephen M. Cameron */ 1100edd16368SStephen M. Cameron if ((asc == 0x25) && (ascq == 0x0)) { 1101edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p " 1102edd16368SStephen M. Cameron "has check condition\n", cp); 1103edd16368SStephen M. Cameron break; 1104edd16368SStephen M. Cameron } 1105edd16368SStephen M. Cameron } 1106edd16368SStephen M. Cameron 1107edd16368SStephen M. Cameron if (sense_key == NOT_READY) { 1108edd16368SStephen M. Cameron /* If Sense is Not Ready, Logical Unit 1109edd16368SStephen M. Cameron * Not ready, Manual Intervention 1110edd16368SStephen M. Cameron * required 1111edd16368SStephen M. Cameron */ 1112edd16368SStephen M. Cameron if ((asc == 0x04) && (ascq == 0x03)) { 1113edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p " 1114edd16368SStephen M. Cameron "has check condition: unit " 1115edd16368SStephen M. Cameron "not ready, manual " 1116edd16368SStephen M. Cameron "intervention required\n", cp); 1117edd16368SStephen M. Cameron break; 1118edd16368SStephen M. Cameron } 1119edd16368SStephen M. Cameron } 11201d3b3609SMatt Gates if (sense_key == ABORTED_COMMAND) { 11211d3b3609SMatt Gates /* Aborted command is retryable */ 11221d3b3609SMatt Gates dev_warn(&h->pdev->dev, "cp %p " 11231d3b3609SMatt Gates "has check condition: aborted command: " 11241d3b3609SMatt Gates "ASC: 0x%x, ASCQ: 0x%x\n", 11251d3b3609SMatt Gates cp, asc, ascq); 11261d3b3609SMatt Gates cmd->result = DID_SOFT_ERROR << 16; 11271d3b3609SMatt Gates break; 11281d3b3609SMatt Gates } 1129edd16368SStephen M. Cameron /* Must be some other type of check condition */ 1130edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p has check condition: " 1131edd16368SStephen M. Cameron "unknown type: " 1132edd16368SStephen M. Cameron "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, " 1133edd16368SStephen M. Cameron "Returning result: 0x%x, " 1134edd16368SStephen M. Cameron "cmd=[%02x %02x %02x %02x %02x " 1135807be732SMike Miller "%02x %02x %02x %02x %02x %02x " 1136edd16368SStephen M. Cameron "%02x %02x %02x %02x %02x]\n", 1137edd16368SStephen M. Cameron cp, sense_key, asc, ascq, 1138edd16368SStephen M. Cameron cmd->result, 1139edd16368SStephen M. Cameron cmd->cmnd[0], cmd->cmnd[1], 1140edd16368SStephen M. Cameron cmd->cmnd[2], cmd->cmnd[3], 1141edd16368SStephen M. Cameron cmd->cmnd[4], cmd->cmnd[5], 1142edd16368SStephen M. Cameron cmd->cmnd[6], cmd->cmnd[7], 1143807be732SMike Miller cmd->cmnd[8], cmd->cmnd[9], 1144807be732SMike Miller cmd->cmnd[10], cmd->cmnd[11], 1145807be732SMike Miller cmd->cmnd[12], cmd->cmnd[13], 1146807be732SMike Miller cmd->cmnd[14], cmd->cmnd[15]); 1147edd16368SStephen M. Cameron break; 1148edd16368SStephen M. Cameron } 1149edd16368SStephen M. Cameron 1150edd16368SStephen M. Cameron 1151edd16368SStephen M. Cameron /* Problem was not a check condition 1152edd16368SStephen M. Cameron * Pass it up to the upper layers... 1153edd16368SStephen M. Cameron */ 1154edd16368SStephen M. Cameron if (ei->ScsiStatus) { 1155edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p has status 0x%x " 1156edd16368SStephen M. Cameron "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, " 1157edd16368SStephen M. Cameron "Returning result: 0x%x\n", 1158edd16368SStephen M. Cameron cp, ei->ScsiStatus, 1159edd16368SStephen M. Cameron sense_key, asc, ascq, 1160edd16368SStephen M. Cameron cmd->result); 1161edd16368SStephen M. Cameron } else { /* scsi status is zero??? How??? */ 1162edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. " 1163edd16368SStephen M. Cameron "Returning no connection.\n", cp), 1164edd16368SStephen M. Cameron 1165edd16368SStephen M. Cameron /* Ordinarily, this case should never happen, 1166edd16368SStephen M. Cameron * but there is a bug in some released firmware 1167edd16368SStephen M. Cameron * revisions that allows it to happen if, for 1168edd16368SStephen M. Cameron * example, a 4100 backplane loses power and 1169edd16368SStephen M. Cameron * the tape drive is in it. We assume that 1170edd16368SStephen M. Cameron * it's a fatal error of some kind because we 1171edd16368SStephen M. Cameron * can't show that it wasn't. We will make it 1172edd16368SStephen M. Cameron * look like selection timeout since that is 1173edd16368SStephen M. Cameron * the most common reason for this to occur, 1174edd16368SStephen M. Cameron * and it's severe enough. 1175edd16368SStephen M. Cameron */ 1176edd16368SStephen M. Cameron 1177edd16368SStephen M. Cameron cmd->result = DID_NO_CONNECT << 16; 1178edd16368SStephen M. Cameron } 1179edd16368SStephen M. Cameron break; 1180edd16368SStephen M. Cameron 1181edd16368SStephen M. Cameron case CMD_DATA_UNDERRUN: /* let mid layer handle it. */ 1182edd16368SStephen M. Cameron break; 1183edd16368SStephen M. Cameron case CMD_DATA_OVERRUN: 1184edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p has" 1185edd16368SStephen M. Cameron " completed with data overrun " 1186edd16368SStephen M. Cameron "reported\n", cp); 1187edd16368SStephen M. Cameron break; 1188edd16368SStephen M. Cameron case CMD_INVALID: { 1189edd16368SStephen M. Cameron /* print_bytes(cp, sizeof(*cp), 1, 0); 1190edd16368SStephen M. Cameron print_cmd(cp); */ 1191edd16368SStephen M. Cameron /* We get CMD_INVALID if you address a non-existent device 1192edd16368SStephen M. Cameron * instead of a selection timeout (no response). You will 1193edd16368SStephen M. Cameron * see this if you yank out a drive, then try to access it. 1194edd16368SStephen M. Cameron * This is kind of a shame because it means that any other 1195edd16368SStephen M. Cameron * CMD_INVALID (e.g. driver bug) will get interpreted as a 1196edd16368SStephen M. Cameron * missing target. */ 1197edd16368SStephen M. Cameron cmd->result = DID_NO_CONNECT << 16; 1198edd16368SStephen M. Cameron } 1199edd16368SStephen M. Cameron break; 1200edd16368SStephen M. Cameron case CMD_PROTOCOL_ERR: 1201edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p has " 1202edd16368SStephen M. Cameron "protocol error \n", cp); 1203edd16368SStephen M. Cameron break; 1204edd16368SStephen M. Cameron case CMD_HARDWARE_ERR: 1205edd16368SStephen M. Cameron cmd->result = DID_ERROR << 16; 1206edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp); 1207edd16368SStephen M. Cameron break; 1208edd16368SStephen M. Cameron case CMD_CONNECTION_LOST: 1209edd16368SStephen M. Cameron cmd->result = DID_ERROR << 16; 1210edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp); 1211edd16368SStephen M. Cameron break; 1212edd16368SStephen M. Cameron case CMD_ABORTED: 1213edd16368SStephen M. Cameron cmd->result = DID_ABORT << 16; 1214edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n", 1215edd16368SStephen M. Cameron cp, ei->ScsiStatus); 1216edd16368SStephen M. Cameron break; 1217edd16368SStephen M. Cameron case CMD_ABORT_FAILED: 1218edd16368SStephen M. Cameron cmd->result = DID_ERROR << 16; 1219edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp); 1220edd16368SStephen M. Cameron break; 1221edd16368SStephen M. Cameron case CMD_UNSOLICITED_ABORT: 12225f0325abSMatt Gates cmd->result = DID_RESET << 16; 1223edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited " 1224edd16368SStephen M. Cameron "abort\n", cp); 1225edd16368SStephen M. Cameron break; 1226edd16368SStephen M. Cameron case CMD_TIMEOUT: 1227edd16368SStephen M. Cameron cmd->result = DID_TIME_OUT << 16; 1228edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p timedout\n", cp); 1229edd16368SStephen M. Cameron break; 12301d5e2ed0SStephen M. Cameron case CMD_UNABORTABLE: 12311d5e2ed0SStephen M. Cameron cmd->result = DID_ERROR << 16; 12321d5e2ed0SStephen M. Cameron dev_warn(&h->pdev->dev, "Command unabortable\n"); 12331d5e2ed0SStephen M. Cameron break; 1234edd16368SStephen M. Cameron default: 1235edd16368SStephen M. Cameron cmd->result = DID_ERROR << 16; 1236edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n", 1237edd16368SStephen M. Cameron cp, ei->CommandStatus); 1238edd16368SStephen M. Cameron } 1239edd16368SStephen M. Cameron cmd->scsi_done(cmd); 1240edd16368SStephen M. Cameron cmd_free(h, cp); 1241edd16368SStephen M. Cameron } 1242edd16368SStephen M. Cameron 1243edd16368SStephen M. Cameron static int hpsa_scsi_detect(struct ctlr_info *h) 1244edd16368SStephen M. Cameron { 1245edd16368SStephen M. Cameron struct Scsi_Host *sh; 1246edd16368SStephen M. Cameron int error; 1247edd16368SStephen M. Cameron 1248edd16368SStephen M. Cameron sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h)); 1249edd16368SStephen M. Cameron if (sh == NULL) 1250edd16368SStephen M. Cameron goto fail; 1251edd16368SStephen M. Cameron 1252edd16368SStephen M. Cameron sh->io_port = 0; 1253edd16368SStephen M. Cameron sh->n_io_port = 0; 1254edd16368SStephen M. Cameron sh->this_id = -1; 1255edd16368SStephen M. Cameron sh->max_channel = 3; 1256edd16368SStephen M. Cameron sh->max_cmd_len = MAX_COMMAND_SIZE; 1257edd16368SStephen M. Cameron sh->max_lun = HPSA_MAX_LUN; 1258edd16368SStephen M. Cameron sh->max_id = HPSA_MAX_LUN; 1259303932fdSDon Brace sh->can_queue = h->nr_cmds; 1260303932fdSDon Brace sh->cmd_per_lun = h->nr_cmds; 126133a2ffceSStephen M. Cameron sh->sg_tablesize = h->maxsgentries; 1262edd16368SStephen M. Cameron h->scsi_host = sh; 1263edd16368SStephen M. Cameron sh->hostdata[0] = (unsigned long) h; 1264a9a3a273SStephen M. Cameron sh->irq = h->intr[h->intr_mode]; 1265edd16368SStephen M. Cameron sh->unique_id = sh->irq; 1266edd16368SStephen M. Cameron error = scsi_add_host(sh, &h->pdev->dev); 1267edd16368SStephen M. Cameron if (error) 1268edd16368SStephen M. Cameron goto fail_host_put; 1269edd16368SStephen M. Cameron scsi_scan_host(sh); 1270edd16368SStephen M. Cameron return 0; 1271edd16368SStephen M. Cameron 1272edd16368SStephen M. Cameron fail_host_put: 1273edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host" 1274edd16368SStephen M. Cameron " failed for controller %d\n", h->ctlr); 1275edd16368SStephen M. Cameron scsi_host_put(sh); 1276ecd9aad4SStephen M. Cameron return error; 1277edd16368SStephen M. Cameron fail: 1278edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc" 1279edd16368SStephen M. Cameron " failed for controller %d\n", h->ctlr); 1280ecd9aad4SStephen M. Cameron return -ENOMEM; 1281edd16368SStephen M. Cameron } 1282edd16368SStephen M. Cameron 1283edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev, 1284edd16368SStephen M. Cameron struct CommandList *c, int sg_used, int data_direction) 1285edd16368SStephen M. Cameron { 1286edd16368SStephen M. Cameron int i; 1287edd16368SStephen M. Cameron union u64bit addr64; 1288edd16368SStephen M. Cameron 1289edd16368SStephen M. Cameron for (i = 0; i < sg_used; i++) { 1290edd16368SStephen M. Cameron addr64.val32.lower = c->SG[i].Addr.lower; 1291edd16368SStephen M. Cameron addr64.val32.upper = c->SG[i].Addr.upper; 1292edd16368SStephen M. Cameron pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len, 1293edd16368SStephen M. Cameron data_direction); 1294edd16368SStephen M. Cameron } 1295edd16368SStephen M. Cameron } 1296edd16368SStephen M. Cameron 1297edd16368SStephen M. Cameron static void hpsa_map_one(struct pci_dev *pdev, 1298edd16368SStephen M. Cameron struct CommandList *cp, 1299edd16368SStephen M. Cameron unsigned char *buf, 1300edd16368SStephen M. Cameron size_t buflen, 1301edd16368SStephen M. Cameron int data_direction) 1302edd16368SStephen M. Cameron { 130301a02ffcSStephen M. Cameron u64 addr64; 1304edd16368SStephen M. Cameron 1305edd16368SStephen M. Cameron if (buflen == 0 || data_direction == PCI_DMA_NONE) { 1306edd16368SStephen M. Cameron cp->Header.SGList = 0; 1307edd16368SStephen M. Cameron cp->Header.SGTotal = 0; 1308edd16368SStephen M. Cameron return; 1309edd16368SStephen M. Cameron } 1310edd16368SStephen M. Cameron 131101a02ffcSStephen M. Cameron addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction); 1312edd16368SStephen M. Cameron cp->SG[0].Addr.lower = 131301a02ffcSStephen M. Cameron (u32) (addr64 & (u64) 0x00000000FFFFFFFF); 1314edd16368SStephen M. Cameron cp->SG[0].Addr.upper = 131501a02ffcSStephen M. Cameron (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF); 1316edd16368SStephen M. Cameron cp->SG[0].Len = buflen; 131701a02ffcSStephen M. Cameron cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */ 131801a02ffcSStephen M. Cameron cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */ 1319edd16368SStephen M. Cameron } 1320edd16368SStephen M. Cameron 1321edd16368SStephen M. Cameron static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h, 1322edd16368SStephen M. Cameron struct CommandList *c) 1323edd16368SStephen M. Cameron { 1324edd16368SStephen M. Cameron DECLARE_COMPLETION_ONSTACK(wait); 1325edd16368SStephen M. Cameron 1326edd16368SStephen M. Cameron c->waiting = &wait; 1327edd16368SStephen M. Cameron enqueue_cmd_and_start_io(h, c); 1328edd16368SStephen M. Cameron wait_for_completion(&wait); 1329edd16368SStephen M. Cameron } 1330edd16368SStephen M. Cameron 1331edd16368SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h, 1332edd16368SStephen M. Cameron struct CommandList *c, int data_direction) 1333edd16368SStephen M. Cameron { 1334edd16368SStephen M. Cameron int retry_count = 0; 1335edd16368SStephen M. Cameron 1336edd16368SStephen M. Cameron do { 13377630abd0SJoe Perches memset(c->err_info, 0, sizeof(*c->err_info)); 1338edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 1339edd16368SStephen M. Cameron retry_count++; 1340edd16368SStephen M. Cameron } while (check_for_unit_attention(h, c) && retry_count <= 3); 1341edd16368SStephen M. Cameron hpsa_pci_unmap(h->pdev, c, 1, data_direction); 1342edd16368SStephen M. Cameron } 1343edd16368SStephen M. Cameron 1344edd16368SStephen M. Cameron static void hpsa_scsi_interpret_error(struct CommandList *cp) 1345edd16368SStephen M. Cameron { 1346edd16368SStephen M. Cameron struct ErrorInfo *ei; 1347edd16368SStephen M. Cameron struct device *d = &cp->h->pdev->dev; 1348edd16368SStephen M. Cameron 1349edd16368SStephen M. Cameron ei = cp->err_info; 1350edd16368SStephen M. Cameron switch (ei->CommandStatus) { 1351edd16368SStephen M. Cameron case CMD_TARGET_STATUS: 1352edd16368SStephen M. Cameron dev_warn(d, "cmd %p has completed with errors\n", cp); 1353edd16368SStephen M. Cameron dev_warn(d, "cmd %p has SCSI Status = %x\n", cp, 1354edd16368SStephen M. Cameron ei->ScsiStatus); 1355edd16368SStephen M. Cameron if (ei->ScsiStatus == 0) 1356edd16368SStephen M. Cameron dev_warn(d, "SCSI status is abnormally zero. " 1357edd16368SStephen M. Cameron "(probably indicates selection timeout " 1358edd16368SStephen M. Cameron "reported incorrectly due to a known " 1359edd16368SStephen M. Cameron "firmware bug, circa July, 2001.)\n"); 1360edd16368SStephen M. Cameron break; 1361edd16368SStephen M. Cameron case CMD_DATA_UNDERRUN: /* let mid layer handle it. */ 1362edd16368SStephen M. Cameron dev_info(d, "UNDERRUN\n"); 1363edd16368SStephen M. Cameron break; 1364edd16368SStephen M. Cameron case CMD_DATA_OVERRUN: 1365edd16368SStephen M. Cameron dev_warn(d, "cp %p has completed with data overrun\n", cp); 1366edd16368SStephen M. Cameron break; 1367edd16368SStephen M. Cameron case CMD_INVALID: { 1368edd16368SStephen M. Cameron /* controller unfortunately reports SCSI passthru's 1369edd16368SStephen M. Cameron * to non-existent targets as invalid commands. 1370edd16368SStephen M. Cameron */ 1371edd16368SStephen M. Cameron dev_warn(d, "cp %p is reported invalid (probably means " 1372edd16368SStephen M. Cameron "target device no longer present)\n", cp); 1373edd16368SStephen M. Cameron /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0); 1374edd16368SStephen M. Cameron print_cmd(cp); */ 1375edd16368SStephen M. Cameron } 1376edd16368SStephen M. Cameron break; 1377edd16368SStephen M. Cameron case CMD_PROTOCOL_ERR: 1378edd16368SStephen M. Cameron dev_warn(d, "cp %p has protocol error \n", cp); 1379edd16368SStephen M. Cameron break; 1380edd16368SStephen M. Cameron case CMD_HARDWARE_ERR: 1381edd16368SStephen M. Cameron /* cmd->result = DID_ERROR << 16; */ 1382edd16368SStephen M. Cameron dev_warn(d, "cp %p had hardware error\n", cp); 1383edd16368SStephen M. Cameron break; 1384edd16368SStephen M. Cameron case CMD_CONNECTION_LOST: 1385edd16368SStephen M. Cameron dev_warn(d, "cp %p had connection lost\n", cp); 1386edd16368SStephen M. Cameron break; 1387edd16368SStephen M. Cameron case CMD_ABORTED: 1388edd16368SStephen M. Cameron dev_warn(d, "cp %p was aborted\n", cp); 1389edd16368SStephen M. Cameron break; 1390edd16368SStephen M. Cameron case CMD_ABORT_FAILED: 1391edd16368SStephen M. Cameron dev_warn(d, "cp %p reports abort failed\n", cp); 1392edd16368SStephen M. Cameron break; 1393edd16368SStephen M. Cameron case CMD_UNSOLICITED_ABORT: 1394edd16368SStephen M. Cameron dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp); 1395edd16368SStephen M. Cameron break; 1396edd16368SStephen M. Cameron case CMD_TIMEOUT: 1397edd16368SStephen M. Cameron dev_warn(d, "cp %p timed out\n", cp); 1398edd16368SStephen M. Cameron break; 13991d5e2ed0SStephen M. Cameron case CMD_UNABORTABLE: 14001d5e2ed0SStephen M. Cameron dev_warn(d, "Command unabortable\n"); 14011d5e2ed0SStephen M. Cameron break; 1402edd16368SStephen M. Cameron default: 1403edd16368SStephen M. Cameron dev_warn(d, "cp %p returned unknown status %x\n", cp, 1404edd16368SStephen M. Cameron ei->CommandStatus); 1405edd16368SStephen M. Cameron } 1406edd16368SStephen M. Cameron } 1407edd16368SStephen M. Cameron 1408edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr, 1409edd16368SStephen M. Cameron unsigned char page, unsigned char *buf, 1410edd16368SStephen M. Cameron unsigned char bufsize) 1411edd16368SStephen M. Cameron { 1412edd16368SStephen M. Cameron int rc = IO_OK; 1413edd16368SStephen M. Cameron struct CommandList *c; 1414edd16368SStephen M. Cameron struct ErrorInfo *ei; 1415edd16368SStephen M. Cameron 1416edd16368SStephen M. Cameron c = cmd_special_alloc(h); 1417edd16368SStephen M. Cameron 1418edd16368SStephen M. Cameron if (c == NULL) { /* trouble... */ 1419edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); 1420ecd9aad4SStephen M. Cameron return -ENOMEM; 1421edd16368SStephen M. Cameron } 1422edd16368SStephen M. Cameron 1423edd16368SStephen M. Cameron fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD); 1424edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE); 1425edd16368SStephen M. Cameron ei = c->err_info; 1426edd16368SStephen M. Cameron if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) { 1427edd16368SStephen M. Cameron hpsa_scsi_interpret_error(c); 1428edd16368SStephen M. Cameron rc = -1; 1429edd16368SStephen M. Cameron } 1430edd16368SStephen M. Cameron cmd_special_free(h, c); 1431edd16368SStephen M. Cameron return rc; 1432edd16368SStephen M. Cameron } 1433edd16368SStephen M. Cameron 1434edd16368SStephen M. Cameron static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr) 1435edd16368SStephen M. Cameron { 1436edd16368SStephen M. Cameron int rc = IO_OK; 1437edd16368SStephen M. Cameron struct CommandList *c; 1438edd16368SStephen M. Cameron struct ErrorInfo *ei; 1439edd16368SStephen M. Cameron 1440edd16368SStephen M. Cameron c = cmd_special_alloc(h); 1441edd16368SStephen M. Cameron 1442edd16368SStephen M. Cameron if (c == NULL) { /* trouble... */ 1443edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); 1444e9ea04a6SStephen M. Cameron return -ENOMEM; 1445edd16368SStephen M. Cameron } 1446edd16368SStephen M. Cameron 1447edd16368SStephen M. Cameron fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG); 1448edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 1449edd16368SStephen M. Cameron /* no unmap needed here because no data xfer. */ 1450edd16368SStephen M. Cameron 1451edd16368SStephen M. Cameron ei = c->err_info; 1452edd16368SStephen M. Cameron if (ei->CommandStatus != 0) { 1453edd16368SStephen M. Cameron hpsa_scsi_interpret_error(c); 1454edd16368SStephen M. Cameron rc = -1; 1455edd16368SStephen M. Cameron } 1456edd16368SStephen M. Cameron cmd_special_free(h, c); 1457edd16368SStephen M. Cameron return rc; 1458edd16368SStephen M. Cameron } 1459edd16368SStephen M. Cameron 1460edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h, 1461edd16368SStephen M. Cameron unsigned char *scsi3addr, unsigned char *raid_level) 1462edd16368SStephen M. Cameron { 1463edd16368SStephen M. Cameron int rc; 1464edd16368SStephen M. Cameron unsigned char *buf; 1465edd16368SStephen M. Cameron 1466edd16368SStephen M. Cameron *raid_level = RAID_UNKNOWN; 1467edd16368SStephen M. Cameron buf = kzalloc(64, GFP_KERNEL); 1468edd16368SStephen M. Cameron if (!buf) 1469edd16368SStephen M. Cameron return; 1470edd16368SStephen M. Cameron rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64); 1471edd16368SStephen M. Cameron if (rc == 0) 1472edd16368SStephen M. Cameron *raid_level = buf[8]; 1473edd16368SStephen M. Cameron if (*raid_level > RAID_UNKNOWN) 1474edd16368SStephen M. Cameron *raid_level = RAID_UNKNOWN; 1475edd16368SStephen M. Cameron kfree(buf); 1476edd16368SStephen M. Cameron return; 1477edd16368SStephen M. Cameron } 1478edd16368SStephen M. Cameron 1479edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */ 1480edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr, 1481edd16368SStephen M. Cameron unsigned char *device_id, int buflen) 1482edd16368SStephen M. Cameron { 1483edd16368SStephen M. Cameron int rc; 1484edd16368SStephen M. Cameron unsigned char *buf; 1485edd16368SStephen M. Cameron 1486edd16368SStephen M. Cameron if (buflen > 16) 1487edd16368SStephen M. Cameron buflen = 16; 1488edd16368SStephen M. Cameron buf = kzalloc(64, GFP_KERNEL); 1489edd16368SStephen M. Cameron if (!buf) 1490edd16368SStephen M. Cameron return -1; 1491edd16368SStephen M. Cameron rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64); 1492edd16368SStephen M. Cameron if (rc == 0) 1493edd16368SStephen M. Cameron memcpy(device_id, &buf[8], buflen); 1494edd16368SStephen M. Cameron kfree(buf); 1495edd16368SStephen M. Cameron return rc != 0; 1496edd16368SStephen M. Cameron } 1497edd16368SStephen M. Cameron 1498edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical, 1499edd16368SStephen M. Cameron struct ReportLUNdata *buf, int bufsize, 1500edd16368SStephen M. Cameron int extended_response) 1501edd16368SStephen M. Cameron { 1502edd16368SStephen M. Cameron int rc = IO_OK; 1503edd16368SStephen M. Cameron struct CommandList *c; 1504edd16368SStephen M. Cameron unsigned char scsi3addr[8]; 1505edd16368SStephen M. Cameron struct ErrorInfo *ei; 1506edd16368SStephen M. Cameron 1507edd16368SStephen M. Cameron c = cmd_special_alloc(h); 1508edd16368SStephen M. Cameron if (c == NULL) { /* trouble... */ 1509edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); 1510edd16368SStephen M. Cameron return -1; 1511edd16368SStephen M. Cameron } 1512e89c0ae7SStephen M. Cameron /* address the controller */ 1513e89c0ae7SStephen M. Cameron memset(scsi3addr, 0, sizeof(scsi3addr)); 1514edd16368SStephen M. Cameron fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h, 1515edd16368SStephen M. Cameron buf, bufsize, 0, scsi3addr, TYPE_CMD); 1516edd16368SStephen M. Cameron if (extended_response) 1517edd16368SStephen M. Cameron c->Request.CDB[1] = extended_response; 1518edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE); 1519edd16368SStephen M. Cameron ei = c->err_info; 1520edd16368SStephen M. Cameron if (ei->CommandStatus != 0 && 1521edd16368SStephen M. Cameron ei->CommandStatus != CMD_DATA_UNDERRUN) { 1522edd16368SStephen M. Cameron hpsa_scsi_interpret_error(c); 1523edd16368SStephen M. Cameron rc = -1; 1524edd16368SStephen M. Cameron } 1525edd16368SStephen M. Cameron cmd_special_free(h, c); 1526edd16368SStephen M. Cameron return rc; 1527edd16368SStephen M. Cameron } 1528edd16368SStephen M. Cameron 1529edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h, 1530edd16368SStephen M. Cameron struct ReportLUNdata *buf, 1531edd16368SStephen M. Cameron int bufsize, int extended_response) 1532edd16368SStephen M. Cameron { 1533edd16368SStephen M. Cameron return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response); 1534edd16368SStephen M. Cameron } 1535edd16368SStephen M. Cameron 1536edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h, 1537edd16368SStephen M. Cameron struct ReportLUNdata *buf, int bufsize) 1538edd16368SStephen M. Cameron { 1539edd16368SStephen M. Cameron return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0); 1540edd16368SStephen M. Cameron } 1541edd16368SStephen M. Cameron 1542edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device, 1543edd16368SStephen M. Cameron int bus, int target, int lun) 1544edd16368SStephen M. Cameron { 1545edd16368SStephen M. Cameron device->bus = bus; 1546edd16368SStephen M. Cameron device->target = target; 1547edd16368SStephen M. Cameron device->lun = lun; 1548edd16368SStephen M. Cameron } 1549edd16368SStephen M. Cameron 1550edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h, 1551edd16368SStephen M. Cameron unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device) 1552edd16368SStephen M. Cameron { 1553edd16368SStephen M. Cameron #define OBDR_TAPE_INQ_SIZE 49 1554ea6d3bc3SStephen M. Cameron unsigned char *inq_buff; 1555edd16368SStephen M. Cameron 1556ea6d3bc3SStephen M. Cameron inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL); 1557edd16368SStephen M. Cameron if (!inq_buff) 1558edd16368SStephen M. Cameron goto bail_out; 1559edd16368SStephen M. Cameron 1560edd16368SStephen M. Cameron /* Do an inquiry to the device to see what it is. */ 1561edd16368SStephen M. Cameron if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff, 1562edd16368SStephen M. Cameron (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) { 1563edd16368SStephen M. Cameron /* Inquiry failed (msg printed already) */ 1564edd16368SStephen M. Cameron dev_err(&h->pdev->dev, 1565edd16368SStephen M. Cameron "hpsa_update_device_info: inquiry failed\n"); 1566edd16368SStephen M. Cameron goto bail_out; 1567edd16368SStephen M. Cameron } 1568edd16368SStephen M. Cameron 1569edd16368SStephen M. Cameron this_device->devtype = (inq_buff[0] & 0x1f); 1570edd16368SStephen M. Cameron memcpy(this_device->scsi3addr, scsi3addr, 8); 1571edd16368SStephen M. Cameron memcpy(this_device->vendor, &inq_buff[8], 1572edd16368SStephen M. Cameron sizeof(this_device->vendor)); 1573edd16368SStephen M. Cameron memcpy(this_device->model, &inq_buff[16], 1574edd16368SStephen M. Cameron sizeof(this_device->model)); 1575edd16368SStephen M. Cameron memset(this_device->device_id, 0, 1576edd16368SStephen M. Cameron sizeof(this_device->device_id)); 1577edd16368SStephen M. Cameron hpsa_get_device_id(h, scsi3addr, this_device->device_id, 1578edd16368SStephen M. Cameron sizeof(this_device->device_id)); 1579edd16368SStephen M. Cameron 1580edd16368SStephen M. Cameron if (this_device->devtype == TYPE_DISK && 1581edd16368SStephen M. Cameron is_logical_dev_addr_mode(scsi3addr)) 1582edd16368SStephen M. Cameron hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level); 1583edd16368SStephen M. Cameron else 1584edd16368SStephen M. Cameron this_device->raid_level = RAID_UNKNOWN; 1585edd16368SStephen M. Cameron 1586edd16368SStephen M. Cameron kfree(inq_buff); 1587edd16368SStephen M. Cameron return 0; 1588edd16368SStephen M. Cameron 1589edd16368SStephen M. Cameron bail_out: 1590edd16368SStephen M. Cameron kfree(inq_buff); 1591edd16368SStephen M. Cameron return 1; 1592edd16368SStephen M. Cameron } 1593edd16368SStephen M. Cameron 1594edd16368SStephen M. Cameron static unsigned char *msa2xxx_model[] = { 1595edd16368SStephen M. Cameron "MSA2012", 1596edd16368SStephen M. Cameron "MSA2024", 1597edd16368SStephen M. Cameron "MSA2312", 1598edd16368SStephen M. Cameron "MSA2324", 1599fda38518SStephen M. Cameron "P2000 G3 SAS", 1600edd16368SStephen M. Cameron NULL, 1601edd16368SStephen M. Cameron }; 1602edd16368SStephen M. Cameron 1603edd16368SStephen M. Cameron static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device) 1604edd16368SStephen M. Cameron { 1605edd16368SStephen M. Cameron int i; 1606edd16368SStephen M. Cameron 1607edd16368SStephen M. Cameron for (i = 0; msa2xxx_model[i]; i++) 1608edd16368SStephen M. Cameron if (strncmp(device->model, msa2xxx_model[i], 1609edd16368SStephen M. Cameron strlen(msa2xxx_model[i])) == 0) 1610edd16368SStephen M. Cameron return 1; 1611edd16368SStephen M. Cameron return 0; 1612edd16368SStephen M. Cameron } 1613edd16368SStephen M. Cameron 1614edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices. 1615edd16368SStephen M. Cameron * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical 1616edd16368SStephen M. Cameron * volumes on bus 1, physical devices on bus 2. and the hba on bus 3. 1617edd16368SStephen M. Cameron * Logical drive target and lun are assigned at this time, but 1618edd16368SStephen M. Cameron * physical device lun and target assignment are deferred (assigned 1619edd16368SStephen M. Cameron * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.) 1620edd16368SStephen M. Cameron */ 1621edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h, 162201a02ffcSStephen M. Cameron u8 *lunaddrbytes, int *bus, int *target, int *lun, 1623edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *device) 1624edd16368SStephen M. Cameron { 162501a02ffcSStephen M. Cameron u32 lunid; 1626edd16368SStephen M. Cameron 1627edd16368SStephen M. Cameron if (is_logical_dev_addr_mode(lunaddrbytes)) { 1628edd16368SStephen M. Cameron /* logical device */ 1629339b2b14SStephen M. Cameron if (unlikely(is_scsi_rev_5(h))) { 1630339b2b14SStephen M. Cameron /* p1210m, logical drives lun assignments 1631339b2b14SStephen M. Cameron * match SCSI REPORT LUNS data. 1632339b2b14SStephen M. Cameron */ 1633339b2b14SStephen M. Cameron lunid = le32_to_cpu(*((__le32 *) lunaddrbytes)); 1634339b2b14SStephen M. Cameron *bus = 0; 1635339b2b14SStephen M. Cameron *target = 0; 1636339b2b14SStephen M. Cameron *lun = (lunid & 0x3fff) + 1; 1637339b2b14SStephen M. Cameron } else { 1638339b2b14SStephen M. Cameron /* not p1210m... */ 16396df1e954SStephen M. Cameron lunid = le32_to_cpu(*((__le32 *) lunaddrbytes)); 1640edd16368SStephen M. Cameron if (is_msa2xxx(h, device)) { 1641339b2b14SStephen M. Cameron /* msa2xxx way, put logicals on bus 1 1642339b2b14SStephen M. Cameron * and match target/lun numbers box 1643339b2b14SStephen M. Cameron * reports. 1644339b2b14SStephen M. Cameron */ 1645edd16368SStephen M. Cameron *bus = 1; 1646edd16368SStephen M. Cameron *target = (lunid >> 16) & 0x3fff; 1647edd16368SStephen M. Cameron *lun = lunid & 0x00ff; 1648edd16368SStephen M. Cameron } else { 1649339b2b14SStephen M. Cameron /* Traditional smart array way. */ 1650edd16368SStephen M. Cameron *bus = 0; 1651edd16368SStephen M. Cameron *lun = 0; 1652edd16368SStephen M. Cameron *target = lunid & 0x3fff; 1653edd16368SStephen M. Cameron } 1654339b2b14SStephen M. Cameron } 1655edd16368SStephen M. Cameron } else { 1656edd16368SStephen M. Cameron /* physical device */ 1657edd16368SStephen M. Cameron if (is_hba_lunid(lunaddrbytes)) 1658339b2b14SStephen M. Cameron if (unlikely(is_scsi_rev_5(h))) { 1659339b2b14SStephen M. Cameron *bus = 0; /* put p1210m ctlr at 0,0,0 */ 1660339b2b14SStephen M. Cameron *target = 0; 1661339b2b14SStephen M. Cameron *lun = 0; 1662339b2b14SStephen M. Cameron return; 1663339b2b14SStephen M. Cameron } else 1664339b2b14SStephen M. Cameron *bus = 3; /* traditional smartarray */ 1665edd16368SStephen M. Cameron else 1666339b2b14SStephen M. Cameron *bus = 2; /* physical disk */ 1667edd16368SStephen M. Cameron *target = -1; 1668edd16368SStephen M. Cameron *lun = -1; /* we will fill these in later. */ 1669edd16368SStephen M. Cameron } 1670edd16368SStephen M. Cameron } 1671edd16368SStephen M. Cameron 1672edd16368SStephen M. Cameron /* 1673edd16368SStephen M. Cameron * If there is no lun 0 on a target, linux won't find any devices. 1674edd16368SStephen M. Cameron * For the MSA2xxx boxes, we have to manually detect the enclosure 1675edd16368SStephen M. Cameron * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report 1676edd16368SStephen M. Cameron * it for some reason. *tmpdevice is the target we're adding, 1677edd16368SStephen M. Cameron * this_device is a pointer into the current element of currentsd[] 1678edd16368SStephen M. Cameron * that we're building up in update_scsi_devices(), below. 1679edd16368SStephen M. Cameron * lunzerobits is a bitmap that tracks which targets already have a 1680edd16368SStephen M. Cameron * lun 0 assigned. 1681edd16368SStephen M. Cameron * Returns 1 if an enclosure was added, 0 if not. 1682edd16368SStephen M. Cameron */ 1683edd16368SStephen M. Cameron static int add_msa2xxx_enclosure_device(struct ctlr_info *h, 1684edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *tmpdevice, 168501a02ffcSStephen M. Cameron struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes, 1686edd16368SStephen M. Cameron int bus, int target, int lun, unsigned long lunzerobits[], 1687edd16368SStephen M. Cameron int *nmsa2xxx_enclosures) 1688edd16368SStephen M. Cameron { 1689edd16368SStephen M. Cameron unsigned char scsi3addr[8]; 1690edd16368SStephen M. Cameron 1691edd16368SStephen M. Cameron if (test_bit(target, lunzerobits)) 1692edd16368SStephen M. Cameron return 0; /* There is already a lun 0 on this target. */ 1693edd16368SStephen M. Cameron 1694edd16368SStephen M. Cameron if (!is_logical_dev_addr_mode(lunaddrbytes)) 1695edd16368SStephen M. Cameron return 0; /* It's the logical targets that may lack lun 0. */ 1696edd16368SStephen M. Cameron 1697edd16368SStephen M. Cameron if (!is_msa2xxx(h, tmpdevice)) 1698edd16368SStephen M. Cameron return 0; /* It's only the MSA2xxx that have this problem. */ 1699edd16368SStephen M. Cameron 1700edd16368SStephen M. Cameron if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */ 1701edd16368SStephen M. Cameron return 0; 1702edd16368SStephen M. Cameron 1703c4f8a299SStephen M. Cameron memset(scsi3addr, 0, 8); 1704c4f8a299SStephen M. Cameron scsi3addr[3] = target; 1705edd16368SStephen M. Cameron if (is_hba_lunid(scsi3addr)) 1706edd16368SStephen M. Cameron return 0; /* Don't add the RAID controller here. */ 1707edd16368SStephen M. Cameron 1708339b2b14SStephen M. Cameron if (is_scsi_rev_5(h)) 1709339b2b14SStephen M. Cameron return 0; /* p1210m doesn't need to do this. */ 1710339b2b14SStephen M. Cameron 1711edd16368SStephen M. Cameron #define MAX_MSA2XXX_ENCLOSURES 32 1712edd16368SStephen M. Cameron if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) { 1713edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX " 1714edd16368SStephen M. Cameron "enclosures exceeded. Check your hardware " 1715edd16368SStephen M. Cameron "configuration."); 1716edd16368SStephen M. Cameron return 0; 1717edd16368SStephen M. Cameron } 1718edd16368SStephen M. Cameron 1719edd16368SStephen M. Cameron if (hpsa_update_device_info(h, scsi3addr, this_device)) 1720edd16368SStephen M. Cameron return 0; 1721edd16368SStephen M. Cameron (*nmsa2xxx_enclosures)++; 1722edd16368SStephen M. Cameron hpsa_set_bus_target_lun(this_device, bus, target, 0); 1723edd16368SStephen M. Cameron set_bit(target, lunzerobits); 1724edd16368SStephen M. Cameron return 1; 1725edd16368SStephen M. Cameron } 1726edd16368SStephen M. Cameron 1727edd16368SStephen M. Cameron /* 1728edd16368SStephen M. Cameron * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev, 1729edd16368SStephen M. Cameron * logdev. The number of luns in physdev and logdev are returned in 1730edd16368SStephen M. Cameron * *nphysicals and *nlogicals, respectively. 1731edd16368SStephen M. Cameron * Returns 0 on success, -1 otherwise. 1732edd16368SStephen M. Cameron */ 1733edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h, 1734edd16368SStephen M. Cameron int reportlunsize, 173501a02ffcSStephen M. Cameron struct ReportLUNdata *physdev, u32 *nphysicals, 173601a02ffcSStephen M. Cameron struct ReportLUNdata *logdev, u32 *nlogicals) 1737edd16368SStephen M. Cameron { 1738edd16368SStephen M. Cameron if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) { 1739edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "report physical LUNs failed.\n"); 1740edd16368SStephen M. Cameron return -1; 1741edd16368SStephen M. Cameron } 17426df1e954SStephen M. Cameron *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8; 1743edd16368SStephen M. Cameron if (*nphysicals > HPSA_MAX_PHYS_LUN) { 1744edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded." 1745edd16368SStephen M. Cameron " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN, 1746edd16368SStephen M. Cameron *nphysicals - HPSA_MAX_PHYS_LUN); 1747edd16368SStephen M. Cameron *nphysicals = HPSA_MAX_PHYS_LUN; 1748edd16368SStephen M. Cameron } 1749edd16368SStephen M. Cameron if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) { 1750edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "report logical LUNs failed.\n"); 1751edd16368SStephen M. Cameron return -1; 1752edd16368SStephen M. Cameron } 17536df1e954SStephen M. Cameron *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8; 1754edd16368SStephen M. Cameron /* Reject Logicals in excess of our max capability. */ 1755edd16368SStephen M. Cameron if (*nlogicals > HPSA_MAX_LUN) { 1756edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, 1757edd16368SStephen M. Cameron "maximum logical LUNs (%d) exceeded. " 1758edd16368SStephen M. Cameron "%d LUNs ignored.\n", HPSA_MAX_LUN, 1759edd16368SStephen M. Cameron *nlogicals - HPSA_MAX_LUN); 1760edd16368SStephen M. Cameron *nlogicals = HPSA_MAX_LUN; 1761edd16368SStephen M. Cameron } 1762edd16368SStephen M. Cameron if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) { 1763edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, 1764edd16368SStephen M. Cameron "maximum logical + physical LUNs (%d) exceeded. " 1765edd16368SStephen M. Cameron "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN, 1766edd16368SStephen M. Cameron *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN); 1767edd16368SStephen M. Cameron *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals; 1768edd16368SStephen M. Cameron } 1769edd16368SStephen M. Cameron return 0; 1770edd16368SStephen M. Cameron } 1771edd16368SStephen M. Cameron 1772339b2b14SStephen M. Cameron u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i, 1773339b2b14SStephen M. Cameron int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list, 1774339b2b14SStephen M. Cameron struct ReportLUNdata *logdev_list) 1775339b2b14SStephen M. Cameron { 1776339b2b14SStephen M. Cameron /* Helper function, figure out where the LUN ID info is coming from 1777339b2b14SStephen M. Cameron * given index i, lists of physical and logical devices, where in 1778339b2b14SStephen M. Cameron * the list the raid controller is supposed to appear (first or last) 1779339b2b14SStephen M. Cameron */ 1780339b2b14SStephen M. Cameron 1781339b2b14SStephen M. Cameron int logicals_start = nphysicals + (raid_ctlr_position == 0); 1782339b2b14SStephen M. Cameron int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0); 1783339b2b14SStephen M. Cameron 1784339b2b14SStephen M. Cameron if (i == raid_ctlr_position) 1785339b2b14SStephen M. Cameron return RAID_CTLR_LUNID; 1786339b2b14SStephen M. Cameron 1787339b2b14SStephen M. Cameron if (i < logicals_start) 1788339b2b14SStephen M. Cameron return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0]; 1789339b2b14SStephen M. Cameron 1790339b2b14SStephen M. Cameron if (i < last_device) 1791339b2b14SStephen M. Cameron return &logdev_list->LUN[i - nphysicals - 1792339b2b14SStephen M. Cameron (raid_ctlr_position == 0)][0]; 1793339b2b14SStephen M. Cameron BUG(); 1794339b2b14SStephen M. Cameron return NULL; 1795339b2b14SStephen M. Cameron } 1796339b2b14SStephen M. Cameron 1797edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno) 1798edd16368SStephen M. Cameron { 1799edd16368SStephen M. Cameron /* the idea here is we could get notified 1800edd16368SStephen M. Cameron * that some devices have changed, so we do a report 1801edd16368SStephen M. Cameron * physical luns and report logical luns cmd, and adjust 1802edd16368SStephen M. Cameron * our list of devices accordingly. 1803edd16368SStephen M. Cameron * 1804edd16368SStephen M. Cameron * The scsi3addr's of devices won't change so long as the 1805edd16368SStephen M. Cameron * adapter is not reset. That means we can rescan and 1806edd16368SStephen M. Cameron * tell which devices we already know about, vs. new 1807edd16368SStephen M. Cameron * devices, vs. disappearing devices. 1808edd16368SStephen M. Cameron */ 1809edd16368SStephen M. Cameron struct ReportLUNdata *physdev_list = NULL; 1810edd16368SStephen M. Cameron struct ReportLUNdata *logdev_list = NULL; 1811edd16368SStephen M. Cameron unsigned char *inq_buff = NULL; 181201a02ffcSStephen M. Cameron u32 nphysicals = 0; 181301a02ffcSStephen M. Cameron u32 nlogicals = 0; 181401a02ffcSStephen M. Cameron u32 ndev_allocated = 0; 1815edd16368SStephen M. Cameron struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice; 1816edd16368SStephen M. Cameron int ncurrent = 0; 1817edd16368SStephen M. Cameron int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8; 1818edd16368SStephen M. Cameron int i, nmsa2xxx_enclosures, ndevs_to_allocate; 1819edd16368SStephen M. Cameron int bus, target, lun; 1820339b2b14SStephen M. Cameron int raid_ctlr_position; 1821edd16368SStephen M. Cameron DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR); 1822edd16368SStephen M. Cameron 1823edd16368SStephen M. Cameron currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA, 1824edd16368SStephen M. Cameron GFP_KERNEL); 1825edd16368SStephen M. Cameron physdev_list = kzalloc(reportlunsize, GFP_KERNEL); 1826edd16368SStephen M. Cameron logdev_list = kzalloc(reportlunsize, GFP_KERNEL); 1827edd16368SStephen M. Cameron inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL); 1828edd16368SStephen M. Cameron tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL); 1829edd16368SStephen M. Cameron 1830edd16368SStephen M. Cameron if (!currentsd || !physdev_list || !logdev_list || 1831edd16368SStephen M. Cameron !inq_buff || !tmpdevice) { 1832edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "out of memory\n"); 1833edd16368SStephen M. Cameron goto out; 1834edd16368SStephen M. Cameron } 1835edd16368SStephen M. Cameron memset(lunzerobits, 0, sizeof(lunzerobits)); 1836edd16368SStephen M. Cameron 1837edd16368SStephen M. Cameron if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals, 1838edd16368SStephen M. Cameron logdev_list, &nlogicals)) 1839edd16368SStephen M. Cameron goto out; 1840edd16368SStephen M. Cameron 1841edd16368SStephen M. Cameron /* We might see up to 32 MSA2xxx enclosures, actually 8 of them 1842edd16368SStephen M. Cameron * but each of them 4 times through different paths. The plus 1 1843edd16368SStephen M. Cameron * is for the RAID controller. 1844edd16368SStephen M. Cameron */ 1845edd16368SStephen M. Cameron ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1; 1846edd16368SStephen M. Cameron 1847edd16368SStephen M. Cameron /* Allocate the per device structures */ 1848edd16368SStephen M. Cameron for (i = 0; i < ndevs_to_allocate; i++) { 1849edd16368SStephen M. Cameron currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL); 1850edd16368SStephen M. Cameron if (!currentsd[i]) { 1851edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "out of memory at %s:%d\n", 1852edd16368SStephen M. Cameron __FILE__, __LINE__); 1853edd16368SStephen M. Cameron goto out; 1854edd16368SStephen M. Cameron } 1855edd16368SStephen M. Cameron ndev_allocated++; 1856edd16368SStephen M. Cameron } 1857edd16368SStephen M. Cameron 1858339b2b14SStephen M. Cameron if (unlikely(is_scsi_rev_5(h))) 1859339b2b14SStephen M. Cameron raid_ctlr_position = 0; 1860339b2b14SStephen M. Cameron else 1861339b2b14SStephen M. Cameron raid_ctlr_position = nphysicals + nlogicals; 1862339b2b14SStephen M. Cameron 1863edd16368SStephen M. Cameron /* adjust our table of devices */ 1864edd16368SStephen M. Cameron nmsa2xxx_enclosures = 0; 1865edd16368SStephen M. Cameron for (i = 0; i < nphysicals + nlogicals + 1; i++) { 186601a02ffcSStephen M. Cameron u8 *lunaddrbytes; 1867edd16368SStephen M. Cameron 1868edd16368SStephen M. Cameron /* Figure out where the LUN ID info is coming from */ 1869339b2b14SStephen M. Cameron lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position, 1870339b2b14SStephen M. Cameron i, nphysicals, nlogicals, physdev_list, logdev_list); 1871edd16368SStephen M. Cameron /* skip masked physical devices. */ 1872339b2b14SStephen M. Cameron if (lunaddrbytes[3] & 0xC0 && 1873339b2b14SStephen M. Cameron i < nphysicals + (raid_ctlr_position == 0)) 1874edd16368SStephen M. Cameron continue; 1875edd16368SStephen M. Cameron 1876edd16368SStephen M. Cameron /* Get device type, vendor, model, device id */ 1877edd16368SStephen M. Cameron if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice)) 1878edd16368SStephen M. Cameron continue; /* skip it if we can't talk to it. */ 1879edd16368SStephen M. Cameron figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun, 1880edd16368SStephen M. Cameron tmpdevice); 1881edd16368SStephen M. Cameron this_device = currentsd[ncurrent]; 1882edd16368SStephen M. Cameron 1883edd16368SStephen M. Cameron /* 1884edd16368SStephen M. Cameron * For the msa2xxx boxes, we have to insert a LUN 0 which 1885edd16368SStephen M. Cameron * doesn't show up in CCISS_REPORT_PHYSICAL data, but there 1886edd16368SStephen M. Cameron * is nonetheless an enclosure device there. We have to 1887edd16368SStephen M. Cameron * present that otherwise linux won't find anything if 1888edd16368SStephen M. Cameron * there is no lun 0. 1889edd16368SStephen M. Cameron */ 1890edd16368SStephen M. Cameron if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device, 1891edd16368SStephen M. Cameron lunaddrbytes, bus, target, lun, lunzerobits, 1892edd16368SStephen M. Cameron &nmsa2xxx_enclosures)) { 1893edd16368SStephen M. Cameron ncurrent++; 1894edd16368SStephen M. Cameron this_device = currentsd[ncurrent]; 1895edd16368SStephen M. Cameron } 1896edd16368SStephen M. Cameron 1897edd16368SStephen M. Cameron *this_device = *tmpdevice; 1898edd16368SStephen M. Cameron hpsa_set_bus_target_lun(this_device, bus, target, lun); 1899edd16368SStephen M. Cameron 1900edd16368SStephen M. Cameron switch (this_device->devtype) { 1901edd16368SStephen M. Cameron case TYPE_ROM: { 1902edd16368SStephen M. Cameron /* We don't *really* support actual CD-ROM devices, 1903edd16368SStephen M. Cameron * just "One Button Disaster Recovery" tape drive 1904edd16368SStephen M. Cameron * which temporarily pretends to be a CD-ROM drive. 1905edd16368SStephen M. Cameron * So we check that the device is really an OBDR tape 1906edd16368SStephen M. Cameron * device by checking for "$DR-10" in bytes 43-48 of 1907edd16368SStephen M. Cameron * the inquiry data. 1908edd16368SStephen M. Cameron */ 1909edd16368SStephen M. Cameron char obdr_sig[7]; 1910edd16368SStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10" 1911edd16368SStephen M. Cameron strncpy(obdr_sig, &inq_buff[43], 6); 1912edd16368SStephen M. Cameron obdr_sig[6] = '\0'; 1913edd16368SStephen M. Cameron if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0) 1914edd16368SStephen M. Cameron /* Not OBDR device, ignore it. */ 1915edd16368SStephen M. Cameron break; 1916edd16368SStephen M. Cameron } 1917edd16368SStephen M. Cameron ncurrent++; 1918edd16368SStephen M. Cameron break; 1919edd16368SStephen M. Cameron case TYPE_DISK: 1920edd16368SStephen M. Cameron if (i < nphysicals) 1921edd16368SStephen M. Cameron break; 1922edd16368SStephen M. Cameron ncurrent++; 1923edd16368SStephen M. Cameron break; 1924edd16368SStephen M. Cameron case TYPE_TAPE: 1925edd16368SStephen M. Cameron case TYPE_MEDIUM_CHANGER: 1926edd16368SStephen M. Cameron ncurrent++; 1927edd16368SStephen M. Cameron break; 1928edd16368SStephen M. Cameron case TYPE_RAID: 1929edd16368SStephen M. Cameron /* Only present the Smartarray HBA as a RAID controller. 1930edd16368SStephen M. Cameron * If it's a RAID controller other than the HBA itself 1931edd16368SStephen M. Cameron * (an external RAID controller, MSA500 or similar) 1932edd16368SStephen M. Cameron * don't present it. 1933edd16368SStephen M. Cameron */ 1934edd16368SStephen M. Cameron if (!is_hba_lunid(lunaddrbytes)) 1935edd16368SStephen M. Cameron break; 1936edd16368SStephen M. Cameron ncurrent++; 1937edd16368SStephen M. Cameron break; 1938edd16368SStephen M. Cameron default: 1939edd16368SStephen M. Cameron break; 1940edd16368SStephen M. Cameron } 1941edd16368SStephen M. Cameron if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA) 1942edd16368SStephen M. Cameron break; 1943edd16368SStephen M. Cameron } 1944edd16368SStephen M. Cameron adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent); 1945edd16368SStephen M. Cameron out: 1946edd16368SStephen M. Cameron kfree(tmpdevice); 1947edd16368SStephen M. Cameron for (i = 0; i < ndev_allocated; i++) 1948edd16368SStephen M. Cameron kfree(currentsd[i]); 1949edd16368SStephen M. Cameron kfree(currentsd); 1950edd16368SStephen M. Cameron kfree(inq_buff); 1951edd16368SStephen M. Cameron kfree(physdev_list); 1952edd16368SStephen M. Cameron kfree(logdev_list); 1953edd16368SStephen M. Cameron } 1954edd16368SStephen M. Cameron 1955edd16368SStephen M. Cameron /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci 1956edd16368SStephen M. Cameron * dma mapping and fills in the scatter gather entries of the 1957edd16368SStephen M. Cameron * hpsa command, cp. 1958edd16368SStephen M. Cameron */ 195933a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h, 1960edd16368SStephen M. Cameron struct CommandList *cp, 1961edd16368SStephen M. Cameron struct scsi_cmnd *cmd) 1962edd16368SStephen M. Cameron { 1963edd16368SStephen M. Cameron unsigned int len; 1964edd16368SStephen M. Cameron struct scatterlist *sg; 196501a02ffcSStephen M. Cameron u64 addr64; 196633a2ffceSStephen M. Cameron int use_sg, i, sg_index, chained; 196733a2ffceSStephen M. Cameron struct SGDescriptor *curr_sg; 1968edd16368SStephen M. Cameron 196933a2ffceSStephen M. Cameron BUG_ON(scsi_sg_count(cmd) > h->maxsgentries); 1970edd16368SStephen M. Cameron 1971edd16368SStephen M. Cameron use_sg = scsi_dma_map(cmd); 1972edd16368SStephen M. Cameron if (use_sg < 0) 1973edd16368SStephen M. Cameron return use_sg; 1974edd16368SStephen M. Cameron 1975edd16368SStephen M. Cameron if (!use_sg) 1976edd16368SStephen M. Cameron goto sglist_finished; 1977edd16368SStephen M. Cameron 197833a2ffceSStephen M. Cameron curr_sg = cp->SG; 197933a2ffceSStephen M. Cameron chained = 0; 198033a2ffceSStephen M. Cameron sg_index = 0; 1981edd16368SStephen M. Cameron scsi_for_each_sg(cmd, sg, use_sg, i) { 198233a2ffceSStephen M. Cameron if (i == h->max_cmd_sg_entries - 1 && 198333a2ffceSStephen M. Cameron use_sg > h->max_cmd_sg_entries) { 198433a2ffceSStephen M. Cameron chained = 1; 198533a2ffceSStephen M. Cameron curr_sg = h->cmd_sg_list[cp->cmdindex]; 198633a2ffceSStephen M. Cameron sg_index = 0; 198733a2ffceSStephen M. Cameron } 198801a02ffcSStephen M. Cameron addr64 = (u64) sg_dma_address(sg); 1989edd16368SStephen M. Cameron len = sg_dma_len(sg); 199033a2ffceSStephen M. Cameron curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL); 199133a2ffceSStephen M. Cameron curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL); 199233a2ffceSStephen M. Cameron curr_sg->Len = len; 199333a2ffceSStephen M. Cameron curr_sg->Ext = 0; /* we are not chaining */ 199433a2ffceSStephen M. Cameron curr_sg++; 199533a2ffceSStephen M. Cameron } 199633a2ffceSStephen M. Cameron 199733a2ffceSStephen M. Cameron if (use_sg + chained > h->maxSG) 199833a2ffceSStephen M. Cameron h->maxSG = use_sg + chained; 199933a2ffceSStephen M. Cameron 200033a2ffceSStephen M. Cameron if (chained) { 200133a2ffceSStephen M. Cameron cp->Header.SGList = h->max_cmd_sg_entries; 200233a2ffceSStephen M. Cameron cp->Header.SGTotal = (u16) (use_sg + 1); 200333a2ffceSStephen M. Cameron hpsa_map_sg_chain_block(h, cp); 200433a2ffceSStephen M. Cameron return 0; 2005edd16368SStephen M. Cameron } 2006edd16368SStephen M. Cameron 2007edd16368SStephen M. Cameron sglist_finished: 2008edd16368SStephen M. Cameron 200901a02ffcSStephen M. Cameron cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */ 201001a02ffcSStephen M. Cameron cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */ 2011edd16368SStephen M. Cameron return 0; 2012edd16368SStephen M. Cameron } 2013edd16368SStephen M. Cameron 2014edd16368SStephen M. Cameron 2015f281233dSJeff Garzik static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd, 2016edd16368SStephen M. Cameron void (*done)(struct scsi_cmnd *)) 2017edd16368SStephen M. Cameron { 2018edd16368SStephen M. Cameron struct ctlr_info *h; 2019edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *dev; 2020edd16368SStephen M. Cameron unsigned char scsi3addr[8]; 2021edd16368SStephen M. Cameron struct CommandList *c; 2022edd16368SStephen M. Cameron unsigned long flags; 2023edd16368SStephen M. Cameron 2024edd16368SStephen M. Cameron /* Get the ptr to our adapter structure out of cmd->host. */ 2025edd16368SStephen M. Cameron h = sdev_to_hba(cmd->device); 2026edd16368SStephen M. Cameron dev = cmd->device->hostdata; 2027edd16368SStephen M. Cameron if (!dev) { 2028edd16368SStephen M. Cameron cmd->result = DID_NO_CONNECT << 16; 2029edd16368SStephen M. Cameron done(cmd); 2030edd16368SStephen M. Cameron return 0; 2031edd16368SStephen M. Cameron } 2032edd16368SStephen M. Cameron memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr)); 2033edd16368SStephen M. Cameron 2034edd16368SStephen M. Cameron /* Need a lock as this is being allocated from the pool */ 2035edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 2036edd16368SStephen M. Cameron c = cmd_alloc(h); 2037edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 2038edd16368SStephen M. Cameron if (c == NULL) { /* trouble... */ 2039edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n"); 2040edd16368SStephen M. Cameron return SCSI_MLQUEUE_HOST_BUSY; 2041edd16368SStephen M. Cameron } 2042edd16368SStephen M. Cameron 2043edd16368SStephen M. Cameron /* Fill in the command list header */ 2044edd16368SStephen M. Cameron 2045edd16368SStephen M. Cameron cmd->scsi_done = done; /* save this for use by completion code */ 2046edd16368SStephen M. Cameron 2047edd16368SStephen M. Cameron /* save c in case we have to abort it */ 2048edd16368SStephen M. Cameron cmd->host_scribble = (unsigned char *) c; 2049edd16368SStephen M. Cameron 2050edd16368SStephen M. Cameron c->cmd_type = CMD_SCSI; 2051edd16368SStephen M. Cameron c->scsi_cmd = cmd; 2052edd16368SStephen M. Cameron c->Header.ReplyQueue = 0; /* unused in simple mode */ 2053edd16368SStephen M. Cameron memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8); 2054303932fdSDon Brace c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT); 2055303932fdSDon Brace c->Header.Tag.lower |= DIRECT_LOOKUP_BIT; 2056edd16368SStephen M. Cameron 2057edd16368SStephen M. Cameron /* Fill in the request block... */ 2058edd16368SStephen M. Cameron 2059edd16368SStephen M. Cameron c->Request.Timeout = 0; 2060edd16368SStephen M. Cameron memset(c->Request.CDB, 0, sizeof(c->Request.CDB)); 2061edd16368SStephen M. Cameron BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB)); 2062edd16368SStephen M. Cameron c->Request.CDBLen = cmd->cmd_len; 2063edd16368SStephen M. Cameron memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len); 2064edd16368SStephen M. Cameron c->Request.Type.Type = TYPE_CMD; 2065edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2066edd16368SStephen M. Cameron switch (cmd->sc_data_direction) { 2067edd16368SStephen M. Cameron case DMA_TO_DEVICE: 2068edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_WRITE; 2069edd16368SStephen M. Cameron break; 2070edd16368SStephen M. Cameron case DMA_FROM_DEVICE: 2071edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_READ; 2072edd16368SStephen M. Cameron break; 2073edd16368SStephen M. Cameron case DMA_NONE: 2074edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_NONE; 2075edd16368SStephen M. Cameron break; 2076edd16368SStephen M. Cameron case DMA_BIDIRECTIONAL: 2077edd16368SStephen M. Cameron /* This can happen if a buggy application does a scsi passthru 2078edd16368SStephen M. Cameron * and sets both inlen and outlen to non-zero. ( see 2079edd16368SStephen M. Cameron * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() ) 2080edd16368SStephen M. Cameron */ 2081edd16368SStephen M. Cameron 2082edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_RSVD; 2083edd16368SStephen M. Cameron /* This is technically wrong, and hpsa controllers should 2084edd16368SStephen M. Cameron * reject it with CMD_INVALID, which is the most correct 2085edd16368SStephen M. Cameron * response, but non-fibre backends appear to let it 2086edd16368SStephen M. Cameron * slide by, and give the same results as if this field 2087edd16368SStephen M. Cameron * were set correctly. Either way is acceptable for 2088edd16368SStephen M. Cameron * our purposes here. 2089edd16368SStephen M. Cameron */ 2090edd16368SStephen M. Cameron 2091edd16368SStephen M. Cameron break; 2092edd16368SStephen M. Cameron 2093edd16368SStephen M. Cameron default: 2094edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "unknown data direction: %d\n", 2095edd16368SStephen M. Cameron cmd->sc_data_direction); 2096edd16368SStephen M. Cameron BUG(); 2097edd16368SStephen M. Cameron break; 2098edd16368SStephen M. Cameron } 2099edd16368SStephen M. Cameron 210033a2ffceSStephen M. Cameron if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */ 2101edd16368SStephen M. Cameron cmd_free(h, c); 2102edd16368SStephen M. Cameron return SCSI_MLQUEUE_HOST_BUSY; 2103edd16368SStephen M. Cameron } 2104edd16368SStephen M. Cameron enqueue_cmd_and_start_io(h, c); 2105edd16368SStephen M. Cameron /* the cmd'll come back via intr handler in complete_scsi_command() */ 2106edd16368SStephen M. Cameron return 0; 2107edd16368SStephen M. Cameron } 2108edd16368SStephen M. Cameron 2109f281233dSJeff Garzik static DEF_SCSI_QCMD(hpsa_scsi_queue_command) 2110f281233dSJeff Garzik 2111a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh) 2112a08a8471SStephen M. Cameron { 2113a08a8471SStephen M. Cameron struct ctlr_info *h = shost_to_hba(sh); 2114a08a8471SStephen M. Cameron unsigned long flags; 2115a08a8471SStephen M. Cameron 2116a08a8471SStephen M. Cameron /* wait until any scan already in progress is finished. */ 2117a08a8471SStephen M. Cameron while (1) { 2118a08a8471SStephen M. Cameron spin_lock_irqsave(&h->scan_lock, flags); 2119a08a8471SStephen M. Cameron if (h->scan_finished) 2120a08a8471SStephen M. Cameron break; 2121a08a8471SStephen M. Cameron spin_unlock_irqrestore(&h->scan_lock, flags); 2122a08a8471SStephen M. Cameron wait_event(h->scan_wait_queue, h->scan_finished); 2123a08a8471SStephen M. Cameron /* Note: We don't need to worry about a race between this 2124a08a8471SStephen M. Cameron * thread and driver unload because the midlayer will 2125a08a8471SStephen M. Cameron * have incremented the reference count, so unload won't 2126a08a8471SStephen M. Cameron * happen if we're in here. 2127a08a8471SStephen M. Cameron */ 2128a08a8471SStephen M. Cameron } 2129a08a8471SStephen M. Cameron h->scan_finished = 0; /* mark scan as in progress */ 2130a08a8471SStephen M. Cameron spin_unlock_irqrestore(&h->scan_lock, flags); 2131a08a8471SStephen M. Cameron 2132a08a8471SStephen M. Cameron hpsa_update_scsi_devices(h, h->scsi_host->host_no); 2133a08a8471SStephen M. Cameron 2134a08a8471SStephen M. Cameron spin_lock_irqsave(&h->scan_lock, flags); 2135a08a8471SStephen M. Cameron h->scan_finished = 1; /* mark scan as finished. */ 2136a08a8471SStephen M. Cameron wake_up_all(&h->scan_wait_queue); 2137a08a8471SStephen M. Cameron spin_unlock_irqrestore(&h->scan_lock, flags); 2138a08a8471SStephen M. Cameron } 2139a08a8471SStephen M. Cameron 2140a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh, 2141a08a8471SStephen M. Cameron unsigned long elapsed_time) 2142a08a8471SStephen M. Cameron { 2143a08a8471SStephen M. Cameron struct ctlr_info *h = shost_to_hba(sh); 2144a08a8471SStephen M. Cameron unsigned long flags; 2145a08a8471SStephen M. Cameron int finished; 2146a08a8471SStephen M. Cameron 2147a08a8471SStephen M. Cameron spin_lock_irqsave(&h->scan_lock, flags); 2148a08a8471SStephen M. Cameron finished = h->scan_finished; 2149a08a8471SStephen M. Cameron spin_unlock_irqrestore(&h->scan_lock, flags); 2150a08a8471SStephen M. Cameron return finished; 2151a08a8471SStephen M. Cameron } 2152a08a8471SStephen M. Cameron 2153667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev, 2154667e23d4SStephen M. Cameron int qdepth, int reason) 2155667e23d4SStephen M. Cameron { 2156667e23d4SStephen M. Cameron struct ctlr_info *h = sdev_to_hba(sdev); 2157667e23d4SStephen M. Cameron 2158667e23d4SStephen M. Cameron if (reason != SCSI_QDEPTH_DEFAULT) 2159667e23d4SStephen M. Cameron return -ENOTSUPP; 2160667e23d4SStephen M. Cameron 2161667e23d4SStephen M. Cameron if (qdepth < 1) 2162667e23d4SStephen M. Cameron qdepth = 1; 2163667e23d4SStephen M. Cameron else 2164667e23d4SStephen M. Cameron if (qdepth > h->nr_cmds) 2165667e23d4SStephen M. Cameron qdepth = h->nr_cmds; 2166667e23d4SStephen M. Cameron scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth); 2167667e23d4SStephen M. Cameron return sdev->queue_depth; 2168667e23d4SStephen M. Cameron } 2169667e23d4SStephen M. Cameron 2170edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h) 2171edd16368SStephen M. Cameron { 2172edd16368SStephen M. Cameron /* we are being forcibly unloaded, and may not refuse. */ 2173edd16368SStephen M. Cameron scsi_remove_host(h->scsi_host); 2174edd16368SStephen M. Cameron scsi_host_put(h->scsi_host); 2175edd16368SStephen M. Cameron h->scsi_host = NULL; 2176edd16368SStephen M. Cameron } 2177edd16368SStephen M. Cameron 2178edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h) 2179edd16368SStephen M. Cameron { 2180edd16368SStephen M. Cameron int rc; 2181edd16368SStephen M. Cameron 2182edd16368SStephen M. Cameron rc = hpsa_scsi_detect(h); 2183edd16368SStephen M. Cameron if (rc != 0) 2184edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "hpsa_register_scsi: failed" 2185edd16368SStephen M. Cameron " hpsa_scsi_detect(), rc is %d\n", rc); 2186edd16368SStephen M. Cameron return rc; 2187edd16368SStephen M. Cameron } 2188edd16368SStephen M. Cameron 2189edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h, 2190edd16368SStephen M. Cameron unsigned char lunaddr[]) 2191edd16368SStephen M. Cameron { 2192edd16368SStephen M. Cameron int rc = 0; 2193edd16368SStephen M. Cameron int count = 0; 2194edd16368SStephen M. Cameron int waittime = 1; /* seconds */ 2195edd16368SStephen M. Cameron struct CommandList *c; 2196edd16368SStephen M. Cameron 2197edd16368SStephen M. Cameron c = cmd_special_alloc(h); 2198edd16368SStephen M. Cameron if (!c) { 2199edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "out of memory in " 2200edd16368SStephen M. Cameron "wait_for_device_to_become_ready.\n"); 2201edd16368SStephen M. Cameron return IO_ERROR; 2202edd16368SStephen M. Cameron } 2203edd16368SStephen M. Cameron 2204edd16368SStephen M. Cameron /* Send test unit ready until device ready, or give up. */ 2205edd16368SStephen M. Cameron while (count < HPSA_TUR_RETRY_LIMIT) { 2206edd16368SStephen M. Cameron 2207edd16368SStephen M. Cameron /* Wait for a bit. do this first, because if we send 2208edd16368SStephen M. Cameron * the TUR right away, the reset will just abort it. 2209edd16368SStephen M. Cameron */ 2210edd16368SStephen M. Cameron msleep(1000 * waittime); 2211edd16368SStephen M. Cameron count++; 2212edd16368SStephen M. Cameron 2213edd16368SStephen M. Cameron /* Increase wait time with each try, up to a point. */ 2214edd16368SStephen M. Cameron if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS) 2215edd16368SStephen M. Cameron waittime = waittime * 2; 2216edd16368SStephen M. Cameron 2217edd16368SStephen M. Cameron /* Send the Test Unit Ready */ 2218edd16368SStephen M. Cameron fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD); 2219edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 2220edd16368SStephen M. Cameron /* no unmap needed here because no data xfer. */ 2221edd16368SStephen M. Cameron 2222edd16368SStephen M. Cameron if (c->err_info->CommandStatus == CMD_SUCCESS) 2223edd16368SStephen M. Cameron break; 2224edd16368SStephen M. Cameron 2225edd16368SStephen M. Cameron if (c->err_info->CommandStatus == CMD_TARGET_STATUS && 2226edd16368SStephen M. Cameron c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION && 2227edd16368SStephen M. Cameron (c->err_info->SenseInfo[2] == NO_SENSE || 2228edd16368SStephen M. Cameron c->err_info->SenseInfo[2] == UNIT_ATTENTION)) 2229edd16368SStephen M. Cameron break; 2230edd16368SStephen M. Cameron 2231edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "waiting %d secs " 2232edd16368SStephen M. Cameron "for device to become ready.\n", waittime); 2233edd16368SStephen M. Cameron rc = 1; /* device not ready. */ 2234edd16368SStephen M. Cameron } 2235edd16368SStephen M. Cameron 2236edd16368SStephen M. Cameron if (rc) 2237edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "giving up on device.\n"); 2238edd16368SStephen M. Cameron else 2239edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "device is ready.\n"); 2240edd16368SStephen M. Cameron 2241edd16368SStephen M. Cameron cmd_special_free(h, c); 2242edd16368SStephen M. Cameron return rc; 2243edd16368SStephen M. Cameron } 2244edd16368SStephen M. Cameron 2245edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from 2246edd16368SStephen M. Cameron * complaining. Doing a host- or bus-reset can't do anything good here. 2247edd16368SStephen M. Cameron */ 2248edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd) 2249edd16368SStephen M. Cameron { 2250edd16368SStephen M. Cameron int rc; 2251edd16368SStephen M. Cameron struct ctlr_info *h; 2252edd16368SStephen M. Cameron struct hpsa_scsi_dev_t *dev; 2253edd16368SStephen M. Cameron 2254edd16368SStephen M. Cameron /* find the controller to which the command to be aborted was sent */ 2255edd16368SStephen M. Cameron h = sdev_to_hba(scsicmd->device); 2256edd16368SStephen M. Cameron if (h == NULL) /* paranoia */ 2257edd16368SStephen M. Cameron return FAILED; 2258edd16368SStephen M. Cameron dev = scsicmd->device->hostdata; 2259edd16368SStephen M. Cameron if (!dev) { 2260edd16368SStephen M. Cameron dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: " 2261edd16368SStephen M. Cameron "device lookup failed.\n"); 2262edd16368SStephen M. Cameron return FAILED; 2263edd16368SStephen M. Cameron } 2264d416b0c7SStephen M. Cameron dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n", 2265d416b0c7SStephen M. Cameron h->scsi_host->host_no, dev->bus, dev->target, dev->lun); 2266edd16368SStephen M. Cameron /* send a reset to the SCSI LUN which the command was sent to */ 2267edd16368SStephen M. Cameron rc = hpsa_send_reset(h, dev->scsi3addr); 2268edd16368SStephen M. Cameron if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0) 2269edd16368SStephen M. Cameron return SUCCESS; 2270edd16368SStephen M. Cameron 2271edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "resetting device failed.\n"); 2272edd16368SStephen M. Cameron return FAILED; 2273edd16368SStephen M. Cameron } 2274edd16368SStephen M. Cameron 2275edd16368SStephen M. Cameron /* 2276edd16368SStephen M. Cameron * For operations that cannot sleep, a command block is allocated at init, 2277edd16368SStephen M. Cameron * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track 2278edd16368SStephen M. Cameron * which ones are free or in use. Lock must be held when calling this. 2279edd16368SStephen M. Cameron * cmd_free() is the complement. 2280edd16368SStephen M. Cameron */ 2281edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h) 2282edd16368SStephen M. Cameron { 2283edd16368SStephen M. Cameron struct CommandList *c; 2284edd16368SStephen M. Cameron int i; 2285edd16368SStephen M. Cameron union u64bit temp64; 2286edd16368SStephen M. Cameron dma_addr_t cmd_dma_handle, err_dma_handle; 2287edd16368SStephen M. Cameron 2288edd16368SStephen M. Cameron do { 2289edd16368SStephen M. Cameron i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds); 2290edd16368SStephen M. Cameron if (i == h->nr_cmds) 2291edd16368SStephen M. Cameron return NULL; 2292edd16368SStephen M. Cameron } while (test_and_set_bit 2293edd16368SStephen M. Cameron (i & (BITS_PER_LONG - 1), 2294edd16368SStephen M. Cameron h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0); 2295edd16368SStephen M. Cameron c = h->cmd_pool + i; 2296edd16368SStephen M. Cameron memset(c, 0, sizeof(*c)); 2297edd16368SStephen M. Cameron cmd_dma_handle = h->cmd_pool_dhandle 2298edd16368SStephen M. Cameron + i * sizeof(*c); 2299edd16368SStephen M. Cameron c->err_info = h->errinfo_pool + i; 2300edd16368SStephen M. Cameron memset(c->err_info, 0, sizeof(*c->err_info)); 2301edd16368SStephen M. Cameron err_dma_handle = h->errinfo_pool_dhandle 2302edd16368SStephen M. Cameron + i * sizeof(*c->err_info); 2303edd16368SStephen M. Cameron h->nr_allocs++; 2304edd16368SStephen M. Cameron 2305edd16368SStephen M. Cameron c->cmdindex = i; 2306edd16368SStephen M. Cameron 23079e0fc764SStephen M. Cameron INIT_LIST_HEAD(&c->list); 230801a02ffcSStephen M. Cameron c->busaddr = (u32) cmd_dma_handle; 230901a02ffcSStephen M. Cameron temp64.val = (u64) err_dma_handle; 2310edd16368SStephen M. Cameron c->ErrDesc.Addr.lower = temp64.val32.lower; 2311edd16368SStephen M. Cameron c->ErrDesc.Addr.upper = temp64.val32.upper; 2312edd16368SStephen M. Cameron c->ErrDesc.Len = sizeof(*c->err_info); 2313edd16368SStephen M. Cameron 2314edd16368SStephen M. Cameron c->h = h; 2315edd16368SStephen M. Cameron return c; 2316edd16368SStephen M. Cameron } 2317edd16368SStephen M. Cameron 2318edd16368SStephen M. Cameron /* For operations that can wait for kmalloc to possibly sleep, 2319edd16368SStephen M. Cameron * this routine can be called. Lock need not be held to call 2320edd16368SStephen M. Cameron * cmd_special_alloc. cmd_special_free() is the complement. 2321edd16368SStephen M. Cameron */ 2322edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h) 2323edd16368SStephen M. Cameron { 2324edd16368SStephen M. Cameron struct CommandList *c; 2325edd16368SStephen M. Cameron union u64bit temp64; 2326edd16368SStephen M. Cameron dma_addr_t cmd_dma_handle, err_dma_handle; 2327edd16368SStephen M. Cameron 2328edd16368SStephen M. Cameron c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle); 2329edd16368SStephen M. Cameron if (c == NULL) 2330edd16368SStephen M. Cameron return NULL; 2331edd16368SStephen M. Cameron memset(c, 0, sizeof(*c)); 2332edd16368SStephen M. Cameron 2333edd16368SStephen M. Cameron c->cmdindex = -1; 2334edd16368SStephen M. Cameron 2335edd16368SStephen M. Cameron c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info), 2336edd16368SStephen M. Cameron &err_dma_handle); 2337edd16368SStephen M. Cameron 2338edd16368SStephen M. Cameron if (c->err_info == NULL) { 2339edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 2340edd16368SStephen M. Cameron sizeof(*c), c, cmd_dma_handle); 2341edd16368SStephen M. Cameron return NULL; 2342edd16368SStephen M. Cameron } 2343edd16368SStephen M. Cameron memset(c->err_info, 0, sizeof(*c->err_info)); 2344edd16368SStephen M. Cameron 23459e0fc764SStephen M. Cameron INIT_LIST_HEAD(&c->list); 234601a02ffcSStephen M. Cameron c->busaddr = (u32) cmd_dma_handle; 234701a02ffcSStephen M. Cameron temp64.val = (u64) err_dma_handle; 2348edd16368SStephen M. Cameron c->ErrDesc.Addr.lower = temp64.val32.lower; 2349edd16368SStephen M. Cameron c->ErrDesc.Addr.upper = temp64.val32.upper; 2350edd16368SStephen M. Cameron c->ErrDesc.Len = sizeof(*c->err_info); 2351edd16368SStephen M. Cameron 2352edd16368SStephen M. Cameron c->h = h; 2353edd16368SStephen M. Cameron return c; 2354edd16368SStephen M. Cameron } 2355edd16368SStephen M. Cameron 2356edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c) 2357edd16368SStephen M. Cameron { 2358edd16368SStephen M. Cameron int i; 2359edd16368SStephen M. Cameron 2360edd16368SStephen M. Cameron i = c - h->cmd_pool; 2361edd16368SStephen M. Cameron clear_bit(i & (BITS_PER_LONG - 1), 2362edd16368SStephen M. Cameron h->cmd_pool_bits + (i / BITS_PER_LONG)); 2363edd16368SStephen M. Cameron h->nr_frees++; 2364edd16368SStephen M. Cameron } 2365edd16368SStephen M. Cameron 2366edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c) 2367edd16368SStephen M. Cameron { 2368edd16368SStephen M. Cameron union u64bit temp64; 2369edd16368SStephen M. Cameron 2370edd16368SStephen M. Cameron temp64.val32.lower = c->ErrDesc.Addr.lower; 2371edd16368SStephen M. Cameron temp64.val32.upper = c->ErrDesc.Addr.upper; 2372edd16368SStephen M. Cameron pci_free_consistent(h->pdev, sizeof(*c->err_info), 2373edd16368SStephen M. Cameron c->err_info, (dma_addr_t) temp64.val); 2374edd16368SStephen M. Cameron pci_free_consistent(h->pdev, sizeof(*c), 2375d896f3f3SStephen M. Cameron c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK)); 2376edd16368SStephen M. Cameron } 2377edd16368SStephen M. Cameron 2378edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT 2379edd16368SStephen M. Cameron 2380edd16368SStephen M. Cameron static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg) 2381edd16368SStephen M. Cameron { 2382edd16368SStephen M. Cameron IOCTL32_Command_struct __user *arg32 = 2383edd16368SStephen M. Cameron (IOCTL32_Command_struct __user *) arg; 2384edd16368SStephen M. Cameron IOCTL_Command_struct arg64; 2385edd16368SStephen M. Cameron IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64)); 2386edd16368SStephen M. Cameron int err; 2387edd16368SStephen M. Cameron u32 cp; 2388edd16368SStephen M. Cameron 2389938abd84SVasiliy Kulikov memset(&arg64, 0, sizeof(arg64)); 2390edd16368SStephen M. Cameron err = 0; 2391edd16368SStephen M. Cameron err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, 2392edd16368SStephen M. Cameron sizeof(arg64.LUN_info)); 2393edd16368SStephen M. Cameron err |= copy_from_user(&arg64.Request, &arg32->Request, 2394edd16368SStephen M. Cameron sizeof(arg64.Request)); 2395edd16368SStephen M. Cameron err |= copy_from_user(&arg64.error_info, &arg32->error_info, 2396edd16368SStephen M. Cameron sizeof(arg64.error_info)); 2397edd16368SStephen M. Cameron err |= get_user(arg64.buf_size, &arg32->buf_size); 2398edd16368SStephen M. Cameron err |= get_user(cp, &arg32->buf); 2399edd16368SStephen M. Cameron arg64.buf = compat_ptr(cp); 2400edd16368SStephen M. Cameron err |= copy_to_user(p, &arg64, sizeof(arg64)); 2401edd16368SStephen M. Cameron 2402edd16368SStephen M. Cameron if (err) 2403edd16368SStephen M. Cameron return -EFAULT; 2404edd16368SStephen M. Cameron 2405e39eeaedSStephen M. Cameron err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p); 2406edd16368SStephen M. Cameron if (err) 2407edd16368SStephen M. Cameron return err; 2408edd16368SStephen M. Cameron err |= copy_in_user(&arg32->error_info, &p->error_info, 2409edd16368SStephen M. Cameron sizeof(arg32->error_info)); 2410edd16368SStephen M. Cameron if (err) 2411edd16368SStephen M. Cameron return -EFAULT; 2412edd16368SStephen M. Cameron return err; 2413edd16368SStephen M. Cameron } 2414edd16368SStephen M. Cameron 2415edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev, 2416edd16368SStephen M. Cameron int cmd, void *arg) 2417edd16368SStephen M. Cameron { 2418edd16368SStephen M. Cameron BIG_IOCTL32_Command_struct __user *arg32 = 2419edd16368SStephen M. Cameron (BIG_IOCTL32_Command_struct __user *) arg; 2420edd16368SStephen M. Cameron BIG_IOCTL_Command_struct arg64; 2421edd16368SStephen M. Cameron BIG_IOCTL_Command_struct __user *p = 2422edd16368SStephen M. Cameron compat_alloc_user_space(sizeof(arg64)); 2423edd16368SStephen M. Cameron int err; 2424edd16368SStephen M. Cameron u32 cp; 2425edd16368SStephen M. Cameron 2426938abd84SVasiliy Kulikov memset(&arg64, 0, sizeof(arg64)); 2427edd16368SStephen M. Cameron err = 0; 2428edd16368SStephen M. Cameron err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, 2429edd16368SStephen M. Cameron sizeof(arg64.LUN_info)); 2430edd16368SStephen M. Cameron err |= copy_from_user(&arg64.Request, &arg32->Request, 2431edd16368SStephen M. Cameron sizeof(arg64.Request)); 2432edd16368SStephen M. Cameron err |= copy_from_user(&arg64.error_info, &arg32->error_info, 2433edd16368SStephen M. Cameron sizeof(arg64.error_info)); 2434edd16368SStephen M. Cameron err |= get_user(arg64.buf_size, &arg32->buf_size); 2435edd16368SStephen M. Cameron err |= get_user(arg64.malloc_size, &arg32->malloc_size); 2436edd16368SStephen M. Cameron err |= get_user(cp, &arg32->buf); 2437edd16368SStephen M. Cameron arg64.buf = compat_ptr(cp); 2438edd16368SStephen M. Cameron err |= copy_to_user(p, &arg64, sizeof(arg64)); 2439edd16368SStephen M. Cameron 2440edd16368SStephen M. Cameron if (err) 2441edd16368SStephen M. Cameron return -EFAULT; 2442edd16368SStephen M. Cameron 2443e39eeaedSStephen M. Cameron err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p); 2444edd16368SStephen M. Cameron if (err) 2445edd16368SStephen M. Cameron return err; 2446edd16368SStephen M. Cameron err |= copy_in_user(&arg32->error_info, &p->error_info, 2447edd16368SStephen M. Cameron sizeof(arg32->error_info)); 2448edd16368SStephen M. Cameron if (err) 2449edd16368SStephen M. Cameron return -EFAULT; 2450edd16368SStephen M. Cameron return err; 2451edd16368SStephen M. Cameron } 245271fe75a7SStephen M. Cameron 245371fe75a7SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg) 245471fe75a7SStephen M. Cameron { 245571fe75a7SStephen M. Cameron switch (cmd) { 245671fe75a7SStephen M. Cameron case CCISS_GETPCIINFO: 245771fe75a7SStephen M. Cameron case CCISS_GETINTINFO: 245871fe75a7SStephen M. Cameron case CCISS_SETINTINFO: 245971fe75a7SStephen M. Cameron case CCISS_GETNODENAME: 246071fe75a7SStephen M. Cameron case CCISS_SETNODENAME: 246171fe75a7SStephen M. Cameron case CCISS_GETHEARTBEAT: 246271fe75a7SStephen M. Cameron case CCISS_GETBUSTYPES: 246371fe75a7SStephen M. Cameron case CCISS_GETFIRMVER: 246471fe75a7SStephen M. Cameron case CCISS_GETDRIVVER: 246571fe75a7SStephen M. Cameron case CCISS_REVALIDVOLS: 246671fe75a7SStephen M. Cameron case CCISS_DEREGDISK: 246771fe75a7SStephen M. Cameron case CCISS_REGNEWDISK: 246871fe75a7SStephen M. Cameron case CCISS_REGNEWD: 246971fe75a7SStephen M. Cameron case CCISS_RESCANDISK: 247071fe75a7SStephen M. Cameron case CCISS_GETLUNINFO: 247171fe75a7SStephen M. Cameron return hpsa_ioctl(dev, cmd, arg); 247271fe75a7SStephen M. Cameron 247371fe75a7SStephen M. Cameron case CCISS_PASSTHRU32: 247471fe75a7SStephen M. Cameron return hpsa_ioctl32_passthru(dev, cmd, arg); 247571fe75a7SStephen M. Cameron case CCISS_BIG_PASSTHRU32: 247671fe75a7SStephen M. Cameron return hpsa_ioctl32_big_passthru(dev, cmd, arg); 247771fe75a7SStephen M. Cameron 247871fe75a7SStephen M. Cameron default: 247971fe75a7SStephen M. Cameron return -ENOIOCTLCMD; 248071fe75a7SStephen M. Cameron } 248171fe75a7SStephen M. Cameron } 2482edd16368SStephen M. Cameron #endif 2483edd16368SStephen M. Cameron 2484edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp) 2485edd16368SStephen M. Cameron { 2486edd16368SStephen M. Cameron struct hpsa_pci_info pciinfo; 2487edd16368SStephen M. Cameron 2488edd16368SStephen M. Cameron if (!argp) 2489edd16368SStephen M. Cameron return -EINVAL; 2490edd16368SStephen M. Cameron pciinfo.domain = pci_domain_nr(h->pdev->bus); 2491edd16368SStephen M. Cameron pciinfo.bus = h->pdev->bus->number; 2492edd16368SStephen M. Cameron pciinfo.dev_fn = h->pdev->devfn; 2493edd16368SStephen M. Cameron pciinfo.board_id = h->board_id; 2494edd16368SStephen M. Cameron if (copy_to_user(argp, &pciinfo, sizeof(pciinfo))) 2495edd16368SStephen M. Cameron return -EFAULT; 2496edd16368SStephen M. Cameron return 0; 2497edd16368SStephen M. Cameron } 2498edd16368SStephen M. Cameron 2499edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp) 2500edd16368SStephen M. Cameron { 2501edd16368SStephen M. Cameron DriverVer_type DriverVer; 2502edd16368SStephen M. Cameron unsigned char vmaj, vmin, vsubmin; 2503edd16368SStephen M. Cameron int rc; 2504edd16368SStephen M. Cameron 2505edd16368SStephen M. Cameron rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu", 2506edd16368SStephen M. Cameron &vmaj, &vmin, &vsubmin); 2507edd16368SStephen M. Cameron if (rc != 3) { 2508edd16368SStephen M. Cameron dev_info(&h->pdev->dev, "driver version string '%s' " 2509edd16368SStephen M. Cameron "unrecognized.", HPSA_DRIVER_VERSION); 2510edd16368SStephen M. Cameron vmaj = 0; 2511edd16368SStephen M. Cameron vmin = 0; 2512edd16368SStephen M. Cameron vsubmin = 0; 2513edd16368SStephen M. Cameron } 2514edd16368SStephen M. Cameron DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin; 2515edd16368SStephen M. Cameron if (!argp) 2516edd16368SStephen M. Cameron return -EINVAL; 2517edd16368SStephen M. Cameron if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type))) 2518edd16368SStephen M. Cameron return -EFAULT; 2519edd16368SStephen M. Cameron return 0; 2520edd16368SStephen M. Cameron } 2521edd16368SStephen M. Cameron 2522edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp) 2523edd16368SStephen M. Cameron { 2524edd16368SStephen M. Cameron IOCTL_Command_struct iocommand; 2525edd16368SStephen M. Cameron struct CommandList *c; 2526edd16368SStephen M. Cameron char *buff = NULL; 2527edd16368SStephen M. Cameron union u64bit temp64; 2528edd16368SStephen M. Cameron 2529edd16368SStephen M. Cameron if (!argp) 2530edd16368SStephen M. Cameron return -EINVAL; 2531edd16368SStephen M. Cameron if (!capable(CAP_SYS_RAWIO)) 2532edd16368SStephen M. Cameron return -EPERM; 2533edd16368SStephen M. Cameron if (copy_from_user(&iocommand, argp, sizeof(iocommand))) 2534edd16368SStephen M. Cameron return -EFAULT; 2535edd16368SStephen M. Cameron if ((iocommand.buf_size < 1) && 2536edd16368SStephen M. Cameron (iocommand.Request.Type.Direction != XFER_NONE)) { 2537edd16368SStephen M. Cameron return -EINVAL; 2538edd16368SStephen M. Cameron } 2539edd16368SStephen M. Cameron if (iocommand.buf_size > 0) { 2540edd16368SStephen M. Cameron buff = kmalloc(iocommand.buf_size, GFP_KERNEL); 2541edd16368SStephen M. Cameron if (buff == NULL) 2542edd16368SStephen M. Cameron return -EFAULT; 2543edd16368SStephen M. Cameron if (iocommand.Request.Type.Direction == XFER_WRITE) { 2544edd16368SStephen M. Cameron /* Copy the data into the buffer we created */ 2545b03a7771SStephen M. Cameron if (copy_from_user(buff, iocommand.buf, 2546b03a7771SStephen M. Cameron iocommand.buf_size)) { 2547edd16368SStephen M. Cameron kfree(buff); 2548edd16368SStephen M. Cameron return -EFAULT; 2549edd16368SStephen M. Cameron } 2550b03a7771SStephen M. Cameron } else { 2551edd16368SStephen M. Cameron memset(buff, 0, iocommand.buf_size); 2552b03a7771SStephen M. Cameron } 2553b03a7771SStephen M. Cameron } 2554edd16368SStephen M. Cameron c = cmd_special_alloc(h); 2555edd16368SStephen M. Cameron if (c == NULL) { 2556edd16368SStephen M. Cameron kfree(buff); 2557edd16368SStephen M. Cameron return -ENOMEM; 2558edd16368SStephen M. Cameron } 2559edd16368SStephen M. Cameron /* Fill in the command type */ 2560edd16368SStephen M. Cameron c->cmd_type = CMD_IOCTL_PEND; 2561edd16368SStephen M. Cameron /* Fill in Command Header */ 2562edd16368SStephen M. Cameron c->Header.ReplyQueue = 0; /* unused in simple mode */ 2563edd16368SStephen M. Cameron if (iocommand.buf_size > 0) { /* buffer to fill */ 2564edd16368SStephen M. Cameron c->Header.SGList = 1; 2565edd16368SStephen M. Cameron c->Header.SGTotal = 1; 2566edd16368SStephen M. Cameron } else { /* no buffers to fill */ 2567edd16368SStephen M. Cameron c->Header.SGList = 0; 2568edd16368SStephen M. Cameron c->Header.SGTotal = 0; 2569edd16368SStephen M. Cameron } 2570edd16368SStephen M. Cameron memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN)); 2571edd16368SStephen M. Cameron /* use the kernel address the cmd block for tag */ 2572edd16368SStephen M. Cameron c->Header.Tag.lower = c->busaddr; 2573edd16368SStephen M. Cameron 2574edd16368SStephen M. Cameron /* Fill in Request block */ 2575edd16368SStephen M. Cameron memcpy(&c->Request, &iocommand.Request, 2576edd16368SStephen M. Cameron sizeof(c->Request)); 2577edd16368SStephen M. Cameron 2578edd16368SStephen M. Cameron /* Fill in the scatter gather information */ 2579edd16368SStephen M. Cameron if (iocommand.buf_size > 0) { 2580edd16368SStephen M. Cameron temp64.val = pci_map_single(h->pdev, buff, 2581edd16368SStephen M. Cameron iocommand.buf_size, PCI_DMA_BIDIRECTIONAL); 2582edd16368SStephen M. Cameron c->SG[0].Addr.lower = temp64.val32.lower; 2583edd16368SStephen M. Cameron c->SG[0].Addr.upper = temp64.val32.upper; 2584edd16368SStephen M. Cameron c->SG[0].Len = iocommand.buf_size; 2585edd16368SStephen M. Cameron c->SG[0].Ext = 0; /* we are not chaining*/ 2586edd16368SStephen M. Cameron } 2587edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 2588c2dd32e0SStephen M. Cameron if (iocommand.buf_size > 0) 2589edd16368SStephen M. Cameron hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL); 2590edd16368SStephen M. Cameron check_ioctl_unit_attention(h, c); 2591edd16368SStephen M. Cameron 2592edd16368SStephen M. Cameron /* Copy the error information out */ 2593edd16368SStephen M. Cameron memcpy(&iocommand.error_info, c->err_info, 2594edd16368SStephen M. Cameron sizeof(iocommand.error_info)); 2595edd16368SStephen M. Cameron if (copy_to_user(argp, &iocommand, sizeof(iocommand))) { 2596edd16368SStephen M. Cameron kfree(buff); 2597edd16368SStephen M. Cameron cmd_special_free(h, c); 2598edd16368SStephen M. Cameron return -EFAULT; 2599edd16368SStephen M. Cameron } 2600b03a7771SStephen M. Cameron if (iocommand.Request.Type.Direction == XFER_READ && 2601b03a7771SStephen M. Cameron iocommand.buf_size > 0) { 2602edd16368SStephen M. Cameron /* Copy the data out of the buffer we created */ 2603edd16368SStephen M. Cameron if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) { 2604edd16368SStephen M. Cameron kfree(buff); 2605edd16368SStephen M. Cameron cmd_special_free(h, c); 2606edd16368SStephen M. Cameron return -EFAULT; 2607edd16368SStephen M. Cameron } 2608edd16368SStephen M. Cameron } 2609edd16368SStephen M. Cameron kfree(buff); 2610edd16368SStephen M. Cameron cmd_special_free(h, c); 2611edd16368SStephen M. Cameron return 0; 2612edd16368SStephen M. Cameron } 2613edd16368SStephen M. Cameron 2614edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp) 2615edd16368SStephen M. Cameron { 2616edd16368SStephen M. Cameron BIG_IOCTL_Command_struct *ioc; 2617edd16368SStephen M. Cameron struct CommandList *c; 2618edd16368SStephen M. Cameron unsigned char **buff = NULL; 2619edd16368SStephen M. Cameron int *buff_size = NULL; 2620edd16368SStephen M. Cameron union u64bit temp64; 2621edd16368SStephen M. Cameron BYTE sg_used = 0; 2622edd16368SStephen M. Cameron int status = 0; 2623edd16368SStephen M. Cameron int i; 262401a02ffcSStephen M. Cameron u32 left; 262501a02ffcSStephen M. Cameron u32 sz; 2626edd16368SStephen M. Cameron BYTE __user *data_ptr; 2627edd16368SStephen M. Cameron 2628edd16368SStephen M. Cameron if (!argp) 2629edd16368SStephen M. Cameron return -EINVAL; 2630edd16368SStephen M. Cameron if (!capable(CAP_SYS_RAWIO)) 2631edd16368SStephen M. Cameron return -EPERM; 2632edd16368SStephen M. Cameron ioc = (BIG_IOCTL_Command_struct *) 2633edd16368SStephen M. Cameron kmalloc(sizeof(*ioc), GFP_KERNEL); 2634edd16368SStephen M. Cameron if (!ioc) { 2635edd16368SStephen M. Cameron status = -ENOMEM; 2636edd16368SStephen M. Cameron goto cleanup1; 2637edd16368SStephen M. Cameron } 2638edd16368SStephen M. Cameron if (copy_from_user(ioc, argp, sizeof(*ioc))) { 2639edd16368SStephen M. Cameron status = -EFAULT; 2640edd16368SStephen M. Cameron goto cleanup1; 2641edd16368SStephen M. Cameron } 2642edd16368SStephen M. Cameron if ((ioc->buf_size < 1) && 2643edd16368SStephen M. Cameron (ioc->Request.Type.Direction != XFER_NONE)) { 2644edd16368SStephen M. Cameron status = -EINVAL; 2645edd16368SStephen M. Cameron goto cleanup1; 2646edd16368SStephen M. Cameron } 2647edd16368SStephen M. Cameron /* Check kmalloc limits using all SGs */ 2648edd16368SStephen M. Cameron if (ioc->malloc_size > MAX_KMALLOC_SIZE) { 2649edd16368SStephen M. Cameron status = -EINVAL; 2650edd16368SStephen M. Cameron goto cleanup1; 2651edd16368SStephen M. Cameron } 2652edd16368SStephen M. Cameron if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) { 2653edd16368SStephen M. Cameron status = -EINVAL; 2654edd16368SStephen M. Cameron goto cleanup1; 2655edd16368SStephen M. Cameron } 2656edd16368SStephen M. Cameron buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL); 2657edd16368SStephen M. Cameron if (!buff) { 2658edd16368SStephen M. Cameron status = -ENOMEM; 2659edd16368SStephen M. Cameron goto cleanup1; 2660edd16368SStephen M. Cameron } 2661edd16368SStephen M. Cameron buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL); 2662edd16368SStephen M. Cameron if (!buff_size) { 2663edd16368SStephen M. Cameron status = -ENOMEM; 2664edd16368SStephen M. Cameron goto cleanup1; 2665edd16368SStephen M. Cameron } 2666edd16368SStephen M. Cameron left = ioc->buf_size; 2667edd16368SStephen M. Cameron data_ptr = ioc->buf; 2668edd16368SStephen M. Cameron while (left) { 2669edd16368SStephen M. Cameron sz = (left > ioc->malloc_size) ? ioc->malloc_size : left; 2670edd16368SStephen M. Cameron buff_size[sg_used] = sz; 2671edd16368SStephen M. Cameron buff[sg_used] = kmalloc(sz, GFP_KERNEL); 2672edd16368SStephen M. Cameron if (buff[sg_used] == NULL) { 2673edd16368SStephen M. Cameron status = -ENOMEM; 2674edd16368SStephen M. Cameron goto cleanup1; 2675edd16368SStephen M. Cameron } 2676edd16368SStephen M. Cameron if (ioc->Request.Type.Direction == XFER_WRITE) { 2677edd16368SStephen M. Cameron if (copy_from_user(buff[sg_used], data_ptr, sz)) { 2678edd16368SStephen M. Cameron status = -ENOMEM; 2679edd16368SStephen M. Cameron goto cleanup1; 2680edd16368SStephen M. Cameron } 2681edd16368SStephen M. Cameron } else 2682edd16368SStephen M. Cameron memset(buff[sg_used], 0, sz); 2683edd16368SStephen M. Cameron left -= sz; 2684edd16368SStephen M. Cameron data_ptr += sz; 2685edd16368SStephen M. Cameron sg_used++; 2686edd16368SStephen M. Cameron } 2687edd16368SStephen M. Cameron c = cmd_special_alloc(h); 2688edd16368SStephen M. Cameron if (c == NULL) { 2689edd16368SStephen M. Cameron status = -ENOMEM; 2690edd16368SStephen M. Cameron goto cleanup1; 2691edd16368SStephen M. Cameron } 2692edd16368SStephen M. Cameron c->cmd_type = CMD_IOCTL_PEND; 2693edd16368SStephen M. Cameron c->Header.ReplyQueue = 0; 2694b03a7771SStephen M. Cameron c->Header.SGList = c->Header.SGTotal = sg_used; 2695edd16368SStephen M. Cameron memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN)); 2696edd16368SStephen M. Cameron c->Header.Tag.lower = c->busaddr; 2697edd16368SStephen M. Cameron memcpy(&c->Request, &ioc->Request, sizeof(c->Request)); 2698edd16368SStephen M. Cameron if (ioc->buf_size > 0) { 2699edd16368SStephen M. Cameron int i; 2700edd16368SStephen M. Cameron for (i = 0; i < sg_used; i++) { 2701edd16368SStephen M. Cameron temp64.val = pci_map_single(h->pdev, buff[i], 2702edd16368SStephen M. Cameron buff_size[i], PCI_DMA_BIDIRECTIONAL); 2703edd16368SStephen M. Cameron c->SG[i].Addr.lower = temp64.val32.lower; 2704edd16368SStephen M. Cameron c->SG[i].Addr.upper = temp64.val32.upper; 2705edd16368SStephen M. Cameron c->SG[i].Len = buff_size[i]; 2706edd16368SStephen M. Cameron /* we are not chaining */ 2707edd16368SStephen M. Cameron c->SG[i].Ext = 0; 2708edd16368SStephen M. Cameron } 2709edd16368SStephen M. Cameron } 2710edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_core(h, c); 2711b03a7771SStephen M. Cameron if (sg_used) 2712edd16368SStephen M. Cameron hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL); 2713edd16368SStephen M. Cameron check_ioctl_unit_attention(h, c); 2714edd16368SStephen M. Cameron /* Copy the error information out */ 2715edd16368SStephen M. Cameron memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info)); 2716edd16368SStephen M. Cameron if (copy_to_user(argp, ioc, sizeof(*ioc))) { 2717edd16368SStephen M. Cameron cmd_special_free(h, c); 2718edd16368SStephen M. Cameron status = -EFAULT; 2719edd16368SStephen M. Cameron goto cleanup1; 2720edd16368SStephen M. Cameron } 2721b03a7771SStephen M. Cameron if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) { 2722edd16368SStephen M. Cameron /* Copy the data out of the buffer we created */ 2723edd16368SStephen M. Cameron BYTE __user *ptr = ioc->buf; 2724edd16368SStephen M. Cameron for (i = 0; i < sg_used; i++) { 2725edd16368SStephen M. Cameron if (copy_to_user(ptr, buff[i], buff_size[i])) { 2726edd16368SStephen M. Cameron cmd_special_free(h, c); 2727edd16368SStephen M. Cameron status = -EFAULT; 2728edd16368SStephen M. Cameron goto cleanup1; 2729edd16368SStephen M. Cameron } 2730edd16368SStephen M. Cameron ptr += buff_size[i]; 2731edd16368SStephen M. Cameron } 2732edd16368SStephen M. Cameron } 2733edd16368SStephen M. Cameron cmd_special_free(h, c); 2734edd16368SStephen M. Cameron status = 0; 2735edd16368SStephen M. Cameron cleanup1: 2736edd16368SStephen M. Cameron if (buff) { 2737edd16368SStephen M. Cameron for (i = 0; i < sg_used; i++) 2738edd16368SStephen M. Cameron kfree(buff[i]); 2739edd16368SStephen M. Cameron kfree(buff); 2740edd16368SStephen M. Cameron } 2741edd16368SStephen M. Cameron kfree(buff_size); 2742edd16368SStephen M. Cameron kfree(ioc); 2743edd16368SStephen M. Cameron return status; 2744edd16368SStephen M. Cameron } 2745edd16368SStephen M. Cameron 2746edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h, 2747edd16368SStephen M. Cameron struct CommandList *c) 2748edd16368SStephen M. Cameron { 2749edd16368SStephen M. Cameron if (c->err_info->CommandStatus == CMD_TARGET_STATUS && 2750edd16368SStephen M. Cameron c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) 2751edd16368SStephen M. Cameron (void) check_for_unit_attention(h, c); 2752edd16368SStephen M. Cameron } 2753edd16368SStephen M. Cameron /* 2754edd16368SStephen M. Cameron * ioctl 2755edd16368SStephen M. Cameron */ 2756edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg) 2757edd16368SStephen M. Cameron { 2758edd16368SStephen M. Cameron struct ctlr_info *h; 2759edd16368SStephen M. Cameron void __user *argp = (void __user *)arg; 2760edd16368SStephen M. Cameron 2761edd16368SStephen M. Cameron h = sdev_to_hba(dev); 2762edd16368SStephen M. Cameron 2763edd16368SStephen M. Cameron switch (cmd) { 2764edd16368SStephen M. Cameron case CCISS_DEREGDISK: 2765edd16368SStephen M. Cameron case CCISS_REGNEWDISK: 2766edd16368SStephen M. Cameron case CCISS_REGNEWD: 2767a08a8471SStephen M. Cameron hpsa_scan_start(h->scsi_host); 2768edd16368SStephen M. Cameron return 0; 2769edd16368SStephen M. Cameron case CCISS_GETPCIINFO: 2770edd16368SStephen M. Cameron return hpsa_getpciinfo_ioctl(h, argp); 2771edd16368SStephen M. Cameron case CCISS_GETDRIVVER: 2772edd16368SStephen M. Cameron return hpsa_getdrivver_ioctl(h, argp); 2773edd16368SStephen M. Cameron case CCISS_PASSTHRU: 2774edd16368SStephen M. Cameron return hpsa_passthru_ioctl(h, argp); 2775edd16368SStephen M. Cameron case CCISS_BIG_PASSTHRU: 2776edd16368SStephen M. Cameron return hpsa_big_passthru_ioctl(h, argp); 2777edd16368SStephen M. Cameron default: 2778edd16368SStephen M. Cameron return -ENOTTY; 2779edd16368SStephen M. Cameron } 2780edd16368SStephen M. Cameron } 2781edd16368SStephen M. Cameron 278264670ac8SStephen M. Cameron static int __devinit hpsa_send_host_reset(struct ctlr_info *h, 278364670ac8SStephen M. Cameron unsigned char *scsi3addr, u8 reset_type) 278464670ac8SStephen M. Cameron { 278564670ac8SStephen M. Cameron struct CommandList *c; 278664670ac8SStephen M. Cameron 278764670ac8SStephen M. Cameron c = cmd_alloc(h); 278864670ac8SStephen M. Cameron if (!c) 278964670ac8SStephen M. Cameron return -ENOMEM; 279064670ac8SStephen M. Cameron fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, 279164670ac8SStephen M. Cameron RAID_CTLR_LUNID, TYPE_MSG); 279264670ac8SStephen M. Cameron c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */ 279364670ac8SStephen M. Cameron c->waiting = NULL; 279464670ac8SStephen M. Cameron enqueue_cmd_and_start_io(h, c); 279564670ac8SStephen M. Cameron /* Don't wait for completion, the reset won't complete. Don't free 279664670ac8SStephen M. Cameron * the command either. This is the last command we will send before 279764670ac8SStephen M. Cameron * re-initializing everything, so it doesn't matter and won't leak. 279864670ac8SStephen M. Cameron */ 279964670ac8SStephen M. Cameron return 0; 280064670ac8SStephen M. Cameron } 280164670ac8SStephen M. Cameron 280201a02ffcSStephen M. Cameron static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h, 280301a02ffcSStephen M. Cameron void *buff, size_t size, u8 page_code, unsigned char *scsi3addr, 2804edd16368SStephen M. Cameron int cmd_type) 2805edd16368SStephen M. Cameron { 2806edd16368SStephen M. Cameron int pci_dir = XFER_NONE; 2807edd16368SStephen M. Cameron 2808edd16368SStephen M. Cameron c->cmd_type = CMD_IOCTL_PEND; 2809edd16368SStephen M. Cameron c->Header.ReplyQueue = 0; 2810edd16368SStephen M. Cameron if (buff != NULL && size > 0) { 2811edd16368SStephen M. Cameron c->Header.SGList = 1; 2812edd16368SStephen M. Cameron c->Header.SGTotal = 1; 2813edd16368SStephen M. Cameron } else { 2814edd16368SStephen M. Cameron c->Header.SGList = 0; 2815edd16368SStephen M. Cameron c->Header.SGTotal = 0; 2816edd16368SStephen M. Cameron } 2817edd16368SStephen M. Cameron c->Header.Tag.lower = c->busaddr; 2818edd16368SStephen M. Cameron memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8); 2819edd16368SStephen M. Cameron 2820edd16368SStephen M. Cameron c->Request.Type.Type = cmd_type; 2821edd16368SStephen M. Cameron if (cmd_type == TYPE_CMD) { 2822edd16368SStephen M. Cameron switch (cmd) { 2823edd16368SStephen M. Cameron case HPSA_INQUIRY: 2824edd16368SStephen M. Cameron /* are we trying to read a vital product page */ 2825edd16368SStephen M. Cameron if (page_code != 0) { 2826edd16368SStephen M. Cameron c->Request.CDB[1] = 0x01; 2827edd16368SStephen M. Cameron c->Request.CDB[2] = page_code; 2828edd16368SStephen M. Cameron } 2829edd16368SStephen M. Cameron c->Request.CDBLen = 6; 2830edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2831edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_READ; 2832edd16368SStephen M. Cameron c->Request.Timeout = 0; 2833edd16368SStephen M. Cameron c->Request.CDB[0] = HPSA_INQUIRY; 2834edd16368SStephen M. Cameron c->Request.CDB[4] = size & 0xFF; 2835edd16368SStephen M. Cameron break; 2836edd16368SStephen M. Cameron case HPSA_REPORT_LOG: 2837edd16368SStephen M. Cameron case HPSA_REPORT_PHYS: 2838edd16368SStephen M. Cameron /* Talking to controller so It's a physical command 2839edd16368SStephen M. Cameron mode = 00 target = 0. Nothing to write. 2840edd16368SStephen M. Cameron */ 2841edd16368SStephen M. Cameron c->Request.CDBLen = 12; 2842edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2843edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_READ; 2844edd16368SStephen M. Cameron c->Request.Timeout = 0; 2845edd16368SStephen M. Cameron c->Request.CDB[0] = cmd; 2846edd16368SStephen M. Cameron c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */ 2847edd16368SStephen M. Cameron c->Request.CDB[7] = (size >> 16) & 0xFF; 2848edd16368SStephen M. Cameron c->Request.CDB[8] = (size >> 8) & 0xFF; 2849edd16368SStephen M. Cameron c->Request.CDB[9] = size & 0xFF; 2850edd16368SStephen M. Cameron break; 2851edd16368SStephen M. Cameron case HPSA_CACHE_FLUSH: 2852edd16368SStephen M. Cameron c->Request.CDBLen = 12; 2853edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2854edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_WRITE; 2855edd16368SStephen M. Cameron c->Request.Timeout = 0; 2856edd16368SStephen M. Cameron c->Request.CDB[0] = BMIC_WRITE; 2857edd16368SStephen M. Cameron c->Request.CDB[6] = BMIC_CACHE_FLUSH; 2858edd16368SStephen M. Cameron break; 2859edd16368SStephen M. Cameron case TEST_UNIT_READY: 2860edd16368SStephen M. Cameron c->Request.CDBLen = 6; 2861edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2862edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_NONE; 2863edd16368SStephen M. Cameron c->Request.Timeout = 0; 2864edd16368SStephen M. Cameron break; 2865edd16368SStephen M. Cameron default: 2866edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd); 2867edd16368SStephen M. Cameron BUG(); 2868edd16368SStephen M. Cameron return; 2869edd16368SStephen M. Cameron } 2870edd16368SStephen M. Cameron } else if (cmd_type == TYPE_MSG) { 2871edd16368SStephen M. Cameron switch (cmd) { 2872edd16368SStephen M. Cameron 2873edd16368SStephen M. Cameron case HPSA_DEVICE_RESET_MSG: 2874edd16368SStephen M. Cameron c->Request.CDBLen = 16; 2875edd16368SStephen M. Cameron c->Request.Type.Type = 1; /* It is a MSG not a CMD */ 2876edd16368SStephen M. Cameron c->Request.Type.Attribute = ATTR_SIMPLE; 2877edd16368SStephen M. Cameron c->Request.Type.Direction = XFER_NONE; 2878edd16368SStephen M. Cameron c->Request.Timeout = 0; /* Don't time out */ 287964670ac8SStephen M. Cameron memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB)); 288064670ac8SStephen M. Cameron c->Request.CDB[0] = cmd; 2881edd16368SStephen M. Cameron c->Request.CDB[1] = 0x03; /* Reset target above */ 2882edd16368SStephen M. Cameron /* If bytes 4-7 are zero, it means reset the */ 2883edd16368SStephen M. Cameron /* LunID device */ 2884edd16368SStephen M. Cameron c->Request.CDB[4] = 0x00; 2885edd16368SStephen M. Cameron c->Request.CDB[5] = 0x00; 2886edd16368SStephen M. Cameron c->Request.CDB[6] = 0x00; 2887edd16368SStephen M. Cameron c->Request.CDB[7] = 0x00; 2888edd16368SStephen M. Cameron break; 2889edd16368SStephen M. Cameron 2890edd16368SStephen M. Cameron default: 2891edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "unknown message type %d\n", 2892edd16368SStephen M. Cameron cmd); 2893edd16368SStephen M. Cameron BUG(); 2894edd16368SStephen M. Cameron } 2895edd16368SStephen M. Cameron } else { 2896edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type); 2897edd16368SStephen M. Cameron BUG(); 2898edd16368SStephen M. Cameron } 2899edd16368SStephen M. Cameron 2900edd16368SStephen M. Cameron switch (c->Request.Type.Direction) { 2901edd16368SStephen M. Cameron case XFER_READ: 2902edd16368SStephen M. Cameron pci_dir = PCI_DMA_FROMDEVICE; 2903edd16368SStephen M. Cameron break; 2904edd16368SStephen M. Cameron case XFER_WRITE: 2905edd16368SStephen M. Cameron pci_dir = PCI_DMA_TODEVICE; 2906edd16368SStephen M. Cameron break; 2907edd16368SStephen M. Cameron case XFER_NONE: 2908edd16368SStephen M. Cameron pci_dir = PCI_DMA_NONE; 2909edd16368SStephen M. Cameron break; 2910edd16368SStephen M. Cameron default: 2911edd16368SStephen M. Cameron pci_dir = PCI_DMA_BIDIRECTIONAL; 2912edd16368SStephen M. Cameron } 2913edd16368SStephen M. Cameron 2914edd16368SStephen M. Cameron hpsa_map_one(h->pdev, c, buff, size, pci_dir); 2915edd16368SStephen M. Cameron 2916edd16368SStephen M. Cameron return; 2917edd16368SStephen M. Cameron } 2918edd16368SStephen M. Cameron 2919edd16368SStephen M. Cameron /* 2920edd16368SStephen M. Cameron * Map (physical) PCI mem into (virtual) kernel space 2921edd16368SStephen M. Cameron */ 2922edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size) 2923edd16368SStephen M. Cameron { 2924edd16368SStephen M. Cameron ulong page_base = ((ulong) base) & PAGE_MASK; 2925edd16368SStephen M. Cameron ulong page_offs = ((ulong) base) - page_base; 2926edd16368SStephen M. Cameron void __iomem *page_remapped = ioremap(page_base, page_offs + size); 2927edd16368SStephen M. Cameron 2928edd16368SStephen M. Cameron return page_remapped ? (page_remapped + page_offs) : NULL; 2929edd16368SStephen M. Cameron } 2930edd16368SStephen M. Cameron 2931edd16368SStephen M. Cameron /* Takes cmds off the submission queue and sends them to the hardware, 2932edd16368SStephen M. Cameron * then puts them on the queue of cmds waiting for completion. 2933edd16368SStephen M. Cameron */ 2934edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h) 2935edd16368SStephen M. Cameron { 2936edd16368SStephen M. Cameron struct CommandList *c; 2937edd16368SStephen M. Cameron 29389e0fc764SStephen M. Cameron while (!list_empty(&h->reqQ)) { 29399e0fc764SStephen M. Cameron c = list_entry(h->reqQ.next, struct CommandList, list); 2940edd16368SStephen M. Cameron /* can't do anything if fifo is full */ 2941edd16368SStephen M. Cameron if ((h->access.fifo_full(h))) { 2942edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "fifo full\n"); 2943edd16368SStephen M. Cameron break; 2944edd16368SStephen M. Cameron } 2945edd16368SStephen M. Cameron 2946edd16368SStephen M. Cameron /* Get the first entry from the Request Q */ 2947edd16368SStephen M. Cameron removeQ(c); 2948edd16368SStephen M. Cameron h->Qdepth--; 2949edd16368SStephen M. Cameron 2950edd16368SStephen M. Cameron /* Tell the controller execute command */ 2951edd16368SStephen M. Cameron h->access.submit_command(h, c); 2952edd16368SStephen M. Cameron 2953edd16368SStephen M. Cameron /* Put job onto the completed Q */ 2954edd16368SStephen M. Cameron addQ(&h->cmpQ, c); 2955edd16368SStephen M. Cameron } 2956edd16368SStephen M. Cameron } 2957edd16368SStephen M. Cameron 2958edd16368SStephen M. Cameron static inline unsigned long get_next_completion(struct ctlr_info *h) 2959edd16368SStephen M. Cameron { 2960edd16368SStephen M. Cameron return h->access.command_completed(h); 2961edd16368SStephen M. Cameron } 2962edd16368SStephen M. Cameron 2963900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h) 2964edd16368SStephen M. Cameron { 2965edd16368SStephen M. Cameron return h->access.intr_pending(h); 2966edd16368SStephen M. Cameron } 2967edd16368SStephen M. Cameron 2968edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h) 2969edd16368SStephen M. Cameron { 297010f66018SStephen M. Cameron return (h->access.intr_pending(h) == 0) || 297110f66018SStephen M. Cameron (h->interrupts_enabled == 0); 2972edd16368SStephen M. Cameron } 2973edd16368SStephen M. Cameron 297401a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index, 297501a02ffcSStephen M. Cameron u32 raw_tag) 2976edd16368SStephen M. Cameron { 2977edd16368SStephen M. Cameron if (unlikely(tag_index >= h->nr_cmds)) { 2978edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag); 2979edd16368SStephen M. Cameron return 1; 2980edd16368SStephen M. Cameron } 2981edd16368SStephen M. Cameron return 0; 2982edd16368SStephen M. Cameron } 2983edd16368SStephen M. Cameron 298401a02ffcSStephen M. Cameron static inline void finish_cmd(struct CommandList *c, u32 raw_tag) 2985edd16368SStephen M. Cameron { 2986edd16368SStephen M. Cameron removeQ(c); 2987edd16368SStephen M. Cameron if (likely(c->cmd_type == CMD_SCSI)) 29881fb011fbSStephen M. Cameron complete_scsi_command(c); 2989edd16368SStephen M. Cameron else if (c->cmd_type == CMD_IOCTL_PEND) 2990edd16368SStephen M. Cameron complete(c->waiting); 2991edd16368SStephen M. Cameron } 2992edd16368SStephen M. Cameron 2993a104c99fSStephen M. Cameron static inline u32 hpsa_tag_contains_index(u32 tag) 2994a104c99fSStephen M. Cameron { 2995a104c99fSStephen M. Cameron return tag & DIRECT_LOOKUP_BIT; 2996a104c99fSStephen M. Cameron } 2997a104c99fSStephen M. Cameron 2998a104c99fSStephen M. Cameron static inline u32 hpsa_tag_to_index(u32 tag) 2999a104c99fSStephen M. Cameron { 3000a104c99fSStephen M. Cameron return tag >> DIRECT_LOOKUP_SHIFT; 3001a104c99fSStephen M. Cameron } 3002a104c99fSStephen M. Cameron 3003a9a3a273SStephen M. Cameron 3004a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag) 3005a104c99fSStephen M. Cameron { 3006a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1) 3007a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03 3008960a30e7SStephen M. Cameron if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant))) 3009a9a3a273SStephen M. Cameron return tag & ~HPSA_SIMPLE_ERROR_BITS; 3010a9a3a273SStephen M. Cameron return tag & ~HPSA_PERF_ERROR_BITS; 3011a104c99fSStephen M. Cameron } 3012a104c99fSStephen M. Cameron 3013303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */ 3014303932fdSDon Brace static inline u32 process_indexed_cmd(struct ctlr_info *h, 3015303932fdSDon Brace u32 raw_tag) 3016303932fdSDon Brace { 3017303932fdSDon Brace u32 tag_index; 3018303932fdSDon Brace struct CommandList *c; 3019303932fdSDon Brace 3020303932fdSDon Brace tag_index = hpsa_tag_to_index(raw_tag); 3021303932fdSDon Brace if (bad_tag(h, tag_index, raw_tag)) 3022303932fdSDon Brace return next_command(h); 3023303932fdSDon Brace c = h->cmd_pool + tag_index; 3024303932fdSDon Brace finish_cmd(c, raw_tag); 3025303932fdSDon Brace return next_command(h); 3026303932fdSDon Brace } 3027303932fdSDon Brace 3028303932fdSDon Brace /* process completion of a non-indexed command */ 3029303932fdSDon Brace static inline u32 process_nonindexed_cmd(struct ctlr_info *h, 3030303932fdSDon Brace u32 raw_tag) 3031303932fdSDon Brace { 3032303932fdSDon Brace u32 tag; 3033303932fdSDon Brace struct CommandList *c = NULL; 3034303932fdSDon Brace 3035a9a3a273SStephen M. Cameron tag = hpsa_tag_discard_error_bits(h, raw_tag); 30369e0fc764SStephen M. Cameron list_for_each_entry(c, &h->cmpQ, list) { 3037303932fdSDon Brace if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) { 3038303932fdSDon Brace finish_cmd(c, raw_tag); 3039303932fdSDon Brace return next_command(h); 3040303932fdSDon Brace } 3041303932fdSDon Brace } 3042303932fdSDon Brace bad_tag(h, h->nr_cmds + 1, raw_tag); 3043303932fdSDon Brace return next_command(h); 3044303932fdSDon Brace } 3045303932fdSDon Brace 304664670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt 304764670ac8SStephen M. Cameron * after a soft reset, even if we turned interrupts off. 304864670ac8SStephen M. Cameron * Only need to check for this in the hpsa_xxx_discard_completions 304964670ac8SStephen M. Cameron * functions. 305064670ac8SStephen M. Cameron */ 305164670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h) 305264670ac8SStephen M. Cameron { 305364670ac8SStephen M. Cameron if (likely(!reset_devices)) 305464670ac8SStephen M. Cameron return 0; 305564670ac8SStephen M. Cameron 305664670ac8SStephen M. Cameron if (likely(h->interrupts_enabled)) 305764670ac8SStephen M. Cameron return 0; 305864670ac8SStephen M. Cameron 305964670ac8SStephen M. Cameron dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled " 306064670ac8SStephen M. Cameron "(known firmware bug.) Ignoring.\n"); 306164670ac8SStephen M. Cameron 306264670ac8SStephen M. Cameron return 1; 306364670ac8SStephen M. Cameron } 306464670ac8SStephen M. Cameron 306564670ac8SStephen M. Cameron static irqreturn_t hpsa_intx_discard_completions(int irq, void *dev_id) 306664670ac8SStephen M. Cameron { 306764670ac8SStephen M. Cameron struct ctlr_info *h = dev_id; 306864670ac8SStephen M. Cameron unsigned long flags; 306964670ac8SStephen M. Cameron u32 raw_tag; 307064670ac8SStephen M. Cameron 307164670ac8SStephen M. Cameron if (ignore_bogus_interrupt(h)) 307264670ac8SStephen M. Cameron return IRQ_NONE; 307364670ac8SStephen M. Cameron 307464670ac8SStephen M. Cameron if (interrupt_not_for_us(h)) 307564670ac8SStephen M. Cameron return IRQ_NONE; 307664670ac8SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 307764670ac8SStephen M. Cameron while (interrupt_pending(h)) { 307864670ac8SStephen M. Cameron raw_tag = get_next_completion(h); 307964670ac8SStephen M. Cameron while (raw_tag != FIFO_EMPTY) 308064670ac8SStephen M. Cameron raw_tag = next_command(h); 308164670ac8SStephen M. Cameron } 308264670ac8SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 308364670ac8SStephen M. Cameron return IRQ_HANDLED; 308464670ac8SStephen M. Cameron } 308564670ac8SStephen M. Cameron 308664670ac8SStephen M. Cameron static irqreturn_t hpsa_msix_discard_completions(int irq, void *dev_id) 308764670ac8SStephen M. Cameron { 308864670ac8SStephen M. Cameron struct ctlr_info *h = dev_id; 308964670ac8SStephen M. Cameron unsigned long flags; 309064670ac8SStephen M. Cameron u32 raw_tag; 309164670ac8SStephen M. Cameron 309264670ac8SStephen M. Cameron if (ignore_bogus_interrupt(h)) 309364670ac8SStephen M. Cameron return IRQ_NONE; 309464670ac8SStephen M. Cameron 309564670ac8SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 309664670ac8SStephen M. Cameron raw_tag = get_next_completion(h); 309764670ac8SStephen M. Cameron while (raw_tag != FIFO_EMPTY) 309864670ac8SStephen M. Cameron raw_tag = next_command(h); 309964670ac8SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 310064670ac8SStephen M. Cameron return IRQ_HANDLED; 310164670ac8SStephen M. Cameron } 310264670ac8SStephen M. Cameron 310310f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id) 3104edd16368SStephen M. Cameron { 3105edd16368SStephen M. Cameron struct ctlr_info *h = dev_id; 3106edd16368SStephen M. Cameron unsigned long flags; 3107303932fdSDon Brace u32 raw_tag; 3108edd16368SStephen M. Cameron 3109edd16368SStephen M. Cameron if (interrupt_not_for_us(h)) 3110edd16368SStephen M. Cameron return IRQ_NONE; 3111edd16368SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 311210f66018SStephen M. Cameron while (interrupt_pending(h)) { 311310f66018SStephen M. Cameron raw_tag = get_next_completion(h); 311410f66018SStephen M. Cameron while (raw_tag != FIFO_EMPTY) { 311510f66018SStephen M. Cameron if (hpsa_tag_contains_index(raw_tag)) 311610f66018SStephen M. Cameron raw_tag = process_indexed_cmd(h, raw_tag); 311710f66018SStephen M. Cameron else 311810f66018SStephen M. Cameron raw_tag = process_nonindexed_cmd(h, raw_tag); 311910f66018SStephen M. Cameron } 312010f66018SStephen M. Cameron } 312110f66018SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 312210f66018SStephen M. Cameron return IRQ_HANDLED; 312310f66018SStephen M. Cameron } 312410f66018SStephen M. Cameron 312510f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id) 312610f66018SStephen M. Cameron { 312710f66018SStephen M. Cameron struct ctlr_info *h = dev_id; 312810f66018SStephen M. Cameron unsigned long flags; 312910f66018SStephen M. Cameron u32 raw_tag; 313010f66018SStephen M. Cameron 313110f66018SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 3132303932fdSDon Brace raw_tag = get_next_completion(h); 3133303932fdSDon Brace while (raw_tag != FIFO_EMPTY) { 3134303932fdSDon Brace if (hpsa_tag_contains_index(raw_tag)) 3135303932fdSDon Brace raw_tag = process_indexed_cmd(h, raw_tag); 3136303932fdSDon Brace else 3137303932fdSDon Brace raw_tag = process_nonindexed_cmd(h, raw_tag); 3138edd16368SStephen M. Cameron } 3139edd16368SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 3140edd16368SStephen M. Cameron return IRQ_HANDLED; 3141edd16368SStephen M. Cameron } 3142edd16368SStephen M. Cameron 3143a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works 3144a9a3a273SStephen M. Cameron * in simple mode, not performant mode due to the tag lookup. 3145a9a3a273SStephen M. Cameron * We only ever use this immediately after a controller reset. 3146a9a3a273SStephen M. Cameron */ 3147edd16368SStephen M. Cameron static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode, 3148edd16368SStephen M. Cameron unsigned char type) 3149edd16368SStephen M. Cameron { 3150edd16368SStephen M. Cameron struct Command { 3151edd16368SStephen M. Cameron struct CommandListHeader CommandHeader; 3152edd16368SStephen M. Cameron struct RequestBlock Request; 3153edd16368SStephen M. Cameron struct ErrDescriptor ErrorDescriptor; 3154edd16368SStephen M. Cameron }; 3155edd16368SStephen M. Cameron struct Command *cmd; 3156edd16368SStephen M. Cameron static const size_t cmd_sz = sizeof(*cmd) + 3157edd16368SStephen M. Cameron sizeof(cmd->ErrorDescriptor); 3158edd16368SStephen M. Cameron dma_addr_t paddr64; 3159edd16368SStephen M. Cameron uint32_t paddr32, tag; 3160edd16368SStephen M. Cameron void __iomem *vaddr; 3161edd16368SStephen M. Cameron int i, err; 3162edd16368SStephen M. Cameron 3163edd16368SStephen M. Cameron vaddr = pci_ioremap_bar(pdev, 0); 3164edd16368SStephen M. Cameron if (vaddr == NULL) 3165edd16368SStephen M. Cameron return -ENOMEM; 3166edd16368SStephen M. Cameron 3167edd16368SStephen M. Cameron /* The Inbound Post Queue only accepts 32-bit physical addresses for the 3168edd16368SStephen M. Cameron * CCISS commands, so they must be allocated from the lower 4GiB of 3169edd16368SStephen M. Cameron * memory. 3170edd16368SStephen M. Cameron */ 3171edd16368SStephen M. Cameron err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 3172edd16368SStephen M. Cameron if (err) { 3173edd16368SStephen M. Cameron iounmap(vaddr); 3174edd16368SStephen M. Cameron return -ENOMEM; 3175edd16368SStephen M. Cameron } 3176edd16368SStephen M. Cameron 3177edd16368SStephen M. Cameron cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64); 3178edd16368SStephen M. Cameron if (cmd == NULL) { 3179edd16368SStephen M. Cameron iounmap(vaddr); 3180edd16368SStephen M. Cameron return -ENOMEM; 3181edd16368SStephen M. Cameron } 3182edd16368SStephen M. Cameron 3183edd16368SStephen M. Cameron /* This must fit, because of the 32-bit consistent DMA mask. Also, 3184edd16368SStephen M. Cameron * although there's no guarantee, we assume that the address is at 3185edd16368SStephen M. Cameron * least 4-byte aligned (most likely, it's page-aligned). 3186edd16368SStephen M. Cameron */ 3187edd16368SStephen M. Cameron paddr32 = paddr64; 3188edd16368SStephen M. Cameron 3189edd16368SStephen M. Cameron cmd->CommandHeader.ReplyQueue = 0; 3190edd16368SStephen M. Cameron cmd->CommandHeader.SGList = 0; 3191edd16368SStephen M. Cameron cmd->CommandHeader.SGTotal = 0; 3192edd16368SStephen M. Cameron cmd->CommandHeader.Tag.lower = paddr32; 3193edd16368SStephen M. Cameron cmd->CommandHeader.Tag.upper = 0; 3194edd16368SStephen M. Cameron memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8); 3195edd16368SStephen M. Cameron 3196edd16368SStephen M. Cameron cmd->Request.CDBLen = 16; 3197edd16368SStephen M. Cameron cmd->Request.Type.Type = TYPE_MSG; 3198edd16368SStephen M. Cameron cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE; 3199edd16368SStephen M. Cameron cmd->Request.Type.Direction = XFER_NONE; 3200edd16368SStephen M. Cameron cmd->Request.Timeout = 0; /* Don't time out */ 3201edd16368SStephen M. Cameron cmd->Request.CDB[0] = opcode; 3202edd16368SStephen M. Cameron cmd->Request.CDB[1] = type; 3203edd16368SStephen M. Cameron memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */ 3204edd16368SStephen M. Cameron cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd); 3205edd16368SStephen M. Cameron cmd->ErrorDescriptor.Addr.upper = 0; 3206edd16368SStephen M. Cameron cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo); 3207edd16368SStephen M. Cameron 3208edd16368SStephen M. Cameron writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET); 3209edd16368SStephen M. Cameron 3210edd16368SStephen M. Cameron for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) { 3211edd16368SStephen M. Cameron tag = readl(vaddr + SA5_REPLY_PORT_OFFSET); 3212a9a3a273SStephen M. Cameron if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32) 3213edd16368SStephen M. Cameron break; 3214edd16368SStephen M. Cameron msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS); 3215edd16368SStephen M. Cameron } 3216edd16368SStephen M. Cameron 3217edd16368SStephen M. Cameron iounmap(vaddr); 3218edd16368SStephen M. Cameron 3219edd16368SStephen M. Cameron /* we leak the DMA buffer here ... no choice since the controller could 3220edd16368SStephen M. Cameron * still complete the command. 3221edd16368SStephen M. Cameron */ 3222edd16368SStephen M. Cameron if (i == HPSA_MSG_SEND_RETRY_LIMIT) { 3223edd16368SStephen M. Cameron dev_err(&pdev->dev, "controller message %02x:%02x timed out\n", 3224edd16368SStephen M. Cameron opcode, type); 3225edd16368SStephen M. Cameron return -ETIMEDOUT; 3226edd16368SStephen M. Cameron } 3227edd16368SStephen M. Cameron 3228edd16368SStephen M. Cameron pci_free_consistent(pdev, cmd_sz, cmd, paddr64); 3229edd16368SStephen M. Cameron 3230edd16368SStephen M. Cameron if (tag & HPSA_ERROR_BIT) { 3231edd16368SStephen M. Cameron dev_err(&pdev->dev, "controller message %02x:%02x failed\n", 3232edd16368SStephen M. Cameron opcode, type); 3233edd16368SStephen M. Cameron return -EIO; 3234edd16368SStephen M. Cameron } 3235edd16368SStephen M. Cameron 3236edd16368SStephen M. Cameron dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n", 3237edd16368SStephen M. Cameron opcode, type); 3238edd16368SStephen M. Cameron return 0; 3239edd16368SStephen M. Cameron } 3240edd16368SStephen M. Cameron 3241edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0) 3242edd16368SStephen M. Cameron 32431df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev, 3244cf0b08d0SStephen M. Cameron void * __iomem vaddr, u32 use_doorbell) 3245edd16368SStephen M. Cameron { 32461df8552aSStephen M. Cameron u16 pmcsr; 32471df8552aSStephen M. Cameron int pos; 3248edd16368SStephen M. Cameron 32491df8552aSStephen M. Cameron if (use_doorbell) { 32501df8552aSStephen M. Cameron /* For everything after the P600, the PCI power state method 32511df8552aSStephen M. Cameron * of resetting the controller doesn't work, so we have this 32521df8552aSStephen M. Cameron * other way using the doorbell register. 3253edd16368SStephen M. Cameron */ 32541df8552aSStephen M. Cameron dev_info(&pdev->dev, "using doorbell to reset controller\n"); 3255cf0b08d0SStephen M. Cameron writel(use_doorbell, vaddr + SA5_DOORBELL); 32561df8552aSStephen M. Cameron } else { /* Try to do it the PCI power state way */ 3257edd16368SStephen M. Cameron 3258edd16368SStephen M. Cameron /* Quoting from the Open CISS Specification: "The Power 3259edd16368SStephen M. Cameron * Management Control/Status Register (CSR) controls the power 3260edd16368SStephen M. Cameron * state of the device. The normal operating state is D0, 3261edd16368SStephen M. Cameron * CSR=00h. The software off state is D3, CSR=03h. To reset 32621df8552aSStephen M. Cameron * the controller, place the interface device in D3 then to D0, 32631df8552aSStephen M. Cameron * this causes a secondary PCI reset which will reset the 32641df8552aSStephen M. Cameron * controller." */ 3265edd16368SStephen M. Cameron 32661df8552aSStephen M. Cameron pos = pci_find_capability(pdev, PCI_CAP_ID_PM); 32671df8552aSStephen M. Cameron if (pos == 0) { 32681df8552aSStephen M. Cameron dev_err(&pdev->dev, 32691df8552aSStephen M. Cameron "hpsa_reset_controller: " 32701df8552aSStephen M. Cameron "PCI PM not supported\n"); 32711df8552aSStephen M. Cameron return -ENODEV; 32721df8552aSStephen M. Cameron } 32731df8552aSStephen M. Cameron dev_info(&pdev->dev, "using PCI PM to reset controller\n"); 3274edd16368SStephen M. Cameron /* enter the D3hot power management state */ 3275edd16368SStephen M. Cameron pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr); 3276edd16368SStephen M. Cameron pmcsr &= ~PCI_PM_CTRL_STATE_MASK; 3277edd16368SStephen M. Cameron pmcsr |= PCI_D3hot; 3278edd16368SStephen M. Cameron pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr); 3279edd16368SStephen M. Cameron 3280edd16368SStephen M. Cameron msleep(500); 3281edd16368SStephen M. Cameron 3282edd16368SStephen M. Cameron /* enter the D0 power management state */ 3283edd16368SStephen M. Cameron pmcsr &= ~PCI_PM_CTRL_STATE_MASK; 3284edd16368SStephen M. Cameron pmcsr |= PCI_D0; 3285edd16368SStephen M. Cameron pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr); 32861df8552aSStephen M. Cameron } 32871df8552aSStephen M. Cameron return 0; 32881df8552aSStephen M. Cameron } 32891df8552aSStephen M. Cameron 3290580ada3cSStephen M. Cameron static __devinit void init_driver_version(char *driver_version, int len) 3291580ada3cSStephen M. Cameron { 3292580ada3cSStephen M. Cameron memset(driver_version, 0, len); 3293580ada3cSStephen M. Cameron strncpy(driver_version, "hpsa " HPSA_DRIVER_VERSION, len - 1); 3294580ada3cSStephen M. Cameron } 3295580ada3cSStephen M. Cameron 3296580ada3cSStephen M. Cameron static __devinit int write_driver_ver_to_cfgtable( 3297580ada3cSStephen M. Cameron struct CfgTable __iomem *cfgtable) 3298580ada3cSStephen M. Cameron { 3299580ada3cSStephen M. Cameron char *driver_version; 3300580ada3cSStephen M. Cameron int i, size = sizeof(cfgtable->driver_version); 3301580ada3cSStephen M. Cameron 3302580ada3cSStephen M. Cameron driver_version = kmalloc(size, GFP_KERNEL); 3303580ada3cSStephen M. Cameron if (!driver_version) 3304580ada3cSStephen M. Cameron return -ENOMEM; 3305580ada3cSStephen M. Cameron 3306580ada3cSStephen M. Cameron init_driver_version(driver_version, size); 3307580ada3cSStephen M. Cameron for (i = 0; i < size; i++) 3308580ada3cSStephen M. Cameron writeb(driver_version[i], &cfgtable->driver_version[i]); 3309580ada3cSStephen M. Cameron kfree(driver_version); 3310580ada3cSStephen M. Cameron return 0; 3311580ada3cSStephen M. Cameron } 3312580ada3cSStephen M. Cameron 3313580ada3cSStephen M. Cameron static __devinit void read_driver_ver_from_cfgtable( 3314580ada3cSStephen M. Cameron struct CfgTable __iomem *cfgtable, unsigned char *driver_ver) 3315580ada3cSStephen M. Cameron { 3316580ada3cSStephen M. Cameron int i; 3317580ada3cSStephen M. Cameron 3318580ada3cSStephen M. Cameron for (i = 0; i < sizeof(cfgtable->driver_version); i++) 3319580ada3cSStephen M. Cameron driver_ver[i] = readb(&cfgtable->driver_version[i]); 3320580ada3cSStephen M. Cameron } 3321580ada3cSStephen M. Cameron 3322580ada3cSStephen M. Cameron static __devinit int controller_reset_failed( 3323580ada3cSStephen M. Cameron struct CfgTable __iomem *cfgtable) 3324580ada3cSStephen M. Cameron { 3325580ada3cSStephen M. Cameron 3326580ada3cSStephen M. Cameron char *driver_ver, *old_driver_ver; 3327580ada3cSStephen M. Cameron int rc, size = sizeof(cfgtable->driver_version); 3328580ada3cSStephen M. Cameron 3329580ada3cSStephen M. Cameron old_driver_ver = kmalloc(2 * size, GFP_KERNEL); 3330580ada3cSStephen M. Cameron if (!old_driver_ver) 3331580ada3cSStephen M. Cameron return -ENOMEM; 3332580ada3cSStephen M. Cameron driver_ver = old_driver_ver + size; 3333580ada3cSStephen M. Cameron 3334580ada3cSStephen M. Cameron /* After a reset, the 32 bytes of "driver version" in the cfgtable 3335580ada3cSStephen M. Cameron * should have been changed, otherwise we know the reset failed. 3336580ada3cSStephen M. Cameron */ 3337580ada3cSStephen M. Cameron init_driver_version(old_driver_ver, size); 3338580ada3cSStephen M. Cameron read_driver_ver_from_cfgtable(cfgtable, driver_ver); 3339580ada3cSStephen M. Cameron rc = !memcmp(driver_ver, old_driver_ver, size); 3340580ada3cSStephen M. Cameron kfree(old_driver_ver); 3341580ada3cSStephen M. Cameron return rc; 3342580ada3cSStephen M. Cameron } 33431df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management 33441df8552aSStephen M. Cameron * states or the using the doorbell register. 33451df8552aSStephen M. Cameron */ 33461df8552aSStephen M. Cameron static __devinit int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev) 33471df8552aSStephen M. Cameron { 33481df8552aSStephen M. Cameron u64 cfg_offset; 33491df8552aSStephen M. Cameron u32 cfg_base_addr; 33501df8552aSStephen M. Cameron u64 cfg_base_addr_index; 33511df8552aSStephen M. Cameron void __iomem *vaddr; 33521df8552aSStephen M. Cameron unsigned long paddr; 3353580ada3cSStephen M. Cameron u32 misc_fw_support; 3354270d05deSStephen M. Cameron int rc; 33551df8552aSStephen M. Cameron struct CfgTable __iomem *cfgtable; 3356cf0b08d0SStephen M. Cameron u32 use_doorbell; 335718867659SStephen M. Cameron u32 board_id; 3358270d05deSStephen M. Cameron u16 command_register; 33591df8552aSStephen M. Cameron 33601df8552aSStephen M. Cameron /* For controllers as old as the P600, this is very nearly 33611df8552aSStephen M. Cameron * the same thing as 33621df8552aSStephen M. Cameron * 33631df8552aSStephen M. Cameron * pci_save_state(pci_dev); 33641df8552aSStephen M. Cameron * pci_set_power_state(pci_dev, PCI_D3hot); 33651df8552aSStephen M. Cameron * pci_set_power_state(pci_dev, PCI_D0); 33661df8552aSStephen M. Cameron * pci_restore_state(pci_dev); 33671df8552aSStephen M. Cameron * 33681df8552aSStephen M. Cameron * For controllers newer than the P600, the pci power state 33691df8552aSStephen M. Cameron * method of resetting doesn't work so we have another way 33701df8552aSStephen M. Cameron * using the doorbell register. 33711df8552aSStephen M. Cameron */ 337218867659SStephen M. Cameron 337325c1e56aSStephen M. Cameron rc = hpsa_lookup_board_id(pdev, &board_id); 337446380786SStephen M. Cameron if (rc < 0 || !ctlr_is_resettable(board_id)) { 337525c1e56aSStephen M. Cameron dev_warn(&pdev->dev, "Not resetting device.\n"); 337625c1e56aSStephen M. Cameron return -ENODEV; 337725c1e56aSStephen M. Cameron } 337846380786SStephen M. Cameron 337946380786SStephen M. Cameron /* if controller is soft- but not hard resettable... */ 338046380786SStephen M. Cameron if (!ctlr_is_hard_resettable(board_id)) 338146380786SStephen M. Cameron return -ENOTSUPP; /* try soft reset later. */ 338218867659SStephen M. Cameron 3383270d05deSStephen M. Cameron /* Save the PCI command register */ 3384270d05deSStephen M. Cameron pci_read_config_word(pdev, 4, &command_register); 3385270d05deSStephen M. Cameron /* Turn the board off. This is so that later pci_restore_state() 3386270d05deSStephen M. Cameron * won't turn the board on before the rest of config space is ready. 3387270d05deSStephen M. Cameron */ 3388270d05deSStephen M. Cameron pci_disable_device(pdev); 3389270d05deSStephen M. Cameron pci_save_state(pdev); 33901df8552aSStephen M. Cameron 33911df8552aSStephen M. Cameron /* find the first memory BAR, so we can find the cfg table */ 33921df8552aSStephen M. Cameron rc = hpsa_pci_find_memory_BAR(pdev, &paddr); 33931df8552aSStephen M. Cameron if (rc) 33941df8552aSStephen M. Cameron return rc; 33951df8552aSStephen M. Cameron vaddr = remap_pci_mem(paddr, 0x250); 33961df8552aSStephen M. Cameron if (!vaddr) 33971df8552aSStephen M. Cameron return -ENOMEM; 33981df8552aSStephen M. Cameron 33991df8552aSStephen M. Cameron /* find cfgtable in order to check if reset via doorbell is supported */ 34001df8552aSStephen M. Cameron rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr, 34011df8552aSStephen M. Cameron &cfg_base_addr_index, &cfg_offset); 34021df8552aSStephen M. Cameron if (rc) 34031df8552aSStephen M. Cameron goto unmap_vaddr; 34041df8552aSStephen M. Cameron cfgtable = remap_pci_mem(pci_resource_start(pdev, 34051df8552aSStephen M. Cameron cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable)); 34061df8552aSStephen M. Cameron if (!cfgtable) { 34071df8552aSStephen M. Cameron rc = -ENOMEM; 34081df8552aSStephen M. Cameron goto unmap_vaddr; 34091df8552aSStephen M. Cameron } 3410580ada3cSStephen M. Cameron rc = write_driver_ver_to_cfgtable(cfgtable); 3411580ada3cSStephen M. Cameron if (rc) 3412580ada3cSStephen M. Cameron goto unmap_vaddr; 34131df8552aSStephen M. Cameron 3414cf0b08d0SStephen M. Cameron /* If reset via doorbell register is supported, use that. 3415cf0b08d0SStephen M. Cameron * There are two such methods. Favor the newest method. 3416cf0b08d0SStephen M. Cameron */ 34171df8552aSStephen M. Cameron misc_fw_support = readl(&cfgtable->misc_fw_support); 3418cf0b08d0SStephen M. Cameron use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2; 3419cf0b08d0SStephen M. Cameron if (use_doorbell) { 3420cf0b08d0SStephen M. Cameron use_doorbell = DOORBELL_CTLR_RESET2; 3421cf0b08d0SStephen M. Cameron } else { 34221df8552aSStephen M. Cameron use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET; 3423cf0b08d0SStephen M. Cameron if (use_doorbell) { 3424cf0b08d0SStephen M. Cameron dev_warn(&pdev->dev, "Controller claims that " 3425cf0b08d0SStephen M. Cameron "'Bit 2 doorbell reset' is " 3426cf0b08d0SStephen M. Cameron "supported, but not 'bit 5 doorbell reset'. " 3427cf0b08d0SStephen M. Cameron "Firmware update is recommended.\n"); 342864670ac8SStephen M. Cameron rc = -ENOTSUPP; /* try soft reset */ 3429cf0b08d0SStephen M. Cameron goto unmap_cfgtable; 3430cf0b08d0SStephen M. Cameron } 3431cf0b08d0SStephen M. Cameron } 34321df8552aSStephen M. Cameron 34331df8552aSStephen M. Cameron rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell); 34341df8552aSStephen M. Cameron if (rc) 34351df8552aSStephen M. Cameron goto unmap_cfgtable; 3436edd16368SStephen M. Cameron 3437270d05deSStephen M. Cameron pci_restore_state(pdev); 3438270d05deSStephen M. Cameron rc = pci_enable_device(pdev); 3439270d05deSStephen M. Cameron if (rc) { 3440270d05deSStephen M. Cameron dev_warn(&pdev->dev, "failed to enable device.\n"); 3441270d05deSStephen M. Cameron goto unmap_cfgtable; 3442edd16368SStephen M. Cameron } 3443270d05deSStephen M. Cameron pci_write_config_word(pdev, 4, command_register); 3444edd16368SStephen M. Cameron 34451df8552aSStephen M. Cameron /* Some devices (notably the HP Smart Array 5i Controller) 34461df8552aSStephen M. Cameron need a little pause here */ 34471df8552aSStephen M. Cameron msleep(HPSA_POST_RESET_PAUSE_MSECS); 34481df8552aSStephen M. Cameron 3449fe5389c8SStephen M. Cameron /* Wait for board to become not ready, then ready. */ 34502b870cb3SStephen M. Cameron dev_info(&pdev->dev, "Waiting for board to reset.\n"); 3451fe5389c8SStephen M. Cameron rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_NOT_READY); 345264670ac8SStephen M. Cameron if (rc) { 3453fe5389c8SStephen M. Cameron dev_warn(&pdev->dev, 345464670ac8SStephen M. Cameron "failed waiting for board to reset." 345564670ac8SStephen M. Cameron " Will try soft reset.\n"); 345664670ac8SStephen M. Cameron rc = -ENOTSUPP; /* Not expected, but try soft reset later */ 345764670ac8SStephen M. Cameron goto unmap_cfgtable; 345864670ac8SStephen M. Cameron } 3459fe5389c8SStephen M. Cameron rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY); 3460fe5389c8SStephen M. Cameron if (rc) { 3461fe5389c8SStephen M. Cameron dev_warn(&pdev->dev, 346264670ac8SStephen M. Cameron "failed waiting for board to become ready " 346364670ac8SStephen M. Cameron "after hard reset\n"); 3464fe5389c8SStephen M. Cameron goto unmap_cfgtable; 3465fe5389c8SStephen M. Cameron } 3466fe5389c8SStephen M. Cameron 3467580ada3cSStephen M. Cameron rc = controller_reset_failed(vaddr); 3468580ada3cSStephen M. Cameron if (rc < 0) 3469580ada3cSStephen M. Cameron goto unmap_cfgtable; 3470580ada3cSStephen M. Cameron if (rc) { 347164670ac8SStephen M. Cameron dev_warn(&pdev->dev, "Unable to successfully reset " 347264670ac8SStephen M. Cameron "controller. Will try soft reset.\n"); 347364670ac8SStephen M. Cameron rc = -ENOTSUPP; 3474580ada3cSStephen M. Cameron } else { 347564670ac8SStephen M. Cameron dev_info(&pdev->dev, "board ready after hard reset.\n"); 34761df8552aSStephen M. Cameron } 34771df8552aSStephen M. Cameron 34781df8552aSStephen M. Cameron unmap_cfgtable: 34791df8552aSStephen M. Cameron iounmap(cfgtable); 34801df8552aSStephen M. Cameron 34811df8552aSStephen M. Cameron unmap_vaddr: 34821df8552aSStephen M. Cameron iounmap(vaddr); 34831df8552aSStephen M. Cameron return rc; 3484edd16368SStephen M. Cameron } 3485edd16368SStephen M. Cameron 3486edd16368SStephen M. Cameron /* 3487edd16368SStephen M. Cameron * We cannot read the structure directly, for portability we must use 3488edd16368SStephen M. Cameron * the io functions. 3489edd16368SStephen M. Cameron * This is for debug only. 3490edd16368SStephen M. Cameron */ 3491edd16368SStephen M. Cameron static void print_cfg_table(struct device *dev, struct CfgTable *tb) 3492edd16368SStephen M. Cameron { 349358f8665cSStephen M. Cameron #ifdef HPSA_DEBUG 3494edd16368SStephen M. Cameron int i; 3495edd16368SStephen M. Cameron char temp_name[17]; 3496edd16368SStephen M. Cameron 3497edd16368SStephen M. Cameron dev_info(dev, "Controller Configuration information\n"); 3498edd16368SStephen M. Cameron dev_info(dev, "------------------------------------\n"); 3499edd16368SStephen M. Cameron for (i = 0; i < 4; i++) 3500edd16368SStephen M. Cameron temp_name[i] = readb(&(tb->Signature[i])); 3501edd16368SStephen M. Cameron temp_name[4] = '\0'; 3502edd16368SStephen M. Cameron dev_info(dev, " Signature = %s\n", temp_name); 3503edd16368SStephen M. Cameron dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence))); 3504edd16368SStephen M. Cameron dev_info(dev, " Transport methods supported = 0x%x\n", 3505edd16368SStephen M. Cameron readl(&(tb->TransportSupport))); 3506edd16368SStephen M. Cameron dev_info(dev, " Transport methods active = 0x%x\n", 3507edd16368SStephen M. Cameron readl(&(tb->TransportActive))); 3508edd16368SStephen M. Cameron dev_info(dev, " Requested transport Method = 0x%x\n", 3509edd16368SStephen M. Cameron readl(&(tb->HostWrite.TransportRequest))); 3510edd16368SStephen M. Cameron dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n", 3511edd16368SStephen M. Cameron readl(&(tb->HostWrite.CoalIntDelay))); 3512edd16368SStephen M. Cameron dev_info(dev, " Coalesce Interrupt Count = 0x%x\n", 3513edd16368SStephen M. Cameron readl(&(tb->HostWrite.CoalIntCount))); 3514edd16368SStephen M. Cameron dev_info(dev, " Max outstanding commands = 0x%d\n", 3515edd16368SStephen M. Cameron readl(&(tb->CmdsOutMax))); 3516edd16368SStephen M. Cameron dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes))); 3517edd16368SStephen M. Cameron for (i = 0; i < 16; i++) 3518edd16368SStephen M. Cameron temp_name[i] = readb(&(tb->ServerName[i])); 3519edd16368SStephen M. Cameron temp_name[16] = '\0'; 3520edd16368SStephen M. Cameron dev_info(dev, " Server Name = %s\n", temp_name); 3521edd16368SStephen M. Cameron dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n", 3522edd16368SStephen M. Cameron readl(&(tb->HeartBeat))); 3523edd16368SStephen M. Cameron #endif /* HPSA_DEBUG */ 352458f8665cSStephen M. Cameron } 3525edd16368SStephen M. Cameron 3526edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr) 3527edd16368SStephen M. Cameron { 3528edd16368SStephen M. Cameron int i, offset, mem_type, bar_type; 3529edd16368SStephen M. Cameron 3530edd16368SStephen M. Cameron if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */ 3531edd16368SStephen M. Cameron return 0; 3532edd16368SStephen M. Cameron offset = 0; 3533edd16368SStephen M. Cameron for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 3534edd16368SStephen M. Cameron bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE; 3535edd16368SStephen M. Cameron if (bar_type == PCI_BASE_ADDRESS_SPACE_IO) 3536edd16368SStephen M. Cameron offset += 4; 3537edd16368SStephen M. Cameron else { 3538edd16368SStephen M. Cameron mem_type = pci_resource_flags(pdev, i) & 3539edd16368SStephen M. Cameron PCI_BASE_ADDRESS_MEM_TYPE_MASK; 3540edd16368SStephen M. Cameron switch (mem_type) { 3541edd16368SStephen M. Cameron case PCI_BASE_ADDRESS_MEM_TYPE_32: 3542edd16368SStephen M. Cameron case PCI_BASE_ADDRESS_MEM_TYPE_1M: 3543edd16368SStephen M. Cameron offset += 4; /* 32 bit */ 3544edd16368SStephen M. Cameron break; 3545edd16368SStephen M. Cameron case PCI_BASE_ADDRESS_MEM_TYPE_64: 3546edd16368SStephen M. Cameron offset += 8; 3547edd16368SStephen M. Cameron break; 3548edd16368SStephen M. Cameron default: /* reserved in PCI 2.2 */ 3549edd16368SStephen M. Cameron dev_warn(&pdev->dev, 3550edd16368SStephen M. Cameron "base address is invalid\n"); 3551edd16368SStephen M. Cameron return -1; 3552edd16368SStephen M. Cameron break; 3553edd16368SStephen M. Cameron } 3554edd16368SStephen M. Cameron } 3555edd16368SStephen M. Cameron if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0) 3556edd16368SStephen M. Cameron return i + 1; 3557edd16368SStephen M. Cameron } 3558edd16368SStephen M. Cameron return -1; 3559edd16368SStephen M. Cameron } 3560edd16368SStephen M. Cameron 3561edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on 3562edd16368SStephen M. Cameron * controllers that are capable. If not, we use IO-APIC mode. 3563edd16368SStephen M. Cameron */ 3564edd16368SStephen M. Cameron 35656b3f4c52SStephen M. Cameron static void __devinit hpsa_interrupt_mode(struct ctlr_info *h) 3566edd16368SStephen M. Cameron { 3567edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI 3568edd16368SStephen M. Cameron int err; 3569edd16368SStephen M. Cameron struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1}, 3570edd16368SStephen M. Cameron {0, 2}, {0, 3} 3571edd16368SStephen M. Cameron }; 3572edd16368SStephen M. Cameron 3573edd16368SStephen M. Cameron /* Some boards advertise MSI but don't really support it */ 35746b3f4c52SStephen M. Cameron if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) || 35756b3f4c52SStephen M. Cameron (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11)) 3576edd16368SStephen M. Cameron goto default_int_mode; 357755c06c71SStephen M. Cameron if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) { 357855c06c71SStephen M. Cameron dev_info(&h->pdev->dev, "MSIX\n"); 357955c06c71SStephen M. Cameron err = pci_enable_msix(h->pdev, hpsa_msix_entries, 4); 3580edd16368SStephen M. Cameron if (!err) { 3581edd16368SStephen M. Cameron h->intr[0] = hpsa_msix_entries[0].vector; 3582edd16368SStephen M. Cameron h->intr[1] = hpsa_msix_entries[1].vector; 3583edd16368SStephen M. Cameron h->intr[2] = hpsa_msix_entries[2].vector; 3584edd16368SStephen M. Cameron h->intr[3] = hpsa_msix_entries[3].vector; 3585edd16368SStephen M. Cameron h->msix_vector = 1; 3586edd16368SStephen M. Cameron return; 3587edd16368SStephen M. Cameron } 3588edd16368SStephen M. Cameron if (err > 0) { 358955c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "only %d MSI-X vectors " 3590edd16368SStephen M. Cameron "available\n", err); 3591edd16368SStephen M. Cameron goto default_int_mode; 3592edd16368SStephen M. Cameron } else { 359355c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", 3594edd16368SStephen M. Cameron err); 3595edd16368SStephen M. Cameron goto default_int_mode; 3596edd16368SStephen M. Cameron } 3597edd16368SStephen M. Cameron } 359855c06c71SStephen M. Cameron if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) { 359955c06c71SStephen M. Cameron dev_info(&h->pdev->dev, "MSI\n"); 360055c06c71SStephen M. Cameron if (!pci_enable_msi(h->pdev)) 3601edd16368SStephen M. Cameron h->msi_vector = 1; 3602edd16368SStephen M. Cameron else 360355c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "MSI init failed\n"); 3604edd16368SStephen M. Cameron } 3605edd16368SStephen M. Cameron default_int_mode: 3606edd16368SStephen M. Cameron #endif /* CONFIG_PCI_MSI */ 3607edd16368SStephen M. Cameron /* if we get here we're going to use the default interrupt mode */ 3608a9a3a273SStephen M. Cameron h->intr[h->intr_mode] = h->pdev->irq; 3609edd16368SStephen M. Cameron } 3610edd16368SStephen M. Cameron 3611e5c880d1SStephen M. Cameron static int __devinit hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id) 3612e5c880d1SStephen M. Cameron { 3613e5c880d1SStephen M. Cameron int i; 3614e5c880d1SStephen M. Cameron u32 subsystem_vendor_id, subsystem_device_id; 3615e5c880d1SStephen M. Cameron 3616e5c880d1SStephen M. Cameron subsystem_vendor_id = pdev->subsystem_vendor; 3617e5c880d1SStephen M. Cameron subsystem_device_id = pdev->subsystem_device; 3618e5c880d1SStephen M. Cameron *board_id = ((subsystem_device_id << 16) & 0xffff0000) | 3619e5c880d1SStephen M. Cameron subsystem_vendor_id; 3620e5c880d1SStephen M. Cameron 3621e5c880d1SStephen M. Cameron for (i = 0; i < ARRAY_SIZE(products); i++) 3622e5c880d1SStephen M. Cameron if (*board_id == products[i].board_id) 3623e5c880d1SStephen M. Cameron return i; 3624e5c880d1SStephen M. Cameron 36256798cc0aSStephen M. Cameron if ((subsystem_vendor_id != PCI_VENDOR_ID_HP && 36266798cc0aSStephen M. Cameron subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) || 36276798cc0aSStephen M. Cameron !hpsa_allow_any) { 3628e5c880d1SStephen M. Cameron dev_warn(&pdev->dev, "unrecognized board ID: " 3629e5c880d1SStephen M. Cameron "0x%08x, ignoring.\n", *board_id); 3630e5c880d1SStephen M. Cameron return -ENODEV; 3631e5c880d1SStephen M. Cameron } 3632e5c880d1SStephen M. Cameron return ARRAY_SIZE(products) - 1; /* generic unknown smart array */ 3633e5c880d1SStephen M. Cameron } 3634e5c880d1SStephen M. Cameron 363585bdbabbSStephen M. Cameron static inline bool hpsa_board_disabled(struct pci_dev *pdev) 363685bdbabbSStephen M. Cameron { 363785bdbabbSStephen M. Cameron u16 command; 363885bdbabbSStephen M. Cameron 363985bdbabbSStephen M. Cameron (void) pci_read_config_word(pdev, PCI_COMMAND, &command); 364085bdbabbSStephen M. Cameron return ((command & PCI_COMMAND_MEMORY) == 0); 364185bdbabbSStephen M. Cameron } 364285bdbabbSStephen M. Cameron 364312d2cd47SStephen M. Cameron static int __devinit hpsa_pci_find_memory_BAR(struct pci_dev *pdev, 36443a7774ceSStephen M. Cameron unsigned long *memory_bar) 36453a7774ceSStephen M. Cameron { 36463a7774ceSStephen M. Cameron int i; 36473a7774ceSStephen M. Cameron 36483a7774ceSStephen M. Cameron for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) 364912d2cd47SStephen M. Cameron if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) { 36503a7774ceSStephen M. Cameron /* addressing mode bits already removed */ 365112d2cd47SStephen M. Cameron *memory_bar = pci_resource_start(pdev, i); 365212d2cd47SStephen M. Cameron dev_dbg(&pdev->dev, "memory BAR = %lx\n", 36533a7774ceSStephen M. Cameron *memory_bar); 36543a7774ceSStephen M. Cameron return 0; 36553a7774ceSStephen M. Cameron } 365612d2cd47SStephen M. Cameron dev_warn(&pdev->dev, "no memory BAR found\n"); 36573a7774ceSStephen M. Cameron return -ENODEV; 36583a7774ceSStephen M. Cameron } 36593a7774ceSStephen M. Cameron 3660fe5389c8SStephen M. Cameron static int __devinit hpsa_wait_for_board_state(struct pci_dev *pdev, 3661fe5389c8SStephen M. Cameron void __iomem *vaddr, int wait_for_ready) 36622c4c8c8bSStephen M. Cameron { 3663fe5389c8SStephen M. Cameron int i, iterations; 36642c4c8c8bSStephen M. Cameron u32 scratchpad; 3665fe5389c8SStephen M. Cameron if (wait_for_ready) 3666fe5389c8SStephen M. Cameron iterations = HPSA_BOARD_READY_ITERATIONS; 3667fe5389c8SStephen M. Cameron else 3668fe5389c8SStephen M. Cameron iterations = HPSA_BOARD_NOT_READY_ITERATIONS; 36692c4c8c8bSStephen M. Cameron 3670fe5389c8SStephen M. Cameron for (i = 0; i < iterations; i++) { 3671fe5389c8SStephen M. Cameron scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET); 3672fe5389c8SStephen M. Cameron if (wait_for_ready) { 36732c4c8c8bSStephen M. Cameron if (scratchpad == HPSA_FIRMWARE_READY) 36742c4c8c8bSStephen M. Cameron return 0; 3675fe5389c8SStephen M. Cameron } else { 3676fe5389c8SStephen M. Cameron if (scratchpad != HPSA_FIRMWARE_READY) 3677fe5389c8SStephen M. Cameron return 0; 3678fe5389c8SStephen M. Cameron } 36792c4c8c8bSStephen M. Cameron msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS); 36802c4c8c8bSStephen M. Cameron } 3681fe5389c8SStephen M. Cameron dev_warn(&pdev->dev, "board not ready, timed out.\n"); 36822c4c8c8bSStephen M. Cameron return -ENODEV; 36832c4c8c8bSStephen M. Cameron } 36842c4c8c8bSStephen M. Cameron 3685a51fd47fSStephen M. Cameron static int __devinit hpsa_find_cfg_addrs(struct pci_dev *pdev, 3686a51fd47fSStephen M. Cameron void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index, 3687a51fd47fSStephen M. Cameron u64 *cfg_offset) 3688a51fd47fSStephen M. Cameron { 3689a51fd47fSStephen M. Cameron *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET); 3690a51fd47fSStephen M. Cameron *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET); 3691a51fd47fSStephen M. Cameron *cfg_base_addr &= (u32) 0x0000ffff; 3692a51fd47fSStephen M. Cameron *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr); 3693a51fd47fSStephen M. Cameron if (*cfg_base_addr_index == -1) { 3694a51fd47fSStephen M. Cameron dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n"); 3695a51fd47fSStephen M. Cameron return -ENODEV; 3696a51fd47fSStephen M. Cameron } 3697a51fd47fSStephen M. Cameron return 0; 3698a51fd47fSStephen M. Cameron } 3699a51fd47fSStephen M. Cameron 370077c4495cSStephen M. Cameron static int __devinit hpsa_find_cfgtables(struct ctlr_info *h) 3701edd16368SStephen M. Cameron { 370201a02ffcSStephen M. Cameron u64 cfg_offset; 370301a02ffcSStephen M. Cameron u32 cfg_base_addr; 370401a02ffcSStephen M. Cameron u64 cfg_base_addr_index; 3705303932fdSDon Brace u32 trans_offset; 3706a51fd47fSStephen M. Cameron int rc; 370777c4495cSStephen M. Cameron 3708a51fd47fSStephen M. Cameron rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr, 3709a51fd47fSStephen M. Cameron &cfg_base_addr_index, &cfg_offset); 3710a51fd47fSStephen M. Cameron if (rc) 3711a51fd47fSStephen M. Cameron return rc; 371277c4495cSStephen M. Cameron h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev, 3713a51fd47fSStephen M. Cameron cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable)); 371477c4495cSStephen M. Cameron if (!h->cfgtable) 371577c4495cSStephen M. Cameron return -ENOMEM; 3716580ada3cSStephen M. Cameron rc = write_driver_ver_to_cfgtable(h->cfgtable); 3717580ada3cSStephen M. Cameron if (rc) 3718580ada3cSStephen M. Cameron return rc; 371977c4495cSStephen M. Cameron /* Find performant mode table. */ 3720a51fd47fSStephen M. Cameron trans_offset = readl(&h->cfgtable->TransMethodOffset); 372177c4495cSStephen M. Cameron h->transtable = remap_pci_mem(pci_resource_start(h->pdev, 372277c4495cSStephen M. Cameron cfg_base_addr_index)+cfg_offset+trans_offset, 372377c4495cSStephen M. Cameron sizeof(*h->transtable)); 372477c4495cSStephen M. Cameron if (!h->transtable) 372577c4495cSStephen M. Cameron return -ENOMEM; 372677c4495cSStephen M. Cameron return 0; 372777c4495cSStephen M. Cameron } 372877c4495cSStephen M. Cameron 3729cba3d38bSStephen M. Cameron static void __devinit hpsa_get_max_perf_mode_cmds(struct ctlr_info *h) 3730cba3d38bSStephen M. Cameron { 3731cba3d38bSStephen M. Cameron h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands)); 373272ceeaecSStephen M. Cameron 373372ceeaecSStephen M. Cameron /* Limit commands in memory limited kdump scenario. */ 373472ceeaecSStephen M. Cameron if (reset_devices && h->max_commands > 32) 373572ceeaecSStephen M. Cameron h->max_commands = 32; 373672ceeaecSStephen M. Cameron 3737cba3d38bSStephen M. Cameron if (h->max_commands < 16) { 3738cba3d38bSStephen M. Cameron dev_warn(&h->pdev->dev, "Controller reports " 3739cba3d38bSStephen M. Cameron "max supported commands of %d, an obvious lie. " 3740cba3d38bSStephen M. Cameron "Using 16. Ensure that firmware is up to date.\n", 3741cba3d38bSStephen M. Cameron h->max_commands); 3742cba3d38bSStephen M. Cameron h->max_commands = 16; 3743cba3d38bSStephen M. Cameron } 3744cba3d38bSStephen M. Cameron } 3745cba3d38bSStephen M. Cameron 3746b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits: 3747b93d7536SStephen M. Cameron * max commands, max SG elements without chaining, and with chaining, 3748b93d7536SStephen M. Cameron * SG chain block size, etc. 3749b93d7536SStephen M. Cameron */ 3750b93d7536SStephen M. Cameron static void __devinit hpsa_find_board_params(struct ctlr_info *h) 3751b93d7536SStephen M. Cameron { 3752cba3d38bSStephen M. Cameron hpsa_get_max_perf_mode_cmds(h); 3753b93d7536SStephen M. Cameron h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */ 3754b93d7536SStephen M. Cameron h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements)); 3755b93d7536SStephen M. Cameron /* 3756b93d7536SStephen M. Cameron * Limit in-command s/g elements to 32 save dma'able memory. 3757b93d7536SStephen M. Cameron * Howvever spec says if 0, use 31 3758b93d7536SStephen M. Cameron */ 3759b93d7536SStephen M. Cameron h->max_cmd_sg_entries = 31; 3760b93d7536SStephen M. Cameron if (h->maxsgentries > 512) { 3761b93d7536SStephen M. Cameron h->max_cmd_sg_entries = 32; 3762b93d7536SStephen M. Cameron h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1; 3763b93d7536SStephen M. Cameron h->maxsgentries--; /* save one for chain pointer */ 3764b93d7536SStephen M. Cameron } else { 3765b93d7536SStephen M. Cameron h->maxsgentries = 31; /* default to traditional values */ 3766b93d7536SStephen M. Cameron h->chainsize = 0; 3767b93d7536SStephen M. Cameron } 3768b93d7536SStephen M. Cameron } 3769b93d7536SStephen M. Cameron 377076c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h) 377176c46e49SStephen M. Cameron { 377276c46e49SStephen M. Cameron if ((readb(&h->cfgtable->Signature[0]) != 'C') || 377376c46e49SStephen M. Cameron (readb(&h->cfgtable->Signature[1]) != 'I') || 377476c46e49SStephen M. Cameron (readb(&h->cfgtable->Signature[2]) != 'S') || 377576c46e49SStephen M. Cameron (readb(&h->cfgtable->Signature[3]) != 'S')) { 377676c46e49SStephen M. Cameron dev_warn(&h->pdev->dev, "not a valid CISS config table\n"); 377776c46e49SStephen M. Cameron return false; 377876c46e49SStephen M. Cameron } 377976c46e49SStephen M. Cameron return true; 378076c46e49SStephen M. Cameron } 378176c46e49SStephen M. Cameron 3782f7c39101SStephen M. Cameron /* Need to enable prefetch in the SCSI core for 6400 in x86 */ 3783f7c39101SStephen M. Cameron static inline void hpsa_enable_scsi_prefetch(struct ctlr_info *h) 3784f7c39101SStephen M. Cameron { 3785f7c39101SStephen M. Cameron #ifdef CONFIG_X86 3786f7c39101SStephen M. Cameron u32 prefetch; 3787f7c39101SStephen M. Cameron 3788f7c39101SStephen M. Cameron prefetch = readl(&(h->cfgtable->SCSI_Prefetch)); 3789f7c39101SStephen M. Cameron prefetch |= 0x100; 3790f7c39101SStephen M. Cameron writel(prefetch, &(h->cfgtable->SCSI_Prefetch)); 3791f7c39101SStephen M. Cameron #endif 3792f7c39101SStephen M. Cameron } 3793f7c39101SStephen M. Cameron 37943d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result 37953d0eab67SStephen M. Cameron * in a prefetch beyond physical memory. 37963d0eab67SStephen M. Cameron */ 37973d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h) 37983d0eab67SStephen M. Cameron { 37993d0eab67SStephen M. Cameron u32 dma_prefetch; 38003d0eab67SStephen M. Cameron 38013d0eab67SStephen M. Cameron if (h->board_id != 0x3225103C) 38023d0eab67SStephen M. Cameron return; 38033d0eab67SStephen M. Cameron dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG); 38043d0eab67SStephen M. Cameron dma_prefetch |= 0x8000; 38053d0eab67SStephen M. Cameron writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG); 38063d0eab67SStephen M. Cameron } 38073d0eab67SStephen M. Cameron 38083f4336f3SStephen M. Cameron static void __devinit hpsa_wait_for_mode_change_ack(struct ctlr_info *h) 3809eb6b2ae9SStephen M. Cameron { 3810eb6b2ae9SStephen M. Cameron int i; 38116eaf46fdSStephen M. Cameron u32 doorbell_value; 38126eaf46fdSStephen M. Cameron unsigned long flags; 3813eb6b2ae9SStephen M. Cameron 3814eb6b2ae9SStephen M. Cameron /* under certain very rare conditions, this can take awhile. 3815eb6b2ae9SStephen M. Cameron * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right 3816eb6b2ae9SStephen M. Cameron * as we enter this code.) 3817eb6b2ae9SStephen M. Cameron */ 3818eb6b2ae9SStephen M. Cameron for (i = 0; i < MAX_CONFIG_WAIT; i++) { 38196eaf46fdSStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 38206eaf46fdSStephen M. Cameron doorbell_value = readl(h->vaddr + SA5_DOORBELL); 38216eaf46fdSStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 3822382be668SDan Carpenter if (!(doorbell_value & CFGTBL_ChangeReq)) 3823eb6b2ae9SStephen M. Cameron break; 3824eb6b2ae9SStephen M. Cameron /* delay and try again */ 382560d3f5b0SStephen M. Cameron usleep_range(10000, 20000); 3826eb6b2ae9SStephen M. Cameron } 38273f4336f3SStephen M. Cameron } 38283f4336f3SStephen M. Cameron 38293f4336f3SStephen M. Cameron static int __devinit hpsa_enter_simple_mode(struct ctlr_info *h) 38303f4336f3SStephen M. Cameron { 38313f4336f3SStephen M. Cameron u32 trans_support; 38323f4336f3SStephen M. Cameron 38333f4336f3SStephen M. Cameron trans_support = readl(&(h->cfgtable->TransportSupport)); 38343f4336f3SStephen M. Cameron if (!(trans_support & SIMPLE_MODE)) 38353f4336f3SStephen M. Cameron return -ENOTSUPP; 38363f4336f3SStephen M. Cameron 38373f4336f3SStephen M. Cameron h->max_commands = readl(&(h->cfgtable->CmdsOutMax)); 38383f4336f3SStephen M. Cameron /* Update the field, and then ring the doorbell */ 38393f4336f3SStephen M. Cameron writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest)); 38403f4336f3SStephen M. Cameron writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL); 38413f4336f3SStephen M. Cameron hpsa_wait_for_mode_change_ack(h); 3842eb6b2ae9SStephen M. Cameron print_cfg_table(&h->pdev->dev, h->cfgtable); 3843eb6b2ae9SStephen M. Cameron if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) { 3844eb6b2ae9SStephen M. Cameron dev_warn(&h->pdev->dev, 3845eb6b2ae9SStephen M. Cameron "unable to get board into simple mode\n"); 3846eb6b2ae9SStephen M. Cameron return -ENODEV; 3847eb6b2ae9SStephen M. Cameron } 3848960a30e7SStephen M. Cameron h->transMethod = CFGTBL_Trans_Simple; 3849eb6b2ae9SStephen M. Cameron return 0; 3850eb6b2ae9SStephen M. Cameron } 3851eb6b2ae9SStephen M. Cameron 385277c4495cSStephen M. Cameron static int __devinit hpsa_pci_init(struct ctlr_info *h) 385377c4495cSStephen M. Cameron { 3854eb6b2ae9SStephen M. Cameron int prod_index, err; 3855edd16368SStephen M. Cameron 3856e5c880d1SStephen M. Cameron prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id); 3857e5c880d1SStephen M. Cameron if (prod_index < 0) 3858edd16368SStephen M. Cameron return -ENODEV; 3859e5c880d1SStephen M. Cameron h->product_name = products[prod_index].product_name; 3860e5c880d1SStephen M. Cameron h->access = *(products[prod_index].access); 3861e5c880d1SStephen M. Cameron 386285bdbabbSStephen M. Cameron if (hpsa_board_disabled(h->pdev)) { 386355c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "controller appears to be disabled\n"); 3864edd16368SStephen M. Cameron return -ENODEV; 3865edd16368SStephen M. Cameron } 386655c06c71SStephen M. Cameron err = pci_enable_device(h->pdev); 3867edd16368SStephen M. Cameron if (err) { 386855c06c71SStephen M. Cameron dev_warn(&h->pdev->dev, "unable to enable PCI device\n"); 3869edd16368SStephen M. Cameron return err; 3870edd16368SStephen M. Cameron } 3871edd16368SStephen M. Cameron 387255c06c71SStephen M. Cameron err = pci_request_regions(h->pdev, "hpsa"); 3873edd16368SStephen M. Cameron if (err) { 387455c06c71SStephen M. Cameron dev_err(&h->pdev->dev, 387555c06c71SStephen M. Cameron "cannot obtain PCI resources, aborting\n"); 3876edd16368SStephen M. Cameron return err; 3877edd16368SStephen M. Cameron } 38786b3f4c52SStephen M. Cameron hpsa_interrupt_mode(h); 387912d2cd47SStephen M. Cameron err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr); 38803a7774ceSStephen M. Cameron if (err) 3881edd16368SStephen M. Cameron goto err_out_free_res; 3882edd16368SStephen M. Cameron h->vaddr = remap_pci_mem(h->paddr, 0x250); 3883204892e9SStephen M. Cameron if (!h->vaddr) { 3884204892e9SStephen M. Cameron err = -ENOMEM; 3885204892e9SStephen M. Cameron goto err_out_free_res; 3886204892e9SStephen M. Cameron } 3887fe5389c8SStephen M. Cameron err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY); 38882c4c8c8bSStephen M. Cameron if (err) 3889edd16368SStephen M. Cameron goto err_out_free_res; 389077c4495cSStephen M. Cameron err = hpsa_find_cfgtables(h); 389177c4495cSStephen M. Cameron if (err) 3892edd16368SStephen M. Cameron goto err_out_free_res; 3893b93d7536SStephen M. Cameron hpsa_find_board_params(h); 3894edd16368SStephen M. Cameron 389576c46e49SStephen M. Cameron if (!hpsa_CISS_signature_present(h)) { 3896edd16368SStephen M. Cameron err = -ENODEV; 3897edd16368SStephen M. Cameron goto err_out_free_res; 3898edd16368SStephen M. Cameron } 3899f7c39101SStephen M. Cameron hpsa_enable_scsi_prefetch(h); 39003d0eab67SStephen M. Cameron hpsa_p600_dma_prefetch_quirk(h); 3901eb6b2ae9SStephen M. Cameron err = hpsa_enter_simple_mode(h); 3902eb6b2ae9SStephen M. Cameron if (err) 3903edd16368SStephen M. Cameron goto err_out_free_res; 3904edd16368SStephen M. Cameron return 0; 3905edd16368SStephen M. Cameron 3906edd16368SStephen M. Cameron err_out_free_res: 3907204892e9SStephen M. Cameron if (h->transtable) 3908204892e9SStephen M. Cameron iounmap(h->transtable); 3909204892e9SStephen M. Cameron if (h->cfgtable) 3910204892e9SStephen M. Cameron iounmap(h->cfgtable); 3911204892e9SStephen M. Cameron if (h->vaddr) 3912204892e9SStephen M. Cameron iounmap(h->vaddr); 3913edd16368SStephen M. Cameron /* 3914edd16368SStephen M. Cameron * Deliberately omit pci_disable_device(): it does something nasty to 3915edd16368SStephen M. Cameron * Smart Array controllers that pci_enable_device does not undo 3916edd16368SStephen M. Cameron */ 391755c06c71SStephen M. Cameron pci_release_regions(h->pdev); 3918edd16368SStephen M. Cameron return err; 3919edd16368SStephen M. Cameron } 3920edd16368SStephen M. Cameron 3921339b2b14SStephen M. Cameron static void __devinit hpsa_hba_inquiry(struct ctlr_info *h) 3922339b2b14SStephen M. Cameron { 3923339b2b14SStephen M. Cameron int rc; 3924339b2b14SStephen M. Cameron 3925339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64 3926339b2b14SStephen M. Cameron h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL); 3927339b2b14SStephen M. Cameron if (!h->hba_inquiry_data) 3928339b2b14SStephen M. Cameron return; 3929339b2b14SStephen M. Cameron rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0, 3930339b2b14SStephen M. Cameron h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT); 3931339b2b14SStephen M. Cameron if (rc != 0) { 3932339b2b14SStephen M. Cameron kfree(h->hba_inquiry_data); 3933339b2b14SStephen M. Cameron h->hba_inquiry_data = NULL; 3934339b2b14SStephen M. Cameron } 3935339b2b14SStephen M. Cameron } 3936339b2b14SStephen M. Cameron 39374c2a8c40SStephen M. Cameron static __devinit int hpsa_init_reset_devices(struct pci_dev *pdev) 3938edd16368SStephen M. Cameron { 39391df8552aSStephen M. Cameron int rc, i; 3940edd16368SStephen M. Cameron 39414c2a8c40SStephen M. Cameron if (!reset_devices) 39424c2a8c40SStephen M. Cameron return 0; 39434c2a8c40SStephen M. Cameron 39441df8552aSStephen M. Cameron /* Reset the controller with a PCI power-cycle or via doorbell */ 39451df8552aSStephen M. Cameron rc = hpsa_kdump_hard_reset_controller(pdev); 3946edd16368SStephen M. Cameron 39471df8552aSStephen M. Cameron /* -ENOTSUPP here means we cannot reset the controller 39481df8552aSStephen M. Cameron * but it's already (and still) up and running in 394918867659SStephen M. Cameron * "performant mode". Or, it might be 640x, which can't reset 395018867659SStephen M. Cameron * due to concerns about shared bbwc between 6402/6404 pair. 39511df8552aSStephen M. Cameron */ 39521df8552aSStephen M. Cameron if (rc == -ENOTSUPP) 395364670ac8SStephen M. Cameron return rc; /* just try to do the kdump anyhow. */ 39541df8552aSStephen M. Cameron if (rc) 39551df8552aSStephen M. Cameron return -ENODEV; 3956edd16368SStephen M. Cameron 3957edd16368SStephen M. Cameron /* Now try to get the controller to respond to a no-op */ 39582b870cb3SStephen M. Cameron dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n"); 3959edd16368SStephen M. Cameron for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) { 3960edd16368SStephen M. Cameron if (hpsa_noop(pdev) == 0) 3961edd16368SStephen M. Cameron break; 3962edd16368SStephen M. Cameron else 3963edd16368SStephen M. Cameron dev_warn(&pdev->dev, "no-op failed%s\n", 3964edd16368SStephen M. Cameron (i < 11 ? "; re-trying" : "")); 3965edd16368SStephen M. Cameron } 39664c2a8c40SStephen M. Cameron return 0; 3967edd16368SStephen M. Cameron } 3968edd16368SStephen M. Cameron 39692e9d1b36SStephen M. Cameron static __devinit int hpsa_allocate_cmd_pool(struct ctlr_info *h) 39702e9d1b36SStephen M. Cameron { 39712e9d1b36SStephen M. Cameron h->cmd_pool_bits = kzalloc( 39722e9d1b36SStephen M. Cameron DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) * 39732e9d1b36SStephen M. Cameron sizeof(unsigned long), GFP_KERNEL); 39742e9d1b36SStephen M. Cameron h->cmd_pool = pci_alloc_consistent(h->pdev, 39752e9d1b36SStephen M. Cameron h->nr_cmds * sizeof(*h->cmd_pool), 39762e9d1b36SStephen M. Cameron &(h->cmd_pool_dhandle)); 39772e9d1b36SStephen M. Cameron h->errinfo_pool = pci_alloc_consistent(h->pdev, 39782e9d1b36SStephen M. Cameron h->nr_cmds * sizeof(*h->errinfo_pool), 39792e9d1b36SStephen M. Cameron &(h->errinfo_pool_dhandle)); 39802e9d1b36SStephen M. Cameron if ((h->cmd_pool_bits == NULL) 39812e9d1b36SStephen M. Cameron || (h->cmd_pool == NULL) 39822e9d1b36SStephen M. Cameron || (h->errinfo_pool == NULL)) { 39832e9d1b36SStephen M. Cameron dev_err(&h->pdev->dev, "out of memory in %s", __func__); 39842e9d1b36SStephen M. Cameron return -ENOMEM; 39852e9d1b36SStephen M. Cameron } 39862e9d1b36SStephen M. Cameron return 0; 39872e9d1b36SStephen M. Cameron } 39882e9d1b36SStephen M. Cameron 39892e9d1b36SStephen M. Cameron static void hpsa_free_cmd_pool(struct ctlr_info *h) 39902e9d1b36SStephen M. Cameron { 39912e9d1b36SStephen M. Cameron kfree(h->cmd_pool_bits); 39922e9d1b36SStephen M. Cameron if (h->cmd_pool) 39932e9d1b36SStephen M. Cameron pci_free_consistent(h->pdev, 39942e9d1b36SStephen M. Cameron h->nr_cmds * sizeof(struct CommandList), 39952e9d1b36SStephen M. Cameron h->cmd_pool, h->cmd_pool_dhandle); 39962e9d1b36SStephen M. Cameron if (h->errinfo_pool) 39972e9d1b36SStephen M. Cameron pci_free_consistent(h->pdev, 39982e9d1b36SStephen M. Cameron h->nr_cmds * sizeof(struct ErrorInfo), 39992e9d1b36SStephen M. Cameron h->errinfo_pool, 40002e9d1b36SStephen M. Cameron h->errinfo_pool_dhandle); 40012e9d1b36SStephen M. Cameron } 40022e9d1b36SStephen M. Cameron 40030ae01a32SStephen M. Cameron static int hpsa_request_irq(struct ctlr_info *h, 40040ae01a32SStephen M. Cameron irqreturn_t (*msixhandler)(int, void *), 40050ae01a32SStephen M. Cameron irqreturn_t (*intxhandler)(int, void *)) 40060ae01a32SStephen M. Cameron { 40070ae01a32SStephen M. Cameron int rc; 40080ae01a32SStephen M. Cameron 40090ae01a32SStephen M. Cameron if (h->msix_vector || h->msi_vector) 40100ae01a32SStephen M. Cameron rc = request_irq(h->intr[h->intr_mode], msixhandler, 40110ae01a32SStephen M. Cameron IRQF_DISABLED, h->devname, h); 40120ae01a32SStephen M. Cameron else 40130ae01a32SStephen M. Cameron rc = request_irq(h->intr[h->intr_mode], intxhandler, 40140ae01a32SStephen M. Cameron IRQF_DISABLED, h->devname, h); 40150ae01a32SStephen M. Cameron if (rc) { 40160ae01a32SStephen M. Cameron dev_err(&h->pdev->dev, "unable to get irq %d for %s\n", 40170ae01a32SStephen M. Cameron h->intr[h->intr_mode], h->devname); 40180ae01a32SStephen M. Cameron return -ENODEV; 40190ae01a32SStephen M. Cameron } 40200ae01a32SStephen M. Cameron return 0; 40210ae01a32SStephen M. Cameron } 40220ae01a32SStephen M. Cameron 402364670ac8SStephen M. Cameron static int __devinit hpsa_kdump_soft_reset(struct ctlr_info *h) 402464670ac8SStephen M. Cameron { 402564670ac8SStephen M. Cameron if (hpsa_send_host_reset(h, RAID_CTLR_LUNID, 402664670ac8SStephen M. Cameron HPSA_RESET_TYPE_CONTROLLER)) { 402764670ac8SStephen M. Cameron dev_warn(&h->pdev->dev, "Resetting array controller failed.\n"); 402864670ac8SStephen M. Cameron return -EIO; 402964670ac8SStephen M. Cameron } 403064670ac8SStephen M. Cameron 403164670ac8SStephen M. Cameron dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n"); 403264670ac8SStephen M. Cameron if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) { 403364670ac8SStephen M. Cameron dev_warn(&h->pdev->dev, "Soft reset had no effect.\n"); 403464670ac8SStephen M. Cameron return -1; 403564670ac8SStephen M. Cameron } 403664670ac8SStephen M. Cameron 403764670ac8SStephen M. Cameron dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n"); 403864670ac8SStephen M. Cameron if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) { 403964670ac8SStephen M. Cameron dev_warn(&h->pdev->dev, "Board failed to become ready " 404064670ac8SStephen M. Cameron "after soft reset.\n"); 404164670ac8SStephen M. Cameron return -1; 404264670ac8SStephen M. Cameron } 404364670ac8SStephen M. Cameron 404464670ac8SStephen M. Cameron return 0; 404564670ac8SStephen M. Cameron } 404664670ac8SStephen M. Cameron 404764670ac8SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h) 404864670ac8SStephen M. Cameron { 404964670ac8SStephen M. Cameron free_irq(h->intr[h->intr_mode], h); 405064670ac8SStephen M. Cameron #ifdef CONFIG_PCI_MSI 405164670ac8SStephen M. Cameron if (h->msix_vector) 405264670ac8SStephen M. Cameron pci_disable_msix(h->pdev); 405364670ac8SStephen M. Cameron else if (h->msi_vector) 405464670ac8SStephen M. Cameron pci_disable_msi(h->pdev); 405564670ac8SStephen M. Cameron #endif /* CONFIG_PCI_MSI */ 405664670ac8SStephen M. Cameron hpsa_free_sg_chain_blocks(h); 405764670ac8SStephen M. Cameron hpsa_free_cmd_pool(h); 405864670ac8SStephen M. Cameron kfree(h->blockFetchTable); 405964670ac8SStephen M. Cameron pci_free_consistent(h->pdev, h->reply_pool_size, 406064670ac8SStephen M. Cameron h->reply_pool, h->reply_pool_dhandle); 406164670ac8SStephen M. Cameron if (h->vaddr) 406264670ac8SStephen M. Cameron iounmap(h->vaddr); 406364670ac8SStephen M. Cameron if (h->transtable) 406464670ac8SStephen M. Cameron iounmap(h->transtable); 406564670ac8SStephen M. Cameron if (h->cfgtable) 406664670ac8SStephen M. Cameron iounmap(h->cfgtable); 406764670ac8SStephen M. Cameron pci_release_regions(h->pdev); 406864670ac8SStephen M. Cameron kfree(h); 406964670ac8SStephen M. Cameron } 407064670ac8SStephen M. Cameron 40714c2a8c40SStephen M. Cameron static int __devinit hpsa_init_one(struct pci_dev *pdev, 40724c2a8c40SStephen M. Cameron const struct pci_device_id *ent) 40734c2a8c40SStephen M. Cameron { 40744c2a8c40SStephen M. Cameron int dac, rc; 40754c2a8c40SStephen M. Cameron struct ctlr_info *h; 407664670ac8SStephen M. Cameron int try_soft_reset = 0; 407764670ac8SStephen M. Cameron unsigned long flags; 40784c2a8c40SStephen M. Cameron 40794c2a8c40SStephen M. Cameron if (number_of_controllers == 0) 40804c2a8c40SStephen M. Cameron printk(KERN_INFO DRIVER_NAME "\n"); 40814c2a8c40SStephen M. Cameron 40824c2a8c40SStephen M. Cameron rc = hpsa_init_reset_devices(pdev); 408364670ac8SStephen M. Cameron if (rc) { 408464670ac8SStephen M. Cameron if (rc != -ENOTSUPP) 40854c2a8c40SStephen M. Cameron return rc; 408664670ac8SStephen M. Cameron /* If the reset fails in a particular way (it has no way to do 408764670ac8SStephen M. Cameron * a proper hard reset, so returns -ENOTSUPP) we can try to do 408864670ac8SStephen M. Cameron * a soft reset once we get the controller configured up to the 408964670ac8SStephen M. Cameron * point that it can accept a command. 409064670ac8SStephen M. Cameron */ 409164670ac8SStephen M. Cameron try_soft_reset = 1; 409264670ac8SStephen M. Cameron rc = 0; 409364670ac8SStephen M. Cameron } 409464670ac8SStephen M. Cameron 409564670ac8SStephen M. Cameron reinit_after_soft_reset: 40964c2a8c40SStephen M. Cameron 4097303932fdSDon Brace /* Command structures must be aligned on a 32-byte boundary because 4098303932fdSDon Brace * the 5 lower bits of the address are used by the hardware. and by 4099303932fdSDon Brace * the driver. See comments in hpsa.h for more info. 4100303932fdSDon Brace */ 4101303932fdSDon Brace #define COMMANDLIST_ALIGNMENT 32 4102303932fdSDon Brace BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT); 4103edd16368SStephen M. Cameron h = kzalloc(sizeof(*h), GFP_KERNEL); 4104edd16368SStephen M. Cameron if (!h) 4105ecd9aad4SStephen M. Cameron return -ENOMEM; 4106edd16368SStephen M. Cameron 410755c06c71SStephen M. Cameron h->pdev = pdev; 4108edd16368SStephen M. Cameron h->busy_initializing = 1; 4109a9a3a273SStephen M. Cameron h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT; 41109e0fc764SStephen M. Cameron INIT_LIST_HEAD(&h->cmpQ); 41119e0fc764SStephen M. Cameron INIT_LIST_HEAD(&h->reqQ); 41126eaf46fdSStephen M. Cameron spin_lock_init(&h->lock); 41136eaf46fdSStephen M. Cameron spin_lock_init(&h->scan_lock); 411455c06c71SStephen M. Cameron rc = hpsa_pci_init(h); 4115ecd9aad4SStephen M. Cameron if (rc != 0) 4116edd16368SStephen M. Cameron goto clean1; 4117edd16368SStephen M. Cameron 4118edd16368SStephen M. Cameron sprintf(h->devname, "hpsa%d", number_of_controllers); 4119edd16368SStephen M. Cameron h->ctlr = number_of_controllers; 4120edd16368SStephen M. Cameron number_of_controllers++; 4121edd16368SStephen M. Cameron 4122edd16368SStephen M. Cameron /* configure PCI DMA stuff */ 4123ecd9aad4SStephen M. Cameron rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 4124ecd9aad4SStephen M. Cameron if (rc == 0) { 4125edd16368SStephen M. Cameron dac = 1; 4126ecd9aad4SStephen M. Cameron } else { 4127ecd9aad4SStephen M. Cameron rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 4128ecd9aad4SStephen M. Cameron if (rc == 0) { 4129edd16368SStephen M. Cameron dac = 0; 4130ecd9aad4SStephen M. Cameron } else { 4131edd16368SStephen M. Cameron dev_err(&pdev->dev, "no suitable DMA available\n"); 4132edd16368SStephen M. Cameron goto clean1; 4133edd16368SStephen M. Cameron } 4134ecd9aad4SStephen M. Cameron } 4135edd16368SStephen M. Cameron 4136edd16368SStephen M. Cameron /* make sure the board interrupts are off */ 4137edd16368SStephen M. Cameron h->access.set_intr_mask(h, HPSA_INTR_OFF); 413810f66018SStephen M. Cameron 41390ae01a32SStephen M. Cameron if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx)) 4140edd16368SStephen M. Cameron goto clean2; 4141303932fdSDon Brace dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n", 4142303932fdSDon Brace h->devname, pdev->device, 4143a9a3a273SStephen M. Cameron h->intr[h->intr_mode], dac ? "" : " not"); 41442e9d1b36SStephen M. Cameron if (hpsa_allocate_cmd_pool(h)) 4145edd16368SStephen M. Cameron goto clean4; 414633a2ffceSStephen M. Cameron if (hpsa_allocate_sg_chain_blocks(h)) 414733a2ffceSStephen M. Cameron goto clean4; 4148a08a8471SStephen M. Cameron init_waitqueue_head(&h->scan_wait_queue); 4149a08a8471SStephen M. Cameron h->scan_finished = 1; /* no scan currently in progress */ 4150edd16368SStephen M. Cameron 4151edd16368SStephen M. Cameron pci_set_drvdata(pdev, h); 41529a41338eSStephen M. Cameron h->ndevices = 0; 41539a41338eSStephen M. Cameron h->scsi_host = NULL; 41549a41338eSStephen M. Cameron spin_lock_init(&h->devlock); 415564670ac8SStephen M. Cameron hpsa_put_ctlr_into_performant_mode(h); 415664670ac8SStephen M. Cameron 415764670ac8SStephen M. Cameron /* At this point, the controller is ready to take commands. 415864670ac8SStephen M. Cameron * Now, if reset_devices and the hard reset didn't work, try 415964670ac8SStephen M. Cameron * the soft reset and see if that works. 416064670ac8SStephen M. Cameron */ 416164670ac8SStephen M. Cameron if (try_soft_reset) { 416264670ac8SStephen M. Cameron 416364670ac8SStephen M. Cameron /* This is kind of gross. We may or may not get a completion 416464670ac8SStephen M. Cameron * from the soft reset command, and if we do, then the value 416564670ac8SStephen M. Cameron * from the fifo may or may not be valid. So, we wait 10 secs 416664670ac8SStephen M. Cameron * after the reset throwing away any completions we get during 416764670ac8SStephen M. Cameron * that time. Unregister the interrupt handler and register 416864670ac8SStephen M. Cameron * fake ones to scoop up any residual completions. 416964670ac8SStephen M. Cameron */ 417064670ac8SStephen M. Cameron spin_lock_irqsave(&h->lock, flags); 417164670ac8SStephen M. Cameron h->access.set_intr_mask(h, HPSA_INTR_OFF); 417264670ac8SStephen M. Cameron spin_unlock_irqrestore(&h->lock, flags); 417364670ac8SStephen M. Cameron free_irq(h->intr[h->intr_mode], h); 417464670ac8SStephen M. Cameron rc = hpsa_request_irq(h, hpsa_msix_discard_completions, 417564670ac8SStephen M. Cameron hpsa_intx_discard_completions); 417664670ac8SStephen M. Cameron if (rc) { 417764670ac8SStephen M. Cameron dev_warn(&h->pdev->dev, "Failed to request_irq after " 417864670ac8SStephen M. Cameron "soft reset.\n"); 417964670ac8SStephen M. Cameron goto clean4; 418064670ac8SStephen M. Cameron } 418164670ac8SStephen M. Cameron 418264670ac8SStephen M. Cameron rc = hpsa_kdump_soft_reset(h); 418364670ac8SStephen M. Cameron if (rc) 418464670ac8SStephen M. Cameron /* Neither hard nor soft reset worked, we're hosed. */ 418564670ac8SStephen M. Cameron goto clean4; 418664670ac8SStephen M. Cameron 418764670ac8SStephen M. Cameron dev_info(&h->pdev->dev, "Board READY.\n"); 418864670ac8SStephen M. Cameron dev_info(&h->pdev->dev, 418964670ac8SStephen M. Cameron "Waiting for stale completions to drain.\n"); 419064670ac8SStephen M. Cameron h->access.set_intr_mask(h, HPSA_INTR_ON); 419164670ac8SStephen M. Cameron msleep(10000); 419264670ac8SStephen M. Cameron h->access.set_intr_mask(h, HPSA_INTR_OFF); 419364670ac8SStephen M. Cameron 419464670ac8SStephen M. Cameron rc = controller_reset_failed(h->cfgtable); 419564670ac8SStephen M. Cameron if (rc) 419664670ac8SStephen M. Cameron dev_info(&h->pdev->dev, 419764670ac8SStephen M. Cameron "Soft reset appears to have failed.\n"); 419864670ac8SStephen M. Cameron 419964670ac8SStephen M. Cameron /* since the controller's reset, we have to go back and re-init 420064670ac8SStephen M. Cameron * everything. Easiest to just forget what we've done and do it 420164670ac8SStephen M. Cameron * all over again. 420264670ac8SStephen M. Cameron */ 420364670ac8SStephen M. Cameron hpsa_undo_allocations_after_kdump_soft_reset(h); 420464670ac8SStephen M. Cameron try_soft_reset = 0; 420564670ac8SStephen M. Cameron if (rc) 420664670ac8SStephen M. Cameron /* don't go to clean4, we already unallocated */ 420764670ac8SStephen M. Cameron return -ENODEV; 420864670ac8SStephen M. Cameron 420964670ac8SStephen M. Cameron goto reinit_after_soft_reset; 421064670ac8SStephen M. Cameron } 4211edd16368SStephen M. Cameron 4212edd16368SStephen M. Cameron /* Turn the interrupts on so we can service requests */ 4213edd16368SStephen M. Cameron h->access.set_intr_mask(h, HPSA_INTR_ON); 4214edd16368SStephen M. Cameron 4215339b2b14SStephen M. Cameron hpsa_hba_inquiry(h); 4216edd16368SStephen M. Cameron hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */ 4217edd16368SStephen M. Cameron h->busy_initializing = 0; 4218edd16368SStephen M. Cameron return 1; 4219edd16368SStephen M. Cameron 4220edd16368SStephen M. Cameron clean4: 422133a2ffceSStephen M. Cameron hpsa_free_sg_chain_blocks(h); 42222e9d1b36SStephen M. Cameron hpsa_free_cmd_pool(h); 4223a9a3a273SStephen M. Cameron free_irq(h->intr[h->intr_mode], h); 4224edd16368SStephen M. Cameron clean2: 4225edd16368SStephen M. Cameron clean1: 4226edd16368SStephen M. Cameron h->busy_initializing = 0; 4227edd16368SStephen M. Cameron kfree(h); 4228ecd9aad4SStephen M. Cameron return rc; 4229edd16368SStephen M. Cameron } 4230edd16368SStephen M. Cameron 4231edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h) 4232edd16368SStephen M. Cameron { 4233edd16368SStephen M. Cameron char *flush_buf; 4234edd16368SStephen M. Cameron struct CommandList *c; 4235edd16368SStephen M. Cameron 4236edd16368SStephen M. Cameron flush_buf = kzalloc(4, GFP_KERNEL); 4237edd16368SStephen M. Cameron if (!flush_buf) 4238edd16368SStephen M. Cameron return; 4239edd16368SStephen M. Cameron 4240edd16368SStephen M. Cameron c = cmd_special_alloc(h); 4241edd16368SStephen M. Cameron if (!c) { 4242edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n"); 4243edd16368SStephen M. Cameron goto out_of_memory; 4244edd16368SStephen M. Cameron } 4245edd16368SStephen M. Cameron fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0, 4246edd16368SStephen M. Cameron RAID_CTLR_LUNID, TYPE_CMD); 4247edd16368SStephen M. Cameron hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE); 4248edd16368SStephen M. Cameron if (c->err_info->CommandStatus != 0) 4249edd16368SStephen M. Cameron dev_warn(&h->pdev->dev, 4250edd16368SStephen M. Cameron "error flushing cache on controller\n"); 4251edd16368SStephen M. Cameron cmd_special_free(h, c); 4252edd16368SStephen M. Cameron out_of_memory: 4253edd16368SStephen M. Cameron kfree(flush_buf); 4254edd16368SStephen M. Cameron } 4255edd16368SStephen M. Cameron 4256edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev) 4257edd16368SStephen M. Cameron { 4258edd16368SStephen M. Cameron struct ctlr_info *h; 4259edd16368SStephen M. Cameron 4260edd16368SStephen M. Cameron h = pci_get_drvdata(pdev); 4261edd16368SStephen M. Cameron /* Turn board interrupts off and send the flush cache command 4262edd16368SStephen M. Cameron * sendcmd will turn off interrupt, and send the flush... 4263edd16368SStephen M. Cameron * To write all data in the battery backed cache to disks 4264edd16368SStephen M. Cameron */ 4265edd16368SStephen M. Cameron hpsa_flush_cache(h); 4266edd16368SStephen M. Cameron h->access.set_intr_mask(h, HPSA_INTR_OFF); 4267a9a3a273SStephen M. Cameron free_irq(h->intr[h->intr_mode], h); 4268edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI 4269edd16368SStephen M. Cameron if (h->msix_vector) 4270edd16368SStephen M. Cameron pci_disable_msix(h->pdev); 4271edd16368SStephen M. Cameron else if (h->msi_vector) 4272edd16368SStephen M. Cameron pci_disable_msi(h->pdev); 4273edd16368SStephen M. Cameron #endif /* CONFIG_PCI_MSI */ 4274edd16368SStephen M. Cameron } 4275edd16368SStephen M. Cameron 4276edd16368SStephen M. Cameron static void __devexit hpsa_remove_one(struct pci_dev *pdev) 4277edd16368SStephen M. Cameron { 4278edd16368SStephen M. Cameron struct ctlr_info *h; 4279edd16368SStephen M. Cameron 4280edd16368SStephen M. Cameron if (pci_get_drvdata(pdev) == NULL) { 4281edd16368SStephen M. Cameron dev_err(&pdev->dev, "unable to remove device \n"); 4282edd16368SStephen M. Cameron return; 4283edd16368SStephen M. Cameron } 4284edd16368SStephen M. Cameron h = pci_get_drvdata(pdev); 4285edd16368SStephen M. Cameron hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */ 4286edd16368SStephen M. Cameron hpsa_shutdown(pdev); 4287edd16368SStephen M. Cameron iounmap(h->vaddr); 4288204892e9SStephen M. Cameron iounmap(h->transtable); 4289204892e9SStephen M. Cameron iounmap(h->cfgtable); 429033a2ffceSStephen M. Cameron hpsa_free_sg_chain_blocks(h); 4291edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 4292edd16368SStephen M. Cameron h->nr_cmds * sizeof(struct CommandList), 4293edd16368SStephen M. Cameron h->cmd_pool, h->cmd_pool_dhandle); 4294edd16368SStephen M. Cameron pci_free_consistent(h->pdev, 4295edd16368SStephen M. Cameron h->nr_cmds * sizeof(struct ErrorInfo), 4296edd16368SStephen M. Cameron h->errinfo_pool, h->errinfo_pool_dhandle); 4297303932fdSDon Brace pci_free_consistent(h->pdev, h->reply_pool_size, 4298303932fdSDon Brace h->reply_pool, h->reply_pool_dhandle); 4299edd16368SStephen M. Cameron kfree(h->cmd_pool_bits); 4300303932fdSDon Brace kfree(h->blockFetchTable); 4301339b2b14SStephen M. Cameron kfree(h->hba_inquiry_data); 4302edd16368SStephen M. Cameron /* 4303edd16368SStephen M. Cameron * Deliberately omit pci_disable_device(): it does something nasty to 4304edd16368SStephen M. Cameron * Smart Array controllers that pci_enable_device does not undo 4305edd16368SStephen M. Cameron */ 4306edd16368SStephen M. Cameron pci_release_regions(pdev); 4307edd16368SStephen M. Cameron pci_set_drvdata(pdev, NULL); 4308edd16368SStephen M. Cameron kfree(h); 4309edd16368SStephen M. Cameron } 4310edd16368SStephen M. Cameron 4311edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev, 4312edd16368SStephen M. Cameron __attribute__((unused)) pm_message_t state) 4313edd16368SStephen M. Cameron { 4314edd16368SStephen M. Cameron return -ENOSYS; 4315edd16368SStephen M. Cameron } 4316edd16368SStephen M. Cameron 4317edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev) 4318edd16368SStephen M. Cameron { 4319edd16368SStephen M. Cameron return -ENOSYS; 4320edd16368SStephen M. Cameron } 4321edd16368SStephen M. Cameron 4322edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = { 4323edd16368SStephen M. Cameron .name = "hpsa", 4324edd16368SStephen M. Cameron .probe = hpsa_init_one, 4325edd16368SStephen M. Cameron .remove = __devexit_p(hpsa_remove_one), 4326edd16368SStephen M. Cameron .id_table = hpsa_pci_device_id, /* id_table */ 4327edd16368SStephen M. Cameron .shutdown = hpsa_shutdown, 4328edd16368SStephen M. Cameron .suspend = hpsa_suspend, 4329edd16368SStephen M. Cameron .resume = hpsa_resume, 4330edd16368SStephen M. Cameron }; 4331edd16368SStephen M. Cameron 4332303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of 4333303932fdSDon Brace * scatter gather elements supported) and bucket[], 4334303932fdSDon Brace * which is an array of 8 integers. The bucket[] array 4335303932fdSDon Brace * contains 8 different DMA transfer sizes (in 16 4336303932fdSDon Brace * byte increments) which the controller uses to fetch 4337303932fdSDon Brace * commands. This function fills in bucket_map[], which 4338303932fdSDon Brace * maps a given number of scatter gather elements to one of 4339303932fdSDon Brace * the 8 DMA transfer sizes. The point of it is to allow the 4340303932fdSDon Brace * controller to only do as much DMA as needed to fetch the 4341303932fdSDon Brace * command, with the DMA transfer size encoded in the lower 4342303932fdSDon Brace * bits of the command address. 4343303932fdSDon Brace */ 4344303932fdSDon Brace static void calc_bucket_map(int bucket[], int num_buckets, 4345303932fdSDon Brace int nsgs, int *bucket_map) 4346303932fdSDon Brace { 4347303932fdSDon Brace int i, j, b, size; 4348303932fdSDon Brace 4349303932fdSDon Brace /* even a command with 0 SGs requires 4 blocks */ 4350303932fdSDon Brace #define MINIMUM_TRANSFER_BLOCKS 4 4351303932fdSDon Brace #define NUM_BUCKETS 8 4352303932fdSDon Brace /* Note, bucket_map must have nsgs+1 entries. */ 4353303932fdSDon Brace for (i = 0; i <= nsgs; i++) { 4354303932fdSDon Brace /* Compute size of a command with i SG entries */ 4355303932fdSDon Brace size = i + MINIMUM_TRANSFER_BLOCKS; 4356303932fdSDon Brace b = num_buckets; /* Assume the biggest bucket */ 4357303932fdSDon Brace /* Find the bucket that is just big enough */ 4358303932fdSDon Brace for (j = 0; j < 8; j++) { 4359303932fdSDon Brace if (bucket[j] >= size) { 4360303932fdSDon Brace b = j; 4361303932fdSDon Brace break; 4362303932fdSDon Brace } 4363303932fdSDon Brace } 4364303932fdSDon Brace /* for a command with i SG entries, use bucket b. */ 4365303932fdSDon Brace bucket_map[i] = b; 4366303932fdSDon Brace } 4367303932fdSDon Brace } 4368303932fdSDon Brace 4369960a30e7SStephen M. Cameron static __devinit void hpsa_enter_performant_mode(struct ctlr_info *h, 4370960a30e7SStephen M. Cameron u32 use_short_tags) 4371303932fdSDon Brace { 43726c311b57SStephen M. Cameron int i; 43736c311b57SStephen M. Cameron unsigned long register_value; 4374def342bdSStephen M. Cameron 4375def342bdSStephen M. Cameron /* This is a bit complicated. There are 8 registers on 4376def342bdSStephen M. Cameron * the controller which we write to to tell it 8 different 4377def342bdSStephen M. Cameron * sizes of commands which there may be. It's a way of 4378def342bdSStephen M. Cameron * reducing the DMA done to fetch each command. Encoded into 4379def342bdSStephen M. Cameron * each command's tag are 3 bits which communicate to the controller 4380def342bdSStephen M. Cameron * which of the eight sizes that command fits within. The size of 4381def342bdSStephen M. Cameron * each command depends on how many scatter gather entries there are. 4382def342bdSStephen M. Cameron * Each SG entry requires 16 bytes. The eight registers are programmed 4383def342bdSStephen M. Cameron * with the number of 16-byte blocks a command of that size requires. 4384def342bdSStephen M. Cameron * The smallest command possible requires 5 such 16 byte blocks. 4385def342bdSStephen M. Cameron * the largest command possible requires MAXSGENTRIES + 4 16-byte 4386def342bdSStephen M. Cameron * blocks. Note, this only extends to the SG entries contained 4387def342bdSStephen M. Cameron * within the command block, and does not extend to chained blocks 4388def342bdSStephen M. Cameron * of SG elements. bft[] contains the eight values we write to 4389def342bdSStephen M. Cameron * the registers. They are not evenly distributed, but have more 4390def342bdSStephen M. Cameron * sizes for small commands, and fewer sizes for larger commands. 4391def342bdSStephen M. Cameron */ 4392def342bdSStephen M. Cameron int bft[8] = {5, 6, 8, 10, 12, 20, 28, MAXSGENTRIES + 4}; 4393def342bdSStephen M. Cameron BUILD_BUG_ON(28 > MAXSGENTRIES + 4); 4394303932fdSDon Brace /* 5 = 1 s/g entry or 4k 4395303932fdSDon Brace * 6 = 2 s/g entry or 8k 4396303932fdSDon Brace * 8 = 4 s/g entry or 16k 4397303932fdSDon Brace * 10 = 6 s/g entry or 24k 4398303932fdSDon Brace */ 4399303932fdSDon Brace 4400303932fdSDon Brace h->reply_pool_wraparound = 1; /* spec: init to 1 */ 4401303932fdSDon Brace 4402303932fdSDon Brace /* Controller spec: zero out this buffer. */ 4403303932fdSDon Brace memset(h->reply_pool, 0, h->reply_pool_size); 4404303932fdSDon Brace h->reply_pool_head = h->reply_pool; 4405303932fdSDon Brace 4406303932fdSDon Brace bft[7] = h->max_sg_entries + 4; 4407303932fdSDon Brace calc_bucket_map(bft, ARRAY_SIZE(bft), 32, h->blockFetchTable); 4408303932fdSDon Brace for (i = 0; i < 8; i++) 4409303932fdSDon Brace writel(bft[i], &h->transtable->BlockFetch[i]); 4410303932fdSDon Brace 4411303932fdSDon Brace /* size of controller ring buffer */ 4412303932fdSDon Brace writel(h->max_commands, &h->transtable->RepQSize); 4413303932fdSDon Brace writel(1, &h->transtable->RepQCount); 4414303932fdSDon Brace writel(0, &h->transtable->RepQCtrAddrLow32); 4415303932fdSDon Brace writel(0, &h->transtable->RepQCtrAddrHigh32); 4416303932fdSDon Brace writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32); 4417303932fdSDon Brace writel(0, &h->transtable->RepQAddr0High32); 4418960a30e7SStephen M. Cameron writel(CFGTBL_Trans_Performant | use_short_tags, 4419303932fdSDon Brace &(h->cfgtable->HostWrite.TransportRequest)); 4420303932fdSDon Brace writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL); 44213f4336f3SStephen M. Cameron hpsa_wait_for_mode_change_ack(h); 4422303932fdSDon Brace register_value = readl(&(h->cfgtable->TransportActive)); 4423303932fdSDon Brace if (!(register_value & CFGTBL_Trans_Performant)) { 4424303932fdSDon Brace dev_warn(&h->pdev->dev, "unable to get board into" 4425303932fdSDon Brace " performant mode\n"); 4426303932fdSDon Brace return; 4427303932fdSDon Brace } 4428960a30e7SStephen M. Cameron /* Change the access methods to the performant access methods */ 4429960a30e7SStephen M. Cameron h->access = SA5_performant_access; 4430960a30e7SStephen M. Cameron h->transMethod = CFGTBL_Trans_Performant; 44316c311b57SStephen M. Cameron } 44326c311b57SStephen M. Cameron 44336c311b57SStephen M. Cameron static __devinit void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h) 44346c311b57SStephen M. Cameron { 44356c311b57SStephen M. Cameron u32 trans_support; 44366c311b57SStephen M. Cameron 443702ec19c8SStephen M. Cameron if (hpsa_simple_mode) 443802ec19c8SStephen M. Cameron return; 443902ec19c8SStephen M. Cameron 44406c311b57SStephen M. Cameron trans_support = readl(&(h->cfgtable->TransportSupport)); 44416c311b57SStephen M. Cameron if (!(trans_support & PERFORMANT_MODE)) 44426c311b57SStephen M. Cameron return; 44436c311b57SStephen M. Cameron 4444cba3d38bSStephen M. Cameron hpsa_get_max_perf_mode_cmds(h); 44456c311b57SStephen M. Cameron h->max_sg_entries = 32; 44466c311b57SStephen M. Cameron /* Performant mode ring buffer and supporting data structures */ 44476c311b57SStephen M. Cameron h->reply_pool_size = h->max_commands * sizeof(u64); 44486c311b57SStephen M. Cameron h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size, 44496c311b57SStephen M. Cameron &(h->reply_pool_dhandle)); 44506c311b57SStephen M. Cameron 44516c311b57SStephen M. Cameron /* Need a block fetch table for performant mode */ 44526c311b57SStephen M. Cameron h->blockFetchTable = kmalloc(((h->max_sg_entries+1) * 44536c311b57SStephen M. Cameron sizeof(u32)), GFP_KERNEL); 44546c311b57SStephen M. Cameron 44556c311b57SStephen M. Cameron if ((h->reply_pool == NULL) 44566c311b57SStephen M. Cameron || (h->blockFetchTable == NULL)) 44576c311b57SStephen M. Cameron goto clean_up; 44586c311b57SStephen M. Cameron 4459960a30e7SStephen M. Cameron hpsa_enter_performant_mode(h, 4460960a30e7SStephen M. Cameron trans_support & CFGTBL_Trans_use_short_tags); 4461303932fdSDon Brace 4462303932fdSDon Brace return; 4463303932fdSDon Brace 4464303932fdSDon Brace clean_up: 4465303932fdSDon Brace if (h->reply_pool) 4466303932fdSDon Brace pci_free_consistent(h->pdev, h->reply_pool_size, 4467303932fdSDon Brace h->reply_pool, h->reply_pool_dhandle); 4468303932fdSDon Brace kfree(h->blockFetchTable); 4469303932fdSDon Brace } 4470303932fdSDon Brace 4471edd16368SStephen M. Cameron /* 4472edd16368SStephen M. Cameron * This is it. Register the PCI driver information for the cards we control 4473edd16368SStephen M. Cameron * the OS will call our registered routines when it finds one of our cards. 4474edd16368SStephen M. Cameron */ 4475edd16368SStephen M. Cameron static int __init hpsa_init(void) 4476edd16368SStephen M. Cameron { 447731468401SMike Miller return pci_register_driver(&hpsa_pci_driver); 4478edd16368SStephen M. Cameron } 4479edd16368SStephen M. Cameron 4480edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void) 4481edd16368SStephen M. Cameron { 4482edd16368SStephen M. Cameron pci_unregister_driver(&hpsa_pci_driver); 4483edd16368SStephen M. Cameron } 4484edd16368SStephen M. Cameron 4485edd16368SStephen M. Cameron module_init(hpsa_init); 4486edd16368SStephen M. Cameron module_exit(hpsa_cleanup); 4487