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