GNU Linux-libre 6.8.9-gnu
[releases.git] / drivers / scsi / ipr.c
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 <asm/io.h>
65 #include <asm/irq.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                         del_timer(&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  * @ipr_cmd:    ipr command struct
1010  * @timeout_func:       function to invoke if command times out
1011  * @timeout:    timeout
1012  *
1013  * Return value:
1014  *      none
1015  **/
1016 static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
1017                                   void (*timeout_func) (struct timer_list *),
1018                                   u32 timeout)
1019 {
1020         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
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(ioa_cfg->host->host_lock);
1026         wait_for_completion(&ipr_cmd->completion);
1027         spin_lock_irq(ioa_cfg->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 = from_timer(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 = from_timer(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 = (__be32 *)__get_free_page(GFP_ATOMIC);
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                 free_page((unsigned long) 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                               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 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(sizeof(struct ipr_sglist), GFP_KERNEL);
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                                 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                                 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 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                              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(sizeof(struct ipr_dump), GFP_KERNEL);
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_size(IPR_FMT3_MAX_NUM_DUMP_PAGES,
4285                                               sizeof(__be32 *)));
4286         else
4287                 ioa_data = vmalloc(array_size(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                               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 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  * @block_device:       block device 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 block_device *block_device,
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_slave_destroy - Unconfigure a SCSI device
4749  * @sdev:       scsi device struct
4750  *
4751  * Return value:
4752  *      nothing
4753  **/
4754 static void ipr_slave_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_slave_configure - Configure a SCSI device
4773  * @sdev:       scsi device struct
4774  *
4775  * This function configures the specified scsi device.
4776  *
4777  * Return value:
4778  *      0 on success
4779  **/
4780 static int ipr_slave_configure(struct scsi_device *sdev)
4781 {
4782         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4783         struct ipr_resource_entry *res;
4784         unsigned long lock_flags = 0;
4785         char buffer[IPR_MAX_RES_PATH_LENGTH];
4786
4787         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4788         res = sdev->hostdata;
4789         if (res) {
4790                 if (ipr_is_af_dasd_device(res))
4791                         sdev->type = TYPE_RAID;
4792                 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
4793                         sdev->scsi_level = 4;
4794                         sdev->no_uld_attach = 1;
4795                 }
4796                 if (ipr_is_vset_device(res)) {
4797                         sdev->scsi_level = SCSI_SPC_3;
4798                         sdev->no_report_opcodes = 1;
4799                         blk_queue_rq_timeout(sdev->request_queue,
4800                                              IPR_VSET_RW_TIMEOUT);
4801                         blk_queue_max_hw_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
4802                 }
4803                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4804
4805                 if (ioa_cfg->sis64)
4806                         sdev_printk(KERN_INFO, sdev, "Resource path: %s\n",
4807                                     ipr_format_res_path(ioa_cfg,
4808                                 res->res_path, buffer, sizeof(buffer)));
4809                 return 0;
4810         }
4811         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4812         return 0;
4813 }
4814
4815 /**
4816  * ipr_slave_alloc - Prepare for commands to a device.
4817  * @sdev:       scsi device struct
4818  *
4819  * This function saves a pointer to the resource entry
4820  * in the scsi device struct if the device exists. We
4821  * can then use this pointer in ipr_queuecommand when
4822  * handling new commands.
4823  *
4824  * Return value:
4825  *      0 on success / -ENXIO if device does not exist
4826  **/
4827 static int ipr_slave_alloc(struct scsi_device *sdev)
4828 {
4829         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4830         struct ipr_resource_entry *res;
4831         unsigned long lock_flags;
4832         int rc = -ENXIO;
4833
4834         sdev->hostdata = NULL;
4835
4836         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4837
4838         res = ipr_find_sdev(sdev);
4839         if (res) {
4840                 res->sdev = sdev;
4841                 res->add_to_ml = 0;
4842                 res->in_erp = 0;
4843                 sdev->hostdata = res;
4844                 if (!ipr_is_naca_model(res))
4845                         res->needs_sync_complete = 1;
4846                 rc = 0;
4847                 if (ipr_is_gata(res)) {
4848                         sdev_printk(KERN_ERR, sdev, "SATA devices are no longer "
4849                                 "supported by this driver. Skipping device.\n");
4850                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4851                         return -ENXIO;
4852                 }
4853         }
4854
4855         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4856
4857         return rc;
4858 }
4859
4860 /**
4861  * ipr_match_lun - Match function for specified LUN
4862  * @ipr_cmd:    ipr command struct
4863  * @device:             device to match (sdev)
4864  *
4865  * Returns:
4866  *      1 if command matches sdev / 0 if command does not match sdev
4867  **/
4868 static int ipr_match_lun(struct ipr_cmnd *ipr_cmd, void *device)
4869 {
4870         if (ipr_cmd->scsi_cmd && ipr_cmd->scsi_cmd->device == device)
4871                 return 1;
4872         return 0;
4873 }
4874
4875 /**
4876  * ipr_cmnd_is_free - Check if a command is free or not
4877  * @ipr_cmd:    ipr command struct
4878  *
4879  * Returns:
4880  *      true / false
4881  **/
4882 static bool ipr_cmnd_is_free(struct ipr_cmnd *ipr_cmd)
4883 {
4884         struct ipr_cmnd *loop_cmd;
4885
4886         list_for_each_entry(loop_cmd, &ipr_cmd->hrrq->hrrq_free_q, queue) {
4887                 if (loop_cmd == ipr_cmd)
4888                         return true;
4889         }
4890
4891         return false;
4892 }
4893
4894 /**
4895  * ipr_wait_for_ops - Wait for matching commands to complete
4896  * @ioa_cfg:    ioa config struct
4897  * @device:             device to match (sdev)
4898  * @match:              match function to use
4899  *
4900  * Returns:
4901  *      SUCCESS / FAILED
4902  **/
4903 static int ipr_wait_for_ops(struct ipr_ioa_cfg *ioa_cfg, void *device,
4904                             int (*match)(struct ipr_cmnd *, void *))
4905 {
4906         struct ipr_cmnd *ipr_cmd;
4907         int wait, i;
4908         unsigned long flags;
4909         struct ipr_hrr_queue *hrrq;
4910         signed long timeout = IPR_ABORT_TASK_TIMEOUT;
4911         DECLARE_COMPLETION_ONSTACK(comp);
4912
4913         ENTER;
4914         do {
4915                 wait = 0;
4916
4917                 for_each_hrrq(hrrq, ioa_cfg) {
4918                         spin_lock_irqsave(hrrq->lock, flags);
4919                         for (i = hrrq->min_cmd_id; i <= hrrq->max_cmd_id; i++) {
4920                                 ipr_cmd = ioa_cfg->ipr_cmnd_list[i];
4921                                 if (!ipr_cmnd_is_free(ipr_cmd)) {
4922                                         if (match(ipr_cmd, device)) {
4923                                                 ipr_cmd->eh_comp = &comp;
4924                                                 wait++;
4925                                         }
4926                                 }
4927                         }
4928                         spin_unlock_irqrestore(hrrq->lock, flags);
4929                 }
4930
4931                 if (wait) {
4932                         timeout = wait_for_completion_timeout(&comp, timeout);
4933
4934                         if (!timeout) {
4935                                 wait = 0;
4936
4937                                 for_each_hrrq(hrrq, ioa_cfg) {
4938                                         spin_lock_irqsave(hrrq->lock, flags);
4939                                         for (i = hrrq->min_cmd_id; i <= hrrq->max_cmd_id; i++) {
4940                                                 ipr_cmd = ioa_cfg->ipr_cmnd_list[i];
4941                                                 if (!ipr_cmnd_is_free(ipr_cmd)) {
4942                                                         if (match(ipr_cmd, device)) {
4943                                                                 ipr_cmd->eh_comp = NULL;
4944                                                                 wait++;
4945                                                         }
4946                                                 }
4947                                         }
4948                                         spin_unlock_irqrestore(hrrq->lock, flags);
4949                                 }
4950
4951                                 if (wait)
4952                                         dev_err(&ioa_cfg->pdev->dev, "Timed out waiting for aborted commands\n");
4953                                 LEAVE;
4954                                 return wait ? FAILED : SUCCESS;
4955                         }
4956                 }
4957         } while (wait);
4958
4959         LEAVE;
4960         return SUCCESS;
4961 }
4962
4963 static int ipr_eh_host_reset(struct scsi_cmnd *cmd)
4964 {
4965         struct ipr_ioa_cfg *ioa_cfg;
4966         unsigned long lock_flags = 0;
4967         int rc = SUCCESS;
4968
4969         ENTER;
4970         ioa_cfg = (struct ipr_ioa_cfg *) cmd->device->host->hostdata;
4971         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4972
4973         if (!ioa_cfg->in_reset_reload && !ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
4974                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
4975                 dev_err(&ioa_cfg->pdev->dev,
4976                         "Adapter being reset as a result of error recovery.\n");
4977
4978                 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4979                         ioa_cfg->sdt_state = GET_DUMP;
4980         }
4981
4982         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4983         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4984         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4985
4986         /* If we got hit with a host reset while we were already resetting
4987          the adapter for some reason, and the reset failed. */
4988         if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
4989                 ipr_trace;
4990                 rc = FAILED;
4991         }
4992
4993         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4994         LEAVE;
4995         return rc;
4996 }
4997
4998 /**
4999  * ipr_device_reset - Reset the device
5000  * @ioa_cfg:    ioa config struct
5001  * @res:                resource entry struct
5002  *
5003  * This function issues a device reset to the affected device.
5004  * If the device is a SCSI device, a LUN reset will be sent
5005  * to the device first. If that does not work, a target reset
5006  * will be sent.
5007  *
5008  * Return value:
5009  *      0 on success / non-zero on failure
5010  **/
5011 static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
5012                             struct ipr_resource_entry *res)
5013 {
5014         struct ipr_cmnd *ipr_cmd;
5015         struct ipr_ioarcb *ioarcb;
5016         struct ipr_cmd_pkt *cmd_pkt;
5017         u32 ioasc;
5018
5019         ENTER;
5020         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5021         ioarcb = &ipr_cmd->ioarcb;
5022         cmd_pkt = &ioarcb->cmd_pkt;
5023
5024         if (ipr_cmd->ioa_cfg->sis64)
5025                 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
5026
5027         ioarcb->res_handle = res->res_handle;
5028         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5029         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
5030
5031         ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
5032         ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5033         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
5034
5035         LEAVE;
5036         return IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0;
5037 }
5038
5039 /**
5040  * __ipr_eh_dev_reset - Reset the device
5041  * @scsi_cmd:   scsi command struct
5042  *
5043  * This function issues a device reset to the affected device.
5044  * A LUN reset will be sent to the device first. If that does
5045  * not work, a target reset will be sent.
5046  *
5047  * Return value:
5048  *      SUCCESS / FAILED
5049  **/
5050 static int __ipr_eh_dev_reset(struct scsi_cmnd *scsi_cmd)
5051 {
5052         struct ipr_ioa_cfg *ioa_cfg;
5053         struct ipr_resource_entry *res;
5054         int rc = 0;
5055
5056         ENTER;
5057         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
5058         res = scsi_cmd->device->hostdata;
5059
5060         /*
5061          * If we are currently going through reset/reload, return failed. This will force the
5062          * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
5063          * reset to complete
5064          */
5065         if (ioa_cfg->in_reset_reload)
5066                 return FAILED;
5067         if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
5068                 return FAILED;
5069
5070         res->resetting_device = 1;
5071         scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
5072
5073         rc = ipr_device_reset(ioa_cfg, res);
5074         res->resetting_device = 0;
5075         res->reset_occurred = 1;
5076
5077         LEAVE;
5078         return rc ? FAILED : SUCCESS;
5079 }
5080
5081 static int ipr_eh_dev_reset(struct scsi_cmnd *cmd)
5082 {
5083         int rc;
5084         struct ipr_ioa_cfg *ioa_cfg;
5085         struct ipr_resource_entry *res;
5086
5087         ioa_cfg = (struct ipr_ioa_cfg *) cmd->device->host->hostdata;
5088         res = cmd->device->hostdata;
5089
5090         if (!res)
5091                 return FAILED;
5092
5093         spin_lock_irq(cmd->device->host->host_lock);
5094         rc = __ipr_eh_dev_reset(cmd);
5095         spin_unlock_irq(cmd->device->host->host_lock);
5096
5097         if (rc == SUCCESS)
5098                 rc = ipr_wait_for_ops(ioa_cfg, cmd->device, ipr_match_lun);
5099
5100         return rc;
5101 }
5102
5103 /**
5104  * ipr_bus_reset_done - Op done function for bus reset.
5105  * @ipr_cmd:    ipr command struct
5106  *
5107  * This function is the op done function for a bus reset
5108  *
5109  * Return value:
5110  *      none
5111  **/
5112 static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
5113 {
5114         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5115         struct ipr_resource_entry *res;
5116
5117         ENTER;
5118         if (!ioa_cfg->sis64)
5119                 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
5120                         if (res->res_handle == ipr_cmd->ioarcb.res_handle) {
5121                                 scsi_report_bus_reset(ioa_cfg->host, res->bus);
5122                                 break;
5123                         }
5124                 }
5125
5126         /*
5127          * If abort has not completed, indicate the reset has, else call the
5128          * abort's done function to wake the sleeping eh thread
5129          */
5130         if (ipr_cmd->sibling->sibling)
5131                 ipr_cmd->sibling->sibling = NULL;
5132         else
5133                 ipr_cmd->sibling->done(ipr_cmd->sibling);
5134
5135         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
5136         LEAVE;
5137 }
5138
5139 /**
5140  * ipr_abort_timeout - An abort task has timed out
5141  * @t: Timer context used to fetch ipr command struct
5142  *
5143  * This function handles when an abort task times out. If this
5144  * happens we issue a bus reset since we have resources tied
5145  * up that must be freed before returning to the midlayer.
5146  *
5147  * Return value:
5148  *      none
5149  **/
5150 static void ipr_abort_timeout(struct timer_list *t)
5151 {
5152         struct ipr_cmnd *ipr_cmd = from_timer(ipr_cmd, t, timer);
5153         struct ipr_cmnd *reset_cmd;
5154         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5155         struct ipr_cmd_pkt *cmd_pkt;
5156         unsigned long lock_flags = 0;
5157
5158         ENTER;
5159         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5160         if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
5161                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5162                 return;
5163         }
5164
5165         sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
5166         reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5167         ipr_cmd->sibling = reset_cmd;
5168         reset_cmd->sibling = ipr_cmd;
5169         reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
5170         cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
5171         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5172         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
5173         cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
5174
5175         ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
5176         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5177         LEAVE;
5178 }
5179
5180 /**
5181  * ipr_cancel_op - Cancel specified op
5182  * @scsi_cmd:   scsi command struct
5183  *
5184  * This function cancels specified op.
5185  *
5186  * Return value:
5187  *      SUCCESS / FAILED
5188  **/
5189 static int ipr_cancel_op(struct scsi_cmnd *scsi_cmd)
5190 {
5191         struct ipr_cmnd *ipr_cmd;
5192         struct ipr_ioa_cfg *ioa_cfg;
5193         struct ipr_resource_entry *res;
5194         struct ipr_cmd_pkt *cmd_pkt;
5195         u32 ioasc;
5196         int i, op_found = 0;
5197         struct ipr_hrr_queue *hrrq;
5198
5199         ENTER;
5200         ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
5201         res = scsi_cmd->device->hostdata;
5202
5203         /* If we are currently going through reset/reload, return failed.
5204          * This will force the mid-layer to call ipr_eh_host_reset,
5205          * which will then go to sleep and wait for the reset to complete
5206          */
5207         if (ioa_cfg->in_reset_reload ||
5208             ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
5209                 return FAILED;
5210         if (!res)
5211                 return FAILED;
5212
5213         /*
5214          * If we are aborting a timed out op, chances are that the timeout was caused
5215          * by a still not detected EEH error. In such cases, reading a register will
5216          * trigger the EEH recovery infrastructure.
5217          */
5218         readl(ioa_cfg->regs.sense_interrupt_reg);
5219
5220         if (!ipr_is_gscsi(res))
5221                 return FAILED;
5222
5223         for_each_hrrq(hrrq, ioa_cfg) {
5224                 spin_lock(&hrrq->_lock);
5225                 for (i = hrrq->min_cmd_id; i <= hrrq->max_cmd_id; i++) {
5226                         if (ioa_cfg->ipr_cmnd_list[i]->scsi_cmd == scsi_cmd) {
5227                                 if (!ipr_cmnd_is_free(ioa_cfg->ipr_cmnd_list[i])) {
5228                                         op_found = 1;
5229                                         break;
5230                                 }
5231                         }
5232                 }
5233                 spin_unlock(&hrrq->_lock);
5234         }
5235
5236         if (!op_found)
5237                 return SUCCESS;
5238
5239         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5240         ipr_cmd->ioarcb.res_handle = res->res_handle;
5241         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5242         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5243         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
5244         ipr_cmd->u.sdev = scsi_cmd->device;
5245
5246         scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
5247                     scsi_cmd->cmnd[0]);
5248         ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
5249         ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5250
5251         /*
5252          * If the abort task timed out and we sent a bus reset, we will get
5253          * one the following responses to the abort
5254          */
5255         if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
5256                 ioasc = 0;
5257                 ipr_trace;
5258         }
5259
5260         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
5261         if (!ipr_is_naca_model(res))
5262                 res->needs_sync_complete = 1;
5263
5264         LEAVE;
5265         return IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
5266 }
5267
5268 /**
5269  * ipr_scan_finished - Report whether scan is done
5270  * @shost:           scsi host struct
5271  * @elapsed_time:    elapsed time
5272  *
5273  * Return value:
5274  *      0 if scan in progress / 1 if scan is complete
5275  **/
5276 static int ipr_scan_finished(struct Scsi_Host *shost, unsigned long elapsed_time)
5277 {
5278         unsigned long lock_flags;
5279         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
5280         int rc = 0;
5281
5282         spin_lock_irqsave(shost->host_lock, lock_flags);
5283         if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead || ioa_cfg->scan_done)
5284                 rc = 1;
5285         if ((elapsed_time/HZ) > (ioa_cfg->transop_timeout * 2))
5286                 rc = 1;
5287         spin_unlock_irqrestore(shost->host_lock, lock_flags);
5288         return rc;
5289 }
5290
5291 /**
5292  * ipr_eh_abort - Reset the host adapter
5293  * @scsi_cmd:   scsi command struct
5294  *
5295  * Return value:
5296  *      SUCCESS / FAILED
5297  **/
5298 static int ipr_eh_abort(struct scsi_cmnd *scsi_cmd)
5299 {
5300         unsigned long flags;
5301         int rc;
5302         struct ipr_ioa_cfg *ioa_cfg;
5303
5304         ENTER;
5305
5306         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
5307
5308         spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
5309         rc = ipr_cancel_op(scsi_cmd);
5310         spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
5311
5312         if (rc == SUCCESS)
5313                 rc = ipr_wait_for_ops(ioa_cfg, scsi_cmd->device, ipr_match_lun);
5314         LEAVE;
5315         return rc;
5316 }
5317
5318 /**
5319  * ipr_handle_other_interrupt - Handle "other" interrupts
5320  * @ioa_cfg:    ioa config struct
5321  * @int_reg:    interrupt register
5322  *
5323  * Return value:
5324  *      IRQ_NONE / IRQ_HANDLED
5325  **/
5326 static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
5327                                               u32 int_reg)
5328 {
5329         irqreturn_t rc = IRQ_HANDLED;
5330         u32 int_mask_reg;
5331
5332         int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
5333         int_reg &= ~int_mask_reg;
5334
5335         /* If an interrupt on the adapter did not occur, ignore it.
5336          * Or in the case of SIS 64, check for a stage change interrupt.
5337          */
5338         if ((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0) {
5339                 if (ioa_cfg->sis64) {
5340                         int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
5341                         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
5342                         if (int_reg & IPR_PCII_IPL_STAGE_CHANGE) {
5343
5344                                 /* clear stage change */
5345                                 writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.clr_interrupt_reg);
5346                                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
5347                                 list_del(&ioa_cfg->reset_cmd->queue);
5348                                 del_timer(&ioa_cfg->reset_cmd->timer);
5349                                 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
5350                                 return IRQ_HANDLED;
5351                         }
5352                 }
5353
5354                 return IRQ_NONE;
5355         }
5356
5357         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
5358                 /* Mask the interrupt */
5359                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
5360                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5361
5362                 list_del(&ioa_cfg->reset_cmd->queue);
5363                 del_timer(&ioa_cfg->reset_cmd->timer);
5364                 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
5365         } else if ((int_reg & IPR_PCII_HRRQ_UPDATED) == int_reg) {
5366                 if (ioa_cfg->clear_isr) {
5367                         if (ipr_debug && printk_ratelimit())
5368                                 dev_err(&ioa_cfg->pdev->dev,
5369                                         "Spurious interrupt detected. 0x%08X\n", int_reg);
5370                         writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg32);
5371                         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
5372                         return IRQ_NONE;
5373                 }
5374         } else {
5375                 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
5376                         ioa_cfg->ioa_unit_checked = 1;
5377                 else if (int_reg & IPR_PCII_NO_HOST_RRQ)
5378                         dev_err(&ioa_cfg->pdev->dev,
5379                                 "No Host RRQ. 0x%08X\n", int_reg);
5380                 else
5381                         dev_err(&ioa_cfg->pdev->dev,
5382                                 "Permanent IOA failure. 0x%08X\n", int_reg);
5383
5384                 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
5385                         ioa_cfg->sdt_state = GET_DUMP;
5386
5387                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
5388                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5389         }
5390
5391         return rc;
5392 }
5393
5394 /**
5395  * ipr_isr_eh - Interrupt service routine error handler
5396  * @ioa_cfg:    ioa config struct
5397  * @msg:        message to log
5398  * @number:     various meanings depending on the caller/message
5399  *
5400  * Return value:
5401  *      none
5402  **/
5403 static void ipr_isr_eh(struct ipr_ioa_cfg *ioa_cfg, char *msg, u16 number)
5404 {
5405         ioa_cfg->errors_logged++;
5406         dev_err(&ioa_cfg->pdev->dev, "%s %d\n", msg, number);
5407
5408         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
5409                 ioa_cfg->sdt_state = GET_DUMP;
5410
5411         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5412 }
5413
5414 static int ipr_process_hrrq(struct ipr_hrr_queue *hrr_queue, int budget,
5415                                                 struct list_head *doneq)
5416 {
5417         u32 ioasc;
5418         u16 cmd_index;
5419         struct ipr_cmnd *ipr_cmd;
5420         struct ipr_ioa_cfg *ioa_cfg = hrr_queue->ioa_cfg;
5421         int num_hrrq = 0;
5422
5423         /* If interrupts are disabled, ignore the interrupt */
5424         if (!hrr_queue->allow_interrupts)
5425                 return 0;
5426
5427         while ((be32_to_cpu(*hrr_queue->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
5428                hrr_queue->toggle_bit) {
5429
5430                 cmd_index = (be32_to_cpu(*hrr_queue->hrrq_curr) &
5431                              IPR_HRRQ_REQ_RESP_HANDLE_MASK) >>
5432                              IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
5433
5434                 if (unlikely(cmd_index > hrr_queue->max_cmd_id ||
5435                              cmd_index < hrr_queue->min_cmd_id)) {
5436                         ipr_isr_eh(ioa_cfg,
5437                                 "Invalid response handle from IOA: ",
5438                                 cmd_index);
5439                         break;
5440                 }
5441
5442                 ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
5443                 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5444
5445                 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
5446
5447                 list_move_tail(&ipr_cmd->queue, doneq);
5448
5449                 if (hrr_queue->hrrq_curr < hrr_queue->hrrq_end) {
5450                         hrr_queue->hrrq_curr++;
5451                 } else {
5452                         hrr_queue->hrrq_curr = hrr_queue->hrrq_start;
5453                         hrr_queue->toggle_bit ^= 1u;
5454                 }
5455                 num_hrrq++;
5456                 if (budget > 0 && num_hrrq >= budget)
5457                         break;
5458         }
5459
5460         return num_hrrq;
5461 }
5462
5463 static int ipr_iopoll(struct irq_poll *iop, int budget)
5464 {
5465         struct ipr_hrr_queue *hrrq;
5466         struct ipr_cmnd *ipr_cmd, *temp;
5467         unsigned long hrrq_flags;
5468         int completed_ops;
5469         LIST_HEAD(doneq);
5470
5471         hrrq = container_of(iop, struct ipr_hrr_queue, iopoll);
5472
5473         spin_lock_irqsave(hrrq->lock, hrrq_flags);
5474         completed_ops = ipr_process_hrrq(hrrq, budget, &doneq);
5475
5476         if (completed_ops < budget)
5477                 irq_poll_complete(iop);
5478         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5479
5480         list_for_each_entry_safe(ipr_cmd, temp, &doneq, queue) {
5481                 list_del(&ipr_cmd->queue);
5482                 del_timer(&ipr_cmd->timer);
5483                 ipr_cmd->fast_done(ipr_cmd);
5484         }
5485
5486         return completed_ops;
5487 }
5488
5489 /**
5490  * ipr_isr - Interrupt service routine
5491  * @irq:        irq number
5492  * @devp:       pointer to ioa config struct
5493  *
5494  * Return value:
5495  *      IRQ_NONE / IRQ_HANDLED
5496  **/
5497 static irqreturn_t ipr_isr(int irq, void *devp)
5498 {
5499         struct ipr_hrr_queue *hrrq = (struct ipr_hrr_queue *)devp;
5500         struct ipr_ioa_cfg *ioa_cfg = hrrq->ioa_cfg;
5501         unsigned long hrrq_flags = 0;
5502         u32 int_reg = 0;
5503         int num_hrrq = 0;
5504         int irq_none = 0;
5505         struct ipr_cmnd *ipr_cmd, *temp;
5506         irqreturn_t rc = IRQ_NONE;
5507         LIST_HEAD(doneq);
5508
5509         spin_lock_irqsave(hrrq->lock, hrrq_flags);
5510         /* If interrupts are disabled, ignore the interrupt */
5511         if (!hrrq->allow_interrupts) {
5512                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5513                 return IRQ_NONE;
5514         }
5515
5516         while (1) {
5517                 if (ipr_process_hrrq(hrrq, -1, &doneq)) {
5518                         rc =  IRQ_HANDLED;
5519
5520                         if (!ioa_cfg->clear_isr)
5521                                 break;
5522
5523                         /* Clear the PCI interrupt */
5524                         num_hrrq = 0;
5525                         do {
5526                                 writel(IPR_PCII_HRRQ_UPDATED,
5527                                      ioa_cfg->regs.clr_interrupt_reg32);
5528                                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
5529                         } while (int_reg & IPR_PCII_HRRQ_UPDATED &&
5530                                 num_hrrq++ < IPR_MAX_HRRQ_RETRIES);
5531
5532                 } else if (rc == IRQ_NONE && irq_none == 0) {
5533                         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
5534                         irq_none++;
5535                 } else if (num_hrrq == IPR_MAX_HRRQ_RETRIES &&
5536                            int_reg & IPR_PCII_HRRQ_UPDATED) {
5537                         ipr_isr_eh(ioa_cfg,
5538                                 "Error clearing HRRQ: ", num_hrrq);
5539                         rc = IRQ_HANDLED;
5540                         break;
5541                 } else
5542                         break;
5543         }
5544
5545         if (unlikely(rc == IRQ_NONE))
5546                 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
5547
5548         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5549         list_for_each_entry_safe(ipr_cmd, temp, &doneq, queue) {
5550                 list_del(&ipr_cmd->queue);
5551                 del_timer(&ipr_cmd->timer);
5552                 ipr_cmd->fast_done(ipr_cmd);
5553         }
5554         return rc;
5555 }
5556
5557 /**
5558  * ipr_isr_mhrrq - Interrupt service routine
5559  * @irq:        irq number
5560  * @devp:       pointer to ioa config struct
5561  *
5562  * Return value:
5563  *      IRQ_NONE / IRQ_HANDLED
5564  **/
5565 static irqreturn_t ipr_isr_mhrrq(int irq, void *devp)
5566 {
5567         struct ipr_hrr_queue *hrrq = (struct ipr_hrr_queue *)devp;
5568         struct ipr_ioa_cfg *ioa_cfg = hrrq->ioa_cfg;
5569         unsigned long hrrq_flags = 0;
5570         struct ipr_cmnd *ipr_cmd, *temp;
5571         irqreturn_t rc = IRQ_NONE;
5572         LIST_HEAD(doneq);
5573
5574         spin_lock_irqsave(hrrq->lock, hrrq_flags);
5575
5576         /* If interrupts are disabled, ignore the interrupt */
5577         if (!hrrq->allow_interrupts) {
5578                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5579                 return IRQ_NONE;
5580         }
5581
5582         if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
5583                 if ((be32_to_cpu(*hrrq->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
5584                        hrrq->toggle_bit) {
5585                         irq_poll_sched(&hrrq->iopoll);
5586                         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5587                         return IRQ_HANDLED;
5588                 }
5589         } else {
5590                 if ((be32_to_cpu(*hrrq->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
5591                         hrrq->toggle_bit)
5592
5593                         if (ipr_process_hrrq(hrrq, -1, &doneq))
5594                                 rc =  IRQ_HANDLED;
5595         }
5596
5597         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5598
5599         list_for_each_entry_safe(ipr_cmd, temp, &doneq, queue) {
5600                 list_del(&ipr_cmd->queue);
5601                 del_timer(&ipr_cmd->timer);
5602                 ipr_cmd->fast_done(ipr_cmd);
5603         }
5604         return rc;
5605 }
5606
5607 /**
5608  * ipr_build_ioadl64 - Build a scatter/gather list and map the buffer
5609  * @ioa_cfg:    ioa config struct
5610  * @ipr_cmd:    ipr command struct
5611  *
5612  * Return value:
5613  *      0 on success / -1 on failure
5614  **/
5615 static int ipr_build_ioadl64(struct ipr_ioa_cfg *ioa_cfg,
5616                              struct ipr_cmnd *ipr_cmd)
5617 {
5618         int i, nseg;
5619         struct scatterlist *sg;
5620         u32 length;
5621         u32 ioadl_flags = 0;
5622         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5623         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5624         struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
5625
5626         length = scsi_bufflen(scsi_cmd);
5627         if (!length)
5628                 return 0;
5629
5630         nseg = scsi_dma_map(scsi_cmd);
5631         if (nseg < 0) {
5632                 if (printk_ratelimit())
5633                         dev_err(&ioa_cfg->pdev->dev, "scsi_dma_map failed!\n");
5634                 return -1;
5635         }
5636
5637         ipr_cmd->dma_use_sg = nseg;
5638
5639         ioarcb->data_transfer_length = cpu_to_be32(length);
5640         ioarcb->ioadl_len =
5641                 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
5642
5643         if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
5644                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5645                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5646         } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE)
5647                 ioadl_flags = IPR_IOADL_FLAGS_READ;
5648
5649         scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
5650                 ioadl64[i].flags = cpu_to_be32(ioadl_flags);
5651                 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(sg));
5652                 ioadl64[i].address = cpu_to_be64(sg_dma_address(sg));
5653         }
5654
5655         ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5656         return 0;
5657 }
5658
5659 /**
5660  * ipr_build_ioadl - Build a scatter/gather list and map the buffer
5661  * @ioa_cfg:    ioa config struct
5662  * @ipr_cmd:    ipr command struct
5663  *
5664  * Return value:
5665  *      0 on success / -1 on failure
5666  **/
5667 static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
5668                            struct ipr_cmnd *ipr_cmd)
5669 {
5670         int i, nseg;
5671         struct scatterlist *sg;
5672         u32 length;
5673         u32 ioadl_flags = 0;
5674         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5675         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5676         struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
5677
5678         length = scsi_bufflen(scsi_cmd);
5679         if (!length)
5680                 return 0;
5681
5682         nseg = scsi_dma_map(scsi_cmd);
5683         if (nseg < 0) {
5684                 dev_err(&ioa_cfg->pdev->dev, "scsi_dma_map failed!\n");
5685                 return -1;
5686         }
5687
5688         ipr_cmd->dma_use_sg = nseg;
5689
5690         if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
5691                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5692                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5693                 ioarcb->data_transfer_length = cpu_to_be32(length);
5694                 ioarcb->ioadl_len =
5695                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5696         } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
5697                 ioadl_flags = IPR_IOADL_FLAGS_READ;
5698                 ioarcb->read_data_transfer_length = cpu_to_be32(length);
5699                 ioarcb->read_ioadl_len =
5700                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5701         }
5702
5703         if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->u.add_data.u.ioadl)) {
5704                 ioadl = ioarcb->u.add_data.u.ioadl;
5705                 ioarcb->write_ioadl_addr = cpu_to_be32((ipr_cmd->dma_addr) +
5706                                     offsetof(struct ipr_ioarcb, u.add_data));
5707                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5708         }
5709
5710         scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
5711                 ioadl[i].flags_and_data_len =
5712                         cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5713                 ioadl[i].address = cpu_to_be32(sg_dma_address(sg));
5714         }
5715
5716         ioadl[i-1].flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5717         return 0;
5718 }
5719
5720 /**
5721  * __ipr_erp_done - Process completion of ERP for a device
5722  * @ipr_cmd:            ipr command struct
5723  *
5724  * This function copies the sense buffer into the scsi_cmd
5725  * struct and pushes the scsi_done function.
5726  *
5727  * Return value:
5728  *      nothing
5729  **/
5730 static void __ipr_erp_done(struct ipr_cmnd *ipr_cmd)
5731 {
5732         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5733         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5734         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5735
5736         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
5737                 scsi_cmd->result |= (DID_ERROR << 16);
5738                 scmd_printk(KERN_ERR, scsi_cmd,
5739                             "Request Sense failed with IOASC: 0x%08X\n", ioasc);
5740         } else {
5741                 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
5742                        SCSI_SENSE_BUFFERSIZE);
5743         }
5744
5745         if (res) {
5746                 if (!ipr_is_naca_model(res))
5747                         res->needs_sync_complete = 1;
5748                 res->in_erp = 0;
5749         }
5750         scsi_dma_unmap(ipr_cmd->scsi_cmd);
5751         scsi_done(scsi_cmd);
5752         if (ipr_cmd->eh_comp)
5753                 complete(ipr_cmd->eh_comp);
5754         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
5755 }
5756
5757 /**
5758  * ipr_erp_done - Process completion of ERP for a device
5759  * @ipr_cmd:            ipr command struct
5760  *
5761  * This function copies the sense buffer into the scsi_cmd
5762  * struct and pushes the scsi_done function.
5763  *
5764  * Return value:
5765  *      nothing
5766  **/
5767 static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
5768 {
5769         struct ipr_hrr_queue *hrrq = ipr_cmd->hrrq;
5770         unsigned long hrrq_flags;
5771
5772         spin_lock_irqsave(&hrrq->_lock, hrrq_flags);
5773         __ipr_erp_done(ipr_cmd);
5774         spin_unlock_irqrestore(&hrrq->_lock, hrrq_flags);
5775 }
5776
5777 /**
5778  * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
5779  * @ipr_cmd:    ipr command struct
5780  *
5781  * Return value:
5782  *      none
5783  **/
5784 static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
5785 {
5786         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5787         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5788         dma_addr_t dma_addr = ipr_cmd->dma_addr;
5789
5790         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
5791         ioarcb->data_transfer_length = 0;
5792         ioarcb->read_data_transfer_length = 0;
5793         ioarcb->ioadl_len = 0;
5794         ioarcb->read_ioadl_len = 0;
5795         ioasa->hdr.ioasc = 0;
5796         ioasa->hdr.residual_data_len = 0;
5797
5798         if (ipr_cmd->ioa_cfg->sis64)
5799                 ioarcb->u.sis64_addr_data.data_ioadl_addr =
5800                         cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
5801         else {
5802                 ioarcb->write_ioadl_addr =
5803                         cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
5804                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5805         }
5806 }
5807
5808 /**
5809  * __ipr_erp_request_sense - Send request sense to a device
5810  * @ipr_cmd:    ipr command struct
5811  *
5812  * This function sends a request sense to a device as a result
5813  * of a check condition.
5814  *
5815  * Return value:
5816  *      nothing
5817  **/
5818 static void __ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
5819 {
5820         struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5821         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5822
5823         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
5824                 __ipr_erp_done(ipr_cmd);
5825                 return;
5826         }
5827
5828         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
5829
5830         cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
5831         cmd_pkt->cdb[0] = REQUEST_SENSE;
5832         cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
5833         cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
5834         cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5835         cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
5836
5837         ipr_init_ioadl(ipr_cmd, ipr_cmd->sense_buffer_dma,
5838                        SCSI_SENSE_BUFFERSIZE, IPR_IOADL_FLAGS_READ_LAST);
5839
5840         ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
5841                    IPR_REQUEST_SENSE_TIMEOUT * 2);
5842 }
5843
5844 /**
5845  * ipr_erp_request_sense - Send request sense to a device
5846  * @ipr_cmd:    ipr command struct
5847  *
5848  * This function sends a request sense to a device as a result
5849  * of a check condition.
5850  *
5851  * Return value:
5852  *      nothing
5853  **/
5854 static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
5855 {
5856         struct ipr_hrr_queue *hrrq = ipr_cmd->hrrq;
5857         unsigned long hrrq_flags;
5858
5859         spin_lock_irqsave(&hrrq->_lock, hrrq_flags);
5860         __ipr_erp_request_sense(ipr_cmd);
5861         spin_unlock_irqrestore(&hrrq->_lock, hrrq_flags);
5862 }
5863
5864 /**
5865  * ipr_erp_cancel_all - Send cancel all to a device
5866  * @ipr_cmd:    ipr command struct
5867  *
5868  * This function sends a cancel all to a device to clear the
5869  * queue. If we are running TCQ on the device, QERR is set to 1,
5870  * which means all outstanding ops have been dropped on the floor.
5871  * Cancel all will return them to us.
5872  *
5873  * Return value:
5874  *      nothing
5875  **/
5876 static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
5877 {
5878         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5879         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5880         struct ipr_cmd_pkt *cmd_pkt;
5881
5882         res->in_erp = 1;
5883
5884         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
5885
5886         if (!scsi_cmd->device->simple_tags) {
5887                 __ipr_erp_request_sense(ipr_cmd);
5888                 return;
5889         }
5890
5891         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5892         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5893         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
5894
5895         ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
5896                    IPR_CANCEL_ALL_TIMEOUT);
5897 }
5898
5899 /**
5900  * ipr_dump_ioasa - Dump contents of IOASA
5901  * @ioa_cfg:    ioa config struct
5902  * @ipr_cmd:    ipr command struct
5903  * @res:                resource entry struct
5904  *
5905  * This function is invoked by the interrupt handler when ops
5906  * fail. It will log the IOASA if appropriate. Only called
5907  * for GPDD ops.
5908  *
5909  * Return value:
5910  *      none
5911  **/
5912 static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
5913                            struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
5914 {
5915         int i;
5916         u16 data_len;
5917         u32 ioasc, fd_ioasc;
5918         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5919         __be32 *ioasa_data = (__be32 *)ioasa;
5920         int error_index;
5921
5922         ioasc = be32_to_cpu(ioasa->hdr.ioasc) & IPR_IOASC_IOASC_MASK;
5923         fd_ioasc = be32_to_cpu(ioasa->hdr.fd_ioasc) & IPR_IOASC_IOASC_MASK;
5924
5925         if (0 == ioasc)
5926                 return;
5927
5928         if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
5929                 return;
5930
5931         if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
5932                 error_index = ipr_get_error(fd_ioasc);
5933         else
5934                 error_index = ipr_get_error(ioasc);
5935
5936         if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
5937                 /* Don't log an error if the IOA already logged one */
5938                 if (ioasa->hdr.ilid != 0)
5939                         return;
5940
5941                 if (!ipr_is_gscsi(res))
5942                         return;
5943
5944                 if (ipr_error_table[error_index].log_ioasa == 0)
5945                         return;
5946         }
5947
5948         ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
5949
5950         data_len = be16_to_cpu(ioasa->hdr.ret_stat_len);
5951         if (ioa_cfg->sis64 && sizeof(struct ipr_ioasa64) < data_len)
5952                 data_len = sizeof(struct ipr_ioasa64);
5953         else if (!ioa_cfg->sis64 && sizeof(struct ipr_ioasa) < data_len)
5954                 data_len = sizeof(struct ipr_ioasa);
5955
5956         ipr_err("IOASA Dump:\n");
5957
5958         for (i = 0; i < data_len / 4; i += 4) {
5959                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
5960                         be32_to_cpu(ioasa_data[i]),
5961                         be32_to_cpu(ioasa_data[i+1]),
5962                         be32_to_cpu(ioasa_data[i+2]),
5963                         be32_to_cpu(ioasa_data[i+3]));
5964         }
5965 }
5966
5967 /**
5968  * ipr_gen_sense - Generate SCSI sense data from an IOASA
5969  * @ipr_cmd:    ipr command struct
5970  *
5971  * Return value:
5972  *      none
5973  **/
5974 static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
5975 {
5976         u32 failing_lba;
5977         u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
5978         struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
5979         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5980         u32 ioasc = be32_to_cpu(ioasa->hdr.ioasc);
5981
5982         memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
5983
5984         if (ioasc >= IPR_FIRST_DRIVER_IOASC)
5985                 return;
5986
5987         ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
5988
5989         if (ipr_is_vset_device(res) &&
5990             ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
5991             ioasa->u.vset.failing_lba_hi != 0) {
5992                 sense_buf[0] = 0x72;
5993                 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
5994                 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
5995                 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
5996
5997                 sense_buf[7] = 12;
5998                 sense_buf[8] = 0;
5999                 sense_buf[9] = 0x0A;
6000                 sense_buf[10] = 0x80;
6001
6002                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
6003
6004                 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
6005                 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
6006                 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
6007                 sense_buf[15] = failing_lba & 0x000000ff;
6008
6009                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
6010
6011                 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
6012                 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
6013                 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
6014                 sense_buf[19] = failing_lba & 0x000000ff;
6015         } else {
6016                 sense_buf[0] = 0x70;
6017                 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
6018                 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
6019                 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
6020
6021                 /* Illegal request */
6022                 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
6023                     (be32_to_cpu(ioasa->hdr.ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
6024                         sense_buf[7] = 10;      /* additional length */
6025
6026                         /* IOARCB was in error */
6027                         if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
6028                                 sense_buf[15] = 0xC0;
6029                         else    /* Parameter data was invalid */
6030                                 sense_buf[15] = 0x80;
6031
6032                         sense_buf[16] =
6033                             ((IPR_FIELD_POINTER_MASK &
6034                               be32_to_cpu(ioasa->hdr.ioasc_specific)) >> 8) & 0xff;
6035                         sense_buf[17] =
6036                             (IPR_FIELD_POINTER_MASK &
6037                              be32_to_cpu(ioasa->hdr.ioasc_specific)) & 0xff;
6038                 } else {
6039                         if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
6040                                 if (ipr_is_vset_device(res))
6041                                         failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
6042                                 else
6043                                         failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
6044
6045                                 sense_buf[0] |= 0x80;   /* Or in the Valid bit */
6046                                 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
6047                                 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
6048                                 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
6049                                 sense_buf[6] = failing_lba & 0x000000ff;
6050                         }
6051
6052                         sense_buf[7] = 6;       /* additional length */
6053                 }
6054         }
6055 }
6056
6057 /**
6058  * ipr_get_autosense - Copy autosense data to sense buffer
6059  * @ipr_cmd:    ipr command struct
6060  *
6061  * This function copies the autosense buffer to the buffer
6062  * in the scsi_cmd, if there is autosense available.
6063  *
6064  * Return value:
6065  *      1 if autosense was available / 0 if not
6066  **/
6067 static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
6068 {
6069         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
6070         struct ipr_ioasa64 *ioasa64 = &ipr_cmd->s.ioasa64;
6071
6072         if ((be32_to_cpu(ioasa->hdr.ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
6073                 return 0;
6074
6075         if (ipr_cmd->ioa_cfg->sis64)
6076                 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa64->auto_sense.data,
6077                        min_t(u16, be16_to_cpu(ioasa64->auto_sense.auto_sense_len),
6078                            SCSI_SENSE_BUFFERSIZE));
6079         else
6080                 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
6081                        min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
6082                            SCSI_SENSE_BUFFERSIZE));
6083         return 1;
6084 }
6085
6086 /**
6087  * ipr_erp_start - Process an error response for a SCSI op
6088  * @ioa_cfg:    ioa config struct
6089  * @ipr_cmd:    ipr command struct
6090  *
6091  * This function determines whether or not to initiate ERP
6092  * on the affected device.
6093  *
6094  * Return value:
6095  *      nothing
6096  **/
6097 static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
6098                               struct ipr_cmnd *ipr_cmd)
6099 {
6100         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
6101         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
6102         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
6103         u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
6104
6105         if (!res) {
6106                 __ipr_scsi_eh_done(ipr_cmd);
6107                 return;
6108         }
6109
6110         if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
6111                 ipr_gen_sense(ipr_cmd);
6112
6113         ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
6114
6115         switch (masked_ioasc) {
6116         case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
6117                 if (ipr_is_naca_model(res))
6118                         scsi_cmd->result |= (DID_ABORT << 16);
6119                 else
6120                         scsi_cmd->result |= (DID_IMM_RETRY << 16);
6121                 break;
6122         case IPR_IOASC_IR_RESOURCE_HANDLE:
6123         case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
6124                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
6125                 break;
6126         case IPR_IOASC_HW_SEL_TIMEOUT:
6127                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
6128                 if (!ipr_is_naca_model(res))
6129                         res->needs_sync_complete = 1;
6130                 break;
6131         case IPR_IOASC_SYNC_REQUIRED:
6132                 if (!res->in_erp)
6133                         res->needs_sync_complete = 1;
6134                 scsi_cmd->result |= (DID_IMM_RETRY << 16);
6135                 break;
6136         case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
6137         case IPR_IOASA_IR_DUAL_IOA_DISABLED:
6138                 /*
6139                  * exception: do not set DID_PASSTHROUGH on CHECK CONDITION
6140                  * so SCSI mid-layer and upper layers handle it accordingly.
6141                  */
6142                 if (scsi_cmd->result != SAM_STAT_CHECK_CONDITION)
6143                         scsi_cmd->result |= (DID_PASSTHROUGH << 16);
6144                 break;
6145         case IPR_IOASC_BUS_WAS_RESET:
6146         case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
6147                 /*
6148                  * Report the bus reset and ask for a retry. The device
6149                  * will give CC/UA the next command.
6150                  */
6151                 if (!res->resetting_device)
6152                         scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
6153                 scsi_cmd->result |= (DID_ERROR << 16);
6154                 if (!ipr_is_naca_model(res))
6155                         res->needs_sync_complete = 1;
6156                 break;
6157         case IPR_IOASC_HW_DEV_BUS_STATUS:
6158                 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
6159                 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
6160                         if (!ipr_get_autosense(ipr_cmd)) {
6161                                 if (!ipr_is_naca_model(res)) {
6162                                         ipr_erp_cancel_all(ipr_cmd);
6163                                         return;
6164                                 }
6165                         }
6166                 }
6167                 if (!ipr_is_naca_model(res))
6168                         res->needs_sync_complete = 1;
6169                 break;
6170         case IPR_IOASC_NR_INIT_CMD_REQUIRED:
6171                 break;
6172         case IPR_IOASC_IR_NON_OPTIMIZED:
6173                 if (res->raw_mode) {
6174                         res->raw_mode = 0;
6175                         scsi_cmd->result |= (DID_IMM_RETRY << 16);
6176                 } else
6177                         scsi_cmd->result |= (DID_ERROR << 16);
6178                 break;
6179         default:
6180                 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
6181                         scsi_cmd->result |= (DID_ERROR << 16);
6182                 if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
6183                         res->needs_sync_complete = 1;
6184                 break;
6185         }
6186
6187         scsi_dma_unmap(ipr_cmd->scsi_cmd);
6188         scsi_done(scsi_cmd);
6189         if (ipr_cmd->eh_comp)
6190                 complete(ipr_cmd->eh_comp);
6191         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6192 }
6193
6194 /**
6195  * ipr_scsi_done - mid-layer done function
6196  * @ipr_cmd:    ipr command struct
6197  *
6198  * This function is invoked by the interrupt handler for
6199  * ops generated by the SCSI mid-layer
6200  *
6201  * Return value:
6202  *      none
6203  **/
6204 static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
6205 {
6206         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6207         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
6208         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
6209         unsigned long lock_flags;
6210
6211         scsi_set_resid(scsi_cmd, be32_to_cpu(ipr_cmd->s.ioasa.hdr.residual_data_len));
6212
6213         if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
6214                 scsi_dma_unmap(scsi_cmd);
6215
6216                 spin_lock_irqsave(ipr_cmd->hrrq->lock, lock_flags);
6217                 scsi_done(scsi_cmd);
6218                 if (ipr_cmd->eh_comp)
6219                         complete(ipr_cmd->eh_comp);
6220                 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6221                 spin_unlock_irqrestore(ipr_cmd->hrrq->lock, lock_flags);
6222         } else {
6223                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
6224                 spin_lock(&ipr_cmd->hrrq->_lock);
6225                 ipr_erp_start(ioa_cfg, ipr_cmd);
6226                 spin_unlock(&ipr_cmd->hrrq->_lock);
6227                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
6228         }
6229 }
6230
6231 /**
6232  * ipr_queuecommand - Queue a mid-layer request
6233  * @shost:              scsi host struct
6234  * @scsi_cmd:   scsi command struct
6235  *
6236  * This function queues a request generated by the mid-layer.
6237  *
6238  * Return value:
6239  *      0 on success
6240  *      SCSI_MLQUEUE_DEVICE_BUSY if device is busy
6241  *      SCSI_MLQUEUE_HOST_BUSY if host is busy
6242  **/
6243 static int ipr_queuecommand(struct Scsi_Host *shost,
6244                             struct scsi_cmnd *scsi_cmd)
6245 {
6246         struct ipr_ioa_cfg *ioa_cfg;
6247         struct ipr_resource_entry *res;
6248         struct ipr_ioarcb *ioarcb;
6249         struct ipr_cmnd *ipr_cmd;
6250         unsigned long hrrq_flags;
6251         int rc;
6252         struct ipr_hrr_queue *hrrq;
6253         int hrrq_id;
6254
6255         ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
6256
6257         scsi_cmd->result = (DID_OK << 16);
6258         res = scsi_cmd->device->hostdata;
6259
6260         hrrq_id = ipr_get_hrrq_index(ioa_cfg);
6261         hrrq = &ioa_cfg->hrrq[hrrq_id];
6262
6263         spin_lock_irqsave(hrrq->lock, hrrq_flags);
6264         /*
6265          * We are currently blocking all devices due to a host reset
6266          * We have told the host to stop giving us new requests, but
6267          * ERP ops don't count. FIXME
6268          */
6269         if (unlikely(!hrrq->allow_cmds && !hrrq->ioa_is_dead && !hrrq->removing_ioa)) {
6270                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6271                 return SCSI_MLQUEUE_HOST_BUSY;
6272         }
6273
6274         /*
6275          * FIXME - Create scsi_set_host_offline interface
6276          *  and the ioa_is_dead check can be removed
6277          */
6278         if (unlikely(hrrq->ioa_is_dead || hrrq->removing_ioa || !res)) {
6279                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6280                 goto err_nodev;
6281         }
6282
6283         ipr_cmd = __ipr_get_free_ipr_cmnd(hrrq);
6284         if (ipr_cmd == NULL) {
6285                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6286                 return SCSI_MLQUEUE_HOST_BUSY;
6287         }
6288         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6289
6290         ipr_init_ipr_cmnd(ipr_cmd, ipr_scsi_done);
6291         ioarcb = &ipr_cmd->ioarcb;
6292
6293         memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
6294         ipr_cmd->scsi_cmd = scsi_cmd;
6295         ipr_cmd->done = ipr_scsi_eh_done;
6296
6297         if (ipr_is_gscsi(res)) {
6298                 if (scsi_cmd->underflow == 0)
6299                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
6300
6301                 if (res->reset_occurred) {
6302                         res->reset_occurred = 0;
6303                         ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
6304                 }
6305         }
6306
6307         if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
6308                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
6309
6310                 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
6311                 if (scsi_cmd->flags & SCMD_TAGGED)
6312                         ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_SIMPLE_TASK;
6313                 else
6314                         ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_UNTAGGED_TASK;
6315         }
6316
6317         if (scsi_cmd->cmnd[0] >= 0xC0 &&
6318             (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE)) {
6319                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6320         }
6321         if (res->raw_mode && ipr_is_af_dasd_device(res)) {
6322                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_PIPE;
6323
6324                 if (scsi_cmd->underflow == 0)
6325                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
6326         }
6327
6328         if (ioa_cfg->sis64)
6329                 rc = ipr_build_ioadl64(ioa_cfg, ipr_cmd);
6330         else
6331                 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
6332
6333         spin_lock_irqsave(hrrq->lock, hrrq_flags);
6334         if (unlikely(rc || (!hrrq->allow_cmds && !hrrq->ioa_is_dead))) {
6335                 list_add_tail(&ipr_cmd->queue, &hrrq->hrrq_free_q);
6336                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6337                 if (!rc)
6338                         scsi_dma_unmap(scsi_cmd);
6339                 return SCSI_MLQUEUE_HOST_BUSY;
6340         }
6341
6342         if (unlikely(hrrq->ioa_is_dead)) {
6343                 list_add_tail(&ipr_cmd->queue, &hrrq->hrrq_free_q);
6344                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6345                 scsi_dma_unmap(scsi_cmd);
6346                 goto err_nodev;
6347         }
6348
6349         ioarcb->res_handle = res->res_handle;
6350         if (res->needs_sync_complete) {
6351                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
6352                 res->needs_sync_complete = 0;
6353         }
6354         list_add_tail(&ipr_cmd->queue, &hrrq->hrrq_pending_q);
6355         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
6356         ipr_send_command(ipr_cmd);
6357         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6358         return 0;
6359
6360 err_nodev:
6361         spin_lock_irqsave(hrrq->lock, hrrq_flags);
6362         memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
6363         scsi_cmd->result = (DID_NO_CONNECT << 16);
6364         scsi_done(scsi_cmd);
6365         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6366         return 0;
6367 }
6368
6369 /**
6370  * ipr_ioa_info - Get information about the card/driver
6371  * @host:       scsi host struct
6372  *
6373  * Return value:
6374  *      pointer to buffer with description string
6375  **/
6376 static const char *ipr_ioa_info(struct Scsi_Host *host)
6377 {
6378         static char buffer[512];
6379         struct ipr_ioa_cfg *ioa_cfg;
6380         unsigned long lock_flags = 0;
6381
6382         ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
6383
6384         spin_lock_irqsave(host->host_lock, lock_flags);
6385         sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
6386         spin_unlock_irqrestore(host->host_lock, lock_flags);
6387
6388         return buffer;
6389 }
6390
6391 static const struct scsi_host_template driver_template = {
6392         .module = THIS_MODULE,
6393         .name = "IPR",
6394         .info = ipr_ioa_info,
6395         .queuecommand = ipr_queuecommand,
6396         .eh_abort_handler = ipr_eh_abort,
6397         .eh_device_reset_handler = ipr_eh_dev_reset,
6398         .eh_host_reset_handler = ipr_eh_host_reset,
6399         .slave_alloc = ipr_slave_alloc,
6400         .slave_configure = ipr_slave_configure,
6401         .slave_destroy = ipr_slave_destroy,
6402         .scan_finished = ipr_scan_finished,
6403         .target_destroy = ipr_target_destroy,
6404         .change_queue_depth = ipr_change_queue_depth,
6405         .bios_param = ipr_biosparam,
6406         .can_queue = IPR_MAX_COMMANDS,
6407         .this_id = -1,
6408         .sg_tablesize = IPR_MAX_SGLIST,
6409         .max_sectors = IPR_IOA_MAX_SECTORS,
6410         .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
6411         .shost_groups = ipr_ioa_groups,
6412         .sdev_groups = ipr_dev_groups,
6413         .proc_name = IPR_NAME,
6414 };
6415
6416 /**
6417  * ipr_ioa_bringdown_done - IOA bring down completion.
6418  * @ipr_cmd:    ipr command struct
6419  *
6420  * This function processes the completion of an adapter bring down.
6421  * It wakes any reset sleepers.
6422  *
6423  * Return value:
6424  *      IPR_RC_JOB_RETURN
6425  **/
6426 static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
6427 {
6428         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6429         int i;
6430
6431         ENTER;
6432         if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].removing_ioa) {
6433                 ipr_trace;
6434                 ioa_cfg->scsi_unblock = 1;
6435                 schedule_work(&ioa_cfg->work_q);
6436         }
6437
6438         ioa_cfg->in_reset_reload = 0;
6439         ioa_cfg->reset_retries = 0;
6440         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
6441                 spin_lock(&ioa_cfg->hrrq[i]._lock);
6442                 ioa_cfg->hrrq[i].ioa_is_dead = 1;
6443                 spin_unlock(&ioa_cfg->hrrq[i]._lock);
6444         }
6445         wmb();
6446
6447         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6448         wake_up_all(&ioa_cfg->reset_wait_q);
6449         LEAVE;
6450
6451         return IPR_RC_JOB_RETURN;
6452 }
6453
6454 /**
6455  * ipr_ioa_reset_done - IOA reset completion.
6456  * @ipr_cmd:    ipr command struct
6457  *
6458  * This function processes the completion of an adapter reset.
6459  * It schedules any necessary mid-layer add/removes and
6460  * wakes any reset sleepers.
6461  *
6462  * Return value:
6463  *      IPR_RC_JOB_RETURN
6464  **/
6465 static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
6466 {
6467         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6468         struct ipr_resource_entry *res;
6469         int j;
6470
6471         ENTER;
6472         ioa_cfg->in_reset_reload = 0;
6473         for (j = 0; j < ioa_cfg->hrrq_num; j++) {
6474                 spin_lock(&ioa_cfg->hrrq[j]._lock);
6475                 ioa_cfg->hrrq[j].allow_cmds = 1;
6476                 spin_unlock(&ioa_cfg->hrrq[j]._lock);
6477         }
6478         wmb();
6479         ioa_cfg->reset_cmd = NULL;
6480         ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
6481
6482         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
6483                 if (res->add_to_ml || res->del_from_ml) {
6484                         ipr_trace;
6485                         break;
6486                 }
6487         }
6488         schedule_work(&ioa_cfg->work_q);
6489
6490         for (j = 0; j < IPR_NUM_HCAMS; j++) {
6491                 list_del_init(&ioa_cfg->hostrcb[j]->queue);
6492                 if (j < IPR_NUM_LOG_HCAMS)
6493                         ipr_send_hcam(ioa_cfg,
6494                                 IPR_HCAM_CDB_OP_CODE_LOG_DATA,
6495                                 ioa_cfg->hostrcb[j]);
6496                 else
6497                         ipr_send_hcam(ioa_cfg,
6498                                 IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
6499                                 ioa_cfg->hostrcb[j]);
6500         }
6501
6502         scsi_report_bus_reset(ioa_cfg->host, IPR_VSET_BUS);
6503         dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
6504
6505         ioa_cfg->reset_retries = 0;
6506         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6507         wake_up_all(&ioa_cfg->reset_wait_q);
6508
6509         ioa_cfg->scsi_unblock = 1;
6510         schedule_work(&ioa_cfg->work_q);
6511         LEAVE;
6512         return IPR_RC_JOB_RETURN;
6513 }
6514
6515 /**
6516  * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
6517  * @supported_dev:      supported device struct
6518  * @vpids:                      vendor product id struct
6519  *
6520  * Return value:
6521  *      none
6522  **/
6523 static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
6524                                  struct ipr_std_inq_vpids *vpids)
6525 {
6526         memset(supported_dev, 0, sizeof(struct ipr_supported_device));
6527         memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
6528         supported_dev->num_records = 1;
6529         supported_dev->data_length =
6530                 cpu_to_be16(sizeof(struct ipr_supported_device));
6531         supported_dev->reserved = 0;
6532 }
6533
6534 /**
6535  * ipr_set_supported_devs - Send Set Supported Devices for a device
6536  * @ipr_cmd:    ipr command struct
6537  *
6538  * This function sends a Set Supported Devices to the adapter
6539  *
6540  * Return value:
6541  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6542  **/
6543 static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
6544 {
6545         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6546         struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
6547         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6548         struct ipr_resource_entry *res = ipr_cmd->u.res;
6549
6550         ipr_cmd->job_step = ipr_ioa_reset_done;
6551
6552         list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
6553                 if (!ipr_is_scsi_disk(res))
6554                         continue;
6555
6556                 ipr_cmd->u.res = res;
6557                 ipr_set_sup_dev_dflt(supp_dev, &res->std_inq_data.vpids);
6558
6559                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6560                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6561                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6562
6563                 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
6564                 ioarcb->cmd_pkt.cdb[1] = IPR_SET_ALL_SUPPORTED_DEVICES;
6565                 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
6566                 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
6567
6568                 ipr_init_ioadl(ipr_cmd,
6569                                ioa_cfg->vpd_cbs_dma +
6570                                  offsetof(struct ipr_misc_cbs, supp_dev),
6571                                sizeof(struct ipr_supported_device),
6572                                IPR_IOADL_FLAGS_WRITE_LAST);
6573
6574                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
6575                            IPR_SET_SUP_DEVICE_TIMEOUT);
6576
6577                 if (!ioa_cfg->sis64)
6578                         ipr_cmd->job_step = ipr_set_supported_devs;
6579                 LEAVE;
6580                 return IPR_RC_JOB_RETURN;
6581         }
6582
6583         LEAVE;
6584         return IPR_RC_JOB_CONTINUE;
6585 }
6586
6587 /**
6588  * ipr_get_mode_page - Locate specified mode page
6589  * @mode_pages: mode page buffer
6590  * @page_code:  page code to find
6591  * @len:                minimum required length for mode page
6592  *
6593  * Return value:
6594  *      pointer to mode page / NULL on failure
6595  **/
6596 static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
6597                                u32 page_code, u32 len)
6598 {
6599         struct ipr_mode_page_hdr *mode_hdr;
6600         u32 page_length;
6601         u32 length;
6602
6603         if (!mode_pages || (mode_pages->hdr.length == 0))
6604                 return NULL;
6605
6606         length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
6607         mode_hdr = (struct ipr_mode_page_hdr *)
6608                 (mode_pages->data + mode_pages->hdr.block_desc_len);
6609
6610         while (length) {
6611                 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
6612                         if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
6613                                 return mode_hdr;
6614                         break;
6615                 } else {
6616                         page_length = (sizeof(struct ipr_mode_page_hdr) +
6617                                        mode_hdr->page_length);
6618                         length -= page_length;
6619                         mode_hdr = (struct ipr_mode_page_hdr *)
6620                                 ((unsigned long)mode_hdr + page_length);
6621                 }
6622         }
6623         return NULL;
6624 }
6625
6626 /**
6627  * ipr_check_term_power - Check for term power errors
6628  * @ioa_cfg:    ioa config struct
6629  * @mode_pages: IOAFP mode pages buffer
6630  *
6631  * Check the IOAFP's mode page 28 for term power errors
6632  *
6633  * Return value:
6634  *      nothing
6635  **/
6636 static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
6637                                  struct ipr_mode_pages *mode_pages)
6638 {
6639         int i;
6640         int entry_length;
6641         struct ipr_dev_bus_entry *bus;
6642         struct ipr_mode_page28 *mode_page;
6643
6644         mode_page = ipr_get_mode_page(mode_pages, 0x28,
6645                                       sizeof(struct ipr_mode_page28));
6646
6647         entry_length = mode_page->entry_length;
6648
6649         bus = mode_page->bus;
6650
6651         for (i = 0; i < mode_page->num_entries; i++) {
6652                 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
6653                         dev_err(&ioa_cfg->pdev->dev,
6654                                 "Term power is absent on scsi bus %d\n",
6655                                 bus->res_addr.bus);
6656                 }
6657
6658                 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
6659         }
6660 }
6661
6662 /**
6663  * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
6664  * @ioa_cfg:    ioa config struct
6665  *
6666  * Looks through the config table checking for SES devices. If
6667  * the SES device is in the SES table indicating a maximum SCSI
6668  * bus speed, the speed is limited for the bus.
6669  *
6670  * Return value:
6671  *      none
6672  **/
6673 static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
6674 {
6675         u32 max_xfer_rate;
6676         int i;
6677
6678         for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
6679                 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
6680                                                        ioa_cfg->bus_attr[i].bus_width);
6681
6682                 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
6683                         ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
6684         }
6685 }
6686
6687 /**
6688  * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
6689  * @ioa_cfg:    ioa config struct
6690  * @mode_pages: mode page 28 buffer
6691  *
6692  * Updates mode page 28 based on driver configuration
6693  *
6694  * Return value:
6695  *      none
6696  **/
6697 static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
6698                                           struct ipr_mode_pages *mode_pages)
6699 {
6700         int i, entry_length;
6701         struct ipr_dev_bus_entry *bus;
6702         struct ipr_bus_attributes *bus_attr;
6703         struct ipr_mode_page28 *mode_page;
6704
6705         mode_page = ipr_get_mode_page(mode_pages, 0x28,
6706                                       sizeof(struct ipr_mode_page28));
6707
6708         entry_length = mode_page->entry_length;
6709
6710         /* Loop for each device bus entry */
6711         for (i = 0, bus = mode_page->bus;
6712              i < mode_page->num_entries;
6713              i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
6714                 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
6715                         dev_err(&ioa_cfg->pdev->dev,
6716                                 "Invalid resource address reported: 0x%08X\n",
6717                                 IPR_GET_PHYS_LOC(bus->res_addr));
6718                         continue;
6719                 }
6720
6721                 bus_attr = &ioa_cfg->bus_attr[i];
6722                 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
6723                 bus->bus_width = bus_attr->bus_width;
6724                 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
6725                 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
6726                 if (bus_attr->qas_enabled)
6727                         bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
6728                 else
6729                         bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
6730         }
6731 }
6732
6733 /**
6734  * ipr_build_mode_select - Build a mode select command
6735  * @ipr_cmd:    ipr command struct
6736  * @res_handle: resource handle to send command to
6737  * @parm:               Byte 2 of Mode Sense command
6738  * @dma_addr:   DMA buffer address
6739  * @xfer_len:   data transfer length
6740  *
6741  * Return value:
6742  *      none
6743  **/
6744 static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
6745                                   __be32 res_handle, u8 parm,
6746                                   dma_addr_t dma_addr, u8 xfer_len)
6747 {
6748         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6749
6750         ioarcb->res_handle = res_handle;
6751         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6752         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6753         ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
6754         ioarcb->cmd_pkt.cdb[1] = parm;
6755         ioarcb->cmd_pkt.cdb[4] = xfer_len;
6756
6757         ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_WRITE_LAST);
6758 }
6759
6760 /**
6761  * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
6762  * @ipr_cmd:    ipr command struct
6763  *
6764  * This function sets up the SCSI bus attributes and sends
6765  * a Mode Select for Page 28 to activate them.
6766  *
6767  * Return value:
6768  *      IPR_RC_JOB_RETURN
6769  **/
6770 static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
6771 {
6772         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6773         struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
6774         int length;
6775
6776         ENTER;
6777         ipr_scsi_bus_speed_limit(ioa_cfg);
6778         ipr_check_term_power(ioa_cfg, mode_pages);
6779         ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
6780         length = mode_pages->hdr.length + 1;
6781         mode_pages->hdr.length = 0;
6782
6783         ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
6784                               ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
6785                               length);
6786
6787         ipr_cmd->job_step = ipr_set_supported_devs;
6788         ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
6789                                     struct ipr_resource_entry, queue);
6790         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6791
6792         LEAVE;
6793         return IPR_RC_JOB_RETURN;
6794 }
6795
6796 /**
6797  * ipr_build_mode_sense - Builds a mode sense command
6798  * @ipr_cmd:    ipr command struct
6799  * @res_handle:         resource entry struct
6800  * @parm:               Byte 2 of mode sense command
6801  * @dma_addr:   DMA address of mode sense buffer
6802  * @xfer_len:   Size of DMA buffer
6803  *
6804  * Return value:
6805  *      none
6806  **/
6807 static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
6808                                  __be32 res_handle,
6809                                  u8 parm, dma_addr_t dma_addr, u8 xfer_len)
6810 {
6811         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6812
6813         ioarcb->res_handle = res_handle;
6814         ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
6815         ioarcb->cmd_pkt.cdb[2] = parm;
6816         ioarcb->cmd_pkt.cdb[4] = xfer_len;
6817         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6818
6819         ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
6820 }
6821
6822 /**
6823  * ipr_reset_cmd_failed - Handle failure of IOA reset command
6824  * @ipr_cmd:    ipr command struct
6825  *
6826  * This function handles the failure of an IOA bringup command.
6827  *
6828  * Return value:
6829  *      IPR_RC_JOB_RETURN
6830  **/
6831 static int ipr_reset_cmd_failed(struct ipr_cmnd *ipr_cmd)
6832 {
6833         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6834         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
6835
6836         dev_err(&ioa_cfg->pdev->dev,
6837                 "0x%02X failed with IOASC: 0x%08X\n",
6838                 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
6839
6840         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
6841         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6842         return IPR_RC_JOB_RETURN;
6843 }
6844
6845 /**
6846  * ipr_reset_mode_sense_failed - Handle failure of IOAFP mode sense
6847  * @ipr_cmd:    ipr command struct
6848  *
6849  * This function handles the failure of a Mode Sense to the IOAFP.
6850  * Some adapters do not handle all mode pages.
6851  *
6852  * Return value:
6853  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6854  **/
6855 static int ipr_reset_mode_sense_failed(struct ipr_cmnd *ipr_cmd)
6856 {
6857         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6858         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
6859
6860         if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
6861                 ipr_cmd->job_step = ipr_set_supported_devs;
6862                 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
6863                                             struct ipr_resource_entry, queue);
6864                 return IPR_RC_JOB_CONTINUE;
6865         }
6866
6867         return ipr_reset_cmd_failed(ipr_cmd);
6868 }
6869
6870 /**
6871  * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
6872  * @ipr_cmd:    ipr command struct
6873  *
6874  * This function send a Page 28 mode sense to the IOA to
6875  * retrieve SCSI bus attributes.
6876  *
6877  * Return value:
6878  *      IPR_RC_JOB_RETURN
6879  **/
6880 static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
6881 {
6882         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6883
6884         ENTER;
6885         ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
6886                              0x28, ioa_cfg->vpd_cbs_dma +
6887                              offsetof(struct ipr_misc_cbs, mode_pages),
6888                              sizeof(struct ipr_mode_pages));
6889
6890         ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
6891         ipr_cmd->job_step_failed = ipr_reset_mode_sense_failed;
6892
6893         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6894
6895         LEAVE;
6896         return IPR_RC_JOB_RETURN;
6897 }
6898
6899 /**
6900  * ipr_ioafp_mode_select_page24 - Issue Mode Select to IOA
6901  * @ipr_cmd:    ipr command struct
6902  *
6903  * This function enables dual IOA RAID support if possible.
6904  *
6905  * Return value:
6906  *      IPR_RC_JOB_RETURN
6907  **/
6908 static int ipr_ioafp_mode_select_page24(struct ipr_cmnd *ipr_cmd)
6909 {
6910         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6911         struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
6912         struct ipr_mode_page24 *mode_page;
6913         int length;
6914
6915         ENTER;
6916         mode_page = ipr_get_mode_page(mode_pages, 0x24,
6917                                       sizeof(struct ipr_mode_page24));
6918
6919         if (mode_page)
6920                 mode_page->flags |= IPR_ENABLE_DUAL_IOA_AF;
6921
6922         length = mode_pages->hdr.length + 1;
6923         mode_pages->hdr.length = 0;
6924
6925         ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
6926                               ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
6927                               length);
6928
6929         ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
6930         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6931
6932         LEAVE;
6933         return IPR_RC_JOB_RETURN;
6934 }
6935
6936 /**
6937  * ipr_reset_mode_sense_page24_failed - Handle failure of IOAFP mode sense
6938  * @ipr_cmd:    ipr command struct
6939  *
6940  * This function handles the failure of a Mode Sense to the IOAFP.
6941  * Some adapters do not handle all mode pages.
6942  *
6943  * Return value:
6944  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6945  **/
6946 static int ipr_reset_mode_sense_page24_failed(struct ipr_cmnd *ipr_cmd)
6947 {
6948         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
6949
6950         if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
6951                 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
6952                 return IPR_RC_JOB_CONTINUE;
6953         }
6954
6955         return ipr_reset_cmd_failed(ipr_cmd);
6956 }
6957
6958 /**
6959  * ipr_ioafp_mode_sense_page24 - Issue Page 24 Mode Sense to IOA
6960  * @ipr_cmd:    ipr command struct
6961  *
6962  * This function send a mode sense to the IOA to retrieve
6963  * the IOA Advanced Function Control mode page.
6964  *
6965  * Return value:
6966  *      IPR_RC_JOB_RETURN
6967  **/
6968 static int ipr_ioafp_mode_sense_page24(struct ipr_cmnd *ipr_cmd)
6969 {
6970         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6971
6972         ENTER;
6973         ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
6974                              0x24, ioa_cfg->vpd_cbs_dma +
6975                              offsetof(struct ipr_misc_cbs, mode_pages),
6976                              sizeof(struct ipr_mode_pages));
6977
6978         ipr_cmd->job_step = ipr_ioafp_mode_select_page24;
6979         ipr_cmd->job_step_failed = ipr_reset_mode_sense_page24_failed;
6980
6981         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6982
6983         LEAVE;
6984         return IPR_RC_JOB_RETURN;
6985 }
6986
6987 /**
6988  * ipr_init_res_table - Initialize the resource table
6989  * @ipr_cmd:    ipr command struct
6990  *
6991  * This function looks through the existing resource table, comparing
6992  * it with the config table. This function will take care of old/new
6993  * devices and schedule adding/removing them from the mid-layer
6994  * as appropriate.
6995  *
6996  * Return value:
6997  *      IPR_RC_JOB_CONTINUE
6998  **/
6999 static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
7000 {
7001         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7002         struct ipr_resource_entry *res, *temp;
7003         struct ipr_config_table_entry_wrapper cfgtew;
7004         int entries, found, flag, i;
7005         LIST_HEAD(old_res);
7006
7007         ENTER;
7008         if (ioa_cfg->sis64)
7009                 flag = ioa_cfg->u.cfg_table64->hdr64.flags;
7010         else
7011                 flag = ioa_cfg->u.cfg_table->hdr.flags;
7012
7013         if (flag & IPR_UCODE_DOWNLOAD_REQ)
7014                 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
7015
7016         list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
7017                 list_move_tail(&res->queue, &old_res);
7018
7019         if (ioa_cfg->sis64)
7020                 entries = be16_to_cpu(ioa_cfg->u.cfg_table64->hdr64.num_entries);
7021         else
7022                 entries = ioa_cfg->u.cfg_table->hdr.num_entries;
7023
7024         for (i = 0; i < entries; i++) {
7025                 if (ioa_cfg->sis64)
7026                         cfgtew.u.cfgte64 = &ioa_cfg->u.cfg_table64->dev[i];
7027                 else
7028                         cfgtew.u.cfgte = &ioa_cfg->u.cfg_table->dev[i];
7029                 found = 0;
7030
7031                 list_for_each_entry_safe(res, temp, &old_res, queue) {
7032                         if (ipr_is_same_device(res, &cfgtew)) {
7033                                 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
7034                                 found = 1;
7035                                 break;
7036                         }
7037                 }
7038
7039                 if (!found) {
7040                         if (list_empty(&ioa_cfg->free_res_q)) {
7041                                 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
7042                                 break;
7043                         }
7044
7045                         found = 1;
7046                         res = list_entry(ioa_cfg->free_res_q.next,
7047                                          struct ipr_resource_entry, queue);
7048                         list_move_tail(&res->queue, &ioa_cfg->used_res_q);
7049                         ipr_init_res_entry(res, &cfgtew);
7050                         res->add_to_ml = 1;
7051                 } else if (res->sdev && (ipr_is_vset_device(res) || ipr_is_scsi_disk(res)))
7052                         res->sdev->allow_restart = 1;
7053
7054                 if (found)
7055                         ipr_update_res_entry(res, &cfgtew);
7056         }
7057
7058         list_for_each_entry_safe(res, temp, &old_res, queue) {
7059                 if (res->sdev) {
7060                         res->del_from_ml = 1;
7061                         res->res_handle = IPR_INVALID_RES_HANDLE;
7062                         list_move_tail(&res->queue, &ioa_cfg->used_res_q);
7063                 }
7064         }
7065
7066         list_for_each_entry_safe(res, temp, &old_res, queue) {
7067                 ipr_clear_res_target(res);
7068                 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
7069         }
7070
7071         if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
7072                 ipr_cmd->job_step = ipr_ioafp_mode_sense_page24;
7073         else
7074                 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
7075
7076         LEAVE;
7077         return IPR_RC_JOB_CONTINUE;
7078 }
7079
7080 /**
7081  * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
7082  * @ipr_cmd:    ipr command struct
7083  *
7084  * This function sends a Query IOA Configuration command
7085  * to the adapter to retrieve the IOA configuration table.
7086  *
7087  * Return value:
7088  *      IPR_RC_JOB_RETURN
7089  **/
7090 static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
7091 {
7092         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7093         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7094         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
7095         struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
7096
7097         ENTER;
7098         if (cap->cap & IPR_CAP_DUAL_IOA_RAID)
7099                 ioa_cfg->dual_raid = 1;
7100         dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
7101                  ucode_vpd->major_release, ucode_vpd->card_type,
7102                  ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
7103         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
7104         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7105
7106         ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
7107         ioarcb->cmd_pkt.cdb[6] = (ioa_cfg->cfg_table_size >> 16) & 0xff;
7108         ioarcb->cmd_pkt.cdb[7] = (ioa_cfg->cfg_table_size >> 8) & 0xff;
7109         ioarcb->cmd_pkt.cdb[8] = ioa_cfg->cfg_table_size & 0xff;
7110
7111         ipr_init_ioadl(ipr_cmd, ioa_cfg->cfg_table_dma, ioa_cfg->cfg_table_size,
7112                        IPR_IOADL_FLAGS_READ_LAST);
7113
7114         ipr_cmd->job_step = ipr_init_res_table;
7115
7116         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7117
7118         LEAVE;
7119         return IPR_RC_JOB_RETURN;
7120 }
7121
7122 static int ipr_ioa_service_action_failed(struct ipr_cmnd *ipr_cmd)
7123 {
7124         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
7125
7126         if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT)
7127                 return IPR_RC_JOB_CONTINUE;
7128
7129         return ipr_reset_cmd_failed(ipr_cmd);
7130 }
7131
7132 static void ipr_build_ioa_service_action(struct ipr_cmnd *ipr_cmd,
7133                                          __be32 res_handle, u8 sa_code)
7134 {
7135         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7136
7137         ioarcb->res_handle = res_handle;
7138         ioarcb->cmd_pkt.cdb[0] = IPR_IOA_SERVICE_ACTION;
7139         ioarcb->cmd_pkt.cdb[1] = sa_code;
7140         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
7141 }
7142
7143 /**
7144  * ipr_ioafp_set_caching_parameters - Issue Set Cache parameters service
7145  * action
7146  * @ipr_cmd:    ipr command struct
7147  *
7148  * Return value:
7149  *      none
7150  **/
7151 static int ipr_ioafp_set_caching_parameters(struct ipr_cmnd *ipr_cmd)
7152 {
7153         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7154         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7155         struct ipr_inquiry_pageC4 *pageC4 = &ioa_cfg->vpd_cbs->pageC4_data;
7156
7157         ENTER;
7158
7159         ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
7160
7161         if (pageC4->cache_cap[0] & IPR_CAP_SYNC_CACHE) {
7162                 ipr_build_ioa_service_action(ipr_cmd,
7163                                              cpu_to_be32(IPR_IOA_RES_HANDLE),
7164                                              IPR_IOA_SA_CHANGE_CACHE_PARAMS);
7165
7166                 ioarcb->cmd_pkt.cdb[2] = 0x40;
7167
7168                 ipr_cmd->job_step_failed = ipr_ioa_service_action_failed;
7169                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
7170                            IPR_SET_SUP_DEVICE_TIMEOUT);
7171
7172                 LEAVE;
7173                 return IPR_RC_JOB_RETURN;
7174         }
7175
7176         LEAVE;
7177         return IPR_RC_JOB_CONTINUE;
7178 }
7179
7180 /**
7181  * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
7182  * @ipr_cmd:    ipr command struct
7183  * @flags:      flags to send
7184  * @page:       page to inquire
7185  * @dma_addr:   DMA address
7186  * @xfer_len:   transfer data length
7187  *
7188  * This utility function sends an inquiry to the adapter.
7189  *
7190  * Return value:
7191  *      none
7192  **/
7193 static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
7194                               dma_addr_t dma_addr, u8 xfer_len)
7195 {
7196         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7197
7198         ENTER;
7199         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
7200         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7201
7202         ioarcb->cmd_pkt.cdb[0] = INQUIRY;
7203         ioarcb->cmd_pkt.cdb[1] = flags;
7204         ioarcb->cmd_pkt.cdb[2] = page;
7205         ioarcb->cmd_pkt.cdb[4] = xfer_len;
7206
7207         ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
7208
7209         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7210         LEAVE;
7211 }
7212
7213 /**
7214  * ipr_inquiry_page_supported - Is the given inquiry page supported
7215  * @page0:              inquiry page 0 buffer
7216  * @page:               page code.
7217  *
7218  * This function determines if the specified inquiry page is supported.
7219  *
7220  * Return value:
7221  *      1 if page is supported / 0 if not
7222  **/
7223 static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
7224 {
7225         int i;
7226
7227         for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
7228                 if (page0->page[i] == page)
7229                         return 1;
7230
7231         return 0;
7232 }
7233
7234 /**
7235  * ipr_ioafp_pageC4_inquiry - Send a Page 0xC4 Inquiry to the adapter.
7236  * @ipr_cmd:    ipr command struct
7237  *
7238  * This function sends a Page 0xC4 inquiry to the adapter
7239  * to retrieve software VPD information.
7240  *
7241  * Return value:
7242  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7243  **/
7244 static int ipr_ioafp_pageC4_inquiry(struct ipr_cmnd *ipr_cmd)
7245 {
7246         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7247         struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
7248         struct ipr_inquiry_pageC4 *pageC4 = &ioa_cfg->vpd_cbs->pageC4_data;
7249
7250         ENTER;
7251         ipr_cmd->job_step = ipr_ioafp_set_caching_parameters;
7252         memset(pageC4, 0, sizeof(*pageC4));
7253
7254         if (ipr_inquiry_page_supported(page0, 0xC4)) {
7255                 ipr_ioafp_inquiry(ipr_cmd, 1, 0xC4,
7256                                   (ioa_cfg->vpd_cbs_dma
7257                                    + offsetof(struct ipr_misc_cbs,
7258                                               pageC4_data)),
7259                                   sizeof(struct ipr_inquiry_pageC4));
7260                 return IPR_RC_JOB_RETURN;
7261         }
7262
7263         LEAVE;
7264         return IPR_RC_JOB_CONTINUE;
7265 }
7266
7267 /**
7268  * ipr_ioafp_cap_inquiry - Send a Page 0xD0 Inquiry to the adapter.
7269  * @ipr_cmd:    ipr command struct
7270  *
7271  * This function sends a Page 0xD0 inquiry to the adapter
7272  * to retrieve adapter capabilities.
7273  *
7274  * Return value:
7275  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7276  **/
7277 static int ipr_ioafp_cap_inquiry(struct ipr_cmnd *ipr_cmd)
7278 {
7279         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7280         struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
7281         struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
7282
7283         ENTER;
7284         ipr_cmd->job_step = ipr_ioafp_pageC4_inquiry;
7285         memset(cap, 0, sizeof(*cap));
7286
7287         if (ipr_inquiry_page_supported(page0, 0xD0)) {
7288                 ipr_ioafp_inquiry(ipr_cmd, 1, 0xD0,
7289                                   ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, cap),
7290                                   sizeof(struct ipr_inquiry_cap));
7291                 return IPR_RC_JOB_RETURN;
7292         }
7293
7294         LEAVE;
7295         return IPR_RC_JOB_CONTINUE;
7296 }
7297
7298 /**
7299  * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
7300  * @ipr_cmd:    ipr command struct
7301  *
7302  * This function sends a Page 3 inquiry to the adapter
7303  * to retrieve software VPD information.
7304  *
7305  * Return value:
7306  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7307  **/
7308 static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
7309 {
7310         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7311
7312         ENTER;
7313
7314         ipr_cmd->job_step = ipr_ioafp_cap_inquiry;
7315
7316         ipr_ioafp_inquiry(ipr_cmd, 1, 3,
7317                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
7318                           sizeof(struct ipr_inquiry_page3));
7319
7320         LEAVE;
7321         return IPR_RC_JOB_RETURN;
7322 }
7323
7324 /**
7325  * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
7326  * @ipr_cmd:    ipr command struct
7327  *
7328  * This function sends a Page 0 inquiry to the adapter
7329  * to retrieve supported inquiry pages.
7330  *
7331  * Return value:
7332  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7333  **/
7334 static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
7335 {
7336         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7337         char type[5];
7338
7339         ENTER;
7340
7341         /* Grab the type out of the VPD and store it away */
7342         memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
7343         type[4] = '\0';
7344         ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
7345
7346         ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
7347
7348         ipr_ioafp_inquiry(ipr_cmd, 1, 0,
7349                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
7350                           sizeof(struct ipr_inquiry_page0));
7351
7352         LEAVE;
7353         return IPR_RC_JOB_RETURN;
7354 }
7355
7356 /**
7357  * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
7358  * @ipr_cmd:    ipr command struct
7359  *
7360  * This function sends a standard inquiry to the adapter.
7361  *
7362  * Return value:
7363  *      IPR_RC_JOB_RETURN
7364  **/
7365 static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
7366 {
7367         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7368
7369         ENTER;
7370         ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
7371
7372         ipr_ioafp_inquiry(ipr_cmd, 0, 0,
7373                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
7374                           sizeof(struct ipr_ioa_vpd));
7375
7376         LEAVE;
7377         return IPR_RC_JOB_RETURN;
7378 }
7379
7380 /**
7381  * ipr_ioafp_identify_hrrq - Send Identify Host RRQ.
7382  * @ipr_cmd:    ipr command struct
7383  *
7384  * This function send an Identify Host Request Response Queue
7385  * command to establish the HRRQ with the adapter.
7386  *
7387  * Return value:
7388  *      IPR_RC_JOB_RETURN
7389  **/
7390 static int ipr_ioafp_identify_hrrq(struct ipr_cmnd *ipr_cmd)
7391 {
7392         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7393         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7394         struct ipr_hrr_queue *hrrq;
7395
7396         ENTER;
7397         ipr_cmd->job_step = ipr_ioafp_std_inquiry;
7398         if (ioa_cfg->identify_hrrq_index == 0)
7399                 dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
7400
7401         if (ioa_cfg->identify_hrrq_index < ioa_cfg->hrrq_num) {
7402                 hrrq = &ioa_cfg->hrrq[ioa_cfg->identify_hrrq_index];
7403
7404                 ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
7405                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7406
7407                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
7408                 if (ioa_cfg->sis64)
7409                         ioarcb->cmd_pkt.cdb[1] = 0x1;
7410
7411                 if (ioa_cfg->nvectors == 1)
7412                         ioarcb->cmd_pkt.cdb[1] &= ~IPR_ID_HRRQ_SELE_ENABLE;
7413                 else
7414                         ioarcb->cmd_pkt.cdb[1] |= IPR_ID_HRRQ_SELE_ENABLE;
7415
7416                 ioarcb->cmd_pkt.cdb[2] =
7417                         ((u64) hrrq->host_rrq_dma >> 24) & 0xff;
7418                 ioarcb->cmd_pkt.cdb[3] =
7419                         ((u64) hrrq->host_rrq_dma >> 16) & 0xff;
7420                 ioarcb->cmd_pkt.cdb[4] =
7421                         ((u64) hrrq->host_rrq_dma >> 8) & 0xff;
7422                 ioarcb->cmd_pkt.cdb[5] =
7423                         ((u64) hrrq->host_rrq_dma) & 0xff;
7424                 ioarcb->cmd_pkt.cdb[7] =
7425                         ((sizeof(u32) * hrrq->size) >> 8) & 0xff;
7426                 ioarcb->cmd_pkt.cdb[8] =
7427                         (sizeof(u32) * hrrq->size) & 0xff;
7428
7429                 if (ioarcb->cmd_pkt.cdb[1] & IPR_ID_HRRQ_SELE_ENABLE)
7430                         ioarcb->cmd_pkt.cdb[9] =
7431                                         ioa_cfg->identify_hrrq_index;
7432
7433                 if (ioa_cfg->sis64) {
7434                         ioarcb->cmd_pkt.cdb[10] =
7435                                 ((u64) hrrq->host_rrq_dma >> 56) & 0xff;
7436                         ioarcb->cmd_pkt.cdb[11] =
7437                                 ((u64) hrrq->host_rrq_dma >> 48) & 0xff;
7438                         ioarcb->cmd_pkt.cdb[12] =
7439                                 ((u64) hrrq->host_rrq_dma >> 40) & 0xff;
7440                         ioarcb->cmd_pkt.cdb[13] =
7441                                 ((u64) hrrq->host_rrq_dma >> 32) & 0xff;
7442                 }
7443
7444                 if (ioarcb->cmd_pkt.cdb[1] & IPR_ID_HRRQ_SELE_ENABLE)
7445                         ioarcb->cmd_pkt.cdb[14] =
7446                                         ioa_cfg->identify_hrrq_index;
7447
7448                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
7449                            IPR_INTERNAL_TIMEOUT);
7450
7451                 if (++ioa_cfg->identify_hrrq_index < ioa_cfg->hrrq_num)
7452                         ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7453
7454                 LEAVE;
7455                 return IPR_RC_JOB_RETURN;
7456         }
7457
7458         LEAVE;
7459         return IPR_RC_JOB_CONTINUE;
7460 }
7461
7462 /**
7463  * ipr_reset_timer_done - Adapter reset timer function
7464  * @t: Timer context used to fetch ipr command struct
7465  *
7466  * Description: This function is used in adapter reset processing
7467  * for timing events. If the reset_cmd pointer in the IOA
7468  * config struct is not this adapter's we are doing nested
7469  * resets and fail_all_ops will take care of freeing the
7470  * command block.
7471  *
7472  * Return value:
7473  *      none
7474  **/
7475 static void ipr_reset_timer_done(struct timer_list *t)
7476 {
7477         struct ipr_cmnd *ipr_cmd = from_timer(ipr_cmd, t, timer);
7478         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7479         unsigned long lock_flags = 0;
7480
7481         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7482
7483         if (ioa_cfg->reset_cmd == ipr_cmd) {
7484                 list_del(&ipr_cmd->queue);
7485                 ipr_cmd->done(ipr_cmd);
7486         }
7487
7488         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7489 }
7490
7491 /**
7492  * ipr_reset_start_timer - Start a timer for adapter reset job
7493  * @ipr_cmd:    ipr command struct
7494  * @timeout:    timeout value
7495  *
7496  * Description: This function is used in adapter reset processing
7497  * for timing events. If the reset_cmd pointer in the IOA
7498  * config struct is not this adapter's we are doing nested
7499  * resets and fail_all_ops will take care of freeing the
7500  * command block.
7501  *
7502  * Return value:
7503  *      none
7504  **/
7505 static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
7506                                   unsigned long timeout)
7507 {
7508
7509         ENTER;
7510         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
7511         ipr_cmd->done = ipr_reset_ioa_job;
7512
7513         ipr_cmd->timer.expires = jiffies + timeout;
7514         ipr_cmd->timer.function = ipr_reset_timer_done;
7515         add_timer(&ipr_cmd->timer);
7516 }
7517
7518 /**
7519  * ipr_init_ioa_mem - Initialize ioa_cfg control block
7520  * @ioa_cfg:    ioa cfg struct
7521  *
7522  * Return value:
7523  *      nothing
7524  **/
7525 static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
7526 {
7527         struct ipr_hrr_queue *hrrq;
7528
7529         for_each_hrrq(hrrq, ioa_cfg) {
7530                 spin_lock(&hrrq->_lock);
7531                 memset(hrrq->host_rrq, 0, sizeof(u32) * hrrq->size);
7532
7533                 /* Initialize Host RRQ pointers */
7534                 hrrq->hrrq_start = hrrq->host_rrq;
7535                 hrrq->hrrq_end = &hrrq->host_rrq[hrrq->size - 1];
7536                 hrrq->hrrq_curr = hrrq->hrrq_start;
7537                 hrrq->toggle_bit = 1;
7538                 spin_unlock(&hrrq->_lock);
7539         }
7540         wmb();
7541
7542         ioa_cfg->identify_hrrq_index = 0;
7543         if (ioa_cfg->hrrq_num == 1)
7544                 atomic_set(&ioa_cfg->hrrq_index, 0);
7545         else
7546                 atomic_set(&ioa_cfg->hrrq_index, 1);
7547
7548         /* Zero out config table */
7549         memset(ioa_cfg->u.cfg_table, 0, ioa_cfg->cfg_table_size);
7550 }
7551
7552 /**
7553  * ipr_reset_next_stage - Process IPL stage change based on feedback register.
7554  * @ipr_cmd:    ipr command struct
7555  *
7556  * Return value:
7557  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7558  **/
7559 static int ipr_reset_next_stage(struct ipr_cmnd *ipr_cmd)
7560 {
7561         unsigned long stage, stage_time;
7562         u32 feedback;
7563         volatile u32 int_reg;
7564         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7565         u64 maskval = 0;
7566
7567         feedback = readl(ioa_cfg->regs.init_feedback_reg);
7568         stage = feedback & IPR_IPL_INIT_STAGE_MASK;
7569         stage_time = feedback & IPR_IPL_INIT_STAGE_TIME_MASK;
7570
7571         ipr_dbg("IPL stage = 0x%lx, IPL stage time = %ld\n", stage, stage_time);
7572
7573         /* sanity check the stage_time value */
7574         if (stage_time == 0)
7575                 stage_time = IPR_IPL_INIT_DEFAULT_STAGE_TIME;
7576         else if (stage_time < IPR_IPL_INIT_MIN_STAGE_TIME)
7577                 stage_time = IPR_IPL_INIT_MIN_STAGE_TIME;
7578         else if (stage_time > IPR_LONG_OPERATIONAL_TIMEOUT)
7579                 stage_time = IPR_LONG_OPERATIONAL_TIMEOUT;
7580
7581         if (stage == IPR_IPL_INIT_STAGE_UNKNOWN) {
7582                 writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.set_interrupt_mask_reg);
7583                 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7584                 stage_time = ioa_cfg->transop_timeout;
7585                 ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7586         } else if (stage == IPR_IPL_INIT_STAGE_TRANSOP) {
7587                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
7588                 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
7589                         ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7590                         maskval = IPR_PCII_IPL_STAGE_CHANGE;
7591                         maskval = (maskval << 32) | IPR_PCII_IOA_TRANS_TO_OPER;
7592                         writeq(maskval, ioa_cfg->regs.set_interrupt_mask_reg);
7593                         int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7594                         return IPR_RC_JOB_CONTINUE;
7595                 }
7596         }
7597
7598         ipr_cmd->timer.expires = jiffies + stage_time * HZ;
7599         ipr_cmd->timer.function = ipr_oper_timeout;
7600         ipr_cmd->done = ipr_reset_ioa_job;
7601         add_timer(&ipr_cmd->timer);
7602
7603         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
7604
7605         return IPR_RC_JOB_RETURN;
7606 }
7607
7608 /**
7609  * ipr_reset_enable_ioa - Enable the IOA following a reset.
7610  * @ipr_cmd:    ipr command struct
7611  *
7612  * This function reinitializes some control blocks and
7613  * enables destructive diagnostics on the adapter.
7614  *
7615  * Return value:
7616  *      IPR_RC_JOB_RETURN
7617  **/
7618 static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
7619 {
7620         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7621         volatile u32 int_reg;
7622         volatile u64 maskval;
7623         int i;
7624
7625         ENTER;
7626         ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7627         ipr_init_ioa_mem(ioa_cfg);
7628
7629         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
7630                 spin_lock(&ioa_cfg->hrrq[i]._lock);
7631                 ioa_cfg->hrrq[i].allow_interrupts = 1;
7632                 spin_unlock(&ioa_cfg->hrrq[i]._lock);
7633         }
7634         if (ioa_cfg->sis64) {
7635                 /* Set the adapter to the correct endian mode. */
7636                 writel(IPR_ENDIAN_SWAP_KEY, ioa_cfg->regs.endian_swap_reg);
7637                 int_reg = readl(ioa_cfg->regs.endian_swap_reg);
7638         }
7639
7640         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
7641
7642         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
7643                 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
7644                        ioa_cfg->regs.clr_interrupt_mask_reg32);
7645                 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7646                 return IPR_RC_JOB_CONTINUE;
7647         }
7648
7649         /* Enable destructive diagnostics on IOA */
7650         writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg32);
7651
7652         if (ioa_cfg->sis64) {
7653                 maskval = IPR_PCII_IPL_STAGE_CHANGE;
7654                 maskval = (maskval << 32) | IPR_PCII_OPER_INTERRUPTS;
7655                 writeq(maskval, ioa_cfg->regs.clr_interrupt_mask_reg);
7656         } else
7657                 writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg32);
7658
7659         int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7660
7661         dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
7662
7663         if (ioa_cfg->sis64) {
7664                 ipr_cmd->job_step = ipr_reset_next_stage;
7665                 return IPR_RC_JOB_CONTINUE;
7666         }
7667
7668         ipr_cmd->timer.expires = jiffies + (ioa_cfg->transop_timeout * HZ);
7669         ipr_cmd->timer.function = ipr_oper_timeout;
7670         ipr_cmd->done = ipr_reset_ioa_job;
7671         add_timer(&ipr_cmd->timer);
7672         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
7673
7674         LEAVE;
7675         return IPR_RC_JOB_RETURN;
7676 }
7677
7678 /**
7679  * ipr_reset_wait_for_dump - Wait for a dump to timeout.
7680  * @ipr_cmd:    ipr command struct
7681  *
7682  * This function is invoked when an adapter dump has run out
7683  * of processing time.
7684  *
7685  * Return value:
7686  *      IPR_RC_JOB_CONTINUE
7687  **/
7688 static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
7689 {
7690         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7691
7692         if (ioa_cfg->sdt_state == GET_DUMP)
7693                 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
7694         else if (ioa_cfg->sdt_state == READ_DUMP)
7695                 ioa_cfg->sdt_state = ABORT_DUMP;
7696
7697         ioa_cfg->dump_timeout = 1;
7698         ipr_cmd->job_step = ipr_reset_alert;
7699
7700         return IPR_RC_JOB_CONTINUE;
7701 }
7702
7703 /**
7704  * ipr_unit_check_no_data - Log a unit check/no data error log
7705  * @ioa_cfg:            ioa config struct
7706  *
7707  * Logs an error indicating the adapter unit checked, but for some
7708  * reason, we were unable to fetch the unit check buffer.
7709  *
7710  * Return value:
7711  *      nothing
7712  **/
7713 static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
7714 {
7715         ioa_cfg->errors_logged++;
7716         dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
7717 }
7718
7719 /**
7720  * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
7721  * @ioa_cfg:            ioa config struct
7722  *
7723  * Fetches the unit check buffer from the adapter by clocking the data
7724  * through the mailbox register.
7725  *
7726  * Return value:
7727  *      nothing
7728  **/
7729 static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
7730 {
7731         unsigned long mailbox;
7732         struct ipr_hostrcb *hostrcb;
7733         struct ipr_uc_sdt sdt;
7734         int rc, length;
7735         u32 ioasc;
7736
7737         mailbox = readl(ioa_cfg->ioa_mailbox);
7738
7739         if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(mailbox)) {
7740                 ipr_unit_check_no_data(ioa_cfg);
7741                 return;
7742         }
7743
7744         memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
7745         rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
7746                                         (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
7747
7748         if (rc || !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY) ||
7749             ((be32_to_cpu(sdt.hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
7750             (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
7751                 ipr_unit_check_no_data(ioa_cfg);
7752                 return;
7753         }
7754
7755         /* Find length of the first sdt entry (UC buffer) */
7756         if (be32_to_cpu(sdt.hdr.state) == IPR_FMT3_SDT_READY_TO_USE)
7757                 length = be32_to_cpu(sdt.entry[0].end_token);
7758         else
7759                 length = (be32_to_cpu(sdt.entry[0].end_token) -
7760                           be32_to_cpu(sdt.entry[0].start_token)) &
7761                           IPR_FMT2_MBX_ADDR_MASK;
7762
7763         hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
7764                              struct ipr_hostrcb, queue);
7765         list_del_init(&hostrcb->queue);
7766         memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
7767
7768         rc = ipr_get_ldump_data_section(ioa_cfg,
7769                                         be32_to_cpu(sdt.entry[0].start_token),
7770                                         (__be32 *)&hostrcb->hcam,
7771                                         min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
7772
7773         if (!rc) {
7774                 ipr_handle_log_data(ioa_cfg, hostrcb);
7775                 ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
7776                 if (ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED &&
7777                     ioa_cfg->sdt_state == GET_DUMP)
7778                         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
7779         } else
7780                 ipr_unit_check_no_data(ioa_cfg);
7781
7782         list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
7783 }
7784
7785 /**
7786  * ipr_reset_get_unit_check_job - Call to get the unit check buffer.
7787  * @ipr_cmd:    ipr command struct
7788  *
7789  * Description: This function will call to get the unit check buffer.
7790  *
7791  * Return value:
7792  *      IPR_RC_JOB_RETURN
7793  **/
7794 static int ipr_reset_get_unit_check_job(struct ipr_cmnd *ipr_cmd)
7795 {
7796         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7797
7798         ENTER;
7799         ioa_cfg->ioa_unit_checked = 0;
7800         ipr_get_unit_check_buffer(ioa_cfg);
7801         ipr_cmd->job_step = ipr_reset_alert;
7802         ipr_reset_start_timer(ipr_cmd, 0);
7803
7804         LEAVE;
7805         return IPR_RC_JOB_RETURN;
7806 }
7807
7808 static int ipr_dump_mailbox_wait(struct ipr_cmnd *ipr_cmd)
7809 {
7810         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7811
7812         ENTER;
7813
7814         if (ioa_cfg->sdt_state != GET_DUMP)
7815                 return IPR_RC_JOB_RETURN;
7816
7817         if (!ioa_cfg->sis64 || !ipr_cmd->u.time_left ||
7818             (readl(ioa_cfg->regs.sense_interrupt_reg) &
7819              IPR_PCII_MAILBOX_STABLE)) {
7820
7821                 if (!ipr_cmd->u.time_left)
7822                         dev_err(&ioa_cfg->pdev->dev,
7823                                 "Timed out waiting for Mailbox register.\n");
7824
7825                 ioa_cfg->sdt_state = READ_DUMP;
7826                 ioa_cfg->dump_timeout = 0;
7827                 if (ioa_cfg->sis64)
7828                         ipr_reset_start_timer(ipr_cmd, IPR_SIS64_DUMP_TIMEOUT);
7829                 else
7830                         ipr_reset_start_timer(ipr_cmd, IPR_SIS32_DUMP_TIMEOUT);
7831                 ipr_cmd->job_step = ipr_reset_wait_for_dump;
7832                 schedule_work(&ioa_cfg->work_q);
7833
7834         } else {
7835                 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
7836                 ipr_reset_start_timer(ipr_cmd,
7837                                       IPR_CHECK_FOR_RESET_TIMEOUT);
7838         }
7839
7840         LEAVE;
7841         return IPR_RC_JOB_RETURN;
7842 }
7843
7844 /**
7845  * ipr_reset_restore_cfg_space - Restore PCI config space.
7846  * @ipr_cmd:    ipr command struct
7847  *
7848  * Description: This function restores the saved PCI config space of
7849  * the adapter, fails all outstanding ops back to the callers, and
7850  * fetches the dump/unit check if applicable to this reset.
7851  *
7852  * Return value:
7853  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7854  **/
7855 static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
7856 {
7857         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7858
7859         ENTER;
7860         ioa_cfg->pdev->state_saved = true;
7861         pci_restore_state(ioa_cfg->pdev);
7862
7863         if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
7864                 ipr_cmd->s.ioasa.hdr.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
7865                 return IPR_RC_JOB_CONTINUE;
7866         }
7867
7868         ipr_fail_all_ops(ioa_cfg);
7869
7870         if (ioa_cfg->sis64) {
7871                 /* Set the adapter to the correct endian mode. */
7872                 writel(IPR_ENDIAN_SWAP_KEY, ioa_cfg->regs.endian_swap_reg);
7873                 readl(ioa_cfg->regs.endian_swap_reg);
7874         }
7875
7876         if (ioa_cfg->ioa_unit_checked) {
7877                 if (ioa_cfg->sis64) {
7878                         ipr_cmd->job_step = ipr_reset_get_unit_check_job;
7879                         ipr_reset_start_timer(ipr_cmd, IPR_DUMP_DELAY_TIMEOUT);
7880                         return IPR_RC_JOB_RETURN;
7881                 } else {
7882                         ioa_cfg->ioa_unit_checked = 0;
7883                         ipr_get_unit_check_buffer(ioa_cfg);
7884                         ipr_cmd->job_step = ipr_reset_alert;
7885                         ipr_reset_start_timer(ipr_cmd, 0);
7886                         return IPR_RC_JOB_RETURN;
7887                 }
7888         }
7889
7890         if (ioa_cfg->in_ioa_bringdown) {
7891                 ipr_cmd->job_step = ipr_ioa_bringdown_done;
7892         } else if (ioa_cfg->sdt_state == GET_DUMP) {
7893                 ipr_cmd->job_step = ipr_dump_mailbox_wait;
7894                 ipr_cmd->u.time_left = IPR_WAIT_FOR_MAILBOX;
7895         } else {
7896                 ipr_cmd->job_step = ipr_reset_enable_ioa;
7897         }
7898
7899         LEAVE;
7900         return IPR_RC_JOB_CONTINUE;
7901 }
7902
7903 /**
7904  * ipr_reset_bist_done - BIST has completed on the adapter.
7905  * @ipr_cmd:    ipr command struct
7906  *
7907  * Description: Unblock config space and resume the reset process.
7908  *
7909  * Return value:
7910  *      IPR_RC_JOB_CONTINUE
7911  **/
7912 static int ipr_reset_bist_done(struct ipr_cmnd *ipr_cmd)
7913 {
7914         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7915
7916         ENTER;
7917         if (ioa_cfg->cfg_locked)
7918                 pci_cfg_access_unlock(ioa_cfg->pdev);
7919         ioa_cfg->cfg_locked = 0;
7920         ipr_cmd->job_step = ipr_reset_restore_cfg_space;
7921         LEAVE;
7922         return IPR_RC_JOB_CONTINUE;
7923 }
7924
7925 /**
7926  * ipr_reset_start_bist - Run BIST on the adapter.
7927  * @ipr_cmd:    ipr command struct
7928  *
7929  * Description: This function runs BIST on the adapter, then delays 2 seconds.
7930  *
7931  * Return value:
7932  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7933  **/
7934 static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
7935 {
7936         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7937         int rc = PCIBIOS_SUCCESSFUL;
7938
7939         ENTER;
7940         if (ioa_cfg->ipr_chip->bist_method == IPR_MMIO)
7941                 writel(IPR_UPROCI_SIS64_START_BIST,
7942                        ioa_cfg->regs.set_uproc_interrupt_reg32);
7943         else
7944                 rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
7945
7946         if (rc == PCIBIOS_SUCCESSFUL) {
7947                 ipr_cmd->job_step = ipr_reset_bist_done;
7948                 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
7949                 rc = IPR_RC_JOB_RETURN;
7950         } else {
7951                 if (ioa_cfg->cfg_locked)
7952                         pci_cfg_access_unlock(ipr_cmd->ioa_cfg->pdev);
7953                 ioa_cfg->cfg_locked = 0;
7954                 ipr_cmd->s.ioasa.hdr.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
7955                 rc = IPR_RC_JOB_CONTINUE;
7956         }
7957
7958         LEAVE;
7959         return rc;
7960 }
7961
7962 /**
7963  * ipr_reset_slot_reset_done - Clear PCI reset to the adapter
7964  * @ipr_cmd:    ipr command struct
7965  *
7966  * Description: This clears PCI reset to the adapter and delays two seconds.
7967  *
7968  * Return value:
7969  *      IPR_RC_JOB_RETURN
7970  **/
7971 static int ipr_reset_slot_reset_done(struct ipr_cmnd *ipr_cmd)
7972 {
7973         ENTER;
7974         ipr_cmd->job_step = ipr_reset_bist_done;
7975         ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
7976         LEAVE;
7977         return IPR_RC_JOB_RETURN;
7978 }
7979
7980 /**
7981  * ipr_reset_reset_work - Pulse a PCIe fundamental reset
7982  * @work:       work struct
7983  *
7984  * Description: This pulses warm reset to a slot.
7985  *
7986  **/
7987 static void ipr_reset_reset_work(struct work_struct *work)
7988 {
7989         struct ipr_cmnd *ipr_cmd = container_of(work, struct ipr_cmnd, work);
7990         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7991         struct pci_dev *pdev = ioa_cfg->pdev;
7992         unsigned long lock_flags = 0;
7993
7994         ENTER;
7995         pci_set_pcie_reset_state(pdev, pcie_warm_reset);
7996         msleep(jiffies_to_msecs(IPR_PCI_RESET_TIMEOUT));
7997         pci_set_pcie_reset_state(pdev, pcie_deassert_reset);
7998
7999         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8000         if (ioa_cfg->reset_cmd == ipr_cmd)
8001                 ipr_reset_ioa_job(ipr_cmd);
8002         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8003         LEAVE;
8004 }
8005
8006 /**
8007  * ipr_reset_slot_reset - Reset the PCI slot of the adapter.
8008  * @ipr_cmd:    ipr command struct
8009  *
8010  * Description: This asserts PCI reset to the adapter.
8011  *
8012  * Return value:
8013  *      IPR_RC_JOB_RETURN
8014  **/
8015 static int ipr_reset_slot_reset(struct ipr_cmnd *ipr_cmd)
8016 {
8017         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8018
8019         ENTER;
8020         INIT_WORK(&ipr_cmd->work, ipr_reset_reset_work);
8021         queue_work(ioa_cfg->reset_work_q, &ipr_cmd->work);
8022         ipr_cmd->job_step = ipr_reset_slot_reset_done;
8023         LEAVE;
8024         return IPR_RC_JOB_RETURN;
8025 }
8026
8027 /**
8028  * ipr_reset_block_config_access_wait - Wait for permission to block config access
8029  * @ipr_cmd:    ipr command struct
8030  *
8031  * Description: This attempts to block config access to the IOA.
8032  *
8033  * Return value:
8034  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8035  **/
8036 static int ipr_reset_block_config_access_wait(struct ipr_cmnd *ipr_cmd)
8037 {
8038         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8039         int rc = IPR_RC_JOB_CONTINUE;
8040
8041         if (pci_cfg_access_trylock(ioa_cfg->pdev)) {
8042                 ioa_cfg->cfg_locked = 1;
8043                 ipr_cmd->job_step = ioa_cfg->reset;
8044         } else {
8045                 if (ipr_cmd->u.time_left) {
8046                         rc = IPR_RC_JOB_RETURN;
8047                         ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
8048                         ipr_reset_start_timer(ipr_cmd,
8049                                               IPR_CHECK_FOR_RESET_TIMEOUT);
8050                 } else {
8051                         ipr_cmd->job_step = ioa_cfg->reset;
8052                         dev_err(&ioa_cfg->pdev->dev,
8053                                 "Timed out waiting to lock config access. Resetting anyway.\n");
8054                 }
8055         }
8056
8057         return rc;
8058 }
8059
8060 /**
8061  * ipr_reset_block_config_access - Block config access to the IOA
8062  * @ipr_cmd:    ipr command struct
8063  *
8064  * Description: This attempts to block config access to the IOA
8065  *
8066  * Return value:
8067  *      IPR_RC_JOB_CONTINUE
8068  **/
8069 static int ipr_reset_block_config_access(struct ipr_cmnd *ipr_cmd)
8070 {
8071         ipr_cmd->ioa_cfg->cfg_locked = 0;
8072         ipr_cmd->job_step = ipr_reset_block_config_access_wait;
8073         ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
8074         return IPR_RC_JOB_CONTINUE;
8075 }
8076
8077 /**
8078  * ipr_reset_allowed - Query whether or not IOA can be reset
8079  * @ioa_cfg:    ioa config struct
8080  *
8081  * Return value:
8082  *      0 if reset not allowed / non-zero if reset is allowed
8083  **/
8084 static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
8085 {
8086         volatile u32 temp_reg;
8087
8088         temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
8089         return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
8090 }
8091
8092 /**
8093  * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
8094  * @ipr_cmd:    ipr command struct
8095  *
8096  * Description: This function waits for adapter permission to run BIST,
8097  * then runs BIST. If the adapter does not give permission after a
8098  * reasonable time, we will reset the adapter anyway. The impact of
8099  * resetting the adapter without warning the adapter is the risk of
8100  * losing the persistent error log on the adapter. If the adapter is
8101  * reset while it is writing to the flash on the adapter, the flash
8102  * segment will have bad ECC and be zeroed.
8103  *
8104  * Return value:
8105  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8106  **/
8107 static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
8108 {
8109         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8110         int rc = IPR_RC_JOB_RETURN;
8111
8112         if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
8113                 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
8114                 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
8115         } else {
8116                 ipr_cmd->job_step = ipr_reset_block_config_access;
8117                 rc = IPR_RC_JOB_CONTINUE;
8118         }
8119
8120         return rc;
8121 }
8122
8123 /**
8124  * ipr_reset_alert - Alert the adapter of a pending reset
8125  * @ipr_cmd:    ipr command struct
8126  *
8127  * Description: This function alerts the adapter that it will be reset.
8128  * If memory space is not currently enabled, proceed directly
8129  * to running BIST on the adapter. The timer must always be started
8130  * so we guarantee we do not run BIST from ipr_isr.
8131  *
8132  * Return value:
8133  *      IPR_RC_JOB_RETURN
8134  **/
8135 static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
8136 {
8137         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8138         u16 cmd_reg;
8139         int rc;
8140
8141         ENTER;
8142         rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
8143
8144         if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
8145                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
8146                 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg32);
8147                 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
8148         } else {
8149                 ipr_cmd->job_step = ipr_reset_block_config_access;
8150         }
8151
8152         ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
8153         ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
8154
8155         LEAVE;
8156         return IPR_RC_JOB_RETURN;
8157 }
8158
8159 /**
8160  * ipr_reset_quiesce_done - Complete IOA disconnect
8161  * @ipr_cmd:    ipr command struct
8162  *
8163  * Description: Freeze the adapter to complete quiesce processing
8164  *
8165  * Return value:
8166  *      IPR_RC_JOB_CONTINUE
8167  **/
8168 static int ipr_reset_quiesce_done(struct ipr_cmnd *ipr_cmd)
8169 {
8170         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8171
8172         ENTER;
8173         ipr_cmd->job_step = ipr_ioa_bringdown_done;
8174         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
8175         LEAVE;
8176         return IPR_RC_JOB_CONTINUE;
8177 }
8178
8179 /**
8180  * ipr_reset_cancel_hcam_done - Check for outstanding commands
8181  * @ipr_cmd:    ipr command struct
8182  *
8183  * Description: Ensure nothing is outstanding to the IOA and
8184  *                      proceed with IOA disconnect. Otherwise reset the IOA.
8185  *
8186  * Return value:
8187  *      IPR_RC_JOB_RETURN / IPR_RC_JOB_CONTINUE
8188  **/
8189 static int ipr_reset_cancel_hcam_done(struct ipr_cmnd *ipr_cmd)
8190 {
8191         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8192         struct ipr_cmnd *loop_cmd;
8193         struct ipr_hrr_queue *hrrq;
8194         int rc = IPR_RC_JOB_CONTINUE;
8195         int count = 0;
8196
8197         ENTER;
8198         ipr_cmd->job_step = ipr_reset_quiesce_done;
8199
8200         for_each_hrrq(hrrq, ioa_cfg) {
8201                 spin_lock(&hrrq->_lock);
8202                 list_for_each_entry(loop_cmd, &hrrq->hrrq_pending_q, queue) {
8203                         count++;
8204                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8205                         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
8206                         rc = IPR_RC_JOB_RETURN;
8207                         break;
8208                 }
8209                 spin_unlock(&hrrq->_lock);
8210
8211                 if (count)
8212                         break;
8213         }
8214
8215         LEAVE;
8216         return rc;
8217 }
8218
8219 /**
8220  * ipr_reset_cancel_hcam - Cancel outstanding HCAMs
8221  * @ipr_cmd:    ipr command struct
8222  *
8223  * Description: Cancel any oustanding HCAMs to the IOA.
8224  *
8225  * Return value:
8226  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8227  **/
8228 static int ipr_reset_cancel_hcam(struct ipr_cmnd *ipr_cmd)
8229 {
8230         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8231         int rc = IPR_RC_JOB_CONTINUE;
8232         struct ipr_cmd_pkt *cmd_pkt;
8233         struct ipr_cmnd *hcam_cmd;
8234         struct ipr_hrr_queue *hrrq = &ioa_cfg->hrrq[IPR_INIT_HRRQ];
8235
8236         ENTER;
8237         ipr_cmd->job_step = ipr_reset_cancel_hcam_done;
8238
8239         if (!hrrq->ioa_is_dead) {
8240                 if (!list_empty(&ioa_cfg->hostrcb_pending_q)) {
8241                         list_for_each_entry(hcam_cmd, &hrrq->hrrq_pending_q, queue) {
8242                                 if (hcam_cmd->ioarcb.cmd_pkt.cdb[0] != IPR_HOST_CONTROLLED_ASYNC)
8243                                         continue;
8244
8245                                 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
8246                                 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
8247                                 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
8248                                 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
8249                                 cmd_pkt->cdb[0] = IPR_CANCEL_REQUEST;
8250                                 cmd_pkt->cdb[1] = IPR_CANCEL_64BIT_IOARCB;
8251                                 cmd_pkt->cdb[10] = ((u64) hcam_cmd->dma_addr >> 56) & 0xff;
8252                                 cmd_pkt->cdb[11] = ((u64) hcam_cmd->dma_addr >> 48) & 0xff;
8253                                 cmd_pkt->cdb[12] = ((u64) hcam_cmd->dma_addr >> 40) & 0xff;
8254                                 cmd_pkt->cdb[13] = ((u64) hcam_cmd->dma_addr >> 32) & 0xff;
8255                                 cmd_pkt->cdb[2] = ((u64) hcam_cmd->dma_addr >> 24) & 0xff;
8256                                 cmd_pkt->cdb[3] = ((u64) hcam_cmd->dma_addr >> 16) & 0xff;
8257                                 cmd_pkt->cdb[4] = ((u64) hcam_cmd->dma_addr >> 8) & 0xff;
8258                                 cmd_pkt->cdb[5] = ((u64) hcam_cmd->dma_addr) & 0xff;
8259
8260                                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
8261                                            IPR_CANCEL_TIMEOUT);
8262
8263                                 rc = IPR_RC_JOB_RETURN;
8264                                 ipr_cmd->job_step = ipr_reset_cancel_hcam;
8265                                 break;
8266                         }
8267                 }
8268         } else
8269                 ipr_cmd->job_step = ipr_reset_alert;
8270
8271         LEAVE;
8272         return rc;
8273 }
8274
8275 /**
8276  * ipr_reset_ucode_download_done - Microcode download completion
8277  * @ipr_cmd:    ipr command struct
8278  *
8279  * Description: This function unmaps the microcode download buffer.
8280  *
8281  * Return value:
8282  *      IPR_RC_JOB_CONTINUE
8283  **/
8284 static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
8285 {
8286         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8287         struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
8288
8289         dma_unmap_sg(&ioa_cfg->pdev->dev, sglist->scatterlist,
8290                      sglist->num_sg, DMA_TO_DEVICE);
8291
8292         ipr_cmd->job_step = ipr_reset_alert;
8293         return IPR_RC_JOB_CONTINUE;
8294 }
8295
8296 /**
8297  * ipr_reset_ucode_download - Download microcode to the adapter
8298  * @ipr_cmd:    ipr command struct
8299  *
8300  * Description: This function checks to see if it there is microcode
8301  * to download to the adapter. If there is, a download is performed.
8302  *
8303  * Return value:
8304  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8305  **/
8306 static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
8307 {
8308         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8309         struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
8310
8311         ENTER;
8312         ipr_cmd->job_step = ipr_reset_alert;
8313
8314         if (!sglist)
8315                 return IPR_RC_JOB_CONTINUE;
8316
8317         ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
8318         ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
8319         ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
8320         ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
8321         ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
8322         ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
8323         ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
8324
8325         if (ioa_cfg->sis64)
8326                 ipr_build_ucode_ioadl64(ipr_cmd, sglist);
8327         else
8328                 ipr_build_ucode_ioadl(ipr_cmd, sglist);
8329         ipr_cmd->job_step = ipr_reset_ucode_download_done;
8330
8331         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
8332                    IPR_WRITE_BUFFER_TIMEOUT);
8333
8334         LEAVE;
8335         return IPR_RC_JOB_RETURN;
8336 }
8337
8338 /**
8339  * ipr_reset_shutdown_ioa - Shutdown the adapter
8340  * @ipr_cmd:    ipr command struct
8341  *
8342  * Description: This function issues an adapter shutdown of the
8343  * specified type to the specified adapter as part of the
8344  * adapter reset job.
8345  *
8346  * Return value:
8347  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8348  **/
8349 static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
8350 {
8351         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8352         enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
8353         unsigned long timeout;
8354         int rc = IPR_RC_JOB_CONTINUE;
8355
8356         ENTER;
8357         if (shutdown_type == IPR_SHUTDOWN_QUIESCE)
8358                 ipr_cmd->job_step = ipr_reset_cancel_hcam;
8359         else if (shutdown_type != IPR_SHUTDOWN_NONE &&
8360                         !ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
8361                 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
8362                 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
8363                 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
8364                 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
8365
8366                 if (shutdown_type == IPR_SHUTDOWN_NORMAL)
8367                         timeout = IPR_SHUTDOWN_TIMEOUT;
8368                 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
8369                         timeout = IPR_INTERNAL_TIMEOUT;
8370                 else if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
8371                         timeout = IPR_DUAL_IOA_ABBR_SHUTDOWN_TO;
8372                 else
8373                         timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
8374
8375                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
8376
8377                 rc = IPR_RC_JOB_RETURN;
8378                 ipr_cmd->job_step = ipr_reset_ucode_download;
8379         } else
8380                 ipr_cmd->job_step = ipr_reset_alert;
8381
8382         LEAVE;
8383         return rc;
8384 }
8385
8386 /**
8387  * ipr_reset_ioa_job - Adapter reset job
8388  * @ipr_cmd:    ipr command struct
8389  *
8390  * Description: This function is the job router for the adapter reset job.
8391  *
8392  * Return value:
8393  *      none
8394  **/
8395 static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
8396 {
8397         u32 rc, ioasc;
8398         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8399
8400         do {
8401                 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
8402
8403                 if (ioa_cfg->reset_cmd != ipr_cmd) {
8404                         /*
8405                          * We are doing nested adapter resets and this is
8406                          * not the current reset job.
8407                          */
8408                         list_add_tail(&ipr_cmd->queue,
8409                                         &ipr_cmd->hrrq->hrrq_free_q);
8410                         return;
8411                 }
8412
8413                 if (IPR_IOASC_SENSE_KEY(ioasc)) {
8414                         rc = ipr_cmd->job_step_failed(ipr_cmd);
8415                         if (rc == IPR_RC_JOB_RETURN)
8416                                 return;
8417                 }
8418
8419                 ipr_reinit_ipr_cmnd(ipr_cmd);
8420                 ipr_cmd->job_step_failed = ipr_reset_cmd_failed;
8421                 rc = ipr_cmd->job_step(ipr_cmd);
8422         } while (rc == IPR_RC_JOB_CONTINUE);
8423 }
8424
8425 /**
8426  * _ipr_initiate_ioa_reset - Initiate an adapter reset
8427  * @ioa_cfg:            ioa config struct
8428  * @job_step:           first job step of reset job
8429  * @shutdown_type:      shutdown type
8430  *
8431  * Description: This function will initiate the reset of the given adapter
8432  * starting at the selected job step.
8433  * If the caller needs to wait on the completion of the reset,
8434  * the caller must sleep on the reset_wait_q.
8435  *
8436  * Return value:
8437  *      none
8438  **/
8439 static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
8440                                     int (*job_step) (struct ipr_cmnd *),
8441                                     enum ipr_shutdown_type shutdown_type)
8442 {
8443         struct ipr_cmnd *ipr_cmd;
8444         int i;
8445
8446         ioa_cfg->in_reset_reload = 1;
8447         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8448                 spin_lock(&ioa_cfg->hrrq[i]._lock);
8449                 ioa_cfg->hrrq[i].allow_cmds = 0;
8450                 spin_unlock(&ioa_cfg->hrrq[i]._lock);
8451         }
8452         wmb();
8453         if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].removing_ioa) {
8454                 ioa_cfg->scsi_unblock = 0;
8455                 ioa_cfg->scsi_blocked = 1;
8456                 scsi_block_requests(ioa_cfg->host);
8457         }
8458
8459         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
8460         ioa_cfg->reset_cmd = ipr_cmd;
8461         ipr_cmd->job_step = job_step;
8462         ipr_cmd->u.shutdown_type = shutdown_type;
8463
8464         ipr_reset_ioa_job(ipr_cmd);
8465 }
8466
8467 /**
8468  * ipr_initiate_ioa_reset - Initiate an adapter reset
8469  * @ioa_cfg:            ioa config struct
8470  * @shutdown_type:      shutdown type
8471  *
8472  * Description: This function will initiate the reset of the given adapter.
8473  * If the caller needs to wait on the completion of the reset,
8474  * the caller must sleep on the reset_wait_q.
8475  *
8476  * Return value:
8477  *      none
8478  **/
8479 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
8480                                    enum ipr_shutdown_type shutdown_type)
8481 {
8482         int i;
8483
8484         if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
8485                 return;
8486
8487         if (ioa_cfg->in_reset_reload) {
8488                 if (ioa_cfg->sdt_state == GET_DUMP)
8489                         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
8490                 else if (ioa_cfg->sdt_state == READ_DUMP)
8491                         ioa_cfg->sdt_state = ABORT_DUMP;
8492         }
8493
8494         if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
8495                 dev_err(&ioa_cfg->pdev->dev,
8496                         "IOA taken offline - error recovery failed\n");
8497
8498                 ioa_cfg->reset_retries = 0;
8499                 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8500                         spin_lock(&ioa_cfg->hrrq[i]._lock);
8501                         ioa_cfg->hrrq[i].ioa_is_dead = 1;
8502                         spin_unlock(&ioa_cfg->hrrq[i]._lock);
8503                 }
8504                 wmb();
8505
8506                 if (ioa_cfg->in_ioa_bringdown) {
8507                         ioa_cfg->reset_cmd = NULL;
8508                         ioa_cfg->in_reset_reload = 0;
8509                         ipr_fail_all_ops(ioa_cfg);
8510                         wake_up_all(&ioa_cfg->reset_wait_q);
8511
8512                         if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].removing_ioa) {
8513                                 ioa_cfg->scsi_unblock = 1;
8514                                 schedule_work(&ioa_cfg->work_q);
8515                         }
8516                         return;
8517                 } else {
8518                         ioa_cfg->in_ioa_bringdown = 1;
8519                         shutdown_type = IPR_SHUTDOWN_NONE;
8520                 }
8521         }
8522
8523         _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
8524                                 shutdown_type);
8525 }
8526
8527 /**
8528  * ipr_reset_freeze - Hold off all I/O activity
8529  * @ipr_cmd:    ipr command struct
8530  *
8531  * Description: If the PCI slot is frozen, hold off all I/O
8532  * activity; then, as soon as the slot is available again,
8533  * initiate an adapter reset.
8534  */
8535 static int ipr_reset_freeze(struct ipr_cmnd *ipr_cmd)
8536 {
8537         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8538         int i;
8539
8540         /* Disallow new interrupts, avoid loop */
8541         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8542                 spin_lock(&ioa_cfg->hrrq[i]._lock);
8543                 ioa_cfg->hrrq[i].allow_interrupts = 0;
8544                 spin_unlock(&ioa_cfg->hrrq[i]._lock);
8545         }
8546         wmb();
8547         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
8548         ipr_cmd->done = ipr_reset_ioa_job;
8549         return IPR_RC_JOB_RETURN;
8550 }
8551
8552 /**
8553  * ipr_pci_mmio_enabled - Called when MMIO has been re-enabled
8554  * @pdev:       PCI device struct
8555  *
8556  * Description: This routine is called to tell us that the MMIO
8557  * access to the IOA has been restored
8558  */
8559 static pci_ers_result_t ipr_pci_mmio_enabled(struct pci_dev *pdev)
8560 {
8561         unsigned long flags = 0;
8562         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8563
8564         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
8565         if (!ioa_cfg->probe_done)
8566                 pci_save_state(pdev);
8567         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8568         return PCI_ERS_RESULT_NEED_RESET;
8569 }
8570
8571 /**
8572  * ipr_pci_frozen - Called when slot has experienced a PCI bus error.
8573  * @pdev:       PCI device struct
8574  *
8575  * Description: This routine is called to tell us that the PCI bus
8576  * is down. Can't do anything here, except put the device driver
8577  * into a holding pattern, waiting for the PCI bus to come back.
8578  */
8579 static void ipr_pci_frozen(struct pci_dev *pdev)
8580 {
8581         unsigned long flags = 0;
8582         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8583
8584         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
8585         if (ioa_cfg->probe_done)
8586                 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_freeze, IPR_SHUTDOWN_NONE);
8587         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8588 }
8589
8590 /**
8591  * ipr_pci_slot_reset - Called when PCI slot has been reset.
8592  * @pdev:       PCI device struct
8593  *
8594  * Description: This routine is called by the pci error recovery
8595  * code after the PCI slot has been reset, just before we
8596  * should resume normal operations.
8597  */
8598 static pci_ers_result_t ipr_pci_slot_reset(struct pci_dev *pdev)
8599 {
8600         unsigned long flags = 0;
8601         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8602
8603         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
8604         if (ioa_cfg->probe_done) {
8605                 if (ioa_cfg->needs_warm_reset)
8606                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8607                 else
8608                         _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_restore_cfg_space,
8609                                                 IPR_SHUTDOWN_NONE);
8610         } else
8611                 wake_up_all(&ioa_cfg->eeh_wait_q);
8612         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8613         return PCI_ERS_RESULT_RECOVERED;
8614 }
8615
8616 /**
8617  * ipr_pci_perm_failure - Called when PCI slot is dead for good.
8618  * @pdev:       PCI device struct
8619  *
8620  * Description: This routine is called when the PCI bus has
8621  * permanently failed.
8622  */
8623 static void ipr_pci_perm_failure(struct pci_dev *pdev)
8624 {
8625         unsigned long flags = 0;
8626         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8627         int i;
8628
8629         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
8630         if (ioa_cfg->probe_done) {
8631                 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
8632                         ioa_cfg->sdt_state = ABORT_DUMP;
8633                 ioa_cfg->reset_retries = IPR_NUM_RESET_RELOAD_RETRIES - 1;
8634                 ioa_cfg->in_ioa_bringdown = 1;
8635                 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8636                         spin_lock(&ioa_cfg->hrrq[i]._lock);
8637                         ioa_cfg->hrrq[i].allow_cmds = 0;
8638                         spin_unlock(&ioa_cfg->hrrq[i]._lock);
8639                 }
8640                 wmb();
8641                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8642         } else
8643                 wake_up_all(&ioa_cfg->eeh_wait_q);
8644         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8645 }
8646
8647 /**
8648  * ipr_pci_error_detected - Called when a PCI error is detected.
8649  * @pdev:       PCI device struct
8650  * @state:      PCI channel state
8651  *
8652  * Description: Called when a PCI error is detected.
8653  *
8654  * Return value:
8655  *      PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8656  */
8657 static pci_ers_result_t ipr_pci_error_detected(struct pci_dev *pdev,
8658                                                pci_channel_state_t state)
8659 {
8660         switch (state) {
8661         case pci_channel_io_frozen:
8662                 ipr_pci_frozen(pdev);
8663                 return PCI_ERS_RESULT_CAN_RECOVER;
8664         case pci_channel_io_perm_failure:
8665                 ipr_pci_perm_failure(pdev);
8666                 return PCI_ERS_RESULT_DISCONNECT;
8667         default:
8668                 break;
8669         }
8670         return PCI_ERS_RESULT_NEED_RESET;
8671 }
8672
8673 /**
8674  * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
8675  * @ioa_cfg:    ioa cfg struct
8676  *
8677  * Description: This is the second phase of adapter initialization
8678  * This function takes care of initilizing the adapter to the point
8679  * where it can accept new commands.
8680  * Return value:
8681  *     none
8682  **/
8683 static void ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
8684 {
8685         unsigned long host_lock_flags = 0;
8686
8687         ENTER;
8688         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
8689         dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
8690         ioa_cfg->probe_done = 1;
8691         if (ioa_cfg->needs_hard_reset) {
8692                 ioa_cfg->needs_hard_reset = 0;
8693                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8694         } else
8695                 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa,
8696                                         IPR_SHUTDOWN_NONE);
8697         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
8698
8699         LEAVE;
8700 }
8701
8702 /**
8703  * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
8704  * @ioa_cfg:    ioa config struct
8705  *
8706  * Return value:
8707  *      none
8708  **/
8709 static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
8710 {
8711         int i;
8712
8713         if (ioa_cfg->ipr_cmnd_list) {
8714                 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
8715                         if (ioa_cfg->ipr_cmnd_list[i])
8716                                 dma_pool_free(ioa_cfg->ipr_cmd_pool,
8717                                               ioa_cfg->ipr_cmnd_list[i],
8718                                               ioa_cfg->ipr_cmnd_list_dma[i]);
8719
8720                         ioa_cfg->ipr_cmnd_list[i] = NULL;
8721                 }
8722         }
8723
8724         dma_pool_destroy(ioa_cfg->ipr_cmd_pool);
8725
8726         kfree(ioa_cfg->ipr_cmnd_list);
8727         kfree(ioa_cfg->ipr_cmnd_list_dma);
8728         ioa_cfg->ipr_cmnd_list = NULL;
8729         ioa_cfg->ipr_cmnd_list_dma = NULL;
8730         ioa_cfg->ipr_cmd_pool = NULL;
8731 }
8732
8733 /**
8734  * ipr_free_mem - Frees memory allocated for an adapter
8735  * @ioa_cfg:    ioa cfg struct
8736  *
8737  * Return value:
8738  *      nothing
8739  **/
8740 static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
8741 {
8742         int i;
8743
8744         kfree(ioa_cfg->res_entries);
8745         dma_free_coherent(&ioa_cfg->pdev->dev, sizeof(struct ipr_misc_cbs),
8746                           ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
8747         ipr_free_cmd_blks(ioa_cfg);
8748
8749         for (i = 0; i < ioa_cfg->hrrq_num; i++)
8750                 dma_free_coherent(&ioa_cfg->pdev->dev,
8751                                   sizeof(u32) * ioa_cfg->hrrq[i].size,
8752                                   ioa_cfg->hrrq[i].host_rrq,
8753                                   ioa_cfg->hrrq[i].host_rrq_dma);
8754
8755         dma_free_coherent(&ioa_cfg->pdev->dev, ioa_cfg->cfg_table_size,
8756                           ioa_cfg->u.cfg_table, ioa_cfg->cfg_table_dma);
8757
8758         for (i = 0; i < IPR_MAX_HCAMS; i++) {
8759                 dma_free_coherent(&ioa_cfg->pdev->dev,
8760                                   sizeof(struct ipr_hostrcb),
8761                                   ioa_cfg->hostrcb[i],
8762                                   ioa_cfg->hostrcb_dma[i]);
8763         }
8764
8765         ipr_free_dump(ioa_cfg);
8766         kfree(ioa_cfg->trace);
8767 }
8768
8769 /**
8770  * ipr_free_irqs - Free all allocated IRQs for the adapter.
8771  * @ioa_cfg:    ipr cfg struct
8772  *
8773  * This function frees all allocated IRQs for the
8774  * specified adapter.
8775  *
8776  * Return value:
8777  *      none
8778  **/
8779 static void ipr_free_irqs(struct ipr_ioa_cfg *ioa_cfg)
8780 {
8781         struct pci_dev *pdev = ioa_cfg->pdev;
8782         int i;
8783
8784         for (i = 0; i < ioa_cfg->nvectors; i++)
8785                 free_irq(pci_irq_vector(pdev, i), &ioa_cfg->hrrq[i]);
8786         pci_free_irq_vectors(pdev);
8787 }
8788
8789 /**
8790  * ipr_free_all_resources - Free all allocated resources for an adapter.
8791  * @ioa_cfg:    ioa config struct
8792  *
8793  * This function frees all allocated resources for the
8794  * specified adapter.
8795  *
8796  * Return value:
8797  *      none
8798  **/
8799 static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
8800 {
8801         struct pci_dev *pdev = ioa_cfg->pdev;
8802
8803         ENTER;
8804         ipr_free_irqs(ioa_cfg);
8805         if (ioa_cfg->reset_work_q)
8806                 destroy_workqueue(ioa_cfg->reset_work_q);
8807         iounmap(ioa_cfg->hdw_dma_regs);
8808         pci_release_regions(pdev);
8809         ipr_free_mem(ioa_cfg);
8810         scsi_host_put(ioa_cfg->host);
8811         pci_disable_device(pdev);
8812         LEAVE;
8813 }
8814
8815 /**
8816  * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
8817  * @ioa_cfg:    ioa config struct
8818  *
8819  * Return value:
8820  *      0 on success / -ENOMEM on allocation failure
8821  **/
8822 static int ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
8823 {
8824         struct ipr_cmnd *ipr_cmd;
8825         struct ipr_ioarcb *ioarcb;
8826         dma_addr_t dma_addr;
8827         int i, entries_each_hrrq, hrrq_id = 0;
8828
8829         ioa_cfg->ipr_cmd_pool = dma_pool_create(IPR_NAME, &ioa_cfg->pdev->dev,
8830                                                 sizeof(struct ipr_cmnd), 512, 0);
8831
8832         if (!ioa_cfg->ipr_cmd_pool)
8833                 return -ENOMEM;
8834
8835         ioa_cfg->ipr_cmnd_list = kcalloc(IPR_NUM_CMD_BLKS, sizeof(struct ipr_cmnd *), GFP_KERNEL);
8836         ioa_cfg->ipr_cmnd_list_dma = kcalloc(IPR_NUM_CMD_BLKS, sizeof(dma_addr_t), GFP_KERNEL);
8837
8838         if (!ioa_cfg->ipr_cmnd_list || !ioa_cfg->ipr_cmnd_list_dma) {
8839                 ipr_free_cmd_blks(ioa_cfg);
8840                 return -ENOMEM;
8841         }
8842
8843         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8844                 if (ioa_cfg->hrrq_num > 1) {
8845                         if (i == 0) {
8846                                 entries_each_hrrq = IPR_NUM_INTERNAL_CMD_BLKS;
8847                                 ioa_cfg->hrrq[i].min_cmd_id = 0;
8848                                 ioa_cfg->hrrq[i].max_cmd_id =
8849                                         (entries_each_hrrq - 1);
8850                         } else {
8851                                 entries_each_hrrq =
8852                                         IPR_NUM_BASE_CMD_BLKS/
8853                                         (ioa_cfg->hrrq_num - 1);
8854                                 ioa_cfg->hrrq[i].min_cmd_id =
8855                                         IPR_NUM_INTERNAL_CMD_BLKS +
8856                                         (i - 1) * entries_each_hrrq;
8857                                 ioa_cfg->hrrq[i].max_cmd_id =
8858                                         (IPR_NUM_INTERNAL_CMD_BLKS +
8859                                         i * entries_each_hrrq - 1);
8860                         }
8861                 } else {
8862                         entries_each_hrrq = IPR_NUM_CMD_BLKS;
8863                         ioa_cfg->hrrq[i].min_cmd_id = 0;
8864                         ioa_cfg->hrrq[i].max_cmd_id = (entries_each_hrrq - 1);
8865                 }
8866                 ioa_cfg->hrrq[i].size = entries_each_hrrq;
8867         }
8868
8869         BUG_ON(ioa_cfg->hrrq_num == 0);
8870
8871         i = IPR_NUM_CMD_BLKS -
8872                 ioa_cfg->hrrq[ioa_cfg->hrrq_num - 1].max_cmd_id - 1;
8873         if (i > 0) {
8874                 ioa_cfg->hrrq[ioa_cfg->hrrq_num - 1].size += i;
8875                 ioa_cfg->hrrq[ioa_cfg->hrrq_num - 1].max_cmd_id += i;
8876         }
8877
8878         for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
8879                 ipr_cmd = dma_pool_zalloc(ioa_cfg->ipr_cmd_pool,
8880                                 GFP_KERNEL, &dma_addr);
8881
8882                 if (!ipr_cmd) {
8883                         ipr_free_cmd_blks(ioa_cfg);
8884                         return -ENOMEM;
8885                 }
8886
8887                 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
8888                 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
8889
8890                 ioarcb = &ipr_cmd->ioarcb;
8891                 ipr_cmd->dma_addr = dma_addr;
8892                 if (ioa_cfg->sis64)
8893                         ioarcb->a.ioarcb_host_pci_addr64 = cpu_to_be64(dma_addr);
8894                 else
8895                         ioarcb->a.ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
8896
8897                 ioarcb->host_response_handle = cpu_to_be32(i << 2);
8898                 if (ioa_cfg->sis64) {
8899                         ioarcb->u.sis64_addr_data.data_ioadl_addr =
8900                                 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
8901                         ioarcb->u.sis64_addr_data.ioasa_host_pci_addr =
8902                                 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, s.ioasa64));
8903                 } else {
8904                         ioarcb->write_ioadl_addr =
8905                                 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
8906                         ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
8907                         ioarcb->ioasa_host_pci_addr =
8908                                 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, s.ioasa));
8909                 }
8910                 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
8911                 ipr_cmd->cmd_index = i;
8912                 ipr_cmd->ioa_cfg = ioa_cfg;
8913                 ipr_cmd->sense_buffer_dma = dma_addr +
8914                         offsetof(struct ipr_cmnd, sense_buffer);
8915
8916                 ipr_cmd->ioarcb.cmd_pkt.hrrq_id = hrrq_id;
8917                 ipr_cmd->hrrq = &ioa_cfg->hrrq[hrrq_id];
8918                 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
8919                 if (i >= ioa_cfg->hrrq[hrrq_id].max_cmd_id)
8920                         hrrq_id++;
8921         }
8922
8923         return 0;
8924 }
8925
8926 /**
8927  * ipr_alloc_mem - Allocate memory for an adapter
8928  * @ioa_cfg:    ioa config struct
8929  *
8930  * Return value:
8931  *      0 on success / non-zero for error
8932  **/
8933 static int ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
8934 {
8935         struct pci_dev *pdev = ioa_cfg->pdev;
8936         int i, rc = -ENOMEM;
8937
8938         ENTER;
8939         ioa_cfg->res_entries = kcalloc(ioa_cfg->max_devs_supported,
8940                                        sizeof(struct ipr_resource_entry),
8941                                        GFP_KERNEL);
8942
8943         if (!ioa_cfg->res_entries)
8944                 goto out;
8945
8946         for (i = 0; i < ioa_cfg->max_devs_supported; i++) {
8947                 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
8948                 ioa_cfg->res_entries[i].ioa_cfg = ioa_cfg;
8949         }
8950
8951         ioa_cfg->vpd_cbs = dma_alloc_coherent(&pdev->dev,
8952                                               sizeof(struct ipr_misc_cbs),
8953                                               &ioa_cfg->vpd_cbs_dma,
8954                                               GFP_KERNEL);
8955
8956         if (!ioa_cfg->vpd_cbs)
8957                 goto out_free_res_entries;
8958
8959         if (ipr_alloc_cmd_blks(ioa_cfg))
8960                 goto out_free_vpd_cbs;
8961
8962         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8963                 ioa_cfg->hrrq[i].host_rrq = dma_alloc_coherent(&pdev->dev,
8964                                         sizeof(u32) * ioa_cfg->hrrq[i].size,
8965                                         &ioa_cfg->hrrq[i].host_rrq_dma,
8966                                         GFP_KERNEL);
8967
8968                 if (!ioa_cfg->hrrq[i].host_rrq)  {
8969                         while (--i >= 0)
8970                                 dma_free_coherent(&pdev->dev,
8971                                         sizeof(u32) * ioa_cfg->hrrq[i].size,
8972                                         ioa_cfg->hrrq[i].host_rrq,
8973                                         ioa_cfg->hrrq[i].host_rrq_dma);
8974                         goto out_ipr_free_cmd_blocks;
8975                 }
8976                 ioa_cfg->hrrq[i].ioa_cfg = ioa_cfg;
8977         }
8978
8979         ioa_cfg->u.cfg_table = dma_alloc_coherent(&pdev->dev,
8980                                                   ioa_cfg->cfg_table_size,
8981                                                   &ioa_cfg->cfg_table_dma,
8982                                                   GFP_KERNEL);
8983
8984         if (!ioa_cfg->u.cfg_table)
8985                 goto out_free_host_rrq;
8986
8987         for (i = 0; i < IPR_MAX_HCAMS; i++) {
8988                 ioa_cfg->hostrcb[i] = dma_alloc_coherent(&pdev->dev,
8989                                                          sizeof(struct ipr_hostrcb),
8990                                                          &ioa_cfg->hostrcb_dma[i],
8991                                                          GFP_KERNEL);
8992
8993                 if (!ioa_cfg->hostrcb[i])
8994                         goto out_free_hostrcb_dma;
8995
8996                 ioa_cfg->hostrcb[i]->hostrcb_dma =
8997                         ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
8998                 ioa_cfg->hostrcb[i]->ioa_cfg = ioa_cfg;
8999                 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
9000         }
9001
9002         ioa_cfg->trace = kcalloc(IPR_NUM_TRACE_ENTRIES,
9003                                  sizeof(struct ipr_trace_entry),
9004                                  GFP_KERNEL);
9005
9006         if (!ioa_cfg->trace)
9007                 goto out_free_hostrcb_dma;
9008
9009         rc = 0;
9010 out:
9011         LEAVE;
9012         return rc;
9013
9014 out_free_hostrcb_dma:
9015         while (i-- > 0) {
9016                 dma_free_coherent(&pdev->dev, sizeof(struct ipr_hostrcb),
9017                                   ioa_cfg->hostrcb[i],
9018                                   ioa_cfg->hostrcb_dma[i]);
9019         }
9020         dma_free_coherent(&pdev->dev, ioa_cfg->cfg_table_size,
9021                           ioa_cfg->u.cfg_table, ioa_cfg->cfg_table_dma);
9022 out_free_host_rrq:
9023         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
9024                 dma_free_coherent(&pdev->dev,
9025                                   sizeof(u32) * ioa_cfg->hrrq[i].size,
9026                                   ioa_cfg->hrrq[i].host_rrq,
9027                                   ioa_cfg->hrrq[i].host_rrq_dma);
9028         }
9029 out_ipr_free_cmd_blocks:
9030         ipr_free_cmd_blks(ioa_cfg);
9031 out_free_vpd_cbs:
9032         dma_free_coherent(&pdev->dev, sizeof(struct ipr_misc_cbs),
9033                           ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
9034 out_free_res_entries:
9035         kfree(ioa_cfg->res_entries);
9036         goto out;
9037 }
9038
9039 /**
9040  * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
9041  * @ioa_cfg:    ioa config struct
9042  *
9043  * Return value:
9044  *      none
9045  **/
9046 static void ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
9047 {
9048         int i;
9049
9050         for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
9051                 ioa_cfg->bus_attr[i].bus = i;
9052                 ioa_cfg->bus_attr[i].qas_enabled = 0;
9053                 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
9054                 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
9055                         ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
9056                 else
9057                         ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
9058         }
9059 }
9060
9061 /**
9062  * ipr_init_regs - Initialize IOA registers
9063  * @ioa_cfg:    ioa config struct
9064  *
9065  * Return value:
9066  *      none
9067  **/
9068 static void ipr_init_regs(struct ipr_ioa_cfg *ioa_cfg)
9069 {
9070         const struct ipr_interrupt_offsets *p;
9071         struct ipr_interrupts *t;
9072         void __iomem *base;
9073
9074         p = &ioa_cfg->chip_cfg->regs;
9075         t = &ioa_cfg->regs;
9076         base = ioa_cfg->hdw_dma_regs;
9077
9078         t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
9079         t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
9080         t->clr_interrupt_mask_reg32 = base + p->clr_interrupt_mask_reg32;
9081         t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
9082         t->sense_interrupt_mask_reg32 = base + p->sense_interrupt_mask_reg32;
9083         t->clr_interrupt_reg = base + p->clr_interrupt_reg;
9084         t->clr_interrupt_reg32 = base + p->clr_interrupt_reg32;
9085         t->sense_interrupt_reg = base + p->sense_interrupt_reg;
9086         t->sense_interrupt_reg32 = base + p->sense_interrupt_reg32;
9087         t->ioarrin_reg = base + p->ioarrin_reg;
9088         t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
9089         t->sense_uproc_interrupt_reg32 = base + p->sense_uproc_interrupt_reg32;
9090         t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
9091         t->set_uproc_interrupt_reg32 = base + p->set_uproc_interrupt_reg32;
9092         t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
9093         t->clr_uproc_interrupt_reg32 = base + p->clr_uproc_interrupt_reg32;
9094
9095         if (ioa_cfg->sis64) {
9096                 t->init_feedback_reg = base + p->init_feedback_reg;
9097                 t->dump_addr_reg = base + p->dump_addr_reg;
9098                 t->dump_data_reg = base + p->dump_data_reg;
9099                 t->endian_swap_reg = base + p->endian_swap_reg;
9100         }
9101 }
9102
9103 /**
9104  * ipr_init_ioa_cfg - Initialize IOA config struct
9105  * @ioa_cfg:    ioa config struct
9106  * @host:               scsi host struct
9107  * @pdev:               PCI dev struct
9108  *
9109  * Return value:
9110  *      none
9111  **/
9112 static void ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
9113                              struct Scsi_Host *host, struct pci_dev *pdev)
9114 {
9115         int i;
9116
9117         ioa_cfg->host = host;
9118         ioa_cfg->pdev = pdev;
9119         ioa_cfg->log_level = ipr_log_level;
9120         ioa_cfg->doorbell = IPR_DOORBELL;
9121         sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
9122         sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
9123         sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
9124         sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
9125         sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
9126         sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
9127
9128         INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
9129         INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
9130         INIT_LIST_HEAD(&ioa_cfg->hostrcb_report_q);
9131         INIT_LIST_HEAD(&ioa_cfg->free_res_q);
9132         INIT_LIST_HEAD(&ioa_cfg->used_res_q);
9133         INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread);
9134         INIT_WORK(&ioa_cfg->scsi_add_work_q, ipr_add_remove_thread);
9135         init_waitqueue_head(&ioa_cfg->reset_wait_q);
9136         init_waitqueue_head(&ioa_cfg->msi_wait_q);
9137         init_waitqueue_head(&ioa_cfg->eeh_wait_q);
9138         ioa_cfg->sdt_state = INACTIVE;
9139
9140         ipr_initialize_bus_attr(ioa_cfg);
9141         ioa_cfg->max_devs_supported = ipr_max_devs;
9142
9143         if (ioa_cfg->sis64) {
9144                 host->max_channel = IPR_MAX_SIS64_BUSES;
9145                 host->max_id = IPR_MAX_SIS64_TARGETS_PER_BUS;
9146                 host->max_lun = IPR_MAX_SIS64_LUNS_PER_TARGET;
9147                 if (ipr_max_devs > IPR_MAX_SIS64_DEVS)
9148                         ioa_cfg->max_devs_supported = IPR_MAX_SIS64_DEVS;
9149                 ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr64)
9150                                            + ((sizeof(struct ipr_config_table_entry64)
9151                                                * ioa_cfg->max_devs_supported)));
9152         } else {
9153                 host->max_channel = IPR_VSET_BUS;
9154                 host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
9155                 host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
9156                 if (ipr_max_devs > IPR_MAX_PHYSICAL_DEVS)
9157                         ioa_cfg->max_devs_supported = IPR_MAX_PHYSICAL_DEVS;
9158                 ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr)
9159                                            + ((sizeof(struct ipr_config_table_entry)
9160                                                * ioa_cfg->max_devs_supported)));
9161         }
9162
9163         host->unique_id = host->host_no;
9164         host->max_cmd_len = IPR_MAX_CDB_LEN;
9165         host->can_queue = ioa_cfg->max_cmds;
9166         pci_set_drvdata(pdev, ioa_cfg);
9167
9168         for (i = 0; i < ARRAY_SIZE(ioa_cfg->hrrq); i++) {
9169                 INIT_LIST_HEAD(&ioa_cfg->hrrq[i].hrrq_free_q);
9170                 INIT_LIST_HEAD(&ioa_cfg->hrrq[i].hrrq_pending_q);
9171                 spin_lock_init(&ioa_cfg->hrrq[i]._lock);
9172                 if (i == 0)
9173                         ioa_cfg->hrrq[i].lock = ioa_cfg->host->host_lock;
9174                 else
9175                         ioa_cfg->hrrq[i].lock = &ioa_cfg->hrrq[i]._lock;
9176         }
9177 }
9178
9179 /**
9180  * ipr_get_chip_info - Find adapter chip information
9181  * @dev_id:             PCI device id struct
9182  *
9183  * Return value:
9184  *      ptr to chip information on success / NULL on failure
9185  **/
9186 static const struct ipr_chip_t *
9187 ipr_get_chip_info(const struct pci_device_id *dev_id)
9188 {
9189         int i;
9190
9191         for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
9192                 if (ipr_chip[i].vendor == dev_id->vendor &&
9193                     ipr_chip[i].device == dev_id->device)
9194                         return &ipr_chip[i];
9195         return NULL;
9196 }
9197
9198 /**
9199  * ipr_wait_for_pci_err_recovery - Wait for any PCI error recovery to complete
9200  *                                              during probe time
9201  * @ioa_cfg:    ioa config struct
9202  *
9203  * Return value:
9204  *      None
9205  **/
9206 static void ipr_wait_for_pci_err_recovery(struct ipr_ioa_cfg *ioa_cfg)
9207 {
9208         struct pci_dev *pdev = ioa_cfg->pdev;
9209
9210         if (pci_channel_offline(pdev)) {
9211                 wait_event_timeout(ioa_cfg->eeh_wait_q,
9212                                    !pci_channel_offline(pdev),
9213                                    IPR_PCI_ERROR_RECOVERY_TIMEOUT);
9214                 pci_restore_state(pdev);
9215         }
9216 }
9217
9218 static void name_msi_vectors(struct ipr_ioa_cfg *ioa_cfg)
9219 {
9220         int vec_idx, n = sizeof(ioa_cfg->vectors_info[0].desc) - 1;
9221
9222         for (vec_idx = 0; vec_idx < ioa_cfg->nvectors; vec_idx++) {
9223                 snprintf(ioa_cfg->vectors_info[vec_idx].desc, n,
9224                          "host%d-%d", ioa_cfg->host->host_no, vec_idx);
9225                 ioa_cfg->vectors_info[vec_idx].
9226                         desc[strlen(ioa_cfg->vectors_info[vec_idx].desc)] = 0;
9227         }
9228 }
9229
9230 static int ipr_request_other_msi_irqs(struct ipr_ioa_cfg *ioa_cfg,
9231                 struct pci_dev *pdev)
9232 {
9233         int i, rc;
9234
9235         for (i = 1; i < ioa_cfg->nvectors; i++) {
9236                 rc = request_irq(pci_irq_vector(pdev, i),
9237                         ipr_isr_mhrrq,
9238                         0,
9239                         ioa_cfg->vectors_info[i].desc,
9240                         &ioa_cfg->hrrq[i]);
9241                 if (rc) {
9242                         while (--i > 0)
9243                                 free_irq(pci_irq_vector(pdev, i),
9244                                         &ioa_cfg->hrrq[i]);
9245                         return rc;
9246                 }
9247         }
9248         return 0;
9249 }
9250
9251 /**
9252  * ipr_test_intr - Handle the interrupt generated in ipr_test_msi().
9253  * @devp:               PCI device struct
9254  * @irq:                IRQ number
9255  *
9256  * Description: Simply set the msi_received flag to 1 indicating that
9257  * Message Signaled Interrupts are supported.
9258  *
9259  * Return value:
9260  *      0 on success / non-zero on failure
9261  **/
9262 static irqreturn_t ipr_test_intr(int irq, void *devp)
9263 {
9264         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
9265         unsigned long lock_flags = 0;
9266
9267         dev_info(&ioa_cfg->pdev->dev, "Received IRQ : %d\n", irq);
9268         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9269
9270         ioa_cfg->msi_received = 1;
9271         wake_up(&ioa_cfg->msi_wait_q);
9272
9273         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9274         return IRQ_HANDLED;
9275 }
9276
9277 /**
9278  * ipr_test_msi - Test for Message Signaled Interrupt (MSI) support.
9279  * @ioa_cfg:            ioa config struct
9280  * @pdev:               PCI device struct
9281  *
9282  * Description: This routine sets up and initiates a test interrupt to determine
9283  * if the interrupt is received via the ipr_test_intr() service routine.
9284  * If the tests fails, the driver will fall back to LSI.
9285  *
9286  * Return value:
9287  *      0 on success / non-zero on failure
9288  **/
9289 static int ipr_test_msi(struct ipr_ioa_cfg *ioa_cfg, struct pci_dev *pdev)
9290 {
9291         int rc;
9292         unsigned long lock_flags = 0;
9293         int irq = pci_irq_vector(pdev, 0);
9294
9295         ENTER;
9296
9297         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9298         init_waitqueue_head(&ioa_cfg->msi_wait_q);
9299         ioa_cfg->msi_received = 0;
9300         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
9301         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.clr_interrupt_mask_reg32);
9302         readl(ioa_cfg->regs.sense_interrupt_mask_reg);
9303         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9304
9305         rc = request_irq(irq, ipr_test_intr, 0, IPR_NAME, ioa_cfg);
9306         if (rc) {
9307                 dev_err(&pdev->dev, "Can not assign irq %d\n", irq);
9308                 return rc;
9309         } else if (ipr_debug)
9310                 dev_info(&pdev->dev, "IRQ assigned: %d\n", irq);
9311
9312         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.sense_interrupt_reg32);
9313         readl(ioa_cfg->regs.sense_interrupt_reg);
9314         wait_event_timeout(ioa_cfg->msi_wait_q, ioa_cfg->msi_received, HZ);
9315         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9316         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
9317
9318         if (!ioa_cfg->msi_received) {
9319                 /* MSI test failed */
9320                 dev_info(&pdev->dev, "MSI test failed.  Falling back to LSI.\n");
9321                 rc = -EOPNOTSUPP;
9322         } else if (ipr_debug)
9323                 dev_info(&pdev->dev, "MSI test succeeded.\n");
9324
9325         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9326
9327         free_irq(irq, ioa_cfg);
9328
9329         LEAVE;
9330
9331         return rc;
9332 }
9333
9334  /* ipr_probe_ioa - Allocates memory and does first stage of initialization
9335  * @pdev:               PCI device struct
9336  * @dev_id:             PCI device id struct
9337  *
9338  * Return value:
9339  *      0 on success / non-zero on failure
9340  **/
9341 static int ipr_probe_ioa(struct pci_dev *pdev,
9342                          const struct pci_device_id *dev_id)
9343 {
9344         struct ipr_ioa_cfg *ioa_cfg;
9345         struct Scsi_Host *host;
9346         unsigned long ipr_regs_pci;
9347         void __iomem *ipr_regs;
9348         int rc = PCIBIOS_SUCCESSFUL;
9349         volatile u32 mask, uproc, interrupts;
9350         unsigned long lock_flags, driver_lock_flags;
9351         unsigned int irq_flag;
9352
9353         ENTER;
9354
9355         dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
9356         host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
9357
9358         if (!host) {
9359                 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
9360                 rc = -ENOMEM;
9361                 goto out;
9362         }
9363
9364         ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
9365         memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
9366
9367         ioa_cfg->ipr_chip = ipr_get_chip_info(dev_id);
9368
9369         if (!ioa_cfg->ipr_chip) {
9370                 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
9371                         dev_id->vendor, dev_id->device);
9372                 goto out_scsi_host_put;
9373         }
9374
9375         /* set SIS 32 or SIS 64 */
9376         ioa_cfg->sis64 = ioa_cfg->ipr_chip->sis_type == IPR_SIS64 ? 1 : 0;
9377         ioa_cfg->chip_cfg = ioa_cfg->ipr_chip->cfg;
9378         ioa_cfg->clear_isr = ioa_cfg->chip_cfg->clear_isr;
9379         ioa_cfg->max_cmds = ioa_cfg->chip_cfg->max_cmds;
9380
9381         if (ipr_transop_timeout)
9382                 ioa_cfg->transop_timeout = ipr_transop_timeout;
9383         else if (dev_id->driver_data & IPR_USE_LONG_TRANSOP_TIMEOUT)
9384                 ioa_cfg->transop_timeout = IPR_LONG_OPERATIONAL_TIMEOUT;
9385         else
9386                 ioa_cfg->transop_timeout = IPR_OPERATIONAL_TIMEOUT;
9387
9388         ioa_cfg->revid = pdev->revision;
9389
9390         ipr_init_ioa_cfg(ioa_cfg, host, pdev);
9391
9392         ipr_regs_pci = pci_resource_start(pdev, 0);
9393
9394         rc = pci_request_regions(pdev, IPR_NAME);
9395         if (rc < 0) {
9396                 dev_err(&pdev->dev,
9397                         "Couldn't register memory range of registers\n");
9398                 goto out_scsi_host_put;
9399         }
9400
9401         rc = pci_enable_device(pdev);
9402
9403         if (rc || pci_channel_offline(pdev)) {
9404                 if (pci_channel_offline(pdev)) {
9405                         ipr_wait_for_pci_err_recovery(ioa_cfg);
9406                         rc = pci_enable_device(pdev);
9407                 }
9408
9409                 if (rc) {
9410                         dev_err(&pdev->dev, "Cannot enable adapter\n");
9411                         ipr_wait_for_pci_err_recovery(ioa_cfg);
9412                         goto out_release_regions;
9413                 }
9414         }
9415
9416         ipr_regs = pci_ioremap_bar(pdev, 0);
9417
9418         if (!ipr_regs) {
9419                 dev_err(&pdev->dev,
9420                         "Couldn't map memory range of registers\n");
9421                 rc = -ENOMEM;
9422                 goto out_disable;
9423         }
9424
9425         ioa_cfg->hdw_dma_regs = ipr_regs;
9426         ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
9427         ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
9428
9429         ipr_init_regs(ioa_cfg);
9430
9431         if (ioa_cfg->sis64) {
9432                 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
9433                 if (rc < 0) {
9434                         dev_dbg(&pdev->dev, "Failed to set 64 bit DMA mask\n");
9435                         rc = dma_set_mask_and_coherent(&pdev->dev,
9436                                                        DMA_BIT_MASK(32));
9437                 }
9438         } else
9439                 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
9440
9441         if (rc < 0) {
9442                 dev_err(&pdev->dev, "Failed to set DMA mask\n");
9443                 goto cleanup_nomem;
9444         }
9445
9446         rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
9447                                    ioa_cfg->chip_cfg->cache_line_size);
9448
9449         if (rc != PCIBIOS_SUCCESSFUL) {
9450                 dev_err(&pdev->dev, "Write of cache line size failed\n");
9451                 ipr_wait_for_pci_err_recovery(ioa_cfg);
9452                 rc = -EIO;
9453                 goto cleanup_nomem;
9454         }
9455
9456         /* Issue MMIO read to ensure card is not in EEH */
9457         interrupts = readl(ioa_cfg->regs.sense_interrupt_reg);
9458         ipr_wait_for_pci_err_recovery(ioa_cfg);
9459
9460         if (ipr_number_of_msix > IPR_MAX_MSIX_VECTORS) {
9461                 dev_err(&pdev->dev, "The max number of MSIX is %d\n",
9462                         IPR_MAX_MSIX_VECTORS);
9463                 ipr_number_of_msix = IPR_MAX_MSIX_VECTORS;
9464         }
9465
9466         irq_flag = PCI_IRQ_LEGACY;
9467         if (ioa_cfg->ipr_chip->has_msi)
9468                 irq_flag |= PCI_IRQ_MSI | PCI_IRQ_MSIX;
9469         rc = pci_alloc_irq_vectors(pdev, 1, ipr_number_of_msix, irq_flag);
9470         if (rc < 0) {
9471                 ipr_wait_for_pci_err_recovery(ioa_cfg);
9472                 goto cleanup_nomem;
9473         }
9474         ioa_cfg->nvectors = rc;
9475
9476         if (!pdev->msi_enabled && !pdev->msix_enabled)
9477                 ioa_cfg->clear_isr = 1;
9478
9479         pci_set_master(pdev);
9480
9481         if (pci_channel_offline(pdev)) {
9482                 ipr_wait_for_pci_err_recovery(ioa_cfg);
9483                 pci_set_master(pdev);
9484                 if (pci_channel_offline(pdev)) {
9485                         rc = -EIO;
9486                         goto out_msi_disable;
9487                 }
9488         }
9489
9490         if (pdev->msi_enabled || pdev->msix_enabled) {
9491                 rc = ipr_test_msi(ioa_cfg, pdev);
9492                 switch (rc) {
9493                 case 0:
9494                         dev_info(&pdev->dev,
9495                                 "Request for %d MSI%ss succeeded.", ioa_cfg->nvectors,
9496                                 pdev->msix_enabled ? "-X" : "");
9497                         break;
9498                 case -EOPNOTSUPP:
9499                         ipr_wait_for_pci_err_recovery(ioa_cfg);
9500                         pci_free_irq_vectors(pdev);
9501
9502                         ioa_cfg->nvectors = 1;
9503                         ioa_cfg->clear_isr = 1;
9504                         break;
9505                 default:
9506                         goto out_msi_disable;
9507                 }
9508         }
9509
9510         ioa_cfg->hrrq_num = min3(ioa_cfg->nvectors,
9511                                 (unsigned int)num_online_cpus(),
9512                                 (unsigned int)IPR_MAX_HRRQ_NUM);
9513
9514         if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
9515                 goto out_msi_disable;
9516
9517         if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
9518                 goto out_msi_disable;
9519
9520         rc = ipr_alloc_mem(ioa_cfg);
9521         if (rc < 0) {
9522                 dev_err(&pdev->dev,
9523                         "Couldn't allocate enough memory for device driver!\n");
9524                 goto out_msi_disable;
9525         }
9526
9527         /* Save away PCI config space for use following IOA reset */
9528         rc = pci_save_state(pdev);
9529
9530         if (rc != PCIBIOS_SUCCESSFUL) {
9531                 dev_err(&pdev->dev, "Failed to save PCI config space\n");
9532                 rc = -EIO;
9533                 goto cleanup_nolog;
9534         }
9535
9536         /*
9537          * If HRRQ updated interrupt is not masked, or reset alert is set,
9538          * the card is in an unknown state and needs a hard reset
9539          */
9540         mask = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
9541         interrupts = readl(ioa_cfg->regs.sense_interrupt_reg32);
9542         uproc = readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
9543         if ((mask & IPR_PCII_HRRQ_UPDATED) == 0 || (uproc & IPR_UPROCI_RESET_ALERT))
9544                 ioa_cfg->needs_hard_reset = 1;
9545         if ((interrupts & IPR_PCII_ERROR_INTERRUPTS) || reset_devices)
9546                 ioa_cfg->needs_hard_reset = 1;
9547         if (interrupts & IPR_PCII_IOA_UNIT_CHECKED)
9548                 ioa_cfg->ioa_unit_checked = 1;
9549
9550         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9551         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
9552         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9553
9554         if (pdev->msi_enabled || pdev->msix_enabled) {
9555                 name_msi_vectors(ioa_cfg);
9556                 rc = request_irq(pci_irq_vector(pdev, 0), ipr_isr, 0,
9557                         ioa_cfg->vectors_info[0].desc,
9558                         &ioa_cfg->hrrq[0]);
9559                 if (!rc)
9560                         rc = ipr_request_other_msi_irqs(ioa_cfg, pdev);
9561         } else {
9562                 rc = request_irq(pdev->irq, ipr_isr,
9563                          IRQF_SHARED,
9564                          IPR_NAME, &ioa_cfg->hrrq[0]);
9565         }
9566         if (rc) {
9567                 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
9568                         pdev->irq, rc);
9569                 goto cleanup_nolog;
9570         }
9571
9572         if ((dev_id->driver_data & IPR_USE_PCI_WARM_RESET) ||
9573             (dev_id->device == PCI_DEVICE_ID_IBM_OBSIDIAN_E && !ioa_cfg->revid)) {
9574                 ioa_cfg->needs_warm_reset = 1;
9575                 ioa_cfg->reset = ipr_reset_slot_reset;
9576
9577                 ioa_cfg->reset_work_q = alloc_ordered_workqueue("ipr_reset_%d",
9578                                                                 WQ_MEM_RECLAIM, host->host_no);
9579
9580                 if (!ioa_cfg->reset_work_q) {
9581                         dev_err(&pdev->dev, "Couldn't register reset workqueue\n");
9582                         rc = -ENOMEM;
9583                         goto out_free_irq;
9584                 }
9585         } else
9586                 ioa_cfg->reset = ipr_reset_start_bist;
9587
9588         spin_lock_irqsave(&ipr_driver_lock, driver_lock_flags);
9589         list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
9590         spin_unlock_irqrestore(&ipr_driver_lock, driver_lock_flags);
9591
9592         LEAVE;
9593 out:
9594         return rc;
9595
9596 out_free_irq:
9597         ipr_free_irqs(ioa_cfg);
9598 cleanup_nolog:
9599         ipr_free_mem(ioa_cfg);
9600 out_msi_disable:
9601         ipr_wait_for_pci_err_recovery(ioa_cfg);
9602         pci_free_irq_vectors(pdev);
9603 cleanup_nomem:
9604         iounmap(ipr_regs);
9605 out_disable:
9606         pci_disable_device(pdev);
9607 out_release_regions:
9608         pci_release_regions(pdev);
9609 out_scsi_host_put:
9610         scsi_host_put(host);
9611         goto out;
9612 }
9613
9614 /**
9615  * ipr_initiate_ioa_bringdown - Bring down an adapter
9616  * @ioa_cfg:            ioa config struct
9617  * @shutdown_type:      shutdown type
9618  *
9619  * Description: This function will initiate bringing down the adapter.
9620  * This consists of issuing an IOA shutdown to the adapter
9621  * to flush the cache, and running BIST.
9622  * If the caller needs to wait on the completion of the reset,
9623  * the caller must sleep on the reset_wait_q.
9624  *
9625  * Return value:
9626  *      none
9627  **/
9628 static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
9629                                        enum ipr_shutdown_type shutdown_type)
9630 {
9631         ENTER;
9632         if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
9633                 ioa_cfg->sdt_state = ABORT_DUMP;
9634         ioa_cfg->reset_retries = 0;
9635         ioa_cfg->in_ioa_bringdown = 1;
9636         ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
9637         LEAVE;
9638 }
9639
9640 /**
9641  * __ipr_remove - Remove a single adapter
9642  * @pdev:       pci device struct
9643  *
9644  * Adapter hot plug remove entry point.
9645  *
9646  * Return value:
9647  *      none
9648  **/
9649 static void __ipr_remove(struct pci_dev *pdev)
9650 {
9651         unsigned long host_lock_flags = 0;
9652         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
9653         int i;
9654         unsigned long driver_lock_flags;
9655         ENTER;
9656
9657         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
9658         while (ioa_cfg->in_reset_reload) {
9659                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
9660                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
9661                 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
9662         }
9663
9664         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
9665                 spin_lock(&ioa_cfg->hrrq[i]._lock);
9666                 ioa_cfg->hrrq[i].removing_ioa = 1;
9667                 spin_unlock(&ioa_cfg->hrrq[i]._lock);
9668         }
9669         wmb();
9670         ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
9671
9672         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
9673         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
9674         flush_work(&ioa_cfg->work_q);
9675         if (ioa_cfg->reset_work_q)
9676                 flush_workqueue(ioa_cfg->reset_work_q);
9677         INIT_LIST_HEAD(&ioa_cfg->used_res_q);
9678         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
9679
9680         spin_lock_irqsave(&ipr_driver_lock, driver_lock_flags);
9681         list_del(&ioa_cfg->queue);
9682         spin_unlock_irqrestore(&ipr_driver_lock, driver_lock_flags);
9683
9684         if (ioa_cfg->sdt_state == ABORT_DUMP)
9685                 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
9686         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
9687
9688         ipr_free_all_resources(ioa_cfg);
9689
9690         LEAVE;
9691 }
9692
9693 /**
9694  * ipr_remove - IOA hot plug remove entry point
9695  * @pdev:       pci device struct
9696  *
9697  * Adapter hot plug remove entry point.
9698  *
9699  * Return value:
9700  *      none
9701  **/
9702 static void ipr_remove(struct pci_dev *pdev)
9703 {
9704         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
9705
9706         ENTER;
9707
9708         ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
9709                               &ipr_trace_attr);
9710         ipr_remove_dump_file(&ioa_cfg->host->shost_dev.kobj,
9711                              &ipr_dump_attr);
9712         sysfs_remove_bin_file(&ioa_cfg->host->shost_dev.kobj,
9713                         &ipr_ioa_async_err_log);
9714         scsi_remove_host(ioa_cfg->host);
9715
9716         __ipr_remove(pdev);
9717
9718         LEAVE;
9719 }
9720
9721 /**
9722  * ipr_probe - Adapter hot plug add entry point
9723  * @pdev:       pci device struct
9724  * @dev_id:     pci device ID
9725  *
9726  * Return value:
9727  *      0 on success / non-zero on failure
9728  **/
9729 static int ipr_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
9730 {
9731         struct ipr_ioa_cfg *ioa_cfg;
9732         unsigned long flags;
9733         int rc, i;
9734
9735         rc = ipr_probe_ioa(pdev, dev_id);
9736
9737         if (rc)
9738                 return rc;
9739
9740         ioa_cfg = pci_get_drvdata(pdev);
9741         ipr_probe_ioa_part2(ioa_cfg);
9742
9743         rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
9744
9745         if (rc) {
9746                 __ipr_remove(pdev);
9747                 return rc;
9748         }
9749
9750         rc = ipr_create_trace_file(&ioa_cfg->host->shost_dev.kobj,
9751                                    &ipr_trace_attr);
9752
9753         if (rc) {
9754                 scsi_remove_host(ioa_cfg->host);
9755                 __ipr_remove(pdev);
9756                 return rc;
9757         }
9758
9759         rc = sysfs_create_bin_file(&ioa_cfg->host->shost_dev.kobj,
9760                         &ipr_ioa_async_err_log);
9761
9762         if (rc) {
9763                 ipr_remove_dump_file(&ioa_cfg->host->shost_dev.kobj,
9764                                 &ipr_dump_attr);
9765                 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
9766                                 &ipr_trace_attr);
9767                 scsi_remove_host(ioa_cfg->host);
9768                 __ipr_remove(pdev);
9769                 return rc;
9770         }
9771
9772         rc = ipr_create_dump_file(&ioa_cfg->host->shost_dev.kobj,
9773                                    &ipr_dump_attr);
9774
9775         if (rc) {
9776                 sysfs_remove_bin_file(&ioa_cfg->host->shost_dev.kobj,
9777                                       &ipr_ioa_async_err_log);
9778                 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
9779                                       &ipr_trace_attr);
9780                 scsi_remove_host(ioa_cfg->host);
9781                 __ipr_remove(pdev);
9782                 return rc;
9783         }
9784         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
9785         ioa_cfg->scan_enabled = 1;
9786         schedule_work(&ioa_cfg->work_q);
9787         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
9788
9789         ioa_cfg->iopoll_weight = ioa_cfg->chip_cfg->iopoll_weight;
9790
9791         if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
9792                 for (i = 1; i < ioa_cfg->hrrq_num; i++) {
9793                         irq_poll_init(&ioa_cfg->hrrq[i].iopoll,
9794                                         ioa_cfg->iopoll_weight, ipr_iopoll);
9795                 }
9796         }
9797
9798         scsi_scan_host(ioa_cfg->host);
9799
9800         return 0;
9801 }
9802
9803 /**
9804  * ipr_shutdown - Shutdown handler.
9805  * @pdev:       pci device struct
9806  *
9807  * This function is invoked upon system shutdown/reboot. It will issue
9808  * an adapter shutdown to the adapter to flush the write cache.
9809  *
9810  * Return value:
9811  *      none
9812  **/
9813 static void ipr_shutdown(struct pci_dev *pdev)
9814 {
9815         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
9816         unsigned long lock_flags = 0;
9817         enum ipr_shutdown_type shutdown_type = IPR_SHUTDOWN_NORMAL;
9818         int i;
9819
9820         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9821         if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
9822                 ioa_cfg->iopoll_weight = 0;
9823                 for (i = 1; i < ioa_cfg->hrrq_num; i++)
9824                         irq_poll_disable(&ioa_cfg->hrrq[i].iopoll);
9825         }
9826
9827         while (ioa_cfg->in_reset_reload) {
9828                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9829                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
9830                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9831         }
9832
9833         if (ipr_fast_reboot && system_state == SYSTEM_RESTART && ioa_cfg->sis64)
9834                 shutdown_type = IPR_SHUTDOWN_QUIESCE;
9835
9836         ipr_initiate_ioa_bringdown(ioa_cfg, shutdown_type);
9837         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9838         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
9839         if (ipr_fast_reboot && system_state == SYSTEM_RESTART && ioa_cfg->sis64) {
9840                 ipr_free_irqs(ioa_cfg);
9841                 pci_disable_device(ioa_cfg->pdev);
9842         }
9843 }
9844
9845 static struct pci_device_id ipr_pci_table[] = {
9846         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
9847                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702, 0, 0, 0 },
9848         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
9849                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703, 0, 0, 0 },
9850         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
9851                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D, 0, 0, 0 },
9852         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
9853                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E, 0, 0, 0 },
9854         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
9855                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B, 0, 0, 0 },
9856         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
9857                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E, 0, 0, 0 },
9858         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
9859                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A, 0, 0, 0 },
9860         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
9861                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575B, 0, 0,
9862                 IPR_USE_LONG_TRANSOP_TIMEOUT },
9863         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
9864               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
9865         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
9866               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
9867               IPR_USE_LONG_TRANSOP_TIMEOUT },
9868         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
9869               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
9870               IPR_USE_LONG_TRANSOP_TIMEOUT },
9871         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
9872               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
9873         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
9874               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
9875               IPR_USE_LONG_TRANSOP_TIMEOUT},
9876         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
9877               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
9878               IPR_USE_LONG_TRANSOP_TIMEOUT },
9879         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
9880               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574E, 0, 0,
9881               IPR_USE_LONG_TRANSOP_TIMEOUT },
9882         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
9883               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B3, 0, 0, 0 },
9884         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
9885               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57CC, 0, 0, 0 },
9886         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
9887               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B7, 0, 0,
9888               IPR_USE_LONG_TRANSOP_TIMEOUT | IPR_USE_PCI_WARM_RESET },
9889         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
9890                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780, 0, 0, 0 },
9891         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
9892                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E, 0, 0, 0 },
9893         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
9894                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571F, 0, 0,
9895                 IPR_USE_LONG_TRANSOP_TIMEOUT },
9896         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
9897                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572F, 0, 0,
9898                 IPR_USE_LONG_TRANSOP_TIMEOUT },
9899         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
9900                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B5, 0, 0, 0 },
9901         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
9902                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574D, 0, 0, 0 },
9903         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
9904                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B2, 0, 0, 0 },
9905         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
9906                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C0, 0, 0, 0 },
9907         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
9908                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C3, 0, 0, 0 },
9909         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
9910                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C4, 0, 0, 0 },
9911         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9912                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B4, 0, 0, 0 },
9913         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9914                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B1, 0, 0, 0 },
9915         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9916                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C6, 0, 0, 0 },
9917         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9918                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C8, 0, 0, 0 },
9919         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9920                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57CE, 0, 0, 0 },
9921         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9922                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D5, 0, 0, 0 },
9923         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9924                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D6, 0, 0, 0 },
9925         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9926                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D7, 0, 0, 0 },
9927         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9928                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D8, 0, 0, 0 },
9929         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9930                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D9, 0, 0, 0 },
9931         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9932                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57DA, 0, 0, 0 },
9933         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9934                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EB, 0, 0, 0 },
9935         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9936                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EC, 0, 0, 0 },
9937         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9938                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57ED, 0, 0, 0 },
9939         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9940                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EE, 0, 0, 0 },
9941         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9942                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EF, 0, 0, 0 },
9943         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9944                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57F0, 0, 0, 0 },
9945         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9946                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2CCA, 0, 0, 0 },
9947         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9948                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2CD2, 0, 0, 0 },
9949         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9950                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2CCD, 0, 0, 0 },
9951         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_RATTLESNAKE,
9952                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_580A, 0, 0, 0 },
9953         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_RATTLESNAKE,
9954                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_580B, 0, 0, 0 },
9955         { }
9956 };
9957 MODULE_DEVICE_TABLE(pci, ipr_pci_table);
9958
9959 static const struct pci_error_handlers ipr_err_handler = {
9960         .error_detected = ipr_pci_error_detected,
9961         .mmio_enabled = ipr_pci_mmio_enabled,
9962         .slot_reset = ipr_pci_slot_reset,
9963 };
9964
9965 static struct pci_driver ipr_driver = {
9966         .name = IPR_NAME,
9967         .id_table = ipr_pci_table,
9968         .probe = ipr_probe,
9969         .remove = ipr_remove,
9970         .shutdown = ipr_shutdown,
9971         .err_handler = &ipr_err_handler,
9972 };
9973
9974 /**
9975  * ipr_halt_done - Shutdown prepare completion
9976  * @ipr_cmd:   ipr command struct
9977  *
9978  * Return value:
9979  *      none
9980  **/
9981 static void ipr_halt_done(struct ipr_cmnd *ipr_cmd)
9982 {
9983         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
9984 }
9985
9986 /**
9987  * ipr_halt - Issue shutdown prepare to all adapters
9988  * @nb: Notifier block
9989  * @event: Notifier event
9990  * @buf: Notifier data (unused)
9991  *
9992  * Return value:
9993  *      NOTIFY_OK on success / NOTIFY_DONE on failure
9994  **/
9995 static int ipr_halt(struct notifier_block *nb, ulong event, void *buf)
9996 {
9997         struct ipr_cmnd *ipr_cmd;
9998         struct ipr_ioa_cfg *ioa_cfg;
9999         unsigned long flags = 0, driver_lock_flags;
10000
10001         if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
10002                 return NOTIFY_DONE;
10003
10004         spin_lock_irqsave(&ipr_driver_lock, driver_lock_flags);
10005
10006         list_for_each_entry(ioa_cfg, &ipr_ioa_head, queue) {
10007                 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
10008                 if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds ||
10009                     (ipr_fast_reboot && event == SYS_RESTART && ioa_cfg->sis64)) {
10010                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
10011                         continue;
10012                 }
10013
10014                 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
10015                 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
10016                 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
10017                 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
10018                 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;
10019
10020                 ipr_do_req(ipr_cmd, ipr_halt_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
10021                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
10022         }
10023         spin_unlock_irqrestore(&ipr_driver_lock, driver_lock_flags);
10024
10025         return NOTIFY_OK;
10026 }
10027
10028 static struct notifier_block ipr_notifier = {
10029         ipr_halt, NULL, 0
10030 };
10031
10032 /**
10033  * ipr_init - Module entry point
10034  *
10035  * Return value:
10036  *      0 on success / negative value on failure
10037  **/
10038 static int __init ipr_init(void)
10039 {
10040         int rc;
10041
10042         ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
10043                  IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
10044
10045         register_reboot_notifier(&ipr_notifier);
10046         rc = pci_register_driver(&ipr_driver);
10047         if (rc) {
10048                 unregister_reboot_notifier(&ipr_notifier);
10049                 return rc;
10050         }
10051
10052         return 0;
10053 }
10054
10055 /**
10056  * ipr_exit - Module unload
10057  *
10058  * Module unload entry point.
10059  *
10060  * Return value:
10061  *      none
10062  **/
10063 static void __exit ipr_exit(void)
10064 {
10065         unregister_reboot_notifier(&ipr_notifier);
10066         pci_unregister_driver(&ipr_driver);
10067 }
10068
10069 module_init(ipr_init);
10070 module_exit(ipr_exit);