linux/drivers/scsi/esas2r/esas2r_main.c
<<
>>
Prefs
   1/*
   2 *  linux/drivers/scsi/esas2r/esas2r_main.c
   3 *      For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
   4 *
   5 *  Copyright (c) 2001-2013 ATTO Technology, Inc.
   6 *  (mailto:linuxdrivers@attotech.com)
   7 *
   8 * This program is free software; you can redistribute it and/or
   9 * modify it under the terms of the GNU General Public License
  10 * as published by the Free Software Foundation; either version 2
  11 * of the License, or (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 * NO WARRANTY
  19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23 * solely responsible for determining the appropriateness of using and
  24 * distributing the Program and assumes all risks associated with its
  25 * exercise of rights under this Agreement, including but not limited to
  26 * the risks and costs of program errors, damage to or loss of data,
  27 * programs or equipment, and unavailability or interruption of operations.
  28 *
  29 * DISCLAIMER OF LIABILITY
  30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37 *
  38 * You should have received a copy of the GNU General Public License
  39 * along with this program; if not, write to the Free Software
  40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
  41 * USA.
  42 */
  43
  44#include "esas2r.h"
  45
  46MODULE_DESCRIPTION(ESAS2R_DRVR_NAME ": " ESAS2R_LONGNAME " driver");
  47MODULE_AUTHOR("ATTO Technology, Inc.");
  48MODULE_LICENSE("GPL");
  49MODULE_VERSION(ESAS2R_VERSION_STR);
  50
  51/* global definitions */
  52
  53static int found_adapters;
  54struct esas2r_adapter *esas2r_adapters[MAX_ADAPTERS];
  55
  56#define ESAS2R_VDA_EVENT_PORT1       54414
  57#define ESAS2R_VDA_EVENT_PORT2       54415
  58#define ESAS2R_VDA_EVENT_SOCK_COUNT  2
  59
  60static struct esas2r_adapter *esas2r_adapter_from_kobj(struct kobject *kobj)
  61{
  62        struct device *dev = container_of(kobj, struct device, kobj);
  63        struct Scsi_Host *host = class_to_shost(dev);
  64
  65        return (struct esas2r_adapter *)host->hostdata;
  66}
  67
  68static ssize_t read_fw(struct file *file, struct kobject *kobj,
  69                       struct bin_attribute *attr,
  70                       char *buf, loff_t off, size_t count)
  71{
  72        struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  73
  74        return esas2r_read_fw(a, buf, off, count);
  75}
  76
  77static ssize_t write_fw(struct file *file, struct kobject *kobj,
  78                        struct bin_attribute *attr,
  79                        char *buf, loff_t off, size_t count)
  80{
  81        struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  82
  83        return esas2r_write_fw(a, buf, off, count);
  84}
  85
  86static ssize_t read_fs(struct file *file, struct kobject *kobj,
  87                       struct bin_attribute *attr,
  88                       char *buf, loff_t off, size_t count)
  89{
  90        struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
  91
  92        return esas2r_read_fs(a, buf, off, count);
  93}
  94
  95static ssize_t write_fs(struct file *file, struct kobject *kobj,
  96                        struct bin_attribute *attr,
  97                        char *buf, loff_t off, size_t count)
  98{
  99        struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
 100        int length = min(sizeof(struct esas2r_ioctl_fs), count);
 101        int result = 0;
 102
 103        result = esas2r_write_fs(a, buf, off, count);
 104
 105        if (result < 0)
 106                result = 0;
 107
 108        return length;
 109}
 110
 111static ssize_t read_vda(struct file *file, struct kobject *kobj,
 112                        struct bin_attribute *attr,
 113                        char *buf, loff_t off, size_t count)
 114{
 115        struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
 116
 117        return esas2r_read_vda(a, buf, off, count);
 118}
 119
 120static ssize_t write_vda(struct file *file, struct kobject *kobj,
 121                         struct bin_attribute *attr,
 122                         char *buf, loff_t off, size_t count)
 123{
 124        struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
 125
 126        return esas2r_write_vda(a, buf, off, count);
 127}
 128
 129static ssize_t read_live_nvram(struct file *file, struct kobject *kobj,
 130                               struct bin_attribute *attr,
 131                               char *buf, loff_t off, size_t count)
 132{
 133        struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
 134        int length = min_t(size_t, sizeof(struct esas2r_sas_nvram), PAGE_SIZE);
 135
 136        memcpy(buf, a->nvram, length);
 137        return length;
 138}
 139
 140static ssize_t write_live_nvram(struct file *file, struct kobject *kobj,
 141                                struct bin_attribute *attr,
 142                                char *buf, loff_t off, size_t count)
 143{
 144        struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
 145        struct esas2r_request *rq;
 146        int result = -EFAULT;
 147
 148        rq = esas2r_alloc_request(a);
 149        if (rq == NULL)
 150                return -ENOMEM;
 151
 152        if (esas2r_write_params(a, rq, (struct esas2r_sas_nvram *)buf))
 153                result = count;
 154
 155        esas2r_free_request(a, rq);
 156
 157        return result;
 158}
 159
 160static ssize_t read_default_nvram(struct file *file, struct kobject *kobj,
 161                                  struct bin_attribute *attr,
 162                                  char *buf, loff_t off, size_t count)
 163{
 164        struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
 165
 166        esas2r_nvram_get_defaults(a, (struct esas2r_sas_nvram *)buf);
 167
 168        return sizeof(struct esas2r_sas_nvram);
 169}
 170
 171static ssize_t read_hw(struct file *file, struct kobject *kobj,
 172                       struct bin_attribute *attr,
 173                       char *buf, loff_t off, size_t count)
 174{
 175        struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
 176        int length = min_t(size_t, sizeof(struct atto_ioctl), PAGE_SIZE);
 177
 178        if (!a->local_atto_ioctl)
 179                return -ENOMEM;
 180
 181        if (handle_hba_ioctl(a, a->local_atto_ioctl) != IOCTL_SUCCESS)
 182                return -ENOMEM;
 183
 184        memcpy(buf, a->local_atto_ioctl, length);
 185
 186        return length;
 187}
 188
 189static ssize_t write_hw(struct file *file, struct kobject *kobj,
 190                        struct bin_attribute *attr,
 191                        char *buf, loff_t off, size_t count)
 192{
 193        struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
 194        int length = min(sizeof(struct atto_ioctl), count);
 195
 196        if (!a->local_atto_ioctl) {
 197                a->local_atto_ioctl = kmalloc(sizeof(struct atto_ioctl),
 198                                              GFP_KERNEL);
 199                if (a->local_atto_ioctl == NULL) {
 200                        esas2r_log(ESAS2R_LOG_WARN,
 201                                   "write_hw kzalloc failed for %zu bytes",
 202                                   sizeof(struct atto_ioctl));
 203                        return -ENOMEM;
 204                }
 205        }
 206
 207        memset(a->local_atto_ioctl, 0, sizeof(struct atto_ioctl));
 208        memcpy(a->local_atto_ioctl, buf, length);
 209
 210        return length;
 211}
 212
 213#define ESAS2R_RW_BIN_ATTR(_name) \
 214        struct bin_attribute bin_attr_ ## _name = { \
 215                .attr   = \
 216                { .name = __stringify(_name), .mode  = S_IRUSR | S_IWUSR }, \
 217                .size   = 0, \
 218                .read   = read_ ## _name, \
 219                .write  = write_ ## _name }
 220
 221ESAS2R_RW_BIN_ATTR(fw);
 222ESAS2R_RW_BIN_ATTR(fs);
 223ESAS2R_RW_BIN_ATTR(vda);
 224ESAS2R_RW_BIN_ATTR(hw);
 225ESAS2R_RW_BIN_ATTR(live_nvram);
 226
 227struct bin_attribute bin_attr_default_nvram = {
 228        .attr   = { .name = "default_nvram", .mode = S_IRUGO },
 229        .size   = 0,
 230        .read   = read_default_nvram,
 231        .write  = NULL
 232};
 233
 234static struct scsi_host_template driver_template = {
 235        .module                         = THIS_MODULE,
 236        .show_info                      = esas2r_show_info,
 237        .name                           = ESAS2R_LONGNAME,
 238        .info                           = esas2r_info,
 239        .ioctl                          = esas2r_ioctl,
 240        .queuecommand                   = esas2r_queuecommand,
 241        .eh_abort_handler               = esas2r_eh_abort,
 242        .eh_device_reset_handler        = esas2r_device_reset,
 243        .eh_bus_reset_handler           = esas2r_bus_reset,
 244        .eh_host_reset_handler          = esas2r_host_reset,
 245        .eh_target_reset_handler        = esas2r_target_reset,
 246        .can_queue                      = 128,
 247        .this_id                        = -1,
 248        .sg_tablesize                   = SG_CHUNK_SIZE,
 249        .cmd_per_lun                    =
 250                ESAS2R_DEFAULT_CMD_PER_LUN,
 251        .present                        = 0,
 252        .unchecked_isa_dma              = 0,
 253        .use_clustering                 = ENABLE_CLUSTERING,
 254        .emulated                       = 0,
 255        .proc_name                      = ESAS2R_DRVR_NAME,
 256        .change_queue_depth             = scsi_change_queue_depth,
 257        .max_sectors                    = 0xFFFF,
 258};
 259
 260int sgl_page_size = 512;
 261module_param(sgl_page_size, int, 0);
 262MODULE_PARM_DESC(sgl_page_size,
 263                 "Scatter/gather list (SGL) page size in number of S/G "
 264                 "entries.  If your application is doing a lot of very large "
 265                 "transfers, you may want to increase the SGL page size.  "
 266                 "Default 512.");
 267
 268int num_sg_lists = 1024;
 269module_param(num_sg_lists, int, 0);
 270MODULE_PARM_DESC(num_sg_lists,
 271                 "Number of scatter/gather lists.  Default 1024.");
 272
 273int sg_tablesize = SG_CHUNK_SIZE;
 274module_param(sg_tablesize, int, 0);
 275MODULE_PARM_DESC(sg_tablesize,
 276                 "Maximum number of entries in a scatter/gather table.");
 277
 278int num_requests = 256;
 279module_param(num_requests, int, 0);
 280MODULE_PARM_DESC(num_requests,
 281                 "Number of requests.  Default 256.");
 282
 283int num_ae_requests = 4;
 284module_param(num_ae_requests, int, 0);
 285MODULE_PARM_DESC(num_ae_requests,
 286                 "Number of VDA asynchronous event requests.  Default 4.");
 287
 288int cmd_per_lun = ESAS2R_DEFAULT_CMD_PER_LUN;
 289module_param(cmd_per_lun, int, 0);
 290MODULE_PARM_DESC(cmd_per_lun,
 291                 "Maximum number of commands per LUN.  Default "
 292                 DEFINED_NUM_TO_STR(ESAS2R_DEFAULT_CMD_PER_LUN) ".");
 293
 294int can_queue = 128;
 295module_param(can_queue, int, 0);
 296MODULE_PARM_DESC(can_queue,
 297                 "Maximum number of commands per adapter.  Default 128.");
 298
 299int esas2r_max_sectors = 0xFFFF;
 300module_param(esas2r_max_sectors, int, 0);
 301MODULE_PARM_DESC(esas2r_max_sectors,
 302                 "Maximum number of disk sectors in a single data transfer.  "
 303                 "Default 65535 (largest possible setting).");
 304
 305int interrupt_mode = 1;
 306module_param(interrupt_mode, int, 0);
 307MODULE_PARM_DESC(interrupt_mode,
 308                 "Defines the interrupt mode to use.  0 for legacy"
 309                 ", 1 for MSI.  Default is MSI (1).");
 310
 311static const struct pci_device_id
 312        esas2r_pci_table[] = {
 313        { ATTO_VENDOR_ID, 0x0049,         ATTO_VENDOR_ID, 0x0049,
 314          0,
 315          0, 0 },
 316        { ATTO_VENDOR_ID, 0x0049,         ATTO_VENDOR_ID, 0x004A,
 317          0,
 318          0, 0 },
 319        { ATTO_VENDOR_ID, 0x0049,         ATTO_VENDOR_ID, 0x004B,
 320          0,
 321          0, 0 },
 322        { ATTO_VENDOR_ID, 0x0049,         ATTO_VENDOR_ID, 0x004C,
 323          0,
 324          0, 0 },
 325        { ATTO_VENDOR_ID, 0x0049,         ATTO_VENDOR_ID, 0x004D,
 326          0,
 327          0, 0 },
 328        { ATTO_VENDOR_ID, 0x0049,         ATTO_VENDOR_ID, 0x004E,
 329          0,
 330          0, 0 },
 331        { 0,              0,              0,              0,
 332          0,
 333          0, 0 }
 334};
 335
 336MODULE_DEVICE_TABLE(pci, esas2r_pci_table);
 337
 338static int
 339esas2r_probe(struct pci_dev *pcid, const struct pci_device_id *id);
 340
 341static void
 342esas2r_remove(struct pci_dev *pcid);
 343
 344static struct pci_driver
 345        esas2r_pci_driver = {
 346        .name           = ESAS2R_DRVR_NAME,
 347        .id_table       = esas2r_pci_table,
 348        .probe          = esas2r_probe,
 349        .remove         = esas2r_remove,
 350        .suspend        = esas2r_suspend,
 351        .resume         = esas2r_resume,
 352};
 353
 354static int esas2r_probe(struct pci_dev *pcid,
 355                        const struct pci_device_id *id)
 356{
 357        struct Scsi_Host *host = NULL;
 358        struct esas2r_adapter *a;
 359        int err;
 360
 361        size_t host_alloc_size = sizeof(struct esas2r_adapter)
 362                                 + ((num_requests) +
 363                                    1) * sizeof(struct esas2r_request);
 364
 365        esas2r_log_dev(ESAS2R_LOG_DEBG, &(pcid->dev),
 366                       "esas2r_probe() 0x%02x 0x%02x 0x%02x 0x%02x",
 367                       pcid->vendor,
 368                       pcid->device,
 369                       pcid->subsystem_vendor,
 370                       pcid->subsystem_device);
 371
 372        esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
 373                       "before pci_enable_device() "
 374                       "enable_cnt: %d",
 375                       pcid->enable_cnt.counter);
 376
 377        err = pci_enable_device(pcid);
 378        if (err != 0) {
 379                esas2r_log_dev(ESAS2R_LOG_CRIT, &(pcid->dev),
 380                               "pci_enable_device() FAIL (%d)",
 381                               err);
 382                return -ENODEV;
 383        }
 384
 385        esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
 386                       "pci_enable_device() OK");
 387        esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
 388                       "after pci_enable_device() enable_cnt: %d",
 389                       pcid->enable_cnt.counter);
 390
 391        host = scsi_host_alloc(&driver_template, host_alloc_size);
 392        if (host == NULL) {
 393                esas2r_log(ESAS2R_LOG_CRIT, "scsi_host_alloc() FAIL");
 394                return -ENODEV;
 395        }
 396
 397        memset(host->hostdata, 0, host_alloc_size);
 398
 399        a = (struct esas2r_adapter *)host->hostdata;
 400
 401        esas2r_log(ESAS2R_LOG_INFO, "scsi_host_alloc() OK host: %p", host);
 402
 403        /* override max LUN and max target id */
 404
 405        host->max_id = ESAS2R_MAX_ID + 1;
 406        host->max_lun = 255;
 407
 408        /* we can handle 16-byte CDbs */
 409
 410        host->max_cmd_len = 16;
 411
 412        host->can_queue = can_queue;
 413        host->cmd_per_lun = cmd_per_lun;
 414        host->this_id = host->max_id + 1;
 415        host->max_channel = 0;
 416        host->unique_id = found_adapters;
 417        host->sg_tablesize = sg_tablesize;
 418        host->max_sectors = esas2r_max_sectors;
 419
 420        /* set to bus master for BIOses that don't do it for us */
 421
 422        esas2r_log(ESAS2R_LOG_INFO, "pci_set_master() called");
 423
 424        pci_set_master(pcid);
 425
 426        if (!esas2r_init_adapter(host, pcid, found_adapters)) {
 427                esas2r_log(ESAS2R_LOG_CRIT,
 428                           "unable to initialize device at PCI bus %x:%x",
 429                           pcid->bus->number,
 430                           pcid->devfn);
 431
 432                esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
 433                               "scsi_host_put() called");
 434
 435                scsi_host_put(host);
 436
 437                return 0;
 438
 439        }
 440
 441        esas2r_log(ESAS2R_LOG_INFO, "pci_set_drvdata(%p, %p) called", pcid,
 442                   host->hostdata);
 443
 444        pci_set_drvdata(pcid, host);
 445
 446        esas2r_log(ESAS2R_LOG_INFO, "scsi_add_host() called");
 447
 448        err = scsi_add_host(host, &pcid->dev);
 449
 450        if (err) {
 451                esas2r_log(ESAS2R_LOG_CRIT, "scsi_add_host returned %d", err);
 452                esas2r_log_dev(ESAS2R_LOG_CRIT, &(host->shost_gendev),
 453                               "scsi_add_host() FAIL");
 454
 455                esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
 456                               "scsi_host_put() called");
 457
 458                scsi_host_put(host);
 459
 460                esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
 461                               "pci_set_drvdata(%p, NULL) called",
 462                               pcid);
 463
 464                pci_set_drvdata(pcid, NULL);
 465
 466                return -ENODEV;
 467        }
 468
 469
 470        esas2r_fw_event_on(a);
 471
 472        esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
 473                       "scsi_scan_host() called");
 474
 475        scsi_scan_host(host);
 476
 477        /* Add sysfs binary files */
 478        if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fw))
 479                esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
 480                               "Failed to create sysfs binary file: fw");
 481        else
 482                a->sysfs_fw_created = 1;
 483
 484        if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fs))
 485                esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
 486                               "Failed to create sysfs binary file: fs");
 487        else
 488                a->sysfs_fs_created = 1;
 489
 490        if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_vda))
 491                esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
 492                               "Failed to create sysfs binary file: vda");
 493        else
 494                a->sysfs_vda_created = 1;
 495
 496        if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_hw))
 497                esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
 498                               "Failed to create sysfs binary file: hw");
 499        else
 500                a->sysfs_hw_created = 1;
 501
 502        if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_live_nvram))
 503                esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
 504                               "Failed to create sysfs binary file: live_nvram");
 505        else
 506                a->sysfs_live_nvram_created = 1;
 507
 508        if (sysfs_create_bin_file(&host->shost_dev.kobj,
 509                                  &bin_attr_default_nvram))
 510                esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
 511                               "Failed to create sysfs binary file: default_nvram");
 512        else
 513                a->sysfs_default_nvram_created = 1;
 514
 515        found_adapters++;
 516
 517        return 0;
 518}
 519
 520static void esas2r_remove(struct pci_dev *pdev)
 521{
 522        struct Scsi_Host *host = pci_get_drvdata(pdev);
 523        struct esas2r_adapter *a = (struct esas2r_adapter *)host->hostdata;
 524
 525        esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
 526                       "esas2r_remove(%p) called; "
 527                       "host:%p", pdev,
 528                       host);
 529
 530        esas2r_kill_adapter(a->index);
 531        found_adapters--;
 532}
 533
 534static int __init esas2r_init(void)
 535{
 536        int i;
 537
 538        esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
 539
 540        /* verify valid parameters */
 541
 542        if (can_queue < 1) {
 543                esas2r_log(ESAS2R_LOG_WARN,
 544                           "warning: can_queue must be at least 1, value "
 545                           "forced.");
 546                can_queue = 1;
 547        } else if (can_queue > 2048) {
 548                esas2r_log(ESAS2R_LOG_WARN,
 549                           "warning: can_queue must be no larger than 2048, "
 550                           "value forced.");
 551                can_queue = 2048;
 552        }
 553
 554        if (cmd_per_lun < 1) {
 555                esas2r_log(ESAS2R_LOG_WARN,
 556                           "warning: cmd_per_lun must be at least 1, value "
 557                           "forced.");
 558                cmd_per_lun = 1;
 559        } else if (cmd_per_lun > 2048) {
 560                esas2r_log(ESAS2R_LOG_WARN,
 561                           "warning: cmd_per_lun must be no larger than "
 562                           "2048, value forced.");
 563                cmd_per_lun = 2048;
 564        }
 565
 566        if (sg_tablesize < 32) {
 567                esas2r_log(ESAS2R_LOG_WARN,
 568                           "warning: sg_tablesize must be at least 32, "
 569                           "value forced.");
 570                sg_tablesize = 32;
 571        }
 572
 573        if (esas2r_max_sectors < 1) {
 574                esas2r_log(ESAS2R_LOG_WARN,
 575                           "warning: esas2r_max_sectors must be at least "
 576                           "1, value forced.");
 577                esas2r_max_sectors = 1;
 578        } else if (esas2r_max_sectors > 0xffff) {
 579                esas2r_log(ESAS2R_LOG_WARN,
 580                           "warning: esas2r_max_sectors must be no larger "
 581                           "than 0xffff, value forced.");
 582                esas2r_max_sectors = 0xffff;
 583        }
 584
 585        sgl_page_size &= ~(ESAS2R_SGL_ALIGN - 1);
 586
 587        if (sgl_page_size < SGL_PG_SZ_MIN)
 588                sgl_page_size = SGL_PG_SZ_MIN;
 589        else if (sgl_page_size > SGL_PG_SZ_MAX)
 590                sgl_page_size = SGL_PG_SZ_MAX;
 591
 592        if (num_sg_lists < NUM_SGL_MIN)
 593                num_sg_lists = NUM_SGL_MIN;
 594        else if (num_sg_lists > NUM_SGL_MAX)
 595                num_sg_lists = NUM_SGL_MAX;
 596
 597        if (num_requests < NUM_REQ_MIN)
 598                num_requests = NUM_REQ_MIN;
 599        else if (num_requests > NUM_REQ_MAX)
 600                num_requests = NUM_REQ_MAX;
 601
 602        if (num_ae_requests < NUM_AE_MIN)
 603                num_ae_requests = NUM_AE_MIN;
 604        else if (num_ae_requests > NUM_AE_MAX)
 605                num_ae_requests = NUM_AE_MAX;
 606
 607        /* set up other globals */
 608
 609        for (i = 0; i < MAX_ADAPTERS; i++)
 610                esas2r_adapters[i] = NULL;
 611
 612        return pci_register_driver(&esas2r_pci_driver);
 613}
 614
 615/* Handle ioctl calls to "/proc/scsi/esas2r/ATTOnode" */
 616static const struct file_operations esas2r_proc_fops = {
 617        .compat_ioctl   = esas2r_proc_ioctl,
 618        .unlocked_ioctl = esas2r_proc_ioctl,
 619};
 620
 621static struct Scsi_Host *esas2r_proc_host;
 622static int esas2r_proc_major;
 623
 624long esas2r_proc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
 625{
 626        return esas2r_ioctl_handler(esas2r_proc_host->hostdata,
 627                                    (int)cmd, (void __user *)arg);
 628}
 629
 630static void __exit esas2r_exit(void)
 631{
 632        esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
 633
 634        if (esas2r_proc_major > 0) {
 635                esas2r_log(ESAS2R_LOG_INFO, "unregister proc");
 636
 637                remove_proc_entry(ATTONODE_NAME,
 638                                  esas2r_proc_host->hostt->proc_dir);
 639                unregister_chrdev(esas2r_proc_major, ESAS2R_DRVR_NAME);
 640
 641                esas2r_proc_major = 0;
 642        }
 643
 644        esas2r_log(ESAS2R_LOG_INFO, "pci_unregister_driver() called");
 645
 646        pci_unregister_driver(&esas2r_pci_driver);
 647}
 648
 649int esas2r_show_info(struct seq_file *m, struct Scsi_Host *sh)
 650{
 651        struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
 652
 653        struct esas2r_target *t;
 654        int dev_count = 0;
 655
 656        esas2r_log(ESAS2R_LOG_DEBG, "esas2r_show_info (%p,%d)", m, sh->host_no);
 657
 658        seq_printf(m, ESAS2R_LONGNAME "\n"
 659                   "Driver version: "ESAS2R_VERSION_STR "\n"
 660                   "Flash version: %s\n"
 661                   "Firmware version: %s\n"
 662                   "Copyright "ESAS2R_COPYRIGHT_YEARS "\n"
 663                   "http://www.attotech.com\n"
 664                   "\n",
 665                   a->flash_rev,
 666                   a->fw_rev[0] ? a->fw_rev : "(none)");
 667
 668
 669        seq_printf(m, "Adapter information:\n"
 670                   "--------------------\n"
 671                   "Model: %s\n"
 672                   "SAS address: %02X%02X%02X%02X:%02X%02X%02X%02X\n",
 673                   esas2r_get_model_name(a),
 674                   a->nvram->sas_addr[0],
 675                   a->nvram->sas_addr[1],
 676                   a->nvram->sas_addr[2],
 677                   a->nvram->sas_addr[3],
 678                   a->nvram->sas_addr[4],
 679                   a->nvram->sas_addr[5],
 680                   a->nvram->sas_addr[6],
 681                   a->nvram->sas_addr[7]);
 682
 683        seq_puts(m, "\n"
 684                   "Discovered devices:\n"
 685                   "\n"
 686                   "   #  Target ID\n"
 687                   "---------------\n");
 688
 689        for (t = a->targetdb; t < a->targetdb_end; t++)
 690                if (t->buffered_target_state == TS_PRESENT) {
 691                        seq_printf(m, " %3d   %3d\n",
 692                                   ++dev_count,
 693                                   (u16)(uintptr_t)(t - a->targetdb));
 694                }
 695
 696        if (dev_count == 0)
 697                seq_puts(m, "none\n");
 698
 699        seq_putc(m, '\n');
 700        return 0;
 701
 702}
 703
 704const char *esas2r_info(struct Scsi_Host *sh)
 705{
 706        struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
 707        static char esas2r_info_str[512];
 708
 709        esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
 710                       "esas2r_info() called");
 711
 712        /*
 713         * if we haven't done so already, register as a char driver
 714         * and stick a node under "/proc/scsi/esas2r/ATTOnode"
 715         */
 716
 717        if (esas2r_proc_major <= 0) {
 718                esas2r_proc_host = sh;
 719
 720                esas2r_proc_major = register_chrdev(0, ESAS2R_DRVR_NAME,
 721                                                    &esas2r_proc_fops);
 722
 723                esas2r_log_dev(ESAS2R_LOG_DEBG, &(sh->shost_gendev),
 724                               "register_chrdev (major %d)",
 725                               esas2r_proc_major);
 726
 727                if (esas2r_proc_major > 0) {
 728                        struct proc_dir_entry *pde;
 729
 730                        pde = proc_create(ATTONODE_NAME, 0,
 731                                          sh->hostt->proc_dir,
 732                                          &esas2r_proc_fops);
 733
 734                        if (!pde) {
 735                                esas2r_log_dev(ESAS2R_LOG_WARN,
 736                                               &(sh->shost_gendev),
 737                                               "failed to create_proc_entry");
 738                                esas2r_proc_major = -1;
 739                        }
 740                }
 741        }
 742
 743        sprintf(esas2r_info_str,
 744                ESAS2R_LONGNAME " (bus 0x%02X, device 0x%02X, IRQ 0x%02X)"
 745                " driver version: "ESAS2R_VERSION_STR "  firmware version: "
 746                "%s\n",
 747                a->pcid->bus->number, a->pcid->devfn, a->pcid->irq,
 748                a->fw_rev[0] ? a->fw_rev : "(none)");
 749
 750        return esas2r_info_str;
 751}
 752
 753/* Callback for building a request scatter/gather list */
 754static u32 get_physaddr_from_sgc(struct esas2r_sg_context *sgc, u64 *addr)
 755{
 756        u32 len;
 757
 758        if (likely(sgc->cur_offset == sgc->exp_offset)) {
 759                /*
 760                 * the normal case: caller used all bytes from previous call, so
 761                 * expected offset is the same as the current offset.
 762                 */
 763
 764                if (sgc->sgel_count < sgc->num_sgel) {
 765                        /* retrieve next segment, except for first time */
 766                        if (sgc->exp_offset > (u8 *)0) {
 767                                /* advance current segment */
 768                                sgc->cur_sgel = sg_next(sgc->cur_sgel);
 769                                ++(sgc->sgel_count);
 770                        }
 771
 772
 773                        len = sg_dma_len(sgc->cur_sgel);
 774                        (*addr) = sg_dma_address(sgc->cur_sgel);
 775
 776                        /* save the total # bytes returned to caller so far */
 777                        sgc->exp_offset += len;
 778
 779                } else {
 780                        len = 0;
 781                }
 782        } else if (sgc->cur_offset < sgc->exp_offset) {
 783                /*
 784                 * caller did not use all bytes from previous call. need to
 785                 * compute the address based on current segment.
 786                 */
 787
 788                len = sg_dma_len(sgc->cur_sgel);
 789                (*addr) = sg_dma_address(sgc->cur_sgel);
 790
 791                sgc->exp_offset -= len;
 792
 793                /* calculate PA based on prev segment address and offsets */
 794                *addr = *addr +
 795                        (sgc->cur_offset - sgc->exp_offset);
 796
 797                sgc->exp_offset += len;
 798
 799                /* re-calculate length based on offset */
 800                len = lower_32_bits(
 801                        sgc->exp_offset - sgc->cur_offset);
 802        } else {   /* if ( sgc->cur_offset > sgc->exp_offset ) */
 803                   /*
 804                    * we don't expect the caller to skip ahead.
 805                    * cur_offset will never exceed the len we return
 806                    */
 807                len = 0;
 808        }
 809
 810        return len;
 811}
 812
 813int esas2r_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
 814{
 815        struct esas2r_adapter *a =
 816                (struct esas2r_adapter *)cmd->device->host->hostdata;
 817        struct esas2r_request *rq;
 818        struct esas2r_sg_context sgc;
 819        unsigned bufflen;
 820
 821        /* Assume success, if it fails we will fix the result later. */
 822        cmd->result = DID_OK << 16;
 823
 824        if (unlikely(test_bit(AF_DEGRADED_MODE, &a->flags))) {
 825                cmd->result = DID_NO_CONNECT << 16;
 826                cmd->scsi_done(cmd);
 827                return 0;
 828        }
 829
 830        rq = esas2r_alloc_request(a);
 831        if (unlikely(rq == NULL)) {
 832                esas2r_debug("esas2r_alloc_request failed");
 833                return SCSI_MLQUEUE_HOST_BUSY;
 834        }
 835
 836        rq->cmd = cmd;
 837        bufflen = scsi_bufflen(cmd);
 838
 839        if (likely(bufflen != 0)) {
 840                if (cmd->sc_data_direction == DMA_TO_DEVICE)
 841                        rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD);
 842                else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
 843                        rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD);
 844        }
 845
 846        memcpy(rq->vrq->scsi.cdb, cmd->cmnd, cmd->cmd_len);
 847        rq->vrq->scsi.length = cpu_to_le32(bufflen);
 848        rq->target_id = cmd->device->id;
 849        rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
 850        rq->sense_buf = cmd->sense_buffer;
 851        rq->sense_len = SCSI_SENSE_BUFFERSIZE;
 852
 853        esas2r_sgc_init(&sgc, a, rq, NULL);
 854
 855        sgc.length = bufflen;
 856        sgc.cur_offset = NULL;
 857
 858        sgc.cur_sgel = scsi_sglist(cmd);
 859        sgc.exp_offset = NULL;
 860        sgc.num_sgel = scsi_dma_map(cmd);
 861        sgc.sgel_count = 0;
 862
 863        if (unlikely(sgc.num_sgel < 0)) {
 864                esas2r_free_request(a, rq);
 865                return SCSI_MLQUEUE_HOST_BUSY;
 866        }
 867
 868        sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_from_sgc;
 869
 870        if (unlikely(!esas2r_build_sg_list(a, rq, &sgc))) {
 871                scsi_dma_unmap(cmd);
 872                esas2r_free_request(a, rq);
 873                return SCSI_MLQUEUE_HOST_BUSY;
 874        }
 875
 876        esas2r_debug("start request %p to %d:%d\n", rq, (int)cmd->device->id,
 877                     (int)cmd->device->lun);
 878
 879        esas2r_start_request(a, rq);
 880
 881        return 0;
 882}
 883
 884static void complete_task_management_request(struct esas2r_adapter *a,
 885                                             struct esas2r_request *rq)
 886{
 887        (*rq->task_management_status_ptr) = rq->req_stat;
 888        esas2r_free_request(a, rq);
 889}
 890
 891/**
 892 * Searches the specified queue for the specified queue for the command
 893 * to abort.
 894 *
 895 * @param [in] a
 896 * @param [in] abort_request
 897 * @param [in] cmd
 898 * t
 899 * @return 0 on failure, 1 if command was not found, 2 if command was found
 900 */
 901static int esas2r_check_active_queue(struct esas2r_adapter *a,
 902                                     struct esas2r_request **abort_request,
 903                                     struct scsi_cmnd *cmd,
 904                                     struct list_head *queue)
 905{
 906        bool found = false;
 907        struct esas2r_request *ar = *abort_request;
 908        struct esas2r_request *rq;
 909        struct list_head *element, *next;
 910
 911        list_for_each_safe(element, next, queue) {
 912
 913                rq = list_entry(element, struct esas2r_request, req_list);
 914
 915                if (rq->cmd == cmd) {
 916
 917                        /* Found the request.  See what to do with it. */
 918                        if (queue == &a->active_list) {
 919                                /*
 920                                 * We are searching the active queue, which
 921                                 * means that we need to send an abort request
 922                                 * to the firmware.
 923                                 */
 924                                ar = esas2r_alloc_request(a);
 925                                if (ar == NULL) {
 926                                        esas2r_log_dev(ESAS2R_LOG_WARN,
 927                                                       &(a->host->shost_gendev),
 928                                                       "unable to allocate an abort request for cmd %p",
 929                                                       cmd);
 930                                        return 0; /* Failure */
 931                                }
 932
 933                                /*
 934                                 * Task management request must be formatted
 935                                 * with a lock held.
 936                                 */
 937                                ar->sense_len = 0;
 938                                ar->vrq->scsi.length = 0;
 939                                ar->target_id = rq->target_id;
 940                                ar->vrq->scsi.flags |= cpu_to_le32(
 941                                        (u8)le32_to_cpu(rq->vrq->scsi.flags));
 942
 943                                memset(ar->vrq->scsi.cdb, 0,
 944                                       sizeof(ar->vrq->scsi.cdb));
 945
 946                                ar->vrq->scsi.flags |= cpu_to_le32(
 947                                        FCP_CMND_TRM);
 948                                ar->vrq->scsi.u.abort_handle =
 949                                        rq->vrq->scsi.handle;
 950                        } else {
 951                                /*
 952                                 * The request is pending but not active on
 953                                 * the firmware.  Just free it now and we'll
 954                                 * report the successful abort below.
 955                                 */
 956                                list_del_init(&rq->req_list);
 957                                esas2r_free_request(a, rq);
 958                        }
 959
 960                        found = true;
 961                        break;
 962                }
 963
 964        }
 965
 966        if (!found)
 967                return 1;       /* Not found */
 968
 969        return 2;               /* found */
 970
 971
 972}
 973
 974int esas2r_eh_abort(struct scsi_cmnd *cmd)
 975{
 976        struct esas2r_adapter *a =
 977                (struct esas2r_adapter *)cmd->device->host->hostdata;
 978        struct esas2r_request *abort_request = NULL;
 979        unsigned long flags;
 980        struct list_head *queue;
 981        int result;
 982
 983        esas2r_log(ESAS2R_LOG_INFO, "eh_abort (%p)", cmd);
 984
 985        if (test_bit(AF_DEGRADED_MODE, &a->flags)) {
 986                cmd->result = DID_ABORT << 16;
 987
 988                scsi_set_resid(cmd, 0);
 989
 990                cmd->scsi_done(cmd);
 991
 992                return SUCCESS;
 993        }
 994
 995        spin_lock_irqsave(&a->queue_lock, flags);
 996
 997        /*
 998         * Run through the defer and active queues looking for the request
 999         * to abort.
1000         */
1001
1002        queue = &a->defer_list;
1003
1004check_active_queue:
1005
1006        result = esas2r_check_active_queue(a, &abort_request, cmd, queue);
1007
1008        if (!result) {
1009                spin_unlock_irqrestore(&a->queue_lock, flags);
1010                return FAILED;
1011        } else if (result == 2 && (queue == &a->defer_list)) {
1012                queue = &a->active_list;
1013                goto check_active_queue;
1014        }
1015
1016        spin_unlock_irqrestore(&a->queue_lock, flags);
1017
1018        if (abort_request) {
1019                u8 task_management_status = RS_PENDING;
1020
1021                /*
1022                 * the request is already active, so we need to tell
1023                 * the firmware to abort it and wait for the response.
1024                 */
1025
1026                abort_request->comp_cb = complete_task_management_request;
1027                abort_request->task_management_status_ptr =
1028                        &task_management_status;
1029
1030                esas2r_start_request(a, abort_request);
1031
1032                if (atomic_read(&a->disable_cnt) == 0)
1033                        esas2r_do_deferred_processes(a);
1034
1035                while (task_management_status == RS_PENDING)
1036                        msleep(10);
1037
1038                /*
1039                 * Once we get here, the original request will have been
1040                 * completed by the firmware and the abort request will have
1041                 * been cleaned up.  we're done!
1042                 */
1043
1044                return SUCCESS;
1045        }
1046
1047        /*
1048         * If we get here, either we found the inactive request and
1049         * freed it, or we didn't find it at all.  Either way, success!
1050         */
1051
1052        cmd->result = DID_ABORT << 16;
1053
1054        scsi_set_resid(cmd, 0);
1055
1056        cmd->scsi_done(cmd);
1057
1058        return SUCCESS;
1059}
1060
1061static int esas2r_host_bus_reset(struct scsi_cmnd *cmd, bool host_reset)
1062{
1063        struct esas2r_adapter *a =
1064                (struct esas2r_adapter *)cmd->device->host->hostdata;
1065
1066        if (test_bit(AF_DEGRADED_MODE, &a->flags))
1067                return FAILED;
1068
1069        if (host_reset)
1070                esas2r_reset_adapter(a);
1071        else
1072                esas2r_reset_bus(a);
1073
1074        /* above call sets the AF_OS_RESET flag.  wait for it to clear. */
1075
1076        while (test_bit(AF_OS_RESET, &a->flags)) {
1077                msleep(10);
1078
1079                if (test_bit(AF_DEGRADED_MODE, &a->flags))
1080                        return FAILED;
1081        }
1082
1083        if (test_bit(AF_DEGRADED_MODE, &a->flags))
1084                return FAILED;
1085
1086        return SUCCESS;
1087}
1088
1089int esas2r_host_reset(struct scsi_cmnd *cmd)
1090{
1091        esas2r_log(ESAS2R_LOG_INFO, "host_reset (%p)", cmd);
1092
1093        return esas2r_host_bus_reset(cmd, true);
1094}
1095
1096int esas2r_bus_reset(struct scsi_cmnd *cmd)
1097{
1098        esas2r_log(ESAS2R_LOG_INFO, "bus_reset (%p)", cmd);
1099
1100        return esas2r_host_bus_reset(cmd, false);
1101}
1102
1103static int esas2r_dev_targ_reset(struct scsi_cmnd *cmd, bool target_reset)
1104{
1105        struct esas2r_adapter *a =
1106                (struct esas2r_adapter *)cmd->device->host->hostdata;
1107        struct esas2r_request *rq;
1108        u8 task_management_status = RS_PENDING;
1109        bool completed;
1110
1111        if (test_bit(AF_DEGRADED_MODE, &a->flags))
1112                return FAILED;
1113
1114retry:
1115        rq = esas2r_alloc_request(a);
1116        if (rq == NULL) {
1117                if (target_reset) {
1118                        esas2r_log(ESAS2R_LOG_CRIT,
1119                                   "unable to allocate a request for a "
1120                                   "target reset (%d)!",
1121                                   cmd->device->id);
1122                } else {
1123                        esas2r_log(ESAS2R_LOG_CRIT,
1124                                   "unable to allocate a request for a "
1125                                   "device reset (%d:%llu)!",
1126                                   cmd->device->id,
1127                                   cmd->device->lun);
1128                }
1129
1130
1131                return FAILED;
1132        }
1133
1134        rq->target_id = cmd->device->id;
1135        rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
1136        rq->req_stat = RS_PENDING;
1137
1138        rq->comp_cb = complete_task_management_request;
1139        rq->task_management_status_ptr = &task_management_status;
1140
1141        if (target_reset) {
1142                esas2r_debug("issuing target reset (%p) to id %d", rq,
1143                             cmd->device->id);
1144                completed = esas2r_send_task_mgmt(a, rq, 0x20);
1145        } else {
1146                esas2r_debug("issuing device reset (%p) to id %d lun %d", rq,
1147                             cmd->device->id, cmd->device->lun);
1148                completed = esas2r_send_task_mgmt(a, rq, 0x10);
1149        }
1150
1151        if (completed) {
1152                /* Task management cmd completed right away, need to free it. */
1153
1154                esas2r_free_request(a, rq);
1155        } else {
1156                /*
1157                 * Wait for firmware to complete the request.  Completion
1158                 * callback will free it.
1159                 */
1160                while (task_management_status == RS_PENDING)
1161                        msleep(10);
1162        }
1163
1164        if (test_bit(AF_DEGRADED_MODE, &a->flags))
1165                return FAILED;
1166
1167        if (task_management_status == RS_BUSY) {
1168                /*
1169                 * Busy, probably because we are flashing.  Wait a bit and
1170                 * try again.
1171                 */
1172                msleep(100);
1173                goto retry;
1174        }
1175
1176        return SUCCESS;
1177}
1178
1179int esas2r_device_reset(struct scsi_cmnd *cmd)
1180{
1181        esas2r_log(ESAS2R_LOG_INFO, "device_reset (%p)", cmd);
1182
1183        return esas2r_dev_targ_reset(cmd, false);
1184
1185}
1186
1187int esas2r_target_reset(struct scsi_cmnd *cmd)
1188{
1189        esas2r_log(ESAS2R_LOG_INFO, "target_reset (%p)", cmd);
1190
1191        return esas2r_dev_targ_reset(cmd, true);
1192}
1193
1194void esas2r_log_request_failure(struct esas2r_adapter *a,
1195                                struct esas2r_request *rq)
1196{
1197        u8 reqstatus = rq->req_stat;
1198
1199        if (reqstatus == RS_SUCCESS)
1200                return;
1201
1202        if (rq->vrq->scsi.function == VDA_FUNC_SCSI) {
1203                if (reqstatus == RS_SCSI_ERROR) {
1204                        if (rq->func_rsp.scsi_rsp.sense_len >= 13) {
1205                                esas2r_log(ESAS2R_LOG_WARN,
1206                                           "request failure - SCSI error %x ASC:%x ASCQ:%x CDB:%x",
1207                                           rq->sense_buf[2], rq->sense_buf[12],
1208                                           rq->sense_buf[13],
1209                                           rq->vrq->scsi.cdb[0]);
1210                        } else {
1211                                esas2r_log(ESAS2R_LOG_WARN,
1212                                           "request failure - SCSI error CDB:%x\n",
1213                                           rq->vrq->scsi.cdb[0]);
1214                        }
1215                } else if ((rq->vrq->scsi.cdb[0] != INQUIRY
1216                            && rq->vrq->scsi.cdb[0] != REPORT_LUNS)
1217                           || (reqstatus != RS_SEL
1218                               && reqstatus != RS_SEL2)) {
1219                        if ((reqstatus == RS_UNDERRUN) &&
1220                            (rq->vrq->scsi.cdb[0] == INQUIRY)) {
1221                                /* Don't log inquiry underruns */
1222                        } else {
1223                                esas2r_log(ESAS2R_LOG_WARN,
1224                                           "request failure - cdb:%x reqstatus:%d target:%d",
1225                                           rq->vrq->scsi.cdb[0], reqstatus,
1226                                           rq->target_id);
1227                        }
1228                }
1229        }
1230}
1231
1232void esas2r_wait_request(struct esas2r_adapter *a, struct esas2r_request *rq)
1233{
1234        u32 starttime;
1235        u32 timeout;
1236
1237        starttime = jiffies_to_msecs(jiffies);
1238        timeout = rq->timeout ? rq->timeout : 5000;
1239
1240        while (true) {
1241                esas2r_polled_interrupt(a);
1242
1243                if (rq->req_stat != RS_STARTED)
1244                        break;
1245
1246                schedule_timeout_interruptible(msecs_to_jiffies(100));
1247
1248                if ((jiffies_to_msecs(jiffies) - starttime) > timeout) {
1249                        esas2r_hdebug("request TMO");
1250                        esas2r_bugon();
1251
1252                        rq->req_stat = RS_TIMEOUT;
1253
1254                        esas2r_local_reset_adapter(a);
1255                        return;
1256                }
1257        }
1258}
1259
1260u32 esas2r_map_data_window(struct esas2r_adapter *a, u32 addr_lo)
1261{
1262        u32 offset = addr_lo & (MW_DATA_WINDOW_SIZE - 1);
1263        u32 base = addr_lo & -(signed int)MW_DATA_WINDOW_SIZE;
1264
1265        if (a->window_base != base) {
1266                esas2r_write_register_dword(a, MVR_PCI_WIN1_REMAP,
1267                                            base | MVRPW1R_ENABLE);
1268                esas2r_flush_register_dword(a, MVR_PCI_WIN1_REMAP);
1269                a->window_base = base;
1270        }
1271
1272        return offset;
1273}
1274
1275/* Read a block of data from chip memory */
1276bool esas2r_read_mem_block(struct esas2r_adapter *a,
1277                           void *to,
1278                           u32 from,
1279                           u32 size)
1280{
1281        u8 *end = (u8 *)to;
1282
1283        while (size) {
1284                u32 len;
1285                u32 offset;
1286                u32 iatvr;
1287
1288                iatvr = (from & -(signed int)MW_DATA_WINDOW_SIZE);
1289
1290                esas2r_map_data_window(a, iatvr);
1291
1292                offset = from & (MW_DATA_WINDOW_SIZE - 1);
1293                len = size;
1294
1295                if (len > MW_DATA_WINDOW_SIZE - offset)
1296                        len = MW_DATA_WINDOW_SIZE - offset;
1297
1298                from += len;
1299                size -= len;
1300
1301                while (len--) {
1302                        *end++ = esas2r_read_data_byte(a, offset);
1303                        offset++;
1304                }
1305        }
1306
1307        return true;
1308}
1309
1310void esas2r_nuxi_mgt_data(u8 function, void *data)
1311{
1312        struct atto_vda_grp_info *g;
1313        struct atto_vda_devinfo *d;
1314        struct atto_vdapart_info *p;
1315        struct atto_vda_dh_info *h;
1316        struct atto_vda_metrics_info *m;
1317        struct atto_vda_schedule_info *s;
1318        struct atto_vda_buzzer_info *b;
1319        u8 i;
1320
1321        switch (function) {
1322        case VDAMGT_BUZZER_INFO:
1323        case VDAMGT_BUZZER_SET:
1324
1325                b = (struct atto_vda_buzzer_info *)data;
1326
1327                b->duration = le32_to_cpu(b->duration);
1328                break;
1329
1330        case VDAMGT_SCHEDULE_INFO:
1331        case VDAMGT_SCHEDULE_EVENT:
1332
1333                s = (struct atto_vda_schedule_info *)data;
1334
1335                s->id = le32_to_cpu(s->id);
1336
1337                break;
1338
1339        case VDAMGT_DEV_INFO:
1340        case VDAMGT_DEV_CLEAN:
1341        case VDAMGT_DEV_PT_INFO:
1342        case VDAMGT_DEV_FEATURES:
1343        case VDAMGT_DEV_PT_FEATURES:
1344        case VDAMGT_DEV_OPERATION:
1345
1346                d = (struct atto_vda_devinfo *)data;
1347
1348                d->capacity = le64_to_cpu(d->capacity);
1349                d->block_size = le32_to_cpu(d->block_size);
1350                d->ses_dev_index = le16_to_cpu(d->ses_dev_index);
1351                d->target_id = le16_to_cpu(d->target_id);
1352                d->lun = le16_to_cpu(d->lun);
1353                d->features = le16_to_cpu(d->features);
1354                break;
1355
1356        case VDAMGT_GRP_INFO:
1357        case VDAMGT_GRP_CREATE:
1358        case VDAMGT_GRP_DELETE:
1359        case VDAMGT_ADD_STORAGE:
1360        case VDAMGT_MEMBER_ADD:
1361        case VDAMGT_GRP_COMMIT:
1362        case VDAMGT_GRP_REBUILD:
1363        case VDAMGT_GRP_COMMIT_INIT:
1364        case VDAMGT_QUICK_RAID:
1365        case VDAMGT_GRP_FEATURES:
1366        case VDAMGT_GRP_COMMIT_INIT_AUTOMAP:
1367        case VDAMGT_QUICK_RAID_INIT_AUTOMAP:
1368        case VDAMGT_SPARE_LIST:
1369        case VDAMGT_SPARE_ADD:
1370        case VDAMGT_SPARE_REMOVE:
1371        case VDAMGT_LOCAL_SPARE_ADD:
1372        case VDAMGT_GRP_OPERATION:
1373
1374                g = (struct atto_vda_grp_info *)data;
1375
1376                g->capacity = le64_to_cpu(g->capacity);
1377                g->block_size = le32_to_cpu(g->block_size);
1378                g->interleave = le32_to_cpu(g->interleave);
1379                g->features = le16_to_cpu(g->features);
1380
1381                for (i = 0; i < 32; i++)
1382                        g->members[i] = le16_to_cpu(g->members[i]);
1383
1384                break;
1385
1386        case VDAMGT_PART_INFO:
1387        case VDAMGT_PART_MAP:
1388        case VDAMGT_PART_UNMAP:
1389        case VDAMGT_PART_AUTOMAP:
1390        case VDAMGT_PART_SPLIT:
1391        case VDAMGT_PART_MERGE:
1392
1393                p = (struct atto_vdapart_info *)data;
1394
1395                p->part_size = le64_to_cpu(p->part_size);
1396                p->start_lba = le32_to_cpu(p->start_lba);
1397                p->block_size = le32_to_cpu(p->block_size);
1398                p->target_id = le16_to_cpu(p->target_id);
1399                break;
1400
1401        case VDAMGT_DEV_HEALTH_REQ:
1402
1403                h = (struct atto_vda_dh_info *)data;
1404
1405                h->med_defect_cnt = le32_to_cpu(h->med_defect_cnt);
1406                h->info_exc_cnt = le32_to_cpu(h->info_exc_cnt);
1407                break;
1408
1409        case VDAMGT_DEV_METRICS:
1410
1411                m = (struct atto_vda_metrics_info *)data;
1412
1413                for (i = 0; i < 32; i++)
1414                        m->dev_indexes[i] = le16_to_cpu(m->dev_indexes[i]);
1415
1416                break;
1417
1418        default:
1419                break;
1420        }
1421}
1422
1423void esas2r_nuxi_cfg_data(u8 function, void *data)
1424{
1425        struct atto_vda_cfg_init *ci;
1426
1427        switch (function) {
1428        case VDA_CFG_INIT:
1429        case VDA_CFG_GET_INIT:
1430        case VDA_CFG_GET_INIT2:
1431
1432                ci = (struct atto_vda_cfg_init *)data;
1433
1434                ci->date_time.year = le16_to_cpu(ci->date_time.year);
1435                ci->sgl_page_size = le32_to_cpu(ci->sgl_page_size);
1436                ci->vda_version = le32_to_cpu(ci->vda_version);
1437                ci->epoch_time = le32_to_cpu(ci->epoch_time);
1438                ci->ioctl_tunnel = le32_to_cpu(ci->ioctl_tunnel);
1439                ci->num_targets_backend = le32_to_cpu(ci->num_targets_backend);
1440                break;
1441
1442        default:
1443                break;
1444        }
1445}
1446
1447void esas2r_nuxi_ae_data(union atto_vda_ae *ae)
1448{
1449        struct atto_vda_ae_raid *r = &ae->raid;
1450        struct atto_vda_ae_lu *l = &ae->lu;
1451
1452        switch (ae->hdr.bytype) {
1453        case VDAAE_HDR_TYPE_RAID:
1454
1455                r->dwflags = le32_to_cpu(r->dwflags);
1456                break;
1457
1458        case VDAAE_HDR_TYPE_LU:
1459
1460                l->dwevent = le32_to_cpu(l->dwevent);
1461                l->wphys_target_id = le16_to_cpu(l->wphys_target_id);
1462                l->id.tgtlun.wtarget_id = le16_to_cpu(l->id.tgtlun.wtarget_id);
1463
1464                if (l->hdr.bylength >= offsetof(struct atto_vda_ae_lu, id)
1465                    + sizeof(struct atto_vda_ae_lu_tgt_lun_raid)) {
1466                        l->id.tgtlun_raid.dwinterleave
1467                                = le32_to_cpu(l->id.tgtlun_raid.dwinterleave);
1468                        l->id.tgtlun_raid.dwblock_size
1469                                = le32_to_cpu(l->id.tgtlun_raid.dwblock_size);
1470                }
1471
1472                break;
1473
1474        case VDAAE_HDR_TYPE_DISK:
1475        default:
1476                break;
1477        }
1478}
1479
1480void esas2r_free_request(struct esas2r_adapter *a, struct esas2r_request *rq)
1481{
1482        unsigned long flags;
1483
1484        esas2r_rq_destroy_request(rq, a);
1485        spin_lock_irqsave(&a->request_lock, flags);
1486        list_add(&rq->comp_list, &a->avail_request);
1487        spin_unlock_irqrestore(&a->request_lock, flags);
1488}
1489
1490struct esas2r_request *esas2r_alloc_request(struct esas2r_adapter *a)
1491{
1492        struct esas2r_request *rq;
1493        unsigned long flags;
1494
1495        spin_lock_irqsave(&a->request_lock, flags);
1496
1497        if (unlikely(list_empty(&a->avail_request))) {
1498                spin_unlock_irqrestore(&a->request_lock, flags);
1499                return NULL;
1500        }
1501
1502        rq = list_first_entry(&a->avail_request, struct esas2r_request,
1503                              comp_list);
1504        list_del(&rq->comp_list);
1505        spin_unlock_irqrestore(&a->request_lock, flags);
1506        esas2r_rq_init_request(rq, a);
1507
1508        return rq;
1509
1510}
1511
1512void esas2r_complete_request_cb(struct esas2r_adapter *a,
1513                                struct esas2r_request *rq)
1514{
1515        esas2r_debug("completing request %p\n", rq);
1516
1517        scsi_dma_unmap(rq->cmd);
1518
1519        if (unlikely(rq->req_stat != RS_SUCCESS)) {
1520                esas2r_debug("[%x STATUS %x:%x (%x)]", rq->target_id,
1521                             rq->req_stat,
1522                             rq->func_rsp.scsi_rsp.scsi_stat,
1523                             rq->cmd);
1524
1525                rq->cmd->result =
1526                        ((esas2r_req_status_to_error(rq->req_stat) << 16)
1527                         | (rq->func_rsp.scsi_rsp.scsi_stat & STATUS_MASK));
1528
1529                if (rq->req_stat == RS_UNDERRUN)
1530                        scsi_set_resid(rq->cmd,
1531                                       le32_to_cpu(rq->func_rsp.scsi_rsp.
1532                                                   residual_length));
1533                else
1534                        scsi_set_resid(rq->cmd, 0);
1535        }
1536
1537        rq->cmd->scsi_done(rq->cmd);
1538
1539        esas2r_free_request(a, rq);
1540}
1541
1542/* Run tasklet to handle stuff outside of interrupt context. */
1543void esas2r_adapter_tasklet(unsigned long context)
1544{
1545        struct esas2r_adapter *a = (struct esas2r_adapter *)context;
1546
1547        if (unlikely(test_bit(AF2_TIMER_TICK, &a->flags2))) {
1548                clear_bit(AF2_TIMER_TICK, &a->flags2);
1549                esas2r_timer_tick(a);
1550        }
1551
1552        if (likely(test_bit(AF2_INT_PENDING, &a->flags2))) {
1553                clear_bit(AF2_INT_PENDING, &a->flags2);
1554                esas2r_adapter_interrupt(a);
1555        }
1556
1557        if (esas2r_is_tasklet_pending(a))
1558                esas2r_do_tasklet_tasks(a);
1559
1560        if (esas2r_is_tasklet_pending(a)
1561            || (test_bit(AF2_INT_PENDING, &a->flags2))
1562            || (test_bit(AF2_TIMER_TICK, &a->flags2))) {
1563                clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1564                esas2r_schedule_tasklet(a);
1565        } else {
1566                clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1567        }
1568}
1569
1570static void esas2r_timer_callback(struct timer_list *t);
1571
1572void esas2r_kickoff_timer(struct esas2r_adapter *a)
1573{
1574        timer_setup(&a->timer, esas2r_timer_callback, 0);
1575
1576        a->timer.expires = jiffies +
1577                           msecs_to_jiffies(100);
1578
1579        add_timer(&a->timer);
1580}
1581
1582static void esas2r_timer_callback(struct timer_list *t)
1583{
1584        struct esas2r_adapter *a = from_timer(a, t, timer);
1585
1586        set_bit(AF2_TIMER_TICK, &a->flags2);
1587
1588        esas2r_schedule_tasklet(a);
1589
1590        esas2r_kickoff_timer(a);
1591}
1592
1593/*
1594 * Firmware events need to be handled outside of interrupt context
1595 * so we schedule a delayed_work to handle them.
1596 */
1597
1598static void
1599esas2r_free_fw_event(struct esas2r_fw_event_work *fw_event)
1600{
1601        unsigned long flags;
1602        struct esas2r_adapter *a = fw_event->a;
1603
1604        spin_lock_irqsave(&a->fw_event_lock, flags);
1605        list_del(&fw_event->list);
1606        kfree(fw_event);
1607        spin_unlock_irqrestore(&a->fw_event_lock, flags);
1608}
1609
1610void
1611esas2r_fw_event_off(struct esas2r_adapter *a)
1612{
1613        unsigned long flags;
1614
1615        spin_lock_irqsave(&a->fw_event_lock, flags);
1616        a->fw_events_off = 1;
1617        spin_unlock_irqrestore(&a->fw_event_lock, flags);
1618}
1619
1620void
1621esas2r_fw_event_on(struct esas2r_adapter *a)
1622{
1623        unsigned long flags;
1624
1625        spin_lock_irqsave(&a->fw_event_lock, flags);
1626        a->fw_events_off = 0;
1627        spin_unlock_irqrestore(&a->fw_event_lock, flags);
1628}
1629
1630static void esas2r_add_device(struct esas2r_adapter *a, u16 target_id)
1631{
1632        int ret;
1633        struct scsi_device *scsi_dev;
1634
1635        scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
1636
1637        if (scsi_dev) {
1638                esas2r_log_dev(
1639                        ESAS2R_LOG_WARN,
1640                        &(scsi_dev->
1641                          sdev_gendev),
1642                        "scsi device already exists at id %d", target_id);
1643
1644                scsi_device_put(scsi_dev);
1645        } else {
1646                esas2r_log_dev(
1647                        ESAS2R_LOG_INFO,
1648                        &(a->host->
1649                          shost_gendev),
1650                        "scsi_add_device() called for 0:%d:0",
1651                        target_id);
1652
1653                ret = scsi_add_device(a->host, 0, target_id, 0);
1654                if (ret) {
1655                        esas2r_log_dev(
1656                                ESAS2R_LOG_CRIT,
1657                                &(a->host->
1658                                  shost_gendev),
1659                                "scsi_add_device failed with %d for id %d",
1660                                ret, target_id);
1661                }
1662        }
1663}
1664
1665static void esas2r_remove_device(struct esas2r_adapter *a, u16 target_id)
1666{
1667        struct scsi_device *scsi_dev;
1668
1669        scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
1670
1671        if (scsi_dev) {
1672                scsi_device_set_state(scsi_dev, SDEV_OFFLINE);
1673
1674                esas2r_log_dev(
1675                        ESAS2R_LOG_INFO,
1676                        &(scsi_dev->
1677                          sdev_gendev),
1678                        "scsi_remove_device() called for 0:%d:0",
1679                        target_id);
1680
1681                scsi_remove_device(scsi_dev);
1682
1683                esas2r_log_dev(
1684                        ESAS2R_LOG_INFO,
1685                        &(scsi_dev->
1686                          sdev_gendev),
1687                        "scsi_device_put() called");
1688
1689                scsi_device_put(scsi_dev);
1690        } else {
1691                esas2r_log_dev(
1692                        ESAS2R_LOG_WARN,
1693                        &(a->host->shost_gendev),
1694                        "no target found at id %d",
1695                        target_id);
1696        }
1697}
1698
1699/*
1700 * Sends a firmware asynchronous event to anyone who happens to be
1701 * listening on the defined ATTO VDA event ports.
1702 */
1703static void esas2r_send_ae_event(struct esas2r_fw_event_work *fw_event)
1704{
1705        struct esas2r_vda_ae *ae = (struct esas2r_vda_ae *)fw_event->data;
1706        char *type;
1707
1708        switch (ae->vda_ae.hdr.bytype) {
1709        case VDAAE_HDR_TYPE_RAID:
1710                type = "RAID group state change";
1711                break;
1712
1713        case VDAAE_HDR_TYPE_LU:
1714                type = "Mapped destination LU change";
1715                break;
1716
1717        case VDAAE_HDR_TYPE_DISK:
1718                type = "Physical disk inventory change";
1719                break;
1720
1721        case VDAAE_HDR_TYPE_RESET:
1722                type = "Firmware reset";
1723                break;
1724
1725        case VDAAE_HDR_TYPE_LOG_INFO:
1726                type = "Event Log message (INFO level)";
1727                break;
1728
1729        case VDAAE_HDR_TYPE_LOG_WARN:
1730                type = "Event Log message (WARN level)";
1731                break;
1732
1733        case VDAAE_HDR_TYPE_LOG_CRIT:
1734                type = "Event Log message (CRIT level)";
1735                break;
1736
1737        case VDAAE_HDR_TYPE_LOG_FAIL:
1738                type = "Event Log message (FAIL level)";
1739                break;
1740
1741        case VDAAE_HDR_TYPE_NVC:
1742                type = "NVCache change";
1743                break;
1744
1745        case VDAAE_HDR_TYPE_TLG_INFO:
1746                type = "Time stamped log message (INFO level)";
1747                break;
1748
1749        case VDAAE_HDR_TYPE_TLG_WARN:
1750                type = "Time stamped log message (WARN level)";
1751                break;
1752
1753        case VDAAE_HDR_TYPE_TLG_CRIT:
1754                type = "Time stamped log message (CRIT level)";
1755                break;
1756
1757        case VDAAE_HDR_TYPE_PWRMGT:
1758                type = "Power management";
1759                break;
1760
1761        case VDAAE_HDR_TYPE_MUTE:
1762                type = "Mute button pressed";
1763                break;
1764
1765        case VDAAE_HDR_TYPE_DEV:
1766                type = "Device attribute change";
1767                break;
1768
1769        default:
1770                type = "Unknown";
1771                break;
1772        }
1773
1774        esas2r_log(ESAS2R_LOG_WARN,
1775                   "An async event of type \"%s\" was received from the firmware.  The event contents are:",
1776                   type);
1777        esas2r_log_hexdump(ESAS2R_LOG_WARN, &ae->vda_ae,
1778                           ae->vda_ae.hdr.bylength);
1779
1780}
1781
1782static void
1783esas2r_firmware_event_work(struct work_struct *work)
1784{
1785        struct esas2r_fw_event_work *fw_event =
1786                container_of(work, struct esas2r_fw_event_work, work.work);
1787
1788        struct esas2r_adapter *a = fw_event->a;
1789
1790        u16 target_id = *(u16 *)&fw_event->data[0];
1791
1792        if (a->fw_events_off)
1793                goto done;
1794
1795        switch (fw_event->type) {
1796        case fw_event_null:
1797                break; /* do nothing */
1798
1799        case fw_event_lun_change:
1800                esas2r_remove_device(a, target_id);
1801                esas2r_add_device(a, target_id);
1802                break;
1803
1804        case fw_event_present:
1805                esas2r_add_device(a, target_id);
1806                break;
1807
1808        case fw_event_not_present:
1809                esas2r_remove_device(a, target_id);
1810                break;
1811
1812        case fw_event_vda_ae:
1813                esas2r_send_ae_event(fw_event);
1814                break;
1815        }
1816
1817done:
1818        esas2r_free_fw_event(fw_event);
1819}
1820
1821void esas2r_queue_fw_event(struct esas2r_adapter *a,
1822                           enum fw_event_type type,
1823                           void *data,
1824                           int data_sz)
1825{
1826        struct esas2r_fw_event_work *fw_event;
1827        unsigned long flags;
1828
1829        fw_event = kzalloc(sizeof(struct esas2r_fw_event_work), GFP_ATOMIC);
1830        if (!fw_event) {
1831                esas2r_log(ESAS2R_LOG_WARN,
1832                           "esas2r_queue_fw_event failed to alloc");
1833                return;
1834        }
1835
1836        if (type == fw_event_vda_ae) {
1837                struct esas2r_vda_ae *ae =
1838                        (struct esas2r_vda_ae *)fw_event->data;
1839
1840                ae->signature = ESAS2R_VDA_EVENT_SIG;
1841                ae->bus_number = a->pcid->bus->number;
1842                ae->devfn = a->pcid->devfn;
1843                memcpy(&ae->vda_ae, data, sizeof(ae->vda_ae));
1844        } else {
1845                memcpy(fw_event->data, data, data_sz);
1846        }
1847
1848        fw_event->type = type;
1849        fw_event->a = a;
1850
1851        spin_lock_irqsave(&a->fw_event_lock, flags);
1852        list_add_tail(&fw_event->list, &a->fw_event_list);
1853        INIT_DELAYED_WORK(&fw_event->work, esas2r_firmware_event_work);
1854        queue_delayed_work_on(
1855                smp_processor_id(), a->fw_event_q, &fw_event->work,
1856                msecs_to_jiffies(1));
1857        spin_unlock_irqrestore(&a->fw_event_lock, flags);
1858}
1859
1860void esas2r_target_state_changed(struct esas2r_adapter *a, u16 targ_id,
1861                                 u8 state)
1862{
1863        if (state == TS_LUN_CHANGE)
1864                esas2r_queue_fw_event(a, fw_event_lun_change, &targ_id,
1865                                      sizeof(targ_id));
1866        else if (state == TS_PRESENT)
1867                esas2r_queue_fw_event(a, fw_event_present, &targ_id,
1868                                      sizeof(targ_id));
1869        else if (state == TS_NOT_PRESENT)
1870                esas2r_queue_fw_event(a, fw_event_not_present, &targ_id,
1871                                      sizeof(targ_id));
1872}
1873
1874/* Translate status to a Linux SCSI mid-layer error code */
1875int esas2r_req_status_to_error(u8 req_stat)
1876{
1877        switch (req_stat) {
1878        case RS_OVERRUN:
1879        case RS_UNDERRUN:
1880        case RS_SUCCESS:
1881        /*
1882         * NOTE: SCSI mid-layer wants a good status for a SCSI error, because
1883         *       it will check the scsi_stat value in the completion anyway.
1884         */
1885        case RS_SCSI_ERROR:
1886                return DID_OK;
1887
1888        case RS_SEL:
1889        case RS_SEL2:
1890                return DID_NO_CONNECT;
1891
1892        case RS_RESET:
1893                return DID_RESET;
1894
1895        case RS_ABORTED:
1896                return DID_ABORT;
1897
1898        case RS_BUSY:
1899                return DID_BUS_BUSY;
1900        }
1901
1902        /* everything else is just an error. */
1903
1904        return DID_ERROR;
1905}
1906
1907module_init(esas2r_init);
1908module_exit(esas2r_exit);
1909