linux/drivers/scsi/ipr.c
<<
>>
Prefs
   1/*
   2 * ipr.c -- driver for IBM Power Linux RAID adapters
   3 *
   4 * Written By: Brian King <brking@us.ibm.com>, IBM Corporation
   5 *
   6 * Copyright (C) 2003, 2004 IBM Corporation
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, write to the Free Software
  20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21 *
  22 */
  23
  24/*
  25 * Notes:
  26 *
  27 * This driver is used to control the following SCSI adapters:
  28 *
  29 * IBM iSeries: 5702, 5703, 2780, 5709, 570A, 570B
  30 *
  31 * IBM pSeries: PCI-X Dual Channel Ultra 320 SCSI RAID Adapter
  32 *              PCI-X Dual Channel Ultra 320 SCSI Adapter
  33 *              PCI-X Dual Channel Ultra 320 SCSI RAID Enablement Card
  34 *              Embedded SCSI adapter on p615 and p655 systems
  35 *
  36 * Supported Hardware Features:
  37 *      - Ultra 320 SCSI controller
  38 *      - PCI-X host interface
  39 *      - Embedded PowerPC RISC Processor and Hardware XOR DMA Engine
  40 *      - Non-Volatile Write Cache
  41 *      - Supports attachment of non-RAID disks, tape, and optical devices
  42 *      - RAID Levels 0, 5, 10
  43 *      - Hot spare
  44 *      - Background Parity Checking
  45 *      - Background Data Scrubbing
  46 *      - Ability to increase the capacity of an existing RAID 5 disk array
  47 *              by adding disks
  48 *
  49 * Driver Features:
  50 *      - Tagged command queuing
  51 *      - Adapter microcode download
  52 *      - PCI hot plug
  53 *      - SCSI device hot plug
  54 *
  55 */
  56
  57#include <linux/fs.h>
  58#include <linux/init.h>
  59#include <linux/types.h>
  60#include <linux/errno.h>
  61#include <linux/kernel.h>
  62#include <linux/slab.h>
  63#include <linux/vmalloc.h>
  64#include <linux/ioport.h>
  65#include <linux/delay.h>
  66#include <linux/pci.h>
  67#include <linux/wait.h>
  68#include <linux/spinlock.h>
  69#include <linux/sched.h>
  70#include <linux/interrupt.h>
  71#include <linux/blkdev.h>
  72#include <linux/firmware.h>
  73#include <linux/module.h>
  74#include <linux/moduleparam.h>
  75#include <linux/libata.h>
  76#include <linux/hdreg.h>
  77#include <linux/reboot.h>
  78#include <linux/stringify.h>
  79#include <asm/io.h>
  80#include <asm/irq.h>
  81#include <asm/processor.h>
  82#include <scsi/scsi.h>
  83#include <scsi/scsi_host.h>
  84#include <scsi/scsi_tcq.h>
  85#include <scsi/scsi_eh.h>
  86#include <scsi/scsi_cmnd.h>
  87#include "ipr.h"
  88
  89/*
  90 *   Global Data
  91 */
  92static LIST_HEAD(ipr_ioa_head);
  93static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
  94static unsigned int ipr_max_speed = 1;
  95static int ipr_testmode = 0;
  96static unsigned int ipr_fastfail = 0;
  97static unsigned int ipr_transop_timeout = 0;
  98static unsigned int ipr_debug = 0;
  99static unsigned int ipr_max_devs = IPR_DEFAULT_SIS64_DEVS;
 100static unsigned int ipr_dual_ioa_raid = 1;
 101static unsigned int ipr_number_of_msix = 2;
 102static DEFINE_SPINLOCK(ipr_driver_lock);
 103
 104/* This table describes the differences between DMA controller chips */
 105static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
 106        { /* Gemstone, Citrine, Obsidian, and Obsidian-E */
 107                .mailbox = 0x0042C,
 108                .max_cmds = 100,
 109                .cache_line_size = 0x20,
 110                .clear_isr = 1,
 111                .iopoll_weight = 0,
 112                {
 113                        .set_interrupt_mask_reg = 0x0022C,
 114                        .clr_interrupt_mask_reg = 0x00230,
 115                        .clr_interrupt_mask_reg32 = 0x00230,
 116                        .sense_interrupt_mask_reg = 0x0022C,
 117                        .sense_interrupt_mask_reg32 = 0x0022C,
 118                        .clr_interrupt_reg = 0x00228,
 119                        .clr_interrupt_reg32 = 0x00228,
 120                        .sense_interrupt_reg = 0x00224,
 121                        .sense_interrupt_reg32 = 0x00224,
 122                        .ioarrin_reg = 0x00404,
 123                        .sense_uproc_interrupt_reg = 0x00214,
 124                        .sense_uproc_interrupt_reg32 = 0x00214,
 125                        .set_uproc_interrupt_reg = 0x00214,
 126                        .set_uproc_interrupt_reg32 = 0x00214,
 127                        .clr_uproc_interrupt_reg = 0x00218,
 128                        .clr_uproc_interrupt_reg32 = 0x00218
 129                }
 130        },
 131        { /* Snipe and Scamp */
 132                .mailbox = 0x0052C,
 133                .max_cmds = 100,
 134                .cache_line_size = 0x20,
 135                .clear_isr = 1,
 136                .iopoll_weight = 0,
 137                {
 138                        .set_interrupt_mask_reg = 0x00288,
 139                        .clr_interrupt_mask_reg = 0x0028C,
 140                        .clr_interrupt_mask_reg32 = 0x0028C,
 141                        .sense_interrupt_mask_reg = 0x00288,
 142                        .sense_interrupt_mask_reg32 = 0x00288,
 143                        .clr_interrupt_reg = 0x00284,
 144                        .clr_interrupt_reg32 = 0x00284,
 145                        .sense_interrupt_reg = 0x00280,
 146                        .sense_interrupt_reg32 = 0x00280,
 147                        .ioarrin_reg = 0x00504,
 148                        .sense_uproc_interrupt_reg = 0x00290,
 149                        .sense_uproc_interrupt_reg32 = 0x00290,
 150                        .set_uproc_interrupt_reg = 0x00290,
 151                        .set_uproc_interrupt_reg32 = 0x00290,
 152                        .clr_uproc_interrupt_reg = 0x00294,
 153                        .clr_uproc_interrupt_reg32 = 0x00294
 154                }
 155        },
 156        { /* CRoC */
 157                .mailbox = 0x00044,
 158                .max_cmds = 1000,
 159                .cache_line_size = 0x20,
 160                .clear_isr = 0,
 161                .iopoll_weight = 64,
 162                {
 163                        .set_interrupt_mask_reg = 0x00010,
 164                        .clr_interrupt_mask_reg = 0x00018,
 165                        .clr_interrupt_mask_reg32 = 0x0001C,
 166                        .sense_interrupt_mask_reg = 0x00010,
 167                        .sense_interrupt_mask_reg32 = 0x00014,
 168                        .clr_interrupt_reg = 0x00008,
 169                        .clr_interrupt_reg32 = 0x0000C,
 170                        .sense_interrupt_reg = 0x00000,
 171                        .sense_interrupt_reg32 = 0x00004,
 172                        .ioarrin_reg = 0x00070,
 173                        .sense_uproc_interrupt_reg = 0x00020,
 174                        .sense_uproc_interrupt_reg32 = 0x00024,
 175                        .set_uproc_interrupt_reg = 0x00020,
 176                        .set_uproc_interrupt_reg32 = 0x00024,
 177                        .clr_uproc_interrupt_reg = 0x00028,
 178                        .clr_uproc_interrupt_reg32 = 0x0002C,
 179                        .init_feedback_reg = 0x0005C,
 180                        .dump_addr_reg = 0x00064,
 181                        .dump_data_reg = 0x00068,
 182                        .endian_swap_reg = 0x00084
 183                }
 184        },
 185};
 186
 187static const struct ipr_chip_t ipr_chip[] = {
 188        { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
 189        { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
 190        { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
 191        { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
 192        { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, IPR_USE_MSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
 193        { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[1] },
 194        { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[1] },
 195        { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2, IPR_USE_MSI, IPR_SIS64, IPR_MMIO, &ipr_chip_cfg[2] },
 196        { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE, IPR_USE_MSI, IPR_SIS64, IPR_MMIO, &ipr_chip_cfg[2] }
 197};
 198
 199static int ipr_max_bus_speeds[] = {
 200        IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
 201};
 202
 203MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
 204MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
 205module_param_named(max_speed, ipr_max_speed, uint, 0);
 206MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
 207module_param_named(log_level, ipr_log_level, uint, 0);
 208MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
 209module_param_named(testmode, ipr_testmode, int, 0);
 210MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
 211module_param_named(fastfail, ipr_fastfail, int, S_IRUGO | S_IWUSR);
 212MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
 213module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
 214MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
 215module_param_named(debug, ipr_debug, int, S_IRUGO | S_IWUSR);
 216MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
 217module_param_named(dual_ioa_raid, ipr_dual_ioa_raid, int, 0);
 218MODULE_PARM_DESC(dual_ioa_raid, "Enable dual adapter RAID support. Set to 1 to enable. (default: 1)");
 219module_param_named(max_devs, ipr_max_devs, int, 0);
 220MODULE_PARM_DESC(max_devs, "Specify the maximum number of physical devices. "
 221                 "[Default=" __stringify(IPR_DEFAULT_SIS64_DEVS) "]");
 222module_param_named(number_of_msix, ipr_number_of_msix, int, 0);
 223MODULE_PARM_DESC(number_of_msix, "Specify the number of MSIX interrupts to use on capable adapters (1 - 5).  (default:2)");
 224MODULE_LICENSE("GPL");
 225MODULE_VERSION(IPR_DRIVER_VERSION);
 226
 227/*  A constant array of IOASCs/URCs/Error Messages */
 228static const
 229struct ipr_error_table_t ipr_error_table[] = {
 230        {0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
 231        "8155: An unknown error was received"},
 232        {0x00330000, 0, 0,
 233        "Soft underlength error"},
 234        {0x005A0000, 0, 0,
 235        "Command to be cancelled not found"},
 236        {0x00808000, 0, 0,
 237        "Qualified success"},
 238        {0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
 239        "FFFE: Soft device bus error recovered by the IOA"},
 240        {0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
 241        "4101: Soft device bus fabric error"},
 242        {0x01100100, 0, IPR_DEFAULT_LOG_LEVEL,
 243        "FFFC: Logical block guard error recovered by the device"},
 244        {0x01100300, 0, IPR_DEFAULT_LOG_LEVEL,
 245        "FFFC: Logical block reference tag error recovered by the device"},
 246        {0x01108300, 0, IPR_DEFAULT_LOG_LEVEL,
 247        "4171: Recovered scatter list tag / sequence number error"},
 248        {0x01109000, 0, IPR_DEFAULT_LOG_LEVEL,
 249        "FF3D: Recovered logical block CRC error on IOA to Host transfer"},
 250        {0x01109200, 0, IPR_DEFAULT_LOG_LEVEL,
 251        "4171: Recovered logical block sequence number error on IOA to Host transfer"},
 252        {0x0110A000, 0, IPR_DEFAULT_LOG_LEVEL,
 253        "FFFD: Recovered logical block reference tag error detected by the IOA"},
 254        {0x0110A100, 0, IPR_DEFAULT_LOG_LEVEL,
 255        "FFFD: Logical block guard error recovered by the IOA"},
 256        {0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
 257        "FFF9: Device sector reassign successful"},
 258        {0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
 259        "FFF7: Media error recovered by device rewrite procedures"},
 260        {0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
 261        "7001: IOA sector reassignment successful"},
 262        {0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
 263        "FFF9: Soft media error. Sector reassignment recommended"},
 264        {0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
 265        "FFF7: Media error recovered by IOA rewrite procedures"},
 266        {0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
 267        "FF3D: Soft PCI bus error recovered by the IOA"},
 268        {0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
 269        "FFF6: Device hardware error recovered by the IOA"},
 270        {0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
 271        "FFF6: Device hardware error recovered by the device"},
 272        {0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
 273        "FF3D: Soft IOA error recovered by the IOA"},
 274        {0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
 275        "FFFA: Undefined device response recovered by the IOA"},
 276        {0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
 277        "FFF6: Device bus error, message or command phase"},
 278        {0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
 279        "FFFE: Task Management Function failed"},
 280        {0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
 281        "FFF6: Failure prediction threshold exceeded"},
 282        {0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
 283        "8009: Impending cache battery pack failure"},
 284        {0x02040100, 0, 0,
 285        "Logical Unit in process of becoming ready"},
 286        {0x02040200, 0, 0,
 287        "Initializing command required"},
 288        {0x02040400, 0, 0,
 289        "34FF: Disk device format in progress"},
 290        {0x02040C00, 0, 0,
 291        "Logical unit not accessible, target port in unavailable state"},
 292        {0x02048000, 0, IPR_DEFAULT_LOG_LEVEL,
 293        "9070: IOA requested reset"},
 294        {0x023F0000, 0, 0,
 295        "Synchronization required"},
 296        {0x02408500, 0, 0,
 297        "IOA microcode download required"},
 298        {0x02408600, 0, 0,
 299        "Device bus connection is prohibited by host"},
 300        {0x024E0000, 0, 0,
 301        "No ready, IOA shutdown"},
 302        {0x025A0000, 0, 0,
 303        "Not ready, IOA has been shutdown"},
 304        {0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
 305        "3020: Storage subsystem configuration error"},
 306        {0x03110B00, 0, 0,
 307        "FFF5: Medium error, data unreadable, recommend reassign"},
 308        {0x03110C00, 0, 0,
 309        "7000: Medium error, data unreadable, do not reassign"},
 310        {0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
 311        "FFF3: Disk media format bad"},
 312        {0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
 313        "3002: Addressed device failed to respond to selection"},
 314        {0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
 315        "3100: Device bus error"},
 316        {0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
 317        "3109: IOA timed out a device command"},
 318        {0x04088000, 0, 0,
 319        "3120: SCSI bus is not operational"},
 320        {0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
 321        "4100: Hard device bus fabric error"},
 322        {0x04100100, 0, IPR_DEFAULT_LOG_LEVEL,
 323        "310C: Logical block guard error detected by the device"},
 324        {0x04100300, 0, IPR_DEFAULT_LOG_LEVEL,
 325        "310C: Logical block reference tag error detected by the device"},
 326        {0x04108300, 1, IPR_DEFAULT_LOG_LEVEL,
 327        "4170: Scatter list tag / sequence number error"},
 328        {0x04109000, 1, IPR_DEFAULT_LOG_LEVEL,
 329        "8150: Logical block CRC error on IOA to Host transfer"},
 330        {0x04109200, 1, IPR_DEFAULT_LOG_LEVEL,
 331        "4170: Logical block sequence number error on IOA to Host transfer"},
 332        {0x0410A000, 0, IPR_DEFAULT_LOG_LEVEL,
 333        "310D: Logical block reference tag error detected by the IOA"},
 334        {0x0410A100, 0, IPR_DEFAULT_LOG_LEVEL,
 335        "310D: Logical block guard error detected by the IOA"},
 336        {0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
 337        "9000: IOA reserved area data check"},
 338        {0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
 339        "9001: IOA reserved area invalid data pattern"},
 340        {0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
 341        "9002: IOA reserved area LRC error"},
 342        {0x04118300, 1, IPR_DEFAULT_LOG_LEVEL,
 343        "Hardware Error, IOA metadata access error"},
 344        {0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
 345        "102E: Out of alternate sectors for disk storage"},
 346        {0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
 347        "FFF4: Data transfer underlength error"},
 348        {0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
 349        "FFF4: Data transfer overlength error"},
 350        {0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
 351        "3400: Logical unit failure"},
 352        {0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
 353        "FFF4: Device microcode is corrupt"},
 354        {0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
 355        "8150: PCI bus error"},
 356        {0x04430000, 1, 0,
 357        "Unsupported device bus message received"},
 358        {0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
 359        "FFF4: Disk device problem"},
 360        {0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
 361        "8150: Permanent IOA failure"},
 362        {0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
 363        "3010: Disk device returned wrong response to IOA"},
 364        {0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
 365        "8151: IOA microcode error"},
 366        {0x04448500, 0, 0,
 367        "Device bus status error"},
 368        {0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
 369        "8157: IOA error requiring IOA reset to recover"},
 370        {0x04448700, 0, 0,
 371        "ATA device status error"},
 372        {0x04490000, 0, 0,
 373        "Message reject received from the device"},
 374        {0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
 375        "8008: A permanent cache battery pack failure occurred"},
 376        {0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
 377        "9090: Disk unit has been modified after the last known status"},
 378        {0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
 379        "9081: IOA detected device error"},
 380        {0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
 381        "9082: IOA detected device error"},
 382        {0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
 383        "3110: Device bus error, message or command phase"},
 384        {0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
 385        "3110: SAS Command / Task Management Function failed"},
 386        {0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
 387        "9091: Incorrect hardware configuration change has been detected"},
 388        {0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
 389        "9073: Invalid multi-adapter configuration"},
 390        {0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
 391        "4010: Incorrect connection between cascaded expanders"},
 392        {0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
 393        "4020: Connections exceed IOA design limits"},
 394        {0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
 395        "4030: Incorrect multipath connection"},
 396        {0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
 397        "4110: Unsupported enclosure function"},
 398        {0x04679800, 0, IPR_DEFAULT_LOG_LEVEL,
 399        "4120: SAS cable VPD cannot be read"},
 400        {0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
 401        "FFF4: Command to logical unit failed"},
 402        {0x05240000, 1, 0,
 403        "Illegal request, invalid request type or request packet"},
 404        {0x05250000, 0, 0,
 405        "Illegal request, invalid resource handle"},
 406        {0x05258000, 0, 0,
 407        "Illegal request, commands not allowed to this device"},
 408        {0x05258100, 0, 0,
 409        "Illegal request, command not allowed to a secondary adapter"},
 410        {0x05258200, 0, 0,
 411        "Illegal request, command not allowed to a non-optimized resource"},
 412        {0x05260000, 0, 0,
 413        "Illegal request, invalid field in parameter list"},
 414        {0x05260100, 0, 0,
 415        "Illegal request, parameter not supported"},
 416        {0x05260200, 0, 0,
 417        "Illegal request, parameter value invalid"},
 418        {0x052C0000, 0, 0,
 419        "Illegal request, command sequence error"},
 420        {0x052C8000, 1, 0,
 421        "Illegal request, dual adapter support not enabled"},
 422        {0x052C8100, 1, 0,
 423        "Illegal request, another cable connector was physically disabled"},
 424        {0x054E8000, 1, 0,
 425        "Illegal request, inconsistent group id/group count"},
 426        {0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
 427        "9031: Array protection temporarily suspended, protection resuming"},
 428        {0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
 429        "9040: Array protection temporarily suspended, protection resuming"},
 430        {0x060B0100, 0, IPR_DEFAULT_LOG_LEVEL,
 431        "4080: IOA exceeded maximum operating temperature"},
 432        {0x060B8000, 0, IPR_DEFAULT_LOG_LEVEL,
 433        "4085: Service required"},
 434        {0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
 435        "3140: Device bus not ready to ready transition"},
 436        {0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
 437        "FFFB: SCSI bus was reset"},
 438        {0x06290500, 0, 0,
 439        "FFFE: SCSI bus transition to single ended"},
 440        {0x06290600, 0, 0,
 441        "FFFE: SCSI bus transition to LVD"},
 442        {0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
 443        "FFFB: SCSI bus was reset by another initiator"},
 444        {0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
 445        "3029: A device replacement has occurred"},
 446        {0x063F8300, 0, IPR_DEFAULT_LOG_LEVEL,
 447        "4102: Device bus fabric performance degradation"},
 448        {0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
 449        "9051: IOA cache data exists for a missing or failed device"},
 450        {0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
 451        "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
 452        {0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
 453        "9025: Disk unit is not supported at its physical location"},
 454        {0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
 455        "3020: IOA detected a SCSI bus configuration error"},
 456        {0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
 457        "3150: SCSI bus configuration error"},
 458        {0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
 459        "9074: Asymmetric advanced function disk configuration"},
 460        {0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
 461        "4040: Incomplete multipath connection between IOA and enclosure"},
 462        {0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
 463        "4041: Incomplete multipath connection between enclosure and device"},
 464        {0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
 465        "9075: Incomplete multipath connection between IOA and remote IOA"},
 466        {0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
 467        "9076: Configuration error, missing remote IOA"},
 468        {0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
 469        "4050: Enclosure does not support a required multipath function"},
 470        {0x06679800, 0, IPR_DEFAULT_LOG_LEVEL,
 471        "4121: Configuration error, required cable is missing"},
 472        {0x06679900, 0, IPR_DEFAULT_LOG_LEVEL,
 473        "4122: Cable is not plugged into the correct location on remote IOA"},
 474        {0x06679A00, 0, IPR_DEFAULT_LOG_LEVEL,
 475        "4123: Configuration error, invalid cable vital product data"},
 476        {0x06679B00, 0, IPR_DEFAULT_LOG_LEVEL,
 477        "4124: Configuration error, both cable ends are plugged into the same IOA"},
 478        {0x06690000, 0, IPR_DEFAULT_LOG_LEVEL,
 479        "4070: Logically bad block written on device"},
 480        {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
 481        "9041: Array protection temporarily suspended"},
 482        {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
 483        "9042: Corrupt array parity detected on specified device"},
 484        {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
 485        "9030: Array no longer protected due to missing or failed disk unit"},
 486        {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
 487        "9071: Link operational transition"},
 488        {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
 489        "9072: Link not operational transition"},
 490        {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
 491        "9032: Array exposed but still protected"},
 492        {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
 493        "70DD: Device forced failed by disrupt device command"},
 494        {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
 495        "4061: Multipath redundancy level got better"},
 496        {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
 497        "4060: Multipath redundancy level got worse"},
 498        {0x07270000, 0, 0,
 499        "Failure due to other device"},
 500        {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
 501        "9008: IOA does not support functions expected by devices"},
 502        {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
 503        "9010: Cache data associated with attached devices cannot be found"},
 504        {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
 505        "9011: Cache data belongs to devices other than those attached"},
 506        {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
 507        "9020: Array missing 2 or more devices with only 1 device present"},
 508        {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
 509        "9021: Array missing 2 or more devices with 2 or more devices present"},
 510        {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
 511        "9022: Exposed array is missing a required device"},
 512        {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
 513        "9023: Array member(s) not at required physical locations"},
 514        {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
 515        "9024: Array not functional due to present hardware configuration"},
 516        {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
 517        "9026: Array not functional due to present hardware configuration"},
 518        {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
 519        "9027: Array is missing a device and parity is out of sync"},
 520        {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
 521        "9028: Maximum number of arrays already exist"},
 522        {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
 523        "9050: Required cache data cannot be located for a disk unit"},
 524        {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
 525        "9052: Cache data exists for a device that has been modified"},
 526        {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
 527        "9054: IOA resources not available due to previous problems"},
 528        {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
 529        "9092: Disk unit requires initialization before use"},
 530        {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
 531        "9029: Incorrect hardware configuration change has been detected"},
 532        {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
 533        "9060: One or more disk pairs are missing from an array"},
 534        {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
 535        "9061: One or more disks are missing from an array"},
 536        {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
 537        "9062: One or more disks are missing from an array"},
 538        {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
 539        "9063: Maximum number of functional arrays has been exceeded"},
 540        {0x07279A00, 0, 0,
 541        "Data protect, other volume set problem"},
 542        {0x0B260000, 0, 0,
 543        "Aborted command, invalid descriptor"},
 544        {0x0B3F9000, 0, 0,
 545        "Target operating conditions have changed, dual adapter takeover"},
 546        {0x0B530200, 0, 0,
 547        "Aborted command, medium removal prevented"},
 548        {0x0B5A0000, 0, 0,
 549        "Command terminated by host"},
 550        {0x0B5B8000, 0, 0,
 551        "Aborted command, command terminated by host"}
 552};
 553
 554static const struct ipr_ses_table_entry ipr_ses_table[] = {
 555        { "2104-DL1        ", "XXXXXXXXXXXXXXXX", 80 },
 556        { "2104-TL1        ", "XXXXXXXXXXXXXXXX", 80 },
 557        { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
 558        { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
 559        { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
 560        { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
 561        { "2104-DU3        ", "XXXXXXXXXXXXXXXX", 160 },
 562        { "2104-TU3        ", "XXXXXXXXXXXXXXXX", 160 },
 563        { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
 564        { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
 565        { "St  V1S2        ", "XXXXXXXXXXXXXXXX", 160 },
 566        { "HSBPD4M  PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
 567        { "VSBPD1H   U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
 568};
 569
 570/*
 571 *  Function Prototypes
 572 */
 573static int ipr_reset_alert(struct ipr_cmnd *);
 574static void ipr_process_ccn(struct ipr_cmnd *);
 575static void ipr_process_error(struct ipr_cmnd *);
 576static void ipr_reset_ioa_job(struct ipr_cmnd *);
 577static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
 578                                   enum ipr_shutdown_type);
 579
 580#ifdef CONFIG_SCSI_IPR_TRACE
 581/**
 582 * ipr_trc_hook - Add a trace entry to the driver trace
 583 * @ipr_cmd:    ipr command struct
 584 * @type:               trace type
 585 * @add_data:   additional data
 586 *
 587 * Return value:
 588 *      none
 589 **/
 590static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
 591                         u8 type, u32 add_data)
 592{
 593        struct ipr_trace_entry *trace_entry;
 594        struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
 595
 596        trace_entry = &ioa_cfg->trace[atomic_add_return
 597                        (1, &ioa_cfg->trace_index)%IPR_NUM_TRACE_ENTRIES];
 598        trace_entry->time = jiffies;
 599        trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
 600        trace_entry->type = type;
 601        if (ipr_cmd->ioa_cfg->sis64)
 602                trace_entry->ata_op_code = ipr_cmd->i.ata_ioadl.regs.command;
 603        else
 604                trace_entry->ata_op_code = ipr_cmd->ioarcb.u.add_data.u.regs.command;
 605        trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
 606        trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
 607        trace_entry->u.add_data = add_data;
 608        wmb();
 609}
 610#else
 611#define ipr_trc_hook(ipr_cmd, type, add_data) do { } while (0)
 612#endif
 613
 614/**
 615 * ipr_lock_and_done - Acquire lock and complete command
 616 * @ipr_cmd:    ipr command struct
 617 *
 618 * Return value:
 619 *      none
 620 **/
 621static void ipr_lock_and_done(struct ipr_cmnd *ipr_cmd)
 622{
 623        unsigned long lock_flags;
 624        struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
 625
 626        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
 627        ipr_cmd->done(ipr_cmd);
 628        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
 629}
 630
 631/**
 632 * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
 633 * @ipr_cmd:    ipr command struct
 634 *
 635 * Return value:
 636 *      none
 637 **/
 638static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
 639{
 640        struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
 641        struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
 642        struct ipr_ioasa64 *ioasa64 = &ipr_cmd->s.ioasa64;
 643        dma_addr_t dma_addr = ipr_cmd->dma_addr;
 644        int hrrq_id;
 645
 646        hrrq_id = ioarcb->cmd_pkt.hrrq_id;
 647        memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
 648        ioarcb->cmd_pkt.hrrq_id = hrrq_id;
 649        ioarcb->data_transfer_length = 0;
 650        ioarcb->read_data_transfer_length = 0;
 651        ioarcb->ioadl_len = 0;
 652        ioarcb->read_ioadl_len = 0;
 653
 654        if (ipr_cmd->ioa_cfg->sis64) {
 655                ioarcb->u.sis64_addr_data.data_ioadl_addr =
 656                        cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
 657                ioasa64->u.gata.status = 0;
 658        } else {
 659                ioarcb->write_ioadl_addr =
 660                        cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
 661                ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
 662                ioasa->u.gata.status = 0;
 663        }
 664
 665        ioasa->hdr.ioasc = 0;
 666        ioasa->hdr.residual_data_len = 0;
 667        ipr_cmd->scsi_cmd = NULL;
 668        ipr_cmd->qc = NULL;
 669        ipr_cmd->sense_buffer[0] = 0;
 670        ipr_cmd->dma_use_sg = 0;
 671}
 672
 673/**
 674 * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
 675 * @ipr_cmd:    ipr command struct
 676 *
 677 * Return value:
 678 *      none
 679 **/
 680static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd,
 681                              void (*fast_done) (struct ipr_cmnd *))
 682{
 683        ipr_reinit_ipr_cmnd(ipr_cmd);
 684        ipr_cmd->u.scratch = 0;
 685        ipr_cmd->sibling = NULL;
 686        ipr_cmd->fast_done = fast_done;
 687        init_timer(&ipr_cmd->timer);
 688}
 689
 690/**
 691 * __ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
 692 * @ioa_cfg:    ioa config struct
 693 *
 694 * Return value:
 695 *      pointer to ipr command struct
 696 **/
 697static
 698struct ipr_cmnd *__ipr_get_free_ipr_cmnd(struct ipr_hrr_queue *hrrq)
 699{
 700        struct ipr_cmnd *ipr_cmd = NULL;
 701
 702        if (likely(!list_empty(&hrrq->hrrq_free_q))) {
 703                ipr_cmd = list_entry(hrrq->hrrq_free_q.next,
 704                        struct ipr_cmnd, queue);
 705                list_del(&ipr_cmd->queue);
 706        }
 707
 708
 709        return ipr_cmd;
 710}
 711
 712/**
 713 * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block and initialize it
 714 * @ioa_cfg:    ioa config struct
 715 *
 716 * Return value:
 717 *      pointer to ipr command struct
 718 **/
 719static
 720struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
 721{
 722        struct ipr_cmnd *ipr_cmd =
 723                __ipr_get_free_ipr_cmnd(&ioa_cfg->hrrq[IPR_INIT_HRRQ]);
 724        ipr_init_ipr_cmnd(ipr_cmd, ipr_lock_and_done);
 725        return ipr_cmd;
 726}
 727
 728/**
 729 * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
 730 * @ioa_cfg:    ioa config struct
 731 * @clr_ints:     interrupts to clear
 732 *
 733 * This function masks all interrupts on the adapter, then clears the
 734 * interrupts specified in the mask
 735 *
 736 * Return value:
 737 *      none
 738 **/
 739static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
 740                                          u32 clr_ints)
 741{
 742        volatile u32 int_reg;
 743        int i;
 744
 745        /* Stop new interrupts */
 746        for (i = 0; i < ioa_cfg->hrrq_num; i++) {
 747                spin_lock(&ioa_cfg->hrrq[i]._lock);
 748                ioa_cfg->hrrq[i].allow_interrupts = 0;
 749                spin_unlock(&ioa_cfg->hrrq[i]._lock);
 750        }
 751        wmb();
 752
 753        /* Set interrupt mask to stop all new interrupts */
 754        if (ioa_cfg->sis64)
 755                writeq(~0, ioa_cfg->regs.set_interrupt_mask_reg);
 756        else
 757                writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
 758
 759        /* Clear any pending interrupts */
 760        if (ioa_cfg->sis64)
 761                writel(~0, ioa_cfg->regs.clr_interrupt_reg);
 762        writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg32);
 763        int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
 764}
 765
 766/**
 767 * ipr_save_pcix_cmd_reg - Save PCI-X command register
 768 * @ioa_cfg:    ioa config struct
 769 *
 770 * Return value:
 771 *      0 on success / -EIO on failure
 772 **/
 773static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
 774{
 775        int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
 776
 777        if (pcix_cmd_reg == 0)
 778                return 0;
 779
 780        if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
 781                                 &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
 782                dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
 783                return -EIO;
 784        }
 785
 786        ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
 787        return 0;
 788}
 789
 790/**
 791 * ipr_set_pcix_cmd_reg - Setup PCI-X command register
 792 * @ioa_cfg:    ioa config struct
 793 *
 794 * Return value:
 795 *      0 on success / -EIO on failure
 796 **/
 797static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
 798{
 799        int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
 800
 801        if (pcix_cmd_reg) {
 802                if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
 803                                          ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
 804                        dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
 805                        return -EIO;
 806                }
 807        }
 808
 809        return 0;
 810}
 811
 812/**
 813 * ipr_sata_eh_done - done function for aborted SATA commands
 814 * @ipr_cmd:    ipr command struct
 815 *
 816 * This function is invoked for ops generated to SATA
 817 * devices which are being aborted.
 818 *
 819 * Return value:
 820 *      none
 821 **/
 822static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
 823{
 824        struct ata_queued_cmd *qc = ipr_cmd->qc;
 825        struct ipr_sata_port *sata_port = qc->ap->private_data;
 826
 827        qc->err_mask |= AC_ERR_OTHER;
 828        sata_port->ioasa.status |= ATA_BUSY;
 829        list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
 830        ata_qc_complete(qc);
 831}
 832
 833/**
 834 * ipr_scsi_eh_done - mid-layer done function for aborted ops
 835 * @ipr_cmd:    ipr command struct
 836 *
 837 * This function is invoked by the interrupt handler for
 838 * ops generated by the SCSI mid-layer which are being aborted.
 839 *
 840 * Return value:
 841 *      none
 842 **/
 843static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
 844{
 845        struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
 846
 847        scsi_cmd->result |= (DID_ERROR << 16);
 848
 849        scsi_dma_unmap(ipr_cmd->scsi_cmd);
 850        scsi_cmd->scsi_done(scsi_cmd);
 851        list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
 852}
 853
 854/**
 855 * ipr_fail_all_ops - Fails all outstanding ops.
 856 * @ioa_cfg:    ioa config struct
 857 *
 858 * This function fails all outstanding ops.
 859 *
 860 * Return value:
 861 *      none
 862 **/
 863static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
 864{
 865        struct ipr_cmnd *ipr_cmd, *temp;
 866        struct ipr_hrr_queue *hrrq;
 867
 868        ENTER;
 869        for_each_hrrq(hrrq, ioa_cfg) {
 870                spin_lock(&hrrq->_lock);
 871                list_for_each_entry_safe(ipr_cmd,
 872                                        temp, &hrrq->hrrq_pending_q, queue) {
 873                        list_del(&ipr_cmd->queue);
 874
 875                        ipr_cmd->s.ioasa.hdr.ioasc =
 876                                cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
 877                        ipr_cmd->s.ioasa.hdr.ilid =
 878                                cpu_to_be32(IPR_DRIVER_ILID);
 879
 880                        if (ipr_cmd->scsi_cmd)
 881                                ipr_cmd->done = ipr_scsi_eh_done;
 882                        else if (ipr_cmd->qc)
 883                                ipr_cmd->done = ipr_sata_eh_done;
 884
 885                        ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH,
 886                                     IPR_IOASC_IOA_WAS_RESET);
 887                        del_timer(&ipr_cmd->timer);
 888                        ipr_cmd->done(ipr_cmd);
 889                }
 890                spin_unlock(&hrrq->_lock);
 891        }
 892        LEAVE;
 893}
 894
 895/**
 896 * ipr_send_command -  Send driver initiated requests.
 897 * @ipr_cmd:            ipr command struct
 898 *
 899 * This function sends a command to the adapter using the correct write call.
 900 * In the case of sis64, calculate the ioarcb size required. Then or in the
 901 * appropriate bits.
 902 *
 903 * Return value:
 904 *      none
 905 **/
 906static void ipr_send_command(struct ipr_cmnd *ipr_cmd)
 907{
 908        struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
 909        dma_addr_t send_dma_addr = ipr_cmd->dma_addr;
 910
 911        if (ioa_cfg->sis64) {
 912                /* The default size is 256 bytes */
 913                send_dma_addr |= 0x1;
 914
 915                /* If the number of ioadls * size of ioadl > 128 bytes,
 916                   then use a 512 byte ioarcb */
 917                if (ipr_cmd->dma_use_sg * sizeof(struct ipr_ioadl64_desc) > 128 )
 918                        send_dma_addr |= 0x4;
 919                writeq(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
 920        } else
 921                writel(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
 922}
 923
 924/**
 925 * ipr_do_req -  Send driver initiated requests.
 926 * @ipr_cmd:            ipr command struct
 927 * @done:                       done function
 928 * @timeout_func:       timeout function
 929 * @timeout:            timeout value
 930 *
 931 * This function sends the specified command to the adapter with the
 932 * timeout given. The done function is invoked on command completion.
 933 *
 934 * Return value:
 935 *      none
 936 **/
 937static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
 938                       void (*done) (struct ipr_cmnd *),
 939                       void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
 940{
 941        list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
 942
 943        ipr_cmd->done = done;
 944
 945        ipr_cmd->timer.data = (unsigned long) ipr_cmd;
 946        ipr_cmd->timer.expires = jiffies + timeout;
 947        ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
 948
 949        add_timer(&ipr_cmd->timer);
 950
 951        ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
 952
 953        ipr_send_command(ipr_cmd);
 954}
 955
 956/**
 957 * ipr_internal_cmd_done - Op done function for an internally generated op.
 958 * @ipr_cmd:    ipr command struct
 959 *
 960 * This function is the op done function for an internally generated,
 961 * blocking op. It simply wakes the sleeping thread.
 962 *
 963 * Return value:
 964 *      none
 965 **/
 966static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
 967{
 968        if (ipr_cmd->sibling)
 969                ipr_cmd->sibling = NULL;
 970        else
 971                complete(&ipr_cmd->completion);
 972}
 973
 974/**
 975 * ipr_init_ioadl - initialize the ioadl for the correct SIS type
 976 * @ipr_cmd:    ipr command struct
 977 * @dma_addr:   dma address
 978 * @len:        transfer length
 979 * @flags:      ioadl flag value
 980 *
 981 * This function initializes an ioadl in the case where there is only a single
 982 * descriptor.
 983 *
 984 * Return value:
 985 *      nothing
 986 **/
 987static void ipr_init_ioadl(struct ipr_cmnd *ipr_cmd, dma_addr_t dma_addr,
 988                           u32 len, int flags)
 989{
 990        struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
 991        struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
 992
 993        ipr_cmd->dma_use_sg = 1;
 994
 995        if (ipr_cmd->ioa_cfg->sis64) {
 996                ioadl64->flags = cpu_to_be32(flags);
 997                ioadl64->data_len = cpu_to_be32(len);
 998                ioadl64->address = cpu_to_be64(dma_addr);
 999
1000                ipr_cmd->ioarcb.ioadl_len =
1001                        cpu_to_be32(sizeof(struct ipr_ioadl64_desc));
1002                ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
1003        } else {
1004                ioadl->flags_and_data_len = cpu_to_be32(flags | len);
1005                ioadl->address = cpu_to_be32(dma_addr);
1006
1007                if (flags == IPR_IOADL_FLAGS_READ_LAST) {
1008                        ipr_cmd->ioarcb.read_ioadl_len =
1009                                cpu_to_be32(sizeof(struct ipr_ioadl_desc));
1010                        ipr_cmd->ioarcb.read_data_transfer_length = cpu_to_be32(len);
1011                } else {
1012                        ipr_cmd->ioarcb.ioadl_len =
1013                                cpu_to_be32(sizeof(struct ipr_ioadl_desc));
1014                        ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
1015                }
1016        }
1017}
1018
1019/**
1020 * ipr_send_blocking_cmd - Send command and sleep on its completion.
1021 * @ipr_cmd:    ipr command struct
1022 * @timeout_func:       function to invoke if command times out
1023 * @timeout:    timeout
1024 *
1025 * Return value:
1026 *      none
1027 **/
1028static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
1029                                  void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
1030                                  u32 timeout)
1031{
1032        struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1033
1034        init_completion(&ipr_cmd->completion);
1035        ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
1036
1037        spin_unlock_irq(ioa_cfg->host->host_lock);
1038        wait_for_completion(&ipr_cmd->completion);
1039        spin_lock_irq(ioa_cfg->host->host_lock);
1040}
1041
1042static int ipr_get_hrrq_index(struct ipr_ioa_cfg *ioa_cfg)
1043{
1044        if (ioa_cfg->hrrq_num == 1)
1045                return 0;
1046        else
1047                return (atomic_add_return(1, &ioa_cfg->hrrq_index) % (ioa_cfg->hrrq_num - 1)) + 1;
1048}
1049
1050/**
1051 * ipr_send_hcam - Send an HCAM to the adapter.
1052 * @ioa_cfg:    ioa config struct
1053 * @type:               HCAM type
1054 * @hostrcb:    hostrcb struct
1055 *
1056 * This function will send a Host Controlled Async command to the adapter.
1057 * If HCAMs are currently not allowed to be issued to the adapter, it will
1058 * place the hostrcb on the free queue.
1059 *
1060 * Return value:
1061 *      none
1062 **/
1063static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
1064                          struct ipr_hostrcb *hostrcb)
1065{
1066        struct ipr_cmnd *ipr_cmd;
1067        struct ipr_ioarcb *ioarcb;
1068
1069        if (ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds) {
1070                ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
1071                list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
1072                list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
1073
1074                ipr_cmd->u.hostrcb = hostrcb;
1075                ioarcb = &ipr_cmd->ioarcb;
1076
1077                ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
1078                ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
1079                ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
1080                ioarcb->cmd_pkt.cdb[1] = type;
1081                ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
1082                ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
1083
1084                ipr_init_ioadl(ipr_cmd, hostrcb->hostrcb_dma,
1085                               sizeof(hostrcb->hcam), IPR_IOADL_FLAGS_READ_LAST);
1086
1087                if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
1088                        ipr_cmd->done = ipr_process_ccn;
1089                else
1090                        ipr_cmd->done = ipr_process_error;
1091
1092                ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
1093
1094                ipr_send_command(ipr_cmd);
1095        } else {
1096                list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
1097        }
1098}
1099
1100/**
1101 * ipr_update_ata_class - Update the ata class in the resource entry
1102 * @res:        resource entry struct
1103 * @proto:      cfgte device bus protocol value
1104 *
1105 * Return value:
1106 *      none
1107 **/
1108static void ipr_update_ata_class(struct ipr_resource_entry *res, unsigned int proto)
1109{
1110        switch (proto) {
1111        case IPR_PROTO_SATA:
1112        case IPR_PROTO_SAS_STP:
1113                res->ata_class = ATA_DEV_ATA;
1114                break;
1115        case IPR_PROTO_SATA_ATAPI:
1116        case IPR_PROTO_SAS_STP_ATAPI:
1117                res->ata_class = ATA_DEV_ATAPI;
1118                break;
1119        default:
1120                res->ata_class = ATA_DEV_UNKNOWN;
1121                break;
1122        };
1123}
1124
1125/**
1126 * ipr_init_res_entry - Initialize a resource entry struct.
1127 * @res:        resource entry struct
1128 * @cfgtew:     config table entry wrapper struct
1129 *
1130 * Return value:
1131 *      none
1132 **/
1133static void ipr_init_res_entry(struct ipr_resource_entry *res,
1134                               struct ipr_config_table_entry_wrapper *cfgtew)
1135{
1136        int found = 0;
1137        unsigned int proto;
1138        struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
1139        struct ipr_resource_entry *gscsi_res = NULL;
1140
1141        res->needs_sync_complete = 0;
1142        res->in_erp = 0;
1143        res->add_to_ml = 0;
1144        res->del_from_ml = 0;
1145        res->resetting_device = 0;
1146        res->sdev = NULL;
1147        res->sata_port = NULL;
1148
1149        if (ioa_cfg->sis64) {
1150                proto = cfgtew->u.cfgte64->proto;
1151                res->res_flags = cfgtew->u.cfgte64->res_flags;
1152                res->qmodel = IPR_QUEUEING_MODEL64(res);
1153                res->type = cfgtew->u.cfgte64->res_type;
1154
1155                memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
1156                        sizeof(res->res_path));
1157
1158                res->bus = 0;
1159                memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1160                        sizeof(res->dev_lun.scsi_lun));
1161                res->lun = scsilun_to_int(&res->dev_lun);
1162
1163                if (res->type == IPR_RES_TYPE_GENERIC_SCSI) {
1164                        list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue) {
1165                                if (gscsi_res->dev_id == cfgtew->u.cfgte64->dev_id) {
1166                                        found = 1;
1167                                        res->target = gscsi_res->target;
1168                                        break;
1169                                }
1170                        }
1171                        if (!found) {
1172                                res->target = find_first_zero_bit(ioa_cfg->target_ids,
1173                                                                  ioa_cfg->max_devs_supported);
1174                                set_bit(res->target, ioa_cfg->target_ids);
1175                        }
1176                } else if (res->type == IPR_RES_TYPE_IOAFP) {
1177                        res->bus = IPR_IOAFP_VIRTUAL_BUS;
1178                        res->target = 0;
1179                } else if (res->type == IPR_RES_TYPE_ARRAY) {
1180                        res->bus = IPR_ARRAY_VIRTUAL_BUS;
1181                        res->target = find_first_zero_bit(ioa_cfg->array_ids,
1182                                                          ioa_cfg->max_devs_supported);
1183                        set_bit(res->target, ioa_cfg->array_ids);
1184                } else if (res->type == IPR_RES_TYPE_VOLUME_SET) {
1185                        res->bus = IPR_VSET_VIRTUAL_BUS;
1186                        res->target = find_first_zero_bit(ioa_cfg->vset_ids,
1187                                                          ioa_cfg->max_devs_supported);
1188                        set_bit(res->target, ioa_cfg->vset_ids);
1189                } else {
1190                        res->target = find_first_zero_bit(ioa_cfg->target_ids,
1191                                                          ioa_cfg->max_devs_supported);
1192                        set_bit(res->target, ioa_cfg->target_ids);
1193                }
1194        } else {
1195                proto = cfgtew->u.cfgte->proto;
1196                res->qmodel = IPR_QUEUEING_MODEL(res);
1197                res->flags = cfgtew->u.cfgte->flags;
1198                if (res->flags & IPR_IS_IOA_RESOURCE)
1199                        res->type = IPR_RES_TYPE_IOAFP;
1200                else
1201                        res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
1202
1203                res->bus = cfgtew->u.cfgte->res_addr.bus;
1204                res->target = cfgtew->u.cfgte->res_addr.target;
1205                res->lun = cfgtew->u.cfgte->res_addr.lun;
1206                res->lun_wwn = get_unaligned_be64(cfgtew->u.cfgte->lun_wwn);
1207        }
1208
1209        ipr_update_ata_class(res, proto);
1210}
1211
1212/**
1213 * ipr_is_same_device - Determine if two devices are the same.
1214 * @res:        resource entry struct
1215 * @cfgtew:     config table entry wrapper struct
1216 *
1217 * Return value:
1218 *      1 if the devices are the same / 0 otherwise
1219 **/
1220static int ipr_is_same_device(struct ipr_resource_entry *res,
1221                              struct ipr_config_table_entry_wrapper *cfgtew)
1222{
1223        if (res->ioa_cfg->sis64) {
1224                if (!memcmp(&res->dev_id, &cfgtew->u.cfgte64->dev_id,
1225                                        sizeof(cfgtew->u.cfgte64->dev_id)) &&
1226                        !memcmp(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1227                                        sizeof(cfgtew->u.cfgte64->lun))) {
1228                        return 1;
1229                }
1230        } else {
1231                if (res->bus == cfgtew->u.cfgte->res_addr.bus &&
1232                    res->target == cfgtew->u.cfgte->res_addr.target &&
1233                    res->lun == cfgtew->u.cfgte->res_addr.lun)
1234                        return 1;
1235        }
1236
1237        return 0;
1238}
1239
1240/**
1241 * __ipr_format_res_path - Format the resource path for printing.
1242 * @res_path:   resource path
1243 * @buf:        buffer
1244 * @len:        length of buffer provided
1245 *
1246 * Return value:
1247 *      pointer to buffer
1248 **/
1249static char *__ipr_format_res_path(u8 *res_path, char *buffer, int len)
1250{
1251        int i;
1252        char *p = buffer;
1253
1254        *p = '\0';
1255        p += snprintf(p, buffer + len - p, "%02X", res_path[0]);
1256        for (i = 1; res_path[i] != 0xff && ((i * 3) < len); i++)
1257                p += snprintf(p, buffer + len - p, "-%02X", res_path[i]);
1258
1259        return buffer;
1260}
1261
1262/**
1263 * ipr_format_res_path - Format the resource path for printing.
1264 * @ioa_cfg:    ioa config struct
1265 * @res_path:   resource path
1266 * @buf:        buffer
1267 * @len:        length of buffer provided
1268 *
1269 * Return value:
1270 *      pointer to buffer
1271 **/
1272static char *ipr_format_res_path(struct ipr_ioa_cfg *ioa_cfg,
1273                                 u8 *res_path, char *buffer, int len)
1274{
1275        char *p = buffer;
1276
1277        *p = '\0';
1278        p += snprintf(p, buffer + len - p, "%d/", ioa_cfg->host->host_no);
1279        __ipr_format_res_path(res_path, p, len - (buffer - p));
1280        return buffer;
1281}
1282
1283/**
1284 * ipr_update_res_entry - Update the resource entry.
1285 * @res:        resource entry struct
1286 * @cfgtew:     config table entry wrapper struct
1287 *
1288 * Return value:
1289 *      none
1290 **/
1291static void ipr_update_res_entry(struct ipr_resource_entry *res,
1292                                 struct ipr_config_table_entry_wrapper *cfgtew)
1293{
1294        char buffer[IPR_MAX_RES_PATH_LENGTH];
1295        unsigned int proto;
1296        int new_path = 0;
1297
1298        if (res->ioa_cfg->sis64) {
1299                res->flags = cfgtew->u.cfgte64->flags;
1300                res->res_flags = cfgtew->u.cfgte64->res_flags;
1301                res->type = cfgtew->u.cfgte64->res_type;
1302
1303                memcpy(&res->std_inq_data, &cfgtew->u.cfgte64->std_inq_data,
1304                        sizeof(struct ipr_std_inq_data));
1305
1306                res->qmodel = IPR_QUEUEING_MODEL64(res);
1307                proto = cfgtew->u.cfgte64->proto;
1308                res->res_handle = cfgtew->u.cfgte64->res_handle;
1309                res->dev_id = cfgtew->u.cfgte64->dev_id;
1310
1311                memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1312                        sizeof(res->dev_lun.scsi_lun));
1313
1314                if (memcmp(res->res_path, &cfgtew->u.cfgte64->res_path,
1315                                        sizeof(res->res_path))) {
1316                        memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
1317                                sizeof(res->res_path));
1318                        new_path = 1;
1319                }
1320
1321                if (res->sdev && new_path)
1322                        sdev_printk(KERN_INFO, res->sdev, "Resource path: %s\n",
1323                                    ipr_format_res_path(res->ioa_cfg,
1324                                        res->res_path, buffer, sizeof(buffer)));
1325        } else {
1326                res->flags = cfgtew->u.cfgte->flags;
1327                if (res->flags & IPR_IS_IOA_RESOURCE)
1328                        res->type = IPR_RES_TYPE_IOAFP;
1329                else
1330                        res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
1331
1332                memcpy(&res->std_inq_data, &cfgtew->u.cfgte->std_inq_data,
1333                        sizeof(struct ipr_std_inq_data));
1334
1335                res->qmodel = IPR_QUEUEING_MODEL(res);
1336                proto = cfgtew->u.cfgte->proto;
1337                res->res_handle = cfgtew->u.cfgte->res_handle;
1338        }
1339
1340        ipr_update_ata_class(res, proto);
1341}
1342
1343/**
1344 * ipr_clear_res_target - Clear the bit in the bit map representing the target
1345 *                        for the resource.
1346 * @res:        resource entry struct
1347 * @cfgtew:     config table entry wrapper struct
1348 *
1349 * Return value:
1350 *      none
1351 **/
1352static void ipr_clear_res_target(struct ipr_resource_entry *res)
1353{
1354        struct ipr_resource_entry *gscsi_res = NULL;
1355        struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
1356
1357        if (!ioa_cfg->sis64)
1358                return;
1359
1360        if (res->bus == IPR_ARRAY_VIRTUAL_BUS)
1361                clear_bit(res->target, ioa_cfg->array_ids);
1362        else if (res->bus == IPR_VSET_VIRTUAL_BUS)
1363                clear_bit(res->target, ioa_cfg->vset_ids);
1364        else if (res->bus == 0 && res->type == IPR_RES_TYPE_GENERIC_SCSI) {
1365                list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue)
1366                        if (gscsi_res->dev_id == res->dev_id && gscsi_res != res)
1367                                return;
1368                clear_bit(res->target, ioa_cfg->target_ids);
1369
1370        } else if (res->bus == 0)
1371                clear_bit(res->target, ioa_cfg->target_ids);
1372}
1373
1374/**
1375 * ipr_handle_config_change - Handle a config change from the adapter
1376 * @ioa_cfg:    ioa config struct
1377 * @hostrcb:    hostrcb
1378 *
1379 * Return value:
1380 *      none
1381 **/
1382static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
1383                                     struct ipr_hostrcb *hostrcb)
1384{
1385        struct ipr_resource_entry *res = NULL;
1386        struct ipr_config_table_entry_wrapper cfgtew;
1387        __be32 cc_res_handle;
1388
1389        u32 is_ndn = 1;
1390
1391        if (ioa_cfg->sis64) {
1392                cfgtew.u.cfgte64 = &hostrcb->hcam.u.ccn.u.cfgte64;
1393                cc_res_handle = cfgtew.u.cfgte64->res_handle;
1394        } else {
1395                cfgtew.u.cfgte = &hostrcb->hcam.u.ccn.u.cfgte;
1396                cc_res_handle = cfgtew.u.cfgte->res_handle;
1397        }
1398
1399        list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1400                if (res->res_handle == cc_res_handle) {
1401                        is_ndn = 0;
1402                        break;
1403                }
1404        }
1405
1406        if (is_ndn) {
1407                if (list_empty(&ioa_cfg->free_res_q)) {
1408                        ipr_send_hcam(ioa_cfg,
1409                                      IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
1410                                      hostrcb);
1411                        return;
1412                }
1413
1414                res = list_entry(ioa_cfg->free_res_q.next,
1415                                 struct ipr_resource_entry, queue);
1416
1417                list_del(&res->queue);
1418                ipr_init_res_entry(res, &cfgtew);
1419                list_add_tail(&res->queue, &ioa_cfg->used_res_q);
1420        }
1421
1422        ipr_update_res_entry(res, &cfgtew);
1423
1424        if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
1425                if (res->sdev) {
1426                        res->del_from_ml = 1;
1427                        res->res_handle = IPR_INVALID_RES_HANDLE;
1428                        if (ioa_cfg->allow_ml_add_del)
1429                                schedule_work(&ioa_cfg->work_q);
1430                } else {
1431                        ipr_clear_res_target(res);
1432                        list_move_tail(&res->queue, &ioa_cfg->free_res_q);
1433                }
1434        } else if (!res->sdev || res->del_from_ml) {
1435                res->add_to_ml = 1;
1436                if (ioa_cfg->allow_ml_add_del)
1437                        schedule_work(&ioa_cfg->work_q);
1438        }
1439
1440        ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
1441}
1442
1443/**
1444 * ipr_process_ccn - Op done function for a CCN.
1445 * @ipr_cmd:    ipr command struct
1446 *
1447 * This function is the op done function for a configuration
1448 * change notification host controlled async from the adapter.
1449 *
1450 * Return value:
1451 *      none
1452 **/
1453static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
1454{
1455        struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1456        struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1457        u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1458
1459        list_del(&hostrcb->queue);
1460        list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
1461
1462        if (ioasc) {
1463                if (ioasc != IPR_IOASC_IOA_WAS_RESET)
1464                        dev_err(&ioa_cfg->pdev->dev,
1465                                "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1466
1467                ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
1468        } else {
1469                ipr_handle_config_change(ioa_cfg, hostrcb);
1470        }
1471}
1472
1473/**
1474 * strip_and_pad_whitespace - Strip and pad trailing whitespace.
1475 * @i:          index into buffer
1476 * @buf:                string to modify
1477 *
1478 * This function will strip all trailing whitespace, pad the end
1479 * of the string with a single space, and NULL terminate the string.
1480 *
1481 * Return value:
1482 *      new length of string
1483 **/
1484static int strip_and_pad_whitespace(int i, char *buf)
1485{
1486        while (i && buf[i] == ' ')
1487                i--;
1488        buf[i+1] = ' ';
1489        buf[i+2] = '\0';
1490        return i + 2;
1491}
1492
1493/**
1494 * ipr_log_vpd_compact - Log the passed extended VPD compactly.
1495 * @prefix:             string to print at start of printk
1496 * @hostrcb:    hostrcb pointer
1497 * @vpd:                vendor/product id/sn struct
1498 *
1499 * Return value:
1500 *      none
1501 **/
1502static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1503                                struct ipr_vpd *vpd)
1504{
1505        char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
1506        int i = 0;
1507
1508        memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
1509        i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
1510
1511        memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
1512        i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
1513
1514        memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
1515        buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
1516
1517        ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
1518}
1519
1520/**
1521 * ipr_log_vpd - Log the passed VPD to the error log.
1522 * @vpd:                vendor/product id/sn struct
1523 *
1524 * Return value:
1525 *      none
1526 **/
1527static void ipr_log_vpd(struct ipr_vpd *vpd)
1528{
1529        char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
1530                    + IPR_SERIAL_NUM_LEN];
1531
1532        memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
1533        memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
1534               IPR_PROD_ID_LEN);
1535        buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
1536        ipr_err("Vendor/Product ID: %s\n", buffer);
1537
1538        memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
1539        buffer[IPR_SERIAL_NUM_LEN] = '\0';
1540        ipr_err("    Serial Number: %s\n", buffer);
1541}
1542
1543/**
1544 * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
1545 * @prefix:             string to print at start of printk
1546 * @hostrcb:    hostrcb pointer
1547 * @vpd:                vendor/product id/sn/wwn struct
1548 *
1549 * Return value:
1550 *      none
1551 **/
1552static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1553                                    struct ipr_ext_vpd *vpd)
1554{
1555        ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
1556        ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
1557                     be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
1558}
1559
1560/**
1561 * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
1562 * @vpd:                vendor/product id/sn/wwn struct
1563 *
1564 * Return value:
1565 *      none
1566 **/
1567static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
1568{
1569        ipr_log_vpd(&vpd->vpd);
1570        ipr_err("    WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
1571                be32_to_cpu(vpd->wwid[1]));
1572}
1573
1574/**
1575 * ipr_log_enhanced_cache_error - Log a cache error.
1576 * @ioa_cfg:    ioa config struct
1577 * @hostrcb:    hostrcb struct
1578 *
1579 * Return value:
1580 *      none
1581 **/
1582static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1583                                         struct ipr_hostrcb *hostrcb)
1584{
1585        struct ipr_hostrcb_type_12_error *error;
1586
1587        if (ioa_cfg->sis64)
1588                error = &hostrcb->hcam.u.error64.u.type_12_error;
1589        else
1590                error = &hostrcb->hcam.u.error.u.type_12_error;
1591
1592        ipr_err("-----Current Configuration-----\n");
1593        ipr_err("Cache Directory Card Information:\n");
1594        ipr_log_ext_vpd(&error->ioa_vpd);
1595        ipr_err("Adapter Card Information:\n");
1596        ipr_log_ext_vpd(&error->cfc_vpd);
1597
1598        ipr_err("-----Expected Configuration-----\n");
1599        ipr_err("Cache Directory Card Information:\n");
1600        ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
1601        ipr_err("Adapter Card Information:\n");
1602        ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
1603
1604        ipr_err("Additional IOA Data: %08X %08X %08X\n",
1605                     be32_to_cpu(error->ioa_data[0]),
1606                     be32_to_cpu(error->ioa_data[1]),
1607                     be32_to_cpu(error->ioa_data[2]));
1608}
1609
1610/**
1611 * ipr_log_cache_error - Log a cache error.
1612 * @ioa_cfg:    ioa config struct
1613 * @hostrcb:    hostrcb struct
1614 *
1615 * Return value:
1616 *      none
1617 **/
1618static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1619                                struct ipr_hostrcb *hostrcb)
1620{
1621        struct ipr_hostrcb_type_02_error *error =
1622                &hostrcb->hcam.u.error.u.type_02_error;
1623
1624        ipr_err("-----Current Configuration-----\n");
1625        ipr_err("Cache Directory Card Information:\n");
1626        ipr_log_vpd(&error->ioa_vpd);
1627        ipr_err("Adapter Card Information:\n");
1628        ipr_log_vpd(&error->cfc_vpd);
1629
1630        ipr_err("-----Expected Configuration-----\n");
1631        ipr_err("Cache Directory Card Information:\n");
1632        ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
1633        ipr_err("Adapter Card Information:\n");
1634        ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
1635
1636        ipr_err("Additional IOA Data: %08X %08X %08X\n",
1637                     be32_to_cpu(error->ioa_data[0]),
1638                     be32_to_cpu(error->ioa_data[1]),
1639                     be32_to_cpu(error->ioa_data[2]));
1640}
1641
1642/**
1643 * ipr_log_enhanced_config_error - Log a configuration error.
1644 * @ioa_cfg:    ioa config struct
1645 * @hostrcb:    hostrcb struct
1646 *
1647 * Return value:
1648 *      none
1649 **/
1650static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
1651                                          struct ipr_hostrcb *hostrcb)
1652{
1653        int errors_logged, i;
1654        struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
1655        struct ipr_hostrcb_type_13_error *error;
1656
1657        error = &hostrcb->hcam.u.error.u.type_13_error;
1658        errors_logged = be32_to_cpu(error->errors_logged);
1659
1660        ipr_err("Device Errors Detected/Logged: %d/%d\n",
1661                be32_to_cpu(error->errors_detected), errors_logged);
1662
1663        dev_entry = error->dev;
1664
1665        for (i = 0; i < errors_logged; i++, dev_entry++) {
1666                ipr_err_separator;
1667
1668                ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1669                ipr_log_ext_vpd(&dev_entry->vpd);
1670
1671                ipr_err("-----New Device Information-----\n");
1672                ipr_log_ext_vpd(&dev_entry->new_vpd);
1673
1674                ipr_err("Cache Directory Card Information:\n");
1675                ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1676
1677                ipr_err("Adapter Card Information:\n");
1678                ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1679        }
1680}
1681
1682/**
1683 * ipr_log_sis64_config_error - Log a device error.
1684 * @ioa_cfg:    ioa config struct
1685 * @hostrcb:    hostrcb struct
1686 *
1687 * Return value:
1688 *      none
1689 **/
1690static void ipr_log_sis64_config_error(struct ipr_ioa_cfg *ioa_cfg,
1691                                       struct ipr_hostrcb *hostrcb)
1692{
1693        int errors_logged, i;
1694        struct ipr_hostrcb64_device_data_entry_enhanced *dev_entry;
1695        struct ipr_hostrcb_type_23_error *error;
1696        char buffer[IPR_MAX_RES_PATH_LENGTH];
1697
1698        error = &hostrcb->hcam.u.error64.u.type_23_error;
1699        errors_logged = be32_to_cpu(error->errors_logged);
1700
1701        ipr_err("Device Errors Detected/Logged: %d/%d\n",
1702                be32_to_cpu(error->errors_detected), errors_logged);
1703
1704        dev_entry = error->dev;
1705
1706        for (i = 0; i < errors_logged; i++, dev_entry++) {
1707                ipr_err_separator;
1708
1709                ipr_err("Device %d : %s", i + 1,
1710                        __ipr_format_res_path(dev_entry->res_path,
1711                                              buffer, sizeof(buffer)));
1712                ipr_log_ext_vpd(&dev_entry->vpd);
1713
1714                ipr_err("-----New Device Information-----\n");
1715                ipr_log_ext_vpd(&dev_entry->new_vpd);
1716
1717                ipr_err("Cache Directory Card Information:\n");
1718                ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1719
1720                ipr_err("Adapter Card Information:\n");
1721                ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1722        }
1723}
1724
1725/**
1726 * ipr_log_config_error - Log a configuration error.
1727 * @ioa_cfg:    ioa config struct
1728 * @hostrcb:    hostrcb struct
1729 *
1730 * Return value:
1731 *      none
1732 **/
1733static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
1734                                 struct ipr_hostrcb *hostrcb)
1735{
1736        int errors_logged, i;
1737        struct ipr_hostrcb_device_data_entry *dev_entry;
1738        struct ipr_hostrcb_type_03_error *error;
1739
1740        error = &hostrcb->hcam.u.error.u.type_03_error;
1741        errors_logged = be32_to_cpu(error->errors_logged);
1742
1743        ipr_err("Device Errors Detected/Logged: %d/%d\n",
1744                be32_to_cpu(error->errors_detected), errors_logged);
1745
1746        dev_entry = error->dev;
1747
1748        for (i = 0; i < errors_logged; i++, dev_entry++) {
1749                ipr_err_separator;
1750
1751                ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1752                ipr_log_vpd(&dev_entry->vpd);
1753
1754                ipr_err("-----New Device Information-----\n");
1755                ipr_log_vpd(&dev_entry->new_vpd);
1756
1757                ipr_err("Cache Directory Card Information:\n");
1758                ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
1759
1760                ipr_err("Adapter Card Information:\n");
1761                ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
1762
1763                ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1764                        be32_to_cpu(dev_entry->ioa_data[0]),
1765                        be32_to_cpu(dev_entry->ioa_data[1]),
1766                        be32_to_cpu(dev_entry->ioa_data[2]),
1767                        be32_to_cpu(dev_entry->ioa_data[3]),
1768                        be32_to_cpu(dev_entry->ioa_data[4]));
1769        }
1770}
1771
1772/**
1773 * ipr_log_enhanced_array_error - Log an array configuration error.
1774 * @ioa_cfg:    ioa config struct
1775 * @hostrcb:    hostrcb struct
1776 *
1777 * Return value:
1778 *      none
1779 **/
1780static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
1781                                         struct ipr_hostrcb *hostrcb)
1782{
1783        int i, num_entries;
1784        struct ipr_hostrcb_type_14_error *error;
1785        struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
1786        const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1787
1788        error = &hostrcb->hcam.u.error.u.type_14_error;
1789
1790        ipr_err_separator;
1791
1792        ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1793                error->protection_level,
1794                ioa_cfg->host->host_no,
1795                error->last_func_vset_res_addr.bus,
1796                error->last_func_vset_res_addr.target,
1797                error->last_func_vset_res_addr.lun);
1798
1799        ipr_err_separator;
1800
1801        array_entry = error->array_member;
1802        num_entries = min_t(u32, be32_to_cpu(error->num_entries),
1803                            ARRAY_SIZE(error->array_member));
1804
1805        for (i = 0; i < num_entries; i++, array_entry++) {
1806                if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1807                        continue;
1808
1809                if (be32_to_cpu(error->exposed_mode_adn) == i)
1810                        ipr_err("Exposed Array Member %d:\n", i);
1811                else
1812                        ipr_err("Array Member %d:\n", i);
1813
1814                ipr_log_ext_vpd(&array_entry->vpd);
1815                ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1816                ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1817                                 "Expected Location");
1818
1819                ipr_err_separator;
1820        }
1821}
1822
1823/**
1824 * ipr_log_array_error - Log an array configuration error.
1825 * @ioa_cfg:    ioa config struct
1826 * @hostrcb:    hostrcb struct
1827 *
1828 * Return value:
1829 *      none
1830 **/
1831static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1832                                struct ipr_hostrcb *hostrcb)
1833{
1834        int i;
1835        struct ipr_hostrcb_type_04_error *error;
1836        struct ipr_hostrcb_array_data_entry *array_entry;
1837        const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1838
1839        error = &hostrcb->hcam.u.error.u.type_04_error;
1840
1841        ipr_err_separator;
1842
1843        ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1844                error->protection_level,
1845                ioa_cfg->host->host_no,
1846                error->last_func_vset_res_addr.bus,
1847                error->last_func_vset_res_addr.target,
1848                error->last_func_vset_res_addr.lun);
1849
1850        ipr_err_separator;
1851
1852        array_entry = error->array_member;
1853
1854        for (i = 0; i < 18; i++) {
1855                if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1856                        continue;
1857
1858                if (be32_to_cpu(error->exposed_mode_adn) == i)
1859                        ipr_err("Exposed Array Member %d:\n", i);
1860                else
1861                        ipr_err("Array Member %d:\n", i);
1862
1863                ipr_log_vpd(&array_entry->vpd);
1864
1865                ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1866                ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1867                                 "Expected Location");
1868
1869                ipr_err_separator;
1870
1871                if (i == 9)
1872                        array_entry = error->array_member2;
1873                else
1874                        array_entry++;
1875        }
1876}
1877
1878/**
1879 * ipr_log_hex_data - Log additional hex IOA error data.
1880 * @ioa_cfg:    ioa config struct
1881 * @data:               IOA error data
1882 * @len:                data length
1883 *
1884 * Return value:
1885 *      none
1886 **/
1887static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
1888{
1889        int i;
1890
1891        if (len == 0)
1892                return;
1893
1894        if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
1895                len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
1896
1897        for (i = 0; i < len / 4; i += 4) {
1898                ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1899                        be32_to_cpu(data[i]),
1900                        be32_to_cpu(data[i+1]),
1901                        be32_to_cpu(data[i+2]),
1902                        be32_to_cpu(data[i+3]));
1903        }
1904}
1905
1906/**
1907 * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
1908 * @ioa_cfg:    ioa config struct
1909 * @hostrcb:    hostrcb struct
1910 *
1911 * Return value:
1912 *      none
1913 **/
1914static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1915                                            struct ipr_hostrcb *hostrcb)
1916{
1917        struct ipr_hostrcb_type_17_error *error;
1918
1919        if (ioa_cfg->sis64)
1920                error = &hostrcb->hcam.u.error64.u.type_17_error;
1921        else
1922                error = &hostrcb->hcam.u.error.u.type_17_error;
1923
1924        error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1925        strim(error->failure_reason);
1926
1927        ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1928                     be32_to_cpu(hostrcb->hcam.u.error.prc));
1929        ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1930        ipr_log_hex_data(ioa_cfg, error->data,
1931                         be32_to_cpu(hostrcb->hcam.length) -
1932                         (offsetof(struct ipr_hostrcb_error, u) +
1933                          offsetof(struct ipr_hostrcb_type_17_error, data)));
1934}
1935
1936/**
1937 * ipr_log_dual_ioa_error - Log a dual adapter error.
1938 * @ioa_cfg:    ioa config struct
1939 * @hostrcb:    hostrcb struct
1940 *
1941 * Return value:
1942 *      none
1943 **/
1944static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1945                                   struct ipr_hostrcb *hostrcb)
1946{
1947        struct ipr_hostrcb_type_07_error *error;
1948
1949        error = &hostrcb->hcam.u.error.u.type_07_error;
1950        error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1951        strim(error->failure_reason);
1952
1953        ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1954                     be32_to_cpu(hostrcb->hcam.u.error.prc));
1955        ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1956        ipr_log_hex_data(ioa_cfg, error->data,
1957                         be32_to_cpu(hostrcb->hcam.length) -
1958                         (offsetof(struct ipr_hostrcb_error, u) +
1959                          offsetof(struct ipr_hostrcb_type_07_error, data)));
1960}
1961
1962static const struct {
1963        u8 active;
1964        char *desc;
1965} path_active_desc[] = {
1966        { IPR_PATH_NO_INFO, "Path" },
1967        { IPR_PATH_ACTIVE, "Active path" },
1968        { IPR_PATH_NOT_ACTIVE, "Inactive path" }
1969};
1970
1971static const struct {
1972        u8 state;
1973        char *desc;
1974} path_state_desc[] = {
1975        { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
1976        { IPR_PATH_HEALTHY, "is healthy" },
1977        { IPR_PATH_DEGRADED, "is degraded" },
1978        { IPR_PATH_FAILED, "is failed" }
1979};
1980
1981/**
1982 * ipr_log_fabric_path - Log a fabric path error
1983 * @hostrcb:    hostrcb struct
1984 * @fabric:             fabric descriptor
1985 *
1986 * Return value:
1987 *      none
1988 **/
1989static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
1990                                struct ipr_hostrcb_fabric_desc *fabric)
1991{
1992        int i, j;
1993        u8 path_state = fabric->path_state;
1994        u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1995        u8 state = path_state & IPR_PATH_STATE_MASK;
1996
1997        for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1998                if (path_active_desc[i].active != active)
1999                        continue;
2000
2001                for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
2002                        if (path_state_desc[j].state != state)
2003                                continue;
2004
2005                        if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
2006                                ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
2007                                             path_active_desc[i].desc, path_state_desc[j].desc,
2008                                             fabric->ioa_port);
2009                        } else if (fabric->cascaded_expander == 0xff) {
2010                                ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
2011                                             path_active_desc[i].desc, path_state_desc[j].desc,
2012                                             fabric->ioa_port, fabric->phy);
2013                        } else if (fabric->phy == 0xff) {
2014                                ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
2015                                             path_active_desc[i].desc, path_state_desc[j].desc,
2016                                             fabric->ioa_port, fabric->cascaded_expander);
2017                        } else {
2018                                ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
2019                                             path_active_desc[i].desc, path_state_desc[j].desc,
2020                                             fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
2021                        }
2022                        return;
2023                }
2024        }
2025
2026        ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
2027                fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
2028}
2029
2030/**
2031 * ipr_log64_fabric_path - Log a fabric path error
2032 * @hostrcb:    hostrcb struct
2033 * @fabric:             fabric descriptor
2034 *
2035 * Return value:
2036 *      none
2037 **/
2038static void ipr_log64_fabric_path(struct ipr_hostrcb *hostrcb,
2039                                  struct ipr_hostrcb64_fabric_desc *fabric)
2040{
2041        int i, j;
2042        u8 path_state = fabric->path_state;
2043        u8 active = path_state & IPR_PATH_ACTIVE_MASK;
2044        u8 state = path_state & IPR_PATH_STATE_MASK;
2045        char buffer[IPR_MAX_RES_PATH_LENGTH];
2046
2047        for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
2048                if (path_active_desc[i].active != active)
2049                        continue;
2050
2051                for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
2052                        if (path_state_desc[j].state != state)
2053                                continue;
2054
2055                        ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s\n",
2056                                     path_active_desc[i].desc, path_state_desc[j].desc,
2057                                     ipr_format_res_path(hostrcb->ioa_cfg,
2058                                                fabric->res_path,
2059                                                buffer, sizeof(buffer)));
2060                        return;
2061                }
2062        }
2063
2064        ipr_err("Path state=%02X Resource Path=%s\n", path_state,
2065                ipr_format_res_path(hostrcb->ioa_cfg, fabric->res_path,
2066                                    buffer, sizeof(buffer)));
2067}
2068
2069static const struct {
2070        u8 type;
2071        char *desc;
2072} path_type_desc[] = {
2073        { IPR_PATH_CFG_IOA_PORT, "IOA port" },
2074        { IPR_PATH_CFG_EXP_PORT, "Expander port" },
2075        { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
2076        { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
2077};
2078
2079static const struct {
2080        u8 status;
2081        char *desc;
2082} path_status_desc[] = {
2083        { IPR_PATH_CFG_NO_PROB, "Functional" },
2084        { IPR_PATH_CFG_DEGRADED, "Degraded" },
2085        { IPR_PATH_CFG_FAILED, "Failed" },
2086        { IPR_PATH_CFG_SUSPECT, "Suspect" },
2087        { IPR_PATH_NOT_DETECTED, "Missing" },
2088        { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
2089};
2090
2091static const char *link_rate[] = {
2092        "unknown",
2093        "disabled",
2094        "phy reset problem",
2095        "spinup hold",
2096        "port selector",
2097        "unknown",
2098        "unknown",
2099        "unknown",
2100        "1.5Gbps",
2101        "3.0Gbps",
2102        "unknown",
2103        "unknown",
2104        "unknown",
2105        "unknown",
2106        "unknown",
2107        "unknown"
2108};
2109
2110/**
2111 * ipr_log_path_elem - Log a fabric path element.
2112 * @hostrcb:    hostrcb struct
2113 * @cfg:                fabric path element struct
2114 *
2115 * Return value:
2116 *      none
2117 **/
2118static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
2119                              struct ipr_hostrcb_config_element *cfg)
2120{
2121        int i, j;
2122        u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
2123        u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
2124
2125        if (type == IPR_PATH_CFG_NOT_EXIST)
2126                return;
2127
2128        for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
2129                if (path_type_desc[i].type != type)
2130                        continue;
2131
2132                for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
2133                        if (path_status_desc[j].status != status)
2134                                continue;
2135
2136                        if (type == IPR_PATH_CFG_IOA_PORT) {
2137                                ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
2138                                             path_status_desc[j].desc, path_type_desc[i].desc,
2139                                             cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2140                                             be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2141                        } else {
2142                                if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
2143                                        ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
2144                                                     path_status_desc[j].desc, path_type_desc[i].desc,
2145                                                     link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2146                                                     be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2147                                } else if (cfg->cascaded_expander == 0xff) {
2148                                        ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
2149                                                     "WWN=%08X%08X\n", path_status_desc[j].desc,
2150                                                     path_type_desc[i].desc, cfg->phy,
2151                                                     link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2152                                                     be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2153                                } else if (cfg->phy == 0xff) {
2154                                        ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
2155                                                     "WWN=%08X%08X\n", path_status_desc[j].desc,
2156                                                     path_type_desc[i].desc, cfg->cascaded_expander,
2157                                                     link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2158                                                     be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2159                                } else {
2160                                        ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
2161                                                     "WWN=%08X%08X\n", path_status_desc[j].desc,
2162                                                     path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
2163                                                     link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2164                                                     be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2165                                }
2166                        }
2167                        return;
2168                }
2169        }
2170
2171        ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
2172                     "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
2173                     link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2174                     be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2175}
2176
2177/**
2178 * ipr_log64_path_elem - Log a fabric path element.
2179 * @hostrcb:    hostrcb struct
2180 * @cfg:                fabric path element struct
2181 *
2182 * Return value:
2183 *      none
2184 **/
2185static void ipr_log64_path_elem(struct ipr_hostrcb *hostrcb,
2186                                struct ipr_hostrcb64_config_element *cfg)
2187{
2188        int i, j;
2189        u8 desc_id = cfg->descriptor_id & IPR_DESCRIPTOR_MASK;
2190        u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
2191        u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
2192        char buffer[IPR_MAX_RES_PATH_LENGTH];
2193
2194        if (type == IPR_PATH_CFG_NOT_EXIST || desc_id != IPR_DESCRIPTOR_SIS64)
2195                return;
2196
2197        for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
2198                if (path_type_desc[i].type != type)
2199                        continue;
2200
2201                for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
2202                        if (path_status_desc[j].status != status)
2203                                continue;
2204
2205                        ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s, Link rate=%s, WWN=%08X%08X\n",
2206                                     path_status_desc[j].desc, path_type_desc[i].desc,
2207                                     ipr_format_res_path(hostrcb->ioa_cfg,
2208                                        cfg->res_path, buffer, sizeof(buffer)),
2209                                        link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2210                                        be32_to_cpu(cfg->wwid[0]),
2211                                        be32_to_cpu(cfg->wwid[1]));
2212                        return;
2213                }
2214        }
2215        ipr_hcam_err(hostrcb, "Path element=%02X: Resource Path=%s, Link rate=%s "
2216                     "WWN=%08X%08X\n", cfg->type_status,
2217                     ipr_format_res_path(hostrcb->ioa_cfg,
2218                        cfg->res_path, buffer, sizeof(buffer)),
2219                        link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2220                        be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2221}
2222
2223/**
2224 * ipr_log_fabric_error - Log a fabric error.
2225 * @ioa_cfg:    ioa config struct
2226 * @hostrcb:    hostrcb struct
2227 *
2228 * Return value:
2229 *      none
2230 **/
2231static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
2232                                 struct ipr_hostrcb *hostrcb)
2233{
2234        struct ipr_hostrcb_type_20_error *error;
2235        struct ipr_hostrcb_fabric_desc *fabric;
2236        struct ipr_hostrcb_config_element *cfg;
2237        int i, add_len;
2238
2239        error = &hostrcb->hcam.u.error.u.type_20_error;
2240        error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
2241        ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
2242
2243        add_len = be32_to_cpu(hostrcb->hcam.length) -
2244                (offsetof(struct ipr_hostrcb_error, u) +
2245                 offsetof(struct ipr_hostrcb_type_20_error, desc));
2246
2247        for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
2248                ipr_log_fabric_path(hostrcb, fabric);
2249                for_each_fabric_cfg(fabric, cfg)
2250                        ipr_log_path_elem(hostrcb, cfg);
2251
2252                add_len -= be16_to_cpu(fabric->length);
2253                fabric = (struct ipr_hostrcb_fabric_desc *)
2254                        ((unsigned long)fabric + be16_to_cpu(fabric->length));
2255        }
2256
2257        ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
2258}
2259
2260/**
2261 * ipr_log_sis64_array_error - Log a sis64 array error.
2262 * @ioa_cfg:    ioa config struct
2263 * @hostrcb:    hostrcb struct
2264 *
2265 * Return value:
2266 *      none
2267 **/
2268static void ipr_log_sis64_array_error(struct ipr_ioa_cfg *ioa_cfg,
2269                                      struct ipr_hostrcb *hostrcb)
2270{
2271        int i, num_entries;
2272        struct ipr_hostrcb_type_24_error *error;
2273        struct ipr_hostrcb64_array_data_entry *array_entry;
2274        char buffer[IPR_MAX_RES_PATH_LENGTH];
2275        const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
2276
2277        error = &hostrcb->hcam.u.error64.u.type_24_error;
2278
2279        ipr_err_separator;
2280
2281        ipr_err("RAID %s Array Configuration: %s\n",
2282                error->protection_level,
2283                ipr_format_res_path(ioa_cfg, error->last_res_path,
2284                        buffer, sizeof(buffer)));
2285
2286        ipr_err_separator;
2287
2288        array_entry = error->array_member;
2289        num_entries = min_t(u32, error->num_entries,
2290                            ARRAY_SIZE(error->array_member));
2291
2292        for (i = 0; i < num_entries; i++, array_entry++) {
2293
2294                if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
2295                        continue;
2296
2297                if (error->exposed_mode_adn == i)
2298                        ipr_err("Exposed Array Member %d:\n", i);
2299                else
2300                        ipr_err("Array Member %d:\n", i);
2301
2302                ipr_err("Array Member %d:\n", i);
2303                ipr_log_ext_vpd(&array_entry->vpd);
2304                ipr_err("Current Location: %s\n",
2305                         ipr_format_res_path(ioa_cfg, array_entry->res_path,
2306                                buffer, sizeof(buffer)));
2307                ipr_err("Expected Location: %s\n",
2308                         ipr_format_res_path(ioa_cfg,
2309                                array_entry->expected_res_path,
2310                                buffer, sizeof(buffer)));
2311
2312                ipr_err_separator;
2313        }
2314}
2315
2316/**
2317 * ipr_log_sis64_fabric_error - Log a sis64 fabric error.
2318 * @ioa_cfg:    ioa config struct
2319 * @hostrcb:    hostrcb struct
2320 *
2321 * Return value:
2322 *      none
2323 **/
2324static void ipr_log_sis64_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
2325                                       struct ipr_hostrcb *hostrcb)
2326{
2327        struct ipr_hostrcb_type_30_error *error;
2328        struct ipr_hostrcb64_fabric_desc *fabric;
2329        struct ipr_hostrcb64_config_element *cfg;
2330        int i, add_len;
2331
2332        error = &hostrcb->hcam.u.error64.u.type_30_error;
2333
2334        error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
2335        ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
2336
2337        add_len = be32_to_cpu(hostrcb->hcam.length) -
2338                (offsetof(struct ipr_hostrcb64_error, u) +
2339                 offsetof(struct ipr_hostrcb_type_30_error, desc));
2340
2341        for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
2342                ipr_log64_fabric_path(hostrcb, fabric);
2343                for_each_fabric_cfg(fabric, cfg)
2344                        ipr_log64_path_elem(hostrcb, cfg);
2345
2346                add_len -= be16_to_cpu(fabric->length);
2347                fabric = (struct ipr_hostrcb64_fabric_desc *)
2348                        ((unsigned long)fabric + be16_to_cpu(fabric->length));
2349        }
2350
2351        ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
2352}
2353
2354/**
2355 * ipr_log_generic_error - Log an adapter error.
2356 * @ioa_cfg:    ioa config struct
2357 * @hostrcb:    hostrcb struct
2358 *
2359 * Return value:
2360 *      none
2361 **/
2362static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
2363                                  struct ipr_hostrcb *hostrcb)
2364{
2365        ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
2366                         be32_to_cpu(hostrcb->hcam.length));
2367}
2368
2369/**
2370 * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
2371 * @ioasc:      IOASC
2372 *
2373 * This function will return the index of into the ipr_error_table
2374 * for the specified IOASC. If the IOASC is not in the table,
2375 * 0 will be returned, which points to the entry used for unknown errors.
2376 *
2377 * Return value:
2378 *      index into the ipr_error_table
2379 **/
2380static u32 ipr_get_error(u32 ioasc)
2381{
2382        int i;
2383
2384        for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
2385                if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
2386                        return i;
2387
2388        return 0;
2389}
2390
2391/**
2392 * ipr_handle_log_data - Log an adapter error.
2393 * @ioa_cfg:    ioa config struct
2394 * @hostrcb:    hostrcb struct
2395 *
2396 * This function logs an adapter error to the system.
2397 *
2398 * Return value:
2399 *      none
2400 **/
2401static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
2402                                struct ipr_hostrcb *hostrcb)
2403{
2404        u32 ioasc;
2405        int error_index;
2406
2407        if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
2408                return;
2409
2410        if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
2411                dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
2412
2413        if (ioa_cfg->sis64)
2414                ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
2415        else
2416                ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
2417
2418        if (!ioa_cfg->sis64 && (ioasc == IPR_IOASC_BUS_WAS_RESET ||
2419            ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER)) {
2420                /* Tell the midlayer we had a bus reset so it will handle the UA properly */
2421                scsi_report_bus_reset(ioa_cfg->host,
2422                                      hostrcb->hcam.u.error.fd_res_addr.bus);
2423        }
2424
2425        error_index = ipr_get_error(ioasc);
2426
2427        if (!ipr_error_table[error_index].log_hcam)
2428                return;
2429
2430        ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
2431
2432        /* Set indication we have logged an error */
2433        ioa_cfg->errors_logged++;
2434
2435        if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
2436                return;
2437        if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
2438                hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
2439
2440        switch (hostrcb->hcam.overlay_id) {
2441        case IPR_HOST_RCB_OVERLAY_ID_2:
2442                ipr_log_cache_error(ioa_cfg, hostrcb);
2443                break;
2444        case IPR_HOST_RCB_OVERLAY_ID_3:
2445                ipr_log_config_error(ioa_cfg, hostrcb);
2446                break;
2447        case IPR_HOST_RCB_OVERLAY_ID_4:
2448        case IPR_HOST_RCB_OVERLAY_ID_6:
2449                ipr_log_array_error(ioa_cfg, hostrcb);
2450                break;
2451        case IPR_HOST_RCB_OVERLAY_ID_7:
2452                ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
2453                break;
2454        case IPR_HOST_RCB_OVERLAY_ID_12:
2455                ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
2456                break;
2457        case IPR_HOST_RCB_OVERLAY_ID_13:
2458                ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
2459                break;
2460        case IPR_HOST_RCB_OVERLAY_ID_14:
2461        case IPR_HOST_RCB_OVERLAY_ID_16:
2462                ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
2463                break;
2464        case IPR_HOST_RCB_OVERLAY_ID_17:
2465                ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
2466                break;
2467        case IPR_HOST_RCB_OVERLAY_ID_20:
2468                ipr_log_fabric_error(ioa_cfg, hostrcb);
2469                break;
2470        case IPR_HOST_RCB_OVERLAY_ID_23:
2471                ipr_log_sis64_config_error(ioa_cfg, hostrcb);
2472                break;
2473        case IPR_HOST_RCB_OVERLAY_ID_24:
2474        case IPR_HOST_RCB_OVERLAY_ID_26:
2475                ipr_log_sis64_array_error(ioa_cfg, hostrcb);
2476                break;
2477        case IPR_HOST_RCB_OVERLAY_ID_30:
2478                ipr_log_sis64_fabric_error(ioa_cfg, hostrcb);
2479                break;
2480        case IPR_HOST_RCB_OVERLAY_ID_1:
2481        case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
2482        default:
2483                ipr_log_generic_error(ioa_cfg, hostrcb);
2484                break;
2485        }
2486}
2487
2488/**
2489 * ipr_process_error - Op done function for an adapter error log.
2490 * @ipr_cmd:    ipr command struct
2491 *
2492 * This function is the op done function for an error log host
2493 * controlled async from the adapter. It will log the error and
2494 * send the HCAM back to the adapter.
2495 *
2496 * Return value:
2497 *      none
2498 **/
2499static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
2500{
2501        struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2502        struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
2503        u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
2504        u32 fd_ioasc;
2505
2506        if (ioa_cfg->sis64)
2507                fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
2508        else
2509                fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
2510
2511        list_del(&hostrcb->queue);
2512        list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
2513
2514        if (!ioasc) {
2515                ipr_handle_log_data(ioa_cfg, hostrcb);
2516                if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
2517                        ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
2518        } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
2519                dev_err(&ioa_cfg->pdev->dev,
2520                        "Host RCB failed with IOASC: 0x%08X\n", ioasc);
2521        }
2522
2523        ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
2524}
2525
2526/**
2527 * ipr_timeout -  An internally generated op has timed out.
2528 * @ipr_cmd:    ipr command struct
2529 *
2530 * This function blocks host requests and initiates an
2531 * adapter reset.
2532 *
2533 * Return value:
2534 *      none
2535 **/
2536static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
2537{
2538        unsigned long lock_flags = 0;
2539        struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2540
2541        ENTER;
2542        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2543
2544        ioa_cfg->errors_logged++;
2545        dev_err(&ioa_cfg->pdev->dev,
2546                "Adapter being reset due to command timeout.\n");
2547
2548        if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2549                ioa_cfg->sdt_state = GET_DUMP;
2550
2551        if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
2552                ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2553
2554        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2555        LEAVE;
2556}
2557
2558/**
2559 * ipr_oper_timeout -  Adapter timed out transitioning to operational
2560 * @ipr_cmd:    ipr command struct
2561 *
2562 * This function blocks host requests and initiates an
2563 * adapter reset.
2564 *
2565 * Return value:
2566 *      none
2567 **/
2568static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
2569{
2570        unsigned long lock_flags = 0;
2571        struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2572
2573        ENTER;
2574        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2575
2576        ioa_cfg->errors_logged++;
2577        dev_err(&ioa_cfg->pdev->dev,
2578                "Adapter timed out transitioning to operational.\n");
2579
2580        if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2581                ioa_cfg->sdt_state = GET_DUMP;
2582
2583        if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
2584                if (ipr_fastfail)
2585                        ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
2586                ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2587        }
2588
2589        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2590        LEAVE;
2591}
2592
2593/**
2594 * ipr_find_ses_entry - Find matching SES in SES table
2595 * @res:        resource entry struct of SES
2596 *
2597 * Return value:
2598 *      pointer to SES table entry / NULL on failure
2599 **/
2600static const struct ipr_ses_table_entry *
2601ipr_find_ses_entry(struct ipr_resource_entry *res)
2602{
2603        int i, j, matches;
2604        struct ipr_std_inq_vpids *vpids;
2605        const struct ipr_ses_table_entry *ste = ipr_ses_table;
2606
2607        for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
2608                for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
2609                        if (ste->compare_product_id_byte[j] == 'X') {
2610                                vpids = &res->std_inq_data.vpids;
2611                                if (vpids->product_id[j] == ste->product_id[j])
2612                                        matches++;
2613                                else
2614                                        break;
2615                        } else
2616                                matches++;
2617                }
2618
2619                if (matches == IPR_PROD_ID_LEN)
2620                        return ste;
2621        }
2622
2623        return NULL;
2624}
2625
2626/**
2627 * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
2628 * @ioa_cfg:    ioa config struct
2629 * @bus:                SCSI bus
2630 * @bus_width:  bus width
2631 *
2632 * Return value:
2633 *      SCSI bus speed in units of 100KHz, 1600 is 160 MHz
2634 *      For a 2-byte wide SCSI bus, the maximum transfer speed is
2635 *      twice the maximum transfer rate (e.g. for a wide enabled bus,
2636 *      max 160MHz = max 320MB/sec).
2637 **/
2638static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
2639{
2640        struct ipr_resource_entry *res;
2641        const struct ipr_ses_table_entry *ste;
2642        u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
2643
2644        /* Loop through each config table entry in the config table buffer */
2645        list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2646                if (!(IPR_IS_SES_DEVICE(res->std_inq_data)))
2647                        continue;
2648
2649                if (bus != res->bus)
2650                        continue;
2651
2652                if (!(ste = ipr_find_ses_entry(res)))
2653                        continue;
2654
2655                max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
2656        }
2657
2658        return max_xfer_rate;
2659}
2660
2661/**
2662 * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
2663 * @ioa_cfg:            ioa config struct
2664 * @max_delay:          max delay in micro-seconds to wait
2665 *
2666 * Waits for an IODEBUG ACK from the IOA, doing busy looping.
2667 *
2668 * Return value:
2669 *      0 on success / other on failure
2670 **/
2671static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
2672{
2673        volatile u32 pcii_reg;
2674        int delay = 1;
2675
2676        /* Read interrupt reg until IOA signals IO Debug Acknowledge */
2677        while (delay < max_delay) {
2678                pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
2679
2680                if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
2681                        return 0;
2682
2683                /* udelay cannot be used if delay is more than a few milliseconds */
2684                if ((delay / 1000) > MAX_UDELAY_MS)
2685                        mdelay(delay / 1000);
2686                else
2687                        udelay(delay);
2688
2689                delay += delay;
2690        }
2691        return -EIO;
2692}
2693
2694/**
2695 * ipr_get_sis64_dump_data_section - Dump IOA memory
2696 * @ioa_cfg:                    ioa config struct
2697 * @start_addr:                 adapter address to dump
2698 * @dest:                       destination kernel buffer
2699 * @length_in_words:            length to dump in 4 byte words
2700 *
2701 * Return value:
2702 *      0 on success
2703 **/
2704static int ipr_get_sis64_dump_data_section(struct ipr_ioa_cfg *ioa_cfg,
2705                                           u32 start_addr,
2706                                           __be32 *dest, u32 length_in_words)
2707{
2708        int i;
2709
2710        for (i = 0; i < length_in_words; i++) {
2711                writel(start_addr+(i*4), ioa_cfg->regs.dump_addr_reg);
2712                *dest = cpu_to_be32(readl(ioa_cfg->regs.dump_data_reg));
2713                dest++;
2714        }
2715
2716        return 0;
2717}
2718
2719/**
2720 * ipr_get_ldump_data_section - Dump IOA memory
2721 * @ioa_cfg:                    ioa config struct
2722 * @start_addr:                 adapter address to dump
2723 * @dest:                               destination kernel buffer
2724 * @length_in_words:    length to dump in 4 byte words
2725 *
2726 * Return value:
2727 *      0 on success / -EIO on failure
2728 **/
2729static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
2730                                      u32 start_addr,
2731                                      __be32 *dest, u32 length_in_words)
2732{
2733        volatile u32 temp_pcii_reg;
2734        int i, delay = 0;
2735
2736        if (ioa_cfg->sis64)
2737                return ipr_get_sis64_dump_data_section(ioa_cfg, start_addr,
2738                                                       dest, length_in_words);
2739
2740        /* Write IOA interrupt reg starting LDUMP state  */
2741        writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
2742               ioa_cfg->regs.set_uproc_interrupt_reg32);
2743
2744        /* Wait for IO debug acknowledge */
2745        if (ipr_wait_iodbg_ack(ioa_cfg,
2746                               IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
2747                dev_err(&ioa_cfg->pdev->dev,
2748                        "IOA dump long data transfer timeout\n");
2749                return -EIO;
2750        }
2751
2752        /* Signal LDUMP interlocked - clear IO debug ack */
2753        writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2754               ioa_cfg->regs.clr_interrupt_reg);
2755
2756        /* Write Mailbox with starting address */
2757        writel(start_addr, ioa_cfg->ioa_mailbox);
2758
2759        /* Signal address valid - clear IOA Reset alert */
2760        writel(IPR_UPROCI_RESET_ALERT,
2761               ioa_cfg->regs.clr_uproc_interrupt_reg32);
2762
2763        for (i = 0; i < length_in_words; i++) {
2764                /* Wait for IO debug acknowledge */
2765                if (ipr_wait_iodbg_ack(ioa_cfg,
2766                                       IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
2767                        dev_err(&ioa_cfg->pdev->dev,
2768                                "IOA dump short data transfer timeout\n");
2769                        return -EIO;
2770                }
2771
2772                /* Read data from mailbox and increment destination pointer */
2773                *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
2774                dest++;
2775
2776                /* For all but the last word of data, signal data received */
2777                if (i < (length_in_words - 1)) {
2778                        /* Signal dump data received - Clear IO debug Ack */
2779                        writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2780                               ioa_cfg->regs.clr_interrupt_reg);
2781                }
2782        }
2783
2784        /* Signal end of block transfer. Set reset alert then clear IO debug ack */
2785        writel(IPR_UPROCI_RESET_ALERT,
2786               ioa_cfg->regs.set_uproc_interrupt_reg32);
2787
2788        writel(IPR_UPROCI_IO_DEBUG_ALERT,
2789               ioa_cfg->regs.clr_uproc_interrupt_reg32);
2790
2791        /* Signal dump data received - Clear IO debug Ack */
2792        writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2793               ioa_cfg->regs.clr_interrupt_reg);
2794
2795        /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
2796        while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
2797                temp_pcii_reg =
2798                    readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
2799
2800                if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
2801                        return 0;
2802
2803                udelay(10);
2804                delay += 10;
2805        }
2806
2807        return 0;
2808}
2809
2810#ifdef CONFIG_SCSI_IPR_DUMP
2811/**
2812 * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
2813 * @ioa_cfg:            ioa config struct
2814 * @pci_address:        adapter address
2815 * @length:                     length of data to copy
2816 *
2817 * Copy data from PCI adapter to kernel buffer.
2818 * Note: length MUST be a 4 byte multiple
2819 * Return value:
2820 *      0 on success / other on failure
2821 **/
2822static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
2823                        unsigned long pci_address, u32 length)
2824{
2825        int bytes_copied = 0;
2826        int cur_len, rc, rem_len, rem_page_len, max_dump_size;
2827        __be32 *page;
2828        unsigned long lock_flags = 0;
2829        struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
2830
2831        if (ioa_cfg->sis64)
2832                max_dump_size = IPR_FMT3_MAX_IOA_DUMP_SIZE;
2833        else
2834                max_dump_size = IPR_FMT2_MAX_IOA_DUMP_SIZE;
2835
2836        while (bytes_copied < length &&
2837               (ioa_dump->hdr.len + bytes_copied) < max_dump_size) {
2838                if (ioa_dump->page_offset >= PAGE_SIZE ||
2839                    ioa_dump->page_offset == 0) {
2840                        page = (__be32 *)__get_free_page(GFP_ATOMIC);
2841
2842                        if (!page) {
2843                                ipr_trace;
2844                                return bytes_copied;
2845                        }
2846
2847                        ioa_dump->page_offset = 0;
2848                        ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
2849                        ioa_dump->next_page_index++;
2850                } else
2851                        page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
2852
2853                rem_len = length - bytes_copied;
2854                rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
2855                cur_len = min(rem_len, rem_page_len);
2856
2857                spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2858                if (ioa_cfg->sdt_state == ABORT_DUMP) {
2859                        rc = -EIO;
2860                } else {
2861                        rc = ipr_get_ldump_data_section(ioa_cfg,
2862                                                        pci_address + bytes_copied,
2863                                                        &page[ioa_dump->page_offset / 4],
2864                                                        (cur_len / sizeof(u32)));
2865                }
2866                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2867
2868                if (!rc) {
2869                        ioa_dump->page_offset += cur_len;
2870                        bytes_copied += cur_len;
2871                } else {
2872                        ipr_trace;
2873                        break;
2874                }
2875                schedule();
2876        }
2877
2878        return bytes_copied;
2879}
2880
2881/**
2882 * ipr_init_dump_entry_hdr - Initialize a dump entry header.
2883 * @hdr:        dump entry header struct
2884 *
2885 * Return value:
2886 *      nothing
2887 **/
2888static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
2889{
2890        hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
2891        hdr->num_elems = 1;
2892        hdr->offset = sizeof(*hdr);
2893        hdr->status = IPR_DUMP_STATUS_SUCCESS;
2894}
2895
2896/**
2897 * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
2898 * @ioa_cfg:    ioa config struct
2899 * @driver_dump:        driver dump struct
2900 *
2901 * Return value:
2902 *      nothing
2903 **/
2904static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
2905                                   struct ipr_driver_dump *driver_dump)
2906{
2907        struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2908
2909        ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
2910        driver_dump->ioa_type_entry.hdr.len =
2911                sizeof(struct ipr_dump_ioa_type_entry) -
2912                sizeof(struct ipr_dump_entry_header);
2913        driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2914        driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
2915        driver_dump->ioa_type_entry.type = ioa_cfg->type;
2916        driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
2917                (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
2918                ucode_vpd->minor_release[1];
2919        driver_dump->hdr.num_entries++;
2920}
2921
2922/**
2923 * ipr_dump_version_data - Fill in the driver version in the dump.
2924 * @ioa_cfg:    ioa config struct
2925 * @driver_dump:        driver dump struct
2926 *
2927 * Return value:
2928 *      nothing
2929 **/
2930static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
2931                                  struct ipr_driver_dump *driver_dump)
2932{
2933        ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
2934        driver_dump->version_entry.hdr.len =
2935                sizeof(struct ipr_dump_version_entry) -
2936                sizeof(struct ipr_dump_entry_header);
2937        driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2938        driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
2939        strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
2940        driver_dump->hdr.num_entries++;
2941}
2942
2943/**
2944 * ipr_dump_trace_data - Fill in the IOA trace in the dump.
2945 * @ioa_cfg:    ioa config struct
2946 * @driver_dump:        driver dump struct
2947 *
2948 * Return value:
2949 *      nothing
2950 **/
2951static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
2952                                   struct ipr_driver_dump *driver_dump)
2953{
2954        ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
2955        driver_dump->trace_entry.hdr.len =
2956                sizeof(struct ipr_dump_trace_entry) -
2957                sizeof(struct ipr_dump_entry_header);
2958        driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2959        driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
2960        memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
2961        driver_dump->hdr.num_entries++;
2962}
2963
2964/**
2965 * ipr_dump_location_data - Fill in the IOA location in the dump.
2966 * @ioa_cfg:    ioa config struct
2967 * @driver_dump:        driver dump struct
2968 *
2969 * Return value:
2970 *      nothing
2971 **/
2972static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
2973                                   struct ipr_driver_dump *driver_dump)
2974{
2975        ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
2976        driver_dump->location_entry.hdr.len =
2977                sizeof(struct ipr_dump_location_entry) -
2978                sizeof(struct ipr_dump_entry_header);
2979        driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2980        driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
2981        strcpy(driver_dump->location_entry.location, dev_name(&ioa_cfg->pdev->dev));
2982        driver_dump->hdr.num_entries++;
2983}
2984
2985/**
2986 * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
2987 * @ioa_cfg:    ioa config struct
2988 * @dump:               dump struct
2989 *
2990 * Return value:
2991 *      nothing
2992 **/
2993static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
2994{
2995        unsigned long start_addr, sdt_word;
2996        unsigned long lock_flags = 0;
2997        struct ipr_driver_dump *driver_dump = &dump->driver_dump;
2998        struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
2999        u32 num_entries, max_num_entries, start_off, end_off;
3000        u32 max_dump_size, bytes_to_copy, bytes_copied, rc;
3001        struct ipr_sdt *sdt;
3002        int valid = 1;
3003        int i;
3004
3005        ENTER;
3006
3007        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3008
3009        if (ioa_cfg->sdt_state != READ_DUMP) {
3010                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3011                return;
3012        }
3013
3014        if (ioa_cfg->sis64) {
3015                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3016                ssleep(IPR_DUMP_DELAY_SECONDS);
3017                spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3018        }
3019
3020        start_addr = readl(ioa_cfg->ioa_mailbox);
3021
3022        if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(start_addr)) {
3023                dev_err(&ioa_cfg->pdev->dev,
3024                        "Invalid dump table format: %lx\n", start_addr);
3025                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3026                return;
3027        }
3028
3029        dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
3030
3031        driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
3032
3033        /* Initialize the overall dump header */
3034        driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
3035        driver_dump->hdr.num_entries = 1;
3036        driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
3037        driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
3038        driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
3039        driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
3040
3041        ipr_dump_version_data(ioa_cfg, driver_dump);
3042        ipr_dump_location_data(ioa_cfg, driver_dump);
3043        ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
3044        ipr_dump_trace_data(ioa_cfg, driver_dump);
3045
3046        /* Update dump_header */
3047        driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
3048
3049        /* IOA Dump entry */
3050        ipr_init_dump_entry_hdr(&ioa_dump->hdr);
3051        ioa_dump->hdr.len = 0;
3052        ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
3053        ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
3054
3055        /* First entries in sdt are actually a list of dump addresses and
3056         lengths to gather the real dump data.  sdt represents the pointer
3057         to the ioa generated dump table.  Dump data will be extracted based
3058         on entries in this table */
3059        sdt = &ioa_dump->sdt;
3060
3061        if (ioa_cfg->sis64) {
3062                max_num_entries = IPR_FMT3_NUM_SDT_ENTRIES;
3063                max_dump_size = IPR_FMT3_MAX_IOA_DUMP_SIZE;
3064        } else {
3065                max_num_entries = IPR_FMT2_NUM_SDT_ENTRIES;
3066                max_dump_size = IPR_FMT2_MAX_IOA_DUMP_SIZE;
3067        }
3068
3069        bytes_to_copy = offsetof(struct ipr_sdt, entry) +
3070                        (max_num_entries * sizeof(struct ipr_sdt_entry));
3071        rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
3072                                        bytes_to_copy / sizeof(__be32));
3073
3074        /* Smart Dump table is ready to use and the first entry is valid */
3075        if (rc || ((be32_to_cpu(sdt->hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
3076            (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
3077                dev_err(&ioa_cfg->pdev->dev,
3078                        "Dump of IOA failed. Dump table not valid: %d, %X.\n",
3079                        rc, be32_to_cpu(sdt->hdr.state));
3080                driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
3081                ioa_cfg->sdt_state = DUMP_OBTAINED;
3082                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3083                return;
3084        }
3085
3086        num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
3087
3088        if (num_entries > max_num_entries)
3089                num_entries = max_num_entries;
3090
3091        /* Update dump length to the actual data to be copied */
3092        dump->driver_dump.hdr.len += sizeof(struct ipr_sdt_header);
3093        if (ioa_cfg->sis64)
3094                dump->driver_dump.hdr.len += num_entries * sizeof(struct ipr_sdt_entry);
3095        else
3096                dump->driver_dump.hdr.len += max_num_entries * sizeof(struct ipr_sdt_entry);
3097
3098        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3099
3100        for (i = 0; i < num_entries; i++) {
3101                if (ioa_dump->hdr.len > max_dump_size) {
3102                        driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
3103                        break;
3104                }
3105
3106                if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
3107                        sdt_word = be32_to_cpu(sdt->entry[i].start_token);
3108                        if (ioa_cfg->sis64)
3109                                bytes_to_copy = be32_to_cpu(sdt->entry[i].end_token);
3110                        else {
3111                                start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
3112                                end_off = be32_to_cpu(sdt->entry[i].end_token);
3113
3114                                if (ipr_sdt_is_fmt2(sdt_word) && sdt_word)
3115                                        bytes_to_copy = end_off - start_off;
3116                                else
3117                                        valid = 0;
3118                        }
3119                        if (valid) {
3120                                if (bytes_to_copy > max_dump_size) {
3121                                        sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
3122                                        continue;
3123                                }
3124
3125                                /* Copy data from adapter to driver buffers */
3126                                bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
3127                                                            bytes_to_copy);
3128
3129                                ioa_dump->hdr.len += bytes_copied;
3130
3131                                if (bytes_copied != bytes_to_copy) {
3132                                        driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
3133                                        break;
3134                                }
3135                        }
3136                }
3137        }
3138
3139        dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
3140
3141        /* Update dump_header */
3142        driver_dump->hdr.len += ioa_dump->hdr.len;
3143        wmb();
3144        ioa_cfg->sdt_state = DUMP_OBTAINED;
3145        LEAVE;
3146}
3147
3148#else
3149#define ipr_get_ioa_dump(ioa_cfg, dump) do { } while (0)
3150#endif
3151
3152/**
3153 * ipr_release_dump - Free adapter dump memory
3154 * @kref:       kref struct
3155 *
3156 * Return value:
3157 *      nothing
3158 **/
3159static void ipr_release_dump(struct kref *kref)
3160{
3161        struct ipr_dump *dump = container_of(kref, struct ipr_dump, kref);
3162        struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
3163        unsigned long lock_flags = 0;
3164        int i;
3165
3166        ENTER;
3167        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3168        ioa_cfg->dump = NULL;
3169        ioa_cfg->sdt_state = INACTIVE;
3170        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3171
3172        for (i = 0; i < dump->ioa_dump.next_page_index; i++)
3173                free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
3174
3175        vfree(dump->ioa_dump.ioa_data);
3176        kfree(dump);
3177        LEAVE;
3178}
3179
3180/**
3181 * ipr_worker_thread - Worker thread
3182 * @work:               ioa config struct
3183 *
3184 * Called at task level from a work thread. This function takes care
3185 * of adding and removing device from the mid-layer as configuration
3186 * changes are detected by the adapter.
3187 *
3188 * Return value:
3189 *      nothing
3190 **/
3191static void ipr_worker_thread(struct work_struct *work)
3192{
3193        unsigned long lock_flags;
3194        struct ipr_resource_entry *res;
3195        struct scsi_device *sdev;
3196        struct ipr_dump *dump;
3197        struct ipr_ioa_cfg *ioa_cfg =
3198                container_of(work, struct ipr_ioa_cfg, work_q);
3199        u8 bus, target, lun;
3200        int did_work;
3201
3202        ENTER;
3203        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3204
3205        if (ioa_cfg->sdt_state == READ_DUMP) {
3206                dump = ioa_cfg->dump;
3207                if (!dump) {
3208                        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3209                        return;
3210                }
3211                kref_get(&dump->kref);
3212                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3213                ipr_get_ioa_dump(ioa_cfg, dump);
3214                kref_put(&dump->kref, ipr_release_dump);
3215
3216                spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3217                if (ioa_cfg->sdt_state == DUMP_OBTAINED && !ioa_cfg->dump_timeout)
3218                        ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3219                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3220                return;
3221        }
3222
3223restart:
3224        do {
3225                did_work = 0;
3226                if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds ||
3227                    !ioa_cfg->allow_ml_add_del) {
3228                        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3229                        return;
3230                }
3231
3232                list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3233                        if (res->del_from_ml && res->sdev) {
3234                                did_work = 1;
3235                                sdev = res->sdev;
3236                                if (!scsi_device_get(sdev)) {
3237                                        if (!res->add_to_ml)
3238                                                list_move_tail(&res->queue, &ioa_cfg->free_res_q);
3239                                        else
3240                                                res->del_from_ml = 0;
3241                                        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3242                                        scsi_remove_device(sdev);
3243                                        scsi_device_put(sdev);
3244                                        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3245                                }
3246                                break;
3247                        }
3248                }
3249        } while (did_work);
3250
3251        list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3252                if (res->add_to_ml) {
3253                        bus = res->bus;
3254                        target = res->target;
3255                        lun = res->lun;
3256                        res->add_to_ml = 0;
3257                        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3258                        scsi_add_device(ioa_cfg->host, bus, target, lun);
3259                        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3260                        goto restart;
3261                }
3262        }
3263
3264        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3265        kobject_uevent(&ioa_cfg->host->shost_dev.kobj, KOBJ_CHANGE);
3266        LEAVE;
3267}
3268
3269#ifdef CONFIG_SCSI_IPR_TRACE
3270/**
3271 * ipr_read_trace - Dump the adapter trace
3272 * @filp:               open sysfs file
3273 * @kobj:               kobject struct
3274 * @bin_attr:           bin_attribute struct
3275 * @buf:                buffer
3276 * @off:                offset
3277 * @count:              buffer size
3278 *
3279 * Return value:
3280 *      number of bytes printed to buffer
3281 **/
3282static ssize_t ipr_read_trace(struct file *filp, struct kobject *kobj,
3283                              struct bin_attribute *bin_attr,
3284                              char *buf, loff_t off, size_t count)
3285{
3286        struct device *dev = container_of(kobj, struct device, kobj);
3287        struct Scsi_Host *shost = class_to_shost(dev);
3288        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3289        unsigned long lock_flags = 0;
3290        ssize_t ret;
3291
3292        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3293        ret = memory_read_from_buffer(buf, count, &off, ioa_cfg->trace,
3294                                IPR_TRACE_SIZE);
3295        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3296
3297        return ret;
3298}
3299
3300static struct bin_attribute ipr_trace_attr = {
3301        .attr = {
3302                .name = "trace",
3303                .mode = S_IRUGO,
3304        },
3305        .size = 0,
3306        .read = ipr_read_trace,
3307};
3308#endif
3309
3310/**
3311 * ipr_show_fw_version - Show the firmware version
3312 * @dev:        class device struct
3313 * @buf:        buffer
3314 *
3315 * Return value:
3316 *      number of bytes printed to buffer
3317 **/
3318static ssize_t ipr_show_fw_version(struct device *dev,
3319                                   struct device_attribute *attr, char *buf)
3320{
3321        struct Scsi_Host *shost = class_to_shost(dev);
3322        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3323        struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
3324        unsigned long lock_flags = 0;
3325        int len;
3326
3327        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3328        len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
3329                       ucode_vpd->major_release, ucode_vpd->card_type,
3330                       ucode_vpd->minor_release[0],
3331                       ucode_vpd->minor_release[1]);
3332        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3333        return len;
3334}
3335
3336static struct device_attribute ipr_fw_version_attr = {
3337        .attr = {
3338                .name =         "fw_version",
3339                .mode =         S_IRUGO,
3340        },
3341        .show = ipr_show_fw_version,
3342};
3343
3344/**
3345 * ipr_show_log_level - Show the adapter's error logging level
3346 * @dev:        class device struct
3347 * @buf:        buffer
3348 *
3349 * Return value:
3350 *      number of bytes printed to buffer
3351 **/
3352static ssize_t ipr_show_log_level(struct device *dev,
3353                                   struct device_attribute *attr, char *buf)
3354{
3355        struct Scsi_Host *shost = class_to_shost(dev);
3356        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3357        unsigned long lock_flags = 0;
3358        int len;
3359
3360        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3361        len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
3362        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3363        return len;
3364}
3365
3366/**
3367 * ipr_store_log_level - Change the adapter's error logging level
3368 * @dev:        class device struct
3369 * @buf:        buffer
3370 *
3371 * Return value:
3372 *      number of bytes printed to buffer
3373 **/
3374static ssize_t ipr_store_log_level(struct device *dev,
3375                                   struct device_attribute *attr,
3376                                   const char *buf, size_t count)
3377{
3378        struct Scsi_Host *shost = class_to_shost(dev);
3379        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3380        unsigned long lock_flags = 0;
3381
3382        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3383        ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
3384        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3385        return strlen(buf);
3386}
3387
3388static struct device_attribute ipr_log_level_attr = {
3389        .attr = {
3390                .name =         "log_level",
3391                .mode =         S_IRUGO | S_IWUSR,
3392        },
3393        .show = ipr_show_log_level,
3394        .store = ipr_store_log_level
3395};
3396
3397/**
3398 * ipr_store_diagnostics - IOA Diagnostics interface
3399 * @dev:        device struct
3400 * @buf:        buffer
3401 * @count:      buffer size
3402 *
3403 * This function will reset the adapter and wait a reasonable
3404 * amount of time for any errors that the adapter might log.
3405 *
3406 * Return value:
3407 *      count on success / other on failure
3408 **/
3409static ssize_t ipr_store_diagnostics(struct device *dev,
3410                                     struct device_attribute *attr,
3411                                     const char *buf, size_t count)
3412{
3413        struct Scsi_Host *shost = class_to_shost(dev);
3414        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3415        unsigned long lock_flags = 0;
3416        int rc = count;
3417
3418        if (!capable(CAP_SYS_ADMIN))
3419                return -EACCES;
3420
3421        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3422        while (ioa_cfg->in_reset_reload) {
3423                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3424                wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3425                spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3426        }
3427
3428        ioa_cfg->errors_logged = 0;
3429        ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3430
3431        if (ioa_cfg->in_reset_reload) {
3432                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3433                wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3434
3435                /* Wait for a second for any errors to be logged */
3436                msleep(1000);
3437        } else {
3438                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3439                return -EIO;
3440        }
3441
3442        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3443        if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
3444                rc = -EIO;
3445        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3446
3447        return rc;
3448}
3449
3450static struct device_attribute ipr_diagnostics_attr = {
3451        .attr = {
3452                .name =         "run_diagnostics",
3453                .mode =         S_IWUSR,
3454        },
3455        .store = ipr_store_diagnostics
3456};
3457
3458/**
3459 * ipr_show_adapter_state - Show the adapter's state
3460 * @class_dev:  device struct
3461 * @buf:        buffer
3462 *
3463 * Return value:
3464 *      number of bytes printed to buffer
3465 **/
3466static ssize_t ipr_show_adapter_state(struct device *dev,
3467                                      struct device_attribute *attr, char *buf)
3468{
3469        struct Scsi_Host *shost = class_to_shost(dev);
3470        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3471        unsigned long lock_flags = 0;
3472        int len;
3473
3474        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3475        if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
3476                len = snprintf(buf, PAGE_SIZE, "offline\n");
3477        else
3478                len = snprintf(buf, PAGE_SIZE, "online\n");
3479        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3480        return len;
3481}
3482
3483/**
3484 * ipr_store_adapter_state - Change adapter state
3485 * @dev:        device struct
3486 * @buf:        buffer
3487 * @count:      buffer size
3488 *
3489 * This function will change the adapter's state.
3490 *
3491 * Return value:
3492 *      count on success / other on failure
3493 **/
3494static ssize_t ipr_store_adapter_state(struct device *dev,
3495                                       struct device_attribute *attr,
3496                                       const char *buf, size_t count)
3497{
3498        struct Scsi_Host *shost = class_to_shost(dev);
3499        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3500        unsigned long lock_flags;
3501        int result = count, i;
3502
3503        if (!capable(CAP_SYS_ADMIN))
3504                return -EACCES;
3505
3506        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3507        if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead &&
3508            !strncmp(buf, "online", 6)) {
3509                for (i = 0; i < ioa_cfg->hrrq_num; i++) {
3510                        spin_lock(&ioa_cfg->hrrq[i]._lock);
3511                        ioa_cfg->hrrq[i].ioa_is_dead = 0;
3512                        spin_unlock(&ioa_cfg->hrrq[i]._lock);
3513                }
3514                wmb();
3515                ioa_cfg->reset_retries = 0;
3516                ioa_cfg->in_ioa_bringdown = 0;
3517                ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3518        }
3519        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3520        wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3521
3522        return result;
3523}
3524
3525static struct device_attribute ipr_ioa_state_attr = {
3526        .attr = {
3527                .name =         "online_state",
3528                .mode =         S_IRUGO | S_IWUSR,
3529        },
3530        .show = ipr_show_adapter_state,
3531        .store = ipr_store_adapter_state
3532};
3533
3534/**
3535 * ipr_store_reset_adapter - Reset the adapter
3536 * @dev:        device struct
3537 * @buf:        buffer
3538 * @count:      buffer size
3539 *
3540 * This function will reset the adapter.
3541 *
3542 * Return value:
3543 *      count on success / other on failure
3544 **/
3545static ssize_t ipr_store_reset_adapter(struct device *dev,
3546                                       struct device_attribute *attr,
3547                                       const char *buf, size_t count)
3548{
3549        struct Scsi_Host *shost = class_to_shost(dev);
3550        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3551        unsigned long lock_flags;
3552        int result = count;
3553
3554        if (!capable(CAP_SYS_ADMIN))
3555                return -EACCES;
3556
3557        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3558        if (!ioa_cfg->in_reset_reload)
3559                ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3560        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3561        wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3562
3563        return result;
3564}
3565
3566static struct device_attribute ipr_ioa_reset_attr = {
3567        .attr = {
3568                .name =         "reset_host",
3569                .mode =         S_IWUSR,
3570        },
3571        .store = ipr_store_reset_adapter
3572};
3573
3574static int ipr_iopoll(struct blk_iopoll *iop, int budget);
3575 /**
3576 * ipr_show_iopoll_weight - Show ipr polling mode
3577 * @dev:        class device struct
3578 * @buf:        buffer
3579 *
3580 * Return value:
3581 *      number of bytes printed to buffer
3582 **/
3583static ssize_t ipr_show_iopoll_weight(struct device *dev,
3584                                   struct device_attribute *attr, char *buf)
3585{
3586        struct Scsi_Host *shost = class_to_shost(dev);
3587        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3588        unsigned long lock_flags = 0;
3589        int len;
3590
3591        spin_lock_irqsave(shost->host_lock, lock_flags);
3592        len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->iopoll_weight);
3593        spin_unlock_irqrestore(shost->host_lock, lock_flags);
3594
3595        return len;
3596}
3597
3598/**
3599 * ipr_store_iopoll_weight - Change the adapter's polling mode
3600 * @dev:        class device struct
3601 * @buf:        buffer
3602 *
3603 * Return value:
3604 *      number of bytes printed to buffer
3605 **/
3606static ssize_t ipr_store_iopoll_weight(struct device *dev,
3607                                        struct device_attribute *attr,
3608                                        const char *buf, size_t count)
3609{
3610        struct Scsi_Host *shost = class_to_shost(dev);
3611        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3612        unsigned long user_iopoll_weight;
3613        unsigned long lock_flags = 0;
3614        int i;
3615
3616        if (!ioa_cfg->sis64) {
3617                dev_info(&ioa_cfg->pdev->dev, "blk-iopoll not supported on this adapter\n");
3618                return -EINVAL;
3619        }
3620        if (kstrtoul(buf, 10, &user_iopoll_weight))
3621                return -EINVAL;
3622
3623        if (user_iopoll_weight > 256) {
3624                dev_info(&ioa_cfg->pdev->dev, "Invalid blk-iopoll weight. It must be less than 256\n");
3625                return -EINVAL;
3626        }
3627
3628        if (user_iopoll_weight == ioa_cfg->iopoll_weight) {
3629                dev_info(&ioa_cfg->pdev->dev, "Current blk-iopoll weight has the same weight\n");
3630                return strlen(buf);
3631        }
3632
3633        if (blk_iopoll_enabled && ioa_cfg->iopoll_weight &&
3634                        ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
3635                for (i = 1; i < ioa_cfg->hrrq_num; i++)
3636                        blk_iopoll_disable(&ioa_cfg->hrrq[i].iopoll);
3637        }
3638
3639        spin_lock_irqsave(shost->host_lock, lock_flags);
3640        ioa_cfg->iopoll_weight = user_iopoll_weight;
3641        if (blk_iopoll_enabled && ioa_cfg->iopoll_weight &&
3642                        ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
3643                for (i = 1; i < ioa_cfg->hrrq_num; i++) {
3644                        blk_iopoll_init(&ioa_cfg->hrrq[i].iopoll,
3645                                        ioa_cfg->iopoll_weight, ipr_iopoll);
3646                        blk_iopoll_enable(&ioa_cfg->hrrq[i].iopoll);
3647                }
3648        }
3649        spin_unlock_irqrestore(shost->host_lock, lock_flags);
3650
3651        return strlen(buf);
3652}
3653
3654static struct device_attribute ipr_iopoll_weight_attr = {
3655        .attr = {
3656                .name =         "iopoll_weight",
3657                .mode =         S_IRUGO | S_IWUSR,
3658        },
3659        .show = ipr_show_iopoll_weight,
3660        .store = ipr_store_iopoll_weight
3661};
3662
3663/**
3664 * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
3665 * @buf_len:            buffer length
3666 *
3667 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3668 * list to use for microcode download
3669 *
3670 * Return value:
3671 *      pointer to sglist / NULL on failure
3672 **/
3673static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
3674{
3675        int sg_size, order, bsize_elem, num_elem, i, j;
3676        struct ipr_sglist *sglist;
3677        struct scatterlist *scatterlist;
3678        struct page *page;
3679
3680        /* Get the minimum size per scatter/gather element */
3681        sg_size = buf_len / (IPR_MAX_SGLIST - 1);
3682
3683        /* Get the actual size per element */
3684        order = get_order(sg_size);
3685
3686        /* Determine the actual number of bytes per element */
3687        bsize_elem = PAGE_SIZE * (1 << order);
3688
3689        /* Determine the actual number of sg entries needed */
3690        if (buf_len % bsize_elem)
3691                num_elem = (buf_len / bsize_elem) + 1;
3692        else
3693                num_elem = buf_len / bsize_elem;
3694
3695        /* Allocate a scatter/gather list for the DMA */
3696        sglist = kzalloc(sizeof(struct ipr_sglist) +
3697                         (sizeof(struct scatterlist) * (num_elem - 1)),
3698                         GFP_KERNEL);
3699
3700        if (sglist == NULL) {
3701                ipr_trace;
3702                return NULL;
3703        }
3704
3705        scatterlist = sglist->scatterlist;
3706        sg_init_table(scatterlist, num_elem);
3707
3708        sglist->order = order;
3709        sglist->num_sg = num_elem;
3710
3711        /* Allocate a bunch of sg elements */
3712        for (i = 0; i < num_elem; i++) {
3713                page = alloc_pages(GFP_KERNEL, order);
3714                if (!page) {
3715                        ipr_trace;
3716
3717                        /* Free up what we already allocated */
3718                        for (j = i - 1; j >= 0; j--)
3719                                __free_pages(sg_page(&scatterlist[j]), order);
3720                        kfree(sglist);
3721                        return NULL;
3722                }
3723
3724                sg_set_page(&scatterlist[i], page, 0, 0);
3725        }
3726
3727        return sglist;
3728}
3729
3730/**
3731 * ipr_free_ucode_buffer - Frees a microcode download buffer
3732 * @p_dnld:             scatter/gather list pointer
3733 *
3734 * Free a DMA'able ucode download buffer previously allocated with
3735 * ipr_alloc_ucode_buffer
3736 *
3737 * Return value:
3738 *      nothing
3739 **/
3740static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
3741{
3742        int i;
3743
3744        for (i = 0; i < sglist->num_sg; i++)
3745                __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
3746
3747        kfree(sglist);
3748}
3749
3750/**
3751 * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
3752 * @sglist:             scatter/gather list pointer
3753 * @buffer:             buffer pointer
3754 * @len:                buffer length
3755 *
3756 * Copy a microcode image from a user buffer into a buffer allocated by
3757 * ipr_alloc_ucode_buffer
3758 *
3759 * Return value:
3760 *      0 on success / other on failure
3761 **/
3762static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
3763                                 u8 *buffer, u32 len)
3764{
3765        int bsize_elem, i, result = 0;
3766        struct scatterlist *scatterlist;
3767        void *kaddr;
3768
3769        /* Determine the actual number of bytes per element */
3770        bsize_elem = PAGE_SIZE * (1 << sglist->order);
3771
3772        scatterlist = sglist->scatterlist;
3773
3774        for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
3775                struct page *page = sg_page(&scatterlist[i]);
3776
3777                kaddr = kmap(page);
3778                memcpy(kaddr, buffer, bsize_elem);
3779                kunmap(page);
3780
3781                scatterlist[i].length = bsize_elem;
3782
3783                if (result != 0) {
3784                        ipr_trace;
3785                        return result;
3786                }
3787        }
3788
3789        if (len % bsize_elem) {
3790                struct page *page = sg_page(&scatterlist[i]);
3791
3792                kaddr = kmap(page);
3793                memcpy(kaddr, buffer, len % bsize_elem);
3794                kunmap(page);
3795
3796                scatterlist[i].length = len % bsize_elem;
3797        }
3798
3799        sglist->buffer_len = len;
3800        return result;
3801}
3802
3803/**
3804 * ipr_build_ucode_ioadl64 - Build a microcode download IOADL
3805 * @ipr_cmd:            ipr command struct
3806 * @sglist:             scatter/gather list
3807 *
3808 * Builds a microcode download IOA data list (IOADL).
3809 *
3810 **/
3811static void ipr_build_ucode_ioadl64(struct ipr_cmnd *ipr_cmd,
3812                                    struct ipr_sglist *sglist)
3813{
3814        struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3815        struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
3816        struct scatterlist *scatterlist = sglist->scatterlist;
3817        int i;
3818
3819        ipr_cmd->dma_use_sg = sglist->num_dma_sg;
3820        ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3821        ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3822
3823        ioarcb->ioadl_len =
3824                cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
3825        for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3826                ioadl64[i].flags = cpu_to_be32(IPR_IOADL_FLAGS_WRITE);
3827                ioadl64[i].data_len = cpu_to_be32(sg_dma_len(&scatterlist[i]));
3828                ioadl64[i].address = cpu_to_be64(sg_dma_address(&scatterlist[i]));
3829        }
3830
3831        ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3832}
3833
3834/**
3835 * ipr_build_ucode_ioadl - Build a microcode download IOADL
3836 * @ipr_cmd:    ipr command struct
3837 * @sglist:             scatter/gather list
3838 *
3839 * Builds a microcode download IOA data list (IOADL).
3840 *
3841 **/
3842static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
3843                                  struct ipr_sglist *sglist)
3844{
3845        struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3846        struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
3847        struct scatterlist *scatterlist = sglist->scatterlist;
3848        int i;
3849
3850        ipr_cmd->dma_use_sg = sglist->num_dma_sg;
3851        ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3852        ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3853
3854        ioarcb->ioadl_len =
3855                cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3856
3857        for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3858                ioadl[i].flags_and_data_len =
3859                        cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
3860                ioadl[i].address =
3861                        cpu_to_be32(sg_dma_address(&scatterlist[i]));
3862        }
3863
3864        ioadl[i-1].flags_and_data_len |=
3865                cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3866}
3867
3868/**
3869 * ipr_update_ioa_ucode - Update IOA's microcode
3870 * @ioa_cfg:    ioa config struct
3871 * @sglist:             scatter/gather list
3872 *
3873 * Initiate an adapter reset to update the IOA's microcode
3874 *
3875 * Return value:
3876 *      0 on success / -EIO on failure
3877 **/
3878static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
3879                                struct ipr_sglist *sglist)
3880{
3881        unsigned long lock_flags;
3882
3883        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3884        while (ioa_cfg->in_reset_reload) {
3885                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3886                wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3887                spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3888        }
3889
3890        if (ioa_cfg->ucode_sglist) {
3891                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3892                dev_err(&ioa_cfg->pdev->dev,
3893                        "Microcode download already in progress\n");
3894                return -EIO;
3895        }
3896
3897        sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
3898                                        sglist->num_sg, DMA_TO_DEVICE);
3899
3900        if (!sglist->num_dma_sg) {
3901                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3902                dev_err(&ioa_cfg->pdev->dev,
3903                        "Failed to map microcode download buffer!\n");
3904                return -EIO;
3905        }
3906
3907        ioa_cfg->ucode_sglist = sglist;
3908        ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3909        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3910        wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3911
3912        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3913        ioa_cfg->ucode_sglist = NULL;
3914        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3915        return 0;
3916}
3917
3918/**
3919 * ipr_store_update_fw - Update the firmware on the adapter
3920 * @class_dev:  device struct
3921 * @buf:        buffer
3922 * @count:      buffer size
3923 *
3924 * This function will update the firmware on the adapter.
3925 *
3926 * Return value:
3927 *      count on success / other on failure
3928 **/
3929static ssize_t ipr_store_update_fw(struct device *dev,
3930                                   struct device_attribute *attr,
3931                                   const char *buf, size_t count)
3932{
3933        struct Scsi_Host *shost = class_to_shost(dev);
3934        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3935        struct ipr_ucode_image_header *image_hdr;
3936        const struct firmware *fw_entry;
3937        struct ipr_sglist *sglist;
3938        char fname[100];
3939        char *src;
3940        int len, result, dnld_size;
3941
3942        if (!capable(CAP_SYS_ADMIN))
3943                return -EACCES;
3944
3945        len = snprintf(fname, 99, "%s", buf);
3946        fname[len-1] = '\0';
3947
3948        if (request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
3949                dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
3950                return -EIO;
3951        }
3952
3953        image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
3954
3955        src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
3956        dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
3957        sglist = ipr_alloc_ucode_buffer(dnld_size);
3958
3959        if (!sglist) {
3960                dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
3961                release_firmware(fw_entry);
3962                return -ENOMEM;
3963        }
3964
3965        result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
3966
3967        if (result) {
3968                dev_err(&ioa_cfg->pdev->dev,
3969                        "Microcode buffer copy to DMA buffer failed\n");
3970                goto out;
3971        }
3972
3973        ipr_info("Updating microcode, please be patient.  This may take up to 30 minutes.\n");
3974
3975        result = ipr_update_ioa_ucode(ioa_cfg, sglist);
3976
3977        if (!result)
3978                result = count;
3979out:
3980        ipr_free_ucode_buffer(sglist);
3981        release_firmware(fw_entry);
3982        return result;
3983}
3984
3985static struct device_attribute ipr_update_fw_attr = {
3986        .attr = {
3987                .name =         "update_fw",
3988                .mode =         S_IWUSR,
3989        },
3990        .store = ipr_store_update_fw
3991};
3992
3993/**
3994 * ipr_show_fw_type - Show the adapter's firmware type.
3995 * @dev:        class device struct
3996 * @buf:        buffer
3997 *
3998 * Return value:
3999 *      number of bytes printed to buffer
4000 **/
4001static ssize_t ipr_show_fw_type(struct device *dev,
4002                                struct device_attribute *attr, char *buf)
4003{
4004        struct Scsi_Host *shost = class_to_shost(dev);
4005        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
4006        unsigned long lock_flags = 0;
4007        int len;
4008
4009        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4010        len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->sis64);
4011        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4012        return len;
4013}
4014
4015static struct device_attribute ipr_ioa_fw_type_attr = {
4016        .attr = {
4017                .name =         "fw_type",
4018                .mode =         S_IRUGO,
4019        },
4020        .show = ipr_show_fw_type
4021};
4022
4023static struct device_attribute *ipr_ioa_attrs[] = {
4024        &ipr_fw_version_attr,
4025        &ipr_log_level_attr,
4026        &ipr_diagnostics_attr,
4027        &ipr_ioa_state_attr,
4028        &ipr_ioa_reset_attr,
4029        &ipr_update_fw_attr,
4030        &ipr_ioa_fw_type_attr,
4031        &ipr_iopoll_weight_attr,
4032        NULL,
4033};
4034
4035#ifdef CONFIG_SCSI_IPR_DUMP
4036/**
4037 * ipr_read_dump - Dump the adapter
4038 * @filp:               open sysfs file
4039 * @kobj:               kobject struct
4040 * @bin_attr:           bin_attribute struct
4041 * @buf:                buffer
4042 * @off:                offset
4043 * @count:              buffer size
4044 *
4045 * Return value:
4046 *      number of bytes printed to buffer
4047 **/
4048static ssize_t ipr_read_dump(struct file *filp, struct kobject *kobj,
4049                             struct bin_attribute *bin_attr,
4050                             char *buf, loff_t off, size_t count)
4051{
4052        struct device *cdev = container_of(kobj, struct device, kobj);
4053        struct Scsi_Host *shost = class_to_shost(cdev);
4054        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
4055        struct ipr_dump *dump;
4056        unsigned long lock_flags = 0;
4057        char *src;
4058        int len, sdt_end;
4059        size_t rc = count;
4060
4061        if (!capable(CAP_SYS_ADMIN))
4062                return -EACCES;
4063
4064        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4065        dump = ioa_cfg->dump;
4066
4067        if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
4068                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4069                return 0;
4070        }
4071        kref_get(&dump->kref);
4072        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4073
4074        if (off > dump->driver_dump.hdr.len) {
4075                kref_put(&dump->kref, ipr_release_dump);
4076                return 0;
4077        }
4078
4079        if (off + count > dump->driver_dump.hdr.len) {
4080                count = dump->driver_dump.hdr.len - off;
4081                rc = count;
4082        }
4083
4084        if (count && off < sizeof(dump->driver_dump)) {
4085                if (off + count > sizeof(dump->driver_dump))
4086                        len = sizeof(dump->driver_dump) - off;
4087                else
4088                        len = count;
4089                src = (u8 *)&dump->driver_dump + off;
4090                memcpy(buf, src, len);
4091                buf += len;
4092                off += len;
4093                count -= len;
4094        }
4095
4096        off -= sizeof(dump->driver_dump);
4097
4098        if (ioa_cfg->sis64)
4099                sdt_end = offsetof(struct ipr_ioa_dump, sdt.entry) +
4100                          (be32_to_cpu(dump->ioa_dump.sdt.hdr.num_entries_used) *
4101                           sizeof(struct ipr_sdt_entry));
4102        else
4103                sdt_end = offsetof(struct ipr_ioa_dump, sdt.entry) +
4104                          (IPR_FMT2_NUM_SDT_ENTRIES * sizeof(struct ipr_sdt_entry));
4105
4106        if (count && off < sdt_end) {
4107                if (off + count > sdt_end)
4108                        len = sdt_end - off;
4109                else
4110                        len = count;
4111                src = (u8 *)&dump->ioa_dump + off;
4112                memcpy(buf, src, len);
4113                buf += len;
4114                off += len;
4115                count -= len;
4116        }
4117
4118        off -= sdt_end;
4119
4120        while (count) {
4121                if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
4122                        len = PAGE_ALIGN(off) - off;
4123                else
4124                        len = count;
4125                src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
4126                src += off & ~PAGE_MASK;
4127                memcpy(buf, src, len);
4128                buf += len;
4129                off += len;
4130                count -= len;
4131        }
4132
4133        kref_put(&dump->kref, ipr_release_dump);
4134        return rc;
4135}
4136
4137/**
4138 * ipr_alloc_dump - Prepare for adapter dump
4139 * @ioa_cfg:    ioa config struct
4140 *
4141 * Return value:
4142 *      0 on success / other on failure
4143 **/
4144static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
4145{
4146        struct ipr_dump *dump;
4147        __be32 **ioa_data;
4148        unsigned long lock_flags = 0;
4149
4150        dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
4151
4152        if (!dump) {
4153                ipr_err("Dump memory allocation failed\n");
4154                return -ENOMEM;
4155        }
4156
4157        if (ioa_cfg->sis64)
4158                ioa_data = vmalloc(IPR_FMT3_MAX_NUM_DUMP_PAGES * sizeof(__be32 *));
4159        else
4160                ioa_data = vmalloc(IPR_FMT2_MAX_NUM_DUMP_PAGES * sizeof(__be32 *));
4161
4162        if (!ioa_data) {
4163                ipr_err("Dump memory allocation failed\n");
4164                kfree(dump);
4165                return -ENOMEM;
4166        }
4167
4168        dump->ioa_dump.ioa_data = ioa_data;
4169
4170        kref_init(&dump->kref);
4171        dump->ioa_cfg = ioa_cfg;
4172
4173        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4174
4175        if (INACTIVE != ioa_cfg->sdt_state) {
4176                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4177                vfree(dump->ioa_dump.ioa_data);
4178                kfree(dump);
4179                return 0;
4180        }
4181
4182        ioa_cfg->dump = dump;
4183        ioa_cfg->sdt_state = WAIT_FOR_DUMP;
4184        if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead && !ioa_cfg->dump_taken) {
4185                ioa_cfg->dump_taken = 1;
4186                schedule_work(&ioa_cfg->work_q);
4187        }
4188        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4189
4190        return 0;
4191}
4192
4193/**
4194 * ipr_free_dump - Free adapter dump memory
4195 * @ioa_cfg:    ioa config struct
4196 *
4197 * Return value:
4198 *      0 on success / other on failure
4199 **/
4200static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
4201{
4202        struct ipr_dump *dump;
4203        unsigned long lock_flags = 0;
4204
4205        ENTER;
4206
4207        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4208        dump = ioa_cfg->dump;
4209        if (!dump) {
4210                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4211                return 0;
4212        }
4213
4214        ioa_cfg->dump = NULL;
4215        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4216
4217        kref_put(&dump->kref, ipr_release_dump);
4218
4219        LEAVE;
4220        return 0;
4221}
4222
4223/**
4224 * ipr_write_dump - Setup dump state of adapter
4225 * @filp:               open sysfs file
4226 * @kobj:               kobject struct
4227 * @bin_attr:           bin_attribute struct
4228 * @buf:                buffer
4229 * @off:                offset
4230 * @count:              buffer size
4231 *
4232 * Return value:
4233 *      number of bytes printed to buffer
4234 **/
4235static ssize_t ipr_write_dump(struct file *filp, struct kobject *kobj,
4236                              struct bin_attribute *bin_attr,
4237                              char *buf, loff_t off, size_t count)
4238{
4239        struct device *cdev = container_of(kobj, struct device, kobj);
4240        struct Scsi_Host *shost = class_to_shost(cdev);
4241        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
4242        int rc;
4243
4244        if (!capable(CAP_SYS_ADMIN))
4245                return -EACCES;
4246
4247        if (buf[0] == '1')
4248                rc = ipr_alloc_dump(ioa_cfg);
4249        else if (buf[0] == '0')
4250                rc = ipr_free_dump(ioa_cfg);
4251        else
4252                return -EINVAL;
4253
4254        if (rc)
4255                return rc;
4256        else
4257                return count;
4258}
4259
4260static struct bin_attribute ipr_dump_attr = {
4261        .attr = {
4262                .name = "dump",
4263                .mode = S_IRUSR | S_IWUSR,
4264        },
4265        .size = 0,
4266        .read = ipr_read_dump,
4267        .write = ipr_write_dump
4268};
4269#else
4270static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
4271#endif
4272
4273/**
4274 * ipr_change_queue_depth - Change the device's queue depth
4275 * @sdev:       scsi device struct
4276 * @qdepth:     depth to set
4277 * @reason:     calling context
4278 *
4279 * Return value:
4280 *      actual depth set
4281 **/
4282static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth,
4283                                  int reason)
4284{
4285        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4286        struct ipr_resource_entry *res;
4287        unsigned long lock_flags = 0;
4288
4289        if (reason != SCSI_QDEPTH_DEFAULT)
4290                return -EOPNOTSUPP;
4291
4292        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4293        res = (struct ipr_resource_entry *)sdev->hostdata;
4294
4295        if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
4296                qdepth = IPR_MAX_CMD_PER_ATA_LUN;
4297        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4298
4299        scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
4300        return sdev->queue_depth;
4301}
4302
4303/**
4304 * ipr_change_queue_type - Change the device's queue type
4305 * @dsev:               scsi device struct
4306 * @tag_type:   type of tags to use
4307 *
4308 * Return value:
4309 *      actual queue type set
4310 **/
4311static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
4312{
4313        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4314        struct ipr_resource_entry *res;
4315        unsigned long lock_flags = 0;
4316
4317        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4318        res = (struct ipr_resource_entry *)sdev->hostdata;
4319
4320        if (res) {
4321                if (ipr_is_gscsi(res) && sdev->tagged_supported) {
4322                        /*
4323                         * We don't bother quiescing the device here since the
4324                         * adapter firmware does it for us.
4325                         */
4326                        scsi_set_tag_type(sdev, tag_type);
4327
4328                        if (tag_type)
4329                                scsi_activate_tcq(sdev, sdev->queue_depth);
4330                        else
4331                                scsi_deactivate_tcq(sdev, sdev->queue_depth);
4332                } else
4333                        tag_type = 0;
4334        } else
4335                tag_type = 0;
4336
4337        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4338        return tag_type;
4339}
4340
4341/**
4342 * ipr_show_adapter_handle - Show the adapter's resource handle for this device
4343 * @dev:        device struct
4344 * @attr:       device attribute structure
4345 * @buf:        buffer
4346 *
4347 * Return value:
4348 *      number of bytes printed to buffer
4349 **/
4350static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
4351{
4352        struct scsi_device *sdev = to_scsi_device(dev);
4353        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4354        struct ipr_resource_entry *res;
4355        unsigned long lock_flags = 0;
4356        ssize_t len = -ENXIO;
4357
4358        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4359        res = (struct ipr_resource_entry *)sdev->hostdata;
4360        if (res)
4361                len = snprintf(buf, PAGE_SIZE, "%08X\n", res->res_handle);
4362        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4363        return len;
4364}
4365
4366static struct device_attribute ipr_adapter_handle_attr = {
4367        .attr = {
4368                .name =         "adapter_handle",
4369                .mode =         S_IRUSR,
4370        },
4371        .show = ipr_show_adapter_handle
4372};
4373
4374/**
4375 * ipr_show_resource_path - Show the resource path or the resource address for
4376 *                          this device.
4377 * @dev:        device struct
4378 * @attr:       device attribute structure
4379 * @buf:        buffer
4380 *
4381 * Return value:
4382 *      number of bytes printed to buffer
4383 **/
4384static ssize_t ipr_show_resource_path(struct device *dev, struct device_attribute *attr, char *buf)
4385{
4386        struct scsi_device *sdev = to_scsi_device(dev);
4387        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4388        struct ipr_resource_entry *res;
4389        unsigned long lock_flags = 0;
4390        ssize_t len = -ENXIO;
4391        char buffer[IPR_MAX_RES_PATH_LENGTH];
4392
4393        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4394        res = (struct ipr_resource_entry *)sdev->hostdata;
4395        if (res && ioa_cfg->sis64)
4396                len = snprintf(buf, PAGE_SIZE, "%s\n",
4397                               __ipr_format_res_path(res->res_path, buffer,
4398                                                     sizeof(buffer)));
4399        else if (res)
4400                len = snprintf(buf, PAGE_SIZE, "%d:%d:%d:%d\n", ioa_cfg->host->host_no,
4401                               res->bus, res->target, res->lun);
4402
4403        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4404        return len;
4405}
4406
4407static struct device_attribute ipr_resource_path_attr = {
4408        .attr = {
4409                .name =         "resource_path",
4410                .mode =         S_IRUGO,
4411        },
4412        .show = ipr_show_resource_path
4413};
4414
4415/**
4416 * ipr_show_device_id - Show the device_id for this device.
4417 * @dev:        device struct
4418 * @attr:       device attribute structure
4419 * @buf:        buffer
4420 *
4421 * Return value:
4422 *      number of bytes printed to buffer
4423 **/
4424static ssize_t ipr_show_device_id(struct device *dev, struct device_attribute *attr, char *buf)
4425{
4426        struct scsi_device *sdev = to_scsi_device(dev);
4427        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4428        struct ipr_resource_entry *res;
4429        unsigned long lock_flags = 0;
4430        ssize_t len = -ENXIO;
4431
4432        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4433        res = (struct ipr_resource_entry *)sdev->hostdata;
4434        if (res && ioa_cfg->sis64)
4435                len = snprintf(buf, PAGE_SIZE, "0x%llx\n", res->dev_id);
4436        else if (res)
4437                len = snprintf(buf, PAGE_SIZE, "0x%llx\n", res->lun_wwn);
4438
4439        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4440        return len;
4441}
4442
4443static struct device_attribute ipr_device_id_attr = {
4444        .attr = {
4445                .name =         "device_id",
4446                .mode =         S_IRUGO,
4447        },
4448        .show = ipr_show_device_id
4449};
4450
4451/**
4452 * ipr_show_resource_type - Show the resource type for this device.
4453 * @dev:        device struct
4454 * @attr:       device attribute structure
4455 * @buf:        buffer
4456 *
4457 * Return value:
4458 *      number of bytes printed to buffer
4459 **/
4460static ssize_t ipr_show_resource_type(struct device *dev, struct device_attribute *attr, char *buf)
4461{
4462        struct scsi_device *sdev = to_scsi_device(dev);
4463        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4464        struct ipr_resource_entry *res;
4465        unsigned long lock_flags = 0;
4466        ssize_t len = -ENXIO;
4467
4468        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4469        res = (struct ipr_resource_entry *)sdev->hostdata;
4470
4471        if (res)
4472                len = snprintf(buf, PAGE_SIZE, "%x\n", res->type);
4473
4474        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4475        return len;
4476}
4477
4478static struct device_attribute ipr_resource_type_attr = {
4479        .attr = {
4480                .name =         "resource_type",
4481                .mode =         S_IRUGO,
4482        },
4483        .show = ipr_show_resource_type
4484};
4485
4486static struct device_attribute *ipr_dev_attrs[] = {
4487        &ipr_adapter_handle_attr,
4488        &ipr_resource_path_attr,
4489        &ipr_device_id_attr,
4490        &ipr_resource_type_attr,
4491        NULL,
4492};
4493
4494/**
4495 * ipr_biosparam - Return the HSC mapping
4496 * @sdev:                       scsi device struct
4497 * @block_device:       block device pointer
4498 * @capacity:           capacity of the device
4499 * @parm:                       Array containing returned HSC values.
4500 *
4501 * This function generates the HSC parms that fdisk uses.
4502 * We want to make sure we return something that places partitions
4503 * on 4k boundaries for best performance with the IOA.
4504 *
4505 * Return value:
4506 *      0 on success
4507 **/
4508static int ipr_biosparam(struct scsi_device *sdev,
4509                         struct block_device *block_device,
4510                         sector_t capacity, int *parm)
4511{
4512        int heads, sectors;
4513        sector_t cylinders;
4514
4515        heads = 128;
4516        sectors = 32;
4517
4518        cylinders = capacity;
4519        sector_div(cylinders, (128 * 32));
4520
4521        /* return result */
4522        parm[0] = heads;
4523        parm[1] = sectors;
4524        parm[2] = cylinders;
4525
4526        return 0;
4527}
4528
4529/**
4530 * ipr_find_starget - Find target based on bus/target.
4531 * @starget:    scsi target struct
4532 *
4533 * Return value:
4534 *      resource entry pointer if found / NULL if not found
4535 **/
4536static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
4537{
4538        struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4539        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4540        struct ipr_resource_entry *res;
4541
4542        list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4543                if ((res->bus == starget->channel) &&
4544                    (res->target == starget->id)) {
4545                        return res;
4546                }
4547        }
4548
4549        return NULL;
4550}
4551
4552static struct ata_port_info sata_port_info;
4553
4554/**
4555 * ipr_target_alloc - Prepare for commands to a SCSI target
4556 * @starget:    scsi target struct
4557 *
4558 * If the device is a SATA device, this function allocates an
4559 * ATA port with libata, else it does nothing.
4560 *
4561 * Return value:
4562 *      0 on success / non-0 on failure
4563 **/
4564static int ipr_target_alloc(struct scsi_target *starget)
4565{
4566        struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4567        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4568        struct ipr_sata_port *sata_port;
4569        struct ata_port *ap;
4570        struct ipr_resource_entry *res;
4571        unsigned long lock_flags;
4572
4573        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4574        res = ipr_find_starget(starget);
4575        starget->hostdata = NULL;
4576
4577        if (res && ipr_is_gata(res)) {
4578                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4579                sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
4580                if (!sata_port)
4581                        return -ENOMEM;
4582
4583                ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
4584                if (ap) {
4585                        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4586                        sata_port->ioa_cfg = ioa_cfg;
4587                        sata_port->ap = ap;
4588                        sata_port->res = res;
4589
4590                        res->sata_port = sata_port;
4591                        ap->private_data = sata_port;
4592                        starget->hostdata = sata_port;
4593                } else {
4594                        kfree(sata_port);
4595                        return -ENOMEM;
4596                }
4597        }
4598        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4599
4600        return 0;
4601}
4602
4603/**
4604 * ipr_target_destroy - Destroy a SCSI target
4605 * @starget:    scsi target struct
4606 *
4607 * If the device was a SATA device, this function frees the libata
4608 * ATA port, else it does nothing.
4609 *
4610 **/
4611static void ipr_target_destroy(struct scsi_target *starget)
4612{
4613        struct ipr_sata_port *sata_port = starget->hostdata;
4614        struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4615        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4616
4617        if (ioa_cfg->sis64) {
4618                if (!ipr_find_starget(starget)) {
4619                        if (starget->channel == IPR_ARRAY_VIRTUAL_BUS)
4620                                clear_bit(starget->id, ioa_cfg->array_ids);
4621                        else if (starget->channel == IPR_VSET_VIRTUAL_BUS)
4622                                clear_bit(starget->id, ioa_cfg->vset_ids);
4623                        else if (starget->channel == 0)
4624                                clear_bit(starget->id, ioa_cfg->target_ids);
4625                }
4626        }
4627
4628        if (sata_port) {
4629                starget->hostdata = NULL;
4630                ata_sas_port_destroy(sata_port->ap);
4631                kfree(sata_port);
4632        }
4633}
4634
4635/**
4636 * ipr_find_sdev - Find device based on bus/target/lun.
4637 * @sdev:       scsi device struct
4638 *
4639 * Return value:
4640 *      resource entry pointer if found / NULL if not found
4641 **/
4642static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
4643{
4644        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4645        struct ipr_resource_entry *res;
4646
4647        list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4648                if ((res->bus == sdev->channel) &&
4649                    (res->target == sdev->id) &&
4650                    (res->lun == sdev->lun))
4651                        return res;
4652        }
4653
4654        return NULL;
4655}
4656
4657/**
4658 * ipr_slave_destroy - Unconfigure a SCSI device
4659 * @sdev:       scsi device struct
4660 *
4661 * Return value:
4662 *      nothing
4663 **/
4664static void ipr_slave_destroy(struct scsi_device *sdev)
4665{
4666        struct ipr_resource_entry *res;
4667        struct ipr_ioa_cfg *ioa_cfg;
4668        unsigned long lock_flags = 0;
4669
4670        ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4671
4672        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4673        res = (struct ipr_resource_entry *) sdev->hostdata;
4674        if (res) {
4675                if (res->sata_port)
4676                        res->sata_port->ap->link.device[0].class = ATA_DEV_NONE;
4677                sdev->hostdata = NULL;
4678                res->sdev = NULL;
4679                res->sata_port = NULL;
4680        }
4681        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4682}
4683
4684/**
4685 * ipr_slave_configure - Configure a SCSI device
4686 * @sdev:       scsi device struct
4687 *
4688 * This function configures the specified scsi device.
4689 *
4690 * Return value:
4691 *      0 on success
4692 **/
4693static int ipr_slave_configure(struct scsi_device *sdev)
4694{
4695        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4696        struct ipr_resource_entry *res;
4697        struct ata_port *ap = NULL;
4698        unsigned long lock_flags = 0;
4699        char buffer[IPR_MAX_RES_PATH_LENGTH];
4700
4701        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4702        res = sdev->hostdata;
4703        if (res) {
4704                if (ipr_is_af_dasd_device(res))
4705                        sdev->type = TYPE_RAID;
4706                if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
4707                        sdev->scsi_level = 4;
4708                        sdev->no_uld_attach = 1;
4709                }
4710                if (ipr_is_vset_device(res)) {
4711                        blk_queue_rq_timeout(sdev->request_queue,
4712                                             IPR_VSET_RW_TIMEOUT);
4713                        blk_queue_max_hw_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
4714                }
4715                if (ipr_is_gata(res) && res->sata_port)
4716                        ap = res->sata_port->ap;
4717                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4718
4719                if (ap) {
4720                        scsi_adjust_queue_depth(sdev, 0, IPR_MAX_CMD_PER_ATA_LUN);
4721                        ata_sas_slave_configure(sdev, ap);
4722                } else
4723                        scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
4724                if (ioa_cfg->sis64)
4725                        sdev_printk(KERN_INFO, sdev, "Resource path: %s\n",
4726                                    ipr_format_res_path(ioa_cfg,
4727                                res->res_path, buffer, sizeof(buffer)));
4728                return 0;
4729        }
4730        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4731        return 0;
4732}
4733
4734/**
4735 * ipr_ata_slave_alloc - Prepare for commands to a SATA device
4736 * @sdev:       scsi device struct
4737 *
4738 * This function initializes an ATA port so that future commands
4739 * sent through queuecommand will work.
4740 *
4741 * Return value:
4742 *      0 on success
4743 **/
4744static int ipr_ata_slave_alloc(struct scsi_device *sdev)
4745{
4746        struct ipr_sata_port *sata_port = NULL;
4747        int rc = -ENXIO;
4748
4749        ENTER;
4750        if (sdev->sdev_target)
4751                sata_port = sdev->sdev_target->hostdata;
4752        if (sata_port) {
4753                rc = ata_sas_port_init(sata_port->ap);
4754                if (rc == 0)
4755                        rc = ata_sas_sync_probe(sata_port->ap);
4756        }
4757
4758        if (rc)
4759                ipr_slave_destroy(sdev);
4760
4761        LEAVE;
4762        return rc;
4763}
4764
4765/**
4766 * ipr_slave_alloc - Prepare for commands to a device.
4767 * @sdev:       scsi device struct
4768 *
4769 * This function saves a pointer to the resource entry
4770 * in the scsi device struct if the device exists. We
4771 * can then use this pointer in ipr_queuecommand when
4772 * handling new commands.
4773 *
4774 * Return value:
4775 *      0 on success / -ENXIO if device does not exist
4776 **/
4777static int ipr_slave_alloc(struct scsi_device *sdev)
4778{
4779        struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4780        struct ipr_resource_entry *res;
4781        unsigned long lock_flags;
4782        int rc = -ENXIO;
4783
4784        sdev->hostdata = NULL;
4785
4786        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4787
4788        res = ipr_find_sdev(sdev);
4789        if (res) {
4790                res->sdev = sdev;
4791                res->add_to_ml = 0;
4792                res->in_erp = 0;
4793                sdev->hostdata = res;
4794                if (!ipr_is_naca_model(res))
4795                        res->needs_sync_complete = 1;
4796                rc = 0;
4797                if (ipr_is_gata(res)) {
4798                        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4799                        return ipr_ata_slave_alloc(sdev);
4800                }
4801        }
4802
4803        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4804
4805        return rc;
4806}
4807
4808static int ipr_eh_host_reset(struct scsi_cmnd *cmd)
4809{
4810        struct ipr_ioa_cfg *ioa_cfg;
4811        unsigned long lock_flags = 0;
4812        int rc = SUCCESS;
4813
4814        ENTER;
4815        ioa_cfg = (struct ipr_ioa_cfg *) cmd->device->host->hostdata;
4816        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4817
4818        if (!ioa_cfg->in_reset_reload && !ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
4819                ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
4820                dev_err(&ioa_cfg->pdev->dev,
4821                        "Adapter being reset as a result of error recovery.\n");
4822
4823                if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4824                        ioa_cfg->sdt_state = GET_DUMP;
4825        }
4826
4827        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4828        wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4829        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4830
4831        /* If we got hit with a host reset while we were already resetting
4832         the adapter for some reason, and the reset failed. */
4833        if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
4834                ipr_trace;
4835                rc = FAILED;
4836        }
4837
4838        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4839        LEAVE;
4840        return rc;
4841}
4842
4843/**
4844 * ipr_device_reset - Reset the device
4845 * @ioa_cfg:    ioa config struct
4846 * @res:                resource entry struct
4847 *
4848 * This function issues a device reset to the affected device.
4849 * If the device is a SCSI device, a LUN reset will be sent
4850 * to the device first. If that does not work, a target reset
4851 * will be sent. If the device is a SATA device, a PHY reset will
4852 * be sent.
4853 *
4854 * Return value:
4855 *      0 on success / non-zero on failure
4856 **/
4857static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
4858                            struct ipr_resource_entry *res)
4859{
4860        struct ipr_cmnd *ipr_cmd;
4861        struct ipr_ioarcb *ioarcb;
4862        struct ipr_cmd_pkt *cmd_pkt;
4863        struct ipr_ioarcb_ata_regs *regs;
4864        u32 ioasc;
4865
4866        ENTER;
4867        ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4868        ioarcb = &ipr_cmd->ioarcb;
4869        cmd_pkt = &ioarcb->cmd_pkt;
4870
4871        if (ipr_cmd->ioa_cfg->sis64) {
4872                regs = &ipr_cmd->i.ata_ioadl.regs;
4873                ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
4874        } else
4875                regs = &ioarcb->u.add_data.u.regs;
4876
4877        ioarcb->res_handle = res->res_handle;
4878        cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4879        cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
4880        if (ipr_is_gata(res)) {
4881                cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
4882                ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(regs->flags));
4883                regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
4884        }
4885
4886        ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
4887        ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
4888        list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
4889        if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET) {
4890                if (ipr_cmd->ioa_cfg->sis64)
4891                        memcpy(&res->sata_port->ioasa, &ipr_cmd->s.ioasa64.u.gata,
4892                               sizeof(struct ipr_ioasa_gata));
4893                else
4894                        memcpy(&res->sata_port->ioasa, &ipr_cmd->s.ioasa.u.gata,
4895                               sizeof(struct ipr_ioasa_gata));
4896        }
4897
4898        LEAVE;
4899        return IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0;
4900}
4901
4902/**
4903 * ipr_sata_reset - Reset the SATA port
4904 * @link:       SATA link to reset
4905 * @classes:    class of the attached device
4906 *
4907 * This function issues a SATA phy reset to the affected ATA link.
4908 *
4909 * Return value:
4910 *      0 on success / non-zero on failure
4911 **/
4912static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
4913                                unsigned long deadline)
4914{
4915        struct ipr_sata_port *sata_port = link->ap->private_data;
4916        struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
4917        struct ipr_resource_entry *res;
4918        unsigned long lock_flags = 0;
4919        int rc = -ENXIO;
4920
4921        ENTER;
4922        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4923        while (ioa_cfg->in_reset_reload) {
4924                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4925                wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4926                spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4927        }
4928
4929        res = sata_port->res;
4930        if (res) {
4931                rc = ipr_device_reset(ioa_cfg, res);
4932                *classes = res->ata_class;
4933        }
4934
4935        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4936        LEAVE;
4937        return rc;
4938}
4939
4940/**
4941 * ipr_eh_dev_reset - Reset the device
4942 * @scsi_cmd:   scsi command struct
4943 *
4944 * This function issues a device reset to the affected device.
4945 * A LUN reset will be sent to the device first. If that does
4946 * not work, a target reset will be sent.
4947 *
4948 * Return value:
4949 *      SUCCESS / FAILED
4950 **/
4951static int __ipr_eh_dev_reset(struct scsi_cmnd *scsi_cmd)
4952{
4953        struct ipr_cmnd *ipr_cmd;
4954        struct ipr_ioa_cfg *ioa_cfg;
4955        struct ipr_resource_entry *res;
4956        struct ata_port *ap;
4957        int rc = 0;
4958        struct ipr_hrr_queue *hrrq;
4959
4960        ENTER;
4961        ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
4962        res = scsi_cmd->device->hostdata;
4963
4964        if (!res)
4965                return FAILED;
4966
4967        /*
4968         * If we are currently going through reset/reload, return failed. This will force the
4969         * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
4970         * reset to complete
4971         */
4972        if (ioa_cfg->in_reset_reload)
4973                return FAILED;
4974        if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
4975                return FAILED;
4976
4977        for_each_hrrq(hrrq, ioa_cfg) {
4978                spin_lock(&hrrq->_lock);
4979                list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
4980                        if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
4981                                if (ipr_cmd->scsi_cmd)
4982                                        ipr_cmd->done = ipr_scsi_eh_done;
4983                                if (ipr_cmd->qc)
4984                                        ipr_cmd->done = ipr_sata_eh_done;
4985                                if (ipr_cmd->qc &&
4986                                    !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
4987                                        ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
4988                                        ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
4989                                }
4990                        }
4991                }
4992                spin_unlock(&hrrq->_lock);
4993        }
4994        res->resetting_device = 1;
4995        scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
4996
4997        if (ipr_is_gata(res) && res->sata_port) {
4998                ap = res->sata_port->ap;
4999                spin_unlock_irq(scsi_cmd->device->host->host_lock);
5000                ata_std_error_handler(ap);
5001                spin_lock_irq(scsi_cmd->device->host->host_lock);
5002
5003                for_each_hrrq(hrrq, ioa_cfg) {
5004                        spin_lock(&hrrq->_lock);
5005                        list_for_each_entry(ipr_cmd,
5006                                            &hrrq->hrrq_pending_q, queue) {
5007                                if (ipr_cmd->ioarcb.res_handle ==
5008                                    res->res_handle) {
5009                                        rc = -EIO;
5010                                        break;
5011                                }
5012                        }
5013                        spin_unlock(&hrrq->_lock);
5014                }
5015        } else
5016                rc = ipr_device_reset(ioa_cfg, res);
5017        res->resetting_device = 0;
5018
5019        LEAVE;
5020        return rc ? FAILED : SUCCESS;
5021}
5022
5023static int ipr_eh_dev_reset(struct scsi_cmnd *cmd)
5024{
5025        int rc;
5026
5027        spin_lock_irq(cmd->device->host->host_lock);
5028        rc = __ipr_eh_dev_reset(cmd);
5029        spin_unlock_irq(cmd->device->host->host_lock);
5030
5031        return rc;
5032}
5033
5034/**
5035 * ipr_bus_reset_done - Op done function for bus reset.
5036 * @ipr_cmd:    ipr command struct
5037 *
5038 * This function is the op done function for a bus reset
5039 *
5040 * Return value:
5041 *      none
5042 **/
5043static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
5044{
5045        struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5046        struct ipr_resource_entry *res;
5047
5048        ENTER;
5049        if (!ioa_cfg->sis64)
5050                list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
5051                        if (res->res_handle == ipr_cmd->ioarcb.res_handle) {
5052                                scsi_report_bus_reset(ioa_cfg->host, res->bus);
5053                                break;
5054                        }
5055                }
5056
5057        /*
5058         * If abort has not completed, indicate the reset has, else call the
5059         * abort's done function to wake the sleeping eh thread
5060         */
5061        if (ipr_cmd->sibling->sibling)
5062                ipr_cmd->sibling->sibling = NULL;
5063        else
5064                ipr_cmd->sibling->done(ipr_cmd->sibling);
5065
5066        list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
5067        LEAVE;
5068}
5069
5070/**
5071 * ipr_abort_timeout - An abort task has timed out
5072 * @ipr_cmd:    ipr command struct
5073 *
5074 * This function handles when an abort task times out. If this
5075 * happens we issue a bus reset since we have resources tied
5076 * up that must be freed before returning to the midlayer.
5077 *
5078 * Return value:
5079 *      none
5080 **/
5081static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
5082{
5083        struct ipr_cmnd *reset_cmd;
5084        struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5085        struct ipr_cmd_pkt *cmd_pkt;
5086        unsigned long lock_flags = 0;
5087
5088        ENTER;
5089        spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5090        if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
5091                spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5092                return;
5093        }
5094
5095        sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
5096        reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5097        ipr_cmd->sibling = reset_cmd;
5098        reset_cmd->sibling = ipr_cmd;
5099        reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
5100        cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
5101        cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5102        cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
5103        cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
5104
5105        ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
5106        spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5107        LEAVE;
5108}
5109
5110/**
5111 * ipr_cancel_op - Cancel specified op
5112 * @scsi_cmd:   scsi command struct
5113 *
5114 * This function cancels specified op.
5115 *
5116 * Return value:
5117 *      SUCCESS / FAILED
5118 **/
5119static int ipr_cancel_op(struct scsi_cmnd *scsi_cmd)
5120{
5121        struct ipr_cmnd *ipr_cmd;
5122        struct ipr_ioa_cfg *ioa_cfg;
5123        struct ipr_resource_entry *res;
5124        struct ipr_cmd_pkt *cmd_pkt;
5125        u32 ioasc, int_reg;
5126        int op_found = 0;
5127        struct ipr_hrr_queue *hrrq;
5128
5129        ENTER;
5130        ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
5131        res = scsi_cmd->device->hostdata;
5132
5133        /* If we are currently going through reset/reload, return failed.
5134         * This will force the mid-layer to call ipr_eh_host_reset,
5135         * which will then go to sleep and wait for the reset to complete
5136         */
5137        if (ioa_cfg->in_reset_reload ||
5138            ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
5139                return FAILED;
5140        if (!res)
5141                return FAILED;
5142
5143        /*
5144         * If we are aborting a timed out op, chances are that the timeout was caused
5145         * by a still not detected EEH error. In such cases, reading a register will
5146         * trigger the EEH recovery infrastructure.
5147         */
5148        int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5149
5150        if (!ipr_is_gscsi(res))
5151                return FAILED;
5152
5153        for_each_hrrq(hrrq, ioa_cfg) {
5154                spin_lock(&hrrq->_lock);
5155                list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
5156                        if (ipr_cmd->scsi_cmd == scsi_cmd) {
5157                                ipr_cmd->done = ipr_scsi_eh_done;
5158                                op_found = 1;
5159                                break;
5160                        }
5161                }
5162                spin_unlock(&hrrq->_lock);
5163        }
5164
5165        if (!op_found)
5166                return SUCCESS;
5167
5168        ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5169        ipr_cmd->ioarcb.res_handle = res->res_handle;
5170        cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5171        cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5172        cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
5173        ipr_cmd->u.sdev = scsi_cmd->device;
5174
5175        scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
5176                    scsi_cmd->cmnd[0]);
5177        ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
5178        ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5179
5180        /*
5181         * If the abort task timed out and we sent a bus reset, we will get
5182         * one the following responses to the abort
5183         */
5184        if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
5185                ioasc = 0;
5186                ipr_trace;
5187        }
5188
5189        list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
5190        if (!ipr_is_naca_model(res))
5191                res->needs_sync_complete = 1;
5192
5193        LEAVE;
5194        return IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
5195}
5196
5197/**
5198 * ipr_eh_abort - Abort a single op
5199 * @scsi_cmd:   scsi command struct
5200 *
5201 * Return value:
5202 *      SUCCESS / FAILED
5203 **/
5204static int ipr_eh_abort(struct scsi_cmnd *scsi_cmd)
5205{
5206        unsigned long flags;
5207        int rc;
5208
5209        ENTER;
5210
5211        spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
5212        rc = ipr_cancel_op(scsi_cmd);
5213        spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
5214
5215        LEAVE;
5216        return rc;
5217}
5218
5219/**
5220 * ipr_handle_other_interrupt - Handle "other" interrupts
5221 * @ioa_cfg:    ioa config struct
5222 * @int_reg:    interrupt register
5223 *
5224 * Return value:
5225 *      IRQ_NONE / IRQ_HANDLED
5226 **/
5227static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
5228                                              u32 int_reg)
5229{
5230        irqreturn_t rc = IRQ_HANDLED;
5231        u32 int_mask_reg;
5232
5233        int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
5234        int_reg &= ~int_mask_reg;
5235
5236        /* If an interrupt on the adapter did not occur, ignore it.
5237         * Or in the case of SIS 64, check for a stage change interrupt.
5238         */
5239        if ((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0) {
5240                if (ioa_cfg->sis64) {
5241                        int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
5242                        int_reg = readl(