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