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