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