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