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