Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *  linux/drivers/scsi/esas2r/esas2r_main.c
0003  *      For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
0004  *
0005  *  Copyright (c) 2001-2013 ATTO Technology, Inc.
0006  *  (mailto:linuxdrivers@attotech.com)
0007  *
0008  * This program is free software; you can redistribute it and/or
0009  * modify it under the terms of the GNU General Public License
0010  * as published by the Free Software Foundation; either version 2
0011  * of the License, or (at your option) any later version.
0012  *
0013  * This program is distributed in the hope that it will be useful,
0014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016  * GNU General Public License for more details.
0017  *
0018  * NO WARRANTY
0019  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
0020  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
0021  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
0022  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
0023  * solely responsible for determining the appropriateness of using and
0024  * distributing the Program and assumes all risks associated with its
0025  * exercise of rights under this Agreement, including but not limited to
0026  * the risks and costs of program errors, damage to or loss of data,
0027  * programs or equipment, and unavailability or interruption of operations.
0028  *
0029  * DISCLAIMER OF LIABILITY
0030  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
0031  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0032  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
0033  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
0034  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
0035  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
0036  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
0037  *
0038  * You should have received a copy of the GNU General Public License
0039  * along with this program; if not, write to the Free Software
0040  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
0041  * USA.
0042  */
0043 
0044 #include "esas2r.h"
0045 
0046 MODULE_DESCRIPTION(ESAS2R_DRVR_NAME ": " ESAS2R_LONGNAME " driver");
0047 MODULE_AUTHOR("ATTO Technology, Inc.");
0048 MODULE_LICENSE("GPL");
0049 MODULE_VERSION(ESAS2R_VERSION_STR);
0050 
0051 /* global definitions */
0052 
0053 static int found_adapters;
0054 struct esas2r_adapter *esas2r_adapters[MAX_ADAPTERS];
0055 
0056 #define ESAS2R_VDA_EVENT_PORT1       54414
0057 #define ESAS2R_VDA_EVENT_PORT2       54415
0058 #define ESAS2R_VDA_EVENT_SOCK_COUNT  2
0059 
0060 static struct esas2r_adapter *esas2r_adapter_from_kobj(struct kobject *kobj)
0061 {
0062     struct device *dev = container_of(kobj, struct device, kobj);
0063     struct Scsi_Host *host = class_to_shost(dev);
0064 
0065     return (struct esas2r_adapter *)host->hostdata;
0066 }
0067 
0068 static ssize_t read_fw(struct file *file, struct kobject *kobj,
0069                struct bin_attribute *attr,
0070                char *buf, loff_t off, size_t count)
0071 {
0072     struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
0073 
0074     return esas2r_read_fw(a, buf, off, count);
0075 }
0076 
0077 static ssize_t write_fw(struct file *file, struct kobject *kobj,
0078             struct bin_attribute *attr,
0079             char *buf, loff_t off, size_t count)
0080 {
0081     struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
0082 
0083     return esas2r_write_fw(a, buf, off, count);
0084 }
0085 
0086 static ssize_t read_fs(struct file *file, struct kobject *kobj,
0087                struct bin_attribute *attr,
0088                char *buf, loff_t off, size_t count)
0089 {
0090     struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
0091 
0092     return esas2r_read_fs(a, buf, off, count);
0093 }
0094 
0095 static ssize_t write_fs(struct file *file, struct kobject *kobj,
0096             struct bin_attribute *attr,
0097             char *buf, loff_t off, size_t count)
0098 {
0099     struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
0100     int length = min(sizeof(struct esas2r_ioctl_fs), count);
0101     int result = 0;
0102 
0103     result = esas2r_write_fs(a, buf, off, count);
0104 
0105     if (result < 0)
0106         result = 0;
0107 
0108     return length;
0109 }
0110 
0111 static ssize_t read_vda(struct file *file, struct kobject *kobj,
0112             struct bin_attribute *attr,
0113             char *buf, loff_t off, size_t count)
0114 {
0115     struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
0116 
0117     return esas2r_read_vda(a, buf, off, count);
0118 }
0119 
0120 static ssize_t write_vda(struct file *file, struct kobject *kobj,
0121              struct bin_attribute *attr,
0122              char *buf, loff_t off, size_t count)
0123 {
0124     struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
0125 
0126     return esas2r_write_vda(a, buf, off, count);
0127 }
0128 
0129 static ssize_t read_live_nvram(struct file *file, struct kobject *kobj,
0130                    struct bin_attribute *attr,
0131                    char *buf, loff_t off, size_t count)
0132 {
0133     struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
0134     int length = min_t(size_t, sizeof(struct esas2r_sas_nvram), PAGE_SIZE);
0135 
0136     memcpy(buf, a->nvram, length);
0137     return length;
0138 }
0139 
0140 static ssize_t write_live_nvram(struct file *file, struct kobject *kobj,
0141                 struct bin_attribute *attr,
0142                 char *buf, loff_t off, size_t count)
0143 {
0144     struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
0145     struct esas2r_request *rq;
0146     int result = -EFAULT;
0147 
0148     rq = esas2r_alloc_request(a);
0149     if (rq == NULL)
0150         return -ENOMEM;
0151 
0152     if (esas2r_write_params(a, rq, (struct esas2r_sas_nvram *)buf))
0153         result = count;
0154 
0155     esas2r_free_request(a, rq);
0156 
0157     return result;
0158 }
0159 
0160 static ssize_t read_default_nvram(struct file *file, struct kobject *kobj,
0161                   struct bin_attribute *attr,
0162                   char *buf, loff_t off, size_t count)
0163 {
0164     struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
0165 
0166     esas2r_nvram_get_defaults(a, (struct esas2r_sas_nvram *)buf);
0167 
0168     return sizeof(struct esas2r_sas_nvram);
0169 }
0170 
0171 static ssize_t read_hw(struct file *file, struct kobject *kobj,
0172                struct bin_attribute *attr,
0173                char *buf, loff_t off, size_t count)
0174 {
0175     struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
0176     int length = min_t(size_t, sizeof(struct atto_ioctl), PAGE_SIZE);
0177 
0178     if (!a->local_atto_ioctl)
0179         return -ENOMEM;
0180 
0181     if (handle_hba_ioctl(a, a->local_atto_ioctl) != IOCTL_SUCCESS)
0182         return -ENOMEM;
0183 
0184     memcpy(buf, a->local_atto_ioctl, length);
0185 
0186     return length;
0187 }
0188 
0189 static ssize_t write_hw(struct file *file, struct kobject *kobj,
0190             struct bin_attribute *attr,
0191             char *buf, loff_t off, size_t count)
0192 {
0193     struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
0194     int length = min(sizeof(struct atto_ioctl), count);
0195 
0196     if (!a->local_atto_ioctl) {
0197         a->local_atto_ioctl = kmalloc(sizeof(struct atto_ioctl),
0198                           GFP_KERNEL);
0199         if (a->local_atto_ioctl == NULL) {
0200             esas2r_log(ESAS2R_LOG_WARN,
0201                    "write_hw kzalloc failed for %zu bytes",
0202                    sizeof(struct atto_ioctl));
0203             return -ENOMEM;
0204         }
0205     }
0206 
0207     memset(a->local_atto_ioctl, 0, sizeof(struct atto_ioctl));
0208     memcpy(a->local_atto_ioctl, buf, length);
0209 
0210     return length;
0211 }
0212 
0213 #define ESAS2R_RW_BIN_ATTR(_name) \
0214     struct bin_attribute bin_attr_ ## _name = { \
0215         .attr   = \
0216         { .name = __stringify(_name), .mode  = S_IRUSR | S_IWUSR }, \
0217         .size   = 0, \
0218         .read   = read_ ## _name, \
0219         .write  = write_ ## _name }
0220 
0221 ESAS2R_RW_BIN_ATTR(fw);
0222 ESAS2R_RW_BIN_ATTR(fs);
0223 ESAS2R_RW_BIN_ATTR(vda);
0224 ESAS2R_RW_BIN_ATTR(hw);
0225 ESAS2R_RW_BIN_ATTR(live_nvram);
0226 
0227 struct bin_attribute bin_attr_default_nvram = {
0228     .attr   = { .name = "default_nvram", .mode = S_IRUGO },
0229     .size   = 0,
0230     .read   = read_default_nvram,
0231     .write  = NULL
0232 };
0233 
0234 static struct scsi_host_template driver_template = {
0235     .module             = THIS_MODULE,
0236     .show_info          = esas2r_show_info,
0237     .name               = ESAS2R_LONGNAME,
0238     .info               = esas2r_info,
0239     .ioctl              = esas2r_ioctl,
0240     .queuecommand           = esas2r_queuecommand,
0241     .eh_abort_handler       = esas2r_eh_abort,
0242     .eh_device_reset_handler    = esas2r_device_reset,
0243     .eh_bus_reset_handler       = esas2r_bus_reset,
0244     .eh_host_reset_handler      = esas2r_host_reset,
0245     .eh_target_reset_handler    = esas2r_target_reset,
0246     .can_queue          = 128,
0247     .this_id            = -1,
0248     .sg_tablesize           = SG_CHUNK_SIZE,
0249     .cmd_per_lun            =
0250         ESAS2R_DEFAULT_CMD_PER_LUN,
0251     .present            = 0,
0252     .emulated           = 0,
0253     .proc_name          = ESAS2R_DRVR_NAME,
0254     .change_queue_depth     = scsi_change_queue_depth,
0255     .max_sectors            = 0xFFFF,
0256 };
0257 
0258 int sgl_page_size = 512;
0259 module_param(sgl_page_size, int, 0);
0260 MODULE_PARM_DESC(sgl_page_size,
0261          "Scatter/gather list (SGL) page size in number of S/G "
0262          "entries.  If your application is doing a lot of very large "
0263          "transfers, you may want to increase the SGL page size.  "
0264          "Default 512.");
0265 
0266 int num_sg_lists = 1024;
0267 module_param(num_sg_lists, int, 0);
0268 MODULE_PARM_DESC(num_sg_lists,
0269          "Number of scatter/gather lists.  Default 1024.");
0270 
0271 int sg_tablesize = SG_CHUNK_SIZE;
0272 module_param(sg_tablesize, int, 0);
0273 MODULE_PARM_DESC(sg_tablesize,
0274          "Maximum number of entries in a scatter/gather table.");
0275 
0276 int num_requests = 256;
0277 module_param(num_requests, int, 0);
0278 MODULE_PARM_DESC(num_requests,
0279          "Number of requests.  Default 256.");
0280 
0281 int num_ae_requests = 4;
0282 module_param(num_ae_requests, int, 0);
0283 MODULE_PARM_DESC(num_ae_requests,
0284          "Number of VDA asynchronous event requests.  Default 4.");
0285 
0286 int cmd_per_lun = ESAS2R_DEFAULT_CMD_PER_LUN;
0287 module_param(cmd_per_lun, int, 0);
0288 MODULE_PARM_DESC(cmd_per_lun,
0289          "Maximum number of commands per LUN.  Default "
0290          DEFINED_NUM_TO_STR(ESAS2R_DEFAULT_CMD_PER_LUN) ".");
0291 
0292 int can_queue = 128;
0293 module_param(can_queue, int, 0);
0294 MODULE_PARM_DESC(can_queue,
0295          "Maximum number of commands per adapter.  Default 128.");
0296 
0297 int esas2r_max_sectors = 0xFFFF;
0298 module_param(esas2r_max_sectors, int, 0);
0299 MODULE_PARM_DESC(esas2r_max_sectors,
0300          "Maximum number of disk sectors in a single data transfer.  "
0301          "Default 65535 (largest possible setting).");
0302 
0303 int interrupt_mode = 1;
0304 module_param(interrupt_mode, int, 0);
0305 MODULE_PARM_DESC(interrupt_mode,
0306          "Defines the interrupt mode to use.  0 for legacy"
0307          ", 1 for MSI.  Default is MSI (1).");
0308 
0309 static const struct pci_device_id
0310     esas2r_pci_table[] = {
0311     { ATTO_VENDOR_ID, 0x0049,     ATTO_VENDOR_ID, 0x0049,
0312       0,
0313       0, 0 },
0314     { ATTO_VENDOR_ID, 0x0049,     ATTO_VENDOR_ID, 0x004A,
0315       0,
0316       0, 0 },
0317     { ATTO_VENDOR_ID, 0x0049,     ATTO_VENDOR_ID, 0x004B,
0318       0,
0319       0, 0 },
0320     { ATTO_VENDOR_ID, 0x0049,     ATTO_VENDOR_ID, 0x004C,
0321       0,
0322       0, 0 },
0323     { ATTO_VENDOR_ID, 0x0049,     ATTO_VENDOR_ID, 0x004D,
0324       0,
0325       0, 0 },
0326     { ATTO_VENDOR_ID, 0x0049,     ATTO_VENDOR_ID, 0x004E,
0327       0,
0328       0, 0 },
0329     { 0,          0,          0,          0,
0330       0,
0331       0, 0 }
0332 };
0333 
0334 MODULE_DEVICE_TABLE(pci, esas2r_pci_table);
0335 
0336 static int
0337 esas2r_probe(struct pci_dev *pcid, const struct pci_device_id *id);
0338 
0339 static void
0340 esas2r_remove(struct pci_dev *pcid);
0341 
0342 static struct pci_driver
0343     esas2r_pci_driver = {
0344     .name       = ESAS2R_DRVR_NAME,
0345     .id_table   = esas2r_pci_table,
0346     .probe      = esas2r_probe,
0347     .remove     = esas2r_remove,
0348     .driver.pm  = &esas2r_pm_ops,
0349 };
0350 
0351 static int esas2r_probe(struct pci_dev *pcid,
0352             const struct pci_device_id *id)
0353 {
0354     struct Scsi_Host *host = NULL;
0355     struct esas2r_adapter *a;
0356     int err;
0357 
0358     size_t host_alloc_size = sizeof(struct esas2r_adapter)
0359                  + ((num_requests) +
0360                     1) * sizeof(struct esas2r_request);
0361 
0362     esas2r_log_dev(ESAS2R_LOG_DEBG, &(pcid->dev),
0363                "esas2r_probe() 0x%02x 0x%02x 0x%02x 0x%02x",
0364                pcid->vendor,
0365                pcid->device,
0366                pcid->subsystem_vendor,
0367                pcid->subsystem_device);
0368 
0369     esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
0370                "before pci_enable_device() "
0371                "enable_cnt: %d",
0372                pcid->enable_cnt.counter);
0373 
0374     err = pci_enable_device(pcid);
0375     if (err != 0) {
0376         esas2r_log_dev(ESAS2R_LOG_CRIT, &(pcid->dev),
0377                    "pci_enable_device() FAIL (%d)",
0378                    err);
0379         return -ENODEV;
0380     }
0381 
0382     esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
0383                "pci_enable_device() OK");
0384     esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
0385                "after pci_enable_device() enable_cnt: %d",
0386                pcid->enable_cnt.counter);
0387 
0388     host = scsi_host_alloc(&driver_template, host_alloc_size);
0389     if (host == NULL) {
0390         esas2r_log(ESAS2R_LOG_CRIT, "scsi_host_alloc() FAIL");
0391         return -ENODEV;
0392     }
0393 
0394     memset(host->hostdata, 0, host_alloc_size);
0395 
0396     a = (struct esas2r_adapter *)host->hostdata;
0397 
0398     esas2r_log(ESAS2R_LOG_INFO, "scsi_host_alloc() OK host: %p", host);
0399 
0400     /* override max LUN and max target id */
0401 
0402     host->max_id = ESAS2R_MAX_ID + 1;
0403     host->max_lun = 255;
0404 
0405     /* we can handle 16-byte CDbs */
0406 
0407     host->max_cmd_len = 16;
0408 
0409     host->can_queue = can_queue;
0410     host->cmd_per_lun = cmd_per_lun;
0411     host->this_id = host->max_id + 1;
0412     host->max_channel = 0;
0413     host->unique_id = found_adapters;
0414     host->sg_tablesize = sg_tablesize;
0415     host->max_sectors = esas2r_max_sectors;
0416 
0417     /* set to bus master for BIOses that don't do it for us */
0418 
0419     esas2r_log(ESAS2R_LOG_INFO, "pci_set_master() called");
0420 
0421     pci_set_master(pcid);
0422 
0423     if (!esas2r_init_adapter(host, pcid, found_adapters)) {
0424         esas2r_log(ESAS2R_LOG_CRIT,
0425                "unable to initialize device at PCI bus %x:%x",
0426                pcid->bus->number,
0427                pcid->devfn);
0428 
0429         esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
0430                    "scsi_host_put() called");
0431 
0432         scsi_host_put(host);
0433 
0434         return 0;
0435 
0436     }
0437 
0438     esas2r_log(ESAS2R_LOG_INFO, "pci_set_drvdata(%p, %p) called", pcid,
0439            host->hostdata);
0440 
0441     pci_set_drvdata(pcid, host);
0442 
0443     esas2r_log(ESAS2R_LOG_INFO, "scsi_add_host() called");
0444 
0445     err = scsi_add_host(host, &pcid->dev);
0446 
0447     if (err) {
0448         esas2r_log(ESAS2R_LOG_CRIT, "scsi_add_host returned %d", err);
0449         esas2r_log_dev(ESAS2R_LOG_CRIT, &(host->shost_gendev),
0450                    "scsi_add_host() FAIL");
0451 
0452         esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
0453                    "scsi_host_put() called");
0454 
0455         scsi_host_put(host);
0456 
0457         esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
0458                    "pci_set_drvdata(%p, NULL) called",
0459                    pcid);
0460 
0461         pci_set_drvdata(pcid, NULL);
0462 
0463         return -ENODEV;
0464     }
0465 
0466 
0467     esas2r_fw_event_on(a);
0468 
0469     esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
0470                "scsi_scan_host() called");
0471 
0472     scsi_scan_host(host);
0473 
0474     /* Add sysfs binary files */
0475     if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fw))
0476         esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
0477                    "Failed to create sysfs binary file: fw");
0478     else
0479         a->sysfs_fw_created = 1;
0480 
0481     if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fs))
0482         esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
0483                    "Failed to create sysfs binary file: fs");
0484     else
0485         a->sysfs_fs_created = 1;
0486 
0487     if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_vda))
0488         esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
0489                    "Failed to create sysfs binary file: vda");
0490     else
0491         a->sysfs_vda_created = 1;
0492 
0493     if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_hw))
0494         esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
0495                    "Failed to create sysfs binary file: hw");
0496     else
0497         a->sysfs_hw_created = 1;
0498 
0499     if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_live_nvram))
0500         esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
0501                    "Failed to create sysfs binary file: live_nvram");
0502     else
0503         a->sysfs_live_nvram_created = 1;
0504 
0505     if (sysfs_create_bin_file(&host->shost_dev.kobj,
0506                   &bin_attr_default_nvram))
0507         esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
0508                    "Failed to create sysfs binary file: default_nvram");
0509     else
0510         a->sysfs_default_nvram_created = 1;
0511 
0512     found_adapters++;
0513 
0514     return 0;
0515 }
0516 
0517 static void esas2r_remove(struct pci_dev *pdev)
0518 {
0519     struct Scsi_Host *host = pci_get_drvdata(pdev);
0520     struct esas2r_adapter *a = (struct esas2r_adapter *)host->hostdata;
0521 
0522     esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
0523                "esas2r_remove(%p) called; "
0524                "host:%p", pdev,
0525                host);
0526 
0527     esas2r_kill_adapter(a->index);
0528     found_adapters--;
0529 }
0530 
0531 static int __init esas2r_init(void)
0532 {
0533     int i;
0534 
0535     esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
0536 
0537     /* verify valid parameters */
0538 
0539     if (can_queue < 1) {
0540         esas2r_log(ESAS2R_LOG_WARN,
0541                "warning: can_queue must be at least 1, value "
0542                "forced.");
0543         can_queue = 1;
0544     } else if (can_queue > 2048) {
0545         esas2r_log(ESAS2R_LOG_WARN,
0546                "warning: can_queue must be no larger than 2048, "
0547                "value forced.");
0548         can_queue = 2048;
0549     }
0550 
0551     if (cmd_per_lun < 1) {
0552         esas2r_log(ESAS2R_LOG_WARN,
0553                "warning: cmd_per_lun must be at least 1, value "
0554                "forced.");
0555         cmd_per_lun = 1;
0556     } else if (cmd_per_lun > 2048) {
0557         esas2r_log(ESAS2R_LOG_WARN,
0558                "warning: cmd_per_lun must be no larger than "
0559                "2048, value forced.");
0560         cmd_per_lun = 2048;
0561     }
0562 
0563     if (sg_tablesize < 32) {
0564         esas2r_log(ESAS2R_LOG_WARN,
0565                "warning: sg_tablesize must be at least 32, "
0566                "value forced.");
0567         sg_tablesize = 32;
0568     }
0569 
0570     if (esas2r_max_sectors < 1) {
0571         esas2r_log(ESAS2R_LOG_WARN,
0572                "warning: esas2r_max_sectors must be at least "
0573                "1, value forced.");
0574         esas2r_max_sectors = 1;
0575     } else if (esas2r_max_sectors > 0xffff) {
0576         esas2r_log(ESAS2R_LOG_WARN,
0577                "warning: esas2r_max_sectors must be no larger "
0578                "than 0xffff, value forced.");
0579         esas2r_max_sectors = 0xffff;
0580     }
0581 
0582     sgl_page_size &= ~(ESAS2R_SGL_ALIGN - 1);
0583 
0584     if (sgl_page_size < SGL_PG_SZ_MIN)
0585         sgl_page_size = SGL_PG_SZ_MIN;
0586     else if (sgl_page_size > SGL_PG_SZ_MAX)
0587         sgl_page_size = SGL_PG_SZ_MAX;
0588 
0589     if (num_sg_lists < NUM_SGL_MIN)
0590         num_sg_lists = NUM_SGL_MIN;
0591     else if (num_sg_lists > NUM_SGL_MAX)
0592         num_sg_lists = NUM_SGL_MAX;
0593 
0594     if (num_requests < NUM_REQ_MIN)
0595         num_requests = NUM_REQ_MIN;
0596     else if (num_requests > NUM_REQ_MAX)
0597         num_requests = NUM_REQ_MAX;
0598 
0599     if (num_ae_requests < NUM_AE_MIN)
0600         num_ae_requests = NUM_AE_MIN;
0601     else if (num_ae_requests > NUM_AE_MAX)
0602         num_ae_requests = NUM_AE_MAX;
0603 
0604     /* set up other globals */
0605 
0606     for (i = 0; i < MAX_ADAPTERS; i++)
0607         esas2r_adapters[i] = NULL;
0608 
0609     return pci_register_driver(&esas2r_pci_driver);
0610 }
0611 
0612 /* Handle ioctl calls to "/proc/scsi/esas2r/ATTOnode" */
0613 static const struct file_operations esas2r_proc_fops = {
0614     .compat_ioctl   = compat_ptr_ioctl,
0615     .unlocked_ioctl = esas2r_proc_ioctl,
0616 };
0617 
0618 static const struct proc_ops esas2r_proc_ops = {
0619     .proc_lseek     = default_llseek,
0620     .proc_ioctl     = esas2r_proc_ioctl,
0621 #ifdef CONFIG_COMPAT
0622     .proc_compat_ioctl  = compat_ptr_ioctl,
0623 #endif
0624 };
0625 
0626 static struct Scsi_Host *esas2r_proc_host;
0627 static int esas2r_proc_major;
0628 
0629 long esas2r_proc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
0630 {
0631     return esas2r_ioctl_handler(esas2r_proc_host->hostdata,
0632                     cmd, (void __user *)arg);
0633 }
0634 
0635 static void __exit esas2r_exit(void)
0636 {
0637     esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
0638 
0639     if (esas2r_proc_major > 0) {
0640         esas2r_log(ESAS2R_LOG_INFO, "unregister proc");
0641 
0642         remove_proc_entry(ATTONODE_NAME,
0643                   esas2r_proc_host->hostt->proc_dir);
0644         unregister_chrdev(esas2r_proc_major, ESAS2R_DRVR_NAME);
0645 
0646         esas2r_proc_major = 0;
0647     }
0648 
0649     esas2r_log(ESAS2R_LOG_INFO, "pci_unregister_driver() called");
0650 
0651     pci_unregister_driver(&esas2r_pci_driver);
0652 }
0653 
0654 int esas2r_show_info(struct seq_file *m, struct Scsi_Host *sh)
0655 {
0656     struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
0657 
0658     struct esas2r_target *t;
0659     int dev_count = 0;
0660 
0661     esas2r_log(ESAS2R_LOG_DEBG, "esas2r_show_info (%p,%d)", m, sh->host_no);
0662 
0663     seq_printf(m, ESAS2R_LONGNAME "\n"
0664            "Driver version: "ESAS2R_VERSION_STR "\n"
0665            "Flash version: %s\n"
0666            "Firmware version: %s\n"
0667            "Copyright "ESAS2R_COPYRIGHT_YEARS "\n"
0668            "http://www.attotech.com\n"
0669            "\n",
0670            a->flash_rev,
0671            a->fw_rev[0] ? a->fw_rev : "(none)");
0672 
0673 
0674     seq_printf(m, "Adapter information:\n"
0675            "--------------------\n"
0676            "Model: %s\n"
0677            "SAS address: %02X%02X%02X%02X:%02X%02X%02X%02X\n",
0678            esas2r_get_model_name(a),
0679            a->nvram->sas_addr[0],
0680            a->nvram->sas_addr[1],
0681            a->nvram->sas_addr[2],
0682            a->nvram->sas_addr[3],
0683            a->nvram->sas_addr[4],
0684            a->nvram->sas_addr[5],
0685            a->nvram->sas_addr[6],
0686            a->nvram->sas_addr[7]);
0687 
0688     seq_puts(m, "\n"
0689            "Discovered devices:\n"
0690            "\n"
0691            "   #  Target ID\n"
0692            "---------------\n");
0693 
0694     for (t = a->targetdb; t < a->targetdb_end; t++)
0695         if (t->buffered_target_state == TS_PRESENT) {
0696             seq_printf(m, " %3d   %3d\n",
0697                    ++dev_count,
0698                    (u16)(uintptr_t)(t - a->targetdb));
0699         }
0700 
0701     if (dev_count == 0)
0702         seq_puts(m, "none\n");
0703 
0704     seq_putc(m, '\n');
0705     return 0;
0706 
0707 }
0708 
0709 const char *esas2r_info(struct Scsi_Host *sh)
0710 {
0711     struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
0712     static char esas2r_info_str[512];
0713 
0714     esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
0715                "esas2r_info() called");
0716 
0717     /*
0718      * if we haven't done so already, register as a char driver
0719      * and stick a node under "/proc/scsi/esas2r/ATTOnode"
0720      */
0721 
0722     if (esas2r_proc_major <= 0) {
0723         esas2r_proc_host = sh;
0724 
0725         esas2r_proc_major = register_chrdev(0, ESAS2R_DRVR_NAME,
0726                             &esas2r_proc_fops);
0727 
0728         esas2r_log_dev(ESAS2R_LOG_DEBG, &(sh->shost_gendev),
0729                    "register_chrdev (major %d)",
0730                    esas2r_proc_major);
0731 
0732         if (esas2r_proc_major > 0) {
0733             struct proc_dir_entry *pde;
0734 
0735             pde = proc_create(ATTONODE_NAME, 0,
0736                       sh->hostt->proc_dir,
0737                       &esas2r_proc_ops);
0738 
0739             if (!pde) {
0740                 esas2r_log_dev(ESAS2R_LOG_WARN,
0741                            &(sh->shost_gendev),
0742                            "failed to create_proc_entry");
0743                 esas2r_proc_major = -1;
0744             }
0745         }
0746     }
0747 
0748     sprintf(esas2r_info_str,
0749         ESAS2R_LONGNAME " (bus 0x%02X, device 0x%02X, IRQ 0x%02X)"
0750         " driver version: "ESAS2R_VERSION_STR "  firmware version: "
0751         "%s\n",
0752         a->pcid->bus->number, a->pcid->devfn, a->pcid->irq,
0753         a->fw_rev[0] ? a->fw_rev : "(none)");
0754 
0755     return esas2r_info_str;
0756 }
0757 
0758 /* Callback for building a request scatter/gather list */
0759 static u32 get_physaddr_from_sgc(struct esas2r_sg_context *sgc, u64 *addr)
0760 {
0761     u32 len;
0762 
0763     if (likely(sgc->cur_offset == sgc->exp_offset)) {
0764         /*
0765          * the normal case: caller used all bytes from previous call, so
0766          * expected offset is the same as the current offset.
0767          */
0768 
0769         if (sgc->sgel_count < sgc->num_sgel) {
0770             /* retrieve next segment, except for first time */
0771             if (sgc->exp_offset > (u8 *)0) {
0772                 /* advance current segment */
0773                 sgc->cur_sgel = sg_next(sgc->cur_sgel);
0774                 ++(sgc->sgel_count);
0775             }
0776 
0777 
0778             len = sg_dma_len(sgc->cur_sgel);
0779             (*addr) = sg_dma_address(sgc->cur_sgel);
0780 
0781             /* save the total # bytes returned to caller so far */
0782             sgc->exp_offset += len;
0783 
0784         } else {
0785             len = 0;
0786         }
0787     } else if (sgc->cur_offset < sgc->exp_offset) {
0788         /*
0789          * caller did not use all bytes from previous call. need to
0790          * compute the address based on current segment.
0791          */
0792 
0793         len = sg_dma_len(sgc->cur_sgel);
0794         (*addr) = sg_dma_address(sgc->cur_sgel);
0795 
0796         sgc->exp_offset -= len;
0797 
0798         /* calculate PA based on prev segment address and offsets */
0799         *addr = *addr +
0800             (sgc->cur_offset - sgc->exp_offset);
0801 
0802         sgc->exp_offset += len;
0803 
0804         /* re-calculate length based on offset */
0805         len = lower_32_bits(
0806             sgc->exp_offset - sgc->cur_offset);
0807     } else {   /* if ( sgc->cur_offset > sgc->exp_offset ) */
0808            /*
0809             * we don't expect the caller to skip ahead.
0810             * cur_offset will never exceed the len we return
0811             */
0812         len = 0;
0813     }
0814 
0815     return len;
0816 }
0817 
0818 int esas2r_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
0819 {
0820     struct esas2r_adapter *a =
0821         (struct esas2r_adapter *)cmd->device->host->hostdata;
0822     struct esas2r_request *rq;
0823     struct esas2r_sg_context sgc;
0824     unsigned bufflen;
0825 
0826     /* Assume success, if it fails we will fix the result later. */
0827     cmd->result = DID_OK << 16;
0828 
0829     if (unlikely(test_bit(AF_DEGRADED_MODE, &a->flags))) {
0830         cmd->result = DID_NO_CONNECT << 16;
0831         scsi_done(cmd);
0832         return 0;
0833     }
0834 
0835     rq = esas2r_alloc_request(a);
0836     if (unlikely(rq == NULL)) {
0837         esas2r_debug("esas2r_alloc_request failed");
0838         return SCSI_MLQUEUE_HOST_BUSY;
0839     }
0840 
0841     rq->cmd = cmd;
0842     bufflen = scsi_bufflen(cmd);
0843 
0844     if (likely(bufflen != 0)) {
0845         if (cmd->sc_data_direction == DMA_TO_DEVICE)
0846             rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD);
0847         else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
0848             rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD);
0849     }
0850 
0851     memcpy(rq->vrq->scsi.cdb, cmd->cmnd, cmd->cmd_len);
0852     rq->vrq->scsi.length = cpu_to_le32(bufflen);
0853     rq->target_id = cmd->device->id;
0854     rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
0855     rq->sense_buf = cmd->sense_buffer;
0856     rq->sense_len = SCSI_SENSE_BUFFERSIZE;
0857 
0858     esas2r_sgc_init(&sgc, a, rq, NULL);
0859 
0860     sgc.length = bufflen;
0861     sgc.cur_offset = NULL;
0862 
0863     sgc.cur_sgel = scsi_sglist(cmd);
0864     sgc.exp_offset = NULL;
0865     sgc.num_sgel = scsi_dma_map(cmd);
0866     sgc.sgel_count = 0;
0867 
0868     if (unlikely(sgc.num_sgel < 0)) {
0869         esas2r_free_request(a, rq);
0870         return SCSI_MLQUEUE_HOST_BUSY;
0871     }
0872 
0873     sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_from_sgc;
0874 
0875     if (unlikely(!esas2r_build_sg_list(a, rq, &sgc))) {
0876         scsi_dma_unmap(cmd);
0877         esas2r_free_request(a, rq);
0878         return SCSI_MLQUEUE_HOST_BUSY;
0879     }
0880 
0881     esas2r_debug("start request %p to %d:%d\n", rq, (int)cmd->device->id,
0882              (int)cmd->device->lun);
0883 
0884     esas2r_start_request(a, rq);
0885 
0886     return 0;
0887 }
0888 
0889 static void complete_task_management_request(struct esas2r_adapter *a,
0890                          struct esas2r_request *rq)
0891 {
0892     (*rq->task_management_status_ptr) = rq->req_stat;
0893     esas2r_free_request(a, rq);
0894 }
0895 
0896 /*
0897  * Searches the specified queue for the specified queue for the command
0898  * to abort.
0899  *
0900  * Return 0 on failure, 1 if command was not found, 2 if command was found
0901  */
0902 static int esas2r_check_active_queue(struct esas2r_adapter *a,
0903                      struct esas2r_request **abort_request,
0904                      struct scsi_cmnd *cmd,
0905                      struct list_head *queue)
0906 {
0907     bool found = false;
0908     struct esas2r_request *ar = *abort_request;
0909     struct esas2r_request *rq;
0910     struct list_head *element, *next;
0911 
0912     list_for_each_safe(element, next, queue) {
0913 
0914         rq = list_entry(element, struct esas2r_request, req_list);
0915 
0916         if (rq->cmd == cmd) {
0917 
0918             /* Found the request.  See what to do with it. */
0919             if (queue == &a->active_list) {
0920                 /*
0921                  * We are searching the active queue, which
0922                  * means that we need to send an abort request
0923                  * to the firmware.
0924                  */
0925                 ar = esas2r_alloc_request(a);
0926                 if (ar == NULL) {
0927                     esas2r_log_dev(ESAS2R_LOG_WARN,
0928                                &(a->host->shost_gendev),
0929                                "unable to allocate an abort request for cmd %p",
0930                                cmd);
0931                     return 0; /* Failure */
0932                 }
0933 
0934                 /*
0935                  * Task management request must be formatted
0936                  * with a lock held.
0937                  */
0938                 ar->sense_len = 0;
0939                 ar->vrq->scsi.length = 0;
0940                 ar->target_id = rq->target_id;
0941                 ar->vrq->scsi.flags |= cpu_to_le32(
0942                     (u8)le32_to_cpu(rq->vrq->scsi.flags));
0943 
0944                 memset(ar->vrq->scsi.cdb, 0,
0945                        sizeof(ar->vrq->scsi.cdb));
0946 
0947                 ar->vrq->scsi.flags |= cpu_to_le32(
0948                     FCP_CMND_TRM);
0949                 ar->vrq->scsi.u.abort_handle =
0950                     rq->vrq->scsi.handle;
0951             } else {
0952                 /*
0953                  * The request is pending but not active on
0954                  * the firmware.  Just free it now and we'll
0955                  * report the successful abort below.
0956                  */
0957                 list_del_init(&rq->req_list);
0958                 esas2r_free_request(a, rq);
0959             }
0960 
0961             found = true;
0962             break;
0963         }
0964 
0965     }
0966 
0967     if (!found)
0968         return 1;       /* Not found */
0969 
0970     return 2;               /* found */
0971 
0972 
0973 }
0974 
0975 int esas2r_eh_abort(struct scsi_cmnd *cmd)
0976 {
0977     struct esas2r_adapter *a =
0978         (struct esas2r_adapter *)cmd->device->host->hostdata;
0979     struct esas2r_request *abort_request = NULL;
0980     unsigned long flags;
0981     struct list_head *queue;
0982     int result;
0983 
0984     esas2r_log(ESAS2R_LOG_INFO, "eh_abort (%p)", cmd);
0985 
0986     if (test_bit(AF_DEGRADED_MODE, &a->flags)) {
0987         cmd->result = DID_ABORT << 16;
0988 
0989         scsi_set_resid(cmd, 0);
0990 
0991         scsi_done(cmd);
0992 
0993         return SUCCESS;
0994     }
0995 
0996     spin_lock_irqsave(&a->queue_lock, flags);
0997 
0998     /*
0999      * Run through the defer and active queues looking for the request
1000      * to abort.
1001      */
1002 
1003     queue = &a->defer_list;
1004 
1005 check_active_queue:
1006 
1007     result = esas2r_check_active_queue(a, &abort_request, cmd, queue);
1008 
1009     if (!result) {
1010         spin_unlock_irqrestore(&a->queue_lock, flags);
1011         return FAILED;
1012     } else if (result == 2 && (queue == &a->defer_list)) {
1013         queue = &a->active_list;
1014         goto check_active_queue;
1015     }
1016 
1017     spin_unlock_irqrestore(&a->queue_lock, flags);
1018 
1019     if (abort_request) {
1020         u8 task_management_status = RS_PENDING;
1021 
1022         /*
1023          * the request is already active, so we need to tell
1024          * the firmware to abort it and wait for the response.
1025          */
1026 
1027         abort_request->comp_cb = complete_task_management_request;
1028         abort_request->task_management_status_ptr =
1029             &task_management_status;
1030 
1031         esas2r_start_request(a, abort_request);
1032 
1033         if (atomic_read(&a->disable_cnt) == 0)
1034             esas2r_do_deferred_processes(a);
1035 
1036         while (task_management_status == RS_PENDING)
1037             msleep(10);
1038 
1039         /*
1040          * Once we get here, the original request will have been
1041          * completed by the firmware and the abort request will have
1042          * been cleaned up.  we're done!
1043          */
1044 
1045         return SUCCESS;
1046     }
1047 
1048     /*
1049      * If we get here, either we found the inactive request and
1050      * freed it, or we didn't find it at all.  Either way, success!
1051      */
1052 
1053     cmd->result = DID_ABORT << 16;
1054 
1055     scsi_set_resid(cmd, 0);
1056 
1057     scsi_done(cmd);
1058 
1059     return SUCCESS;
1060 }
1061 
1062 static int esas2r_host_bus_reset(struct scsi_cmnd *cmd, bool host_reset)
1063 {
1064     struct esas2r_adapter *a =
1065         (struct esas2r_adapter *)cmd->device->host->hostdata;
1066 
1067     if (test_bit(AF_DEGRADED_MODE, &a->flags))
1068         return FAILED;
1069 
1070     if (host_reset)
1071         esas2r_reset_adapter(a);
1072     else
1073         esas2r_reset_bus(a);
1074 
1075     /* above call sets the AF_OS_RESET flag.  wait for it to clear. */
1076 
1077     while (test_bit(AF_OS_RESET, &a->flags)) {
1078         msleep(10);
1079 
1080         if (test_bit(AF_DEGRADED_MODE, &a->flags))
1081             return FAILED;
1082     }
1083 
1084     if (test_bit(AF_DEGRADED_MODE, &a->flags))
1085         return FAILED;
1086 
1087     return SUCCESS;
1088 }
1089 
1090 int esas2r_host_reset(struct scsi_cmnd *cmd)
1091 {
1092     esas2r_log(ESAS2R_LOG_INFO, "host_reset (%p)", cmd);
1093 
1094     return esas2r_host_bus_reset(cmd, true);
1095 }
1096 
1097 int esas2r_bus_reset(struct scsi_cmnd *cmd)
1098 {
1099     esas2r_log(ESAS2R_LOG_INFO, "bus_reset (%p)", cmd);
1100 
1101     return esas2r_host_bus_reset(cmd, false);
1102 }
1103 
1104 static int esas2r_dev_targ_reset(struct scsi_cmnd *cmd, bool target_reset)
1105 {
1106     struct esas2r_adapter *a =
1107         (struct esas2r_adapter *)cmd->device->host->hostdata;
1108     struct esas2r_request *rq;
1109     u8 task_management_status = RS_PENDING;
1110     bool completed;
1111 
1112     if (test_bit(AF_DEGRADED_MODE, &a->flags))
1113         return FAILED;
1114 
1115 retry:
1116     rq = esas2r_alloc_request(a);
1117     if (rq == NULL) {
1118         if (target_reset) {
1119             esas2r_log(ESAS2R_LOG_CRIT,
1120                    "unable to allocate a request for a "
1121                    "target reset (%d)!",
1122                    cmd->device->id);
1123         } else {
1124             esas2r_log(ESAS2R_LOG_CRIT,
1125                    "unable to allocate a request for a "
1126                    "device reset (%d:%llu)!",
1127                    cmd->device->id,
1128                    cmd->device->lun);
1129         }
1130 
1131 
1132         return FAILED;
1133     }
1134 
1135     rq->target_id = cmd->device->id;
1136     rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
1137     rq->req_stat = RS_PENDING;
1138 
1139     rq->comp_cb = complete_task_management_request;
1140     rq->task_management_status_ptr = &task_management_status;
1141 
1142     if (target_reset) {
1143         esas2r_debug("issuing target reset (%p) to id %d", rq,
1144                  cmd->device->id);
1145         completed = esas2r_send_task_mgmt(a, rq, 0x20);
1146     } else {
1147         esas2r_debug("issuing device reset (%p) to id %d lun %d", rq,
1148                  cmd->device->id, cmd->device->lun);
1149         completed = esas2r_send_task_mgmt(a, rq, 0x10);
1150     }
1151 
1152     if (completed) {
1153         /* Task management cmd completed right away, need to free it. */
1154 
1155         esas2r_free_request(a, rq);
1156     } else {
1157         /*
1158          * Wait for firmware to complete the request.  Completion
1159          * callback will free it.
1160          */
1161         while (task_management_status == RS_PENDING)
1162             msleep(10);
1163     }
1164 
1165     if (test_bit(AF_DEGRADED_MODE, &a->flags))
1166         return FAILED;
1167 
1168     if (task_management_status == RS_BUSY) {
1169         /*
1170          * Busy, probably because we are flashing.  Wait a bit and
1171          * try again.
1172          */
1173         msleep(100);
1174         goto retry;
1175     }
1176 
1177     return SUCCESS;
1178 }
1179 
1180 int esas2r_device_reset(struct scsi_cmnd *cmd)
1181 {
1182     esas2r_log(ESAS2R_LOG_INFO, "device_reset (%p)", cmd);
1183 
1184     return esas2r_dev_targ_reset(cmd, false);
1185 
1186 }
1187 
1188 int esas2r_target_reset(struct scsi_cmnd *cmd)
1189 {
1190     esas2r_log(ESAS2R_LOG_INFO, "target_reset (%p)", cmd);
1191 
1192     return esas2r_dev_targ_reset(cmd, true);
1193 }
1194 
1195 void esas2r_log_request_failure(struct esas2r_adapter *a,
1196                 struct esas2r_request *rq)
1197 {
1198     u8 reqstatus = rq->req_stat;
1199 
1200     if (reqstatus == RS_SUCCESS)
1201         return;
1202 
1203     if (rq->vrq->scsi.function == VDA_FUNC_SCSI) {
1204         if (reqstatus == RS_SCSI_ERROR) {
1205             if (rq->func_rsp.scsi_rsp.sense_len >= 13) {
1206                 esas2r_log(ESAS2R_LOG_WARN,
1207                        "request failure - SCSI error %x ASC:%x ASCQ:%x CDB:%x",
1208                        rq->sense_buf[2], rq->sense_buf[12],
1209                        rq->sense_buf[13],
1210                        rq->vrq->scsi.cdb[0]);
1211             } else {
1212                 esas2r_log(ESAS2R_LOG_WARN,
1213                        "request failure - SCSI error CDB:%x\n",
1214                        rq->vrq->scsi.cdb[0]);
1215             }
1216         } else if ((rq->vrq->scsi.cdb[0] != INQUIRY
1217                 && rq->vrq->scsi.cdb[0] != REPORT_LUNS)
1218                || (reqstatus != RS_SEL
1219                    && reqstatus != RS_SEL2)) {
1220             if ((reqstatus == RS_UNDERRUN) &&
1221                 (rq->vrq->scsi.cdb[0] == INQUIRY)) {
1222                 /* Don't log inquiry underruns */
1223             } else {
1224                 esas2r_log(ESAS2R_LOG_WARN,
1225                        "request failure - cdb:%x reqstatus:%d target:%d",
1226                        rq->vrq->scsi.cdb[0], reqstatus,
1227                        rq->target_id);
1228             }
1229         }
1230     }
1231 }
1232 
1233 void esas2r_wait_request(struct esas2r_adapter *a, struct esas2r_request *rq)
1234 {
1235     u32 starttime;
1236     u32 timeout;
1237 
1238     starttime = jiffies_to_msecs(jiffies);
1239     timeout = rq->timeout ? rq->timeout : 5000;
1240 
1241     while (true) {
1242         esas2r_polled_interrupt(a);
1243 
1244         if (rq->req_stat != RS_STARTED)
1245             break;
1246 
1247         schedule_timeout_interruptible(msecs_to_jiffies(100));
1248 
1249         if ((jiffies_to_msecs(jiffies) - starttime) > timeout) {
1250             esas2r_hdebug("request TMO");
1251             esas2r_bugon();
1252 
1253             rq->req_stat = RS_TIMEOUT;
1254 
1255             esas2r_local_reset_adapter(a);
1256             return;
1257         }
1258     }
1259 }
1260 
1261 u32 esas2r_map_data_window(struct esas2r_adapter *a, u32 addr_lo)
1262 {
1263     u32 offset = addr_lo & (MW_DATA_WINDOW_SIZE - 1);
1264     u32 base = addr_lo & -(signed int)MW_DATA_WINDOW_SIZE;
1265 
1266     if (a->window_base != base) {
1267         esas2r_write_register_dword(a, MVR_PCI_WIN1_REMAP,
1268                         base | MVRPW1R_ENABLE);
1269         esas2r_flush_register_dword(a, MVR_PCI_WIN1_REMAP);
1270         a->window_base = base;
1271     }
1272 
1273     return offset;
1274 }
1275 
1276 /* Read a block of data from chip memory */
1277 bool esas2r_read_mem_block(struct esas2r_adapter *a,
1278                void *to,
1279                u32 from,
1280                u32 size)
1281 {
1282     u8 *end = (u8 *)to;
1283 
1284     while (size) {
1285         u32 len;
1286         u32 offset;
1287         u32 iatvr;
1288 
1289         iatvr = (from & -(signed int)MW_DATA_WINDOW_SIZE);
1290 
1291         esas2r_map_data_window(a, iatvr);
1292 
1293         offset = from & (MW_DATA_WINDOW_SIZE - 1);
1294         len = size;
1295 
1296         if (len > MW_DATA_WINDOW_SIZE - offset)
1297             len = MW_DATA_WINDOW_SIZE - offset;
1298 
1299         from += len;
1300         size -= len;
1301 
1302         while (len--) {
1303             *end++ = esas2r_read_data_byte(a, offset);
1304             offset++;
1305         }
1306     }
1307 
1308     return true;
1309 }
1310 
1311 void esas2r_nuxi_mgt_data(u8 function, void *data)
1312 {
1313     struct atto_vda_grp_info *g;
1314     struct atto_vda_devinfo *d;
1315     struct atto_vdapart_info *p;
1316     struct atto_vda_dh_info *h;
1317     struct atto_vda_metrics_info *m;
1318     struct atto_vda_schedule_info *s;
1319     struct atto_vda_buzzer_info *b;
1320     u8 i;
1321 
1322     switch (function) {
1323     case VDAMGT_BUZZER_INFO:
1324     case VDAMGT_BUZZER_SET:
1325 
1326         b = (struct atto_vda_buzzer_info *)data;
1327 
1328         b->duration = le32_to_cpu(b->duration);
1329         break;
1330 
1331     case VDAMGT_SCHEDULE_INFO:
1332     case VDAMGT_SCHEDULE_EVENT:
1333 
1334         s = (struct atto_vda_schedule_info *)data;
1335 
1336         s->id = le32_to_cpu(s->id);
1337 
1338         break;
1339 
1340     case VDAMGT_DEV_INFO:
1341     case VDAMGT_DEV_CLEAN:
1342     case VDAMGT_DEV_PT_INFO:
1343     case VDAMGT_DEV_FEATURES:
1344     case VDAMGT_DEV_PT_FEATURES:
1345     case VDAMGT_DEV_OPERATION:
1346 
1347         d = (struct atto_vda_devinfo *)data;
1348 
1349         d->capacity = le64_to_cpu(d->capacity);
1350         d->block_size = le32_to_cpu(d->block_size);
1351         d->ses_dev_index = le16_to_cpu(d->ses_dev_index);
1352         d->target_id = le16_to_cpu(d->target_id);
1353         d->lun = le16_to_cpu(d->lun);
1354         d->features = le16_to_cpu(d->features);
1355         break;
1356 
1357     case VDAMGT_GRP_INFO:
1358     case VDAMGT_GRP_CREATE:
1359     case VDAMGT_GRP_DELETE:
1360     case VDAMGT_ADD_STORAGE:
1361     case VDAMGT_MEMBER_ADD:
1362     case VDAMGT_GRP_COMMIT:
1363     case VDAMGT_GRP_REBUILD:
1364     case VDAMGT_GRP_COMMIT_INIT:
1365     case VDAMGT_QUICK_RAID:
1366     case VDAMGT_GRP_FEATURES:
1367     case VDAMGT_GRP_COMMIT_INIT_AUTOMAP:
1368     case VDAMGT_QUICK_RAID_INIT_AUTOMAP:
1369     case VDAMGT_SPARE_LIST:
1370     case VDAMGT_SPARE_ADD:
1371     case VDAMGT_SPARE_REMOVE:
1372     case VDAMGT_LOCAL_SPARE_ADD:
1373     case VDAMGT_GRP_OPERATION:
1374 
1375         g = (struct atto_vda_grp_info *)data;
1376 
1377         g->capacity = le64_to_cpu(g->capacity);
1378         g->block_size = le32_to_cpu(g->block_size);
1379         g->interleave = le32_to_cpu(g->interleave);
1380         g->features = le16_to_cpu(g->features);
1381 
1382         for (i = 0; i < 32; i++)
1383             g->members[i] = le16_to_cpu(g->members[i]);
1384 
1385         break;
1386 
1387     case VDAMGT_PART_INFO:
1388     case VDAMGT_PART_MAP:
1389     case VDAMGT_PART_UNMAP:
1390     case VDAMGT_PART_AUTOMAP:
1391     case VDAMGT_PART_SPLIT:
1392     case VDAMGT_PART_MERGE:
1393 
1394         p = (struct atto_vdapart_info *)data;
1395 
1396         p->part_size = le64_to_cpu(p->part_size);
1397         p->start_lba = le32_to_cpu(p->start_lba);
1398         p->block_size = le32_to_cpu(p->block_size);
1399         p->target_id = le16_to_cpu(p->target_id);
1400         break;
1401 
1402     case VDAMGT_DEV_HEALTH_REQ:
1403 
1404         h = (struct atto_vda_dh_info *)data;
1405 
1406         h->med_defect_cnt = le32_to_cpu(h->med_defect_cnt);
1407         h->info_exc_cnt = le32_to_cpu(h->info_exc_cnt);
1408         break;
1409 
1410     case VDAMGT_DEV_METRICS:
1411 
1412         m = (struct atto_vda_metrics_info *)data;
1413 
1414         for (i = 0; i < 32; i++)
1415             m->dev_indexes[i] = le16_to_cpu(m->dev_indexes[i]);
1416 
1417         break;
1418 
1419     default:
1420         break;
1421     }
1422 }
1423 
1424 void esas2r_nuxi_cfg_data(u8 function, void *data)
1425 {
1426     struct atto_vda_cfg_init *ci;
1427 
1428     switch (function) {
1429     case VDA_CFG_INIT:
1430     case VDA_CFG_GET_INIT:
1431     case VDA_CFG_GET_INIT2:
1432 
1433         ci = (struct atto_vda_cfg_init *)data;
1434 
1435         ci->date_time.year = le16_to_cpu(ci->date_time.year);
1436         ci->sgl_page_size = le32_to_cpu(ci->sgl_page_size);
1437         ci->vda_version = le32_to_cpu(ci->vda_version);
1438         ci->epoch_time = le32_to_cpu(ci->epoch_time);
1439         ci->ioctl_tunnel = le32_to_cpu(ci->ioctl_tunnel);
1440         ci->num_targets_backend = le32_to_cpu(ci->num_targets_backend);
1441         break;
1442 
1443     default:
1444         break;
1445     }
1446 }
1447 
1448 void esas2r_nuxi_ae_data(union atto_vda_ae *ae)
1449 {
1450     struct atto_vda_ae_raid *r = &ae->raid;
1451     struct atto_vda_ae_lu *l = &ae->lu;
1452 
1453     switch (ae->hdr.bytype) {
1454     case VDAAE_HDR_TYPE_RAID:
1455 
1456         r->dwflags = le32_to_cpu(r->dwflags);
1457         break;
1458 
1459     case VDAAE_HDR_TYPE_LU:
1460 
1461         l->dwevent = le32_to_cpu(l->dwevent);
1462         l->wphys_target_id = le16_to_cpu(l->wphys_target_id);
1463         l->id.tgtlun.wtarget_id = le16_to_cpu(l->id.tgtlun.wtarget_id);
1464 
1465         if (l->hdr.bylength >= offsetof(struct atto_vda_ae_lu, id)
1466             + sizeof(struct atto_vda_ae_lu_tgt_lun_raid)) {
1467             l->id.tgtlun_raid.dwinterleave
1468                 = le32_to_cpu(l->id.tgtlun_raid.dwinterleave);
1469             l->id.tgtlun_raid.dwblock_size
1470                 = le32_to_cpu(l->id.tgtlun_raid.dwblock_size);
1471         }
1472 
1473         break;
1474 
1475     case VDAAE_HDR_TYPE_DISK:
1476     default:
1477         break;
1478     }
1479 }
1480 
1481 void esas2r_free_request(struct esas2r_adapter *a, struct esas2r_request *rq)
1482 {
1483     unsigned long flags;
1484 
1485     esas2r_rq_destroy_request(rq, a);
1486     spin_lock_irqsave(&a->request_lock, flags);
1487     list_add(&rq->comp_list, &a->avail_request);
1488     spin_unlock_irqrestore(&a->request_lock, flags);
1489 }
1490 
1491 struct esas2r_request *esas2r_alloc_request(struct esas2r_adapter *a)
1492 {
1493     struct esas2r_request *rq;
1494     unsigned long flags;
1495 
1496     spin_lock_irqsave(&a->request_lock, flags);
1497 
1498     if (unlikely(list_empty(&a->avail_request))) {
1499         spin_unlock_irqrestore(&a->request_lock, flags);
1500         return NULL;
1501     }
1502 
1503     rq = list_first_entry(&a->avail_request, struct esas2r_request,
1504                   comp_list);
1505     list_del(&rq->comp_list);
1506     spin_unlock_irqrestore(&a->request_lock, flags);
1507     esas2r_rq_init_request(rq, a);
1508 
1509     return rq;
1510 
1511 }
1512 
1513 void esas2r_complete_request_cb(struct esas2r_adapter *a,
1514                 struct esas2r_request *rq)
1515 {
1516     esas2r_debug("completing request %p\n", rq);
1517 
1518     scsi_dma_unmap(rq->cmd);
1519 
1520     if (unlikely(rq->req_stat != RS_SUCCESS)) {
1521         esas2r_debug("[%x STATUS %x:%x (%x)]", rq->target_id,
1522                  rq->req_stat,
1523                  rq->func_rsp.scsi_rsp.scsi_stat,
1524                  rq->cmd);
1525 
1526         rq->cmd->result =
1527             ((esas2r_req_status_to_error(rq->req_stat) << 16)
1528              | rq->func_rsp.scsi_rsp.scsi_stat);
1529 
1530         if (rq->req_stat == RS_UNDERRUN)
1531             scsi_set_resid(rq->cmd,
1532                        le32_to_cpu(rq->func_rsp.scsi_rsp.
1533                            residual_length));
1534         else
1535             scsi_set_resid(rq->cmd, 0);
1536     }
1537 
1538     scsi_done(rq->cmd);
1539 
1540     esas2r_free_request(a, rq);
1541 }
1542 
1543 /* Run tasklet to handle stuff outside of interrupt context. */
1544 void esas2r_adapter_tasklet(unsigned long context)
1545 {
1546     struct esas2r_adapter *a = (struct esas2r_adapter *)context;
1547 
1548     if (unlikely(test_bit(AF2_TIMER_TICK, &a->flags2))) {
1549         clear_bit(AF2_TIMER_TICK, &a->flags2);
1550         esas2r_timer_tick(a);
1551     }
1552 
1553     if (likely(test_bit(AF2_INT_PENDING, &a->flags2))) {
1554         clear_bit(AF2_INT_PENDING, &a->flags2);
1555         esas2r_adapter_interrupt(a);
1556     }
1557 
1558     if (esas2r_is_tasklet_pending(a))
1559         esas2r_do_tasklet_tasks(a);
1560 
1561     if (esas2r_is_tasklet_pending(a)
1562         || (test_bit(AF2_INT_PENDING, &a->flags2))
1563         || (test_bit(AF2_TIMER_TICK, &a->flags2))) {
1564         clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1565         esas2r_schedule_tasklet(a);
1566     } else {
1567         clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1568     }
1569 }
1570 
1571 static void esas2r_timer_callback(struct timer_list *t);
1572 
1573 void esas2r_kickoff_timer(struct esas2r_adapter *a)
1574 {
1575     timer_setup(&a->timer, esas2r_timer_callback, 0);
1576 
1577     a->timer.expires = jiffies +
1578                msecs_to_jiffies(100);
1579 
1580     add_timer(&a->timer);
1581 }
1582 
1583 static void esas2r_timer_callback(struct timer_list *t)
1584 {
1585     struct esas2r_adapter *a = from_timer(a, t, timer);
1586 
1587     set_bit(AF2_TIMER_TICK, &a->flags2);
1588 
1589     esas2r_schedule_tasklet(a);
1590 
1591     esas2r_kickoff_timer(a);
1592 }
1593 
1594 /*
1595  * Firmware events need to be handled outside of interrupt context
1596  * so we schedule a delayed_work to handle them.
1597  */
1598 
1599 static void
1600 esas2r_free_fw_event(struct esas2r_fw_event_work *fw_event)
1601 {
1602     unsigned long flags;
1603     struct esas2r_adapter *a = fw_event->a;
1604 
1605     spin_lock_irqsave(&a->fw_event_lock, flags);
1606     list_del(&fw_event->list);
1607     kfree(fw_event);
1608     spin_unlock_irqrestore(&a->fw_event_lock, flags);
1609 }
1610 
1611 void
1612 esas2r_fw_event_off(struct esas2r_adapter *a)
1613 {
1614     unsigned long flags;
1615 
1616     spin_lock_irqsave(&a->fw_event_lock, flags);
1617     a->fw_events_off = 1;
1618     spin_unlock_irqrestore(&a->fw_event_lock, flags);
1619 }
1620 
1621 void
1622 esas2r_fw_event_on(struct esas2r_adapter *a)
1623 {
1624     unsigned long flags;
1625 
1626     spin_lock_irqsave(&a->fw_event_lock, flags);
1627     a->fw_events_off = 0;
1628     spin_unlock_irqrestore(&a->fw_event_lock, flags);
1629 }
1630 
1631 static void esas2r_add_device(struct esas2r_adapter *a, u16 target_id)
1632 {
1633     int ret;
1634     struct scsi_device *scsi_dev;
1635 
1636     scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
1637 
1638     if (scsi_dev) {
1639         esas2r_log_dev(
1640             ESAS2R_LOG_WARN,
1641             &(scsi_dev->
1642               sdev_gendev),
1643             "scsi device already exists at id %d", target_id);
1644 
1645         scsi_device_put(scsi_dev);
1646     } else {
1647         esas2r_log_dev(
1648             ESAS2R_LOG_INFO,
1649             &(a->host->
1650               shost_gendev),
1651             "scsi_add_device() called for 0:%d:0",
1652             target_id);
1653 
1654         ret = scsi_add_device(a->host, 0, target_id, 0);
1655         if (ret) {
1656             esas2r_log_dev(
1657                 ESAS2R_LOG_CRIT,
1658                 &(a->host->
1659                   shost_gendev),
1660                 "scsi_add_device failed with %d for id %d",
1661                 ret, target_id);
1662         }
1663     }
1664 }
1665 
1666 static void esas2r_remove_device(struct esas2r_adapter *a, u16 target_id)
1667 {
1668     struct scsi_device *scsi_dev;
1669 
1670     scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
1671 
1672     if (scsi_dev) {
1673         scsi_device_set_state(scsi_dev, SDEV_OFFLINE);
1674 
1675         esas2r_log_dev(
1676             ESAS2R_LOG_INFO,
1677             &(scsi_dev->
1678               sdev_gendev),
1679             "scsi_remove_device() called for 0:%d:0",
1680             target_id);
1681 
1682         scsi_remove_device(scsi_dev);
1683 
1684         esas2r_log_dev(
1685             ESAS2R_LOG_INFO,
1686             &(scsi_dev->
1687               sdev_gendev),
1688             "scsi_device_put() called");
1689 
1690         scsi_device_put(scsi_dev);
1691     } else {
1692         esas2r_log_dev(
1693             ESAS2R_LOG_WARN,
1694             &(a->host->shost_gendev),
1695             "no target found at id %d",
1696             target_id);
1697     }
1698 }
1699 
1700 /*
1701  * Sends a firmware asynchronous event to anyone who happens to be
1702  * listening on the defined ATTO VDA event ports.
1703  */
1704 static void esas2r_send_ae_event(struct esas2r_fw_event_work *fw_event)
1705 {
1706     struct esas2r_vda_ae *ae = (struct esas2r_vda_ae *)fw_event->data;
1707     char *type;
1708 
1709     switch (ae->vda_ae.hdr.bytype) {
1710     case VDAAE_HDR_TYPE_RAID:
1711         type = "RAID group state change";
1712         break;
1713 
1714     case VDAAE_HDR_TYPE_LU:
1715         type = "Mapped destination LU change";
1716         break;
1717 
1718     case VDAAE_HDR_TYPE_DISK:
1719         type = "Physical disk inventory change";
1720         break;
1721 
1722     case VDAAE_HDR_TYPE_RESET:
1723         type = "Firmware reset";
1724         break;
1725 
1726     case VDAAE_HDR_TYPE_LOG_INFO:
1727         type = "Event Log message (INFO level)";
1728         break;
1729 
1730     case VDAAE_HDR_TYPE_LOG_WARN:
1731         type = "Event Log message (WARN level)";
1732         break;
1733 
1734     case VDAAE_HDR_TYPE_LOG_CRIT:
1735         type = "Event Log message (CRIT level)";
1736         break;
1737 
1738     case VDAAE_HDR_TYPE_LOG_FAIL:
1739         type = "Event Log message (FAIL level)";
1740         break;
1741 
1742     case VDAAE_HDR_TYPE_NVC:
1743         type = "NVCache change";
1744         break;
1745 
1746     case VDAAE_HDR_TYPE_TLG_INFO:
1747         type = "Time stamped log message (INFO level)";
1748         break;
1749 
1750     case VDAAE_HDR_TYPE_TLG_WARN:
1751         type = "Time stamped log message (WARN level)";
1752         break;
1753 
1754     case VDAAE_HDR_TYPE_TLG_CRIT:
1755         type = "Time stamped log message (CRIT level)";
1756         break;
1757 
1758     case VDAAE_HDR_TYPE_PWRMGT:
1759         type = "Power management";
1760         break;
1761 
1762     case VDAAE_HDR_TYPE_MUTE:
1763         type = "Mute button pressed";
1764         break;
1765 
1766     case VDAAE_HDR_TYPE_DEV:
1767         type = "Device attribute change";
1768         break;
1769 
1770     default:
1771         type = "Unknown";
1772         break;
1773     }
1774 
1775     esas2r_log(ESAS2R_LOG_WARN,
1776            "An async event of type \"%s\" was received from the firmware.  The event contents are:",
1777            type);
1778     esas2r_log_hexdump(ESAS2R_LOG_WARN, &ae->vda_ae,
1779                ae->vda_ae.hdr.bylength);
1780 
1781 }
1782 
1783 static void
1784 esas2r_firmware_event_work(struct work_struct *work)
1785 {
1786     struct esas2r_fw_event_work *fw_event =
1787         container_of(work, struct esas2r_fw_event_work, work.work);
1788 
1789     struct esas2r_adapter *a = fw_event->a;
1790 
1791     u16 target_id = *(u16 *)&fw_event->data[0];
1792 
1793     if (a->fw_events_off)
1794         goto done;
1795 
1796     switch (fw_event->type) {
1797     case fw_event_null:
1798         break; /* do nothing */
1799 
1800     case fw_event_lun_change:
1801         esas2r_remove_device(a, target_id);
1802         esas2r_add_device(a, target_id);
1803         break;
1804 
1805     case fw_event_present:
1806         esas2r_add_device(a, target_id);
1807         break;
1808 
1809     case fw_event_not_present:
1810         esas2r_remove_device(a, target_id);
1811         break;
1812 
1813     case fw_event_vda_ae:
1814         esas2r_send_ae_event(fw_event);
1815         break;
1816     }
1817 
1818 done:
1819     esas2r_free_fw_event(fw_event);
1820 }
1821 
1822 void esas2r_queue_fw_event(struct esas2r_adapter *a,
1823                enum fw_event_type type,
1824                void *data,
1825                int data_sz)
1826 {
1827     struct esas2r_fw_event_work *fw_event;
1828     unsigned long flags;
1829 
1830     fw_event = kzalloc(sizeof(struct esas2r_fw_event_work), GFP_ATOMIC);
1831     if (!fw_event) {
1832         esas2r_log(ESAS2R_LOG_WARN,
1833                "esas2r_queue_fw_event failed to alloc");
1834         return;
1835     }
1836 
1837     if (type == fw_event_vda_ae) {
1838         struct esas2r_vda_ae *ae =
1839             (struct esas2r_vda_ae *)fw_event->data;
1840 
1841         ae->signature = ESAS2R_VDA_EVENT_SIG;
1842         ae->bus_number = a->pcid->bus->number;
1843         ae->devfn = a->pcid->devfn;
1844         memcpy(&ae->vda_ae, data, sizeof(ae->vda_ae));
1845     } else {
1846         memcpy(fw_event->data, data, data_sz);
1847     }
1848 
1849     fw_event->type = type;
1850     fw_event->a = a;
1851 
1852     spin_lock_irqsave(&a->fw_event_lock, flags);
1853     list_add_tail(&fw_event->list, &a->fw_event_list);
1854     INIT_DELAYED_WORK(&fw_event->work, esas2r_firmware_event_work);
1855     queue_delayed_work_on(
1856         smp_processor_id(), a->fw_event_q, &fw_event->work,
1857         msecs_to_jiffies(1));
1858     spin_unlock_irqrestore(&a->fw_event_lock, flags);
1859 }
1860 
1861 void esas2r_target_state_changed(struct esas2r_adapter *a, u16 targ_id,
1862                  u8 state)
1863 {
1864     if (state == TS_LUN_CHANGE)
1865         esas2r_queue_fw_event(a, fw_event_lun_change, &targ_id,
1866                       sizeof(targ_id));
1867     else if (state == TS_PRESENT)
1868         esas2r_queue_fw_event(a, fw_event_present, &targ_id,
1869                       sizeof(targ_id));
1870     else if (state == TS_NOT_PRESENT)
1871         esas2r_queue_fw_event(a, fw_event_not_present, &targ_id,
1872                       sizeof(targ_id));
1873 }
1874 
1875 /* Translate status to a Linux SCSI mid-layer error code */
1876 int esas2r_req_status_to_error(u8 req_stat)
1877 {
1878     switch (req_stat) {
1879     case RS_OVERRUN:
1880     case RS_UNDERRUN:
1881     case RS_SUCCESS:
1882     /*
1883      * NOTE: SCSI mid-layer wants a good status for a SCSI error, because
1884      *       it will check the scsi_stat value in the completion anyway.
1885      */
1886     case RS_SCSI_ERROR:
1887         return DID_OK;
1888 
1889     case RS_SEL:
1890     case RS_SEL2:
1891         return DID_NO_CONNECT;
1892 
1893     case RS_RESET:
1894         return DID_RESET;
1895 
1896     case RS_ABORTED:
1897         return DID_ABORT;
1898 
1899     case RS_BUSY:
1900         return DID_BUS_BUSY;
1901     }
1902 
1903     /* everything else is just an error. */
1904 
1905     return DID_ERROR;
1906 }
1907 
1908 module_init(esas2r_init);
1909 module_exit(esas2r_exit);