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