linux/drivers/scsi/smartpqi/smartpqi_init.c
<<
>>
Prefs
   1/*
   2 *    driver for Microsemi PQI-based storage controllers
   3 *    Copyright (c) 2016-2017 Microsemi Corporation
   4 *    Copyright (c) 2016 PMC-Sierra, Inc.
   5 *
   6 *    This program is free software; you can redistribute it and/or modify
   7 *    it under the terms of the GNU General Public License as published by
   8 *    the Free Software Foundation; version 2 of the License.
   9 *
  10 *    This program is distributed in the hope that it will be useful,
  11 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 *    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  13 *    NON INFRINGEMENT.  See the GNU General Public License for more details.
  14 *
  15 *    Questions/Comments/Bugfixes to esc.storagedev@microsemi.com
  16 *
  17 */
  18
  19#include <linux/module.h>
  20#include <linux/kernel.h>
  21#include <linux/pci.h>
  22#include <linux/delay.h>
  23#include <linux/interrupt.h>
  24#include <linux/sched.h>
  25#include <linux/rtc.h>
  26#include <linux/bcd.h>
  27#include <linux/reboot.h>
  28#include <linux/cciss_ioctl.h>
  29#include <linux/blk-mq-pci.h>
  30#include <scsi/scsi_host.h>
  31#include <scsi/scsi_cmnd.h>
  32#include <scsi/scsi_device.h>
  33#include <scsi/scsi_eh.h>
  34#include <scsi/scsi_transport_sas.h>
  35#include <asm/unaligned.h>
  36#include "smartpqi.h"
  37#include "smartpqi_sis.h"
  38
  39#if !defined(BUILD_TIMESTAMP)
  40#define BUILD_TIMESTAMP
  41#endif
  42
  43#define DRIVER_VERSION          "1.1.2-125"
  44#define DRIVER_MAJOR            1
  45#define DRIVER_MINOR            1
  46#define DRIVER_RELEASE          2
  47#define DRIVER_REVISION         125
  48
  49#define DRIVER_NAME             "Microsemi PQI Driver (v" \
  50                                DRIVER_VERSION BUILD_TIMESTAMP ")"
  51#define DRIVER_NAME_SHORT       "smartpqi"
  52
  53#define PQI_EXTRA_SGL_MEMORY    (12 * sizeof(struct pqi_sg_descriptor))
  54
  55MODULE_AUTHOR("Microsemi");
  56MODULE_DESCRIPTION("Driver for Microsemi Smart Family Controller version "
  57        DRIVER_VERSION);
  58MODULE_SUPPORTED_DEVICE("Microsemi Smart Family Controllers");
  59MODULE_VERSION(DRIVER_VERSION);
  60MODULE_LICENSE("GPL");
  61
  62static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info);
  63static void pqi_ctrl_offline_worker(struct work_struct *work);
  64static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info);
  65static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info);
  66static void pqi_scan_start(struct Scsi_Host *shost);
  67static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
  68        struct pqi_queue_group *queue_group, enum pqi_io_path path,
  69        struct pqi_io_request *io_request);
  70static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
  71        struct pqi_iu_header *request, unsigned int flags,
  72        struct pqi_raid_error_info *error_info, unsigned long timeout_msecs);
  73static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
  74        struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
  75        unsigned int cdb_length, struct pqi_queue_group *queue_group,
  76        struct pqi_encryption_info *encryption_info, bool raid_bypass);
  77
  78/* for flags argument to pqi_submit_raid_request_synchronous() */
  79#define PQI_SYNC_FLAGS_INTERRUPTABLE    0x1
  80
  81static struct scsi_transport_template *pqi_sas_transport_template;
  82
  83static atomic_t pqi_controller_count = ATOMIC_INIT(0);
  84
  85enum pqi_lockup_action {
  86        NONE,
  87        REBOOT,
  88        PANIC
  89};
  90
  91static enum pqi_lockup_action pqi_lockup_action = NONE;
  92
  93static struct {
  94        enum pqi_lockup_action  action;
  95        char                    *name;
  96} pqi_lockup_actions[] = {
  97        {
  98                .action = NONE,
  99                .name = "none",
 100        },
 101        {
 102                .action = REBOOT,
 103                .name = "reboot",
 104        },
 105        {
 106                .action = PANIC,
 107                .name = "panic",
 108        },
 109};
 110
 111static unsigned int pqi_supported_event_types[] = {
 112        PQI_EVENT_TYPE_HOTPLUG,
 113        PQI_EVENT_TYPE_HARDWARE,
 114        PQI_EVENT_TYPE_PHYSICAL_DEVICE,
 115        PQI_EVENT_TYPE_LOGICAL_DEVICE,
 116        PQI_EVENT_TYPE_AIO_STATE_CHANGE,
 117        PQI_EVENT_TYPE_AIO_CONFIG_CHANGE,
 118};
 119
 120static int pqi_disable_device_id_wildcards;
 121module_param_named(disable_device_id_wildcards,
 122        pqi_disable_device_id_wildcards, int, 0644);
 123MODULE_PARM_DESC(disable_device_id_wildcards,
 124        "Disable device ID wildcards.");
 125
 126static int pqi_disable_heartbeat;
 127module_param_named(disable_heartbeat,
 128        pqi_disable_heartbeat, int, 0644);
 129MODULE_PARM_DESC(disable_heartbeat,
 130        "Disable heartbeat.");
 131
 132static int pqi_disable_ctrl_shutdown;
 133module_param_named(disable_ctrl_shutdown,
 134        pqi_disable_ctrl_shutdown, int, 0644);
 135MODULE_PARM_DESC(disable_ctrl_shutdown,
 136        "Disable controller shutdown when controller locked up.");
 137
 138static char *pqi_lockup_action_param;
 139module_param_named(lockup_action,
 140        pqi_lockup_action_param, charp, 0644);
 141MODULE_PARM_DESC(lockup_action, "Action to take when controller locked up.\n"
 142        "\t\tSupported: none, reboot, panic\n"
 143        "\t\tDefault: none");
 144
 145static char *raid_levels[] = {
 146        "RAID-0",
 147        "RAID-4",
 148        "RAID-1(1+0)",
 149        "RAID-5",
 150        "RAID-5+1",
 151        "RAID-ADG",
 152        "RAID-1(ADM)",
 153};
 154
 155static char *pqi_raid_level_to_string(u8 raid_level)
 156{
 157        if (raid_level < ARRAY_SIZE(raid_levels))
 158                return raid_levels[raid_level];
 159
 160        return "RAID UNKNOWN";
 161}
 162
 163#define SA_RAID_0               0
 164#define SA_RAID_4               1
 165#define SA_RAID_1               2       /* also used for RAID 10 */
 166#define SA_RAID_5               3       /* also used for RAID 50 */
 167#define SA_RAID_51              4
 168#define SA_RAID_6               5       /* also used for RAID 60 */
 169#define SA_RAID_ADM             6       /* also used for RAID 1+0 ADM */
 170#define SA_RAID_MAX             SA_RAID_ADM
 171#define SA_RAID_UNKNOWN         0xff
 172
 173static inline void pqi_scsi_done(struct scsi_cmnd *scmd)
 174{
 175        pqi_prep_for_scsi_done(scmd);
 176        scmd->scsi_done(scmd);
 177}
 178
 179static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2)
 180{
 181        return memcmp(scsi3addr1, scsi3addr2, 8) == 0;
 182}
 183
 184static inline struct pqi_ctrl_info *shost_to_hba(struct Scsi_Host *shost)
 185{
 186        void *hostdata = shost_priv(shost);
 187
 188        return *((struct pqi_ctrl_info **)hostdata);
 189}
 190
 191static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device)
 192{
 193        return !device->is_physical_device;
 194}
 195
 196static inline bool pqi_is_external_raid_addr(u8 *scsi3addr)
 197{
 198        return scsi3addr[2] != 0;
 199}
 200
 201static inline bool pqi_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
 202{
 203        return !ctrl_info->controller_online;
 204}
 205
 206static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info)
 207{
 208        if (ctrl_info->controller_online)
 209                if (!sis_is_firmware_running(ctrl_info))
 210                        pqi_take_ctrl_offline(ctrl_info);
 211}
 212
 213static inline bool pqi_is_hba_lunid(u8 *scsi3addr)
 214{
 215        return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID);
 216}
 217
 218static inline enum pqi_ctrl_mode pqi_get_ctrl_mode(
 219        struct pqi_ctrl_info *ctrl_info)
 220{
 221        return sis_read_driver_scratch(ctrl_info);
 222}
 223
 224static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info,
 225        enum pqi_ctrl_mode mode)
 226{
 227        sis_write_driver_scratch(ctrl_info, mode);
 228}
 229
 230static inline void pqi_ctrl_block_requests(struct pqi_ctrl_info *ctrl_info)
 231{
 232        ctrl_info->block_requests = true;
 233        scsi_block_requests(ctrl_info->scsi_host);
 234}
 235
 236static inline void pqi_ctrl_unblock_requests(struct pqi_ctrl_info *ctrl_info)
 237{
 238        ctrl_info->block_requests = false;
 239        wake_up_all(&ctrl_info->block_requests_wait);
 240        pqi_retry_raid_bypass_requests(ctrl_info);
 241        scsi_unblock_requests(ctrl_info->scsi_host);
 242}
 243
 244static inline bool pqi_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
 245{
 246        return ctrl_info->block_requests;
 247}
 248
 249static unsigned long pqi_wait_if_ctrl_blocked(struct pqi_ctrl_info *ctrl_info,
 250        unsigned long timeout_msecs)
 251{
 252        unsigned long remaining_msecs;
 253
 254        if (!pqi_ctrl_blocked(ctrl_info))
 255                return timeout_msecs;
 256
 257        atomic_inc(&ctrl_info->num_blocked_threads);
 258
 259        if (timeout_msecs == NO_TIMEOUT) {
 260                wait_event(ctrl_info->block_requests_wait,
 261                        !pqi_ctrl_blocked(ctrl_info));
 262                remaining_msecs = timeout_msecs;
 263        } else {
 264                unsigned long remaining_jiffies;
 265
 266                remaining_jiffies =
 267                        wait_event_timeout(ctrl_info->block_requests_wait,
 268                                !pqi_ctrl_blocked(ctrl_info),
 269                                msecs_to_jiffies(timeout_msecs));
 270                remaining_msecs = jiffies_to_msecs(remaining_jiffies);
 271        }
 272
 273        atomic_dec(&ctrl_info->num_blocked_threads);
 274
 275        return remaining_msecs;
 276}
 277
 278static inline void pqi_ctrl_busy(struct pqi_ctrl_info *ctrl_info)
 279{
 280        atomic_inc(&ctrl_info->num_busy_threads);
 281}
 282
 283static inline void pqi_ctrl_unbusy(struct pqi_ctrl_info *ctrl_info)
 284{
 285        atomic_dec(&ctrl_info->num_busy_threads);
 286}
 287
 288static inline void pqi_ctrl_wait_until_quiesced(struct pqi_ctrl_info *ctrl_info)
 289{
 290        while (atomic_read(&ctrl_info->num_busy_threads) >
 291                atomic_read(&ctrl_info->num_blocked_threads))
 292                usleep_range(1000, 2000);
 293}
 294
 295static inline bool pqi_device_offline(struct pqi_scsi_dev *device)
 296{
 297        return device->device_offline;
 298}
 299
 300static inline void pqi_device_reset_start(struct pqi_scsi_dev *device)
 301{
 302        device->in_reset = true;
 303}
 304
 305static inline void pqi_device_reset_done(struct pqi_scsi_dev *device)
 306{
 307        device->in_reset = false;
 308}
 309
 310static inline bool pqi_device_in_reset(struct pqi_scsi_dev *device)
 311{
 312        return device->in_reset;
 313}
 314
 315static inline void pqi_schedule_rescan_worker_with_delay(
 316        struct pqi_ctrl_info *ctrl_info, unsigned long delay)
 317{
 318        if (pqi_ctrl_offline(ctrl_info))
 319                return;
 320
 321        schedule_delayed_work(&ctrl_info->rescan_work, delay);
 322}
 323
 324static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info)
 325{
 326        pqi_schedule_rescan_worker_with_delay(ctrl_info, 0);
 327}
 328
 329#define PQI_RESCAN_WORK_DELAY  (10 * HZ)
 330
 331static inline void pqi_schedule_rescan_worker_delayed(
 332        struct pqi_ctrl_info *ctrl_info)
 333{
 334        pqi_schedule_rescan_worker_with_delay(ctrl_info, PQI_RESCAN_WORK_DELAY);
 335}
 336
 337static inline void pqi_cancel_rescan_worker(struct pqi_ctrl_info *ctrl_info)
 338{
 339        cancel_delayed_work_sync(&ctrl_info->rescan_work);
 340}
 341
 342static inline u32 pqi_read_heartbeat_counter(struct pqi_ctrl_info *ctrl_info)
 343{
 344        if (!ctrl_info->heartbeat_counter)
 345                return 0;
 346
 347        return readl(ctrl_info->heartbeat_counter);
 348}
 349
 350static int pqi_map_single(struct pci_dev *pci_dev,
 351        struct pqi_sg_descriptor *sg_descriptor, void *buffer,
 352        size_t buffer_length, int data_direction)
 353{
 354        dma_addr_t bus_address;
 355
 356        if (!buffer || buffer_length == 0 || data_direction == PCI_DMA_NONE)
 357                return 0;
 358
 359        bus_address = pci_map_single(pci_dev, buffer, buffer_length,
 360                data_direction);
 361        if (pci_dma_mapping_error(pci_dev, bus_address))
 362                return -ENOMEM;
 363
 364        put_unaligned_le64((u64)bus_address, &sg_descriptor->address);
 365        put_unaligned_le32(buffer_length, &sg_descriptor->length);
 366        put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
 367
 368        return 0;
 369}
 370
 371static void pqi_pci_unmap(struct pci_dev *pci_dev,
 372        struct pqi_sg_descriptor *descriptors, int num_descriptors,
 373        int data_direction)
 374{
 375        int i;
 376
 377        if (data_direction == PCI_DMA_NONE)
 378                return;
 379
 380        for (i = 0; i < num_descriptors; i++)
 381                pci_unmap_single(pci_dev,
 382                        (dma_addr_t)get_unaligned_le64(&descriptors[i].address),
 383                        get_unaligned_le32(&descriptors[i].length),
 384                        data_direction);
 385}
 386
 387static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info,
 388        struct pqi_raid_path_request *request, u8 cmd,
 389        u8 *scsi3addr, void *buffer, size_t buffer_length,
 390        u16 vpd_page, int *pci_direction)
 391{
 392        u8 *cdb;
 393        int pci_dir;
 394
 395        memset(request, 0, sizeof(*request));
 396
 397        request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
 398        put_unaligned_le16(offsetof(struct pqi_raid_path_request,
 399                sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH,
 400                &request->header.iu_length);
 401        put_unaligned_le32(buffer_length, &request->buffer_length);
 402        memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
 403        request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
 404        request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
 405
 406        cdb = request->cdb;
 407
 408        switch (cmd) {
 409        case INQUIRY:
 410                request->data_direction = SOP_READ_FLAG;
 411                cdb[0] = INQUIRY;
 412                if (vpd_page & VPD_PAGE) {
 413                        cdb[1] = 0x1;
 414                        cdb[2] = (u8)vpd_page;
 415                }
 416                cdb[4] = (u8)buffer_length;
 417                break;
 418        case CISS_REPORT_LOG:
 419        case CISS_REPORT_PHYS:
 420                request->data_direction = SOP_READ_FLAG;
 421                cdb[0] = cmd;
 422                if (cmd == CISS_REPORT_PHYS)
 423                        cdb[1] = CISS_REPORT_PHYS_EXTENDED;
 424                else
 425                        cdb[1] = CISS_REPORT_LOG_EXTENDED;
 426                put_unaligned_be32(buffer_length, &cdb[6]);
 427                break;
 428        case CISS_GET_RAID_MAP:
 429                request->data_direction = SOP_READ_FLAG;
 430                cdb[0] = CISS_READ;
 431                cdb[1] = CISS_GET_RAID_MAP;
 432                put_unaligned_be32(buffer_length, &cdb[6]);
 433                break;
 434        case SA_FLUSH_CACHE:
 435                request->data_direction = SOP_WRITE_FLAG;
 436                cdb[0] = BMIC_WRITE;
 437                cdb[6] = BMIC_FLUSH_CACHE;
 438                put_unaligned_be16(buffer_length, &cdb[7]);
 439                break;
 440        case BMIC_IDENTIFY_CONTROLLER:
 441        case BMIC_IDENTIFY_PHYSICAL_DEVICE:
 442                request->data_direction = SOP_READ_FLAG;
 443                cdb[0] = BMIC_READ;
 444                cdb[6] = cmd;
 445                put_unaligned_be16(buffer_length, &cdb[7]);
 446                break;
 447        case BMIC_WRITE_HOST_WELLNESS:
 448                request->data_direction = SOP_WRITE_FLAG;
 449                cdb[0] = BMIC_WRITE;
 450                cdb[6] = cmd;
 451                put_unaligned_be16(buffer_length, &cdb[7]);
 452                break;
 453        default:
 454                dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n",
 455                        cmd);
 456                break;
 457        }
 458
 459        switch (request->data_direction) {
 460        case SOP_READ_FLAG:
 461                pci_dir = PCI_DMA_FROMDEVICE;
 462                break;
 463        case SOP_WRITE_FLAG:
 464                pci_dir = PCI_DMA_TODEVICE;
 465                break;
 466        case SOP_NO_DIRECTION_FLAG:
 467                pci_dir = PCI_DMA_NONE;
 468                break;
 469        default:
 470                pci_dir = PCI_DMA_BIDIRECTIONAL;
 471                break;
 472        }
 473
 474        *pci_direction = pci_dir;
 475
 476        return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0],
 477                buffer, buffer_length, pci_dir);
 478}
 479
 480static inline void pqi_reinit_io_request(struct pqi_io_request *io_request)
 481{
 482        io_request->scmd = NULL;
 483        io_request->status = 0;
 484        io_request->error_info = NULL;
 485        io_request->raid_bypass = false;
 486}
 487
 488static struct pqi_io_request *pqi_alloc_io_request(
 489        struct pqi_ctrl_info *ctrl_info)
 490{
 491        struct pqi_io_request *io_request;
 492        u16 i = ctrl_info->next_io_request_slot;        /* benignly racy */
 493
 494        while (1) {
 495                io_request = &ctrl_info->io_request_pool[i];
 496                if (atomic_inc_return(&io_request->refcount) == 1)
 497                        break;
 498                atomic_dec(&io_request->refcount);
 499                i = (i + 1) % ctrl_info->max_io_slots;
 500        }
 501
 502        /* benignly racy */
 503        ctrl_info->next_io_request_slot = (i + 1) % ctrl_info->max_io_slots;
 504
 505        pqi_reinit_io_request(io_request);
 506
 507        return io_request;
 508}
 509
 510static void pqi_free_io_request(struct pqi_io_request *io_request)
 511{
 512        atomic_dec(&io_request->refcount);
 513}
 514
 515static int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info,
 516        struct bmic_identify_controller *buffer)
 517{
 518        int rc;
 519        int pci_direction;
 520        struct pqi_raid_path_request request;
 521
 522        rc = pqi_build_raid_path_request(ctrl_info, &request,
 523                BMIC_IDENTIFY_CONTROLLER, RAID_CTLR_LUNID, buffer,
 524                sizeof(*buffer), 0, &pci_direction);
 525        if (rc)
 526                return rc;
 527
 528        rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
 529                NULL, NO_TIMEOUT);
 530
 531        pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
 532                pci_direction);
 533
 534        return rc;
 535}
 536
 537static int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info,
 538        u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length)
 539{
 540        int rc;
 541        int pci_direction;
 542        struct pqi_raid_path_request request;
 543
 544        rc = pqi_build_raid_path_request(ctrl_info, &request,
 545                INQUIRY, scsi3addr, buffer, buffer_length, vpd_page,
 546                &pci_direction);
 547        if (rc)
 548                return rc;
 549
 550        rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
 551                NULL, NO_TIMEOUT);
 552
 553        pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
 554                pci_direction);
 555
 556        return rc;
 557}
 558
 559static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info,
 560        struct pqi_scsi_dev *device,
 561        struct bmic_identify_physical_device *buffer,
 562        size_t buffer_length)
 563{
 564        int rc;
 565        int pci_direction;
 566        u16 bmic_device_index;
 567        struct pqi_raid_path_request request;
 568
 569        rc = pqi_build_raid_path_request(ctrl_info, &request,
 570                BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer,
 571                buffer_length, 0, &pci_direction);
 572        if (rc)
 573                return rc;
 574
 575        bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr);
 576        request.cdb[2] = (u8)bmic_device_index;
 577        request.cdb[9] = (u8)(bmic_device_index >> 8);
 578
 579        rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
 580                0, NULL, NO_TIMEOUT);
 581
 582        pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
 583                pci_direction);
 584
 585        return rc;
 586}
 587
 588static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info,
 589        enum bmic_flush_cache_shutdown_event shutdown_event)
 590{
 591        int rc;
 592        struct pqi_raid_path_request request;
 593        int pci_direction;
 594        struct bmic_flush_cache *flush_cache;
 595
 596        /*
 597         * Don't bother trying to flush the cache if the controller is
 598         * locked up.
 599         */
 600        if (pqi_ctrl_offline(ctrl_info))
 601                return -ENXIO;
 602
 603        flush_cache = kzalloc(sizeof(*flush_cache), GFP_KERNEL);
 604        if (!flush_cache)
 605                return -ENOMEM;
 606
 607        flush_cache->shutdown_event = shutdown_event;
 608
 609        rc = pqi_build_raid_path_request(ctrl_info, &request,
 610                SA_FLUSH_CACHE, RAID_CTLR_LUNID, flush_cache,
 611                sizeof(*flush_cache), 0, &pci_direction);
 612        if (rc)
 613                goto out;
 614
 615        rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
 616                0, NULL, NO_TIMEOUT);
 617
 618        pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
 619                pci_direction);
 620
 621out:
 622        kfree(flush_cache);
 623
 624        return rc;
 625}
 626
 627static int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
 628        void *buffer, size_t buffer_length)
 629{
 630        int rc;
 631        struct pqi_raid_path_request request;
 632        int pci_direction;
 633
 634        rc = pqi_build_raid_path_request(ctrl_info, &request,
 635                BMIC_WRITE_HOST_WELLNESS, RAID_CTLR_LUNID, buffer,
 636                buffer_length, 0, &pci_direction);
 637        if (rc)
 638                return rc;
 639
 640        rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
 641                0, NULL, NO_TIMEOUT);
 642
 643        pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
 644                pci_direction);
 645
 646        return rc;
 647}
 648
 649#pragma pack(1)
 650
 651struct bmic_host_wellness_driver_version {
 652        u8      start_tag[4];
 653        u8      driver_version_tag[2];
 654        __le16  driver_version_length;
 655        char    driver_version[32];
 656        u8      end_tag[2];
 657};
 658
 659#pragma pack()
 660
 661static int pqi_write_driver_version_to_host_wellness(
 662        struct pqi_ctrl_info *ctrl_info)
 663{
 664        int rc;
 665        struct bmic_host_wellness_driver_version *buffer;
 666        size_t buffer_length;
 667
 668        buffer_length = sizeof(*buffer);
 669
 670        buffer = kmalloc(buffer_length, GFP_KERNEL);
 671        if (!buffer)
 672                return -ENOMEM;
 673
 674        buffer->start_tag[0] = '<';
 675        buffer->start_tag[1] = 'H';
 676        buffer->start_tag[2] = 'W';
 677        buffer->start_tag[3] = '>';
 678        buffer->driver_version_tag[0] = 'D';
 679        buffer->driver_version_tag[1] = 'V';
 680        put_unaligned_le16(sizeof(buffer->driver_version),
 681                &buffer->driver_version_length);
 682        strncpy(buffer->driver_version, "Linux " DRIVER_VERSION,
 683                sizeof(buffer->driver_version) - 1);
 684        buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0';
 685        buffer->end_tag[0] = 'Z';
 686        buffer->end_tag[1] = 'Z';
 687
 688        rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
 689
 690        kfree(buffer);
 691
 692        return rc;
 693}
 694
 695#pragma pack(1)
 696
 697struct bmic_host_wellness_time {
 698        u8      start_tag[4];
 699        u8      time_tag[2];
 700        __le16  time_length;
 701        u8      time[8];
 702        u8      dont_write_tag[2];
 703        u8      end_tag[2];
 704};
 705
 706#pragma pack()
 707
 708static int pqi_write_current_time_to_host_wellness(
 709        struct pqi_ctrl_info *ctrl_info)
 710{
 711        int rc;
 712        struct bmic_host_wellness_time *buffer;
 713        size_t buffer_length;
 714        time64_t local_time;
 715        unsigned int year;
 716        struct tm tm;
 717
 718        buffer_length = sizeof(*buffer);
 719
 720        buffer = kmalloc(buffer_length, GFP_KERNEL);
 721        if (!buffer)
 722                return -ENOMEM;
 723
 724        buffer->start_tag[0] = '<';
 725        buffer->start_tag[1] = 'H';
 726        buffer->start_tag[2] = 'W';
 727        buffer->start_tag[3] = '>';
 728        buffer->time_tag[0] = 'T';
 729        buffer->time_tag[1] = 'D';
 730        put_unaligned_le16(sizeof(buffer->time),
 731                &buffer->time_length);
 732
 733        local_time = ktime_get_real_seconds();
 734        time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
 735        year = tm.tm_year + 1900;
 736
 737        buffer->time[0] = bin2bcd(tm.tm_hour);
 738        buffer->time[1] = bin2bcd(tm.tm_min);
 739        buffer->time[2] = bin2bcd(tm.tm_sec);
 740        buffer->time[3] = 0;
 741        buffer->time[4] = bin2bcd(tm.tm_mon + 1);
 742        buffer->time[5] = bin2bcd(tm.tm_mday);
 743        buffer->time[6] = bin2bcd(year / 100);
 744        buffer->time[7] = bin2bcd(year % 100);
 745
 746        buffer->dont_write_tag[0] = 'D';
 747        buffer->dont_write_tag[1] = 'W';
 748        buffer->end_tag[0] = 'Z';
 749        buffer->end_tag[1] = 'Z';
 750
 751        rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
 752
 753        kfree(buffer);
 754
 755        return rc;
 756}
 757
 758#define PQI_UPDATE_TIME_WORK_INTERVAL   (24UL * 60 * 60 * HZ)
 759
 760static void pqi_update_time_worker(struct work_struct *work)
 761{
 762        int rc;
 763        struct pqi_ctrl_info *ctrl_info;
 764
 765        ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
 766                update_time_work);
 767
 768        if (pqi_ctrl_offline(ctrl_info))
 769                return;
 770
 771        rc = pqi_write_current_time_to_host_wellness(ctrl_info);
 772        if (rc)
 773                dev_warn(&ctrl_info->pci_dev->dev,
 774                        "error updating time on controller\n");
 775
 776        schedule_delayed_work(&ctrl_info->update_time_work,
 777                PQI_UPDATE_TIME_WORK_INTERVAL);
 778}
 779
 780static inline void pqi_schedule_update_time_worker(
 781        struct pqi_ctrl_info *ctrl_info)
 782{
 783        schedule_delayed_work(&ctrl_info->update_time_work, 0);
 784}
 785
 786static inline void pqi_cancel_update_time_worker(
 787        struct pqi_ctrl_info *ctrl_info)
 788{
 789        cancel_delayed_work_sync(&ctrl_info->update_time_work);
 790}
 791
 792static int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
 793        void *buffer, size_t buffer_length)
 794{
 795        int rc;
 796        int pci_direction;
 797        struct pqi_raid_path_request request;
 798
 799        rc = pqi_build_raid_path_request(ctrl_info, &request,
 800                cmd, RAID_CTLR_LUNID, buffer, buffer_length, 0, &pci_direction);
 801        if (rc)
 802                return rc;
 803
 804        rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
 805                NULL, NO_TIMEOUT);
 806
 807        pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
 808                pci_direction);
 809
 810        return rc;
 811}
 812
 813static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
 814        void **buffer)
 815{
 816        int rc;
 817        size_t lun_list_length;
 818        size_t lun_data_length;
 819        size_t new_lun_list_length;
 820        void *lun_data = NULL;
 821        struct report_lun_header *report_lun_header;
 822
 823        report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
 824        if (!report_lun_header) {
 825                rc = -ENOMEM;
 826                goto out;
 827        }
 828
 829        rc = pqi_report_luns(ctrl_info, cmd, report_lun_header,
 830                sizeof(*report_lun_header));
 831        if (rc)
 832                goto out;
 833
 834        lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
 835
 836again:
 837        lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
 838
 839        lun_data = kmalloc(lun_data_length, GFP_KERNEL);
 840        if (!lun_data) {
 841                rc = -ENOMEM;
 842                goto out;
 843        }
 844
 845        if (lun_list_length == 0) {
 846                memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
 847                goto out;
 848        }
 849
 850        rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
 851        if (rc)
 852                goto out;
 853
 854        new_lun_list_length = get_unaligned_be32(
 855                &((struct report_lun_header *)lun_data)->list_length);
 856
 857        if (new_lun_list_length > lun_list_length) {
 858                lun_list_length = new_lun_list_length;
 859                kfree(lun_data);
 860                goto again;
 861        }
 862
 863out:
 864        kfree(report_lun_header);
 865
 866        if (rc) {
 867                kfree(lun_data);
 868                lun_data = NULL;
 869        }
 870
 871        *buffer = lun_data;
 872
 873        return rc;
 874}
 875
 876static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info,
 877        void **buffer)
 878{
 879        return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS,
 880                buffer);
 881}
 882
 883static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info,
 884        void **buffer)
 885{
 886        return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
 887}
 888
 889static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
 890        struct report_phys_lun_extended **physdev_list,
 891        struct report_log_lun_extended **logdev_list)
 892{
 893        int rc;
 894        size_t logdev_list_length;
 895        size_t logdev_data_length;
 896        struct report_log_lun_extended *internal_logdev_list;
 897        struct report_log_lun_extended *logdev_data;
 898        struct report_lun_header report_lun_header;
 899
 900        rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
 901        if (rc)
 902                dev_err(&ctrl_info->pci_dev->dev,
 903                        "report physical LUNs failed\n");
 904
 905        rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
 906        if (rc)
 907                dev_err(&ctrl_info->pci_dev->dev,
 908                        "report logical LUNs failed\n");
 909
 910        /*
 911         * Tack the controller itself onto the end of the logical device list.
 912         */
 913
 914        logdev_data = *logdev_list;
 915
 916        if (logdev_data) {
 917                logdev_list_length =
 918                        get_unaligned_be32(&logdev_data->header.list_length);
 919        } else {
 920                memset(&report_lun_header, 0, sizeof(report_lun_header));
 921                logdev_data =
 922                        (struct report_log_lun_extended *)&report_lun_header;
 923                logdev_list_length = 0;
 924        }
 925
 926        logdev_data_length = sizeof(struct report_lun_header) +
 927                logdev_list_length;
 928
 929        internal_logdev_list = kmalloc(logdev_data_length +
 930                sizeof(struct report_log_lun_extended), GFP_KERNEL);
 931        if (!internal_logdev_list) {
 932                kfree(*logdev_list);
 933                *logdev_list = NULL;
 934                return -ENOMEM;
 935        }
 936
 937        memcpy(internal_logdev_list, logdev_data, logdev_data_length);
 938        memset((u8 *)internal_logdev_list + logdev_data_length, 0,
 939                sizeof(struct report_log_lun_extended_entry));
 940        put_unaligned_be32(logdev_list_length +
 941                sizeof(struct report_log_lun_extended_entry),
 942                &internal_logdev_list->header.list_length);
 943
 944        kfree(*logdev_list);
 945        *logdev_list = internal_logdev_list;
 946
 947        return 0;
 948}
 949
 950static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
 951        int bus, int target, int lun)
 952{
 953        device->bus = bus;
 954        device->target = target;
 955        device->lun = lun;
 956}
 957
 958static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
 959{
 960        u8 *scsi3addr;
 961        u32 lunid;
 962        int bus;
 963        int target;
 964        int lun;
 965
 966        scsi3addr = device->scsi3addr;
 967        lunid = get_unaligned_le32(scsi3addr);
 968
 969        if (pqi_is_hba_lunid(scsi3addr)) {
 970                /* The specified device is the controller. */
 971                pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
 972                device->target_lun_valid = true;
 973                return;
 974        }
 975
 976        if (pqi_is_logical_device(device)) {
 977                if (device->is_external_raid_device) {
 978                        bus = PQI_EXTERNAL_RAID_VOLUME_BUS;
 979                        target = (lunid >> 16) & 0x3fff;
 980                        lun = lunid & 0xff;
 981                } else {
 982                        bus = PQI_RAID_VOLUME_BUS;
 983                        target = 0;
 984                        lun = lunid & 0x3fff;
 985                }
 986                pqi_set_bus_target_lun(device, bus, target, lun);
 987                device->target_lun_valid = true;
 988                return;
 989        }
 990
 991        /*
 992         * Defer target and LUN assignment for non-controller physical devices
 993         * because the SAS transport layer will make these assignments later.
 994         */
 995        pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
 996}
 997
 998static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
 999        struct pqi_scsi_dev *device)
