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