linux/drivers/scsi/aacraid/aachba.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *      Adaptec AAC series RAID controller driver
   4 *      (c) Copyright 2001 Red Hat Inc.
   5 *
   6 * based on the old aacraid driver that is..
   7 * Adaptec aacraid device driver for Linux.
   8 *
   9 * Copyright (c) 2000-2010 Adaptec, Inc.
  10 *               2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
  11 *               2016-2017 Microsemi Corp. (aacraid@microsemi.com)
  12 *
  13 * Module Name:
  14 *  aachba.c
  15 *
  16 * Abstract: Contains Interfaces to manage IOs.
  17 */
  18
  19#include <linux/kernel.h>
  20#include <linux/init.h>
  21#include <linux/types.h>
  22#include <linux/pci.h>
  23#include <linux/spinlock.h>
  24#include <linux/slab.h>
  25#include <linux/completion.h>
  26#include <linux/blkdev.h>
  27#include <linux/uaccess.h>
  28#include <linux/highmem.h> /* For flush_kernel_dcache_page */
  29#include <linux/module.h>
  30
  31#include <asm/unaligned.h>
  32
  33#include <scsi/scsi.h>
  34#include <scsi/scsi_cmnd.h>
  35#include <scsi/scsi_device.h>
  36#include <scsi/scsi_host.h>
  37
  38#include "aacraid.h"
  39
  40/* values for inqd_pdt: Peripheral device type in plain English */
  41#define INQD_PDT_DA     0x00    /* Direct-access (DISK) device */
  42#define INQD_PDT_PROC   0x03    /* Processor device */
  43#define INQD_PDT_CHNGR  0x08    /* Changer (jukebox, scsi2) */
  44#define INQD_PDT_COMM   0x09    /* Communication device (scsi2) */
  45#define INQD_PDT_NOLUN2 0x1f    /* Unknown Device (scsi2) */
  46#define INQD_PDT_NOLUN  0x7f    /* Logical Unit Not Present */
  47
  48#define INQD_PDT_DMASK  0x1F    /* Peripheral Device Type Mask */
  49#define INQD_PDT_QMASK  0xE0    /* Peripheral Device Qualifer Mask */
  50
  51/*
  52 *      Sense codes
  53 */
  54
  55#define SENCODE_NO_SENSE                        0x00
  56#define SENCODE_END_OF_DATA                     0x00
  57#define SENCODE_BECOMING_READY                  0x04
  58#define SENCODE_INIT_CMD_REQUIRED               0x04
  59#define SENCODE_UNRECOVERED_READ_ERROR          0x11
  60#define SENCODE_PARAM_LIST_LENGTH_ERROR         0x1A
  61#define SENCODE_INVALID_COMMAND                 0x20
  62#define SENCODE_LBA_OUT_OF_RANGE                0x21
  63#define SENCODE_INVALID_CDB_FIELD               0x24
  64#define SENCODE_LUN_NOT_SUPPORTED               0x25
  65#define SENCODE_INVALID_PARAM_FIELD             0x26
  66#define SENCODE_PARAM_NOT_SUPPORTED             0x26
  67#define SENCODE_PARAM_VALUE_INVALID             0x26
  68#define SENCODE_RESET_OCCURRED                  0x29
  69#define SENCODE_LUN_NOT_SELF_CONFIGURED_YET     0x3E
  70#define SENCODE_INQUIRY_DATA_CHANGED            0x3F
  71#define SENCODE_SAVING_PARAMS_NOT_SUPPORTED     0x39
  72#define SENCODE_DIAGNOSTIC_FAILURE              0x40
  73#define SENCODE_INTERNAL_TARGET_FAILURE         0x44
  74#define SENCODE_INVALID_MESSAGE_ERROR           0x49
  75#define SENCODE_LUN_FAILED_SELF_CONFIG          0x4c
  76#define SENCODE_OVERLAPPED_COMMAND              0x4E
  77
  78/*
  79 *      Additional sense codes
  80 */
  81
  82#define ASENCODE_NO_SENSE                       0x00
  83#define ASENCODE_END_OF_DATA                    0x05
  84#define ASENCODE_BECOMING_READY                 0x01
  85#define ASENCODE_INIT_CMD_REQUIRED              0x02
  86#define ASENCODE_PARAM_LIST_LENGTH_ERROR        0x00
  87#define ASENCODE_INVALID_COMMAND                0x00
  88#define ASENCODE_LBA_OUT_OF_RANGE               0x00
  89#define ASENCODE_INVALID_CDB_FIELD              0x00
  90#define ASENCODE_LUN_NOT_SUPPORTED              0x00
  91#define ASENCODE_INVALID_PARAM_FIELD            0x00
  92#define ASENCODE_PARAM_NOT_SUPPORTED            0x01
  93#define ASENCODE_PARAM_VALUE_INVALID            0x02
  94#define ASENCODE_RESET_OCCURRED                 0x00
  95#define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET    0x00
  96#define ASENCODE_INQUIRY_DATA_CHANGED           0x03
  97#define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED    0x00
  98#define ASENCODE_DIAGNOSTIC_FAILURE             0x80
  99#define ASENCODE_INTERNAL_TARGET_FAILURE        0x00
 100#define ASENCODE_INVALID_MESSAGE_ERROR          0x00
 101#define ASENCODE_LUN_FAILED_SELF_CONFIG         0x00
 102#define ASENCODE_OVERLAPPED_COMMAND             0x00
 103
 104#define BYTE0(x) (unsigned char)(x)
 105#define BYTE1(x) (unsigned char)((x) >> 8)
 106#define BYTE2(x) (unsigned char)((x) >> 16)
 107#define BYTE3(x) (unsigned char)((x) >> 24)
 108
 109/* MODE_SENSE data format */
 110typedef struct {
 111        struct {
 112                u8      data_length;
 113                u8      med_type;
 114                u8      dev_par;
 115                u8      bd_length;
 116        } __attribute__((packed)) hd;
 117        struct {
 118                u8      dens_code;
 119                u8      block_count[3];
 120                u8      reserved;
 121                u8      block_length[3];
 122        } __attribute__((packed)) bd;
 123                u8      mpc_buf[3];
 124} __attribute__((packed)) aac_modep_data;
 125
 126/* MODE_SENSE_10 data format */
 127typedef struct {
 128        struct {
 129                u8      data_length[2];
 130                u8      med_type;
 131                u8      dev_par;
 132                u8      rsrvd[2];
 133                u8      bd_length[2];
 134        } __attribute__((packed)) hd;
 135        struct {
 136                u8      dens_code;
 137                u8      block_count[3];
 138                u8      reserved;
 139                u8      block_length[3];
 140        } __attribute__((packed)) bd;
 141                u8      mpc_buf[3];
 142} __attribute__((packed)) aac_modep10_data;
 143
 144/*------------------------------------------------------------------------------
 145 *              S T R U C T S / T Y P E D E F S
 146 *----------------------------------------------------------------------------*/
 147/* SCSI inquiry data */
 148struct inquiry_data {
 149        u8 inqd_pdt;    /* Peripheral qualifier | Peripheral Device Type */
 150        u8 inqd_dtq;    /* RMB | Device Type Qualifier */
 151        u8 inqd_ver;    /* ISO version | ECMA version | ANSI-approved version */
 152        u8 inqd_rdf;    /* AENC | TrmIOP | Response data format */
 153        u8 inqd_len;    /* Additional length (n-4) */
 154        u8 inqd_pad1[2];/* Reserved - must be zero */
 155        u8 inqd_pad2;   /* RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
 156        u8 inqd_vid[8]; /* Vendor ID */
 157        u8 inqd_pid[16];/* Product ID */
 158        u8 inqd_prl[4]; /* Product Revision Level */
 159};
 160
 161/* Added for VPD 0x83 */
 162struct  tvpd_id_descriptor_type_1 {
 163        u8 codeset:4;           /* VPD_CODE_SET */
 164        u8 reserved:4;
 165        u8 identifiertype:4;    /* VPD_IDENTIFIER_TYPE */
 166        u8 reserved2:4;
 167        u8 reserved3;
 168        u8 identifierlength;
 169        u8 venid[8];
 170        u8 productid[16];
 171        u8 serialnumber[8];     /* SN in ASCII */
 172
 173};
 174
 175struct tvpd_id_descriptor_type_2 {
 176        u8 codeset:4;           /* VPD_CODE_SET */
 177        u8 reserved:4;
 178        u8 identifiertype:4;    /* VPD_IDENTIFIER_TYPE */
 179        u8 reserved2:4;
 180        u8 reserved3;
 181        u8 identifierlength;
 182        struct teu64id {
 183                u32 Serial;
 184                 /* The serial number supposed to be 40 bits,
 185                  * bit we only support 32, so make the last byte zero. */
 186                u8 reserved;
 187                u8 venid[3];
 188        } eu64id;
 189
 190};
 191
 192struct tvpd_id_descriptor_type_3 {
 193        u8 codeset : 4;          /* VPD_CODE_SET */
 194        u8 reserved : 4;
 195        u8 identifiertype : 4;   /* VPD_IDENTIFIER_TYPE */
 196        u8 reserved2 : 4;
 197        u8 reserved3;
 198        u8 identifierlength;
 199        u8 Identifier[16];
 200};
 201
 202struct tvpd_page83 {
 203        u8 DeviceType:5;
 204        u8 DeviceTypeQualifier:3;
 205        u8 PageCode;
 206        u8 reserved;
 207        u8 PageLength;
 208        struct tvpd_id_descriptor_type_1 type1;
 209        struct tvpd_id_descriptor_type_2 type2;
 210        struct tvpd_id_descriptor_type_3 type3;
 211};
 212
 213/*
 214 *              M O D U L E   G L O B A L S
 215 */
 216
 217static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *sgmap);
 218static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg);
 219static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg);
 220static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
 221                                struct aac_raw_io2 *rio2, int sg_max);
 222static long aac_build_sghba(struct scsi_cmnd *scsicmd,
 223                                struct aac_hba_cmd_req *hbacmd,
 224                                int sg_max, u64 sg_address);
 225static int aac_convert_sgraw2(struct aac_raw_io2 *rio2,
 226                                int pages, int nseg, int nseg_new);
 227static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
 228static int aac_send_hba_fib(struct scsi_cmnd *scsicmd);
 229#ifdef AAC_DETAILED_STATUS_INFO
 230static char *aac_get_status_string(u32 status);
 231#endif
 232
 233/*
 234 *      Non dasd selection is handled entirely in aachba now
 235 */
 236
 237static int nondasd = -1;
 238static int aac_cache = 2;       /* WCE=0 to avoid performance problems */
 239static int dacmode = -1;
 240int aac_msi;
 241int aac_commit = -1;
 242int startup_timeout = 180;
 243int aif_timeout = 120;
 244int aac_sync_mode;  /* Only Sync. transfer - disabled */
 245static int aac_convert_sgl = 1; /* convert non-conformable s/g list - enabled */
 246
 247module_param(aac_sync_mode, int, S_IRUGO|S_IWUSR);
 248MODULE_PARM_DESC(aac_sync_mode, "Force sync. transfer mode"
 249        " 0=off, 1=on");
 250module_param(aac_convert_sgl, int, S_IRUGO|S_IWUSR);
 251MODULE_PARM_DESC(aac_convert_sgl, "Convert non-conformable s/g list"
 252        " 0=off, 1=on");
 253module_param(nondasd, int, S_IRUGO|S_IWUSR);
 254MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices."
 255        " 0=off, 1=on");
 256module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
 257MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n"
 258        "\tbit 0 - Disable FUA in WRITE SCSI commands\n"
 259        "\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n"
 260        "\tbit 2 - Disable only if Battery is protecting Cache");
 261module_param(dacmode, int, S_IRUGO|S_IWUSR);
 262MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC."
 263        " 0=off, 1=on");
 264module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
 265MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the"
 266        " adapter for foreign arrays.\n"
 267        "This is typically needed in systems that do not have a BIOS."
 268        " 0=off, 1=on");
 269module_param_named(msi, aac_msi, int, S_IRUGO|S_IWUSR);
 270MODULE_PARM_DESC(msi, "IRQ handling."
 271        " 0=PIC(default), 1=MSI, 2=MSI-X)");
 272module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
 273MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for"
 274        " adapter to have it's kernel up and\n"
 275        "running. This is typically adjusted for large systems that do not"
 276        " have a BIOS.");
 277module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
 278MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for"
 279        " applications to pick up AIFs before\n"
 280        "deregistering them. This is typically adjusted for heavily burdened"
 281        " systems.");
 282
 283int aac_fib_dump;
 284module_param(aac_fib_dump, int, 0644);
 285MODULE_PARM_DESC(aac_fib_dump, "Dump controller fibs prior to IOP_RESET 0=off, 1=on");
 286
 287int numacb = -1;
 288module_param(numacb, int, S_IRUGO|S_IWUSR);
 289MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control"
 290        " blocks (FIB) allocated. Valid values are 512 and down. Default is"
 291        " to use suggestion from Firmware.");
 292
 293static int acbsize = -1;
 294module_param(acbsize, int, S_IRUGO|S_IWUSR);
 295MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB)"
 296        " size. Valid values are 512, 2048, 4096 and 8192. Default is to use"
 297        " suggestion from Firmware.");
 298
 299int update_interval = 30 * 60;
 300module_param(update_interval, int, S_IRUGO|S_IWUSR);
 301MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync"
 302        " updates issued to adapter.");
 303
 304int check_interval = 60;
 305module_param(check_interval, int, S_IRUGO|S_IWUSR);
 306MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health"
 307        " checks.");
 308
 309int aac_check_reset = 1;
 310module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
 311MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the"
 312        " adapter. a value of -1 forces the reset to adapters programmed to"
 313        " ignore it.");
 314
 315int expose_physicals = -1;
 316module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
 317MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays."
 318        " -1=protect 0=off, 1=on");
 319
 320int aac_reset_devices;
 321module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
 322MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
 323
 324static int aac_wwn = 1;
 325module_param_named(wwn, aac_wwn, int, S_IRUGO|S_IWUSR);
 326MODULE_PARM_DESC(wwn, "Select a WWN type for the arrays:\n"
 327        "\t0 - Disable\n"
 328        "\t1 - Array Meta Data Signature (default)\n"
 329        "\t2 - Adapter Serial Number");
 330
 331
 332static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
 333                struct fib *fibptr) {
 334        struct scsi_device *device;
 335
 336        if (unlikely(!scsicmd || !scsicmd->scsi_done)) {
 337                dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
 338                aac_fib_complete(fibptr);
 339                return 0;
 340        }
 341        scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
 342        device = scsicmd->device;
 343        if (unlikely(!device)) {
 344                dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
 345                aac_fib_complete(fibptr);
 346                return 0;
 347        }
 348        return 1;
 349}
 350
 351/**
 352 *      aac_get_config_status   -       check the adapter configuration
 353 *      @dev: aac driver data
 354 *      @commit_flag: force sending CT_COMMIT_CONFIG
 355 *
 356 *      Query config status, and commit the configuration if needed.
 357 */
 358int aac_get_config_status(struct aac_dev *dev, int commit_flag)
 359{
 360        int status = 0;
 361        struct fib * fibptr;
 362
 363        if (!(fibptr = aac_fib_alloc(dev)))
 364                return -ENOMEM;
 365
 366        aac_fib_init(fibptr);
 367        {
 368                struct aac_get_config_status *dinfo;
 369                dinfo = (struct aac_get_config_status *) fib_data(fibptr);
 370
 371                dinfo->command = cpu_to_le32(VM_ContainerConfig);
 372                dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
 373                dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
 374        }
 375
 376        status = aac_fib_send(ContainerCommand,
 377                            fibptr,
 378                            sizeof (struct aac_get_config_status),
 379                            FsaNormal,
 380                            1, 1,
 381                            NULL, NULL);
 382        if (status < 0) {
 383                printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
 384        } else {
 385                struct aac_get_config_status_resp *reply
 386                  = (struct aac_get_config_status_resp *) fib_data(fibptr);
 387                dprintk((KERN_WARNING
 388                  "aac_get_config_status: response=%d status=%d action=%d\n",
 389                  le32_to_cpu(reply->response),
 390                  le32_to_cpu(reply->status),
 391                  le32_to_cpu(reply->data.action)));
 392                if ((le32_to_cpu(reply->response) != ST_OK) ||
 393                     (le32_to_cpu(reply->status) != CT_OK) ||
 394                     (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
 395                        printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
 396                        status = -EINVAL;
 397                }
 398        }
 399        /* Do not set XferState to zero unless receives a response from F/W */
 400        if (status >= 0)
 401                aac_fib_complete(fibptr);
 402
 403        /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
 404        if (status >= 0) {
 405                if ((aac_commit == 1) || commit_flag) {
 406                        struct aac_commit_config * dinfo;
 407                        aac_fib_init(fibptr);
 408                        dinfo = (struct aac_commit_config *) fib_data(fibptr);
 409
 410                        dinfo->command = cpu_to_le32(VM_ContainerConfig);
 411                        dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
 412
 413                        status = aac_fib_send(ContainerCommand,
 414                                    fibptr,
 415                                    sizeof (struct aac_commit_config),
 416                                    FsaNormal,
 417                                    1, 1,
 418                                    NULL, NULL);
 419                        /* Do not set XferState to zero unless
 420                         * receives a response from F/W */
 421                        if (status >= 0)
 422                                aac_fib_complete(fibptr);
 423                } else if (aac_commit == 0) {
 424                        printk(KERN_WARNING
 425                          "aac_get_config_status: Foreign device configurations are being ignored\n");
 426                }
 427        }
 428        /* FIB should be freed only after getting the response from the F/W */
 429        if (status != -ERESTARTSYS)
 430                aac_fib_free(fibptr);
 431        return status;
 432}
 433
 434static void aac_expose_phy_device(struct scsi_cmnd *scsicmd)
 435{
 436        char inq_data;
 437        scsi_sg_copy_to_buffer(scsicmd,  &inq_data, sizeof(inq_data));
 438        if ((inq_data & 0x20) && (inq_data & 0x1f) == TYPE_DISK) {
 439                inq_data &= 0xdf;
 440                scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
 441        }
 442}
 443
 444/**
 445 *      aac_get_containers      -       list containers
 446 *      @dev: aac driver data
 447 *
 448 *      Make a list of all containers on this controller
 449 */
 450int aac_get_containers(struct aac_dev *dev)
 451{
 452        struct fsa_dev_info *fsa_dev_ptr;
 453        u32 index;
 454        int status = 0;
 455        struct fib * fibptr;
 456        struct aac_get_container_count *dinfo;
 457        struct aac_get_container_count_resp *dresp;
 458        int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
 459
 460        if (!(fibptr = aac_fib_alloc(dev)))
 461                return -ENOMEM;
 462
 463        aac_fib_init(fibptr);
 464        dinfo = (struct aac_get_container_count *) fib_data(fibptr);
 465        dinfo->command = cpu_to_le32(VM_ContainerConfig);
 466        dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
 467
 468        status = aac_fib_send(ContainerCommand,
 469                    fibptr,
 470                    sizeof (struct aac_get_container_count),
 471                    FsaNormal,
 472                    1, 1,
 473                    NULL, NULL);
 474        if (status >= 0) {
 475                dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
 476                maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
 477                if (fibptr->dev->supplement_adapter_info.supported_options2 &
 478                    AAC_OPTION_SUPPORTED_240_VOLUMES) {
 479                        maximum_num_containers =
 480                                le32_to_cpu(dresp->MaxSimpleVolumes);
 481                }
 482                aac_fib_complete(fibptr);
 483        }
 484        /* FIB should be freed only after getting the response from the F/W */
 485        if (status != -ERESTARTSYS)
 486                aac_fib_free(fibptr);
 487
 488        if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
 489                maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
 490        if (dev->fsa_dev == NULL ||
 491                dev->maximum_num_containers != maximum_num_containers) {
 492
 493                fsa_dev_ptr = dev->fsa_dev;
 494
 495                dev->fsa_dev = kcalloc(maximum_num_containers,
 496                                        sizeof(*fsa_dev_ptr), GFP_KERNEL);
 497
 498                kfree(fsa_dev_ptr);
 499                fsa_dev_ptr = NULL;
 500
 501
 502                if (!dev->fsa_dev)
 503                        return -ENOMEM;
 504
 505                dev->maximum_num_containers = maximum_num_containers;
 506        }
 507        for (index = 0; index < dev->maximum_num_containers; index++) {
 508                dev->fsa_dev[index].devname[0] = '\0';
 509                dev->fsa_dev[index].valid = 0;
 510
 511                status = aac_probe_container(dev, index);
 512
 513                if (status < 0) {
 514                        printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
 515                        break;
 516                }
 517        }
 518        return status;
 519}
 520
 521static void get_container_name_callback(void *context, struct fib * fibptr)
 522{
 523        struct aac_get_name_resp * get_name_reply;
 524        struct scsi_cmnd * scsicmd;
 525
 526        scsicmd = (struct scsi_cmnd *) context;
 527
 528        if (!aac_valid_context(scsicmd, fibptr))
 529                return;
 530
 531        dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
 532        BUG_ON(fibptr == NULL);
 533
 534        get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
 535        /* Failure is irrelevant, using default value instead */
 536        if ((le32_to_cpu(get_name_reply->status) == CT_OK)
 537         && (get_name_reply->data[0] != '\0')) {
 538                char *sp = get_name_reply->data;
 539                int data_size = sizeof_field(struct aac_get_name_resp, data);
 540
 541                sp[data_size - 1] = '\0';
 542                while (*sp == ' ')
 543                        ++sp;
 544                if (*sp) {
 545                        struct inquiry_data inq;
 546                        char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
 547                        int count = sizeof(d);
 548                        char *dp = d;
 549                        do {
 550                                *dp++ = (*sp) ? *sp++ : ' ';
 551                        } while (--count > 0);
 552
 553                        scsi_sg_copy_to_buffer(scsicmd, &inq, sizeof(inq));
 554                        memcpy(inq.inqd_pid, d, sizeof(d));
 555                        scsi_sg_copy_from_buffer(scsicmd, &inq, sizeof(inq));
 556                }
 557        }
 558
 559        scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
 560
 561        aac_fib_complete(fibptr);
 562        scsicmd->scsi_done(scsicmd);
 563}
 564
 565/*
 566 *      aac_get_container_name  -       get container name, none blocking.
 567 */
 568static int aac_get_container_name(struct scsi_cmnd * scsicmd)
 569{
 570        int status;
 571        int data_size;
 572        struct aac_get_name *dinfo;
 573        struct fib * cmd_fibcontext;
 574        struct aac_dev * dev;
 575
 576        dev = (struct aac_dev *)scsicmd->device->host->hostdata;
 577
 578        data_size = sizeof_field(struct aac_get_name_resp, data);
 579
 580        cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
 581
 582        aac_fib_init(cmd_fibcontext);
 583        dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
 584        scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
 585
 586        dinfo->command = cpu_to_le32(VM_ContainerConfig);
 587        dinfo->type = cpu_to_le32(CT_READ_NAME);
 588        dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
 589        dinfo->count = cpu_to_le32(data_size - 1);
 590
 591        status = aac_fib_send(ContainerCommand,
 592                  cmd_fibcontext,
 593                  sizeof(struct aac_get_name_resp),
 594                  FsaNormal,
 595                  0, 1,
 596                  (fib_callback)get_container_name_callback,
 597                  (void *) scsicmd);
 598
 599        /*
 600         *      Check that the command queued to the controller
 601         */
 602        if (status == -EINPROGRESS)
 603                return 0;
 604
 605        printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
 606        aac_fib_complete(cmd_fibcontext);
 607        return -1;
 608}
 609
 610static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
 611{
 612        struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
 613
 614        if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
 615                return aac_scsi_cmd(scsicmd);
 616
 617        scsicmd->result = DID_NO_CONNECT << 16;
 618        scsicmd->scsi_done(scsicmd);
 619        return 0;
 620}
 621
 622static void _aac_probe_container2(void * context, struct fib * fibptr)
 623{
 624        struct fsa_dev_info *fsa_dev_ptr;
 625        int (*callback)(struct scsi_cmnd *);
 626        struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
 627        int i;
 628
 629
 630        if (!aac_valid_context(scsicmd, fibptr))
 631                return;
 632
 633        scsicmd->SCp.Status = 0;
 634        fsa_dev_ptr = fibptr->dev->fsa_dev;
 635        if (fsa_dev_ptr) {
 636                struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
 637                __le32 sup_options2;
 638
 639                fsa_dev_ptr += scmd_id(scsicmd);
 640                sup_options2 =
 641                        fibptr->dev->supplement_adapter_info.supported_options2;
 642
 643                if ((le32_to_cpu(dresp->status) == ST_OK) &&
 644                    (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
 645                    (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
 646                        if (!(sup_options2 & AAC_OPTION_VARIABLE_BLOCK_SIZE)) {
 647                                dresp->mnt[0].fileinfo.bdevinfo.block_size = 0x200;
 648                                fsa_dev_ptr->block_size = 0x200;
 649                        } else {
 650                                fsa_dev_ptr->block_size =
 651                                        le32_to_cpu(dresp->mnt[0].fileinfo.bdevinfo.block_size);
 652                        }
 653                        for (i = 0; i < 16; i++)
 654                                fsa_dev_ptr->identifier[i] =
 655                                        dresp->mnt[0].fileinfo.bdevinfo
 656                                                                .identifier[i];
 657                        fsa_dev_ptr->valid = 1;
 658                        /* sense_key holds the current state of the spin-up */
 659                        if (dresp->mnt[0].state & cpu_to_le32(FSCS_NOT_READY))
 660                                fsa_dev_ptr->sense_data.sense_key = NOT_READY;
 661                        else if (fsa_dev_ptr->sense_data.sense_key == NOT_READY)
 662                                fsa_dev_ptr->sense_data.sense_key = NO_SENSE;
 663                        fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
 664                        fsa_dev_ptr->size
 665                          = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
 666                            (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
 667                        fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
 668                }
 669                if ((fsa_dev_ptr->valid & 1) == 0)
 670                        fsa_dev_ptr->valid = 0;
 671                scsicmd->SCp.Status = le32_to_cpu(dresp->count);
 672        }
 673        aac_fib_complete(fibptr);
 674        aac_fib_free(fibptr);
 675        callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
 676        scsicmd->SCp.ptr = NULL;
 677        (*callback)(scsicmd);
 678        return;
 679}
 680
 681static void _aac_probe_container1(void * context, struct fib * fibptr)
 682{
 683        struct scsi_cmnd * scsicmd;
 684        struct aac_mount * dresp;
 685        struct aac_query_mount *dinfo;
 686        int status;
 687
 688        dresp = (struct aac_mount *) fib_data(fibptr);
 689        if (!aac_supports_2T(fibptr->dev)) {
 690                dresp->mnt[0].capacityhigh = 0;
 691                if ((le32_to_cpu(dresp->status) == ST_OK) &&
 692                        (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
 693                        _aac_probe_container2(context, fibptr);
 694                        return;
 695                }
 696        }
 697        scsicmd = (struct scsi_cmnd *) context;
 698
 699        if (!aac_valid_context(scsicmd, fibptr))
 700                return;
 701
 702        aac_fib_init(fibptr);
 703
 704        dinfo = (struct aac_query_mount *)fib_data(fibptr);
 705
 706        if (fibptr->dev->supplement_adapter_info.supported_options2 &
 707            AAC_OPTION_VARIABLE_BLOCK_SIZE)
 708                dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
 709        else
 710                dinfo->command = cpu_to_le32(VM_NameServe64);
 711
 712        dinfo->count = cpu_to_le32(scmd_id(scsicmd));
 713        dinfo->type = cpu_to_le32(FT_FILESYS);
 714        scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
 715
 716        status = aac_fib_send(ContainerCommand,
 717                          fibptr,
 718                          sizeof(struct aac_query_mount),
 719                          FsaNormal,
 720                          0, 1,
 721                          _aac_probe_container2,
 722                          (void *) scsicmd);
 723        /*
 724         *      Check that the command queued to the controller
 725         */
 726        if (status < 0 && status != -EINPROGRESS) {
 727                /* Inherit results from VM_NameServe, if any */
 728                dresp->status = cpu_to_le32(ST_OK);
 729                _aac_probe_container2(context, fibptr);
 730        }
 731}
 732
 733static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
 734{
 735        struct fib * fibptr;
 736        int status = -ENOMEM;
 737
 738        if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
 739                struct aac_query_mount *dinfo;
 740
 741                aac_fib_init(fibptr);
 742
 743                dinfo = (struct aac_query_mount *)fib_data(fibptr);
 744
 745                if (fibptr->dev->supplement_adapter_info.supported_options2 &
 746                    AAC_OPTION_VARIABLE_BLOCK_SIZE)
 747                        dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
 748                else
 749                        dinfo->command = cpu_to_le32(VM_NameServe);
 750
 751                dinfo->count = cpu_to_le32(scmd_id(scsicmd));
 752                dinfo->type = cpu_to_le32(FT_FILESYS);
 753                scsicmd->SCp.ptr = (char *)callback;
 754                scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
 755
 756                status = aac_fib_send(ContainerCommand,
 757                          fibptr,
 758                          sizeof(struct aac_query_mount),
 759                          FsaNormal,
 760                          0, 1,
 761                          _aac_probe_container1,
 762                          (void *) scsicmd);
 763                /*
 764                 *      Check that the command queued to the controller
 765                 */
 766                if (status == -EINPROGRESS)
 767                        return 0;
 768
 769                if (status < 0) {
 770                        scsicmd->SCp.ptr = NULL;
 771                        aac_fib_complete(fibptr);
 772                        aac_fib_free(fibptr);
 773                }
 774        }
 775        if (status < 0) {
 776                struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
 777                if (fsa_dev_ptr) {
 778                        fsa_dev_ptr += scmd_id(scsicmd);
 779                        if ((fsa_dev_ptr->valid & 1) == 0) {
 780                                fsa_dev_ptr->valid = 0;
 781                                return (*callback)(scsicmd);
 782                        }
 783                }
 784        }
 785        return status;
 786}
 787
 788/**
 789 *      aac_probe_container_callback1   -       query a logical volume
 790 *      @scsicmd: the scsi command block
 791 *
 792 *      Queries the controller about the given volume. The volume information
 793 *      is updated in the struct fsa_dev_info structure rather than returned.
 794 */
 795static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
 796{
 797        scsicmd->device = NULL;
 798        return 0;
 799}
 800
 801static void aac_probe_container_scsi_done(struct scsi_cmnd *scsi_cmnd)
 802{
 803        aac_probe_container_callback1(scsi_cmnd);
 804}
 805
 806int aac_probe_container(struct aac_dev *dev, int cid)
 807{
 808        struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
 809        struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
 810        int status;
 811
 812        if (!scsicmd || !scsidev) {
 813                kfree(scsicmd);
 814                kfree(scsidev);
 815                return -ENOMEM;
 816        }
 817        scsicmd->scsi_done = aac_probe_container_scsi_done;
 818
 819        scsicmd->device = scsidev;
 820        scsidev->sdev_state = 0;
 821        scsidev->id = cid;
 822        scsidev->host = dev->scsi_host_ptr;
 823
 824        if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
 825                while (scsicmd->device == scsidev)
 826                        schedule();
 827        kfree(scsidev);
 828        status = scsicmd->SCp.Status;
 829        kfree(scsicmd);
 830        return status;
 831}
 832
 833/* Local Structure to set SCSI inquiry data strings */
 834struct scsi_inq {
 835        char vid[8];         /* Vendor ID */
 836        char pid[16];        /* Product ID */
 837        char prl[4];         /* Product Revision Level */
 838};
 839
 840/**
 841 *      inqstrcpy       -       string merge
 842 *      @a:     string to copy from
 843 *      @b:     string to copy to
 844 *
 845 *      Copy a String from one location to another
 846 *      without copying \0
 847 */
 848
 849static void inqstrcpy(char *a, char *b)
 850{
 851
 852        while (*a != (char)0)
 853                *b++ = *a++;
 854}
 855
 856static char *container_types[] = {
 857        "None",
 858        "Volume",
 859        "Mirror",
 860        "Stripe",
 861        "RAID5",
 862        "SSRW",
 863        "SSRO",
 864        "Morph",
 865        "Legacy",
 866        "RAID4",
 867        "RAID10",
 868        "RAID00",
 869        "V-MIRRORS",
 870        "PSEUDO R4",
 871        "RAID50",
 872        "RAID5D",
 873        "RAID5D0",
 874        "RAID1E",
 875        "RAID6",
 876        "RAID60",
 877        "Unknown"
 878};
 879
 880char * get_container_type(unsigned tindex)
 881{
 882        if (tindex >= ARRAY_SIZE(container_types))
 883                tindex = ARRAY_SIZE(container_types) - 1;
 884        return container_types[tindex];
 885}
 886
 887/* Function: setinqstr
 888 *
 889 * Arguments: [1] pointer to void [1] int
 890 *
 891 * Purpose: Sets SCSI inquiry data strings for vendor, product
 892 * and revision level. Allows strings to be set in platform dependent
 893 * files instead of in OS dependent driver source.
 894 */
 895
 896static void setinqstr(struct aac_dev *dev, void *data, int tindex)
 897{
 898        struct scsi_inq *str;
 899        struct aac_supplement_adapter_info *sup_adap_info;
 900
 901        sup_adap_info = &dev->supplement_adapter_info;
 902        str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
 903        memset(str, ' ', sizeof(*str));
 904
 905        if (sup_adap_info->adapter_type_text[0]) {
 906                int c;
 907                char *cp;
 908                char *cname = kmemdup(sup_adap_info->adapter_type_text,
 909                                sizeof(sup_adap_info->adapter_type_text),
 910                                                                GFP_ATOMIC);
 911                if (!cname)
 912                        return;
 913
 914                cp = cname;
 915                if ((cp[0] == 'A') && (cp[1] == 'O') && (cp[2] == 'C'))
 916                        inqstrcpy("SMC", str->vid);
 917                else {
 918                        c = sizeof(str->vid);
 919                        while (*cp && *cp != ' ' && --c)
 920                                ++cp;
 921                        c = *cp;
 922                        *cp = '\0';
 923                        inqstrcpy(cname, str->vid);
 924                        *cp = c;
 925                        while (*cp && *cp != ' ')
 926                                ++cp;
 927                }
 928                while (*cp == ' ')
 929                        ++cp;
 930                /* last six chars reserved for vol type */
 931                if (strlen(cp) > sizeof(str->pid))
 932                        cp[sizeof(str->pid)] = '\0';
 933                inqstrcpy (cp, str->pid);
 934
 935                kfree(cname);
 936        } else {
 937                struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
 938
 939                inqstrcpy (mp->vname, str->vid);
 940                /* last six chars reserved for vol type */
 941                inqstrcpy (mp->model, str->pid);
 942        }
 943
 944        if (tindex < ARRAY_SIZE(container_types)){
 945                char *findit = str->pid;
 946
 947                for ( ; *findit != ' '; findit++); /* walk till we find a space */
 948                /* RAID is superfluous in the context of a RAID device */
 949                if (memcmp(findit-4, "RAID", 4) == 0)
 950                        *(findit -= 4) = ' ';
 951                if (((findit - str->pid) + strlen(container_types[tindex]))
 952                 < (sizeof(str->pid) + sizeof(str->prl)))
 953                        inqstrcpy (container_types[tindex], findit + 1);
 954        }
 955        inqstrcpy ("V1.0", str->prl);
 956}
 957
 958static void build_vpd83_type3(struct tvpd_page83 *vpdpage83data,
 959                struct aac_dev *dev, struct scsi_cmnd *scsicmd)
 960{
 961        int container;
 962
 963        vpdpage83data->type3.codeset = 1;
 964        vpdpage83data->type3.identifiertype = 3;
 965        vpdpage83data->type3.identifierlength = sizeof(vpdpage83data->type3)
 966                        - 4;
 967
 968        for (container = 0; container < dev->maximum_num_containers;
 969                        container++) {
 970
 971                if (scmd_id(scsicmd) == container) {
 972                        memcpy(vpdpage83data->type3.Identifier,
 973                                        dev->fsa_dev[container].identifier,
 974                                        16);
 975                        break;
 976                }
 977        }
 978}
 979
 980static void get_container_serial_callback(void *context, struct fib * fibptr)
 981{
 982        struct aac_get_serial_resp * get_serial_reply;
 983        struct scsi_cmnd * scsicmd;
 984
 985        BUG_ON(fibptr == NULL);
 986
 987        scsicmd = (struct scsi_cmnd *) context;
 988        if (!aac_valid_context(scsicmd, fibptr))
 989                return;
 990
 991        get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
 992        /* Failure is irrelevant, using default value instead */
 993        if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
 994                /*Check to see if it's for VPD 0x83 or 0x80 */
 995                if (scsicmd->cmnd[2] == 0x83) {
 996                        /* vpd page 0x83 - Device Identification Page */
 997                        struct aac_dev *dev;
 998                        int i;
 999                        struct tvpd_page83 vpdpage83data;
1000
1001                        dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1002
1003                        memset(((u8 *)&vpdpage83data), 0,
1004                               sizeof(vpdpage83data));
1005
1006                        /* DIRECT_ACCESS_DEVIC */
1007                        vpdpage83data.DeviceType = 0;
1008                        /* DEVICE_CONNECTED */
1009                        vpdpage83data.DeviceTypeQualifier = 0;
1010                        /* VPD_DEVICE_IDENTIFIERS */
1011                        vpdpage83data.PageCode = 0x83;
1012                        vpdpage83data.reserved = 0;
1013                        vpdpage83data.PageLength =
1014                                sizeof(vpdpage83data.type1) +
1015                                sizeof(vpdpage83data.type2);
1016
1017                        /* VPD 83 Type 3 is not supported for ARC */
1018                        if (dev->sa_firmware)
1019                                vpdpage83data.PageLength +=
1020                                sizeof(vpdpage83data.type3);
1021
1022                        /* T10 Vendor Identifier Field Format */
1023                        /* VpdcodesetAscii */
1024                        vpdpage83data.type1.codeset = 2;
1025                        /* VpdIdentifierTypeVendorId */
1026                        vpdpage83data.type1.identifiertype = 1;
1027                        vpdpage83data.type1.identifierlength =
1028                                sizeof(vpdpage83data.type1) - 4;
1029
1030                        /* "ADAPTEC " for adaptec */
1031                        memcpy(vpdpage83data.type1.venid,
1032                                "ADAPTEC ",
1033                                sizeof(vpdpage83data.type1.venid));
1034                        memcpy(vpdpage83data.type1.productid,
1035                                "ARRAY           ",
1036                                sizeof(
1037                                vpdpage83data.type1.productid));
1038
1039                        /* Convert to ascii based serial number.
1040                         * The LSB is the the end.
1041                         */
1042                        for (i = 0; i < 8; i++) {
1043                                u8 temp =
1044                                        (u8)((get_serial_reply->uid >> ((7 - i) * 4)) & 0xF);
1045                                if (temp  > 0x9) {
1046                                        vpdpage83data.type1.serialnumber[i] =
1047                                                        'A' + (temp - 0xA);
1048                                } else {
1049                                        vpdpage83data.type1.serialnumber[i] =
1050                                                        '0' + temp;
1051                                }
1052                        }
1053
1054                        /* VpdCodeSetBinary */
1055                        vpdpage83data.type2.codeset = 1;
1056                        /* VpdidentifiertypeEUI64 */
1057                        vpdpage83data.type2.identifiertype = 2;
1058                        vpdpage83data.type2.identifierlength =
1059                                sizeof(vpdpage83data.type2) - 4;
1060
1061                        vpdpage83data.type2.eu64id.venid[0] = 0xD0;
1062                        vpdpage83data.type2.eu64id.venid[1] = 0;
1063                        vpdpage83data.type2.eu64id.venid[2] = 0;
1064
1065                        vpdpage83data.type2.eu64id.Serial =
1066                                                        get_serial_reply->uid;
1067                        vpdpage83data.type2.eu64id.reserved = 0;
1068
1069                        /*
1070                         * VpdIdentifierTypeFCPHName
1071                         * VPD 0x83 Type 3 not supported for ARC
1072                         */
1073                        if (dev->sa_firmware) {
1074                                build_vpd83_type3(&vpdpage83data,
1075                                                dev, scsicmd);
1076                        }
1077
1078                        /* Move the inquiry data to the response buffer. */
1079                        scsi_sg_copy_from_buffer(scsicmd, &vpdpage83data,
1080                                                 sizeof(vpdpage83data));
1081                } else {
1082                        /* It must be for VPD 0x80 */
1083                        char sp[13];
1084                        /* EVPD bit set */
1085                        sp[0] = INQD_PDT_DA;
1086                        sp[1] = scsicmd->cmnd[2];
1087                        sp[2] = 0;
1088                        sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
1089                                le32_to_cpu(get_serial_reply->uid));
1090                        scsi_sg_copy_from_buffer(scsicmd, sp,
1091                                                 sizeof(sp));
1092                }
1093        }
1094
1095        scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
1096
1097        aac_fib_complete(fibptr);
1098        scsicmd->scsi_done(scsicmd);
1099}
1100
1101/*
1102 *      aac_get_container_serial - get container serial, none blocking.
1103 */
1104static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
1105{
1106        int status;
1107        struct aac_get_serial *dinfo;
1108        struct fib * cmd_fibcontext;
1109        struct aac_dev * dev;
1110
1111        dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1112
1113        cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
1114
1115        aac_fib_init(cmd_fibcontext);
1116        dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
1117
1118        dinfo->command = cpu_to_le32(VM_ContainerConfig);
1119        dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
1120        dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
1121        scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1122
1123        status = aac_fib_send(ContainerCommand,
1124                  cmd_fibcontext,
1125                  sizeof(struct aac_get_serial_resp),
1126                  FsaNormal,
1127                  0, 1,
1128                  (fib_callback) get_container_serial_callback,
1129                  (void *) scsicmd);
1130
1131        /*
1132         *      Check that the command queued to the controller
1133         */
1134        if (status == -EINPROGRESS)
1135                return 0;
1136
1137        printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
1138        aac_fib_complete(cmd_fibcontext);
1139        return -1;
1140}
1141
1142/* Function: setinqserial
1143 *
1144 * Arguments: [1] pointer to void [1] int
1145 *
1146 * Purpose: Sets SCSI Unit Serial number.
1147 *          This is a fake. We should read a proper
1148 *          serial number from the container. <SuSE>But
1149 *          without docs it's quite hard to do it :-)
1150 *          So this will have to do in the meantime.</SuSE>
1151 */
1152
1153static int setinqserial(struct aac_dev *dev, void *data, int cid)
1154{
1155        /*
1156         *      This breaks array migration.
1157         */
1158        return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
1159                        le32_to_cpu(dev->adapter_info.serial[0]), cid);
1160}
1161
1162static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
1163        u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
1164{
1165        u8 *sense_buf = (u8 *)sense_data;
1166        /* Sense data valid, err code 70h */
1167        sense_buf[0] = 0x70; /* No info field */
1168        sense_buf[1] = 0;       /* Segment number, always zero */
1169
1170        sense_buf[2] = sense_key;       /* Sense key */
1171
1172        sense_buf[12] = sense_code;     /* Additional sense code */
1173        sense_buf[13] = a_sense_code;   /* Additional sense code qualifier */
1174
1175        if (sense_key == ILLEGAL_REQUEST) {
1176                sense_buf[7] = 10;      /* Additional sense length */
1177
1178                sense_buf[15] = bit_pointer;
1179                /* Illegal parameter is in the parameter block */
1180                if (sense_code == SENCODE_INVALID_CDB_FIELD)
1181                        sense_buf[15] |= 0xc0;/* Std sense key specific field */
1182                /* Illegal parameter is in the CDB block */
1183                sense_buf[16] = field_pointer >> 8;     /* MSB */
1184                sense_buf[17] = field_pointer;          /* LSB */
1185        } else
1186                sense_buf[7] = 6;       /* Additional sense length */
1187}
1188
1189static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1190{
1191        if (lba & 0xffffffff00000000LL) {
1192                int cid = scmd_id(cmd);
1193                dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1194                cmd->result = DID_OK << 16 | SAM_STAT_CHECK_CONDITION;
1195                set_sense(&dev->fsa_dev[cid].sense_data,
1196                  HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1197                  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1198                memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1199                       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1200                             SCSI_SENSE_BUFFERSIZE));
1201                cmd->scsi_done(cmd);
1202                return 1;
1203        }
1204        return 0;
1205}
1206
1207static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1208{
1209        return 0;
1210}
1211
1212static void io_callback(void *context, struct fib * fibptr);
1213
1214static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1215{
1216        struct aac_dev *dev = fib->dev;
1217        u16 fibsize, command;
1218        long ret;
1219
1220        aac_fib_init(fib);
1221        if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1222                dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1223                !dev->sync_mode) {
1224                struct aac_raw_io2 *readcmd2;
1225                readcmd2 = (struct aac_raw_io2 *) fib_data(fib);
1226                memset(readcmd2, 0, sizeof(struct aac_raw_io2));
1227                readcmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1228                readcmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1229                readcmd2->byteCount = cpu_to_le32(count *
1230                        dev->fsa_dev[scmd_id(cmd)].block_size);
1231                readcmd2->cid = cpu_to_le16(scmd_id(cmd));
1232                readcmd2->flags = cpu_to_le16(RIO2_IO_TYPE_READ);
1233                ret = aac_build_sgraw2(cmd, readcmd2,
1234                                dev->scsi_host_ptr->sg_tablesize);
1235                if (ret < 0)
1236                        return ret;
1237                command = ContainerRawIo2;
1238                fibsize = struct_size(readcmd2, sge,
1239                                     le32_to_cpu(readcmd2->sgeCnt));
1240        } else {
1241                struct aac_raw_io *readcmd;
1242                readcmd = (struct aac_raw_io *) fib_data(fib);
1243                readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1244                readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1245                readcmd->count = cpu_to_le32(count *
1246                        dev->fsa_dev[scmd_id(cmd)].block_size);
1247                readcmd->cid = cpu_to_le16(scmd_id(cmd));
1248                readcmd->flags = cpu_to_le16(RIO_TYPE_READ);
1249                readcmd->bpTotal = 0;
1250                readcmd->bpComplete = 0;
1251                ret = aac_build_sgraw(cmd, &readcmd->sg);
1252                if (ret < 0)
1253                        return ret;
1254                command = ContainerRawIo;
1255                fibsize = sizeof(struct aac_raw_io) +
1256                        ((le32_to_cpu(readcmd->sg.count)-1) * sizeof(struct sgentryraw));
1257        }
1258
1259        BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1260        /*
1261         *      Now send the Fib to the adapter
1262         */
1263        return aac_fib_send(command,
1264                          fib,
1265                          fibsize,
1266                          FsaNormal,
1267                          0, 1,
1268                          (fib_callback) io_callback,
1269                          (void *) cmd);
1270}
1271
1272static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1273{
1274        u16 fibsize;
1275        struct aac_read64 *readcmd;
1276        long ret;
1277
1278        aac_fib_init(fib);
1279        readcmd = (struct aac_read64 *) fib_data(fib);
1280        readcmd->command = cpu_to_le32(VM_CtHostRead64);
1281        readcmd->cid = cpu_to_le16(scmd_id(cmd));
1282        readcmd->sector_count = cpu_to_le16(count);
1283        readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1284        readcmd->pad   = 0;
1285        readcmd->flags = 0;
1286
1287        ret = aac_build_sg64(cmd, &readcmd->sg);
1288        if (ret < 0)
1289                return ret;
1290        fibsize = sizeof(struct aac_read64) +
1291                ((le32_to_cpu(readcmd->sg.count) - 1) *
1292                 sizeof (struct sgentry64));
1293        BUG_ON (fibsize > (fib->dev->max_fib_size -
1294                                sizeof(struct aac_fibhdr)));
1295        /*
1296         *      Now send the Fib to the adapter
1297         */
1298        return aac_fib_send(ContainerCommand64,
1299                          fib,
1300                          fibsize,
1301                          FsaNormal,
1302                          0, 1,
1303                          (fib_callback) io_callback,
1304                          (void *) cmd);
1305}
1306
1307static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1308{
1309        u16 fibsize;
1310        struct aac_read *readcmd;
1311        struct aac_dev *dev = fib->dev;
1312        long ret;
1313
1314        aac_fib_init(fib);
1315        readcmd = (struct aac_read *) fib_data(fib);
1316        readcmd->command = cpu_to_le32(VM_CtBlockRead);
1317        readcmd->cid = cpu_to_le32(scmd_id(cmd));
1318        readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1319        readcmd->count = cpu_to_le32(count *
1320                dev->fsa_dev[scmd_id(cmd)].block_size);
1321
1322        ret = aac_build_sg(cmd, &readcmd->sg);
1323        if (ret < 0)
1324                return ret;
1325        fibsize = sizeof(struct aac_read) +
1326                        ((le32_to_cpu(readcmd->sg.count) - 1) *
1327                         sizeof (struct sgentry));
1328        BUG_ON (fibsize > (fib->dev->max_fib_size -
1329                                sizeof(struct aac_fibhdr)));
1330        /*
1331         *      Now send the Fib to the adapter
1332         */
1333        return aac_fib_send(ContainerCommand,
1334                          fib,
1335                          fibsize,
1336                          FsaNormal,
1337                          0, 1,
1338                          (fib_callback) io_callback,
1339                          (void *) cmd);
1340}
1341
1342static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1343{
1344        struct aac_dev *dev = fib->dev;
1345        u16 fibsize, command;
1346        long ret;
1347
1348        aac_fib_init(fib);
1349        if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1350                dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1351                !dev->sync_mode) {
1352                struct aac_raw_io2 *writecmd2;
1353                writecmd2 = (struct aac_raw_io2 *) fib_data(fib);
1354                memset(writecmd2, 0, sizeof(struct aac_raw_io2));
1355                writecmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1356                writecmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1357                writecmd2->byteCount = cpu_to_le32(count *
1358                        dev->fsa_dev[scmd_id(cmd)].block_size);
1359                writecmd2->cid = cpu_to_le16(scmd_id(cmd));
1360                writecmd2->flags = (fua && ((aac_cache & 5) != 1) &&
1361                                                   (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1362                        cpu_to_le16(RIO2_IO_TYPE_WRITE|RIO2_IO_SUREWRITE) :
1363                        cpu_to_le16(RIO2_IO_TYPE_WRITE);
1364                ret = aac_build_sgraw2(cmd, writecmd2,
1365                                dev->scsi_host_ptr->sg_tablesize);
1366                if (ret < 0)
1367                        return ret;
1368                command = ContainerRawIo2;
1369                fibsize = struct_size(writecmd2, sge,
1370                                      le32_to_cpu(writecmd2->sgeCnt));
1371        } else {
1372                struct aac_raw_io *writecmd;
1373                writecmd = (struct aac_raw_io *) fib_data(fib);
1374                writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1375                writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1376                writecmd->count = cpu_to_le32(count *
1377                        dev->fsa_dev[scmd_id(cmd)].block_size);
1378                writecmd->cid = cpu_to_le16(scmd_id(cmd));
1379                writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1380                                                   (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1381                        cpu_to_le16(RIO_TYPE_WRITE|RIO_SUREWRITE) :
1382                        cpu_to_le16(RIO_TYPE_WRITE);
1383                writecmd->bpTotal = 0;
1384                writecmd->bpComplete = 0;
1385                ret = aac_build_sgraw(cmd, &writecmd->sg);
1386                if (ret < 0)
1387                        return ret;
1388                command = ContainerRawIo;
1389                fibsize = sizeof(struct aac_raw_io) +
1390                        ((le32_to_cpu(writecmd->sg.count)-1) * sizeof (struct sgentryraw));
1391        }
1392
1393        BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1394        /*
1395         *      Now send the Fib to the adapter
1396         */
1397        return aac_fib_send(command,
1398                          fib,
1399                          fibsize,
1400                          FsaNormal,
1401                          0, 1,
1402                          (fib_callback) io_callback,
1403                          (void *) cmd);
1404}
1405
1406static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1407{
1408        u16 fibsize;
1409        struct aac_write64 *writecmd;
1410        long ret;
1411
1412        aac_fib_init(fib);
1413        writecmd = (struct aac_write64 *) fib_data(fib);
1414        writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1415        writecmd->cid = cpu_to_le16(scmd_id(cmd));
1416        writecmd->sector_count = cpu_to_le16(count);
1417        writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1418        writecmd->pad   = 0;
1419        writecmd->flags = 0;
1420
1421        ret = aac_build_sg64(cmd, &writecmd->sg);
1422        if (ret < 0)
1423                return ret;
1424        fibsize = sizeof(struct aac_write64) +
1425                ((le32_to_cpu(writecmd->sg.count) - 1) *
1426                 sizeof (struct sgentry64));
1427        BUG_ON (fibsize > (fib->dev->max_fib_size -
1428                                sizeof(struct aac_fibhdr)));
1429        /*
1430         *      Now send the Fib to the adapter
1431         */
1432        return aac_fib_send(ContainerCommand64,
1433                          fib,
1434                          fibsize,
1435                          FsaNormal,
1436                          0, 1,
1437                          (fib_callback) io_callback,
1438                          (void *) cmd);
1439}
1440
1441static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1442{
1443        u16 fibsize;
1444        struct aac_write *writecmd;
1445        struct aac_dev *dev = fib->dev;
1446        long ret;
1447
1448        aac_fib_init(fib);
1449        writecmd = (struct aac_write *) fib_data(fib);
1450        writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1451        writecmd->cid = cpu_to_le32(scmd_id(cmd));
1452        writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1453        writecmd->count = cpu_to_le32(count *
1454                dev->fsa_dev[scmd_id(cmd)].block_size);
1455        writecmd->sg.count = cpu_to_le32(1);
1456        /* ->stable is not used - it did mean which type of write */
1457
1458        ret = aac_build_sg(cmd, &writecmd->sg);
1459        if (ret < 0)
1460                return ret;
1461        fibsize = sizeof(struct aac_write) +
1462                ((le32_to_cpu(writecmd->sg.count) - 1) *
1463                 sizeof (struct sgentry));
1464        BUG_ON (fibsize > (fib->dev->max_fib_size -
1465                                sizeof(struct aac_fibhdr)));
1466        /*
1467         *      Now send the Fib to the adapter
1468         */
1469        return aac_fib_send(ContainerCommand,
1470                          fib,
1471                          fibsize,
1472                          FsaNormal,
1473                          0, 1,
1474                          (fib_callback) io_callback,
1475                          (void *) cmd);
1476}
1477
1478static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1479{
1480        struct aac_srb * srbcmd;
1481        u32 flag;
1482        u32 timeout;
1483        struct aac_dev *dev = fib->dev;
1484
1485        aac_fib_init(fib);
1486        switch(cmd->sc_data_direction){
1487        case DMA_TO_DEVICE:
1488                flag = SRB_DataOut;
1489                break;
1490        case DMA_BIDIRECTIONAL:
1491                flag = SRB_DataIn | SRB_DataOut;
1492                break;
1493        case DMA_FROM_DEVICE:
1494                flag = SRB_DataIn;
1495                break;
1496        case DMA_NONE:
1497        default:        /* shuts up some versions of gcc */
1498                flag = SRB_NoDataXfer;
1499                break;
1500        }
1501
1502        srbcmd = (struct aac_srb*) fib_data(fib);
1503        srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1504        srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1505        srbcmd->id       = cpu_to_le32(scmd_id(cmd));
1506        srbcmd->lun      = cpu_to_le32(cmd->device->lun);
1507        srbcmd->flags    = cpu_to_le32(flag);
1508        timeout = cmd->request->timeout/HZ;
1509        if (timeout == 0)
1510                timeout = (dev->sa_firmware ? AAC_SA_TIMEOUT : AAC_ARC_TIMEOUT);
1511        srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
1512        srbcmd->retry_limit = 0; /* Obsolete parameter */
1513        srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1514        return srbcmd;
1515}
1516
1517static struct aac_hba_cmd_req *aac_construct_hbacmd(struct fib *fib,
1518                                                        struct scsi_cmnd *cmd)
1519{
1520        struct aac_hba_cmd_req *hbacmd;
1521        struct aac_dev *dev;
1522        int bus, target;
1523        u64 address;
1524
1525        dev = (struct aac_dev *)cmd->device->host->hostdata;
1526
1527        hbacmd = (struct aac_hba_cmd_req *)fib->hw_fib_va;
1528        memset(hbacmd, 0, 96);  /* sizeof(*hbacmd) is not necessary */
1529        /* iu_type is a parameter of aac_hba_send */
1530        switch (cmd->sc_data_direction) {
1531        case DMA_TO_DEVICE:
1532                hbacmd->byte1 = 2;
1533                break;
1534        case DMA_FROM_DEVICE:
1535        case DMA_BIDIRECTIONAL:
1536                hbacmd->byte1 = 1;
1537                break;
1538        case DMA_NONE:
1539        default:
1540                break;
1541        }
1542        hbacmd->lun[1] = cpu_to_le32(cmd->device->lun);
1543
1544        bus = aac_logical_to_phys(scmd_channel(cmd));
1545        target = scmd_id(cmd);
1546        hbacmd->it_nexus = dev->hba_map[bus][target].rmw_nexus;
1547
1548        /* we fill in reply_qid later in aac_src_deliver_message */
1549        /* we fill in iu_type, request_id later in aac_hba_send */
1550        /* we fill in emb_data_desc_count later in aac_build_sghba */
1551
1552        memcpy(hbacmd->cdb, cmd->cmnd, cmd->cmd_len);
1553        hbacmd->data_length = cpu_to_le32(scsi_bufflen(cmd));
1554
1555        address = (u64)fib->hw_error_pa;
1556        hbacmd->error_ptr_hi = cpu_to_le32((u32)(address >> 32));
1557        hbacmd->error_ptr_lo = cpu_to_le32((u32)(address & 0xffffffff));
1558        hbacmd->error_length = cpu_to_le32(FW_ERROR_BUFFER_SIZE);
1559
1560        return hbacmd;
1561}
1562
1563static void aac_srb_callback(void *context, struct fib * fibptr);
1564
1565static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1566{
1567        u16 fibsize;
1568        struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1569        long ret;
1570
1571        ret = aac_build_sg64(cmd, (struct sgmap64 *) &srbcmd->sg);
1572        if (ret < 0)
1573                return ret;
1574        srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1575
1576        memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1577        memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1578        /*
1579         *      Build Scatter/Gather list
1580         */
1581        fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1582                ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1583                 sizeof (struct sgentry64));
1584        BUG_ON (fibsize > (fib->dev->max_fib_size -
1585                                sizeof(struct aac_fibhdr)));
1586
1587        /*
1588         *      Now send the Fib to the adapter
1589         */
1590        return aac_fib_send(ScsiPortCommand64, fib,
1591                                fibsize, FsaNormal, 0, 1,
1592                                  (fib_callback) aac_srb_callback,
1593                                  (void *) cmd);
1594}
1595
1596static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1597{
1598        u16 fibsize;
1599        struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1600        long ret;
1601
1602        ret = aac_build_sg(cmd, (struct sgmap *)&srbcmd->sg);
1603        if (ret < 0)
1604                return ret;
1605        srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1606
1607        memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1608        memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1609        /*
1610         *      Build Scatter/Gather list
1611         */
1612        fibsize = sizeof (struct aac_srb) +
1613                (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1614                 sizeof (struct sgentry));
1615        BUG_ON (fibsize > (fib->dev->max_fib_size -
1616                                sizeof(struct aac_fibhdr)));
1617
1618        /*
1619         *      Now send the Fib to the adapter
1620         */
1621        return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1622                                  (fib_callback) aac_srb_callback, (void *) cmd);
1623}
1624
1625static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1626{
1627        if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
1628            (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
1629                return FAILED;
1630        return aac_scsi_32(fib, cmd);
1631}
1632
1633static int aac_adapter_hba(struct fib *fib, struct scsi_cmnd *cmd)
1634{
1635        struct aac_hba_cmd_req *hbacmd = aac_construct_hbacmd(fib, cmd);
1636        struct aac_dev *dev;
1637        long ret;
1638
1639        dev = (struct aac_dev *)cmd->device->host->hostdata;
1640
1641        ret = aac_build_sghba(cmd, hbacmd,
1642                dev->scsi_host_ptr->sg_tablesize, (u64)fib->hw_sgl_pa);
1643        if (ret < 0)
1644                return ret;
1645
1646        /*
1647         *      Now send the HBA command to the adapter
1648         */
1649        fib->hbacmd_size = 64 + le32_to_cpu(hbacmd->emb_data_desc_count) *
1650                sizeof(struct aac_hba_sgl);
1651
1652        return aac_hba_send(HBA_IU_TYPE_SCSI_CMD_REQ, fib,
1653                                  (fib_callback) aac_hba_callback,
1654                                  (void *) cmd);
1655}
1656
1657static int aac_send_safw_bmic_cmd(struct aac_dev *dev,
1658        struct aac_srb_unit *srbu, void *xfer_buf, int xfer_len)
1659{
1660        struct fib      *fibptr;
1661        dma_addr_t      addr;
1662        int             rcode;
1663        int             fibsize;
1664        struct aac_srb  *srb;
1665        struct aac_srb_reply *srb_reply;
1666        struct sgmap64  *sg64;
1667        u32 vbus;
1668        u32 vid;
1669
1670        if (!dev->sa_firmware)
1671                return 0;
1672
1673        /* allocate FIB */
1674        fibptr = aac_fib_alloc(dev);
1675        if (!fibptr)
1676                return -ENOMEM;
1677
1678        aac_fib_init(fibptr);
1679        fibptr->hw_fib_va->header.XferState &=
1680                ~cpu_to_le32(FastResponseCapable);
1681
1682        fibsize  = sizeof(struct aac_srb) - sizeof(struct sgentry) +
1683                                                sizeof(struct sgentry64);
1684
1685        /* allocate DMA buffer for response */
1686        addr = dma_map_single(&dev->pdev->dev, xfer_buf, xfer_len,
1687                                                        DMA_BIDIRECTIONAL);
1688        if (dma_mapping_error(&dev->pdev->dev, addr)) {
1689                rcode = -ENOMEM;
1690                goto fib_error;
1691        }
1692
1693        srb = fib_data(fibptr);
1694        memcpy(srb, &srbu->srb, sizeof(struct aac_srb));
1695
1696        vbus = (u32)le16_to_cpu(
1697                        dev->supplement_adapter_info.virt_device_bus);
1698        vid  = (u32)le16_to_cpu(
1699                        dev->supplement_adapter_info.virt_device_target);
1700
1701        /* set the common request fields */
1702        srb->channel            = cpu_to_le32(vbus);
1703        srb->id                 = cpu_to_le32(vid);
1704        srb->lun                = 0;
1705        srb->function           = cpu_to_le32(SRBF_ExecuteScsi);
1706        srb->timeout            = 0;
1707        srb->retry_limit        = 0;
1708        srb->cdb_size           = cpu_to_le32(16);
1709        srb->count              = cpu_to_le32(xfer_len);
1710
1711        sg64 = (struct sgmap64 *)&srb->sg;
1712        sg64->count             = cpu_to_le32(1);
1713        sg64->sg[0].addr[1]     = cpu_to_le32(upper_32_bits(addr));
1714        sg64->sg[0].addr[0]     = cpu_to_le32(lower_32_bits(addr));
1715        sg64->sg[0].count       = cpu_to_le32(xfer_len);
1716
1717        /*
1718         * Copy the updated data for other dumping or other usage if needed
1719         */
1720        memcpy(&srbu->srb, srb, sizeof(struct aac_srb));
1721
1722        /* issue request to the controller */
1723        rcode = aac_fib_send(ScsiPortCommand64, fibptr, fibsize, FsaNormal,
1724                                        1, 1, NULL, NULL);
1725
1726        if (rcode == -ERESTARTSYS)
1727                rcode = -ERESTART;
1728
1729        if (unlikely(rcode < 0))
1730                goto bmic_error;
1731
1732        srb_reply = (struct aac_srb_reply *)fib_data(fibptr);
1733        memcpy(&srbu->srb_reply, srb_reply, sizeof(struct aac_srb_reply));
1734
1735bmic_error:
1736        dma_unmap_single(&dev->pdev->dev, addr, xfer_len, DMA_BIDIRECTIONAL);
1737fib_error:
1738        aac_fib_complete(fibptr);
1739        aac_fib_free(fibptr);
1740        return rcode;
1741}
1742
1743static void aac_set_safw_target_qd(struct aac_dev *dev, int bus, int target)
1744{
1745
1746        struct aac_ciss_identify_pd *identify_resp;
1747
1748        if (dev->hba_map[bus][target].devtype != AAC_DEVTYPE_NATIVE_RAW)
1749                return;
1750
1751        identify_resp = dev->hba_map[bus][target].safw_identify_resp;
1752        if (identify_resp == NULL) {
1753                dev->hba_map[bus][target].qd_limit = 32;
1754                return;
1755        }
1756
1757        if (identify_resp->current_queue_depth_limit <= 0 ||
1758                identify_resp->current_queue_depth_limit > 255)
1759                dev->hba_map[bus][target].qd_limit = 32;
1760        else
1761                dev->hba_map[bus][target].qd_limit =
1762                        identify_resp->current_queue_depth_limit;
1763}
1764
1765static int aac_issue_safw_bmic_identify(struct aac_dev *dev,
1766        struct aac_ciss_identify_pd **identify_resp, u32 bus, u32 target)
1767{
1768        int rcode = -ENOMEM;
1769        int datasize;
1770        struct aac_srb_unit srbu;
1771        struct aac_srb *srbcmd;
1772        struct aac_ciss_identify_pd *identify_reply;
1773
1774        datasize = sizeof(struct aac_ciss_identify_pd);
1775        identify_reply = kmalloc(datasize, GFP_KERNEL);
1776        if (!identify_reply)
1777                goto out;
1778
1779        memset(&srbu, 0, sizeof(struct aac_srb_unit));
1780
1781        srbcmd = &srbu.srb;
1782        srbcmd->flags   = cpu_to_le32(SRB_DataIn);
1783        srbcmd->cdb[0]  = 0x26;
1784        srbcmd->cdb[2]  = (u8)((AAC_MAX_LUN + target) & 0x00FF);
1785        srbcmd->cdb[6]  = CISS_IDENTIFY_PHYSICAL_DEVICE;
1786
1787        rcode = aac_send_safw_bmic_cmd(dev, &srbu, identify_reply, datasize);
1788        if (unlikely(rcode < 0))
1789                goto mem_free_all;
1790
1791        *identify_resp = identify_reply;
1792
1793out:
1794        return rcode;
1795mem_free_all:
1796        kfree(identify_reply);
1797        goto out;
1798}
1799
1800static inline void aac_free_safw_ciss_luns(struct aac_dev *dev)
1801{
1802        kfree(dev->safw_phys_luns);
1803        dev->safw_phys_luns = NULL;
1804}
1805
1806/**
1807 *      aac_get_safw_ciss_luns() - Process topology change
1808 *      @dev:           aac_dev structure
1809 *
1810 *      Execute a CISS REPORT PHYS LUNS and process the results into
1811 *      the current hba_map.
1812 */
1813static int aac_get_safw_ciss_luns(struct aac_dev *dev)
1814{
1815        int rcode = -ENOMEM;
1816        int datasize;
1817        struct aac_srb *srbcmd;
1818        struct aac_srb_unit srbu;
1819        struct aac_ciss_phys_luns_resp *phys_luns;
1820
1821        datasize = sizeof(struct aac_ciss_phys_luns_resp) +
1822                (AAC_MAX_TARGETS - 1) * sizeof(struct _ciss_lun);
1823        phys_luns = kmalloc(datasize, GFP_KERNEL);
1824        if (phys_luns == NULL)
1825                goto out;
1826
1827        memset(&srbu, 0, sizeof(struct aac_srb_unit));
1828
1829        srbcmd = &srbu.srb;
1830        srbcmd->flags   = cpu_to_le32(SRB_DataIn);
1831        srbcmd->cdb[0]  = CISS_REPORT_PHYSICAL_LUNS;
1832        srbcmd->cdb[1]  = 2; /* extended reporting */
1833        srbcmd->cdb[8]  = (u8)(datasize >> 8);
1834        srbcmd->cdb[9]  = (u8)(datasize);
1835
1836        rcode = aac_send_safw_bmic_cmd(dev, &srbu, phys_luns, datasize);
1837        if (unlikely(rcode < 0))
1838                goto mem_free_all;
1839
1840        if (phys_luns->resp_flag != 2) {
1841                rcode = -ENOMSG;
1842                goto mem_free_all;
1843        }
1844
1845        dev->safw_phys_luns = phys_luns;
1846
1847out:
1848        return rcode;
1849mem_free_all:
1850        kfree(phys_luns);
1851        goto out;
1852}
1853
1854static inline u32 aac_get_safw_phys_lun_count(struct aac_dev *dev)
1855{
1856        return get_unaligned_be32(&dev->safw_phys_luns->list_length[0])/24;
1857}
1858
1859static inline u32 aac_get_safw_phys_bus(struct aac_dev *dev, int lun)
1860{
1861        return dev->safw_phys_luns->lun[lun].level2[1] & 0x3f;
1862}
1863
1864static inline u32 aac_get_safw_phys_target(struct aac_dev *dev, int lun)
1865{
1866        return dev->safw_phys_luns->lun[lun].level2[0];
1867}
1868
1869static inline u32 aac_get_safw_phys_expose_flag(struct aac_dev *dev, int lun)
1870{
1871        return dev->safw_phys_luns->lun[lun].bus >> 6;
1872}
1873
1874static inline u32 aac_get_safw_phys_attribs(struct aac_dev *dev, int lun)
1875{
1876        return dev->safw_phys_luns->lun[lun].node_ident[9];
1877}
1878
1879static inline u32 aac_get_safw_phys_nexus(struct aac_dev *dev, int lun)
1880{
1881        return *((u32 *)&dev->safw_phys_luns->lun[lun].node_ident[12]);
1882}
1883
1884static inline void aac_free_safw_identify_resp(struct aac_dev *dev,
1885                                                int bus, int target)
1886{
1887        kfree(dev->hba_map[bus][target].safw_identify_resp);
1888        dev->hba_map[bus][target].safw_identify_resp = NULL;
1889}
1890
1891static inline void aac_free_safw_all_identify_resp(struct aac_dev *dev,
1892        int lun_count)
1893{
1894        int luns;
1895        int i;
1896        u32 bus;
1897        u32 target;
1898
1899        luns = aac_get_safw_phys_lun_count(dev);
1900
1901        if (luns < lun_count)
1902                lun_count = luns;
1903        else if (lun_count < 0)
1904                lun_count = luns;
1905
1906        for (i = 0; i < lun_count; i++) {
1907                bus = aac_get_safw_phys_bus(dev, i);
1908                target = aac_get_safw_phys_target(dev, i);
1909
1910                aac_free_safw_identify_resp(dev, bus, target);
1911        }
1912}
1913
1914static int aac_get_safw_attr_all_targets(struct aac_dev *dev)
1915{
1916        int i;
1917        int rcode = 0;
1918        u32 lun_count;
1919        u32 bus;
1920        u32 target;
1921        struct aac_ciss_identify_pd *identify_resp = NULL;
1922
1923        lun_count = aac_get_safw_phys_lun_count(dev);
1924
1925        for (i = 0; i < lun_count; ++i) {
1926
1927                bus = aac_get_safw_phys_bus(dev, i);
1928                target = aac_get_safw_phys_target(dev, i);
1929
1930                rcode = aac_issue_safw_bmic_identify(dev,
1931                                                &identify_resp, bus, target);
1932
1933                if (unlikely(rcode < 0))
1934                        goto free_identify_resp;
1935
1936                dev->hba_map[bus][target].safw_identify_resp = identify_resp;
1937        }
1938
1939out:
1940        return rcode;
1941free_identify_resp:
1942        aac_free_safw_all_identify_resp(dev, i);
1943        goto out;
1944}
1945
1946/**
1947 *      aac_set_safw_attr_all_targets-  update current hba map with data from FW
1948 *      @dev:   aac_dev structure
1949 *
1950 *      Update our hba map with the information gathered from the FW
1951 */
1952static void aac_set_safw_attr_all_targets(struct aac_dev *dev)
1953{
1954        /* ok and extended reporting */
1955        u32 lun_count, nexus;
1956        u32 i, bus, target;
1957        u8 expose_flag, attribs;
1958
1959        lun_count = aac_get_safw_phys_lun_count(dev);
1960
1961        dev->scan_counter++;
1962
1963        for (i = 0; i < lun_count; ++i) {
1964
1965                bus = aac_get_safw_phys_bus(dev, i);
1966                target = aac_get_safw_phys_target(dev, i);
1967                expose_flag = aac_get_safw_phys_expose_flag(dev, i);
1968                attribs = aac_get_safw_phys_attribs(dev, i);
1969                nexus = aac_get_safw_phys_nexus(dev, i);
1970
1971                if (bus >= AAC_MAX_BUSES || target >= AAC_MAX_TARGETS)
1972                        continue;
1973
1974                if (expose_flag != 0) {
1975                        dev->hba_map[bus][target].devtype =
1976                                AAC_DEVTYPE_RAID_MEMBER;
1977                        continue;
1978                }
1979
1980                if (nexus != 0 && (attribs & 8)) {
1981                        dev->hba_map[bus][target].devtype =
1982                                AAC_DEVTYPE_NATIVE_RAW;
1983                        dev->hba_map[bus][target].rmw_nexus =
1984                                        nexus;
1985                } else
1986                        dev->hba_map[bus][target].devtype =
1987                                AAC_DEVTYPE_ARC_RAW;
1988
1989                dev->hba_map[bus][target].scan_counter = dev->scan_counter;
1990
1991                aac_set_safw_target_qd(dev, bus, target);
1992        }
1993}
1994
1995static int aac_setup_safw_targets(struct aac_dev *dev)
1996{
1997        int rcode = 0;
1998
1999        rcode = aac_get_containers(dev);
2000        if (unlikely(rcode < 0))
2001                goto out;
2002
2003        rcode = aac_get_safw_ciss_luns(dev);
2004        if (unlikely(rcode < 0))
2005                goto out;
2006
2007        rcode = aac_get_safw_attr_all_targets(dev);
2008        if (unlikely(rcode < 0))
2009                goto free_ciss_luns;
2010
2011        aac_set_safw_attr_all_targets(dev);
2012
2013        aac_free_safw_all_identify_resp(dev, -1);
2014free_ciss_luns:
2015        aac_free_safw_ciss_luns(dev);
2016out:
2017        return rcode;
2018}
2019
2020int aac_setup_safw_adapter(struct aac_dev *dev)
2021{
2022        return aac_setup_safw_targets(dev);
2023}
2024
2025int aac_get_adapter_info(struct aac_dev* dev)
2026{
2027        struct fib* fibptr;
2028        int rcode;
2029        u32 tmp, bus, target;
2030        struct aac_adapter_info *info;
2031        struct aac_bus_info *command;
2032        struct aac_bus_info_response *bus_info;
2033
2034        if (!(fibptr = aac_fib_alloc(dev)))
2035                return -ENOMEM;
2036
2037        aac_fib_init(fibptr);
2038        info = (struct aac_adapter_info *) fib_data(fibptr);
2039        memset(info,0,sizeof(*info));
2040
2041        rcode = aac_fib_send(RequestAdapterInfo,
2042                         fibptr,
2043                         sizeof(*info),
2044                         FsaNormal,
2045                         -1, 1, /* First `interrupt' command uses special wait */
2046                         NULL,
2047                         NULL);
2048
2049        if (rcode < 0) {
2050                /* FIB should be freed only after
2051                 * getting the response from the F/W */
2052                if (rcode != -ERESTARTSYS) {
2053                        aac_fib_complete(fibptr);
2054                        aac_fib_free(fibptr);
2055                }
2056                return rcode;
2057        }
2058        memcpy(&dev->adapter_info, info, sizeof(*info));
2059
2060        dev->supplement_adapter_info.virt_device_bus = 0xffff;
2061        if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
2062                struct aac_supplement_adapter_info * sinfo;
2063
2064                aac_fib_init(fibptr);
2065
2066                sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
2067
2068                memset(sinfo,0,sizeof(*sinfo));
2069
2070                rcode = aac_fib_send(RequestSupplementAdapterInfo,
2071                                 fibptr,
2072                                 sizeof(*sinfo),
2073                                 FsaNormal,
2074                                 1, 1,
2075                                 NULL,
2076                                 NULL);
2077
2078                if (rcode >= 0)
2079                        memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
2080                if (rcode == -ERESTARTSYS) {
2081                        fibptr = aac_fib_alloc(dev);
2082                        if (!fibptr)
2083                                return -ENOMEM;
2084                }
2085
2086        }
2087
2088        /* reset all previous mapped devices (i.e. for init. after IOP_RESET) */
2089        for (bus = 0; bus < AAC_MAX_BUSES; bus++) {
2090                for (target = 0; target < AAC_MAX_TARGETS; target++) {
2091                        dev->hba_map[bus][target].devtype = 0;
2092                        dev->hba_map[bus][target].qd_limit = 0;
2093                }
2094        }
2095
2096        /*
2097         * GetBusInfo
2098         */
2099
2100        aac_fib_init(fibptr);
2101
2102        bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
2103
2104        memset(bus_info, 0, sizeof(*bus_info));
2105
2106        command = (struct aac_bus_info *)bus_info;
2107
2108        command->Command = cpu_to_le32(VM_Ioctl);
2109        command->ObjType = cpu_to_le32(FT_DRIVE);
2110        command->MethodId = cpu_to_le32(1);
2111        command->CtlCmd = cpu_to_le32(GetBusInfo);
2112
2113        rcode = aac_fib_send(ContainerCommand,
2114                         fibptr,
2115                         sizeof (*bus_info),
2116                         FsaNormal,
2117                         1, 1,
2118                         NULL, NULL);
2119
2120        /* reasoned default */
2121        dev->maximum_num_physicals = 16;
2122        if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
2123                dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
2124                dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
2125        }
2126
2127        if (!dev->in_reset) {
2128                char buffer[16];
2129                tmp = le32_to_cpu(dev->adapter_info.kernelrev);
2130                printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
2131                        dev->name,
2132                        dev->id,
2133                        tmp>>24,
2134                        (tmp>>16)&0xff,
2135                        tmp&0xff,
2136                        le32_to_cpu(dev->adapter_info.kernelbuild),
2137                        (int)sizeof(dev->supplement_adapter_info.build_date),
2138                        dev->supplement_adapter_info.build_date);
2139                tmp = le32_to_cpu(dev->adapter_info.monitorrev);
2140                printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
2141                        dev->name, dev->id,
2142                        tmp>>24,(tmp>>16)&0xff,tmp&0xff,
2143                        le32_to_cpu(dev->adapter_info.monitorbuild));
2144                tmp = le32_to_cpu(dev->adapter_info.biosrev);
2145                printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
2146                        dev->name, dev->id,
2147                        tmp>>24,(tmp>>16)&0xff,tmp&0xff,
2148                        le32_to_cpu(dev->adapter_info.biosbuild));
2149                buffer[0] = '\0';
2150                if (aac_get_serial_number(
2151                  shost_to_class(dev->scsi_host_ptr), buffer))
2152                        printk(KERN_INFO "%s%d: serial %s",
2153                          dev->name, dev->id, buffer);
2154                if (dev->supplement_adapter_info.vpd_info.tsid[0]) {
2155                        printk(KERN_INFO "%s%d: TSID %.*s\n",
2156                          dev->name, dev->id,
2157                          (int)sizeof(dev->supplement_adapter_info
2158                                                        .vpd_info.tsid),
2159                                dev->supplement_adapter_info.vpd_info.tsid);
2160                }
2161                if (!aac_check_reset || ((aac_check_reset == 1) &&
2162                  (dev->supplement_adapter_info.supported_options2 &
2163                  AAC_OPTION_IGNORE_RESET))) {
2164                        printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
2165                          dev->name, dev->id);
2166                }
2167        }
2168
2169        dev->cache_protected = 0;
2170        dev->jbod = ((dev->supplement_adapter_info.feature_bits &
2171                AAC_FEATURE_JBOD) != 0);
2172        dev->nondasd_support = 0;
2173        dev->raid_scsi_mode = 0;
2174        if(dev->adapter_info.options & AAC_OPT_NONDASD)
2175                dev->nondasd_support = 1;
2176
2177        /*
2178         * If the firmware supports ROMB RAID/SCSI mode and we are currently
2179         * in RAID/SCSI mode, set the flag. For now if in this mode we will
2180         * force nondasd support on. If we decide to allow the non-dasd flag
2181         * additional changes changes will have to be made to support
2182         * RAID/SCSI.  the function aac_scsi_cmd in this module will have to be
2183         * changed to support the new dev->raid_scsi_mode flag instead of
2184         * leaching off of the dev->nondasd_support flag. Also in linit.c the
2185         * function aac_detect will have to be modified where it sets up the
2186         * max number of channels based on the aac->nondasd_support flag only.
2187         */
2188        if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
2189            (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
2190                dev->nondasd_support = 1;
2191                dev->raid_scsi_mode = 1;
2192        }
2193        if (dev->raid_scsi_mode != 0)
2194                printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
2195                                dev->name, dev->id);
2196
2197        if (nondasd != -1)
2198                dev->nondasd_support = (nondasd!=0);
2199        if (dev->nondasd_support && !dev->in_reset)
2200                printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
2201
2202        if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
2203                dev->needs_dac = 1;
2204        dev->dac_support = 0;
2205        if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
2206            (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
2207                if (!dev->in_reset)
2208                        printk(KERN_INFO "%s%d: 64bit support enabled.\n",
2209                                dev->name, dev->id);
2210                dev->dac_support = 1;
2211        }
2212
2213        if(dacmode != -1) {
2214                dev->dac_support = (dacmode!=0);
2215        }
2216
2217        /* avoid problems with AAC_QUIRK_SCSI_32 controllers */
2218        if (dev->dac_support && (aac_get_driver_ident(dev->cardtype)->quirks
2219                & AAC_QUIRK_SCSI_32)) {
2220                dev->nondasd_support = 0;
2221                dev->jbod = 0;
2222                expose_physicals = 0;
2223        }
2224
2225        if (dev->dac_support) {
2226                if (!dma_set_mask(&dev->pdev->dev, DMA_BIT_MASK(64))) {
2227                        if (!dev->in_reset)
2228                                dev_info(&dev->pdev->dev, "64 Bit DAC enabled\n");
2229                } else if (!dma_set_mask(&dev->pdev->dev, DMA_BIT_MASK(32))) {
2230                        dev_info(&dev->pdev->dev, "DMA mask set failed, 64 Bit DAC disabled\n");
2231                        dev->dac_support = 0;
2232                } else {
2233                        dev_info(&dev->pdev->dev, "No suitable DMA available\n");
2234                        rcode = -ENOMEM;
2235                }
2236        }
2237        /*
2238         * Deal with configuring for the individualized limits of each packet
2239         * interface.
2240         */
2241        dev->a_ops.adapter_scsi = (dev->dac_support)
2242          ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
2243                                ? aac_scsi_32_64
2244                                : aac_scsi_64)
2245                                : aac_scsi_32;
2246        if (dev->raw_io_interface) {
2247                dev->a_ops.adapter_bounds = (dev->raw_io_64)
2248                                        ? aac_bounds_64
2249                                        : aac_bounds_32;
2250                dev->a_ops.adapter_read = aac_read_raw_io;
2251                dev->a_ops.adapter_write = aac_write_raw_io;
2252        } else {
2253                dev->a_ops.adapter_bounds = aac_bounds_32;
2254                dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
2255                        sizeof(struct aac_fibhdr) -
2256                        sizeof(struct aac_write) + sizeof(struct sgentry)) /
2257                                sizeof(struct sgentry);
2258                if (dev->dac_support) {
2259                        dev->a_ops.adapter_read = aac_read_block64;
2260                        dev->a_ops.adapter_write = aac_write_block64;
2261                        /*
2262                         * 38 scatter gather elements
2263                         */
2264                        dev->scsi_host_ptr->sg_tablesize =
2265                                (dev->max_fib_size -
2266                                sizeof(struct aac_fibhdr) -
2267                                sizeof(struct aac_write64) +
2268                                sizeof(struct sgentry64)) /
2269                                        sizeof(struct sgentry64);
2270                } else {
2271                        dev->a_ops.adapter_read = aac_read_block;
2272                        dev->a_ops.adapter_write = aac_write_block;
2273                }
2274                dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
2275                if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
2276                        /*
2277                         * Worst case size that could cause sg overflow when
2278                         * we break up SG elements that are larger than 64KB.
2279                         * Would be nice if we could tell the SCSI layer what
2280                         * the maximum SG element size can be. Worst case is
2281                         * (sg_tablesize-1) 4KB elements with one 64KB
2282                         * element.
2283                         *      32bit -> 468 or 238KB   64bit -> 424 or 212KB
2284                         */
2285                        dev->scsi_host_ptr->max_sectors =
2286                          (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
2287                }
2288        }
2289        if (!dev->sync_mode && dev->sa_firmware &&
2290                dev->scsi_host_ptr->sg_tablesize > HBA_MAX_SG_SEPARATE)
2291                dev->scsi_host_ptr->sg_tablesize = dev->sg_tablesize =
2292                        HBA_MAX_SG_SEPARATE;
2293
2294        /* FIB should be freed only after getting the response from the F/W */
2295        if (rcode != -ERESTARTSYS) {
2296                aac_fib_complete(fibptr);
2297                aac_fib_free(fibptr);
2298        }
2299
2300        return rcode;
2301}
2302
2303
2304static void io_callback(void *context, struct fib * fibptr)
2305{
2306        struct aac_dev *dev;
2307        struct aac_read_reply *readreply;
2308        struct scsi_cmnd *scsicmd;
2309        u32 cid;
2310
2311        scsicmd = (struct scsi_cmnd *) context;
2312
2313        if (!aac_valid_context(scsicmd, fibptr))
2314                return;
2315
2316        dev = fibptr->dev;
2317        cid = scmd_id(scsicmd);
2318
2319        if (nblank(dprintk(x))) {
2320                u64 lba;
2321                switch (scsicmd->cmnd[0]) {
2322                case WRITE_6:
2323                case READ_6:
2324                        lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
2325                            (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2326                        break;
2327                case WRITE_16:
2328                case READ_16:
2329                        lba = ((u64)scsicmd->cmnd[2] << 56) |
2330                              ((u64)scsicmd->cmnd[3] << 48) |
2331                              ((u64)scsicmd->cmnd[4] << 40) |
2332                              ((u64)scsicmd->cmnd[5] << 32) |
2333                              ((u64)scsicmd->cmnd[6] << 24) |
2334                              (scsicmd->cmnd[7] << 16) |
2335                              (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2336                        break;
2337                case WRITE_12:
2338                case READ_12:
2339                        lba = ((u64)scsicmd->cmnd[2] << 24) |
2340                              (scsicmd->cmnd[3] << 16) |
2341                              (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2342                        break;
2343                default:
2344                        lba = ((u64)scsicmd->cmnd[2] << 24) |
2345                               (scsicmd->cmnd[3] << 16) |
2346                               (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2347                        break;
2348                }
2349                printk(KERN_DEBUG
2350                  "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
2351                  smp_processor_id(), (unsigned long long)lba, jiffies);
2352        }
2353
2354        BUG_ON(fibptr == NULL);
2355
2356        scsi_dma_unmap(scsicmd);
2357
2358        readreply = (struct aac_read_reply *)fib_data(fibptr);
2359        switch (le32_to_cpu(readreply->status)) {
2360        case ST_OK:
2361                scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
2362                dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
2363                break;
2364        case ST_NOT_READY:
2365                scsicmd->result = DID_OK << 16 | SAM_STAT_CHECK_CONDITION;
2366                set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
2367                  SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
2368                memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2369                       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2370                             SCSI_SENSE_BUFFERSIZE));
2371                break;
2372        case ST_MEDERR:
2373                scsicmd->result = DID_OK << 16 | SAM_STAT_CHECK_CONDITION;
2374                set_sense(&dev->fsa_dev[cid].sense_data, MEDIUM_ERROR,
2375                  SENCODE_UNRECOVERED_READ_ERROR, ASENCODE_NO_SENSE, 0, 0);
2376                memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2377                       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2378                             SCSI_SENSE_BUFFERSIZE));
2379                break;
2380        default:
2381#ifdef AAC_DETAILED_STATUS_INFO
2382                printk(KERN_WARNING "io_callback: io failed, status = %d\n",
2383                  le32_to_cpu(readreply->status));
2384#endif
2385                scsicmd->result = DID_OK << 16 | SAM_STAT_CHECK_CONDITION;
2386                set_sense(&dev->fsa_dev[cid].sense_data,
2387                  HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2388                  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2389                memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2390                       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2391                             SCSI_SENSE_BUFFERSIZE));
2392                break;
2393        }
2394        aac_fib_complete(fibptr);
2395
2396        scsicmd->scsi_done(scsicmd);
2397}
2398
2399static int aac_read(struct scsi_cmnd * scsicmd)
2400{
2401        u64 lba;
2402        u32 count;
2403        int status;
2404        struct aac_dev *dev;
2405        struct fib * cmd_fibcontext;
2406        int cid;
2407
2408        dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2409        /*
2410         *      Get block address and transfer length
2411         */
2412        switch (scsicmd->cmnd[0]) {
2413        case READ_6:
2414                dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
2415
2416                lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
2417                        (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2418                count = scsicmd->cmnd[4];
2419
2420                if (count == 0)
2421                        count = 256;
2422                break;
2423        case READ_16:
2424                dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
2425
2426                lba =   ((u64)scsicmd->cmnd[2] << 56) |
2427                        ((u64)scsicmd->cmnd[3] << 48) |
2428                        ((u64)scsicmd->cmnd[4] << 40) |
2429                        ((u64)scsicmd->cmnd[5] << 32) |
2430                        ((u64)scsicmd->cmnd[6] << 24) |
2431                        (scsicmd->cmnd[7] << 16) |
2432                        (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2433                count = (scsicmd->cmnd[10] << 24) |
2434                        (scsicmd->cmnd[11] << 16) |
2435                        (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2436                break;
2437        case READ_12:
2438                dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
2439
2440                lba = ((u64)scsicmd->cmnd[2] << 24) |
2441                        (scsicmd->cmnd[3] << 16) |
2442                        (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2443                count = (scsicmd->cmnd[6] << 24) |
2444                        (scsicmd->cmnd[7] << 16) |
2445                        (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2446                break;
2447        default:
2448                dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
2449
2450                lba = ((u64)scsicmd->cmnd[2] << 24) |
2451                        (scsicmd->cmnd[3] << 16) |
2452                        (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2453                count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2454                break;
2455        }
2456
2457        if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2458                cid = scmd_id(scsicmd);
2459                dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2460                scsicmd->result = DID_OK << 16 | SAM_STAT_CHECK_CONDITION;
2461                set_sense(&dev->fsa_dev[cid].sense_data,
2462                          ILLEGAL_REQUEST, SENCODE_LBA_OUT_OF_RANGE,
2463                          ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2464                memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2465                       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2466                             SCSI_SENSE_BUFFERSIZE));
2467                scsicmd->scsi_done(scsicmd);
2468                return 0;
2469        }
2470
2471        dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
2472          smp_processor_id(), (unsigned long long)lba, jiffies));
2473        if (aac_adapter_bounds(dev,scsicmd,lba))
2474                return 0;
2475        /*
2476         *      Alocate and initialize a Fib
2477         */
2478        cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
2479        scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2480        status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
2481
2482        /*
2483         *      Check that the command queued to the controller
2484         */
2485        if (status == -EINPROGRESS)
2486                return 0;
2487
2488        printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
2489        /*
2490         *      For some reason, the Fib didn't queue, return QUEUE_FULL
2491         */
2492        scsicmd->result = DID_OK << 16 | SAM_STAT_TASK_SET_FULL;
2493        scsicmd->scsi_done(scsicmd);
2494        aac_fib_complete(cmd_fibcontext);
2495        aac_fib_free(cmd_fibcontext);
2496        return 0;
2497}
2498
2499static int aac_write(struct scsi_cmnd * scsicmd)
2500{
2501        u64 lba;
2502        u32 count;
2503        int fua;
2504        int status;
2505        struct aac_dev *dev;
2506        struct fib * cmd_fibcontext;
2507        int cid;
2508
2509        dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2510        /*
2511         *      Get block address and transfer length
2512         */
2513        if (scsicmd->cmnd[0] == WRITE_6)        /* 6 byte command */
2514        {
2515                lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2516                count = scsicmd->cmnd[4];
2517                if (count == 0)
2518                        count = 256;
2519                fua = 0;
2520        } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
2521                dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
2522
2523                lba =   ((u64)scsicmd->cmnd[2] << 56) |
2524                        ((u64)scsicmd->cmnd[3] << 48) |
2525                        ((u64)scsicmd->cmnd[4] << 40) |
2526                        ((u64)scsicmd->cmnd[5] << 32) |
2527                        ((u64)scsicmd->cmnd[6] << 24) |
2528                        (scsicmd->cmnd[7] << 16) |
2529                        (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2530                count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
2531                        (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2532                fua = scsicmd->cmnd[1] & 0x8;
2533        } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
2534                dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
2535
2536                lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
2537                    | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2538                count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
2539                      | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2540                fua = scsicmd->cmnd[1] & 0x8;
2541        } else {
2542                dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
2543                lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2544                count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2545                fua = scsicmd->cmnd[1] & 0x8;
2546        }
2547
2548        if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2549                cid = scmd_id(scsicmd);
2550                dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2551                scsicmd->result = DID_OK << 16 | SAM_STAT_CHECK_CONDITION;
2552                set_sense(&dev->fsa_dev[cid].sense_data,
2553                          ILLEGAL_REQUEST, SENCODE_LBA_OUT_OF_RANGE,
2554                          ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2555                memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2556                       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2557                             SCSI_SENSE_BUFFERSIZE));
2558                scsicmd->scsi_done(scsicmd);
2559                return 0;
2560        }
2561
2562        dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
2563          smp_processor_id(), (unsigned long long)lba, jiffies));
2564        if (aac_adapter_bounds(dev,scsicmd,lba))
2565                return 0;
2566        /*
2567         *      Allocate and initialize a Fib then setup a BlockWrite command
2568         */
2569        cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
2570        scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2571        status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
2572
2573        /*
2574         *      Check that the command queued to the controller
2575         */
2576        if (status == -EINPROGRESS)
2577                return 0;
2578
2579        printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
2580        /*
2581         *      For some reason, the Fib didn't queue, return QUEUE_FULL
2582         */
2583        scsicmd->result = DID_OK << 16 | SAM_STAT_TASK_SET_FULL;
2584        scsicmd->scsi_done(scsicmd);
2585
2586        aac_fib_complete(cmd_fibcontext);
2587        aac_fib_free(cmd_fibcontext);
2588        return 0;
2589}
2590
2591static void synchronize_callback(void *context, struct fib *fibptr)
2592{
2593        struct aac_synchronize_reply *synchronizereply;
2594        struct scsi_cmnd *cmd = context;
2595
2596        if (!aac_valid_context(cmd, fibptr))
2597                return;
2598
2599        dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
2600                                smp_processor_id(), jiffies));
2601        BUG_ON(fibptr == NULL);
2602
2603
2604        synchronizereply = fib_data(fibptr);
2605        if (le32_to_cpu(synchronizereply->status) == CT_OK)
2606                cmd->result = DID_OK << 16 | SAM_STAT_GOOD;
2607        else {
2608                struct scsi_device *sdev = cmd->device;
2609                struct aac_dev *dev = fibptr->dev;
2610                u32 cid = sdev_id(sdev);
2611                printk(KERN_WARNING
2612                     "synchronize_callback: synchronize failed, status = %d\n",
2613                     le32_to_cpu(synchronizereply->status));
2614                cmd->result = DID_OK << 16 | SAM_STAT_CHECK_CONDITION;
2615                set_sense(&dev->fsa_dev[cid].sense_data,
2616                  HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2617                  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2618                memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2619                       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2620                             SCSI_SENSE_BUFFERSIZE));
2621        }
2622
2623        aac_fib_complete(fibptr);
2624        aac_fib_free(fibptr);
2625        cmd->scsi_done(cmd);
2626}
2627
2628static int aac_synchronize(struct scsi_cmnd *scsicmd)
2629{
2630        int status;
2631        struct fib *cmd_fibcontext;
2632        struct aac_synchronize *synchronizecmd;
2633        struct scsi_device *sdev = scsicmd->device;
2634        struct aac_dev *aac;
2635
2636        aac = (struct aac_dev *)sdev->host->hostdata;
2637        if (aac->in_reset)
2638                return SCSI_MLQUEUE_HOST_BUSY;
2639
2640        /*
2641         *      Allocate and initialize a Fib
2642         */
2643        cmd_fibcontext = aac_fib_alloc_tag(aac, scsicmd);
2644
2645        aac_fib_init(cmd_fibcontext);
2646
2647        synchronizecmd = fib_data(cmd_fibcontext);
2648        synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
2649        synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
2650        synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
2651        synchronizecmd->count =
2652             cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
2653        scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2654
2655        /*
2656         *      Now send the Fib to the adapter
2657         */
2658        status = aac_fib_send(ContainerCommand,
2659                  cmd_fibcontext,
2660                  sizeof(struct aac_synchronize),
2661                  FsaNormal,
2662                  0, 1,
2663                  (fib_callback)synchronize_callback,
2664                  (void *)scsicmd);
2665
2666        /*
2667         *      Check that the command queued to the controller
2668         */
2669        if (status == -EINPROGRESS)
2670                return 0;
2671
2672        printk(KERN_WARNING
2673                "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
2674        aac_fib_complete(cmd_fibcontext);
2675        aac_fib_free(cmd_fibcontext);
2676        return SCSI_MLQUEUE_HOST_BUSY;
2677}
2678
2679static void aac_start_stop_callback(void *context, struct fib *fibptr)
2680{
2681        struct scsi_cmnd *scsicmd = context;
2682
2683        if (!aac_valid_context(scsicmd, fibptr))
2684                return;
2685
2686        BUG_ON(fibptr == NULL);
2687
2688        scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
2689
2690        aac_fib_complete(fibptr);
2691        aac_fib_free(fibptr);
2692        scsicmd->scsi_done(scsicmd);
2693}
2694
2695static int aac_start_stop(struct scsi_cmnd *scsicmd)
2696{
2697        int status;
2698        struct fib *cmd_fibcontext;
2699        struct aac_power_management *pmcmd;
2700        struct scsi_device *sdev = scsicmd->device;
2701        struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
2702
2703        if (!(aac->supplement_adapter_info.supported_options2 &
2704              AAC_OPTION_POWER_MANAGEMENT)) {
2705                scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
2706                scsicmd->scsi_done(scsicmd);
2707                return 0;
2708        }
2709
2710        if (aac->in_reset)
2711                return SCSI_MLQUEUE_HOST_BUSY;
2712
2713        /*
2714         *      Allocate and initialize a Fib
2715         */
2716        cmd_fibcontext = aac_fib_alloc_tag(aac, scsicmd);
2717
2718        aac_fib_init(cmd_fibcontext);
2719
2720        pmcmd = fib_data(cmd_fibcontext);
2721        pmcmd->command = cpu_to_le32(VM_ContainerConfig);
2722        pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
2723        /* Eject bit ignored, not relevant */
2724        pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
2725                cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
2726        pmcmd->cid = cpu_to_le32(sdev_id(sdev));
2727        pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
2728                cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
2729        scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2730
2731        /*
2732         *      Now send the Fib to the adapter
2733         */
2734        status = aac_fib_send(ContainerCommand,
2735                  cmd_fibcontext,
2736                  sizeof(struct aac_power_management),
2737                  FsaNormal,
2738                  0, 1,
2739                  (fib_callback)aac_start_stop_callback,
2740                  (void *)scsicmd);
2741
2742        /*
2743         *      Check that the command queued to the controller
2744         */
2745        if (status == -EINPROGRESS)
2746                return 0;
2747
2748        aac_fib_complete(cmd_fibcontext);
2749        aac_fib_free(cmd_fibcontext);
2750        return SCSI_MLQUEUE_HOST_BUSY;
2751}
2752
2753/**
2754 *      aac_scsi_cmd()          -       Process SCSI command
2755 *      @scsicmd:               SCSI command block
2756 *
2757 *      Emulate a SCSI command and queue the required request for the
2758 *      aacraid firmware.
2759 */
2760
2761int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
2762{
2763        u32 cid, bus;
2764        struct Scsi_Host *host = scsicmd->device->host;
2765        struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2766        struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
2767
2768        if (fsa_dev_ptr == NULL)
2769                return -1;
2770        /*
2771         *      If the bus, id or lun is out of range, return fail
2772         *      Test does not apply to ID 16, the pseudo id for the controller
2773         *      itself.
2774         */
2775        cid = scmd_id(scsicmd);
2776        if (cid != host->this_id) {
2777                if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
2778                        if((cid >= dev->maximum_num_containers) ||
2779                                        (scsicmd->device->lun != 0)) {
2780                                scsicmd->result = DID_NO_CONNECT << 16;
2781                                goto scsi_done_ret;
2782                        }
2783
2784                        /*
2785                         *      If the target container doesn't exist, it may have
2786                         *      been newly created
2787                         */
2788                        if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
2789                          (fsa_dev_ptr[cid].sense_data.sense_key ==
2790                           NOT_READY)) {
2791                                switch (scsicmd->cmnd[0]) {
2792                                case SERVICE_ACTION_IN_16:
2793                                        if (!(dev->raw_io_interface) ||
2794                                            !(dev->raw_io_64) ||
2795                                            ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2796                                                break;
2797                                        fallthrough;
2798                                case INQUIRY:
2799                                case READ_CAPACITY:
2800                                case TEST_UNIT_READY:
2801                                        if (dev->in_reset)
2802                                                return -1;
2803                                        return _aac_probe_container(scsicmd,
2804                                                        aac_probe_container_callback2);
2805                                default:
2806                                        break;
2807                                }
2808                        }
2809                } else {  /* check for physical non-dasd devices */
2810                        bus = aac_logical_to_phys(scmd_channel(scsicmd));
2811
2812                        if (bus < AAC_MAX_BUSES && cid < AAC_MAX_TARGETS &&
2813                                dev->hba_map[bus][cid].devtype
2814                                        == AAC_DEVTYPE_NATIVE_RAW) {
2815                                if (dev->in_reset)
2816                                        return -1;
2817                                return aac_send_hba_fib(scsicmd);
2818                        } else if (dev->nondasd_support || expose_physicals ||
2819                                dev->jbod) {
2820                                if (dev->in_reset)
2821                                        return -1;
2822                                return aac_send_srb_fib(scsicmd);
2823                        } else {
2824                                scsicmd->result = DID_NO_CONNECT << 16;
2825                                goto scsi_done_ret;
2826                        }
2827                }
2828        }
2829        /*
2830         * else Command for the controller itself
2831         */
2832        else if ((scsicmd->cmnd[0] != INQUIRY) &&       /* only INQUIRY & TUR cmnd supported for controller */
2833                (scsicmd->cmnd[0] != TEST_UNIT_READY))
2834        {
2835                dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
2836                scsicmd->result = DID_OK << 16 | SAM_STAT_CHECK_CONDITION;
2837                set_sense(&dev->fsa_dev[cid].sense_data,
2838                  ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2839                  ASENCODE_INVALID_COMMAND, 0, 0);
2840                memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2841                       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2842                             SCSI_SENSE_BUFFERSIZE));
2843                goto scsi_done_ret;
2844        }
2845
2846        switch (scsicmd->cmnd[0]) {
2847        case READ_6:
2848        case READ_10:
2849        case READ_12:
2850        case READ_16:
2851                if (dev->in_reset)
2852                        return -1;
2853                return aac_read(scsicmd);
2854
2855        case WRITE_6:
2856        case WRITE_10:
2857        case WRITE_12:
2858        case WRITE_16:
2859                if (dev->in_reset)
2860                        return -1;
2861                return aac_write(scsicmd);
2862
2863        case SYNCHRONIZE_CACHE:
2864                if (((aac_cache & 6) == 6) && dev->cache_protected) {
2865                        scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
2866                        break;
2867                }
2868                /* Issue FIB to tell Firmware to flush it's cache */
2869                if ((aac_cache & 6) != 2)
2870                        return aac_synchronize(scsicmd);
2871                fallthrough;
2872        case INQUIRY:
2873        {
2874                struct inquiry_data inq_data;
2875
2876                dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
2877                memset(&inq_data, 0, sizeof (struct inquiry_data));
2878
2879                if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
2880                        char *arr = (char *)&inq_data;
2881
2882                        /* EVPD bit set */
2883                        arr[0] = (scmd_id(scsicmd) == host->this_id) ?
2884                          INQD_PDT_PROC : INQD_PDT_DA;
2885                        if (scsicmd->cmnd[2] == 0) {
2886                                /* supported vital product data pages */
2887                                arr[3] = 3;
2888                                arr[4] = 0x0;
2889                                arr[5] = 0x80;
2890                                arr[6] = 0x83;
2891                                arr[1] = scsicmd->cmnd[2];
2892                                scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2893                                                         sizeof(inq_data));
2894                                scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
2895                        } else if (scsicmd->cmnd[2] == 0x80) {
2896                                /* unit serial number page */
2897                                arr[3] = setinqserial(dev, &arr[4],
2898                                  scmd_id(scsicmd));
2899                                arr[1] = scsicmd->cmnd[2];
2900                                scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2901                                                         sizeof(inq_data));
2902                                if (aac_wwn != 2)
2903                                        return aac_get_container_serial(
2904                                                scsicmd);
2905                                scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
2906                        } else if (scsicmd->cmnd[2] == 0x83) {
2907                                /* vpd page 0x83 - Device Identification Page */
2908                                char *sno = (char *)&inq_data;
2909                                sno[3] = setinqserial(dev, &sno[4],
2910                                                      scmd_id(scsicmd));
2911                                if (aac_wwn != 2)
2912                                        return aac_get_container_serial(
2913                                                scsicmd);
2914                                scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
2915                        } else {
2916                                /* vpd page not implemented */
2917                                scsicmd->result = DID_OK << 16 | SAM_STAT_CHECK_CONDITION;
2918                                set_sense(&dev->fsa_dev[cid].sense_data,
2919                                  ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
2920                                  ASENCODE_NO_SENSE, 7, 2);
2921                                memcpy(scsicmd->sense_buffer,
2922                                  &dev->fsa_dev[cid].sense_data,
2923                                  min_t(size_t,
2924                                        sizeof(dev->fsa_dev[cid].sense_data),
2925                                        SCSI_SENSE_BUFFERSIZE));
2926                        }
2927                        break;
2928                }
2929                inq_data.inqd_ver = 2;  /* claim compliance to SCSI-2 */
2930                inq_data.inqd_rdf = 2;  /* A response data format value of two indicates that the data shall be in the format specified in SCSI-2 */
2931                inq_data.inqd_len = 31;
2932                /*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
2933                inq_data.inqd_pad2= 0x32 ;       /*WBus16|Sync|CmdQue */
2934                /*
2935                 *      Set the Vendor, Product, and Revision Level
2936                 *      see: <vendor>.c i.e. aac.c
2937                 */
2938                if (cid == host->this_id) {
2939                        setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
2940                        inq_data.inqd_pdt = INQD_PDT_PROC;      /* Processor device */
2941                        scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2942                                                 sizeof(inq_data));
2943                        scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
2944                        break;
2945                }
2946                if (dev->in_reset)
2947                        return -1;
2948                setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
2949                inq_data.inqd_pdt = INQD_PDT_DA;        /* Direct/random access device */
2950                scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
2951                return aac_get_container_name(scsicmd);
2952        }
2953        case SERVICE_ACTION_IN_16:
2954                if (!(dev->raw_io_interface) ||
2955                    !(dev->raw_io_64) ||
2956                    ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2957                        break;
2958        {
2959                u64 capacity;
2960                char cp[13];
2961                unsigned int alloc_len;
2962
2963                dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2964                capacity = fsa_dev_ptr[cid].size - 1;
2965                cp[0] = (capacity >> 56) & 0xff;
2966                cp[1] = (capacity >> 48) & 0xff;
2967                cp[2] = (capacity >> 40) & 0xff;
2968                cp[3] = (capacity >> 32) & 0xff;
2969                cp[4] = (capacity >> 24) & 0xff;
2970                cp[5] = (capacity >> 16) & 0xff;
2971                cp[6] = (capacity >> 8) & 0xff;
2972                cp[7] = (capacity >> 0) & 0xff;
2973                cp[8] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2974                cp[9] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2975                cp[10] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2976                cp[11] = (fsa_dev_ptr[cid].block_size) & 0xff;
2977                cp[12] = 0;
2978
2979                alloc_len = ((scsicmd->cmnd[10] << 24)
2980                             + (scsicmd->cmnd[11] << 16)
2981                             + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
2982
2983                alloc_len = min_t(size_t, alloc_len, sizeof(cp));
2984                scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
2985                if (alloc_len < scsi_bufflen(scsicmd))
2986                        scsi_set_resid(scsicmd,
2987                                       scsi_bufflen(scsicmd) - alloc_len);
2988
2989                /* Do not cache partition table for arrays */
2990                scsicmd->device->removable = 1;
2991
2992                scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
2993                break;
2994        }
2995
2996        case READ_CAPACITY:
2997        {
2998                u32 capacity;
2999                char cp[8];
3000
3001                dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
3002                if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3003                        capacity = fsa_dev_ptr[cid].size - 1;
3004                else
3005                        capacity = (u32)-1;
3006
3007                cp[0] = (capacity >> 24) & 0xff;
3008                cp[1] = (capacity >> 16) & 0xff;
3009                cp[2] = (capacity >> 8) & 0xff;
3010                cp[3] = (capacity >> 0) & 0xff;
3011                cp[4] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
3012                cp[5] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3013                cp[6] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3014                cp[7] = (fsa_dev_ptr[cid].block_size) & 0xff;
3015                scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
3016                /* Do not cache partition table for arrays */
3017                scsicmd->device->removable = 1;
3018                scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
3019                break;
3020        }
3021
3022        case MODE_SENSE:
3023        {
3024                int mode_buf_length = 4;
3025                u32 capacity;
3026                aac_modep_data mpd;
3027
3028                if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3029                        capacity = fsa_dev_ptr[cid].size - 1;
3030                else
3031                        capacity = (u32)-1;
3032
3033                dprintk((KERN_DEBUG "MODE SENSE command.\n"));
3034                memset((char *)&mpd, 0, sizeof(aac_modep_data));
3035
3036                /* Mode data length */
3037                mpd.hd.data_length = sizeof(mpd.hd) - 1;
3038                /* Medium type - default */
3039                mpd.hd.med_type = 0;
3040                /* Device-specific param,
3041                   bit 8: 0/1 = write enabled/protected
3042                   bit 4: 0/1 = FUA enabled */
3043                mpd.hd.dev_par = 0;
3044
3045                if (dev->raw_io_interface && ((aac_cache & 5) != 1))
3046                        mpd.hd.dev_par = 0x10;
3047                if (scsicmd->cmnd[1] & 0x8)
3048                        mpd.hd.bd_length = 0;   /* Block descriptor length */
3049                else {
3050                        mpd.hd.bd_length = sizeof(mpd.bd);
3051                        mpd.hd.data_length += mpd.hd.bd_length;
3052                        mpd.bd.block_length[0] =
3053                                (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3054                        mpd.bd.block_length[1] =
3055                                (fsa_dev_ptr[cid].block_size >> 8) &  0xff;
3056                        mpd.bd.block_length[2] =
3057                                fsa_dev_ptr[cid].block_size  & 0xff;
3058
3059                        mpd.mpc_buf[0] = scsicmd->cmnd[2];
3060                        if (scsicmd->cmnd[2] == 0x1C) {
3061                                /* page length */
3062                                mpd.mpc_buf[1] = 0xa;
3063                                /* Mode data length */
3064                                mpd.hd.data_length = 23;
3065                        } else {
3066                                /* Mode data length */
3067                                mpd.hd.data_length = 15;
3068                        }
3069
3070                        if (capacity > 0xffffff) {
3071                                mpd.bd.block_count[0] = 0xff;
3072                                mpd.bd.block_count[1] = 0xff;
3073                                mpd.bd.block_count[2] = 0xff;
3074                        } else {
3075                                mpd.bd.block_count[0] = (capacity >> 16) & 0xff;
3076                                mpd.bd.block_count[1] = (capacity >> 8) & 0xff;
3077                                mpd.bd.block_count[2] = capacity  & 0xff;
3078                        }
3079                }
3080                if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
3081                  ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
3082                        mpd.hd.data_length += 3;
3083                        mpd.mpc_buf[0] = 8;
3084                        mpd.mpc_buf[1] = 1;
3085                        mpd.mpc_buf[2] = ((aac_cache & 6) == 2)
3086                                ? 0 : 0x04; /* WCE */
3087                        mode_buf_length = sizeof(mpd);
3088                }
3089
3090                if (mode_buf_length > scsicmd->cmnd[4])
3091                        mode_buf_length = scsicmd->cmnd[4];
3092                else
3093                        mode_buf_length = sizeof(mpd);
3094                scsi_sg_copy_from_buffer(scsicmd,
3095                                         (char *)&mpd,
3096                                         mode_buf_length);
3097                scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
3098                break;
3099        }
3100        case MODE_SENSE_10:
3101        {
3102                u32 capacity;
3103                int mode_buf_length = 8;
3104                aac_modep10_data mpd10;
3105
3106                if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3107                        capacity = fsa_dev_ptr[cid].size - 1;
3108                else
3109                        capacity = (u32)-1;
3110
3111                dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
3112                memset((char *)&mpd10, 0, sizeof(aac_modep10_data));
3113                /* Mode data length (MSB) */
3114                mpd10.hd.data_length[0] = 0;
3115                /* Mode data length (LSB) */
3116                mpd10.hd.data_length[1] = sizeof(mpd10.hd) - 1;
3117                /* Medium type - default */
3118                mpd10.hd.med_type = 0;
3119                /* Device-specific param,
3120                   bit 8: 0/1 = write enabled/protected
3121                   bit 4: 0/1 = FUA enabled */
3122                mpd10.hd.dev_par = 0;
3123
3124                if (dev->raw_io_interface && ((aac_cache & 5) != 1))
3125                        mpd10.hd.dev_par = 0x10;
3126                mpd10.hd.rsrvd[0] = 0;  /* reserved */
3127                mpd10.hd.rsrvd[1] = 0;  /* reserved */
3128                if (scsicmd->cmnd[1] & 0x8) {
3129                        /* Block descriptor length (MSB) */
3130                        mpd10.hd.bd_length[0] = 0;
3131                        /* Block descriptor length (LSB) */
3132                        mpd10.hd.bd_length[1] = 0;
3133                } else {
3134                        mpd10.hd.bd_length[0] = 0;
3135                        mpd10.hd.bd_length[1] = sizeof(mpd10.bd);
3136
3137                        mpd10.hd.data_length[1] += mpd10.hd.bd_length[1];
3138
3139                        mpd10.bd.block_length[0] =
3140                                (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3141                        mpd10.bd.block_length[1] =
3142                                (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3143                        mpd10.bd.block_length[2] =
3144                                fsa_dev_ptr[cid].block_size  & 0xff;
3145
3146                        if (capacity > 0xffffff) {
3147                                mpd10.bd.block_count[0] = 0xff;
3148                                mpd10.bd.block_count[1] = 0xff;
3149                                mpd10.bd.block_count[2] = 0xff;
3150                        } else {
3151                                mpd10.bd.block_count[0] =
3152                                        (capacity >> 16) & 0xff;
3153                                mpd10.bd.block_count[1] =
3154                                        (capacity >> 8) & 0xff;
3155                                mpd10.bd.block_count[2] =
3156                                        capacity  & 0xff;
3157                        }
3158                }
3159                if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
3160                  ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
3161                        mpd10.hd.data_length[1] += 3;
3162                        mpd10.mpc_buf[0] = 8;
3163                        mpd10.mpc_buf[1] = 1;
3164                        mpd10.mpc_buf[2] = ((aac_cache & 6) == 2)
3165                                ? 0 : 0x04; /* WCE */
3166                        mode_buf_length = sizeof(mpd10);
3167                        if (mode_buf_length > scsicmd->cmnd[8])
3168                                mode_buf_length = scsicmd->cmnd[8];
3169                }
3170                scsi_sg_copy_from_buffer(scsicmd,
3171                                         (char *)&mpd10,
3172                                         mode_buf_length);
3173
3174                scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
3175                break;
3176        }
3177        case REQUEST_SENSE:
3178                dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
3179                memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
3180                                sizeof(struct sense_data));
3181                memset(&dev->fsa_dev[cid].sense_data, 0,
3182                                sizeof(struct sense_data));
3183                scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
3184                break;
3185
3186        case ALLOW_MEDIUM_REMOVAL:
3187                dprintk((KERN_DEBUG "LOCK command.\n"));
3188                if (scsicmd->cmnd[4])
3189                        fsa_dev_ptr[cid].locked = 1;
3190                else
3191                        fsa_dev_ptr[cid].locked = 0;
3192
3193                scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
3194                break;
3195        /*
3196         *      These commands are all No-Ops
3197         */
3198        case TEST_UNIT_READY:
3199                if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
3200                        scsicmd->result = DID_OK << 16 | SAM_STAT_CHECK_CONDITION;
3201                        set_sense(&dev->fsa_dev[cid].sense_data,
3202                                  NOT_READY, SENCODE_BECOMING_READY,
3203                                  ASENCODE_BECOMING_READY, 0, 0);
3204                        memcpy(scsicmd->sense_buffer,
3205                               &dev->fsa_dev[cid].sense_data,
3206                               min_t(size_t,
3207                                     sizeof(dev->fsa_dev[cid].sense_data),
3208                                     SCSI_SENSE_BUFFERSIZE));
3209                        break;
3210                }
3211                fallthrough;
3212        case RESERVE:
3213        case RELEASE:
3214        case REZERO_UNIT:
3215        case REASSIGN_BLOCKS:
3216        case SEEK_10:
3217                scsicmd->result = DID_OK << 16 | SAM_STAT_GOOD;
3218                break;
3219
3220        case START_STOP:
3221                return aac_start_stop(scsicmd);
3222
3223        default:
3224        /*
3225         *      Unhandled commands
3226         */
3227                dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n",
3228                                scsicmd->cmnd[0]));
3229                scsicmd->result = DID_OK << 16 | SAM_STAT_CHECK_CONDITION;
3230                set_sense(&dev->fsa_dev[cid].sense_data,
3231                          ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
3232                          ASENCODE_INVALID_COMMAND, 0, 0);
3233                memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
3234                                min_t(size_t,
3235                                      sizeof(dev->fsa_dev[cid].sense_data),
3236                                      SCSI_SENSE_BUFFERSIZE));
3237        }
3238
3239scsi_done_ret:
3240
3241        scsicmd->scsi_done(scsicmd);
3242        return 0;
3243}
3244
3245static int query_disk(struct aac_dev *dev, void __user *arg)
3246{
3247        struct aac_query_disk qd;
3248        struct fsa_dev_info *fsa_dev_ptr;
3249
3250        fsa_dev_ptr = dev->fsa_dev;
3251        if (!fsa_dev_ptr)
3252                return -EBUSY;
3253        if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
3254                return -EFAULT;
3255        if (qd.cnum == -1) {
3256                if (qd.id < 0 || qd.id >= dev->maximum_num_containers)
3257                        return -EINVAL;
3258                qd.cnum = qd.id;
3259        } else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1)) {
3260                if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
3261                        return -EINVAL;
3262                qd.instance = dev->scsi_host_ptr->host_no;
3263                qd.bus = 0;
3264                qd.id = CONTAINER_TO_ID(qd.cnum);
3265                qd.lun = CONTAINER_TO_LUN(qd.cnum);
3266        }
3267        else return -EINVAL;
3268
3269        qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
3270        qd.locked = fsa_dev_ptr[qd.cnum].locked;
3271        qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
3272
3273        if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
3274                qd.unmapped = 1;
3275        else
3276                qd.unmapped = 0;
3277
3278        strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
3279          min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
3280
3281        if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
3282                return -EFAULT;
3283        return 0;
3284}
3285
3286static int force_delete_disk(struct aac_dev *dev, void __user *arg)
3287{
3288        struct aac_delete_disk dd;
3289        struct fsa_dev_info *fsa_dev_ptr;
3290
3291        fsa_dev_ptr = dev->fsa_dev;
3292        if (!fsa_dev_ptr)
3293                return -EBUSY;
3294
3295        if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3296                return -EFAULT;
3297
3298        if (dd.cnum >= dev->maximum_num_containers)
3299                return -EINVAL;
3300        /*
3301         *      Mark this container as being deleted.
3302         */
3303        fsa_dev_ptr[dd.cnum].deleted = 1;
3304        /*
3305         *      Mark the container as no longer valid
3306         */
3307        fsa_dev_ptr[dd.cnum].valid = 0;
3308        return 0;
3309}
3310
3311static int delete_disk(struct aac_dev *dev, void __user *arg)
3312{
3313        struct aac_delete_disk dd;
3314        struct fsa_dev_info *fsa_dev_ptr;
3315
3316        fsa_dev_ptr = dev->fsa_dev;
3317        if (!fsa_dev_ptr)
3318                return -EBUSY;
3319
3320        if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3321                return -EFAULT;
3322
3323        if (dd.cnum >= dev->maximum_num_containers)
3324                return -EINVAL;
3325        /*
3326         *      If the container is locked, it can not be deleted by the API.
3327         */
3328        if (fsa_dev_ptr[dd.cnum].locked)
3329                return -EBUSY;
3330        else {
3331                /*
3332                 *      Mark the container as no longer being valid.
3333                 */
3334                fsa_dev_ptr[dd.cnum].valid = 0;
3335                fsa_dev_ptr[dd.cnum].devname[0] = '\0';
3336                return 0;
3337        }
3338}
3339
3340int aac_dev_ioctl(struct aac_dev *dev, unsigned int cmd, void __user *arg)
3341{
3342        switch (cmd) {
3343        case FSACTL_QUERY_DISK:
3344                return query_disk(dev, arg);
3345        case FSACTL_DELETE_DISK:
3346                return delete_disk(dev, arg);
3347        case FSACTL_FORCE_DELETE_DISK:
3348                return force_delete_disk(dev, arg);
3349        case FSACTL_GET_CONTAINERS:
3350                return aac_get_containers(dev);
3351        default:
3352                return -ENOTTY;
3353        }
3354}
3355
3356/**
3357 * aac_srb_callback
3358 * @context: the context set in the fib - here it is scsi cmd
3359 * @fibptr: pointer to the fib
3360 *
3361 * Handles the completion of a scsi command to a non dasd device
3362 */
3363static void aac_srb_callback(void *context, struct fib * fibptr)
3364{
3365        struct aac_srb_reply *srbreply;
3366        struct scsi_cmnd *scsicmd;
3367
3368        scsicmd = (struct scsi_cmnd *) context;
3369
3370        if (!aac_valid_context(scsicmd, fibptr))
3371                return;
3372
3373        BUG_ON(fibptr == NULL);
3374
3375        srbreply = (struct aac_srb_reply *) fib_data(fibptr);
3376
3377        scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
3378
3379        if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3380                /* fast response */
3381                srbreply->srb_status = cpu_to_le32(SRB_STATUS_SUCCESS);
3382                srbreply->scsi_status = cpu_to_le32(SAM_STAT_GOOD);
3383        } else {
3384                /*
3385                 *      Calculate resid for sg
3386                 */
3387                scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
3388                                   - le32_to_cpu(srbreply->data_xfer_length));
3389        }
3390
3391
3392        scsi_dma_unmap(scsicmd);
3393
3394        /* expose physical device if expose_physicald flag is on */
3395        if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
3396          && expose_physicals > 0)
3397                aac_expose_phy_device(scsicmd);
3398
3399        /*
3400         * First check the fib status
3401         */
3402
3403        if (le32_to_cpu(srbreply->status) != ST_OK) {
3404                int len;
3405
3406                pr_warn("aac_srb_callback: srb failed, status = %d\n",
3407                                le32_to_cpu(srbreply->status));
3408                len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3409                            SCSI_SENSE_BUFFERSIZE);
3410                scsicmd->result = DID_ERROR << 16 | SAM_STAT_CHECK_CONDITION;
3411                memcpy(scsicmd->sense_buffer,
3412                                srbreply->sense_data, len);
3413        }
3414
3415        /*
3416         * Next check the srb status
3417         */
3418        switch ((le32_to_cpu(srbreply->srb_status))&0x3f) {
3419        case SRB_STATUS_ERROR_RECOVERY:
3420        case SRB_STATUS_PENDING:
3421        case SRB_STATUS_SUCCESS:
3422                scsicmd->result = DID_OK << 16;
3423                break;
3424        case SRB_STATUS_DATA_OVERRUN:
3425                switch (scsicmd->cmnd[0]) {
3426                case  READ_6:
3427                case  WRITE_6:
3428                case  READ_10:
3429                case  WRITE_10:
3430                case  READ_12:
3431                case  WRITE_12:
3432                case  READ_16:
3433                case  WRITE_16:
3434                        if (le32_to_cpu(srbreply->data_xfer_length)
3435                                                < scsicmd->underflow)
3436                                pr_warn("aacraid: SCSI CMD underflow\n");
3437                        else
3438                                pr_warn("aacraid: SCSI CMD Data Overrun\n");
3439                        scsicmd->result = DID_ERROR << 16;
3440                        break;
3441                case INQUIRY:
3442                        scsicmd->result = DID_OK << 16;
3443                        break;
3444                default:
3445                        scsicmd->result = DID_OK << 16;
3446                        break;
3447                }
3448                break;
3449        case SRB_STATUS_ABORTED:
3450                scsicmd->result = DID_ABORT << 16;
3451                break;
3452        case SRB_STATUS_ABORT_FAILED:
3453                /*
3454                 * Not sure about this one - but assuming the
3455                 * hba was trying to abort for some reason
3456                 */
3457                scsicmd->result = DID_ERROR << 16;
3458                break;
3459        case SRB_STATUS_PARITY_ERROR:
3460                scsicmd->result = DID_PARITY << 16;
3461                break;
3462        case SRB_STATUS_NO_DEVICE:
3463        case SRB_STATUS_INVALID_PATH_ID:
3464        case SRB_STATUS_INVALID_TARGET_ID:
3465        case SRB_STATUS_INVALID_LUN:
3466        case SRB_STATUS_SELECTION_TIMEOUT:
3467                scsicmd->result = DID_NO_CONNECT << 16;
3468                break;
3469
3470        case SRB_STATUS_COMMAND_TIMEOUT:
3471        case SRB_STATUS_TIMEOUT:
3472                scsicmd->result = DID_TIME_OUT << 16;
3473                break;
3474
3475        case SRB_STATUS_BUSY:
3476                scsicmd->result = DID_BUS_BUSY << 16;
3477                break;
3478
3479        case SRB_STATUS_BUS_RESET:
3480                scsicmd->result = DID_RESET << 16;
3481                break;
3482
3483        case SRB_STATUS_MESSAGE_REJECTED:
3484                scsicmd->result = DID_ERROR << 16;
3485                break;
3486        case SRB_STATUS_REQUEST_FLUSHED:
3487        case SRB_STATUS_ERROR:
3488        case SRB_STATUS_INVALID_REQUEST:
3489        case SRB_STATUS_REQUEST_SENSE_FAILED:
3490        case SRB_STATUS_NO_HBA:
3491        case SRB_STATUS_UNEXPECTED_BUS_FREE:
3492        case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
3493        case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
3494        case SRB_STATUS_DELAYED_RETRY:
3495        case SRB_STATUS_BAD_FUNCTION:
3496        case SRB_STATUS_NOT_STARTED:
3497        case SRB_STATUS_NOT_IN_USE:
3498        case SRB_STATUS_FORCE_ABORT:
3499        case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
3500        default:
3501#ifdef AAC_DETAILED_STATUS_INFO
3502                pr_info("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x -scsi status 0x%x\n",
3503                        le32_to_cpu(srbreply->srb_status) & 0x3F,
3504                        aac_get_status_string(
3505                                le32_to_cpu(srbreply->srb_status) & 0x3F),
3506                        scsicmd->cmnd[0],
3507                        le32_to_cpu(srbreply->scsi_status));
3508#endif
3509                /*
3510                 * When the CC bit is SET by the host in ATA pass thru CDB,
3511                 *  driver is supposed to return DID_OK
3512                 *
3513                 * When the CC bit is RESET by the host, driver should
3514                 *  return DID_ERROR
3515                 */
3516                if ((scsicmd->cmnd[0] == ATA_12)
3517                        || (scsicmd->cmnd[0] == ATA_16)) {
3518
3519                        if (scsicmd->cmnd[2] & (0x01 << 5)) {
3520                                scsicmd->result = DID_OK << 16;
3521                        } else {
3522                                scsicmd->result = DID_ERROR << 16;
3523                        }
3524                } else {
3525                        scsicmd->result = DID_ERROR << 16;
3526                }
3527                break;
3528        }
3529        if (le32_to_cpu(srbreply->scsi_status)
3530                        == SAM_STAT_CHECK_CONDITION) {
3531                int len;
3532
3533                scsicmd->result |= SAM_STAT_CHECK_CONDITION;
3534                len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3535                            SCSI_SENSE_BUFFERSIZE);
3536#ifdef AAC_DETAILED_STATUS_INFO
3537                pr_warn("aac_srb_callback: check condition, status = %d len=%d\n",
3538                                        le32_to_cpu(srbreply->status), len);
3539#endif
3540                memcpy(scsicmd->sense_buffer,
3541                                srbreply->sense_data, len);
3542        }
3543
3544        /*
3545         * OR in the scsi status (already shifted up a bit)
3546         */
3547        scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
3548
3549        aac_fib_complete(fibptr);
3550        scsicmd->scsi_done(scsicmd);
3551}
3552
3553static void hba_resp_task_complete(struct aac_dev *dev,
3554                                        struct scsi_cmnd *scsicmd,
3555                                        struct aac_hba_resp *err) {
3556
3557        scsicmd->result = err->status;
3558        /* set residual count */
3559        scsi_set_resid(scsicmd, le32_to_cpu(err->residual_count));
3560
3561        switch (err->status) {
3562        case SAM_STAT_GOOD:
3563                scsicmd->result |= DID_OK << 16;
3564                break;
3565        case SAM_STAT_CHECK_CONDITION:
3566        {
3567                int len;
3568
3569                len = min_t(u8, err->sense_response_data_len,
3570                        SCSI_SENSE_BUFFERSIZE);
3571                if (len)
3572                        memcpy(scsicmd->sense_buffer,
3573                                err->sense_response_buf, len);
3574                scsicmd->result |= DID_OK << 16;
3575                break;
3576        }
3577        case SAM_STAT_BUSY:
3578                scsicmd->result |= DID_BUS_BUSY << 16;
3579                break;
3580        case SAM_STAT_TASK_ABORTED:
3581                scsicmd->result |= DID_ABORT << 16;
3582                break;
3583        case SAM_STAT_RESERVATION_CONFLICT:
3584        case SAM_STAT_TASK_SET_FULL:
3585        default:
3586                scsicmd->result |= DID_ERROR << 16;
3587                break;
3588        }
3589}
3590
3591static void hba_resp_task_failure(struct aac_dev *dev,
3592                                        struct scsi_cmnd *scsicmd,
3593                                        struct aac_hba_resp *err)
3594{
3595        switch (err->status) {
3596        case HBA_RESP_STAT_HBAMODE_DISABLED:
3597        {
3598                u32 bus, cid;
3599
3600                bus = aac_logical_to_phys(scmd_channel(scsicmd));
3601                cid = scmd_id(scsicmd);
3602                if (dev->hba_map[bus][cid].devtype == AAC_DEVTYPE_NATIVE_RAW) {
3603                        dev->hba_map[bus][cid].devtype = AAC_DEVTYPE_ARC_RAW;
3604                        dev->hba_map[bus][cid].rmw_nexus = 0xffffffff;
3605                }
3606                scsicmd->result = DID_NO_CONNECT << 16;
3607                break;
3608        }
3609        case HBA_RESP_STAT_IO_ERROR:
3610        case HBA_RESP_STAT_NO_PATH_TO_DEVICE:
3611                scsicmd->result = DID_OK << 16 | SAM_STAT_BUSY;
3612                break;
3613        case HBA_RESP_STAT_IO_ABORTED:
3614                scsicmd->result = DID_ABORT << 16;
3615                break;
3616        case HBA_RESP_STAT_INVALID_DEVICE:
3617                scsicmd->result = DID_NO_CONNECT << 16;
3618                break;
3619        case HBA_RESP_STAT_UNDERRUN:
3620                /* UNDERRUN is OK */
3621                scsicmd->result = DID_OK << 16;
3622                break;
3623        case HBA_RESP_STAT_OVERRUN:
3624        default:
3625                scsicmd->result = DID_ERROR << 16;
3626                break;
3627        }
3628}
3629
3630/**
3631 * aac_hba_callback
3632 * @context: the context set in the fib - here it is scsi cmd
3633 * @fibptr: pointer to the fib
3634 *
3635 * Handles the completion of a native HBA scsi command
3636 */
3637void aac_hba_callback(void *context, struct fib *fibptr)
3638{
3639        struct aac_dev *dev;
3640        struct scsi_cmnd *scsicmd;
3641
3642        struct aac_hba_resp *err =
3643                        &((struct aac_native_hba *)fibptr->hw_fib_va)->resp.err;
3644
3645        scsicmd = (struct scsi_cmnd *) context;
3646
3647        if (!aac_valid_context(scsicmd, fibptr))
3648                return;
3649
3650        WARN_ON(fibptr == NULL);
3651        dev = fibptr->dev;
3652
3653        if (!(fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA_TMF))
3654                scsi_dma_unmap(scsicmd);
3655
3656        if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3657                /* fast response */
3658                scsicmd->result = DID_OK << 16;
3659                goto out;
3660        }
3661
3662        switch (err->service_response) {
3663        case HBA_RESP_SVCRES_TASK_COMPLETE:
3664                hba_resp_task_complete(dev, scsicmd, err);
3665                break;
3666        case HBA_RESP_SVCRES_FAILURE:
3667                hba_resp_task_failure(dev, scsicmd, err);
3668                break;
3669        case HBA_RESP_SVCRES_TMF_REJECTED:
3670                scsicmd->result = DID_ERROR << 16;
3671                break;
3672        case HBA_RESP_SVCRES_TMF_LUN_INVALID:
3673                scsicmd->result = DID_NO_CONNECT << 16;
3674                break;
3675        case HBA_RESP_SVCRES_TMF_COMPLETE:
3676        case HBA_RESP_SVCRES_TMF_SUCCEEDED:
3677                scsicmd->result = DID_OK << 16;
3678                break;
3679        default:
3680                scsicmd->result = DID_ERROR << 16;
3681                break;
3682        }
3683
3684out:
3685        aac_fib_complete(fibptr);
3686
3687        if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA_TMF)
3688                scsicmd->SCp.sent_command = 1;
3689        else
3690                scsicmd->scsi_done(scsicmd);
3691}
3692
3693/**
3694 * aac_send_srb_fib
3695 * @scsicmd: the scsi command block
3696 *
3697 * This routine will form a FIB and fill in the aac_srb from the
3698 * scsicmd passed in.
3699 */
3700static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
3701{
3702        struct fib* cmd_fibcontext;
3703        struct aac_dev* dev;
3704        int status;
3705
3706        dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3707        if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
3708                        scsicmd->device->lun > 7) {
3709                scsicmd->result = DID_NO_CONNECT << 16;
3710                scsicmd->scsi_done(scsicmd);
3711                return 0;
3712        }
3713
3714        /*
3715         *      Allocate and initialize a Fib then setup a BlockWrite command
3716         */
3717        cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3718        scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
3719        status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
3720
3721        /*
3722         *      Check that the command queued to the controller
3723         */
3724        if (status == -EINPROGRESS)
3725                return 0;
3726
3727        printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
3728        aac_fib_complete(cmd_fibcontext);
3729        aac_fib_free(cmd_fibcontext);
3730
3731        return -1;
3732}
3733
3734/**
3735 * aac_send_hba_fib
3736 * @scsicmd: the scsi command block
3737 *
3738 * This routine will form a FIB and fill in the aac_hba_cmd_req from the
3739 * scsicmd passed in.
3740 */
3741static int aac_send_hba_fib(struct scsi_cmnd *scsicmd)
3742{
3743        struct fib *cmd_fibcontext;
3744        struct aac_dev *dev;
3745        int status;
3746
3747        dev = shost_priv(scsicmd->device->host);
3748        if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
3749                        scsicmd->device->lun > AAC_MAX_LUN - 1) {
3750                scsicmd->result = DID_NO_CONNECT << 16;
3751                scsicmd->scsi_done(scsicmd);
3752                return 0;
3753        }
3754
3755        /*
3756         *      Allocate and initialize a Fib then setup a BlockWrite command
3757         */
3758        cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3759        if (!cmd_fibcontext)
3760                return -1;
3761
3762        scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
3763        status = aac_adapter_hba(cmd_fibcontext, scsicmd);
3764
3765        /*
3766         *      Check that the command queued to the controller
3767         */
3768        if (status == -EINPROGRESS)
3769                return 0;
3770
3771        pr_warn("aac_hba_cmd_req: aac_fib_send failed with status: %d\n",
3772                status);
3773        aac_fib_complete(cmd_fibcontext);
3774        aac_fib_free(cmd_fibcontext);
3775
3776        return -1;
3777}
3778
3779
3780static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *psg)
3781{
3782        unsigned long byte_count = 0;
3783        int nseg;
3784        struct scatterlist *sg;
3785        int i;
3786
3787        // Get rid of old data
3788        psg->count = 0;
3789        psg->sg[0].addr = 0;
3790        psg->sg[0].count = 0;
3791
3792        nseg = scsi_dma_map(scsicmd);
3793        if (nseg <= 0)
3794                return nseg;
3795
3796        psg->count = cpu_to_le32(nseg);
3797
3798        scsi_for_each_sg(scsicmd, sg, nseg, i) {
3799                psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
3800                psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
3801                byte_count += sg_dma_len(sg);
3802        }
3803        /* hba wants the size to be exact */
3804        if (byte_count > scsi_bufflen(scsicmd)) {
3805                u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3806                        (byte_count - scsi_bufflen(scsicmd));
3807                psg->sg[i-1].count = cpu_to_le32(temp);
3808                byte_count = scsi_bufflen(scsicmd);
3809        }
3810        /* Check for command underflow */
3811        if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3812                printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3813                       byte_count, scsicmd->underflow);
3814        }
3815
3816        return byte_count;
3817}
3818
3819
3820static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg)
3821{
3822        unsigned long byte_count = 0;
3823        u64 addr;
3824        int nseg;
3825        struct scatterlist *sg;
3826        int i;
3827
3828        // Get rid of old data
3829        psg->count = 0;
3830        psg->sg[0].addr[0] = 0;
3831        psg->sg[0].addr[1] = 0;
3832        psg->sg[0].count = 0;
3833
3834        nseg = scsi_dma_map(scsicmd);
3835        if (nseg <= 0)
3836                return nseg;
3837
3838        scsi_for_each_sg(scsicmd, sg, nseg, i) {
3839                int count = sg_dma_len(sg);
3840                addr = sg_dma_address(sg);
3841                psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
3842                psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
3843                psg->sg[i].count = cpu_to_le32(count);
3844                byte_count += count;
3845        }
3846        psg->count = cpu_to_le32(nseg);
3847        /* hba wants the size to be exact */
3848        if (byte_count > scsi_bufflen(scsicmd)) {
3849                u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3850                        (byte_count - scsi_bufflen(scsicmd));
3851                psg->sg[i-1].count = cpu_to_le32(temp);
3852                byte_count = scsi_bufflen(scsicmd);
3853        }
3854        /* Check for command underflow */
3855        if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3856                printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3857                       byte_count, scsicmd->underflow);
3858        }
3859
3860        return byte_count;
3861}
3862
3863static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg)
3864{
3865        unsigned long byte_count = 0;
3866        int nseg;
3867        struct scatterlist *sg;
3868        int i;
3869
3870        // Get rid of old data
3871        psg->count = 0;
3872        psg->sg[0].next = 0;
3873        psg->sg[0].prev = 0;
3874        psg->sg[0].addr[0] = 0;
3875        psg->sg[0].addr[1] = 0;
3876        psg->sg[0].count = 0;
3877        psg->sg[0].flags = 0;
3878
3879        nseg = scsi_dma_map(scsicmd);
3880        if (nseg <= 0)
3881                return nseg;
3882
3883        scsi_for_each_sg(scsicmd, sg, nseg, i) {
3884                int count = sg_dma_len(sg);
3885                u64 addr = sg_dma_address(sg);
3886                psg->sg[i].next = 0;
3887                psg->sg[i].prev = 0;
3888                psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
3889                psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
3890                psg->sg[i].count = cpu_to_le32(count);
3891                psg->sg[i].flags = 0;
3892                byte_count += count;
3893        }
3894        psg->count = cpu_to_le32(nseg);
3895        /* hba wants the size to be exact */
3896        if (byte_count > scsi_bufflen(scsicmd)) {
3897                u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3898                        (byte_count - scsi_bufflen(scsicmd));
3899                psg->sg[i-1].count = cpu_to_le32(temp);
3900                byte_count = scsi_bufflen(scsicmd);
3901        }
3902        /* Check for command underflow */
3903        if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3904                printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3905                       byte_count, scsicmd->underflow);
3906        }
3907
3908        return byte_count;
3909}
3910
3911static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
3912                                struct aac_raw_io2 *rio2, int sg_max)
3913{
3914        unsigned long byte_count = 0;
3915        int nseg;
3916        struct scatterlist *sg;
3917        int i, conformable = 0;
3918        u32 min_size = PAGE_SIZE, cur_size;
3919
3920        nseg = scsi_dma_map(scsicmd);
3921        if (nseg <= 0)
3922                return nseg;
3923
3924        scsi_for_each_sg(scsicmd, sg, nseg, i) {
3925                int count = sg_dma_len(sg);
3926                u64 addr = sg_dma_address(sg);
3927
3928                BUG_ON(i >= sg_max);
3929                rio2->sge[i].addrHigh = cpu_to_le32((u32)(addr>>32));
3930                rio2->sge[i].addrLow = cpu_to_le32((u32)(addr & 0xffffffff));
3931                cur_size = cpu_to_le32(count);
3932                rio2->sge[i].length = cur_size;
3933                rio2->sge[i].flags = 0;
3934                if (i == 0) {
3935                        conformable = 1;
3936                        rio2->sgeFirstSize = cur_size;
3937                } else if (i == 1) {
3938                        rio2->sgeNominalSize = cur_size;
3939                        min_size = cur_size;
3940                } else if ((i+1) < nseg && cur_size != rio2->sgeNominalSize) {
3941                        conformable = 0;
3942                        if (cur_size < min_size)
3943                                min_size = cur_size;
3944                }
3945                byte_count += count;
3946        }
3947
3948        /* hba wants the size to be exact */
3949        if (byte_count > scsi_bufflen(scsicmd)) {
3950                u32 temp = le32_to_cpu(rio2->sge[i-1].length) -
3951                        (byte_count - scsi_bufflen(scsicmd));
3952                rio2->sge[i-1].length = cpu_to_le32(temp);
3953                byte_count = scsi_bufflen(scsicmd);
3954        }
3955
3956        rio2->sgeCnt = cpu_to_le32(nseg);
3957        rio2->flags |= cpu_to_le16(RIO2_SG_FORMAT_IEEE1212);
3958        /* not conformable: evaluate required sg elements */
3959        if (!conformable) {
3960                int j, nseg_new = nseg, err_found;
3961                for (i = min_size / PAGE_SIZE; i >= 1; --i) {
3962                        err_found = 0;
3963                        nseg_new = 2;
3964                        for (j = 1; j < nseg - 1; ++j) {
3965                                if (rio2->sge[j].length % (i*PAGE_SIZE)) {
3966                                        err_found = 1;
3967                                        break;
3968                                }
3969                                nseg_new += (rio2->sge[j].length / (i*PAGE_SIZE));
3970                        }
3971                        if (!err_found)
3972                                break;
3973                }
3974                if (i > 0 && nseg_new <= sg_max) {
3975                        int ret = aac_convert_sgraw2(rio2, i, nseg, nseg_new);
3976
3977                        if (ret < 0)
3978                                return ret;
3979                }
3980        } else
3981                rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
3982
3983        /* Check for command underflow */
3984        if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3985                printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3986                       byte_count, scsicmd->underflow);
3987        }
3988
3989        return byte_count;
3990}
3991
3992static int aac_convert_sgraw2(struct aac_raw_io2 *rio2, int pages, int nseg, int nseg_new)
3993{
3994        struct sge_ieee1212 *sge;
3995        int i, j, pos;
3996        u32 addr_low;
3997
3998        if (aac_convert_sgl == 0)
3999                return 0;
4000
4001        sge = kmalloc_array(nseg_new, sizeof(*sge), GFP_ATOMIC);
4002        if (sge == NULL)
4003                return -ENOMEM;
4004
4005        for (i = 1, pos = 1; i < nseg-1; ++i) {
4006                for (j = 0; j < rio2->sge[i].length / (pages * PAGE_SIZE); ++j) {
4007                        addr_low = rio2->sge[i].addrLow + j * pages * PAGE_SIZE;
4008                        sge[pos].addrLow = addr_low;
4009                        sge[pos].addrHigh = rio2->sge[i].addrHigh;
4010                        if (addr_low < rio2->sge[i].addrLow)
4011                                sge[pos].addrHigh++;
4012                        sge[pos].length = pages * PAGE_SIZE;
4013                        sge[pos].flags = 0;
4014                        pos++;
4015                }
4016        }
4017        sge[pos] = rio2->sge[nseg-1];
4018        memcpy(&rio2->sge[1], &sge[1], (nseg_new-1)*sizeof(struct sge_ieee1212));
4019
4020        kfree(sge);
4021        rio2->sgeCnt = cpu_to_le32(nseg_new);
4022        rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
4023        rio2->sgeNominalSize = pages * PAGE_SIZE;
4024        return 0;
4025}
4026
4027static long aac_build_sghba(struct scsi_cmnd *scsicmd,
4028                        struct aac_hba_cmd_req *hbacmd,
4029                        int sg_max,
4030                        u64 sg_address)
4031{
4032        unsigned long byte_count = 0;
4033        int nseg;
4034        struct scatterlist *sg;
4035        int i;
4036        u32 cur_size;
4037        struct aac_hba_sgl *sge;
4038
4039        nseg = scsi_dma_map(scsicmd);
4040        if (nseg <= 0) {
4041                byte_count = nseg;
4042                goto out;
4043        }
4044
4045        if (nseg > HBA_MAX_SG_EMBEDDED)
4046                sge = &hbacmd->sge[2];
4047        else
4048                sge = &hbacmd->sge[0];
4049
4050        scsi_for_each_sg(scsicmd, sg, nseg, i) {
4051                int count = sg_dma_len(sg);
4052                u64 addr = sg_dma_address(sg);
4053
4054                WARN_ON(i >= sg_max);
4055                sge->addr_hi = cpu_to_le32((u32)(addr>>32));
4056                sge->addr_lo = cpu_to_le32((u32)(addr & 0xffffffff));
4057                cur_size = cpu_to_le32(count);
4058                sge->len = cur_size;
4059                sge->flags = 0;
4060                byte_count += count;
4061                sge++;
4062        }
4063
4064        sge--;
4065        /* hba wants the size to be exact */
4066        if (byte_count > scsi_bufflen(scsicmd)) {
4067                u32 temp;
4068
4069                temp = le32_to_cpu(sge->len) - byte_count
4070                                                - scsi_bufflen(scsicmd);
4071                sge->len = cpu_to_le32(temp);
4072                byte_count = scsi_bufflen(scsicmd);
4073        }
4074
4075        if (nseg <= HBA_MAX_SG_EMBEDDED) {
4076                hbacmd->emb_data_desc_count = cpu_to_le32(nseg);
4077                sge->flags = cpu_to_le32(0x40000000);
4078        } else {
4079                /* not embedded */
4080                hbacmd->sge[0].flags = cpu_to_le32(0x80000000);
4081                hbacmd->emb_data_desc_count = (u8)cpu_to_le32(1);
4082                hbacmd->sge[0].addr_hi = (u32)cpu_to_le32(sg_address >> 32);
4083                hbacmd->sge[0].addr_lo =
4084                        cpu_to_le32((u32)(sg_address & 0xffffffff));
4085        }
4086
4087        /* Check for command underflow */
4088        if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4089                pr_warn("aacraid: cmd len %08lX cmd underflow %08X\n",
4090                                byte_count, scsicmd->underflow);
4091        }
4092out:
4093        return byte_count;
4094}
4095
4096#ifdef AAC_DETAILED_STATUS_INFO
4097
4098struct aac_srb_status_info {
4099        u32     status;
4100        char    *str;
4101};
4102
4103
4104static struct aac_srb_status_info srb_status_info[] = {
4105        { SRB_STATUS_PENDING,           "Pending Status"},
4106        { SRB_STATUS_SUCCESS,           "Success"},
4107        { SRB_STATUS_ABORTED,           "Aborted Command"},
4108        { SRB_STATUS_ABORT_FAILED,      "Abort Failed"},
4109        { SRB_STATUS_ERROR,             "Error Event"},
4110        { SRB_STATUS_BUSY,              "Device Busy"},
4111        { SRB_STATUS_INVALID_REQUEST,   "Invalid Request"},
4112        { SRB_STATUS_INVALID_PATH_ID,   "Invalid Path ID"},
4113        { SRB_STATUS_NO_DEVICE,         "No Device"},
4114        { SRB_STATUS_TIMEOUT,           "Timeout"},
4115        {