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