]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/ipr.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6.26
[linux-2.6-omap-h63xx.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/ioport.h>
63 #include <linux/delay.h>
64 #include <linux/pci.h>
65 #include <linux/wait.h>
66 #include <linux/spinlock.h>
67 #include <linux/sched.h>
68 #include <linux/interrupt.h>
69 #include <linux/blkdev.h>
70 #include <linux/firmware.h>
71 #include <linux/module.h>
72 #include <linux/moduleparam.h>
73 #include <linux/libata.h>
74 #include <asm/io.h>
75 #include <asm/irq.h>
76 #include <asm/processor.h>
77 #include <scsi/scsi.h>
78 #include <scsi/scsi_host.h>
79 #include <scsi/scsi_tcq.h>
80 #include <scsi/scsi_eh.h>
81 #include <scsi/scsi_cmnd.h>
82 #include "ipr.h"
83
84 /*
85  *   Global Data
86  */
87 static LIST_HEAD(ipr_ioa_head);
88 static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
89 static unsigned int ipr_max_speed = 1;
90 static int ipr_testmode = 0;
91 static unsigned int ipr_fastfail = 0;
92 static unsigned int ipr_transop_timeout = 0;
93 static unsigned int ipr_enable_cache = 1;
94 static unsigned int ipr_debug = 0;
95 static unsigned int ipr_dual_ioa_raid = 1;
96 static DEFINE_SPINLOCK(ipr_driver_lock);
97
98 /* This table describes the differences between DMA controller chips */
99 static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
100         { /* Gemstone, Citrine, Obsidian, and Obsidian-E */
101                 .mailbox = 0x0042C,
102                 .cache_line_size = 0x20,
103                 {
104                         .set_interrupt_mask_reg = 0x0022C,
105                         .clr_interrupt_mask_reg = 0x00230,
106                         .sense_interrupt_mask_reg = 0x0022C,
107                         .clr_interrupt_reg = 0x00228,
108                         .sense_interrupt_reg = 0x00224,
109                         .ioarrin_reg = 0x00404,
110                         .sense_uproc_interrupt_reg = 0x00214,
111                         .set_uproc_interrupt_reg = 0x00214,
112                         .clr_uproc_interrupt_reg = 0x00218
113                 }
114         },
115         { /* Snipe and Scamp */
116                 .mailbox = 0x0052C,
117                 .cache_line_size = 0x20,
118                 {
119                         .set_interrupt_mask_reg = 0x00288,
120                         .clr_interrupt_mask_reg = 0x0028C,
121                         .sense_interrupt_mask_reg = 0x00288,
122                         .clr_interrupt_reg = 0x00284,
123                         .sense_interrupt_reg = 0x00280,
124                         .ioarrin_reg = 0x00504,
125                         .sense_uproc_interrupt_reg = 0x00290,
126                         .set_uproc_interrupt_reg = 0x00290,
127                         .clr_uproc_interrupt_reg = 0x00294
128                 }
129         },
130 };
131
132 static const struct ipr_chip_t ipr_chip[] = {
133         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, &ipr_chip_cfg[0] },
134         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, &ipr_chip_cfg[0] },
135         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, &ipr_chip_cfg[0] },
136         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, &ipr_chip_cfg[0] },
137         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, &ipr_chip_cfg[0] },
138         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, &ipr_chip_cfg[1] },
139         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, &ipr_chip_cfg[1] }
140 };
141
142 static int ipr_max_bus_speeds [] = {
143         IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
144 };
145
146 MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
147 MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
148 module_param_named(max_speed, ipr_max_speed, uint, 0);
149 MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
150 module_param_named(log_level, ipr_log_level, uint, 0);
151 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
152 module_param_named(testmode, ipr_testmode, int, 0);
153 MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
154 module_param_named(fastfail, ipr_fastfail, int, 0);
155 MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
156 module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
157 MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
158 module_param_named(enable_cache, ipr_enable_cache, int, 0);
159 MODULE_PARM_DESC(enable_cache, "Enable adapter's non-volatile write cache (default: 1)");
160 module_param_named(debug, ipr_debug, int, 0);
161 MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
162 module_param_named(dual_ioa_raid, ipr_dual_ioa_raid, int, 0);
163 MODULE_PARM_DESC(dual_ioa_raid, "Enable dual adapter RAID support. Set to 1 to enable. (default: 1)");
164 MODULE_LICENSE("GPL");
165 MODULE_VERSION(IPR_DRIVER_VERSION);
166
167 /*  A constant array of IOASCs/URCs/Error Messages */
168 static const
169 struct ipr_error_table_t ipr_error_table[] = {
170         {0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
171         "8155: An unknown error was received"},
172         {0x00330000, 0, 0,
173         "Soft underlength error"},
174         {0x005A0000, 0, 0,
175         "Command to be cancelled not found"},
176         {0x00808000, 0, 0,
177         "Qualified success"},
178         {0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
179         "FFFE: Soft device bus error recovered by the IOA"},
180         {0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
181         "4101: Soft device bus fabric error"},
182         {0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
183         "FFF9: Device sector reassign successful"},
184         {0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
185         "FFF7: Media error recovered by device rewrite procedures"},
186         {0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
187         "7001: IOA sector reassignment successful"},
188         {0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
189         "FFF9: Soft media error. Sector reassignment recommended"},
190         {0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
191         "FFF7: Media error recovered by IOA rewrite procedures"},
192         {0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
193         "FF3D: Soft PCI bus error recovered by the IOA"},
194         {0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
195         "FFF6: Device hardware error recovered by the IOA"},
196         {0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
197         "FFF6: Device hardware error recovered by the device"},
198         {0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
199         "FF3D: Soft IOA error recovered by the IOA"},
200         {0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
201         "FFFA: Undefined device response recovered by the IOA"},
202         {0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
203         "FFF6: Device bus error, message or command phase"},
204         {0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
205         "FFFE: Task Management Function failed"},
206         {0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
207         "FFF6: Failure prediction threshold exceeded"},
208         {0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
209         "8009: Impending cache battery pack failure"},
210         {0x02040400, 0, 0,
211         "34FF: Disk device format in progress"},
212         {0x02048000, 0, IPR_DEFAULT_LOG_LEVEL,
213         "9070: IOA requested reset"},
214         {0x023F0000, 0, 0,
215         "Synchronization required"},
216         {0x024E0000, 0, 0,
217         "No ready, IOA shutdown"},
218         {0x025A0000, 0, 0,
219         "Not ready, IOA has been shutdown"},
220         {0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
221         "3020: Storage subsystem configuration error"},
222         {0x03110B00, 0, 0,
223         "FFF5: Medium error, data unreadable, recommend reassign"},
224         {0x03110C00, 0, 0,
225         "7000: Medium error, data unreadable, do not reassign"},
226         {0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
227         "FFF3: Disk media format bad"},
228         {0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
229         "3002: Addressed device failed to respond to selection"},
230         {0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
231         "3100: Device bus error"},
232         {0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
233         "3109: IOA timed out a device command"},
234         {0x04088000, 0, 0,
235         "3120: SCSI bus is not operational"},
236         {0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
237         "4100: Hard device bus fabric error"},
238         {0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
239         "9000: IOA reserved area data check"},
240         {0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
241         "9001: IOA reserved area invalid data pattern"},
242         {0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
243         "9002: IOA reserved area LRC error"},
244         {0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
245         "102E: Out of alternate sectors for disk storage"},
246         {0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
247         "FFF4: Data transfer underlength error"},
248         {0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
249         "FFF4: Data transfer overlength error"},
250         {0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
251         "3400: Logical unit failure"},
252         {0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
253         "FFF4: Device microcode is corrupt"},
254         {0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
255         "8150: PCI bus error"},
256         {0x04430000, 1, 0,
257         "Unsupported device bus message received"},
258         {0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
259         "FFF4: Disk device problem"},
260         {0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
261         "8150: Permanent IOA failure"},
262         {0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
263         "3010: Disk device returned wrong response to IOA"},
264         {0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
265         "8151: IOA microcode error"},
266         {0x04448500, 0, 0,
267         "Device bus status error"},
268         {0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
269         "8157: IOA error requiring IOA reset to recover"},
270         {0x04448700, 0, 0,
271         "ATA device status error"},
272         {0x04490000, 0, 0,
273         "Message reject received from the device"},
274         {0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
275         "8008: A permanent cache battery pack failure occurred"},
276         {0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
277         "9090: Disk unit has been modified after the last known status"},
278         {0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
279         "9081: IOA detected device error"},
280         {0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
281         "9082: IOA detected device error"},
282         {0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
283         "3110: Device bus error, message or command phase"},
284         {0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
285         "3110: SAS Command / Task Management Function failed"},
286         {0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
287         "9091: Incorrect hardware configuration change has been detected"},
288         {0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
289         "9073: Invalid multi-adapter configuration"},
290         {0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
291         "4010: Incorrect connection between cascaded expanders"},
292         {0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
293         "4020: Connections exceed IOA design limits"},
294         {0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
295         "4030: Incorrect multipath connection"},
296         {0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
297         "4110: Unsupported enclosure function"},
298         {0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
299         "FFF4: Command to logical unit failed"},
300         {0x05240000, 1, 0,
301         "Illegal request, invalid request type or request packet"},
302         {0x05250000, 0, 0,
303         "Illegal request, invalid resource handle"},
304         {0x05258000, 0, 0,
305         "Illegal request, commands not allowed to this device"},
306         {0x05258100, 0, 0,
307         "Illegal request, command not allowed to a secondary adapter"},
308         {0x05260000, 0, 0,
309         "Illegal request, invalid field in parameter list"},
310         {0x05260100, 0, 0,
311         "Illegal request, parameter not supported"},
312         {0x05260200, 0, 0,
313         "Illegal request, parameter value invalid"},
314         {0x052C0000, 0, 0,
315         "Illegal request, command sequence error"},
316         {0x052C8000, 1, 0,
317         "Illegal request, dual adapter support not enabled"},
318         {0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
319         "9031: Array protection temporarily suspended, protection resuming"},
320         {0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
321         "9040: Array protection temporarily suspended, protection resuming"},
322         {0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
323         "3140: Device bus not ready to ready transition"},
324         {0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
325         "FFFB: SCSI bus was reset"},
326         {0x06290500, 0, 0,
327         "FFFE: SCSI bus transition to single ended"},
328         {0x06290600, 0, 0,
329         "FFFE: SCSI bus transition to LVD"},
330         {0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
331         "FFFB: SCSI bus was reset by another initiator"},
332         {0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
333         "3029: A device replacement has occurred"},
334         {0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
335         "9051: IOA cache data exists for a missing or failed device"},
336         {0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
337         "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
338         {0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
339         "9025: Disk unit is not supported at its physical location"},
340         {0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
341         "3020: IOA detected a SCSI bus configuration error"},
342         {0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
343         "3150: SCSI bus configuration error"},
344         {0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
345         "9074: Asymmetric advanced function disk configuration"},
346         {0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
347         "4040: Incomplete multipath connection between IOA and enclosure"},
348         {0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
349         "4041: Incomplete multipath connection between enclosure and device"},
350         {0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
351         "9075: Incomplete multipath connection between IOA and remote IOA"},
352         {0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
353         "9076: Configuration error, missing remote IOA"},
354         {0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
355         "4050: Enclosure does not support a required multipath function"},
356         {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
357         "9041: Array protection temporarily suspended"},
358         {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
359         "9042: Corrupt array parity detected on specified device"},
360         {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
361         "9030: Array no longer protected due to missing or failed disk unit"},
362         {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
363         "9071: Link operational transition"},
364         {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
365         "9072: Link not operational transition"},
366         {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
367         "9032: Array exposed but still protected"},
368         {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
369         "70DD: Device forced failed by disrupt device command"},
370         {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
371         "4061: Multipath redundancy level got better"},
372         {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
373         "4060: Multipath redundancy level got worse"},
374         {0x07270000, 0, 0,
375         "Failure due to other device"},
376         {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
377         "9008: IOA does not support functions expected by devices"},
378         {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
379         "9010: Cache data associated with attached devices cannot be found"},
380         {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
381         "9011: Cache data belongs to devices other than those attached"},
382         {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
383         "9020: Array missing 2 or more devices with only 1 device present"},
384         {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
385         "9021: Array missing 2 or more devices with 2 or more devices present"},
386         {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
387         "9022: Exposed array is missing a required device"},
388         {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
389         "9023: Array member(s) not at required physical locations"},
390         {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
391         "9024: Array not functional due to present hardware configuration"},
392         {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
393         "9026: Array not functional due to present hardware configuration"},
394         {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
395         "9027: Array is missing a device and parity is out of sync"},
396         {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
397         "9028: Maximum number of arrays already exist"},
398         {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
399         "9050: Required cache data cannot be located for a disk unit"},
400         {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
401         "9052: Cache data exists for a device that has been modified"},
402         {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
403         "9054: IOA resources not available due to previous problems"},
404         {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
405         "9092: Disk unit requires initialization before use"},
406         {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
407         "9029: Incorrect hardware configuration change has been detected"},
408         {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
409         "9060: One or more disk pairs are missing from an array"},
410         {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
411         "9061: One or more disks are missing from an array"},
412         {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
413         "9062: One or more disks are missing from an array"},
414         {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
415         "9063: Maximum number of functional arrays has been exceeded"},
416         {0x0B260000, 0, 0,
417         "Aborted command, invalid descriptor"},
418         {0x0B5A0000, 0, 0,
419         "Command terminated by host"}
420 };
421
422 static const struct ipr_ses_table_entry ipr_ses_table[] = {
423         { "2104-DL1        ", "XXXXXXXXXXXXXXXX", 80 },
424         { "2104-TL1        ", "XXXXXXXXXXXXXXXX", 80 },
425         { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
426         { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
427         { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
428         { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
429         { "2104-DU3        ", "XXXXXXXXXXXXXXXX", 160 },
430         { "2104-TU3        ", "XXXXXXXXXXXXXXXX", 160 },
431         { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
432         { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
433         { "St  V1S2        ", "XXXXXXXXXXXXXXXX", 160 },
434         { "HSBPD4M  PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
435         { "VSBPD1H   U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
436 };
437
438 /*
439  *  Function Prototypes
440  */
441 static int ipr_reset_alert(struct ipr_cmnd *);
442 static void ipr_process_ccn(struct ipr_cmnd *);
443 static void ipr_process_error(struct ipr_cmnd *);
444 static void ipr_reset_ioa_job(struct ipr_cmnd *);
445 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
446                                    enum ipr_shutdown_type);
447
448 #ifdef CONFIG_SCSI_IPR_TRACE
449 /**
450  * ipr_trc_hook - Add a trace entry to the driver trace
451  * @ipr_cmd:    ipr command struct
452  * @type:               trace type
453  * @add_data:   additional data
454  *
455  * Return value:
456  *      none
457  **/
458 static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
459                          u8 type, u32 add_data)
460 {
461         struct ipr_trace_entry *trace_entry;
462         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
463
464         trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
465         trace_entry->time = jiffies;
466         trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
467         trace_entry->type = type;
468         trace_entry->ata_op_code = ipr_cmd->ioarcb.add_data.u.regs.command;
469         trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
470         trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
471         trace_entry->u.add_data = add_data;
472 }
473 #else
474 #define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
475 #endif
476
477 /**
478  * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
479  * @ipr_cmd:    ipr command struct
480  *
481  * Return value:
482  *      none
483  **/
484 static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
485 {
486         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
487         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
488         dma_addr_t dma_addr = be32_to_cpu(ioarcb->ioarcb_host_pci_addr);
489
490         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
491         ioarcb->write_data_transfer_length = 0;
492         ioarcb->read_data_transfer_length = 0;
493         ioarcb->write_ioadl_len = 0;
494         ioarcb->read_ioadl_len = 0;
495         ioarcb->write_ioadl_addr =
496                 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
497         ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
498         ioasa->ioasc = 0;
499         ioasa->residual_data_len = 0;
500         ioasa->u.gata.status = 0;
501
502         ipr_cmd->scsi_cmd = NULL;
503         ipr_cmd->qc = NULL;
504         ipr_cmd->sense_buffer[0] = 0;
505         ipr_cmd->dma_use_sg = 0;
506 }
507
508 /**
509  * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
510  * @ipr_cmd:    ipr command struct
511  *
512  * Return value:
513  *      none
514  **/
515 static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
516 {
517         ipr_reinit_ipr_cmnd(ipr_cmd);
518         ipr_cmd->u.scratch = 0;
519         ipr_cmd->sibling = NULL;
520         init_timer(&ipr_cmd->timer);
521 }
522
523 /**
524  * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
525  * @ioa_cfg:    ioa config struct
526  *
527  * Return value:
528  *      pointer to ipr command struct
529  **/
530 static
531 struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
532 {
533         struct ipr_cmnd *ipr_cmd;
534
535         ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
536         list_del(&ipr_cmd->queue);
537         ipr_init_ipr_cmnd(ipr_cmd);
538
539         return ipr_cmd;
540 }
541
542 /**
543  * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
544  * @ioa_cfg:    ioa config struct
545  * @clr_ints:     interrupts to clear
546  *
547  * This function masks all interrupts on the adapter, then clears the
548  * interrupts specified in the mask
549  *
550  * Return value:
551  *      none
552  **/
553 static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
554                                           u32 clr_ints)
555 {
556         volatile u32 int_reg;
557
558         /* Stop new interrupts */
559         ioa_cfg->allow_interrupts = 0;
560
561         /* Set interrupt mask to stop all new interrupts */
562         writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
563
564         /* Clear any pending interrupts */
565         writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg);
566         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
567 }
568
569 /**
570  * ipr_save_pcix_cmd_reg - Save PCI-X command register
571  * @ioa_cfg:    ioa config struct
572  *
573  * Return value:
574  *      0 on success / -EIO on failure
575  **/
576 static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
577 {
578         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
579
580         if (pcix_cmd_reg == 0)
581                 return 0;
582
583         if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
584                                  &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
585                 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
586                 return -EIO;
587         }
588
589         ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
590         return 0;
591 }
592
593 /**
594  * ipr_set_pcix_cmd_reg - Setup PCI-X command register
595  * @ioa_cfg:    ioa config struct
596  *
597  * Return value:
598  *      0 on success / -EIO on failure
599  **/
600 static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
601 {
602         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
603
604         if (pcix_cmd_reg) {
605                 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
606                                           ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
607                         dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
608                         return -EIO;
609                 }
610         }
611
612         return 0;
613 }
614
615 /**
616  * ipr_sata_eh_done - done function for aborted SATA commands
617  * @ipr_cmd:    ipr command struct
618  *
619  * This function is invoked for ops generated to SATA
620  * devices which are being aborted.
621  *
622  * Return value:
623  *      none
624  **/
625 static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
626 {
627         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
628         struct ata_queued_cmd *qc = ipr_cmd->qc;
629         struct ipr_sata_port *sata_port = qc->ap->private_data;
630
631         qc->err_mask |= AC_ERR_OTHER;
632         sata_port->ioasa.status |= ATA_BUSY;
633         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
634         ata_qc_complete(qc);
635 }
636
637 /**
638  * ipr_scsi_eh_done - mid-layer done function for aborted ops
639  * @ipr_cmd:    ipr command struct
640  *
641  * This function is invoked by the interrupt handler for
642  * ops generated by the SCSI mid-layer which are being aborted.
643  *
644  * Return value:
645  *      none
646  **/
647 static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
648 {
649         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
650         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
651
652         scsi_cmd->result |= (DID_ERROR << 16);
653
654         scsi_dma_unmap(ipr_cmd->scsi_cmd);
655         scsi_cmd->scsi_done(scsi_cmd);
656         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
657 }
658
659 /**
660  * ipr_fail_all_ops - Fails all outstanding ops.
661  * @ioa_cfg:    ioa config struct
662  *
663  * This function fails all outstanding ops.
664  *
665  * Return value:
666  *      none
667  **/
668 static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
669 {
670         struct ipr_cmnd *ipr_cmd, *temp;
671
672         ENTER;
673         list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
674                 list_del(&ipr_cmd->queue);
675
676                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
677                 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
678
679                 if (ipr_cmd->scsi_cmd)
680                         ipr_cmd->done = ipr_scsi_eh_done;
681                 else if (ipr_cmd->qc)
682                         ipr_cmd->done = ipr_sata_eh_done;
683
684                 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
685                 del_timer(&ipr_cmd->timer);
686                 ipr_cmd->done(ipr_cmd);
687         }
688
689         LEAVE;
690 }
691
692 /**
693  * ipr_do_req -  Send driver initiated requests.
694  * @ipr_cmd:            ipr command struct
695  * @done:                       done function
696  * @timeout_func:       timeout function
697  * @timeout:            timeout value
698  *
699  * This function sends the specified command to the adapter with the
700  * timeout given. The done function is invoked on command completion.
701  *
702  * Return value:
703  *      none
704  **/
705 static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
706                        void (*done) (struct ipr_cmnd *),
707                        void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
708 {
709         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
710
711         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
712
713         ipr_cmd->done = done;
714
715         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
716         ipr_cmd->timer.expires = jiffies + timeout;
717         ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
718
719         add_timer(&ipr_cmd->timer);
720
721         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
722
723         mb();
724         writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
725                ioa_cfg->regs.ioarrin_reg);
726 }
727
728 /**
729  * ipr_internal_cmd_done - Op done function for an internally generated op.
730  * @ipr_cmd:    ipr command struct
731  *
732  * This function is the op done function for an internally generated,
733  * blocking op. It simply wakes the sleeping thread.
734  *
735  * Return value:
736  *      none
737  **/
738 static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
739 {
740         if (ipr_cmd->sibling)
741                 ipr_cmd->sibling = NULL;
742         else
743                 complete(&ipr_cmd->completion);
744 }
745
746 /**
747  * ipr_send_blocking_cmd - Send command and sleep on its completion.
748  * @ipr_cmd:    ipr command struct
749  * @timeout_func:       function to invoke if command times out
750  * @timeout:    timeout
751  *
752  * Return value:
753  *      none
754  **/
755 static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
756                                   void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
757                                   u32 timeout)
758 {
759         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
760
761         init_completion(&ipr_cmd->completion);
762         ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
763
764         spin_unlock_irq(ioa_cfg->host->host_lock);
765         wait_for_completion(&ipr_cmd->completion);
766         spin_lock_irq(ioa_cfg->host->host_lock);
767 }
768
769 /**
770  * ipr_send_hcam - Send an HCAM to the adapter.
771  * @ioa_cfg:    ioa config struct
772  * @type:               HCAM type
773  * @hostrcb:    hostrcb struct
774  *
775  * This function will send a Host Controlled Async command to the adapter.
776  * If HCAMs are currently not allowed to be issued to the adapter, it will
777  * place the hostrcb on the free queue.
778  *
779  * Return value:
780  *      none
781  **/
782 static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
783                           struct ipr_hostrcb *hostrcb)
784 {
785         struct ipr_cmnd *ipr_cmd;
786         struct ipr_ioarcb *ioarcb;
787
788         if (ioa_cfg->allow_cmds) {
789                 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
790                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
791                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
792
793                 ipr_cmd->u.hostrcb = hostrcb;
794                 ioarcb = &ipr_cmd->ioarcb;
795
796                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
797                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
798                 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
799                 ioarcb->cmd_pkt.cdb[1] = type;
800                 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
801                 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
802
803                 ioarcb->read_data_transfer_length = cpu_to_be32(sizeof(hostrcb->hcam));
804                 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
805                 ipr_cmd->ioadl[0].flags_and_data_len =
806                         cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(hostrcb->hcam));
807                 ipr_cmd->ioadl[0].address = cpu_to_be32(hostrcb->hostrcb_dma);
808
809                 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
810                         ipr_cmd->done = ipr_process_ccn;
811                 else
812                         ipr_cmd->done = ipr_process_error;
813
814                 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
815
816                 mb();
817                 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
818                        ioa_cfg->regs.ioarrin_reg);
819         } else {
820                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
821         }
822 }
823
824 /**
825  * ipr_init_res_entry - Initialize a resource entry struct.
826  * @res:        resource entry struct
827  *
828  * Return value:
829  *      none
830  **/
831 static void ipr_init_res_entry(struct ipr_resource_entry *res)
832 {
833         res->needs_sync_complete = 0;
834         res->in_erp = 0;
835         res->add_to_ml = 0;
836         res->del_from_ml = 0;
837         res->resetting_device = 0;
838         res->sdev = NULL;
839         res->sata_port = NULL;
840 }
841
842 /**
843  * ipr_handle_config_change - Handle a config change from the adapter
844  * @ioa_cfg:    ioa config struct
845  * @hostrcb:    hostrcb
846  *
847  * Return value:
848  *      none
849  **/
850 static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
851                               struct ipr_hostrcb *hostrcb)
852 {
853         struct ipr_resource_entry *res = NULL;
854         struct ipr_config_table_entry *cfgte;
855         u32 is_ndn = 1;
856
857         cfgte = &hostrcb->hcam.u.ccn.cfgte;
858
859         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
860                 if (!memcmp(&res->cfgte.res_addr, &cfgte->res_addr,
861                             sizeof(cfgte->res_addr))) {
862                         is_ndn = 0;
863                         break;
864                 }
865         }
866
867         if (is_ndn) {
868                 if (list_empty(&ioa_cfg->free_res_q)) {
869                         ipr_send_hcam(ioa_cfg,
870                                       IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
871                                       hostrcb);
872                         return;
873                 }
874
875                 res = list_entry(ioa_cfg->free_res_q.next,
876                                  struct ipr_resource_entry, queue);
877
878                 list_del(&res->queue);
879                 ipr_init_res_entry(res);
880                 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
881         }
882
883         memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
884
885         if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
886                 if (res->sdev) {
887                         res->del_from_ml = 1;
888                         res->cfgte.res_handle = IPR_INVALID_RES_HANDLE;
889                         if (ioa_cfg->allow_ml_add_del)
890                                 schedule_work(&ioa_cfg->work_q);
891                 } else
892                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
893         } else if (!res->sdev) {
894                 res->add_to_ml = 1;
895                 if (ioa_cfg->allow_ml_add_del)
896                         schedule_work(&ioa_cfg->work_q);
897         }
898
899         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
900 }
901
902 /**
903  * ipr_process_ccn - Op done function for a CCN.
904  * @ipr_cmd:    ipr command struct
905  *
906  * This function is the op done function for a configuration
907  * change notification host controlled async from the adapter.
908  *
909  * Return value:
910  *      none
911  **/
912 static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
913 {
914         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
915         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
916         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
917
918         list_del(&hostrcb->queue);
919         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
920
921         if (ioasc) {
922                 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
923                         dev_err(&ioa_cfg->pdev->dev,
924                                 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
925
926                 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
927         } else {
928                 ipr_handle_config_change(ioa_cfg, hostrcb);
929         }
930 }
931
932 /**
933  * strip_and_pad_whitespace - Strip and pad trailing whitespace.
934  * @i:          index into buffer
935  * @buf:                string to modify
936  *
937  * This function will strip all trailing whitespace, pad the end
938  * of the string with a single space, and NULL terminate the string.
939  *
940  * Return value:
941  *      new length of string
942  **/
943 static int strip_and_pad_whitespace(int i, char *buf)
944 {
945         while (i && buf[i] == ' ')
946                 i--;
947         buf[i+1] = ' ';
948         buf[i+2] = '\0';
949         return i + 2;
950 }
951
952 /**
953  * ipr_log_vpd_compact - Log the passed extended VPD compactly.
954  * @prefix:             string to print at start of printk
955  * @hostrcb:    hostrcb pointer
956  * @vpd:                vendor/product id/sn struct
957  *
958  * Return value:
959  *      none
960  **/
961 static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
962                                 struct ipr_vpd *vpd)
963 {
964         char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
965         int i = 0;
966
967         memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
968         i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
969
970         memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
971         i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
972
973         memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
974         buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
975
976         ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
977 }
978
979 /**
980  * ipr_log_vpd - Log the passed VPD to the error log.
981  * @vpd:                vendor/product id/sn struct
982  *
983  * Return value:
984  *      none
985  **/
986 static void ipr_log_vpd(struct ipr_vpd *vpd)
987 {
988         char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
989                     + IPR_SERIAL_NUM_LEN];
990
991         memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
992         memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
993                IPR_PROD_ID_LEN);
994         buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
995         ipr_err("Vendor/Product ID: %s\n", buffer);
996
997         memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
998         buffer[IPR_SERIAL_NUM_LEN] = '\0';
999         ipr_err("    Serial Number: %s\n", buffer);
1000 }
1001
1002 /**
1003  * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
1004  * @prefix:             string to print at start of printk
1005  * @hostrcb:    hostrcb pointer
1006  * @vpd:                vendor/product id/sn/wwn struct
1007  *
1008  * Return value:
1009  *      none
1010  **/
1011 static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1012                                     struct ipr_ext_vpd *vpd)
1013 {
1014         ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
1015         ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
1016                      be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
1017 }
1018
1019 /**
1020  * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
1021  * @vpd:                vendor/product id/sn/wwn struct
1022  *
1023  * Return value:
1024  *      none
1025  **/
1026 static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
1027 {
1028         ipr_log_vpd(&vpd->vpd);
1029         ipr_err("    WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
1030                 be32_to_cpu(vpd->wwid[1]));
1031 }
1032
1033 /**
1034  * ipr_log_enhanced_cache_error - Log a cache error.
1035  * @ioa_cfg:    ioa config struct
1036  * @hostrcb:    hostrcb struct
1037  *
1038  * Return value:
1039  *      none
1040  **/
1041 static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1042                                          struct ipr_hostrcb *hostrcb)
1043 {
1044         struct ipr_hostrcb_type_12_error *error =
1045                 &hostrcb->hcam.u.error.u.type_12_error;
1046
1047         ipr_err("-----Current Configuration-----\n");
1048         ipr_err("Cache Directory Card Information:\n");
1049         ipr_log_ext_vpd(&error->ioa_vpd);
1050         ipr_err("Adapter Card Information:\n");
1051         ipr_log_ext_vpd(&error->cfc_vpd);
1052
1053         ipr_err("-----Expected Configuration-----\n");
1054         ipr_err("Cache Directory Card Information:\n");
1055         ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
1056         ipr_err("Adapter Card Information:\n");
1057         ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
1058
1059         ipr_err("Additional IOA Data: %08X %08X %08X\n",
1060                      be32_to_cpu(error->ioa_data[0]),
1061                      be32_to_cpu(error->ioa_data[1]),
1062                      be32_to_cpu(error->ioa_data[2]));
1063 }
1064
1065 /**
1066  * ipr_log_cache_error - Log a cache error.
1067  * @ioa_cfg:    ioa config struct
1068  * @hostrcb:    hostrcb struct
1069  *
1070  * Return value:
1071  *      none
1072  **/
1073 static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1074                                 struct ipr_hostrcb *hostrcb)
1075 {
1076         struct ipr_hostrcb_type_02_error *error =
1077                 &hostrcb->hcam.u.error.u.type_02_error;
1078
1079         ipr_err("-----Current Configuration-----\n");
1080         ipr_err("Cache Directory Card Information:\n");
1081         ipr_log_vpd(&error->ioa_vpd);
1082         ipr_err("Adapter Card Information:\n");
1083         ipr_log_vpd(&error->cfc_vpd);
1084
1085         ipr_err("-----Expected Configuration-----\n");
1086         ipr_err("Cache Directory Card Information:\n");
1087         ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
1088         ipr_err("Adapter Card Information:\n");
1089         ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
1090
1091         ipr_err("Additional IOA Data: %08X %08X %08X\n",
1092                      be32_to_cpu(error->ioa_data[0]),
1093                      be32_to_cpu(error->ioa_data[1]),
1094                      be32_to_cpu(error->ioa_data[2]));
1095 }
1096
1097 /**
1098  * ipr_log_enhanced_config_error - Log a configuration error.
1099  * @ioa_cfg:    ioa config struct
1100  * @hostrcb:    hostrcb struct
1101  *
1102  * Return value:
1103  *      none
1104  **/
1105 static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
1106                                           struct ipr_hostrcb *hostrcb)
1107 {
1108         int errors_logged, i;
1109         struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
1110         struct ipr_hostrcb_type_13_error *error;
1111
1112         error = &hostrcb->hcam.u.error.u.type_13_error;
1113         errors_logged = be32_to_cpu(error->errors_logged);
1114
1115         ipr_err("Device Errors Detected/Logged: %d/%d\n",
1116                 be32_to_cpu(error->errors_detected), errors_logged);
1117
1118         dev_entry = error->dev;
1119
1120         for (i = 0; i < errors_logged; i++, dev_entry++) {
1121                 ipr_err_separator;
1122
1123                 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1124                 ipr_log_ext_vpd(&dev_entry->vpd);
1125
1126                 ipr_err("-----New Device Information-----\n");
1127                 ipr_log_ext_vpd(&dev_entry->new_vpd);
1128
1129                 ipr_err("Cache Directory Card Information:\n");
1130                 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1131
1132                 ipr_err("Adapter Card Information:\n");
1133                 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1134         }
1135 }
1136
1137 /**
1138  * ipr_log_config_error - Log a configuration error.
1139  * @ioa_cfg:    ioa config struct
1140  * @hostrcb:    hostrcb struct
1141  *
1142  * Return value:
1143  *      none
1144  **/
1145 static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
1146                                  struct ipr_hostrcb *hostrcb)
1147 {
1148         int errors_logged, i;
1149         struct ipr_hostrcb_device_data_entry *dev_entry;
1150         struct ipr_hostrcb_type_03_error *error;
1151
1152         error = &hostrcb->hcam.u.error.u.type_03_error;
1153         errors_logged = be32_to_cpu(error->errors_logged);
1154
1155         ipr_err("Device Errors Detected/Logged: %d/%d\n",
1156                 be32_to_cpu(error->errors_detected), errors_logged);
1157
1158         dev_entry = error->dev;
1159
1160         for (i = 0; i < errors_logged; i++, dev_entry++) {
1161                 ipr_err_separator;
1162
1163                 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1164                 ipr_log_vpd(&dev_entry->vpd);
1165
1166                 ipr_err("-----New Device Information-----\n");
1167                 ipr_log_vpd(&dev_entry->new_vpd);
1168
1169                 ipr_err("Cache Directory Card Information:\n");
1170                 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
1171
1172                 ipr_err("Adapter Card Information:\n");
1173                 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
1174
1175                 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1176                         be32_to_cpu(dev_entry->ioa_data[0]),
1177                         be32_to_cpu(dev_entry->ioa_data[1]),
1178                         be32_to_cpu(dev_entry->ioa_data[2]),
1179                         be32_to_cpu(dev_entry->ioa_data[3]),
1180                         be32_to_cpu(dev_entry->ioa_data[4]));
1181         }
1182 }
1183
1184 /**
1185  * ipr_log_enhanced_array_error - Log an array configuration error.
1186  * @ioa_cfg:    ioa config struct
1187  * @hostrcb:    hostrcb struct
1188  *
1189  * Return value:
1190  *      none
1191  **/
1192 static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
1193                                          struct ipr_hostrcb *hostrcb)
1194 {
1195         int i, num_entries;
1196         struct ipr_hostrcb_type_14_error *error;
1197         struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
1198         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1199
1200         error = &hostrcb->hcam.u.error.u.type_14_error;
1201
1202         ipr_err_separator;
1203
1204         ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1205                 error->protection_level,
1206                 ioa_cfg->host->host_no,
1207                 error->last_func_vset_res_addr.bus,
1208                 error->last_func_vset_res_addr.target,
1209                 error->last_func_vset_res_addr.lun);
1210
1211         ipr_err_separator;
1212
1213         array_entry = error->array_member;
1214         num_entries = min_t(u32, be32_to_cpu(error->num_entries),
1215                             sizeof(error->array_member));
1216
1217         for (i = 0; i < num_entries; i++, array_entry++) {
1218                 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1219                         continue;
1220
1221                 if (be32_to_cpu(error->exposed_mode_adn) == i)
1222                         ipr_err("Exposed Array Member %d:\n", i);
1223                 else
1224                         ipr_err("Array Member %d:\n", i);
1225
1226                 ipr_log_ext_vpd(&array_entry->vpd);
1227                 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1228                 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1229                                  "Expected Location");
1230
1231                 ipr_err_separator;
1232         }
1233 }
1234
1235 /**
1236  * ipr_log_array_error - Log an array configuration error.
1237  * @ioa_cfg:    ioa config struct
1238  * @hostrcb:    hostrcb struct
1239  *
1240  * Return value:
1241  *      none
1242  **/
1243 static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1244                                 struct ipr_hostrcb *hostrcb)
1245 {
1246         int i;
1247         struct ipr_hostrcb_type_04_error *error;
1248         struct ipr_hostrcb_array_data_entry *array_entry;
1249         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1250
1251         error = &hostrcb->hcam.u.error.u.type_04_error;
1252
1253         ipr_err_separator;
1254
1255         ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1256                 error->protection_level,
1257                 ioa_cfg->host->host_no,
1258                 error->last_func_vset_res_addr.bus,
1259                 error->last_func_vset_res_addr.target,
1260                 error->last_func_vset_res_addr.lun);
1261
1262         ipr_err_separator;
1263
1264         array_entry = error->array_member;
1265
1266         for (i = 0; i < 18; i++) {
1267                 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1268                         continue;
1269
1270                 if (be32_to_cpu(error->exposed_mode_adn) == i)
1271                         ipr_err("Exposed Array Member %d:\n", i);
1272                 else
1273                         ipr_err("Array Member %d:\n", i);
1274
1275                 ipr_log_vpd(&array_entry->vpd);
1276
1277                 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1278                 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1279                                  "Expected Location");
1280
1281                 ipr_err_separator;
1282
1283                 if (i == 9)
1284                         array_entry = error->array_member2;
1285                 else
1286                         array_entry++;
1287         }
1288 }
1289
1290 /**
1291  * ipr_log_hex_data - Log additional hex IOA error data.
1292  * @ioa_cfg:    ioa config struct
1293  * @data:               IOA error data
1294  * @len:                data length
1295  *
1296  * Return value:
1297  *      none
1298  **/
1299 static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
1300 {
1301         int i;
1302
1303         if (len == 0)
1304                 return;
1305
1306         if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
1307                 len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
1308
1309         for (i = 0; i < len / 4; i += 4) {
1310                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1311                         be32_to_cpu(data[i]),
1312                         be32_to_cpu(data[i+1]),
1313                         be32_to_cpu(data[i+2]),
1314                         be32_to_cpu(data[i+3]));
1315         }
1316 }
1317
1318 /**
1319  * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
1320  * @ioa_cfg:    ioa config struct
1321  * @hostrcb:    hostrcb struct
1322  *
1323  * Return value:
1324  *      none
1325  **/
1326 static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1327                                             struct ipr_hostrcb *hostrcb)
1328 {
1329         struct ipr_hostrcb_type_17_error *error;
1330
1331         error = &hostrcb->hcam.u.error.u.type_17_error;
1332         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1333         strstrip(error->failure_reason);
1334
1335         ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1336                      be32_to_cpu(hostrcb->hcam.u.error.prc));
1337         ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1338         ipr_log_hex_data(ioa_cfg, error->data,
1339                          be32_to_cpu(hostrcb->hcam.length) -
1340                          (offsetof(struct ipr_hostrcb_error, u) +
1341                           offsetof(struct ipr_hostrcb_type_17_error, data)));
1342 }
1343
1344 /**
1345  * ipr_log_dual_ioa_error - Log a dual adapter error.
1346  * @ioa_cfg:    ioa config struct
1347  * @hostrcb:    hostrcb struct
1348  *
1349  * Return value:
1350  *      none
1351  **/
1352 static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1353                                    struct ipr_hostrcb *hostrcb)
1354 {
1355         struct ipr_hostrcb_type_07_error *error;
1356
1357         error = &hostrcb->hcam.u.error.u.type_07_error;
1358         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1359         strstrip(error->failure_reason);
1360
1361         ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1362                      be32_to_cpu(hostrcb->hcam.u.error.prc));
1363         ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1364         ipr_log_hex_data(ioa_cfg, error->data,
1365                          be32_to_cpu(hostrcb->hcam.length) -
1366                          (offsetof(struct ipr_hostrcb_error, u) +
1367                           offsetof(struct ipr_hostrcb_type_07_error, data)));
1368 }
1369
1370 static const struct {
1371         u8 active;
1372         char *desc;
1373 } path_active_desc[] = {
1374         { IPR_PATH_NO_INFO, "Path" },
1375         { IPR_PATH_ACTIVE, "Active path" },
1376         { IPR_PATH_NOT_ACTIVE, "Inactive path" }
1377 };
1378
1379 static const struct {
1380         u8 state;
1381         char *desc;
1382 } path_state_desc[] = {
1383         { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
1384         { IPR_PATH_HEALTHY, "is healthy" },
1385         { IPR_PATH_DEGRADED, "is degraded" },
1386         { IPR_PATH_FAILED, "is failed" }
1387 };
1388
1389 /**
1390  * ipr_log_fabric_path - Log a fabric path error
1391  * @hostrcb:    hostrcb struct
1392  * @fabric:             fabric descriptor
1393  *
1394  * Return value:
1395  *      none
1396  **/
1397 static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
1398                                 struct ipr_hostrcb_fabric_desc *fabric)
1399 {
1400         int i, j;
1401         u8 path_state = fabric->path_state;
1402         u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1403         u8 state = path_state & IPR_PATH_STATE_MASK;
1404
1405         for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1406                 if (path_active_desc[i].active != active)
1407                         continue;
1408
1409                 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
1410                         if (path_state_desc[j].state != state)
1411                                 continue;
1412
1413                         if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
1414                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
1415                                              path_active_desc[i].desc, path_state_desc[j].desc,
1416                                              fabric->ioa_port);
1417                         } else if (fabric->cascaded_expander == 0xff) {
1418                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
1419                                              path_active_desc[i].desc, path_state_desc[j].desc,
1420                                              fabric->ioa_port, fabric->phy);
1421                         } else if (fabric->phy == 0xff) {
1422                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
1423                                              path_active_desc[i].desc, path_state_desc[j].desc,
1424                                              fabric->ioa_port, fabric->cascaded_expander);
1425                         } else {
1426                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
1427                                              path_active_desc[i].desc, path_state_desc[j].desc,
1428                                              fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1429                         }
1430                         return;
1431                 }
1432         }
1433
1434         ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
1435                 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1436 }
1437
1438 static const struct {
1439         u8 type;
1440         char *desc;
1441 } path_type_desc[] = {
1442         { IPR_PATH_CFG_IOA_PORT, "IOA port" },
1443         { IPR_PATH_CFG_EXP_PORT, "Expander port" },
1444         { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
1445         { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
1446 };
1447
1448 static const struct {
1449         u8 status;
1450         char *desc;
1451 } path_status_desc[] = {
1452         { IPR_PATH_CFG_NO_PROB, "Functional" },
1453         { IPR_PATH_CFG_DEGRADED, "Degraded" },
1454         { IPR_PATH_CFG_FAILED, "Failed" },
1455         { IPR_PATH_CFG_SUSPECT, "Suspect" },
1456         { IPR_PATH_NOT_DETECTED, "Missing" },
1457         { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
1458 };
1459
1460 static const char *link_rate[] = {
1461         "unknown",
1462         "disabled",
1463         "phy reset problem",
1464         "spinup hold",
1465         "port selector",
1466         "unknown",
1467         "unknown",
1468         "unknown",
1469         "1.5Gbps",
1470         "3.0Gbps",
1471         "unknown",
1472         "unknown",
1473         "unknown",
1474         "unknown",
1475         "unknown",
1476         "unknown"
1477 };
1478
1479 /**
1480  * ipr_log_path_elem - Log a fabric path element.
1481  * @hostrcb:    hostrcb struct
1482  * @cfg:                fabric path element struct
1483  *
1484  * Return value:
1485  *      none
1486  **/
1487 static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
1488                               struct ipr_hostrcb_config_element *cfg)
1489 {
1490         int i, j;
1491         u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
1492         u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
1493
1494         if (type == IPR_PATH_CFG_NOT_EXIST)
1495                 return;
1496
1497         for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
1498                 if (path_type_desc[i].type != type)
1499                         continue;
1500
1501                 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
1502                         if (path_status_desc[j].status != status)
1503                                 continue;
1504
1505                         if (type == IPR_PATH_CFG_IOA_PORT) {
1506                                 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
1507                                              path_status_desc[j].desc, path_type_desc[i].desc,
1508                                              cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1509                                              be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1510                         } else {
1511                                 if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
1512                                         ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
1513                                                      path_status_desc[j].desc, path_type_desc[i].desc,
1514                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1515                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1516                                 } else if (cfg->cascaded_expander == 0xff) {
1517                                         ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
1518                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
1519                                                      path_type_desc[i].desc, cfg->phy,
1520                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1521                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1522                                 } else if (cfg->phy == 0xff) {
1523                                         ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
1524                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
1525                                                      path_type_desc[i].desc, cfg->cascaded_expander,
1526                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1527                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1528                                 } else {
1529                                         ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
1530                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
1531                                                      path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
1532                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1533                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1534                                 }
1535                         }
1536                         return;
1537                 }
1538         }
1539
1540         ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
1541                      "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
1542                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1543                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1544 }
1545
1546 /**
1547  * ipr_log_fabric_error - Log a fabric error.
1548  * @ioa_cfg:    ioa config struct
1549  * @hostrcb:    hostrcb struct
1550  *
1551  * Return value:
1552  *      none
1553  **/
1554 static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
1555                                  struct ipr_hostrcb *hostrcb)
1556 {
1557         struct ipr_hostrcb_type_20_error *error;
1558         struct ipr_hostrcb_fabric_desc *fabric;
1559         struct ipr_hostrcb_config_element *cfg;
1560         int i, add_len;
1561
1562         error = &hostrcb->hcam.u.error.u.type_20_error;
1563         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1564         ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
1565
1566         add_len = be32_to_cpu(hostrcb->hcam.length) -
1567                 (offsetof(struct ipr_hostrcb_error, u) +
1568                  offsetof(struct ipr_hostrcb_type_20_error, desc));
1569
1570         for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
1571                 ipr_log_fabric_path(hostrcb, fabric);
1572                 for_each_fabric_cfg(fabric, cfg)
1573                         ipr_log_path_elem(hostrcb, cfg);
1574
1575                 add_len -= be16_to_cpu(fabric->length);
1576                 fabric = (struct ipr_hostrcb_fabric_desc *)
1577                         ((unsigned long)fabric + be16_to_cpu(fabric->length));
1578         }
1579
1580         ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
1581 }
1582
1583 /**
1584  * ipr_log_generic_error - Log an adapter error.
1585  * @ioa_cfg:    ioa config struct
1586  * @hostrcb:    hostrcb struct
1587  *
1588  * Return value:
1589  *      none
1590  **/
1591 static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1592                                   struct ipr_hostrcb *hostrcb)
1593 {
1594         ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
1595                          be32_to_cpu(hostrcb->hcam.length));
1596 }
1597
1598 /**
1599  * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1600  * @ioasc:      IOASC
1601  *
1602  * This function will return the index of into the ipr_error_table
1603  * for the specified IOASC. If the IOASC is not in the table,
1604  * 0 will be returned, which points to the entry used for unknown errors.
1605  *
1606  * Return value:
1607  *      index into the ipr_error_table
1608  **/
1609 static u32 ipr_get_error(u32 ioasc)
1610 {
1611         int i;
1612
1613         for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
1614                 if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
1615                         return i;
1616
1617         return 0;
1618 }
1619
1620 /**
1621  * ipr_handle_log_data - Log an adapter error.
1622  * @ioa_cfg:    ioa config struct
1623  * @hostrcb:    hostrcb struct
1624  *
1625  * This function logs an adapter error to the system.
1626  *
1627  * Return value:
1628  *      none
1629  **/
1630 static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1631                                 struct ipr_hostrcb *hostrcb)
1632 {
1633         u32 ioasc;
1634         int error_index;
1635
1636         if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1637                 return;
1638
1639         if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1640                 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1641
1642         ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1643
1644         if (ioasc == IPR_IOASC_BUS_WAS_RESET ||
1645             ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER) {
1646                 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
1647                 scsi_report_bus_reset(ioa_cfg->host,
1648                                       hostrcb->hcam.u.error.failing_dev_res_addr.bus);
1649         }
1650
1651         error_index = ipr_get_error(ioasc);
1652
1653         if (!ipr_error_table[error_index].log_hcam)
1654                 return;
1655
1656         ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
1657
1658         /* Set indication we have logged an error */
1659         ioa_cfg->errors_logged++;
1660
1661         if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
1662                 return;
1663         if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
1664                 hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
1665
1666         switch (hostrcb->hcam.overlay_id) {
1667         case IPR_HOST_RCB_OVERLAY_ID_2:
1668                 ipr_log_cache_error(ioa_cfg, hostrcb);
1669                 break;
1670         case IPR_HOST_RCB_OVERLAY_ID_3:
1671                 ipr_log_config_error(ioa_cfg, hostrcb);
1672                 break;
1673         case IPR_HOST_RCB_OVERLAY_ID_4:
1674         case IPR_HOST_RCB_OVERLAY_ID_6:
1675                 ipr_log_array_error(ioa_cfg, hostrcb);
1676                 break;
1677         case IPR_HOST_RCB_OVERLAY_ID_7:
1678                 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
1679                 break;
1680         case IPR_HOST_RCB_OVERLAY_ID_12:
1681                 ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
1682                 break;
1683         case IPR_HOST_RCB_OVERLAY_ID_13:
1684                 ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
1685                 break;
1686         case IPR_HOST_RCB_OVERLAY_ID_14:
1687         case IPR_HOST_RCB_OVERLAY_ID_16:
1688                 ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
1689                 break;
1690         case IPR_HOST_RCB_OVERLAY_ID_17:
1691                 ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
1692                 break;
1693         case IPR_HOST_RCB_OVERLAY_ID_20:
1694                 ipr_log_fabric_error(ioa_cfg, hostrcb);
1695                 break;
1696         case IPR_HOST_RCB_OVERLAY_ID_1:
1697         case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
1698         default:
1699                 ipr_log_generic_error(ioa_cfg, hostrcb);
1700                 break;
1701         }
1702 }
1703
1704 /**
1705  * ipr_process_error - Op done function for an adapter error log.
1706  * @ipr_cmd:    ipr command struct
1707  *
1708  * This function is the op done function for an error log host
1709  * controlled async from the adapter. It will log the error and
1710  * send the HCAM back to the adapter.
1711  *
1712  * Return value:
1713  *      none
1714  **/
1715 static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
1716 {
1717         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1718         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1719         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
1720         u32 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1721
1722         list_del(&hostrcb->queue);
1723         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1724
1725         if (!ioasc) {
1726                 ipr_handle_log_data(ioa_cfg, hostrcb);
1727                 if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
1728                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
1729         } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
1730                 dev_err(&ioa_cfg->pdev->dev,
1731                         "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1732         }
1733
1734         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
1735 }
1736
1737 /**
1738  * ipr_timeout -  An internally generated op has timed out.
1739  * @ipr_cmd:    ipr command struct
1740  *
1741  * This function blocks host requests and initiates an
1742  * adapter reset.
1743  *
1744  * Return value:
1745  *      none
1746  **/
1747 static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
1748 {
1749         unsigned long lock_flags = 0;
1750         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1751
1752         ENTER;
1753         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1754
1755         ioa_cfg->errors_logged++;
1756         dev_err(&ioa_cfg->pdev->dev,
1757                 "Adapter being reset due to command timeout.\n");
1758
1759         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1760                 ioa_cfg->sdt_state = GET_DUMP;
1761
1762         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
1763                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1764
1765         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1766         LEAVE;
1767 }
1768
1769 /**
1770  * ipr_oper_timeout -  Adapter timed out transitioning to operational
1771  * @ipr_cmd:    ipr command struct
1772  *
1773  * This function blocks host requests and initiates an
1774  * adapter reset.
1775  *
1776  * Return value:
1777  *      none
1778  **/
1779 static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
1780 {
1781         unsigned long lock_flags = 0;
1782         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1783
1784         ENTER;
1785         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1786
1787         ioa_cfg->errors_logged++;
1788         dev_err(&ioa_cfg->pdev->dev,
1789                 "Adapter timed out transitioning to operational.\n");
1790
1791         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1792                 ioa_cfg->sdt_state = GET_DUMP;
1793
1794         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
1795                 if (ipr_fastfail)
1796                         ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
1797                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1798         }
1799
1800         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1801         LEAVE;
1802 }
1803
1804 /**
1805  * ipr_reset_reload - Reset/Reload the IOA
1806  * @ioa_cfg:            ioa config struct
1807  * @shutdown_type:      shutdown type
1808  *
1809  * This function resets the adapter and re-initializes it.
1810  * This function assumes that all new host commands have been stopped.
1811  * Return value:
1812  *      SUCCESS / FAILED
1813  **/
1814 static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
1815                             enum ipr_shutdown_type shutdown_type)
1816 {
1817         if (!ioa_cfg->in_reset_reload)
1818                 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
1819
1820         spin_unlock_irq(ioa_cfg->host->host_lock);
1821         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
1822         spin_lock_irq(ioa_cfg->host->host_lock);
1823
1824         /* If we got hit with a host reset while we were already resetting
1825          the adapter for some reason, and the reset failed. */
1826         if (ioa_cfg->ioa_is_dead) {
1827                 ipr_trace;
1828                 return FAILED;
1829         }
1830
1831         return SUCCESS;
1832 }
1833
1834 /**
1835  * ipr_find_ses_entry - Find matching SES in SES table
1836  * @res:        resource entry struct of SES
1837  *
1838  * Return value:
1839  *      pointer to SES table entry / NULL on failure
1840  **/
1841 static const struct ipr_ses_table_entry *
1842 ipr_find_ses_entry(struct ipr_resource_entry *res)
1843 {
1844         int i, j, matches;
1845         const struct ipr_ses_table_entry *ste = ipr_ses_table;
1846
1847         for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
1848                 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
1849                         if (ste->compare_product_id_byte[j] == 'X') {
1850                                 if (res->cfgte.std_inq_data.vpids.product_id[j] == ste->product_id[j])
1851                                         matches++;
1852                                 else
1853                                         break;
1854                         } else
1855                                 matches++;
1856                 }
1857
1858                 if (matches == IPR_PROD_ID_LEN)
1859                         return ste;
1860         }
1861
1862         return NULL;
1863 }
1864
1865 /**
1866  * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
1867  * @ioa_cfg:    ioa config struct
1868  * @bus:                SCSI bus
1869  * @bus_width:  bus width
1870  *
1871  * Return value:
1872  *      SCSI bus speed in units of 100KHz, 1600 is 160 MHz
1873  *      For a 2-byte wide SCSI bus, the maximum transfer speed is
1874  *      twice the maximum transfer rate (e.g. for a wide enabled bus,
1875  *      max 160MHz = max 320MB/sec).
1876  **/
1877 static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
1878 {
1879         struct ipr_resource_entry *res;
1880         const struct ipr_ses_table_entry *ste;
1881         u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
1882
1883         /* Loop through each config table entry in the config table buffer */
1884         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1885                 if (!(IPR_IS_SES_DEVICE(res->cfgte.std_inq_data)))
1886                         continue;
1887
1888                 if (bus != res->cfgte.res_addr.bus)
1889                         continue;
1890
1891                 if (!(ste = ipr_find_ses_entry(res)))
1892                         continue;
1893
1894                 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
1895         }
1896
1897         return max_xfer_rate;
1898 }
1899
1900 /**
1901  * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
1902  * @ioa_cfg:            ioa config struct
1903  * @max_delay:          max delay in micro-seconds to wait
1904  *
1905  * Waits for an IODEBUG ACK from the IOA, doing busy looping.
1906  *
1907  * Return value:
1908  *      0 on success / other on failure
1909  **/
1910 static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
1911 {
1912         volatile u32 pcii_reg;
1913         int delay = 1;
1914
1915         /* Read interrupt reg until IOA signals IO Debug Acknowledge */
1916         while (delay < max_delay) {
1917                 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
1918
1919                 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
1920                         return 0;
1921
1922                 /* udelay cannot be used if delay is more than a few milliseconds */
1923                 if ((delay / 1000) > MAX_UDELAY_MS)
1924                         mdelay(delay / 1000);
1925                 else
1926                         udelay(delay);
1927
1928                 delay += delay;
1929         }
1930         return -EIO;
1931 }
1932
1933 /**
1934  * ipr_get_ldump_data_section - Dump IOA memory
1935  * @ioa_cfg:                    ioa config struct
1936  * @start_addr:                 adapter address to dump
1937  * @dest:                               destination kernel buffer
1938  * @length_in_words:    length to dump in 4 byte words
1939  *
1940  * Return value:
1941  *      0 on success / -EIO on failure
1942  **/
1943 static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
1944                                       u32 start_addr,
1945                                       __be32 *dest, u32 length_in_words)
1946 {
1947         volatile u32 temp_pcii_reg;
1948         int i, delay = 0;
1949
1950         /* Write IOA interrupt reg starting LDUMP state  */
1951         writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
1952                ioa_cfg->regs.set_uproc_interrupt_reg);
1953
1954         /* Wait for IO debug acknowledge */
1955         if (ipr_wait_iodbg_ack(ioa_cfg,
1956                                IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
1957                 dev_err(&ioa_cfg->pdev->dev,
1958                         "IOA dump long data transfer timeout\n");
1959                 return -EIO;
1960         }
1961
1962         /* Signal LDUMP interlocked - clear IO debug ack */
1963         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1964                ioa_cfg->regs.clr_interrupt_reg);
1965
1966         /* Write Mailbox with starting address */
1967         writel(start_addr, ioa_cfg->ioa_mailbox);
1968
1969         /* Signal address valid - clear IOA Reset alert */
1970         writel(IPR_UPROCI_RESET_ALERT,
1971                ioa_cfg->regs.clr_uproc_interrupt_reg);
1972
1973         for (i = 0; i < length_in_words; i++) {
1974                 /* Wait for IO debug acknowledge */
1975                 if (ipr_wait_iodbg_ack(ioa_cfg,
1976                                        IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
1977                         dev_err(&ioa_cfg->pdev->dev,
1978                                 "IOA dump short data transfer timeout\n");
1979                         return -EIO;
1980                 }
1981
1982                 /* Read data from mailbox and increment destination pointer */
1983                 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
1984                 dest++;
1985
1986                 /* For all but the last word of data, signal data received */
1987                 if (i < (length_in_words - 1)) {
1988                         /* Signal dump data received - Clear IO debug Ack */
1989                         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1990                                ioa_cfg->regs.clr_interrupt_reg);
1991                 }
1992         }
1993
1994         /* Signal end of block transfer. Set reset alert then clear IO debug ack */
1995         writel(IPR_UPROCI_RESET_ALERT,
1996                ioa_cfg->regs.set_uproc_interrupt_reg);
1997
1998         writel(IPR_UPROCI_IO_DEBUG_ALERT,
1999                ioa_cfg->regs.clr_uproc_interrupt_reg);
2000
2001         /* Signal dump data received - Clear IO debug Ack */
2002         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2003                ioa_cfg->regs.clr_interrupt_reg);
2004
2005         /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
2006         while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
2007                 temp_pcii_reg =
2008                     readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
2009
2010                 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
2011                         return 0;
2012
2013                 udelay(10);
2014                 delay += 10;
2015         }
2016
2017         return 0;
2018 }
2019
2020 #ifdef CONFIG_SCSI_IPR_DUMP
2021 /**
2022  * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
2023  * @ioa_cfg:            ioa config struct
2024  * @pci_address:        adapter address
2025  * @length:                     length of data to copy
2026  *
2027  * Copy data from PCI adapter to kernel buffer.
2028  * Note: length MUST be a 4 byte multiple
2029  * Return value:
2030  *      0 on success / other on failure
2031  **/
2032 static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
2033                         unsigned long pci_address, u32 length)
2034 {
2035         int bytes_copied = 0;
2036         int cur_len, rc, rem_len, rem_page_len;
2037         __be32 *page;
2038         unsigned long lock_flags = 0;
2039         struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
2040
2041         while (bytes_copied < length &&
2042                (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
2043                 if (ioa_dump->page_offset >= PAGE_SIZE ||
2044                     ioa_dump->page_offset == 0) {
2045                         page = (__be32 *)__get_free_page(GFP_ATOMIC);
2046
2047                         if (!page) {
2048                                 ipr_trace;
2049                                 return bytes_copied;
2050                         }
2051
2052                         ioa_dump->page_offset = 0;
2053                         ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
2054                         ioa_dump->next_page_index++;
2055                 } else
2056                         page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
2057
2058                 rem_len = length - bytes_copied;
2059                 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
2060                 cur_len = min(rem_len, rem_page_len);
2061
2062                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2063                 if (ioa_cfg->sdt_state == ABORT_DUMP) {
2064                         rc = -EIO;
2065                 } else {
2066                         rc = ipr_get_ldump_data_section(ioa_cfg,
2067                                                         pci_address + bytes_copied,
2068                                                         &page[ioa_dump->page_offset / 4],
2069                                                         (cur_len / sizeof(u32)));
2070                 }
2071                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2072
2073                 if (!rc) {
2074                         ioa_dump->page_offset += cur_len;
2075                         bytes_copied += cur_len;
2076                 } else {
2077                         ipr_trace;
2078                         break;
2079                 }
2080                 schedule();
2081         }
2082
2083         return bytes_copied;
2084 }
2085
2086 /**
2087  * ipr_init_dump_entry_hdr - Initialize a dump entry header.
2088  * @hdr:        dump entry header struct
2089  *
2090  * Return value:
2091  *      nothing
2092  **/
2093 static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
2094 {
2095         hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
2096         hdr->num_elems = 1;
2097         hdr->offset = sizeof(*hdr);
2098         hdr->status = IPR_DUMP_STATUS_SUCCESS;
2099 }
2100
2101 /**
2102  * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
2103  * @ioa_cfg:    ioa config struct
2104  * @driver_dump:        driver dump struct
2105  *
2106  * Return value:
2107  *      nothing
2108  **/
2109 static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
2110                                    struct ipr_driver_dump *driver_dump)
2111 {
2112         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2113
2114         ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
2115         driver_dump->ioa_type_entry.hdr.len =
2116                 sizeof(struct ipr_dump_ioa_type_entry) -
2117                 sizeof(struct ipr_dump_entry_header);
2118         driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2119         driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
2120         driver_dump->ioa_type_entry.type = ioa_cfg->type;
2121         driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
2122                 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
2123                 ucode_vpd->minor_release[1];
2124         driver_dump->hdr.num_entries++;
2125 }
2126
2127 /**
2128  * ipr_dump_version_data - Fill in the driver version in the dump.
2129  * @ioa_cfg:    ioa config struct
2130  * @driver_dump:        driver dump struct
2131  *
2132  * Return value:
2133  *      nothing
2134  **/
2135 static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
2136                                   struct ipr_driver_dump *driver_dump)
2137 {
2138         ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
2139         driver_dump->version_entry.hdr.len =
2140                 sizeof(struct ipr_dump_version_entry) -
2141                 sizeof(struct ipr_dump_entry_header);
2142         driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2143         driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
2144         strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
2145         driver_dump->hdr.num_entries++;
2146 }
2147
2148 /**
2149  * ipr_dump_trace_data - Fill in the IOA trace in the dump.
2150  * @ioa_cfg:    ioa config struct
2151  * @driver_dump:        driver dump struct
2152  *
2153  * Return value:
2154  *      nothing
2155  **/
2156 static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
2157                                    struct ipr_driver_dump *driver_dump)
2158 {
2159         ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
2160         driver_dump->trace_entry.hdr.len =
2161                 sizeof(struct ipr_dump_trace_entry) -
2162                 sizeof(struct ipr_dump_entry_header);
2163         driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2164         driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
2165         memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
2166         driver_dump->hdr.num_entries++;
2167 }
2168
2169 /**
2170  * ipr_dump_location_data - Fill in the IOA location in the dump.
2171  * @ioa_cfg:    ioa config struct
2172  * @driver_dump:        driver dump struct
2173  *
2174  * Return value:
2175  *      nothing
2176  **/
2177 static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
2178                                    struct ipr_driver_dump *driver_dump)
2179 {
2180         ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
2181         driver_dump->location_entry.hdr.len =
2182                 sizeof(struct ipr_dump_location_entry) -
2183                 sizeof(struct ipr_dump_entry_header);
2184         driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2185         driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
2186         strcpy(driver_dump->location_entry.location, ioa_cfg->pdev->dev.bus_id);
2187         driver_dump->hdr.num_entries++;
2188 }
2189
2190 /**
2191  * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
2192  * @ioa_cfg:    ioa config struct
2193  * @dump:               dump struct
2194  *
2195  * Return value:
2196  *      nothing
2197  **/
2198 static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
2199 {
2200         unsigned long start_addr, sdt_word;
2201         unsigned long lock_flags = 0;
2202         struct ipr_driver_dump *driver_dump = &dump->driver_dump;
2203         struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
2204         u32 num_entries, start_off, end_off;
2205         u32 bytes_to_copy, bytes_copied, rc;
2206         struct ipr_sdt *sdt;
2207         int i;
2208
2209         ENTER;
2210
2211         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2212
2213         if (ioa_cfg->sdt_state != GET_DUMP) {
2214                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2215                 return;
2216         }
2217
2218         start_addr = readl(ioa_cfg->ioa_mailbox);
2219
2220         if (!ipr_sdt_is_fmt2(start_addr)) {
2221                 dev_err(&ioa_cfg->pdev->dev,
2222                         "Invalid dump table format: %lx\n", start_addr);
2223                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2224                 return;
2225         }
2226
2227         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
2228
2229         driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
2230
2231         /* Initialize the overall dump header */
2232         driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
2233         driver_dump->hdr.num_entries = 1;
2234         driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
2235         driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
2236         driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
2237         driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
2238
2239         ipr_dump_version_data(ioa_cfg, driver_dump);
2240         ipr_dump_location_data(ioa_cfg, driver_dump);
2241         ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
2242         ipr_dump_trace_data(ioa_cfg, driver_dump);
2243
2244         /* Update dump_header */
2245         driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
2246
2247         /* IOA Dump entry */
2248         ipr_init_dump_entry_hdr(&ioa_dump->hdr);
2249         ioa_dump->format = IPR_SDT_FMT2;
2250         ioa_dump->hdr.len = 0;
2251         ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2252         ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
2253
2254         /* First entries in sdt are actually a list of dump addresses and
2255          lengths to gather the real dump data.  sdt represents the pointer
2256          to the ioa generated dump table.  Dump data will be extracted based
2257          on entries in this table */
2258         sdt = &ioa_dump->sdt;
2259
2260         rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
2261                                         sizeof(struct ipr_sdt) / sizeof(__be32));
2262
2263         /* Smart Dump table is ready to use and the first entry is valid */
2264         if (rc || (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE)) {
2265                 dev_err(&ioa_cfg->pdev->dev,
2266                         "Dump of IOA failed. Dump table not valid: %d, %X.\n",
2267                         rc, be32_to_cpu(sdt->hdr.state));
2268                 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
2269                 ioa_cfg->sdt_state = DUMP_OBTAINED;
2270                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2271                 return;
2272         }
2273
2274         num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
2275
2276         if (num_entries > IPR_NUM_SDT_ENTRIES)
2277                 num_entries = IPR_NUM_SDT_ENTRIES;
2278
2279         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2280
2281         for (i = 0; i < num_entries; i++) {
2282                 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
2283                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2284                         break;
2285                 }
2286
2287                 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
2288                         sdt_word = be32_to_cpu(sdt->entry[i].bar_str_offset);
2289                         start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
2290                         end_off = be32_to_cpu(sdt->entry[i].end_offset);
2291
2292                         if (ipr_sdt_is_fmt2(sdt_word) && sdt_word) {
2293                                 bytes_to_copy = end_off - start_off;
2294                                 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
2295                                         sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
2296                                         continue;
2297                                 }
2298
2299                                 /* Copy data from adapter to driver buffers */
2300                                 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
2301                                                             bytes_to_copy);
2302
2303                                 ioa_dump->hdr.len += bytes_copied;
2304
2305                                 if (bytes_copied != bytes_to_copy) {
2306                                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2307                                         break;
2308                                 }
2309                         }
2310                 }
2311         }
2312
2313         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
2314
2315         /* Update dump_header */
2316         driver_dump->hdr.len += ioa_dump->hdr.len;
2317         wmb();
2318         ioa_cfg->sdt_state = DUMP_OBTAINED;
2319         LEAVE;
2320 }
2321
2322 #else
2323 #define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
2324 #endif
2325
2326 /**
2327  * ipr_release_dump - Free adapter dump memory
2328  * @kref:       kref struct
2329  *
2330  * Return value:
2331  *      nothing
2332  **/
2333 static void ipr_release_dump(struct kref *kref)
2334 {
2335         struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
2336         struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
2337         unsigned long lock_flags = 0;
2338         int i;
2339
2340         ENTER;
2341         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2342         ioa_cfg->dump = NULL;
2343         ioa_cfg->sdt_state = INACTIVE;
2344         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2345
2346         for (i = 0; i < dump->ioa_dump.next_page_index; i++)
2347                 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
2348
2349         kfree(dump);
2350         LEAVE;
2351 }
2352
2353 /**
2354  * ipr_worker_thread - Worker thread
2355  * @work:               ioa config struct
2356  *
2357  * Called at task level from a work thread. This function takes care
2358  * of adding and removing device from the mid-layer as configuration
2359  * changes are detected by the adapter.
2360  *
2361  * Return value:
2362  *      nothing
2363  **/
2364 static void ipr_worker_thread(struct work_struct *work)
2365 {
2366         unsigned long lock_flags;
2367         struct ipr_resource_entry *res;
2368         struct scsi_device *sdev;
2369         struct ipr_dump *dump;
2370         struct ipr_ioa_cfg *ioa_cfg =
2371                 container_of(work, struct ipr_ioa_cfg, work_q);
2372         u8 bus, target, lun;
2373         int did_work;
2374
2375         ENTER;
2376         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2377
2378         if (ioa_cfg->sdt_state == GET_DUMP) {
2379                 dump = ioa_cfg->dump;
2380                 if (!dump) {
2381                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2382                         return;
2383                 }
2384                 kref_get(&dump->kref);
2385                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2386                 ipr_get_ioa_dump(ioa_cfg, dump);
2387                 kref_put(&dump->kref, ipr_release_dump);
2388
2389                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2390                 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
2391                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2392                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2393                 return;
2394         }
2395
2396 restart:
2397         do {
2398                 did_work = 0;
2399                 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
2400                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2401                         return;
2402                 }
2403
2404                 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2405                         if (res->del_from_ml && res->sdev) {
2406                                 did_work = 1;
2407                                 sdev = res->sdev;
2408                                 if (!scsi_device_get(sdev)) {
2409                                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
2410                                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2411                                         scsi_remove_device(sdev);
2412                                         scsi_device_put(sdev);
2413                                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2414                                 }
2415                                 break;
2416                         }
2417                 }
2418         } while(did_work);
2419
2420         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2421                 if (res->add_to_ml) {
2422                         bus = res->cfgte.res_addr.bus;
2423                         target = res->cfgte.res_addr.target;
2424                         lun = res->cfgte.res_addr.lun;
2425                         res->add_to_ml = 0;
2426                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2427                         scsi_add_device(ioa_cfg->host, bus, target, lun);
2428                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2429                         goto restart;
2430                 }
2431         }
2432
2433         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2434         kobject_uevent(&ioa_cfg->host->shost_classdev.kobj, KOBJ_CHANGE);
2435         LEAVE;
2436 }
2437
2438 #ifdef CONFIG_SCSI_IPR_TRACE
2439 /**
2440  * ipr_read_trace - Dump the adapter trace
2441  * @kobj:               kobject struct
2442  * @bin_attr:           bin_attribute struct
2443  * @buf:                buffer
2444  * @off:                offset
2445  * @count:              buffer size
2446  *
2447  * Return value:
2448  *      number of bytes printed to buffer
2449  **/
2450 static ssize_t ipr_read_trace(struct kobject *kobj,
2451                               struct bin_attribute *bin_attr,
2452                               char *buf, loff_t off, size_t count)
2453 {
2454         struct class_device *cdev = container_of(kobj,struct class_device,kobj);
2455         struct Scsi_Host *shost = class_to_shost(cdev);
2456         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2457         unsigned long lock_flags = 0;
2458         int size = IPR_TRACE_SIZE;
2459         char *src = (char *)ioa_cfg->trace;
2460
2461         if (off > size)
2462                 return 0;
2463         if (off + count > size) {
2464                 size -= off;
2465                 count = size;
2466         }
2467
2468         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2469         memcpy(buf, &src[off], count);
2470         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2471         return count;
2472 }
2473
2474 static struct bin_attribute ipr_trace_attr = {
2475         .attr = {
2476                 .name = "trace",
2477                 .mode = S_IRUGO,
2478         },
2479         .size = 0,
2480         .read = ipr_read_trace,
2481 };
2482 #endif
2483
2484 static const struct {
2485         enum ipr_cache_state state;
2486         char *name;
2487 } cache_state [] = {
2488         { CACHE_NONE, "none" },
2489         { CACHE_DISABLED, "disabled" },
2490         { CACHE_ENABLED, "enabled" }
2491 };
2492
2493 /**
2494  * ipr_show_write_caching - Show the write caching attribute
2495  * @class_dev:  class device struct
2496  * @buf:                buffer
2497  *
2498  * Return value:
2499  *      number of bytes printed to buffer
2500  **/
2501 static ssize_t ipr_show_write_caching(struct class_device *class_dev, char *buf)
2502 {
2503         struct Scsi_Host *shost = class_to_shost(class_dev);
2504         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2505         unsigned long lock_flags = 0;
2506         int i, len = 0;
2507
2508         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2509         for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2510                 if (cache_state[i].state == ioa_cfg->cache_state) {
2511                         len = snprintf(buf, PAGE_SIZE, "%s\n", cache_state[i].name);
2512                         break;
2513                 }
2514         }
2515         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2516         return len;
2517 }
2518
2519
2520 /**
2521  * ipr_store_write_caching - Enable/disable adapter write cache
2522  * @class_dev:  class_device struct
2523  * @buf:                buffer
2524  * @count:              buffer size
2525  *
2526  * This function will enable/disable adapter write cache.
2527  *
2528  * Return value:
2529  *      count on success / other on failure
2530  **/
2531 static ssize_t ipr_store_write_caching(struct class_device *class_dev,
2532                                         const char *buf, size_t count)
2533 {
2534         struct Scsi_Host *shost = class_to_shost(class_dev);
2535         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2536         unsigned long lock_flags = 0;
2537         enum ipr_cache_state new_state = CACHE_INVALID;
2538         int i;
2539
2540         if (!capable(CAP_SYS_ADMIN))
2541                 return -EACCES;
2542         if (ioa_cfg->cache_state == CACHE_NONE)
2543                 return -EINVAL;
2544
2545         for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2546                 if (!strncmp(cache_state[i].name, buf, strlen(cache_state[i].name))) {
2547                         new_state = cache_state[i].state;
2548                         break;
2549                 }
2550         }
2551
2552         if (new_state != CACHE_DISABLED && new_state != CACHE_ENABLED)
2553                 return -EINVAL;
2554
2555         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2556         if (ioa_cfg->cache_state == new_state) {
2557                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2558                 return count;
2559         }
2560
2561         ioa_cfg->cache_state = new_state;
2562         dev_info(&ioa_cfg->pdev->dev, "%s adapter write cache.\n",
2563                  new_state == CACHE_ENABLED ? "Enabling" : "Disabling");
2564         if (!ioa_cfg->in_reset_reload)
2565                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2566         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2567         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2568
2569         return count;
2570 }
2571
2572 static struct class_device_attribute ipr_ioa_cache_attr = {
2573         .attr = {
2574                 .name =         "write_cache",
2575                 .mode =         S_IRUGO | S_IWUSR,
2576         },
2577         .show = ipr_show_write_caching,
2578         .store = ipr_store_write_caching
2579 };
2580
2581 /**
2582  * ipr_show_fw_version - Show the firmware version
2583  * @class_dev:  class device struct
2584  * @buf:                buffer
2585  *
2586  * Return value:
2587  *      number of bytes printed to buffer
2588  **/
2589 static ssize_t ipr_show_fw_version(struct class_device *class_dev, char *buf)
2590 {
2591         struct Scsi_Host *shost = class_to_shost(class_dev);
2592         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2593         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2594         unsigned long lock_flags = 0;
2595         int len;
2596
2597         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2598         len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
2599                        ucode_vpd->major_release, ucode_vpd->card_type,
2600                        ucode_vpd->minor_release[0],
2601                        ucode_vpd->minor_release[1]);
2602         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2603         return len;
2604 }
2605
2606 static struct class_device_attribute ipr_fw_version_attr = {
2607         .attr = {
2608                 .name =         "fw_version",
2609                 .mode =         S_IRUGO,
2610         },
2611         .show = ipr_show_fw_version,
2612 };
2613
2614 /**
2615  * ipr_show_log_level - Show the adapter's error logging level
2616  * @class_dev:  class device struct
2617  * @buf:                buffer
2618  *
2619  * Return value:
2620  *      number of bytes printed to buffer
2621  **/
2622 static ssize_t ipr_show_log_level(struct class_device *class_dev, char *buf)
2623 {
2624         struct Scsi_Host *shost = class_to_shost(class_dev);
2625         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2626         unsigned long lock_flags = 0;
2627         int len;
2628
2629         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2630         len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
2631         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2632         return len;
2633 }
2634
2635 /**
2636  * ipr_store_log_level - Change the adapter's error logging level
2637  * @class_dev:  class device struct
2638  * @buf:                buffer
2639  *
2640  * Return value:
2641  *      number of bytes printed to buffer
2642  **/
2643 static ssize_t ipr_store_log_level(struct class_device *class_dev,
2644                                    const char *buf, size_t count)
2645 {
2646         struct Scsi_Host *shost = class_to_shost(class_dev);
2647         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2648         unsigned long lock_flags = 0;
2649
2650         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2651         ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
2652         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2653         return strlen(buf);
2654 }
2655
2656 static struct class_device_attribute ipr_log_level_attr = {
2657         .attr = {
2658                 .name =         "log_level",
2659                 .mode =         S_IRUGO | S_IWUSR,
2660         },
2661         .show = ipr_show_log_level,
2662         .store = ipr_store_log_level
2663 };
2664
2665 /**
2666  * ipr_store_diagnostics - IOA Diagnostics interface
2667  * @class_dev:  class_device struct
2668  * @buf:                buffer
2669  * @count:              buffer size
2670  *
2671  * This function will reset the adapter and wait a reasonable
2672  * amount of time for any errors that the adapter might log.
2673  *
2674  * Return value:
2675  *      count on success / other on failure
2676  **/
2677 static ssize_t ipr_store_diagnostics(struct class_device *class_dev,
2678                                      const char *buf, size_t count)
2679 {
2680         struct Scsi_Host *shost = class_to_shost(class_dev);
2681         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2682         unsigned long lock_flags = 0;
2683         int rc = count;
2684
2685         if (!capable(CAP_SYS_ADMIN))
2686                 return -EACCES;
2687
2688         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2689         while(ioa_cfg->in_reset_reload) {
2690                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2691                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2692                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2693         }
2694
2695         ioa_cfg->errors_logged = 0;
2696         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2697
2698         if (ioa_cfg->in_reset_reload) {
2699                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2700                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2701
2702                 /* Wait for a second for any errors to be logged */
2703                 msleep(1000);
2704         } else {
2705                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2706                 return -EIO;
2707         }
2708
2709         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2710         if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
2711                 rc = -EIO;
2712         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2713
2714         return rc;
2715 }
2716
2717 static struct class_device_attribute ipr_diagnostics_attr = {
2718         .attr = {
2719                 .name =         "run_diagnostics",
2720                 .mode =         S_IWUSR,
2721         },
2722         .store = ipr_store_diagnostics
2723 };
2724
2725 /**
2726  * ipr_show_adapter_state - Show the adapter's state
2727  * @class_dev:  class device struct
2728  * @buf:                buffer
2729  *
2730  * Return value:
2731  *      number of bytes printed to buffer
2732  **/
2733 static ssize_t ipr_show_adapter_state(struct class_device *class_dev, char *buf)
2734 {
2735         struct Scsi_Host *shost = class_to_shost(class_dev);
2736         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2737         unsigned long lock_flags = 0;
2738         int len;
2739
2740         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2741         if (ioa_cfg->ioa_is_dead)
2742                 len = snprintf(buf, PAGE_SIZE, "offline\n");
2743         else
2744                 len = snprintf(buf, PAGE_SIZE, "online\n");
2745         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2746         return len;
2747 }
2748
2749 /**
2750  * ipr_store_adapter_state - Change adapter state
2751  * @class_dev:  class_device struct
2752  * @buf:                buffer
2753  * @count:              buffer size
2754  *
2755  * This function will change the adapter's state.
2756  *
2757  * Return value:
2758  *      count on success / other on failure
2759  **/
2760 static ssize_t ipr_store_adapter_state(struct class_device *class_dev,
2761                                        const char *buf, size_t count)
2762 {
2763         struct Scsi_Host *shost = class_to_shost(class_dev);
2764         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2765         unsigned long lock_flags;
2766         int result = count;
2767
2768         if (!capable(CAP_SYS_ADMIN))
2769                 return -EACCES;
2770
2771         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2772         if (ioa_cfg->ioa_is_dead && !strncmp(buf, "online", 6)) {
2773                 ioa_cfg->ioa_is_dead = 0;
2774                 ioa_cfg->reset_retries = 0;
2775                 ioa_cfg->in_ioa_bringdown = 0;
2776                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2777         }
2778         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2779         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2780
2781         return result;
2782 }
2783
2784 static struct class_device_attribute ipr_ioa_state_attr = {
2785         .attr = {
2786                 .name =         "state",
2787                 .mode =         S_IRUGO | S_IWUSR,
2788         },
2789         .show = ipr_show_adapter_state,
2790         .store = ipr_store_adapter_state
2791 };
2792
2793 /**
2794  * ipr_store_reset_adapter - Reset the adapter
2795  * @class_dev:  class_device struct
2796  * @buf:                buffer
2797  * @count:              buffer size
2798  *
2799  * This function will reset the adapter.
2800  *
2801  * Return value:
2802  *      count on success / other on failure
2803  **/
2804 static ssize_t ipr_store_reset_adapter(struct class_device *class_dev,
2805                                        const char *buf, size_t count)
2806 {
2807         struct Scsi_Host *shost = class_to_shost(class_dev);
2808         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2809         unsigned long lock_flags;
2810         int result = count;
2811
2812         if (!capable(CAP_SYS_ADMIN))
2813                 return -EACCES;
2814
2815         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2816         if (!ioa_cfg->in_reset_reload)
2817                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2818         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2819         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2820
2821         return result;
2822 }
2823
2824 static struct class_device_attribute ipr_ioa_reset_attr = {
2825         .attr = {
2826                 .name =         "reset_host",
2827                 .mode =         S_IWUSR,
2828         },
2829         .store = ipr_store_reset_adapter
2830 };
2831
2832 /**
2833  * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
2834  * @buf_len:            buffer length
2835  *
2836  * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
2837  * list to use for microcode download
2838  *
2839  * Return value:
2840  *      pointer to sglist / NULL on failure
2841  **/
2842 static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
2843 {
2844         int sg_size, order, bsize_elem, num_elem, i, j;
2845         struct ipr_sglist *sglist;
2846         struct scatterlist *scatterlist;
2847         struct page *page;
2848
2849         /* Get the minimum size per scatter/gather element */
2850         sg_size = buf_len / (IPR_MAX_SGLIST - 1);
2851
2852         /* Get the actual size per element */
2853         order = get_order(sg_size);
2854
2855         /* Determine the actual number of bytes per element */
2856         bsize_elem = PAGE_SIZE * (1 << order);
2857
2858         /* Determine the actual number of sg entries needed */
2859         if (buf_len % bsize_elem)
2860                 num_elem = (buf_len / bsize_elem) + 1;
2861         else
2862                 num_elem = buf_len / bsize_elem;
2863
2864         /* Allocate a scatter/gather list for the DMA */
2865         sglist = kzalloc(sizeof(struct ipr_sglist) +
2866                          (sizeof(struct scatterlist) * (num_elem - 1)),
2867                          GFP_KERNEL);
2868
2869         if (sglist == NULL) {
2870                 ipr_trace;
2871                 return NULL;
2872         }
2873
2874         scatterlist = sglist->scatterlist;
2875         sg_init_table(scatterlist, num_elem);
2876
2877         sglist->order = order;
2878         sglist->num_sg = num_elem;
2879
2880         /* Allocate a bunch of sg elements */
2881         for (i = 0; i < num_elem; i++) {
2882                 page = alloc_pages(GFP_KERNEL, order);
2883                 if (!page) {
2884                         ipr_trace;
2885
2886                         /* Free up what we already allocated */
2887                         for (j = i - 1; j >= 0; j--)
2888                                 __free_pages(sg_page(&scatterlist[j]), order);
2889                         kfree(sglist);
2890                         return NULL;
2891                 }
2892
2893                 sg_set_page(&scatterlist[i], page, 0, 0);
2894         }
2895
2896         return sglist;
2897 }
2898
2899 /**
2900  * ipr_free_ucode_buffer - Frees a microcode download buffer
2901  * @p_dnld:             scatter/gather list pointer
2902  *
2903  * Free a DMA'able ucode download buffer previously allocated with
2904  * ipr_alloc_ucode_buffer
2905  *
2906  * Return value:
2907  *      nothing
2908  **/
2909 static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
2910 {
2911         int i;
2912
2913         for (i = 0; i < sglist->num_sg; i++)
2914                 __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
2915
2916         kfree(sglist);
2917 }
2918
2919 /**
2920  * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
2921  * @sglist:             scatter/gather list pointer
2922  * @buffer:             buffer pointer
2923  * @len:                buffer length
2924  *
2925  * Copy a microcode image from a user buffer into a buffer allocated by
2926  * ipr_alloc_ucode_buffer
2927  *
2928  * Return value:
2929  *      0 on success / other on failure
2930  **/
2931 static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
2932                                  u8 *buffer, u32 len)
2933 {
2934         int bsize_elem, i, result = 0;
2935         struct scatterlist *scatterlist;
2936         void *kaddr;
2937
2938         /* Determine the actual number of bytes per element */
2939         bsize_elem = PAGE_SIZE * (1 << sglist->order);
2940
2941         scatterlist = sglist->scatterlist;
2942
2943         for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
2944                 struct page *page = sg_page(&scatterlist[i]);
2945
2946                 kaddr = kmap(page);
2947                 memcpy(kaddr, buffer, bsize_elem);
2948                 kunmap(page);
2949
2950                 scatterlist[i].length = bsize_elem;
2951
2952                 if (result != 0) {
2953                         ipr_trace;
2954                         return result;
2955                 }
2956         }
2957
2958         if (len % bsize_elem) {
2959                 struct page *page = sg_page(&scatterlist[i]);
2960
2961                 kaddr = kmap(page);
2962                 memcpy(kaddr, buffer, len % bsize_elem);
2963                 kunmap(page);
2964
2965                 scatterlist[i].length = len % bsize_elem;
2966         }
2967
2968         sglist->buffer_len = len;
2969         return result;
2970 }
2971
2972 /**
2973  * ipr_build_ucode_ioadl - Build a microcode download IOADL
2974  * @ipr_cmd:    ipr command struct
2975  * @sglist:             scatter/gather list
2976  *
2977  * Builds a microcode download IOA data list (IOADL).
2978  *
2979  **/
2980 static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
2981                                   struct ipr_sglist *sglist)
2982 {
2983         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
2984         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
2985         struct scatterlist *scatterlist = sglist->scatterlist;
2986         int i;
2987
2988         ipr_cmd->dma_use_sg = sglist->num_dma_sg;
2989         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
2990         ioarcb->write_data_transfer_length = cpu_to_be32(sglist->buffer_len);
2991         ioarcb->write_ioadl_len =
2992                 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
2993
2994         for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
2995                 ioadl[i].flags_and_data_len =
2996                         cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
2997                 ioadl[i].address =
2998                         cpu_to_be32(sg_dma_address(&scatterlist[i]));
2999         }
3000
3001         ioadl[i-1].flags_and_data_len |=
3002                 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3003 }
3004
3005 /**
3006  * ipr_update_ioa_ucode - Update IOA's microcode
3007  * @ioa_cfg:    ioa config struct
3008  * @sglist:             scatter/gather list
3009  *
3010  * Initiate an adapter reset to update the IOA's microcode
3011  *
3012  * Return value:
3013  *      0 on success / -EIO on failure
3014  **/
3015 static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
3016                                 struct ipr_sglist *sglist)
3017 {
3018         unsigned long lock_flags;
3019
3020         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3021         while(ioa_cfg->in_reset_reload) {
3022                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3023                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3024                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3025         }
3026
3027         if (ioa_cfg->ucode_sglist) {
3028                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3029                 dev_err(&ioa_cfg->pdev->dev,
3030                         "Microcode download already in progress\n");
3031                 return -EIO;
3032         }
3033
3034         sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
3035                                         sglist->num_sg, DMA_TO_DEVICE);
3036
3037         if (!sglist->num_dma_sg) {
3038                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3039                 dev_err(&ioa_cfg->pdev->dev,
3040                         "Failed to map microcode download buffer!\n");
3041                 return -EIO;
3042         }
3043
3044         ioa_cfg->ucode_sglist = sglist;
3045         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3046         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3047         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3048
3049         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3050         ioa_cfg->ucode_sglist = NULL;
3051         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3052         return 0;
3053 }
3054
3055 /**
3056  * ipr_store_update_fw - Update the firmware on the adapter
3057  * @class_dev:  class_device struct
3058  * @buf:                buffer
3059  * @count:              buffer size
3060  *
3061  * This function will update the firmware on the adapter.
3062  *
3063  * Return value:
3064  *      count on success / other on failure
3065  **/
3066 static ssize_t ipr_store_update_fw(struct class_device *class_dev,
3067                                        const char *buf, size_t count)
3068 {
3069         struct Scsi_Host *shost = class_to_shost(class_dev);
3070         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3071         struct ipr_ucode_image_header *image_hdr;
3072         const struct firmware *fw_entry;
3073         struct ipr_sglist *sglist;
3074         char fname[100];
3075         char *src;
3076         int len, result, dnld_size;
3077
3078         if (!capable(CAP_SYS_ADMIN))
3079                 return -EACCES;
3080
3081         len = snprintf(fname, 99, "%s", buf);
3082         fname[len-1] = '\0';
3083
3084         if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
3085                 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
3086                 return -EIO;
3087         }
3088
3089         image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
3090
3091         if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
3092             (ioa_cfg->vpd_cbs->page3_data.card_type &&
3093              ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
3094                 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
3095                 release_firmware(fw_entry);
3096                 return -EINVAL;
3097         }
3098
3099         src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
3100         dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
3101         sglist = ipr_alloc_ucode_buffer(dnld_size);
3102
3103         if (!sglist) {
3104                 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
3105                 release_firmware(fw_entry);
3106                 return -ENOMEM;
3107         }
3108
3109         result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
3110
3111         if (result) {
3112                 dev_err(&ioa_cfg->pdev->dev,
3113                         "Microcode buffer copy to DMA buffer failed\n");
3114                 goto out;
3115         }
3116
3117         result = ipr_update_ioa_ucode(ioa_cfg, sglist);
3118
3119         if (!result)
3120                 result = count;
3121 out:
3122         ipr_free_ucode_buffer(sglist);
3123         release_firmware(fw_entry);
3124         return result;
3125 }
3126
3127 static struct class_device_attribute ipr_update_fw_attr = {
3128         .attr = {
3129                 .name =         "update_fw",
3130                 .mode =         S_IWUSR,
3131         },
3132         .store = ipr_store_update_fw
3133 };
3134
3135 static struct class_device_attribute *ipr_ioa_attrs[] = {
3136         &ipr_fw_version_attr,
3137         &ipr_log_level_attr,
3138         &ipr_diagnostics_attr,
3139         &ipr_ioa_state_attr,
3140         &ipr_ioa_reset_attr,
3141         &ipr_update_fw_attr,
3142         &ipr_ioa_cache_attr,
3143         NULL,
3144 };
3145
3146 #ifdef CONFIG_SCSI_IPR_DUMP
3147 /**
3148  * ipr_read_dump - Dump the adapter
3149  * @kobj:               kobject struct
3150  * @bin_attr:           bin_attribute struct
3151  * @buf:                buffer
3152  * @off:                offset
3153  * @count:              buffer size
3154  *
3155  * Return value:
3156  *      number of bytes printed to buffer
3157  **/
3158 static ssize_t ipr_read_dump(struct kobject *kobj,
3159                              struct bin_attribute *bin_attr,
3160                              char *buf, loff_t off, size_t count)
3161 {
3162         struct class_device *cdev = container_of(kobj,struct class_device,kobj);
3163         struct Scsi_Host *shost = class_to_shost(cdev);
3164         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3165         struct ipr_dump *dump;
3166         unsigned long lock_flags = 0;
3167         char *src;
3168         int len;
3169         size_t rc = count;
3170
3171         if (!capable(CAP_SYS_ADMIN))
3172                 return -EACCES;
3173
3174         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3175         dump = ioa_cfg->dump;
3176
3177         if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
3178                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3179                 return 0;
3180         }
3181         kref_get(&dump->kref);
3182         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3183
3184         if (off > dump->driver_dump.hdr.len) {
3185                 kref_put(&dump->kref, ipr_release_dump);
3186                 return 0;
3187         }
3188
3189         if (off + count > dump->driver_dump.hdr.len) {
3190                 count = dump->driver_dump.hdr.len - off;
3191                 rc = count;
3192         }
3193
3194         if (count && off < sizeof(dump->driver_dump)) {
3195                 if (off + count > sizeof(dump->driver_dump))
3196                         len = sizeof(dump->driver_dump) - off;
3197                 else
3198                         len = count;
3199                 src = (u8 *)&dump->driver_dump + off;
3200                 memcpy(buf, src, len);
3201                 buf += len;
3202                 off += len;
3203                 count -= len;
3204         }
3205
3206         off -= sizeof(dump->driver_dump);
3207
3208         if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
3209                 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
3210                         len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
3211                 else
3212                         len = count;
3213                 src = (u8 *)&dump->ioa_dump + off;
3214                 memcpy(buf, src, len);
3215                 buf += len;
3216                 off += len;
3217                 count -= len;
3218         }
3219
3220         off -= offsetof(struct ipr_ioa_dump, ioa_data);
3221
3222         while (count) {
3223                 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
3224                         len = PAGE_ALIGN(off) - off;
3225                 else
3226                         len = count;
3227                 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
3228                 src += off & ~PAGE_MASK;
3229                 memcpy(buf, src, len);
3230                 buf += len;
3231                 off += len;
3232                 count -= len;
3233         }
3234
3235         kref_put(&dump->kref, ipr_release_dump);
3236         return rc;
3237 }
3238
3239 /**
3240  * ipr_alloc_dump - Prepare for adapter dump
3241  * @ioa_cfg:    ioa config struct
3242  *
3243  * Return value:
3244  *      0 on success / other on failure
3245  **/
3246 static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
3247 {
3248         struct ipr_dump *dump;
3249         unsigned long lock_flags = 0;
3250
3251         dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
3252
3253         if (!dump) {
3254                 ipr_err("Dump memory allocation failed\n");
3255                 return -ENOMEM;
3256         }
3257
3258         kref_init(&dump->kref);
3259         dump->ioa_cfg = ioa_cfg;
3260
3261         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3262
3263         if (INACTIVE != ioa_cfg->sdt_state) {
3264                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3265                 kfree(dump);
3266                 return 0;
3267         }
3268
3269         ioa_cfg->dump = dump;
3270         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
3271         if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
3272                 ioa_cfg->dump_taken = 1;
3273                 schedule_work(&ioa_cfg->work_q);
3274         }
3275         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3276
3277         return 0;
3278 }
3279
3280 /**
3281  * ipr_free_dump - Free adapter dump memory
3282  * @ioa_cfg:    ioa config struct
3283  *
3284  * Return value:
3285  *      0 on success / other on failure
3286  **/
3287 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
3288 {
3289         struct ipr_dump *dump;
3290         unsigned long lock_flags = 0;
3291
3292         ENTER;
3293
3294         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3295         dump = ioa_cfg->dump;
3296         if (!dump) {
3297                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3298                 return 0;
3299         }
3300
3301         ioa_cfg->dump = NULL;
3302         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3303
3304         kref_put(&dump->kref, ipr_release_dump);
3305
3306         LEAVE;
3307         return 0;
3308 }
3309
3310 /**
3311  * ipr_write_dump - Setup dump state of adapter
3312  * @kobj:               kobject struct
3313  * @bin_attr:           bin_attribute struct
3314  * @buf:                buffer
3315  * @off:                offset
3316  * @count:              buffer size
3317  *
3318  * Return value:
3319  *      number of bytes printed to buffer
3320  **/
3321 static ssize_t ipr_write_dump(struct kobject *kobj,
3322                               struct bin_attribute *bin_attr,
3323                               char *buf, loff_t off, size_t count)
3324 {
3325         struct class_device *cdev = container_of(kobj,struct class_device,kobj);
3326         struct Scsi_Host *shost = class_to_shost(cdev);
3327         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3328         int rc;
3329
3330         if (!capable(CAP_SYS_ADMIN))
3331                 return -EACCES;
3332
3333         if (buf[0] == '1')
3334                 rc = ipr_alloc_dump(ioa_cfg);
3335         else if (buf[0] == '0')
3336                 rc = ipr_free_dump(ioa_cfg);
3337         else
3338                 return -EINVAL;
3339
3340         if (rc)
3341                 return rc;
3342         else
3343                 return count;
3344 }
3345
3346 static struct bin_attribute ipr_dump_attr = {
3347         .attr = {
3348                 .name = "dump",
3349                 .mode = S_IRUSR | S_IWUSR,
3350         },
3351         .size = 0,
3352         .read = ipr_read_dump,
3353         .write = ipr_write_dump
3354 };
3355 #else
3356 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
3357 #endif
3358
3359 /**
3360  * ipr_change_queue_depth - Change the device's queue depth
3361  * @sdev:       scsi device struct
3362  * @qdepth:     depth to set
3363  *
3364  * Return value:
3365  *      actual depth set
3366  **/
3367 static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
3368 {
3369         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3370         struct ipr_resource_entry *res;
3371         unsigned long lock_flags = 0;
3372
3373         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3374         res = (struct ipr_resource_entry *)sdev->hostdata;
3375
3376         if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
3377                 qdepth = IPR_MAX_CMD_PER_ATA_LUN;
3378         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3379
3380         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3381         return sdev->queue_depth;
3382 }
3383
3384 /**
3385  * ipr_change_queue_type - Change the device's queue type
3386  * @dsev:               scsi device struct
3387  * @tag_type:   type of tags to use
3388  *
3389  * Return value:
3390  *      actual queue type set
3391  **/
3392 static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
3393 {
3394         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3395         struct ipr_resource_entry *res;
3396         unsigned long lock_flags = 0;
3397
3398         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3399         res = (struct ipr_resource_entry *)sdev->hostdata;
3400
3401         if (res) {
3402                 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
3403                         /*
3404                          * We don't bother quiescing the device here since the
3405                          * adapter firmware does it for us.
3406                          */
3407                         scsi_set_tag_type(sdev, tag_type);
3408
3409                         if (tag_type)
3410                                 scsi_activate_tcq(sdev, sdev->queue_depth);
3411                         else
3412                                 scsi_deactivate_tcq(sdev, sdev->queue_depth);
3413                 } else
3414                         tag_type = 0;
3415         } else
3416                 tag_type = 0;
3417
3418         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3419         return tag_type;
3420 }
3421
3422 /**
3423  * ipr_show_adapter_handle - Show the adapter's resource handle for this device
3424  * @dev:        device struct
3425  * @buf:        buffer
3426  *
3427  * Return value:
3428  *      number of bytes printed to buffer
3429  **/
3430 static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
3431 {
3432         struct scsi_device *sdev = to_scsi_device(dev);
3433         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3434         struct ipr_resource_entry *res;
3435         unsigned long lock_flags = 0;
3436         ssize_t len = -ENXIO;
3437
3438         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3439         res = (struct ipr_resource_entry *)sdev->hostdata;
3440         if (res)
3441                 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->cfgte.res_handle);
3442         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3443         return len;
3444 }
3445
3446 static struct device_attribute ipr_adapter_handle_attr = {
3447         .attr = {
3448                 .name =         "adapter_handle",
3449                 .mode =         S_IRUSR,
3450         },
3451         .show = ipr_show_adapter_handle
3452 };
3453
3454 static struct device_attribute *ipr_dev_attrs[] = {
3455         &ipr_adapter_handle_attr,
3456         NULL,
3457 };
3458
3459 /**
3460  * ipr_biosparam - Return the HSC mapping
3461  * @sdev:                       scsi device struct
3462  * @block_device:       block device pointer
3463  * @capacity:           capacity of the device
3464  * @parm:                       Array containing returned HSC values.
3465  *
3466  * This function generates the HSC parms that fdisk uses.
3467  * We want to make sure we return something that places partitions
3468  * on 4k boundaries for best performance with the IOA.
3469  *
3470  * Return value:
3471  *      0 on success
3472  **/
3473 static int ipr_biosparam(struct scsi_device *sdev,
3474                          struct block_device *block_device,
3475                          sector_t capacity, int *parm)
3476 {
3477         int heads, sectors;
3478         sector_t cylinders;
3479
3480         heads = 128;
3481         sectors = 32;
3482
3483         cylinders = capacity;
3484         sector_div(cylinders, (128 * 32));
3485
3486         /* return result */
3487         parm[0] = heads;
3488         parm[1] = sectors;
3489         parm[2] = cylinders;
3490
3491         return 0;
3492 }
3493
3494 /**
3495  * ipr_find_starget - Find target based on bus/target.
3496  * @starget:    scsi target struct
3497  *
3498  * Return value:
3499  *      resource entry pointer if found / NULL if not found
3500  **/
3501 static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
3502 {
3503         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3504         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
3505         struct ipr_resource_entry *res;
3506
3507         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3508                 if ((res->cfgte.res_addr.bus == starget->channel) &&
3509                     (res->cfgte.res_addr.target == starget->id) &&
3510                     (res->cfgte.res_addr.lun == 0)) {
3511                         return res;
3512                 }
3513         }
3514
3515         return NULL;
3516 }
3517
3518 static struct ata_port_info sata_port_info;
3519
3520 /**
3521  * ipr_target_alloc - Prepare for commands to a SCSI target
3522  * @starget:    scsi target struct
3523  *
3524  * If the device is a SATA device, this function allocates an
3525  * ATA port with libata, else it does nothing.
3526  *
3527  * Return value:
3528  *      0 on success / non-0 on failure
3529  **/
3530 static int ipr_target_alloc(struct scsi_target *starget)
3531 {
3532         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3533         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
3534         struct ipr_sata_port *sata_port;
3535         struct ata_port *ap;
3536         struct ipr_resource_entry *res;
3537         unsigned long lock_flags;
3538
3539         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3540         res = ipr_find_starget(starget);
3541         starget->hostdata = NULL;
3542
3543         if (res && ipr_is_gata(res)) {
3544                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3545                 sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
3546                 if (!sata_port)
3547                         return -ENOMEM;
3548
3549                 ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
3550                 if (ap) {
3551                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3552                         sata_port->ioa_cfg = ioa_cfg;
3553                         sata_port->ap = ap;
3554                         sata_port->res = res;
3555
3556                         res->sata_port = sata_port;
3557                         ap->private_data = sata_port;
3558                         starget->hostdata = sata_port;
3559                 } else {
3560                         kfree(sata_port);
3561                         return -ENOMEM;
3562                 }
3563         }
3564         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3565
3566         return 0;
3567 }
3568
3569 /**
3570  * ipr_target_destroy - Destroy a SCSI target
3571  * @starget:    scsi target struct
3572  *
3573  * If the device was a SATA device, this function frees the libata
3574  * ATA port, else it does nothing.
3575  *
3576  **/
3577 static void ipr_target_destroy(struct scsi_target *starget)
3578 {
3579         struct ipr_sata_port *sata_port = starget->hostdata;
3580
3581         if (sata_port) {
3582                 starget->hostdata = NULL;
3583                 ata_sas_port_destroy(sata_port->ap);
3584                 kfree(sata_port);
3585         }
3586 }
3587
3588 /**
3589  * ipr_find_sdev - Find device based on bus/target/lun.
3590  * @sdev:       scsi device struct
3591  *
3592  * Return value:
3593  *      resource entry pointer if found / NULL if not found
3594  **/
3595 static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
3596 {
3597         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3598         struct ipr_resource_entry *res;
3599
3600         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3601                 if ((res->cfgte.res_addr.bus == sdev->channel) &&
3602                     (res->cfgte.res_addr.target == sdev->id) &&
3603                     (res->cfgte.res_addr.lun == sdev->lun))
3604                         return res;
3605         }
3606
3607         return NULL;
3608 }
3609
3610 /**
3611  * ipr_slave_destroy - Unconfigure a SCSI device
3612  * @sdev:       scsi device struct
3613  *
3614  * Return value:
3615  *      nothing
3616  **/
3617 static void ipr_slave_destroy(struct scsi_device *sdev)
3618 {
3619         struct ipr_resource_entry *res;
3620         struct ipr_ioa_cfg *ioa_cfg;
3621         unsigned long lock_flags = 0;
3622
3623         ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3624
3625         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3626         res = (struct ipr_resource_entry *) sdev->hostdata;
3627         if (res) {
3628                 if (res->sata_port)
3629                         ata_port_disable(res->sata_port->ap);
3630                 sdev->hostdata = NULL;
3631                 res->sdev = NULL;
3632                 res->sata_port = NULL;
3633         }
3634         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3635 }
3636
3637 /**
3638  * ipr_slave_configure - Configure a SCSI device
3639  * @sdev:       scsi device struct
3640  *
3641  * This function configures the specified scsi device.
3642  *
3643  * Return value:
3644  *      0 on success
3645  **/
3646 static int ipr_slave_configure(struct scsi_device *sdev)
3647 {
3648         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3649         struct ipr_resource_entry *res;
3650         unsigned long lock_flags = 0;
3651
3652         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3653         res = sdev->hostdata;
3654         if (res) {
3655                 if (ipr_is_af_dasd_device(res))
3656                         sdev->type = TYPE_RAID;
3657                 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
3658                         sdev->scsi_level = 4;
3659                         sdev->no_uld_attach = 1;
3660                 }
3661                 if (ipr_is_vset_device(res)) {
3662                         sdev->timeout = IPR_VSET_RW_TIMEOUT;
3663                         blk_queue_max_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
3664                 }
3665                 if (ipr_is_vset_device(res) || ipr_is_scsi_disk(res))
3666                         sdev->allow_restart = 1;
3667                 if (ipr_is_gata(res) && res->sata_port) {
3668                         scsi_adjust_queue_depth(sdev, 0, IPR_MAX_CMD_PER_ATA_LUN);
3669                         ata_sas_slave_configure(sdev, res->sata_port->ap);
3670                 } else {
3671                         scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
3672                 }
3673         }
3674         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3675         return 0;
3676 }
3677
3678 /**
3679  * ipr_ata_slave_alloc - Prepare for commands to a SATA device
3680  * @sdev:       scsi device struct
3681  *
3682  * This function initializes an ATA port so that future commands
3683  * sent through queuecommand will work.
3684  *
3685  * Return value:
3686  *      0 on success
3687  **/
3688 static int ipr_ata_slave_alloc(struct scsi_device *sdev)
3689 {
3690         struct ipr_sata_port *sata_port = NULL;
3691         int rc = -ENXIO;
3692
3693         ENTER;
3694         if (sdev->sdev_target)
3695                 sata_port = sdev->sdev_target->hostdata;
3696         if (sata_port)
3697                 rc = ata_sas_port_init(sata_port->ap);
3698         if (rc)
3699                 ipr_slave_destroy(sdev);
3700
3701         LEAVE;
3702         return rc;
3703 }
3704
3705 /**
3706  * ipr_slave_alloc - Prepare for commands to a device.
3707  * @sdev:       scsi device struct
3708  *
3709  * This function saves a pointer to the resource entry
3710  * in the scsi device struct if the device exists. We
3711  * can then use this pointer in ipr_queuecommand when
3712  * handling new commands.
3713  *
3714  * Return value:
3715  *      0 on success / -ENXIO if device does not exist
3716  **/
3717 static int ipr_slave_alloc(struct scsi_device *sdev)
3718 {
3719         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3720         struct ipr_resource_entry *res;
3721         unsigned long lock_flags;
3722         int rc = -ENXIO;
3723
3724         sdev->hostdata = NULL;
3725
3726         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3727
3728         res = ipr_find_sdev(sdev);
3729         if (res) {
3730                 res->sdev = sdev;
3731                 res->add_to_ml = 0;
3732                 res->in_erp = 0;
3733                 sdev->hostdata = res;
3734                 if (!ipr_is_naca_model(res))
3735                         res->needs_sync_complete = 1;
3736                 rc = 0;
3737                 if (ipr_is_gata(res)) {
3738                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3739                         return ipr_ata_slave_alloc(sdev);
3740                 }
3741         }
3742
3743         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3744
3745         return rc;
3746 }
3747
3748 /**
3749  * ipr_eh_host_reset - Reset the host adapter
3750  * @scsi_cmd:   scsi command struct
3751  *
3752  * Return value:
3753  *      SUCCESS / FAILED
3754  **/
3755 static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
3756 {
3757         struct ipr_ioa_cfg *ioa_cfg;
3758         int rc;
3759
3760         ENTER;
3761         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3762
3763         dev_err(&ioa_cfg->pdev->dev,
3764                 "Adapter being reset as a result of error recovery.\n");
3765
3766         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3767                 ioa_cfg->sdt_state = GET_DUMP;
3768
3769         rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
3770
3771         LEAVE;
3772         return rc;
3773 }
3774
3775 static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
3776 {
3777         int rc;
3778
3779         spin_lock_irq(cmd->device->host->host_lock);
3780         rc = __ipr_eh_host_reset(cmd);
3781         spin_unlock_irq(cmd->device->host->host_lock);
3782
3783         return rc;
3784 }
3785
3786 /**
3787  * ipr_device_reset - Reset the device
3788  * @ioa_cfg:    ioa config struct
3789  * @res:                resource entry struct
3790  *
3791  * This function issues a device reset to the affected device.
3792  * If the device is a SCSI device, a LUN reset will be sent
3793  * to the device first. If that does not work, a target reset
3794  * will be sent. If the device is a SATA device, a PHY reset will
3795  * be sent.
3796  *
3797  * Return value:
3798  *      0 on success / non-zero on failure
3799  **/
3800 static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
3801                             struct ipr_resource_entry *res)
3802 {
3803         struct ipr_cmnd *ipr_cmd;
3804         struct ipr_ioarcb *ioarcb;
3805         struct ipr_cmd_pkt *cmd_pkt;
3806         struct ipr_ioarcb_ata_regs *regs;
3807         u32 ioasc;
3808
3809         ENTER;
3810         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3811         ioarcb = &ipr_cmd->ioarcb;
3812         cmd_pkt = &ioarcb->cmd_pkt;
3813         regs = &ioarcb->add_data.u.regs;
3814
3815         ioarcb->res_handle = res->cfgte.res_handle;
3816         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3817         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
3818         if (ipr_is_gata(res)) {
3819                 cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
3820                 ioarcb->add_cmd_parms_len = cpu_to_be32(sizeof(regs->flags));
3821                 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
3822         }
3823
3824         ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3825         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3826         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3827         if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET)
3828                 memcpy(&res->sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
3829                        sizeof(struct ipr_ioasa_gata));
3830
3831         LEAVE;
3832         return (IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0);
3833 }
3834
3835 /**
3836  * ipr_sata_reset - Reset the SATA port
3837  * @link:       SATA link to reset
3838  * @classes:    class of the attached device
3839  *
3840  * This function issues a SATA phy reset to the affected ATA link.
3841  *
3842  * Return value:
3843  *      0 on success / non-zero on failure
3844  **/
3845 static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
3846                                 unsigned long deadline)
3847 {
3848         struct ipr_sata_port *sata_port = link->ap->private_data;
3849         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
3850         struct ipr_resource_entry *res;
3851         unsigned long lock_flags = 0;
3852         int rc = -ENXIO;
3853
3854         ENTER;
3855         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3856         while(ioa_cfg->in_reset_reload) {
3857                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3858                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3859                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3860         }
3861
3862         res = sata_port->res;
3863         if (res) {
3864                 rc = ipr_device_reset(ioa_cfg, res);
3865                 switch(res->cfgte.proto) {
3866                 case IPR_PROTO_SATA:
3867                 case IPR_PROTO_SAS_STP:
3868                         *classes = ATA_DEV_ATA;
3869                         break;
3870                 case IPR_PROTO_SATA_ATAPI:
3871                 case IPR_PROTO_SAS_STP_ATAPI:
3872                         *classes = ATA_DEV_ATAPI;
3873                         break;
3874                 default:
3875                         *classes = ATA_DEV_UNKNOWN;
3876                         break;
3877                 };
3878         }
3879
3880         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3881         LEAVE;
3882         return rc;
3883 }
3884
3885 /**
3886  * ipr_eh_dev_reset - Reset the device
3887  * @scsi_cmd:   scsi command struct
3888  *
3889  * This function issues a device reset to the affected device.
3890  * A LUN reset will be sent to the device first. If that does
3891  * not work, a target reset will be sent.
3892  *
3893  * Return value:
3894  *      SUCCESS / FAILED
3895  **/
3896 static int __ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
3897 {
3898         struct ipr_cmnd *ipr_cmd;
3899         struct ipr_ioa_cfg *ioa_cfg;
3900         struct ipr_resource_entry *res;
3901         struct ata_port *ap;
3902         int rc = 0;
3903
3904         ENTER;
3905         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3906         res = scsi_cmd->device->hostdata;
3907
3908         if (!res)
3909                 return FAILED;
3910
3911         /*
3912          * If we are currently going through reset/reload, return failed. This will force the
3913          * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
3914          * reset to complete
3915          */
3916         if (ioa_cfg->in_reset_reload)
3917                 return FAILED;
3918         if (ioa_cfg->ioa_is_dead)
3919                 return FAILED;
3920
3921         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3922                 if (ipr_cmd->ioarcb.res_handle == res->cfgte.res_handle) {
3923                         if (ipr_cmd->scsi_cmd)
3924                                 ipr_cmd->done = ipr_scsi_eh_done;
3925                         if (ipr_cmd->qc)
3926                                 ipr_cmd->done = ipr_sata_eh_done;
3927                         if (ipr_cmd->qc && !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
3928                                 ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
3929                                 ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
3930                         }
3931                 }
3932         }
3933
3934         res->resetting_device = 1;
3935         scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
3936
3937         if (ipr_is_gata(res) && res->sata_port) {
3938                 ap = res->sata_port->ap;
3939                 spin_unlock_irq(scsi_cmd->device->host->host_lock);
3940                 ata_std_error_handler(ap);
3941                 spin_lock_irq(scsi_cmd->device->host->host_lock);
3942
3943                 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3944                         if (ipr_cmd->ioarcb.res_handle == res->cfgte.res_handle) {
3945                                 rc = -EIO;
3946                                 break;
3947                         }
3948                 }
3949         } else
3950                 rc = ipr_device_reset(ioa_cfg, res);
3951         res->resetting_device = 0;
3952
3953         LEAVE;
3954         return (rc ? FAILED : SUCCESS);
3955 }
3956
3957 static int ipr_eh_dev_reset(struct scsi_cmnd * cmd)
3958 {
3959         int rc;
3960
3961         spin_lock_irq(cmd->device->host->host_lock);
3962         rc = __ipr_eh_dev_reset(cmd);
3963         spin_unlock_irq(cmd->device->host->host_lock);
3964
3965         return rc;
3966 }
3967
3968 /**
3969  * ipr_bus_reset_done - Op done function for bus reset.
3970  * @ipr_cmd:    ipr command struct
3971  *
3972  * This function is the op done function for a bus reset
3973  *
3974  * Return value:
3975  *      none
3976  **/
3977 static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
3978 {
3979         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3980         struct ipr_resource_entry *res;
3981
3982         ENTER;
3983         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3984                 if (!memcmp(&res->cfgte.res_handle, &ipr_cmd->ioarcb.res_handle,
3985                             sizeof(res->cfgte.res_handle))) {
3986                         scsi_report_bus_reset(ioa_cfg->host, res->cfgte.res_addr.bus);
3987                         break;
3988                 }
3989         }
3990
3991         /*
3992          * If abort has not completed, indicate the reset has, else call the
3993          * abort's done function to wake the sleeping eh thread
3994          */
3995         if (ipr_cmd->sibling->sibling)
3996                 ipr_cmd->sibling->sibling = NULL;
3997         else
3998                 ipr_cmd->sibling->done(ipr_cmd->sibling);
3999
4000         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4001         LEAVE;
4002 }
4003
4004 /**
4005  * ipr_abort_timeout - An abort task has timed out
4006  * @ipr_cmd:    ipr command struct
4007  *
4008  * This function handles when an abort task times out. If this
4009  * happens we issue a bus reset since we have resources tied
4010  * up that must be freed before returning to the midlayer.
4011  *
4012  * Return value:
4013  *      none
4014  **/
4015 static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
4016 {
4017         struct ipr_cmnd *reset_cmd;
4018         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4019         struct ipr_cmd_pkt *cmd_pkt;
4020         unsigned long lock_flags = 0;
4021
4022         ENTER;
4023         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4024         if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
4025                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4026                 return;
4027         }
4028
4029         sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
4030         reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4031         ipr_cmd->sibling = reset_cmd;
4032         reset_cmd->sibling = ipr_cmd;
4033         reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
4034         cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
4035         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4036         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
4037         cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
4038
4039         ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
4040         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4041         LEAVE;
4042 }
4043
4044 /**
4045  * ipr_cancel_op - Cancel specified op
4046  * @scsi_cmd:   scsi command struct
4047  *
4048  * This function cancels specified op.
4049  *
4050  * Return value:
4051  *      SUCCESS / FAILED
4052  **/
4053 static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
4054 {
4055         struct ipr_cmnd *ipr_cmd;
4056         struct ipr_ioa_cfg *ioa_cfg;
4057         struct ipr_resource_entry *res;
4058         struct ipr_cmd_pkt *cmd_pkt;
4059         u32 ioasc;
4060         int op_found = 0;
4061
4062         ENTER;
4063         ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
4064         res = scsi_cmd->device->hostdata;
4065
4066         /* If we are currently going through reset/reload, return failed.
4067          * This will force the mid-layer to call ipr_eh_host_reset,
4068          * which will then go to sleep and wait for the reset to complete
4069          */
4070         if (ioa_cfg->in_reset_reload || ioa_cfg->ioa_is_dead)
4071                 return FAILED;
4072         if (!res || !ipr_is_gscsi(res))
4073                 return FAILED;
4074
4075         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
4076                 if (ipr_cmd->scsi_cmd == scsi_cmd) {
4077                         ipr_cmd->done = ipr_scsi_eh_done;
4078                         op_found = 1;
4079                         break;
4080                 }
4081         }
4082
4083         if (!op_found)
4084                 return SUCCESS;
4085
4086         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4087         ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
4088         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4089         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4090         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4091         ipr_cmd->u.sdev = scsi_cmd->device;
4092
4093         scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
4094                     scsi_cmd->cmnd[0]);
4095         ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
4096         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4097
4098         /*
4099          * If the abort task timed out and we sent a bus reset, we will get
4100          * one the following responses to the abort
4101          */
4102         if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
4103                 ioasc = 0;
4104                 ipr_trace;
4105         }
4106
4107         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4108         if (!ipr_is_naca_model(res))
4109                 res->needs_sync_complete = 1;
4110
4111         LEAVE;
4112         return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
4113 }
4114
4115 /**
4116  * ipr_eh_abort - Abort a single op
4117  * @scsi_cmd:   scsi command struct
4118  *
4119  * Return value:
4120  *      SUCCESS / FAILED
4121  **/
4122 static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
4123 {
4124         unsigned long flags;
4125         int rc;
4126
4127         ENTER;
4128
4129         spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
4130         rc = ipr_cancel_op(scsi_cmd);
4131         spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
4132
4133         LEAVE;
4134         return rc;
4135 }
4136
4137 /**
4138  * ipr_handle_other_interrupt - Handle "other" interrupts
4139  * @ioa_cfg:    ioa config struct
4140  * @int_reg:    interrupt register
4141  *
4142  * Return value:
4143  *      IRQ_NONE / IRQ_HANDLED
4144  **/
4145 static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
4146                                               volatile u32 int_reg)
4147 {
4148         irqreturn_t rc = IRQ_HANDLED;
4149
4150         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
4151                 /* Mask the interrupt */
4152                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
4153
4154                 /* Clear the interrupt */
4155                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
4156                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4157
4158                 list_del(&ioa_cfg->reset_cmd->queue);
4159                 del_timer(&ioa_cfg->reset_cmd->timer);
4160                 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
4161         } else {
4162                 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
4163                         ioa_cfg->ioa_unit_checked = 1;
4164                 else
4165                         dev_err(&ioa_cfg->pdev->dev,
4166                                 "Permanent IOA failure. 0x%08X\n", int_reg);
4167
4168                 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4169                         ioa_cfg->sdt_state = GET_DUMP;
4170
4171                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
4172                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4173         }
4174
4175         return rc;
4176 }
4177
4178 /**
4179  * ipr_isr - Interrupt service routine
4180  * @irq:        irq number
4181  * @devp:       pointer to ioa config struct
4182  *
4183  * Return value:
4184  *      IRQ_NONE / IRQ_HANDLED
4185  **/
4186 static irqreturn_t ipr_isr(int irq, void *devp)
4187 {
4188         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
4189         unsigned long lock_flags = 0;
4190         volatile u32 int_reg, int_mask_reg;
4191         u32 ioasc;
4192         u16 cmd_index;
4193         struct ipr_cmnd *ipr_cmd;
4194         irqreturn_t rc = IRQ_NONE;
4195
4196         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4197
4198         /* If interrupts are disabled, ignore the interrupt */
4199         if (!ioa_cfg->allow_interrupts) {
4200                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4201                 return IRQ_NONE;
4202         }
4203
4204         int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4205         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
4206
4207         /* If an interrupt on the adapter did not occur, ignore it */
4208         if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
4209                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4210                 return IRQ_NONE;
4211         }
4212
4213         while (1) {
4214                 ipr_cmd = NULL;
4215
4216                 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
4217                        ioa_cfg->toggle_bit) {
4218
4219                         cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
4220                                      IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
4221
4222                         if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
4223                                 ioa_cfg->errors_logged++;
4224                                 dev_err(&ioa_cfg->pdev->dev, "Invalid response handle from IOA\n");
4225
4226                                 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4227                                         ioa_cfg->sdt_state = GET_DUMP;
4228
4229                                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4230                                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4231                                 return IRQ_HANDLED;
4232                         }
4233
4234                         ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
4235
4236                         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4237
4238                         ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
4239
4240                         list_del(&ipr_cmd->queue);
4241                         del_timer(&ipr_cmd->timer);
4242                         ipr_cmd->done(ipr_cmd);
4243
4244                         rc = IRQ_HANDLED;
4245
4246                         if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
4247                                 ioa_cfg->hrrq_curr++;
4248                         } else {
4249                                 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
4250                                 ioa_cfg->toggle_bit ^= 1u;
4251                         }
4252                 }
4253
4254                 if (ipr_cmd != NULL) {
4255                         /* Clear the PCI interrupt */
4256                         writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg);
4257                         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
4258                 } else
4259                         break;
4260         }
4261
4262         if (unlikely(rc == IRQ_NONE))
4263                 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
4264
4265         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4266         return rc;
4267 }
4268
4269 /**
4270  * ipr_build_ioadl - Build a scatter/gather list and map the buffer
4271  * @ioa_cfg:    ioa config struct
4272  * @ipr_cmd:    ipr command struct
4273  *
4274  * Return value:
4275  *      0 on success / -1 on failure
4276  **/
4277 static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
4278                            struct ipr_cmnd *ipr_cmd)
4279 {
4280         int i, nseg;
4281         struct scatterlist *sg;
4282         u32 length;
4283         u32 ioadl_flags = 0;
4284         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4285         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4286         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4287
4288         length = scsi_bufflen(scsi_cmd);
4289         if (!length)
4290                 return 0;
4291
4292         nseg = scsi_dma_map(scsi_cmd);
4293         if (nseg < 0) {
4294                 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
4295                 return -1;
4296         }
4297
4298         ipr_cmd->dma_use_sg = nseg;
4299
4300         if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
4301                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
4302                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4303                 ioarcb->write_data_transfer_length = cpu_to_be32(length);
4304                 ioarcb->write_ioadl_len =
4305                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
4306         } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
4307                 ioadl_flags = IPR_IOADL_FLAGS_READ;
4308                 ioarcb->read_data_transfer_length = cpu_to_be32(length);
4309                 ioarcb->read_ioadl_len =
4310                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
4311         }
4312
4313         if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->add_data.u.ioadl)) {
4314                 ioadl = ioarcb->add_data.u.ioadl;
4315                 ioarcb->write_ioadl_addr =
4316                         cpu_to_be32(be32_to_cpu(ioarcb->ioarcb_host_pci_addr) +
4317                                     offsetof(struct ipr_ioarcb, add_data));
4318                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
4319         }
4320
4321         scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
4322                 ioadl[i].flags_and_data_len =
4323                         cpu_to_be32(ioadl_flags | sg_dma_len(sg));
4324                 ioadl[i].address = cpu_to_be32(sg_dma_address(sg));
4325         }
4326
4327         ioadl[i-1].flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
4328         return 0;
4329 }
4330
4331 /**
4332  * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
4333  * @scsi_cmd:   scsi command struct
4334  *
4335  * Return value:
4336  *      task attributes
4337  **/
4338 static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
4339 {
4340         u8 tag[2];
4341         u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
4342
4343         if (scsi_populate_tag_msg(scsi_cmd, tag)) {
4344                 switch (tag[0]) {
4345                 case MSG_SIMPLE_TAG:
4346                         rc = IPR_FLAGS_LO_SIMPLE_TASK;
4347                         break;
4348                 case MSG_HEAD_TAG:
4349                         rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
4350                         break;
4351                 case MSG_ORDERED_TAG:
4352                         rc = IPR_FLAGS_LO_ORDERED_TASK;
4353                         break;
4354                 };
4355         }
4356
4357         return rc;
4358 }
4359
4360 /**
4361  * ipr_erp_done - Process completion of ERP for a device
4362  * @ipr_cmd:            ipr command struct
4363  *
4364  * This function copies the sense buffer into the scsi_cmd
4365  * struct and pushes the scsi_done function.
4366  *
4367  * Return value:
4368  *      nothing
4369  **/
4370 static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
4371 {
4372         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4373         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4374         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4375         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4376
4377         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
4378                 scsi_cmd->result |= (DID_ERROR << 16);
4379                 scmd_printk(KERN_ERR, scsi_cmd,
4380                             "Request Sense failed with IOASC: 0x%08X\n", ioasc);
4381         } else {
4382                 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
4383                        SCSI_SENSE_BUFFERSIZE);
4384         }
4385
4386         if (res) {
4387                 if (!ipr_is_naca_model(res))
4388                         res->needs_sync_complete = 1;
4389                 res->in_erp = 0;
4390         }
4391         scsi_dma_unmap(ipr_cmd->scsi_cmd);
4392         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4393         scsi_cmd->scsi_done(scsi_cmd);
4394 }
4395
4396 /**
4397  * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
4398  * @ipr_cmd:    ipr command struct
4399  *
4400  * Return value:
4401  *      none
4402  **/
4403 static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
4404 {
4405         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4406         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4407         dma_addr_t dma_addr = be32_to_cpu(ioarcb->ioarcb_host_pci_addr);
4408
4409         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
4410         ioarcb->write_data_transfer_length = 0;
4411         ioarcb->read_data_transfer_length = 0;
4412         ioarcb->write_ioadl_len = 0;
4413         ioarcb->read_ioadl_len = 0;
4414         ioasa->ioasc = 0;
4415         ioasa->residual_data_len = 0;
4416         ioarcb->write_ioadl_addr =
4417                 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
4418         ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
4419 }
4420
4421 /**
4422  * ipr_erp_request_sense - Send request sense to a device
4423  * @ipr_cmd:    ipr command struct
4424  *
4425  * This function sends a request sense to a device as a result
4426  * of a check condition.
4427  *
4428  * Return value:
4429  *      nothing
4430  **/
4431 static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
4432 {
4433         struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4434         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4435
4436         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
4437                 ipr_erp_done(ipr_cmd);
4438                 return;
4439         }
4440
4441         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
4442
4443         cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
4444         cmd_pkt->cdb[0] = REQUEST_SENSE;
4445         cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
4446         cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
4447         cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
4448         cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
4449
4450         ipr_cmd->ioadl[0].flags_and_data_len =
4451                 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | SCSI_SENSE_BUFFERSIZE);
4452         ipr_cmd->ioadl[0].address =
4453                 cpu_to_be32(ipr_cmd->sense_buffer_dma);
4454
4455         ipr_cmd->ioarcb.read_ioadl_len =
4456                 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4457         ipr_cmd->ioarcb.read_data_transfer_length =
4458                 cpu_to_be32(SCSI_SENSE_BUFFERSIZE);
4459
4460         ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
4461                    IPR_REQUEST_SENSE_TIMEOUT * 2);
4462 }
4463
4464 /**
4465  * ipr_erp_cancel_all - Send cancel all to a device
4466  * @ipr_cmd:    ipr command struct
4467  *
4468  * This function sends a cancel all to a device to clear the
4469  * queue. If we are running TCQ on the device, QERR is set to 1,
4470  * which means all outstanding ops have been dropped on the floor.
4471  * Cancel all will return them to us.
4472  *
4473  * Return value:
4474  *      nothing
4475  **/
4476 static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
4477 {
4478         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4479         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4480         struct ipr_cmd_pkt *cmd_pkt;
4481
4482         res->in_erp = 1;
4483
4484         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
4485
4486         if (!scsi_get_tag_type(scsi_cmd->device)) {
4487                 ipr_erp_request_sense(ipr_cmd);
4488                 return;
4489         }
4490
4491         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4492         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4493         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4494
4495         ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
4496                    IPR_CANCEL_ALL_TIMEOUT);
4497 }
4498
4499 /**
4500  * ipr_dump_ioasa - Dump contents of IOASA
4501  * @ioa_cfg:    ioa config struct
4502  * @ipr_cmd:    ipr command struct
4503  * @res:                resource entry struct
4504  *
4505  * This function is invoked by the interrupt handler when ops
4506  * fail. It will log the IOASA if appropriate. Only called
4507  * for GPDD ops.
4508  *
4509  * Return value:
4510  *      none
4511  **/
4512 static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
4513                            struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
4514 {
4515         int i;
4516         u16 data_len;
4517         u32 ioasc, fd_ioasc;
4518         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4519         __be32 *ioasa_data = (__be32 *)ioasa;
4520         int error_index;
4521
4522         ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
4523         fd_ioasc = be32_to_cpu(ioasa->fd_ioasc) & IPR_IOASC_IOASC_MASK;
4524
4525         if (0 == ioasc)
4526                 return;
4527
4528         if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
4529                 return;
4530
4531         if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
4532                 error_index = ipr_get_error(fd_ioasc);
4533         else
4534                 error_index = ipr_get_error(ioasc);
4535
4536         if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
4537                 /* Don't log an error if the IOA already logged one */
4538                 if (ioasa->ilid != 0)
4539                         return;
4540
4541                 if (!ipr_is_gscsi(res))
4542                         return;
4543
4544                 if (ipr_error_table[error_index].log_ioasa == 0)
4545                         return;
4546         }
4547
4548         ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
4549
4550         if (sizeof(struct ipr_ioasa) < be16_to_cpu(ioasa->ret_stat_len))
4551                 data_len = sizeof(struct ipr_ioasa);
4552         else
4553                 data_len = be16_to_cpu(ioasa->ret_stat_len);
4554
4555         ipr_err("IOASA Dump:\n");
4556
4557         for (i = 0; i < data_len / 4; i += 4) {
4558                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
4559                         be32_to_cpu(ioasa_data[i]),
4560                         be32_to_cpu(ioasa_data[i+1]),
4561                         be32_to_cpu(ioasa_data[i+2]),
4562                         be32_to_cpu(ioasa_data[i+3]));
4563         }
4564 }
4565
4566 /**
4567  * ipr_gen_sense - Generate SCSI sense data from an IOASA
4568  * @ioasa:              IOASA
4569  * @sense_buf:  sense data buffer
4570  *
4571  * Return value:
4572  *      none
4573  **/
4574 static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
4575 {
4576         u32 failing_lba;
4577         u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
4578         struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
4579         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4580         u32 ioasc = be32_to_cpu(ioasa->ioasc);
4581
4582         memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
4583
4584         if (ioasc >= IPR_FIRST_DRIVER_IOASC)
4585                 return;
4586
4587         ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
4588
4589         if (ipr_is_vset_device(res) &&
4590             ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
4591             ioasa->u.vset.failing_lba_hi != 0) {
4592                 sense_buf[0] = 0x72;
4593                 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
4594                 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
4595                 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
4596
4597                 sense_buf[7] = 12;
4598                 sense_buf[8] = 0;
4599                 sense_buf[9] = 0x0A;
4600                 sense_buf[10] = 0x80;
4601
4602                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
4603
4604                 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
4605                 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
4606                 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
4607                 sense_buf[15] = failing_lba & 0x000000ff;
4608
4609                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
4610
4611                 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
4612                 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
4613                 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
4614                 sense_buf[19] = failing_lba & 0x000000ff;
4615         } else {
4616                 sense_buf[0] = 0x70;
4617                 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
4618                 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
4619                 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
4620
4621                 /* Illegal request */
4622                 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
4623                     (be32_to_cpu(ioasa->ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
4624                         sense_buf[7] = 10;      /* additional length */
4625
4626                         /* IOARCB was in error */
4627                         if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
4628                                 sense_buf[15] = 0xC0;
4629                         else    /* Parameter data was invalid */
4630                                 sense_buf[15] = 0x80;
4631
4632                         sense_buf[16] =
4633                             ((IPR_FIELD_POINTER_MASK &
4634                               be32_to_cpu(ioasa->ioasc_specific)) >> 8) & 0xff;
4635                         sense_buf[17] =
4636                             (IPR_FIELD_POINTER_MASK &
4637                              be32_to_cpu(ioasa->ioasc_specific)) & 0xff;
4638                 } else {
4639                         if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
4640                                 if (ipr_is_vset_device(res))
4641                                         failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
4642                                 else
4643                                         failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
4644
4645                                 sense_buf[0] |= 0x80;   /* Or in the Valid bit */
4646                                 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
4647                                 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
4648                                 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
4649                                 sense_buf[6] = failing_lba & 0x000000ff;
4650                         }
4651
4652                         sense_buf[7] = 6;       /* additional length */
4653                 }
4654         }
4655 }
4656
4657 /**
4658  * ipr_get_autosense - Copy autosense data to sense buffer
4659  * @ipr_cmd:    ipr command struct
4660  *
4661  * This function copies the autosense buffer to the buffer
4662  * in the scsi_cmd, if there is autosense available.
4663  *
4664  * Return value:
4665  *      1 if autosense was available / 0 if not
4666  **/
4667 static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
4668 {
4669         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4670
4671         if ((be32_to_cpu(ioasa->ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
4672                 return 0;
4673
4674         memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
4675                min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
4676                    SCSI_SENSE_BUFFERSIZE));
4677         return 1;
4678 }
4679
4680 /**
4681  * ipr_erp_start - Process an error response for a SCSI op
4682  * @ioa_cfg:    ioa config struct
4683  * @ipr_cmd:    ipr command struct
4684  *
4685  * This function determines whether or not to initiate ERP
4686  * on the affected device.
4687  *
4688  * Return value:
4689  *      nothing
4690  **/
4691 static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
4692                               struct ipr_cmnd *ipr_cmd)
4693 {
4694         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4695         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4696         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4697         u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
4698
4699         if (!res) {
4700                 ipr_scsi_eh_done(ipr_cmd);
4701                 return;
4702         }
4703
4704         if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
4705                 ipr_gen_sense(ipr_cmd);
4706
4707         ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
4708
4709         switch (masked_ioasc) {
4710         case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
4711                 if (ipr_is_naca_model(res))
4712                         scsi_cmd->result |= (DID_ABORT << 16);
4713                 else
4714                         scsi_cmd->result |= (DID_IMM_RETRY << 16);
4715                 break;
4716         case IPR_IOASC_IR_RESOURCE_HANDLE:
4717         case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
4718                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
4719                 break;
4720         case IPR_IOASC_HW_SEL_TIMEOUT:
4721                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
4722                 if (!ipr_is_naca_model(res))
4723                         res->needs_sync_complete = 1;
4724                 break;
4725         case IPR_IOASC_SYNC_REQUIRED:
4726                 if (!res->in_erp)
4727                         res->needs_sync_complete = 1;
4728                 scsi_cmd->result |= (DID_IMM_RETRY << 16);
4729                 break;
4730         case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
4731         case IPR_IOASA_IR_DUAL_IOA_DISABLED:
4732                 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
4733                 break;
4734         case IPR_IOASC_BUS_WAS_RESET:
4735         case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
4736                 /*
4737                  * Report the bus reset and ask for a retry. The device
4738                  * will give CC/UA the next command.
4739                  */
4740                 if (!res->resetting_device)
4741                         scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
4742                 scsi_cmd->result |= (DID_ERROR << 16);
4743                 if (!ipr_is_naca_model(res))
4744                         res->needs_sync_complete = 1;
4745                 break;
4746         case IPR_IOASC_HW_DEV_BUS_STATUS:
4747                 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
4748                 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
4749                         if (!ipr_get_autosense(ipr_cmd)) {
4750                                 if (!ipr_is_naca_model(res)) {
4751                                         ipr_erp_cancel_all(ipr_cmd);
4752                                         return;
4753                                 }
4754                         }
4755                 }
4756                 if (!ipr_is_naca_model(res))
4757                         res->needs_sync_complete = 1;
4758                 break;
4759         case IPR_IOASC_NR_INIT_CMD_REQUIRED:
4760                 break;
4761         default:
4762                 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
4763                         scsi_cmd->result |= (DID_ERROR << 16);
4764                 if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
4765                         res->needs_sync_complete = 1;
4766                 break;
4767         }
4768
4769         scsi_dma_unmap(ipr_cmd->scsi_cmd);
4770         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4771         scsi_cmd->scsi_done(scsi_cmd);
4772 }
4773
4774 /**
4775  * ipr_scsi_done - mid-layer done function
4776  * @ipr_cmd:    ipr command struct
4777  *
4778  * This function is invoked by the interrupt handler for
4779  * ops generated by the SCSI mid-layer
4780  *
4781  * Return value:
4782  *      none
4783  **/
4784 static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
4785 {
4786         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4787         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4788         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4789
4790         scsi_set_resid(scsi_cmd, be32_to_cpu(ipr_cmd->ioasa.residual_data_len));
4791
4792         if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
4793                 scsi_dma_unmap(ipr_cmd->scsi_cmd);
4794                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4795                 scsi_cmd->scsi_done(scsi_cmd);
4796         } else
4797                 ipr_erp_start(ioa_cfg, ipr_cmd);
4798 }
4799
4800 /**
4801  * ipr_queuecommand - Queue a mid-layer request
4802  * @scsi_cmd:   scsi command struct
4803  * @done:               done function
4804  *
4805  * This function queues a request generated by the mid-layer.
4806  *
4807  * Return value:
4808  *      0 on success
4809  *      SCSI_MLQUEUE_DEVICE_BUSY if device is busy
4810  *      SCSI_MLQUEUE_HOST_BUSY if host is busy
4811  **/
4812 static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
4813                             void (*done) (struct scsi_cmnd *))
4814 {
4815         struct ipr_ioa_cfg *ioa_cfg;
4816         struct ipr_resource_entry *res;
4817         struct ipr_ioarcb *ioarcb;
4818         struct ipr_cmnd *ipr_cmd;
4819         int rc = 0;
4820
4821         scsi_cmd->scsi_done = done;
4822         ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
4823         res = scsi_cmd->device->hostdata;
4824         scsi_cmd->result = (DID_OK << 16);
4825
4826         /*
4827          * We are currently blocking all devices due to a host reset
4828          * We have told the host to stop giving us new requests, but
4829          * ERP ops don't count. FIXME
4830          */
4831         if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
4832                 return SCSI_MLQUEUE_HOST_BUSY;
4833
4834         /*
4835          * FIXME - Create scsi_set_host_offline interface
4836          *  and the ioa_is_dead check can be removed
4837          */
4838         if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
4839                 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
4840                 scsi_cmd->result = (DID_NO_CONNECT << 16);
4841                 scsi_cmd->scsi_done(scsi_cmd);
4842                 return 0;
4843         }
4844
4845         if (ipr_is_gata(res) && res->sata_port)
4846                 return ata_sas_queuecmd(scsi_cmd, done, res->sata_port->ap);
4847
4848         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4849         ioarcb = &ipr_cmd->ioarcb;
4850         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
4851
4852         memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
4853         ipr_cmd->scsi_cmd = scsi_cmd;
4854         ioarcb->res_handle = res->cfgte.res_handle;
4855         ipr_cmd->done = ipr_scsi_done;
4856         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
4857
4858         if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
4859                 if (scsi_cmd->underflow == 0)
4860                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
4861
4862                 if (res->needs_sync_complete) {
4863                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
4864                         res->needs_sync_complete = 0;
4865                 }
4866
4867                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
4868                 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
4869                 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
4870                 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
4871         }
4872
4873         if (scsi_cmd->cmnd[0] >= 0xC0 &&
4874             (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
4875                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4876
4877         if (likely(rc == 0))
4878                 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
4879
4880         if (likely(rc == 0)) {
4881                 mb();
4882                 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
4883                        ioa_cfg->regs.ioarrin_reg);
4884         } else {
4885                  list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4886                  return SCSI_MLQUEUE_HOST_BUSY;
4887         }
4888
4889         return 0;
4890 }
4891
4892 /**
4893  * ipr_ioctl - IOCTL handler
4894  * @sdev:       scsi device struct
4895  * @cmd:        IOCTL cmd
4896  * @arg:        IOCTL arg
4897  *
4898  * Return value:
4899  *      0 on success / other on failure
4900  **/
4901 static int ipr_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
4902 {
4903         struct ipr_resource_entry *res;
4904
4905         res = (struct ipr_resource_entry *)sdev->hostdata;
4906         if (res && ipr_is_gata(res))
4907                 return ata_scsi_ioctl(sdev, cmd, arg);
4908
4909         return -EINVAL;
4910 }
4911
4912 /**
4913  * ipr_info - Get information about the card/driver
4914  * @scsi_host:  scsi host struct
4915  *
4916  * Return value:
4917  *      pointer to buffer with description string
4918  **/
4919 static const char * ipr_ioa_info(struct Scsi_Host *host)
4920 {
4921         static char buffer[512];
4922         struct ipr_ioa_cfg *ioa_cfg;
4923         unsigned long lock_flags = 0;
4924
4925         ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
4926
4927         spin_lock_irqsave(host->host_lock, lock_flags);
4928         sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
4929         spin_unlock_irqrestore(host->host_lock, lock_flags);
4930
4931         return buffer;
4932 }
4933
4934 static struct scsi_host_template driver_template = {
4935         .module = THIS_MODULE,
4936         .name = "IPR",
4937         .info = ipr_ioa_info,
4938         .ioctl = ipr_ioctl,
4939         .queuecommand = ipr_queuecommand,
4940         .eh_abort_handler = ipr_eh_abort,
4941         .eh_device_reset_handler = ipr_eh_dev_reset,
4942         .eh_host_reset_handler = ipr_eh_host_reset,
4943         .slave_alloc = ipr_slave_alloc,
4944         .slave_configure = ipr_slave_configure,
4945         .slave_destroy = ipr_slave_destroy,
4946         .target_alloc = ipr_target_alloc,
4947         .target_destroy = ipr_target_destroy,
4948         .change_queue_depth = ipr_change_queue_depth,
4949         .change_queue_type = ipr_change_queue_type,
4950         .bios_param = ipr_biosparam,
4951         .can_queue = IPR_MAX_COMMANDS,
4952         .this_id = -1,
4953         .sg_tablesize = IPR_MAX_SGLIST,
4954         .max_sectors = IPR_IOA_MAX_SECTORS,
4955         .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
4956         .use_clustering = ENABLE_CLUSTERING,
4957         .shost_attrs = ipr_ioa_attrs,
4958         .sdev_attrs = ipr_dev_attrs,
4959         .proc_name = IPR_NAME
4960 };
4961
4962 /**
4963  * ipr_ata_phy_reset - libata phy_reset handler
4964  * @ap:         ata port to reset
4965  *
4966  **/
4967 static void ipr_ata_phy_reset(struct ata_port *ap)
4968 {
4969         unsigned long flags;
4970         struct ipr_sata_port *sata_port = ap->private_data;
4971         struct ipr_resource_entry *res = sata_port->res;
4972         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
4973         int rc;
4974
4975         ENTER;
4976         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
4977         while(ioa_cfg->in_reset_reload) {
4978                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
4979                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4980                 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
4981         }
4982
4983         if (!ioa_cfg->allow_cmds)
4984                 goto out_unlock;
4985
4986         rc = ipr_device_reset(ioa_cfg, res);
4987
4988         if (rc) {
4989                 ata_port_disable(ap);
4990                 goto out_unlock;
4991         }
4992
4993         switch(res->cfgte.proto) {
4994         case IPR_PROTO_SATA:
4995         case IPR_PROTO_SAS_STP:
4996                 ap->link.device[0].class = ATA_DEV_ATA;
4997                 break;
4998         case IPR_PROTO_SATA_ATAPI:
4999         case IPR_PROTO_SAS_STP_ATAPI:
5000                 ap->link.device[0].class = ATA_DEV_ATAPI;
5001                 break;
5002         default:
5003                 ap->link.device[0].class = ATA_DEV_UNKNOWN;
5004                 ata_port_disable(ap);
5005                 break;
5006         };
5007
5008 out_unlock:
5009         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5010         LEAVE;
5011 }
5012
5013 /**
5014  * ipr_ata_post_internal - Cleanup after an internal command
5015  * @qc: ATA queued command
5016  *
5017  * Return value:
5018  *      none
5019  **/
5020 static void ipr_ata_post_internal(struct ata_queued_cmd *qc)
5021 {
5022         struct ipr_sata_port *sata_port = qc->ap->private_data;
5023         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5024         struct ipr_cmnd *ipr_cmd;
5025         unsigned long flags;
5026
5027         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5028         while(ioa_cfg->in_reset_reload) {
5029                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5030                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5031                 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5032         }
5033
5034         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
5035                 if (ipr_cmd->qc == qc) {
5036                         ipr_device_reset(ioa_cfg, sata_port->res);
5037                         break;
5038                 }
5039         }
5040         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5041 }
5042
5043 /**
5044  * ipr_copy_sata_tf - Copy a SATA taskfile to an IOA data structure
5045  * @regs:       destination
5046  * @tf: source ATA taskfile
5047  *
5048  * Return value:
5049  *      none
5050  **/
5051 static void ipr_copy_sata_tf(struct ipr_ioarcb_ata_regs *regs,
5052                              struct ata_taskfile *tf)
5053 {
5054         regs->feature = tf->feature;
5055         regs->nsect = tf->nsect;
5056         regs->lbal = tf->lbal;
5057         regs->lbam = tf->lbam;
5058         regs->lbah = tf->lbah;
5059         regs->device = tf->device;
5060         regs->command = tf->command;
5061         regs->hob_feature = tf->hob_feature;
5062         regs->hob_nsect = tf->hob_nsect;
5063         regs->hob_lbal = tf->hob_lbal;
5064         regs->hob_lbam = tf->hob_lbam;
5065         regs->hob_lbah = tf->hob_lbah;
5066         regs->ctl = tf->ctl;
5067 }
5068
5069 /**
5070  * ipr_sata_done - done function for SATA commands
5071  * @ipr_cmd:    ipr command struct
5072  *
5073  * This function is invoked by the interrupt handler for
5074  * ops generated by the SCSI mid-layer to SATA devices
5075  *
5076  * Return value:
5077  *      none
5078  **/
5079 static void ipr_sata_done(struct ipr_cmnd *ipr_cmd)
5080 {
5081         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5082         struct ata_queued_cmd *qc = ipr_cmd->qc;
5083         struct ipr_sata_port *sata_port = qc->ap->private_data;
5084         struct ipr_resource_entry *res = sata_port->res;
5085         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5086
5087         memcpy(&sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
5088                sizeof(struct ipr_ioasa_gata));
5089         ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
5090
5091         if (be32_to_cpu(ipr_cmd->ioasa.ioasc_specific) & IPR_ATA_DEVICE_WAS_RESET)
5092                 scsi_report_device_reset(ioa_cfg->host, res->cfgte.res_addr.bus,
5093                                          res->cfgte.res_addr.target);
5094
5095         if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
5096                 qc->err_mask |= __ac_err_mask(ipr_cmd->ioasa.u.gata.status);
5097         else
5098                 qc->err_mask |= ac_err_mask(ipr_cmd->ioasa.u.gata.status);
5099         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5100         ata_qc_complete(qc);
5101 }
5102
5103 /**
5104  * ipr_build_ata_ioadl - Build an ATA scatter/gather list
5105  * @ipr_cmd:    ipr command struct
5106  * @qc:         ATA queued command
5107  *
5108  **/
5109 static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
5110                                 struct ata_queued_cmd *qc)
5111 {
5112         u32 ioadl_flags = 0;
5113         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5114         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5115         struct ipr_ioadl_desc *last_ioadl = NULL;
5116         int len = qc->nbytes;
5117         struct scatterlist *sg;
5118         unsigned int si;
5119
5120         if (len == 0)
5121                 return;
5122
5123         if (qc->dma_dir == DMA_TO_DEVICE) {
5124                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5125                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5126                 ioarcb->write_data_transfer_length = cpu_to_be32(len);
5127                 ioarcb->write_ioadl_len =
5128                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5129         } else if (qc->dma_dir == DMA_FROM_DEVICE) {
5130                 ioadl_flags = IPR_IOADL_FLAGS_READ;
5131                 ioarcb->read_data_transfer_length = cpu_to_be32(len);
5132                 ioarcb->read_ioadl_len =
5133                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5134         }
5135
5136         for_each_sg(qc->sg, sg, qc->n_elem, si) {
5137                 ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5138                 ioadl->address = cpu_to_be32(sg_dma_address(sg));
5139
5140                 last_ioadl = ioadl;
5141                 ioadl++;
5142         }
5143
5144         if (likely(last_ioadl))
5145                 last_ioadl->flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5146 }
5147
5148 /**
5149  * ipr_qc_issue - Issue a SATA qc to a device
5150  * @qc: queued command
5151  *
5152  * Return value:
5153  *      0 if success
5154  **/
5155 static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
5156 {
5157         struct ata_port *ap = qc->ap;
5158         struct ipr_sata_port *sata_port = ap->private_data;
5159         struct ipr_resource_entry *res = sata_port->res;
5160         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5161         struct ipr_cmnd *ipr_cmd;
5162         struct ipr_ioarcb *ioarcb;
5163         struct ipr_ioarcb_ata_regs *regs;
5164
5165         if (unlikely(!ioa_cfg->allow_cmds || ioa_cfg->ioa_is_dead))
5166                 return AC_ERR_SYSTEM;
5167
5168         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5169         ioarcb = &ipr_cmd->ioarcb;
5170         regs = &ioarcb->add_data.u.regs;
5171
5172         memset(&ioarcb->add_data, 0, sizeof(ioarcb->add_data));
5173         ioarcb->add_cmd_parms_len = cpu_to_be32(sizeof(ioarcb->add_data.u.regs));
5174
5175         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
5176         ipr_cmd->qc = qc;
5177         ipr_cmd->done = ipr_sata_done;
5178         ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
5179         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_ATA_PASSTHRU;
5180         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
5181         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5182         ipr_cmd->dma_use_sg = qc->n_elem;
5183
5184         ipr_build_ata_ioadl(ipr_cmd, qc);
5185         regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
5186         ipr_copy_sata_tf(regs, &qc->tf);
5187         memcpy(ioarcb->cmd_pkt.cdb, qc->cdb, IPR_MAX_CDB_LEN);
5188         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
5189
5190         switch (qc->tf.protocol) {
5191         case ATA_PROT_NODATA:
5192         case ATA_PROT_PIO:
5193                 break;
5194
5195         case ATA_PROT_DMA:
5196                 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
5197                 break;
5198
5199         case ATAPI_PROT_PIO:
5200         case ATAPI_PROT_NODATA:
5201                 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
5202                 break;
5203
5204         case ATAPI_PROT_DMA:
5205                 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
5206                 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
5207                 break;
5208
5209         default:
5210                 WARN_ON(1);
5211                 return AC_ERR_INVALID;
5212         }
5213
5214         mb();
5215         writel(be32_to_cpu(ioarcb->ioarcb_host_pci_addr),
5216                ioa_cfg->regs.ioarrin_reg);
5217         return 0;
5218 }
5219
5220 /**
5221  * ipr_qc_fill_rtf - Read result TF
5222  * @qc: ATA queued command
5223  *
5224  * Return value:
5225  *      true
5226  **/
5227 static bool ipr_qc_fill_rtf(struct ata_queued_cmd *qc)
5228 {
5229         struct ipr_sata_port *sata_port = qc->ap->private_data;
5230         struct ipr_ioasa_gata *g = &sata_port->ioasa;
5231         struct ata_taskfile *tf = &qc->result_tf;
5232
5233         tf->feature = g->error;
5234         tf->nsect = g->nsect;
5235         tf->lbal = g->lbal;
5236         tf->lbam = g->lbam;
5237         tf->lbah = g->lbah;
5238         tf->device = g->device;
5239         tf->command = g->status;
5240         tf->hob_nsect = g->hob_nsect;
5241         tf->hob_lbal = g->hob_lbal;
5242         tf->hob_lbam = g->hob_lbam;
5243         tf->hob_lbah = g->hob_lbah;
5244         tf->ctl = g->alt_status;
5245
5246         return true;
5247 }
5248
5249 static struct ata_port_operations ipr_sata_ops = {
5250         .phy_reset = ipr_ata_phy_reset,
5251         .hardreset = ipr_sata_reset,
5252         .post_internal_cmd = ipr_ata_post_internal,
5253         .qc_prep = ata_noop_qc_prep,
5254         .qc_issue = ipr_qc_issue,
5255         .qc_fill_rtf = ipr_qc_fill_rtf,
5256         .port_start = ata_sas_port_start,
5257         .port_stop = ata_sas_port_stop
5258 };
5259
5260 static struct ata_port_info sata_port_info = {
5261         .flags  = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
5262         ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
5263         .pio_mask       = 0x10, /* pio4 */
5264         .mwdma_mask = 0x07,
5265         .udma_mask      = 0x7f, /* udma0-6 */
5266         .port_ops       = &ipr_sata_ops
5267 };
5268
5269 #ifdef CONFIG_PPC_PSERIES
5270 static const u16 ipr_blocked_processors[] = {
5271         PV_NORTHSTAR,
5272         PV_PULSAR,
5273         PV_POWER4,
5274         PV_ICESTAR,
5275         PV_SSTAR,
5276         PV_POWER4p,
5277         PV_630,
5278         PV_630p
5279 };
5280
5281 /**
5282  * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
5283  * @ioa_cfg:    ioa cfg struct
5284  *
5285  * Adapters that use Gemstone revision < 3.1 do not work reliably on
5286  * certain pSeries hardware. This function determines if the given
5287  * adapter is in one of these confgurations or not.
5288  *
5289  * Return value:
5290  *      1 if adapter is not supported / 0 if adapter is supported
5291  **/
5292 static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
5293 {
5294         int i;
5295
5296         if ((ioa_cfg->type == 0x5702) && (ioa_cfg->pdev->revision < 4)) {
5297                 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
5298                         if (__is_processor(ipr_blocked_processors[i]))
5299                                 return 1;
5300                 }
5301         }
5302         return 0;
5303 }
5304 #else
5305 #define ipr_invalid_adapter(ioa_cfg) 0
5306 #endif
5307
5308 /**
5309  * ipr_ioa_bringdown_done - IOA bring down completion.
5310  * @ipr_cmd:    ipr command struct
5311  *
5312  * This function processes the completion of an adapter bring down.
5313  * It wakes any reset sleepers.
5314  *
5315  * Return value:
5316  *      IPR_RC_JOB_RETURN
5317  **/
5318 static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
5319 {
5320         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5321
5322         ENTER;
5323         ioa_cfg->in_reset_reload = 0;
5324         ioa_cfg->reset_retries = 0;
5325         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5326         wake_up_all(&ioa_cfg->reset_wait_q);
5327
5328         spin_unlock_irq(ioa_cfg->host->host_lock);
5329         scsi_unblock_requests(ioa_cfg->host);
5330         spin_lock_irq(ioa_cfg->host->host_lock);
5331         LEAVE;
5332
5333         return IPR_RC_JOB_RETURN;
5334 }
5335
5336 /**
5337  * ipr_ioa_reset_done - IOA reset completion.
5338  * @ipr_cmd:    ipr command struct
5339  *
5340  * This function processes the completion of an adapter reset.
5341  * It schedules any necessary mid-layer add/removes and
5342  * wakes any reset sleepers.
5343  *
5344  * Return value:
5345  *      IPR_RC_JOB_RETURN
5346  **/
5347 static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
5348 {
5349         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5350         struct ipr_resource_entry *res;
5351         struct ipr_hostrcb *hostrcb, *temp;
5352         int i = 0;
5353
5354         ENTER;
5355         ioa_cfg->in_reset_reload = 0;
5356         ioa_cfg->allow_cmds = 1;
5357         ioa_cfg->reset_cmd = NULL;
5358         ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
5359
5360         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
5361                 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
5362                         ipr_trace;
5363                         break;
5364                 }
5365         }
5366         schedule_work(&ioa_cfg->work_q);
5367
5368         list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
5369                 list_del(&hostrcb->queue);
5370                 if (i++ < IPR_NUM_LOG_HCAMS)
5371                         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
5372                 else
5373                         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
5374         }
5375
5376         scsi_report_bus_reset(ioa_cfg->host, IPR_VSET_BUS);
5377         dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
5378
5379         ioa_cfg->reset_retries = 0;
5380         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5381         wake_up_all(&ioa_cfg->reset_wait_q);
5382
5383         spin_unlock_irq(ioa_cfg->host->host_lock);
5384         scsi_unblock_requests(ioa_cfg->host);
5385         spin_lock_irq(ioa_cfg->host->host_lock);
5386
5387         if (!ioa_cfg->allow_cmds)
5388                 scsi_block_requests(ioa_cfg->host);
5389
5390         LEAVE;
5391         return IPR_RC_JOB_RETURN;
5392 }
5393
5394 /**
5395  * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
5396  * @supported_dev:      supported device struct
5397  * @vpids:                      vendor product id struct
5398  *
5399  * Return value:
5400  *      none
5401  **/
5402 static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
5403                                  struct ipr_std_inq_vpids *vpids)
5404 {
5405         memset(supported_dev, 0, sizeof(struct ipr_supported_device));
5406         memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
5407         supported_dev->num_records = 1;
5408         supported_dev->data_length =
5409                 cpu_to_be16(sizeof(struct ipr_supported_device));
5410         supported_dev->reserved = 0;
5411 }
5412
5413 /**
5414  * ipr_set_supported_devs - Send Set Supported Devices for a device
5415  * @ipr_cmd:    ipr command struct
5416  *
5417  * This function send a Set Supported Devices to the adapter
5418  *
5419  * Return value:
5420  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5421  **/
5422 static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
5423 {
5424         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5425         struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
5426         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5427         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5428         struct ipr_resource_entry *res = ipr_cmd->u.res;
5429
5430         ipr_cmd->job_step = ipr_ioa_reset_done;
5431
5432         list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
5433                 if (!ipr_is_scsi_disk(res))
5434                         continue;
5435
5436                 ipr_cmd->u.res = res;
5437                 ipr_set_sup_dev_dflt(supp_dev, &res->cfgte.std_inq_data.vpids);
5438
5439                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5440                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5441                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5442
5443                 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
5444                 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
5445                 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
5446
5447                 ioadl->flags_and_data_len = cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST |
5448                                                         sizeof(struct ipr_supported_device));
5449                 ioadl->address = cpu_to_be32(ioa_cfg->vpd_cbs_dma +
5450                                              offsetof(struct ipr_misc_cbs, supp_dev));
5451                 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5452                 ioarcb->write_data_transfer_length =
5453                         cpu_to_be32(sizeof(struct ipr_supported_device));
5454
5455                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
5456                            IPR_SET_SUP_DEVICE_TIMEOUT);
5457
5458                 ipr_cmd->job_step = ipr_set_supported_devs;
5459                 return IPR_RC_JOB_RETURN;
5460         }
5461
5462         return IPR_RC_JOB_CONTINUE;
5463 }
5464
5465 /**
5466  * ipr_setup_write_cache - Disable write cache if needed
5467  * @ipr_cmd:    ipr command struct
5468  *
5469  * This function sets up adapters write cache to desired setting
5470  *
5471  * Return value:
5472  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5473  **/
5474 static int ipr_setup_write_cache(struct ipr_cmnd *ipr_cmd)
5475 {
5476         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5477
5478         ipr_cmd->job_step = ipr_set_supported_devs;
5479         ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
5480                                     struct ipr_resource_entry, queue);
5481
5482         if (ioa_cfg->cache_state != CACHE_DISABLED)
5483                 return IPR_RC_JOB_CONTINUE;
5484
5485         ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5486         ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5487         ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
5488         ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;
5489
5490         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5491
5492         return IPR_RC_JOB_RETURN;
5493 }
5494
5495 /**
5496  * ipr_get_mode_page - Locate specified mode page
5497  * @mode_pages: mode page buffer
5498  * @page_code:  page code to find
5499  * @len:                minimum required length for mode page
5500  *
5501  * Return value:
5502  *      pointer to mode page / NULL on failure
5503  **/
5504 static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
5505                                u32 page_code, u32 len)
5506 {
5507         struct ipr_mode_page_hdr *mode_hdr;
5508         u32 page_length;
5509         u32 length;
5510
5511         if (!mode_pages || (mode_pages->hdr.length == 0))
5512                 return NULL;
5513
5514         length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
5515         mode_hdr = (struct ipr_mode_page_hdr *)
5516                 (mode_pages->data + mode_pages->hdr.block_desc_len);
5517
5518         while (length) {
5519                 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
5520                         if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
5521                                 return mode_hdr;
5522                         break;
5523                 } else {
5524                         page_length = (sizeof(struct ipr_mode_page_hdr) +
5525                                        mode_hdr->page_length);
5526                         length -= page_length;
5527                         mode_hdr = (struct ipr_mode_page_hdr *)
5528                                 ((unsigned long)mode_hdr + page_length);
5529                 }
5530         }
5531         return NULL;
5532 }
5533
5534 /**
5535  * ipr_check_term_power - Check for term power errors
5536  * @ioa_cfg:    ioa config struct
5537  * @mode_pages: IOAFP mode pages buffer
5538  *
5539  * Check the IOAFP's mode page 28 for term power errors
5540  *
5541  * Return value:
5542  *      nothing
5543  **/
5544 static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
5545                                  struct ipr_mode_pages *mode_pages)
5546 {
5547         int i;
5548         int entry_length;
5549         struct ipr_dev_bus_entry *bus;
5550         struct ipr_mode_page28 *mode_page;
5551
5552         mode_page = ipr_get_mode_page(mode_pages, 0x28,
5553                                       sizeof(struct ipr_mode_page28));
5554
5555         entry_length = mode_page->entry_length;
5556
5557         bus = mode_page->bus;
5558
5559         for (i = 0; i < mode_page->num_entries; i++) {
5560                 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
5561                         dev_err(&ioa_cfg->pdev->dev,
5562                                 "Term power is absent on scsi bus %d\n",
5563                                 bus->res_addr.bus);
5564                 }
5565
5566                 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
5567         }
5568 }
5569
5570 /**
5571  * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
5572  * @ioa_cfg:    ioa config struct
5573  *
5574  * Looks through the config table checking for SES devices. If
5575  * the SES device is in the SES table indicating a maximum SCSI
5576  * bus speed, the speed is limited for the bus.
5577  *
5578  * Return value:
5579  *      none
5580  **/
5581 static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
5582 {
5583         u32 max_xfer_rate;
5584         int i;
5585
5586         for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
5587                 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
5588                                                        ioa_cfg->bus_attr[i].bus_width);
5589
5590                 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
5591                         ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
5592         }
5593 }
5594
5595 /**
5596  * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
5597  * @ioa_cfg:    ioa config struct
5598  * @mode_pages: mode page 28 buffer
5599  *
5600  * Updates mode page 28 based on driver configuration
5601  *
5602  * Return value:
5603  *      none
5604  **/
5605 static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
5606                                                 struct ipr_mode_pages *mode_pages)
5607 {
5608         int i, entry_length;
5609         struct ipr_dev_bus_entry *bus;
5610         struct ipr_bus_attributes *bus_attr;
5611         struct ipr_mode_page28 *mode_page;
5612
5613         mode_page = ipr_get_mode_page(mode_pages, 0x28,
5614                                       sizeof(struct ipr_mode_page28));
5615
5616         entry_length = mode_page->entry_length;
5617
5618         /* Loop for each device bus entry */
5619         for (i = 0, bus = mode_page->bus;
5620              i < mode_page->num_entries;
5621              i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
5622                 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
5623                         dev_err(&ioa_cfg->pdev->dev,
5624                                 "Invalid resource address reported: 0x%08X\n",
5625                                 IPR_GET_PHYS_LOC(bus->res_addr));
5626                         continue;
5627                 }
5628
5629                 bus_attr = &ioa_cfg->bus_attr[i];
5630                 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
5631                 bus->bus_width = bus_attr->bus_width;
5632                 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
5633                 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
5634                 if (bus_attr->qas_enabled)
5635                         bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
5636                 else
5637                         bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
5638         }
5639 }
5640
5641 /**
5642  * ipr_build_mode_select - Build a mode select command
5643  * @ipr_cmd:    ipr command struct
5644  * @res_handle: resource handle to send command to
5645  * @parm:               Byte 2 of Mode Sense command
5646  * @dma_addr:   DMA buffer address
5647  * @xfer_len:   data transfer length
5648  *
5649  * Return value:
5650  *      none
5651  **/
5652 static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
5653                                   __be32 res_handle, u8 parm, u32 dma_addr,
5654                                   u8 xfer_len)
5655 {
5656         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5657         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5658
5659         ioarcb->res_handle = res_handle;
5660         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5661         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5662         ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
5663         ioarcb->cmd_pkt.cdb[1] = parm;
5664         ioarcb->cmd_pkt.cdb[4] = xfer_len;
5665
5666         ioadl->flags_and_data_len =
5667                 cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST | xfer_len);
5668         ioadl->address = cpu_to_be32(dma_addr);
5669         ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5670         ioarcb->write_data_transfer_length = cpu_to_be32(xfer_len);
5671 }
5672
5673 /**
5674  * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
5675  * @ipr_cmd:    ipr command struct
5676  *
5677  * This function sets up the SCSI bus attributes and sends
5678  * a Mode Select for Page 28 to activate them.
5679  *
5680  * Return value:
5681  *      IPR_RC_JOB_RETURN
5682  **/
5683 static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
5684 {
5685         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5686         struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
5687         int length;
5688
5689         ENTER;
5690         ipr_scsi_bus_speed_limit(ioa_cfg);
5691         ipr_check_term_power(ioa_cfg, mode_pages);
5692         ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
5693         length = mode_pages->hdr.length + 1;
5694         mode_pages->hdr.length = 0;
5695
5696         ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
5697                               ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
5698                               length);
5699
5700         ipr_cmd->job_step = ipr_setup_write_cache;
5701         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5702
5703         LEAVE;
5704         return IPR_RC_JOB_RETURN;
5705 }
5706
5707 /**
5708  * ipr_build_mode_sense - Builds a mode sense command
5709  * @ipr_cmd:    ipr command struct
5710  * @res:                resource entry struct
5711  * @parm:               Byte 2 of mode sense command
5712  * @dma_addr:   DMA address of mode sense buffer
5713  * @xfer_len:   Size of DMA buffer
5714  *
5715  * Return value:
5716  *      none
5717  **/
5718 static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
5719                                  __be32 res_handle,
5720                                  u8 parm, u32 dma_addr, u8 xfer_len)
5721 {
5722         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5723         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5724
5725         ioarcb->res_handle = res_handle;
5726         ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
5727         ioarcb->cmd_pkt.cdb[2] = parm;
5728         ioarcb->cmd_pkt.cdb[4] = xfer_len;
5729         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5730
5731         ioadl->flags_and_data_len =
5732                 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
5733         ioadl->address = cpu_to_be32(dma_addr);
5734         ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5735         ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
5736 }
5737
5738 /**
5739  * ipr_reset_cmd_failed - Handle failure of IOA reset command
5740  * @ipr_cmd:    ipr command struct
5741  *
5742  * This function handles the failure of an IOA bringup command.
5743  *
5744  * Return value:
5745  *      IPR_RC_JOB_RETURN
5746  **/
5747 static int ipr_reset_cmd_failed(struct ipr_cmnd *ipr_cmd)
5748 {
5749         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5750         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5751
5752         dev_err(&ioa_cfg->pdev->dev,
5753                 "0x%02X failed with IOASC: 0x%08X\n",
5754                 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
5755
5756         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5757         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5758         return IPR_RC_JOB_RETURN;
5759 }
5760
5761 /**
5762  * ipr_reset_mode_sense_failed - Handle failure of IOAFP mode sense
5763  * @ipr_cmd:    ipr command struct
5764  *
5765  * This function handles the failure of a Mode Sense to the IOAFP.
5766  * Some adapters do not handle all mode pages.
5767  *
5768  * Return value:
5769  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5770  **/
5771 static int ipr_reset_mode_sense_failed(struct ipr_cmnd *ipr_cmd)
5772 {
5773         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5774
5775         if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
5776                 ipr_cmd->job_step = ipr_setup_write_cache;
5777                 return IPR_RC_JOB_CONTINUE;
5778         }
5779
5780         return ipr_reset_cmd_failed(ipr_cmd);
5781 }
5782
5783 /**
5784  * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
5785  * @ipr_cmd:    ipr command struct
5786  *
5787  * This function send a Page 28 mode sense to the IOA to
5788  * retrieve SCSI bus attributes.
5789  *
5790  * Return value:
5791  *      IPR_RC_JOB_RETURN
5792  **/
5793 static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
5794 {
5795         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5796
5797         ENTER;
5798         ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
5799                              0x28, ioa_cfg->vpd_cbs_dma +
5800                              offsetof(struct ipr_misc_cbs, mode_pages),
5801                              sizeof(struct ipr_mode_pages));
5802
5803         ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
5804         ipr_cmd->job_step_failed = ipr_reset_mode_sense_failed;
5805
5806         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5807
5808         LEAVE;
5809         return IPR_RC_JOB_RETURN;
5810 }
5811
5812 /**
5813  * ipr_ioafp_mode_select_page24 - Issue Mode Select to IOA
5814  * @ipr_cmd:    ipr command struct
5815  *
5816  * This function enables dual IOA RAID support if possible.
5817  *
5818  * Return value:
5819  *      IPR_RC_JOB_RETURN
5820  **/
5821 static int ipr_ioafp_mode_select_page24(struct ipr_cmnd *ipr_cmd)
5822 {
5823         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5824         struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
5825         struct ipr_mode_page24 *mode_page;
5826         int length;
5827
5828         ENTER;
5829         mode_page = ipr_get_mode_page(mode_pages, 0x24,
5830                                       sizeof(struct ipr_mode_page24));
5831
5832         if (mode_page)
5833                 mode_page->flags |= IPR_ENABLE_DUAL_IOA_AF;
5834
5835         length = mode_pages->hdr.length + 1;
5836         mode_pages->hdr.length = 0;
5837
5838         ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
5839                               ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
5840                               length);
5841
5842         ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
5843         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5844
5845         LEAVE;
5846         return IPR_RC_JOB_RETURN;
5847 }
5848
5849 /**
5850  * ipr_reset_mode_sense_page24_failed - Handle failure of IOAFP mode sense
5851  * @ipr_cmd:    ipr command struct
5852  *
5853  * This function handles the failure of a Mode Sense to the IOAFP.
5854  * Some adapters do not handle all mode pages.
5855  *
5856  * Return value:
5857  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5858  **/
5859 static int ipr_reset_mode_sense_page24_failed(struct ipr_cmnd *ipr_cmd)
5860 {
5861         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5862
5863         if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
5864                 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
5865                 return IPR_RC_JOB_CONTINUE;
5866         }
5867
5868         return ipr_reset_cmd_failed(ipr_cmd);
5869 }
5870
5871 /**
5872  * ipr_ioafp_mode_sense_page24 - Issue Page 24 Mode Sense to IOA
5873  * @ipr_cmd:    ipr command struct
5874  *
5875  * This function send a mode sense to the IOA to retrieve
5876  * the IOA Advanced Function Control mode page.
5877  *
5878  * Return value:
5879  *      IPR_RC_JOB_RETURN
5880  **/
5881 static int ipr_ioafp_mode_sense_page24(struct ipr_cmnd *ipr_cmd)
5882 {
5883         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5884
5885         ENTER;
5886         ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
5887                              0x24, ioa_cfg->vpd_cbs_dma +
5888                              offsetof(struct ipr_misc_cbs, mode_pages),
5889                              sizeof(struct ipr_mode_pages));
5890
5891         ipr_cmd->job_step = ipr_ioafp_mode_select_page24;
5892         ipr_cmd->job_step_failed = ipr_reset_mode_sense_page24_failed;
5893
5894         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5895
5896         LEAVE;
5897         return IPR_RC_JOB_RETURN;
5898 }
5899
5900 /**
5901  * ipr_init_res_table - Initialize the resource table
5902  * @ipr_cmd:    ipr command struct
5903  *
5904  * This function looks through the existing resource table, comparing
5905  * it with the config table. This function will take care of old/new
5906  * devices and schedule adding/removing them from the mid-layer
5907  * as appropriate.
5908  *
5909  * Return value:
5910  *      IPR_RC_JOB_CONTINUE
5911  **/
5912 static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
5913 {
5914         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5915         struct ipr_resource_entry *res, *temp;
5916         struct ipr_config_table_entry *cfgte;
5917         int found, i;
5918         LIST_HEAD(old_res);
5919
5920         ENTER;
5921         if (ioa_cfg->cfg_table->hdr.flags & IPR_UCODE_DOWNLOAD_REQ)
5922                 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
5923
5924         list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
5925                 list_move_tail(&res->queue, &old_res);
5926
5927         for (i = 0; i < ioa_cfg->cfg_table->hdr.num_entries; i++) {
5928                 cfgte = &ioa_cfg->cfg_table->dev[i];
5929                 found = 0;
5930
5931                 list_for_each_entry_safe(res, temp, &old_res, queue) {
5932                         if (!memcmp(&res->cfgte.res_addr,
5933                                     &cfgte->res_addr, sizeof(cfgte->res_addr))) {
5934                                 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
5935                                 found = 1;
5936                                 break;
5937                         }
5938                 }
5939
5940                 if (!found) {
5941                         if (list_empty(&ioa_cfg->free_res_q)) {
5942                                 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
5943                                 break;
5944                         }
5945
5946                         found = 1;
5947                         res = list_entry(ioa_cfg->free_res_q.next,
5948                                          struct ipr_resource_entry, queue);
5949                         list_move_tail(&res->queue, &ioa_cfg->used_res_q);
5950                         ipr_init_res_entry(res);
5951                         res->add_to_ml = 1;
5952                 }
5953
5954                 if (found)
5955                         memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
5956         }
5957
5958         list_for_each_entry_safe(res, temp, &old_res, queue) {
5959                 if (res->sdev) {
5960                         res->del_from_ml = 1;
5961                         res->cfgte.res_handle = IPR_INVALID_RES_HANDLE;
5962                         list_move_tail(&res->queue, &ioa_cfg->used_res_q);
5963                 } else {
5964                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
5965                 }
5966         }
5967
5968         if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
5969                 ipr_cmd->job_step = ipr_ioafp_mode_sense_page24;
5970         else
5971                 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
5972
5973         LEAVE;
5974         return IPR_RC_JOB_CONTINUE;
5975 }
5976
5977 /**
5978  * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
5979  * @ipr_cmd:    ipr command struct
5980  *
5981  * This function sends a Query IOA Configuration command
5982  * to the adapter to retrieve the IOA configuration table.
5983  *
5984  * Return value:
5985  *      IPR_RC_JOB_RETURN
5986  **/
5987 static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
5988 {
5989         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5990         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5991         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5992         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
5993         struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
5994
5995         ENTER;
5996         if (cap->cap & IPR_CAP_DUAL_IOA_RAID)
5997                 ioa_cfg->dual_raid = 1;
5998         dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
5999                  ucode_vpd->major_release, ucode_vpd->card_type,
6000                  ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
6001         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6002         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6003
6004         ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
6005         ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_config_table) >> 8) & 0xff;
6006         ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_config_table) & 0xff;
6007
6008         ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
6009         ioarcb->read_data_transfer_length =
6010                 cpu_to_be32(sizeof(struct ipr_config_table));
6011
6012         ioadl->address = cpu_to_be32(ioa_cfg->cfg_table_dma);
6013         ioadl->flags_and_data_len =
6014                 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(struct ipr_config_table));
6015
6016         ipr_cmd->job_step = ipr_init_res_table;
6017
6018         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6019
6020         LEAVE;
6021         return IPR_RC_JOB_RETURN;
6022 }
6023
6024 /**
6025  * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
6026  * @ipr_cmd:    ipr command struct
6027  *
6028  * This utility function sends an inquiry to the adapter.
6029  *
6030  * Return value:
6031  *      none
6032  **/
6033 static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
6034                               u32 dma_addr, u8 xfer_len)
6035 {
6036         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6037         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
6038
6039         ENTER;
6040         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6041         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6042
6043         ioarcb->cmd_pkt.cdb[0] = INQUIRY;
6044         ioarcb->cmd_pkt.cdb[1] = flags;
6045         ioarcb->cmd_pkt.cdb[2] = page;
6046         ioarcb->cmd_pkt.cdb[4] = xfer_len;
6047
6048         ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
6049         ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
6050
6051         ioadl->address = cpu_to_be32(dma_addr);
6052         ioadl->flags_and_data_len =
6053                 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
6054
6055         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6056         LEAVE;
6057 }
6058
6059 /**
6060  * ipr_inquiry_page_supported - Is the given inquiry page supported
6061  * @page0:              inquiry page 0 buffer
6062  * @page:               page code.
6063  *
6064  * This function determines if the specified inquiry page is supported.
6065  *
6066  * Return value:
6067  *      1 if page is supported / 0 if not
6068  **/
6069 static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
6070 {
6071         int i;
6072
6073         for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
6074                 if (page0->page[i] == page)
6075                         return 1;
6076
6077         return 0;
6078 }
6079
6080 /**
6081  * ipr_ioafp_cap_inquiry - Send a Page 0xD0 Inquiry to the adapter.
6082  * @ipr_cmd:    ipr command struct
6083  *
6084  * This function sends a Page 0xD0 inquiry to the adapter
6085  * to retrieve adapter capabilities.
6086  *
6087  * Return value:
6088  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6089  **/
6090 static int ipr_ioafp_cap_inquiry(struct ipr_cmnd *ipr_cmd)
6091 {
6092         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6093         struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
6094         struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
6095
6096         ENTER;
6097         ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
6098         memset(cap, 0, sizeof(*cap));
6099
6100         if (ipr_inquiry_page_supported(page0, 0xD0)) {
6101                 ipr_ioafp_inquiry(ipr_cmd, 1, 0xD0,
6102                                   ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, cap),
6103                                   sizeof(struct ipr_inquiry_cap));
6104                 return IPR_RC_JOB_RETURN;
6105         }
6106
6107         LEAVE;
6108         return IPR_RC_JOB_CONTINUE;
6109 }
6110
6111 /**
6112  * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
6113  * @ipr_cmd:    ipr command struct
6114  *
6115  * This function sends a Page 3 inquiry to the adapter
6116  * to retrieve software VPD information.
6117  *
6118  * Return value:
6119  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6120  **/
6121 static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
6122 {
6123         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6124         struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
6125
6126         ENTER;
6127
6128         if (!ipr_inquiry_page_supported(page0, 1))
6129                 ioa_cfg->cache_state = CACHE_NONE;
6130
6131         ipr_cmd->job_step = ipr_ioafp_cap_inquiry;
6132
6133         ipr_ioafp_inquiry(ipr_cmd, 1, 3,
6134                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
6135                           sizeof(struct ipr_inquiry_page3));
6136
6137         LEAVE;
6138         return IPR_RC_JOB_RETURN;
6139 }
6140
6141 /**
6142  * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
6143  * @ipr_cmd:    ipr command struct
6144  *
6145  * This function sends a Page 0 inquiry to the adapter
6146  * to retrieve supported inquiry pages.
6147  *
6148  * Return value:
6149  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6150  **/
6151 static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
6152 {
6153         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6154         char type[5];
6155
6156         ENTER;
6157
6158         /* Grab the type out of the VPD and store it away */
6159         memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
6160         type[4] = '\0';
6161         ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
6162
6163         ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
6164
6165         ipr_ioafp_inquiry(ipr_cmd, 1, 0,
6166                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
6167                           sizeof(struct ipr_inquiry_page0));
6168
6169         LEAVE;
6170         return IPR_RC_JOB_RETURN;
6171 }
6172
6173 /**
6174  * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
6175  * @ipr_cmd:    ipr command struct
6176  *
6177  * This function sends a standard inquiry to the adapter.
6178  *
6179  * Return value:
6180  *      IPR_RC_JOB_RETURN
6181  **/
6182 static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
6183 {
6184         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6185
6186         ENTER;
6187         ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
6188
6189         ipr_ioafp_inquiry(ipr_cmd, 0, 0,
6190                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
6191                           sizeof(struct ipr_ioa_vpd));
6192
6193         LEAVE;
6194         return IPR_RC_JOB_RETURN;
6195 }
6196
6197 /**
6198  * ipr_ioafp_indentify_hrrq - Send Identify Host RRQ.
6199  * @ipr_cmd:    ipr command struct
6200  *
6201  * This function send an Identify Host Request Response Queue
6202  * command to establish the HRRQ with the adapter.
6203  *
6204  * Return value:
6205  *      IPR_RC_JOB_RETURN
6206  **/
6207 static int ipr_ioafp_indentify_hrrq(struct ipr_cmnd *ipr_cmd)
6208 {
6209         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6210         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6211
6212         ENTER;
6213         dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
6214
6215         ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
6216         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6217
6218         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6219         ioarcb->cmd_pkt.cdb[2] =
6220                 ((u32) ioa_cfg->host_rrq_dma >> 24) & 0xff;
6221         ioarcb->cmd_pkt.cdb[3] =
6222                 ((u32) ioa_cfg->host_rrq_dma >> 16) & 0xff;
6223         ioarcb->cmd_pkt.cdb[4] =
6224                 ((u32) ioa_cfg->host_rrq_dma >> 8) & 0xff;
6225         ioarcb->cmd_pkt.cdb[5] =
6226                 ((u32) ioa_cfg->host_rrq_dma) & 0xff;
6227         ioarcb->cmd_pkt.cdb[7] =
6228                 ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
6229         ioarcb->cmd_pkt.cdb[8] =
6230                 (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
6231
6232         ipr_cmd->job_step = ipr_ioafp_std_inquiry;
6233
6234         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6235
6236         LEAVE;
6237         return IPR_RC_JOB_RETURN;
6238 }
6239
6240 /**
6241  * ipr_reset_timer_done - Adapter reset timer function
6242  * @ipr_cmd:    ipr command struct
6243  *
6244  * Description: This function is used in adapter reset processing
6245  * for timing events. If the reset_cmd pointer in the IOA
6246  * config struct is not this adapter's we are doing nested
6247  * resets and fail_all_ops will take care of freeing the
6248  * command block.
6249  *
6250  * Return value:
6251  *      none
6252  **/
6253 static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
6254 {
6255         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6256         unsigned long lock_flags = 0;
6257
6258         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
6259
6260         if (ioa_cfg->reset_cmd == ipr_cmd) {
6261                 list_del(&ipr_cmd->queue);
6262                 ipr_cmd->done(ipr_cmd);
6263         }
6264
6265         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
6266 }
6267
6268 /**
6269  * ipr_reset_start_timer - Start a timer for adapter reset job
6270  * @ipr_cmd:    ipr command struct
6271  * @timeout:    timeout value
6272  *
6273  * Description: This function is used in adapter reset processing
6274  * for timing events. If the reset_cmd pointer in the IOA
6275  * config struct is not this adapter's we are doing nested
6276  * resets and fail_all_ops will take care of freeing the
6277  * command block.
6278  *
6279  * Return value:
6280  *      none
6281  **/
6282 static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
6283                                   unsigned long timeout)
6284 {
6285         list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
6286         ipr_cmd->done = ipr_reset_ioa_job;
6287
6288         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
6289         ipr_cmd->timer.expires = jiffies + timeout;
6290         ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
6291         add_timer(&ipr_cmd->timer);
6292 }
6293
6294 /**
6295  * ipr_init_ioa_mem - Initialize ioa_cfg control block
6296  * @ioa_cfg:    ioa cfg struct
6297  *
6298  * Return value:
6299  *      nothing
6300  **/
6301 static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
6302 {
6303         memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
6304
6305         /* Initialize Host RRQ pointers */
6306         ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
6307         ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
6308         ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
6309         ioa_cfg->toggle_bit = 1;
6310
6311         /* Zero out config table */
6312         memset(ioa_cfg->cfg_table, 0, sizeof(struct ipr_config_table));
6313 }
6314
6315 /**
6316  * ipr_reset_enable_ioa - Enable the IOA following a reset.
6317  * @ipr_cmd:    ipr command struct
6318  *
6319  * This function reinitializes some control blocks and
6320  * enables destructive diagnostics on the adapter.
6321  *
6322  * Return value:
6323  *      IPR_RC_JOB_RETURN
6324  **/
6325 static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
6326 {
6327         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6328         volatile u32 int_reg;
6329
6330         ENTER;
6331         ipr_cmd->job_step = ipr_ioafp_indentify_hrrq;
6332         ipr_init_ioa_mem(ioa_cfg);
6333
6334         ioa_cfg->allow_interrupts = 1;
6335         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
6336
6337         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
6338                 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
6339                        ioa_cfg->regs.clr_interrupt_mask_reg);
6340                 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
6341                 return IPR_RC_JOB_CONTINUE;
6342         }
6343
6344         /* Enable destructive diagnostics on IOA */
6345         writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg);
6346
6347         writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg);
6348         int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
6349
6350         dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
6351
6352         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
6353         ipr_cmd->timer.expires = jiffies + (ioa_cfg->transop_timeout * HZ);
6354         ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
6355         ipr_cmd->done = ipr_reset_ioa_job;
6356         add_timer(&ipr_cmd->timer);
6357         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
6358
6359         LEAVE;
6360         return IPR_RC_JOB_RETURN;
6361 }
6362
6363 /**
6364  * ipr_reset_wait_for_dump - Wait for a dump to timeout.
6365  * @ipr_cmd:    ipr command struct
6366  *
6367  * This function is invoked when an adapter dump has run out
6368  * of processing time.
6369  *
6370  * Return value:
6371  *      IPR_RC_JOB_CONTINUE
6372  **/
6373 static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
6374 {
6375         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6376
6377         if (ioa_cfg->sdt_state == GET_DUMP)
6378                 ioa_cfg->sdt_state = ABORT_DUMP;
6379
6380         ipr_cmd->job_step = ipr_reset_alert;
6381
6382         return IPR_RC_JOB_CONTINUE;
6383 }
6384
6385 /**
6386  * ipr_unit_check_no_data - Log a unit check/no data error log
6387  * @ioa_cfg:            ioa config struct
6388  *
6389  * Logs an error indicating the adapter unit checked, but for some
6390  * reason, we were unable to fetch the unit check buffer.
6391  *
6392  * Return value:
6393  *      nothing
6394  **/
6395 static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
6396 {
6397         ioa_cfg->errors_logged++;
6398         dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
6399 }
6400
6401 /**
6402  * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
6403  * @ioa_cfg:            ioa config struct
6404  *
6405  * Fetches the unit check buffer from the adapter by clocking the data
6406  * through the mailbox register.
6407  *
6408  * Return value:
6409  *      nothing
6410  **/
6411 static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
6412 {
6413         unsigned long mailbox;
6414         struct ipr_hostrcb *hostrcb;
6415         struct ipr_uc_sdt sdt;
6416         int rc, length;
6417         u32 ioasc;
6418
6419         mailbox = readl(ioa_cfg->ioa_mailbox);
6420
6421         if (!ipr_sdt_is_fmt2(mailbox)) {
6422                 ipr_unit_check_no_data(ioa_cfg);
6423                 return;
6424         }
6425
6426         memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
6427         rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
6428                                         (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
6429
6430         if (rc || (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE) ||
6431             !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY)) {
6432                 ipr_unit_check_no_data(ioa_cfg);
6433                 return;
6434         }
6435
6436         /* Find length of the first sdt entry (UC buffer) */
6437         length = (be32_to_cpu(sdt.entry[0].end_offset) -
6438                   be32_to_cpu(sdt.entry[0].bar_str_offset)) & IPR_FMT2_MBX_ADDR_MASK;
6439
6440         hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
6441                              struct ipr_hostrcb, queue);
6442         list_del(&hostrcb->queue);
6443         memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
6444
6445         rc = ipr_get_ldump_data_section(ioa_cfg,
6446                                         be32_to_cpu(sdt.entry[0].bar_str_offset),
6447                                         (__be32 *)&hostrcb->hcam,
6448                                         min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
6449
6450         if (!rc) {
6451                 ipr_handle_log_data(ioa_cfg, hostrcb);
6452                 ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
6453                 if (ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED &&
6454                     ioa_cfg->sdt_state == GET_DUMP)
6455                         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
6456         } else
6457                 ipr_unit_check_no_data(ioa_cfg);
6458
6459         list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
6460 }
6461
6462 /**
6463  * ipr_reset_restore_cfg_space - Restore PCI config space.
6464  * @ipr_cmd:    ipr command struct
6465  *
6466  * Description: This function restores the saved PCI config space of
6467  * the adapter, fails all outstanding ops back to the callers, and
6468  * fetches the dump/unit check if applicable to this reset.
6469  *
6470  * Return value:
6471  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6472  **/
6473 static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
6474 {
6475         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6476         int rc;
6477
6478         ENTER;
6479         rc = pci_restore_state(ioa_cfg->pdev);
6480
6481         if (rc != PCIBIOS_SUCCESSFUL) {
6482                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
6483                 return IPR_RC_JOB_CONTINUE;
6484         }
6485
6486         if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
6487                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
6488                 return IPR_RC_JOB_CONTINUE;
6489         }
6490
6491         ipr_fail_all_ops(ioa_cfg);
6492
6493         if (ioa_cfg->ioa_unit_checked) {
6494                 ioa_cfg->ioa_unit_checked = 0;
6495                 ipr_get_unit_check_buffer(ioa_cfg);
6496                 ipr_cmd->job_step = ipr_reset_alert;
6497                 ipr_reset_start_timer(ipr_cmd, 0);
6498                 return IPR_RC_JOB_RETURN;
6499         }
6500
6501         if (ioa_cfg->in_ioa_bringdown) {
6502                 ipr_cmd->job_step = ipr_ioa_bringdown_done;
6503         } else {
6504                 ipr_cmd->job_step = ipr_reset_enable_ioa;
6505
6506                 if (GET_DUMP == ioa_cfg->sdt_state) {
6507                         ipr_reset_start_timer(ipr_cmd, IPR_DUMP_TIMEOUT);
6508                         ipr_cmd->job_step = ipr_reset_wait_for_dump;
6509                         schedule_work(&ioa_cfg->work_q);
6510                         return IPR_RC_JOB_RETURN;
6511                 }
6512         }
6513
6514         ENTER;
6515         return IPR_RC_JOB_CONTINUE;
6516 }
6517
6518 /**
6519  * ipr_reset_bist_done - BIST has completed on the adapter.
6520  * @ipr_cmd:    ipr command struct
6521  *
6522  * Description: Unblock config space and resume the reset process.
6523  *
6524  * Return value:
6525  *      IPR_RC_JOB_CONTINUE
6526  **/
6527 static int ipr_reset_bist_done(struct ipr_cmnd *ipr_cmd)
6528 {
6529         ENTER;
6530         pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
6531         ipr_cmd->job_step = ipr_reset_restore_cfg_space;
6532         LEAVE;
6533         return IPR_RC_JOB_CONTINUE;
6534 }
6535
6536 /**
6537  * ipr_reset_start_bist - Run BIST on the adapter.
6538  * @ipr_cmd:    ipr command struct
6539  *
6540  * Description: This function runs BIST on the adapter, then delays 2 seconds.
6541  *
6542  * Return value:
6543  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6544  **/
6545 static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
6546 {
6547         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6548         int rc;
6549
6550         ENTER;
6551         pci_block_user_cfg_access(ioa_cfg->pdev);
6552         rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
6553
6554         if (rc != PCIBIOS_SUCCESSFUL) {
6555                 pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
6556                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
6557                 rc = IPR_RC_JOB_CONTINUE;
6558         } else {
6559                 ipr_cmd->job_step = ipr_reset_bist_done;
6560                 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
6561                 rc = IPR_RC_JOB_RETURN;
6562         }
6563
6564         LEAVE;
6565         return rc;
6566 }
6567
6568 /**
6569  * ipr_reset_slot_reset_done - Clear PCI reset to the adapter
6570  * @ipr_cmd:    ipr command struct
6571  *
6572  * Description: This clears PCI reset to the adapter and delays two seconds.
6573  *
6574  * Return value:
6575  *      IPR_RC_JOB_RETURN
6576  **/
6577 static int ipr_reset_slot_reset_done(struct ipr_cmnd *ipr_cmd)
6578 {
6579         ENTER;
6580         pci_set_pcie_reset_state(ipr_cmd->ioa_cfg->pdev, pcie_deassert_reset);
6581         ipr_cmd->job_step = ipr_reset_bist_done;
6582         ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
6583         LEAVE;
6584         return IPR_RC_JOB_RETURN;
6585 }
6586
6587 /**
6588  * ipr_reset_slot_reset - Reset the PCI slot of the adapter.
6589  * @ipr_cmd:    ipr command struct
6590  *
6591  * Description: This asserts PCI reset to the adapter.
6592  *
6593  * Return value:
6594  *      IPR_RC_JOB_RETURN
6595  **/
6596 static int ipr_reset_slot_reset(struct ipr_cmnd *ipr_cmd)
6597 {
6598         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6599         struct pci_dev *pdev = ioa_cfg->pdev;
6600
6601         ENTER;
6602         pci_block_user_cfg_access(pdev);
6603         pci_set_pcie_reset_state(pdev, pcie_warm_reset);
6604         ipr_cmd->job_step = ipr_reset_slot_reset_done;
6605         ipr_reset_start_timer(ipr_cmd, IPR_PCI_RESET_TIMEOUT);
6606         LEAVE;
6607         return IPR_RC_JOB_RETURN;
6608 }
6609
6610 /**
6611  * ipr_reset_allowed - Query whether or not IOA can be reset
6612  * @ioa_cfg:    ioa config struct
6613  *
6614  * Return value:
6615  *      0 if reset not allowed / non-zero if reset is allowed
6616  **/
6617 static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
6618 {
6619         volatile u32 temp_reg;
6620
6621         temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
6622         return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
6623 }
6624
6625 /**
6626  * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
6627  * @ipr_cmd:    ipr command struct
6628  *
6629  * Description: This function waits for adapter permission to run BIST,
6630  * then runs BIST. If the adapter does not give permission after a
6631  * reasonable time, we will reset the adapter anyway. The impact of
6632  * resetting the adapter without warning the adapter is the risk of
6633  * losing the persistent error log on the adapter. If the adapter is
6634  * reset while it is writing to the flash on the adapter, the flash
6635  * segment will have bad ECC and be zeroed.
6636  *
6637  * Return value:
6638  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6639  **/
6640 static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
6641 {
6642         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6643         int rc = IPR_RC_JOB_RETURN;
6644
6645         if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
6646                 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
6647                 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
6648         } else {
6649                 ipr_cmd->job_step = ioa_cfg->reset;
6650                 rc = IPR_RC_JOB_CONTINUE;
6651         }
6652
6653         return rc;
6654 }
6655
6656 /**
6657  * ipr_reset_alert_part2 - Alert the adapter of a pending reset
6658  * @ipr_cmd:    ipr command struct
6659  *
6660  * Description: This function alerts the adapter that it will be reset.
6661  * If memory space is not currently enabled, proceed directly
6662  * to running BIST on the adapter. The timer must always be started
6663  * so we guarantee we do not run BIST from ipr_isr.
6664  *
6665  * Return value:
6666  *      IPR_RC_JOB_RETURN
6667  **/
6668 static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
6669 {
6670         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6671         u16 cmd_reg;
6672         int rc;
6673
6674         ENTER;
6675         rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
6676
6677         if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
6678                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
6679                 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg);
6680                 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
6681         } else {
6682                 ipr_cmd->job_step = ioa_cfg->reset;
6683         }
6684
6685         ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
6686         ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
6687
6688         LEAVE;
6689         return IPR_RC_JOB_RETURN;
6690 }
6691
6692 /**
6693  * ipr_reset_ucode_download_done - Microcode download completion
6694  * @ipr_cmd:    ipr command struct
6695  *
6696  * Description: This function unmaps the microcode download buffer.
6697  *
6698  * Return value:
6699  *      IPR_RC_JOB_CONTINUE
6700  **/
6701 static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
6702 {
6703         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6704         struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
6705
6706         pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
6707                      sglist->num_sg, DMA_TO_DEVICE);
6708
6709         ipr_cmd->job_step = ipr_reset_alert;
6710         return IPR_RC_JOB_CONTINUE;
6711 }
6712
6713 /**
6714  * ipr_reset_ucode_download - Download microcode to the adapter
6715  * @ipr_cmd:    ipr command struct
6716  *
6717  * Description: This function checks to see if it there is microcode
6718  * to download to the adapter. If there is, a download is performed.
6719  *
6720  * Return value:
6721  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6722  **/
6723 static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
6724 {
6725         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6726         struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
6727
6728         ENTER;
6729         ipr_cmd->job_step = ipr_reset_alert;
6730
6731         if (!sglist)
6732                 return IPR_RC_JOB_CONTINUE;
6733
6734         ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6735         ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6736         ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
6737         ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
6738         ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
6739         ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
6740         ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
6741
6742         ipr_build_ucode_ioadl(ipr_cmd, sglist);
6743         ipr_cmd->job_step = ipr_reset_ucode_download_done;
6744
6745         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
6746                    IPR_WRITE_BUFFER_TIMEOUT);
6747
6748         LEAVE;
6749         return IPR_RC_JOB_RETURN;
6750 }
6751
6752 /**
6753  * ipr_reset_shutdown_ioa - Shutdown the adapter
6754  * @ipr_cmd:    ipr command struct
6755  *
6756  * Description: This function issues an adapter shutdown of the
6757  * specified type to the specified adapter as part of the
6758  * adapter reset job.
6759  *
6760  * Return value:
6761  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6762  **/
6763 static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
6764 {
6765         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6766         enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
6767         unsigned long timeout;
6768         int rc = IPR_RC_JOB_CONTINUE;
6769
6770         ENTER;
6771         if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
6772                 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6773                 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6774                 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
6775                 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
6776
6777                 if (shutdown_type == IPR_SHUTDOWN_NORMAL)
6778                         timeout = IPR_SHUTDOWN_TIMEOUT;
6779                 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
6780                         timeout = IPR_INTERNAL_TIMEOUT;
6781                 else if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
6782                         timeout = IPR_DUAL_IOA_ABBR_SHUTDOWN_TO;
6783                 else
6784                         timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
6785
6786                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
6787
6788                 rc = IPR_RC_JOB_RETURN;
6789                 ipr_cmd->job_step = ipr_reset_ucode_download;
6790         } else
6791                 ipr_cmd->job_step = ipr_reset_alert;
6792
6793         LEAVE;
6794         return rc;
6795 }
6796
6797 /**
6798  * ipr_reset_ioa_job - Adapter reset job
6799  * @ipr_cmd:    ipr command struct
6800  *
6801  * Description: This function is the job router for the adapter reset job.
6802  *
6803  * Return value:
6804  *      none
6805  **/
6806 static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
6807 {
6808         u32 rc, ioasc;
6809         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6810
6811         do {
6812                 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
6813
6814                 if (ioa_cfg->reset_cmd != ipr_cmd) {
6815                         /*
6816                          * We are doing nested adapter resets and this is
6817                          * not the current reset job.
6818                          */
6819                         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6820                         return;
6821                 }
6822
6823                 if (IPR_IOASC_SENSE_KEY(ioasc)) {
6824                         rc = ipr_cmd->job_step_failed(ipr_cmd);
6825                         if (rc == IPR_RC_JOB_RETURN)
6826                                 return;
6827                 }
6828
6829                 ipr_reinit_ipr_cmnd(ipr_cmd);
6830                 ipr_cmd->job_step_failed = ipr_reset_cmd_failed;
6831                 rc = ipr_cmd->job_step(ipr_cmd);
6832         } while(rc == IPR_RC_JOB_CONTINUE);
6833 }
6834
6835 /**
6836  * _ipr_initiate_ioa_reset - Initiate an adapter reset
6837  * @ioa_cfg:            ioa config struct
6838  * @job_step:           first job step of reset job
6839  * @shutdown_type:      shutdown type
6840  *
6841  * Description: This function will initiate the reset of the given adapter
6842  * starting at the selected job step.
6843  * If the caller needs to wait on the completion of the reset,
6844  * the caller must sleep on the reset_wait_q.
6845  *
6846  * Return value:
6847  *      none
6848  **/
6849 static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
6850                                     int (*job_step) (struct ipr_cmnd *),
6851                                     enum ipr_shutdown_type shutdown_type)
6852 {
6853         struct ipr_cmnd *ipr_cmd;
6854
6855         ioa_cfg->in_reset_reload = 1;
6856         ioa_cfg->allow_cmds = 0;
6857         scsi_block_requests(ioa_cfg->host);
6858
6859         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
6860         ioa_cfg->reset_cmd = ipr_cmd;
6861         ipr_cmd->job_step = job_step;
6862         ipr_cmd->u.shutdown_type = shutdown_type;
6863
6864         ipr_reset_ioa_job(ipr_cmd);
6865 }
6866
6867 /**
6868  * ipr_initiate_ioa_reset - Initiate an adapter reset
6869  * @ioa_cfg:            ioa config struct
6870  * @shutdown_type:      shutdown type
6871  *
6872  * Description: This function will initiate the reset of the given adapter.
6873  * If the caller needs to wait on the completion of the reset,
6874  * the caller must sleep on the reset_wait_q.
6875  *
6876  * Return value:
6877  *      none
6878  **/
6879 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
6880                                    enum ipr_shutdown_type shutdown_type)
6881 {
6882         if (ioa_cfg->ioa_is_dead)
6883                 return;
6884
6885         if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
6886                 ioa_cfg->sdt_state = ABORT_DUMP;
6887
6888         if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
6889                 dev_err(&ioa_cfg->pdev->dev,
6890                         "IOA taken offline - error recovery failed\n");
6891
6892                 ioa_cfg->reset_retries = 0;
6893                 ioa_cfg->ioa_is_dead = 1;
6894
6895                 if (ioa_cfg->in_ioa_bringdown) {
6896                         ioa_cfg->reset_cmd = NULL;
6897                         ioa_cfg->in_reset_reload = 0;
6898                         ipr_fail_all_ops(ioa_cfg);
6899                         wake_up_all(&ioa_cfg->reset_wait_q);
6900
6901                         spin_unlock_irq(ioa_cfg->host->host_lock);
6902                         scsi_unblock_requests(ioa_cfg->host);
6903                         spin_lock_irq(ioa_cfg->host->host_lock);
6904                         return;
6905                 } else {
6906                         ioa_cfg->in_ioa_bringdown = 1;
6907                         shutdown_type = IPR_SHUTDOWN_NONE;
6908                 }
6909         }
6910
6911         _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
6912                                 shutdown_type);
6913 }
6914
6915 /**
6916  * ipr_reset_freeze - Hold off all I/O activity
6917  * @ipr_cmd:    ipr command struct
6918  *
6919  * Description: If the PCI slot is frozen, hold off all I/O
6920  * activity; then, as soon as the slot is available again,
6921  * initiate an adapter reset.
6922  */
6923 static int ipr_reset_freeze(struct ipr_cmnd *ipr_cmd)
6924 {
6925         /* Disallow new interrupts, avoid loop */
6926         ipr_cmd->ioa_cfg->allow_interrupts = 0;
6927         list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
6928         ipr_cmd->done = ipr_reset_ioa_job;
6929         return IPR_RC_JOB_RETURN;
6930 }
6931
6932 /**
6933  * ipr_pci_frozen - Called when slot has experienced a PCI bus error.
6934  * @pdev:       PCI device struct
6935  *
6936  * Description: This routine is called to tell us that the PCI bus
6937  * is down. Can't do anything here, except put the device driver
6938  * into a holding pattern, waiting for the PCI bus to come back.
6939  */
6940 static void ipr_pci_frozen(struct pci_dev *pdev)
6941 {
6942         unsigned long flags = 0;
6943         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6944
6945         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6946         _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_freeze, IPR_SHUTDOWN_NONE);
6947         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6948 }
6949
6950 /**
6951  * ipr_pci_slot_reset - Called when PCI slot has been reset.
6952  * @pdev:       PCI device struct
6953  *
6954  * Description: This routine is called by the pci error recovery
6955  * code after the PCI slot has been reset, just before we
6956  * should resume normal operations.
6957  */
6958 static pci_ers_result_t ipr_pci_slot_reset(struct pci_dev *pdev)
6959 {
6960         unsigned long flags = 0;
6961         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6962
6963         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6964         if (ioa_cfg->needs_warm_reset)
6965                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
6966         else
6967                 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_restore_cfg_space,
6968                                         IPR_SHUTDOWN_NONE);
6969         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6970         return PCI_ERS_RESULT_RECOVERED;
6971 }
6972
6973 /**
6974  * ipr_pci_perm_failure - Called when PCI slot is dead for good.
6975  * @pdev:       PCI device struct
6976  *
6977  * Description: This routine is called when the PCI bus has
6978  * permanently failed.
6979  */
6980 static void ipr_pci_perm_failure(struct pci_dev *pdev)
6981 {
6982         unsigned long flags = 0;
6983         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6984
6985         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6986         if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
6987                 ioa_cfg->sdt_state = ABORT_DUMP;
6988         ioa_cfg->reset_retries = IPR_NUM_RESET_RELOAD_RETRIES;
6989         ioa_cfg->in_ioa_bringdown = 1;
6990         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
6991         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6992 }
6993
6994 /**
6995  * ipr_pci_error_detected - Called when a PCI error is detected.
6996  * @pdev:       PCI device struct
6997  * @state:      PCI channel state
6998  *
6999  * Description: Called when a PCI error is detected.
7000  *
7001  * Return value:
7002  *      PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7003  */
7004 static pci_ers_result_t ipr_pci_error_detected(struct pci_dev *pdev,
7005                                                pci_channel_state_t state)
7006 {
7007         switch (state) {
7008         case pci_channel_io_frozen:
7009                 ipr_pci_frozen(pdev);
7010                 return PCI_ERS_RESULT_NEED_RESET;
7011         case pci_channel_io_perm_failure:
7012                 ipr_pci_perm_failure(pdev);
7013                 return PCI_ERS_RESULT_DISCONNECT;
7014                 break;
7015         default:
7016                 break;
7017         }
7018         return PCI_ERS_RESULT_NEED_RESET;
7019 }
7020
7021 /**
7022  * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
7023  * @ioa_cfg:    ioa cfg struct
7024  *
7025  * Description: This is the second phase of adapter intialization
7026  * This function takes care of initilizing the adapter to the point
7027  * where it can accept new commands.
7028
7029  * Return value:
7030  *      0 on success / -EIO on failure
7031  **/
7032 static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
7033 {
7034         int rc = 0;
7035         unsigned long host_lock_flags = 0;
7036
7037         ENTER;
7038         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7039         dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
7040         if (ioa_cfg->needs_hard_reset) {
7041                 ioa_cfg->needs_hard_reset = 0;
7042                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7043         } else
7044                 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa,
7045                                         IPR_SHUTDOWN_NONE);
7046
7047         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7048         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7049         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7050
7051         if (ioa_cfg->ioa_is_dead) {
7052                 rc = -EIO;
7053         } else if (ipr_invalid_adapter(ioa_cfg)) {
7054                 if (!ipr_testmode)
7055                         rc = -EIO;
7056
7057                 dev_err(&ioa_cfg->pdev->dev,
7058                         "Adapter not supported in this hardware configuration.\n");
7059         }
7060
7061         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7062
7063         LEAVE;
7064         return rc;
7065 }
7066
7067 /**
7068  * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
7069  * @ioa_cfg:    ioa config struct
7070  *
7071  * Return value:
7072  *      none
7073  **/
7074 static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
7075 {
7076         int i;
7077
7078         for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
7079                 if (ioa_cfg->ipr_cmnd_list[i])
7080                         pci_pool_free(ioa_cfg->ipr_cmd_pool,
7081                                       ioa_cfg->ipr_cmnd_list[i],
7082                                       ioa_cfg->ipr_cmnd_list_dma[i]);
7083
7084                 ioa_cfg->ipr_cmnd_list[i] = NULL;
7085         }
7086
7087         if (ioa_cfg->ipr_cmd_pool)
7088                 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
7089
7090         ioa_cfg->ipr_cmd_pool = NULL;
7091 }
7092
7093 /**
7094  * ipr_free_mem - Frees memory allocated for an adapter
7095  * @ioa_cfg:    ioa cfg struct
7096  *
7097  * Return value:
7098  *      nothing
7099  **/
7100 static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
7101 {
7102         int i;
7103
7104         kfree(ioa_cfg->res_entries);
7105         pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
7106                             ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
7107         ipr_free_cmd_blks(ioa_cfg);
7108         pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
7109                             ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
7110         pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_config_table),
7111                             ioa_cfg->cfg_table,
7112                             ioa_cfg->cfg_table_dma);
7113
7114         for (i = 0; i < IPR_NUM_HCAMS; i++) {
7115                 pci_free_consistent(ioa_cfg->pdev,
7116                                     sizeof(struct ipr_hostrcb),
7117                                     ioa_cfg->hostrcb[i],
7118                                     ioa_cfg->hostrcb_dma[i]);
7119         }
7120
7121         ipr_free_dump(ioa_cfg);
7122         kfree(ioa_cfg->trace);
7123 }
7124
7125 /**
7126  * ipr_free_all_resources - Free all allocated resources for an adapter.
7127  * @ipr_cmd:    ipr command struct
7128  *
7129  * This function frees all allocated resources for the
7130  * specified adapter.
7131  *
7132  * Return value:
7133  *      none
7134  **/
7135 static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
7136 {
7137         struct pci_dev *pdev = ioa_cfg->pdev;
7138
7139         ENTER;
7140         free_irq(pdev->irq, ioa_cfg);
7141         iounmap(ioa_cfg->hdw_dma_regs);
7142         pci_release_regions(pdev);
7143         ipr_free_mem(ioa_cfg);
7144         scsi_host_put(ioa_cfg->host);
7145         pci_disable_device(pdev);
7146         LEAVE;
7147 }
7148
7149 /**
7150  * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
7151  * @ioa_cfg:    ioa config struct
7152  *
7153  * Return value:
7154  *      0 on success / -ENOMEM on allocation failure
7155  **/
7156 static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
7157 {
7158         struct ipr_cmnd *ipr_cmd;
7159         struct ipr_ioarcb *ioarcb;
7160         dma_addr_t dma_addr;
7161         int i;
7162
7163         ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
7164                                                  sizeof(struct ipr_cmnd), 8, 0);
7165
7166         if (!ioa_cfg->ipr_cmd_pool)
7167                 return -ENOMEM;
7168
7169         for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
7170                 ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, GFP_KERNEL, &dma_addr);
7171
7172                 if (!ipr_cmd) {
7173                         ipr_free_cmd_blks(ioa_cfg);
7174                         return -ENOMEM;
7175                 }
7176
7177                 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
7178                 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
7179                 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
7180
7181                 ioarcb = &ipr_cmd->ioarcb;
7182                 ioarcb->ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
7183                 ioarcb->host_response_handle = cpu_to_be32(i << 2);
7184                 ioarcb->write_ioadl_addr =
7185                         cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
7186                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
7187                 ioarcb->ioasa_host_pci_addr =
7188                         cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioasa));
7189                 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
7190                 ipr_cmd->cmd_index = i;
7191                 ipr_cmd->ioa_cfg = ioa_cfg;
7192                 ipr_cmd->sense_buffer_dma = dma_addr +
7193                         offsetof(struct ipr_cmnd, sense_buffer);
7194
7195                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
7196         }
7197
7198         return 0;
7199 }
7200
7201 /**
7202  * ipr_alloc_mem - Allocate memory for an adapter
7203  * @ioa_cfg:    ioa config struct
7204  *
7205  * Return value:
7206  *      0 on success / non-zero for error
7207  **/
7208 static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
7209 {
7210         struct pci_dev *pdev = ioa_cfg->pdev;
7211         int i, rc = -ENOMEM;
7212
7213         ENTER;
7214         ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
7215                                        IPR_MAX_PHYSICAL_DEVS, GFP_KERNEL);
7216
7217         if (!ioa_cfg->res_entries)
7218                 goto out;
7219
7220         for (i = 0; i < IPR_MAX_PHYSICAL_DEVS; i++)
7221                 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
7222
7223         ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
7224                                                 sizeof(struct ipr_misc_cbs),
7225                                                 &ioa_cfg->vpd_cbs_dma);
7226
7227         if (!ioa_cfg->vpd_cbs)
7228                 goto out_free_res_entries;
7229
7230         if (ipr_alloc_cmd_blks(ioa_cfg))
7231                 goto out_free_vpd_cbs;
7232
7233         ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
7234                                                  sizeof(u32) * IPR_NUM_CMD_BLKS,
7235                                                  &ioa_cfg->host_rrq_dma);
7236
7237         if (!ioa_cfg->host_rrq)
7238                 goto out_ipr_free_cmd_blocks;
7239
7240         ioa_cfg->cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
7241                                                   sizeof(struct ipr_config_table),
7242                                                   &ioa_cfg->cfg_table_dma);
7243
7244         if (!ioa_cfg->cfg_table)
7245                 goto out_free_host_rrq;
7246
7247         for (i = 0; i < IPR_NUM_HCAMS; i++) {
7248                 ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
7249                                                            sizeof(struct ipr_hostrcb),
7250                                                            &ioa_cfg->hostrcb_dma[i]);
7251
7252                 if (!ioa_cfg->hostrcb[i])
7253                         goto out_free_hostrcb_dma;
7254
7255                 ioa_cfg->hostrcb[i]->hostrcb_dma =
7256                         ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
7257                 ioa_cfg->hostrcb[i]->ioa_cfg = ioa_cfg;
7258                 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
7259         }
7260
7261         ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
7262                                  IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
7263
7264         if (!ioa_cfg->trace)
7265                 goto out_free_hostrcb_dma;
7266
7267         rc = 0;
7268 out:
7269         LEAVE;
7270         return rc;
7271
7272 out_free_hostrcb_dma:
7273         while (i-- > 0) {
7274                 pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
7275                                     ioa_cfg->hostrcb[i],
7276                                     ioa_cfg->hostrcb_dma[i]);
7277         }
7278         pci_free_consistent(pdev, sizeof(struct ipr_config_table),
7279                             ioa_cfg->cfg_table, ioa_cfg->cfg_table_dma);
7280 out_free_host_rrq:
7281         pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
7282                             ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
7283 out_ipr_free_cmd_blocks:
7284         ipr_free_cmd_blks(ioa_cfg);
7285 out_free_vpd_cbs:
7286         pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
7287                             ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
7288 out_free_res_entries:
7289         kfree(ioa_cfg->res_entries);
7290         goto out;
7291 }
7292
7293 /**
7294  * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
7295  * @ioa_cfg:    ioa config struct
7296  *
7297  * Return value:
7298  *      none
7299  **/
7300 static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
7301 {
7302         int i;
7303
7304         for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
7305                 ioa_cfg->bus_attr[i].bus = i;
7306                 ioa_cfg->bus_attr[i].qas_enabled = 0;
7307                 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
7308                 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
7309                         ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
7310                 else
7311                         ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
7312         }
7313 }
7314
7315 /**
7316  * ipr_init_ioa_cfg - Initialize IOA config struct
7317  * @ioa_cfg:    ioa config struct
7318  * @host:               scsi host struct
7319  * @pdev:               PCI dev struct
7320  *
7321  * Return value:
7322  *      none
7323  **/
7324 static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
7325                                        struct Scsi_Host *host, struct pci_dev *pdev)
7326 {
7327         const struct ipr_interrupt_offsets *p;
7328         struct ipr_interrupts *t;
7329         void __iomem *base;
7330
7331         ioa_cfg->host = host;
7332         ioa_cfg->pdev = pdev;
7333         ioa_cfg->log_level = ipr_log_level;
7334         ioa_cfg->doorbell = IPR_DOORBELL;
7335         sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
7336         sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
7337         sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
7338         sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
7339         sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
7340         sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
7341         sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
7342         sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
7343
7344         INIT_LIST_HEAD(&ioa_cfg->free_q);
7345         INIT_LIST_HEAD(&ioa_cfg->pending_q);
7346         INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
7347         INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
7348         INIT_LIST_HEAD(&ioa_cfg->free_res_q);
7349         INIT_LIST_HEAD(&ioa_cfg->used_res_q);
7350         INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread);
7351         init_waitqueue_head(&ioa_cfg->reset_wait_q);
7352         ioa_cfg->sdt_state = INACTIVE;
7353         if (ipr_enable_cache)
7354                 ioa_cfg->cache_state = CACHE_ENABLED;
7355         else
7356                 ioa_cfg->cache_state = CACHE_DISABLED;
7357
7358         ipr_initialize_bus_attr(ioa_cfg);
7359
7360         host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
7361         host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
7362         host->max_channel = IPR_MAX_BUS_TO_SCAN;
7363         host->unique_id = host->host_no;
7364         host->max_cmd_len = IPR_MAX_CDB_LEN;
7365         pci_set_drvdata(pdev, ioa_cfg);
7366
7367         p = &ioa_cfg->chip_cfg->regs;
7368         t = &ioa_cfg->regs;
7369         base = ioa_cfg->hdw_dma_regs;
7370
7371         t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
7372         t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
7373         t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
7374         t->clr_interrupt_reg = base + p->clr_interrupt_reg;
7375         t->sense_interrupt_reg = base + p->sense_interrupt_reg;
7376         t->ioarrin_reg = base + p->ioarrin_reg;
7377         t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
7378         t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
7379         t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
7380 }
7381
7382 /**
7383  * ipr_get_chip_cfg - Find adapter chip configuration
7384  * @dev_id:             PCI device id struct
7385  *
7386  * Return value:
7387  *      ptr to chip config on success / NULL on failure
7388  **/
7389 static const struct ipr_chip_cfg_t * __devinit
7390 ipr_get_chip_cfg(const struct pci_device_id *dev_id)
7391 {
7392         int i;
7393
7394         for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
7395                 if (ipr_chip[i].vendor == dev_id->vendor &&
7396                     ipr_chip[i].device == dev_id->device)
7397                         return ipr_chip[i].cfg;
7398         return NULL;
7399 }
7400
7401 /**
7402  * ipr_probe_ioa - Allocates memory and does first stage of initialization
7403  * @pdev:               PCI device struct
7404  * @dev_id:             PCI device id struct
7405  *
7406  * Return value:
7407  *      0 on success / non-zero on failure
7408  **/
7409 static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
7410                                    const struct pci_device_id *dev_id)
7411 {
7412         struct ipr_ioa_cfg *ioa_cfg;
7413         struct Scsi_Host *host;
7414         unsigned long ipr_regs_pci;
7415         void __iomem *ipr_regs;
7416         int rc = PCIBIOS_SUCCESSFUL;
7417         volatile u32 mask, uproc, interrupts;
7418
7419         ENTER;
7420
7421         if ((rc = pci_enable_device(pdev))) {
7422                 dev_err(&pdev->dev, "Cannot enable adapter\n");
7423                 goto out;
7424         }
7425
7426         dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
7427
7428         host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
7429
7430         if (!host) {
7431                 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
7432                 rc = -ENOMEM;
7433                 goto out_disable;
7434         }
7435
7436         ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
7437         memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
7438         ata_host_init(&ioa_cfg->ata_host, &pdev->dev,
7439                       sata_port_info.flags, &ipr_sata_ops);
7440
7441         ioa_cfg->chip_cfg = ipr_get_chip_cfg(dev_id);
7442
7443         if (!ioa_cfg->chip_cfg) {
7444                 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
7445                         dev_id->vendor, dev_id->device);
7446                 goto out_scsi_host_put;
7447         }
7448
7449         if (ipr_transop_timeout)
7450                 ioa_cfg->transop_timeout = ipr_transop_timeout;
7451         else if (dev_id->driver_data & IPR_USE_LONG_TRANSOP_TIMEOUT)
7452                 ioa_cfg->transop_timeout = IPR_LONG_OPERATIONAL_TIMEOUT;
7453         else
7454                 ioa_cfg->transop_timeout = IPR_OPERATIONAL_TIMEOUT;
7455
7456         ioa_cfg->revid = pdev->revision;
7457
7458         ipr_regs_pci = pci_resource_start(pdev, 0);
7459
7460         rc = pci_request_regions(pdev, IPR_NAME);
7461         if (rc < 0) {
7462                 dev_err(&pdev->dev,
7463                         "Couldn't register memory range of registers\n");
7464                 goto out_scsi_host_put;
7465         }
7466
7467         ipr_regs = ioremap(ipr_regs_pci, pci_resource_len(pdev, 0));
7468
7469         if (!ipr_regs) {
7470                 dev_err(&pdev->dev,
7471                         "Couldn't map memory range of registers\n");
7472                 rc = -ENOMEM;
7473                 goto out_release_regions;
7474         }
7475
7476         ioa_cfg->hdw_dma_regs = ipr_regs;
7477         ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
7478         ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
7479
7480         ipr_init_ioa_cfg(ioa_cfg, host, pdev);
7481
7482         pci_set_master(pdev);
7483
7484         rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7485         if (rc < 0) {
7486                 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
7487                 goto cleanup_nomem;
7488         }
7489
7490         rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
7491                                    ioa_cfg->chip_cfg->cache_line_size);
7492
7493         if (rc != PCIBIOS_SUCCESSFUL) {
7494                 dev_err(&pdev->dev, "Write of cache line size failed\n");
7495                 rc = -EIO;
7496                 goto cleanup_nomem;
7497         }
7498
7499         /* Save away PCI config space for use following IOA reset */
7500         rc = pci_save_state(pdev);
7501
7502         if (rc != PCIBIOS_SUCCESSFUL) {
7503                 dev_err(&pdev->dev, "Failed to save PCI config space\n");
7504                 rc = -EIO;
7505                 goto cleanup_nomem;
7506         }
7507
7508         if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
7509                 goto cleanup_nomem;
7510
7511         if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
7512                 goto cleanup_nomem;
7513
7514         rc = ipr_alloc_mem(ioa_cfg);
7515         if (rc < 0) {
7516                 dev_err(&pdev->dev,
7517                         "Couldn't allocate enough memory for device driver!\n");
7518                 goto cleanup_nomem;
7519         }
7520
7521         /*
7522          * If HRRQ updated interrupt is not masked, or reset alert is set,
7523          * the card is in an unknown state and needs a hard reset
7524          */
7525         mask = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7526         interrupts = readl(ioa_cfg->regs.sense_interrupt_reg);
7527         uproc = readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
7528         if ((mask & IPR_PCII_HRRQ_UPDATED) == 0 || (uproc & IPR_UPROCI_RESET_ALERT))
7529                 ioa_cfg->needs_hard_reset = 1;
7530         if (interrupts & IPR_PCII_ERROR_INTERRUPTS)
7531                 ioa_cfg->needs_hard_reset = 1;
7532         if (interrupts & IPR_PCII_IOA_UNIT_CHECKED)
7533                 ioa_cfg->ioa_unit_checked = 1;
7534
7535         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
7536         rc = request_irq(pdev->irq, ipr_isr, IRQF_SHARED, IPR_NAME, ioa_cfg);
7537
7538         if (rc) {
7539                 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
7540                         pdev->irq, rc);
7541                 goto cleanup_nolog;
7542         }
7543
7544         if ((dev_id->driver_data & IPR_USE_PCI_WARM_RESET) ||
7545             (dev_id->device == PCI_DEVICE_ID_IBM_OBSIDIAN_E && !ioa_cfg->revid)) {
7546                 ioa_cfg->needs_warm_reset = 1;
7547                 ioa_cfg->reset = ipr_reset_slot_reset;
7548         } else
7549                 ioa_cfg->reset = ipr_reset_start_bist;
7550
7551         spin_lock(&ipr_driver_lock);
7552         list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
7553         spin_unlock(&ipr_driver_lock);
7554
7555         LEAVE;
7556 out:
7557         return rc;
7558
7559 cleanup_nolog:
7560         ipr_free_mem(ioa_cfg);
7561 cleanup_nomem:
7562         iounmap(ipr_regs);
7563 out_release_regions:
7564         pci_release_regions(pdev);
7565 out_scsi_host_put:
7566         scsi_host_put(host);
7567 out_disable:
7568         pci_disable_device(pdev);
7569         goto out;
7570 }
7571
7572 /**
7573  * ipr_scan_vsets - Scans for VSET devices
7574  * @ioa_cfg:    ioa config struct
7575  *
7576  * Description: Since the VSET resources do not follow SAM in that we can have
7577  * sparse LUNs with no LUN 0, we have to scan for these ourselves.
7578  *
7579  * Return value:
7580  *      none
7581  **/
7582 static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
7583 {
7584         int target, lun;
7585
7586         for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
7587                 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
7588                         scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
7589 }
7590
7591 /**
7592  * ipr_initiate_ioa_bringdown - Bring down an adapter
7593  * @ioa_cfg:            ioa config struct
7594  * @shutdown_type:      shutdown type
7595  *
7596  * Description: This function will initiate bringing down the adapter.
7597  * This consists of issuing an IOA shutdown to the adapter
7598  * to flush the cache, and running BIST.
7599  * If the caller needs to wait on the completion of the reset,
7600  * the caller must sleep on the reset_wait_q.
7601  *
7602  * Return value:
7603  *      none
7604  **/
7605 static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
7606                                        enum ipr_shutdown_type shutdown_type)
7607 {
7608         ENTER;
7609         if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
7610                 ioa_cfg->sdt_state = ABORT_DUMP;
7611         ioa_cfg->reset_retries = 0;
7612         ioa_cfg->in_ioa_bringdown = 1;
7613         ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
7614         LEAVE;
7615 }
7616
7617 /**
7618  * __ipr_remove - Remove a single adapter
7619  * @pdev:       pci device struct
7620  *
7621  * Adapter hot plug remove entry point.
7622  *
7623  * Return value:
7624  *      none
7625  **/
7626 static void __ipr_remove(struct pci_dev *pdev)
7627 {
7628         unsigned long host_lock_flags = 0;
7629         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7630         ENTER;
7631
7632         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7633         while(ioa_cfg->in_reset_reload) {
7634                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7635                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7636                 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7637         }
7638
7639         ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
7640
7641         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7642         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7643         flush_scheduled_work();
7644         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7645
7646         spin_lock(&ipr_driver_lock);
7647         list_del(&ioa_cfg->queue);
7648         spin_unlock(&ipr_driver_lock);
7649
7650         if (ioa_cfg->sdt_state == ABORT_DUMP)
7651                 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
7652         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7653
7654         ipr_free_all_resources(ioa_cfg);
7655
7656         LEAVE;
7657 }
7658
7659 /**
7660  * ipr_remove - IOA hot plug remove entry point
7661  * @pdev:       pci device struct
7662  *
7663  * Adapter hot plug remove entry point.
7664  *
7665  * Return value:
7666  *      none
7667  **/
7668 static void ipr_remove(struct pci_dev *pdev)
7669 {
7670         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7671
7672         ENTER;
7673
7674         ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
7675                               &ipr_trace_attr);
7676         ipr_remove_dump_file(&ioa_cfg->host->shost_classdev.kobj,
7677                              &ipr_dump_attr);
7678         scsi_remove_host(ioa_cfg->host);
7679
7680         __ipr_remove(pdev);
7681
7682         LEAVE;
7683 }
7684
7685 /**
7686  * ipr_probe - Adapter hot plug add entry point
7687  *
7688  * Return value:
7689  *      0 on success / non-zero on failure
7690  **/
7691 static int __devinit ipr_probe(struct pci_dev *pdev,
7692                                const struct pci_device_id *dev_id)
7693 {
7694         struct ipr_ioa_cfg *ioa_cfg;
7695         int rc;
7696
7697         rc = ipr_probe_ioa(pdev, dev_id);
7698
7699         if (rc)
7700                 return rc;
7701
7702         ioa_cfg = pci_get_drvdata(pdev);
7703         rc = ipr_probe_ioa_part2(ioa_cfg);
7704
7705         if (rc) {
7706                 __ipr_remove(pdev);
7707                 return rc;
7708         }
7709
7710         rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
7711
7712         if (rc) {
7713                 __ipr_remove(pdev);
7714                 return rc;
7715         }
7716
7717         rc = ipr_create_trace_file(&ioa_cfg->host->shost_classdev.kobj,
7718                                    &ipr_trace_attr);
7719
7720         if (rc) {
7721                 scsi_remove_host(ioa_cfg->host);
7722                 __ipr_remove(pdev);
7723                 return rc;
7724         }
7725
7726         rc = ipr_create_dump_file(&ioa_cfg->host->shost_classdev.kobj,
7727                                    &ipr_dump_attr);
7728
7729         if (rc) {
7730                 ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
7731                                       &ipr_trace_attr);
7732                 scsi_remove_host(ioa_cfg->host);
7733                 __ipr_remove(pdev);
7734                 return rc;
7735         }
7736
7737         scsi_scan_host(ioa_cfg->host);
7738         ipr_scan_vsets(ioa_cfg);
7739         scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
7740         ioa_cfg->allow_ml_add_del = 1;
7741         ioa_cfg->host->max_channel = IPR_VSET_BUS;
7742         schedule_work(&ioa_cfg->work_q);
7743         return 0;
7744 }
7745
7746 /**
7747  * ipr_shutdown - Shutdown handler.
7748  * @pdev:       pci device struct
7749  *
7750  * This function is invoked upon system shutdown/reboot. It will issue
7751  * an adapter shutdown to the adapter to flush the write cache.
7752  *
7753  * Return value:
7754  *      none
7755  **/
7756 static void ipr_shutdown(struct pci_dev *pdev)
7757 {
7758         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7759         unsigned long lock_flags = 0;
7760
7761         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7762         while(ioa_cfg->in_reset_reload) {
7763                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7764                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7765                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7766         }
7767
7768         ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
7769         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7770         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7771 }
7772
7773 static struct pci_device_id ipr_pci_table[] __devinitdata = {
7774         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
7775                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702, 0, 0, 0 },
7776         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
7777                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703, 0, 0, 0 },
7778         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
7779                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D, 0, 0, 0 },
7780         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
7781                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E, 0, 0, 0 },
7782         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
7783                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B, 0, 0, 0 },
7784         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
7785                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E, 0, 0, 0 },
7786         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
7787                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A, 0, 0, 0 },
7788         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
7789                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575B, 0, 0,
7790                 IPR_USE_LONG_TRANSOP_TIMEOUT },
7791         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
7792               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
7793         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
7794               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
7795               IPR_USE_LONG_TRANSOP_TIMEOUT },
7796         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
7797               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
7798               IPR_USE_LONG_TRANSOP_TIMEOUT },
7799         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
7800               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
7801         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
7802               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
7803               IPR_USE_LONG_TRANSOP_TIMEOUT},
7804         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
7805               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
7806               IPR_USE_LONG_TRANSOP_TIMEOUT },
7807         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
7808               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574E, 0, 0,
7809               IPR_USE_LONG_TRANSOP_TIMEOUT },
7810         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
7811               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575D, 0, 0,
7812               IPR_USE_LONG_TRANSOP_TIMEOUT },
7813         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
7814               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B3, 0, 0, 0 },
7815         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
7816               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B7, 0, 0,
7817               IPR_USE_LONG_TRANSOP_TIMEOUT | IPR_USE_PCI_WARM_RESET },
7818         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
7819                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780, 0, 0, 0 },
7820         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
7821                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E, 0, 0, 0 },
7822         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
7823                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571F, 0, 0,
7824                 IPR_USE_LONG_TRANSOP_TIMEOUT },
7825         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
7826                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572F, 0, 0,
7827                 IPR_USE_LONG_TRANSOP_TIMEOUT },
7828         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SCAMP_E,
7829                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574D, 0, 0,
7830                 IPR_USE_LONG_TRANSOP_TIMEOUT },
7831         { }
7832 };
7833 MODULE_DEVICE_TABLE(pci, ipr_pci_table);
7834
7835 static struct pci_error_handlers ipr_err_handler = {
7836         .error_detected = ipr_pci_error_detected,
7837         .slot_reset = ipr_pci_slot_reset,
7838 };
7839
7840 static struct pci_driver ipr_driver = {
7841         .name = IPR_NAME,
7842         .id_table = ipr_pci_table,
7843         .probe = ipr_probe,
7844         .remove = ipr_remove,
7845         .shutdown = ipr_shutdown,
7846         .err_handler = &ipr_err_handler,
7847         .dynids.use_driver_data = 1
7848 };
7849
7850 /**
7851  * ipr_init - Module entry point
7852  *
7853  * Return value:
7854  *      0 on success / negative value on failure
7855  **/
7856 static int __init ipr_init(void)
7857 {
7858         ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
7859                  IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
7860
7861         return pci_register_driver(&ipr_driver);
7862 }
7863
7864 /**
7865  * ipr_exit - Module unload
7866  *
7867  * Module unload entry point.
7868  *
7869  * Return value:
7870  *      none
7871  **/
7872 static void __exit ipr_exit(void)
7873 {
7874         pci_unregister_driver(&ipr_driver);
7875 }
7876
7877 module_init(ipr_init);
7878 module_exit(ipr_exit);