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