1000{
1001        int rc;
1002        u8 raid_level;
1003        u8 *buffer;
1004
1005        raid_level = SA_RAID_UNKNOWN;
1006
1007        buffer = kmalloc(64, GFP_KERNEL);
1008        if (buffer) {
1009                rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1010                        VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
1011                if (rc == 0) {
1012                        raid_level = buffer[8];
1013                        if (raid_level > SA_RAID_MAX)
1014                                raid_level = SA_RAID_UNKNOWN;
1015                }
1016                kfree(buffer);
1017        }
1018
1019        device->raid_level = raid_level;
1020}
1021
1022static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
1023        struct pqi_scsi_dev *device, struct raid_map *raid_map)
1024{
1025        char *err_msg;
1026        u32 raid_map_size;
1027        u32 r5or6_blocks_per_row;
1028        unsigned int num_phys_disks;
1029        unsigned int num_raid_map_entries;
1030
1031        raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1032
1033        if (raid_map_size < offsetof(struct raid_map, disk_data)) {
1034                err_msg = "RAID map too small";
1035                goto bad_raid_map;
1036        }
1037
1038        if (raid_map_size > sizeof(*raid_map)) {
1039                err_msg = "RAID map too large";
1040                goto bad_raid_map;
1041        }
1042
1043        num_phys_disks = get_unaligned_le16(&raid_map->layout_map_count) *
1044                (get_unaligned_le16(&raid_map->data_disks_per_row) +
1045                get_unaligned_le16(&raid_map->metadata_disks_per_row));
1046        num_raid_map_entries = num_phys_disks *
1047                get_unaligned_le16(&raid_map->row_cnt);
1048
1049        if (num_raid_map_entries > RAID_MAP_MAX_ENTRIES) {
1050                err_msg = "invalid number of map entries in RAID map";
1051                goto bad_raid_map;
1052        }
1053
1054        if (device->raid_level == SA_RAID_1) {
1055                if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
1056                        err_msg = "invalid RAID-1 map";
1057                        goto bad_raid_map;
1058                }
1059        } else if (device->raid_level == SA_RAID_ADM) {
1060                if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
1061                        err_msg = "invalid RAID-1(ADM) map";
1062                        goto bad_raid_map;
1063                }
1064        } else if ((device->raid_level == SA_RAID_5 ||
1065                device->raid_level == SA_RAID_6) &&
1066                get_unaligned_le16(&raid_map->layout_map_count) > 1) {
1067                /* RAID 50/60 */
1068                r5or6_blocks_per_row =
1069                        get_unaligned_le16(&raid_map->strip_size) *
1070                        get_unaligned_le16(&raid_map->data_disks_per_row);
1071                if (r5or6_blocks_per_row == 0) {
1072                        err_msg = "invalid RAID-5 or RAID-6 map";
1073                        goto bad_raid_map;
1074                }
1075        }
1076
1077        return 0;
1078
1079bad_raid_map:
1080        dev_warn(&ctrl_info->pci_dev->dev,
1081                "scsi %d:%d:%d:%d %s\n",
1082                ctrl_info->scsi_host->host_no,
1083                device->bus, device->target, device->lun, err_msg);
1084
1085        return -EINVAL;
1086}
1087
1088static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
1089        struct pqi_scsi_dev *device)
1090{
1091        int rc;
1092        int pci_direction;
1093        struct pqi_raid_path_request request;
1094        struct raid_map *raid_map;
1095
1096        raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
1097        if (!raid_map)
1098                return -ENOMEM;
1099
1100        rc = pqi_build_raid_path_request(ctrl_info, &request,
1101                CISS_GET_RAID_MAP, device->scsi3addr, raid_map,
1102                sizeof(*raid_map), 0, &pci_direction);
1103        if (rc)
1104                goto error;
1105
1106        rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
1107                NULL, NO_TIMEOUT);
1108
1109        pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
1110                pci_direction);
1111
1112        if (rc)
1113                goto error;
1114
1115        rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
1116        if (rc)
1117                goto error;
1118
1119        device->raid_map = raid_map;
1120
1121        return 0;
1122
1123error:
1124        kfree(raid_map);
1125
1126        return rc;
1127}
1128
1129static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info,
1130        struct pqi_scsi_dev *device)
1131{
1132        int rc;
1133        u8 *buffer;
1134        u8 bypass_status;
1135
1136        buffer = kmalloc(64, GFP_KERNEL);
1137        if (!buffer)
1138                return;
1139
1140        rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1141                VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64);
1142        if (rc)
1143                goto out;
1144
1145#define RAID_BYPASS_STATUS      4
1146#define RAID_BYPASS_CONFIGURED  0x1
1147#define RAID_BYPASS_ENABLED     0x2
1148
1149        bypass_status = buffer[RAID_BYPASS_STATUS];
1150        device->raid_bypass_configured =
1151                (bypass_status & RAID_BYPASS_CONFIGURED) != 0;
1152        if (device->raid_bypass_configured &&
1153                (bypass_status & RAID_BYPASS_ENABLED) &&
1154                pqi_get_raid_map(ctrl_info, device) == 0)
1155                device->raid_bypass_enabled = true;
1156
1157out:
1158        kfree(buffer);
1159}
1160
1161/*
1162 * Use vendor-specific VPD to determine online/offline status of a volume.
1163 */
1164
1165static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
1166        struct pqi_scsi_dev *device)
1167{
1168        int rc;
1169        size_t page_length;
1170        u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
1171        bool volume_offline = true;
1172        u32 volume_flags;
1173        struct ciss_vpd_logical_volume_status *vpd;
1174
1175        vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
1176        if (!vpd)
1177                goto no_buffer;
1178
1179        rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1180                VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
1181        if (rc)
1182                goto out;
1183
1184        page_length = offsetof(struct ciss_vpd_logical_volume_status,
1185                volume_status) + vpd->page_length;
1186        if (page_length < sizeof(*vpd))
1187                goto out;
1188
1189        volume_status = vpd->volume_status;
1190        volume_flags = get_unaligned_be32(&vpd->flags);
1191        volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1192
1193out:
1194        kfree(vpd);
1195no_buffer:
1196        device->volume_status = volume_status;
1197        device->volume_offline = volume_offline;
1198}
1199
1200static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1201        struct pqi_scsi_dev *device)
1202{
1203        int rc;
1204        u8 *buffer;
1205
1206        buffer = kmalloc(64, GFP_KERNEL);
1207        if (!buffer)
1208                return -ENOMEM;
1209
1210        /* Send an inquiry to the device to see what it is. */
1211        rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0, buffer, 64);
1212        if (rc)
1213                goto out;
1214
1215        scsi_sanitize_inquiry_string(&buffer[8], 8);
1216        scsi_sanitize_inquiry_string(&buffer[16], 16);
1217
1218        device->devtype = buffer[0] & 0x1f;
1219        memcpy(device->vendor, &buffer[8], sizeof(device->vendor));
1220        memcpy(device->model, &buffer[16], sizeof(device->model));
1221
1222        if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK) {
1223                if (device->is_external_raid_device) {
1224                        device->raid_level = SA_RAID_UNKNOWN;
1225                        device->volume_status = CISS_LV_OK;
1226                        device->volume_offline = false;
1227                } else {
1228                        pqi_get_raid_level(ctrl_info, device);
1229                        pqi_get_raid_bypass_status(ctrl_info, device);
1230                        pqi_get_volume_status(ctrl_info, device);
1231                }
1232        }
1233
1234out:
1235        kfree(buffer);
1236
1237        return rc;
1238}
1239
1240static void pqi_get_physical_disk_info(struct pqi_ctrl_info *ctrl_info,
1241        struct pqi_scsi_dev *device,
1242        struct bmic_identify_physical_device *id_phys)
1243{
1244        int rc;
1245
1246        memset(id_phys, 0, sizeof(*id_phys));
1247
1248        rc = pqi_identify_physical_device(ctrl_info, device,
1249                id_phys, sizeof(*id_phys));
1250        if (rc) {
1251                device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1252                return;
1253        }
1254
1255        device->queue_depth =
1256                get_unaligned_le16(&id_phys->current_queue_depth_limit);
1257        device->device_type = id_phys->device_type;
1258        device->active_path_index = id_phys->active_path_number;
1259        device->path_map = id_phys->redundant_path_present_map;
1260        memcpy(&device->box,
1261                &id_phys->alternate_paths_phys_box_on_port,
1262                sizeof(device->box));
1263        memcpy(&device->phys_connector,
1264                &id_phys->alternate_paths_phys_connector,
1265                sizeof(device->phys_connector));
1266        device->bay = id_phys->phys_bay_in_box;
1267}
1268
1269static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1270        struct pqi_scsi_dev *device)
1271{
1272        char *status;
1273        static const char unknown_state_str[] =
1274                "Volume is in an unknown state (%u)";
1275        char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1276
1277        switch (device->volume_status) {
1278        case CISS_LV_OK:
1279                status = "Volume online";
1280                break;
1281        case CISS_LV_FAILED:
1282                status = "Volume failed";
1283                break;
1284        case CISS_LV_NOT_CONFIGURED:
1285                status = "Volume not configured";
1286                break;
1287        case CISS_LV_DEGRADED:
1288                status = "Volume degraded";
1289                break;
1290        case CISS_LV_READY_FOR_RECOVERY:
1291                status = "Volume ready for recovery operation";
1292                break;
1293        case CISS_LV_UNDERGOING_RECOVERY:
1294                status = "Volume undergoing recovery";
1295                break;
1296        case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1297                status = "Wrong physical drive was replaced";
1298                break;
1299        case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1300                status = "A physical drive not properly connected";
1301                break;
1302        case CISS_LV_HARDWARE_OVERHEATING:
1303                status = "Hardware is overheating";
1304                break;
1305        case CISS_LV_HARDWARE_HAS_OVERHEATED:
1306                status = "Hardware has overheated";
1307                break;
1308        case CISS_LV_UNDERGOING_EXPANSION:
1309                status = "Volume undergoing expansion";
1310                break;
1311        case CISS_LV_NOT_AVAILABLE:
1312                status = "Volume waiting for transforming volume";
1313                break;
1314        case CISS_LV_QUEUED_FOR_EXPANSION:
1315                status = "Volume queued for expansion";
1316                break;
1317        case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1318                status = "Volume disabled due to SCSI ID conflict";
1319                break;
1320        case CISS_LV_EJECTED:
1321                status = "Volume has been ejected";
1322                break;
1323        case CISS_LV_UNDERGOING_ERASE:
1324                status = "Volume undergoing background erase";
1325                break;
1326        case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1327                status = "Volume ready for predictive spare rebuild";
1328                break;
1329        case CISS_LV_UNDERGOING_RPI:
1330                status = "Volume undergoing rapid parity initialization";
1331                break;
1332        case CISS_LV_PENDING_RPI:
1333                status = "Volume queued for rapid parity initialization";
1334                break;
1335        case CISS_LV_ENCRYPTED_NO_KEY:
1336                status = "Encrypted volume inaccessible - key not present";
1337                break;
1338        case CISS_LV_UNDERGOING_ENCRYPTION:
1339                status = "Volume undergoing encryption process";
1340                break;
1341        case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1342                status = "Volume undergoing encryption re-keying process";
1343                break;
1344        case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1345                status = "Volume encrypted but encryption is disabled";
1346                break;
1347        case CISS_LV_PENDING_ENCRYPTION:
1348                status = "Volume pending migration to encrypted state";
1349                break;
1350        case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1351                status = "Volume pending encryption rekeying";
1352                break;
1353        case CISS_LV_NOT_SUPPORTED:
1354                status = "Volume not supported on this controller";
1355                break;
1356        case CISS_LV_STATUS_UNAVAILABLE:
1357                status = "Volume status not available";
1358                break;
1359        default:
1360                snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1361                        unknown_state_str, device->volume_status);
1362                status = unknown_state_buffer;
1363                break;
1364        }
1365
1366        dev_info(&ctrl_info->pci_dev->dev,
1367                "scsi %d:%d:%d:%d %s\n",
1368                ctrl_info->scsi_host->host_no,
1369                device->bus, device->target, device->lun, status);
1370}
1371
1372static void pqi_rescan_worker(struct work_struct *work)
1373{
1374        struct pqi_ctrl_info *ctrl_info;
1375
1376        ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1377                rescan_work);
1378
1379        pqi_scan_scsi_devices(ctrl_info);
1380}
1381
1382static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1383        struct pqi_scsi_dev *device)
1384{
1385        int rc;
1386
1387        if (pqi_is_logical_device(device))
1388                rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1389                        device->target, device->lun);
1390        else
1391                rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1392
1393        return rc;
1394}
1395
1396static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info,
1397        struct pqi_scsi_dev *device)
1398{
1399        if (pqi_is_logical_device(device))
1400                scsi_remove_device(device->sdev);
1401        else
1402                pqi_remove_sas_device(device);
1403}
1404
1405/* Assumes the SCSI device list lock is held. */
1406
1407static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1408        int bus, int target, int lun)
1409{
1410        struct pqi_scsi_dev *device;
1411
1412        list_for_each_entry(device, &ctrl_info->scsi_device_list,
1413                scsi_device_list_entry)
1414                if (device->bus == bus && device->target == target &&
1415                        device->lun == lun)
1416                        return device;
1417
1418        return NULL;
1419}
1420
1421static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1,
1422        struct pqi_scsi_dev *dev2)
1423{
1424        if (dev1->is_physical_device != dev2->is_physical_device)
1425                return false;
1426
1427        if (dev1->is_physical_device)
1428                return dev1->wwid == dev2->wwid;
1429
1430        return memcmp(dev1->volume_id, dev2->volume_id,
1431                sizeof(dev1->volume_id)) == 0;
1432}
1433
1434enum pqi_find_result {
1435        DEVICE_NOT_FOUND,
1436        DEVICE_CHANGED,
1437        DEVICE_SAME,
1438};
1439
1440static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1441        struct pqi_scsi_dev *device_to_find,
1442        struct pqi_scsi_dev **matching_device)
1443{
1444        struct pqi_scsi_dev *device;
1445
1446        list_for_each_entry(device, &ctrl_info->scsi_device_list,
1447                scsi_device_list_entry) {
1448                if (pqi_scsi3addr_equal(device_to_find->scsi3addr,
1449                        device->scsi3addr)) {
1450                        *matching_device = device;
1451                        if (pqi_device_equal(device_to_find, device)) {
1452                                if (device_to_find->volume_offline)
1453                                        return DEVICE_CHANGED;
1454                                return DEVICE_SAME;
1455                        }
1456                        return DEVICE_CHANGED;
1457                }
1458        }
1459
1460        return DEVICE_NOT_FOUND;
1461}
1462
1463#define PQI_DEV_INFO_BUFFER_LENGTH      128
1464
1465static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1466        char *action, struct pqi_scsi_dev *device)
1467{
1468        ssize_t count;
1469        char buffer[PQI_DEV_INFO_BUFFER_LENGTH];
1470
1471        count = snprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH,
1472                "%d:%d:", ctrl_info->scsi_host->host_no, device->bus);
1473
1474        if (device->target_lun_valid)
1475                count += snprintf(buffer + count,
1476                        PQI_DEV_INFO_BUFFER_LENGTH - count,
1477                        "%d:%d",
1478                        device->target,
1479                        device->lun);
1480        else
1481                count += snprintf(buffer + count,
1482                        PQI_DEV_INFO_BUFFER_LENGTH - count,
1483                        "-:-");
1484
1485        if (pqi_is_logical_device(device))
1486                count += snprintf(buffer + count,
1487                        PQI_DEV_INFO_BUFFER_LENGTH - count,
1488                        " %08x%08x",
1489                        *((u32 *)&device->scsi3addr),
1490                        *((u32 *)&device->scsi3addr[4]));
1491        else
1492                count += snprintf(buffer + count,
1493                        PQI_DEV_INFO_BUFFER_LENGTH - count,
1494                        " %016llx", device->sas_address);
1495
1496        count += snprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count,
1497                " %s %.8s %.16s ",
1498                scsi_device_type(device->devtype),
1499                device->vendor,
1500                device->model);
1501
1502        if (pqi_is_logical_device(device)) {
1503                if (device->devtype == TYPE_DISK)
1504                        count += snprintf(buffer + count,
1505                                PQI_DEV_INFO_BUFFER_LENGTH - count,
1506                                "SSDSmartPathCap%c En%c %-12s",
1507                                device->raid_bypass_configured ? '+' : '-',
1508                                device->raid_bypass_enabled ? '+' : '-',
1509                                pqi_raid_level_to_string(device->raid_level));
1510        } else {
1511                count += snprintf(buffer + count,
1512                        PQI_DEV_INFO_BUFFER_LENGTH - count,
1513                        "AIO%c", device->aio_enabled ? '+' : '-');
1514                if (device->devtype == TYPE_DISK ||
1515                        device->devtype == TYPE_ZBC)
1516                        count += snprintf(buffer + count,
1517                                PQI_DEV_INFO_BUFFER_LENGTH - count,
1518                                " qd=%-6d", device->queue_depth);
1519        }
1520
1521        dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer);
1522}
1523
1524/* Assumes the SCSI device list lock is held. */
1525
1526static void pqi_scsi_update_device(struct pqi_scsi_dev *existing_device,
1527        struct pqi_scsi_dev *new_device)
1528{
1529        existing_device->devtype = new_device->devtype;
1530        existing_device->device_type = new_device->device_type;
1531        existing_device->bus = new_device->bus;
1532        if (new_device->target_lun_valid) {
1533                existing_device->target = new_device->target;
1534                existing_device->lun = new_device->lun;
1535                existing_device->target_lun_valid = true;
1536        }
1537
1538        /* By definition, the scsi3addr and wwid fields are already the same. */
1539
1540        existing_device->is_physical_device = new_device->is_physical_device;
1541        existing_device->is_external_raid_device =
1542                new_device->is_external_raid_device;
1543        existing_device->aio_enabled = new_device->aio_enabled;
1544        memcpy(existing_device->vendor, new_device->vendor,
1545                sizeof(existing_device->vendor));
1546        memcpy(existing_device->model, new_device->model,
1547                sizeof(existing_device->model));
1548        existing_device->sas_address = new_device->sas_address;
1549        existing_device->raid_level = new_device->raid_level;
1550        existing_device->queue_depth = new_device->queue_depth;
1551        existing_device->aio_handle = new_device->aio_handle;
1552        existing_device->volume_status = new_device->volume_status;
1553        existing_device->active_path_index = new_device->active_path_index;
1554        existing_device->path_map = new_device->path_map;
1555        existing_device->bay = new_device->bay;
1556        memcpy(existing_device->box, new_device->box,
1557                sizeof(existing_device->box));
1558        memcpy(existing_device->phys_connector, new_device->phys_connector,
1559                sizeof(existing_device->phys_connector));
1560        existing_device->offload_to_mirror = 0;
1561        kfree(existing_device->raid_map);
1562        existing_device->raid_map = new_device->raid_map;
1563        existing_device->raid_bypass_configured =
1564                new_device->raid_bypass_configured;
1565        existing_device->raid_bypass_enabled =
1566                new_device->raid_bypass_enabled;
1567
1568        /* To prevent this from being freed later. */
1569        new_device->raid_map = NULL;
1570}
1571
1572static inline void pqi_free_device(struct pqi_scsi_dev *device)
1573{
1574        if (device) {
1575                kfree(device->raid_map);
1576                kfree(device);
1577        }
1578}
1579
1580/*
1581 * Called when exposing a new device to the OS fails in order to re-adjust
1582 * our internal SCSI device list to match the SCSI ML's view.
1583 */
1584
1585static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
1586        struct pqi_scsi_dev *device)
1587{
1588        unsigned long flags;
1589
1590        spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1591        list_del(&device->scsi_device_list_entry);
1592        spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1593
1594        /* Allow the device structure to be freed later. */
1595        device->keep_device = false;
1596}
1597
1598static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
1599        struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
1600{
1601        int rc;
1602        unsigned int i;
1603        unsigned long flags;
1604        enum pqi_find_result find_result;
1605        struct pqi_scsi_dev *device;
1606        struct pqi_scsi_dev *next;
1607        struct pqi_scsi_dev *matching_device;
1608        LIST_HEAD(add_list);
1609        LIST_HEAD(delete_list);
1610
1611        /*
1612         * The idea here is to do as little work as possible while holding the
1613         * spinlock.  That's why we go to great pains to defer anything other
1614         * than updating the internal device list until after we release the
1615         * spinlock.
1616         */
1617
1618        spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1619
1620        /* Assume that all devices in the existing list have gone away. */
1621        list_for_each_entry(device, &ctrl_info->scsi_device_list,
1622                scsi_device_list_entry)
1623                device->device_gone = true;
1624
1625        for (i = 0; i < num_new_devices; i++) {
1626                device = new_device_list[i];
1627
1628                find_result = pqi_scsi_find_entry(ctrl_info, device,
1629                                                &matching_device);
1630
1631                switch (find_result) {
1632                case DEVICE_SAME:
1633                        /*
1634                         * The newly found device is already in the existing
1635                         * device list.
1636                         */
1637                        device->new_device = false;
1638                        matching_device->device_gone = false;
1639                        pqi_scsi_update_device(matching_device, device);
1640                        break;
1641                case DEVICE_NOT_FOUND:
1642                        /*
1643                         * The newly found device is NOT in the existing device
1644                         * list.
1645                         */
1646                        device->new_device = true;
1647                        break;
1648                case DEVICE_CHANGED:
1649                        /*
1650                         * The original device has gone away and we need to add
1651                         * the new device.
1652                         */
1653                        device->new_device = true;
1654                        break;
1655                }
1656        }
1657
1658        /* Process all devices that have gone away. */
1659        list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
1660                scsi_device_list_entry) {
1661                if (device->device_gone) {
1662                        list_del(&device->scsi_device_list_entry);
1663                        list_add_tail(&device->delete_list_entry, &delete_list);
1664                }
1665        }
1666
1667        /* Process all new devices. */
1668        for (i = 0; i < num_new_devices; i++) {
1669                device = new_device_list[i];
1670                if (!device->new_device)
1671                        continue;
1672                if (device->volume_offline)
1673                        continue;
1674                list_add_tail(&device->scsi_device_list_entry,
1675                        &ctrl_info->scsi_device_list);
1676                list_add_tail(&device->add_list_entry, &add_list);
1677                /* To prevent this device structure from being freed later. */
1678                device->keep_device = true;
1679        }
1680
1681        spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1682
1683        /* Remove all devices that have gone away. */
1684        list_for_each_entry_safe(device, next, &delete_list,
1685                delete_list_entry) {
1686                if (device->volume_offline) {
1687                        pqi_dev_info(ctrl_info, "offline", device);
1688                        pqi_show_volume_status(ctrl_info, device);
1689                } else {
1690                        pqi_dev_info(ctrl_info, "removed", device);
1691                }
1692                if (device->sdev)
1693                        pqi_remove_device(ctrl_info, device);
1694                list_del(&device->delete_list_entry);
1695                pqi_free_device(device);
1696        }
1697
1698        /*
1699         * Notify the SCSI ML if the queue depth of any existing device has
1700         * changed.
1701         */
1702        list_for_each_entry(device, &ctrl_info->scsi_device_list,
1703                scsi_device_list_entry) {
1704                if (device->sdev && device->queue_depth !=
1705                        device->advertised_queue_depth) {
1706                        device->advertised_queue_depth = device->queue_depth;
1707                        scsi_change_queue_depth(device->sdev,
1708                                device->advertised_queue_depth);
1709                }
1710        }
1711
1712        /* Expose any new devices. */
1713        list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
1714                if (!device->sdev) {
1715                        pqi_dev_info(ctrl_info, "added", device);
1716                        rc = pqi_add_device(ctrl_info, device);
1717                        if (rc) {
1718                                dev_warn(&ctrl_info->pci_dev->dev,
1719                                        "scsi %d:%d:%d:%d addition failed, device not added\n",
1720                                        ctrl_info->scsi_host->host_no,
1721                                        device->bus, device->target,
1722                                        device->lun);
1723                                pqi_fixup_botched_add(ctrl_info, device);
1724                        }
1725                }
1726        }
1727}
1728
1729static bool pqi_is_supported_device(struct pqi_scsi_dev *device)
1730{
1731        bool is_supported = false;
1732
1733        switch (device->devtype) {
1734        case TYPE_DISK:
1735        case TYPE_ZBC:
1736        case TYPE_TAPE:
1737        case TYPE_MEDIUM_CHANGER:
1738        case TYPE_ENCLOSURE:
1739                is_supported = true;
1740                break;
1741        case TYPE_RAID:
1742                /*
1743                 * Only support the HBA controller itself as a RAID
1744                 * controller.  If it's a RAID controller other than
1745                 * the HBA itself (an external RAID controller, for
1746                 * example), we don't support it.
1747                 */
1748                if (pqi_is_hba_lunid(device->scsi3addr))
1749                        is_supported = true;
1750                break;
1751        }
1752
1753        return is_supported;
1754}
1755
1756static inline bool pqi_skip_device(u8 *scsi3addr)
1757{
1758        /* Ignore all masked devices. */
1759        if (MASKED_DEVICE(scsi3addr))
1760                return true;
1761
1762        return false;
1763}
1764
1765static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1766{
1767        int i;
1768        int rc;
1769        LIST_HEAD(new_device_list_head);
1770        struct report_phys_lun_extended *physdev_list = NULL;
1771        struct report_log_lun_extended *logdev_list = NULL;
1772        struct report_phys_lun_extended_entry *phys_lun_ext_entry;
1773        struct report_log_lun_extended_entry *log_lun_ext_entry;
1774        struct bmic_identify_physical_device *id_phys = NULL;
1775        u32 num_physicals;
1776        u32 num_logicals;
1777        struct pqi_scsi_dev **new_device_list = NULL;
1778        struct pqi_scsi_dev *device;
1779        struct pqi_scsi_dev *next;
1780        unsigned int num_new_devices;
1781        unsigned int num_valid_devices;
1782        bool is_physical_device;
1783        u8 *scsi3addr;
1784        static char *out_of_memory_msg =
1785                "failed to allocate memory, device discovery stopped";
1786
1787        rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
1788        if (rc)
1789                goto out;
1790
1791        if (physdev_list)
1792                num_physicals =
1793                        get_unaligned_be32(&physdev_list->header.list_length)
1794                                / sizeof(physdev_list->lun_entries[0]);
1795        else
1796                num_physicals = 0;
1797
1798        if (logdev_list)
1799                num_logicals =
1800                        get_unaligned_be32(&logdev_list->header.list_length)
1801                                / sizeof(logdev_list->lun_entries[0]);
1802        else
1803                num_logicals = 0;
1804
1805        if (num_physicals) {
1806                /*
1807                 * We need this buffer for calls to pqi_get_physical_disk_info()
1808                 * below.  We allocate it here instead of inside
1809                 * pqi_get_physical_disk_info() because it's a fairly large
1810                 * buffer.
1811                 */
1812                id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
1813                if (!id_phys) {
1814                        dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1815                                out_of_memory_msg);
1816                        rc = -ENOMEM;
1817                        goto out;
1818                }
1819        }
1820
1821        num_new_devices = num_physicals + num_logicals;
1822
1823        new_device_list = kmalloc(sizeof(*new_device_list) *
1824                num_new_devices, GFP_KERNEL);
1825        if (!new_device_list) {
1826                dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
1827                rc = -ENOMEM;
1828                goto out;
1829        }
1830
1831        for (i = 0; i < num_new_devices; i++) {
1832                device = kzalloc(sizeof(*device), GFP_KERNEL);
1833                if (!device) {
1834                        dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1835                                out_of_memory_msg);
1836                        rc = -ENOMEM;
1837                        goto out;
1838                }
1839                list_add_tail(&device->new_device_list_entry,
1840                        &new_device_list_head);
1841        }
1842
1843        device = NULL;
1844        num_valid_devices = 0;
1845
1846        for (i = 0; i < num_new_devices; i++) {
1847
1848                if (i < num_physicals) {
1849                        is_physical_device = true;
1850                        phys_lun_ext_entry = &physdev_list->lun_entries[i];
1851                        log_lun_ext_entry = NULL;
1852                        scsi3addr = phys_lun_ext_entry->lunid;
1853                } else {
1854                        is_physical_device = false;
1855                        phys_lun_ext_entry = NULL;
1856                        log_lun_ext_entry =
1857                                &logdev_list->lun_entries[i - num_physicals];
1858                        scsi3addr = log_lun_ext_entry->lunid;
1859                }
1860
1861                if (is_physical_device && pqi_skip_device(scsi3addr))
1862                        continue;
1863
1864                if (device)
1865                        device = list_next_entry(device, new_device_list_entry);
1866                else
1867                        device = list_first_entry(&new_device_list_head,
1868                                struct pqi_scsi_dev, new_device_list_entry);
1869
1870                memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1871                device->is_physical_device = is_physical_device;
1872                if (!is_physical_device)
1873                        device->is_external_raid_device =
1874                                pqi_is_external_raid_addr(scsi3addr);
1875
1876                /* Gather information about the device. */
1877                rc = pqi_get_device_info(ctrl_info, device);
1878                if (rc == -ENOMEM) {
1879                        dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1880                                out_of_memory_msg);
1881                        goto out;
1882                }
1883                if (rc) {
1884                        if (device->is_physical_device)
1885                                dev_warn(&ctrl_info->pci_dev->dev,
1886                                        "obtaining device info failed, skipping physical device %016llx\n",
1887                                        get_unaligned_be64(
1888                                                &phys_lun_ext_entry->wwid));
1889                        else
1890                                dev_warn(&ctrl_info->pci_dev->dev,
1891                                        "obtaining device info failed, skipping logical device %08x%08x\n",
1892                                        *((u32 *)&device->scsi3addr),
1893                                        *((u32 *)&device->scsi3addr[4]));
1894                        rc = 0;
1895                        continue;
1896                }
1897
1898                if (!pqi_is_supported_device(device))
1899                        continue;
1900
1901                pqi_assign_bus_target_lun(device);
1902
1903                if (device->is_physical_device) {
1904                        device->wwid = phys_lun_ext_entry->wwid;
1905                        if ((phys_lun_ext_entry->device_flags &
1906                                REPORT_PHYS_LUN_DEV_FLAG_AIO_ENABLED) &&
1907                                phys_lun_ext_entry->aio_handle)
1908                                device->aio_enabled = true;
1909                } else {
1910                        memcpy(device->volume_id, log_lun_ext_entry->volume_id,
1911                                sizeof(device->volume_id));
1912                }
1913
1914                switch (device->devtype) {
1915                case TYPE_DISK:
1916                case TYPE_ZBC:
1917                case TYPE_ENCLOSURE:
1918                        if (device->is_physical_device) {
1919                                device->sas_address =
1920                                        get_unaligned_be64(&device->wwid);
1921                                if (device->devtype == TYPE_DISK ||
1922                                        device->devtype == TYPE_ZBC) {
1923                                        device->aio_handle =
1924                                                phys_lun_ext_entry->aio_handle;
1925                                        pqi_get_physical_disk_info(ctrl_info,
1926                                                device, id_phys);
1927                                }
1928                        }
1929                        break;
1930                }
1931
1932                new_device_list[num_valid_devices++] = device;
1933        }
1934
1935        pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
1936
1937out:
1938        list_for_each_entry_safe(device, next, &new_device_list_head,
1939                new_device_list_entry) {
1940                if (device->keep_device)
1941                        continue;
1942                list_del(&device->new_device_list_entry);
1943                pqi_free_device(device);
1944        }
1945
1946        kfree(new_device_list);
1947        kfree(physdev_list);
1948        kfree(logdev_list);
1949        kfree(id_phys);
1950
1951        return rc;
1952}
1953
1954static void pqi_remove_all_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1955{
1956        unsigned long flags;
1957        struct pqi_scsi_dev *device;
1958
1959        while (1) {
1960                spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1961
1962                device = list_first_entry_or_null(&ctrl_info->scsi_device_list,
1963                        struct pqi_scsi_dev, scsi_device_list_entry);
1964                if (device)
1965                        list_del(&device->scsi_device_list_entry);
1966
1967                spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
1968                        flags);
1969
1970                if (!device)
1971                        break;
1972
1973                if (device->sdev)
1974                        pqi_remove_device(ctrl_info, device);
1975                pqi_free_device(device);
1976        }
1977}
1978
1979static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1980{
1981        int rc;
1982
1983        if (pqi_ctrl_offline(ctrl_info))
1984                return -ENXIO;
1985
1986        mutex_lock(&ctrl_info->scan_mutex);
1987
1988        rc = pqi_update_scsi_devices(ctrl_info);
1989        if (rc)
1990                pqi_schedule_rescan_worker_delayed(ctrl_info);
1991
1992        mutex_unlock(&ctrl_info->scan_mutex);
1993
1994        return rc;
1995}
1996
1997static void pqi_scan_start(struct Scsi_Host *shost)
1998{
1999        pqi_scan_scsi_devices(shost_to_hba(shost));
2000}
2001
2002/* Returns TRUE if scan is finished. */
2003
2004static int pqi_scan_finished(struct Scsi_Host *shost,
2005        unsigned long elapsed_time)
2006{
2007        struct pqi_ctrl_info *ctrl_info;
2008
2009        ctrl_info = shost_priv(shost);
2010
2011        return !mutex_is_locked(&ctrl_info->scan_mutex);
2012}
2013
2014static void pqi_wait_until_scan_finished(struct pqi_ctrl_info *ctrl_info)
2015{
2016        mutex_lock(&ctrl_info->scan_mutex);
2017        mutex_unlock(&ctrl_info->scan_mutex);
2018}
2019
2020static void pqi_wait_until_lun_reset_finished(struct pqi_ctrl_info *ctrl_info)
2021{
2022        mutex_lock(&ctrl_info->lun_reset_mutex);
2023        mutex_unlock(&ctrl_info->lun_reset_mutex);
2024}
2025
2026static inline void pqi_set_encryption_info(
2027        struct pqi_encryption_info *encryption_info, struct raid_map *raid_map,
2028        u64 first_block)
2029{
2030        u32 volume_blk_size;
2031
2032        /*
2033         * Set the encryption tweak values based on logical block address.
2034         * If the block size is 512, the tweak value is equal to the LBA.
2035         * For other block sizes, tweak value is (LBA * block size) / 512.
2036         */
2037        volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
2038        if (volume_blk_size != 512)
2039                first_block = (first_block * volume_blk_size) / 512;
2040
2041        encryption_info->data_encryption_key_index =
2042                get_unaligned_le16(&raid_map->data_encryption_key_index);
2043        encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
2044        encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
2045}
2046
2047/*
2048 * Attempt to perform RAID bypass mapping for a logical volume I/O.
2049 */
2050
2051#define PQI_RAID_BYPASS_INELIGIBLE      1
2052
2053static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
2054        struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
2055        struct pqi_queue_group *queue_group)
2056{
2057        struct raid_map *raid_map;
2058        bool is_write = false;
2059        u32 map_index;
2060        u64 first_block;
2061        u64 last_block;
2062        u32 block_cnt;
2063        u32 blocks_per_row;
2064        u64 first_row;
2065        u64 last_row;
2066        u32 first_row_offset;
2067        u32 last_row_offset;
2068        u32 first_column;
2069        u32 last_column;
2070        u64 r0_first_row;
2071        u64 r0_last_row;
2072        u32 r5or6_blocks_per_row;
2073        u64 r5or6_first_row;
2074        u64 r5or6_last_row;
2075        u32 r5or6_first_row_offset;
2076        u32 r5or6_last_row_offset;
2077        u32 r5or6_first_column;
2078        u32 r5or6_last_column;
2079        u16 data_disks_per_row;
2080        u32 total_disks_per_row;
2081        u16 layout_map_count;
2082        u32 stripesize;
2083        u16 strip_size;
2084        u32 first_group;
2085        u32 last_group;
2086        u32 current_group;
2087        u32 map_row;
2088        u32 aio_handle;
2089        u64 disk_block;
2090        u32 disk_block_cnt;
2091        u8 cdb[16];
2092        u8 cdb_length;
2093        int offload_to_mirror;
2094        struct pqi_encryption_info *encryption_info_ptr;
2095        struct pqi_encryption_info encryption_info;
2096#if BITS_PER_LONG == 32
2097        u64 tmpdiv;
2098#endif
2099
2100        /* Check for valid opcode, get LBA and block count. */
2101        switch (scmd->cmnd[0]) {
2102        case WRITE_6:
2103                is_write = true;
2104                /* fall through */
2105        case READ_6:
2106                first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
2107                        (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
2108                block_cnt = (u32)scmd->cmnd[4];
2109                if (block_cnt == 0)
2110                        block_cnt = 256;
2111                break;
2112        case WRITE_10:
2113                is_write = true;
2114                /* fall through */
2115        case READ_10:
2116                first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2117                block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
2118                break;
2119        case WRITE_12:
2120                is_write = true;
2121                /* fall through */
2122        case READ_12:
2123                first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2124                block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
2125                break;
2126        case WRITE_16:
2127                is_write = true;
2128                /* fall through */
2129        case READ_16:
2130                first_block = get_unaligned_be64(&scmd->cmnd[2]);
2131                block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
2132                break;
2133        default:
2134                /* Process via normal I/O path. */
2135                return PQI_RAID_BYPASS_INELIGIBLE;
2136        }
2137
2138        /* Check for write to non-RAID-0. */
2139        if (is_write && device->raid_level != SA_RAID_0)
2140                return PQI_RAID_BYPASS_INELIGIBLE;
2141
2142        if (unlikely(block_cnt == 0))
2143                return PQI_RAID_BYPASS_INELIGIBLE;
2144
2145        last_block = first_block + block_cnt - 1;
2146        raid_map = device->raid_map;
2147
2148        /* Check for invalid block or wraparound. */
2149        if (last_block >= get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2150                last_block < first_block)
2151                return PQI_RAID_BYPASS_INELIGIBLE;
2152
2153        data_disks_per_row = get_unaligned_le16(&raid_map->data_disks_per_row);
2154        strip_size = get_unaligned_le16(&raid_map->strip_size);
2155        layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2156
2157        /* Calculate stripe information for the request. */
2158        blocks_per_row = data_disks_per_row * strip_size;
2159#if BITS_PER_LONG == 32
2160        tmpdiv = first_block;
2161        do_div(tmpdiv, blocks_per_row);
2162        first_row = tmpdiv;
2163        tmpdiv = last_block;
2164        do_div(tmpdiv, blocks_per_row);
2165        last_row = tmpdiv;
2166        first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2167        last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2168        tmpdiv = first_row_offset;
2169        do_div(tmpdiv, strip_size);
2170        first_column = tmpdiv;
2171        tmpdiv = last_row_offset;
2172        do_div(tmpdiv, strip_size);
2173        last_column = tmpdiv;
2174#else
2175        first_row = first_block / blocks_per_row;
2176        last_row = last_block / blocks_per_row;
2177        first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2178        last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2179        first_column = first_row_offset / strip_size;
2180        last_column = last_row_offset / strip_size;
2181#endif
2182
2183        /* If this isn't a single row/column then give to the controller. */
2184        if (first_row != last_row || first_column != last_column)
2185                return PQI_RAID_BYPASS_INELIGIBLE;
2186
2187        /* Proceeding with driver mapping. */
2188        total_disks_per_row = data_disks_per_row +
2189                get_unaligned_le16(&raid_map->metadata_disks_per_row);
2190        map_row = ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2191                get_unaligned_le16(&raid_map->row_cnt);
2192        map_index = (map_row * total_disks_per_row) + first_column;
2193
2194        /* RAID 1 */
2195        if (device->raid_level == SA_RAID_1) {
2196                if (device->offload_to_mirror)
2197                        map_index += data_disks_per_row;
2198                device->offload_to_mirror = !device->offload_to_mirror;
2199        } else if (device->raid_level == SA_RAID_ADM) {
2200                /* RAID ADM */
2201                /*
2202                 * Handles N-way mirrors  (R1-ADM) and R10 with # of drives
2203                 * divisible by 3.
2204                 */
2205                offload_to_mirror = device->offload_to_mirror;
2206                if (offload_to_mirror == 0)  {
2207                        /* use physical disk in the first mirrored group. */
2208                        map_index %= data_disks_per_row;
2209                } else {
2210                        do {
2211                                /*
2212                                 * Determine mirror group that map_index
2213                                 * indicates.
2214                                 */
2215                                current_group = map_index / data_disks_per_row;
2216
2217                                if (offload_to_mirror != current_group) {
2218                                        if (current_group <
2219                                                layout_map_count - 1) {
2220                                                /*
2221                                                 * Select raid index from
2222                                                 * next group.
2223                                                 */
2224                                                map_index += data_disks_per_row;
2225                                                current_group++;
2226                                        } else {
2227                                                /*
2228                                                 * Select raid index from first
2229                                                 * group.
2230                                                 */
2231                                                map_index %= data_disks_per_row;
2232                                                current_group = 0;
2233                                        }
2234                                }
2235                        } while (offload_to_mirror != current_group);
2236                }
2237
2238                /* Set mirror group to use next time. */
2239                offload_to_mirror =
2240                        (offload_to_mirror >= layout_map_count - 1) ?
2241                                0 : offload_to_mirror + 1;
2242                WARN_ON(offload_to_mirror >= layout_map_count);
2243                device->offload_to_mirror = offload_to_mirror;
2244                /*
2245                 * Avoid direct use of device->offload_to_mirror within this
2246                 * function since multiple threads might simultaneously
2247                 * increment it beyond the range of device->layout_map_count -1.
2248                 */
2249        } else if ((device->raid_level == SA_RAID_5 ||
2250                device->raid_level == SA_RAID_6) && layout_map_count > 1) {
2251                /* RAID 50/60 */
2252                /* Verify first and last block are in same RAID group */
2253                r5or6_blocks_per_row = strip_size * data_disks_per_row;
2254                stripesize = r5or6_blocks_per_row * layout_map_count;
2255#if BITS_PER_LONG == 32
2256                tmpdiv = first_block;
2257                first_group = do_div(tmpdiv, stripesize);
2258                tmpdiv = first_group;
2259                do_div(tmpdiv, r5or6_blocks_per_row);
2260                first_group = tmpdiv;
2261                tmpdiv = last_block;
2262                last_group = do_div(tmpdiv, stripesize);
2263                tmpdiv = last_group;
2264                do_div(tmpdiv, r5or6_blocks_per_row);
2265                last_group = tmpdiv;
2266#else
2267                first_group = (first_block % stripesize) / r5or6_blocks_per_row;
2268                last_group = (last_block % stripesize) / r5or6_blocks_per_row;
2269#endif
2270                if (first_group != last_group)
2271                        return PQI_RAID_BYPASS_INELIGIBLE;
2272
2273                /* Verify request is in a single row of RAID 5/6 */
2274#if BITS_PER_LONG == 32
2275                tmpdiv = first_block;
2276                do_div(tmpdiv, stripesize);
2277                first_row = r5or6_first_row = r0_first_row = tmpdiv;
2278                tmpdiv = last_block;
2279                do_div(tmpdiv, stripesize);
2280                r5or6_last_row = r0_last_row = tmpdiv;
2281#else
2282                first_row = r5or6_first_row = r0_first_row =
2283                        first_block / stripesize;
2284                r5or6_last_row = r0_last_row = last_block / stripesize;
2285#endif
2286                if (r5or6_first_row != r5or6_last_row)
2287                        return PQI_RAID_BYPASS_INELIGIBLE;
2288
2289                /* Verify request is in a single column */
2290#if BITS_PER_LONG == 32
2291                tmpdiv = first_block;
2292                first_row_offset = do_div(tmpdiv, stripesize);
2293                tmpdiv = first_row_offset;
2294                first_row_offset = (u32)do_div(tmpdiv, r5or6_blocks_per_row);
2295                r5or6_first_row_offset = first_row_offset;
2296                tmpdiv = last_block;
2297                r5or6_last_row_offset = do_div(tmpdiv, stripesize);
2298                tmpdiv = r5or6_last_row_offset;
2299                r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
2300                tmpdiv = r5or6_first_row_offset;
2301                do_div(tmpdiv, strip_size);
2302                first_column = r5or6_first_column = tmpdiv;
2303                tmpdiv = r5or6_last_row_offset;
2304                do_div(tmpdiv, strip_size);
2305                r5or6_last_column = tmpdiv;
2306#else
2307                first_row_offset = r5or6_first_row_offset =
2308                        (u32)((first_block % stripesize) %
2309                        r5or6_blocks_per_row);
2310
2311                r5or6_last_row_offset =
2312                        (u32)((last_block % stripesize) %
2313                        r5or6_blocks_per_row);
2314
2315                first_column = r5or6_first_row_offset / strip_size;
2316                r5or6_first_column = first_column;
2317                r5or6_last_column = r5or6_last_row_offset / strip_size;
2318#endif
2319                if (r5or6_first_column != r5or6_last_column)
2320                        return PQI_RAID_BYPASS_INELIGIBLE;
2321
2322                /* Request is eligible */
2323                map_row =
2324                        ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2325                        get_unaligned_le16(&raid_map->row_cnt);
2326
2327                map_index = (first_group *
2328                        (get_unaligned_le16(&raid_map->row_cnt) *
2329                        total_disks_per_row)) +
2330                        (map_row * total_disks_per_row) + first_column;
2331        }
2332
2333        if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
2334                return PQI_RAID_BYPASS_INELIGIBLE;
2335
2336        aio_handle = raid_map->disk_data[map_index].aio_handle;
2337        disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
2338                first_row * strip_size +
2339                (first_row_offset - first_column * strip_size);
2340        disk_block_cnt = block_cnt;
2341
2342        /* Handle differing logical/physical block sizes. */
2343        if (raid_map->phys_blk_shift) {
2344                disk_block <<= raid_map->phys_blk_shift;
2345                disk_block_cnt <<= raid_map->phys_blk_shift;
2346        }
2347
2348        if (unlikely(disk_block_cnt > 0xffff))
2349                return PQI_RAID_BYPASS_INELIGIBLE;
2350
2351        /* Build the new CDB for the physical disk I/O. */
2352        if (disk_block > 0xffffffff) {
2353                cdb[0] = is_write ? WRITE_16 : READ_16;
2354                cdb[1] = 0;
2355                put_unaligned_be64(disk_block, &cdb[2]);
2356                put_unaligned_be32(disk_block_cnt, &cdb[10]);
2357                cdb[14] = 0;
2358                cdb[15] = 0;
2359                cdb_length = 16;
2360        } else {
2361                cdb[0] = is_write ? WRITE_10 : READ_10;
2362                cdb[1] = 0;
2363                put_unaligned_be32((u32)disk_block, &cdb[2]);
2364                cdb[6] = 0;
2365                put_unaligned_be16((u16)disk_block_cnt, &cdb[7]);
2366                cdb[9] = 0;
2367                cdb_length = 10;
2368        }
2369
2370        if (get_unaligned_le16(&raid_map->flags) &
2371                RAID_MAP_ENCRYPTION_ENABLED) {
2372                pqi_set_encryption_info(&encryption_info, raid_map,
2373                        first_block);
2374                encryption_info_ptr = &encryption_info;
2375        } else {
2376                encryption_info_ptr = NULL;
2377        }
2378
2379        return pqi_aio_submit_io(ctrl_info, scmd, aio_handle,
2380                cdb, cdb_length, queue_group, encryption_info_ptr, true);
2381}
2382
2383#define PQI_STATUS_IDLE         0x0
2384
2385#define PQI_CREATE_ADMIN_QUEUE_PAIR     1
2386#define PQI_DELETE_ADMIN_QUEUE_PAIR     2
2387
2388#define PQI_DEVICE_STATE_POWER_ON_AND_RESET             0x0
2389#define PQI_DEVICE_STATE_STATUS_AVAILABLE               0x1
2390#define PQI_DEVICE_STATE_ALL_REGISTERS_READY            0x2
2391#define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY         0x3
2392#define PQI_DEVICE_STATE_ERROR                          0x4
2393
2394#define PQI_MODE_READY_TIMEOUT_SECS             30
2395#define PQI_MODE_READY_POLL_INTERVAL_MSECS      1
2396
2397static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
2398{
2399        struct pqi_device_registers __iomem *pqi_registers;
2400        unsigned long timeout;
2401        u64 signature;
2402        u8 status;
2403
2404        pqi_registers = ctrl_info->pqi_registers;
2405        timeout = (PQI_MODE_READY_TIMEOUT_SECS * HZ) + jiffies;
2406
2407        while (1) {
2408                signature = readq(&pqi_registers->signature);
2409                if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
2410                        sizeof(signature)) == 0)
2411                        break;
2412                if (time_after(jiffies, timeout)) {
2413                        dev_err(&ctrl_info->pci_dev->dev,
2414                                "timed out waiting for PQI signature\n");
2415                        return -ETIMEDOUT;
2416                }
2417                msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2418        }
2419
2420        while (1) {
2421                status = readb(&pqi_registers->function_and_status_code);
2422                if (status == PQI_STATUS_IDLE)
2423                        break;
2424                if (time_after(jiffies, timeout)) {
2425                        dev_err(&ctrl_info->pci_dev->dev,
2426                                "timed out waiting for PQI IDLE\n");
2427                        return -ETIMEDOUT;
2428                }
2429                msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2430        }
2431
2432        while (1) {
2433                if (readl(&pqi_registers->device_status) ==
2434                        PQI_DEVICE_STATE_ALL_REGISTERS_READY)
2435                        break;
2436                if (time_after(jiffies, timeout)) {
2437                        dev_err(&ctrl_info->pci_dev->dev,
2438                                "timed out waiting for PQI all registers ready\n");
2439                        return -ETIMEDOUT;
2440                }
2441                msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2442        }
2443
2444        return 0;
2445}
2446
2447static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
2448{
2449        struct pqi_scsi_dev *device;
2450
2451        device = io_request->scmd->device->hostdata;
2452        device->raid_bypass_enabled = false;
2453        device->aio_enabled = false;
2454}
2455
2456static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path)
2457{
2458        struct pqi_ctrl_info *ctrl_info;
2459        struct pqi_scsi_dev *device;
2460
2461        device = sdev->hostdata;
2462        if (device->device_offline)
2463                return;
2464
2465        device->device_offline = true;
2466        scsi_device_set_state(sdev, SDEV_OFFLINE);
2467        ctrl_info = shost_to_hba(sdev->host);
2468        pqi_schedule_rescan_worker(ctrl_info);
2469        dev_err(&ctrl_info->pci_dev->dev, "offlined %s scsi %d:%d:%d:%d\n",
2470                path, ctrl_info->scsi_host->host_no, device->bus,
2471                device->target, device->lun);
2472}
2473
2474static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
2475{
2476        u8 scsi_status;
2477        u8 host_byte;
2478        struct scsi_cmnd *scmd;
2479        struct pqi_raid_error_info *error_info;
2480        size_t sense_data_length;
2481        int residual_count;
2482        int xfer_count;
2483        struct scsi_sense_hdr sshdr;
2484
2485        scmd = io_request->scmd;
2486        if (!scmd)
2487                return;
2488
2489        error_info = io_request->error_info;
2490        scsi_status = error_info->status;
2491        host_byte = DID_OK;
2492
2493        switch (error_info->data_out_result) {
2494        case PQI_DATA_IN_OUT_GOOD:
2495                break;
2496        case PQI_DATA_IN_OUT_UNDERFLOW:
2497                xfer_count =
2498                        get_unaligned_le32(&error_info->data_out_transferred);
2499                residual_count = scsi_bufflen(scmd) - xfer_count;
2500                scsi_set_resid(scmd, residual_count);
2501                if (xfer_count < scmd->underflow)
2502                        host_byte = DID_SOFT_ERROR;
2503                break;
2504        case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
2505        case PQI_DATA_IN_OUT_ABORTED:
2506                host_byte = DID_ABORT;
2507                break;
2508        case PQI_DATA_IN_OUT_TIMEOUT:
2509                host_byte = DID_TIME_OUT;
2510                break;
2511        case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
2512        case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
2513        case PQI_DATA_IN_OUT_BUFFER_ERROR:
2514        case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
2515        case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
2516        case PQI_DATA_IN_OUT_ERROR:
2517        case PQI_DATA_IN_OUT_HARDWARE_ERROR:
2518        case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
2519        case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
2520        case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
2521        case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
2522        case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
2523        case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
2524        case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
2525        case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
2526        case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
2527        default:
2528                host_byte = DID_ERROR;
2529                break;
2530        }
2531
2532        sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
2533        if (sense_data_length == 0)
2534                sense_data_length =
2535                        get_unaligned_le16(&error_info->response_data_length);
2536        if (sense_data_length) {
2537                if (sense_data_length > sizeof(error_info->data))
2538                        sense_data_length = sizeof(error_info->data);
2539
2540                if (scsi_status == SAM_STAT_CHECK_CONDITION &&
2541                        scsi_normalize_sense(error_info->data,
2542                                sense_data_length, &sshdr) &&
2543                                sshdr.sense_key == HARDWARE_ERROR &&
2544                                sshdr.asc == 0x3e &&
2545                                sshdr.ascq == 0x1) {
2546                        pqi_take_device_offline(scmd->device, "RAID");
2547                        host_byte = DID_NO_CONNECT;
2548                }
2549
2550                if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2551                        sense_data_length = SCSI_SENSE_BUFFERSIZE;
2552                memcpy(scmd->sense_buffer, error_info->data,
2553                        sense_data_length);
2554        }
2555
2556        scmd->result = scsi_status;
2557        set_host_byte(scmd, host_byte);
2558}
2559
2560static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
2561{
2562        u8 scsi_status;
2563        u8 host_byte;
2564        struct scsi_cmnd *scmd;
2565        struct pqi_aio_error_info *error_info;
2566        size_t sense_data_length;
2567        int residual_count;
2568        int xfer_count;
2569        bool device_offline;
2570
2571        scmd = io_request->scmd;
2572        error_info = io_request->error_info;
2573        host_byte = DID_OK;
2574        sense_data_length = 0;
2575        device_offline = false;
2576
2577        switch (error_info->service_response) {
2578        case PQI_AIO_SERV_RESPONSE_COMPLETE:
2579                scsi_status = error_info->status;
2580                break;
2581        case PQI_AIO_SERV_RESPONSE_FAILURE:
2582                switch (error_info->status) {
2583                case PQI_AIO_STATUS_IO_ABORTED:
2584                        scsi_status = SAM_STAT_TASK_ABORTED;
2585                        break;
2586                case PQI_AIO_STATUS_UNDERRUN:
2587                        scsi_status = SAM_STAT_GOOD;
2588                        residual_count = get_unaligned_le32(
2589                                                &error_info->residual_count);
2590                        scsi_set_resid(scmd, residual_count);
2591                        xfer_count = scsi_bufflen(scmd) - residual_count;
2592                        if (xfer_count < scmd->underflow)
2593                                host_byte = DID_SOFT_ERROR;
2594                        break;
2595                case PQI_AIO_STATUS_OVERRUN:
2596                        scsi_status = SAM_STAT_GOOD;
2597                        break;
2598                case PQI_AIO_STATUS_AIO_PATH_DISABLED:
2599                        pqi_aio_path_disabled(io_request);
2600                        scsi_status = SAM_STAT_GOOD;
2601                        io_request->status = -EAGAIN;
2602                        break;
2603                case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
2604                case PQI_AIO_STATUS_INVALID_DEVICE:
2605                        if (!io_request->raid_bypass) {
2606                                device_offline = true;
2607                                pqi_take_device_offline(scmd->device, "AIO");
2608                                host_byte = DID_NO_CONNECT;
2609                        }
2610                        scsi_status = SAM_STAT_CHECK_CONDITION;
2611                        break;
2612                case PQI_AIO_STATUS_IO_ERROR:
2613                default:
2614                        scsi_status = SAM_STAT_CHECK_CONDITION;
2615                        break;
2616                }
2617                break;
2618        case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
2619        case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
2620                scsi_status = SAM_STAT_GOOD;
2621                break;
2622        case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
2623        case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
2624        default:
2625                scsi_status = SAM_STAT_CHECK_CONDITION;
2626                break;
2627        }
2628
2629        if (error_info->data_present) {
2630                sense_data_length =
2631                        get_unaligned_le16(&error_info->data_length);
2632                if (sense_data_length) {
2633                        if (sense_data_length > sizeof(error_info->data))
2634                                sense_data_length = sizeof(error_info->data);
2635                        if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2636                                sense_data_length = SCSI_SENSE_BUFFERSIZE;
2637                        memcpy(scmd->sense_buffer, error_info->data,
2638                                sense_data_length);
2639                }
2640        }
2641
2642        if (device_offline && sense_data_length == 0)
2643                scsi_build_sense_buffer(0, scmd->sense_buffer, HARDWARE_ERROR,
2644                        0x3e, 0x1);
2645
2646        scmd->result = scsi_status;
2647        set_host_byte(scmd, host_byte);
2648}
2649
2650static void pqi_process_io_error(unsigned int iu_type,
2651        struct pqi_io_request *io_request)
2652{
2653        switch (iu_type) {
2654        case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2655                pqi_process_raid_io_error(io_request);
2656                break;
2657        case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2658                pqi_process_aio_io_error(io_request);
2659                break;
2660        }
2661}
2662
2663static int pqi_interpret_task_management_response(
2664        struct pqi_task_management_response *response)
2665{
2666        int rc;
2667
2668        switch (response->response_code) {
2669        case SOP_TMF_COMPLETE:
2670        case SOP_TMF_FUNCTION_SUCCEEDED:
2671                rc = 0;
2672                break;
2673        default:
2674                rc = -EIO;
2675                break;
2676        }
2677
2678        return rc;
2679}
2680
2681static unsigned int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info,
2682        struct pqi_queue_group *queue_group)
2683{
2684        unsigned int num_responses;
2685        pqi_index_t oq_pi;
2686        pqi_index_t oq_ci;
2687        struct pqi_io_request *io_request;
2688        struct pqi_io_response *response;
2689        u16 request_id;
2690
2691        num_responses = 0;
2692        oq_ci = queue_group->oq_ci_copy;
2693
2694        while (1) {
2695                oq_pi = *queue_group->oq_pi;
2696                if (oq_pi == oq_ci)
2697                        break;
2698
2699                num_responses++;
2700                response = queue_group->oq_element_array +
2701                        (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
2702
2703                request_id = get_unaligned_le16(&response->request_id);
2704                WARN_ON(request_id >= ctrl_info->max_io_slots);
2705
2706                io_request = &ctrl_info->io_request_pool[request_id];
2707                WARN_ON(atomic_read(&io_request->refcount) == 0);
2708
2709                switch (response->header.iu_type) {
2710                case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
2711                case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
2712                case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
2713                        break;
2714                case PQI_RESPONSE_IU_TASK_MANAGEMENT:
2715                        io_request->status =
2716                                pqi_interpret_task_management_response(
2717                                        (void *)response);
2718                        break;
2719                case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
2720                        pqi_aio_path_disabled(io_request);
2721                        io_request->status = -EAGAIN;
2722                        break;
2723                case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2724                case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2725                        io_request->error_info = ctrl_info->error_buffer +
2726                                (get_unaligned_le16(&response->error_index) *
2727                                PQI_ERROR_BUFFER_ELEMENT_LENGTH);
2728                        pqi_process_io_error(response->header.iu_type,
2729                                io_request);
2730                        break;
2731                default:
2732                        dev_err(&ctrl_info->pci_dev->dev,
2733                                "unexpected IU type: 0x%x\n",
2734                                response->header.iu_type);
2735                        break;
2736                }
2737
2738                io_request->io_complete_callback(io_request,
2739                        io_request->context);
2740
2741                /*
2742                 * Note that the I/O request structure CANNOT BE TOUCHED after
2743                 * returning from the I/O completion callback!
2744                 */
2745
2746                oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
2747        }
2748
2749        if (num_responses) {
2750                queue_group->oq_ci_copy = oq_ci;
2751                writel(oq_ci, queue_group->oq_ci);
2752        }
2753
2754        return num_responses;
2755}
2756
2757static inline unsigned int pqi_num_elements_free(unsigned int pi,
2758        unsigned int ci, unsigned int elements_in_queue)
2759{
2760        unsigned int num_elements_used;
2761
2762        if (pi >= ci)
2763                num_elements_used = pi - ci;
2764        else
2765                num_elements_used = elements_in_queue - ci + pi;
2766
2767        return elements_in_queue - num_elements_used - 1;
2768}
2769
2770static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info,
2771        struct pqi_event_acknowledge_request *iu, size_t iu_length)
2772{
2773        pqi_index_t iq_pi;
2774        pqi_index_t iq_ci;
2775        unsigned long flags;
2776        void *next_element;
2777        struct pqi_queue_group *queue_group;
2778
2779        queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
2780        put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
2781
2782        while (1) {
2783                spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
2784
2785                iq_pi = queue_group->iq_pi_copy[RAID_PATH];
2786                iq_ci = *queue_group->iq_ci[RAID_PATH];
2787
2788                if (pqi_num_elements_free(iq_pi, iq_ci,
2789                        ctrl_info->num_elements_per_iq))
2790                        break;
2791
2792                spin_unlock_irqrestore(
2793                        &queue_group->submit_lock[RAID_PATH], flags);
2794
2795                if (pqi_ctrl_offline(ctrl_info))
2796                        return;
2797        }
2798
2799        next_element = queue_group->iq_element_array[RAID_PATH] +
2800                (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
2801
2802        memcpy(next_element, iu, iu_length);
2803
2804        iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
2805        queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
2806
2807        /*
2808         * This write notifies the controller that an IU is available to be
2809         * processed.
2810         */
2811        writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
2812
2813        spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
2814}
2815
2816static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
2817        struct pqi_event *event)
2818{
2819        struct pqi_event_acknowledge_request request;
2820
2821        memset(&request, 0, sizeof(request));
2822
2823        request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
2824        put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
2825                &request.header.iu_length);
2826        request.event_type = event->event_type;
2827        request.event_id = event->event_id;
2828        request.additional_event_id = event->additional_event_id;
2829
2830        pqi_send_event_ack(ctrl_info, &request, sizeof(request));
2831}
2832
2833static void pqi_event_worker(struct work_struct *work)
2834{
2835        unsigned int i;
2836        struct pqi_ctrl_info *ctrl_info;
2837        struct pqi_event *event;
2838
2839        ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
2840
2841        pqi_ctrl_busy(ctrl_info);
2842        pqi_wait_if_ctrl_blocked(ctrl_info, NO_TIMEOUT);
2843        if (pqi_ctrl_offline(ctrl_info))
2844                goto out;
2845
2846        pqi_schedule_rescan_worker_delayed(ctrl_info);
2847
2848        event = ctrl_info->events;
2849        for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
2850                if (event->pending) {
2851                        event->pending = false;
2852                        pqi_acknowledge_event(ctrl_info, event);
2853                }
2854                event++;
2855        }
2856
2857out:
2858        pqi_ctrl_unbusy(ctrl_info);
2859}
2860
2861#define PQI_HEARTBEAT_TIMER_INTERVAL    (10 * HZ)
2862
2863static void pqi_heartbeat_timer_handler(unsigned long data)
2864{
2865        int num_interrupts;
2866        u32 heartbeat_count;
2867        struct pqi_ctrl_info *ctrl_info = (struct pqi_ctrl_info *)data;
2868
2869        pqi_check_ctrl_health(ctrl_info);
2870        if (pqi_ctrl_offline(ctrl_info))
2871                return;
2872
2873        num_interrupts = atomic_read(&ctrl_info->num_interrupts);
2874        heartbeat_count = pqi_read_heartbeat_counter(ctrl_info);
2875
2876        if (num_interrupts == ctrl_info->previous_num_interrupts) {
2877                if (heartbeat_count == ctrl_info->previous_heartbeat_count) {
2878                        dev_err(&ctrl_info->pci_dev->dev,
2879                                "no heartbeat detected - last heartbeat count: %u\n",
2880                                heartbeat_count);
2881                        pqi_take_ctrl_offline(ctrl_info);
2882                        return;
2883                }
2884        } else {
2885                ctrl_info->previous_num_interrupts = num_interrupts;
2886        }
2887
2888        ctrl_info->previous_heartbeat_count = heartbeat_count;
2889        mod_timer(&ctrl_info->heartbeat_timer,
2890                jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
2891}
2892
2893static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
2894{
2895        if (!ctrl_info->heartbeat_counter)
2896                return;
2897
2898        ctrl_info->previous_num_interrupts =
2899                atomic_read(&ctrl_info->num_interrupts);
2900        ctrl_info->previous_heartbeat_count =
2901                pqi_read_heartbeat_counter(ctrl_info);
2902
2903        ctrl_info->heartbeat_timer.expires =
2904                jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
2905        ctrl_info->heartbeat_timer.data = (unsigned long)ctrl_info;
2906        ctrl_info->heartbeat_timer.function = pqi_heartbeat_timer_handler;
2907        add_timer(&ctrl_info->heartbeat_timer);
2908}
2909
2910static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
2911{
2912        del_timer_sync(&ctrl_info->heartbeat_timer);
2913}
2914
2915static inline int pqi_event_type_to_event_index(unsigned int event_type)
2916{
2917        int index;
2918
2919        for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++)
2920                if (event_type == pqi_supported_event_types[index])
2921                        return index;
2922
2923        return -1;
2924}
2925
2926static inline bool pqi_is_supported_event(unsigned int event_type)
2927{
2928        return pqi_event_type_to_event_index(event_type) != -1;
2929}
2930
2931static unsigned int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info)
2932{
2933        unsigned int num_events;
2934        pqi_index_t oq_pi;
2935        pqi_index_t oq_ci;
2936        struct pqi_event_queue *event_queue;
2937        struct pqi_event_response *response;
2938        struct pqi_event *event;
2939        int event_index;
2940
2941        event_queue = &ctrl_info->event_queue;
2942        num_events = 0;
2943        oq_ci = event_queue->oq_ci_copy;
2944
2945        while (1) {
2946                oq_pi = *event_queue->oq_pi;
2947                if (oq_pi == oq_ci)
2948                        break;
2949
2950                num_events++;
2951                response = event_queue->oq_element_array +
2952                        (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH);
2953
2954                event_index =
2955                        pqi_event_type_to_event_index(response->event_type);
2956
2957                if (event_index >= 0) {
2958                        if (response->request_acknowlege) {
2959                                event = &ctrl_info->events[event_index];
2960                                event->pending = true;
2961                                event->event_type = response->event_type;
2962                                event->event_id = response->event_id;
2963                                event->additional_event_id =
2964                                        response->additional_event_id;
2965                        }
2966                }
2967
2968                oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS;
2969        }
2970
2971        if (num_events) {
2972                event_queue->oq_ci_copy = oq_ci;
2973                writel(oq_ci, event_queue->oq_ci);
2974                schedule_work(&ctrl_info->event_work);
2975        }
2976
2977        return num_events;
2978}
2979
2980#define PQI_LEGACY_INTX_MASK    0x1
2981
2982static inline void pqi_configure_legacy_intx(struct pqi_ctrl_info *ctrl_info,
2983                                                bool enable_intx)
2984{
2985        u32 intx_mask;
2986        struct pqi_device_registers __iomem *pqi_registers;
2987        volatile void __iomem *register_addr;
2988
2989        pqi_registers = ctrl_info->pqi_registers;
2990
2991        if (enable_intx)
2992                register_addr = &pqi_registers->legacy_intx_mask_clear;
2993        else
2994                register_addr = &pqi_registers->legacy_intx_mask_set;
2995
2996        intx_mask = readl(register_addr);
2997        intx_mask |= PQI_LEGACY_INTX_MASK;
2998        writel(intx_mask, register_addr);
2999}
3000
3001static void pqi_change_irq_mode(struct pqi_ctrl_info *ctrl_info,
3002        enum pqi_irq_mode new_mode)
3003{
3004        switch (ctrl_info->irq_mode) {
3005        case IRQ_MODE_MSIX:
3006                switch (new_mode) {
3007                case IRQ_MODE_MSIX:
3008                        break;
3009                case IRQ_MODE_INTX:
3010                        pqi_configure_legacy_intx(ctrl_info, true);
3011                        sis_enable_intx(ctrl_info);
3012                        break;
3013                case IRQ_MODE_NONE:
3014                        break;
3015                }
3016                break;
3017        case IRQ_MODE_INTX:
3018                switch (new_mode) {
3019                case IRQ_MODE_MSIX:
3020                        pqi_configure_legacy_intx(ctrl_info, false);
3021                        sis_enable_msix(ctrl_info);
3022                        break;
3023                case IRQ_MODE_INTX:
3024                        break;
3025                case IRQ_MODE_NONE:
3026                        pqi_configure_legacy_intx(ctrl_info, false);
3027                        break;
3028                }
3029                break;
3030        case IRQ_MODE_NONE:
3031                switch (new_mode) {
3032                case IRQ_MODE_MSIX:
3033                        sis_enable_msix(ctrl_info);
3034                        break;
3035                case IRQ_MODE_INTX:
3036                        pqi_configure_legacy_intx(ctrl_info, true);
3037                        sis_enable_intx(ctrl_info);
3038                        break;
3039                case IRQ_MODE_NONE:
3040                        break;
3041                }
3042                break;
3043        }
3044
3045        ctrl_info->irq_mode = new_mode;
3046}
3047
3048#define PQI_LEGACY_INTX_PENDING         0x1
3049
3050static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info)
3051{
3052        bool valid_irq;
3053        u32 intx_status;
3054
3055        switch (ctrl_info->irq_mode) {
3056        case IRQ_MODE_MSIX:
3057                valid_irq = true;
3058                break;
3059        case IRQ_MODE_INTX:
3060                intx_status =
3061                        readl(&ctrl_info->pqi_registers->legacy_intx_status);
3062                if (intx_status & PQI_LEGACY_INTX_PENDING)
3063                        valid_irq = true;
3064                else
3065                        valid_irq = false;
3066                break;
3067        case IRQ_MODE_NONE:
3068        default:
3069                valid_irq = false;
3070                break;
3071        }
3072
3073        return valid_irq;
3074}
3075
3076static irqreturn_t pqi_irq_handler(int irq, void *data)
3077{
3078        struct pqi_ctrl_info *ctrl_info;
3079        struct pqi_queue_group *queue_group;
3080        unsigned int num_responses_handled;
3081
3082        queue_group = data;
3083        ctrl_info = queue_group->ctrl_info;
3084
3085        if (!pqi_is_valid_irq(ctrl_info))
3086                return IRQ_NONE;
3087
3088        num_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
3089
3090        if (irq == ctrl_info->event_irq)
3091                num_responses_handled += pqi_process_event_intr(ctrl_info);
3092
3093        if (num_responses_handled)
3094                atomic_inc(&ctrl_info->num_interrupts);
3095
3096        pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
3097        pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
3098
3099        return IRQ_HANDLED;
3100}
3101
3102static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
3103{
3104        struct pci_dev *pci_dev = ctrl_info->pci_dev;
3105        int i;
3106        int rc;
3107
3108        ctrl_info->event_irq = pci_irq_vector(pci_dev, 0);
3109
3110        for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
3111                rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0,
3112                        DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
3113                if (rc) {
3114                        dev_err(&pci_dev->dev,
3115                                "irq %u init failed with error %d\n",
3116                                pci_irq_vector(pci_dev, i), rc);
3117                        return rc;
3118                }
3119                ctrl_info->num_msix_vectors_initialized++;
3120        }
3121
3122        return 0;
3123}
3124
3125static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
3126{
3127        int i;
3128
3129        for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
3130                free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
3131                        &ctrl_info->queue_groups[i]);
3132
3133        ctrl_info->num_msix_vectors_initialized = 0;
3134}
3135
3136static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3137{
3138        int num_vectors_enabled;
3139
3140        num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
3141                        PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
3142                        PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
3143        if (num_vectors_enabled < 0) {
3144                dev_err(&ctrl_info->pci_dev->dev,
3145                        "MSI-X init failed with error %d\n",
3146                        num_vectors_enabled);
3147                return num_vectors_enabled;
3148        }
3149
3150        ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
3151        ctrl_info->irq_mode = IRQ_MODE_MSIX;
3152        return 0;
3153}
3154
3155static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3156{
3157        if (ctrl_info->num_msix_vectors_enabled) {
3158                pci_free_irq_vectors(ctrl_info->pci_dev);
3159                ctrl_info->num_msix_vectors_enabled = 0;
3160        }
3161}
3162
3163static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
3164{
3165        unsigned int i;
3166        size_t alloc_length;
3167        size_t element_array_length_per_iq;
3168        size_t element_array_length_per_oq;
3169        void *element_array;
3170        void *next_queue_index;
3171        void *aligned_pointer;
3172        unsigned int num_inbound_queues;
3173        unsigned int num_outbound_queues;
3174        unsigned int num_queue_indexes;
3175        struct pqi_queue_group *queue_group;
3176
3177        element_array_length_per_iq =
3178                PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
3179                ctrl_info->num_elements_per_iq;
3180        element_array_length_per_oq =
3181                PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
3182                ctrl_info->num_elements_per_oq;
3183        num_inbound_queues = ctrl_info->num_queue_groups * 2;
3184        num_outbound_queues = ctrl_info->num_queue_groups;
3185        num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
3186
3187        aligned_pointer = NULL;
3188
3189        for (i = 0; i < num_inbound_queues; i++) {
3190                aligned_pointer = PTR_ALIGN(aligned_pointer,
3191                        PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3192                aligned_pointer += element_array_length_per_iq;
3193        }
3194
3195        for (i = 0; i < num_outbound_queues; i++) {
3196                aligned_pointer = PTR_ALIGN(aligned_pointer,
3197                        PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3198                aligned_pointer += element_array_length_per_oq;
3199        }
3200
3201        aligned_pointer = PTR_ALIGN(aligned_pointer,
3202                PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3203        aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3204                PQI_EVENT_OQ_ELEMENT_LENGTH;
3205
3206        for (i = 0; i < num_queue_indexes; i++) {
3207                aligned_pointer = PTR_ALIGN(aligned_pointer,
3208                        PQI_OPERATIONAL_INDEX_ALIGNMENT);
3209                aligned_pointer += sizeof(pqi_index_t);
3210        }
3211
3212        alloc_length = (size_t)aligned_pointer +
3213                PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3214
3215        alloc_length += PQI_EXTRA_SGL_MEMORY;
3216
3217        ctrl_info->queue_memory_base =
3218                dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3219                        alloc_length,
3220                        &ctrl_info->queue_memory_base_dma_handle, GFP_KERNEL);
3221
3222        if (!ctrl_info->queue_memory_base)
3223                return -ENOMEM;
3224
3225        ctrl_info->queue_memory_length = alloc_length;
3226
3227        element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
3228                PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3229
3230        for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3231                queue_group = &ctrl_info->queue_groups[i];
3232                queue_group->iq_element_array[RAID_PATH] = element_array;
3233                queue_group->iq_element_array_bus_addr[RAID_PATH] =
3234                        ctrl_info->queue_memory_base_dma_handle +
3235                                (element_array - ctrl_info->queue_memory_base);
3236                element_array += element_array_length_per_iq;
3237                element_array = PTR_ALIGN(element_array,
3238                        PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3239                queue_group->iq_element_array[AIO_PATH] = element_array;
3240                queue_group->iq_element_array_bus_addr[AIO_PATH] =
3241                        ctrl_info->queue_memory_base_dma_handle +
3242                        (element_array - ctrl_info->queue_memory_base);
3243                element_array += element_array_length_per_iq;
3244                element_array = PTR_ALIGN(element_array,
3245                        PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3246        }
3247
3248        for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3249                queue_group = &ctrl_info->queue_groups[i];
3250                queue_group->oq_element_array = element_array;
3251                queue_group->oq_element_array_bus_addr =
3252                        ctrl_info->queue_memory_base_dma_handle +
3253                        (element_array - ctrl_info->queue_memory_base);
3254                element_array += element_array_length_per_oq;
3255                element_array = PTR_ALIGN(element_array,
3256                        PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3257        }
3258
3259        ctrl_info->event_queue.oq_element_array = element_array;
3260        ctrl_info->event_queue.oq_element_array_bus_addr =
3261                ctrl_info->queue_memory_base_dma_handle +
3262                (element_array - ctrl_info->queue_memory_base);
3263        element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3264                PQI_EVENT_OQ_ELEMENT_LENGTH;
3265
3266        next_queue_index = PTR_ALIGN(element_array,
3267                PQI_OPERATIONAL_INDEX_ALIGNMENT);
3268
3269        for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3270                queue_group = &ctrl_info->queue_groups[i];
3271                queue_group->iq_ci[RAID_PATH] = next_queue_index;
3272                queue_group->iq_ci_bus_addr[RAID_PATH] =
3273                        ctrl_info->queue_memory_base_dma_handle +
3274                        (next_queue_index - ctrl_info->queue_memory_base);
3275                next_queue_index += sizeof(pqi_index_t);
3276                next_queue_index = PTR_ALIGN(next_queue_index,
3277                        PQI_OPERATIONAL_INDEX_ALIGNMENT);
3278                queue_group->iq_ci[AIO_PATH] = next_queue_index;
3279                queue_group->iq_ci_bus_addr[AIO_PATH] =
3280                        ctrl_info->queue_memory_base_dma_handle +
3281                        (next_queue_index - ctrl_info->queue_memory_base);
3282                next_queue_index += sizeof(pqi_index_t);
3283                next_queue_index = PTR_ALIGN(next_queue_index,
3284                        PQI_OPERATIONAL_INDEX_ALIGNMENT);
3285                queue_group->oq_pi = next_queue_index;
3286                queue_group->oq_pi_bus_addr =
3287                        ctrl_info->queue_memory_base_dma_handle +
3288                        (next_queue_index - ctrl_info->queue_memory_base);
3289                next_queue_index += sizeof(pqi_index_t);
3290                next_queue_index = PTR_ALIGN(next_queue_index,
3291                        PQI_OPERATIONAL_INDEX_ALIGNMENT);
3292        }
3293
3294        ctrl_info->event_queue.oq_pi = next_queue_index;
3295        ctrl_info->event_queue.oq_pi_bus_addr =
3296                ctrl_info->queue_memory_base_dma_handle +
3297                (next_queue_index - ctrl_info->queue_memory_base);
3298
3299        return 0;
3300}
3301
3302static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
3303{
3304        unsigned int i;
3305        u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3306        u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3307
3308        /*
3309         * Initialize the backpointers to the controller structure in
3310         * each operational queue group structure.
3311         */
3312        for (i = 0; i < ctrl_info->num_queue_groups; i++)
3313                ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
3314
3315        /*
3316         * Assign IDs to all operational queues.  Note that the IDs
3317         * assigned to operational IQs are independent of the IDs
3318         * assigned to operational OQs.
3319         */
3320        ctrl_info->event_queue.oq_id = next_oq_id++;
3321        for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3322                ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
3323                ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
3324                ctrl_info->queue_groups[i].oq_id = next_oq_id++;
3325        }
3326
3327        /*
3328         * Assign MSI-X table entry indexes to all queues.  Note that the
3329         * interrupt for the event queue is shared with the first queue group.
3330         */
3331        ctrl_info->event_queue.int_msg_num = 0;
3332        for (i = 0; i < ctrl_info->num_queue_groups; i++)
3333                ctrl_info->queue_groups[i].int_msg_num = i;
3334
3335        for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3336                spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
3337                spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
3338                INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
3339                INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
3340        }
3341}
3342
3343static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
3344{
3345        size_t alloc_length;
3346        struct pqi_admin_queues_aligned *admin_queues_aligned;
3347        struct pqi_admin_queues *admin_queues;
3348
3349        alloc_length = sizeof(struct pqi_admin_queues_aligned) +
3350                PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3351
3352        ctrl_info->admin_queue_memory_base =
3353                dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3354                        alloc_length,
3355                        &ctrl_info->admin_queue_memory_base_dma_handle,
3356                        GFP_KERNEL);
3357
3358        if (!ctrl_info->admin_queue_memory_base)
3359                return -ENOMEM;
3360
3361        ctrl_info->admin_queue_memory_length = alloc_length;
3362
3363        admin_queues = &ctrl_info->admin_queues;
3364        admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
3365                PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3366        admin_queues->iq_element_array =
3367                &admin_queues_aligned->iq_element_array;
3368        admin_queues->oq_element_array =
3369                &admin_queues_aligned->oq_element_array;
3370        admin_queues->iq_ci = &admin_queues_aligned->iq_ci;
3371        admin_queues->oq_pi = &admin_queues_aligned->oq_pi;
3372
3373        admin_queues->iq_element_array_bus_addr =
3374                ctrl_info->admin_queue_memory_base_dma_handle +
3375                (admin_queues->iq_element_array -
3376                ctrl_info->admin_queue_memory_base);
3377        admin_queues->oq_element_array_bus_addr =
3378                ctrl_info->admin_queue_memory_base_dma_handle +
3379                (admin_queues->oq_element_array -
3380                ctrl_info->admin_queue_memory_base);
3381        admin_queues->iq_ci_bus_addr =
3382                ctrl_info->admin_queue_memory_base_dma_handle +
3383                ((void *)admin_queues->iq_ci -
3384                ctrl_info->admin_queue_memory_base);
3385        admin_queues->oq_pi_bus_addr =
3386                ctrl_info->admin_queue_memory_base_dma_handle +
3387                ((void *)admin_queues->oq_pi -
3388                ctrl_info->admin_queue_memory_base);
3389
3390        return 0;
3391}
3392
3393#define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES          HZ
3394#define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS      1
3395
3396static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
3397{
3398        struct pqi_device_registers __iomem *pqi_registers;
3399        struct pqi_admin_queues *admin_queues;
3400        unsigned long timeout;
3401        u8 status;
3402        u32 reg;
3403
3404        pqi_registers = ctrl_info->pqi_registers;
3405        admin_queues = &ctrl_info->admin_queues;
3406
3407        writeq((u64)admin_queues->iq_element_array_bus_addr,
3408                &pqi_registers->admin_iq_element_array_addr);
3409        writeq((u64)admin_queues->oq_element_array_bus_addr,
3410                &pqi_registers->admin_oq_element_array_addr);
3411        writeq((u64)admin_queues->iq_ci_bus_addr,
3412                &pqi_registers->admin_iq_ci_addr);
3413        writeq((u64)admin_queues->oq_pi_bus_addr,
3414                &pqi_registers->admin_oq_pi_addr);
3415
3416        reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
3417                (PQI_ADMIN_OQ_NUM_ELEMENTS) << 8 |
3418                (admin_queues->int_msg_num << 16);
3419        writel(reg, &pqi_registers->admin_iq_num_elements);
3420        writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
3421                &pqi_registers->function_and_status_code);
3422
3423        timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
3424        while (1) {
3425                status = readb(&pqi_registers->function_and_status_code);
3426                if (status == PQI_STATUS_IDLE)
3427                        break;
3428                if (time_after(jiffies, timeout))
3429                        return -ETIMEDOUT;
3430                msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
3431        }
3432
3433        /*
3434         * The offset registers are not initialized to the correct
3435         * offsets until *after* the create admin queue pair command
3436         * completes successfully.
3437         */
3438        admin_queues->iq_pi = ctrl_info->iomem_base +
3439                PQI_DEVICE_REGISTERS_OFFSET +
3440                readq(&pqi_registers->admin_iq_pi_offset);
3441        admin_queues->oq_ci = ctrl_info->iomem_base +
3442                PQI_DEVICE_REGISTERS_OFFSET +
3443                readq(&pqi_registers->admin_oq_ci_offset);
3444
3445        return 0;
3446}
3447
3448static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
3449        struct pqi_general_admin_request *request)
3450{
3451        struct pqi_admin_queues *admin_queues;
3452        void *next_element;
3453        pqi_index_t iq_pi;
3454
3455        admin_queues = &ctrl_info->admin_queues;
3456        iq_pi = admin_queues->iq_pi_copy;
3457
3458        next_element = admin_queues->iq_element_array +
3459                (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
3460
3461        memcpy(next_element, request, sizeof(*request));
3462
3463        iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
3464        admin_queues->iq_pi_copy = iq_pi;
3465
3466        /*
3467         * This write notifies the controller that an IU is available to be
3468         * processed.
3469         */
3470        writel(iq_pi, admin_queues->iq_pi);
3471}
3472
3473#define PQI_ADMIN_REQUEST_TIMEOUT_SECS  60
3474
3475static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
3476        struct pqi_general_admin_response *response)
3477{
3478        struct pqi_admin_queues *admin_queues;
3479        pqi_index_t oq_pi;
3480        pqi_index_t oq_ci;
3481        unsigned long timeout;
3482
3483        admin_queues = &ctrl_info->admin_queues;
3484        oq_ci = admin_queues->oq_ci_copy;
3485
3486        timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * HZ) + jiffies;
3487
3488        while (1) {
3489                oq_pi = *admin_queues->oq_pi;
3490                if (oq_pi != oq_ci)
3491                        break;
3492                if (time_after(jiffies, timeout)) {
3493                        dev_err(&ctrl_info->pci_dev->dev,
3494                                "timed out waiting for admin response\n");
3495                        return -ETIMEDOUT;
3496                }
3497                if (!sis_is_firmware_running(ctrl_info))
3498                        return -ENXIO;
3499                usleep_range(1000, 2000);
3500        }
3501
3502        memcpy(response, admin_queues->oq_element_array +
3503                (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
3504
3505        oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
3506        admin_queues->oq_ci_copy = oq_ci;
3507        writel(oq_ci, admin_queues->oq_ci);
3508
3509        return 0;
3510}
3511
3512static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
3513        struct pqi_queue_group *queue_group, enum pqi_io_path path,
3514        struct pqi_io_request *io_request)
3515{
3516        struct pqi_io_request *next;
3517        void *next_element;
3518        pqi_index_t iq_pi;
3519        pqi_index_t iq_ci;
3520        size_t iu_length;
3521        unsigned long flags;
3522        unsigned int num_elements_needed;
3523        unsigned int num_elements_to_end_of_queue;
3524        size_t copy_count;
3525        struct pqi_iu_header *request;
3526
3527        spin_lock_irqsave(&queue_group->submit_lock[path], flags);
3528
3529        if (io_request) {
3530                io_request->queue_group = queue_group;
3531                list_add_tail(&io_request->request_list_entry,
3532                        &queue_group->request_list[path]);
3533        }
3534
3535        iq_pi = queue_group->iq_pi_copy[path];
3536
3537        list_for_each_entry_safe(io_request, next,
3538                &queue_group->request_list[path], request_list_entry) {
3539
3540                request = io_request->iu;
3541
3542                iu_length = get_unaligned_le16(&request->iu_length) +
3543                        PQI_REQUEST_HEADER_LENGTH;
3544                num_elements_needed =
3545                        DIV_ROUND_UP(iu_length,
3546                                PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3547
3548                iq_ci = *queue_group->iq_ci[path];
3549
3550                if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
3551                        ctrl_info->num_elements_per_iq))
3552                        break;
3553
3554                put_unaligned_le16(queue_group->oq_id,
3555                        &request->response_queue_id);
3556
3557                next_element = queue_group->iq_element_array[path] +
3558                        (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3559
3560                num_elements_to_end_of_queue =
3561                        ctrl_info->num_elements_per_iq - iq_pi;
3562
3563                if (num_elements_needed <= num_elements_to_end_of_queue) {
3564                        memcpy(next_element, request, iu_length);
3565                } else {
3566                        copy_count = num_elements_to_end_of_queue *
3567                                PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
3568                        memcpy(next_element, request, copy_count);
3569                        memcpy(queue_group->iq_element_array[path],
3570                                (u8 *)request + copy_count,
3571                                iu_length - copy_count);
3572                }
3573
3574                iq_pi = (iq_pi + num_elements_needed) %
3575                        ctrl_info->num_elements_per_iq;
3576
3577                list_del(&io_request->request_list_entry);
3578        }
3579
3580        if (iq_pi != queue_group->iq_pi_copy[path]) {
3581                queue_group->iq_pi_copy[path] = iq_pi;
3582                /*
3583                 * This write notifies the controller that one or more IUs are
3584                 * available to be processed.
3585                 */
3586                writel(iq_pi, queue_group->iq_pi[path]);
3587        }
3588
3589        spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
3590}
3591
3592#define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS         10
3593
3594static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info,
3595        struct completion *wait)
3596{
3597        int rc;
3598
3599        while (1) {
3600                if (wait_for_completion_io_timeout(wait,
3601                        PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * HZ)) {
3602                        rc = 0;
3603                        break;
3604                }
3605
3606                pqi_check_ctrl_health(ctrl_info);
3607                if (pqi_ctrl_offline(ctrl_info)) {
3608                        rc = -ENXIO;
3609                        break;
3610                }
3611        }
3612
3613        return rc;
3614}
3615
3616static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
3617        void *context)
3618{
3619        struct completion *waiting = context;
3620
3621        complete(waiting);
3622}
3623
3624static int pqi_submit_raid_request_synchronous_with_io_request(
3625        struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
3626        unsigned long timeout_msecs)
3627{
3628        int rc = 0;
3629        DECLARE_COMPLETION_ONSTACK(wait);
3630
3631        io_request->io_complete_callback = pqi_raid_synchronous_complete;
3632        io_request->context = &wait;
3633
3634        pqi_start_io(ctrl_info,
3635                &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
3636                io_request);
3637
3638        if (timeout_msecs == NO_TIMEOUT) {
3639                pqi_wait_for_completion_io(ctrl_info, &wait);
3640        } else {
3641                if (!wait_for_completion_io_timeout(&wait,
3642                        msecs_to_jiffies(timeout_msecs))) {
3643                        dev_warn(&ctrl_info->pci_dev->dev,
3644                                "command timed out\n");
3645                        rc = -ETIMEDOUT;
3646                }
3647        }
3648
3649        return rc;
3650}
3651
3652static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
3653        struct pqi_iu_header *request, unsigned int flags,
3654        struct pqi_raid_error_info *error_info, unsigned long timeout_msecs)
3655{
3656        int rc;
3657        struct pqi_io_request *io_request;
3658        unsigned long start_jiffies;
3659        unsigned long msecs_blocked;
3660        size_t iu_length;
3661
3662        /*
3663         * Note that specifying PQI_SYNC_FLAGS_INTERRUPTABLE and a timeout value
3664         * are mutually exclusive.
3665         */
3666
3667        if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
3668                if (down_interruptible(&ctrl_info->sync_request_sem))
3669                        return -ERESTARTSYS;
3670        } else {
3671                if (timeout_msecs == NO_TIMEOUT) {
3672                        down(&ctrl_info->sync_request_sem);
3673                } else {
3674                        start_jiffies = jiffies;
3675                        if (down_timeout(&ctrl_info->sync_request_sem,
3676                                msecs_to_jiffies(timeout_msecs)))
3677                                return -ETIMEDOUT;
3678                        msecs_blocked =
3679                                jiffies_to_msecs(jiffies - start_jiffies);
3680                        if (msecs_blocked >= timeout_msecs)
3681                                return -ETIMEDOUT;
3682                        timeout_msecs -= msecs_blocked;
3683                }
3684        }
3685
3686        pqi_ctrl_busy(ctrl_info);
3687        timeout_msecs = pqi_wait_if_ctrl_blocked(ctrl_info, timeout_msecs);
3688        if (timeout_msecs == 0) {
3689                rc = -ETIMEDOUT;
3690                goto out;
3691        }
3692
3693        if (pqi_ctrl_offline(ctrl_info)) {
3694                rc = -ENXIO;
3695                goto out;
3696        }
3697
3698        io_request = pqi_alloc_io_request(ctrl_info);
3699
3700        put_unaligned_le16(io_request->index,
3701                &(((struct pqi_raid_path_request *)request)->request_id));
3702
3703        if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
3704                ((struct pqi_raid_path_request *)request)->error_index =
3705                        ((struct pqi_raid_path_request *)request)->request_id;
3706
3707        iu_length = get_unaligned_le16(&request->iu_length) +
3708                PQI_REQUEST_HEADER_LENGTH;
3709        memcpy(io_request->iu, request, iu_length);
3710
3711        rc = pqi_submit_raid_request_synchronous_with_io_request(ctrl_info,
3712                io_request, timeout_msecs);
3713
3714        if (error_info) {
3715                if (io_request->error_info)
3716                        memcpy(error_info, io_request->error_info,
3717                                sizeof(*error_info));
3718                else
3719                        memset(error_info, 0, sizeof(*error_info));
3720        } else if (rc == 0 && io_request->error_info) {
3721                u8 scsi_status;
3722                struct pqi_raid_error_info *raid_error_info;
3723
3724                raid_error_info = io_request->error_info;
3725                scsi_status = raid_error_info->status;
3726
3727                if (scsi_status == SAM_STAT_CHECK_CONDITION &&
3728                        raid_error_info->data_out_result ==
3729                        PQI_DATA_IN_OUT_UNDERFLOW)
3730                        scsi_status = SAM_STAT_GOOD;
3731
3732                if (scsi_status != SAM_STAT_GOOD)
3733                        rc = -EIO;
3734        }
3735
3736        pqi_free_io_request(io_request);
3737
3738out:
3739        pqi_ctrl_unbusy(ctrl_info);
3740        up(&ctrl_info->sync_request_sem);
3741
3742        return rc;
3743}
3744
3745static int pqi_validate_admin_response(
3746        struct pqi_general_admin_response *response, u8 expected_function_code)
3747{
3748        if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
3749                return -EINVAL;
3750
3751        if (get_unaligned_le16(&response->header.iu_length) !=
3752                PQI_GENERAL_ADMIN_IU_LENGTH)
3753                return -EINVAL;
3754
3755        if (response->function_code != expected_function_code)
3756                return -EINVAL;
3757
3758        if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
3759                return -EINVAL;
3760
3761        return 0;
3762}
3763
3764static int pqi_submit_admin_request_synchronous(
3765        struct pqi_ctrl_info *ctrl_info,
3766        struct pqi_general_admin_request *request,
3767        struct pqi_general_admin_response *response)
3768{
3769        int rc;
3770
3771        pqi_submit_admin_request(ctrl_info, request);
3772
3773        rc = pqi_poll_for_admin_response(ctrl_info, response);
3774
3775        if (rc == 0)
3776                rc = pqi_validate_admin_response(response,
3777                        request->function_code);
3778
3779        return rc;
3780}
3781
3782static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
3783{
3784        int rc;
3785        struct pqi_general_admin_request request;
3786        struct pqi_general_admin_response response;
3787        struct pqi_device_capability *capability;
3788        struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
3789
3790        capability = kmalloc(sizeof(*capability), GFP_KERNEL);
3791        if (!capability)
3792                return -ENOMEM;
3793
3794        memset(&request, 0, sizeof(request));
3795
3796        request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3797        put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3798                &request.header.iu_length);
3799        request.function_code =
3800                PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
3801        put_unaligned_le32(sizeof(*capability),
3802                &request.data.report_device_capability.buffer_length);
3803
3804        rc = pqi_map_single(ctrl_info->pci_dev,
3805                &request.data.report_device_capability.sg_descriptor,
3806                capability, sizeof(*capability),
3807                PCI_DMA_FROMDEVICE);
3808        if (rc)
3809                goto out;
3810
3811        rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3812                &response);
3813
3814        pqi_pci_unmap(ctrl_info->pci_dev,
3815                &request.data.report_device_capability.sg_descriptor, 1,
3816                PCI_DMA_FROMDEVICE);
3817
3818        if (rc)
3819                goto out;
3820
3821        if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
3822                rc = -EIO;
3823                goto out;
3824        }
3825
3826        ctrl_info->max_inbound_queues =
3827                get_unaligned_le16(&capability->max_inbound_queues);
3828        ctrl_info->max_elements_per_iq =
3829                get_unaligned_le16(&capability->max_elements_per_iq);
3830        ctrl_info->max_iq_element_length =
3831                get_unaligned_le16(&capability->max_iq_element_length)
3832                * 16;
3833        ctrl_info->max_outbound_queues =
3834                get_unaligned_le16(&capability->max_outbound_queues);
3835        ctrl_info->max_elements_per_oq =
3836                get_unaligned_le16(&capability->max_elements_per_oq);
3837        ctrl_info->max_oq_element_length =
3838                get_unaligned_le16(&capability->max_oq_element_length)
3839                * 16;
3840
3841        sop_iu_layer_descriptor =
3842                &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
3843
3844        ctrl_info->max_inbound_iu_length_per_firmware =
3845                get_unaligned_le16(
3846                        &sop_iu_layer_descriptor->max_inbound_iu_length);
3847        ctrl_info->inbound_spanning_supported =
3848                sop_iu_layer_descriptor->inbound_spanning_supported;
3849        ctrl_info->outbound_spanning_supported =
3850                sop_iu_layer_descriptor->outbound_spanning_supported;
3851
3852out:
3853        kfree(capability);
3854
3855        return rc;
3856}
3857
3858static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
3859{
3860        if (ctrl_info->max_iq_element_length <
3861                PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
3862                dev_err(&ctrl_info->pci_dev->dev,
3863                        "max. inbound queue element length of %d is less than the required length of %d\n",
3864                        ctrl_info->max_iq_element_length,
3865                        PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3866                return -EINVAL;
3867        }
3868
3869        if (ctrl_info->max_oq_element_length <
3870                PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
3871                dev_err(&ctrl_info->pci_dev->dev,
3872                        "max. outbound queue element length of %d is less than the required length of %d\n",
3873                        ctrl_info->max_oq_element_length,
3874                        PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
3875                return -EINVAL;
3876        }
3877
3878        if (ctrl_info->max_inbound_iu_length_per_firmware <
3879                PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
3880                dev_err(&ctrl_info->pci_dev->dev,
3881                        "max. inbound IU length of %u is less than the min. required length of %d\n",
3882                        ctrl_info->max_inbound_iu_length_per_firmware,
3883                        PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3884                return -EINVAL;
3885        }
3886
3887        if (!ctrl_info->inbound_spanning_supported) {
3888                dev_err(&ctrl_info->pci_dev->dev,
3889                        "the controller does not support inbound spanning\n");
3890                return -EINVAL;
3891        }
3892
3893        if (ctrl_info->outbound_spanning_supported) {
3894                dev_err(&ctrl_info->pci_dev->dev,
3895                        "the controller supports outbound spanning but this driver does not\n");
3896                return -EINVAL;
3897        }
3898
3899        return 0;
3900}
3901
3902static int pqi_delete_operational_queue(struct pqi_ctrl_info *ctrl_info,
3903        bool inbound_queue, u16 queue_id)
3904{
3905        struct pqi_general_admin_request request;
3906        struct pqi_general_admin_response response;
3907
3908        memset(&request, 0, sizeof(request));
3909        request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3910        put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3911                &request.header.iu_length);
3912        if (inbound_queue)
3913                request.function_code =
3914                        PQI_GENERAL_ADMIN_FUNCTION_DELETE_IQ;
3915        else
3916                request.function_code =
3917                        PQI_GENERAL_ADMIN_FUNCTION_DELETE_OQ;
3918        put_unaligned_le16(queue_id,
3919                &request.data.delete_operational_queue.queue_id);
3920
3921        return pqi_submit_admin_request_synchronous(ctrl_info, &request,
3922                &response);
3923}
3924
3925static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
3926{
3927        int rc;
3928        struct pqi_event_queue *event_queue;
3929        struct pqi_general_admin_request request;
3930        struct pqi_general_admin_response response;
3931
3932        event_queue = &ctrl_info->event_queue;
3933
3934        /*
3935         * Create OQ (Outbound Queue - device to host queue) to dedicate
3936         * to events.
3937         */
3938        memset(&request, 0, sizeof(request));
3939        request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3940        put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3941                &request.header.iu_length);
3942        request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
3943        put_unaligned_le16(event_queue->oq_id,
3944                &request.data.create_operational_oq.queue_id);
3945        put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
3946                &request.data.create_operational_oq.element_array_addr);
3947        put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
3948                &request.data.create_operational_oq.pi_addr);
3949        put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
3950                &request.data.create_operational_oq.num_elements);
3951        put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
3952                &request.data.create_operational_oq.element_length);
3953        request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
3954        put_unaligned_le16(event_queue->int_msg_num,
3955                &request.data.create_operational_oq.int_msg_num);
3956
3957        rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3958                &response);
3959        if (rc)
3960                return rc;
3961
3962        event_queue->oq_ci = ctrl_info->iomem_base +
3963                PQI_DEVICE_REGISTERS_OFFSET +
3964                get_unaligned_le64(
3965                        &response.data.create_operational_oq.oq_ci_offset);
3966
3967        return 0;
3968}
3969
3970static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info,
3971        unsigned int group_number)
3972{
3973        int rc;
3974        struct pqi_queue_group *queue_group;
3975        struct pqi_general_admin_request request;
3976        struct pqi_general_admin_response response;
3977
3978        queue_group = &ctrl_info->queue_groups[group_number];
3979
3980        /*
3981         * Create IQ (Inbound Queue - host to device queue) for
3982         * RAID path.
3983         */
3984        memset(&request, 0, sizeof(request));
3985        request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3986        put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3987                &request.header.iu_length);
3988        request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
3989        put_unaligned_le16(queue_group->iq_id[RAID_PATH],
3990                &request.data.create_operational_iq.queue_id);
3991        put_unaligned_le64(
3992                (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
3993                &request.data.create_operational_iq.element_array_addr);
3994        put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
3995                &request.data.create_operational_iq.ci_addr);
3996        put_unaligned_le16(ctrl_info->num_elements_per_iq,
3997                &request.data.create_operational_iq.num_elements);
3998        put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
3999                &request.data.create_operational_iq.element_length);
4000        request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4001
4002        rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4003                &response);
4004        if (rc) {
4005                dev_err(&ctrl_info->pci_dev->dev,
4006                        "error creating inbound RAID queue\n");
4007                return rc;
4008        }
4009
4010        queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
4011                PQI_DEVICE_REGISTERS_OFFSET +
4012                get_unaligned_le64(
4013                        &response.data.create_operational_iq.iq_pi_offset);
4014
4015        /*
4016         * Create IQ (Inbound Queue - host to device queue) for
4017         * Advanced I/O (AIO) path.
4018         */
4019        memset(&request, 0, sizeof(request));
4020        request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4021        put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4022                &request.header.iu_length);
4023        request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4024        put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4025                &request.data.create_operational_iq.queue_id);
4026        put_unaligned_le64((u64)queue_group->
4027                iq_element_array_bus_addr[AIO_PATH],
4028                &request.data.create_operational_iq.element_array_addr);
4029        put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
4030                &request.data.create_operational_iq.ci_addr);
4031        put_unaligned_le16(ctrl_info->num_elements_per_iq,
4032                &request.data.create_operational_iq.num_elements);
4033        put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4034                &request.data.create_operational_iq.element_length);
4035        request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4036
4037        rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4038                &response);
4039        if (rc) {
4040                dev_err(&ctrl_info->pci_dev->dev,
4041                        "error creating inbound AIO queue\n");
4042                goto delete_inbound_queue_raid;
4043        }
4044
4045        queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
4046                PQI_DEVICE_REGISTERS_OFFSET +
4047                get_unaligned_le64(
4048                        &response.data.create_operational_iq.iq_pi_offset);
4049
4050        /*
4051         * Designate the 2nd IQ as the AIO path.  By default, all IQs are
4052         * assumed to be for RAID path I/O unless we change the queue's
4053         * property.
4054         */
4055        memset(&request, 0, sizeof(request));
4056        request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4057        put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4058                &request.header.iu_length);
4059        request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
4060        put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4061                &request.data.change_operational_iq_properties.queue_id);
4062        put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
4063                &request.data.change_operational_iq_properties.vendor_specific);
4064
4065        rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4066                &response);
4067        if (rc) {
4068                dev_err(&ctrl_info->pci_dev->dev,
4069                        "error changing queue property\n");
4070                goto delete_inbound_queue_aio;
4071        }
4072
4073        /*
4074         * Create OQ (Outbound Queue - device to host queue).
4075         */
4076        memset(&request, 0, sizeof(request));
4077        request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4078        put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4079                &request.header.iu_length);
4080        request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4081        put_unaligned_le16(queue_group->oq_id,
4082                &request.data.create_operational_oq.queue_id);
4083        put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
4084                &request.data.create_operational_oq.element_array_addr);
4085        put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
4086                &request.data.create_operational_oq.pi_addr);
4087        put_unaligned_le16(ctrl_info->num_elements_per_oq,
4088                &request.data.create_operational_oq.num_elements);
4089        put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
4090                &request.data.create_operational_oq.element_length);
4091        request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4092        put_unaligned_le16(queue_group->int_msg_num,
4093                &request.data.create_operational_oq.int_msg_num);
4094
4095        rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4096                &response);
4097        if (rc) {
4098                dev_err(&ctrl_info->pci_dev->dev,
4099                        "error creating outbound queue\n");
4100                goto delete_inbound_queue_aio;
4101        }
4102
4103        queue_group->oq_ci = ctrl_info->iomem_base +
4104                PQI_DEVICE_REGISTERS_OFFSET +
4105                get_unaligned_le64(
4106                        &response.data.create_operational_oq.oq_ci_offset);
4107
4108        return 0;
4109
4110delete_inbound_queue_aio:
4111        pqi_delete_operational_queue(ctrl_info, true,
4112                queue_group->iq_id[AIO_PATH]);
4113
4114delete_inbound_queue_raid:
4115        pqi_delete_operational_queue(ctrl_info, true,
4116                queue_group->iq_id[RAID_PATH]);
4117
4118        return rc;
4119}
4120
4121static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
4122{
4123        int rc;
4124        unsigned int i;
4125
4126        rc = pqi_create_event_queue(ctrl_info);
4127        if (rc) {
4128                dev_err(&ctrl_info->pci_dev->dev,
4129                        "error creating event queue\n");
4130                return rc;
4131        }
4132
4133        for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4134                rc = pqi_create_queue_group(ctrl_info, i);
4135                if (rc) {
4136                        dev_err(&ctrl_info->pci_dev->dev,
4137                                "error creating queue group number %u/%u\n",
4138                                i, ctrl_info->num_queue_groups);
4139                        return rc;
4140                }
4141        }
4142
4143        return 0;
4144}
4145
4146#define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH   \
4147        (offsetof(struct pqi_event_config, descriptors) + \
4148        (PQI_MAX_EVENT_DESCRIPTORS * sizeof(struct pqi_event_descriptor)))
4149
4150static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
4151        bool enable_events)
4152{
4153        int rc;
4154        unsigned int i;
4155        struct pqi_event_config *event_config;
4156        struct pqi_event_descriptor *event_descriptor;
4157        struct pqi_general_management_request request;
4158
4159        event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4160                GFP_KERNEL);
4161        if (!event_config)
4162                return -ENOMEM;
4163
4164        memset(&request, 0, sizeof(request));
4165
4166        request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
4167        put_unaligned_le16(offsetof(struct pqi_general_management_request,
4168                data.report_event_configuration.sg_descriptors[1]) -
4169                PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4170        put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4171                &request.data.report_event_configuration.buffer_length);
4172
4173        rc = pqi_map_single(ctrl_info->pci_dev,
4174                request.data.report_event_configuration.sg_descriptors,
4175                event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4176                PCI_DMA_FROMDEVICE);
4177        if (rc)
4178                goto out;
4179
4180        rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
4181                0, NULL, NO_TIMEOUT);
4182
4183        pqi_pci_unmap(ctrl_info->pci_dev,
4184                request.data.report_event_configuration.sg_descriptors, 1,
4185                PCI_DMA_FROMDEVICE);
4186
4187        if (rc)
4188                goto out;
4189
4190        for (i = 0; i < event_config->num_event_descriptors; i++) {
4191                event_descriptor = &event_config->descriptors[i];
4192                if (enable_events &&
4193                        pqi_is_supported_event(event_descriptor->event_type))
4194                        put_unaligned_le16(ctrl_info->event_queue.oq_id,
4195                                        &event_descriptor->oq_id);
4196                else
4197                        put_unaligned_le16(0, &event_descriptor->oq_id);
4198        }
4199
4200        memset(&request, 0, sizeof(request));
4201
4202        request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
4203        put_unaligned_le16(offsetof(struct pqi_general_management_request,
4204                data.report_event_configuration.sg_descriptors[1]) -
4205                PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4206        put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4207                &request.data.report_event_configuration.buffer_length);
4208
4209        rc = pqi_map_single(ctrl_info->pci_dev,
4210                request.data.report_event_configuration.sg_descriptors,
4211                event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4212                PCI_DMA_TODEVICE);
4213        if (rc)
4214                goto out;
4215
4216        rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
4217                NULL, NO_TIMEOUT);
4218
4219        pqi_pci_unmap(ctrl_info->pci_dev,
4220                request.data.report_event_configuration.sg_descriptors, 1,
4221                PCI_DMA_TODEVICE);
4222
4223out:
4224        kfree(event_config);
4225
4226        return rc;
4227}
4228
4229static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
4230{
4231        return pqi_configure_events(ctrl_info, true);
4232}
4233
4234static inline int pqi_disable_events(struct pqi_ctrl_info *ctrl_info)
4235{
4236        return pqi_configure_events(ctrl_info, false);
4237}
4238
4239static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
4240{
4241        unsigned int i;
4242        struct device *dev;
4243        size_t sg_chain_buffer_length;
4244        struct pqi_io_request *io_request;
4245
4246        if (!ctrl_info->io_request_pool)
4247                return;
4248
4249        dev = &ctrl_info->pci_dev->dev;
4250        sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4251        io_request = ctrl_info->io_request_pool;
4252
4253        for (i = 0; i < ctrl_info->max_io_slots; i++) {
4254                kfree(io_request->iu);
4255                if (!io_request->sg_chain_buffer)
4256                        break;
4257                dma_free_coherent(dev, sg_chain_buffer_length,
4258                        io_request->sg_chain_buffer,
4259                        io_request->sg_chain_buffer_dma_handle);
4260                io_request++;
4261        }
4262
4263        kfree(ctrl_info->io_request_pool);
4264        ctrl_info->io_request_pool = NULL;
4265}
4266
4267static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
4268{
4269        ctrl_info->error_buffer = dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
4270                ctrl_info->error_buffer_length,
4271                &ctrl_info->error_buffer_dma_handle, GFP_KERNEL);
4272
4273        if (!ctrl_info->error_buffer)
4274                return -ENOMEM;
4275
4276        return 0;
4277}
4278
4279static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
4280{
4281        unsigned int i;
4282        void *sg_chain_buffer;
4283        size_t sg_chain_buffer_length;
4284        dma_addr_t sg_chain_buffer_dma_handle;
4285        struct device *dev;
4286        struct pqi_io_request *io_request;
4287
4288        ctrl_info->io_request_pool = kzalloc(ctrl_info->max_io_slots *
4289                sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
4290
4291        if (!ctrl_info->io_request_pool) {
4292                dev_err(&ctrl_info->pci_dev->dev,
4293                        "failed to allocate I/O request pool\n");
4294                goto error;
4295        }
4296
4297        dev = &ctrl_info->pci_dev->dev;
4298        sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4299        io_request = ctrl_info->io_request_pool;
4300
4301        for (i = 0; i < ctrl_info->max_io_slots; i++) {
4302                io_request->iu =
4303                        kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
4304
4305                if (!io_request->iu) {
4306                        dev_err(&ctrl_info->pci_dev->dev,
4307                                "failed to allocate IU buffers\n");
4308                        goto error;
4309                }
4310
4311                sg_chain_buffer = dma_alloc_coherent(dev,
4312                        sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
4313                        GFP_KERNEL);
4314
4315                if (!sg_chain_buffer) {
4316                        dev_err(&ctrl_info->pci_dev->dev,
4317                                "failed to allocate PQI scatter-gather chain buffers\n");
4318                        goto error;
4319                }
4320
4321                io_request->index = i;
4322                io_request->sg_chain_buffer = sg_chain_buffer;
4323                io_request->sg_chain_buffer_dma_handle =
4324                        sg_chain_buffer_dma_handle;
4325                io_request++;
4326        }
4327
4328        return 0;
4329
4330error:
4331        pqi_free_all_io_requests(ctrl_info);
4332
4333        return -ENOMEM;
4334}
4335
4336/*
4337 * Calculate required resources that are sized based on max. outstanding
4338 * requests and max. transfer size.
4339 */
4340
4341static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
4342{
4343        u32 max_transfer_size;
4344        u32 max_sg_entries;
4345
4346        ctrl_info->scsi_ml_can_queue =
4347                ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
4348        ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
4349
4350        ctrl_info->error_buffer_length =
4351                ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
4352
4353        if (reset_devices)
4354                max_transfer_size = min(ctrl_info->max_transfer_size,
4355                        PQI_MAX_TRANSFER_SIZE_KDUMP);
4356        else
4357                max_transfer_size = min(ctrl_info->max_transfer_size,
4358                        PQI_MAX_TRANSFER_SIZE);
4359
4360        max_sg_entries = max_transfer_size / PAGE_SIZE;
4361
4362        /* +1 to cover when the buffer is not page-aligned. */
4363        max_sg_entries++;
4364
4365        max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
4366
4367        max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
4368
4369        ctrl_info->sg_chain_buffer_length =
4370                (max_sg_entries * sizeof(struct pqi_sg_descriptor)) +
4371                PQI_EXTRA_SGL_MEMORY;
4372        ctrl_info->sg_tablesize = max_sg_entries;
4373        ctrl_info->max_sectors = max_transfer_size / 512;
4374}
4375
4376static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
4377{
4378        int num_queue_groups;
4379        u16 num_elements_per_iq;
4380        u16 num_elements_per_oq;
4381
4382        if (reset_devices) {
4383                num_queue_groups = 1;
4384        } else {
4385                int num_cpus;
4386                int max_queue_groups;
4387
4388                max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
4389                        ctrl_info->max_outbound_queues - 1);
4390                max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
4391
4392                num_cpus = num_online_cpus();
4393                num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
4394                num_queue_groups = min(num_queue_groups, max_queue_groups);
4395        }
4396
4397        ctrl_info->num_queue_groups = num_queue_groups;
4398        ctrl_info->max_hw_queue_index = num_queue_groups - 1;
4399
4400        /*
4401         * Make sure that the max. inbound IU length is an even multiple
4402         * of our inbound element length.
4403         */
4404        ctrl_info->max_inbound_iu_length =
4405                (ctrl_info->max_inbound_iu_length_per_firmware /
4406                PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
4407                PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
4408
4409        num_elements_per_iq =
4410                (ctrl_info->max_inbound_iu_length /
4411                PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4412
4413        /* Add one because one element in each queue is unusable. */
4414        num_elements_per_iq++;
4415
4416        num_elements_per_iq = min(num_elements_per_iq,
4417                ctrl_info->max_elements_per_iq);
4418
4419        num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
4420        num_elements_per_oq = min(num_elements_per_oq,
4421                ctrl_info->max_elements_per_oq);
4422
4423        ctrl_info->num_elements_per_iq = num_elements_per_iq;
4424        ctrl_info->num_elements_per_oq = num_elements_per_oq;
4425
4426        ctrl_info->max_sg_per_iu =
4427                ((ctrl_info->max_inbound_iu_length -
4428                PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
4429                sizeof(struct pqi_sg_descriptor)) +
4430                PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
4431}
4432
4433static inline void pqi_set_sg_descriptor(
4434        struct pqi_sg_descriptor *sg_descriptor, struct scatterlist *sg)
4435{
4436        u64 address = (u64)sg_dma_address(sg);
4437        unsigned int length = sg_dma_len(sg);
4438
4439        put_unaligned_le64(address, &sg_descriptor->address);
4440        put_unaligned_le32(length, &sg_descriptor->length);
4441        put_unaligned_le32(0, &sg_descriptor->flags);
4442}
4443
4444static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
4445        struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
4446        struct pqi_io_request *io_request)
4447{
4448        int i;
4449        u16 iu_length;
4450        int sg_count;
4451        bool chained;
4452        unsigned int num_sg_in_iu;
4453        unsigned int max_sg_per_iu;
4454        struct scatterlist *sg;
4455        struct pqi_sg_descriptor *sg_descriptor;
4456
4457        sg_count = scsi_dma_map(scmd);
4458        if (sg_count < 0)
4459                return sg_count;
4460
4461        iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
4462                PQI_REQUEST_HEADER_LENGTH;
4463
4464        if (sg_count == 0)
4465                goto out;
4466
4467        sg = scsi_sglist(scmd);
4468        sg_descriptor = request->sg_descriptors;
4469        max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4470        chained = false;
4471        num_sg_in_iu = 0;
4472        i = 0;
4473
4474        while (1) {
4475                pqi_set_sg_descriptor(sg_descriptor, sg);
4476                if (!chained)
4477                        num_sg_in_iu++;
4478                i++;
4479                if (i == sg_count)
4480                        break;
4481                sg_descriptor++;
4482                if (i == max_sg_per_iu) {
4483                        put_unaligned_le64(
4484                                (u64)io_request->sg_chain_buffer_dma_handle,
4485                                &sg_descriptor->address);
4486                        put_unaligned_le32((sg_count - num_sg_in_iu)
4487                                * sizeof(*sg_descriptor),
4488                                &sg_descriptor->length);
4489                        put_unaligned_le32(CISS_SG_CHAIN,
4490                                &sg_descriptor->flags);
4491                        chained = true;
4492                        num_sg_in_iu++;
4493                        sg_descriptor = io_request->sg_chain_buffer;
4494                }
4495                sg = sg_next(sg);
4496        }
4497
4498        put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4499        request->partial = chained;
4500        iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4501
4502out:
4503        put_unaligned_le16(iu_length, &request->header.iu_length);
4504
4505        return 0;
4506}
4507
4508static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
4509        struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
4510        struct pqi_io_request *io_request)
4511{
4512        int i;
4513        u16 iu_length;
4514        int sg_count;
4515        bool chained;
4516        unsigned int num_sg_in_iu;
4517        unsigned int max_sg_per_iu;
4518        struct scatterlist *sg;
4519        struct pqi_sg_descriptor *sg_descriptor;
4520
4521        sg_count = scsi_dma_map(scmd);
4522        if (sg_count < 0)
4523                return sg_count;
4524
4525        iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
4526                PQI_REQUEST_HEADER_LENGTH;
4527        num_sg_in_iu = 0;
4528
4529        if (sg_count == 0)
4530                goto out;
4531
4532        sg = scsi_sglist(scmd);
4533        sg_descriptor = request->sg_descriptors;
4534        max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4535        chained = false;
4536        i = 0;
4537
4538        while (1) {
4539                pqi_set_sg_descriptor(sg_descriptor, sg);
4540                if (!chained)
4541                        num_sg_in_iu++;
4542                i++;
4543                if (i == sg_count)
4544                        break;
4545                sg_descriptor++;
4546                if (i == max_sg_per_iu) {
4547                        put_unaligned_le64(
4548                                (u64)io_request->sg_chain_buffer_dma_handle,
4549                                &sg_descriptor->address);
4550                        put_unaligned_le32((sg_count - num_sg_in_iu)
4551                                * sizeof(*sg_descriptor),
4552                                &sg_descriptor->length);
4553                        put_unaligned_le32(CISS_SG_CHAIN,
4554                                &sg_descriptor->flags);
4555                        chained = true;
4556                        num_sg_in_iu++;
4557                        sg_descriptor = io_request->sg_chain_buffer;
4558                }
4559                sg = sg_next(sg);
4560        }
4561
4562        put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4563        request->partial = chained;
4564        iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4565
4566out:
4567        put_unaligned_le16(iu_length, &request->header.iu_length);
4568        request->num_sg_descriptors = num_sg_in_iu;
4569
4570        return 0;
4571}
4572
4573static void pqi_raid_io_complete(struct pqi_io_request *io_request,
4574        void *context)
4575{
4576        struct scsi_cmnd *scmd;
4577
4578        scmd = io_request->scmd;
4579        pqi_free_io_request(io_request);
4580        scsi_dma_unmap(scmd);
4581        pqi_scsi_done(scmd);
4582}
4583
4584static int pqi_raid_submit_scsi_cmd_with_io_request(
4585        struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
4586        struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4587        struct pqi_queue_group *queue_group)
4588{
4589        int rc;
4590        size_t cdb_length;
4591        struct pqi_raid_path_request *request;
4592
4593        io_request->io_complete_callback = pqi_raid_io_complete;
4594        io_request->scmd = scmd;
4595
4596        request = io_request->iu;
4597        memset(request, 0,
4598                offsetof(struct pqi_raid_path_request, sg_descriptors));
4599
4600        request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
4601        put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4602        request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4603        put_unaligned_le16(io_request->index, &request->request_id);
4604        request->error_index = request->request_id;
4605        memcpy(request->lun_number, device->scsi3addr,
4606                sizeof(request->lun_number));
4607
4608        cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
4609        memcpy(request->cdb, scmd->cmnd, cdb_length);
4610
4611        switch (cdb_length) {
4612        case 6:
4613        case 10:
4614        case 12:
4615        case 16:
4616                /* No bytes in the Additional CDB bytes field */
4617                request->additional_cdb_bytes_usage =
4618                        SOP_ADDITIONAL_CDB_BYTES_0;
4619                break;
4620        case 20:
4621                /* 4 bytes in the Additional cdb field */
4622                request->additional_cdb_bytes_usage =
4623                        SOP_ADDITIONAL_CDB_BYTES_4;
4624                break;
4625        case 24:
4626                /* 8 bytes in the Additional cdb field */
4627                request->additional_cdb_bytes_usage =
4628                        SOP_ADDITIONAL_CDB_BYTES_8;
4629                break;
4630        case 28:
4631                /* 12 bytes in the Additional cdb field */
4632                request->additional_cdb_bytes_usage =
4633                        SOP_ADDITIONAL_CDB_BYTES_12;
4634                break;
4635        case 32:
4636        default:
4637                /* 16 bytes in the Additional cdb field */
4638                request->additional_cdb_bytes_usage =
4639                        SOP_ADDITIONAL_CDB_BYTES_16;
4640                break;
4641        }
4642
4643        switch (scmd->sc_data_direction) {
4644        case DMA_TO_DEVICE:
4645                request->data_direction = SOP_READ_FLAG;
4646                break;
4647        case DMA_FROM_DEVICE:
4648                request->data_direction = SOP_WRITE_FLAG;
4649                break;
4650        case DMA_NONE:
4651                request->data_direction = SOP_NO_DIRECTION_FLAG;
4652                break;
4653        case DMA_BIDIRECTIONAL:
4654                request->data_direction = SOP_BIDIRECTIONAL;
4655                break;
4656        default:
4657                dev_err(&ctrl_info->pci_dev->dev,
4658                        "unknown data direction: %d\n",
4659                        scmd->sc_data_direction);
4660                break;
4661        }
4662
4663        rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
4664        if (rc) {
4665                pqi_free_io_request(io_request);
4666                return SCSI_MLQUEUE_HOST_BUSY;
4667        }
4668
4669        pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
4670
4671        return 0;
4672}
4673
4674static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4675        struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4676        struct pqi_queue_group *queue_group)
4677{
4678        struct pqi_io_request *io_request;
4679
4680        io_request = pqi_alloc_io_request(ctrl_info);
4681
4682        return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
4683                device, scmd, queue_group);
4684}
4685
4686static inline void pqi_schedule_bypass_retry(struct pqi_ctrl_info *ctrl_info)
4687{
4688        if (!pqi_ctrl_blocked(ctrl_info))
4689                schedule_work(&ctrl_info->raid_bypass_retry_work);
4690}
4691
4692static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request)
4693{
4694        struct scsi_cmnd *scmd;
4695        struct pqi_scsi_dev *device;
4696        struct pqi_ctrl_info *ctrl_info;
4697
4698        if (!io_request->raid_bypass)
4699                return false;
4700
4701        scmd = io_request->scmd;
4702        if ((scmd->result & 0xff) == SAM_STAT_GOOD)
4703                return false;
4704        if (host_byte(scmd->result) == DID_NO_CONNECT)
4705                return false;
4706
4707        device = scmd->device->hostdata;
4708        if (pqi_device_offline(device))
4709                return false;
4710
4711        ctrl_info = shost_to_hba(scmd->device->host);
4712        if (pqi_ctrl_offline(ctrl_info))
4713                return false;
4714
4715        return true;
4716}
4717
4718static inline void pqi_add_to_raid_bypass_retry_list(
4719        struct pqi_ctrl_info *ctrl_info,
4720        struct pqi_io_request *io_request, bool at_head)
4721{
4722        unsigned long flags;
4723
4724        spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4725        if (at_head)
4726                list_add(&io_request->request_list_entry,
4727                        &ctrl_info->raid_bypass_retry_list);
4728        else
4729                list_add_tail(&io_request->request_list_entry,
4730                        &ctrl_info->raid_bypass_retry_list);
4731        spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4732}
4733
4734static void pqi_queued_raid_bypass_complete(struct pqi_io_request *io_request,
4735        void *context)
4736{
4737        struct scsi_cmnd *scmd;
4738
4739        scmd = io_request->scmd;
4740        pqi_free_io_request(io_request);
4741        pqi_scsi_done(scmd);
4742}
4743
4744static void pqi_queue_raid_bypass_retry(struct pqi_io_request *io_request)
4745{
4746        struct scsi_cmnd *scmd;
4747        struct pqi_ctrl_info *ctrl_info;
4748
4749        io_request->io_complete_callback = pqi_queued_raid_bypass_complete;
4750        scmd = io_request->scmd;
4751        scmd->result = 0;
4752        ctrl_info = shost_to_hba(scmd->device->host);
4753
4754        pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request, false);
4755        pqi_schedule_bypass_retry(ctrl_info);
4756}
4757
4758static int pqi_retry_raid_bypass(struct pqi_io_request *io_request)
4759{
4760        struct scsi_cmnd *scmd;
4761        struct pqi_scsi_dev *device;
4762        struct pqi_ctrl_info *ctrl_info;
4763        struct pqi_queue_group *queue_group;
4764
4765        scmd = io_request->scmd;
4766        device = scmd->device->hostdata;
4767        if (pqi_device_in_reset(device)) {
4768                pqi_free_io_request(io_request);
4769                set_host_byte(scmd, DID_RESET);
4770                pqi_scsi_done(scmd);
4771                return 0;
4772        }
4773
4774        ctrl_info = shost_to_hba(scmd->device->host);
4775        queue_group = io_request->queue_group;
4776
4777        pqi_reinit_io_request(io_request);
4778
4779        return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
4780                device, scmd, queue_group);
4781}
4782
4783static inline struct pqi_io_request *pqi_next_queued_raid_bypass_request(
4784        struct pqi_ctrl_info *ctrl_info)
4785{
4786        unsigned long flags;
4787        struct pqi_io_request *io_request;
4788
4789        spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4790        io_request = list_first_entry_or_null(
4791                &ctrl_info->raid_bypass_retry_list,
4792                struct pqi_io_request, request_list_entry);
4793        if (io_request)
4794                list_del(&io_request->request_list_entry);
4795        spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4796
4797        return io_request;
4798}
4799
4800static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info)
4801{
4802        int rc;
4803        struct pqi_io_request *io_request;
4804
4805        pqi_ctrl_busy(ctrl_info);
4806
4807        while (1) {
4808                if (pqi_ctrl_blocked(ctrl_info))
4809                        break;
4810                io_request = pqi_next_queued_raid_bypass_request(ctrl_info);
4811                if (!io_request)
4812                        break;
4813                rc = pqi_retry_raid_bypass(io_request);
4814                if (rc) {
4815                        pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request,
4816                                true);
4817                        pqi_schedule_bypass_retry(ctrl_info);
4818                        break;
4819                }
4820        }
4821
4822        pqi_ctrl_unbusy(ctrl_info);
4823}
4824
4825static void pqi_raid_bypass_retry_worker(struct work_struct *work)
4826{
4827        struct pqi_ctrl_info *ctrl_info;
4828
4829        ctrl_info = container_of(work, struct pqi_ctrl_info,
4830                raid_bypass_retry_work);
4831        pqi_retry_raid_bypass_requests(ctrl_info);
4832}
4833
4834static void pqi_clear_all_queued_raid_bypass_retries(
4835        struct pqi_ctrl_info *ctrl_info)
4836{
4837        unsigned long flags;
4838
4839        spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4840        INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
4841        spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4842}
4843
4844static void pqi_aio_io_complete(struct pqi_io_request *io_request,
4845        void *context)
4846{
4847        struct scsi_cmnd *scmd;
4848
4849        scmd = io_request->scmd;
4850        scsi_dma_unmap(scmd);
4851        if (io_request->status == -EAGAIN)
4852                set_host_byte(scmd, DID_IMM_RETRY);
4853        else if (pqi_raid_bypass_retry_needed(io_request)) {
4854                pqi_queue_raid_bypass_retry(io_request);
4855                return;
4856        }
4857        pqi_free_io_request(io_request);
4858        pqi_scsi_done(scmd);
4859}
4860
4861static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4862        struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4863        struct pqi_queue_group *queue_group)
4864{
4865        return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
4866                scmd->cmnd, scmd->cmd_len, queue_group, NULL, false);
4867}
4868
4869static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
4870        struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
4871        unsigned int cdb_length, struct pqi_queue_group *queue_group,
4872        struct pqi_encryption_info *encryption_info, bool raid_bypass)
4873{
4874        int rc;
4875        struct pqi_io_request *io_request;
4876        struct pqi_aio_path_request *request;
4877
4878        io_request = pqi_alloc_io_request(ctrl_info);
4879        io_request->io_complete_callback = pqi_aio_io_complete;
4880        io_request->scmd = scmd;
4881        io_request->raid_bypass = raid_bypass;
4882
4883        request = io_request->iu;
4884        memset(request, 0,
4885                offsetof(struct pqi_raid_path_request, sg_descriptors));
4886
4887        request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
4888        put_unaligned_le32(aio_handle, &request->nexus_id);
4889        put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4890        request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4891        put_unaligned_le16(io_request->index, &request->request_id);
4892        request->error_index = request->request_id;
4893        if (cdb_length > sizeof(request->cdb))
4894                cdb_length = sizeof(request->cdb);
4895        request->cdb_length = cdb_length;
4896        memcpy(request->cdb, cdb, cdb_length);
4897
4898        switch (scmd->sc_data_direction) {
4899        case DMA_TO_DEVICE:
4900                request->data_direction = SOP_READ_FLAG;
4901                break;
4902        case DMA_FROM_DEVICE:
4903                request->data_direction = SOP_WRITE_FLAG;
4904                break;
4905        case DMA_NONE:
4906                request->data_direction = SOP_NO_DIRECTION_FLAG;
4907                break;
4908        case DMA_BIDIRECTIONAL:
4909                request->data_direction = SOP_BIDIRECTIONAL;
4910                break;
4911        default:
4912                dev_err(&ctrl_info->pci_dev->dev,
4913                        "unknown data direction: %d\n",
4914                        scmd->sc_data_direction);
4915                break;
4916        }
4917
4918        if (encryption_info) {
4919                request->encryption_enable = true;
4920                put_unaligned_le16(encryption_info->data_encryption_key_index,
4921                        &request->data_encryption_key_index);
4922                put_unaligned_le32(encryption_info->encrypt_tweak_lower,
4923                        &request->encrypt_tweak_lower);
4924                put_unaligned_le32(encryption_info->encrypt_tweak_upper,
4925                        &request->encrypt_tweak_upper);
4926        }
4927
4928        rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
4929        if (rc) {
4930                pqi_free_io_request(io_request);
4931                return SCSI_MLQUEUE_HOST_BUSY;
4932        }
4933
4934        pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
4935
4936        return 0;
4937}
4938
4939static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info,
4940        struct scsi_cmnd *scmd)
4941{
4942        u16 hw_queue;
4943
4944        hw_queue = blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scmd->request));
4945        if (hw_queue > ctrl_info->max_hw_queue_index)
4946                hw_queue = 0;
4947
4948        return hw_queue;
4949}
4950
4951/*
4952 * This function gets called just before we hand the completed SCSI request
4953 * back to the SML.
4954 */
4955
4956void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd)
4957{
4958        struct pqi_scsi_dev *device;
4959
4960        device = scmd->device->hostdata;
4961        atomic_dec(&device->scsi_cmds_outstanding);
4962}
4963
4964static int pqi_scsi_queue_command(struct Scsi_Host *shost,
4965        struct scsi_cmnd *scmd)
4966{
4967        int rc;
4968        struct pqi_ctrl_info *ctrl_info;
4969        struct pqi_scsi_dev *device;
4970        u16 hw_queue;
4971        struct pqi_queue_group *queue_group;
4972        bool raid_bypassed;
4973
4974        device = scmd->device->hostdata;
4975        ctrl_info = shost_to_hba(shost);
4976
4977        atomic_inc(&device->scsi_cmds_outstanding);
4978
4979        if (pqi_ctrl_offline(ctrl_info)) {
4980                set_host_byte(scmd, DID_NO_CONNECT);
4981                pqi_scsi_done(scmd);
4982                return 0;
4983        }
4984
4985        pqi_ctrl_busy(ctrl_info);
4986        if (pqi_ctrl_blocked(ctrl_info) || pqi_device_in_reset(device)) {
4987                rc = SCSI_MLQUEUE_HOST_BUSY;
4988                goto out;
4989        }
4990
4991        /*
4992         * This is necessary because the SML doesn't zero out this field during
4993         * error recovery.
4994         */
4995        scmd->result = 0;
4996
4997        hw_queue = pqi_get_hw_queue(ctrl_info, scmd);
4998        queue_group = &ctrl_info->queue_groups[hw_queue];
4999
5000        if (pqi_is_logical_device(device)) {
5001                raid_bypassed = false;
5002                if (device->raid_bypass_enabled &&
5003                                !blk_rq_is_passthrough(scmd->request)) {
5004                        rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device,
5005                                scmd, queue_group);
5006                        if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY)
5007                                raid_bypassed = true;
5008                }
5009                if (!raid_bypassed)
5010                        rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5011                                queue_group);
5012        } else {
5013                if (device->aio_enabled)
5014                        rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd,
5015                                queue_group);
5016                else
5017                        rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5018                                queue_group);
5019        }
5020
5021out:
5022        pqi_ctrl_unbusy(ctrl_info);
5023        if (rc)
5024                atomic_dec(&device->scsi_cmds_outstanding);
5025
5026        return rc;
5027}
5028
5029static int pqi_wait_until_queued_io_drained(struct pqi_ctrl_info *ctrl_info,
5030        struct pqi_queue_group *queue_group)
5031{
5032        unsigned int path;
5033        unsigned long flags;
5034        bool list_is_empty;
5035
5036        for (path = 0; path < 2; path++) {
5037                while (1) {
5038                        spin_lock_irqsave(
5039                                &queue_group->submit_lock[path], flags);
5040                        list_is_empty =
5041                                list_empty(&queue_group->request_list[path]);
5042                        spin_unlock_irqrestore(
5043                                &queue_group->submit_lock[path], flags);
5044                        if (list_is_empty)
5045                                break;
5046                        pqi_check_ctrl_health(ctrl_info);
5047                        if (pqi_ctrl_offline(ctrl_info))
5048                                return -ENXIO;
5049                        usleep_range(1000, 2000);
5050                }
5051        }
5052
5053        return 0;
5054}
5055
5056static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info)
5057{
5058        int rc;
5059        unsigned int i;
5060        unsigned int path;
5061        struct pqi_queue_group *queue_group;
5062        pqi_index_t iq_pi;
5063        pqi_index_t iq_ci;
5064
5065        for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5066                queue_group = &ctrl_info->queue_groups[i];
5067
5068                rc = pqi_wait_until_queued_io_drained(ctrl_info, queue_group);
5069                if (rc)
5070                        return rc;
5071
5072                for (path = 0; path < 2; path++) {
5073                        iq_pi = queue_group->iq_pi_copy[path];
5074
5075                        while (1) {
5076                                iq_ci = *queue_group->iq_ci[path];
5077                                if (iq_ci == iq_pi)
5078                                        break;
5079                                pqi_check_ctrl_health(ctrl_info);
5080                                if (pqi_ctrl_offline(ctrl_info))
5081                                        return -ENXIO;
5082                                usleep_range(1000, 2000);
5083                        }
5084                }
5085        }
5086
5087        return 0;
5088}
5089
5090static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info,
5091        struct pqi_scsi_dev *device)
5092{
5093        unsigned int i;
5094        unsigned int path;
5095        struct pqi_queue_group *queue_group;
5096        unsigned long flags;
5097        struct pqi_io_request *io_request;
5098        struct pqi_io_request *next;
5099        struct scsi_cmnd *scmd;
5100        struct pqi_scsi_dev *scsi_device;
5101
5102        for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5103                queue_group = &ctrl_info->queue_groups[i];
5104
5105                for (path = 0; path < 2; path++) {
5106                        spin_lock_irqsave(
5107                                &queue_group->submit_lock[path], flags);
5108
5109                        list_for_each_entry_safe(io_request, next,
5110                                &queue_group->request_list[path],
5111                                request_list_entry) {
5112                                scmd = io_request->scmd;
5113                                if (!scmd)
5114                                        continue;
5115
5116                                scsi_device = scmd->device->hostdata;
5117                                if (scsi_device != device)
5118                                        continue;
5119
5120                                list_del(&io_request->request_list_entry);
5121                                set_host_byte(scmd, DID_RESET);
5122                                pqi_scsi_done(scmd);
5123                        }
5124
5125                        spin_unlock_irqrestore(
5126                                &queue_group->submit_lock[path], flags);
5127                }
5128        }
5129}
5130
5131static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
5132        struct pqi_scsi_dev *device)
5133{
5134        while (atomic_read(&device->scsi_cmds_outstanding)) {
5135                pqi_check_ctrl_health(ctrl_info);
5136                if (pqi_ctrl_offline(ctrl_info))
5137                        return -ENXIO;
5138                usleep_range(1000, 2000);
5139        }
5140
5141        return 0;
5142}
5143
5144static int pqi_ctrl_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info)
5145{
5146        bool io_pending;
5147        unsigned long flags;
5148        struct pqi_scsi_dev *device;
5149
5150        while (1) {
5151                io_pending = false;
5152
5153                spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5154                list_for_each_entry(device, &ctrl_info->scsi_device_list,
5155                        scsi_device_list_entry) {
5156                        if (atomic_read(&device->scsi_cmds_outstanding)) {
5157                                io_pending = true;
5158                                break;
5159                        }
5160                }
5161                spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5162                                        flags);
5163
5164                if (!io_pending)
5165                        break;
5166
5167                pqi_check_ctrl_health(ctrl_info);
5168                if (pqi_ctrl_offline(ctrl_info))
5169                        return -ENXIO;
5170
5171                usleep_range(1000, 2000);
5172        }
5173
5174        return 0;
5175}
5176
5177static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
5178        void *context)
5179{
5180        struct completion *waiting = context;
5181
5182        complete(waiting);
5183}
5184
5185#define PQI_LUN_RESET_TIMEOUT_SECS      10
5186
5187static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
5188        struct pqi_scsi_dev *device, struct completion *wait)
5189{
5190        int rc;
5191
5192        while (1) {
5193                if (wait_for_completion_io_timeout(wait,
5194                        PQI_LUN_RESET_TIMEOUT_SECS * HZ)) {
5195                        rc = 0;
5196                        break;
5197                }
5198
5199                pqi_check_ctrl_health(ctrl_info);
5200                if (pqi_ctrl_offline(ctrl_info)) {
5201                        rc = -ENXIO;
5202                        break;
5203                }
5204        }
5205
5206        return rc;
5207}
5208
5209static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info,
5210        struct pqi_scsi_dev *device)
5211{
5212        int rc;
5213        struct pqi_io_request *io_request;
5214        DECLARE_COMPLETION_ONSTACK(wait);
5215        struct pqi_task_management_request *request;
5216
5217        io_request = pqi_alloc_io_request(ctrl_info);
5218        io_request->io_complete_callback = pqi_lun_reset_complete;
5219        io_request->context = &wait;
5220
5221        request = io_request->iu;
5222        memset(request, 0, sizeof(*request));
5223
5224        request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
5225        put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
5226                &request->header.iu_length);
5227        put_unaligned_le16(io_request->index, &request->request_id);
5228        memcpy(request->lun_number, device->scsi3addr,
5229                sizeof(request->lun_number));
5230        request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
5231
5232        pqi_start_io(ctrl_info,
5233                &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
5234                io_request);
5235
5236        rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, &wait);
5237        if (rc == 0)
5238                rc = io_request->status;
5239
5240        pqi_free_io_request(io_request);
5241
5242        return rc;
5243}
5244
5245/* Performs a reset at the LUN level. */
5246
5247static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
5248        struct pqi_scsi_dev *device)
5249{
5250        int rc;
5251
5252        rc = pqi_lun_reset(ctrl_info, device);
5253        if (rc == 0)
5254                rc = pqi_device_wait_for_pending_io(ctrl_info, device);
5255
5256        return rc == 0 ? SUCCESS : FAILED;
5257}
5258
5259static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
5260{
5261        int rc;
5262        struct Scsi_Host *shost;
5263        struct pqi_ctrl_info *ctrl_info;
5264        struct pqi_scsi_dev *device;
5265
5266        shost = scmd->device->host;
5267        ctrl_info = shost_to_hba(shost);
5268        device = scmd->device->hostdata;
5269
5270        dev_err(&ctrl_info->pci_dev->dev,
5271                "resetting scsi %d:%d:%d:%d\n",
5272                shost->host_no, device->bus, device->target, device->lun);
5273
5274        pqi_check_ctrl_health(ctrl_info);
5275        if (pqi_ctrl_offline(ctrl_info)) {
5276                rc = FAILED;
5277                goto out;
5278        }
5279
5280        mutex_lock(&ctrl_info->lun_reset_mutex);
5281
5282        pqi_ctrl_block_requests(ctrl_info);
5283        pqi_ctrl_wait_until_quiesced(ctrl_info);
5284        pqi_fail_io_queued_for_device(ctrl_info, device);
5285        rc = pqi_wait_until_inbound_queues_empty(ctrl_info);
5286        pqi_device_reset_start(device);
5287        pqi_ctrl_unblock_requests(ctrl_info);
5288
5289        if (rc)
5290                rc = FAILED;
5291        else
5292                rc = pqi_device_reset(ctrl_info, device);
5293
5294        pqi_device_reset_done(device);
5295
5296        mutex_unlock(&ctrl_info->lun_reset_mutex);
5297
5298out:
5299        dev_err(&ctrl_info->pci_dev->dev,
5300                "reset of scsi %d:%d:%d:%d: %s\n",
5301                shost->host_no, device->bus, device->target, device->lun,
5302                rc == SUCCESS ? "SUCCESS" : "FAILED");
5303
5304        return rc;
5305}
5306
5307static int pqi_slave_alloc(struct scsi_device *sdev)
5308{
5309        struct pqi_scsi_dev *device;
5310        unsigned long flags;
5311        struct pqi_ctrl_info *ctrl_info;
5312        struct scsi_target *starget;
5313        struct sas_rphy *rphy;
5314
5315        ctrl_info = shost_to_hba(sdev->host);
5316
5317        spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5318
5319        if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
5320                starget = scsi_target(sdev);
5321                rphy = target_to_rphy(starget);
5322                device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
5323                if (device) {
5324                        device->target = sdev_id(sdev);
5325                        device->lun = sdev->lun;
5326                        device->target_lun_valid = true;
5327                }
5328        } else {
5329                device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
5330                        sdev_id(sdev), sdev->lun);
5331        }
5332
5333        if (device) {
5334                sdev->hostdata = device;
5335                device->sdev = sdev;
5336                if (device->queue_depth) {
5337                        device->advertised_queue_depth = device->queue_depth;
5338                        scsi_change_queue_depth(sdev,
5339                                device->advertised_queue_depth);
5340                }
5341        }
5342
5343        spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5344
5345        return 0;
5346}
5347
5348static int pqi_map_queues(struct Scsi_Host *shost)
5349{
5350        struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
5351
5352        return blk_mq_pci_map_queues(&shost->tag_set, ctrl_info->pci_dev);
5353}
5354
5355static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info,
5356        void __user *arg)
5357{
5358        struct pci_dev *pci_dev;
5359        u32 subsystem_vendor;
5360        u32 subsystem_device;
5361        cciss_pci_info_struct pciinfo;
5362
5363        if (!arg)
5364                return -EINVAL;
5365
5366        pci_dev = ctrl_info->pci_dev;
5367
5368        pciinfo.domain = pci_domain_nr(pci_dev->bus);
5369        pciinfo.bus = pci_dev->bus->number;
5370        pciinfo.dev_fn = pci_dev->devfn;
5371        subsystem_vendor = pci_dev->subsystem_vendor;
5372        subsystem_device = pci_dev->subsystem_device;
5373        pciinfo.board_id = ((subsystem_device << 16) & 0xffff0000) |
5374                subsystem_vendor;
5375
5376        if (copy_to_user(arg, &pciinfo, sizeof(pciinfo)))
5377                return -EFAULT;
5378
5379        return 0;
5380}
5381
5382static int pqi_getdrivver_ioctl(void __user *arg)
5383{
5384        u32 version;
5385
5386        if (!arg)
5387                return -EINVAL;
5388
5389        version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
5390                (DRIVER_RELEASE << 16) | DRIVER_REVISION;
5391
5392        if (copy_to_user(arg, &version, sizeof(version)))
5393                return -EFAULT;
5394
5395        return 0;
5396}
5397
5398struct ciss_error_info {
5399        u8      scsi_status;
5400        int     command_status;
5401        size_t  sense_data_length;
5402};
5403
5404static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
5405        struct ciss_error_info *ciss_error_info)
5406{
5407        int ciss_cmd_status;
5408        size_t sense_data_length;
5409
5410        switch (pqi_error_info->data_out_result) {
5411        case PQI_DATA_IN_OUT_GOOD:
5412                ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
5413                break;
5414        case PQI_DATA_IN_OUT_UNDERFLOW:
5415                ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
5416                break;
5417        case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
5418                ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
5419                break;
5420        case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
5421        case PQI_DATA_IN_OUT_BUFFER_ERROR:
5422        case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
5423        case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
5424        case PQI_DATA_IN_OUT_ERROR:
5425                ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
5426                break;
5427        case PQI_DATA_IN_OUT_HARDWARE_ERROR:
5428        case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
5429        case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
5430        case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
5431        case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
5432        case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
5433        case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
5434        case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
5435        case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
5436        case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
5437                ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
5438                break;
5439        case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
5440                ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
5441                break;
5442        case PQI_DATA_IN_OUT_ABORTED:
5443                ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
5444                break;
5445        case PQI_DATA_IN_OUT_TIMEOUT:
5446                ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
5447                break;
5448        default:
5449                ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
5450                break;
5451        }
5452
5453        sense_data_length =
5454                get_unaligned_le16(&pqi_error_info->sense_data_length);
5455        if (sense_data_length == 0)
5456                sense_data_length =
5457                get_unaligned_le16(&pqi_error_info->response_data_length);
5458        if (sense_data_length)
5459                if (sense_data_length > sizeof(pqi_error_info->data))
5460                        sense_data_length = sizeof(pqi_error_info->data);
5461
5462        ciss_error_info->scsi_status = pqi_error_info->status;
5463        ciss_error_info->command_status = ciss_cmd_status;
5464        ciss_error_info->sense_data_length = sense_data_length;
5465}
5466
5467static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
5468{
5469        int rc;
5470        char *kernel_buffer = NULL;
5471        u16 iu_length;
5472        size_t sense_data_length;
5473        IOCTL_Command_struct iocommand;
5474        struct pqi_raid_path_request request;
5475        struct pqi_raid_error_info pqi_error_info;
5476        struct ciss_error_info ciss_error_info;
5477
5478        if (pqi_ctrl_offline(ctrl_info))
5479                return -ENXIO;
5480        if (!arg)
5481                return -EINVAL;
5482        if (!capable(CAP_SYS_RAWIO))
5483                return -EPERM;
5484        if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
5485                return -EFAULT;
5486        if (iocommand.buf_size < 1 &&
5487                iocommand.Request.Type.Direction != XFER_NONE)
5488                return -EINVAL;
5489        if (iocommand.Request.CDBLen > sizeof(request.cdb))
5490                return -EINVAL;
5491        if (iocommand.Request.Type.Type != TYPE_CMD)
5492                return -EINVAL;
5493
5494        switch (iocommand.Request.Type.Direction) {
5495        case XFER_NONE:
5496        case XFER_WRITE:
5497        case XFER_READ:
5498        case XFER_READ | XFER_WRITE:
5499                break;
5500        default:
5501                return -EINVAL;
5502        }
5503
5504        if (iocommand.buf_size > 0) {
5505                kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
5506                if (!kernel_buffer)
5507                        return -ENOMEM;
5508                if (iocommand.Request.Type.Direction & XFER_WRITE) {
5509                        if (copy_from_user(kernel_buffer, iocommand.buf,
5510                                iocommand.buf_size)) {
5511                                rc = -EFAULT;
5512                                goto out;
5513                        }
5514                } else {
5515                        memset(kernel_buffer, 0, iocommand.buf_size);
5516                }
5517        }
5518
5519        memset(&request, 0, sizeof(request));
5520
5521        request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
5522        iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
5523                PQI_REQUEST_HEADER_LENGTH;
5524        memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
5525                sizeof(request.lun_number));
5526        memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
5527        request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
5528
5529        switch (iocommand.Request.Type.Direction) {
5530        case XFER_NONE:
5531                request.data_direction = SOP_NO_DIRECTION_FLAG;
5532                break;
5533        case XFER_WRITE:
5534                request.data_direction = SOP_WRITE_FLAG;
5535                break;
5536        case XFER_READ:
5537                request.data_direction = SOP_READ_FLAG;
5538                break;
5539        case XFER_READ | XFER_WRITE:
5540                request.data_direction = SOP_BIDIRECTIONAL;
5541                break;
5542        }
5543
5544        request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5545
5546        if (iocommand.buf_size > 0) {
5547                put_unaligned_le32(iocommand.buf_size, &request.buffer_length);
5548
5549                rc = pqi_map_single(ctrl_info->pci_dev,
5550                        &request.sg_descriptors[0], kernel_buffer,
5551                        iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
5552                if (rc)
5553                        goto out;
5554
5555                iu_length += sizeof(request.sg_descriptors[0]);
5556        }
5557
5558        put_unaligned_le16(iu_length, &request.header.iu_length);
5559
5560        rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
5561                PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info, NO_TIMEOUT);
5562
5563        if (iocommand.buf_size > 0)
5564                pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
5565                        PCI_DMA_BIDIRECTIONAL);
5566
5567        memset(&iocommand.error_info, 0, sizeof(iocommand.error_info));
5568
5569        if (rc == 0) {
5570                pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info);
5571                iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status;
5572                iocommand.error_info.CommandStatus =
5573                        ciss_error_info.command_status;
5574                sense_data_length = ciss_error_info.sense_data_length;
5575                if (sense_data_length) {
5576                        if (sense_data_length >
5577                                sizeof(iocommand.error_info.SenseInfo))
5578                                sense_data_length =
5579                                        sizeof(iocommand.error_info.SenseInfo);
5580                        memcpy(iocommand.error_info.SenseInfo,
5581                                pqi_error_info.data, sense_data_length);
5582                        iocommand.error_info.SenseLen = sense_data_length;
5583                }
5584        }
5585
5586        if (copy_to_user(arg, &iocommand, sizeof(iocommand))) {
5587                rc = -EFAULT;
5588                goto out;
5589        }
5590
5591        if (rc == 0 && iocommand.buf_size > 0 &&
5592                (iocommand.Request.Type.Direction & XFER_READ)) {
5593                if (copy_to_user(iocommand.buf, kernel_buffer,
5594                        iocommand.buf_size)) {
5595                        rc = -EFAULT;
5596                }
5597        }
5598
5599out:
5600        kfree(kernel_buffer);
5601
5602        return rc;
5603}
5604
5605static int pqi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
5606{
5607        int rc;
5608        struct pqi_ctrl_info *ctrl_info;
5609
5610        ctrl_info = shost_to_hba(sdev->host);
5611
5612        switch (cmd) {
5613        case CCISS_DEREGDISK:
5614        case CCISS_REGNEWDISK:
5615        case CCISS_REGNEWD:
5616                rc = pqi_scan_scsi_devices(ctrl_info);
5617                break;
5618        case CCISS_GETPCIINFO:
5619                rc = pqi_getpciinfo_ioctl(ctrl_info, arg);
5620                break;
5621        case CCISS_GETDRIVVER:
5622                rc = pqi_getdrivver_ioctl(arg);
5623                break;
5624        case CCISS_PASSTHRU:
5625                rc = pqi_passthru_ioctl(ctrl_info, arg);
5626                break;
5627        default:
5628                rc = -EINVAL;
5629                break;
5630        }
5631
5632        return rc;
5633}
5634
5635static ssize_t pqi_version_show(struct device *dev,
5636        struct device_attribute *attr, char *buffer)
5637{
5638        ssize_t count = 0;
5639        struct Scsi_Host *shost;
5640        struct pqi_ctrl_info *ctrl_info;
5641
5642        shost = class_to_shost(dev);
5643        ctrl_info = shost_to_hba(shost);
5644
5645        count += snprintf(buffer + count, PAGE_SIZE - count,
5646                "  driver: %s\n", DRIVER_VERSION BUILD_TIMESTAMP);
5647
5648        count += snprintf(buffer + count, PAGE_SIZE - count,
5649                "firmware: %s\n", ctrl_info->firmware_version);
5650
5651        return count;
5652}
5653
5654static ssize_t pqi_host_rescan_store(struct device *dev,
5655        struct device_attribute *attr, const char *buffer, size_t count)
5656{
5657        struct Scsi_Host *shost = class_to_shost(dev);
5658
5659        pqi_scan_start(shost);
5660
5661        return count;
5662}
5663
5664static ssize_t pqi_lockup_action_show(struct device *dev,
5665        struct device_attribute *attr, char *buffer)
5666{
5667        int count = 0;
5668        unsigned int i;
5669
5670        for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
5671                if (pqi_lockup_actions[i].action == pqi_lockup_action)
5672                        count += snprintf(buffer + count, PAGE_SIZE - count,
5673                                "[%s] ", pqi_lockup_actions[i].name);
5674                else
5675                        count += snprintf(buffer + count, PAGE_SIZE - count,
5676                                "%s ", pqi_lockup_actions[i].name);
5677        }
5678
5679        count += snprintf(buffer + count, PAGE_SIZE - count, "\n");
5680
5681        return count;
5682}
5683
5684static ssize_t pqi_lockup_action_store(struct device *dev,
5685        struct device_attribute *attr, const char *buffer, size_t count)
5686{
5687        unsigned int i;
5688        char *action_name;
5689        char action_name_buffer[32];
5690
5691        strlcpy(action_name_buffer, buffer, sizeof(action_name_buffer));
5692        action_name = strstrip(action_name_buffer);
5693
5694        for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
5695                if (strcmp(action_name, pqi_lockup_actions[i].name) == 0) {
5696                        pqi_lockup_action = pqi_lockup_actions[i].action;
5697                        return count;
5698                }
5699        }
5700
5701        return -EINVAL;
5702}
5703
5704static DEVICE_ATTR(version, 0444, pqi_version_show, NULL);
5705static DEVICE_ATTR(rescan, 0200, NULL, pqi_host_rescan_store);
5706static DEVICE_ATTR(lockup_action, 0644,
5707        pqi_lockup_action_show, pqi_lockup_action_store);
5708
5709static struct device_attribute *pqi_shost_attrs[] = {
5710        &dev_attr_version,
5711        &dev_attr_rescan,
5712        &dev_attr_lockup_action,
5713        NULL
5714};
5715
5716static ssize_t pqi_sas_address_show(struct device *dev,
5717        struct device_attribute *attr, char *buffer)
5718{
5719        struct pqi_ctrl_info *ctrl_info;
5720        struct scsi_device *sdev;
5721        struct pqi_scsi_dev *device;
5722        unsigned long flags;
5723        u64 sas_address;
5724
5725        sdev = to_scsi_device(dev);
5726        ctrl_info = shost_to_hba(sdev->host);
5727
5728        spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5729
5730        device = sdev->hostdata;
5731        if (pqi_is_logical_device(device)) {
5732                spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5733                        flags);
5734                return -ENODEV;
5735        }
5736        sas_address = device->sas_address;
5737
5738        spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5739
5740        return snprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
5741}
5742
5743static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
5744        struct device_attribute *attr, char *buffer)
5745{
5746        struct pqi_ctrl_info *ctrl_info;
5747        struct scsi_device *sdev;
5748        struct pqi_scsi_dev *device;
5749        unsigned long flags;
5750
5751        sdev = to_scsi_device(dev);
5752        ctrl_info = shost_to_hba(sdev->host);
5753
5754        spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5755
5756        device = sdev->hostdata;
5757        buffer[0] = device->raid_bypass_enabled ? '1' : '0';
5758        buffer[1] = '\n';
5759        buffer[2] = '\0';
5760
5761        spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5762
5763        return 2;
5764}
5765
5766static ssize_t pqi_raid_level_show(struct device *dev,
5767        struct device_attribute *attr, char *buffer)
5768{
5769        struct pqi_ctrl_info *ctrl_info;
5770        struct scsi_device *sdev;
5771        struct pqi_scsi_dev *device;
5772        unsigned long flags;
5773        char *raid_level;
5774
5775        sdev = to_scsi_device(dev);
5776        ctrl_info = shost_to_hba(sdev->host);
5777
5778        spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5779
5780        device = sdev->hostdata;
5781
5782        if (pqi_is_logical_device(device))
5783                raid_level = pqi_raid_level_to_string(device->raid_level);
5784        else
5785                raid_level = "N/A";
5786
5787        spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5788
5789        return snprintf(buffer, PAGE_SIZE, "%s\n", raid_level);
5790}
5791
5792static DEVICE_ATTR(sas_address, 0444, pqi_sas_address_show, NULL);
5793static DEVICE_ATTR(ssd_smart_path_enabled, 0444,
5794        pqi_ssd_smart_path_enabled_show, NULL);
5795static DEVICE_ATTR(raid_level, 0444, pqi_raid_level_show, NULL);
5796
5797static struct device_attribute *pqi_sdev_attrs[] = {
5798        &dev_attr_sas_address,
5799        &dev_attr_ssd_smart_path_enabled,
5800        &dev_attr_raid_level,
5801        NULL
5802};
5803
5804static struct scsi_host_template pqi_driver_template = {
5805        .module = THIS_MODULE,
5806        .name = DRIVER_NAME_SHORT,
5807        .proc_name = DRIVER_NAME_SHORT,
5808        .queuecommand = pqi_scsi_queue_command,
5809        .scan_start = pqi_scan_start,
5810        .scan_finished = pqi_scan_finished,
5811        .this_id = -1,
5812        .use_clustering = ENABLE_CLUSTERING,
5813        .eh_device_reset_handler = pqi_eh_device_reset_handler,
5814        .ioctl = pqi_ioctl,
5815        .slave_alloc = pqi_slave_alloc,
5816        .map_queues = pqi_map_queues,
5817        .sdev_attrs = pqi_sdev_attrs,
5818        .shost_attrs = pqi_shost_attrs,
5819};
5820
5821static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info)
5822{
5823        int rc;
5824        struct Scsi_Host *shost;
5825
5826        shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info));
5827        if (!shost) {
5828                dev_err(&ctrl_info->pci_dev->dev,
5829                        "scsi_host_alloc failed for controller %u\n",
5830                        ctrl_info->ctrl_id);
5831                return -ENOMEM;
5832        }
5833
5834        shost->io_port = 0;
5835        shost->n_io_port = 0;
5836        shost->this_id = -1;
5837        shost->max_channel = PQI_MAX_BUS;
5838        shost->max_cmd_len = MAX_COMMAND_SIZE;
5839        shost->max_lun = ~0;
5840        shost->max_id = ~0;
5841        shost->max_sectors = ctrl_info->max_sectors;
5842        shost->can_queue = ctrl_info->scsi_ml_can_queue;
5843        shost->cmd_per_lun = shost->can_queue;
5844        shost->sg_tablesize = ctrl_info->sg_tablesize;
5845        shost->transportt = pqi_sas_transport_template;
5846        shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0);
5847        shost->unique_id = shost->irq;
5848        shost->nr_hw_queues = ctrl_info->num_queue_groups;
5849        shost->hostdata[0] = (unsigned long)ctrl_info;
5850
5851        rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev);
5852        if (rc) {
5853                dev_err(&ctrl_info->pci_dev->dev,
5854                        "scsi_add_host failed for controller %u\n",
5855                        ctrl_info->ctrl_id);
5856                goto free_host;
5857        }
5858
5859        rc = pqi_add_sas_host(shost, ctrl_info);
5860        if (rc) {
5861                dev_err(&ctrl_info->pci_dev->dev,
5862                        "add SAS host failed for controller %u\n",
5863                        ctrl_info->ctrl_id);
5864                goto remove_host;
5865        }
5866
5867        ctrl_info->scsi_host = shost;
5868
5869        return 0;
5870
5871remove_host:
5872        scsi_remove_host(shost);
5873free_host:
5874        scsi_host_put(shost);
5875
5876        return rc;
5877}
5878
5879static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info)
5880{
5881        struct Scsi_Host *shost;
5882
5883        pqi_delete_sas_host(ctrl_info);
5884
5885        shost = ctrl_info->scsi_host;
5886        if (!shost)
5887                return;
5888
5889        scsi_remove_host(shost);
5890        scsi_host_put(shost);
5891}
5892
5893static int pqi_wait_for_pqi_reset_completion(struct pqi_ctrl_info *ctrl_info)
5894{
5895        int rc = 0;
5896        struct pqi_device_registers __iomem *pqi_registers;
5897        unsigned long timeout;
5898        unsigned int timeout_msecs;
5899        union pqi_reset_register reset_reg;
5900
5901        pqi_registers = ctrl_info->pqi_registers;
5902        timeout_msecs = readw(&pqi_registers->max_reset_timeout) * 100;
5903        timeout = msecs_to_jiffies(timeout_msecs) + jiffies;
5904
5905        while (1) {
5906                msleep(PQI_RESET_POLL_INTERVAL_MSECS);
5907                reset_reg.all_bits = readl(&pqi_registers->device_reset);
5908                if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED)
5909                        break;
5910                pqi_check_ctrl_health(ctrl_info);
5911                if (pqi_ctrl_offline(ctrl_info)) {
5912                        rc = -ENXIO;
5913                        break;
5914                }
5915                if (time_after(jiffies, timeout)) {
5916                        rc = -ETIMEDOUT;
5917                        break;
5918                }
5919        }
5920
5921        return rc;
5922}
5923
5924static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
5925{
5926        int rc;
5927        union pqi_reset_register reset_reg;
5928
5929        if (ctrl_info->pqi_reset_quiesce_supported) {
5930                rc = sis_pqi_reset_quiesce(ctrl_info);
5931                if (rc) {
5932                        dev_err(&ctrl_info->pci_dev->dev,
5933                                "PQI reset failed during quiesce with error %d\n",
5934                                rc);
5935                        return rc;
5936                }
5937        }
5938
5939        reset_reg.all_bits = 0;
5940        reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET;
5941        reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET;
5942
5943        writel(reset_reg.all_bits, &ctrl_info->pqi_registers->device_reset);
5944
5945        rc = pqi_wait_for_pqi_reset_completion(ctrl_info);
5946        if (rc)
5947                dev_err(&ctrl_info->pci_dev->dev,
5948                        "PQI reset failed with error %d\n", rc);
5949
5950        return rc;
5951}
5952
5953static int pqi_get_ctrl_firmware_version(struct pqi_ctrl_info *ctrl_info)
5954{
5955        int rc;
5956        struct bmic_identify_controller *identify;
5957
5958        identify = kmalloc(sizeof(*identify), GFP_KERNEL);
5959        if (!identify)
5960                return -ENOMEM;
5961
5962        rc = pqi_identify_controller(ctrl_info, identify);
5963        if (rc)
5964                goto out;
5965
5966        memcpy(ctrl_info->firmware_version, identify->firmware_version,
5967                sizeof(identify->firmware_version));
5968        ctrl_info->firmware_version[sizeof(identify->firmware_version)] = '\0';
5969        snprintf(ctrl_info->firmware_version +
5970                strlen(ctrl_info->firmware_version),
5971                sizeof(ctrl_info->firmware_version),
5972                "-%u", get_unaligned_le16(&identify->firmware_build_number));
5973
5974out:
5975        kfree(identify);
5976
5977        return rc;
5978}
5979
5980static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info)
5981{
5982        u32 table_length;
5983        u32 section_offset;
5984        void __iomem *table_iomem_addr;
5985        struct pqi_config_table *config_table;
5986        struct pqi_config_table_section_header *section;
5987
5988        table_length = ctrl_info->config_table_length;
5989
5990        config_table = kmalloc(table_length, GFP_KERNEL);
5991        if (!config_table) {
5992                dev_err(&ctrl_info->pci_dev->dev,
5993                        "failed to allocate memory for PQI configuration table\n");
5994                return -ENOMEM;
5995        }
5996
5997        /*
5998         * Copy the config table contents from I/O memory space into the
5999         * temporary buffer.
6000         */
6001        table_iomem_addr = ctrl_info->iomem_base +
6002                ctrl_info->config_table_offset;
6003        memcpy_fromio(config_table, table_iomem_addr, table_length);
6004
6005        section_offset =
6006                get_unaligned_le32(&config_table->first_section_offset);
6007
6008        while (section_offset) {
6009                section = (void *)config_table + section_offset;
6010
6011                switch (get_unaligned_le16(&section->section_id)) {
6012                case PQI_CONFIG_TABLE_SECTION_HEARTBEAT:
6013                        if (pqi_disable_heartbeat)
6014                                dev_warn(&ctrl_info->pci_dev->dev,
6015                                "heartbeat disabled by module parameter\n");
6016                        else
6017                                ctrl_info->heartbeat_counter =
6018                                        table_iomem_addr +
6019                                        section_offset +
6020                                        offsetof(
6021                                        struct pqi_config_table_heartbeat,
6022                                                heartbeat_counter);
6023                        break;
6024                }
6025
6026                section_offset =
6027                        get_unaligned_le16(&section->next_section_offset);
6028        }
6029
6030        kfree(config_table);
6031
6032        return 0;
6033}
6034
6035/* Switches the controller from PQI mode back into SIS mode. */
6036
6037static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
6038{
6039        int rc;
6040
6041        pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE);
6042        rc = pqi_reset(ctrl_info);
6043        if (rc)
6044                return rc;
6045        rc = sis_reenable_sis_mode(ctrl_info);
6046        if (rc) {
6047                dev_err(&ctrl_info->pci_dev->dev,
6048                        "re-enabling SIS mode failed with error %d\n", rc);
6049                return rc;
6050        }
6051        pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6052
6053        return 0;
6054}
6055
6056/*
6057 * If the controller isn't already in SIS mode, this function forces it into
6058 * SIS mode.
6059 */
6060
6061static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
6062{
6063        if (!sis_is_firmware_running(ctrl_info))
6064                return -ENXIO;
6065
6066        if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
6067                return 0;
6068
6069        if (sis_is_kernel_up(ctrl_info)) {
6070                pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6071                return 0;
6072        }
6073
6074        return pqi_revert_to_sis_mode(ctrl_info);
6075}
6076
6077static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
6078{
6079        int rc;
6080
6081        rc = pqi_force_sis_mode(ctrl_info);
6082        if (rc)
6083                return rc;
6084
6085        /*
6086         * Wait until the controller is ready to start accepting SIS
6087         * commands.
6088         */
6089        rc = sis_wait_for_ctrl_ready(ctrl_info);
6090        if (rc)
6091                return rc;
6092
6093        /*
6094         * Get the controller properties.  This allows us to determine
6095         * whether or not it supports PQI mode.
6096         */
6097        rc = sis_get_ctrl_properties(ctrl_info);
6098        if (rc) {
6099                dev_err(&ctrl_info->pci_dev->dev,
6100                        "error obtaining controller properties\n");
6101                return rc;
6102        }
6103
6104        rc = sis_get_pqi_capabilities(ctrl_info);
6105        if (rc) {
6106                dev_err(&ctrl_info->pci_dev->dev,
6107                        "error obtaining controller capabilities\n");
6108                return rc;
6109        }
6110
6111        if (reset_devices) {
6112                if (ctrl_info->max_outstanding_requests >
6113                        PQI_MAX_OUTSTANDING_REQUESTS_KDUMP)
6114                        ctrl_info->max_outstanding_requests =
6115                                        PQI_MAX_OUTSTANDING_REQUESTS_KDUMP;
6116        } else {
6117                if (ctrl_info->max_outstanding_requests >
6118                        PQI_MAX_OUTSTANDING_REQUESTS)
6119                        ctrl_info->max_outstanding_requests =
6120                                        PQI_MAX_OUTSTANDING_REQUESTS;
6121        }
6122
6123        pqi_calculate_io_resources(ctrl_info);
6124
6125        rc = pqi_alloc_error_buffer(ctrl_info);
6126        if (rc) {
6127                dev_err(&ctrl_info->pci_dev->dev,
6128                        "failed to allocate PQI error buffer\n");
6129                return rc;
6130        }
6131
6132        /*
6133         * If the function we are about to call succeeds, the
6134         * controller will transition from legacy SIS mode
6135         * into PQI mode.
6136         */
6137        rc = sis_init_base_struct_addr(ctrl_info);
6138        if (rc) {
6139                dev_err(&ctrl_info->pci_dev->dev,
6140                        "error initializing PQI mode\n");
6141                return rc;
6142        }
6143
6144        /* Wait for the controller to complete the SIS -> PQI transition. */
6145        rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6146        if (rc) {
6147                dev_err(&ctrl_info->pci_dev->dev,
6148                        "transition to PQI mode failed\n");
6149                return rc;
6150        }
6151
6152        /* From here on, we are running in PQI mode. */
6153        ctrl_info->pqi_mode_enabled = true;
6154        pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
6155
6156        rc = pqi_process_config_table(ctrl_info);
6157        if (rc)
6158                return rc;
6159
6160        rc = pqi_alloc_admin_queues(ctrl_info);
6161        if (rc) {
6162                dev_err(&ctrl_info->pci_dev->dev,
6163                        "failed to allocate admin queues\n");
6164                return rc;
6165        }
6166
6167        rc = pqi_create_admin_queues(ctrl_info);
6168        if (rc) {
6169                dev_err(&ctrl_info->pci_dev->dev,
6170                        "error creating admin queues\n");
6171                return rc;
6172        }
6173
6174        rc = pqi_report_device_capability(ctrl_info);
6175        if (rc) {
6176                dev_err(&ctrl_info->pci_dev->dev,
6177                        "obtaining device capability failed\n");
6178                return rc;
6179        }
6180
6181        rc = pqi_validate_device_capability(ctrl_info);
6182        if (rc)
6183                return rc;
6184
6185        pqi_calculate_queue_resources(ctrl_info);
6186
6187        rc = pqi_enable_msix_interrupts(ctrl_info);
6188        if (rc)
6189                return rc;
6190
6191        if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
6192                ctrl_info->max_msix_vectors =
6193                        ctrl_info->num_msix_vectors_enabled;
6194                pqi_calculate_queue_resources(ctrl_info);
6195        }
6196
6197        rc = pqi_alloc_io_resources(ctrl_info);
6198        if (rc)
6199                return rc;
6200
6201        rc = pqi_alloc_operational_queues(ctrl_info);
6202        if (rc) {
6203                dev_err(&ctrl_info->pci_dev->dev,
6204                        "failed to allocate operational queues\n");
6205                return rc;
6206        }
6207
6208        pqi_init_operational_queues(ctrl_info);
6209
6210        rc = pqi_request_irqs(ctrl_info);
6211        if (rc)
6212                return rc;
6213
6214        rc = pqi_create_queues(ctrl_info);
6215        if (rc)
6216                return rc;
6217
6218        pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
6219
6220        ctrl_info->controller_online = true;
6221        pqi_start_heartbeat_timer(ctrl_info);
6222
6223        rc = pqi_enable_events(ctrl_info);
6224        if (rc) {
6225                dev_err(&ctrl_info->pci_dev->dev,
6226                        "error enabling events\n");
6227                return rc;
6228        }
6229
6230        /* Register with the SCSI subsystem. */
6231        rc = pqi_register_scsi(ctrl_info);
6232        if (rc)
6233                return rc;
6234
6235        rc = pqi_get_ctrl_firmware_version(ctrl_info);
6236        if (rc) {
6237                dev_err(&ctrl_info->pci_dev->dev,
6238                        "error obtaining firmware version\n");
6239                return rc;
6240        }
6241
6242        rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
6243        if (rc) {
6244                dev_err(&ctrl_info->pci_dev->dev,
6245                        "error updating host wellness\n");
6246                return rc;
6247        }
6248
6249        pqi_schedule_update_time_worker(ctrl_info);
6250
6251        pqi_scan_scsi_devices(ctrl_info);
6252
6253        return 0;
6254}
6255
6256static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info)
6257{
6258        unsigned int i;
6259        struct pqi_admin_queues *admin_queues;
6260        struct pqi_event_queue *event_queue;
6261
6262        admin_queues = &ctrl_info->admin_queues;
6263        admin_queues->iq_pi_copy = 0;
6264        admin_queues->oq_ci_copy = 0;
6265        *admin_queues->oq_pi = 0;
6266
6267        for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6268                ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0;
6269                ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0;
6270                ctrl_info->queue_groups[i].oq_ci_copy = 0;
6271
6272                *ctrl_info->queue_groups[i].iq_ci[RAID_PATH] = 0;
6273                *ctrl_info->queue_groups[i].iq_ci[AIO_PATH] = 0;
6274                *ctrl_info->queue_groups[i].oq_pi = 0;
6275        }
6276
6277        event_queue = &ctrl_info->event_queue;
6278        *event_queue->oq_pi = 0;
6279        event_queue->oq_ci_copy = 0;
6280}
6281
6282static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info)
6283{
6284        int rc;
6285
6286        rc = pqi_force_sis_mode(ctrl_info);
6287        if (rc)
6288                return rc;
6289
6290        /*
6291         * Wait until the controller is ready to start accepting SIS
6292         * commands.
6293         */
6294        rc = sis_wait_for_ctrl_ready_resume(ctrl_info);
6295        if (rc)
6296                return rc;
6297
6298        /*
6299         * If the function we are about to call succeeds, the
6300         * controller will transition from legacy SIS mode
6301         * into PQI mode.
6302         */
6303        rc = sis_init_base_struct_addr(ctrl_info);
6304        if (rc) {
6305                dev_err(&ctrl_info->pci_dev->dev,
6306                        "error initializing PQI mode\n");
6307                return rc;
6308        }
6309
6310        /* Wait for the controller to complete the SIS -> PQI transition. */
6311        rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6312        if (rc) {
6313                dev_err(&ctrl_info->pci_dev->dev,
6314                        "transition to PQI mode failed\n");
6315                return rc;
6316        }
6317
6318        /* From here on, we are running in PQI mode. */
6319        ctrl_info->pqi_mode_enabled = true;
6320        pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
6321
6322        pqi_reinit_queues(ctrl_info);
6323
6324        rc = pqi_create_admin_queues(ctrl_info);
6325        if (rc) {
6326                dev_err(&ctrl_info->pci_dev->dev,
6327                        "error creating admin queues\n");
6328                return rc;
6329        }
6330
6331        rc = pqi_create_queues(ctrl_info);
6332        if (rc)
6333                return rc;
6334
6335        pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
6336
6337        ctrl_info->controller_online = true;
6338        pqi_start_heartbeat_timer(ctrl_info);
6339        pqi_ctrl_unblock_requests(ctrl_info);
6340
6341        rc = pqi_enable_events(ctrl_info);
6342        if (rc) {
6343                dev_err(&ctrl_info->pci_dev->dev,
6344                        "error enabling events\n");
6345                return rc;
6346        }
6347
6348        rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
6349        if (rc) {
6350                dev_err(&ctrl_info->pci_dev->dev,
6351                        "error updating host wellness\n");
6352                return rc;
6353        }
6354
6355        pqi_schedule_update_time_worker(ctrl_info);
6356
6357        pqi_scan_scsi_devices(ctrl_info);
6358
6359        return 0;
6360}
6361
6362static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev,
6363        u16 timeout)
6364{
6365        return pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
6366                PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
6367}
6368
6369static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
6370{
6371        int rc;
6372        u64 mask;
6373
6374        rc = pci_enable_device(ctrl_info->pci_dev);
6375        if (rc) {
6376                dev_err(&ctrl_info->pci_dev->dev,
6377                        "failed to enable PCI device\n");
6378                return rc;
6379        }
6380
6381        if (sizeof(dma_addr_t) > 4)
6382                mask = DMA_BIT_MASK(64);
6383        else
6384                mask = DMA_BIT_MASK(32);
6385
6386        rc = dma_set_mask(&ctrl_info->pci_dev->dev, mask);
6387        if (rc) {
6388                dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
6389                goto disable_device;
6390        }
6391
6392        rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
6393        if (rc) {
6394                dev_err(&ctrl_info->pci_dev->dev,
6395                        "failed to obtain PCI resources\n");
6396                goto disable_device;
6397        }
6398
6399        ctrl_info->iomem_base = ioremap_nocache(pci_resource_start(
6400                ctrl_info->pci_dev, 0),
6401                sizeof(struct pqi_ctrl_registers));
6402        if (!ctrl_info->iomem_base) {
6403                dev_err(&ctrl_info->pci_dev->dev,
6404                        "failed to map memory for controller registers\n");
6405                rc = -ENOMEM;
6406                goto release_regions;
6407        }
6408
6409#define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS               0x6
6410
6411        /* Increase the PCIe completion timeout. */
6412        rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
6413                PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
6414        if (rc) {
6415                dev_err(&ctrl_info->pci_dev->dev,
6416                        "failed to set PCIe completion timeout\n");
6417                goto release_regions;
6418        }
6419
6420        /* Enable bus mastering. */
6421        pci_set_master(ctrl_info->pci_dev);
6422
6423        ctrl_info->registers = ctrl_info->iomem_base;
6424        ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
6425
6426        pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
6427
6428        return 0;
6429
6430release_regions:
6431        pci_release_regions(ctrl_info->pci_dev);
6432disable_device:
6433        pci_disable_device(ctrl_info->pci_dev);
6434
6435        return rc;
6436}
6437
6438static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
6439{
6440        iounmap(ctrl_info->iomem_base);
6441        pci_release_regions(ctrl_info->pci_dev);
6442        if (pci_is_enabled(ctrl_info->pci_dev))
6443                pci_disable_device(ctrl_info->pci_dev);
6444        pci_set_drvdata(ctrl_info->pci_dev, NULL);
6445}
6446
6447static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
6448{
6449        struct pqi_ctrl_info *ctrl_info;
6450
6451        ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
6452                        GFP_KERNEL, numa_node);
6453        if (!ctrl_info)
6454                return NULL;
6455
6456        mutex_init(&ctrl_info->scan_mutex);
6457        mutex_init(&ctrl_info->lun_reset_mutex);
6458
6459        INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
6460        spin_lock_init(&ctrl_info->scsi_device_list_lock);
6461
6462        INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
6463        atomic_set(&ctrl_info->num_interrupts, 0);
6464
6465        INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
6466        INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
6467
6468        init_timer(&ctrl_info->heartbeat_timer);
6469        INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker);
6470
6471        sema_init(&ctrl_info->sync_request_sem,
6472                PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
6473        init_waitqueue_head(&ctrl_info->block_requests_wait);
6474
6475        INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
6476        spin_lock_init(&ctrl_info->raid_bypass_retry_list_lock);
6477        INIT_WORK(&ctrl_info->raid_bypass_retry_work,
6478                pqi_raid_bypass_retry_worker);
6479
6480        ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
6481        ctrl_info->irq_mode = IRQ_MODE_NONE;
6482        ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
6483
6484        return ctrl_info;
6485}
6486
6487static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
6488{
6489        kfree(ctrl_info);
6490}
6491
6492static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
6493{
6494        pqi_free_irqs(ctrl_info);
6495        pqi_disable_msix_interrupts(ctrl_info);
6496}
6497
6498static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
6499{
6500        pqi_stop_heartbeat_timer(ctrl_info);
6501        pqi_free_interrupts(ctrl_info);
6502        if (ctrl_info->queue_memory_base)
6503                dma_free_coherent(&ctrl_info->pci_dev->dev,
6504                        ctrl_info->queue_memory_length,
6505                        ctrl_info->queue_memory_base,
6506                        ctrl_info->queue_memory_base_dma_handle);
6507        if (ctrl_info->admin_queue_memory_base)
6508                dma_free_coherent(&ctrl_info->pci_dev->dev,
6509                        ctrl_info->admin_queue_memory_length,
6510                        ctrl_info->admin_queue_memory_base,
6511                        ctrl_info->admin_queue_memory_base_dma_handle);
6512        pqi_free_all_io_requests(ctrl_info);
6513        if (ctrl_info->error_buffer)
6514                dma_free_coherent(&ctrl_info->pci_dev->dev,
6515                        ctrl_info->error_buffer_length,
6516                        ctrl_info->error_buffer,
6517                        ctrl_info->error_buffer_dma_handle);
6518        if (ctrl_info->iomem_base)
6519                pqi_cleanup_pci_init(ctrl_info);
6520        pqi_free_ctrl_info(ctrl_info);
6521}
6522
6523static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
6524{
6525        pqi_cancel_rescan_worker(ctrl_info);
6526        pqi_cancel_update_time_worker(ctrl_info);
6527        pqi_remove_all_scsi_devices(ctrl_info);
6528        pqi_unregister_scsi(ctrl_info);
6529        if (ctrl_info->pqi_mode_enabled)
6530                pqi_revert_to_sis_mode(ctrl_info);
6531        pqi_free_ctrl_resources(ctrl_info);
6532}
6533
6534static void pqi_perform_lockup_action(void)
6535{
6536        switch (pqi_lockup_action) {
6537        case PANIC:
6538                panic("FATAL: Smart Family Controller lockup detected");
6539                break;
6540        case REBOOT:
6541                emergency_restart();
6542                break;
6543        case NONE:
6544        default:
6545                break;
6546        }
6547}
6548
6549static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = {
6550        .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR,
6551        .status = SAM_STAT_CHECK_CONDITION,
6552};
6553
6554static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info)
6555{
6556        unsigned int i;
6557        struct pqi_io_request *io_request;
6558        struct scsi_cmnd *scmd;
6559
6560        for (i = 0; i < ctrl_info->max_io_slots; i++) {
6561                io_request = &ctrl_info->io_request_pool[i];
6562                if (atomic_read(&io_request->refcount) == 0)
6563                        continue;
6564
6565                scmd = io_request->scmd;
6566                if (scmd) {
6567                        set_host_byte(scmd, DID_NO_CONNECT);
6568                } else {
6569                        io_request->status = -ENXIO;
6570                        io_request->error_info =
6571                                &pqi_ctrl_offline_raid_error_info;
6572                }
6573
6574                io_request->io_complete_callback(io_request,
6575                        io_request->context);
6576        }
6577}
6578
6579static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info)
6580{
6581        pqi_perform_lockup_action();
6582        pqi_stop_heartbeat_timer(ctrl_info);
6583        pqi_free_interrupts(ctrl_info);
6584        pqi_cancel_rescan_worker(ctrl_info);
6585        pqi_cancel_update_time_worker(ctrl_info);
6586        pqi_ctrl_wait_until_quiesced(ctrl_info);
6587        pqi_fail_all_outstanding_requests(ctrl_info);
6588        pqi_clear_all_queued_raid_bypass_retries(ctrl_info);
6589        pqi_ctrl_unblock_requests(ctrl_info);
6590}
6591
6592static void pqi_ctrl_offline_worker(struct work_struct *work)
6593{
6594        struct pqi_ctrl_info *ctrl_info;
6595
6596        ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work);
6597        pqi_take_ctrl_offline_deferred(ctrl_info);
6598}
6599
6600static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
6601{
6602        if (!ctrl_info->controller_online)
6603                return;
6604
6605        ctrl_info->controller_online = false;
6606        ctrl_info->pqi_mode_enabled = false;
6607        pqi_ctrl_block_requests(ctrl_info);
6608        if (!pqi_disable_ctrl_shutdown)
6609                sis_shutdown_ctrl(ctrl_info);
6610        pci_disable_device(ctrl_info->pci_dev);
6611        dev_err(&ctrl_info->pci_dev->dev, "controller offline\n");
6612        schedule_work(&ctrl_info->ctrl_offline_work);
6613}
6614
6615static void pqi_print_ctrl_info(struct pci_dev *pci_dev,
6616        const struct pci_device_id *id)
6617{
6618        char *ctrl_description;
6619
6620        if (id->driver_data)
6621                ctrl_description = (char *)id->driver_data;
6622        else
6623                ctrl_description = "Microsemi Smart Family Controller";
6624
6625        dev_info(&pci_dev->dev, "%s found\n", ctrl_description);
6626}
6627
6628static int pqi_pci_probe(struct pci_dev *pci_dev,
6629        const struct pci_device_id *id)
6630{
6631        int rc;
6632        int node;
6633        struct pqi_ctrl_info *ctrl_info;
6634
6635        pqi_print_ctrl_info(pci_dev, id);
6636
6637        if (pqi_disable_device_id_wildcards &&
6638                id->subvendor == PCI_ANY_ID &&
6639                id->subdevice == PCI_ANY_ID) {
6640                dev_warn(&pci_dev->dev,
6641                        "controller not probed because device ID wildcards are disabled\n");
6642                return -ENODEV;
6643        }
6644
6645        if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
6646                dev_warn(&pci_dev->dev,
6647                        "controller device ID matched using wildcards\n");
6648
6649        node = dev_to_node(&pci_dev->dev);
6650        if (node == NUMA_NO_NODE)
6651                set_dev_node(&pci_dev->dev, 0);
6652
6653        ctrl_info = pqi_alloc_ctrl_info(node);
6654        if (!ctrl_info) {
6655                dev_err(&pci_dev->dev,
6656                        "failed to allocate controller info block\n");
6657                return -ENOMEM;
6658        }
6659
6660        ctrl_info->pci_dev = pci_dev;
6661
6662        rc = pqi_pci_init(ctrl_info);
6663        if (rc)
6664                goto error;
6665
6666        rc = pqi_ctrl_init(ctrl_info);
6667        if (rc)
6668                goto error;
6669
6670        return 0;
6671
6672error:
6673        pqi_remove_ctrl(ctrl_info);
6674
6675        return rc;
6676}
6677
6678static void pqi_pci_remove(struct pci_dev *pci_dev)
6679{
6680        struct pqi_ctrl_info *ctrl_info;
6681
6682        ctrl_info = pci_get_drvdata(pci_dev);
6683        if (!ctrl_info)
6684                return;
6685
6686        pqi_remove_ctrl(ctrl_info);
6687}
6688
6689static void pqi_shutdown(struct pci_dev *pci_dev)
6690{
6691        int rc;
6692        struct pqi_ctrl_info *ctrl_info;
6693
6694        ctrl_info = pci_get_drvdata(pci_dev);
6695        if (!ctrl_info)
6696                goto error;
6697
6698        /*
6699         * Write all data in the controller's battery-backed cache to
6700         * storage.
6701         */
6702        rc = pqi_flush_cache(ctrl_info, SHUTDOWN);
6703        pqi_reset(ctrl_info);
6704        if (rc == 0)
6705                return;
6706
6707error:
6708        dev_warn(&pci_dev->dev,
6709                "unable to flush controller cache\n");
6710}
6711
6712static void pqi_process_lockup_action_param(void)
6713{
6714        unsigned int i;
6715
6716        if (!pqi_lockup_action_param)
6717                return;
6718
6719        for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6720                if (strcmp(pqi_lockup_action_param,
6721                        pqi_lockup_actions[i].name) == 0) {
6722                        pqi_lockup_action = pqi_lockup_actions[i].action;
6723                        return;
6724                }
6725        }
6726
6727        pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n",
6728                DRIVER_NAME_SHORT, pqi_lockup_action_param);
6729}
6730
6731static void pqi_process_module_params(void)
6732{
6733        pqi_process_lockup_action_param();
6734}
6735
6736static __maybe_unused int pqi_suspend(struct pci_dev *pci_dev, pm_message_t state)
6737{
6738        struct pqi_ctrl_info *ctrl_info;
6739
6740        ctrl_info = pci_get_drvdata(pci_dev);
6741
6742        pqi_disable_events(ctrl_info);
6743        pqi_cancel_update_time_worker(ctrl_info);
6744        pqi_cancel_rescan_worker(ctrl_info);
6745        pqi_wait_until_scan_finished(ctrl_info);
6746        pqi_wait_until_lun_reset_finished(ctrl_info);
6747        pqi_flush_cache(ctrl_info, SUSPEND);
6748        pqi_ctrl_block_requests(ctrl_info);
6749        pqi_ctrl_wait_until_quiesced(ctrl_info);
6750        pqi_wait_until_inbound_queues_empty(ctrl_info);
6751        pqi_ctrl_wait_for_pending_io(ctrl_info);
6752        pqi_stop_heartbeat_timer(ctrl_info);
6753
6754        if (state.event == PM_EVENT_FREEZE)
6755                return 0;
6756
6757        pci_save_state(pci_dev);
6758        pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
6759
6760        ctrl_info->controller_online = false;
6761        ctrl_info->pqi_mode_enabled = false;
6762
6763        return 0;
6764}
6765
6766static __maybe_unused int pqi_resume(struct pci_dev *pci_dev)
6767{
6768        int rc;
6769        struct pqi_ctrl_info *ctrl_info;
6770
6771        ctrl_info = pci_get_drvdata(pci_dev);
6772
6773        if (pci_dev->current_state != PCI_D0) {
6774                ctrl_info->max_hw_queue_index = 0;
6775                pqi_free_interrupts(ctrl_info);
6776                pqi_change_irq_mode(ctrl_info, IRQ_MODE_INTX);
6777                rc = request_irq(pci_irq_vector(pci_dev, 0), pqi_irq_handler,
6778                        IRQF_SHARED, DRIVER_NAME_SHORT,
6779                        &ctrl_info->queue_groups[0]);
6780                if (rc) {
6781                        dev_err(&ctrl_info->pci_dev->dev,
6782                                "irq %u init failed with error %d\n",
6783                                pci_dev->irq, rc);
6784                        return rc;
6785                }
6786                pqi_start_heartbeat_timer(ctrl_info);
6787                pqi_ctrl_unblock_requests(ctrl_info);
6788                return 0;
6789        }
6790
6791        pci_set_power_state(pci_dev, PCI_D0);
6792        pci_restore_state(pci_dev);
6793
6794        return pqi_ctrl_init_resume(ctrl_info);
6795}
6796
6797/* Define the PCI IDs for the controllers that we support. */
6798static const struct pci_device_id pqi_pci_id_table[] = {
6799        {
6800                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6801                               0x152d, 0x8a22)
6802        },
6803        {
6804                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6805                               0x152d, 0x8a23)
6806        },
6807        {
6808                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6809                               0x152d, 0x8a24)
6810        },
6811        {
6812                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6813                               0x152d, 0x8a36)
6814        },
6815        {
6816                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6817                               0x152d, 0x8a37)
6818        },
6819        {
6820                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6821                               PCI_VENDOR_ID_ADAPTEC2, 0x0110)
6822        },
6823        {
6824                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6825                               PCI_VENDOR_ID_ADAPTEC2, 0x0608)
6826        },
6827        {
6828                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6829                               PCI_VENDOR_ID_ADAPTEC2, 0x0800)
6830        },
6831        {
6832                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6833                               PCI_VENDOR_ID_ADAPTEC2, 0x0801)
6834        },
6835        {
6836                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6837                               PCI_VENDOR_ID_ADAPTEC2, 0x0802)
6838        },
6839        {
6840                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6841                               PCI_VENDOR_ID_ADAPTEC2, 0x0803)
6842        },
6843        {
6844                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6845                               PCI_VENDOR_ID_ADAPTEC2, 0x0804)
6846        },
6847        {
6848                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6849                               PCI_VENDOR_ID_ADAPTEC2, 0x0805)
6850        },
6851        {
6852                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6853                               PCI_VENDOR_ID_ADAPTEC2, 0x0806)
6854        },
6855        {
6856                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6857                               PCI_VENDOR_ID_ADAPTEC2, 0x0807)
6858        },
6859        {
6860                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6861                               PCI_VENDOR_ID_ADAPTEC2, 0x0900)
6862        },
6863        {
6864                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6865                               PCI_VENDOR_ID_ADAPTEC2, 0x0901)
6866        },
6867        {
6868                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6869                               PCI_VENDOR_ID_ADAPTEC2, 0x0902)
6870        },
6871        {
6872                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6873                               PCI_VENDOR_ID_ADAPTEC2, 0x0903)
6874        },
6875        {
6876                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6877                               PCI_VENDOR_ID_ADAPTEC2, 0x0904)
6878        },
6879        {
6880                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6881                               PCI_VENDOR_ID_ADAPTEC2, 0x0905)
6882        },
6883        {
6884                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6885                               PCI_VENDOR_ID_ADAPTEC2, 0x0906)
6886        },
6887        {
6888                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6889                               PCI_VENDOR_ID_ADAPTEC2, 0x0907)
6890        },
6891        {
6892                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6893                               PCI_VENDOR_ID_ADAPTEC2, 0x0908)
6894        },
6895        {
6896                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6897                               PCI_VENDOR_ID_ADAPTEC2, 0x090a)
6898        },
6899        {
6900                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6901                               PCI_VENDOR_ID_ADAPTEC2, 0x1200)
6902        },
6903        {
6904                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6905                               PCI_VENDOR_ID_ADAPTEC2, 0x1201)
6906        },
6907        {
6908                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6909                               PCI_VENDOR_ID_ADAPTEC2, 0x1202)
6910        },
6911        {
6912                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6913                               PCI_VENDOR_ID_ADAPTEC2, 0x1280)
6914        },
6915        {
6916                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6917                               PCI_VENDOR_ID_ADAPTEC2, 0x1281)
6918        },
6919        {
6920                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6921                               PCI_VENDOR_ID_ADAPTEC2, 0x1300)
6922        },
6923        {
6924                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6925                               PCI_VENDOR_ID_ADAPTEC2, 0x1301)
6926        },
6927        {
6928                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6929                               PCI_VENDOR_ID_ADAPTEC2, 0x1380)
6930        },
6931        {
6932                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6933                               PCI_VENDOR_ID_DELL, 0x1fe0)
6934        },
6935        {
6936                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6937                               PCI_VENDOR_ID_HP, 0x0600)
6938        },
6939        {
6940                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6941                               PCI_VENDOR_ID_HP, 0x0601)
6942        },
6943        {
6944                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6945                               PCI_VENDOR_ID_HP, 0x0602)
6946        },
6947        {
6948                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6949                               PCI_VENDOR_ID_HP, 0x0603)
6950        },
6951        {
6952                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6953                               PCI_VENDOR_ID_HP, 0x0609)
6954        },
6955        {
6956                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6957                               PCI_VENDOR_ID_HP, 0x0650)
6958        },
6959        {
6960                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6961                               PCI_VENDOR_ID_HP, 0x0651)
6962        },
6963        {
6964                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6965                               PCI_VENDOR_ID_HP, 0x0652)
6966        },
6967        {
6968                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6969                               PCI_VENDOR_ID_HP, 0x0653)
6970        },
6971        {
6972                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6973                               PCI_VENDOR_ID_HP, 0x0654)
6974        },
6975        {
6976                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6977                               PCI_VENDOR_ID_HP, 0x0655)
6978        },
6979        {
6980                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6981                               PCI_VENDOR_ID_HP, 0x0700)
6982        },
6983        {
6984                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6985                               PCI_VENDOR_ID_HP, 0x0701)
6986        },
6987        {
6988                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6989                               PCI_VENDOR_ID_HP, 0x1001)
6990        },
6991        {
6992                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6993                               PCI_VENDOR_ID_HP, 0x1100)
6994        },
6995        {
6996                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6997                               PCI_VENDOR_ID_HP, 0x1101)
6998        },
6999        {
7000                PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7001                               PCI_ANY_ID, PCI_ANY_ID)
7002        },
7003        { 0 }
7004};
7005
7006MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
7007
7008static struct pci_driver pqi_pci_driver = {
7009        .name = DRIVER_NAME_SHORT,
7010        .id_table = pqi_pci_id_table,
7011        .probe = pqi_pci_probe,
7012        .remove = pqi_pci_remove,
7013        .shutdown = pqi_shutdown,
7014#if defined(CONFIG_PM)
7015        .suspend = pqi_suspend,
7016        .resume = pqi_resume,
7017#endif
7018};
7019
7020static int __init pqi_init(void)
7021{
7022        int rc;
7023
7024        pr_info(DRIVER_NAME "\n");
7025
7026        pqi_sas_transport_template =
7027                sas_attach_transport(&pqi_sas_transport_functions);
7028        if (!pqi_sas_transport_template)
7029                return -ENODEV;
7030
7031        pqi_process_module_params();
7032
7033        rc = pci_register_driver(&pqi_pci_driver);
7034        if (rc)
7035                sas_release_transport(pqi_sas_transport_template);
7036
7037        return rc;
7038}
7039
7040static void __exit pqi_cleanup(void)
7041{
7042        pci_unregister_driver(&pqi_pci_driver);
7043        sas_release_transport(pqi_sas_transport_template);
7044}
7045
7046module_init(pqi_init);
7047module_exit(pqi_cleanup);
7048
7049static void __attribute__((unused)) verify_structures(void)
7050{
7051        BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7052                sis_host_to_ctrl_doorbell) != 0x20);
7053        BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7054                sis_interrupt_mask) != 0x34);
7055        BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7056                sis_ctrl_to_host_doorbell) != 0x9c);
7057        BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7058                sis_ctrl_to_host_doorbell_clear) != 0xa0);
7059        BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7060                sis_driver_scratch) != 0xb0);
7061        BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7062                sis_firmware_status) != 0xbc);
7063        BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7064                sis_mailbox) != 0x1000);
7065        BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7066                pqi_registers) != 0x4000);
7067
7068        BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7069                iu_type) != 0x0);
7070        BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7071                iu_length) != 0x2);
7072        BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7073                response_queue_id) != 0x4);
7074        BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7075                work_area) != 0x6);
7076        BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
7077
7078        BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7079                status) != 0x0);
7080        BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7081                service_response) != 0x1);
7082        BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7083                data_present) != 0x2);
7084        BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7085                reserved) != 0x3);
7086        BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7087                residual_count) != 0x4);
7088        BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7089                data_length) != 0x8);
7090        BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7091                reserved1) != 0xa);
7092        BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7093                data) != 0xc);
7094        BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
7095
7096        BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7097                data_in_result) != 0x0);
7098        BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7099                data_out_result) != 0x1);
7100        BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7101                reserved) != 0x2);
7102        BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7103                status) != 0x5);
7104        BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7105                status_qualifier) != 0x6);
7106        BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7107                sense_data_length) != 0x8);
7108        BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7109                response_data_length) != 0xa);
7110        BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7111                data_in_transferred) != 0xc);
7112        BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7113                data_out_transferred) != 0x10);
7114        BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7115                data) != 0x14);
7116        BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
7117
7118        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7119                signature) != 0x0);
7120        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7121                function_and_status_code) != 0x8);
7122        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7123                max_admin_iq_elements) != 0x10);
7124        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7125                max_admin_oq_elements) != 0x11);
7126        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7127                admin_iq_element_length) != 0x12);
7128        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7129                admin_oq_element_length) != 0x13);
7130        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7131                max_reset_timeout) != 0x14);
7132        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7133                legacy_intx_status) != 0x18);
7134        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7135                legacy_intx_mask_set) != 0x1c);
7136        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7137                legacy_intx_mask_clear) != 0x20);
7138        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7139                device_status) != 0x40);
7140        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7141                admin_iq_pi_offset) != 0x48);
7142        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7143                admin_oq_ci_offset) != 0x50);
7144        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7145                admin_iq_element_array_addr) != 0x58);
7146        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7147                admin_oq_element_array_addr) != 0x60);
7148        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7149                admin_iq_ci_addr) != 0x68);
7150        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7151                admin_oq_pi_addr) != 0x70);
7152        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7153                admin_iq_num_elements) != 0x78);
7154        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7155                admin_oq_num_elements) != 0x79);
7156        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7157                admin_queue_int_msg_num) != 0x7a);
7158        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7159                device_error) != 0x80);
7160        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7161                error_details) != 0x88);
7162        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7163                device_reset) != 0x90);
7164        BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7165                power_action) != 0x94);
7166        BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
7167
7168        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7169                header.iu_type) != 0);
7170        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7171                header.iu_length) != 2);
7172        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7173                header.work_area) != 6);
7174        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7175                request_id) != 8);
7176        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7177                function_code) != 10);
7178        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7179                data.report_device_capability.buffer_length) != 44);
7180        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7181                data.report_device_capability.sg_descriptor) != 48);
7182        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7183                data.create_operational_iq.queue_id) != 12);
7184        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7185                data.create_operational_iq.element_array_addr) != 16);
7186        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7187                data.create_operational_iq.ci_addr) != 24);
7188        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7189                data.create_operational_iq.num_elements) != 32);
7190        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7191                data.create_operational_iq.element_length) != 34);
7192        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7193                data.create_operational_iq.queue_protocol) != 36);
7194        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7195                data.create_operational_oq.queue_id) != 12);
7196        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7197                data.create_operational_oq.element_array_addr) != 16);
7198        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7199                data.create_operational_oq.pi_addr) != 24);
7200        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7201                data.create_operational_oq.num_elements) != 32);
7202        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7203                data.create_operational_oq.element_length) != 34);
7204        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7205                data.create_operational_oq.queue_protocol) != 36);
7206        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7207                data.create_operational_oq.int_msg_num) != 40);
7208        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7209                data.create_operational_oq.coalescing_count) != 42);
7210        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7211                data.create_operational_oq.min_coalescing_time) != 44);
7212        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7213                data.create_operational_oq.max_coalescing_time) != 48);
7214        BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7215                data.delete_operational_queue.queue_id) != 12);
7216        BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
7217        BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7218                data.create_operational_iq) != 64 - 11);
7219        BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7220                data.create_operational_oq) != 64 - 11);
7221        BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7222                data.delete_operational_queue) != 64 - 11);
7223
7224        BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7225                header.iu_type) != 0);
7226        BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7227                header.iu_length) != 2);
7228        BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7229                header.work_area) != 6);
7230        BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7231                request_id) != 8);
7232        BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7233                function_code) != 10);
7234        BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7235                status) != 11);
7236        BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7237                data.create_operational_iq.status_descriptor) != 12);
7238        BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7239                data.create_operational_iq.iq_pi_offset) != 16);
7240        BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7241                data.create_operational_oq.status_descriptor) != 12);
7242        BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7243                data.create_operational_oq.oq_ci_offset) != 16);
7244        BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
7245
7246        BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7247                header.iu_type) != 0);
7248        BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7249                header.iu_length) != 2);
7250        BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7251                header.response_queue_id) != 4);
7252        BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7253                header.work_area) != 6);
7254        BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7255                request_id) != 8);
7256        BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7257                nexus_id) != 10);
7258        BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7259                buffer_length) != 12);
7260        BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7261                lun_number) != 16);
7262        BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7263                protocol_specific) != 24);
7264        BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7265                error_index) != 27);
7266        BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7267                cdb) != 32);
7268        BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7269                sg_descriptors) != 64);
7270        BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
7271                PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
7272
7273        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7274                header.iu_type) != 0);
7275        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7276                header.iu_length) != 2);
7277        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7278                header.response_queue_id) != 4);
7279        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7280                header.work_area) != 6);
7281        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7282                request_id) != 8);
7283        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7284                nexus_id) != 12);
7285        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7286                buffer_length) != 16);
7287        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7288                data_encryption_key_index) != 22);
7289        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7290                encrypt_tweak_lower) != 24);
7291        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7292                encrypt_tweak_upper) != 28);
7293        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7294                cdb) != 32);
7295        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7296                error_index) != 48);
7297        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7298                num_sg_descriptors) != 50);
7299        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7300                cdb_length) != 51);
7301        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7302                lun_number) != 52);
7303        BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7304                sg_descriptors) != 64);
7305        BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
7306                PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
7307
7308        BUILD_BUG_ON(offsetof(struct pqi_io_response,
7309                header.iu_type) != 0);
7310        BUILD_BUG_ON(offsetof(struct pqi_io_response,
7311                header.iu_length) != 2);
7312        BUILD_BUG_ON(offsetof(struct pqi_io_response,
7313                request_id) != 8);
7314        BUILD_BUG_ON(offsetof(struct pqi_io_response,
7315                error_index) != 10);
7316
7317        BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7318                header.iu_type) != 0);
7319        BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7320                header.iu_length) != 2);
7321        BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7322                header.response_queue_id) != 4);
7323        BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7324                request_id) != 8);
7325        BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7326                data.report_event_configuration.buffer_length) != 12);
7327        BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7328                data.report_event_configuration.sg_descriptors) != 16);
7329        BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7330                data.set_event_configuration.global_event_oq_id) != 10);
7331        BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7332                data.set_event_configuration.buffer_length) != 12);
7333        BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7334                data.set_event_configuration.sg_descriptors) != 16);
7335
7336        BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
7337                max_inbound_iu_length) != 6);
7338        BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
7339                max_outbound_iu_length) != 14);
7340        BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
7341
7342        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7343                data_length) != 0);
7344        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7345                iq_arbitration_priority_support_bitmask) != 8);
7346        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7347                maximum_aw_a) != 9);
7348        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7349                maximum_aw_b) != 10);
7350        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7351                maximum_aw_c) != 11);
7352        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7353                max_inbound_queues) != 16);
7354        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7355                max_elements_per_iq) != 18);
7356        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7357                max_iq_element_length) != 24);
7358        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7359                min_iq_element_length) != 26);
7360        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7361                max_outbound_queues) != 30);
7362        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7363                max_elements_per_oq) != 32);
7364        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7365                intr_coalescing_time_granularity) != 34);
7366        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7367                max_oq_element_length) != 36);
7368        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7369                min_oq_element_length) != 38);
7370        BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7371                iu_layer_descriptors) != 64);
7372        BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
7373
7374        BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
7375                event_type) != 0);
7376        BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
7377                oq_id) != 2);
7378        BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
7379
7380        BUILD_BUG_ON(offsetof(struct pqi_event_config,
7381                num_event_descriptors) != 2);
7382        BUILD_BUG_ON(offsetof(struct pqi_event_config,
7383                descriptors) != 4);
7384
7385        BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS !=
7386                ARRAY_SIZE(pqi_supported_event_types));
7387
7388        BUILD_BUG_ON(offsetof(struct pqi_event_response,
7389                header.iu_type) != 0);
7390        BUILD_BUG_ON(offsetof(struct pqi_event_response,
7391                header.iu_length) != 2);
7392        BUILD_BUG_ON(offsetof(struct pqi_event_response,
7393                event_type) != 8);
7394        BUILD_BUG_ON(offsetof(struct pqi_event_response,
7395                event_id) != 10);
7396        BUILD_BUG_ON(offsetof(struct pqi_event_response,
7397                additional_event_id) != 12);
7398        BUILD_BUG_ON(offsetof(struct pqi_event_response,
7399                data) != 16);
7400        BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
7401
7402        BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7403                header.iu_type) != 0);
7404        BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7405                header.iu_length) != 2);
7406        BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7407                event_type) != 8);
7408        BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7409                event_id) != 10);
7410        BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7411                additional_event_id) != 12);
7412        BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
7413
7414        BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7415                header.iu_type) != 0);
7416        BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7417                header.iu_length) != 2);
7418        BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7419                request_id) != 8);
7420        BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7421                nexus_id) != 10);
7422        BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7423                lun_number) != 16);
7424        BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7425                protocol_specific) != 24);
7426        BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7427                outbound_queue_id_to_manage) != 26);
7428        BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7429                request_id_to_manage) != 28);
7430        BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7431                task_management_function) != 30);
7432        BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
7433
7434        BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7435                header.iu_type) != 0);
7436        BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7437                header.iu_length) != 2);
7438        BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7439                request_id) != 8);
7440        BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7441                nexus_id) != 10);
7442        BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7443                additional_response_info) != 12);
7444        BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7445                response_code) != 15);
7446        BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
7447
7448        BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7449                configured_logical_drive_count) != 0);
7450        BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7451                configuration_signature) != 1);
7452        BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7453                firmware_version) != 5);
7454        BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7455                extended_logical_unit_count) != 154);
7456        BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7457                firmware_build_number) != 190);
7458        BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7459                controller_mode) != 292);
7460
7461        BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7462                phys_bay_in_box) != 115);
7463        BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7464                device_type) != 120);
7465        BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7466                redundant_path_present_map) != 1736);
7467        BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7468                active_path_number) != 1738);
7469        BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7470                alternate_paths_phys_connector) != 1739);
7471        BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7472                alternate_paths_phys_box_on_port) != 1755);
7473        BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7474                current_queue_depth_limit) != 1796);
7475        BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560);
7476
7477        BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
7478        BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
7479        BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
7480                PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7481        BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
7482                PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7483        BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
7484        BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
7485                PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7486        BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
7487        BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
7488                PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7489
7490        BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
7491        BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >=
7492                PQI_MAX_OUTSTANDING_REQUESTS_KDUMP);
7493}
7494