]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/scsi/ipr.c
scsi: never drop to untagged mode during queue ramp down
[mirror_ubuntu-zesty-kernel.git] / drivers / scsi / ipr.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
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>
5a0e3ad6 62#include <linux/slab.h>
4d4dd706 63#include <linux/vmalloc.h>
1da177e4
LT
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>
35a39691 75#include <linux/libata.h>
0ce3a7e5 76#include <linux/hdreg.h>
f72919ec 77#include <linux/reboot.h>
3e7ebdfa 78#include <linux/stringify.h>
1da177e4
LT
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>
1da177e4
LT
87#include "ipr.h"
88
89/*
90 * Global Data
91 */
b7d68ca3 92static LIST_HEAD(ipr_ioa_head);
1da177e4
LT
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;
5469cb5b 97static unsigned int ipr_transop_timeout = 0;
d3c74871 98static unsigned int ipr_debug = 0;
3e7ebdfa 99static unsigned int ipr_max_devs = IPR_DEFAULT_SIS64_DEVS;
ac09c349 100static unsigned int ipr_dual_ioa_raid = 1;
05a6538a 101static unsigned int ipr_number_of_msix = 2;
1da177e4
LT
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[] = {
60e7486b 106 { /* Gemstone, Citrine, Obsidian, and Obsidian-E */
1da177e4 107 .mailbox = 0x0042C,
89aad428 108 .max_cmds = 100,
1da177e4 109 .cache_line_size = 0x20,
7dd21308 110 .clear_isr = 1,
b53d124a 111 .iopoll_weight = 0,
1da177e4
LT
112 {
113 .set_interrupt_mask_reg = 0x0022C,
114 .clr_interrupt_mask_reg = 0x00230,
214777ba 115 .clr_interrupt_mask_reg32 = 0x00230,
1da177e4 116 .sense_interrupt_mask_reg = 0x0022C,
214777ba 117 .sense_interrupt_mask_reg32 = 0x0022C,
1da177e4 118 .clr_interrupt_reg = 0x00228,
214777ba 119 .clr_interrupt_reg32 = 0x00228,
1da177e4 120 .sense_interrupt_reg = 0x00224,
214777ba 121 .sense_interrupt_reg32 = 0x00224,
1da177e4
LT
122 .ioarrin_reg = 0x00404,
123 .sense_uproc_interrupt_reg = 0x00214,
214777ba 124 .sense_uproc_interrupt_reg32 = 0x00214,
1da177e4 125 .set_uproc_interrupt_reg = 0x00214,
214777ba
WB
126 .set_uproc_interrupt_reg32 = 0x00214,
127 .clr_uproc_interrupt_reg = 0x00218,
128 .clr_uproc_interrupt_reg32 = 0x00218
1da177e4
LT
129 }
130 },
131 { /* Snipe and Scamp */
132 .mailbox = 0x0052C,
89aad428 133 .max_cmds = 100,
1da177e4 134 .cache_line_size = 0x20,
7dd21308 135 .clear_isr = 1,
b53d124a 136 .iopoll_weight = 0,
1da177e4
LT
137 {
138 .set_interrupt_mask_reg = 0x00288,
139 .clr_interrupt_mask_reg = 0x0028C,
214777ba 140 .clr_interrupt_mask_reg32 = 0x0028C,
1da177e4 141 .sense_interrupt_mask_reg = 0x00288,
214777ba 142 .sense_interrupt_mask_reg32 = 0x00288,
1da177e4 143 .clr_interrupt_reg = 0x00284,
214777ba 144 .clr_interrupt_reg32 = 0x00284,
1da177e4 145 .sense_interrupt_reg = 0x00280,
214777ba 146 .sense_interrupt_reg32 = 0x00280,
1da177e4
LT
147 .ioarrin_reg = 0x00504,
148 .sense_uproc_interrupt_reg = 0x00290,
214777ba 149 .sense_uproc_interrupt_reg32 = 0x00290,
1da177e4 150 .set_uproc_interrupt_reg = 0x00290,
214777ba
WB
151 .set_uproc_interrupt_reg32 = 0x00290,
152 .clr_uproc_interrupt_reg = 0x00294,
153 .clr_uproc_interrupt_reg32 = 0x00294
1da177e4
LT
154 }
155 },
a74c1639 156 { /* CRoC */
110def85 157 .mailbox = 0x00044,
89aad428 158 .max_cmds = 1000,
a74c1639 159 .cache_line_size = 0x20,
7dd21308 160 .clear_isr = 0,
b53d124a 161 .iopoll_weight = 64,
a74c1639
WB
162 {
163 .set_interrupt_mask_reg = 0x00010,
164 .clr_interrupt_mask_reg = 0x00018,
214777ba 165 .clr_interrupt_mask_reg32 = 0x0001C,
a74c1639 166 .sense_interrupt_mask_reg = 0x00010,
214777ba 167 .sense_interrupt_mask_reg32 = 0x00014,
a74c1639 168 .clr_interrupt_reg = 0x00008,
214777ba 169 .clr_interrupt_reg32 = 0x0000C,
a74c1639 170 .sense_interrupt_reg = 0x00000,
214777ba 171 .sense_interrupt_reg32 = 0x00004,
a74c1639
WB
172 .ioarrin_reg = 0x00070,
173 .sense_uproc_interrupt_reg = 0x00020,
214777ba 174 .sense_uproc_interrupt_reg32 = 0x00024,
a74c1639 175 .set_uproc_interrupt_reg = 0x00020,
214777ba 176 .set_uproc_interrupt_reg32 = 0x00024,
dcbad00e 177 .clr_uproc_interrupt_reg = 0x00028,
214777ba
WB
178 .clr_uproc_interrupt_reg32 = 0x0002C,
179 .init_feedback_reg = 0x0005C,
dcbad00e 180 .dump_addr_reg = 0x00064,
8701f185
WB
181 .dump_data_reg = 0x00068,
182 .endian_swap_reg = 0x00084
a74c1639
WB
183 }
184 },
1da177e4
LT
185};
186
187static const struct ipr_chip_t ipr_chip[] = {
cb237ef7
WB
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] },
cd9b3d04 196 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE, IPR_USE_MSI, IPR_SIS64, IPR_MMIO, &ipr_chip_cfg[2] }
1da177e4
LT
197};
198
203fa3fe 199static int ipr_max_bus_speeds[] = {
1da177e4
LT
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");
2cf22be0 211module_param_named(fastfail, ipr_fastfail, int, S_IRUGO | S_IWUSR);
1da177e4
LT
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)");
2cf22be0 215module_param_named(debug, ipr_debug, int, S_IRUGO | S_IWUSR);
d3c74871 216MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
ac09c349
BK
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)");
3e7ebdfa
WB
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) "]");
05a6538a 222module_param_named(number_of_msix, ipr_number_of_msix, int, 0);
6634ff7c 223MODULE_PARM_DESC(number_of_msix, "Specify the number of MSIX interrupts to use on capable adapters (1 - 16). (default:2)");
1da177e4
LT
224MODULE_LICENSE("GPL");
225MODULE_VERSION(IPR_DRIVER_VERSION);
226
1da177e4
LT
227/* A constant array of IOASCs/URCs/Error Messages */
228static const
229struct ipr_error_table_t ipr_error_table[] = {
933916f3 230 {0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
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"},
933916f3 238 {0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 239 "FFFE: Soft device bus error recovered by the IOA"},
933916f3 240 {0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 241 "4101: Soft device bus fabric error"},
5aa3a333
WB
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"},
933916f3 256 {0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 257 "FFF9: Device sector reassign successful"},
933916f3 258 {0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 259 "FFF7: Media error recovered by device rewrite procedures"},
933916f3 260 {0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 261 "7001: IOA sector reassignment successful"},
933916f3 262 {0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 263 "FFF9: Soft media error. Sector reassignment recommended"},
933916f3 264 {0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 265 "FFF7: Media error recovered by IOA rewrite procedures"},
933916f3 266 {0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 267 "FF3D: Soft PCI bus error recovered by the IOA"},
933916f3 268 {0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 269 "FFF6: Device hardware error recovered by the IOA"},
933916f3 270 {0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 271 "FFF6: Device hardware error recovered by the device"},
933916f3 272 {0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 273 "FF3D: Soft IOA error recovered by the IOA"},
933916f3 274 {0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 275 "FFFA: Undefined device response recovered by the IOA"},
933916f3 276 {0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 277 "FFF6: Device bus error, message or command phase"},
933916f3 278 {0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
35a39691 279 "FFFE: Task Management Function failed"},
933916f3 280 {0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 281 "FFF6: Failure prediction threshold exceeded"},
933916f3 282 {0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 283 "8009: Impending cache battery pack failure"},
ed7bd661 284 {0x02040100, 0, 0,
285 "Logical Unit in process of becoming ready"},
286 {0x02040200, 0, 0,
287 "Initializing command required"},
1da177e4
LT
288 {0x02040400, 0, 0,
289 "34FF: Disk device format in progress"},
ed7bd661 290 {0x02040C00, 0, 0,
291 "Logical unit not accessible, target port in unavailable state"},
65f56475
BK
292 {0x02048000, 0, IPR_DEFAULT_LOG_LEVEL,
293 "9070: IOA requested reset"},
1da177e4
LT
294 {0x023F0000, 0, 0,
295 "Synchronization required"},
ed7bd661 296 {0x02408500, 0, 0,
297 "IOA microcode download required"},
298 {0x02408600, 0, 0,
299 "Device bus connection is prohibited by host"},
1da177e4
LT
300 {0x024E0000, 0, 0,
301 "No ready, IOA shutdown"},
302 {0x025A0000, 0, 0,
303 "Not ready, IOA has been shutdown"},
933916f3 304 {0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
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"},
933916f3 310 {0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 311 "FFF3: Disk media format bad"},
933916f3 312 {0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 313 "3002: Addressed device failed to respond to selection"},
933916f3 314 {0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 315 "3100: Device bus error"},
933916f3 316 {0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
317 "3109: IOA timed out a device command"},
318 {0x04088000, 0, 0,
319 "3120: SCSI bus is not operational"},
933916f3 320 {0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 321 "4100: Hard device bus fabric error"},
5aa3a333
WB
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"},
933916f3 336 {0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 337 "9000: IOA reserved area data check"},
933916f3 338 {0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 339 "9001: IOA reserved area invalid data pattern"},
933916f3 340 {0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 341 "9002: IOA reserved area LRC error"},
5aa3a333
WB
342 {0x04118300, 1, IPR_DEFAULT_LOG_LEVEL,
343 "Hardware Error, IOA metadata access error"},
933916f3 344 {0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 345 "102E: Out of alternate sectors for disk storage"},
933916f3 346 {0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 347 "FFF4: Data transfer underlength error"},
933916f3 348 {0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 349 "FFF4: Data transfer overlength error"},
933916f3 350 {0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 351 "3400: Logical unit failure"},
933916f3 352 {0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 353 "FFF4: Device microcode is corrupt"},
933916f3 354 {0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
355 "8150: PCI bus error"},
356 {0x04430000, 1, 0,
357 "Unsupported device bus message received"},
933916f3 358 {0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 359 "FFF4: Disk device problem"},
933916f3 360 {0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 361 "8150: Permanent IOA failure"},
933916f3 362 {0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 363 "3010: Disk device returned wrong response to IOA"},
933916f3 364 {0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
365 "8151: IOA microcode error"},
366 {0x04448500, 0, 0,
367 "Device bus status error"},
933916f3 368 {0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 369 "8157: IOA error requiring IOA reset to recover"},
35a39691
BK
370 {0x04448700, 0, 0,
371 "ATA device status error"},
1da177e4
LT
372 {0x04490000, 0, 0,
373 "Message reject received from the device"},
933916f3 374 {0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 375 "8008: A permanent cache battery pack failure occurred"},
933916f3 376 {0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 377 "9090: Disk unit has been modified after the last known status"},
933916f3 378 {0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 379 "9081: IOA detected device error"},
933916f3 380 {0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 381 "9082: IOA detected device error"},
933916f3 382 {0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 383 "3110: Device bus error, message or command phase"},
933916f3 384 {0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
35a39691 385 "3110: SAS Command / Task Management Function failed"},
933916f3 386 {0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 387 "9091: Incorrect hardware configuration change has been detected"},
933916f3 388 {0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
b0df54bb 389 "9073: Invalid multi-adapter configuration"},
933916f3 390 {0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 391 "4010: Incorrect connection between cascaded expanders"},
933916f3 392 {0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 393 "4020: Connections exceed IOA design limits"},
933916f3 394 {0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 395 "4030: Incorrect multipath connection"},
933916f3 396 {0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 397 "4110: Unsupported enclosure function"},
ed7bd661 398 {0x04679800, 0, IPR_DEFAULT_LOG_LEVEL,
399 "4120: SAS cable VPD cannot be read"},
933916f3 400 {0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
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"},
b0df54bb
BK
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"},
5aa3a333
WB
410 {0x05258200, 0, 0,
411 "Illegal request, command not allowed to a non-optimized resource"},
1da177e4
LT
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"},
b0df54bb
BK
420 {0x052C8000, 1, 0,
421 "Illegal request, dual adapter support not enabled"},
ed7bd661 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"},
933916f3 426 {0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 427 "9031: Array protection temporarily suspended, protection resuming"},
933916f3 428 {0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 429 "9040: Array protection temporarily suspended, protection resuming"},
ed7bd661 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"},
933916f3 434 {0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 435 "3140: Device bus not ready to ready transition"},
933916f3 436 {0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
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"},
933916f3 442 {0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 443 "FFFB: SCSI bus was reset by another initiator"},
933916f3 444 {0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 445 "3029: A device replacement has occurred"},
ed7bd661 446 {0x063F8300, 0, IPR_DEFAULT_LOG_LEVEL,
447 "4102: Device bus fabric performance degradation"},
933916f3 448 {0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 449 "9051: IOA cache data exists for a missing or failed device"},
933916f3 450 {0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
b0df54bb 451 "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
933916f3 452 {0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 453 "9025: Disk unit is not supported at its physical location"},
933916f3 454 {0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 455 "3020: IOA detected a SCSI bus configuration error"},
933916f3 456 {0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 457 "3150: SCSI bus configuration error"},
933916f3 458 {0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
b0df54bb 459 "9074: Asymmetric advanced function disk configuration"},
933916f3 460 {0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 461 "4040: Incomplete multipath connection between IOA and enclosure"},
933916f3 462 {0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 463 "4041: Incomplete multipath connection between enclosure and device"},
933916f3 464 {0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 465 "9075: Incomplete multipath connection between IOA and remote IOA"},
933916f3 466 {0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 467 "9076: Configuration error, missing remote IOA"},
933916f3 468 {0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 469 "4050: Enclosure does not support a required multipath function"},
ed7bd661 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"},
b75424fc
WB
478 {0x06690000, 0, IPR_DEFAULT_LOG_LEVEL,
479 "4070: Logically bad block written on device"},
933916f3 480 {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 481 "9041: Array protection temporarily suspended"},
933916f3 482 {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 483 "9042: Corrupt array parity detected on specified device"},
933916f3 484 {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 485 "9030: Array no longer protected due to missing or failed disk unit"},
933916f3 486 {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
b0df54bb 487 "9071: Link operational transition"},
933916f3 488 {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
b0df54bb 489 "9072: Link not operational transition"},
933916f3 490 {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 491 "9032: Array exposed but still protected"},
e435340c
BK
492 {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
493 "70DD: Device forced failed by disrupt device command"},
933916f3 494 {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 495 "4061: Multipath redundancy level got better"},
933916f3 496 {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 497 "4060: Multipath redundancy level got worse"},
1da177e4
LT
498 {0x07270000, 0, 0,
499 "Failure due to other device"},
933916f3 500 {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 501 "9008: IOA does not support functions expected by devices"},
933916f3 502 {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 503 "9010: Cache data associated with attached devices cannot be found"},
933916f3 504 {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 505 "9011: Cache data belongs to devices other than those attached"},
933916f3 506 {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 507 "9020: Array missing 2 or more devices with only 1 device present"},
933916f3 508 {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 509 "9021: Array missing 2 or more devices with 2 or more devices present"},
933916f3 510 {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 511 "9022: Exposed array is missing a required device"},
933916f3 512 {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 513 "9023: Array member(s) not at required physical locations"},
933916f3 514 {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 515 "9024: Array not functional due to present hardware configuration"},
933916f3 516 {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 517 "9026: Array not functional due to present hardware configuration"},
933916f3 518 {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 519 "9027: Array is missing a device and parity is out of sync"},
933916f3 520 {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 521 "9028: Maximum number of arrays already exist"},
933916f3 522 {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 523 "9050: Required cache data cannot be located for a disk unit"},
933916f3 524 {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 525 "9052: Cache data exists for a device that has been modified"},
933916f3 526 {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 527 "9054: IOA resources not available due to previous problems"},
933916f3 528 {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 529 "9092: Disk unit requires initialization before use"},
933916f3 530 {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 531 "9029: Incorrect hardware configuration change has been detected"},
933916f3 532 {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 533 "9060: One or more disk pairs are missing from an array"},
933916f3 534 {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 535 "9061: One or more disks are missing from an array"},
933916f3 536 {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 537 "9062: One or more disks are missing from an array"},
933916f3 538 {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 539 "9063: Maximum number of functional arrays has been exceeded"},
ed7bd661 540 {0x07279A00, 0, 0,
541 "Data protect, other volume set problem"},
1da177e4
LT
542 {0x0B260000, 0, 0,
543 "Aborted command, invalid descriptor"},
ed7bd661 544 {0x0B3F9000, 0, 0,
545 "Target operating conditions have changed, dual adapter takeover"},
546 {0x0B530200, 0, 0,
547 "Aborted command, medium removal prevented"},
1da177e4 548 {0x0B5A0000, 0, 0,
ed7bd661 549 "Command terminated by host"},
550 {0x0B5B8000, 0, 0,
551 "Aborted command, command terminated by host"}
1da177e4
LT
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
56d6aa33 596 trace_entry = &ioa_cfg->trace[atomic_add_return
597 (1, &ioa_cfg->trace_index)%IPR_NUM_TRACE_ENTRIES];
1da177e4
LT
598 trace_entry->time = jiffies;
599 trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
600 trace_entry->type = type;
a32c055f
WB
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;
35a39691 605 trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
1da177e4
LT
606 trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
607 trace_entry->u.add_data = add_data;
56d6aa33 608 wmb();
1da177e4
LT
609}
610#else
203fa3fe 611#define ipr_trc_hook(ipr_cmd, type, add_data) do { } while (0)
1da177e4
LT
612#endif
613
172cd6e1
BK
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
1da177e4
LT
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;
96d21f00
WB
641 struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
642 struct ipr_ioasa64 *ioasa64 = &ipr_cmd->s.ioasa64;
a32c055f 643 dma_addr_t dma_addr = ipr_cmd->dma_addr;
05a6538a 644 int hrrq_id;
1da177e4 645
05a6538a 646 hrrq_id = ioarcb->cmd_pkt.hrrq_id;
1da177e4 647 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
05a6538a 648 ioarcb->cmd_pkt.hrrq_id = hrrq_id;
a32c055f 649 ioarcb->data_transfer_length = 0;
1da177e4 650 ioarcb->read_data_transfer_length = 0;
a32c055f 651 ioarcb->ioadl_len = 0;
1da177e4 652 ioarcb->read_ioadl_len = 0;
a32c055f 653
96d21f00 654 if (ipr_cmd->ioa_cfg->sis64) {
a32c055f
WB
655 ioarcb->u.sis64_addr_data.data_ioadl_addr =
656 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
96d21f00
WB
657 ioasa64->u.gata.status = 0;
658 } else {
a32c055f
WB
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;
96d21f00 662 ioasa->u.gata.status = 0;
a32c055f
WB
663 }
664
96d21f00
WB
665 ioasa->hdr.ioasc = 0;
666 ioasa->hdr.residual_data_len = 0;
1da177e4 667 ipr_cmd->scsi_cmd = NULL;
35a39691 668 ipr_cmd->qc = NULL;
1da177e4
LT
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 **/
172cd6e1
BK
680static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd,
681 void (*fast_done) (struct ipr_cmnd *))
1da177e4
LT
682{
683 ipr_reinit_ipr_cmnd(ipr_cmd);
684 ipr_cmd->u.scratch = 0;
685 ipr_cmd->sibling = NULL;
172cd6e1 686 ipr_cmd->fast_done = fast_done;
1da177e4
LT
687 init_timer(&ipr_cmd->timer);
688}
689
690/**
00bfef2c 691 * __ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
1da177e4
LT
692 * @ioa_cfg: ioa config struct
693 *
694 * Return value:
695 * pointer to ipr command struct
696 **/
697static
05a6538a 698struct ipr_cmnd *__ipr_get_free_ipr_cmnd(struct ipr_hrr_queue *hrrq)
1da177e4 699{
05a6538a 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 }
1da177e4 707
1da177e4
LT
708
709 return ipr_cmd;
710}
711
00bfef2c
BK
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{
05a6538a 722 struct ipr_cmnd *ipr_cmd =
723 __ipr_get_free_ipr_cmnd(&ioa_cfg->hrrq[IPR_INIT_HRRQ]);
172cd6e1 724 ipr_init_ipr_cmnd(ipr_cmd, ipr_lock_and_done);
00bfef2c
BK
725 return ipr_cmd;
726}
727
1da177e4
LT
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;
56d6aa33 743 int i;
1da177e4
LT
744
745 /* Stop new interrupts */
56d6aa33 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();
1da177e4
LT
752
753 /* Set interrupt mask to stop all new interrupts */
214777ba
WB
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);
1da177e4
LT
758
759 /* Clear any pending interrupts */
214777ba
WB
760 if (ioa_cfg->sis64)
761 writel(~0, ioa_cfg->regs.clr_interrupt_reg);
762 writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg32);
1da177e4
LT
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
7dce0e1c
BK
777 if (pcix_cmd_reg == 0)
778 return 0;
1da177e4
LT
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 }
1da177e4
LT
807 }
808
809 return 0;
810}
811
35a39691
BK
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{
35a39691
BK
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;
05a6538a 829 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
35a39691
BK
830 ata_qc_complete(qc);
831}
832
1da177e4
LT
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{
1da177e4
LT
845 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
846
847 scsi_cmd->result |= (DID_ERROR << 16);
848
63015bc9 849 scsi_dma_unmap(ipr_cmd->scsi_cmd);
1da177e4 850 scsi_cmd->scsi_done(scsi_cmd);
05a6538a 851 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
1da177e4
LT
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;
05a6538a 866 struct ipr_hrr_queue *hrrq;
1da177e4
LT
867
868 ENTER;
05a6538a 869 for_each_hrrq(hrrq, ioa_cfg) {
56d6aa33 870 spin_lock(&hrrq->_lock);
05a6538a 871 list_for_each_entry_safe(ipr_cmd,
872 temp, &hrrq->hrrq_pending_q, queue) {
873 list_del(&ipr_cmd->queue);
1da177e4 874
05a6538a 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);
1da177e4 879
05a6538a 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;
1da177e4 884
05a6538a 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 }
56d6aa33 890 spin_unlock(&hrrq->_lock);
1da177e4 891 }
1da177e4
LT
892 LEAVE;
893}
894
a32c055f
WB
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
1da177e4
LT
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{
05a6538a 941 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
1da177e4
LT
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
a32c055f 953 ipr_send_command(ipr_cmd);
1da177e4
LT
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
a32c055f
WB
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
1da177e4
LT
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
05a6538a 1042static int ipr_get_hrrq_index(struct ipr_ioa_cfg *ioa_cfg)
1043{
1044 if (ioa_cfg->hrrq_num == 1)
56d6aa33 1045 return 0;
1046 else
1047 return (atomic_add_return(1, &ioa_cfg->hrrq_index) % (ioa_cfg->hrrq_num - 1)) + 1;
05a6538a 1048}
1049
1da177e4
LT
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
56d6aa33 1069 if (ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds) {
1da177e4 1070 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
05a6538a 1071 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
1da177e4
LT
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
a32c055f
WB
1084 ipr_init_ioadl(ipr_cmd, hostrcb->hostrcb_dma,
1085 sizeof(hostrcb->hcam), IPR_IOADL_FLAGS_READ_LAST);
1da177e4
LT
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
a32c055f 1094 ipr_send_command(ipr_cmd);
1da177e4
LT
1095 } else {
1096 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
1097 }
1098}
1099
3e7ebdfa
WB
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{
203fa3fe 1110 switch (proto) {
3e7ebdfa
WB
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
1da177e4
LT
1125/**
1126 * ipr_init_res_entry - Initialize a resource entry struct.
1127 * @res: resource entry struct
3e7ebdfa 1128 * @cfgtew: config table entry wrapper struct
1da177e4
LT
1129 *
1130 * Return value:
1131 * none
1132 **/
3e7ebdfa
WB
1133static void ipr_init_res_entry(struct ipr_resource_entry *res,
1134 struct ipr_config_table_entry_wrapper *cfgtew)
1da177e4 1135{
3e7ebdfa
WB
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
ee0a90fa 1141 res->needs_sync_complete = 0;
1da177e4
LT
1142 res->in_erp = 0;
1143 res->add_to_ml = 0;
1144 res->del_from_ml = 0;
1145 res->resetting_device = 0;
0b1f8d44 1146 res->reset_occurred = 0;
1da177e4 1147 res->sdev = NULL;
35a39691 1148 res->sata_port = NULL;
3e7ebdfa
WB
1149
1150 if (ioa_cfg->sis64) {
1151 proto = cfgtew->u.cfgte64->proto;
1152 res->res_flags = cfgtew->u.cfgte64->res_flags;
1153 res->qmodel = IPR_QUEUEING_MODEL64(res);
438b0331 1154 res->type = cfgtew->u.cfgte64->res_type;
3e7ebdfa
WB
1155
1156 memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
1157 sizeof(res->res_path));
1158
1159 res->bus = 0;
0cb992ed
WB
1160 memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1161 sizeof(res->dev_lun.scsi_lun));
3e7ebdfa
WB
1162 res->lun = scsilun_to_int(&res->dev_lun);
1163
1164 if (res->type == IPR_RES_TYPE_GENERIC_SCSI) {
1165 list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue) {
1166 if (gscsi_res->dev_id == cfgtew->u.cfgte64->dev_id) {
1167 found = 1;
1168 res->target = gscsi_res->target;
1169 break;
1170 }
1171 }
1172 if (!found) {
1173 res->target = find_first_zero_bit(ioa_cfg->target_ids,
1174 ioa_cfg->max_devs_supported);
1175 set_bit(res->target, ioa_cfg->target_ids);
1176 }
3e7ebdfa
WB
1177 } else if (res->type == IPR_RES_TYPE_IOAFP) {
1178 res->bus = IPR_IOAFP_VIRTUAL_BUS;
1179 res->target = 0;
1180 } else if (res->type == IPR_RES_TYPE_ARRAY) {
1181 res->bus = IPR_ARRAY_VIRTUAL_BUS;
1182 res->target = find_first_zero_bit(ioa_cfg->array_ids,
1183 ioa_cfg->max_devs_supported);
1184 set_bit(res->target, ioa_cfg->array_ids);
1185 } else if (res->type == IPR_RES_TYPE_VOLUME_SET) {
1186 res->bus = IPR_VSET_VIRTUAL_BUS;
1187 res->target = find_first_zero_bit(ioa_cfg->vset_ids,
1188 ioa_cfg->max_devs_supported);
1189 set_bit(res->target, ioa_cfg->vset_ids);
1190 } else {
1191 res->target = find_first_zero_bit(ioa_cfg->target_ids,
1192 ioa_cfg->max_devs_supported);
1193 set_bit(res->target, ioa_cfg->target_ids);
1194 }
1195 } else {
1196 proto = cfgtew->u.cfgte->proto;
1197 res->qmodel = IPR_QUEUEING_MODEL(res);
1198 res->flags = cfgtew->u.cfgte->flags;
1199 if (res->flags & IPR_IS_IOA_RESOURCE)
1200 res->type = IPR_RES_TYPE_IOAFP;
1201 else
1202 res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
1203
1204 res->bus = cfgtew->u.cfgte->res_addr.bus;
1205 res->target = cfgtew->u.cfgte->res_addr.target;
1206 res->lun = cfgtew->u.cfgte->res_addr.lun;
46d74563 1207 res->lun_wwn = get_unaligned_be64(cfgtew->u.cfgte->lun_wwn);
3e7ebdfa
WB
1208 }
1209
1210 ipr_update_ata_class(res, proto);
1211}
1212
1213/**
1214 * ipr_is_same_device - Determine if two devices are the same.
1215 * @res: resource entry struct
1216 * @cfgtew: config table entry wrapper struct
1217 *
1218 * Return value:
1219 * 1 if the devices are the same / 0 otherwise
1220 **/
1221static int ipr_is_same_device(struct ipr_resource_entry *res,
1222 struct ipr_config_table_entry_wrapper *cfgtew)
1223{
1224 if (res->ioa_cfg->sis64) {
1225 if (!memcmp(&res->dev_id, &cfgtew->u.cfgte64->dev_id,
1226 sizeof(cfgtew->u.cfgte64->dev_id)) &&
0cb992ed 1227 !memcmp(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
3e7ebdfa
WB
1228 sizeof(cfgtew->u.cfgte64->lun))) {
1229 return 1;
1230 }
1231 } else {
1232 if (res->bus == cfgtew->u.cfgte->res_addr.bus &&
1233 res->target == cfgtew->u.cfgte->res_addr.target &&
1234 res->lun == cfgtew->u.cfgte->res_addr.lun)
1235 return 1;
1236 }
1237
1238 return 0;
1239}
1240
1241/**
b3b3b407 1242 * __ipr_format_res_path - Format the resource path for printing.
3e7ebdfa
WB
1243 * @res_path: resource path
1244 * @buf: buffer
b3b3b407 1245 * @len: length of buffer provided
3e7ebdfa
WB
1246 *
1247 * Return value:
1248 * pointer to buffer
1249 **/
b3b3b407 1250static char *__ipr_format_res_path(u8 *res_path, char *buffer, int len)
3e7ebdfa
WB
1251{
1252 int i;
5adcbeb3 1253 char *p = buffer;
3e7ebdfa 1254
46d74563 1255 *p = '\0';
5adcbeb3
WB
1256 p += snprintf(p, buffer + len - p, "%02X", res_path[0]);
1257 for (i = 1; res_path[i] != 0xff && ((i * 3) < len); i++)
1258 p += snprintf(p, buffer + len - p, "-%02X", res_path[i]);
3e7ebdfa
WB
1259
1260 return buffer;
1261}
1262
b3b3b407
BK
1263/**
1264 * ipr_format_res_path - Format the resource path for printing.
1265 * @ioa_cfg: ioa config struct
1266 * @res_path: resource path
1267 * @buf: buffer
1268 * @len: length of buffer provided
1269 *
1270 * Return value:
1271 * pointer to buffer
1272 **/
1273static char *ipr_format_res_path(struct ipr_ioa_cfg *ioa_cfg,
1274 u8 *res_path, char *buffer, int len)
1275{
1276 char *p = buffer;
1277
1278 *p = '\0';
1279 p += snprintf(p, buffer + len - p, "%d/", ioa_cfg->host->host_no);
1280 __ipr_format_res_path(res_path, p, len - (buffer - p));
1281 return buffer;
1282}
1283
3e7ebdfa
WB
1284/**
1285 * ipr_update_res_entry - Update the resource entry.
1286 * @res: resource entry struct
1287 * @cfgtew: config table entry wrapper struct
1288 *
1289 * Return value:
1290 * none
1291 **/
1292static void ipr_update_res_entry(struct ipr_resource_entry *res,
1293 struct ipr_config_table_entry_wrapper *cfgtew)
1294{
1295 char buffer[IPR_MAX_RES_PATH_LENGTH];
1296 unsigned int proto;
1297 int new_path = 0;
1298
1299 if (res->ioa_cfg->sis64) {
1300 res->flags = cfgtew->u.cfgte64->flags;
1301 res->res_flags = cfgtew->u.cfgte64->res_flags;
75576bb9 1302 res->type = cfgtew->u.cfgte64->res_type;
3e7ebdfa
WB
1303
1304 memcpy(&res->std_inq_data, &cfgtew->u.cfgte64->std_inq_data,
1305 sizeof(struct ipr_std_inq_data));
1306
1307 res->qmodel = IPR_QUEUEING_MODEL64(res);
1308 proto = cfgtew->u.cfgte64->proto;
1309 res->res_handle = cfgtew->u.cfgte64->res_handle;
1310 res->dev_id = cfgtew->u.cfgte64->dev_id;
1311
1312 memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1313 sizeof(res->dev_lun.scsi_lun));
1314
1315 if (memcmp(res->res_path, &cfgtew->u.cfgte64->res_path,
1316 sizeof(res->res_path))) {
1317 memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
1318 sizeof(res->res_path));
1319 new_path = 1;
1320 }
1321
1322 if (res->sdev && new_path)
1323 sdev_printk(KERN_INFO, res->sdev, "Resource path: %s\n",
b3b3b407
BK
1324 ipr_format_res_path(res->ioa_cfg,
1325 res->res_path, buffer, sizeof(buffer)));
3e7ebdfa
WB
1326 } else {
1327 res->flags = cfgtew->u.cfgte->flags;
1328 if (res->flags & IPR_IS_IOA_RESOURCE)
1329 res->type = IPR_RES_TYPE_IOAFP;
1330 else
1331 res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
1332
1333 memcpy(&res->std_inq_data, &cfgtew->u.cfgte->std_inq_data,
1334 sizeof(struct ipr_std_inq_data));
1335
1336 res->qmodel = IPR_QUEUEING_MODEL(res);
1337 proto = cfgtew->u.cfgte->proto;
1338 res->res_handle = cfgtew->u.cfgte->res_handle;
1339 }
1340
1341 ipr_update_ata_class(res, proto);
1342}
1343
1344/**
1345 * ipr_clear_res_target - Clear the bit in the bit map representing the target
1346 * for the resource.
1347 * @res: resource entry struct
1348 * @cfgtew: config table entry wrapper struct
1349 *
1350 * Return value:
1351 * none
1352 **/
1353static void ipr_clear_res_target(struct ipr_resource_entry *res)
1354{
1355 struct ipr_resource_entry *gscsi_res = NULL;
1356 struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
1357
1358 if (!ioa_cfg->sis64)
1359 return;
1360
1361 if (res->bus == IPR_ARRAY_VIRTUAL_BUS)
1362 clear_bit(res->target, ioa_cfg->array_ids);
1363 else if (res->bus == IPR_VSET_VIRTUAL_BUS)
1364 clear_bit(res->target, ioa_cfg->vset_ids);
1365 else if (res->bus == 0 && res->type == IPR_RES_TYPE_GENERIC_SCSI) {
1366 list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue)
1367 if (gscsi_res->dev_id == res->dev_id && gscsi_res != res)
1368 return;
1369 clear_bit(res->target, ioa_cfg->target_ids);
1370
1371 } else if (res->bus == 0)
1372 clear_bit(res->target, ioa_cfg->target_ids);
1da177e4
LT
1373}
1374
1375/**
1376 * ipr_handle_config_change - Handle a config change from the adapter
1377 * @ioa_cfg: ioa config struct
1378 * @hostrcb: hostrcb
1379 *
1380 * Return value:
1381 * none
1382 **/
1383static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
3e7ebdfa 1384 struct ipr_hostrcb *hostrcb)
1da177e4
LT
1385{
1386 struct ipr_resource_entry *res = NULL;
3e7ebdfa
WB
1387 struct ipr_config_table_entry_wrapper cfgtew;
1388 __be32 cc_res_handle;
1389
1da177e4
LT
1390 u32 is_ndn = 1;
1391
3e7ebdfa
WB
1392 if (ioa_cfg->sis64) {
1393 cfgtew.u.cfgte64 = &hostrcb->hcam.u.ccn.u.cfgte64;
1394 cc_res_handle = cfgtew.u.cfgte64->res_handle;
1395 } else {
1396 cfgtew.u.cfgte = &hostrcb->hcam.u.ccn.u.cfgte;
1397 cc_res_handle = cfgtew.u.cfgte->res_handle;
1398 }
1da177e4
LT
1399
1400 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3e7ebdfa 1401 if (res->res_handle == cc_res_handle) {
1da177e4
LT
1402 is_ndn = 0;
1403 break;
1404 }
1405 }
1406
1407 if (is_ndn) {
1408 if (list_empty(&ioa_cfg->free_res_q)) {
1409 ipr_send_hcam(ioa_cfg,
1410 IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
1411 hostrcb);
1412 return;
1413 }
1414
1415 res = list_entry(ioa_cfg->free_res_q.next,
1416 struct ipr_resource_entry, queue);
1417
1418 list_del(&res->queue);
3e7ebdfa 1419 ipr_init_res_entry(res, &cfgtew);
1da177e4
LT
1420 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
1421 }
1422
3e7ebdfa 1423 ipr_update_res_entry(res, &cfgtew);
1da177e4
LT
1424
1425 if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
1426 if (res->sdev) {
1da177e4 1427 res->del_from_ml = 1;
3e7ebdfa 1428 res->res_handle = IPR_INVALID_RES_HANDLE;
1da177e4
LT
1429 if (ioa_cfg->allow_ml_add_del)
1430 schedule_work(&ioa_cfg->work_q);
3e7ebdfa
WB
1431 } else {
1432 ipr_clear_res_target(res);
1da177e4 1433 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
3e7ebdfa 1434 }
5767a1c4 1435 } else if (!res->sdev || res->del_from_ml) {
1da177e4
LT
1436 res->add_to_ml = 1;
1437 if (ioa_cfg->allow_ml_add_del)
1438 schedule_work(&ioa_cfg->work_q);
1439 }
1440
1441 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
1442}
1443
1444/**
1445 * ipr_process_ccn - Op done function for a CCN.
1446 * @ipr_cmd: ipr command struct
1447 *
1448 * This function is the op done function for a configuration
1449 * change notification host controlled async from the adapter.
1450 *
1451 * Return value:
1452 * none
1453 **/
1454static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
1455{
1456 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1457 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
96d21f00 1458 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1da177e4
LT
1459
1460 list_del(&hostrcb->queue);
05a6538a 1461 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
1da177e4
LT
1462
1463 if (ioasc) {
1464 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
1465 dev_err(&ioa_cfg->pdev->dev,
1466 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1467
1468 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
1469 } else {
1470 ipr_handle_config_change(ioa_cfg, hostrcb);
1471 }
1472}
1473
8cf093e2
BK
1474/**
1475 * strip_and_pad_whitespace - Strip and pad trailing whitespace.
1476 * @i: index into buffer
1477 * @buf: string to modify
1478 *
1479 * This function will strip all trailing whitespace, pad the end
1480 * of the string with a single space, and NULL terminate the string.
1481 *
1482 * Return value:
1483 * new length of string
1484 **/
1485static int strip_and_pad_whitespace(int i, char *buf)
1486{
1487 while (i && buf[i] == ' ')
1488 i--;
1489 buf[i+1] = ' ';
1490 buf[i+2] = '\0';
1491 return i + 2;
1492}
1493
1494/**
1495 * ipr_log_vpd_compact - Log the passed extended VPD compactly.
1496 * @prefix: string to print at start of printk
1497 * @hostrcb: hostrcb pointer
1498 * @vpd: vendor/product id/sn struct
1499 *
1500 * Return value:
1501 * none
1502 **/
1503static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1504 struct ipr_vpd *vpd)
1505{
1506 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
1507 int i = 0;
1508
1509 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
1510 i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
1511
1512 memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
1513 i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
1514
1515 memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
1516 buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
1517
1518 ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
1519}
1520
1da177e4
LT
1521/**
1522 * ipr_log_vpd - Log the passed VPD to the error log.
cfc32139 1523 * @vpd: vendor/product id/sn struct
1da177e4
LT
1524 *
1525 * Return value:
1526 * none
1527 **/
cfc32139 1528static void ipr_log_vpd(struct ipr_vpd *vpd)
1da177e4
LT
1529{
1530 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
1531 + IPR_SERIAL_NUM_LEN];
1532
cfc32139
BK
1533 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
1534 memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
1da177e4
LT
1535 IPR_PROD_ID_LEN);
1536 buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
1537 ipr_err("Vendor/Product ID: %s\n", buffer);
1538
cfc32139 1539 memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
1da177e4
LT
1540 buffer[IPR_SERIAL_NUM_LEN] = '\0';
1541 ipr_err(" Serial Number: %s\n", buffer);
1542}
1543
8cf093e2
BK
1544/**
1545 * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
1546 * @prefix: string to print at start of printk
1547 * @hostrcb: hostrcb pointer
1548 * @vpd: vendor/product id/sn/wwn struct
1549 *
1550 * Return value:
1551 * none
1552 **/
1553static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1554 struct ipr_ext_vpd *vpd)
1555{
1556 ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
1557 ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
1558 be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
1559}
1560
ee0f05b8
BK
1561/**
1562 * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
1563 * @vpd: vendor/product id/sn/wwn struct
1564 *
1565 * Return value:
1566 * none
1567 **/
1568static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
1569{
1570 ipr_log_vpd(&vpd->vpd);
1571 ipr_err(" WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
1572 be32_to_cpu(vpd->wwid[1]));
1573}
1574
1575/**
1576 * ipr_log_enhanced_cache_error - Log a cache error.
1577 * @ioa_cfg: ioa config struct
1578 * @hostrcb: hostrcb struct
1579 *
1580 * Return value:
1581 * none
1582 **/
1583static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1584 struct ipr_hostrcb *hostrcb)
1585{
4565e370
WB
1586 struct ipr_hostrcb_type_12_error *error;
1587
1588 if (ioa_cfg->sis64)
1589 error = &hostrcb->hcam.u.error64.u.type_12_error;
1590 else
1591 error = &hostrcb->hcam.u.error.u.type_12_error;
ee0f05b8
BK
1592
1593 ipr_err("-----Current Configuration-----\n");
1594 ipr_err("Cache Directory Card Information:\n");
1595 ipr_log_ext_vpd(&error->ioa_vpd);
1596 ipr_err("Adapter Card Information:\n");
1597 ipr_log_ext_vpd(&error->cfc_vpd);
1598
1599 ipr_err("-----Expected Configuration-----\n");
1600 ipr_err("Cache Directory Card Information:\n");
1601 ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
1602 ipr_err("Adapter Card Information:\n");
1603 ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
1604
1605 ipr_err("Additional IOA Data: %08X %08X %08X\n",
1606 be32_to_cpu(error->ioa_data[0]),
1607 be32_to_cpu(error->ioa_data[1]),
1608 be32_to_cpu(error->ioa_data[2]));
1609}
1610
1da177e4
LT
1611/**
1612 * ipr_log_cache_error - Log a cache error.
1613 * @ioa_cfg: ioa config struct
1614 * @hostrcb: hostrcb struct
1615 *
1616 * Return value:
1617 * none
1618 **/
1619static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1620 struct ipr_hostrcb *hostrcb)
1621{
1622 struct ipr_hostrcb_type_02_error *error =
1623 &hostrcb->hcam.u.error.u.type_02_error;
1624
1625 ipr_err("-----Current Configuration-----\n");
1626 ipr_err("Cache Directory Card Information:\n");
cfc32139 1627 ipr_log_vpd(&error->ioa_vpd);
1da177e4 1628 ipr_err("Adapter Card Information:\n");
cfc32139 1629 ipr_log_vpd(&error->cfc_vpd);
1da177e4
LT
1630
1631 ipr_err("-----Expected Configuration-----\n");
1632 ipr_err("Cache Directory Card Information:\n");
cfc32139 1633 ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
1da177e4 1634 ipr_err("Adapter Card Information:\n");
cfc32139 1635 ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
1da177e4
LT
1636
1637 ipr_err("Additional IOA Data: %08X %08X %08X\n",
1638 be32_to_cpu(error->ioa_data[0]),
1639 be32_to_cpu(error->ioa_data[1]),
1640 be32_to_cpu(error->ioa_data[2]));
1641}
1642
ee0f05b8
BK
1643/**
1644 * ipr_log_enhanced_config_error - Log a configuration error.
1645 * @ioa_cfg: ioa config struct
1646 * @hostrcb: hostrcb struct
1647 *
1648 * Return value:
1649 * none
1650 **/
1651static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
1652 struct ipr_hostrcb *hostrcb)
1653{
1654 int errors_logged, i;
1655 struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
1656 struct ipr_hostrcb_type_13_error *error;
1657
1658 error = &hostrcb->hcam.u.error.u.type_13_error;
1659 errors_logged = be32_to_cpu(error->errors_logged);
1660
1661 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1662 be32_to_cpu(error->errors_detected), errors_logged);
1663
1664 dev_entry = error->dev;
1665
1666 for (i = 0; i < errors_logged; i++, dev_entry++) {
1667 ipr_err_separator;
1668
1669 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1670 ipr_log_ext_vpd(&dev_entry->vpd);
1671
1672 ipr_err("-----New Device Information-----\n");
1673 ipr_log_ext_vpd(&dev_entry->new_vpd);
1674
1675 ipr_err("Cache Directory Card Information:\n");
1676 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1677
1678 ipr_err("Adapter Card Information:\n");
1679 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1680 }
1681}
1682
4565e370
WB
1683/**
1684 * ipr_log_sis64_config_error - Log a device error.
1685 * @ioa_cfg: ioa config struct
1686 * @hostrcb: hostrcb struct
1687 *
1688 * Return value:
1689 * none
1690 **/
1691static void ipr_log_sis64_config_error(struct ipr_ioa_cfg *ioa_cfg,
1692 struct ipr_hostrcb *hostrcb)
1693{
1694 int errors_logged, i;
1695 struct ipr_hostrcb64_device_data_entry_enhanced *dev_entry;
1696 struct ipr_hostrcb_type_23_error *error;
1697 char buffer[IPR_MAX_RES_PATH_LENGTH];
1698
1699 error = &hostrcb->hcam.u.error64.u.type_23_error;
1700 errors_logged = be32_to_cpu(error->errors_logged);
1701
1702 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1703 be32_to_cpu(error->errors_detected), errors_logged);
1704
1705 dev_entry = error->dev;
1706
1707 for (i = 0; i < errors_logged; i++, dev_entry++) {
1708 ipr_err_separator;
1709
1710 ipr_err("Device %d : %s", i + 1,
b3b3b407
BK
1711 __ipr_format_res_path(dev_entry->res_path,
1712 buffer, sizeof(buffer)));
4565e370
WB
1713 ipr_log_ext_vpd(&dev_entry->vpd);
1714
1715 ipr_err("-----New Device Information-----\n");
1716 ipr_log_ext_vpd(&dev_entry->new_vpd);
1717
1718 ipr_err("Cache Directory Card Information:\n");
1719 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1720
1721 ipr_err("Adapter Card Information:\n");
1722 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1723 }
1724}
1725
1da177e4
LT
1726/**
1727 * ipr_log_config_error - Log a configuration error.
1728 * @ioa_cfg: ioa config struct
1729 * @hostrcb: hostrcb struct
1730 *
1731 * Return value:
1732 * none
1733 **/
1734static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
1735 struct ipr_hostrcb *hostrcb)
1736{
1737 int errors_logged, i;
1738 struct ipr_hostrcb_device_data_entry *dev_entry;
1739 struct ipr_hostrcb_type_03_error *error;
1740
1741 error = &hostrcb->hcam.u.error.u.type_03_error;
1742 errors_logged = be32_to_cpu(error->errors_logged);
1743
1744 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1745 be32_to_cpu(error->errors_detected), errors_logged);
1746
cfc32139 1747 dev_entry = error->dev;
1da177e4
LT
1748
1749 for (i = 0; i < errors_logged; i++, dev_entry++) {
1750 ipr_err_separator;
1751
fa15b1f6 1752 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
cfc32139 1753 ipr_log_vpd(&dev_entry->vpd);
1da177e4
LT
1754
1755 ipr_err("-----New Device Information-----\n");
cfc32139 1756 ipr_log_vpd(&dev_entry->new_vpd);
1da177e4
LT
1757
1758 ipr_err("Cache Directory Card Information:\n");
cfc32139 1759 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
1da177e4
LT
1760
1761 ipr_err("Adapter Card Information:\n");
cfc32139 1762 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
1da177e4
LT
1763
1764 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1765 be32_to_cpu(dev_entry->ioa_data[0]),
1766 be32_to_cpu(dev_entry->ioa_data[1]),
1767 be32_to_cpu(dev_entry->ioa_data[2]),
1768 be32_to_cpu(dev_entry->ioa_data[3]),
1769 be32_to_cpu(dev_entry->ioa_data[4]));
1770 }
1771}
1772
ee0f05b8
BK
1773/**
1774 * ipr_log_enhanced_array_error - Log an array configuration error.
1775 * @ioa_cfg: ioa config struct
1776 * @hostrcb: hostrcb struct
1777 *
1778 * Return value:
1779 * none
1780 **/
1781static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
1782 struct ipr_hostrcb *hostrcb)
1783{
1784 int i, num_entries;
1785 struct ipr_hostrcb_type_14_error *error;
1786 struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
1787 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1788
1789 error = &hostrcb->hcam.u.error.u.type_14_error;
1790
1791 ipr_err_separator;
1792
1793 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1794 error->protection_level,
1795 ioa_cfg->host->host_no,
1796 error->last_func_vset_res_addr.bus,
1797 error->last_func_vset_res_addr.target,
1798 error->last_func_vset_res_addr.lun);
1799
1800 ipr_err_separator;
1801
1802 array_entry = error->array_member;
1803 num_entries = min_t(u32, be32_to_cpu(error->num_entries),
7262026f 1804 ARRAY_SIZE(error->array_member));
ee0f05b8
BK
1805
1806 for (i = 0; i < num_entries; i++, array_entry++) {
1807 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1808 continue;
1809
1810 if (be32_to_cpu(error->exposed_mode_adn) == i)
1811 ipr_err("Exposed Array Member %d:\n", i);
1812 else
1813 ipr_err("Array Member %d:\n", i);
1814
1815 ipr_log_ext_vpd(&array_entry->vpd);
1816 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1817 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1818 "Expected Location");
1819
1820 ipr_err_separator;
1821 }
1822}
1823
1da177e4
LT
1824/**
1825 * ipr_log_array_error - Log an array configuration error.
1826 * @ioa_cfg: ioa config struct
1827 * @hostrcb: hostrcb struct
1828 *
1829 * Return value:
1830 * none
1831 **/
1832static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1833 struct ipr_hostrcb *hostrcb)
1834{
1835 int i;
1836 struct ipr_hostrcb_type_04_error *error;
1837 struct ipr_hostrcb_array_data_entry *array_entry;
1838 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1839
1840 error = &hostrcb->hcam.u.error.u.type_04_error;
1841
1842 ipr_err_separator;
1843
1844 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1845 error->protection_level,
1846 ioa_cfg->host->host_no,
1847 error->last_func_vset_res_addr.bus,
1848 error->last_func_vset_res_addr.target,
1849 error->last_func_vset_res_addr.lun);
1850
1851 ipr_err_separator;
1852
1853 array_entry = error->array_member;
1854
1855 for (i = 0; i < 18; i++) {
cfc32139 1856 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1da177e4
LT
1857 continue;
1858
fa15b1f6 1859 if (be32_to_cpu(error->exposed_mode_adn) == i)
1da177e4 1860 ipr_err("Exposed Array Member %d:\n", i);
fa15b1f6 1861 else
1da177e4 1862 ipr_err("Array Member %d:\n", i);
1da177e4 1863
cfc32139 1864 ipr_log_vpd(&array_entry->vpd);
1da177e4 1865
fa15b1f6
BK
1866 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1867 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1868 "Expected Location");
1da177e4
LT
1869
1870 ipr_err_separator;
1871
1872 if (i == 9)
1873 array_entry = error->array_member2;
1874 else
1875 array_entry++;
1876 }
1877}
1878
1879/**
b0df54bb 1880 * ipr_log_hex_data - Log additional hex IOA error data.
ac719aba 1881 * @ioa_cfg: ioa config struct
b0df54bb
BK
1882 * @data: IOA error data
1883 * @len: data length
1da177e4
LT
1884 *
1885 * Return value:
1886 * none
1887 **/
ac719aba 1888static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
1da177e4
LT
1889{
1890 int i;
1da177e4 1891
b0df54bb 1892 if (len == 0)
1da177e4
LT
1893 return;
1894
ac719aba
BK
1895 if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
1896 len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
1897
b0df54bb 1898 for (i = 0; i < len / 4; i += 4) {
1da177e4 1899 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
b0df54bb
BK
1900 be32_to_cpu(data[i]),
1901 be32_to_cpu(data[i+1]),
1902 be32_to_cpu(data[i+2]),
1903 be32_to_cpu(data[i+3]));
1da177e4
LT
1904 }
1905}
1906
ee0f05b8
BK
1907/**
1908 * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
1909 * @ioa_cfg: ioa config struct
1910 * @hostrcb: hostrcb struct
1911 *
1912 * Return value:
1913 * none
1914 **/
1915static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1916 struct ipr_hostrcb *hostrcb)
1917{
1918 struct ipr_hostrcb_type_17_error *error;
1919
4565e370
WB
1920 if (ioa_cfg->sis64)
1921 error = &hostrcb->hcam.u.error64.u.type_17_error;
1922 else
1923 error = &hostrcb->hcam.u.error.u.type_17_error;
1924
ee0f05b8 1925 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
ca54cb8c 1926 strim(error->failure_reason);
ee0f05b8 1927
8cf093e2
BK
1928 ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1929 be32_to_cpu(hostrcb->hcam.u.error.prc));
1930 ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
ac719aba 1931 ipr_log_hex_data(ioa_cfg, error->data,
ee0f05b8
BK
1932 be32_to_cpu(hostrcb->hcam.length) -
1933 (offsetof(struct ipr_hostrcb_error, u) +
1934 offsetof(struct ipr_hostrcb_type_17_error, data)));
1935}
1936
b0df54bb
BK
1937/**
1938 * ipr_log_dual_ioa_error - Log a dual adapter error.
1939 * @ioa_cfg: ioa config struct
1940 * @hostrcb: hostrcb struct
1941 *
1942 * Return value:
1943 * none
1944 **/
1945static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1946 struct ipr_hostrcb *hostrcb)
1947{
1948 struct ipr_hostrcb_type_07_error *error;
1949
1950 error = &hostrcb->hcam.u.error.u.type_07_error;
1951 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
ca54cb8c 1952 strim(error->failure_reason);
b0df54bb 1953
8cf093e2
BK
1954 ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1955 be32_to_cpu(hostrcb->hcam.u.error.prc));
1956 ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
ac719aba 1957 ipr_log_hex_data(ioa_cfg, error->data,
b0df54bb
BK
1958 be32_to_cpu(hostrcb->hcam.length) -
1959 (offsetof(struct ipr_hostrcb_error, u) +
1960 offsetof(struct ipr_hostrcb_type_07_error, data)));
1961}
1962
49dc6a18
BK
1963static const struct {
1964 u8 active;
1965 char *desc;
1966} path_active_desc[] = {
1967 { IPR_PATH_NO_INFO, "Path" },
1968 { IPR_PATH_ACTIVE, "Active path" },
1969 { IPR_PATH_NOT_ACTIVE, "Inactive path" }
1970};
1971
1972static const struct {
1973 u8 state;
1974 char *desc;
1975} path_state_desc[] = {
1976 { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
1977 { IPR_PATH_HEALTHY, "is healthy" },
1978 { IPR_PATH_DEGRADED, "is degraded" },
1979 { IPR_PATH_FAILED, "is failed" }
1980};
1981
1982/**
1983 * ipr_log_fabric_path - Log a fabric path error
1984 * @hostrcb: hostrcb struct
1985 * @fabric: fabric descriptor
1986 *
1987 * Return value:
1988 * none
1989 **/
1990static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
1991 struct ipr_hostrcb_fabric_desc *fabric)
1992{
1993 int i, j;
1994 u8 path_state = fabric->path_state;
1995 u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1996 u8 state = path_state & IPR_PATH_STATE_MASK;
1997
1998 for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1999 if (path_active_desc[i].active != active)
2000 continue;
2001
2002 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
2003 if (path_state_desc[j].state != state)
2004 continue;
2005
2006 if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
2007 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
2008 path_active_desc[i].desc, path_state_desc[j].desc,
2009 fabric->ioa_port);
2010 } else if (fabric->cascaded_expander == 0xff) {
2011 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
2012 path_active_desc[i].desc, path_state_desc[j].desc,
2013 fabric->ioa_port, fabric->phy);
2014 } else if (fabric->phy == 0xff) {
2015 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
2016 path_active_desc[i].desc, path_state_desc[j].desc,
2017 fabric->ioa_port, fabric->cascaded_expander);
2018 } else {
2019 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
2020 path_active_desc[i].desc, path_state_desc[j].desc,
2021 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
2022 }
2023 return;
2024 }
2025 }
2026
2027 ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
2028 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
2029}
2030
4565e370
WB
2031/**
2032 * ipr_log64_fabric_path - Log a fabric path error
2033 * @hostrcb: hostrcb struct
2034 * @fabric: fabric descriptor
2035 *
2036 * Return value:
2037 * none
2038 **/
2039static void ipr_log64_fabric_path(struct ipr_hostrcb *hostrcb,
2040 struct ipr_hostrcb64_fabric_desc *fabric)
2041{
2042 int i, j;
2043 u8 path_state = fabric->path_state;
2044 u8 active = path_state & IPR_PATH_ACTIVE_MASK;
2045 u8 state = path_state & IPR_PATH_STATE_MASK;
2046 char buffer[IPR_MAX_RES_PATH_LENGTH];
2047
2048 for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
2049 if (path_active_desc[i].active != active)
2050 continue;
2051
2052 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
2053 if (path_state_desc[j].state != state)
2054 continue;
2055
2056 ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s\n",
2057 path_active_desc[i].desc, path_state_desc[j].desc,
b3b3b407
BK
2058 ipr_format_res_path(hostrcb->ioa_cfg,
2059 fabric->res_path,
2060 buffer, sizeof(buffer)));
4565e370
WB
2061 return;
2062 }
2063 }
2064
2065 ipr_err("Path state=%02X Resource Path=%s\n", path_state,
b3b3b407
BK
2066 ipr_format_res_path(hostrcb->ioa_cfg, fabric->res_path,
2067 buffer, sizeof(buffer)));
4565e370
WB
2068}
2069
49dc6a18
BK
2070static const struct {
2071 u8 type;
2072 char *desc;
2073} path_type_desc[] = {
2074 { IPR_PATH_CFG_IOA_PORT, "IOA port" },
2075 { IPR_PATH_CFG_EXP_PORT, "Expander port" },
2076 { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
2077 { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
2078};
2079
2080static const struct {
2081 u8 status;
2082 char *desc;
2083} path_status_desc[] = {
2084 { IPR_PATH_CFG_NO_PROB, "Functional" },
2085 { IPR_PATH_CFG_DEGRADED, "Degraded" },
2086 { IPR_PATH_CFG_FAILED, "Failed" },
2087 { IPR_PATH_CFG_SUSPECT, "Suspect" },
2088 { IPR_PATH_NOT_DETECTED, "Missing" },
2089 { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
2090};
2091
2092static const char *link_rate[] = {
2093 "unknown",
2094 "disabled",
2095 "phy reset problem",
2096 "spinup hold",
2097 "port selector",
2098 "unknown",
2099 "unknown",
2100 "unknown",
2101 "1.5Gbps",
2102 "3.0Gbps",
2103 "unknown",
2104 "unknown",
2105 "unknown",
2106 "unknown",
2107 "unknown",
2108 "unknown"
2109};
2110
2111/**
2112 * ipr_log_path_elem - Log a fabric path element.
2113 * @hostrcb: hostrcb struct
2114 * @cfg: fabric path element struct
2115 *
2116 * Return value:
2117 * none
2118 **/
2119static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
2120 struct ipr_hostrcb_config_element *cfg)
2121{
2122 int i, j;
2123 u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
2124 u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
2125
2126 if (type == IPR_PATH_CFG_NOT_EXIST)
2127 return;
2128
2129 for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
2130 if (path_type_desc[i].type != type)
2131 continue;
2132
2133 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
2134 if (path_status_desc[j].status != status)
2135 continue;
2136
2137 if (type == IPR_PATH_CFG_IOA_PORT) {
2138 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
2139 path_status_desc[j].desc, path_type_desc[i].desc,
2140 cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2141 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2142 } else {
2143 if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
2144 ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
2145 path_status_desc[j].desc, path_type_desc[i].desc,
2146 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2147 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2148 } else if (cfg->cascaded_expander == 0xff) {
2149 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
2150 "WWN=%08X%08X\n", path_status_desc[j].desc,
2151 path_type_desc[i].desc, cfg->phy,
2152 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2153 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2154 } else if (cfg->phy == 0xff) {
2155 ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
2156 "WWN=%08X%08X\n", path_status_desc[j].desc,
2157 path_type_desc[i].desc, cfg->cascaded_expander,
2158 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2159 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2160 } else {
2161 ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
2162 "WWN=%08X%08X\n", path_status_desc[j].desc,
2163 path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
2164 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2165 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2166 }
2167 }
2168 return;
2169 }
2170 }
2171
2172 ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
2173 "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
2174 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2175 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2176}
2177
4565e370
WB
2178/**
2179 * ipr_log64_path_elem - Log a fabric path element.
2180 * @hostrcb: hostrcb struct
2181 * @cfg: fabric path element struct
2182 *
2183 * Return value:
2184 * none
2185 **/
2186static void ipr_log64_path_elem(struct ipr_hostrcb *hostrcb,
2187 struct ipr_hostrcb64_config_element *cfg)
2188{
2189 int i, j;
2190 u8 desc_id = cfg->descriptor_id & IPR_DESCRIPTOR_MASK;
2191 u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
2192 u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
2193 char buffer[IPR_MAX_RES_PATH_LENGTH];
2194
2195 if (type == IPR_PATH_CFG_NOT_EXIST || desc_id != IPR_DESCRIPTOR_SIS64)
2196 return;
2197
2198 for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
2199 if (path_type_desc[i].type != type)
2200 continue;
2201
2202 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
2203 if (path_status_desc[j].status != status)
2204 continue;
2205
2206 ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s, Link rate=%s, WWN=%08X%08X\n",
2207 path_status_desc[j].desc, path_type_desc[i].desc,
b3b3b407
BK
2208 ipr_format_res_path(hostrcb->ioa_cfg,
2209 cfg->res_path, buffer, sizeof(buffer)),
2210 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2211 be32_to_cpu(cfg->wwid[0]),
2212 be32_to_cpu(cfg->wwid[1]));
4565e370
WB
2213 return;
2214 }
2215 }
2216 ipr_hcam_err(hostrcb, "Path element=%02X: Resource Path=%s, Link rate=%s "
2217 "WWN=%08X%08X\n", cfg->type_status,
b3b3b407
BK
2218 ipr_format_res_path(hostrcb->ioa_cfg,
2219 cfg->res_path, buffer, sizeof(buffer)),
2220 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2221 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
4565e370
WB
2222}
2223
49dc6a18
BK
2224/**
2225 * ipr_log_fabric_error - Log a fabric error.
2226 * @ioa_cfg: ioa config struct
2227 * @hostrcb: hostrcb struct
2228 *
2229 * Return value:
2230 * none
2231 **/
2232static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
2233 struct ipr_hostrcb *hostrcb)
2234{
2235 struct ipr_hostrcb_type_20_error *error;
2236 struct ipr_hostrcb_fabric_desc *fabric;
2237 struct ipr_hostrcb_config_element *cfg;
2238 int i, add_len;
2239
2240 error = &hostrcb->hcam.u.error.u.type_20_error;
2241 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
2242 ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
2243
2244 add_len = be32_to_cpu(hostrcb->hcam.length) -
2245 (offsetof(struct ipr_hostrcb_error, u) +
2246 offsetof(struct ipr_hostrcb_type_20_error, desc));
2247
2248 for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
2249 ipr_log_fabric_path(hostrcb, fabric);
2250 for_each_fabric_cfg(fabric, cfg)
2251 ipr_log_path_elem(hostrcb, cfg);
2252
2253 add_len -= be16_to_cpu(fabric->length);
2254 fabric = (struct ipr_hostrcb_fabric_desc *)
2255 ((unsigned long)fabric + be16_to_cpu(fabric->length));
2256 }
2257
ac719aba 2258 ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
49dc6a18
BK
2259}
2260
4565e370
WB
2261/**
2262 * ipr_log_sis64_array_error - Log a sis64 array error.
2263 * @ioa_cfg: ioa config struct
2264 * @hostrcb: hostrcb struct
2265 *
2266 * Return value:
2267 * none
2268 **/
2269static void ipr_log_sis64_array_error(struct ipr_ioa_cfg *ioa_cfg,
2270 struct ipr_hostrcb *hostrcb)
2271{
2272 int i, num_entries;
2273 struct ipr_hostrcb_type_24_error *error;
2274 struct ipr_hostrcb64_array_data_entry *array_entry;
2275 char buffer[IPR_MAX_RES_PATH_LENGTH];
2276 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
2277
2278 error = &hostrcb->hcam.u.error64.u.type_24_error;
2279
2280 ipr_err_separator;
2281
2282 ipr_err("RAID %s Array Configuration: %s\n",
2283 error->protection_level,
b3b3b407
BK
2284 ipr_format_res_path(ioa_cfg, error->last_res_path,
2285 buffer, sizeof(buffer)));
4565e370
WB
2286
2287 ipr_err_separator;
2288
2289 array_entry = error->array_member;
7262026f
WB
2290 num_entries = min_t(u32, error->num_entries,
2291 ARRAY_SIZE(error->array_member));
4565e370
WB
2292
2293 for (i = 0; i < num_entries; i++, array_entry++) {
2294
2295 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
2296 continue;
2297
2298 if (error->exposed_mode_adn == i)
2299 ipr_err("Exposed Array Member %d:\n", i);
2300 else
2301 ipr_err("Array Member %d:\n", i);
2302
2303 ipr_err("Array Member %d:\n", i);
2304 ipr_log_ext_vpd(&array_entry->vpd);
7262026f 2305 ipr_err("Current Location: %s\n",
b3b3b407
BK
2306 ipr_format_res_path(ioa_cfg, array_entry->res_path,
2307 buffer, sizeof(buffer)));
7262026f 2308 ipr_err("Expected Location: %s\n",
b3b3b407
BK
2309 ipr_format_res_path(ioa_cfg,
2310 array_entry->expected_res_path,
2311 buffer, sizeof(buffer)));
4565e370
WB
2312
2313 ipr_err_separator;
2314 }
2315}
2316
2317/**
2318 * ipr_log_sis64_fabric_error - Log a sis64 fabric error.
2319 * @ioa_cfg: ioa config struct
2320 * @hostrcb: hostrcb struct
2321 *
2322 * Return value:
2323 * none
2324 **/
2325static void ipr_log_sis64_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
2326 struct ipr_hostrcb *hostrcb)
2327{
2328 struct ipr_hostrcb_type_30_error *error;
2329 struct ipr_hostrcb64_fabric_desc *fabric;
2330 struct ipr_hostrcb64_config_element *cfg;
2331 int i, add_len;
2332
2333 error = &hostrcb->hcam.u.error64.u.type_30_error;
2334
2335 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
2336 ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
2337
2338 add_len = be32_to_cpu(hostrcb->hcam.length) -
2339 (offsetof(struct ipr_hostrcb64_error, u) +
2340 offsetof(struct ipr_hostrcb_type_30_error, desc));
2341
2342 for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
2343 ipr_log64_fabric_path(hostrcb, fabric);
2344 for_each_fabric_cfg(fabric, cfg)
2345 ipr_log64_path_elem(hostrcb, cfg);
2346
2347 add_len -= be16_to_cpu(fabric->length);
2348 fabric = (struct ipr_hostrcb64_fabric_desc *)
2349 ((unsigned long)fabric + be16_to_cpu(fabric->length));
2350 }
2351
2352 ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
2353}
2354
b0df54bb
BK
2355/**
2356 * ipr_log_generic_error - Log an adapter error.
2357 * @ioa_cfg: ioa config struct
2358 * @hostrcb: hostrcb struct
2359 *
2360 * Return value:
2361 * none
2362 **/
2363static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
2364 struct ipr_hostrcb *hostrcb)
2365{
ac719aba 2366 ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
b0df54bb
BK
2367 be32_to_cpu(hostrcb->hcam.length));
2368}
2369
169b9ec8
WX
2370/**
2371 * ipr_log_sis64_device_error - Log a cache error.
2372 * @ioa_cfg: ioa config struct
2373 * @hostrcb: hostrcb struct
2374 *
2375 * Return value:
2376 * none
2377 **/
2378static void ipr_log_sis64_device_error(struct ipr_ioa_cfg *ioa_cfg,
2379 struct ipr_hostrcb *hostrcb)
2380{
2381 struct ipr_hostrcb_type_21_error *error;
2382 char buffer[IPR_MAX_RES_PATH_LENGTH];
2383
2384 error = &hostrcb->hcam.u.error64.u.type_21_error;
2385
2386 ipr_err("-----Failing Device Information-----\n");
2387 ipr_err("World Wide Unique ID: %08X%08X%08X%08X\n",
2388 be32_to_cpu(error->wwn[0]), be32_to_cpu(error->wwn[1]),
2389 be32_to_cpu(error->wwn[2]), be32_to_cpu(error->wwn[3]));
2390 ipr_err("Device Resource Path: %s\n",
2391 __ipr_format_res_path(error->res_path,
2392 buffer, sizeof(buffer)));
2393 error->primary_problem_desc[sizeof(error->primary_problem_desc) - 1] = '\0';
2394 error->second_problem_desc[sizeof(error->second_problem_desc) - 1] = '\0';
2395 ipr_err("Primary Problem Description: %s\n", error->primary_problem_desc);
2396 ipr_err("Secondary Problem Description: %s\n", error->second_problem_desc);
2397 ipr_err("SCSI Sense Data:\n");
2398 ipr_log_hex_data(ioa_cfg, error->sense_data, sizeof(error->sense_data));
2399 ipr_err("SCSI Command Descriptor Block: \n");
2400 ipr_log_hex_data(ioa_cfg, error->cdb, sizeof(error->cdb));
2401
2402 ipr_err("Additional IOA Data:\n");
2403 ipr_log_hex_data(ioa_cfg, error->ioa_data, be32_to_cpu(error->length_of_error));
2404}
2405
1da177e4
LT
2406/**
2407 * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
2408 * @ioasc: IOASC
2409 *
2410 * This function will return the index of into the ipr_error_table
2411 * for the specified IOASC. If the IOASC is not in the table,
2412 * 0 will be returned, which points to the entry used for unknown errors.
2413 *
2414 * Return value:
2415 * index into the ipr_error_table
2416 **/
2417static u32 ipr_get_error(u32 ioasc)
2418{
2419 int i;
2420
2421 for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
35a39691 2422 if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
1da177e4
LT
2423 return i;
2424
2425 return 0;
2426}
2427
2428/**
2429 * ipr_handle_log_data - Log an adapter error.
2430 * @ioa_cfg: ioa config struct
2431 * @hostrcb: hostrcb struct
2432 *
2433 * This function logs an adapter error to the system.
2434 *
2435 * Return value:
2436 * none
2437 **/
2438static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
2439 struct ipr_hostrcb *hostrcb)
2440{
2441 u32 ioasc;
2442 int error_index;
3185ea63 2443 struct ipr_hostrcb_type_21_error *error;
1da177e4
LT
2444
2445 if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
2446 return;
2447
2448 if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
2449 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
2450
4565e370
WB
2451 if (ioa_cfg->sis64)
2452 ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
2453 else
2454 ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
1da177e4 2455
4565e370
WB
2456 if (!ioa_cfg->sis64 && (ioasc == IPR_IOASC_BUS_WAS_RESET ||
2457 ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER)) {
1da177e4
LT
2458 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
2459 scsi_report_bus_reset(ioa_cfg->host,
4565e370 2460 hostrcb->hcam.u.error.fd_res_addr.bus);
1da177e4
LT
2461 }
2462
2463 error_index = ipr_get_error(ioasc);
2464
2465 if (!ipr_error_table[error_index].log_hcam)
2466 return;
2467
3185ea63 2468 if (ioasc == IPR_IOASC_HW_CMD_FAILED &&
2469 hostrcb->hcam.overlay_id == IPR_HOST_RCB_OVERLAY_ID_21) {
2470 error = &hostrcb->hcam.u.error64.u.type_21_error;
2471
2472 if (((be32_to_cpu(error->sense_data[0]) & 0x0000ff00) >> 8) == ILLEGAL_REQUEST &&
2473 ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
2474 return;
2475 }
2476
49dc6a18 2477 ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
1da177e4
LT
2478
2479 /* Set indication we have logged an error */
2480 ioa_cfg->errors_logged++;
2481
933916f3 2482 if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
1da177e4 2483 return;
cf852037
BK
2484 if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
2485 hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
1da177e4
LT
2486
2487 switch (hostrcb->hcam.overlay_id) {
1da177e4
LT
2488 case IPR_HOST_RCB_OVERLAY_ID_2:
2489 ipr_log_cache_error(ioa_cfg, hostrcb);
2490 break;
2491 case IPR_HOST_RCB_OVERLAY_ID_3:
2492 ipr_log_config_error(ioa_cfg, hostrcb);
2493 break;
2494 case IPR_HOST_RCB_OVERLAY_ID_4:
2495 case IPR_HOST_RCB_OVERLAY_ID_6:
2496 ipr_log_array_error(ioa_cfg, hostrcb);
2497 break;
b0df54bb
BK
2498 case IPR_HOST_RCB_OVERLAY_ID_7:
2499 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
2500 break;
ee0f05b8
BK
2501 case IPR_HOST_RCB_OVERLAY_ID_12:
2502 ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
2503 break;
2504 case IPR_HOST_RCB_OVERLAY_ID_13:
2505 ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
2506 break;
2507 case IPR_HOST_RCB_OVERLAY_ID_14:
2508 case IPR_HOST_RCB_OVERLAY_ID_16:
2509 ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
2510 break;
2511 case IPR_HOST_RCB_OVERLAY_ID_17:
2512 ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
2513 break;
49dc6a18
BK
2514 case IPR_HOST_RCB_OVERLAY_ID_20:
2515 ipr_log_fabric_error(ioa_cfg, hostrcb);
2516 break;
169b9ec8
WX
2517 case IPR_HOST_RCB_OVERLAY_ID_21:
2518 ipr_log_sis64_device_error(ioa_cfg, hostrcb);
2519 break;
4565e370
WB
2520 case IPR_HOST_RCB_OVERLAY_ID_23:
2521 ipr_log_sis64_config_error(ioa_cfg, hostrcb);
2522 break;
2523 case IPR_HOST_RCB_OVERLAY_ID_24:
2524 case IPR_HOST_RCB_OVERLAY_ID_26:
2525 ipr_log_sis64_array_error(ioa_cfg, hostrcb);
2526 break;
2527 case IPR_HOST_RCB_OVERLAY_ID_30:
2528 ipr_log_sis64_fabric_error(ioa_cfg, hostrcb);
2529 break;
cf852037 2530 case IPR_HOST_RCB_OVERLAY_ID_1:
1da177e4 2531 case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
1da177e4 2532 default:
a9cfca96 2533 ipr_log_generic_error(ioa_cfg, hostrcb);
1da177e4
LT
2534 break;
2535 }
2536}
2537
2538/**
2539 * ipr_process_error - Op done function for an adapter error log.
2540 * @ipr_cmd: ipr command struct
2541 *
2542 * This function is the op done function for an error log host
2543 * controlled async from the adapter. It will log the error and
2544 * send the HCAM back to the adapter.
2545 *
2546 * Return value:
2547 * none
2548 **/
2549static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
2550{
2551 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2552 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
96d21f00 2553 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
4565e370
WB
2554 u32 fd_ioasc;
2555
2556 if (ioa_cfg->sis64)
2557 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
2558 else
2559 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
1da177e4
LT
2560
2561 list_del(&hostrcb->queue);
05a6538a 2562 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
1da177e4
LT
2563
2564 if (!ioasc) {
2565 ipr_handle_log_data(ioa_cfg, hostrcb);
65f56475
BK
2566 if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
2567 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
1da177e4
LT
2568 } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
2569 dev_err(&ioa_cfg->pdev->dev,
2570 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
2571 }
2572
2573 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
2574}
2575
2576/**
2577 * ipr_timeout - An internally generated op has timed out.
2578 * @ipr_cmd: ipr command struct
2579 *
2580 * This function blocks host requests and initiates an
2581 * adapter reset.
2582 *
2583 * Return value:
2584 * none
2585 **/
2586static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
2587{
2588 unsigned long lock_flags = 0;
2589 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2590
2591 ENTER;
2592 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2593
2594 ioa_cfg->errors_logged++;
2595 dev_err(&ioa_cfg->pdev->dev,
2596 "Adapter being reset due to command timeout.\n");
2597
2598 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2599 ioa_cfg->sdt_state = GET_DUMP;
2600
2601 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
2602 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2603
2604 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2605 LEAVE;
2606}
2607
2608/**
2609 * ipr_oper_timeout - Adapter timed out transitioning to operational
2610 * @ipr_cmd: ipr command struct
2611 *
2612 * This function blocks host requests and initiates an
2613 * adapter reset.
2614 *
2615 * Return value:
2616 * none
2617 **/
2618static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
2619{
2620 unsigned long lock_flags = 0;
2621 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2622
2623 ENTER;
2624 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2625
2626 ioa_cfg->errors_logged++;
2627 dev_err(&ioa_cfg->pdev->dev,
2628 "Adapter timed out transitioning to operational.\n");
2629
2630 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2631 ioa_cfg->sdt_state = GET_DUMP;
2632
2633 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
2634 if (ipr_fastfail)
2635 ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
2636 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2637 }
2638
2639 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2640 LEAVE;
2641}
2642
1da177e4
LT
2643/**
2644 * ipr_find_ses_entry - Find matching SES in SES table
2645 * @res: resource entry struct of SES
2646 *
2647 * Return value:
2648 * pointer to SES table entry / NULL on failure
2649 **/
2650static const struct ipr_ses_table_entry *
2651ipr_find_ses_entry(struct ipr_resource_entry *res)
2652{
2653 int i, j, matches;
3e7ebdfa 2654 struct ipr_std_inq_vpids *vpids;
1da177e4
LT
2655 const struct ipr_ses_table_entry *ste = ipr_ses_table;
2656
2657 for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
2658 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
2659 if (ste->compare_product_id_byte[j] == 'X') {
3e7ebdfa
WB
2660 vpids = &res->std_inq_data.vpids;
2661 if (vpids->product_id[j] == ste->product_id[j])
1da177e4
LT
2662 matches++;
2663 else
2664 break;
2665 } else
2666 matches++;
2667 }
2668
2669 if (matches == IPR_PROD_ID_LEN)
2670 return ste;
2671 }
2672
2673 return NULL;
2674}
2675
2676/**
2677 * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
2678 * @ioa_cfg: ioa config struct
2679 * @bus: SCSI bus
2680 * @bus_width: bus width
2681 *
2682 * Return value:
2683 * SCSI bus speed in units of 100KHz, 1600 is 160 MHz
2684 * For a 2-byte wide SCSI bus, the maximum transfer speed is
2685 * twice the maximum transfer rate (e.g. for a wide enabled bus,
2686 * max 160MHz = max 320MB/sec).
2687 **/
2688static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
2689{
2690 struct ipr_resource_entry *res;
2691 const struct ipr_ses_table_entry *ste;
2692 u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
2693
2694 /* Loop through each config table entry in the config table buffer */
2695 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3e7ebdfa 2696 if (!(IPR_IS_SES_DEVICE(res->std_inq_data)))
1da177e4
LT
2697 continue;
2698
3e7ebdfa 2699 if (bus != res->bus)
1da177e4
LT
2700 continue;
2701
2702 if (!(ste = ipr_find_ses_entry(res)))
2703 continue;
2704
2705 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
2706 }
2707
2708 return max_xfer_rate;
2709}
2710
2711/**
2712 * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
2713 * @ioa_cfg: ioa config struct
2714 * @max_delay: max delay in micro-seconds to wait
2715 *
2716 * Waits for an IODEBUG ACK from the IOA, doing busy looping.
2717 *
2718 * Return value:
2719 * 0 on success / other on failure
2720 **/
2721static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
2722{
2723 volatile u32 pcii_reg;
2724 int delay = 1;
2725
2726 /* Read interrupt reg until IOA signals IO Debug Acknowledge */
2727 while (delay < max_delay) {
2728 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
2729
2730 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
2731 return 0;
2732
2733 /* udelay cannot be used if delay is more than a few milliseconds */
2734 if ((delay / 1000) > MAX_UDELAY_MS)
2735 mdelay(delay / 1000);
2736 else
2737 udelay(delay);
2738
2739 delay += delay;
2740 }
2741 return -EIO;
2742}
2743
dcbad00e
WB
2744/**
2745 * ipr_get_sis64_dump_data_section - Dump IOA memory
2746 * @ioa_cfg: ioa config struct
2747 * @start_addr: adapter address to dump
2748 * @dest: destination kernel buffer
2749 * @length_in_words: length to dump in 4 byte words
2750 *
2751 * Return value:
2752 * 0 on success
2753 **/
2754static int ipr_get_sis64_dump_data_section(struct ipr_ioa_cfg *ioa_cfg,
2755 u32 start_addr,
2756 __be32 *dest, u32 length_in_words)
2757{
2758 int i;
2759
2760 for (i = 0; i < length_in_words; i++) {
2761 writel(start_addr+(i*4), ioa_cfg->regs.dump_addr_reg);
2762 *dest = cpu_to_be32(readl(ioa_cfg->regs.dump_data_reg));
2763 dest++;
2764 }
2765
2766 return 0;
2767}
2768
1da177e4
LT
2769/**
2770 * ipr_get_ldump_data_section - Dump IOA memory
2771 * @ioa_cfg: ioa config struct
2772 * @start_addr: adapter address to dump
2773 * @dest: destination kernel buffer
2774 * @length_in_words: length to dump in 4 byte words
2775 *
2776 * Return value:
2777 * 0 on success / -EIO on failure
2778 **/
2779static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
2780 u32 start_addr,
2781 __be32 *dest, u32 length_in_words)
2782{
2783 volatile u32 temp_pcii_reg;
2784 int i, delay = 0;
2785
dcbad00e
WB
2786 if (ioa_cfg->sis64)
2787 return ipr_get_sis64_dump_data_section(ioa_cfg, start_addr,
2788 dest, length_in_words);
2789
1da177e4
LT
2790 /* Write IOA interrupt reg starting LDUMP state */
2791 writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
214777ba 2792 ioa_cfg->regs.set_uproc_interrupt_reg32);
1da177e4
LT
2793
2794 /* Wait for IO debug acknowledge */
2795 if (ipr_wait_iodbg_ack(ioa_cfg,
2796 IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
2797 dev_err(&ioa_cfg->pdev->dev,
2798 "IOA dump long data transfer timeout\n");
2799 return -EIO;
2800 }
2801
2802 /* Signal LDUMP interlocked - clear IO debug ack */
2803 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2804 ioa_cfg->regs.clr_interrupt_reg);
2805
2806 /* Write Mailbox with starting address */
2807 writel(start_addr, ioa_cfg->ioa_mailbox);
2808
2809 /* Signal address valid - clear IOA Reset alert */
2810 writel(IPR_UPROCI_RESET_ALERT,
214777ba 2811 ioa_cfg->regs.clr_uproc_interrupt_reg32);
1da177e4
LT
2812
2813 for (i = 0; i < length_in_words; i++) {
2814 /* Wait for IO debug acknowledge */
2815 if (ipr_wait_iodbg_ack(ioa_cfg,
2816 IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
2817 dev_err(&ioa_cfg->pdev->dev,
2818 "IOA dump short data transfer timeout\n");
2819 return -EIO;
2820 }
2821
2822 /* Read data from mailbox and increment destination pointer */
2823 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
2824 dest++;
2825
2826 /* For all but the last word of data, signal data received */
2827 if (i < (length_in_words - 1)) {
2828 /* Signal dump data received - Clear IO debug Ack */
2829 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2830 ioa_cfg->regs.clr_interrupt_reg);
2831 }
2832 }
2833
2834 /* Signal end of block transfer. Set reset alert then clear IO debug ack */
2835 writel(IPR_UPROCI_RESET_ALERT,
214777ba 2836 ioa_cfg->regs.set_uproc_interrupt_reg32);
1da177e4
LT
2837
2838 writel(IPR_UPROCI_IO_DEBUG_ALERT,
214777ba 2839 ioa_cfg->regs.clr_uproc_interrupt_reg32);
1da177e4
LT
2840
2841 /* Signal dump data received - Clear IO debug Ack */
2842 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2843 ioa_cfg->regs.clr_interrupt_reg);
2844
2845 /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
2846 while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
2847 temp_pcii_reg =
214777ba 2848 readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
1da177e4
LT
2849
2850 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
2851 return 0;
2852
2853 udelay(10);
2854 delay += 10;
2855 }
2856
2857 return 0;
2858}
2859
2860#ifdef CONFIG_SCSI_IPR_DUMP
2861/**
2862 * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
2863 * @ioa_cfg: ioa config struct
2864 * @pci_address: adapter address
2865 * @length: length of data to copy
2866 *
2867 * Copy data from PCI adapter to kernel buffer.
2868 * Note: length MUST be a 4 byte multiple
2869 * Return value:
2870 * 0 on success / other on failure
2871 **/
2872static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
2873 unsigned long pci_address, u32 length)
2874{
2875 int bytes_copied = 0;
4d4dd706 2876 int cur_len, rc, rem_len, rem_page_len, max_dump_size;
1da177e4
LT
2877 __be32 *page;
2878 unsigned long lock_flags = 0;
2879 struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
2880
4d4dd706
KSS
2881 if (ioa_cfg->sis64)
2882 max_dump_size = IPR_FMT3_MAX_IOA_DUMP_SIZE;
2883 else
2884 max_dump_size = IPR_FMT2_MAX_IOA_DUMP_SIZE;
2885
1da177e4 2886 while (bytes_copied < length &&
4d4dd706 2887 (ioa_dump->hdr.len + bytes_copied) < max_dump_size) {
1da177e4
LT
2888 if (ioa_dump->page_offset >= PAGE_SIZE ||
2889 ioa_dump->page_offset == 0) {
2890 page = (__be32 *)__get_free_page(GFP_ATOMIC);
2891
2892 if (!page) {
2893 ipr_trace;
2894 return bytes_copied;
2895 }
2896
2897 ioa_dump->page_offset = 0;
2898 ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
2899 ioa_dump->next_page_index++;
2900 } else
2901 page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
2902
2903 rem_len = length - bytes_copied;
2904 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
2905 cur_len = min(rem_len, rem_page_len);
2906
2907 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2908 if (ioa_cfg->sdt_state == ABORT_DUMP) {
2909 rc = -EIO;
2910 } else {
2911 rc = ipr_get_ldump_data_section(ioa_cfg,
2912 pci_address + bytes_copied,
2913 &page[ioa_dump->page_offset / 4],
2914 (cur_len / sizeof(u32)));
2915 }
2916 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2917
2918 if (!rc) {
2919 ioa_dump->page_offset += cur_len;
2920 bytes_copied += cur_len;
2921 } else {
2922 ipr_trace;
2923 break;
2924 }
2925 schedule();
2926 }
2927
2928 return bytes_copied;
2929}
2930
2931/**
2932 * ipr_init_dump_entry_hdr - Initialize a dump entry header.
2933 * @hdr: dump entry header struct
2934 *
2935 * Return value:
2936 * nothing
2937 **/
2938static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
2939{
2940 hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
2941 hdr->num_elems = 1;
2942 hdr->offset = sizeof(*hdr);
2943 hdr->status = IPR_DUMP_STATUS_SUCCESS;
2944}
2945
2946/**
2947 * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
2948 * @ioa_cfg: ioa config struct
2949 * @driver_dump: driver dump struct
2950 *
2951 * Return value:
2952 * nothing
2953 **/
2954static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
2955 struct ipr_driver_dump *driver_dump)
2956{
2957 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2958
2959 ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
2960 driver_dump->ioa_type_entry.hdr.len =
2961 sizeof(struct ipr_dump_ioa_type_entry) -
2962 sizeof(struct ipr_dump_entry_header);
2963 driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2964 driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
2965 driver_dump->ioa_type_entry.type = ioa_cfg->type;
2966 driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
2967 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
2968 ucode_vpd->minor_release[1];
2969 driver_dump->hdr.num_entries++;
2970}
2971
2972/**
2973 * ipr_dump_version_data - Fill in the driver version in the dump.
2974 * @ioa_cfg: ioa config struct
2975 * @driver_dump: driver dump struct
2976 *
2977 * Return value:
2978 * nothing
2979 **/
2980static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
2981 struct ipr_driver_dump *driver_dump)
2982{
2983 ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
2984 driver_dump->version_entry.hdr.len =
2985 sizeof(struct ipr_dump_version_entry) -
2986 sizeof(struct ipr_dump_entry_header);
2987 driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2988 driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
2989 strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
2990 driver_dump->hdr.num_entries++;
2991}
2992
2993/**
2994 * ipr_dump_trace_data - Fill in the IOA trace in the dump.
2995 * @ioa_cfg: ioa config struct
2996 * @driver_dump: driver dump struct
2997 *
2998 * Return value:
2999 * nothing
3000 **/
3001static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
3002 struct ipr_driver_dump *driver_dump)
3003{
3004 ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
3005 driver_dump->trace_entry.hdr.len =
3006 sizeof(struct ipr_dump_trace_entry) -
3007 sizeof(struct ipr_dump_entry_header);
3008 driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
3009 driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
3010 memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
3011 driver_dump->hdr.num_entries++;
3012}
3013
3014/**
3015 * ipr_dump_location_data - Fill in the IOA location in the dump.
3016 * @ioa_cfg: ioa config struct
3017 * @driver_dump: driver dump struct
3018 *
3019 * Return value:
3020 * nothing
3021 **/
3022static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
3023 struct ipr_driver_dump *driver_dump)
3024{
3025 ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
3026 driver_dump->location_entry.hdr.len =
3027 sizeof(struct ipr_dump_location_entry) -
3028 sizeof(struct ipr_dump_entry_header);
3029 driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
3030 driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
71610f55 3031 strcpy(driver_dump->location_entry.location, dev_name(&ioa_cfg->pdev->dev));
1da177e4
LT
3032 driver_dump->hdr.num_entries++;
3033}
3034
3035/**
3036 * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
3037 * @ioa_cfg: ioa config struct
3038 * @dump: dump struct
3039 *
3040 * Return value:
3041 * nothing
3042 **/
3043static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
3044{
3045 unsigned long start_addr, sdt_word;
3046 unsigned long lock_flags = 0;
3047 struct ipr_driver_dump *driver_dump = &dump->driver_dump;
3048 struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
4d4dd706
KSS
3049 u32 num_entries, max_num_entries, start_off, end_off;
3050 u32 max_dump_size, bytes_to_copy, bytes_copied, rc;
1da177e4 3051 struct ipr_sdt *sdt;
dcbad00e 3052 int valid = 1;
1da177e4
LT
3053 int i;
3054
3055 ENTER;
3056
3057 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3058
41e9a696 3059 if (ioa_cfg->sdt_state != READ_DUMP) {
1da177e4
LT
3060 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3061 return;
3062 }
3063
110def85
WB
3064 if (ioa_cfg->sis64) {
3065 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3066 ssleep(IPR_DUMP_DELAY_SECONDS);
3067 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3068 }
3069
1da177e4
LT
3070 start_addr = readl(ioa_cfg->ioa_mailbox);
3071
dcbad00e 3072 if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(start_addr)) {
1da177e4
LT
3073 dev_err(&ioa_cfg->pdev->dev,
3074 "Invalid dump table format: %lx\n", start_addr);
3075 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3076 return;
3077 }
3078
3079 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
3080
3081 driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
3082
3083 /* Initialize the overall dump header */
3084 driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
3085 driver_dump->hdr.num_entries = 1;
3086 driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
3087 driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
3088 driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
3089 driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
3090
3091 ipr_dump_version_data(ioa_cfg, driver_dump);
3092 ipr_dump_location_data(ioa_cfg, driver_dump);
3093 ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
3094 ipr_dump_trace_data(ioa_cfg, driver_dump);
3095
3096 /* Update dump_header */
3097 driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
3098
3099 /* IOA Dump entry */
3100 ipr_init_dump_entry_hdr(&ioa_dump->hdr);
1da177e4
LT
3101 ioa_dump->hdr.len = 0;
3102 ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
3103 ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
3104
3105 /* First entries in sdt are actually a list of dump addresses and
3106 lengths to gather the real dump data. sdt represents the pointer
3107 to the ioa generated dump table. Dump data will be extracted based
3108 on entries in this table */
3109 sdt = &ioa_dump->sdt;
3110
4d4dd706
KSS
3111 if (ioa_cfg->sis64) {
3112 max_num_entries = IPR_FMT3_NUM_SDT_ENTRIES;
3113 max_dump_size = IPR_FMT3_MAX_IOA_DUMP_SIZE;
3114 } else {
3115 max_num_entries = IPR_FMT2_NUM_SDT_ENTRIES;
3116 max_dump_size = IPR_FMT2_MAX_IOA_DUMP_SIZE;
3117 }
3118
3119 bytes_to_copy = offsetof(struct ipr_sdt, entry) +
3120 (max_num_entries * sizeof(struct ipr_sdt_entry));
1da177e4 3121 rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
4d4dd706 3122 bytes_to_copy / sizeof(__be32));
1da177e4
LT
3123
3124 /* Smart Dump table is ready to use and the first entry is valid */
dcbad00e
WB
3125 if (rc || ((be32_to_cpu(sdt->hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
3126 (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
1da177e4
LT
3127 dev_err(&ioa_cfg->pdev->dev,
3128 "Dump of IOA failed. Dump table not valid: %d, %X.\n",
3129 rc, be32_to_cpu(sdt->hdr.state));
3130 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
3131 ioa_cfg->sdt_state = DUMP_OBTAINED;
3132 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3133 return;
3134 }
3135
3136 num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
3137
4d4dd706
KSS
3138 if (num_entries > max_num_entries)
3139 num_entries = max_num_entries;
3140
3141 /* Update dump length to the actual data to be copied */
3142 dump->driver_dump.hdr.len += sizeof(struct ipr_sdt_header);
3143 if (ioa_cfg->sis64)
3144 dump->driver_dump.hdr.len += num_entries * sizeof(struct ipr_sdt_entry);
3145 else
3146 dump->driver_dump.hdr.len += max_num_entries * sizeof(struct ipr_sdt_entry);
1da177e4
LT
3147
3148 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3149
3150 for (i = 0; i < num_entries; i++) {
4d4dd706 3151 if (ioa_dump->hdr.len > max_dump_size) {
1da177e4
LT
3152 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
3153 break;
3154 }
3155
3156 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
dcbad00e
WB
3157 sdt_word = be32_to_cpu(sdt->entry[i].start_token);
3158 if (ioa_cfg->sis64)
3159 bytes_to_copy = be32_to_cpu(sdt->entry[i].end_token);
3160 else {
3161 start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
3162 end_off = be32_to_cpu(sdt->entry[i].end_token);
3163
3164 if (ipr_sdt_is_fmt2(sdt_word) && sdt_word)
3165 bytes_to_copy = end_off - start_off;
3166 else
3167 valid = 0;
3168 }
3169 if (valid) {
4d4dd706 3170 if (bytes_to_copy > max_dump_size) {
1da177e4
LT
3171 sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
3172 continue;
3173 }
3174
3175 /* Copy data from adapter to driver buffers */
3176 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
3177 bytes_to_copy);
3178
3179 ioa_dump->hdr.len += bytes_copied;
3180
3181 if (bytes_copied != bytes_to_copy) {
3182 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
3183 break;
3184 }
3185 }
3186 }
3187 }
3188
3189 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
3190
3191 /* Update dump_header */
3192 driver_dump->hdr.len += ioa_dump->hdr.len;
3193 wmb();
3194 ioa_cfg->sdt_state = DUMP_OBTAINED;
3195 LEAVE;
3196}
3197
3198#else
203fa3fe 3199#define ipr_get_ioa_dump(ioa_cfg, dump) do { } while (0)
1da177e4
LT
3200#endif
3201
3202/**
3203 * ipr_release_dump - Free adapter dump memory
3204 * @kref: kref struct
3205 *
3206 * Return value:
3207 * nothing
3208 **/
3209static void ipr_release_dump(struct kref *kref)
3210{
203fa3fe 3211 struct ipr_dump *dump = container_of(kref, struct ipr_dump, kref);
1da177e4
LT
3212 struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
3213 unsigned long lock_flags = 0;
3214 int i;
3215
3216 ENTER;
3217 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3218 ioa_cfg->dump = NULL;
3219 ioa_cfg->sdt_state = INACTIVE;
3220 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3221
3222 for (i = 0; i < dump->ioa_dump.next_page_index; i++)
3223 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
3224
4d4dd706 3225 vfree(dump->ioa_dump.ioa_data);
1da177e4
LT
3226 kfree(dump);
3227 LEAVE;
3228}
3229
3230/**
3231 * ipr_worker_thread - Worker thread
c4028958 3232 * @work: ioa config struct
1da177e4
LT
3233 *
3234 * Called at task level from a work thread. This function takes care
3235 * of adding and removing device from the mid-layer as configuration
3236 * changes are detected by the adapter.
3237 *
3238 * Return value:
3239 * nothing
3240 **/
c4028958 3241static void ipr_worker_thread(struct work_struct *work)
1da177e4
LT
3242{
3243 unsigned long lock_flags;
3244 struct ipr_resource_entry *res;
3245 struct scsi_device *sdev;
3246 struct ipr_dump *dump;
c4028958
DH
3247 struct ipr_ioa_cfg *ioa_cfg =
3248 container_of(work, struct ipr_ioa_cfg, work_q);
1da177e4
LT
3249 u8 bus, target, lun;
3250 int did_work;
3251
3252 ENTER;
3253 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3254
41e9a696 3255 if (ioa_cfg->sdt_state == READ_DUMP) {
1da177e4
LT
3256 dump = ioa_cfg->dump;
3257 if (!dump) {
3258 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3259 return;
3260 }
3261 kref_get(&dump->kref);
3262 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3263 ipr_get_ioa_dump(ioa_cfg, dump);
3264 kref_put(&dump->kref, ipr_release_dump);
3265
3266 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4c647e90 3267 if (ioa_cfg->sdt_state == DUMP_OBTAINED && !ioa_cfg->dump_timeout)
1da177e4
LT
3268 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3269 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3270 return;
3271 }
3272
3273restart:
3274 do {
3275 did_work = 0;
56d6aa33 3276 if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds ||
3277 !ioa_cfg->allow_ml_add_del) {
1da177e4
LT
3278 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3279 return;
3280 }
3281
3282 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3283 if (res->del_from_ml && res->sdev) {
3284 did_work = 1;
3285 sdev = res->sdev;
3286 if (!scsi_device_get(sdev)) {
5767a1c4
KSS
3287 if (!res->add_to_ml)
3288 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
3289 else
3290 res->del_from_ml = 0;
1da177e4
LT
3291 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3292 scsi_remove_device(sdev);
3293 scsi_device_put(sdev);
3294 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3295 }
3296 break;
3297 }
3298 }
203fa3fe 3299 } while (did_work);
1da177e4
LT
3300
3301 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3302 if (res->add_to_ml) {
3e7ebdfa
WB
3303 bus = res->bus;
3304 target = res->target;
3305 lun = res->lun;
1121b794 3306 res->add_to_ml = 0;
1da177e4
LT
3307 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3308 scsi_add_device(ioa_cfg->host, bus, target, lun);
3309 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3310 goto restart;
3311 }
3312 }
3313
3314 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
ee959b00 3315 kobject_uevent(&ioa_cfg->host->shost_dev.kobj, KOBJ_CHANGE);
1da177e4
LT
3316 LEAVE;
3317}
3318
3319#ifdef CONFIG_SCSI_IPR_TRACE
3320/**
3321 * ipr_read_trace - Dump the adapter trace
2c3c8bea 3322 * @filp: open sysfs file
1da177e4 3323 * @kobj: kobject struct
91a69029 3324 * @bin_attr: bin_attribute struct
1da177e4
LT
3325 * @buf: buffer
3326 * @off: offset
3327 * @count: buffer size
3328 *
3329 * Return value:
3330 * number of bytes printed to buffer
3331 **/
2c3c8bea 3332static ssize_t ipr_read_trace(struct file *filp, struct kobject *kobj,
91a69029
ZR
3333 struct bin_attribute *bin_attr,
3334 char *buf, loff_t off, size_t count)
1da177e4 3335{
ee959b00
TJ
3336 struct device *dev = container_of(kobj, struct device, kobj);
3337 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3338 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3339 unsigned long lock_flags = 0;
d777aaf3 3340 ssize_t ret;
1da177e4
LT
3341
3342 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
d777aaf3
AM
3343 ret = memory_read_from_buffer(buf, count, &off, ioa_cfg->trace,
3344 IPR_TRACE_SIZE);
1da177e4 3345 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
d777aaf3
AM
3346
3347 return ret;
1da177e4
LT
3348}
3349
3350static struct bin_attribute ipr_trace_attr = {
3351 .attr = {
3352 .name = "trace",
3353 .mode = S_IRUGO,
3354 },
3355 .size = 0,
3356 .read = ipr_read_trace,
3357};
3358#endif
3359
3360/**
3361 * ipr_show_fw_version - Show the firmware version
ee959b00
TJ
3362 * @dev: class device struct
3363 * @buf: buffer
1da177e4
LT
3364 *
3365 * Return value:
3366 * number of bytes printed to buffer
3367 **/
ee959b00
TJ
3368static ssize_t ipr_show_fw_version(struct device *dev,
3369 struct device_attribute *attr, char *buf)
1da177e4 3370{
ee959b00 3371 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3372 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3373 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
3374 unsigned long lock_flags = 0;
3375 int len;
3376
3377 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3378 len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
3379 ucode_vpd->major_release, ucode_vpd->card_type,
3380 ucode_vpd->minor_release[0],
3381 ucode_vpd->minor_release[1]);
3382 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3383 return len;
3384}
3385
ee959b00 3386static struct device_attribute ipr_fw_version_attr = {
1da177e4
LT
3387 .attr = {
3388 .name = "fw_version",
3389 .mode = S_IRUGO,
3390 },
3391 .show = ipr_show_fw_version,
3392};
3393
3394/**
3395 * ipr_show_log_level - Show the adapter's error logging level
ee959b00
TJ
3396 * @dev: class device struct
3397 * @buf: buffer
1da177e4
LT
3398 *
3399 * Return value:
3400 * number of bytes printed to buffer
3401 **/
ee959b00
TJ
3402static ssize_t ipr_show_log_level(struct device *dev,
3403 struct device_attribute *attr, char *buf)
1da177e4 3404{
ee959b00 3405 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3406 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3407 unsigned long lock_flags = 0;
3408 int len;
3409
3410 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3411 len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
3412 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3413 return len;
3414}
3415
3416/**
3417 * ipr_store_log_level - Change the adapter's error logging level
ee959b00
TJ
3418 * @dev: class device struct
3419 * @buf: buffer
1da177e4
LT
3420 *
3421 * Return value:
3422 * number of bytes printed to buffer
3423 **/
ee959b00 3424static ssize_t ipr_store_log_level(struct device *dev,
203fa3fe 3425 struct device_attribute *attr,
1da177e4
LT
3426 const char *buf, size_t count)
3427{
ee959b00 3428 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3429 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3430 unsigned long lock_flags = 0;
3431
3432 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3433 ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
3434 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3435 return strlen(buf);
3436}
3437
ee959b00 3438static struct device_attribute ipr_log_level_attr = {
1da177e4
LT
3439 .attr = {
3440 .name = "log_level",
3441 .mode = S_IRUGO | S_IWUSR,
3442 },
3443 .show = ipr_show_log_level,
3444 .store = ipr_store_log_level
3445};
3446
3447/**
3448 * ipr_store_diagnostics - IOA Diagnostics interface
ee959b00
TJ
3449 * @dev: device struct
3450 * @buf: buffer
3451 * @count: buffer size
1da177e4
LT
3452 *
3453 * This function will reset the adapter and wait a reasonable
3454 * amount of time for any errors that the adapter might log.
3455 *
3456 * Return value:
3457 * count on success / other on failure
3458 **/
ee959b00
TJ
3459static ssize_t ipr_store_diagnostics(struct device *dev,
3460 struct device_attribute *attr,
1da177e4
LT
3461 const char *buf, size_t count)
3462{
ee959b00 3463 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3464 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3465 unsigned long lock_flags = 0;
3466 int rc = count;
3467
3468 if (!capable(CAP_SYS_ADMIN))
3469 return -EACCES;
3470
1da177e4 3471 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
203fa3fe 3472 while (ioa_cfg->in_reset_reload) {
970ea294
BK
3473 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3474 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3475 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3476 }
3477
1da177e4
LT
3478 ioa_cfg->errors_logged = 0;
3479 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3480
3481 if (ioa_cfg->in_reset_reload) {
3482 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3483 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3484
3485 /* Wait for a second for any errors to be logged */
3486 msleep(1000);
3487 } else {
3488 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3489 return -EIO;
3490 }
3491
3492 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3493 if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
3494 rc = -EIO;
3495 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3496
3497 return rc;
3498}
3499
ee959b00 3500static struct device_attribute ipr_diagnostics_attr = {
1da177e4
LT
3501 .attr = {
3502 .name = "run_diagnostics",
3503 .mode = S_IWUSR,
3504 },
3505 .store = ipr_store_diagnostics
3506};
3507
f37eb54b
BK
3508/**
3509 * ipr_show_adapter_state - Show the adapter's state
ee959b00
TJ
3510 * @class_dev: device struct
3511 * @buf: buffer
f37eb54b
BK
3512 *
3513 * Return value:
3514 * number of bytes printed to buffer
3515 **/
ee959b00
TJ
3516static ssize_t ipr_show_adapter_state(struct device *dev,
3517 struct device_attribute *attr, char *buf)
f37eb54b 3518{
ee959b00 3519 struct Scsi_Host *shost = class_to_shost(dev);
f37eb54b
BK
3520 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3521 unsigned long lock_flags = 0;
3522 int len;
3523
3524 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
56d6aa33 3525 if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
f37eb54b
BK
3526 len = snprintf(buf, PAGE_SIZE, "offline\n");
3527 else
3528 len = snprintf(buf, PAGE_SIZE, "online\n");
3529 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3530 return len;
3531}
3532
3533/**
3534 * ipr_store_adapter_state - Change adapter state
ee959b00
TJ
3535 * @dev: device struct
3536 * @buf: buffer
3537 * @count: buffer size
f37eb54b
BK
3538 *
3539 * This function will change the adapter's state.
3540 *
3541 * Return value:
3542 * count on success / other on failure
3543 **/
ee959b00
TJ
3544static ssize_t ipr_store_adapter_state(struct device *dev,
3545 struct device_attribute *attr,
f37eb54b
BK
3546 const char *buf, size_t count)
3547{
ee959b00 3548 struct Scsi_Host *shost = class_to_shost(dev);
f37eb54b
BK
3549 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3550 unsigned long lock_flags;
56d6aa33 3551 int result = count, i;
f37eb54b
BK
3552
3553 if (!capable(CAP_SYS_ADMIN))
3554 return -EACCES;
3555
3556 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
56d6aa33 3557 if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead &&
3558 !strncmp(buf, "online", 6)) {
3559 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
3560 spin_lock(&ioa_cfg->hrrq[i]._lock);
3561 ioa_cfg->hrrq[i].ioa_is_dead = 0;
3562 spin_unlock(&ioa_cfg->hrrq[i]._lock);
3563 }
3564 wmb();
f37eb54b
BK
3565 ioa_cfg->reset_retries = 0;
3566 ioa_cfg->in_ioa_bringdown = 0;
3567 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3568 }
3569 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3570 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3571
3572 return result;
3573}
3574
ee959b00 3575static struct device_attribute ipr_ioa_state_attr = {
f37eb54b 3576 .attr = {
49dd0961 3577 .name = "online_state",
f37eb54b
BK
3578 .mode = S_IRUGO | S_IWUSR,
3579 },
3580 .show = ipr_show_adapter_state,
3581 .store = ipr_store_adapter_state
3582};
3583
1da177e4
LT
3584/**
3585 * ipr_store_reset_adapter - Reset the adapter
ee959b00
TJ
3586 * @dev: device struct
3587 * @buf: buffer
3588 * @count: buffer size
1da177e4
LT
3589 *
3590 * This function will reset the adapter.
3591 *
3592 * Return value:
3593 * count on success / other on failure
3594 **/
ee959b00
TJ
3595static ssize_t ipr_store_reset_adapter(struct device *dev,
3596 struct device_attribute *attr,
1da177e4
LT
3597 const char *buf, size_t count)
3598{
ee959b00 3599 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3600 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3601 unsigned long lock_flags;
3602 int result = count;
3603
3604 if (!capable(CAP_SYS_ADMIN))
3605 return -EACCES;
3606
3607 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3608 if (!ioa_cfg->in_reset_reload)
3609 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3610 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3611 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3612
3613 return result;
3614}
3615
ee959b00 3616static struct device_attribute ipr_ioa_reset_attr = {
1da177e4
LT
3617 .attr = {
3618 .name = "reset_host",
3619 .mode = S_IWUSR,
3620 },
3621 .store = ipr_store_reset_adapter
3622};
3623
b53d124a 3624static int ipr_iopoll(struct blk_iopoll *iop, int budget);
3625 /**
3626 * ipr_show_iopoll_weight - Show ipr polling mode
3627 * @dev: class device struct
3628 * @buf: buffer
3629 *
3630 * Return value:
3631 * number of bytes printed to buffer
3632 **/
3633static ssize_t ipr_show_iopoll_weight(struct device *dev,
3634 struct device_attribute *attr, char *buf)
3635{
3636 struct Scsi_Host *shost = class_to_shost(dev);
3637 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3638 unsigned long lock_flags = 0;
3639 int len;
3640
3641 spin_lock_irqsave(shost->host_lock, lock_flags);
3642 len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->iopoll_weight);
3643 spin_unlock_irqrestore(shost->host_lock, lock_flags);
3644
3645 return len;
3646}
3647
3648/**
3649 * ipr_store_iopoll_weight - Change the adapter's polling mode
3650 * @dev: class device struct
3651 * @buf: buffer
3652 *
3653 * Return value:
3654 * number of bytes printed to buffer
3655 **/
3656static ssize_t ipr_store_iopoll_weight(struct device *dev,
3657 struct device_attribute *attr,
3658 const char *buf, size_t count)
3659{
3660 struct Scsi_Host *shost = class_to_shost(dev);
3661 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3662 unsigned long user_iopoll_weight;
3663 unsigned long lock_flags = 0;
3664 int i;
3665
3666 if (!ioa_cfg->sis64) {
3667 dev_info(&ioa_cfg->pdev->dev, "blk-iopoll not supported on this adapter\n");
3668 return -EINVAL;
3669 }
3670 if (kstrtoul(buf, 10, &user_iopoll_weight))
3671 return -EINVAL;
3672
3673 if (user_iopoll_weight > 256) {
3674 dev_info(&ioa_cfg->pdev->dev, "Invalid blk-iopoll weight. It must be less than 256\n");
3675 return -EINVAL;
3676 }
3677
3678 if (user_iopoll_weight == ioa_cfg->iopoll_weight) {
3679 dev_info(&ioa_cfg->pdev->dev, "Current blk-iopoll weight has the same weight\n");
3680 return strlen(buf);
3681 }
3682
89f8b33c 3683 if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
b53d124a 3684 for (i = 1; i < ioa_cfg->hrrq_num; i++)
3685 blk_iopoll_disable(&ioa_cfg->hrrq[i].iopoll);
3686 }
3687
3688 spin_lock_irqsave(shost->host_lock, lock_flags);
3689 ioa_cfg->iopoll_weight = user_iopoll_weight;
89f8b33c 3690 if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
b53d124a 3691 for (i = 1; i < ioa_cfg->hrrq_num; i++) {
3692 blk_iopoll_init(&ioa_cfg->hrrq[i].iopoll,
3693 ioa_cfg->iopoll_weight, ipr_iopoll);
3694 blk_iopoll_enable(&ioa_cfg->hrrq[i].iopoll);
3695 }
3696 }
3697 spin_unlock_irqrestore(shost->host_lock, lock_flags);
3698
3699 return strlen(buf);
3700}
3701
3702static struct device_attribute ipr_iopoll_weight_attr = {
3703 .attr = {
3704 .name = "iopoll_weight",
3705 .mode = S_IRUGO | S_IWUSR,
3706 },
3707 .show = ipr_show_iopoll_weight,
3708 .store = ipr_store_iopoll_weight
3709};
3710
1da177e4
LT
3711/**
3712 * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
3713 * @buf_len: buffer length
3714 *
3715 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3716 * list to use for microcode download
3717 *
3718 * Return value:
3719 * pointer to sglist / NULL on failure
3720 **/
3721static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
3722{
3723 int sg_size, order, bsize_elem, num_elem, i, j;
3724 struct ipr_sglist *sglist;
3725 struct scatterlist *scatterlist;
3726 struct page *page;
3727
3728 /* Get the minimum size per scatter/gather element */
3729 sg_size = buf_len / (IPR_MAX_SGLIST - 1);
3730
3731 /* Get the actual size per element */
3732 order = get_order(sg_size);
3733
3734 /* Determine the actual number of bytes per element */
3735 bsize_elem = PAGE_SIZE * (1 << order);
3736
3737 /* Determine the actual number of sg entries needed */
3738 if (buf_len % bsize_elem)
3739 num_elem = (buf_len / bsize_elem) + 1;
3740 else
3741 num_elem = buf_len / bsize_elem;
3742
3743 /* Allocate a scatter/gather list for the DMA */
0bc42e35 3744 sglist = kzalloc(sizeof(struct ipr_sglist) +
1da177e4
LT
3745 (sizeof(struct scatterlist) * (num_elem - 1)),
3746 GFP_KERNEL);
3747
3748 if (sglist == NULL) {
3749 ipr_trace;
3750 return NULL;
3751 }
3752
1da177e4 3753 scatterlist = sglist->scatterlist;
45711f1a 3754 sg_init_table(scatterlist, num_elem);
1da177e4
LT
3755
3756 sglist->order = order;
3757 sglist->num_sg = num_elem;
3758
3759 /* Allocate a bunch of sg elements */
3760 for (i = 0; i < num_elem; i++) {
3761 page = alloc_pages(GFP_KERNEL, order);
3762 if (!page) {
3763 ipr_trace;
3764
3765 /* Free up what we already allocated */
3766 for (j = i - 1; j >= 0; j--)
45711f1a 3767 __free_pages(sg_page(&scatterlist[j]), order);
1da177e4
LT
3768 kfree(sglist);
3769 return NULL;
3770 }
3771
642f1490 3772 sg_set_page(&scatterlist[i], page, 0, 0);
1da177e4
LT
3773 }
3774
3775 return sglist;
3776}
3777
3778/**
3779 * ipr_free_ucode_buffer - Frees a microcode download buffer
3780 * @p_dnld: scatter/gather list pointer
3781 *
3782 * Free a DMA'able ucode download buffer previously allocated with
3783 * ipr_alloc_ucode_buffer
3784 *
3785 * Return value:
3786 * nothing
3787 **/
3788static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
3789{
3790 int i;
3791
3792 for (i = 0; i < sglist->num_sg; i++)
45711f1a 3793 __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
1da177e4
LT
3794
3795 kfree(sglist);
3796}
3797
3798/**
3799 * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
3800 * @sglist: scatter/gather list pointer
3801 * @buffer: buffer pointer
3802 * @len: buffer length
3803 *
3804 * Copy a microcode image from a user buffer into a buffer allocated by
3805 * ipr_alloc_ucode_buffer
3806 *
3807 * Return value:
3808 * 0 on success / other on failure
3809 **/
3810static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
3811 u8 *buffer, u32 len)
3812{
3813 int bsize_elem, i, result = 0;
3814 struct scatterlist *scatterlist;
3815 void *kaddr;
3816
3817 /* Determine the actual number of bytes per element */
3818 bsize_elem = PAGE_SIZE * (1 << sglist->order);
3819
3820 scatterlist = sglist->scatterlist;
3821
3822 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
45711f1a
JA
3823 struct page *page = sg_page(&scatterlist[i]);
3824
3825 kaddr = kmap(page);
1da177e4 3826 memcpy(kaddr, buffer, bsize_elem);
45711f1a 3827 kunmap(page);
1da177e4
LT
3828
3829 scatterlist[i].length = bsize_elem;
3830
3831 if (result != 0) {
3832 ipr_trace;
3833 return result;
3834 }
3835 }
3836
3837 if (len % bsize_elem) {
45711f1a
JA
3838 struct page *page = sg_page(&scatterlist[i]);
3839
3840 kaddr = kmap(page);
1da177e4 3841 memcpy(kaddr, buffer, len % bsize_elem);
45711f1a 3842 kunmap(page);
1da177e4
LT
3843
3844 scatterlist[i].length = len % bsize_elem;
3845 }
3846
3847 sglist->buffer_len = len;
3848 return result;
3849}
3850
a32c055f
WB
3851/**
3852 * ipr_build_ucode_ioadl64 - Build a microcode download IOADL
3853 * @ipr_cmd: ipr command struct
3854 * @sglist: scatter/gather list
3855 *
3856 * Builds a microcode download IOA data list (IOADL).
3857 *
3858 **/
3859static void ipr_build_ucode_ioadl64(struct ipr_cmnd *ipr_cmd,
3860 struct ipr_sglist *sglist)
3861{
3862 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3863 struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
3864 struct scatterlist *scatterlist = sglist->scatterlist;
3865 int i;
3866
3867 ipr_cmd->dma_use_sg = sglist->num_dma_sg;
3868 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3869 ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3870
3871 ioarcb->ioadl_len =
3872 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
3873 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3874 ioadl64[i].flags = cpu_to_be32(IPR_IOADL_FLAGS_WRITE);
3875 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(&scatterlist[i]));
3876 ioadl64[i].address = cpu_to_be64(sg_dma_address(&scatterlist[i]));
3877 }
3878
3879 ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3880}
3881
1da177e4 3882/**
12baa420 3883 * ipr_build_ucode_ioadl - Build a microcode download IOADL
1da177e4
LT
3884 * @ipr_cmd: ipr command struct
3885 * @sglist: scatter/gather list
1da177e4 3886 *
12baa420 3887 * Builds a microcode download IOA data list (IOADL).
1da177e4 3888 *
1da177e4 3889 **/
12baa420
BK
3890static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
3891 struct ipr_sglist *sglist)
1da177e4 3892{
1da177e4 3893 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
a32c055f 3894 struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
1da177e4
LT
3895 struct scatterlist *scatterlist = sglist->scatterlist;
3896 int i;
3897
12baa420 3898 ipr_cmd->dma_use_sg = sglist->num_dma_sg;
1da177e4 3899 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
a32c055f
WB
3900 ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3901
3902 ioarcb->ioadl_len =
1da177e4
LT
3903 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3904
3905 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3906 ioadl[i].flags_and_data_len =
3907 cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
3908 ioadl[i].address =
3909 cpu_to_be32(sg_dma_address(&scatterlist[i]));
3910 }
3911
12baa420
BK
3912 ioadl[i-1].flags_and_data_len |=
3913 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3914}
3915
3916/**
3917 * ipr_update_ioa_ucode - Update IOA's microcode
3918 * @ioa_cfg: ioa config struct
3919 * @sglist: scatter/gather list
3920 *
3921 * Initiate an adapter reset to update the IOA's microcode
3922 *
3923 * Return value:
3924 * 0 on success / -EIO on failure
3925 **/
3926static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
3927 struct ipr_sglist *sglist)
3928{
3929 unsigned long lock_flags;
3930
3931 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
203fa3fe 3932 while (ioa_cfg->in_reset_reload) {
970ea294
BK
3933 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3934 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3935 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3936 }
12baa420
BK
3937
3938 if (ioa_cfg->ucode_sglist) {
3939 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3940 dev_err(&ioa_cfg->pdev->dev,
3941 "Microcode download already in progress\n");
3942 return -EIO;
1da177e4 3943 }
12baa420 3944
d73341bf
AB
3945 sglist->num_dma_sg = dma_map_sg(&ioa_cfg->pdev->dev,
3946 sglist->scatterlist, sglist->num_sg,
3947 DMA_TO_DEVICE);
12baa420
BK
3948
3949 if (!sglist->num_dma_sg) {
3950 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3951 dev_err(&ioa_cfg->pdev->dev,
3952 "Failed to map microcode download buffer!\n");
1da177e4
LT
3953 return -EIO;
3954 }
3955
12baa420
BK
3956 ioa_cfg->ucode_sglist = sglist;
3957 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3958 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3959 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3960
3961 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3962 ioa_cfg->ucode_sglist = NULL;
3963 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1da177e4
LT
3964 return 0;
3965}
3966
3967/**
3968 * ipr_store_update_fw - Update the firmware on the adapter
ee959b00
TJ
3969 * @class_dev: device struct
3970 * @buf: buffer
3971 * @count: buffer size
1da177e4
LT
3972 *
3973 * This function will update the firmware on the adapter.
3974 *
3975 * Return value:
3976 * count on success / other on failure
3977 **/
ee959b00
TJ
3978static ssize_t ipr_store_update_fw(struct device *dev,
3979 struct device_attribute *attr,
3980 const char *buf, size_t count)
1da177e4 3981{
ee959b00 3982 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3983 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3984 struct ipr_ucode_image_header *image_hdr;
3985 const struct firmware *fw_entry;
3986 struct ipr_sglist *sglist;
1da177e4
LT
3987 char fname[100];
3988 char *src;
3989 int len, result, dnld_size;
3990
3991 if (!capable(CAP_SYS_ADMIN))
3992 return -EACCES;
3993
3994 len = snprintf(fname, 99, "%s", buf);
3995 fname[len-1] = '\0';
3996
203fa3fe 3997 if (request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
1da177e4
LT
3998 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
3999 return -EIO;
4000 }
4001
4002 image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
4003
1da177e4
LT
4004 src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
4005 dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
4006 sglist = ipr_alloc_ucode_buffer(dnld_size);
4007
4008 if (!sglist) {
4009 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
4010 release_firmware(fw_entry);
4011 return -ENOMEM;
4012 }
4013
4014 result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
4015
4016 if (result) {
4017 dev_err(&ioa_cfg->pdev->dev,
4018 "Microcode buffer copy to DMA buffer failed\n");
12baa420 4019 goto out;
1da177e4
LT
4020 }
4021
14ed9cc7
WB
4022 ipr_info("Updating microcode, please be patient. This may take up to 30 minutes.\n");
4023
12baa420 4024 result = ipr_update_ioa_ucode(ioa_cfg, sglist);
1da177e4 4025
12baa420
BK
4026 if (!result)
4027 result = count;
4028out:
1da177e4
LT
4029 ipr_free_ucode_buffer(sglist);
4030 release_firmware(fw_entry);
12baa420 4031 return result;
1da177e4
LT
4032}
4033
ee959b00 4034static struct device_attribute ipr_update_fw_attr = {
1da177e4
LT
4035 .attr = {
4036 .name = "update_fw",
4037 .mode = S_IWUSR,
4038 },
4039 .store = ipr_store_update_fw
4040};
4041
75576bb9
WB
4042/**
4043 * ipr_show_fw_type - Show the adapter's firmware type.
4044 * @dev: class device struct
4045 * @buf: buffer
4046 *
4047 * Return value:
4048 * number of bytes printed to buffer
4049 **/
4050static ssize_t ipr_show_fw_type(struct device *dev,
4051 struct device_attribute *attr, char *buf)
4052{
4053 struct Scsi_Host *shost = class_to_shost(dev);
4054 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
4055 unsigned long lock_flags = 0;
4056 int len;
4057
4058 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4059 len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->sis64);
4060 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4061 return len;
4062}
4063
4064static struct device_attribute ipr_ioa_fw_type_attr = {
4065 .attr = {
4066 .name = "fw_type",
4067 .mode = S_IRUGO,
4068 },
4069 .show = ipr_show_fw_type
4070};
4071
ee959b00 4072static struct device_attribute *ipr_ioa_attrs[] = {
1da177e4
LT
4073 &ipr_fw_version_attr,
4074 &ipr_log_level_attr,
4075 &ipr_diagnostics_attr,
f37eb54b 4076 &ipr_ioa_state_attr,
1da177e4
LT
4077 &ipr_ioa_reset_attr,
4078 &ipr_update_fw_attr,
75576bb9 4079 &ipr_ioa_fw_type_attr,
b53d124a 4080 &ipr_iopoll_weight_attr,
1da177e4
LT
4081 NULL,
4082};
4083
4084#ifdef CONFIG_SCSI_IPR_DUMP
4085/**
4086 * ipr_read_dump - Dump the adapter
2c3c8bea 4087 * @filp: open sysfs file
1da177e4 4088 * @kobj: kobject struct
91a69029 4089 * @bin_attr: bin_attribute struct
1da177e4
LT
4090 * @buf: buffer
4091 * @off: offset
4092 * @count: buffer size
4093 *
4094 * Return value:
4095 * number of bytes printed to buffer
4096 **/
2c3c8bea 4097static ssize_t ipr_read_dump(struct file *filp, struct kobject *kobj,
91a69029
ZR
4098 struct bin_attribute *bin_attr,
4099 char *buf, loff_t off, size_t count)
1da177e4 4100{
ee959b00 4101 struct device *cdev = container_of(kobj, struct device, kobj);
1da177e4
LT
4102 struct Scsi_Host *shost = class_to_shost(cdev);
4103 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
4104 struct ipr_dump *dump;
4105 unsigned long lock_flags = 0;
4106 char *src;
4d4dd706 4107 int len, sdt_end;
1da177e4
LT
4108 size_t rc = count;
4109
4110 if (!capable(CAP_SYS_ADMIN))
4111 return -EACCES;
4112
4113 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4114 dump = ioa_cfg->dump;
4115
4116 if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
4117 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4118 return 0;
4119 }
4120 kref_get(&dump->kref);
4121 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4122
4123 if (off > dump->driver_dump.hdr.len) {
4124 kref_put(&dump->kref, ipr_release_dump);
4125 return 0;
4126 }
4127
4128 if (off + count > dump->driver_dump.hdr.len) {
4129 count = dump->driver_dump.hdr.len - off;
4130 rc = count;
4131 }
4132
4133 if (count && off < sizeof(dump->driver_dump)) {
4134 if (off + count > sizeof(dump->driver_dump))
4135 len = sizeof(dump->driver_dump) - off;
4136 else
4137 len = count;
4138 src = (u8 *)&dump->driver_dump + off;
4139 memcpy(buf, src, len);
4140 buf += len;
4141 off += len;
4142 count -= len;
4143 }
4144
4145 off -= sizeof(dump->driver_dump);
4146
4d4dd706
KSS
4147 if (ioa_cfg->sis64)
4148 sdt_end = offsetof(struct ipr_ioa_dump, sdt.entry) +
4149 (be32_to_cpu(dump->ioa_dump.sdt.hdr.num_entries_used) *
4150 sizeof(struct ipr_sdt_entry));
4151 else
4152 sdt_end = offsetof(struct ipr_ioa_dump, sdt.entry) +
4153 (IPR_FMT2_NUM_SDT_ENTRIES * sizeof(struct ipr_sdt_entry));
4154
4155 if (count && off < sdt_end) {
4156 if (off + count > sdt_end)
4157 len = sdt_end - off;
1da177e4
LT
4158 else
4159 len = count;
4160 src = (u8 *)&dump->ioa_dump + off;
4161 memcpy(buf, src, len);
4162 buf += len;
4163 off += len;
4164 count -= len;
4165 }
4166
4d4dd706 4167 off -= sdt_end;
1da177e4
LT
4168
4169 while (count) {
4170 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
4171 len = PAGE_ALIGN(off) - off;
4172 else
4173 len = count;
4174 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
4175 src += off & ~PAGE_MASK;
4176 memcpy(buf, src, len);
4177 buf += len;
4178 off += len;
4179 count -= len;
4180 }
4181
4182 kref_put(&dump->kref, ipr_release_dump);
4183 return rc;
4184}
4185
4186/**
4187 * ipr_alloc_dump - Prepare for adapter dump
4188 * @ioa_cfg: ioa config struct
4189 *
4190 * Return value:
4191 * 0 on success / other on failure
4192 **/
4193static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
4194{
4195 struct ipr_dump *dump;
4d4dd706 4196 __be32 **ioa_data;
1da177e4
LT
4197 unsigned long lock_flags = 0;
4198
0bc42e35 4199 dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
1da177e4
LT
4200
4201 if (!dump) {
4202 ipr_err("Dump memory allocation failed\n");
4203 return -ENOMEM;
4204 }
4205
4d4dd706
KSS
4206 if (ioa_cfg->sis64)
4207 ioa_data = vmalloc(IPR_FMT3_MAX_NUM_DUMP_PAGES * sizeof(__be32 *));
4208 else
4209 ioa_data = vmalloc(IPR_FMT2_MAX_NUM_DUMP_PAGES * sizeof(__be32 *));
4210
4211 if (!ioa_data) {
4212 ipr_err("Dump memory allocation failed\n");
4213 kfree(dump);
4214 return -ENOMEM;
4215 }
4216
4217 dump->ioa_dump.ioa_data = ioa_data;
4218
1da177e4
LT
4219 kref_init(&dump->kref);
4220 dump->ioa_cfg = ioa_cfg;
4221
4222 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4223
4224 if (INACTIVE != ioa_cfg->sdt_state) {
4225 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4d4dd706 4226 vfree(dump->ioa_dump.ioa_data);
1da177e4
LT
4227 kfree(dump);
4228 return 0;
4229 }
4230
4231 ioa_cfg->dump = dump;
4232 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
56d6aa33 4233 if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead && !ioa_cfg->dump_taken) {
1da177e4
LT
4234 ioa_cfg->dump_taken = 1;
4235 schedule_work(&ioa_cfg->work_q);
4236 }
4237 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4238
1da177e4
LT
4239 return 0;
4240}
4241
4242/**
4243 * ipr_free_dump - Free adapter dump memory
4244 * @ioa_cfg: ioa config struct
4245 *
4246 * Return value:
4247 * 0 on success / other on failure
4248 **/
4249static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
4250{
4251 struct ipr_dump *dump;
4252 unsigned long lock_flags = 0;
4253
4254 ENTER;
4255
4256 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4257 dump = ioa_cfg->dump;
4258 if (!dump) {
4259 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4260 return 0;
4261 }
4262
4263 ioa_cfg->dump = NULL;
4264 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4265
4266 kref_put(&dump->kref, ipr_release_dump);
4267
4268 LEAVE;
4269 return 0;
4270}
4271
4272/**
4273 * ipr_write_dump - Setup dump state of adapter
2c3c8bea 4274 * @filp: open sysfs file
1da177e4 4275 * @kobj: kobject struct
91a69029 4276 * @bin_attr: bin_attribute struct
1da177e4
LT
4277 * @buf: buffer
4278 * @off: offset
4279 * @count: buffer size
4280 *
4281 * Return value:
4282 * number of bytes printed to buffer
4283 **/
2c3c8bea 4284static ssize_t ipr_write_dump(struct file *filp, struct kobject *kobj,
91a69029
ZR
4285 struct bin_attribute *bin_attr,
4286 char *buf, loff_t off, size_t count)
1da177e4 4287{
ee959b00 4288 struct device *cdev = container_of(kobj, struct device, kobj);
1da177e4
LT
4289 struct Scsi_Host *shost = class_to_shost(cdev);
4290 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
4291 int rc;
4292
4293 if (!capable(CAP_SYS_ADMIN))
4294 return -EACCES;
4295
4296 if (buf[0] == '1')
4297 rc = ipr_alloc_dump(ioa_cfg);
4298 else if (buf[0] == '0')
4299 rc = ipr_free_dump(ioa_cfg);
4300 else
4301 return -EINVAL;
4302
4303 if (rc)
4304 return rc;
4305 else
4306 return count;
4307}
4308
4309static struct bin_attribute ipr_dump_attr = {
4310 .attr = {
4311 .name = "dump",
4312 .mode = S_IRUSR | S_IWUSR,
4313 },
4314 .size = 0,
4315 .read = ipr_read_dump,
4316 .write = ipr_write_dump
4317};
4318#else
4319static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
4320#endif
4321
4322/**
4323 * ipr_change_queue_depth - Change the device's queue depth
4324 * @sdev: scsi device struct
4325 * @qdepth: depth to set
e881a172 4326 * @reason: calling context
1da177e4
LT
4327 *
4328 * Return value:
4329 * actual depth set
4330 **/
db5ed4df 4331static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
1da177e4 4332{
35a39691
BK
4333 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4334 struct ipr_resource_entry *res;
4335 unsigned long lock_flags = 0;
4336
4337 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4338 res = (struct ipr_resource_entry *)sdev->hostdata;
4339
4340 if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
4341 qdepth = IPR_MAX_CMD_PER_ATA_LUN;
4342 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4343
db5ed4df 4344 scsi_change_queue_depth(sdev, qdepth);
1da177e4
LT
4345 return sdev->queue_depth;
4346}
4347
1da177e4
LT
4348/**
4349 * ipr_show_adapter_handle - Show the adapter's resource handle for this device
4350 * @dev: device struct
46d74563 4351 * @attr: device attribute structure
1da177e4
LT
4352 * @buf: buffer
4353 *
4354 * Return value:
4355 * number of bytes printed to buffer
4356 **/
10523b3b 4357static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
4358{
4359 struct scsi_device *sdev = to_scsi_device(dev);
4360 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4361 struct ipr_resource_entry *res;
4362 unsigned long lock_flags = 0;
4363 ssize_t len = -ENXIO;
4364
4365 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4366 res = (struct ipr_resource_entry *)sdev->hostdata;
4367 if (res)
3e7ebdfa 4368 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->res_handle);
1da177e4
LT
4369 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4370 return len;
4371}
4372
4373static struct device_attribute ipr_adapter_handle_attr = {
4374 .attr = {
4375 .name = "adapter_handle",
4376 .mode = S_IRUSR,
4377 },
4378 .show = ipr_show_adapter_handle
4379};
4380
3e7ebdfa 4381/**
5adcbeb3
WB
4382 * ipr_show_resource_path - Show the resource path or the resource address for
4383 * this device.
3e7ebdfa 4384 * @dev: device struct
46d74563 4385 * @attr: device attribute structure
3e7ebdfa
WB
4386 * @buf: buffer
4387 *
4388 * Return value:
4389 * number of bytes printed to buffer
4390 **/
4391static ssize_t ipr_show_resource_path(struct device *dev, struct device_attribute *attr, char *buf)
4392{
4393 struct scsi_device *sdev = to_scsi_device(dev);
4394 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4395 struct ipr_resource_entry *res;
4396 unsigned long lock_flags = 0;
4397 ssize_t len = -ENXIO;
4398 char buffer[IPR_MAX_RES_PATH_LENGTH];
4399
4400 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4401 res = (struct ipr_resource_entry *)sdev->hostdata;
5adcbeb3 4402 if (res && ioa_cfg->sis64)
3e7ebdfa 4403 len = snprintf(buf, PAGE_SIZE, "%s\n",
b3b3b407
BK
4404 __ipr_format_res_path(res->res_path, buffer,
4405 sizeof(buffer)));
5adcbeb3
WB
4406 else if (res)
4407 len = snprintf(buf, PAGE_SIZE, "%d:%d:%d:%d\n", ioa_cfg->host->host_no,
4408 res->bus, res->target, res->lun);
4409
3e7ebdfa
WB
4410 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4411 return len;
4412}
4413
4414static struct device_attribute ipr_resource_path_attr = {
4415 .attr = {
4416 .name = "resource_path",
75576bb9 4417 .mode = S_IRUGO,
3e7ebdfa
WB
4418 },
4419 .show = ipr_show_resource_path
4420};
4421
46d74563
WB
4422/**
4423 * ipr_show_device_id - Show the device_id for this device.
4424 * @dev: device struct
4425 * @attr: device attribute structure
4426 * @buf: buffer
4427 *
4428 * Return value:
4429 * number of bytes printed to buffer
4430 **/
4431static ssize_t ipr_show_device_id(struct device *dev, struct device_attribute *attr, char *buf)
4432{
4433 struct scsi_device *sdev = to_scsi_device(dev);
4434 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4435 struct ipr_resource_entry *res;
4436 unsigned long lock_flags = 0;
4437 ssize_t len = -ENXIO;
4438
4439 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4440 res = (struct ipr_resource_entry *)sdev->hostdata;
4441 if (res && ioa_cfg->sis64)
4442 len = snprintf(buf, PAGE_SIZE, "0x%llx\n", res->dev_id);
4443 else if (res)
4444 len = snprintf(buf, PAGE_SIZE, "0x%llx\n", res->lun_wwn);
4445
4446 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4447 return len;
4448}
4449
4450static struct device_attribute ipr_device_id_attr = {
4451 .attr = {
4452 .name = "device_id",
4453 .mode = S_IRUGO,
4454 },
4455 .show = ipr_show_device_id
4456};
4457
75576bb9
WB
4458/**
4459 * ipr_show_resource_type - Show the resource type for this device.
4460 * @dev: device struct
46d74563 4461 * @attr: device attribute structure
75576bb9
WB
4462 * @buf: buffer
4463 *
4464 * Return value:
4465 * number of bytes printed to buffer
4466 **/
4467static ssize_t ipr_show_resource_type(struct device *dev, struct device_attribute *attr, char *buf)
4468{
4469 struct scsi_device *sdev = to_scsi_device(dev);
4470 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4471 struct ipr_resource_entry *res;
4472 unsigned long lock_flags = 0;
4473 ssize_t len = -ENXIO;
4474
4475 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4476 res = (struct ipr_resource_entry *)sdev->hostdata;
4477
4478 if (res)
4479 len = snprintf(buf, PAGE_SIZE, "%x\n", res->type);
4480
4481 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4482 return len;
4483}
4484
4485static struct device_attribute ipr_resource_type_attr = {
4486 .attr = {
4487 .name = "resource_type",
4488 .mode = S_IRUGO,
4489 },
4490 .show = ipr_show_resource_type
4491};
4492
1da177e4
LT
4493static struct device_attribute *ipr_dev_attrs[] = {
4494 &ipr_adapter_handle_attr,
3e7ebdfa 4495 &ipr_resource_path_attr,
46d74563 4496 &ipr_device_id_attr,
75576bb9 4497 &ipr_resource_type_attr,
1da177e4
LT
4498 NULL,
4499};
4500
4501/**
4502 * ipr_biosparam - Return the HSC mapping
4503 * @sdev: scsi device struct
4504 * @block_device: block device pointer
4505 * @capacity: capacity of the device
4506 * @parm: Array containing returned HSC values.
4507 *
4508 * This function generates the HSC parms that fdisk uses.
4509 * We want to make sure we return something that places partitions
4510 * on 4k boundaries for best performance with the IOA.
4511 *
4512 * Return value:
4513 * 0 on success
4514 **/
4515static int ipr_biosparam(struct scsi_device *sdev,
4516 struct block_device *block_device,
4517 sector_t capacity, int *parm)
4518{
4519 int heads, sectors;
4520 sector_t cylinders;
4521
4522 heads = 128;
4523 sectors = 32;
4524
4525 cylinders = capacity;
4526 sector_div(cylinders, (128 * 32));
4527
4528 /* return result */
4529 parm[0] = heads;
4530 parm[1] = sectors;
4531 parm[2] = cylinders;
4532
4533 return 0;
4534}
4535
35a39691
BK
4536/**
4537 * ipr_find_starget - Find target based on bus/target.
4538 * @starget: scsi target struct
4539 *
4540 * Return value:
4541 * resource entry pointer if found / NULL if not found
4542 **/
4543static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
4544{
4545 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4546 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4547 struct ipr_resource_entry *res;
4548
4549 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3e7ebdfa 4550 if ((res->bus == starget->channel) &&
0ee1d714 4551 (res->target == starget->id)) {
35a39691
BK
4552 return res;
4553 }
4554 }
4555
4556 return NULL;
4557}
4558
4559static struct ata_port_info sata_port_info;
4560
4561/**
4562 * ipr_target_alloc - Prepare for commands to a SCSI target
4563 * @starget: scsi target struct
4564 *
4565 * If the device is a SATA device, this function allocates an
4566 * ATA port with libata, else it does nothing.
4567 *
4568 * Return value:
4569 * 0 on success / non-0 on failure
4570 **/
4571static int ipr_target_alloc(struct scsi_target *starget)
4572{
4573 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4574 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4575 struct ipr_sata_port *sata_port;
4576 struct ata_port *ap;
4577 struct ipr_resource_entry *res;
4578 unsigned long lock_flags;
4579
4580 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4581 res = ipr_find_starget(starget);
4582 starget->hostdata = NULL;
4583
4584 if (res && ipr_is_gata(res)) {
4585 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4586 sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
4587 if (!sata_port)
4588 return -ENOMEM;
4589
4590 ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
4591 if (ap) {
4592 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4593 sata_port->ioa_cfg = ioa_cfg;
4594 sata_port->ap = ap;
4595 sata_port->res = res;
4596
4597 res->sata_port = sata_port;
4598 ap->private_data = sata_port;
4599 starget->hostdata = sata_port;
4600 } else {
4601 kfree(sata_port);
4602 return -ENOMEM;
4603 }
4604 }
4605 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4606
4607 return 0;
4608}
4609
4610/**
4611 * ipr_target_destroy - Destroy a SCSI target
4612 * @starget: scsi target struct
4613 *
4614 * If the device was a SATA device, this function frees the libata
4615 * ATA port, else it does nothing.
4616 *
4617 **/
4618static void ipr_target_destroy(struct scsi_target *starget)
4619{
4620 struct ipr_sata_port *sata_port = starget->hostdata;
3e7ebdfa
WB
4621 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4622 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4623
4624 if (ioa_cfg->sis64) {
0ee1d714
BK
4625 if (!ipr_find_starget(starget)) {
4626 if (starget->channel == IPR_ARRAY_VIRTUAL_BUS)
4627 clear_bit(starget->id, ioa_cfg->array_ids);
4628 else if (starget->channel == IPR_VSET_VIRTUAL_BUS)
4629 clear_bit(starget->id, ioa_cfg->vset_ids);
4630 else if (starget->channel == 0)
4631 clear_bit(starget->id, ioa_cfg->target_ids);
4632 }
3e7ebdfa 4633 }
35a39691
BK
4634
4635 if (sata_port) {
4636 starget->hostdata = NULL;
4637 ata_sas_port_destroy(sata_port->ap);
4638 kfree(sata_port);
4639 }
4640}
4641
4642/**
4643 * ipr_find_sdev - Find device based on bus/target/lun.
4644 * @sdev: scsi device struct
4645 *
4646 * Return value:
4647 * resource entry pointer if found / NULL if not found
4648 **/
4649static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
4650{
4651 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4652 struct ipr_resource_entry *res;
4653
4654 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3e7ebdfa
WB
4655 if ((res->bus == sdev->channel) &&
4656 (res->target == sdev->id) &&
4657 (res->lun == sdev->lun))
35a39691
BK
4658 return res;
4659 }
4660
4661 return NULL;
4662}
4663
1da177e4
LT
4664/**
4665 * ipr_slave_destroy - Unconfigure a SCSI device
4666 * @sdev: scsi device struct
4667 *
4668 * Return value:
4669 * nothing
4670 **/
4671static void ipr_slave_destroy(struct scsi_device *sdev)
4672{
4673 struct ipr_resource_entry *res;
4674 struct ipr_ioa_cfg *ioa_cfg;
4675 unsigned long lock_flags = 0;
4676
4677 ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4678
4679 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4680 res = (struct ipr_resource_entry *) sdev->hostdata;
4681 if (res) {
35a39691 4682 if (res->sata_port)
3e4ec344 4683 res->sata_port->ap->link.device[0].class = ATA_DEV_NONE;
1da177e4
LT
4684 sdev->hostdata = NULL;
4685 res->sdev = NULL;
35a39691 4686 res->sata_port = NULL;
1da177e4
LT
4687 }
4688 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4689}
4690
4691/**
4692 * ipr_slave_configure - Configure a SCSI device
4693 * @sdev: scsi device struct
4694 *
4695 * This function configures the specified scsi device.
4696 *
4697 * Return value:
4698 * 0 on success
4699 **/
4700static int ipr_slave_configure(struct scsi_device *sdev)
4701{
4702 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4703 struct ipr_resource_entry *res;
dd406ef8 4704 struct ata_port *ap = NULL;
1da177e4 4705 unsigned long lock_flags = 0;
3e7ebdfa 4706 char buffer[IPR_MAX_RES_PATH_LENGTH];
1da177e4
LT
4707
4708 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4709 res = sdev->hostdata;
4710 if (res) {
4711 if (ipr_is_af_dasd_device(res))
4712 sdev->type = TYPE_RAID;
0726ce26 4713 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
1da177e4 4714 sdev->scsi_level = 4;
0726ce26
BK
4715 sdev->no_uld_attach = 1;
4716 }
1da177e4 4717 if (ipr_is_vset_device(res)) {
242f9dcb
JA
4718 blk_queue_rq_timeout(sdev->request_queue,
4719 IPR_VSET_RW_TIMEOUT);
086fa5ff 4720 blk_queue_max_hw_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
1da177e4 4721 }
dd406ef8
BK
4722 if (ipr_is_gata(res) && res->sata_port)
4723 ap = res->sata_port->ap;
4724 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4725
4726 if (ap) {
db5ed4df 4727 scsi_change_queue_depth(sdev, IPR_MAX_CMD_PER_ATA_LUN);
dd406ef8 4728 ata_sas_slave_configure(sdev, ap);
c8b09f6f
CH
4729 }
4730
3e7ebdfa
WB
4731 if (ioa_cfg->sis64)
4732 sdev_printk(KERN_INFO, sdev, "Resource path: %s\n",
b3b3b407
BK
4733 ipr_format_res_path(ioa_cfg,
4734 res->res_path, buffer, sizeof(buffer)));
dd406ef8 4735 return 0;
1da177e4
LT
4736 }
4737 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4738 return 0;
4739}
4740
35a39691
BK
4741/**
4742 * ipr_ata_slave_alloc - Prepare for commands to a SATA device
4743 * @sdev: scsi device struct
4744 *
4745 * This function initializes an ATA port so that future commands
4746 * sent through queuecommand will work.
4747 *
4748 * Return value:
4749 * 0 on success
4750 **/
4751static int ipr_ata_slave_alloc(struct scsi_device *sdev)
4752{
4753 struct ipr_sata_port *sata_port = NULL;
4754 int rc = -ENXIO;
4755
4756 ENTER;
4757 if (sdev->sdev_target)
4758 sata_port = sdev->sdev_target->hostdata;
b2024459 4759 if (sata_port) {
35a39691 4760 rc = ata_sas_port_init(sata_port->ap);
b2024459
DW
4761 if (rc == 0)
4762 rc = ata_sas_sync_probe(sata_port->ap);
4763 }
4764
35a39691
BK
4765 if (rc)
4766 ipr_slave_destroy(sdev);
4767
4768 LEAVE;
4769 return rc;
4770}
4771
1da177e4
LT
4772/**
4773 * ipr_slave_alloc - Prepare for commands to a device.
4774 * @sdev: scsi device struct
4775 *
4776 * This function saves a pointer to the resource entry
4777 * in the scsi device struct if the device exists. We
4778 * can then use this pointer in ipr_queuecommand when
4779 * handling new commands.
4780 *
4781 * Return value:
692aebfc 4782 * 0 on success / -ENXIO if device does not exist
1da177e4
LT
4783 **/
4784static int ipr_slave_alloc(struct scsi_device *sdev)
4785{
4786 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4787 struct ipr_resource_entry *res;
4788 unsigned long lock_flags;
692aebfc 4789 int rc = -ENXIO;
1da177e4
LT
4790
4791 sdev->hostdata = NULL;
4792
4793 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4794
35a39691
BK
4795 res = ipr_find_sdev(sdev);
4796 if (res) {
4797 res->sdev = sdev;
4798 res->add_to_ml = 0;
4799 res->in_erp = 0;
4800 sdev->hostdata = res;
4801 if (!ipr_is_naca_model(res))
4802 res->needs_sync_complete = 1;
4803 rc = 0;
4804 if (ipr_is_gata(res)) {
4805 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4806 return ipr_ata_slave_alloc(sdev);
1da177e4
LT
4807 }
4808 }
4809
4810 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4811
692aebfc 4812 return rc;
1da177e4
LT
4813}
4814
70233ac5 4815static int ipr_eh_host_reset(struct scsi_cmnd *cmd)
1da177e4
LT
4816{
4817 struct ipr_ioa_cfg *ioa_cfg;
70233ac5 4818 unsigned long lock_flags = 0;
4819 int rc = SUCCESS;
1da177e4
LT
4820
4821 ENTER;
70233ac5 4822 ioa_cfg = (struct ipr_ioa_cfg *) cmd->device->host->hostdata;
4823 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1da177e4 4824
96b04db9 4825 if (!ioa_cfg->in_reset_reload && !ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
70233ac5 4826 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
a92fa25c
KSS
4827 dev_err(&ioa_cfg->pdev->dev,
4828 "Adapter being reset as a result of error recovery.\n");
1da177e4 4829
a92fa25c
KSS
4830 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4831 ioa_cfg->sdt_state = GET_DUMP;
4832 }
1da177e4 4833
70233ac5 4834 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4835 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4836 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
df0ae249 4837
70233ac5 4838 /* If we got hit with a host reset while we were already resetting
4839 the adapter for some reason, and the reset failed. */
4840 if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
4841 ipr_trace;
4842 rc = FAILED;
4843 }
df0ae249 4844
70233ac5 4845 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4846 LEAVE;
df0ae249
JG
4847 return rc;
4848}
4849
c6513096
BK
4850/**
4851 * ipr_device_reset - Reset the device
4852 * @ioa_cfg: ioa config struct
4853 * @res: resource entry struct
4854 *
4855 * This function issues a device reset to the affected device.
4856 * If the device is a SCSI device, a LUN reset will be sent
4857 * to the device first. If that does not work, a target reset
35a39691
BK
4858 * will be sent. If the device is a SATA device, a PHY reset will
4859 * be sent.
c6513096
BK
4860 *
4861 * Return value:
4862 * 0 on success / non-zero on failure
4863 **/
4864static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
4865 struct ipr_resource_entry *res)
4866{
4867 struct ipr_cmnd *ipr_cmd;
4868 struct ipr_ioarcb *ioarcb;
4869 struct ipr_cmd_pkt *cmd_pkt;
35a39691 4870 struct ipr_ioarcb_ata_regs *regs;
c6513096
BK
4871 u32 ioasc;
4872
4873 ENTER;
4874 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4875 ioarcb = &ipr_cmd->ioarcb;
4876 cmd_pkt = &ioarcb->cmd_pkt;
a32c055f
WB
4877
4878 if (ipr_cmd->ioa_cfg->sis64) {
4879 regs = &ipr_cmd->i.ata_ioadl.regs;
4880 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
4881 } else
4882 regs = &ioarcb->u.add_data.u.regs;
c6513096 4883
3e7ebdfa 4884 ioarcb->res_handle = res->res_handle;
c6513096
BK
4885 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4886 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
35a39691
BK
4887 if (ipr_is_gata(res)) {
4888 cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
a32c055f 4889 ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(regs->flags));
35a39691
BK
4890 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
4891 }
c6513096
BK
4892
4893 ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
96d21f00 4894 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
05a6538a 4895 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
96d21f00
WB
4896 if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET) {
4897 if (ipr_cmd->ioa_cfg->sis64)
4898 memcpy(&res->sata_port->ioasa, &ipr_cmd->s.ioasa64.u.gata,
4899 sizeof(struct ipr_ioasa_gata));
4900 else
4901 memcpy(&res->sata_port->ioasa, &ipr_cmd->s.ioasa.u.gata,
4902 sizeof(struct ipr_ioasa_gata));
4903 }
c6513096
BK
4904
4905 LEAVE;
203fa3fe 4906 return IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0;
c6513096
BK
4907}
4908
35a39691
BK
4909/**
4910 * ipr_sata_reset - Reset the SATA port
cc0680a5 4911 * @link: SATA link to reset
35a39691
BK
4912 * @classes: class of the attached device
4913 *
cc0680a5 4914 * This function issues a SATA phy reset to the affected ATA link.
35a39691
BK
4915 *
4916 * Return value:
4917 * 0 on success / non-zero on failure
4918 **/
cc0680a5 4919static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
120bda35 4920 unsigned long deadline)
35a39691 4921{
cc0680a5 4922 struct ipr_sata_port *sata_port = link->ap->private_data;
35a39691
BK
4923 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
4924 struct ipr_resource_entry *res;
4925 unsigned long lock_flags = 0;
4926 int rc = -ENXIO;
4927
4928 ENTER;
4929 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
203fa3fe 4930 while (ioa_cfg->in_reset_reload) {
73d98ff0
BK
4931 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4932 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4933 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4934 }
4935
35a39691
BK
4936 res = sata_port->res;
4937 if (res) {
4938 rc = ipr_device_reset(ioa_cfg, res);
3e7ebdfa 4939 *classes = res->ata_class;
35a39691
BK
4940 }
4941
4942 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4943 LEAVE;
4944 return rc;
4945}
4946
1da177e4
LT
4947/**
4948 * ipr_eh_dev_reset - Reset the device
4949 * @scsi_cmd: scsi command struct
4950 *
4951 * This function issues a device reset to the affected device.
4952 * A LUN reset will be sent to the device first. If that does
4953 * not work, a target reset will be sent.
4954 *
4955 * Return value:
4956 * SUCCESS / FAILED
4957 **/
203fa3fe 4958static int __ipr_eh_dev_reset(struct scsi_cmnd *scsi_cmd)
1da177e4
LT
4959{
4960 struct ipr_cmnd *ipr_cmd;
4961 struct ipr_ioa_cfg *ioa_cfg;
4962 struct ipr_resource_entry *res;
35a39691
BK
4963 struct ata_port *ap;
4964 int rc = 0;
05a6538a 4965 struct ipr_hrr_queue *hrrq;
1da177e4
LT
4966
4967 ENTER;
4968 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
4969 res = scsi_cmd->device->hostdata;
4970
eeb88307 4971 if (!res)
1da177e4
LT
4972 return FAILED;
4973
4974 /*
4975 * If we are currently going through reset/reload, return failed. This will force the
4976 * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
4977 * reset to complete
4978 */
4979 if (ioa_cfg->in_reset_reload)
4980 return FAILED;
56d6aa33 4981 if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
1da177e4
LT
4982 return FAILED;
4983
05a6538a 4984 for_each_hrrq(hrrq, ioa_cfg) {
56d6aa33 4985 spin_lock(&hrrq->_lock);
05a6538a 4986 list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
4987 if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
4988 if (ipr_cmd->scsi_cmd)
4989 ipr_cmd->done = ipr_scsi_eh_done;
4990 if (ipr_cmd->qc)
4991 ipr_cmd->done = ipr_sata_eh_done;
4992 if (ipr_cmd->qc &&
4993 !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
4994 ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
4995 ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
4996 }
7402ecef 4997 }
1da177e4 4998 }
56d6aa33 4999 spin_unlock(&hrrq->_lock);
1da177e4 5000 }
1da177e4 5001 res->resetting_device = 1;
fb3ed3cb 5002 scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
35a39691
BK
5003
5004 if (ipr_is_gata(res) && res->sata_port) {
5005 ap = res->sata_port->ap;
5006 spin_unlock_irq(scsi_cmd->device->host->host_lock);
a1efdaba 5007 ata_std_error_handler(ap);
35a39691 5008 spin_lock_irq(scsi_cmd->device->host->host_lock);
5af23d26 5009
05a6538a 5010 for_each_hrrq(hrrq, ioa_cfg) {
56d6aa33 5011 spin_lock(&hrrq->_lock);
05a6538a 5012 list_for_each_entry(ipr_cmd,
5013 &hrrq->hrrq_pending_q, queue) {
5014 if (ipr_cmd->ioarcb.res_handle ==
5015 res->res_handle) {
5016 rc = -EIO;
5017 break;
5018 }
5af23d26 5019 }
56d6aa33 5020 spin_unlock(&hrrq->_lock);
5af23d26 5021 }
35a39691
BK
5022 } else
5023 rc = ipr_device_reset(ioa_cfg, res);
1da177e4 5024 res->resetting_device = 0;
0b1f8d44 5025 res->reset_occurred = 1;
1da177e4 5026
1da177e4 5027 LEAVE;
203fa3fe 5028 return rc ? FAILED : SUCCESS;
1da177e4
LT
5029}
5030
203fa3fe 5031static int ipr_eh_dev_reset(struct scsi_cmnd *cmd)
94d0e7b8
JG
5032{
5033 int rc;
5034
5035 spin_lock_irq(cmd->device->host->host_lock);
5036 rc = __ipr_eh_dev_reset(cmd);
5037 spin_unlock_irq(cmd->device->host->host_lock);
5038
5039 return rc;
5040}
5041
1da177e4
LT
5042/**
5043 * ipr_bus_reset_done - Op done function for bus reset.
5044 * @ipr_cmd: ipr command struct
5045 *
5046 * This function is the op done function for a bus reset
5047 *
5048 * Return value:
5049 * none
5050 **/
5051static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
5052{
5053 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5054 struct ipr_resource_entry *res;
5055
5056 ENTER;
3e7ebdfa
WB
5057 if (!ioa_cfg->sis64)
5058 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
5059 if (res->res_handle == ipr_cmd->ioarcb.res_handle) {
5060 scsi_report_bus_reset(ioa_cfg->host, res->bus);
5061 break;
5062 }
1da177e4 5063 }
1da177e4
LT
5064
5065 /*
5066 * If abort has not completed, indicate the reset has, else call the
5067 * abort's done function to wake the sleeping eh thread
5068 */
5069 if (ipr_cmd->sibling->sibling)
5070 ipr_cmd->sibling->sibling = NULL;
5071 else
5072 ipr_cmd->sibling->done(ipr_cmd->sibling);
5073
05a6538a 5074 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
1da177e4
LT
5075 LEAVE;
5076}
5077
5078/**
5079 * ipr_abort_timeout - An abort task has timed out
5080 * @ipr_cmd: ipr command struct
5081 *
5082 * This function handles when an abort task times out. If this
5083 * happens we issue a bus reset since we have resources tied
5084 * up that must be freed before returning to the midlayer.
5085 *
5086 * Return value:
5087 * none
5088 **/
5089static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
5090{
5091 struct ipr_cmnd *reset_cmd;
5092 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5093 struct ipr_cmd_pkt *cmd_pkt;
5094 unsigned long lock_flags = 0;
5095
5096 ENTER;
5097 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5098 if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
5099 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5100 return;
5101 }
5102
fb3ed3cb 5103 sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
1da177e4
LT
5104 reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5105 ipr_cmd->sibling = reset_cmd;
5106 reset_cmd->sibling = ipr_cmd;
5107 reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
5108 cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
5109 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5110 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
5111 cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
5112
5113 ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
5114 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5115 LEAVE;
5116}
5117
5118/**
5119 * ipr_cancel_op - Cancel specified op
5120 * @scsi_cmd: scsi command struct
5121 *
5122 * This function cancels specified op.
5123 *
5124 * Return value:
5125 * SUCCESS / FAILED
5126 **/
203fa3fe 5127static int ipr_cancel_op(struct scsi_cmnd *scsi_cmd)
1da177e4
LT
5128{
5129 struct ipr_cmnd *ipr_cmd;
5130 struct ipr_ioa_cfg *ioa_cfg;
5131 struct ipr_resource_entry *res;
5132 struct ipr_cmd_pkt *cmd_pkt;
a92fa25c 5133 u32 ioasc, int_reg;
1da177e4 5134 int op_found = 0;
05a6538a 5135 struct ipr_hrr_queue *hrrq;
1da177e4
LT
5136
5137 ENTER;
5138 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
5139 res = scsi_cmd->device->hostdata;
5140
8fa728a2
JG
5141 /* If we are currently going through reset/reload, return failed.
5142 * This will force the mid-layer to call ipr_eh_host_reset,
5143 * which will then go to sleep and wait for the reset to complete
5144 */
56d6aa33 5145 if (ioa_cfg->in_reset_reload ||
5146 ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
8fa728a2 5147 return FAILED;
a92fa25c
KSS
5148 if (!res)
5149 return FAILED;
5150
5151 /*
5152 * If we are aborting a timed out op, chances are that the timeout was caused
5153 * by a still not detected EEH error. In such cases, reading a register will
5154 * trigger the EEH recovery infrastructure.
5155 */
5156 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5157
5158 if (!ipr_is_gscsi(res))
1da177e4
LT
5159 return FAILED;
5160
05a6538a 5161 for_each_hrrq(hrrq, ioa_cfg) {
56d6aa33 5162 spin_lock(&hrrq->_lock);
05a6538a 5163 list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
5164 if (ipr_cmd->scsi_cmd == scsi_cmd) {
5165 ipr_cmd->done = ipr_scsi_eh_done;
5166 op_found = 1;
5167 break;
5168 }
1da177e4 5169 }
56d6aa33 5170 spin_unlock(&hrrq->_lock);
1da177e4
LT
5171 }
5172
5173 if (!op_found)
5174 return SUCCESS;
5175
5176 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3e7ebdfa 5177 ipr_cmd->ioarcb.res_handle = res->res_handle;
1da177e4
LT
5178 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5179 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5180 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
5181 ipr_cmd->u.sdev = scsi_cmd->device;
5182
fb3ed3cb
BK
5183 scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
5184 scsi_cmd->cmnd[0]);
1da177e4 5185 ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
96d21f00 5186 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1da177e4
LT
5187
5188 /*
5189 * If the abort task timed out and we sent a bus reset, we will get
5190 * one the following responses to the abort
5191 */
5192 if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
5193 ioasc = 0;
5194 ipr_trace;
5195 }
5196
c4ee22a3 5197 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
ee0a90fa
BK
5198 if (!ipr_is_naca_model(res))
5199 res->needs_sync_complete = 1;
1da177e4
LT
5200
5201 LEAVE;
203fa3fe 5202 return IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
1da177e4
LT
5203}
5204
5205/**
5206 * ipr_eh_abort - Abort a single op
5207 * @scsi_cmd: scsi command struct
5208 *
5209 * Return value:
5210 * SUCCESS / FAILED
5211 **/
203fa3fe 5212static int ipr_eh_abort(struct scsi_cmnd *scsi_cmd)
1da177e4 5213{
8fa728a2
JG
5214 unsigned long flags;
5215 int rc;
1da177e4
LT
5216
5217 ENTER;
1da177e4 5218
8fa728a2
JG
5219 spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
5220 rc = ipr_cancel_op(scsi_cmd);
5221 spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
1da177e4
LT
5222
5223 LEAVE;
8fa728a2 5224 return rc;
1da177e4
LT
5225}
5226
5227/**
5228 * ipr_handle_other_interrupt - Handle "other" interrupts
5229 * @ioa_cfg: ioa config struct
634651fa 5230 * @int_reg: interrupt register
1da177e4
LT
5231 *
5232 * Return value:
5233 * IRQ_NONE / IRQ_HANDLED
5234 **/
634651fa 5235static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
630ad831 5236 u32 int_reg)
1da177e4
LT
5237{
5238 irqreturn_t rc = IRQ_HANDLED;
7dacb64f 5239 u32 int_mask_reg;
56d6aa33 5240
7dacb64f
WB
5241 int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
5242 int_reg &= ~int_mask_reg;
5243
5244 /* If an interrupt on the adapter did not occur, ignore it.
5245 * Or in the case of SIS 64, check for a stage change interrupt.
5246 */
5247 if ((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0) {
5248 if (ioa_cfg->sis64) {
5249 int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
5250 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
5251 if (int_reg & IPR_PCII_IPL_STAGE_CHANGE) {
5252
5253 /* clear stage change */
5254 writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.clr_interrupt_reg);
5255 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
5256 list_del(&ioa_cfg->reset_cmd->queue);
5257 del_timer(&ioa_cfg->reset_cmd->timer);
5258 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
5259 return IRQ_HANDLED;
5260 }
5261 }
5262
5263 return IRQ_NONE;
5264 }
1da177e4
LT
5265
5266 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
5267 /* Mask the interrupt */
5268 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
5269
5270 /* Clear the interrupt */
5271 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
5272 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5273
5274 list_del(&ioa_cfg->reset_cmd->queue);
5275 del_timer(&ioa_cfg->reset_cmd->timer);
5276 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
7dacb64f 5277 } else if ((int_reg & IPR_PCII_HRRQ_UPDATED) == int_reg) {
7dd21308
BK
5278 if (ioa_cfg->clear_isr) {
5279 if (ipr_debug && printk_ratelimit())
5280 dev_err(&ioa_cfg->pdev->dev,
5281 "Spurious interrupt detected. 0x%08X\n", int_reg);
5282 writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg32);
5283 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
5284 return IRQ_NONE;
5285 }
1da177e4
LT
5286 } else {
5287 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
5288 ioa_cfg->ioa_unit_checked = 1;
05a6538a 5289 else if (int_reg & IPR_PCII_NO_HOST_RRQ)
5290 dev_err(&ioa_cfg->pdev->dev,
5291 "No Host RRQ. 0x%08X\n", int_reg);
1da177e4
LT
5292 else
5293 dev_err(&ioa_cfg->pdev->dev,
5294 "Permanent IOA failure. 0x%08X\n", int_reg);
5295
5296 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
5297 ioa_cfg->sdt_state = GET_DUMP;
5298
5299 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
5300 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5301 }
56d6aa33 5302
1da177e4
LT
5303 return rc;
5304}
5305
3feeb89d
WB
5306/**
5307 * ipr_isr_eh - Interrupt service routine error handler
5308 * @ioa_cfg: ioa config struct
5309 * @msg: message to log
5310 *
5311 * Return value:
5312 * none
5313 **/
05a6538a 5314static void ipr_isr_eh(struct ipr_ioa_cfg *ioa_cfg, char *msg, u16 number)
3feeb89d
WB
5315{
5316 ioa_cfg->errors_logged++;
05a6538a 5317 dev_err(&ioa_cfg->pdev->dev, "%s %d\n", msg, number);
3feeb89d
WB
5318
5319 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
5320 ioa_cfg->sdt_state = GET_DUMP;
5321
5322 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5323}
5324
b53d124a 5325static int ipr_process_hrrq(struct ipr_hrr_queue *hrr_queue, int budget,
05a6538a 5326 struct list_head *doneq)
5327{
5328 u32 ioasc;
5329 u16 cmd_index;
5330 struct ipr_cmnd *ipr_cmd;
5331 struct ipr_ioa_cfg *ioa_cfg = hrr_queue->ioa_cfg;
5332 int num_hrrq = 0;
5333
5334 /* If interrupts are disabled, ignore the interrupt */
56d6aa33 5335 if (!hrr_queue->allow_interrupts)
05a6538a 5336 return 0;
5337
5338 while ((be32_to_cpu(*hrr_queue->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
5339 hrr_queue->toggle_bit) {
5340
5341 cmd_index = (be32_to_cpu(*hrr_queue->hrrq_curr) &
5342 IPR_HRRQ_REQ_RESP_HANDLE_MASK) >>
5343 IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
5344
5345 if (unlikely(cmd_index > hrr_queue->max_cmd_id ||
5346 cmd_index < hrr_queue->min_cmd_id)) {
5347 ipr_isr_eh(ioa_cfg,
5348 "Invalid response handle from IOA: ",
5349 cmd_index);
5350 break;
5351 }
5352
5353 ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
5354 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5355
5356 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
5357
5358 list_move_tail(&ipr_cmd->queue, doneq);
5359
5360 if (hrr_queue->hrrq_curr < hrr_queue->hrrq_end) {
5361 hrr_queue->hrrq_curr++;
5362 } else {
5363 hrr_queue->hrrq_curr = hrr_queue->hrrq_start;
5364 hrr_queue->toggle_bit ^= 1u;
5365 }
5366 num_hrrq++;
b53d124a 5367 if (budget > 0 && num_hrrq >= budget)
5368 break;
05a6538a 5369 }
b53d124a 5370
05a6538a 5371 return num_hrrq;
5372}
b53d124a 5373
5374static int ipr_iopoll(struct blk_iopoll *iop, int budget)
5375{
5376 struct ipr_ioa_cfg *ioa_cfg;
5377 struct ipr_hrr_queue *hrrq;
5378 struct ipr_cmnd *ipr_cmd, *temp;
5379 unsigned long hrrq_flags;
5380 int completed_ops;
5381 LIST_HEAD(doneq);
5382
5383 hrrq = container_of(iop, struct ipr_hrr_queue, iopoll);
5384 ioa_cfg = hrrq->ioa_cfg;
5385
5386 spin_lock_irqsave(hrrq->lock, hrrq_flags);
5387 completed_ops = ipr_process_hrrq(hrrq, budget, &doneq);
5388
5389 if (completed_ops < budget)
5390 blk_iopoll_complete(iop);
5391 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5392
5393 list_for_each_entry_safe(ipr_cmd, temp, &doneq, queue) {
5394 list_del(&ipr_cmd->queue);
5395 del_timer(&ipr_cmd->timer);
5396 ipr_cmd->fast_done(ipr_cmd);
5397 }
5398
5399 return completed_ops;
5400}
5401
1da177e4
LT
5402/**
5403 * ipr_isr - Interrupt service routine
5404 * @irq: irq number
5405 * @devp: pointer to ioa config struct
1da177e4
LT
5406 *
5407 * Return value:
5408 * IRQ_NONE / IRQ_HANDLED
5409 **/
7d12e780 5410static irqreturn_t ipr_isr(int irq, void *devp)
1da177e4 5411{
05a6538a 5412 struct ipr_hrr_queue *hrrq = (struct ipr_hrr_queue *)devp;
5413 struct ipr_ioa_cfg *ioa_cfg = hrrq->ioa_cfg;
56d6aa33 5414 unsigned long hrrq_flags = 0;
7dacb64f 5415 u32 int_reg = 0;
3feeb89d 5416 int num_hrrq = 0;
7dacb64f 5417 int irq_none = 0;
172cd6e1 5418 struct ipr_cmnd *ipr_cmd, *temp;
1da177e4 5419 irqreturn_t rc = IRQ_NONE;
172cd6e1 5420 LIST_HEAD(doneq);
1da177e4 5421
56d6aa33 5422 spin_lock_irqsave(hrrq->lock, hrrq_flags);
1da177e4 5423 /* If interrupts are disabled, ignore the interrupt */
56d6aa33 5424 if (!hrrq->allow_interrupts) {
5425 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
1da177e4
LT
5426 return IRQ_NONE;
5427 }
5428
1da177e4 5429 while (1) {
b53d124a 5430 if (ipr_process_hrrq(hrrq, -1, &doneq)) {
5431 rc = IRQ_HANDLED;
1da177e4 5432
b53d124a 5433 if (!ioa_cfg->clear_isr)
5434 break;
7dd21308 5435
1da177e4 5436 /* Clear the PCI interrupt */
a5442ba4 5437 num_hrrq = 0;
3feeb89d 5438 do {
b53d124a 5439 writel(IPR_PCII_HRRQ_UPDATED,
5440 ioa_cfg->regs.clr_interrupt_reg32);
7dacb64f 5441 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
3feeb89d 5442 } while (int_reg & IPR_PCII_HRRQ_UPDATED &&
b53d124a 5443 num_hrrq++ < IPR_MAX_HRRQ_RETRIES);
3feeb89d 5444
7dacb64f
WB
5445 } else if (rc == IRQ_NONE && irq_none == 0) {
5446 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
5447 irq_none++;
a5442ba4
WB
5448 } else if (num_hrrq == IPR_MAX_HRRQ_RETRIES &&
5449 int_reg & IPR_PCII_HRRQ_UPDATED) {
b53d124a 5450 ipr_isr_eh(ioa_cfg,
5451 "Error clearing HRRQ: ", num_hrrq);
172cd6e1 5452 rc = IRQ_HANDLED;
b53d124a 5453 break;
1da177e4
LT
5454 } else
5455 break;
5456 }
5457
5458 if (unlikely(rc == IRQ_NONE))
634651fa 5459 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
1da177e4 5460
56d6aa33 5461 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
172cd6e1
BK
5462 list_for_each_entry_safe(ipr_cmd, temp, &doneq, queue) {
5463 list_del(&ipr_cmd->queue);
5464 del_timer(&ipr_cmd->timer);
5465 ipr_cmd->fast_done(ipr_cmd);
5466 }
05a6538a 5467 return rc;
5468}
5469
5470/**
5471 * ipr_isr_mhrrq - Interrupt service routine
5472 * @irq: irq number
5473 * @devp: pointer to ioa config struct
5474 *
5475 * Return value:
5476 * IRQ_NONE / IRQ_HANDLED
5477 **/
5478static irqreturn_t ipr_isr_mhrrq(int irq, void *devp)
5479{
5480 struct ipr_hrr_queue *hrrq = (struct ipr_hrr_queue *)devp;
b53d124a 5481 struct ipr_ioa_cfg *ioa_cfg = hrrq->ioa_cfg;
56d6aa33 5482 unsigned long hrrq_flags = 0;
05a6538a 5483 struct ipr_cmnd *ipr_cmd, *temp;
5484 irqreturn_t rc = IRQ_NONE;
5485 LIST_HEAD(doneq);
172cd6e1 5486
56d6aa33 5487 spin_lock_irqsave(hrrq->lock, hrrq_flags);
05a6538a 5488
5489 /* If interrupts are disabled, ignore the interrupt */
56d6aa33 5490 if (!hrrq->allow_interrupts) {
5491 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
05a6538a 5492 return IRQ_NONE;
5493 }
5494
89f8b33c 5495 if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
b53d124a 5496 if ((be32_to_cpu(*hrrq->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
5497 hrrq->toggle_bit) {
5498 if (!blk_iopoll_sched_prep(&hrrq->iopoll))
5499 blk_iopoll_sched(&hrrq->iopoll);
5500 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5501 return IRQ_HANDLED;
5502 }
5503 } else {
5504 if ((be32_to_cpu(*hrrq->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
5505 hrrq->toggle_bit)
05a6538a 5506
b53d124a 5507 if (ipr_process_hrrq(hrrq, -1, &doneq))
5508 rc = IRQ_HANDLED;
5509 }
05a6538a 5510
56d6aa33 5511 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
05a6538a 5512
5513 list_for_each_entry_safe(ipr_cmd, temp, &doneq, queue) {
5514 list_del(&ipr_cmd->queue);
5515 del_timer(&ipr_cmd->timer);
5516 ipr_cmd->fast_done(ipr_cmd);
5517 }
1da177e4
LT
5518 return rc;
5519}
5520
a32c055f
WB
5521/**
5522 * ipr_build_ioadl64 - Build a scatter/gather list and map the buffer
5523 * @ioa_cfg: ioa config struct
5524 * @ipr_cmd: ipr command struct
5525 *
5526 * Return value:
5527 * 0 on success / -1 on failure
5528 **/
5529static int ipr_build_ioadl64(struct ipr_ioa_cfg *ioa_cfg,
5530 struct ipr_cmnd *ipr_cmd)
5531{
5532 int i, nseg;
5533 struct scatterlist *sg;
5534 u32 length;
5535 u32 ioadl_flags = 0;
5536 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5537 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5538 struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
5539
5540 length = scsi_bufflen(scsi_cmd);
5541 if (!length)
5542 return 0;
5543
5544 nseg = scsi_dma_map(scsi_cmd);
5545 if (nseg < 0) {
51f52a47 5546 if (printk_ratelimit())
d73341bf 5547 dev_err(&ioa_cfg->pdev->dev, "scsi_dma_map failed!\n");
a32c055f
WB
5548 return -1;
5549 }
5550
5551 ipr_cmd->dma_use_sg = nseg;
5552
438b0331 5553 ioarcb->data_transfer_length = cpu_to_be32(length);
b8803b1c
WB
5554 ioarcb->ioadl_len =
5555 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
438b0331 5556
a32c055f
WB
5557 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
5558 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5559 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5560 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE)
5561 ioadl_flags = IPR_IOADL_FLAGS_READ;
5562
5563 scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
5564 ioadl64[i].flags = cpu_to_be32(ioadl_flags);
5565 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(sg));
5566 ioadl64[i].address = cpu_to_be64(sg_dma_address(sg));
5567 }
5568
5569 ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5570 return 0;
5571}
5572
1da177e4
LT
5573/**
5574 * ipr_build_ioadl - Build a scatter/gather list and map the buffer
5575 * @ioa_cfg: ioa config struct
5576 * @ipr_cmd: ipr command struct
5577 *
5578 * Return value:
5579 * 0 on success / -1 on failure
5580 **/
5581static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
5582 struct ipr_cmnd *ipr_cmd)
5583{
63015bc9
FT
5584 int i, nseg;
5585 struct scatterlist *sg;
1da177e4
LT
5586 u32 length;
5587 u32 ioadl_flags = 0;
5588 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5589 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
a32c055f 5590 struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
1da177e4 5591
63015bc9
FT
5592 length = scsi_bufflen(scsi_cmd);
5593 if (!length)
1da177e4
LT
5594 return 0;
5595
63015bc9
FT
5596 nseg = scsi_dma_map(scsi_cmd);
5597 if (nseg < 0) {
d73341bf 5598 dev_err(&ioa_cfg->pdev->dev, "scsi_dma_map failed!\n");
63015bc9
FT
5599 return -1;
5600 }
51b1c7e1 5601
63015bc9
FT
5602 ipr_cmd->dma_use_sg = nseg;
5603
5604 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
5605 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5606 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
a32c055f
WB
5607 ioarcb->data_transfer_length = cpu_to_be32(length);
5608 ioarcb->ioadl_len =
63015bc9
FT
5609 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5610 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
5611 ioadl_flags = IPR_IOADL_FLAGS_READ;
5612 ioarcb->read_data_transfer_length = cpu_to_be32(length);
5613 ioarcb->read_ioadl_len =
5614 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5615 }
1da177e4 5616
a32c055f
WB
5617 if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->u.add_data.u.ioadl)) {
5618 ioadl = ioarcb->u.add_data.u.ioadl;
5619 ioarcb->write_ioadl_addr = cpu_to_be32((ipr_cmd->dma_addr) +
5620 offsetof(struct ipr_ioarcb, u.add_data));
63015bc9
FT
5621 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5622 }
1da177e4 5623
63015bc9
FT
5624 scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
5625 ioadl[i].flags_and_data_len =
5626 cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5627 ioadl[i].address = cpu_to_be32(sg_dma_address(sg));
1da177e4
LT
5628 }
5629
63015bc9
FT
5630 ioadl[i-1].flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5631 return 0;
1da177e4
LT
5632}
5633
1da177e4
LT
5634/**
5635 * ipr_erp_done - Process completion of ERP for a device
5636 * @ipr_cmd: ipr command struct
5637 *
5638 * This function copies the sense buffer into the scsi_cmd
5639 * struct and pushes the scsi_done function.
5640 *
5641 * Return value:
5642 * nothing
5643 **/
5644static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
5645{
5646 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5647 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
96d21f00 5648 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1da177e4
LT
5649
5650 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
5651 scsi_cmd->result |= (DID_ERROR << 16);
fb3ed3cb
BK
5652 scmd_printk(KERN_ERR, scsi_cmd,
5653 "Request Sense failed with IOASC: 0x%08X\n", ioasc);
1da177e4
LT
5654 } else {
5655 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
5656 SCSI_SENSE_BUFFERSIZE);
5657 }
5658
5659 if (res) {
ee0a90fa
BK
5660 if (!ipr_is_naca_model(res))
5661 res->needs_sync_complete = 1;
1da177e4
LT
5662 res->in_erp = 0;
5663 }
63015bc9 5664 scsi_dma_unmap(ipr_cmd->scsi_cmd);
05a6538a 5665 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
1da177e4
LT
5666 scsi_cmd->scsi_done(scsi_cmd);
5667}
5668
5669/**
5670 * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
5671 * @ipr_cmd: ipr command struct
5672 *
5673 * Return value:
5674 * none
5675 **/
5676static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
5677{
51b1c7e1 5678 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
96d21f00 5679 struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
a32c055f 5680 dma_addr_t dma_addr = ipr_cmd->dma_addr;
1da177e4
LT
5681
5682 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
a32c055f 5683 ioarcb->data_transfer_length = 0;
1da177e4 5684 ioarcb->read_data_transfer_length = 0;
a32c055f 5685 ioarcb->ioadl_len = 0;
1da177e4 5686 ioarcb->read_ioadl_len = 0;
96d21f00
WB
5687 ioasa->hdr.ioasc = 0;
5688 ioasa->hdr.residual_data_len = 0;
a32c055f
WB
5689
5690 if (ipr_cmd->ioa_cfg->sis64)
5691 ioarcb->u.sis64_addr_data.data_ioadl_addr =
5692 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
5693 else {
5694 ioarcb->write_ioadl_addr =
5695 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
5696 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5697 }
1da177e4
LT
5698}
5699
5700/**
5701 * ipr_erp_request_sense - Send request sense to a device
5702 * @ipr_cmd: ipr command struct
5703 *
5704 * This function sends a request sense to a device as a result
5705 * of a check condition.
5706 *
5707 * Return value:
5708 * nothing
5709 **/
5710static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
5711{
5712 struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
96d21f00 5713 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1da177e4
LT
5714
5715 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
5716 ipr_erp_done(ipr_cmd);
5717 return;
5718 }
5719
5720 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
5721
5722 cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
5723 cmd_pkt->cdb[0] = REQUEST_SENSE;
5724 cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
5725 cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
5726 cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5727 cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
5728
a32c055f
WB
5729 ipr_init_ioadl(ipr_cmd, ipr_cmd->sense_buffer_dma,
5730 SCSI_SENSE_BUFFERSIZE, IPR_IOADL_FLAGS_READ_LAST);
1da177e4
LT
5731
5732 ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
5733 IPR_REQUEST_SENSE_TIMEOUT * 2);
5734}
5735
5736/**
5737 * ipr_erp_cancel_all - Send cancel all to a device
5738 * @ipr_cmd: ipr command struct
5739 *
5740 * This function sends a cancel all to a device to clear the
5741 * queue. If we are running TCQ on the device, QERR is set to 1,
5742 * which means all outstanding ops have been dropped on the floor.
5743 * Cancel all will return them to us.
5744 *
5745 * Return value:
5746 * nothing
5747 **/
5748static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
5749{
5750 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5751 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5752 struct ipr_cmd_pkt *cmd_pkt;
5753
5754 res->in_erp = 1;
5755
5756 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
5757
5758 if (!scsi_get_tag_type(scsi_cmd->device)) {
5759 ipr_erp_request_sense(ipr_cmd);
5760 return;
5761 }
5762
5763 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5764 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5765 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
5766
5767 ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
5768 IPR_CANCEL_ALL_TIMEOUT);
5769}
5770
5771/**
5772 * ipr_dump_ioasa - Dump contents of IOASA
5773 * @ioa_cfg: ioa config struct
5774 * @ipr_cmd: ipr command struct
fe964d0a 5775 * @res: resource entry struct
1da177e4
LT
5776 *
5777 * This function is invoked by the interrupt handler when ops
5778 * fail. It will log the IOASA if appropriate. Only called
5779 * for GPDD ops.
5780 *
5781 * Return value:
5782 * none
5783 **/
5784static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
fe964d0a 5785 struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
1da177e4
LT
5786{
5787 int i;
5788 u16 data_len;
b0692dd4 5789 u32 ioasc, fd_ioasc;
96d21f00 5790 struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
1da177e4
LT
5791 __be32 *ioasa_data = (__be32 *)ioasa;
5792 int error_index;
5793
96d21f00
WB
5794 ioasc = be32_to_cpu(ioasa->hdr.ioasc) & IPR_IOASC_IOASC_MASK;
5795 fd_ioasc = be32_to_cpu(ioasa->hdr.fd_ioasc) & IPR_IOASC_IOASC_MASK;
1da177e4
LT
5796
5797 if (0 == ioasc)
5798 return;
5799
5800 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
5801 return;
5802
b0692dd4
BK
5803 if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
5804 error_index = ipr_get_error(fd_ioasc);
5805 else
5806 error_index = ipr_get_error(ioasc);
1da177e4
LT
5807
5808 if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
5809 /* Don't log an error if the IOA already logged one */
96d21f00 5810 if (ioasa->hdr.ilid != 0)
1da177e4
LT
5811 return;
5812
cc9bd5d4
BK
5813 if (!ipr_is_gscsi(res))
5814 return;
5815
1da177e4
LT
5816 if (ipr_error_table[error_index].log_ioasa == 0)
5817 return;
5818 }
5819
fe964d0a 5820 ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
1da177e4 5821
96d21f00
WB
5822 data_len = be16_to_cpu(ioasa->hdr.ret_stat_len);
5823 if (ioa_cfg->sis64 && sizeof(struct ipr_ioasa64) < data_len)
5824 data_len = sizeof(struct ipr_ioasa64);
5825 else if (!ioa_cfg->sis64 && sizeof(struct ipr_ioasa) < data_len)
1da177e4 5826 data_len = sizeof(struct ipr_ioasa);
1da177e4
LT
5827
5828 ipr_err("IOASA Dump:\n");
5829
5830 for (i = 0; i < data_len / 4; i += 4) {
5831 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
5832 be32_to_cpu(ioasa_data[i]),
5833 be32_to_cpu(ioasa_data[i+1]),
5834 be32_to_cpu(ioasa_data[i+2]),
5835 be32_to_cpu(ioasa_data[i+3]));
5836 }
5837}
5838
5839/**
5840 * ipr_gen_sense - Generate SCSI sense data from an IOASA
5841 * @ioasa: IOASA
5842 * @sense_buf: sense data buffer
5843 *
5844 * Return value:
5845 * none
5846 **/
5847static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
5848{
5849 u32 failing_lba;
5850 u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
5851 struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
96d21f00
WB
5852 struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5853 u32 ioasc = be32_to_cpu(ioasa->hdr.ioasc);
1da177e4
LT
5854
5855 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
5856
5857 if (ioasc >= IPR_FIRST_DRIVER_IOASC)
5858 return;
5859
5860 ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
5861
5862 if (ipr_is_vset_device(res) &&
5863 ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
5864 ioasa->u.vset.failing_lba_hi != 0) {
5865 sense_buf[0] = 0x72;
5866 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
5867 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
5868 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
5869
5870 sense_buf[7] = 12;
5871 sense_buf[8] = 0;
5872 sense_buf[9] = 0x0A;
5873 sense_buf[10] = 0x80;
5874
5875 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
5876
5877 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
5878 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
5879 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
5880 sense_buf[15] = failing_lba & 0x000000ff;
5881
5882 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
5883
5884 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
5885 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
5886 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
5887 sense_buf[19] = failing_lba & 0x000000ff;
5888 } else {
5889 sense_buf[0] = 0x70;
5890 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
5891 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
5892 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
5893
5894 /* Illegal request */
5895 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
96d21f00 5896 (be32_to_cpu(ioasa->hdr.ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
1da177e4
LT
5897 sense_buf[7] = 10; /* additional length */
5898
5899 /* IOARCB was in error */
5900 if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
5901 sense_buf[15] = 0xC0;
5902 else /* Parameter data was invalid */
5903 sense_buf[15] = 0x80;
5904
5905 sense_buf[16] =
5906 ((IPR_FIELD_POINTER_MASK &
96d21f00 5907 be32_to_cpu(ioasa->hdr.ioasc_specific)) >> 8) & 0xff;
1da177e4
LT
5908 sense_buf[17] =
5909 (IPR_FIELD_POINTER_MASK &
96d21f00 5910 be32_to_cpu(ioasa->hdr.ioasc_specific)) & 0xff;
1da177e4
LT
5911 } else {
5912 if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
5913 if (ipr_is_vset_device(res))
5914 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
5915 else
5916 failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
5917
5918 sense_buf[0] |= 0x80; /* Or in the Valid bit */
5919 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
5920 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
5921 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
5922 sense_buf[6] = failing_lba & 0x000000ff;
5923 }
5924
5925 sense_buf[7] = 6; /* additional length */
5926 }
5927 }
5928}
5929
ee0a90fa
BK
5930/**
5931 * ipr_get_autosense - Copy autosense data to sense buffer
5932 * @ipr_cmd: ipr command struct
5933 *
5934 * This function copies the autosense buffer to the buffer
5935 * in the scsi_cmd, if there is autosense available.
5936 *
5937 * Return value:
5938 * 1 if autosense was available / 0 if not
5939 **/
5940static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
5941{
96d21f00
WB
5942 struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5943 struct ipr_ioasa64 *ioasa64 = &ipr_cmd->s.ioasa64;
ee0a90fa 5944
96d21f00 5945 if ((be32_to_cpu(ioasa->hdr.ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
ee0a90fa
BK
5946 return 0;
5947
96d21f00
WB
5948 if (ipr_cmd->ioa_cfg->sis64)
5949 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa64->auto_sense.data,
5950 min_t(u16, be16_to_cpu(ioasa64->auto_sense.auto_sense_len),
5951 SCSI_SENSE_BUFFERSIZE));
5952 else
5953 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
5954 min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
5955 SCSI_SENSE_BUFFERSIZE));
ee0a90fa
BK
5956 return 1;
5957}
5958
1da177e4
LT
5959/**
5960 * ipr_erp_start - Process an error response for a SCSI op
5961 * @ioa_cfg: ioa config struct
5962 * @ipr_cmd: ipr command struct
5963 *
5964 * This function determines whether or not to initiate ERP
5965 * on the affected device.
5966 *
5967 * Return value:
5968 * nothing
5969 **/
5970static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
5971 struct ipr_cmnd *ipr_cmd)
5972{
5973 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5974 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
96d21f00 5975 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
8a048994 5976 u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
1da177e4
LT
5977
5978 if (!res) {
5979 ipr_scsi_eh_done(ipr_cmd);
5980 return;
5981 }
5982
8a048994 5983 if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
1da177e4
LT
5984 ipr_gen_sense(ipr_cmd);
5985
cc9bd5d4
BK
5986 ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
5987
8a048994 5988 switch (masked_ioasc) {
1da177e4 5989 case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
ee0a90fa
BK
5990 if (ipr_is_naca_model(res))
5991 scsi_cmd->result |= (DID_ABORT << 16);
5992 else
5993 scsi_cmd->result |= (DID_IMM_RETRY << 16);
1da177e4
LT
5994 break;
5995 case IPR_IOASC_IR_RESOURCE_HANDLE:
b0df54bb 5996 case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
1da177e4
LT
5997 scsi_cmd->result |= (DID_NO_CONNECT << 16);
5998 break;
5999 case IPR_IOASC_HW_SEL_TIMEOUT:
6000 scsi_cmd->result |= (DID_NO_CONNECT << 16);
ee0a90fa
BK
6001 if (!ipr_is_naca_model(res))
6002 res->needs_sync_complete = 1;
1da177e4
LT
6003 break;
6004 case IPR_IOASC_SYNC_REQUIRED:
6005 if (!res->in_erp)
6006 res->needs_sync_complete = 1;
6007 scsi_cmd->result |= (DID_IMM_RETRY << 16);
6008 break;
6009 case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
b0df54bb 6010 case IPR_IOASA_IR_DUAL_IOA_DISABLED:
1da177e4
LT
6011 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
6012 break;
6013 case IPR_IOASC_BUS_WAS_RESET:
6014 case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
6015 /*
6016 * Report the bus reset and ask for a retry. The device
6017 * will give CC/UA the next command.
6018 */
6019 if (!res->resetting_device)
6020 scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
6021 scsi_cmd->result |= (DID_ERROR << 16);
ee0a90fa
BK
6022 if (!ipr_is_naca_model(res))
6023 res->needs_sync_complete = 1;
1da177e4
LT
6024 break;
6025 case IPR_IOASC_HW_DEV_BUS_STATUS:
6026 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
6027 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
ee0a90fa
BK
6028 if (!ipr_get_autosense(ipr_cmd)) {
6029 if (!ipr_is_naca_model(res)) {
6030 ipr_erp_cancel_all(ipr_cmd);
6031 return;
6032 }
6033 }
1da177e4 6034 }
ee0a90fa
BK
6035 if (!ipr_is_naca_model(res))
6036 res->needs_sync_complete = 1;
1da177e4
LT
6037 break;
6038 case IPR_IOASC_NR_INIT_CMD_REQUIRED:
6039 break;
6040 default:
5b7304fb
BK
6041 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
6042 scsi_cmd->result |= (DID_ERROR << 16);
ee0a90fa 6043 if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
1da177e4
LT
6044 res->needs_sync_complete = 1;
6045 break;
6046 }
6047
63015bc9 6048 scsi_dma_unmap(ipr_cmd->scsi_cmd);
05a6538a 6049 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
1da177e4
LT
6050 scsi_cmd->scsi_done(scsi_cmd);
6051}
6052
6053/**
6054 * ipr_scsi_done - mid-layer done function
6055 * @ipr_cmd: ipr command struct
6056 *
6057 * This function is invoked by the interrupt handler for
6058 * ops generated by the SCSI mid-layer
6059 *
6060 * Return value:
6061 * none
6062 **/
6063static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
6064{
6065 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6066 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
96d21f00 6067 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
05a6538a 6068 unsigned long hrrq_flags;
1da177e4 6069
96d21f00 6070 scsi_set_resid(scsi_cmd, be32_to_cpu(ipr_cmd->s.ioasa.hdr.residual_data_len));
1da177e4
LT
6071
6072 if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
172cd6e1
BK
6073 scsi_dma_unmap(scsi_cmd);
6074
56d6aa33 6075 spin_lock_irqsave(ipr_cmd->hrrq->lock, hrrq_flags);
05a6538a 6076 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
1da177e4 6077 scsi_cmd->scsi_done(scsi_cmd);
56d6aa33 6078 spin_unlock_irqrestore(ipr_cmd->hrrq->lock, hrrq_flags);
172cd6e1 6079 } else {
56d6aa33 6080 spin_lock_irqsave(ipr_cmd->hrrq->lock, hrrq_flags);
1da177e4 6081 ipr_erp_start(ioa_cfg, ipr_cmd);
56d6aa33 6082 spin_unlock_irqrestore(ipr_cmd->hrrq->lock, hrrq_flags);
172cd6e1 6083 }
1da177e4
LT
6084}
6085
1da177e4
LT
6086/**
6087 * ipr_queuecommand - Queue a mid-layer request
00bfef2c 6088 * @shost: scsi host struct
1da177e4 6089 * @scsi_cmd: scsi command struct
1da177e4
LT
6090 *
6091 * This function queues a request generated by the mid-layer.
6092 *
6093 * Return value:
6094 * 0 on success
6095 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
6096 * SCSI_MLQUEUE_HOST_BUSY if host is busy
6097 **/
00bfef2c
BK
6098static int ipr_queuecommand(struct Scsi_Host *shost,
6099 struct scsi_cmnd *scsi_cmd)
1da177e4
LT
6100{
6101 struct ipr_ioa_cfg *ioa_cfg;
6102 struct ipr_resource_entry *res;
6103 struct ipr_ioarcb *ioarcb;
6104 struct ipr_cmnd *ipr_cmd;
56d6aa33 6105 unsigned long hrrq_flags, lock_flags;
d12f1576 6106 int rc;
05a6538a 6107 struct ipr_hrr_queue *hrrq;
6108 int hrrq_id;
1da177e4 6109
00bfef2c
BK
6110 ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
6111
1da177e4 6112 scsi_cmd->result = (DID_OK << 16);
00bfef2c 6113 res = scsi_cmd->device->hostdata;
56d6aa33 6114
6115 if (ipr_is_gata(res) && res->sata_port) {
6116 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
6117 rc = ata_sas_queuecmd(scsi_cmd, res->sata_port->ap);
6118 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
6119 return rc;
6120 }
6121
05a6538a 6122 hrrq_id = ipr_get_hrrq_index(ioa_cfg);
6123 hrrq = &ioa_cfg->hrrq[hrrq_id];
1da177e4 6124
56d6aa33 6125 spin_lock_irqsave(hrrq->lock, hrrq_flags);
1da177e4
LT
6126 /*
6127 * We are currently blocking all devices due to a host reset
6128 * We have told the host to stop giving us new requests, but
6129 * ERP ops don't count. FIXME
6130 */
bfae7820 6131 if (unlikely(!hrrq->allow_cmds && !hrrq->ioa_is_dead && !hrrq->removing_ioa)) {
56d6aa33 6132 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
1da177e4 6133 return SCSI_MLQUEUE_HOST_BUSY;
00bfef2c 6134 }
1da177e4
LT
6135
6136 /*
6137 * FIXME - Create scsi_set_host_offline interface
6138 * and the ioa_is_dead check can be removed
6139 */
bfae7820 6140 if (unlikely(hrrq->ioa_is_dead || hrrq->removing_ioa || !res)) {
56d6aa33 6141 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
00bfef2c 6142 goto err_nodev;
1da177e4
LT
6143 }
6144
05a6538a 6145 ipr_cmd = __ipr_get_free_ipr_cmnd(hrrq);
6146 if (ipr_cmd == NULL) {
56d6aa33 6147 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
05a6538a 6148 return SCSI_MLQUEUE_HOST_BUSY;
6149 }
56d6aa33 6150 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
35a39691 6151
172cd6e1 6152 ipr_init_ipr_cmnd(ipr_cmd, ipr_scsi_done);
1da177e4 6153 ioarcb = &ipr_cmd->ioarcb;
1da177e4
LT
6154
6155 memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
6156 ipr_cmd->scsi_cmd = scsi_cmd;
172cd6e1 6157 ipr_cmd->done = ipr_scsi_eh_done;
1da177e4
LT
6158
6159 if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
6160 if (scsi_cmd->underflow == 0)
6161 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
6162
1da177e4 6163 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
0b1f8d44
WX
6164 if (ipr_is_gscsi(res) && res->reset_occurred) {
6165 res->reset_occurred = 0;
ab6c10b1 6166 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
0b1f8d44 6167 }
1da177e4 6168 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
50668633
CH
6169 if (scsi_cmd->flags & SCMD_TAGGED)
6170 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_SIMPLE_TASK;
6171 else
6172 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_UNTAGGED_TASK;
1da177e4
LT
6173 }
6174
6175 if (scsi_cmd->cmnd[0] >= 0xC0 &&
05a6538a 6176 (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE)) {
1da177e4 6177 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
05a6538a 6178 }
1da177e4 6179
d12f1576
DC
6180 if (ioa_cfg->sis64)
6181 rc = ipr_build_ioadl64(ioa_cfg, ipr_cmd);
6182 else
6183 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
1da177e4 6184
56d6aa33 6185 spin_lock_irqsave(hrrq->lock, hrrq_flags);
6186 if (unlikely(rc || (!hrrq->allow_cmds && !hrrq->ioa_is_dead))) {
05a6538a 6187 list_add_tail(&ipr_cmd->queue, &hrrq->hrrq_free_q);
56d6aa33 6188 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
00bfef2c
BK
6189 if (!rc)
6190 scsi_dma_unmap(scsi_cmd);
a5fb407e 6191 return SCSI_MLQUEUE_HOST_BUSY;
1da177e4
LT
6192 }
6193
56d6aa33 6194 if (unlikely(hrrq->ioa_is_dead)) {
05a6538a 6195 list_add_tail(&ipr_cmd->queue, &hrrq->hrrq_free_q);
56d6aa33 6196 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
00bfef2c
BK
6197 scsi_dma_unmap(scsi_cmd);
6198 goto err_nodev;
6199 }
6200
6201 ioarcb->res_handle = res->res_handle;
6202 if (res->needs_sync_complete) {
6203 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
6204 res->needs_sync_complete = 0;
6205 }
05a6538a 6206 list_add_tail(&ipr_cmd->queue, &hrrq->hrrq_pending_q);
00bfef2c 6207 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
a5fb407e 6208 ipr_send_command(ipr_cmd);
56d6aa33 6209 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
1da177e4 6210 return 0;
1da177e4 6211
00bfef2c 6212err_nodev:
56d6aa33 6213 spin_lock_irqsave(hrrq->lock, hrrq_flags);
00bfef2c
BK
6214 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
6215 scsi_cmd->result = (DID_NO_CONNECT << 16);
6216 scsi_cmd->scsi_done(scsi_cmd);
56d6aa33 6217 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
00bfef2c
BK
6218 return 0;
6219}
f281233d 6220
35a39691
BK
6221/**
6222 * ipr_ioctl - IOCTL handler
6223 * @sdev: scsi device struct
6224 * @cmd: IOCTL cmd
6225 * @arg: IOCTL arg
6226 *
6227 * Return value:
6228 * 0 on success / other on failure
6229 **/
bd705f2d 6230static int ipr_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
35a39691
BK
6231{
6232 struct ipr_resource_entry *res;
6233
6234 res = (struct ipr_resource_entry *)sdev->hostdata;
0ce3a7e5
BK
6235 if (res && ipr_is_gata(res)) {
6236 if (cmd == HDIO_GET_IDENTITY)
6237 return -ENOTTY;
94be9a58 6238 return ata_sas_scsi_ioctl(res->sata_port->ap, sdev, cmd, arg);
0ce3a7e5 6239 }
35a39691
BK
6240
6241 return -EINVAL;
6242}
6243
1da177e4
LT
6244/**
6245 * ipr_info - Get information about the card/driver
6246 * @scsi_host: scsi host struct
6247 *
6248 * Return value:
6249 * pointer to buffer with description string
6250 **/
203fa3fe 6251static const char *ipr_ioa_info(struct Scsi_Host *host)
1da177e4
LT
6252{
6253 static char buffer[512];
6254 struct ipr_ioa_cfg *ioa_cfg;
6255 unsigned long lock_flags = 0;
6256
6257 ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
6258
6259 spin_lock_irqsave(host->host_lock, lock_flags);
6260 sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
6261 spin_unlock_irqrestore(host->host_lock, lock_flags);
6262
6263 return buffer;
6264}
6265
6266static struct scsi_host_template driver_template = {
6267 .module = THIS_MODULE,
6268 .name = "IPR",
6269 .info = ipr_ioa_info,
35a39691 6270 .ioctl = ipr_ioctl,
1da177e4
LT
6271 .queuecommand = ipr_queuecommand,
6272 .eh_abort_handler = ipr_eh_abort,
6273 .eh_device_reset_handler = ipr_eh_dev_reset,
6274 .eh_host_reset_handler = ipr_eh_host_reset,
6275 .slave_alloc = ipr_slave_alloc,
6276 .slave_configure = ipr_slave_configure,
6277 .slave_destroy = ipr_slave_destroy,
35a39691
BK
6278 .target_alloc = ipr_target_alloc,
6279 .target_destroy = ipr_target_destroy,
1da177e4 6280 .change_queue_depth = ipr_change_queue_depth,
1da177e4
LT
6281 .bios_param = ipr_biosparam,
6282 .can_queue = IPR_MAX_COMMANDS,
6283 .this_id = -1,
6284 .sg_tablesize = IPR_MAX_SGLIST,
6285 .max_sectors = IPR_IOA_MAX_SECTORS,
6286 .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
6287 .use_clustering = ENABLE_CLUSTERING,
6288 .shost_attrs = ipr_ioa_attrs,
6289 .sdev_attrs = ipr_dev_attrs,
54b2b50c
MP
6290 .proc_name = IPR_NAME,
6291 .no_write_same = 1,
2ecb204d 6292 .use_blk_tags = 1,
1da177e4
LT
6293};
6294
35a39691
BK
6295/**
6296 * ipr_ata_phy_reset - libata phy_reset handler
6297 * @ap: ata port to reset
6298 *
6299 **/
6300static void ipr_ata_phy_reset(struct ata_port *ap)
6301{
6302 unsigned long flags;
6303 struct ipr_sata_port *sata_port = ap->private_data;
6304 struct ipr_resource_entry *res = sata_port->res;
6305 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
6306 int rc;
6307
6308 ENTER;
6309 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
203fa3fe 6310 while (ioa_cfg->in_reset_reload) {
35a39691
BK
6311 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6312 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
6313 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6314 }
6315
56d6aa33 6316 if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds)
35a39691
BK
6317 goto out_unlock;
6318
6319 rc = ipr_device_reset(ioa_cfg, res);
6320
6321 if (rc) {
3e4ec344 6322 ap->link.device[0].class = ATA_DEV_NONE;
35a39691
BK
6323 goto out_unlock;
6324 }
6325
3e7ebdfa
WB
6326 ap->link.device[0].class = res->ata_class;
6327 if (ap->link.device[0].class == ATA_DEV_UNKNOWN)
3e4ec344 6328 ap->link.device[0].class = ATA_DEV_NONE;
35a39691
BK
6329
6330out_unlock:
6331 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6332 LEAVE;
6333}
6334
6335/**
6336 * ipr_ata_post_internal - Cleanup after an internal command
6337 * @qc: ATA queued command
6338 *
6339 * Return value:
6340 * none
6341 **/
6342static void ipr_ata_post_internal(struct ata_queued_cmd *qc)
6343{
6344 struct ipr_sata_port *sata_port = qc->ap->private_data;
6345 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
6346 struct ipr_cmnd *ipr_cmd;
05a6538a 6347 struct ipr_hrr_queue *hrrq;
35a39691
BK
6348 unsigned long flags;
6349
6350 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
203fa3fe 6351 while (ioa_cfg->in_reset_reload) {
73d98ff0
BK
6352 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6353 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
6354 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6355 }
6356
05a6538a 6357 for_each_hrrq(hrrq, ioa_cfg) {
56d6aa33 6358 spin_lock(&hrrq->_lock);
05a6538a 6359 list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
6360 if (ipr_cmd->qc == qc) {
6361 ipr_device_reset(ioa_cfg, sata_port->res);
6362 break;
6363 }
35a39691 6364 }
56d6aa33 6365 spin_unlock(&hrrq->_lock);
35a39691
BK
6366 }
6367 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6368}
6369
35a39691
BK
6370/**
6371 * ipr_copy_sata_tf - Copy a SATA taskfile to an IOA data structure
6372 * @regs: destination
6373 * @tf: source ATA taskfile
6374 *
6375 * Return value:
6376 * none
6377 **/
6378static void ipr_copy_sata_tf(struct ipr_ioarcb_ata_regs *regs,
6379 struct ata_taskfile *tf)
6380{
6381 regs->feature = tf->feature;
6382 regs->nsect = tf->nsect;
6383 regs->lbal = tf->lbal;
6384 regs->lbam = tf->lbam;
6385 regs->lbah = tf->lbah;
6386 regs->device = tf->device;
6387 regs->command = tf->command;
6388 regs->hob_feature = tf->hob_feature;
6389 regs->hob_nsect = tf->hob_nsect;
6390 regs->hob_lbal = tf->hob_lbal;
6391 regs->hob_lbam = tf->hob_lbam;
6392 regs->hob_lbah = tf->hob_lbah;
6393 regs->ctl = tf->ctl;
6394}
6395
6396/**
6397 * ipr_sata_done - done function for SATA commands
6398 * @ipr_cmd: ipr command struct
6399 *
6400 * This function is invoked by the interrupt handler for
6401 * ops generated by the SCSI mid-layer to SATA devices
6402 *
6403 * Return value:
6404 * none
6405 **/
6406static void ipr_sata_done(struct ipr_cmnd *ipr_cmd)
6407{
6408 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6409 struct ata_queued_cmd *qc = ipr_cmd->qc;
6410 struct ipr_sata_port *sata_port = qc->ap->private_data;
6411 struct ipr_resource_entry *res = sata_port->res;
96d21f00 6412 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
35a39691 6413
56d6aa33 6414 spin_lock(&ipr_cmd->hrrq->_lock);
96d21f00
WB
6415 if (ipr_cmd->ioa_cfg->sis64)
6416 memcpy(&sata_port->ioasa, &ipr_cmd->s.ioasa64.u.gata,
6417 sizeof(struct ipr_ioasa_gata));
6418 else
6419 memcpy(&sata_port->ioasa, &ipr_cmd->s.ioasa.u.gata,
6420 sizeof(struct ipr_ioasa_gata));
35a39691
BK
6421 ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
6422
96d21f00 6423 if (be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc_specific) & IPR_ATA_DEVICE_WAS_RESET)
3e7ebdfa 6424 scsi_report_device_reset(ioa_cfg->host, res->bus, res->target);
35a39691
BK
6425
6426 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
96d21f00 6427 qc->err_mask |= __ac_err_mask(sata_port->ioasa.status);
35a39691 6428 else
96d21f00 6429 qc->err_mask |= ac_err_mask(sata_port->ioasa.status);
05a6538a 6430 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
56d6aa33 6431 spin_unlock(&ipr_cmd->hrrq->_lock);
35a39691
BK
6432 ata_qc_complete(qc);
6433}
6434
a32c055f
WB
6435/**
6436 * ipr_build_ata_ioadl64 - Build an ATA scatter/gather list
6437 * @ipr_cmd: ipr command struct
6438 * @qc: ATA queued command
6439 *
6440 **/
6441static void ipr_build_ata_ioadl64(struct ipr_cmnd *ipr_cmd,
6442 struct ata_queued_cmd *qc)
6443{
6444 u32 ioadl_flags = 0;
6445 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
1ac7c26d 6446 struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ata_ioadl.ioadl64;
a32c055f
WB
6447 struct ipr_ioadl64_desc *last_ioadl64 = NULL;
6448 int len = qc->nbytes;
6449 struct scatterlist *sg;
6450 unsigned int si;
6451 dma_addr_t dma_addr = ipr_cmd->dma_addr;
6452
6453 if (len == 0)
6454 return;
6455
6456 if (qc->dma_dir == DMA_TO_DEVICE) {
6457 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
6458 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6459 } else if (qc->dma_dir == DMA_FROM_DEVICE)
6460 ioadl_flags = IPR_IOADL_FLAGS_READ;
6461
6462 ioarcb->data_transfer_length = cpu_to_be32(len);
6463 ioarcb->ioadl_len =
6464 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
6465 ioarcb->u.sis64_addr_data.data_ioadl_addr =
1ac7c26d 6466 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ata_ioadl.ioadl64));
a32c055f
WB
6467
6468 for_each_sg(qc->sg, sg, qc->n_elem, si) {
6469 ioadl64->flags = cpu_to_be32(ioadl_flags);
6470 ioadl64->data_len = cpu_to_be32(sg_dma_len(sg));
6471 ioadl64->address = cpu_to_be64(sg_dma_address(sg));
6472
6473 last_ioadl64 = ioadl64;
6474 ioadl64++;
6475 }
6476
6477 if (likely(last_ioadl64))
6478 last_ioadl64->flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
6479}
6480
35a39691
BK
6481/**
6482 * ipr_build_ata_ioadl - Build an ATA scatter/gather list
6483 * @ipr_cmd: ipr command struct
6484 * @qc: ATA queued command
6485 *
6486 **/
6487static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
6488 struct ata_queued_cmd *qc)
6489{
6490 u32 ioadl_flags = 0;
6491 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
a32c055f 6492 struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
3be6cbd7 6493 struct ipr_ioadl_desc *last_ioadl = NULL;
dde20207 6494 int len = qc->nbytes;
35a39691 6495 struct scatterlist *sg;
ff2aeb1e 6496 unsigned int si;
35a39691
BK
6497
6498 if (len == 0)
6499 return;
6500
6501 if (qc->dma_dir == DMA_TO_DEVICE) {
6502 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
6503 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
a32c055f
WB
6504 ioarcb->data_transfer_length = cpu_to_be32(len);
6505 ioarcb->ioadl_len =
35a39691
BK
6506 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
6507 } else if (qc->dma_dir == DMA_FROM_DEVICE) {
6508 ioadl_flags = IPR_IOADL_FLAGS_READ;
6509 ioarcb->read_data_transfer_length = cpu_to_be32(len);
6510 ioarcb->read_ioadl_len =
6511 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
6512 }
6513
ff2aeb1e 6514 for_each_sg(qc->sg, sg, qc->n_elem, si) {
35a39691
BK
6515 ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
6516 ioadl->address = cpu_to_be32(sg_dma_address(sg));
3be6cbd7
JG
6517
6518 last_ioadl = ioadl;
6519 ioadl++;
35a39691 6520 }
3be6cbd7
JG
6521
6522 if (likely(last_ioadl))
6523 last_ioadl->flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
35a39691
BK
6524}
6525
56d6aa33 6526/**
6527 * ipr_qc_defer - Get a free ipr_cmd
6528 * @qc: queued command
6529 *
6530 * Return value:
6531 * 0 if success
6532 **/
6533static int ipr_qc_defer(struct ata_queued_cmd *qc)
6534{
6535 struct ata_port *ap = qc->ap;
6536 struct ipr_sata_port *sata_port = ap->private_data;
6537 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
6538 struct ipr_cmnd *ipr_cmd;
6539 struct ipr_hrr_queue *hrrq;
6540 int hrrq_id;
6541
6542 hrrq_id = ipr_get_hrrq_index(ioa_cfg);
6543 hrrq = &ioa_cfg->hrrq[hrrq_id];
6544
6545 qc->lldd_task = NULL;
6546 spin_lock(&hrrq->_lock);
6547 if (unlikely(hrrq->ioa_is_dead)) {
6548 spin_unlock(&hrrq->_lock);
6549 return 0;
6550 }
6551
6552 if (unlikely(!hrrq->allow_cmds)) {
6553 spin_unlock(&hrrq->_lock);
6554 return ATA_DEFER_LINK;
6555 }
6556
6557 ipr_cmd = __ipr_get_free_ipr_cmnd(hrrq);
6558 if (ipr_cmd == NULL) {
6559 spin_unlock(&hrrq->_lock);
6560 return ATA_DEFER_LINK;
6561 }
6562
6563 qc->lldd_task = ipr_cmd;
6564 spin_unlock(&hrrq->_lock);
6565 return 0;
6566}
6567
35a39691
BK
6568/**
6569 * ipr_qc_issue - Issue a SATA qc to a device
6570 * @qc: queued command
6571 *
6572 * Return value:
6573 * 0 if success
6574 **/
6575static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
6576{
6577 struct ata_port *ap = qc->ap;
6578 struct ipr_sata_port *sata_port = ap->private_data;
6579 struct ipr_resource_entry *res = sata_port->res;
6580 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
6581 struct ipr_cmnd *ipr_cmd;
6582 struct ipr_ioarcb *ioarcb;
6583 struct ipr_ioarcb_ata_regs *regs;
6584
56d6aa33 6585 if (qc->lldd_task == NULL)
6586 ipr_qc_defer(qc);
6587
6588 ipr_cmd = qc->lldd_task;
6589 if (ipr_cmd == NULL)
0feeed82 6590 return AC_ERR_SYSTEM;
35a39691 6591
56d6aa33 6592 qc->lldd_task = NULL;
6593 spin_lock(&ipr_cmd->hrrq->_lock);
6594 if (unlikely(!ipr_cmd->hrrq->allow_cmds ||
6595 ipr_cmd->hrrq->ioa_is_dead)) {
6596 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6597 spin_unlock(&ipr_cmd->hrrq->_lock);
6598 return AC_ERR_SYSTEM;
6599 }
6600
05a6538a 6601 ipr_init_ipr_cmnd(ipr_cmd, ipr_lock_and_done);
35a39691 6602 ioarcb = &ipr_cmd->ioarcb;
35a39691 6603
a32c055f
WB
6604 if (ioa_cfg->sis64) {
6605 regs = &ipr_cmd->i.ata_ioadl.regs;
6606 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
6607 } else
6608 regs = &ioarcb->u.add_data.u.regs;
6609
6610 memset(regs, 0, sizeof(*regs));
6611 ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(*regs));
35a39691 6612
56d6aa33 6613 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
35a39691
BK
6614 ipr_cmd->qc = qc;
6615 ipr_cmd->done = ipr_sata_done;
3e7ebdfa 6616 ipr_cmd->ioarcb.res_handle = res->res_handle;
35a39691
BK
6617 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_ATA_PASSTHRU;
6618 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
6619 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
dde20207 6620 ipr_cmd->dma_use_sg = qc->n_elem;
35a39691 6621
a32c055f
WB
6622 if (ioa_cfg->sis64)
6623 ipr_build_ata_ioadl64(ipr_cmd, qc);
6624 else
6625 ipr_build_ata_ioadl(ipr_cmd, qc);
6626
35a39691
BK
6627 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
6628 ipr_copy_sata_tf(regs, &qc->tf);
6629 memcpy(ioarcb->cmd_pkt.cdb, qc->cdb, IPR_MAX_CDB_LEN);
3e7ebdfa 6630 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
35a39691
BK
6631
6632 switch (qc->tf.protocol) {
6633 case ATA_PROT_NODATA:
6634 case ATA_PROT_PIO:
6635 break;
6636
6637 case ATA_PROT_DMA:
6638 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
6639 break;
6640
0dc36888
TH
6641 case ATAPI_PROT_PIO:
6642 case ATAPI_PROT_NODATA:
35a39691
BK
6643 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
6644 break;
6645
0dc36888 6646 case ATAPI_PROT_DMA:
35a39691
BK
6647 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
6648 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
6649 break;
6650
6651 default:
6652 WARN_ON(1);
56d6aa33 6653 spin_unlock(&ipr_cmd->hrrq->_lock);
0feeed82 6654 return AC_ERR_INVALID;
35a39691
BK
6655 }
6656
a32c055f 6657 ipr_send_command(ipr_cmd);
56d6aa33 6658 spin_unlock(&ipr_cmd->hrrq->_lock);
a32c055f 6659
35a39691
BK
6660 return 0;
6661}
6662
4c9bf4e7
TH
6663/**
6664 * ipr_qc_fill_rtf - Read result TF
6665 * @qc: ATA queued command
6666 *
6667 * Return value:
6668 * true
6669 **/
6670static bool ipr_qc_fill_rtf(struct ata_queued_cmd *qc)
6671{
6672 struct ipr_sata_port *sata_port = qc->ap->private_data;
6673 struct ipr_ioasa_gata *g = &sata_port->ioasa;
6674 struct ata_taskfile *tf = &qc->result_tf;
6675
6676 tf->feature = g->error;
6677 tf->nsect = g->nsect;
6678 tf->lbal = g->lbal;
6679 tf->lbam = g->lbam;
6680 tf->lbah = g->lbah;
6681 tf->device = g->device;
6682 tf->command = g->status;
6683 tf->hob_nsect = g->hob_nsect;
6684 tf->hob_lbal = g->hob_lbal;
6685 tf->hob_lbam = g->hob_lbam;
6686 tf->hob_lbah = g->hob_lbah;
4c9bf4e7
TH
6687
6688 return true;
6689}
6690
35a39691 6691static struct ata_port_operations ipr_sata_ops = {
35a39691 6692 .phy_reset = ipr_ata_phy_reset,
a1efdaba 6693 .hardreset = ipr_sata_reset,
35a39691 6694 .post_internal_cmd = ipr_ata_post_internal,
35a39691 6695 .qc_prep = ata_noop_qc_prep,
56d6aa33 6696 .qc_defer = ipr_qc_defer,
35a39691 6697 .qc_issue = ipr_qc_issue,
4c9bf4e7 6698 .qc_fill_rtf = ipr_qc_fill_rtf,
35a39691
BK
6699 .port_start = ata_sas_port_start,
6700 .port_stop = ata_sas_port_stop
6701};
6702
6703static struct ata_port_info sata_port_info = {
9cbe056f 6704 .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA,
0f2e0330
SS
6705 .pio_mask = ATA_PIO4_ONLY,
6706 .mwdma_mask = ATA_MWDMA2,
6707 .udma_mask = ATA_UDMA6,
35a39691
BK
6708 .port_ops = &ipr_sata_ops
6709};
6710
1da177e4
LT
6711#ifdef CONFIG_PPC_PSERIES
6712static const u16 ipr_blocked_processors[] = {
d3dbeef6
ME
6713 PVR_NORTHSTAR,
6714 PVR_PULSAR,
6715 PVR_POWER4,
6716 PVR_ICESTAR,
6717 PVR_SSTAR,
6718 PVR_POWER4p,
6719 PVR_630,
6720 PVR_630p
1da177e4
LT
6721};
6722
6723/**
6724 * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
6725 * @ioa_cfg: ioa cfg struct
6726 *
6727 * Adapters that use Gemstone revision < 3.1 do not work reliably on
6728 * certain pSeries hardware. This function determines if the given
6729 * adapter is in one of these confgurations or not.
6730 *
6731 * Return value:
6732 * 1 if adapter is not supported / 0 if adapter is supported
6733 **/
6734static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
6735{
1da177e4
LT
6736 int i;
6737
44c10138 6738 if ((ioa_cfg->type == 0x5702) && (ioa_cfg->pdev->revision < 4)) {
203fa3fe 6739 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++) {
d3dbeef6 6740 if (pvr_version_is(ipr_blocked_processors[i]))
44c10138 6741 return 1;
1da177e4
LT
6742 }
6743 }
6744 return 0;
6745}
6746#else
6747#define ipr_invalid_adapter(ioa_cfg) 0
6748#endif
6749
6750/**
6751 * ipr_ioa_bringdown_done - IOA bring down completion.
6752 * @ipr_cmd: ipr command struct
6753 *
6754 * This function processes the completion of an adapter bring down.
6755 * It wakes any reset sleepers.
6756 *
6757 * Return value:
6758 * IPR_RC_JOB_RETURN
6759 **/
6760static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
6761{
6762 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
96b04db9 6763 int i;
1da177e4
LT
6764
6765 ENTER;
bfae7820
BK
6766 if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].removing_ioa) {
6767 ipr_trace;
6768 spin_unlock_irq(ioa_cfg->host->host_lock);
6769 scsi_unblock_requests(ioa_cfg->host);
6770 spin_lock_irq(ioa_cfg->host->host_lock);
6771 }
6772
1da177e4
LT
6773 ioa_cfg->in_reset_reload = 0;
6774 ioa_cfg->reset_retries = 0;
96b04db9 6775 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
6776 spin_lock(&ioa_cfg->hrrq[i]._lock);
6777 ioa_cfg->hrrq[i].ioa_is_dead = 1;
6778 spin_unlock(&ioa_cfg->hrrq[i]._lock);
6779 }
6780 wmb();
6781
05a6538a 6782 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
1da177e4 6783 wake_up_all(&ioa_cfg->reset_wait_q);
1da177e4
LT
6784 LEAVE;
6785
6786 return IPR_RC_JOB_RETURN;
6787}
6788
6789/**
6790 * ipr_ioa_reset_done - IOA reset completion.
6791 * @ipr_cmd: ipr command struct
6792 *
6793 * This function processes the completion of an adapter reset.
6794 * It schedules any necessary mid-layer add/removes and
6795 * wakes any reset sleepers.
6796 *
6797 * Return value:
6798 * IPR_RC_JOB_RETURN
6799 **/
6800static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
6801{
6802 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6803 struct ipr_resource_entry *res;
6804 struct ipr_hostrcb *hostrcb, *temp;
56d6aa33 6805 int i = 0, j;
1da177e4
LT
6806
6807 ENTER;
6808 ioa_cfg->in_reset_reload = 0;
56d6aa33 6809 for (j = 0; j < ioa_cfg->hrrq_num; j++) {
6810 spin_lock(&ioa_cfg->hrrq[j]._lock);
6811 ioa_cfg->hrrq[j].allow_cmds = 1;
6812 spin_unlock(&ioa_cfg->hrrq[j]._lock);
6813 }
6814 wmb();
1da177e4 6815 ioa_cfg->reset_cmd = NULL;
3d1d0da6 6816 ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
1da177e4
LT
6817
6818 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
6819 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
6820 ipr_trace;
6821 break;
6822 }
6823 }
6824 schedule_work(&ioa_cfg->work_q);
6825
6826 list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
6827 list_del(&hostrcb->queue);
6828 if (i++ < IPR_NUM_LOG_HCAMS)
6829 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
6830 else
6831 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
6832 }
6833
6bb04170 6834 scsi_report_bus_reset(ioa_cfg->host, IPR_VSET_BUS);
1da177e4
LT
6835 dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
6836
6837 ioa_cfg->reset_retries = 0;
05a6538a 6838 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
1da177e4
LT
6839 wake_up_all(&ioa_cfg->reset_wait_q);
6840
30237853 6841 spin_unlock(ioa_cfg->host->host_lock);
1da177e4 6842 scsi_unblock_requests(ioa_cfg->host);
30237853 6843 spin_lock(ioa_cfg->host->host_lock);
1da177e4 6844
56d6aa33 6845 if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds)
1da177e4
LT
6846 scsi_block_requests(ioa_cfg->host);
6847
6848 LEAVE;
6849 return IPR_RC_JOB_RETURN;
6850}
6851
6852/**
6853 * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
6854 * @supported_dev: supported device struct
6855 * @vpids: vendor product id struct
6856 *
6857 * Return value:
6858 * none
6859 **/
6860static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
6861 struct ipr_std_inq_vpids *vpids)
6862{
6863 memset(supported_dev, 0, sizeof(struct ipr_supported_device));
6864 memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
6865 supported_dev->num_records = 1;
6866 supported_dev->data_length =
6867 cpu_to_be16(sizeof(struct ipr_supported_device));
6868 supported_dev->reserved = 0;
6869}
6870
6871/**
6872 * ipr_set_supported_devs - Send Set Supported Devices for a device
6873 * @ipr_cmd: ipr command struct
6874 *
a32c055f 6875 * This function sends a Set Supported Devices to the adapter
1da177e4
LT
6876 *
6877 * Return value:
6878 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6879 **/
6880static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
6881{
6882 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6883 struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
1da177e4
LT
6884 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6885 struct ipr_resource_entry *res = ipr_cmd->u.res;
6886
6887 ipr_cmd->job_step = ipr_ioa_reset_done;
6888
6889 list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
e4fbf44e 6890 if (!ipr_is_scsi_disk(res))
1da177e4
LT
6891 continue;
6892
6893 ipr_cmd->u.res = res;
3e7ebdfa 6894 ipr_set_sup_dev_dflt(supp_dev, &res->std_inq_data.vpids);
1da177e4
LT
6895
6896 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6897 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6898 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6899
6900 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
3e7ebdfa 6901 ioarcb->cmd_pkt.cdb[1] = IPR_SET_ALL_SUPPORTED_DEVICES;
1da177e4
LT
6902 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
6903 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
6904
a32c055f
WB
6905 ipr_init_ioadl(ipr_cmd,
6906 ioa_cfg->vpd_cbs_dma +
6907 offsetof(struct ipr_misc_cbs, supp_dev),
6908 sizeof(struct ipr_supported_device),
6909 IPR_IOADL_FLAGS_WRITE_LAST);
1da177e4
LT
6910
6911 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
6912 IPR_SET_SUP_DEVICE_TIMEOUT);
6913
3e7ebdfa
WB
6914 if (!ioa_cfg->sis64)
6915 ipr_cmd->job_step = ipr_set_supported_devs;
05a6538a 6916 LEAVE;
1da177e4
LT
6917 return IPR_RC_JOB_RETURN;
6918 }
6919
05a6538a 6920 LEAVE;
1da177e4
LT
6921 return IPR_RC_JOB_CONTINUE;
6922}
6923
6924/**
6925 * ipr_get_mode_page - Locate specified mode page
6926 * @mode_pages: mode page buffer
6927 * @page_code: page code to find
6928 * @len: minimum required length for mode page
6929 *
6930 * Return value:
6931 * pointer to mode page / NULL on failure
6932 **/
6933static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
6934 u32 page_code, u32 len)
6935{
6936 struct ipr_mode_page_hdr *mode_hdr;
6937 u32 page_length;
6938 u32 length;
6939
6940 if (!mode_pages || (mode_pages->hdr.length == 0))
6941 return NULL;
6942
6943 length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
6944 mode_hdr = (struct ipr_mode_page_hdr *)
6945 (mode_pages->data + mode_pages->hdr.block_desc_len);
6946
6947 while (length) {
6948 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
6949 if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
6950 return mode_hdr;
6951 break;
6952 } else {
6953 page_length = (sizeof(struct ipr_mode_page_hdr) +
6954 mode_hdr->page_length);
6955 length -= page_length;
6956 mode_hdr = (struct ipr_mode_page_hdr *)
6957 ((unsigned long)mode_hdr + page_length);
6958 }
6959 }
6960 return NULL;
6961}
6962
6963/**
6964 * ipr_check_term_power - Check for term power errors
6965 * @ioa_cfg: ioa config struct
6966 * @mode_pages: IOAFP mode pages buffer
6967 *
6968 * Check the IOAFP's mode page 28 for term power errors
6969 *
6970 * Return value:
6971 * nothing
6972 **/
6973static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
6974 struct ipr_mode_pages *mode_pages)
6975{
6976 int i;
6977 int entry_length;
6978 struct ipr_dev_bus_entry *bus;
6979 struct ipr_mode_page28 *mode_page;
6980
6981 mode_page = ipr_get_mode_page(mode_pages, 0x28,
6982 sizeof(struct ipr_mode_page28));
6983
6984 entry_length = mode_page->entry_length;
6985
6986 bus = mode_page->bus;
6987
6988 for (i = 0; i < mode_page->num_entries; i++) {
6989 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
6990 dev_err(&ioa_cfg->pdev->dev,
6991 "Term power is absent on scsi bus %d\n",
6992 bus->res_addr.bus);
6993 }
6994
6995 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
6996 }
6997}
6998
6999/**
7000 * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
7001 * @ioa_cfg: ioa config struct
7002 *
7003 * Looks through the config table checking for SES devices. If
7004 * the SES device is in the SES table indicating a maximum SCSI
7005 * bus speed, the speed is limited for the bus.
7006 *
7007 * Return value:
7008 * none
7009 **/
7010static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
7011{
7012 u32 max_xfer_rate;
7013 int i;
7014
7015 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
7016 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
7017 ioa_cfg->bus_attr[i].bus_width);
7018
7019 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
7020 ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
7021 }
7022}
7023
7024/**
7025 * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
7026 * @ioa_cfg: ioa config struct
7027 * @mode_pages: mode page 28 buffer
7028 *
7029 * Updates mode page 28 based on driver configuration
7030 *
7031 * Return value:
7032 * none
7033 **/
7034static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
203fa3fe 7035 struct ipr_mode_pages *mode_pages)
1da177e4
LT
7036{
7037 int i, entry_length;
7038 struct ipr_dev_bus_entry *bus;
7039 struct ipr_bus_attributes *bus_attr;
7040 struct ipr_mode_page28 *mode_page;
7041
7042 mode_page = ipr_get_mode_page(mode_pages, 0x28,
7043 sizeof(struct ipr_mode_page28));
7044
7045 entry_length = mode_page->entry_length;
7046
7047 /* Loop for each device bus entry */
7048 for (i = 0, bus = mode_page->bus;
7049 i < mode_page->num_entries;
7050 i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
7051 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
7052 dev_err(&ioa_cfg->pdev->dev,
7053 "Invalid resource address reported: 0x%08X\n",
7054 IPR_GET_PHYS_LOC(bus->res_addr));
7055 continue;
7056 }
7057
7058 bus_attr = &ioa_cfg->bus_attr[i];
7059 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
7060 bus->bus_width = bus_attr->bus_width;
7061 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
7062 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
7063 if (bus_attr->qas_enabled)
7064 bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
7065 else
7066 bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
7067 }
7068}
7069
7070/**
7071 * ipr_build_mode_select - Build a mode select command
7072 * @ipr_cmd: ipr command struct
7073 * @res_handle: resource handle to send command to
7074 * @parm: Byte 2 of Mode Sense command
7075 * @dma_addr: DMA buffer address
7076 * @xfer_len: data transfer length
7077 *
7078 * Return value:
7079 * none
7080 **/
7081static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
a32c055f
WB
7082 __be32 res_handle, u8 parm,
7083 dma_addr_t dma_addr, u8 xfer_len)
1da177e4 7084{
1da177e4
LT
7085 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7086
7087 ioarcb->res_handle = res_handle;
7088 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
7089 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
7090 ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
7091 ioarcb->cmd_pkt.cdb[1] = parm;
7092 ioarcb->cmd_pkt.cdb[4] = xfer_len;
7093
a32c055f 7094 ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_WRITE_LAST);
1da177e4
LT
7095}
7096
7097/**
7098 * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
7099 * @ipr_cmd: ipr command struct
7100 *
7101 * This function sets up the SCSI bus attributes and sends
7102 * a Mode Select for Page 28 to activate them.
7103 *
7104 * Return value:
7105 * IPR_RC_JOB_RETURN
7106 **/
7107static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
7108{
7109 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7110 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
7111 int length;
7112
7113 ENTER;
4733804c
BK
7114 ipr_scsi_bus_speed_limit(ioa_cfg);
7115 ipr_check_term_power(ioa_cfg, mode_pages);
7116 ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
7117 length = mode_pages->hdr.length + 1;
7118 mode_pages->hdr.length = 0;
1da177e4
LT
7119
7120 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
7121 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
7122 length);
7123
f72919ec
WB
7124 ipr_cmd->job_step = ipr_set_supported_devs;
7125 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
7126 struct ipr_resource_entry, queue);
1da177e4
LT
7127 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7128
7129 LEAVE;
7130 return IPR_RC_JOB_RETURN;
7131}
7132
7133/**
7134 * ipr_build_mode_sense - Builds a mode sense command
7135 * @ipr_cmd: ipr command struct
7136 * @res: resource entry struct
7137 * @parm: Byte 2 of mode sense command
7138 * @dma_addr: DMA address of mode sense buffer
7139 * @xfer_len: Size of DMA buffer
7140 *
7141 * Return value:
7142 * none
7143 **/
7144static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
7145 __be32 res_handle,
a32c055f 7146 u8 parm, dma_addr_t dma_addr, u8 xfer_len)
1da177e4 7147{
1da177e4
LT
7148 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7149
7150 ioarcb->res_handle = res_handle;
7151 ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
7152 ioarcb->cmd_pkt.cdb[2] = parm;
7153 ioarcb->cmd_pkt.cdb[4] = xfer_len;
7154 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
7155
a32c055f 7156 ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
1da177e4
LT
7157}
7158
dfed823e
BK
7159/**
7160 * ipr_reset_cmd_failed - Handle failure of IOA reset command
7161 * @ipr_cmd: ipr command struct
7162 *
7163 * This function handles the failure of an IOA bringup command.
7164 *
7165 * Return value:
7166 * IPR_RC_JOB_RETURN
7167 **/
7168static int ipr_reset_cmd_failed(struct ipr_cmnd *ipr_cmd)
7169{
7170 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
96d21f00 7171 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
dfed823e
BK
7172
7173 dev_err(&ioa_cfg->pdev->dev,
7174 "0x%02X failed with IOASC: 0x%08X\n",
7175 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
7176
7177 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
05a6538a 7178 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
dfed823e
BK
7179 return IPR_RC_JOB_RETURN;
7180}
7181
7182/**
7183 * ipr_reset_mode_sense_failed - Handle failure of IOAFP mode sense
7184 * @ipr_cmd: ipr command struct
7185 *
7186 * This function handles the failure of a Mode Sense to the IOAFP.
7187 * Some adapters do not handle all mode pages.
7188 *
7189 * Return value:
7190 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7191 **/
7192static int ipr_reset_mode_sense_failed(struct ipr_cmnd *ipr_cmd)
7193{
f72919ec 7194 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
96d21f00 7195 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
dfed823e
BK
7196
7197 if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
f72919ec
WB
7198 ipr_cmd->job_step = ipr_set_supported_devs;
7199 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
7200 struct ipr_resource_entry, queue);
dfed823e
BK
7201 return IPR_RC_JOB_CONTINUE;
7202 }
7203
7204 return ipr_reset_cmd_failed(ipr_cmd);
7205}
7206
1da177e4
LT
7207/**
7208 * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
7209 * @ipr_cmd: ipr command struct
7210 *
7211 * This function send a Page 28 mode sense to the IOA to
7212 * retrieve SCSI bus attributes.
7213 *
7214 * Return value:
7215 * IPR_RC_JOB_RETURN
7216 **/
7217static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
7218{
7219 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7220
7221 ENTER;
7222 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
7223 0x28, ioa_cfg->vpd_cbs_dma +
7224 offsetof(struct ipr_misc_cbs, mode_pages),
7225 sizeof(struct ipr_mode_pages));
7226
7227 ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
dfed823e 7228 ipr_cmd->job_step_failed = ipr_reset_mode_sense_failed;
1da177e4
LT
7229
7230 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7231
7232 LEAVE;
7233 return IPR_RC_JOB_RETURN;
7234}
7235
ac09c349
BK
7236/**
7237 * ipr_ioafp_mode_select_page24 - Issue Mode Select to IOA
7238 * @ipr_cmd: ipr command struct
7239 *
7240 * This function enables dual IOA RAID support if possible.
7241 *
7242 * Return value:
7243 * IPR_RC_JOB_RETURN
7244 **/
7245static int ipr_ioafp_mode_select_page24(struct ipr_cmnd *ipr_cmd)
7246{
7247 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7248 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
7249 struct ipr_mode_page24 *mode_page;
7250 int length;
7251
7252 ENTER;
7253 mode_page = ipr_get_mode_page(mode_pages, 0x24,
7254 sizeof(struct ipr_mode_page24));
7255
7256 if (mode_page)
7257 mode_page->flags |= IPR_ENABLE_DUAL_IOA_AF;
7258
7259 length = mode_pages->hdr.length + 1;
7260 mode_pages->hdr.length = 0;
7261
7262 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
7263 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
7264 length);
7265
7266 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
7267 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7268
7269 LEAVE;
7270 return IPR_RC_JOB_RETURN;
7271}
7272
7273/**
7274 * ipr_reset_mode_sense_page24_failed - Handle failure of IOAFP mode sense
7275 * @ipr_cmd: ipr command struct
7276 *
7277 * This function handles the failure of a Mode Sense to the IOAFP.
7278 * Some adapters do not handle all mode pages.
7279 *
7280 * Return value:
7281 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7282 **/
7283static int ipr_reset_mode_sense_page24_failed(struct ipr_cmnd *ipr_cmd)
7284{
96d21f00 7285 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
ac09c349
BK
7286
7287 if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
7288 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
7289 return IPR_RC_JOB_CONTINUE;
7290 }
7291
7292 return ipr_reset_cmd_failed(ipr_cmd);
7293}
7294
7295/**
7296 * ipr_ioafp_mode_sense_page24 - Issue Page 24 Mode Sense to IOA
7297 * @ipr_cmd: ipr command struct
7298 *
7299 * This function send a mode sense to the IOA to retrieve
7300 * the IOA Advanced Function Control mode page.
7301 *
7302 * Return value:
7303 * IPR_RC_JOB_RETURN
7304 **/
7305static int ipr_ioafp_mode_sense_page24(struct ipr_cmnd *ipr_cmd)
7306{
7307 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7308
7309 ENTER;
7310 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
7311 0x24, ioa_cfg->vpd_cbs_dma +
7312 offsetof(struct ipr_misc_cbs, mode_pages),
7313 sizeof(struct ipr_mode_pages));
7314
7315 ipr_cmd->job_step = ipr_ioafp_mode_select_page24;
7316 ipr_cmd->job_step_failed = ipr_reset_mode_sense_page24_failed;
7317
7318 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7319
7320 LEAVE;
7321 return IPR_RC_JOB_RETURN;
7322}
7323
1da177e4
LT
7324/**
7325 * ipr_init_res_table - Initialize the resource table
7326 * @ipr_cmd: ipr command struct
7327 *
7328 * This function looks through the existing resource table, comparing
7329 * it with the config table. This function will take care of old/new
7330 * devices and schedule adding/removing them from the mid-layer
7331 * as appropriate.
7332 *
7333 * Return value:
7334 * IPR_RC_JOB_CONTINUE
7335 **/
7336static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
7337{
7338 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7339 struct ipr_resource_entry *res, *temp;
3e7ebdfa
WB
7340 struct ipr_config_table_entry_wrapper cfgtew;
7341 int entries, found, flag, i;
1da177e4
LT
7342 LIST_HEAD(old_res);
7343
7344 ENTER;
3e7ebdfa
WB
7345 if (ioa_cfg->sis64)
7346 flag = ioa_cfg->u.cfg_table64->hdr64.flags;
7347 else
7348 flag = ioa_cfg->u.cfg_table->hdr.flags;
7349
7350 if (flag & IPR_UCODE_DOWNLOAD_REQ)
1da177e4
LT
7351 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
7352
7353 list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
7354 list_move_tail(&res->queue, &old_res);
7355
3e7ebdfa 7356 if (ioa_cfg->sis64)
438b0331 7357 entries = be16_to_cpu(ioa_cfg->u.cfg_table64->hdr64.num_entries);
3e7ebdfa
WB
7358 else
7359 entries = ioa_cfg->u.cfg_table->hdr.num_entries;
7360
7361 for (i = 0; i < entries; i++) {
7362 if (ioa_cfg->sis64)
7363 cfgtew.u.cfgte64 = &ioa_cfg->u.cfg_table64->dev[i];
7364 else
7365 cfgtew.u.cfgte = &ioa_cfg->u.cfg_table->dev[i];
1da177e4
LT
7366 found = 0;
7367
7368 list_for_each_entry_safe(res, temp, &old_res, queue) {
3e7ebdfa 7369 if (ipr_is_same_device(res, &cfgtew)) {
1da177e4
LT
7370 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
7371 found = 1;
7372 break;
7373 }
7374 }
7375
7376 if (!found) {
7377 if (list_empty(&ioa_cfg->free_res_q)) {
7378 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
7379 break;
7380 }
7381
7382 found = 1;
7383 res = list_entry(ioa_cfg->free_res_q.next,
7384 struct ipr_resource_entry, queue);
7385 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
3e7ebdfa 7386 ipr_init_res_entry(res, &cfgtew);
1da177e4 7387 res->add_to_ml = 1;
56115598
WB
7388 } else if (res->sdev && (ipr_is_vset_device(res) || ipr_is_scsi_disk(res)))
7389 res->sdev->allow_restart = 1;
1da177e4
LT
7390
7391 if (found)
3e7ebdfa 7392 ipr_update_res_entry(res, &cfgtew);
1da177e4
LT
7393 }
7394
7395 list_for_each_entry_safe(res, temp, &old_res, queue) {
7396 if (res->sdev) {
7397 res->del_from_ml = 1;
3e7ebdfa 7398 res->res_handle = IPR_INVALID_RES_HANDLE;
1da177e4 7399 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
1da177e4
LT
7400 }
7401 }
7402
3e7ebdfa
WB
7403 list_for_each_entry_safe(res, temp, &old_res, queue) {
7404 ipr_clear_res_target(res);
7405 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
7406 }
7407
ac09c349
BK
7408 if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
7409 ipr_cmd->job_step = ipr_ioafp_mode_sense_page24;
7410 else
7411 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
1da177e4
LT
7412
7413 LEAVE;
7414 return IPR_RC_JOB_CONTINUE;
7415}
7416
7417/**
7418 * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
7419 * @ipr_cmd: ipr command struct
7420 *
7421 * This function sends a Query IOA Configuration command
7422 * to the adapter to retrieve the IOA configuration table.
7423 *
7424 * Return value:
7425 * IPR_RC_JOB_RETURN
7426 **/
7427static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
7428{
7429 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7430 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
1da177e4 7431 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
ac09c349 7432 struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
1da177e4
LT
7433
7434 ENTER;
ac09c349
BK
7435 if (cap->cap & IPR_CAP_DUAL_IOA_RAID)
7436 ioa_cfg->dual_raid = 1;
1da177e4
LT
7437 dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
7438 ucode_vpd->major_release, ucode_vpd->card_type,
7439 ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
7440 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
7441 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7442
7443 ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
438b0331 7444 ioarcb->cmd_pkt.cdb[6] = (ioa_cfg->cfg_table_size >> 16) & 0xff;
3e7ebdfa
WB
7445 ioarcb->cmd_pkt.cdb[7] = (ioa_cfg->cfg_table_size >> 8) & 0xff;
7446 ioarcb->cmd_pkt.cdb[8] = ioa_cfg->cfg_table_size & 0xff;
1da177e4 7447
3e7ebdfa 7448 ipr_init_ioadl(ipr_cmd, ioa_cfg->cfg_table_dma, ioa_cfg->cfg_table_size,
a32c055f 7449 IPR_IOADL_FLAGS_READ_LAST);
1da177e4
LT
7450
7451 ipr_cmd->job_step = ipr_init_res_table;
7452
7453 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7454
7455 LEAVE;
7456 return IPR_RC_JOB_RETURN;
7457}
7458
7459/**
7460 * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
7461 * @ipr_cmd: ipr command struct
7462 *
7463 * This utility function sends an inquiry to the adapter.
7464 *
7465 * Return value:
7466 * none
7467 **/
7468static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
a32c055f 7469 dma_addr_t dma_addr, u8 xfer_len)
1da177e4
LT
7470{
7471 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
1da177e4
LT
7472
7473 ENTER;
7474 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
7475 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7476
7477 ioarcb->cmd_pkt.cdb[0] = INQUIRY;
7478 ioarcb->cmd_pkt.cdb[1] = flags;
7479 ioarcb->cmd_pkt.cdb[2] = page;
7480 ioarcb->cmd_pkt.cdb[4] = xfer_len;
7481
a32c055f 7482 ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
1da177e4
LT
7483
7484 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7485 LEAVE;
7486}
7487
62275040
BK
7488/**
7489 * ipr_inquiry_page_supported - Is the given inquiry page supported
7490 * @page0: inquiry page 0 buffer
7491 * @page: page code.
7492 *
7493 * This function determines if the specified inquiry page is supported.
7494 *
7495 * Return value:
7496 * 1 if page is supported / 0 if not
7497 **/
7498static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
7499{
7500 int i;
7501
7502 for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
7503 if (page0->page[i] == page)
7504 return 1;
7505
7506 return 0;
7507}
7508
ac09c349
BK
7509/**
7510 * ipr_ioafp_cap_inquiry - Send a Page 0xD0 Inquiry to the adapter.
7511 * @ipr_cmd: ipr command struct
7512 *
7513 * This function sends a Page 0xD0 inquiry to the adapter
7514 * to retrieve adapter capabilities.
7515 *
7516 * Return value:
7517 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7518 **/
7519static int ipr_ioafp_cap_inquiry(struct ipr_cmnd *ipr_cmd)
7520{
7521 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7522 struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
7523 struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
7524
7525 ENTER;
7526 ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
7527 memset(cap, 0, sizeof(*cap));
7528
7529 if (ipr_inquiry_page_supported(page0, 0xD0)) {
7530 ipr_ioafp_inquiry(ipr_cmd, 1, 0xD0,
7531 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, cap),
7532 sizeof(struct ipr_inquiry_cap));
7533 return IPR_RC_JOB_RETURN;
7534 }
7535
7536 LEAVE;
7537 return IPR_RC_JOB_CONTINUE;
7538}
7539
1da177e4
LT
7540/**
7541 * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
7542 * @ipr_cmd: ipr command struct
7543 *
7544 * This function sends a Page 3 inquiry to the adapter
7545 * to retrieve software VPD information.
7546 *
7547 * Return value:
7548 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7549 **/
7550static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
62275040
BK
7551{
7552 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
62275040
BK
7553
7554 ENTER;
7555
ac09c349 7556 ipr_cmd->job_step = ipr_ioafp_cap_inquiry;
62275040
BK
7557
7558 ipr_ioafp_inquiry(ipr_cmd, 1, 3,
7559 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
7560 sizeof(struct ipr_inquiry_page3));
7561
7562 LEAVE;
7563 return IPR_RC_JOB_RETURN;
7564}
7565
7566/**
7567 * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
7568 * @ipr_cmd: ipr command struct
7569 *
7570 * This function sends a Page 0 inquiry to the adapter
7571 * to retrieve supported inquiry pages.
7572 *
7573 * Return value:
7574 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7575 **/
7576static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
1da177e4
LT
7577{
7578 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7579 char type[5];
7580
7581 ENTER;
7582
7583 /* Grab the type out of the VPD and store it away */
7584 memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
7585 type[4] = '\0';
7586 ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
7587
62275040 7588 ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
1da177e4 7589
62275040
BK
7590 ipr_ioafp_inquiry(ipr_cmd, 1, 0,
7591 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
7592 sizeof(struct ipr_inquiry_page0));
1da177e4
LT
7593
7594 LEAVE;
7595 return IPR_RC_JOB_RETURN;
7596}
7597
7598/**
7599 * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
7600 * @ipr_cmd: ipr command struct
7601 *
7602 * This function sends a standard inquiry to the adapter.
7603 *
7604 * Return value:
7605 * IPR_RC_JOB_RETURN
7606 **/
7607static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
7608{
7609 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7610
7611 ENTER;
62275040 7612 ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
1da177e4
LT
7613
7614 ipr_ioafp_inquiry(ipr_cmd, 0, 0,
7615 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
7616 sizeof(struct ipr_ioa_vpd));
7617
7618 LEAVE;
7619 return IPR_RC_JOB_RETURN;
7620}
7621
7622/**
214777ba 7623 * ipr_ioafp_identify_hrrq - Send Identify Host RRQ.
1da177e4
LT
7624 * @ipr_cmd: ipr command struct
7625 *
7626 * This function send an Identify Host Request Response Queue
7627 * command to establish the HRRQ with the adapter.
7628 *
7629 * Return value:
7630 * IPR_RC_JOB_RETURN
7631 **/
214777ba 7632static int ipr_ioafp_identify_hrrq(struct ipr_cmnd *ipr_cmd)
1da177e4
LT
7633{
7634 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7635 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
05a6538a 7636 struct ipr_hrr_queue *hrrq;
1da177e4
LT
7637
7638 ENTER;
05a6538a 7639 ipr_cmd->job_step = ipr_ioafp_std_inquiry;
1da177e4
LT
7640 dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
7641
56d6aa33 7642 if (ioa_cfg->identify_hrrq_index < ioa_cfg->hrrq_num) {
7643 hrrq = &ioa_cfg->hrrq[ioa_cfg->identify_hrrq_index];
1da177e4 7644
05a6538a 7645 ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
7646 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
1da177e4 7647
05a6538a 7648 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
7649 if (ioa_cfg->sis64)
7650 ioarcb->cmd_pkt.cdb[1] = 0x1;
214777ba 7651
05a6538a 7652 if (ioa_cfg->nvectors == 1)
7653 ioarcb->cmd_pkt.cdb[1] &= ~IPR_ID_HRRQ_SELE_ENABLE;
7654 else
7655 ioarcb->cmd_pkt.cdb[1] |= IPR_ID_HRRQ_SELE_ENABLE;
7656
7657 ioarcb->cmd_pkt.cdb[2] =
7658 ((u64) hrrq->host_rrq_dma >> 24) & 0xff;
7659 ioarcb->cmd_pkt.cdb[3] =
7660 ((u64) hrrq->host_rrq_dma >> 16) & 0xff;
7661 ioarcb->cmd_pkt.cdb[4] =
7662 ((u64) hrrq->host_rrq_dma >> 8) & 0xff;
7663 ioarcb->cmd_pkt.cdb[5] =
7664 ((u64) hrrq->host_rrq_dma) & 0xff;
7665 ioarcb->cmd_pkt.cdb[7] =
7666 ((sizeof(u32) * hrrq->size) >> 8) & 0xff;
7667 ioarcb->cmd_pkt.cdb[8] =
7668 (sizeof(u32) * hrrq->size) & 0xff;
7669
7670 if (ioarcb->cmd_pkt.cdb[1] & IPR_ID_HRRQ_SELE_ENABLE)
56d6aa33 7671 ioarcb->cmd_pkt.cdb[9] =
7672 ioa_cfg->identify_hrrq_index;
1da177e4 7673
05a6538a 7674 if (ioa_cfg->sis64) {
7675 ioarcb->cmd_pkt.cdb[10] =
7676 ((u64) hrrq->host_rrq_dma >> 56) & 0xff;
7677 ioarcb->cmd_pkt.cdb[11] =
7678 ((u64) hrrq->host_rrq_dma >> 48) & 0xff;
7679 ioarcb->cmd_pkt.cdb[12] =
7680 ((u64) hrrq->host_rrq_dma >> 40) & 0xff;
7681 ioarcb->cmd_pkt.cdb[13] =
7682 ((u64) hrrq->host_rrq_dma >> 32) & 0xff;
7683 }
7684
7685 if (ioarcb->cmd_pkt.cdb[1] & IPR_ID_HRRQ_SELE_ENABLE)
56d6aa33 7686 ioarcb->cmd_pkt.cdb[14] =
7687 ioa_cfg->identify_hrrq_index;
05a6538a 7688
7689 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
7690 IPR_INTERNAL_TIMEOUT);
7691
56d6aa33 7692 if (++ioa_cfg->identify_hrrq_index < ioa_cfg->hrrq_num)
7693 ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
05a6538a 7694
7695 LEAVE;
7696 return IPR_RC_JOB_RETURN;
05a6538a 7697 }
7698
1da177e4 7699 LEAVE;
05a6538a 7700 return IPR_RC_JOB_CONTINUE;
1da177e4
LT
7701}
7702
7703/**
7704 * ipr_reset_timer_done - Adapter reset timer function
7705 * @ipr_cmd: ipr command struct
7706 *
7707 * Description: This function is used in adapter reset processing
7708 * for timing events. If the reset_cmd pointer in the IOA
7709 * config struct is not this adapter's we are doing nested
7710 * resets and fail_all_ops will take care of freeing the
7711 * command block.
7712 *
7713 * Return value:
7714 * none
7715 **/
7716static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
7717{
7718 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7719 unsigned long lock_flags = 0;
7720
7721 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7722
7723 if (ioa_cfg->reset_cmd == ipr_cmd) {
7724 list_del(&ipr_cmd->queue);
7725 ipr_cmd->done(ipr_cmd);
7726 }
7727
7728 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7729}
7730
7731/**
7732 * ipr_reset_start_timer - Start a timer for adapter reset job
7733 * @ipr_cmd: ipr command struct
7734 * @timeout: timeout value
7735 *
7736 * Description: This function is used in adapter reset processing
7737 * for timing events. If the reset_cmd pointer in the IOA
7738 * config struct is not this adapter's we are doing nested
7739 * resets and fail_all_ops will take care of freeing the
7740 * command block.
7741 *
7742 * Return value:
7743 * none
7744 **/
7745static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
7746 unsigned long timeout)
7747{
05a6538a 7748
7749 ENTER;
7750 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
1da177e4
LT
7751 ipr_cmd->done = ipr_reset_ioa_job;
7752
7753 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
7754 ipr_cmd->timer.expires = jiffies + timeout;
7755 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
7756 add_timer(&ipr_cmd->timer);
7757}
7758
7759/**
7760 * ipr_init_ioa_mem - Initialize ioa_cfg control block
7761 * @ioa_cfg: ioa cfg struct
7762 *
7763 * Return value:
7764 * nothing
7765 **/
7766static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
7767{
05a6538a 7768 struct ipr_hrr_queue *hrrq;
1da177e4 7769
05a6538a 7770 for_each_hrrq(hrrq, ioa_cfg) {
56d6aa33 7771 spin_lock(&hrrq->_lock);
05a6538a 7772 memset(hrrq->host_rrq, 0, sizeof(u32) * hrrq->size);
7773
7774 /* Initialize Host RRQ pointers */
7775 hrrq->hrrq_start = hrrq->host_rrq;
7776 hrrq->hrrq_end = &hrrq->host_rrq[hrrq->size - 1];
7777 hrrq->hrrq_curr = hrrq->hrrq_start;
7778 hrrq->toggle_bit = 1;
56d6aa33 7779 spin_unlock(&hrrq->_lock);
05a6538a 7780 }
56d6aa33 7781 wmb();
05a6538a 7782
56d6aa33 7783 ioa_cfg->identify_hrrq_index = 0;
7784 if (ioa_cfg->hrrq_num == 1)
7785 atomic_set(&ioa_cfg->hrrq_index, 0);
7786 else
7787 atomic_set(&ioa_cfg->hrrq_index, 1);
1da177e4
LT
7788
7789 /* Zero out config table */
3e7ebdfa 7790 memset(ioa_cfg->u.cfg_table, 0, ioa_cfg->cfg_table_size);
1da177e4
LT
7791}
7792
214777ba
WB
7793/**
7794 * ipr_reset_next_stage - Process IPL stage change based on feedback register.
7795 * @ipr_cmd: ipr command struct
7796 *
7797 * Return value:
7798 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7799 **/
7800static int ipr_reset_next_stage(struct ipr_cmnd *ipr_cmd)
7801{
7802 unsigned long stage, stage_time;
7803 u32 feedback;
7804 volatile u32 int_reg;
7805 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7806 u64 maskval = 0;
7807
7808 feedback = readl(ioa_cfg->regs.init_feedback_reg);
7809 stage = feedback & IPR_IPL_INIT_STAGE_MASK;
7810 stage_time = feedback & IPR_IPL_INIT_STAGE_TIME_MASK;
7811
7812 ipr_dbg("IPL stage = 0x%lx, IPL stage time = %ld\n", stage, stage_time);
7813
7814 /* sanity check the stage_time value */
438b0331
WB
7815 if (stage_time == 0)
7816 stage_time = IPR_IPL_INIT_DEFAULT_STAGE_TIME;
7817 else if (stage_time < IPR_IPL_INIT_MIN_STAGE_TIME)
214777ba
WB
7818 stage_time = IPR_IPL_INIT_MIN_STAGE_TIME;
7819 else if (stage_time > IPR_LONG_OPERATIONAL_TIMEOUT)
7820 stage_time = IPR_LONG_OPERATIONAL_TIMEOUT;
7821
7822 if (stage == IPR_IPL_INIT_STAGE_UNKNOWN) {
7823 writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.set_interrupt_mask_reg);
7824 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7825 stage_time = ioa_cfg->transop_timeout;
7826 ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7827 } else if (stage == IPR_IPL_INIT_STAGE_TRANSOP) {
1df79ca4
WB
7828 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
7829 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
7830 ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7831 maskval = IPR_PCII_IPL_STAGE_CHANGE;
7832 maskval = (maskval << 32) | IPR_PCII_IOA_TRANS_TO_OPER;
7833 writeq(maskval, ioa_cfg->regs.set_interrupt_mask_reg);
7834 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7835 return IPR_RC_JOB_CONTINUE;
7836 }
214777ba
WB
7837 }
7838
7839 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
7840 ipr_cmd->timer.expires = jiffies + stage_time * HZ;
7841 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
7842 ipr_cmd->done = ipr_reset_ioa_job;
7843 add_timer(&ipr_cmd->timer);
05a6538a 7844
7845 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
214777ba
WB
7846
7847 return IPR_RC_JOB_RETURN;
7848}
7849
1da177e4
LT
7850/**
7851 * ipr_reset_enable_ioa - Enable the IOA following a reset.
7852 * @ipr_cmd: ipr command struct
7853 *
7854 * This function reinitializes some control blocks and
7855 * enables destructive diagnostics on the adapter.
7856 *
7857 * Return value:
7858 * IPR_RC_JOB_RETURN
7859 **/
7860static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
7861{
7862 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7863 volatile u32 int_reg;
7be96900 7864 volatile u64 maskval;
56d6aa33 7865 int i;
1da177e4
LT
7866
7867 ENTER;
214777ba 7868 ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
1da177e4
LT
7869 ipr_init_ioa_mem(ioa_cfg);
7870
56d6aa33 7871 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
7872 spin_lock(&ioa_cfg->hrrq[i]._lock);
7873 ioa_cfg->hrrq[i].allow_interrupts = 1;
7874 spin_unlock(&ioa_cfg->hrrq[i]._lock);
7875 }
7876 wmb();
8701f185
WB
7877 if (ioa_cfg->sis64) {
7878 /* Set the adapter to the correct endian mode. */
7879 writel(IPR_ENDIAN_SWAP_KEY, ioa_cfg->regs.endian_swap_reg);
7880 int_reg = readl(ioa_cfg->regs.endian_swap_reg);
7881 }
7882
7be96900 7883 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
1da177e4
LT
7884
7885 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
7886 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
214777ba 7887 ioa_cfg->regs.clr_interrupt_mask_reg32);
1da177e4
LT
7888 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7889 return IPR_RC_JOB_CONTINUE;
7890 }
7891
7892 /* Enable destructive diagnostics on IOA */
214777ba
WB
7893 writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg32);
7894
7be96900
WB
7895 if (ioa_cfg->sis64) {
7896 maskval = IPR_PCII_IPL_STAGE_CHANGE;
7897 maskval = (maskval << 32) | IPR_PCII_OPER_INTERRUPTS;
7898 writeq(maskval, ioa_cfg->regs.clr_interrupt_mask_reg);
7899 } else
7900 writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg32);
1da177e4 7901
1da177e4
LT
7902 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7903
7904 dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
7905
214777ba
WB
7906 if (ioa_cfg->sis64) {
7907 ipr_cmd->job_step = ipr_reset_next_stage;
7908 return IPR_RC_JOB_CONTINUE;
7909 }
7910
1da177e4 7911 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
5469cb5b 7912 ipr_cmd->timer.expires = jiffies + (ioa_cfg->transop_timeout * HZ);
1da177e4
LT
7913 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
7914 ipr_cmd->done = ipr_reset_ioa_job;
7915 add_timer(&ipr_cmd->timer);
05a6538a 7916 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
1da177e4
LT
7917
7918 LEAVE;
7919 return IPR_RC_JOB_RETURN;
7920}
7921
7922/**
7923 * ipr_reset_wait_for_dump - Wait for a dump to timeout.
7924 * @ipr_cmd: ipr command struct
7925 *
7926 * This function is invoked when an adapter dump has run out
7927 * of processing time.
7928 *
7929 * Return value:
7930 * IPR_RC_JOB_CONTINUE
7931 **/
7932static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
7933{
7934 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7935
7936 if (ioa_cfg->sdt_state == GET_DUMP)
41e9a696
BK
7937 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
7938 else if (ioa_cfg->sdt_state == READ_DUMP)
1da177e4
LT
7939 ioa_cfg->sdt_state = ABORT_DUMP;
7940
4c647e90 7941 ioa_cfg->dump_timeout = 1;
1da177e4
LT
7942 ipr_cmd->job_step = ipr_reset_alert;
7943
7944 return IPR_RC_JOB_CONTINUE;
7945}
7946
7947/**
7948 * ipr_unit_check_no_data - Log a unit check/no data error log
7949 * @ioa_cfg: ioa config struct
7950 *
7951 * Logs an error indicating the adapter unit checked, but for some
7952 * reason, we were unable to fetch the unit check buffer.
7953 *
7954 * Return value:
7955 * nothing
7956 **/
7957static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
7958{
7959 ioa_cfg->errors_logged++;
7960 dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
7961}
7962
7963/**
7964 * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
7965 * @ioa_cfg: ioa config struct
7966 *
7967 * Fetches the unit check buffer from the adapter by clocking the data
7968 * through the mailbox register.
7969 *
7970 * Return value:
7971 * nothing
7972 **/
7973static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
7974{
7975 unsigned long mailbox;
7976 struct ipr_hostrcb *hostrcb;
7977 struct ipr_uc_sdt sdt;
7978 int rc, length;
65f56475 7979 u32 ioasc;
1da177e4
LT
7980
7981 mailbox = readl(ioa_cfg->ioa_mailbox);
7982
dcbad00e 7983 if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(mailbox)) {
1da177e4
LT
7984 ipr_unit_check_no_data(ioa_cfg);
7985 return;
7986 }
7987
7988 memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
7989 rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
7990 (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
7991
dcbad00e
WB
7992 if (rc || !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY) ||
7993 ((be32_to_cpu(sdt.hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
7994 (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
1da177e4
LT
7995 ipr_unit_check_no_data(ioa_cfg);
7996 return;
7997 }
7998
7999 /* Find length of the first sdt entry (UC buffer) */
dcbad00e
WB
8000 if (be32_to_cpu(sdt.hdr.state) == IPR_FMT3_SDT_READY_TO_USE)
8001 length = be32_to_cpu(sdt.entry[0].end_token);
8002 else
8003 length = (be32_to_cpu(sdt.entry[0].end_token) -
8004 be32_to_cpu(sdt.entry[0].start_token)) &
8005 IPR_FMT2_MBX_ADDR_MASK;
1da177e4
LT
8006
8007 hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
8008 struct ipr_hostrcb, queue);
8009 list_del(&hostrcb->queue);
8010 memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
8011
8012 rc = ipr_get_ldump_data_section(ioa_cfg,
dcbad00e 8013 be32_to_cpu(sdt.entry[0].start_token),
1da177e4
LT
8014 (__be32 *)&hostrcb->hcam,
8015 min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
8016
65f56475 8017 if (!rc) {
1da177e4 8018 ipr_handle_log_data(ioa_cfg, hostrcb);
4565e370 8019 ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
65f56475
BK
8020 if (ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED &&
8021 ioa_cfg->sdt_state == GET_DUMP)
8022 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
8023 } else
1da177e4
LT
8024 ipr_unit_check_no_data(ioa_cfg);
8025
8026 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
8027}
8028
110def85
WB
8029/**
8030 * ipr_reset_get_unit_check_job - Call to get the unit check buffer.
8031 * @ipr_cmd: ipr command struct
8032 *
8033 * Description: This function will call to get the unit check buffer.
8034 *
8035 * Return value:
8036 * IPR_RC_JOB_RETURN
8037 **/
8038static int ipr_reset_get_unit_check_job(struct ipr_cmnd *ipr_cmd)
8039{
8040 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8041
8042 ENTER;
8043 ioa_cfg->ioa_unit_checked = 0;
8044 ipr_get_unit_check_buffer(ioa_cfg);
8045 ipr_cmd->job_step = ipr_reset_alert;
8046 ipr_reset_start_timer(ipr_cmd, 0);
8047
8048 LEAVE;
8049 return IPR_RC_JOB_RETURN;
8050}
8051
1da177e4
LT
8052/**
8053 * ipr_reset_restore_cfg_space - Restore PCI config space.
8054 * @ipr_cmd: ipr command struct
8055 *
8056 * Description: This function restores the saved PCI config space of
8057 * the adapter, fails all outstanding ops back to the callers, and
8058 * fetches the dump/unit check if applicable to this reset.
8059 *
8060 * Return value:
8061 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8062 **/
8063static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
8064{
8065 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
630ad831 8066 u32 int_reg;
1da177e4
LT
8067
8068 ENTER;
99c965dd 8069 ioa_cfg->pdev->state_saved = true;
1d3c16a8 8070 pci_restore_state(ioa_cfg->pdev);
1da177e4
LT
8071
8072 if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
96d21f00 8073 ipr_cmd->s.ioasa.hdr.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
1da177e4
LT
8074 return IPR_RC_JOB_CONTINUE;
8075 }
8076
8077 ipr_fail_all_ops(ioa_cfg);
8078
8701f185
WB
8079 if (ioa_cfg->sis64) {
8080 /* Set the adapter to the correct endian mode. */
8081 writel(IPR_ENDIAN_SWAP_KEY, ioa_cfg->regs.endian_swap_reg);
8082 int_reg = readl(ioa_cfg->regs.endian_swap_reg);
8083 }
8084
1da177e4 8085 if (ioa_cfg->ioa_unit_checked) {
110def85
WB
8086 if (ioa_cfg->sis64) {
8087 ipr_cmd->job_step = ipr_reset_get_unit_check_job;
8088 ipr_reset_start_timer(ipr_cmd, IPR_DUMP_DELAY_TIMEOUT);
8089 return IPR_RC_JOB_RETURN;
8090 } else {
8091 ioa_cfg->ioa_unit_checked = 0;
8092 ipr_get_unit_check_buffer(ioa_cfg);
8093 ipr_cmd->job_step = ipr_reset_alert;
8094 ipr_reset_start_timer(ipr_cmd, 0);
8095 return IPR_RC_JOB_RETURN;
8096 }
1da177e4
LT
8097 }
8098
8099 if (ioa_cfg->in_ioa_bringdown) {
8100 ipr_cmd->job_step = ipr_ioa_bringdown_done;
8101 } else {
8102 ipr_cmd->job_step = ipr_reset_enable_ioa;
8103
8104 if (GET_DUMP == ioa_cfg->sdt_state) {
41e9a696 8105 ioa_cfg->sdt_state = READ_DUMP;
4c647e90 8106 ioa_cfg->dump_timeout = 0;
4d4dd706
KSS
8107 if (ioa_cfg->sis64)
8108 ipr_reset_start_timer(ipr_cmd, IPR_SIS64_DUMP_TIMEOUT);
8109 else
8110 ipr_reset_start_timer(ipr_cmd, IPR_SIS32_DUMP_TIMEOUT);
1da177e4
LT
8111 ipr_cmd->job_step = ipr_reset_wait_for_dump;
8112 schedule_work(&ioa_cfg->work_q);
8113 return IPR_RC_JOB_RETURN;
8114 }
8115 }
8116
438b0331 8117 LEAVE;
1da177e4
LT
8118 return IPR_RC_JOB_CONTINUE;
8119}
8120
e619e1a7
BK
8121/**
8122 * ipr_reset_bist_done - BIST has completed on the adapter.
8123 * @ipr_cmd: ipr command struct
8124 *
8125 * Description: Unblock config space and resume the reset process.
8126 *
8127 * Return value:
8128 * IPR_RC_JOB_CONTINUE
8129 **/
8130static int ipr_reset_bist_done(struct ipr_cmnd *ipr_cmd)
8131{
fb51ccbf
JK
8132 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8133
e619e1a7 8134 ENTER;
fb51ccbf
JK
8135 if (ioa_cfg->cfg_locked)
8136 pci_cfg_access_unlock(ioa_cfg->pdev);
8137 ioa_cfg->cfg_locked = 0;
e619e1a7
BK
8138 ipr_cmd->job_step = ipr_reset_restore_cfg_space;
8139 LEAVE;
8140 return IPR_RC_JOB_CONTINUE;
8141}
8142
1da177e4
LT
8143/**
8144 * ipr_reset_start_bist - Run BIST on the adapter.
8145 * @ipr_cmd: ipr command struct
8146 *
8147 * Description: This function runs BIST on the adapter, then delays 2 seconds.
8148 *
8149 * Return value:
8150 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8151 **/
8152static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
8153{
8154 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
cb237ef7 8155 int rc = PCIBIOS_SUCCESSFUL;
1da177e4
LT
8156
8157 ENTER;
cb237ef7
WB
8158 if (ioa_cfg->ipr_chip->bist_method == IPR_MMIO)
8159 writel(IPR_UPROCI_SIS64_START_BIST,
8160 ioa_cfg->regs.set_uproc_interrupt_reg32);
8161 else
8162 rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
8163
8164 if (rc == PCIBIOS_SUCCESSFUL) {
e619e1a7 8165 ipr_cmd->job_step = ipr_reset_bist_done;
1da177e4
LT
8166 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
8167 rc = IPR_RC_JOB_RETURN;
cb237ef7 8168 } else {
fb51ccbf
JK
8169 if (ioa_cfg->cfg_locked)
8170 pci_cfg_access_unlock(ipr_cmd->ioa_cfg->pdev);
8171 ioa_cfg->cfg_locked = 0;
cb237ef7
WB
8172 ipr_cmd->s.ioasa.hdr.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
8173 rc = IPR_RC_JOB_CONTINUE;
1da177e4
LT
8174 }
8175
8176 LEAVE;
8177 return rc;
8178}
8179
463fc696
BK
8180/**
8181 * ipr_reset_slot_reset_done - Clear PCI reset to the adapter
8182 * @ipr_cmd: ipr command struct
8183 *
8184 * Description: This clears PCI reset to the adapter and delays two seconds.
8185 *
8186 * Return value:
8187 * IPR_RC_JOB_RETURN
8188 **/
8189static int ipr_reset_slot_reset_done(struct ipr_cmnd *ipr_cmd)
8190{
8191 ENTER;
8192 pci_set_pcie_reset_state(ipr_cmd->ioa_cfg->pdev, pcie_deassert_reset);
8193 ipr_cmd->job_step = ipr_reset_bist_done;
8194 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
8195 LEAVE;
8196 return IPR_RC_JOB_RETURN;
8197}
8198
8199/**
8200 * ipr_reset_slot_reset - Reset the PCI slot of the adapter.
8201 * @ipr_cmd: ipr command struct
8202 *
8203 * Description: This asserts PCI reset to the adapter.
8204 *
8205 * Return value:
8206 * IPR_RC_JOB_RETURN
8207 **/
8208static int ipr_reset_slot_reset(struct ipr_cmnd *ipr_cmd)
8209{
8210 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8211 struct pci_dev *pdev = ioa_cfg->pdev;
8212
8213 ENTER;
463fc696
BK
8214 pci_set_pcie_reset_state(pdev, pcie_warm_reset);
8215 ipr_cmd->job_step = ipr_reset_slot_reset_done;
8216 ipr_reset_start_timer(ipr_cmd, IPR_PCI_RESET_TIMEOUT);
8217 LEAVE;
8218 return IPR_RC_JOB_RETURN;
8219}
8220
fb51ccbf
JK
8221/**
8222 * ipr_reset_block_config_access_wait - Wait for permission to block config access
8223 * @ipr_cmd: ipr command struct
8224 *
8225 * Description: This attempts to block config access to the IOA.
8226 *
8227 * Return value:
8228 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8229 **/
8230static int ipr_reset_block_config_access_wait(struct ipr_cmnd *ipr_cmd)
8231{
8232 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8233 int rc = IPR_RC_JOB_CONTINUE;
8234
8235 if (pci_cfg_access_trylock(ioa_cfg->pdev)) {
8236 ioa_cfg->cfg_locked = 1;
8237 ipr_cmd->job_step = ioa_cfg->reset;
8238 } else {
8239 if (ipr_cmd->u.time_left) {
8240 rc = IPR_RC_JOB_RETURN;
8241 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
8242 ipr_reset_start_timer(ipr_cmd,
8243 IPR_CHECK_FOR_RESET_TIMEOUT);
8244 } else {
8245 ipr_cmd->job_step = ioa_cfg->reset;
8246 dev_err(&ioa_cfg->pdev->dev,
8247 "Timed out waiting to lock config access. Resetting anyway.\n");
8248 }
8249 }
8250
8251 return rc;
8252}
8253
8254/**
8255 * ipr_reset_block_config_access - Block config access to the IOA
8256 * @ipr_cmd: ipr command struct
8257 *
8258 * Description: This attempts to block config access to the IOA
8259 *
8260 * Return value:
8261 * IPR_RC_JOB_CONTINUE
8262 **/
8263static int ipr_reset_block_config_access(struct ipr_cmnd *ipr_cmd)
8264{
8265 ipr_cmd->ioa_cfg->cfg_locked = 0;
8266 ipr_cmd->job_step = ipr_reset_block_config_access_wait;
8267 ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
8268 return IPR_RC_JOB_CONTINUE;
8269}
8270
1da177e4
LT
8271/**
8272 * ipr_reset_allowed - Query whether or not IOA can be reset
8273 * @ioa_cfg: ioa config struct
8274 *
8275 * Return value:
8276 * 0 if reset not allowed / non-zero if reset is allowed
8277 **/
8278static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
8279{
8280 volatile u32 temp_reg;
8281
8282 temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
8283 return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
8284}
8285
8286/**
8287 * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
8288 * @ipr_cmd: ipr command struct
8289 *
8290 * Description: This function waits for adapter permission to run BIST,
8291 * then runs BIST. If the adapter does not give permission after a
8292 * reasonable time, we will reset the adapter anyway. The impact of
8293 * resetting the adapter without warning the adapter is the risk of
8294 * losing the persistent error log on the adapter. If the adapter is
8295 * reset while it is writing to the flash on the adapter, the flash
8296 * segment will have bad ECC and be zeroed.
8297 *
8298 * Return value:
8299 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8300 **/
8301static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
8302{
8303 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8304 int rc = IPR_RC_JOB_RETURN;
8305
8306 if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
8307 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
8308 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
8309 } else {
fb51ccbf 8310 ipr_cmd->job_step = ipr_reset_block_config_access;
1da177e4
LT
8311 rc = IPR_RC_JOB_CONTINUE;
8312 }
8313
8314 return rc;
8315}
8316
8317/**
8701f185 8318 * ipr_reset_alert - Alert the adapter of a pending reset
1da177e4
LT
8319 * @ipr_cmd: ipr command struct
8320 *
8321 * Description: This function alerts the adapter that it will be reset.
8322 * If memory space is not currently enabled, proceed directly
8323 * to running BIST on the adapter. The timer must always be started
8324 * so we guarantee we do not run BIST from ipr_isr.
8325 *
8326 * Return value:
8327 * IPR_RC_JOB_RETURN
8328 **/
8329static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
8330{
8331 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8332 u16 cmd_reg;
8333 int rc;
8334
8335 ENTER;
8336 rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
8337
8338 if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
8339 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
214777ba 8340 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg32);
1da177e4
LT
8341 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
8342 } else {
fb51ccbf 8343 ipr_cmd->job_step = ipr_reset_block_config_access;
1da177e4
LT
8344 }
8345
8346 ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
8347 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
8348
8349 LEAVE;
8350 return IPR_RC_JOB_RETURN;
8351}
8352
8353/**
8354 * ipr_reset_ucode_download_done - Microcode download completion
8355 * @ipr_cmd: ipr command struct
8356 *
8357 * Description: This function unmaps the microcode download buffer.
8358 *
8359 * Return value:
8360 * IPR_RC_JOB_CONTINUE
8361 **/
8362static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
8363{
8364 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8365 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
8366
d73341bf 8367 dma_unmap_sg(&ioa_cfg->pdev->dev, sglist->scatterlist,
1da177e4
LT
8368 sglist->num_sg, DMA_TO_DEVICE);
8369
8370 ipr_cmd->job_step = ipr_reset_alert;
8371 return IPR_RC_JOB_CONTINUE;
8372}
8373
8374/**
8375 * ipr_reset_ucode_download - Download microcode to the adapter
8376 * @ipr_cmd: ipr command struct
8377 *
8378 * Description: This function checks to see if it there is microcode
8379 * to download to the adapter. If there is, a download is performed.
8380 *
8381 * Return value:
8382 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8383 **/
8384static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
8385{
8386 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8387 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
8388
8389 ENTER;
8390 ipr_cmd->job_step = ipr_reset_alert;
8391
8392 if (!sglist)
8393 return IPR_RC_JOB_CONTINUE;
8394
8395 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
8396 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
8397 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
8398 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
8399 ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
8400 ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
8401 ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
8402
a32c055f
WB
8403 if (ioa_cfg->sis64)
8404 ipr_build_ucode_ioadl64(ipr_cmd, sglist);
8405 else
8406 ipr_build_ucode_ioadl(ipr_cmd, sglist);
1da177e4
LT
8407 ipr_cmd->job_step = ipr_reset_ucode_download_done;
8408
8409 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
8410 IPR_WRITE_BUFFER_TIMEOUT);
8411
8412 LEAVE;
8413 return IPR_RC_JOB_RETURN;
8414}
8415
8416/**
8417 * ipr_reset_shutdown_ioa - Shutdown the adapter
8418 * @ipr_cmd: ipr command struct
8419 *
8420 * Description: This function issues an adapter shutdown of the
8421 * specified type to the specified adapter as part of the
8422 * adapter reset job.
8423 *
8424 * Return value:
8425 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8426 **/
8427static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
8428{
8429 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8430 enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
8431 unsigned long timeout;
8432 int rc = IPR_RC_JOB_CONTINUE;
8433
8434 ENTER;
56d6aa33 8435 if (shutdown_type != IPR_SHUTDOWN_NONE &&
8436 !ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
1da177e4
LT
8437 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
8438 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
8439 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
8440 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
8441
ac09c349
BK
8442 if (shutdown_type == IPR_SHUTDOWN_NORMAL)
8443 timeout = IPR_SHUTDOWN_TIMEOUT;
1da177e4
LT
8444 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
8445 timeout = IPR_INTERNAL_TIMEOUT;
ac09c349
BK
8446 else if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
8447 timeout = IPR_DUAL_IOA_ABBR_SHUTDOWN_TO;
1da177e4 8448 else
ac09c349 8449 timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
1da177e4
LT
8450
8451 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
8452
8453 rc = IPR_RC_JOB_RETURN;
8454 ipr_cmd->job_step = ipr_reset_ucode_download;
8455 } else
8456 ipr_cmd->job_step = ipr_reset_alert;
8457
8458 LEAVE;
8459 return rc;
8460}
8461
8462/**
8463 * ipr_reset_ioa_job - Adapter reset job
8464 * @ipr_cmd: ipr command struct
8465 *
8466 * Description: This function is the job router for the adapter reset job.
8467 *
8468 * Return value:
8469 * none
8470 **/
8471static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
8472{
8473 u32 rc, ioasc;
1da177e4
LT
8474 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8475
8476 do {
96d21f00 8477 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1da177e4
LT
8478
8479 if (ioa_cfg->reset_cmd != ipr_cmd) {
8480 /*
8481 * We are doing nested adapter resets and this is
8482 * not the current reset job.
8483 */
05a6538a 8484 list_add_tail(&ipr_cmd->queue,
8485 &ipr_cmd->hrrq->hrrq_free_q);
1da177e4
LT
8486 return;
8487 }
8488
8489 if (IPR_IOASC_SENSE_KEY(ioasc)) {
dfed823e
BK
8490 rc = ipr_cmd->job_step_failed(ipr_cmd);
8491 if (rc == IPR_RC_JOB_RETURN)
8492 return;
1da177e4
LT
8493 }
8494
8495 ipr_reinit_ipr_cmnd(ipr_cmd);
dfed823e 8496 ipr_cmd->job_step_failed = ipr_reset_cmd_failed;
1da177e4 8497 rc = ipr_cmd->job_step(ipr_cmd);
203fa3fe 8498 } while (rc == IPR_RC_JOB_CONTINUE);
1da177e4
LT
8499}
8500
8501/**
8502 * _ipr_initiate_ioa_reset - Initiate an adapter reset
8503 * @ioa_cfg: ioa config struct
8504 * @job_step: first job step of reset job
8505 * @shutdown_type: shutdown type
8506 *
8507 * Description: This function will initiate the reset of the given adapter
8508 * starting at the selected job step.
8509 * If the caller needs to wait on the completion of the reset,
8510 * the caller must sleep on the reset_wait_q.
8511 *
8512 * Return value:
8513 * none
8514 **/
8515static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
8516 int (*job_step) (struct ipr_cmnd *),
8517 enum ipr_shutdown_type shutdown_type)
8518{
8519 struct ipr_cmnd *ipr_cmd;
56d6aa33 8520 int i;
1da177e4
LT
8521
8522 ioa_cfg->in_reset_reload = 1;
56d6aa33 8523 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8524 spin_lock(&ioa_cfg->hrrq[i]._lock);
8525 ioa_cfg->hrrq[i].allow_cmds = 0;
8526 spin_unlock(&ioa_cfg->hrrq[i]._lock);
8527 }
8528 wmb();
bfae7820
BK
8529 if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].removing_ioa)
8530 scsi_block_requests(ioa_cfg->host);
1da177e4
LT
8531
8532 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
8533 ioa_cfg->reset_cmd = ipr_cmd;
8534 ipr_cmd->job_step = job_step;
8535 ipr_cmd->u.shutdown_type = shutdown_type;
8536
8537 ipr_reset_ioa_job(ipr_cmd);
8538}
8539
8540/**
8541 * ipr_initiate_ioa_reset - Initiate an adapter reset
8542 * @ioa_cfg: ioa config struct
8543 * @shutdown_type: shutdown type
8544 *
8545 * Description: This function will initiate the reset of the given adapter.
8546 * If the caller needs to wait on the completion of the reset,
8547 * the caller must sleep on the reset_wait_q.
8548 *
8549 * Return value:
8550 * none
8551 **/
8552static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
8553 enum ipr_shutdown_type shutdown_type)
8554{
56d6aa33 8555 int i;
8556
8557 if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
1da177e4
LT
8558 return;
8559
41e9a696
BK
8560 if (ioa_cfg->in_reset_reload) {
8561 if (ioa_cfg->sdt_state == GET_DUMP)
8562 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
8563 else if (ioa_cfg->sdt_state == READ_DUMP)
8564 ioa_cfg->sdt_state = ABORT_DUMP;
8565 }
1da177e4
LT
8566
8567 if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
8568 dev_err(&ioa_cfg->pdev->dev,
8569 "IOA taken offline - error recovery failed\n");
8570
8571 ioa_cfg->reset_retries = 0;
56d6aa33 8572 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8573 spin_lock(&ioa_cfg->hrrq[i]._lock);
8574 ioa_cfg->hrrq[i].ioa_is_dead = 1;
8575 spin_unlock(&ioa_cfg->hrrq[i]._lock);
8576 }
8577 wmb();
1da177e4
LT
8578
8579 if (ioa_cfg->in_ioa_bringdown) {
8580 ioa_cfg->reset_cmd = NULL;
8581 ioa_cfg->in_reset_reload = 0;
8582 ipr_fail_all_ops(ioa_cfg);
8583 wake_up_all(&ioa_cfg->reset_wait_q);
8584
bfae7820
BK
8585 if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].removing_ioa) {
8586 spin_unlock_irq(ioa_cfg->host->host_lock);
8587 scsi_unblock_requests(ioa_cfg->host);
8588 spin_lock_irq(ioa_cfg->host->host_lock);
8589 }
1da177e4
LT
8590 return;
8591 } else {
8592 ioa_cfg->in_ioa_bringdown = 1;
8593 shutdown_type = IPR_SHUTDOWN_NONE;
8594 }
8595 }
8596
8597 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
8598 shutdown_type);
8599}
8600
f8a88b19
LV
8601/**
8602 * ipr_reset_freeze - Hold off all I/O activity
8603 * @ipr_cmd: ipr command struct
8604 *
8605 * Description: If the PCI slot is frozen, hold off all I/O
8606 * activity; then, as soon as the slot is available again,
8607 * initiate an adapter reset.
8608 */
8609static int ipr_reset_freeze(struct ipr_cmnd *ipr_cmd)
8610{
56d6aa33 8611 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8612 int i;
8613
f8a88b19 8614 /* Disallow new interrupts, avoid loop */
56d6aa33 8615 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8616 spin_lock(&ioa_cfg->hrrq[i]._lock);
8617 ioa_cfg->hrrq[i].allow_interrupts = 0;
8618 spin_unlock(&ioa_cfg->hrrq[i]._lock);
8619 }
8620 wmb();
05a6538a 8621 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
f8a88b19
LV
8622 ipr_cmd->done = ipr_reset_ioa_job;
8623 return IPR_RC_JOB_RETURN;
8624}
8625
6270e593
BK
8626/**
8627 * ipr_pci_mmio_enabled - Called when MMIO has been re-enabled
8628 * @pdev: PCI device struct
8629 *
8630 * Description: This routine is called to tell us that the MMIO
8631 * access to the IOA has been restored
8632 */
8633static pci_ers_result_t ipr_pci_mmio_enabled(struct pci_dev *pdev)
8634{
8635 unsigned long flags = 0;
8636 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8637
8638 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
8639 if (!ioa_cfg->probe_done)
8640 pci_save_state(pdev);
8641 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8642 return PCI_ERS_RESULT_NEED_RESET;
8643}
8644
f8a88b19
LV
8645/**
8646 * ipr_pci_frozen - Called when slot has experienced a PCI bus error.
8647 * @pdev: PCI device struct
8648 *
8649 * Description: This routine is called to tell us that the PCI bus
8650 * is down. Can't do anything here, except put the device driver
8651 * into a holding pattern, waiting for the PCI bus to come back.
8652 */
8653static void ipr_pci_frozen(struct pci_dev *pdev)
8654{
8655 unsigned long flags = 0;
8656 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8657
8658 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6270e593
BK
8659 if (ioa_cfg->probe_done)
8660 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_freeze, IPR_SHUTDOWN_NONE);
f8a88b19
LV
8661 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8662}
8663
8664/**
8665 * ipr_pci_slot_reset - Called when PCI slot has been reset.
8666 * @pdev: PCI device struct
8667 *
8668 * Description: This routine is called by the pci error recovery
8669 * code after the PCI slot has been reset, just before we
8670 * should resume normal operations.
8671 */
8672static pci_ers_result_t ipr_pci_slot_reset(struct pci_dev *pdev)
8673{
8674 unsigned long flags = 0;
8675 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8676
8677 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6270e593
BK
8678 if (ioa_cfg->probe_done) {
8679 if (ioa_cfg->needs_warm_reset)
8680 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8681 else
8682 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_restore_cfg_space,
8683 IPR_SHUTDOWN_NONE);
8684 } else
8685 wake_up_all(&ioa_cfg->eeh_wait_q);
f8a88b19
LV
8686 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8687 return PCI_ERS_RESULT_RECOVERED;
8688}
8689
8690/**
8691 * ipr_pci_perm_failure - Called when PCI slot is dead for good.
8692 * @pdev: PCI device struct
8693 *
8694 * Description: This routine is called when the PCI bus has
8695 * permanently failed.
8696 */
8697static void ipr_pci_perm_failure(struct pci_dev *pdev)
8698{
8699 unsigned long flags = 0;
8700 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
56d6aa33 8701 int i;
f8a88b19
LV
8702
8703 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6270e593
BK
8704 if (ioa_cfg->probe_done) {
8705 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
8706 ioa_cfg->sdt_state = ABORT_DUMP;
8707 ioa_cfg->reset_retries = IPR_NUM_RESET_RELOAD_RETRIES - 1;
8708 ioa_cfg->in_ioa_bringdown = 1;
8709 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8710 spin_lock(&ioa_cfg->hrrq[i]._lock);
8711 ioa_cfg->hrrq[i].allow_cmds = 0;
8712 spin_unlock(&ioa_cfg->hrrq[i]._lock);
8713 }
8714 wmb();
8715 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8716 } else
8717 wake_up_all(&ioa_cfg->eeh_wait_q);
f8a88b19
LV
8718 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8719}
8720
8721/**
8722 * ipr_pci_error_detected - Called when a PCI error is detected.
8723 * @pdev: PCI device struct
8724 * @state: PCI channel state
8725 *
8726 * Description: Called when a PCI error is detected.
8727 *
8728 * Return value:
8729 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8730 */
8731static pci_ers_result_t ipr_pci_error_detected(struct pci_dev *pdev,
8732 pci_channel_state_t state)
8733{
8734 switch (state) {
8735 case pci_channel_io_frozen:
8736 ipr_pci_frozen(pdev);
6270e593 8737 return PCI_ERS_RESULT_CAN_RECOVER;
f8a88b19
LV
8738 case pci_channel_io_perm_failure:
8739 ipr_pci_perm_failure(pdev);
8740 return PCI_ERS_RESULT_DISCONNECT;
8741 break;
8742 default:
8743 break;
8744 }
8745 return PCI_ERS_RESULT_NEED_RESET;
8746}
8747
1da177e4
LT
8748/**
8749 * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
8750 * @ioa_cfg: ioa cfg struct
8751 *
8752 * Description: This is the second phase of adapter intialization
8753 * This function takes care of initilizing the adapter to the point
8754 * where it can accept new commands.
8755
8756 * Return value:
b1c11812 8757 * 0 on success / -EIO on failure
1da177e4 8758 **/
6f039790 8759static int ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
1da177e4
LT
8760{
8761 int rc = 0;
8762 unsigned long host_lock_flags = 0;
8763
8764 ENTER;
8765 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
8766 dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
6270e593 8767 ioa_cfg->probe_done = 1;
ce155cce
BK
8768 if (ioa_cfg->needs_hard_reset) {
8769 ioa_cfg->needs_hard_reset = 0;
8770 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8771 } else
8772 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa,
8773 IPR_SHUTDOWN_NONE);
1da177e4
LT
8774 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
8775 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
8776 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
8777
56d6aa33 8778 if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
1da177e4
LT
8779 rc = -EIO;
8780 } else if (ipr_invalid_adapter(ioa_cfg)) {
8781 if (!ipr_testmode)
8782 rc = -EIO;
8783
8784 dev_err(&ioa_cfg->pdev->dev,
8785 "Adapter not supported in this hardware configuration.\n");
8786 }
8787
8788 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
8789
8790 LEAVE;
8791 return rc;
8792}
8793
8794/**
8795 * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
8796 * @ioa_cfg: ioa config struct
8797 *
8798 * Return value:
8799 * none
8800 **/
8801static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
8802{
8803 int i;
8804
8805 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
8806 if (ioa_cfg->ipr_cmnd_list[i])
d73341bf 8807 dma_pool_free(ioa_cfg->ipr_cmd_pool,
1da177e4
LT
8808 ioa_cfg->ipr_cmnd_list[i],
8809 ioa_cfg->ipr_cmnd_list_dma[i]);
8810
8811 ioa_cfg->ipr_cmnd_list[i] = NULL;
8812 }
8813
8814 if (ioa_cfg->ipr_cmd_pool)
d73341bf 8815 dma_pool_destroy(ioa_cfg->ipr_cmd_pool);
1da177e4 8816
89aad428
BK
8817 kfree(ioa_cfg->ipr_cmnd_list);
8818 kfree(ioa_cfg->ipr_cmnd_list_dma);
8819 ioa_cfg->ipr_cmnd_list = NULL;
8820 ioa_cfg->ipr_cmnd_list_dma = NULL;
1da177e4
LT
8821 ioa_cfg->ipr_cmd_pool = NULL;
8822}
8823
8824/**
8825 * ipr_free_mem - Frees memory allocated for an adapter
8826 * @ioa_cfg: ioa cfg struct
8827 *
8828 * Return value:
8829 * nothing
8830 **/
8831static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
8832{
8833 int i;
8834
8835 kfree(ioa_cfg->res_entries);
d73341bf
AB
8836 dma_free_coherent(&ioa_cfg->pdev->dev, sizeof(struct ipr_misc_cbs),
8837 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
1da177e4 8838 ipr_free_cmd_blks(ioa_cfg);
05a6538a 8839
8840 for (i = 0; i < ioa_cfg->hrrq_num; i++)
d73341bf
AB
8841 dma_free_coherent(&ioa_cfg->pdev->dev,
8842 sizeof(u32) * ioa_cfg->hrrq[i].size,
8843 ioa_cfg->hrrq[i].host_rrq,
8844 ioa_cfg->hrrq[i].host_rrq_dma);
05a6538a 8845
d73341bf
AB
8846 dma_free_coherent(&ioa_cfg->pdev->dev, ioa_cfg->cfg_table_size,
8847 ioa_cfg->u.cfg_table, ioa_cfg->cfg_table_dma);
1da177e4
LT
8848
8849 for (i = 0; i < IPR_NUM_HCAMS; i++) {
d73341bf
AB
8850 dma_free_coherent(&ioa_cfg->pdev->dev,
8851 sizeof(struct ipr_hostrcb),
8852 ioa_cfg->hostrcb[i],
8853 ioa_cfg->hostrcb_dma[i]);
1da177e4
LT
8854 }
8855
8856 ipr_free_dump(ioa_cfg);
1da177e4
LT
8857 kfree(ioa_cfg->trace);
8858}
8859
8860/**
8861 * ipr_free_all_resources - Free all allocated resources for an adapter.
8862 * @ipr_cmd: ipr command struct
8863 *
8864 * This function frees all allocated resources for the
8865 * specified adapter.
8866 *
8867 * Return value:
8868 * none
8869 **/
8870static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
8871{
8872 struct pci_dev *pdev = ioa_cfg->pdev;
8873
8874 ENTER;
05a6538a 8875 if (ioa_cfg->intr_flag == IPR_USE_MSI ||
8876 ioa_cfg->intr_flag == IPR_USE_MSIX) {
8877 int i;
8878 for (i = 0; i < ioa_cfg->nvectors; i++)
8879 free_irq(ioa_cfg->vectors_info[i].vec,
8880 &ioa_cfg->hrrq[i]);
8881 } else
8882 free_irq(pdev->irq, &ioa_cfg->hrrq[0]);
8883
56d6aa33 8884 if (ioa_cfg->intr_flag == IPR_USE_MSI) {
05a6538a 8885 pci_disable_msi(pdev);
56d6aa33 8886 ioa_cfg->intr_flag &= ~IPR_USE_MSI;
8887 } else if (ioa_cfg->intr_flag == IPR_USE_MSIX) {
05a6538a 8888 pci_disable_msix(pdev);
56d6aa33 8889 ioa_cfg->intr_flag &= ~IPR_USE_MSIX;
8890 }
05a6538a 8891
1da177e4
LT
8892 iounmap(ioa_cfg->hdw_dma_regs);
8893 pci_release_regions(pdev);
8894 ipr_free_mem(ioa_cfg);
8895 scsi_host_put(ioa_cfg->host);
8896 pci_disable_device(pdev);
8897 LEAVE;
8898}
8899
8900/**
8901 * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
8902 * @ioa_cfg: ioa config struct
8903 *
8904 * Return value:
8905 * 0 on success / -ENOMEM on allocation failure
8906 **/
6f039790 8907static int ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
1da177e4
LT
8908{
8909 struct ipr_cmnd *ipr_cmd;
8910 struct ipr_ioarcb *ioarcb;
8911 dma_addr_t dma_addr;
05a6538a 8912 int i, entries_each_hrrq, hrrq_id = 0;
1da177e4 8913
d73341bf 8914 ioa_cfg->ipr_cmd_pool = dma_pool_create(IPR_NAME, &ioa_cfg->pdev->dev,
203fa3fe 8915 sizeof(struct ipr_cmnd), 512, 0);
1da177e4
LT
8916
8917 if (!ioa_cfg->ipr_cmd_pool)
8918 return -ENOMEM;
8919
89aad428
BK
8920 ioa_cfg->ipr_cmnd_list = kcalloc(IPR_NUM_CMD_BLKS, sizeof(struct ipr_cmnd *), GFP_KERNEL);
8921 ioa_cfg->ipr_cmnd_list_dma = kcalloc(IPR_NUM_CMD_BLKS, sizeof(dma_addr_t), GFP_KERNEL);
8922
8923 if (!ioa_cfg->ipr_cmnd_list || !ioa_cfg->ipr_cmnd_list_dma) {
8924 ipr_free_cmd_blks(ioa_cfg);
8925 return -ENOMEM;
8926 }
8927
05a6538a 8928 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8929 if (ioa_cfg->hrrq_num > 1) {
8930 if (i == 0) {
8931 entries_each_hrrq = IPR_NUM_INTERNAL_CMD_BLKS;
8932 ioa_cfg->hrrq[i].min_cmd_id = 0;
8933 ioa_cfg->hrrq[i].max_cmd_id =
8934 (entries_each_hrrq - 1);
8935 } else {
8936 entries_each_hrrq =
8937 IPR_NUM_BASE_CMD_BLKS/
8938 (ioa_cfg->hrrq_num - 1);
8939 ioa_cfg->hrrq[i].min_cmd_id =
8940 IPR_NUM_INTERNAL_CMD_BLKS +
8941 (i - 1) * entries_each_hrrq;
8942 ioa_cfg->hrrq[i].max_cmd_id =
8943 (IPR_NUM_INTERNAL_CMD_BLKS +
8944 i * entries_each_hrrq - 1);
8945 }
8946 } else {
8947 entries_each_hrrq = IPR_NUM_CMD_BLKS;
8948 ioa_cfg->hrrq[i].min_cmd_id = 0;
8949 ioa_cfg->hrrq[i].max_cmd_id = (entries_each_hrrq - 1);
8950 }
8951 ioa_cfg->hrrq[i].size = entries_each_hrrq;
8952 }
8953
8954 BUG_ON(ioa_cfg->hrrq_num == 0);
8955
8956 i = IPR_NUM_CMD_BLKS -
8957 ioa_cfg->hrrq[ioa_cfg->hrrq_num - 1].max_cmd_id - 1;
8958 if (i > 0) {
8959 ioa_cfg->hrrq[ioa_cfg->hrrq_num - 1].size += i;
8960 ioa_cfg->hrrq[ioa_cfg->hrrq_num - 1].max_cmd_id += i;
8961 }
8962
1da177e4 8963 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
d73341bf 8964 ipr_cmd = dma_pool_alloc(ioa_cfg->ipr_cmd_pool, GFP_KERNEL, &dma_addr);
1da177e4
LT
8965
8966 if (!ipr_cmd) {
8967 ipr_free_cmd_blks(ioa_cfg);
8968 return -ENOMEM;
8969 }
8970
8971 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
8972 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
8973 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
8974
8975 ioarcb = &ipr_cmd->ioarcb;
a32c055f
WB
8976 ipr_cmd->dma_addr = dma_addr;
8977 if (ioa_cfg->sis64)
8978 ioarcb->a.ioarcb_host_pci_addr64 = cpu_to_be64(dma_addr);
8979 else
8980 ioarcb->a.ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
8981
1da177e4 8982 ioarcb->host_response_handle = cpu_to_be32(i << 2);
a32c055f
WB
8983 if (ioa_cfg->sis64) {
8984 ioarcb->u.sis64_addr_data.data_ioadl_addr =
8985 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
8986 ioarcb->u.sis64_addr_data.ioasa_host_pci_addr =
96d21f00 8987 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, s.ioasa64));
a32c055f
WB
8988 } else {
8989 ioarcb->write_ioadl_addr =
8990 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
8991 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
8992 ioarcb->ioasa_host_pci_addr =
96d21f00 8993 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, s.ioasa));
a32c055f 8994 }
1da177e4
LT
8995 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
8996 ipr_cmd->cmd_index = i;
8997 ipr_cmd->ioa_cfg = ioa_cfg;
8998 ipr_cmd->sense_buffer_dma = dma_addr +
8999 offsetof(struct ipr_cmnd, sense_buffer);
9000
05a6538a 9001 ipr_cmd->ioarcb.cmd_pkt.hrrq_id = hrrq_id;
9002 ipr_cmd->hrrq = &ioa_cfg->hrrq[hrrq_id];
9003 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
9004 if (i >= ioa_cfg->hrrq[hrrq_id].max_cmd_id)
9005 hrrq_id++;
1da177e4
LT
9006 }
9007
9008 return 0;
9009}
9010
9011/**
9012 * ipr_alloc_mem - Allocate memory for an adapter
9013 * @ioa_cfg: ioa config struct
9014 *
9015 * Return value:
9016 * 0 on success / non-zero for error
9017 **/
6f039790 9018static int ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
1da177e4
LT
9019{
9020 struct pci_dev *pdev = ioa_cfg->pdev;
9021 int i, rc = -ENOMEM;
9022
9023 ENTER;
0bc42e35 9024 ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
3e7ebdfa 9025 ioa_cfg->max_devs_supported, GFP_KERNEL);
1da177e4
LT
9026
9027 if (!ioa_cfg->res_entries)
9028 goto out;
9029
3e7ebdfa 9030 for (i = 0; i < ioa_cfg->max_devs_supported; i++) {
1da177e4 9031 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
3e7ebdfa
WB
9032 ioa_cfg->res_entries[i].ioa_cfg = ioa_cfg;
9033 }
1da177e4 9034
d73341bf
AB
9035 ioa_cfg->vpd_cbs = dma_alloc_coherent(&pdev->dev,
9036 sizeof(struct ipr_misc_cbs),
9037 &ioa_cfg->vpd_cbs_dma,
9038 GFP_KERNEL);
1da177e4
LT
9039
9040 if (!ioa_cfg->vpd_cbs)
9041 goto out_free_res_entries;
9042
9043 if (ipr_alloc_cmd_blks(ioa_cfg))
9044 goto out_free_vpd_cbs;
9045
05a6538a 9046 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
d73341bf 9047 ioa_cfg->hrrq[i].host_rrq = dma_alloc_coherent(&pdev->dev,
05a6538a 9048 sizeof(u32) * ioa_cfg->hrrq[i].size,
d73341bf
AB
9049 &ioa_cfg->hrrq[i].host_rrq_dma,
9050 GFP_KERNEL);
05a6538a 9051
9052 if (!ioa_cfg->hrrq[i].host_rrq) {
9053 while (--i > 0)
d73341bf 9054 dma_free_coherent(&pdev->dev,
05a6538a 9055 sizeof(u32) * ioa_cfg->hrrq[i].size,
9056 ioa_cfg->hrrq[i].host_rrq,
9057 ioa_cfg->hrrq[i].host_rrq_dma);
9058 goto out_ipr_free_cmd_blocks;
9059 }
9060 ioa_cfg->hrrq[i].ioa_cfg = ioa_cfg;
9061 }
1da177e4 9062
d73341bf
AB
9063 ioa_cfg->u.cfg_table = dma_alloc_coherent(&pdev->dev,
9064 ioa_cfg->cfg_table_size,
9065 &ioa_cfg->cfg_table_dma,
9066 GFP_KERNEL);
1da177e4 9067
3e7ebdfa 9068 if (!ioa_cfg->u.cfg_table)
1da177e4
LT
9069 goto out_free_host_rrq;
9070
9071 for (i = 0; i < IPR_NUM_HCAMS; i++) {
d73341bf
AB
9072 ioa_cfg->hostrcb[i] = dma_alloc_coherent(&pdev->dev,
9073 sizeof(struct ipr_hostrcb),
9074 &ioa_cfg->hostrcb_dma[i],
9075 GFP_KERNEL);
1da177e4
LT
9076
9077 if (!ioa_cfg->hostrcb[i])
9078 goto out_free_hostrcb_dma;
9079
9080 ioa_cfg->hostrcb[i]->hostrcb_dma =
9081 ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
49dc6a18 9082 ioa_cfg->hostrcb[i]->ioa_cfg = ioa_cfg;
1da177e4
LT
9083 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
9084 }
9085
0bc42e35 9086 ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
1da177e4
LT
9087 IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
9088
9089 if (!ioa_cfg->trace)
9090 goto out_free_hostrcb_dma;
9091
1da177e4
LT
9092 rc = 0;
9093out:
9094 LEAVE;
9095 return rc;
9096
9097out_free_hostrcb_dma:
9098 while (i-- > 0) {
d73341bf
AB
9099 dma_free_coherent(&pdev->dev, sizeof(struct ipr_hostrcb),
9100 ioa_cfg->hostrcb[i],
9101 ioa_cfg->hostrcb_dma[i]);
1da177e4 9102 }
d73341bf
AB
9103 dma_free_coherent(&pdev->dev, ioa_cfg->cfg_table_size,
9104 ioa_cfg->u.cfg_table, ioa_cfg->cfg_table_dma);
1da177e4 9105out_free_host_rrq:
05a6538a 9106 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
d73341bf
AB
9107 dma_free_coherent(&pdev->dev,
9108 sizeof(u32) * ioa_cfg->hrrq[i].size,
9109 ioa_cfg->hrrq[i].host_rrq,
9110 ioa_cfg->hrrq[i].host_rrq_dma);
05a6538a 9111 }
1da177e4
LT
9112out_ipr_free_cmd_blocks:
9113 ipr_free_cmd_blks(ioa_cfg);
9114out_free_vpd_cbs:
d73341bf
AB
9115 dma_free_coherent(&pdev->dev, sizeof(struct ipr_misc_cbs),
9116 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
1da177e4
LT
9117out_free_res_entries:
9118 kfree(ioa_cfg->res_entries);
9119 goto out;
9120}
9121
9122/**
9123 * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
9124 * @ioa_cfg: ioa config struct
9125 *
9126 * Return value:
9127 * none
9128 **/
6f039790 9129static void ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
1da177e4
LT
9130{
9131 int i;
9132
9133 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
9134 ioa_cfg->bus_attr[i].bus = i;
9135 ioa_cfg->bus_attr[i].qas_enabled = 0;
9136 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
9137 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
9138 ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
9139 else
9140 ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
9141 }
9142}
9143
6270e593
BK
9144/**
9145 * ipr_init_regs - Initialize IOA registers
9146 * @ioa_cfg: ioa config struct
9147 *
9148 * Return value:
9149 * none
9150 **/
9151static void ipr_init_regs(struct ipr_ioa_cfg *ioa_cfg)
9152{
9153 const struct ipr_interrupt_offsets *p;
9154 struct ipr_interrupts *t;
9155 void __iomem *base;
9156
9157 p = &ioa_cfg->chip_cfg->regs;
9158 t = &ioa_cfg->regs;
9159 base = ioa_cfg->hdw_dma_regs;
9160
9161 t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
9162 t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
9163 t->clr_interrupt_mask_reg32 = base + p->clr_interrupt_mask_reg32;
9164 t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
9165 t->sense_interrupt_mask_reg32 = base + p->sense_interrupt_mask_reg32;
9166 t->clr_interrupt_reg = base + p->clr_interrupt_reg;
9167 t->clr_interrupt_reg32 = base + p->clr_interrupt_reg32;
9168 t->sense_interrupt_reg = base + p->sense_interrupt_reg;
9169 t->sense_interrupt_reg32 = base + p->sense_interrupt_reg32;
9170 t->ioarrin_reg = base + p->ioarrin_reg;
9171 t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
9172 t->sense_uproc_interrupt_reg32 = base + p->sense_uproc_interrupt_reg32;
9173 t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
9174 t->set_uproc_interrupt_reg32 = base + p->set_uproc_interrupt_reg32;
9175 t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
9176 t->clr_uproc_interrupt_reg32 = base + p->clr_uproc_interrupt_reg32;
9177
9178 if (ioa_cfg->sis64) {
9179 t->init_feedback_reg = base + p->init_feedback_reg;
9180 t->dump_addr_reg = base + p->dump_addr_reg;
9181 t->dump_data_reg = base + p->dump_data_reg;
9182 t->endian_swap_reg = base + p->endian_swap_reg;
9183 }
9184}
9185
1da177e4
LT
9186/**
9187 * ipr_init_ioa_cfg - Initialize IOA config struct
9188 * @ioa_cfg: ioa config struct
9189 * @host: scsi host struct
9190 * @pdev: PCI dev struct
9191 *
9192 * Return value:
9193 * none
9194 **/
6f039790
GKH
9195static void ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
9196 struct Scsi_Host *host, struct pci_dev *pdev)
1da177e4 9197{
6270e593 9198 int i;
1da177e4
LT
9199
9200 ioa_cfg->host = host;
9201 ioa_cfg->pdev = pdev;
9202 ioa_cfg->log_level = ipr_log_level;
3d1d0da6 9203 ioa_cfg->doorbell = IPR_DOORBELL;
1da177e4
LT
9204 sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
9205 sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
1da177e4
LT
9206 sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
9207 sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
9208 sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
9209 sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
9210
1da177e4
LT
9211 INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
9212 INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
9213 INIT_LIST_HEAD(&ioa_cfg->free_res_q);
9214 INIT_LIST_HEAD(&ioa_cfg->used_res_q);
c4028958 9215 INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread);
1da177e4 9216 init_waitqueue_head(&ioa_cfg->reset_wait_q);
95fecd90 9217 init_waitqueue_head(&ioa_cfg->msi_wait_q);
6270e593 9218 init_waitqueue_head(&ioa_cfg->eeh_wait_q);
1da177e4
LT
9219 ioa_cfg->sdt_state = INACTIVE;
9220
9221 ipr_initialize_bus_attr(ioa_cfg);
3e7ebdfa 9222 ioa_cfg->max_devs_supported = ipr_max_devs;
1da177e4 9223
3e7ebdfa
WB
9224 if (ioa_cfg->sis64) {
9225 host->max_id = IPR_MAX_SIS64_TARGETS_PER_BUS;
9226 host->max_lun = IPR_MAX_SIS64_LUNS_PER_TARGET;
9227 if (ipr_max_devs > IPR_MAX_SIS64_DEVS)
9228 ioa_cfg->max_devs_supported = IPR_MAX_SIS64_DEVS;
6270e593
BK
9229 ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr64)
9230 + ((sizeof(struct ipr_config_table_entry64)
9231 * ioa_cfg->max_devs_supported)));
3e7ebdfa
WB
9232 } else {
9233 host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
9234 host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
9235 if (ipr_max_devs > IPR_MAX_PHYSICAL_DEVS)
9236 ioa_cfg->max_devs_supported = IPR_MAX_PHYSICAL_DEVS;
6270e593
BK
9237 ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr)
9238 + ((sizeof(struct ipr_config_table_entry)
9239 * ioa_cfg->max_devs_supported)));
3e7ebdfa 9240 }
6270e593 9241
1da177e4
LT
9242 host->max_channel = IPR_MAX_BUS_TO_SCAN;
9243 host->unique_id = host->host_no;
9244 host->max_cmd_len = IPR_MAX_CDB_LEN;
89aad428 9245 host->can_queue = ioa_cfg->max_cmds;
1da177e4
LT
9246 pci_set_drvdata(pdev, ioa_cfg);
9247
6270e593
BK
9248 for (i = 0; i < ARRAY_SIZE(ioa_cfg->hrrq); i++) {
9249 INIT_LIST_HEAD(&ioa_cfg->hrrq[i].hrrq_free_q);
9250 INIT_LIST_HEAD(&ioa_cfg->hrrq[i].hrrq_pending_q);
9251 spin_lock_init(&ioa_cfg->hrrq[i]._lock);
9252 if (i == 0)
9253 ioa_cfg->hrrq[i].lock = ioa_cfg->host->host_lock;
9254 else
9255 ioa_cfg->hrrq[i].lock = &ioa_cfg->hrrq[i]._lock;
dcbad00e 9256 }
1da177e4
LT
9257}
9258
9259/**
1be7bd82 9260 * ipr_get_chip_info - Find adapter chip information
1da177e4
LT
9261 * @dev_id: PCI device id struct
9262 *
9263 * Return value:
1be7bd82 9264 * ptr to chip information on success / NULL on failure
1da177e4 9265 **/
6f039790 9266static const struct ipr_chip_t *
1be7bd82 9267ipr_get_chip_info(const struct pci_device_id *dev_id)
1da177e4
LT
9268{
9269 int i;
9270
1da177e4
LT
9271 for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
9272 if (ipr_chip[i].vendor == dev_id->vendor &&
9273 ipr_chip[i].device == dev_id->device)
1be7bd82 9274 return &ipr_chip[i];
1da177e4
LT
9275 return NULL;
9276}
9277
6270e593
BK
9278/**
9279 * ipr_wait_for_pci_err_recovery - Wait for any PCI error recovery to complete
9280 * during probe time
9281 * @ioa_cfg: ioa config struct
9282 *
9283 * Return value:
9284 * None
9285 **/
9286static void ipr_wait_for_pci_err_recovery(struct ipr_ioa_cfg *ioa_cfg)
9287{
9288 struct pci_dev *pdev = ioa_cfg->pdev;
9289
9290 if (pci_channel_offline(pdev)) {
9291 wait_event_timeout(ioa_cfg->eeh_wait_q,
9292 !pci_channel_offline(pdev),
9293 IPR_PCI_ERROR_RECOVERY_TIMEOUT);
9294 pci_restore_state(pdev);
9295 }
9296}
9297
05a6538a 9298static int ipr_enable_msix(struct ipr_ioa_cfg *ioa_cfg)
9299{
9300 struct msix_entry entries[IPR_MAX_MSIX_VECTORS];
60e76b77 9301 int i, vectors;
05a6538a 9302
9303 for (i = 0; i < ARRAY_SIZE(entries); ++i)
9304 entries[i].entry = i;
9305
60e76b77
AG
9306 vectors = pci_enable_msix_range(ioa_cfg->pdev,
9307 entries, 1, ipr_number_of_msix);
9308 if (vectors < 0) {
6270e593 9309 ipr_wait_for_pci_err_recovery(ioa_cfg);
60e76b77 9310 return vectors;
05a6538a 9311 }
9312
60e76b77
AG
9313 for (i = 0; i < vectors; i++)
9314 ioa_cfg->vectors_info[i].vec = entries[i].vector;
9315 ioa_cfg->nvectors = vectors;
05a6538a 9316
60e76b77 9317 return 0;
05a6538a 9318}
9319
9320static int ipr_enable_msi(struct ipr_ioa_cfg *ioa_cfg)
9321{
60e76b77 9322 int i, vectors;
05a6538a 9323
60e76b77
AG
9324 vectors = pci_enable_msi_range(ioa_cfg->pdev, 1, ipr_number_of_msix);
9325 if (vectors < 0) {
6270e593 9326 ipr_wait_for_pci_err_recovery(ioa_cfg);
60e76b77 9327 return vectors;
05a6538a 9328 }
9329
60e76b77
AG
9330 for (i = 0; i < vectors; i++)
9331 ioa_cfg->vectors_info[i].vec = ioa_cfg->pdev->irq + i;
9332 ioa_cfg->nvectors = vectors;
05a6538a 9333
60e76b77 9334 return 0;
05a6538a 9335}
9336
9337static void name_msi_vectors(struct ipr_ioa_cfg *ioa_cfg)
9338{
9339 int vec_idx, n = sizeof(ioa_cfg->vectors_info[0].desc) - 1;
9340
9341 for (vec_idx = 0; vec_idx < ioa_cfg->nvectors; vec_idx++) {
9342 snprintf(ioa_cfg->vectors_info[vec_idx].desc, n,
9343 "host%d-%d", ioa_cfg->host->host_no, vec_idx);
9344 ioa_cfg->vectors_info[vec_idx].
9345 desc[strlen(ioa_cfg->vectors_info[vec_idx].desc)] = 0;
9346 }
9347}
9348
9349static int ipr_request_other_msi_irqs(struct ipr_ioa_cfg *ioa_cfg)
9350{
9351 int i, rc;
9352
9353 for (i = 1; i < ioa_cfg->nvectors; i++) {
9354 rc = request_irq(ioa_cfg->vectors_info[i].vec,
9355 ipr_isr_mhrrq,
9356 0,
9357 ioa_cfg->vectors_info[i].desc,
9358 &ioa_cfg->hrrq[i]);
9359 if (rc) {
9360 while (--i >= 0)
9361 free_irq(ioa_cfg->vectors_info[i].vec,
9362 &ioa_cfg->hrrq[i]);
9363 return rc;
9364 }
9365 }
9366 return 0;
9367}
9368
95fecd90
WB
9369/**
9370 * ipr_test_intr - Handle the interrupt generated in ipr_test_msi().
9371 * @pdev: PCI device struct
9372 *
9373 * Description: Simply set the msi_received flag to 1 indicating that
9374 * Message Signaled Interrupts are supported.
9375 *
9376 * Return value:
9377 * 0 on success / non-zero on failure
9378 **/
6f039790 9379static irqreturn_t ipr_test_intr(int irq, void *devp)
95fecd90
WB
9380{
9381 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
9382 unsigned long lock_flags = 0;
9383 irqreturn_t rc = IRQ_HANDLED;
9384
05a6538a 9385 dev_info(&ioa_cfg->pdev->dev, "Received IRQ : %d\n", irq);
95fecd90
WB
9386 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9387
9388 ioa_cfg->msi_received = 1;
9389 wake_up(&ioa_cfg->msi_wait_q);
9390
9391 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9392 return rc;
9393}
9394
9395/**
9396 * ipr_test_msi - Test for Message Signaled Interrupt (MSI) support.
9397 * @pdev: PCI device struct
9398 *
60e76b77 9399 * Description: The return value from pci_enable_msi_range() can not always be
95fecd90
WB
9400 * trusted. This routine sets up and initiates a test interrupt to determine
9401 * if the interrupt is received via the ipr_test_intr() service routine.
9402 * If the tests fails, the driver will fall back to LSI.
9403 *
9404 * Return value:
9405 * 0 on success / non-zero on failure
9406 **/
6f039790 9407static int ipr_test_msi(struct ipr_ioa_cfg *ioa_cfg, struct pci_dev *pdev)
95fecd90
WB
9408{
9409 int rc;
9410 volatile u32 int_reg;
9411 unsigned long lock_flags = 0;
9412
9413 ENTER;
9414
9415 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9416 init_waitqueue_head(&ioa_cfg->msi_wait_q);
9417 ioa_cfg->msi_received = 0;
9418 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
214777ba 9419 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.clr_interrupt_mask_reg32);
95fecd90
WB
9420 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
9421 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9422
f19799f4 9423 if (ioa_cfg->intr_flag == IPR_USE_MSIX)
9424 rc = request_irq(ioa_cfg->vectors_info[0].vec, ipr_test_intr, 0, IPR_NAME, ioa_cfg);
9425 else
9426 rc = request_irq(pdev->irq, ipr_test_intr, 0, IPR_NAME, ioa_cfg);
95fecd90
WB
9427 if (rc) {
9428 dev_err(&pdev->dev, "Can not assign irq %d\n", pdev->irq);
9429 return rc;
9430 } else if (ipr_debug)
9431 dev_info(&pdev->dev, "IRQ assigned: %d\n", pdev->irq);
9432
214777ba 9433 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.sense_interrupt_reg32);
95fecd90
WB
9434 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
9435 wait_event_timeout(ioa_cfg->msi_wait_q, ioa_cfg->msi_received, HZ);
56d6aa33 9436 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
95fecd90
WB
9437 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
9438
95fecd90
WB
9439 if (!ioa_cfg->msi_received) {
9440 /* MSI test failed */
9441 dev_info(&pdev->dev, "MSI test failed. Falling back to LSI.\n");
9442 rc = -EOPNOTSUPP;
9443 } else if (ipr_debug)
9444 dev_info(&pdev->dev, "MSI test succeeded.\n");
9445
9446 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9447
f19799f4 9448 if (ioa_cfg->intr_flag == IPR_USE_MSIX)
9449 free_irq(ioa_cfg->vectors_info[0].vec, ioa_cfg);
9450 else
9451 free_irq(pdev->irq, ioa_cfg);
95fecd90
WB
9452
9453 LEAVE;
9454
9455 return rc;
9456}
9457
05a6538a 9458 /* ipr_probe_ioa - Allocates memory and does first stage of initialization
1da177e4
LT
9459 * @pdev: PCI device struct
9460 * @dev_id: PCI device id struct
9461 *
9462 * Return value:
9463 * 0 on success / non-zero on failure
9464 **/
6f039790
GKH
9465static int ipr_probe_ioa(struct pci_dev *pdev,
9466 const struct pci_device_id *dev_id)
1da177e4
LT
9467{
9468 struct ipr_ioa_cfg *ioa_cfg;
9469 struct Scsi_Host *host;
9470 unsigned long ipr_regs_pci;
9471 void __iomem *ipr_regs;
a2a65a3e 9472 int rc = PCIBIOS_SUCCESSFUL;
473b1e8e 9473 volatile u32 mask, uproc, interrupts;
feccada9 9474 unsigned long lock_flags, driver_lock_flags;
1da177e4
LT
9475
9476 ENTER;
9477
1da177e4 9478 dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
1da177e4
LT
9479 host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
9480
9481 if (!host) {
9482 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
9483 rc = -ENOMEM;
6270e593 9484 goto out;
1da177e4
LT
9485 }
9486
9487 ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
9488 memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
8d8e7d13 9489 ata_host_init(&ioa_cfg->ata_host, &pdev->dev, &ipr_sata_ops);
1da177e4 9490
1be7bd82 9491 ioa_cfg->ipr_chip = ipr_get_chip_info(dev_id);
1da177e4 9492
1be7bd82 9493 if (!ioa_cfg->ipr_chip) {
1da177e4
LT
9494 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
9495 dev_id->vendor, dev_id->device);
9496 goto out_scsi_host_put;
9497 }
9498
a32c055f
WB
9499 /* set SIS 32 or SIS 64 */
9500 ioa_cfg->sis64 = ioa_cfg->ipr_chip->sis_type == IPR_SIS64 ? 1 : 0;
1be7bd82 9501 ioa_cfg->chip_cfg = ioa_cfg->ipr_chip->cfg;
7dd21308 9502 ioa_cfg->clear_isr = ioa_cfg->chip_cfg->clear_isr;
89aad428 9503 ioa_cfg->max_cmds = ioa_cfg->chip_cfg->max_cmds;
1be7bd82 9504
5469cb5b
BK
9505 if (ipr_transop_timeout)
9506 ioa_cfg->transop_timeout = ipr_transop_timeout;
9507 else if (dev_id->driver_data & IPR_USE_LONG_TRANSOP_TIMEOUT)
9508 ioa_cfg->transop_timeout = IPR_LONG_OPERATIONAL_TIMEOUT;
9509 else
9510 ioa_cfg->transop_timeout = IPR_OPERATIONAL_TIMEOUT;
9511
44c10138 9512 ioa_cfg->revid = pdev->revision;
463fc696 9513
6270e593
BK
9514 ipr_init_ioa_cfg(ioa_cfg, host, pdev);
9515
1da177e4
LT
9516 ipr_regs_pci = pci_resource_start(pdev, 0);
9517
9518 rc = pci_request_regions(pdev, IPR_NAME);
9519 if (rc < 0) {
9520 dev_err(&pdev->dev,
9521 "Couldn't register memory range of registers\n");
9522 goto out_scsi_host_put;
9523 }
9524
6270e593
BK
9525 rc = pci_enable_device(pdev);
9526
9527 if (rc || pci_channel_offline(pdev)) {
9528 if (pci_channel_offline(pdev)) {
9529 ipr_wait_for_pci_err_recovery(ioa_cfg);
9530 rc = pci_enable_device(pdev);
9531 }
9532
9533 if (rc) {
9534 dev_err(&pdev->dev, "Cannot enable adapter\n");
9535 ipr_wait_for_pci_err_recovery(ioa_cfg);
9536 goto out_release_regions;
9537 }
9538 }
9539
25729a7f 9540 ipr_regs = pci_ioremap_bar(pdev, 0);
1da177e4
LT
9541
9542 if (!ipr_regs) {
9543 dev_err(&pdev->dev,
9544 "Couldn't map memory range of registers\n");
9545 rc = -ENOMEM;
6270e593 9546 goto out_disable;
1da177e4
LT
9547 }
9548
9549 ioa_cfg->hdw_dma_regs = ipr_regs;
9550 ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
9551 ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
9552
6270e593 9553 ipr_init_regs(ioa_cfg);
1da177e4 9554
a32c055f 9555 if (ioa_cfg->sis64) {
869404cb 9556 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
a32c055f 9557 if (rc < 0) {
869404cb
AB
9558 dev_dbg(&pdev->dev, "Failed to set 64 bit DMA mask\n");
9559 rc = dma_set_mask_and_coherent(&pdev->dev,
9560 DMA_BIT_MASK(32));
a32c055f 9561 }
a32c055f 9562 } else
869404cb 9563 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
a32c055f 9564
1da177e4 9565 if (rc < 0) {
869404cb 9566 dev_err(&pdev->dev, "Failed to set DMA mask\n");
1da177e4
LT
9567 goto cleanup_nomem;
9568 }
9569
9570 rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
9571 ioa_cfg->chip_cfg->cache_line_size);
9572
9573 if (rc != PCIBIOS_SUCCESSFUL) {
9574 dev_err(&pdev->dev, "Write of cache line size failed\n");
6270e593 9575 ipr_wait_for_pci_err_recovery(ioa_cfg);
1da177e4
LT
9576 rc = -EIO;
9577 goto cleanup_nomem;
9578 }
9579
6270e593
BK
9580 /* Issue MMIO read to ensure card is not in EEH */
9581 interrupts = readl(ioa_cfg->regs.sense_interrupt_reg);
9582 ipr_wait_for_pci_err_recovery(ioa_cfg);
9583
05a6538a 9584 if (ipr_number_of_msix > IPR_MAX_MSIX_VECTORS) {
9585 dev_err(&pdev->dev, "The max number of MSIX is %d\n",
9586 IPR_MAX_MSIX_VECTORS);
9587 ipr_number_of_msix = IPR_MAX_MSIX_VECTORS;
9588 }
9589
9590 if (ioa_cfg->ipr_chip->intr_type == IPR_USE_MSI &&
56d6aa33 9591 ipr_enable_msix(ioa_cfg) == 0)
05a6538a 9592 ioa_cfg->intr_flag = IPR_USE_MSIX;
9593 else if (ioa_cfg->ipr_chip->intr_type == IPR_USE_MSI &&
56d6aa33 9594 ipr_enable_msi(ioa_cfg) == 0)
05a6538a 9595 ioa_cfg->intr_flag = IPR_USE_MSI;
9596 else {
9597 ioa_cfg->intr_flag = IPR_USE_LSI;
9598 ioa_cfg->nvectors = 1;
9599 dev_info(&pdev->dev, "Cannot enable MSI.\n");
9600 }
9601
6270e593
BK
9602 pci_set_master(pdev);
9603
9604 if (pci_channel_offline(pdev)) {
9605 ipr_wait_for_pci_err_recovery(ioa_cfg);
9606 pci_set_master(pdev);
9607 if (pci_channel_offline(pdev)) {
9608 rc = -EIO;
9609 goto out_msi_disable;
9610 }
9611 }
9612
05a6538a 9613 if (ioa_cfg->intr_flag == IPR_USE_MSI ||
9614 ioa_cfg->intr_flag == IPR_USE_MSIX) {
95fecd90 9615 rc = ipr_test_msi(ioa_cfg, pdev);
05a6538a 9616 if (rc == -EOPNOTSUPP) {
6270e593 9617 ipr_wait_for_pci_err_recovery(ioa_cfg);
05a6538a 9618 if (ioa_cfg->intr_flag == IPR_USE_MSI) {
9619 ioa_cfg->intr_flag &= ~IPR_USE_MSI;
9620 pci_disable_msi(pdev);
9621 } else if (ioa_cfg->intr_flag == IPR_USE_MSIX) {
9622 ioa_cfg->intr_flag &= ~IPR_USE_MSIX;
9623 pci_disable_msix(pdev);
9624 }
9625
9626 ioa_cfg->intr_flag = IPR_USE_LSI;
9627 ioa_cfg->nvectors = 1;
9628 }
95fecd90
WB
9629 else if (rc)
9630 goto out_msi_disable;
05a6538a 9631 else {
9632 if (ioa_cfg->intr_flag == IPR_USE_MSI)
9633 dev_info(&pdev->dev,
9634 "Request for %d MSIs succeeded with starting IRQ: %d\n",
9635 ioa_cfg->nvectors, pdev->irq);
9636 else if (ioa_cfg->intr_flag == IPR_USE_MSIX)
9637 dev_info(&pdev->dev,
9638 "Request for %d MSIXs succeeded.",
9639 ioa_cfg->nvectors);
9640 }
9641 }
9642
9643 ioa_cfg->hrrq_num = min3(ioa_cfg->nvectors,
9644 (unsigned int)num_online_cpus(),
9645 (unsigned int)IPR_MAX_HRRQ_NUM);
95fecd90 9646
1da177e4 9647 if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
f170c684 9648 goto out_msi_disable;
1da177e4
LT
9649
9650 if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
f170c684 9651 goto out_msi_disable;
1da177e4
LT
9652
9653 rc = ipr_alloc_mem(ioa_cfg);
9654 if (rc < 0) {
9655 dev_err(&pdev->dev,
9656 "Couldn't allocate enough memory for device driver!\n");
f170c684 9657 goto out_msi_disable;
1da177e4
LT
9658 }
9659
6270e593
BK
9660 /* Save away PCI config space for use following IOA reset */
9661 rc = pci_save_state(pdev);
9662
9663 if (rc != PCIBIOS_SUCCESSFUL) {
9664 dev_err(&pdev->dev, "Failed to save PCI config space\n");
9665 rc = -EIO;
9666 goto cleanup_nolog;
9667 }
9668
ce155cce
BK
9669 /*
9670 * If HRRQ updated interrupt is not masked, or reset alert is set,
9671 * the card is in an unknown state and needs a hard reset
9672 */
214777ba
WB
9673 mask = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
9674 interrupts = readl(ioa_cfg->regs.sense_interrupt_reg32);
9675 uproc = readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
ce155cce
BK
9676 if ((mask & IPR_PCII_HRRQ_UPDATED) == 0 || (uproc & IPR_UPROCI_RESET_ALERT))
9677 ioa_cfg->needs_hard_reset = 1;
5d7c20b7 9678 if ((interrupts & IPR_PCII_ERROR_INTERRUPTS) || reset_devices)
473b1e8e
BK
9679 ioa_cfg->needs_hard_reset = 1;
9680 if (interrupts & IPR_PCII_IOA_UNIT_CHECKED)
9681 ioa_cfg->ioa_unit_checked = 1;
ce155cce 9682
56d6aa33 9683 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1da177e4 9684 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
56d6aa33 9685 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1da177e4 9686
05a6538a 9687 if (ioa_cfg->intr_flag == IPR_USE_MSI
9688 || ioa_cfg->intr_flag == IPR_USE_MSIX) {
9689 name_msi_vectors(ioa_cfg);
9690 rc = request_irq(ioa_cfg->vectors_info[0].vec, ipr_isr,
9691 0,
9692 ioa_cfg->vectors_info[0].desc,
9693 &ioa_cfg->hrrq[0]);
9694 if (!rc)
9695 rc = ipr_request_other_msi_irqs(ioa_cfg);
9696 } else {
9697 rc = request_irq(pdev->irq, ipr_isr,
9698 IRQF_SHARED,
9699 IPR_NAME, &ioa_cfg->hrrq[0]);
9700 }
1da177e4
LT
9701 if (rc) {
9702 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
9703 pdev->irq, rc);
9704 goto cleanup_nolog;
9705 }
9706
463fc696
BK
9707 if ((dev_id->driver_data & IPR_USE_PCI_WARM_RESET) ||
9708 (dev_id->device == PCI_DEVICE_ID_IBM_OBSIDIAN_E && !ioa_cfg->revid)) {
9709 ioa_cfg->needs_warm_reset = 1;
9710 ioa_cfg->reset = ipr_reset_slot_reset;
9711 } else
9712 ioa_cfg->reset = ipr_reset_start_bist;
9713
feccada9 9714 spin_lock_irqsave(&ipr_driver_lock, driver_lock_flags);
1da177e4 9715 list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
feccada9 9716 spin_unlock_irqrestore(&ipr_driver_lock, driver_lock_flags);
1da177e4
LT
9717
9718 LEAVE;
9719out:
9720 return rc;
9721
9722cleanup_nolog:
9723 ipr_free_mem(ioa_cfg);
95fecd90 9724out_msi_disable:
6270e593 9725 ipr_wait_for_pci_err_recovery(ioa_cfg);
05a6538a 9726 if (ioa_cfg->intr_flag == IPR_USE_MSI)
9727 pci_disable_msi(pdev);
9728 else if (ioa_cfg->intr_flag == IPR_USE_MSIX)
9729 pci_disable_msix(pdev);
f170c684
JL
9730cleanup_nomem:
9731 iounmap(ipr_regs);
6270e593
BK
9732out_disable:
9733 pci_disable_device(pdev);
1da177e4
LT
9734out_release_regions:
9735 pci_release_regions(pdev);
9736out_scsi_host_put:
9737 scsi_host_put(host);
1da177e4
LT
9738 goto out;
9739}
9740
9741/**
9742 * ipr_scan_vsets - Scans for VSET devices
9743 * @ioa_cfg: ioa config struct
9744 *
9745 * Description: Since the VSET resources do not follow SAM in that we can have
9746 * sparse LUNs with no LUN 0, we have to scan for these ourselves.
9747 *
9748 * Return value:
9749 * none
9750 **/
9751static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
9752{
9753 int target, lun;
9754
9755 for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
203fa3fe 9756 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++)
1da177e4
LT
9757 scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
9758}
9759
9760/**
9761 * ipr_initiate_ioa_bringdown - Bring down an adapter
9762 * @ioa_cfg: ioa config struct
9763 * @shutdown_type: shutdown type
9764 *
9765 * Description: This function will initiate bringing down the adapter.
9766 * This consists of issuing an IOA shutdown to the adapter
9767 * to flush the cache, and running BIST.
9768 * If the caller needs to wait on the completion of the reset,
9769 * the caller must sleep on the reset_wait_q.
9770 *
9771 * Return value:
9772 * none
9773 **/
9774static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
9775 enum ipr_shutdown_type shutdown_type)
9776{
9777 ENTER;
9778 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
9779 ioa_cfg->sdt_state = ABORT_DUMP;
9780 ioa_cfg->reset_retries = 0;
9781 ioa_cfg->in_ioa_bringdown = 1;
9782 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
9783 LEAVE;
9784}
9785
9786/**
9787 * __ipr_remove - Remove a single adapter
9788 * @pdev: pci device struct
9789 *
9790 * Adapter hot plug remove entry point.
9791 *
9792 * Return value:
9793 * none
9794 **/
9795static void __ipr_remove(struct pci_dev *pdev)
9796{
9797 unsigned long host_lock_flags = 0;
9798 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
bfae7820 9799 int i;
feccada9 9800 unsigned long driver_lock_flags;
1da177e4
LT
9801 ENTER;
9802
9803 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
203fa3fe 9804 while (ioa_cfg->in_reset_reload) {
970ea294
BK
9805 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
9806 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
9807 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
9808 }
9809
bfae7820
BK
9810 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
9811 spin_lock(&ioa_cfg->hrrq[i]._lock);
9812 ioa_cfg->hrrq[i].removing_ioa = 1;
9813 spin_unlock(&ioa_cfg->hrrq[i]._lock);
9814 }
9815 wmb();
1da177e4
LT
9816 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
9817
9818 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
9819 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
43829731 9820 flush_work(&ioa_cfg->work_q);
9077a944 9821 INIT_LIST_HEAD(&ioa_cfg->used_res_q);
1da177e4
LT
9822 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
9823
feccada9 9824 spin_lock_irqsave(&ipr_driver_lock, driver_lock_flags);
1da177e4 9825 list_del(&ioa_cfg->queue);
feccada9 9826 spin_unlock_irqrestore(&ipr_driver_lock, driver_lock_flags);
1da177e4
LT
9827
9828 if (ioa_cfg->sdt_state == ABORT_DUMP)
9829 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
9830 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
9831
9832 ipr_free_all_resources(ioa_cfg);
9833
9834 LEAVE;
9835}
9836
9837/**
9838 * ipr_remove - IOA hot plug remove entry point
9839 * @pdev: pci device struct
9840 *
9841 * Adapter hot plug remove entry point.
9842 *
9843 * Return value:
9844 * none
9845 **/
6f039790 9846static void ipr_remove(struct pci_dev *pdev)
1da177e4
LT
9847{
9848 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
9849
9850 ENTER;
9851
ee959b00 9852 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
1da177e4 9853 &ipr_trace_attr);
ee959b00 9854 ipr_remove_dump_file(&ioa_cfg->host->shost_dev.kobj,
1da177e4
LT
9855 &ipr_dump_attr);
9856 scsi_remove_host(ioa_cfg->host);
9857
9858 __ipr_remove(pdev);
9859
9860 LEAVE;
9861}
9862
9863/**
9864 * ipr_probe - Adapter hot plug add entry point
9865 *
9866 * Return value:
9867 * 0 on success / non-zero on failure
9868 **/
6f039790 9869static int ipr_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
1da177e4
LT
9870{
9871 struct ipr_ioa_cfg *ioa_cfg;
b53d124a 9872 int rc, i;
1da177e4
LT
9873
9874 rc = ipr_probe_ioa(pdev, dev_id);
9875
9876 if (rc)
9877 return rc;
9878
9879 ioa_cfg = pci_get_drvdata(pdev);
9880 rc = ipr_probe_ioa_part2(ioa_cfg);
9881
9882 if (rc) {
9883 __ipr_remove(pdev);
9884 return rc;
9885 }
9886
9887 rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
9888
9889 if (rc) {
9890 __ipr_remove(pdev);
9891 return rc;
9892 }
9893
ee959b00 9894 rc = ipr_create_trace_file(&ioa_cfg->host->shost_dev.kobj,
1da177e4
LT
9895 &ipr_trace_attr);
9896
9897 if (rc) {
9898 scsi_remove_host(ioa_cfg->host);
9899 __ipr_remove(pdev);
9900 return rc;
9901 }
9902
ee959b00 9903 rc = ipr_create_dump_file(&ioa_cfg->host->shost_dev.kobj,
1da177e4
LT
9904 &ipr_dump_attr);
9905
9906 if (rc) {
ee959b00 9907 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
1da177e4
LT
9908 &ipr_trace_attr);
9909 scsi_remove_host(ioa_cfg->host);
9910 __ipr_remove(pdev);
9911 return rc;
9912 }
9913
9914 scsi_scan_host(ioa_cfg->host);
9915 ipr_scan_vsets(ioa_cfg);
9916 scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
9917 ioa_cfg->allow_ml_add_del = 1;
11cd8f12 9918 ioa_cfg->host->max_channel = IPR_VSET_BUS;
b53d124a 9919 ioa_cfg->iopoll_weight = ioa_cfg->chip_cfg->iopoll_weight;
9920
89f8b33c 9921 if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
b53d124a 9922 for (i = 1; i < ioa_cfg->hrrq_num; i++) {
9923 blk_iopoll_init(&ioa_cfg->hrrq[i].iopoll,
9924 ioa_cfg->iopoll_weight, ipr_iopoll);
9925 blk_iopoll_enable(&ioa_cfg->hrrq[i].iopoll);
9926 }
9927 }
9928
1da177e4
LT
9929 schedule_work(&ioa_cfg->work_q);
9930 return 0;
9931}
9932
9933/**
9934 * ipr_shutdown - Shutdown handler.
d18c3db5 9935 * @pdev: pci device struct
1da177e4
LT
9936 *
9937 * This function is invoked upon system shutdown/reboot. It will issue
9938 * an adapter shutdown to the adapter to flush the write cache.
9939 *
9940 * Return value:
9941 * none
9942 **/
d18c3db5 9943static void ipr_shutdown(struct pci_dev *pdev)
1da177e4 9944{
d18c3db5 9945 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
1da177e4 9946 unsigned long lock_flags = 0;
b53d124a 9947 int i;
1da177e4
LT
9948
9949 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
89f8b33c 9950 if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
b53d124a 9951 ioa_cfg->iopoll_weight = 0;
9952 for (i = 1; i < ioa_cfg->hrrq_num; i++)
9953 blk_iopoll_disable(&ioa_cfg->hrrq[i].iopoll);
9954 }
9955
203fa3fe 9956 while (ioa_cfg->in_reset_reload) {
970ea294
BK
9957 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9958 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
9959 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9960 }
9961
1da177e4
LT
9962 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
9963 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9964 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
9965}
9966
6f039790 9967static struct pci_device_id ipr_pci_table[] = {
1da177e4 9968 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6d84c944 9969 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702, 0, 0, 0 },
1da177e4 9970 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6d84c944 9971 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703, 0, 0, 0 },
1da177e4 9972 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6d84c944 9973 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D, 0, 0, 0 },
1da177e4 9974 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6d84c944 9975 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E, 0, 0, 0 },
1da177e4 9976 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6d84c944 9977 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B, 0, 0, 0 },
1da177e4 9978 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6d84c944 9979 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E, 0, 0, 0 },
1da177e4 9980 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6d84c944 9981 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A, 0, 0, 0 },
86f51436 9982 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
5469cb5b
BK
9983 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575B, 0, 0,
9984 IPR_USE_LONG_TRANSOP_TIMEOUT },
86f51436 9985 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
6d84c944 9986 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
86f51436 9987 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
22d2e402
BK
9988 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
9989 IPR_USE_LONG_TRANSOP_TIMEOUT },
60e7486b 9990 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
5469cb5b
BK
9991 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
9992 IPR_USE_LONG_TRANSOP_TIMEOUT },
86f51436 9993 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
6d84c944 9994 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
86f51436 9995 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
22d2e402
BK
9996 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
9997 IPR_USE_LONG_TRANSOP_TIMEOUT},
60e7486b 9998 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
5469cb5b
BK
9999 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
10000 IPR_USE_LONG_TRANSOP_TIMEOUT },
185eb31c 10001 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
22d2e402
BK
10002 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574E, 0, 0,
10003 IPR_USE_LONG_TRANSOP_TIMEOUT },
185eb31c
BK
10004 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
10005 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B3, 0, 0, 0 },
b0f56d3d
WB
10006 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
10007 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57CC, 0, 0, 0 },
60e7486b 10008 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
5469cb5b 10009 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B7, 0, 0,
463fc696 10010 IPR_USE_LONG_TRANSOP_TIMEOUT | IPR_USE_PCI_WARM_RESET },
1da177e4 10011 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
6d84c944 10012 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780, 0, 0, 0 },
1da177e4 10013 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
6d84c944 10014 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E, 0, 0, 0 },
86f51436 10015 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
5469cb5b
BK
10016 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571F, 0, 0,
10017 IPR_USE_LONG_TRANSOP_TIMEOUT },
60e7486b 10018 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
5469cb5b
BK
10019 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572F, 0, 0,
10020 IPR_USE_LONG_TRANSOP_TIMEOUT },
d7b4627f
WB
10021 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
10022 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B5, 0, 0, 0 },
10023 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
10024 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574D, 0, 0, 0 },
10025 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
10026 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B2, 0, 0, 0 },
b8d5d568 10027 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
10028 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C0, 0, 0, 0 },
5a918353
WB
10029 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
10030 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C3, 0, 0, 0 },
32622bde
WB
10031 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
10032 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C4, 0, 0, 0 },
cd9b3d04 10033 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
d7b4627f 10034 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B4, 0, 0, 0 },
cd9b3d04 10035 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
d7b4627f 10036 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B1, 0, 0, 0 },
cd9b3d04 10037 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
d7b4627f 10038 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C6, 0, 0, 0 },
cd9b3d04
WB
10039 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10040 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C8, 0, 0, 0 },
10041 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
d7b4627f 10042 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57CE, 0, 0, 0 },
b8d5d568 10043 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10044 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D5, 0, 0, 0 },
10045 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10046 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D6, 0, 0, 0 },
10047 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10048 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D7, 0, 0, 0 },
10049 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10050 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D8, 0, 0, 0 },
43c5fdaf 10051 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10052 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D9, 0, 0, 0 },
10053 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
f94d9964
WX
10054 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57DA, 0, 0, 0 },
10055 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
43c5fdaf 10056 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EB, 0, 0, 0 },
10057 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10058 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EC, 0, 0, 0 },
10059 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10060 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57ED, 0, 0, 0 },
10061 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10062 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EE, 0, 0, 0 },
10063 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10064 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EF, 0, 0, 0 },
10065 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10066 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57F0, 0, 0, 0 },
5eeac3e9
WX
10067 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10068 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2CCA, 0, 0, 0 },
10069 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10070 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2CD2, 0, 0, 0 },
10071 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10072 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2CCD, 0, 0, 0 },
1da177e4
LT
10073 { }
10074};
10075MODULE_DEVICE_TABLE(pci, ipr_pci_table);
10076
a55b2d21 10077static const struct pci_error_handlers ipr_err_handler = {
f8a88b19 10078 .error_detected = ipr_pci_error_detected,
6270e593 10079 .mmio_enabled = ipr_pci_mmio_enabled,
f8a88b19
LV
10080 .slot_reset = ipr_pci_slot_reset,
10081};
10082
1da177e4
LT
10083static struct pci_driver ipr_driver = {
10084 .name = IPR_NAME,
10085 .id_table = ipr_pci_table,
10086 .probe = ipr_probe,
6f039790 10087 .remove = ipr_remove,
d18c3db5 10088 .shutdown = ipr_shutdown,
f8a88b19 10089 .err_handler = &ipr_err_handler,
1da177e4
LT
10090};
10091
f72919ec
WB
10092/**
10093 * ipr_halt_done - Shutdown prepare completion
10094 *
10095 * Return value:
10096 * none
10097 **/
10098static void ipr_halt_done(struct ipr_cmnd *ipr_cmd)
10099{
05a6538a 10100 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
f72919ec
WB
10101}
10102
10103/**
10104 * ipr_halt - Issue shutdown prepare to all adapters
10105 *
10106 * Return value:
10107 * NOTIFY_OK on success / NOTIFY_DONE on failure
10108 **/
10109static int ipr_halt(struct notifier_block *nb, ulong event, void *buf)
10110{
10111 struct ipr_cmnd *ipr_cmd;
10112 struct ipr_ioa_cfg *ioa_cfg;
feccada9 10113 unsigned long flags = 0, driver_lock_flags;
f72919ec
WB
10114
10115 if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
10116 return NOTIFY_DONE;
10117
feccada9 10118 spin_lock_irqsave(&ipr_driver_lock, driver_lock_flags);
f72919ec
WB
10119
10120 list_for_each_entry(ioa_cfg, &ipr_ioa_head, queue) {
10121 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
56d6aa33 10122 if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds) {
f72919ec
WB
10123 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
10124 continue;
10125 }
10126
10127 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
10128 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
10129 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
10130 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
10131 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;
10132
10133 ipr_do_req(ipr_cmd, ipr_halt_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
10134 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
10135 }
feccada9 10136 spin_unlock_irqrestore(&ipr_driver_lock, driver_lock_flags);
f72919ec
WB
10137
10138 return NOTIFY_OK;
10139}
10140
10141static struct notifier_block ipr_notifier = {
10142 ipr_halt, NULL, 0
10143};
10144
1da177e4
LT
10145/**
10146 * ipr_init - Module entry point
10147 *
10148 * Return value:
10149 * 0 on success / negative value on failure
10150 **/
10151static int __init ipr_init(void)
10152{
10153 ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
10154 IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
10155
f72919ec 10156 register_reboot_notifier(&ipr_notifier);
dcbccbde 10157 return pci_register_driver(&ipr_driver);
1da177e4
LT
10158}
10159
10160/**
10161 * ipr_exit - Module unload
10162 *
10163 * Module unload entry point.
10164 *
10165 * Return value:
10166 * none
10167 **/
10168static void __exit ipr_exit(void)
10169{
f72919ec 10170 unregister_reboot_notifier(&ipr_notifier);
1da177e4
LT
10171 pci_unregister_driver(&ipr_driver);
10172}
10173
10174module_init(ipr_init);
10175module_exit(ipr_exit);