Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[linux-2.6.git] / drivers / scsi / ipr.c
1 /*
2  * ipr.c -- driver for IBM Power Linux RAID adapters
3  *
4  * Written By: Brian King <brking@us.ibm.com>, IBM Corporation
5  *
6  * Copyright (C) 2003, 2004 IBM Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 /*
25  * Notes:
26  *
27  * This driver is used to control the following SCSI adapters:
28  *
29  * IBM iSeries: 5702, 5703, 2780, 5709, 570A, 570B
30  *
31  * IBM pSeries: PCI-X Dual Channel Ultra 320 SCSI RAID Adapter
32  *              PCI-X Dual Channel Ultra 320 SCSI Adapter
33  *              PCI-X Dual Channel Ultra 320 SCSI RAID Enablement Card
34  *              Embedded SCSI adapter on p615 and p655 systems
35  *
36  * Supported Hardware Features:
37  *      - Ultra 320 SCSI controller
38  *      - PCI-X host interface
39  *      - Embedded PowerPC RISC Processor and Hardware XOR DMA Engine
40  *      - Non-Volatile Write Cache
41  *      - Supports attachment of non-RAID disks, tape, and optical devices
42  *      - RAID Levels 0, 5, 10
43  *      - Hot spare
44  *      - Background Parity Checking
45  *      - Background Data Scrubbing
46  *      - Ability to increase the capacity of an existing RAID 5 disk array
47  *              by adding disks
48  *
49  * Driver Features:
50  *      - Tagged command queuing
51  *      - Adapter microcode download
52  *      - PCI hot plug
53  *      - SCSI device hot plug
54  *
55  */
56
57 #include <linux/fs.h>
58 #include <linux/init.h>
59 #include <linux/types.h>
60 #include <linux/errno.h>
61 #include <linux/kernel.h>
62 #include <linux/ioport.h>
63 #include <linux/delay.h>
64 #include <linux/pci.h>
65 #include <linux/wait.h>
66 #include <linux/spinlock.h>
67 #include <linux/sched.h>
68 #include <linux/interrupt.h>
69 #include <linux/blkdev.h>
70 #include <linux/firmware.h>
71 #include <linux/module.h>
72 #include <linux/moduleparam.h>
73 #include <linux/libata.h>
74 #include <linux/hdreg.h>
75 #include <asm/io.h>
76 #include <asm/irq.h>
77 #include <asm/processor.h>
78 #include <scsi/scsi.h>
79 #include <scsi/scsi_host.h>
80 #include <scsi/scsi_tcq.h>
81 #include <scsi/scsi_eh.h>
82 #include <scsi/scsi_cmnd.h>
83 #include "ipr.h"
84
85 /*
86  *   Global Data
87  */
88 static LIST_HEAD(ipr_ioa_head);
89 static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
90 static unsigned int ipr_max_speed = 1;
91 static int ipr_testmode = 0;
92 static unsigned int ipr_fastfail = 0;
93 static unsigned int ipr_transop_timeout = 0;
94 static unsigned int ipr_enable_cache = 1;
95 static unsigned int ipr_debug = 0;
96 static unsigned int ipr_dual_ioa_raid = 1;
97 static DEFINE_SPINLOCK(ipr_driver_lock);
98
99 /* This table describes the differences between DMA controller chips */
100 static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
101         { /* Gemstone, Citrine, Obsidian, and Obsidian-E */
102                 .mailbox = 0x0042C,
103                 .cache_line_size = 0x20,
104                 {
105                         .set_interrupt_mask_reg = 0x0022C,
106                         .clr_interrupt_mask_reg = 0x00230,
107                         .sense_interrupt_mask_reg = 0x0022C,
108                         .clr_interrupt_reg = 0x00228,
109                         .sense_interrupt_reg = 0x00224,
110                         .ioarrin_reg = 0x00404,
111                         .sense_uproc_interrupt_reg = 0x00214,
112                         .set_uproc_interrupt_reg = 0x00214,
113                         .clr_uproc_interrupt_reg = 0x00218
114                 }
115         },
116         { /* Snipe and Scamp */
117                 .mailbox = 0x0052C,
118                 .cache_line_size = 0x20,
119                 {
120                         .set_interrupt_mask_reg = 0x00288,
121                         .clr_interrupt_mask_reg = 0x0028C,
122                         .sense_interrupt_mask_reg = 0x00288,
123                         .clr_interrupt_reg = 0x00284,
124                         .sense_interrupt_reg = 0x00280,
125                         .ioarrin_reg = 0x00504,
126                         .sense_uproc_interrupt_reg = 0x00290,
127                         .set_uproc_interrupt_reg = 0x00290,
128                         .clr_uproc_interrupt_reg = 0x00294
129                 }
130         },
131 };
132
133 static const struct ipr_chip_t ipr_chip[] = {
134         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, &ipr_chip_cfg[0] },
135         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, &ipr_chip_cfg[0] },
136         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, &ipr_chip_cfg[0] },
137         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, &ipr_chip_cfg[0] },
138         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, &ipr_chip_cfg[0] },
139         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, &ipr_chip_cfg[1] },
140         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, &ipr_chip_cfg[1] }
141 };
142
143 static int ipr_max_bus_speeds [] = {
144         IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
145 };
146
147 MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
148 MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
149 module_param_named(max_speed, ipr_max_speed, uint, 0);
150 MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
151 module_param_named(log_level, ipr_log_level, uint, 0);
152 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
153 module_param_named(testmode, ipr_testmode, int, 0);
154 MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
155 module_param_named(fastfail, ipr_fastfail, int, 0);
156 MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
157 module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
158 MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
159 module_param_named(enable_cache, ipr_enable_cache, int, 0);
160 MODULE_PARM_DESC(enable_cache, "Enable adapter's non-volatile write cache (default: 1)");
161 module_param_named(debug, ipr_debug, int, 0);
162 MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
163 module_param_named(dual_ioa_raid, ipr_dual_ioa_raid, int, 0);
164 MODULE_PARM_DESC(dual_ioa_raid, "Enable dual adapter RAID support. Set to 1 to enable. (default: 1)");
165 MODULE_LICENSE("GPL");
166 MODULE_VERSION(IPR_DRIVER_VERSION);
167
168 /*  A constant array of IOASCs/URCs/Error Messages */
169 static const
170 struct ipr_error_table_t ipr_error_table[] = {
171         {0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
172         "8155: An unknown error was received"},
173         {0x00330000, 0, 0,
174         "Soft underlength error"},
175         {0x005A0000, 0, 0,
176         "Command to be cancelled not found"},
177         {0x00808000, 0, 0,
178         "Qualified success"},
179         {0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
180         "FFFE: Soft device bus error recovered by the IOA"},
181         {0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
182         "4101: Soft device bus fabric error"},
183         {0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
184         "FFF9: Device sector reassign successful"},
185         {0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
186         "FFF7: Media error recovered by device rewrite procedures"},
187         {0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
188         "7001: IOA sector reassignment successful"},
189         {0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
190         "FFF9: Soft media error. Sector reassignment recommended"},
191         {0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
192         "FFF7: Media error recovered by IOA rewrite procedures"},
193         {0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
194         "FF3D: Soft PCI bus error recovered by the IOA"},
195         {0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
196         "FFF6: Device hardware error recovered by the IOA"},
197         {0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
198         "FFF6: Device hardware error recovered by the device"},
199         {0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
200         "FF3D: Soft IOA error recovered by the IOA"},
201         {0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
202         "FFFA: Undefined device response recovered by the IOA"},
203         {0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
204         "FFF6: Device bus error, message or command phase"},
205         {0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
206         "FFFE: Task Management Function failed"},
207         {0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
208         "FFF6: Failure prediction threshold exceeded"},
209         {0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
210         "8009: Impending cache battery pack failure"},
211         {0x02040400, 0, 0,
212         "34FF: Disk device format in progress"},
213         {0x02048000, 0, IPR_DEFAULT_LOG_LEVEL,
214         "9070: IOA requested reset"},
215         {0x023F0000, 0, 0,
216         "Synchronization required"},
217         {0x024E0000, 0, 0,
218         "No ready, IOA shutdown"},
219         {0x025A0000, 0, 0,
220         "Not ready, IOA has been shutdown"},
221         {0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
222         "3020: Storage subsystem configuration error"},
223         {0x03110B00, 0, 0,
224         "FFF5: Medium error, data unreadable, recommend reassign"},
225         {0x03110C00, 0, 0,
226         "7000: Medium error, data unreadable, do not reassign"},
227         {0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
228         "FFF3: Disk media format bad"},
229         {0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
230         "3002: Addressed device failed to respond to selection"},
231         {0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
232         "3100: Device bus error"},
233         {0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
234         "3109: IOA timed out a device command"},
235         {0x04088000, 0, 0,
236         "3120: SCSI bus is not operational"},
237         {0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
238         "4100: Hard device bus fabric error"},
239         {0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
240         "9000: IOA reserved area data check"},
241         {0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
242         "9001: IOA reserved area invalid data pattern"},
243         {0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
244         "9002: IOA reserved area LRC error"},
245         {0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
246         "102E: Out of alternate sectors for disk storage"},
247         {0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
248         "FFF4: Data transfer underlength error"},
249         {0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
250         "FFF4: Data transfer overlength error"},
251         {0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
252         "3400: Logical unit failure"},
253         {0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
254         "FFF4: Device microcode is corrupt"},
255         {0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
256         "8150: PCI bus error"},
257         {0x04430000, 1, 0,
258         "Unsupported device bus message received"},
259         {0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
260         "FFF4: Disk device problem"},
261         {0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
262         "8150: Permanent IOA failure"},
263         {0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
264         "3010: Disk device returned wrong response to IOA"},
265         {0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
266         "8151: IOA microcode error"},
267         {0x04448500, 0, 0,
268         "Device bus status error"},
269         {0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
270         "8157: IOA error requiring IOA reset to recover"},
271         {0x04448700, 0, 0,
272         "ATA device status error"},
273         {0x04490000, 0, 0,
274         "Message reject received from the device"},
275         {0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
276         "8008: A permanent cache battery pack failure occurred"},
277         {0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
278         "9090: Disk unit has been modified after the last known status"},
279         {0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
280         "9081: IOA detected device error"},
281         {0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
282         "9082: IOA detected device error"},
283         {0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
284         "3110: Device bus error, message or command phase"},
285         {0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
286         "3110: SAS Command / Task Management Function failed"},
287         {0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
288         "9091: Incorrect hardware configuration change has been detected"},
289         {0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
290         "9073: Invalid multi-adapter configuration"},
291         {0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
292         "4010: Incorrect connection between cascaded expanders"},
293         {0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
294         "4020: Connections exceed IOA design limits"},
295         {0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
296         "4030: Incorrect multipath connection"},
297         {0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
298         "4110: Unsupported enclosure function"},
299         {0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
300         "FFF4: Command to logical unit failed"},
301         {0x05240000, 1, 0,
302         "Illegal request, invalid request type or request packet"},
303         {0x05250000, 0, 0,
304         "Illegal request, invalid resource handle"},
305         {0x05258000, 0, 0,
306         "Illegal request, commands not allowed to this device"},
307         {0x05258100, 0, 0,
308         "Illegal request, command not allowed to a secondary adapter"},
309         {0x05260000, 0, 0,
310         "Illegal request, invalid field in parameter list"},
311         {0x05260100, 0, 0,
312         "Illegal request, parameter not supported"},
313         {0x05260200, 0, 0,
314         "Illegal request, parameter value invalid"},
315         {0x052C0000, 0, 0,
316         "Illegal request, command sequence error"},
317         {0x052C8000, 1, 0,
318         "Illegal request, dual adapter support not enabled"},
319         {0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
320         "9031: Array protection temporarily suspended, protection resuming"},
321         {0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
322         "9040: Array protection temporarily suspended, protection resuming"},
323         {0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
324         "3140: Device bus not ready to ready transition"},
325         {0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
326         "FFFB: SCSI bus was reset"},
327         {0x06290500, 0, 0,
328         "FFFE: SCSI bus transition to single ended"},
329         {0x06290600, 0, 0,
330         "FFFE: SCSI bus transition to LVD"},
331         {0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
332         "FFFB: SCSI bus was reset by another initiator"},
333         {0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
334         "3029: A device replacement has occurred"},
335         {0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
336         "9051: IOA cache data exists for a missing or failed device"},
337         {0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
338         "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
339         {0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
340         "9025: Disk unit is not supported at its physical location"},
341         {0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
342         "3020: IOA detected a SCSI bus configuration error"},
343         {0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
344         "3150: SCSI bus configuration error"},
345         {0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
346         "9074: Asymmetric advanced function disk configuration"},
347         {0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
348         "4040: Incomplete multipath connection between IOA and enclosure"},
349         {0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
350         "4041: Incomplete multipath connection between enclosure and device"},
351         {0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
352         "9075: Incomplete multipath connection between IOA and remote IOA"},
353         {0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
354         "9076: Configuration error, missing remote IOA"},
355         {0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
356         "4050: Enclosure does not support a required multipath function"},
357         {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
358         "9041: Array protection temporarily suspended"},
359         {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
360         "9042: Corrupt array parity detected on specified device"},
361         {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
362         "9030: Array no longer protected due to missing or failed disk unit"},
363         {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
364         "9071: Link operational transition"},
365         {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
366         "9072: Link not operational transition"},
367         {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
368         "9032: Array exposed but still protected"},
369         {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
370         "70DD: Device forced failed by disrupt device command"},
371         {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
372         "4061: Multipath redundancy level got better"},
373         {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
374         "4060: Multipath redundancy level got worse"},
375         {0x07270000, 0, 0,
376         "Failure due to other device"},
377         {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
378         "9008: IOA does not support functions expected by devices"},
379         {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
380         "9010: Cache data associated with attached devices cannot be found"},
381         {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
382         "9011: Cache data belongs to devices other than those attached"},
383         {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
384         "9020: Array missing 2 or more devices with only 1 device present"},
385         {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
386         "9021: Array missing 2 or more devices with 2 or more devices present"},
387         {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
388         "9022: Exposed array is missing a required device"},
389         {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
390         "9023: Array member(s) not at required physical locations"},
391         {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
392         "9024: Array not functional due to present hardware configuration"},
393         {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
394         "9026: Array not functional due to present hardware configuration"},
395         {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
396         "9027: Array is missing a device and parity is out of sync"},
397         {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
398         "9028: Maximum number of arrays already exist"},
399         {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
400         "9050: Required cache data cannot be located for a disk unit"},
401         {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
402         "9052: Cache data exists for a device that has been modified"},
403         {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
404         "9054: IOA resources not available due to previous problems"},
405         {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
406         "9092: Disk unit requires initialization before use"},
407         {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
408         "9029: Incorrect hardware configuration change has been detected"},
409         {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
410         "9060: One or more disk pairs are missing from an array"},
411         {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
412         "9061: One or more disks are missing from an array"},
413         {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
414         "9062: One or more disks are missing from an array"},
415         {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
416         "9063: Maximum number of functional arrays has been exceeded"},
417         {0x0B260000, 0, 0,
418         "Aborted command, invalid descriptor"},
419         {0x0B5A0000, 0, 0,
420         "Command terminated by host"}
421 };
422
423 static const struct ipr_ses_table_entry ipr_ses_table[] = {
424         { "2104-DL1        ", "XXXXXXXXXXXXXXXX", 80 },
425         { "2104-TL1        ", "XXXXXXXXXXXXXXXX", 80 },
426         { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
427         { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
428         { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
429         { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
430         { "2104-DU3        ", "XXXXXXXXXXXXXXXX", 160 },
431         { "2104-TU3        ", "XXXXXXXXXXXXXXXX", 160 },
432         { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
433         { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
434         { "St  V1S2        ", "XXXXXXXXXXXXXXXX", 160 },
435         { "HSBPD4M  PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
436         { "VSBPD1H   U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
437 };
438
439 /*
440  *  Function Prototypes
441  */
442 static int ipr_reset_alert(struct ipr_cmnd *);
443 static void ipr_process_ccn(struct ipr_cmnd *);
444 static void ipr_process_error(struct ipr_cmnd *);
445 static void ipr_reset_ioa_job(struct ipr_cmnd *);
446 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
447                                    enum ipr_shutdown_type);
448
449 #ifdef CONFIG_SCSI_IPR_TRACE
450 /**
451  * ipr_trc_hook - Add a trace entry to the driver trace
452  * @ipr_cmd:    ipr command struct
453  * @type:               trace type
454  * @add_data:   additional data
455  *
456  * Return value:
457  *      none
458  **/
459 static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
460                          u8 type, u32 add_data)
461 {
462         struct ipr_trace_entry *trace_entry;
463         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
464
465         trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
466         trace_entry->time = jiffies;
467         trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
468         trace_entry->type = type;
469         trace_entry->ata_op_code = ipr_cmd->ioarcb.add_data.u.regs.command;
470         trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
471         trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
472         trace_entry->u.add_data = add_data;
473 }
474 #else
475 #define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
476 #endif
477
478 /**
479  * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
480  * @ipr_cmd:    ipr command struct
481  *
482  * Return value:
483  *      none
484  **/
485 static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
486 {
487         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
488         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
489         dma_addr_t dma_addr = be32_to_cpu(ioarcb->ioarcb_host_pci_addr);
490
491         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
492         ioarcb->write_data_transfer_length = 0;
493         ioarcb->read_data_transfer_length = 0;
494         ioarcb->write_ioadl_len = 0;
495         ioarcb->read_ioadl_len = 0;
496         ioarcb->write_ioadl_addr =
497                 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
498         ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
499         ioasa->ioasc = 0;
500         ioasa->residual_data_len = 0;
501         ioasa->u.gata.status = 0;
502
503         ipr_cmd->scsi_cmd = NULL;
504         ipr_cmd->qc = NULL;
505         ipr_cmd->sense_buffer[0] = 0;
506         ipr_cmd->dma_use_sg = 0;
507 }
508
509 /**
510  * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
511  * @ipr_cmd:    ipr command struct
512  *
513  * Return value:
514  *      none
515  **/
516 static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
517 {
518         ipr_reinit_ipr_cmnd(ipr_cmd);
519         ipr_cmd->u.scratch = 0;
520         ipr_cmd->sibling = NULL;
521         init_timer(&ipr_cmd->timer);
522 }
523
524 /**
525  * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
526  * @ioa_cfg:    ioa config struct
527  *
528  * Return value:
529  *      pointer to ipr command struct
530  **/
531 static
532 struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
533 {
534         struct ipr_cmnd *ipr_cmd;
535
536         ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
537         list_del(&ipr_cmd->queue);
538         ipr_init_ipr_cmnd(ipr_cmd);
539
540         return ipr_cmd;
541 }
542
543 /**
544  * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
545  * @ioa_cfg:    ioa config struct
546  * @clr_ints:     interrupts to clear
547  *
548  * This function masks all interrupts on the adapter, then clears the
549  * interrupts specified in the mask
550  *
551  * Return value:
552  *      none
553  **/
554 static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
555                                           u32 clr_ints)
556 {
557         volatile u32 int_reg;
558
559         /* Stop new interrupts */
560         ioa_cfg->allow_interrupts = 0;
561
562         /* Set interrupt mask to stop all new interrupts */
563         writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
564
565         /* Clear any pending interrupts */
566         writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg);
567         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
568 }
569
570 /**
571  * ipr_save_pcix_cmd_reg - Save PCI-X command register
572  * @ioa_cfg:    ioa config struct
573  *
574  * Return value:
575  *      0 on success / -EIO on failure
576  **/
577 static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
578 {
579         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
580
581         if (pcix_cmd_reg == 0)
582                 return 0;
583
584         if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
585                                  &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
586                 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
587                 return -EIO;
588         }
589
590         ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
591         return 0;
592 }
593
594 /**
595  * ipr_set_pcix_cmd_reg - Setup PCI-X command register
596  * @ioa_cfg:    ioa config struct
597  *
598  * Return value:
599  *      0 on success / -EIO on failure
600  **/
601 static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
602 {
603         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
604
605         if (pcix_cmd_reg) {
606                 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
607                                           ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
608                         dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
609                         return -EIO;
610                 }
611         }
612
613         return 0;
614 }
615
616 /**
617  * ipr_sata_eh_done - done function for aborted SATA commands
618  * @ipr_cmd:    ipr command struct
619  *
620  * This function is invoked for ops generated to SATA
621  * devices which are being aborted.
622  *
623  * Return value:
624  *      none
625  **/
626 static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
627 {
628         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
629         struct ata_queued_cmd *qc = ipr_cmd->qc;
630         struct ipr_sata_port *sata_port = qc->ap->private_data;
631
632         qc->err_mask |= AC_ERR_OTHER;
633         sata_port->ioasa.status |= ATA_BUSY;
634         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
635         ata_qc_complete(qc);
636 }
637
638 /**
639  * ipr_scsi_eh_done - mid-layer done function for aborted ops
640  * @ipr_cmd:    ipr command struct
641  *
642  * This function is invoked by the interrupt handler for
643  * ops generated by the SCSI mid-layer which are being aborted.
644  *
645  * Return value:
646  *      none
647  **/
648 static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
649 {
650         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
651         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
652
653         scsi_cmd->result |= (DID_ERROR << 16);
654
655         scsi_dma_unmap(ipr_cmd->scsi_cmd);
656         scsi_cmd->scsi_done(scsi_cmd);
657         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
658 }
659
660 /**
661  * ipr_fail_all_ops - Fails all outstanding ops.
662  * @ioa_cfg:    ioa config struct
663  *
664  * This function fails all outstanding ops.
665  *
666  * Return value:
667  *      none
668  **/
669 static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
670 {
671         struct ipr_cmnd *ipr_cmd, *temp;
672
673         ENTER;
674         list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
675                 list_del(&ipr_cmd->queue);
676
677                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
678                 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
679
680                 if (ipr_cmd->scsi_cmd)
681                         ipr_cmd->done = ipr_scsi_eh_done;
682                 else if (ipr_cmd->qc)
683                         ipr_cmd->done = ipr_sata_eh_done;
684
685                 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
686                 del_timer(&ipr_cmd->timer);
687                 ipr_cmd->done(ipr_cmd);
688         }
689
690         LEAVE;
691 }
692
693 /**
694  * ipr_do_req -  Send driver initiated requests.
695  * @ipr_cmd:            ipr command struct
696  * @done:                       done function
697  * @timeout_func:       timeout function
698  * @timeout:            timeout value
699  *
700  * This function sends the specified command to the adapter with the
701  * timeout given. The done function is invoked on command completion.
702  *
703  * Return value:
704  *      none
705  **/
706 static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
707                        void (*done) (struct ipr_cmnd *),
708                        void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
709 {
710         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
711
712         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
713
714         ipr_cmd->done = done;
715
716         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
717         ipr_cmd->timer.expires = jiffies + timeout;
718         ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
719
720         add_timer(&ipr_cmd->timer);
721
722         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
723
724         mb();
725         writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
726                ioa_cfg->regs.ioarrin_reg);
727 }
728
729 /**
730  * ipr_internal_cmd_done - Op done function for an internally generated op.
731  * @ipr_cmd:    ipr command struct
732  *
733  * This function is the op done function for an internally generated,
734  * blocking op. It simply wakes the sleeping thread.
735  *
736  * Return value:
737  *      none
738  **/
739 static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
740 {
741         if (ipr_cmd->sibling)
742                 ipr_cmd->sibling = NULL;
743         else
744                 complete(&ipr_cmd->completion);
745 }
746
747 /**
748  * ipr_send_blocking_cmd - Send command and sleep on its completion.
749  * @ipr_cmd:    ipr command struct
750  * @timeout_func:       function to invoke if command times out
751  * @timeout:    timeout
752  *
753  * Return value:
754  *      none
755  **/
756 static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
757                                   void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
758                                   u32 timeout)
759 {
760         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
761
762         init_completion(&ipr_cmd->completion);
763         ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
764
765         spin_unlock_irq(ioa_cfg->host->host_lock);
766         wait_for_completion(&ipr_cmd->completion);
767         spin_lock_irq(ioa_cfg->host->host_lock);
768 }
769
770 /**
771  * ipr_send_hcam - Send an HCAM to the adapter.
772  * @ioa_cfg:    ioa config struct
773  * @type:               HCAM type
774  * @hostrcb:    hostrcb struct
775  *
776  * This function will send a Host Controlled Async command to the adapter.
777  * If HCAMs are currently not allowed to be issued to the adapter, it will
778  * place the hostrcb on the free queue.
779  *
780  * Return value:
781  *      none
782  **/
783 static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
784                           struct ipr_hostrcb *hostrcb)
785 {
786         struct ipr_cmnd *ipr_cmd;
787         struct ipr_ioarcb *ioarcb;
788
789         if (ioa_cfg->allow_cmds) {
790                 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
791                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
792                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
793
794                 ipr_cmd->u.hostrcb = hostrcb;
795                 ioarcb = &ipr_cmd->ioarcb;
796
797                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
798                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
799                 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
800                 ioarcb->cmd_pkt.cdb[1] = type;
801                 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
802                 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
803
804                 ioarcb->read_data_transfer_length = cpu_to_be32(sizeof(hostrcb->hcam));
805                 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
806                 ipr_cmd->ioadl[0].flags_and_data_len =
807                         cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(hostrcb->hcam));
808                 ipr_cmd->ioadl[0].address = cpu_to_be32(hostrcb->hostrcb_dma);
809
810                 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
811                         ipr_cmd->done = ipr_process_ccn;
812                 else
813                         ipr_cmd->done = ipr_process_error;
814
815                 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
816
817                 mb();
818                 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
819                        ioa_cfg->regs.ioarrin_reg);
820         } else {
821                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
822         }
823 }
824
825 /**
826  * ipr_init_res_entry - Initialize a resource entry struct.
827  * @res:        resource entry struct
828  *
829  * Return value:
830  *      none
831  **/
832 static void ipr_init_res_entry(struct ipr_resource_entry *res)
833 {
834         res->needs_sync_complete = 0;
835         res->in_erp = 0;
836         res->add_to_ml = 0;
837         res->del_from_ml = 0;
838         res->resetting_device = 0;
839         res->sdev = NULL;
840         res->sata_port = NULL;
841 }
842
843 /**
844  * ipr_handle_config_change - Handle a config change from the adapter
845  * @ioa_cfg:    ioa config struct
846  * @hostrcb:    hostrcb
847  *
848  * Return value:
849  *      none
850  **/
851 static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
852                               struct ipr_hostrcb *hostrcb)
853 {
854         struct ipr_resource_entry *res = NULL;
855         struct ipr_config_table_entry *cfgte;
856         u32 is_ndn = 1;
857
858         cfgte = &hostrcb->hcam.u.ccn.cfgte;
859
860         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
861                 if (!memcmp(&res->cfgte.res_addr, &cfgte->res_addr,
862                             sizeof(cfgte->res_addr))) {
863                         is_ndn = 0;
864                         break;
865                 }
866         }
867
868         if (is_ndn) {
869                 if (list_empty(&ioa_cfg->free_res_q)) {
870                         ipr_send_hcam(ioa_cfg,
871                                       IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
872                                       hostrcb);
873                         return;
874                 }
875
876                 res = list_entry(ioa_cfg->free_res_q.next,
877                                  struct ipr_resource_entry, queue);
878
879                 list_del(&res->queue);
880                 ipr_init_res_entry(res);
881                 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
882         }
883
884         memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
885
886         if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
887                 if (res->sdev) {
888                         res->del_from_ml = 1;
889                         res->cfgte.res_handle = IPR_INVALID_RES_HANDLE;
890                         if (ioa_cfg->allow_ml_add_del)
891                                 schedule_work(&ioa_cfg->work_q);
892                 } else
893                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
894         } else if (!res->sdev) {
895                 res->add_to_ml = 1;
896                 if (ioa_cfg->allow_ml_add_del)
897                         schedule_work(&ioa_cfg->work_q);
898         }
899
900         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
901 }
902
903 /**
904  * ipr_process_ccn - Op done function for a CCN.
905  * @ipr_cmd:    ipr command struct
906  *
907  * This function is the op done function for a configuration
908  * change notification host controlled async from the adapter.
909  *
910  * Return value:
911  *      none
912  **/
913 static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
914 {
915         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
916         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
917         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
918
919         list_del(&hostrcb->queue);
920         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
921
922         if (ioasc) {
923                 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
924                         dev_err(&ioa_cfg->pdev->dev,
925                                 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
926
927                 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
928         } else {
929                 ipr_handle_config_change(ioa_cfg, hostrcb);
930         }
931 }
932
933 /**
934  * strip_and_pad_whitespace - Strip and pad trailing whitespace.
935  * @i:          index into buffer
936  * @buf:                string to modify
937  *
938  * This function will strip all trailing whitespace, pad the end
939  * of the string with a single space, and NULL terminate the string.
940  *
941  * Return value:
942  *      new length of string
943  **/
944 static int strip_and_pad_whitespace(int i, char *buf)
945 {
946         while (i && buf[i] == ' ')
947                 i--;
948         buf[i+1] = ' ';
949         buf[i+2] = '\0';
950         return i + 2;
951 }
952
953 /**
954  * ipr_log_vpd_compact - Log the passed extended VPD compactly.
955  * @prefix:             string to print at start of printk
956  * @hostrcb:    hostrcb pointer
957  * @vpd:                vendor/product id/sn struct
958  *
959  * Return value:
960  *      none
961  **/
962 static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
963                                 struct ipr_vpd *vpd)
964 {
965         char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
966         int i = 0;
967
968         memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
969         i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
970
971         memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
972         i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
973
974         memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
975         buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
976
977         ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
978 }
979
980 /**
981  * ipr_log_vpd - Log the passed VPD to the error log.
982  * @vpd:                vendor/product id/sn struct
983  *
984  * Return value:
985  *      none
986  **/
987 static void ipr_log_vpd(struct ipr_vpd *vpd)
988 {
989         char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
990                     + IPR_SERIAL_NUM_LEN];
991
992         memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
993         memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
994                IPR_PROD_ID_LEN);
995         buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
996         ipr_err("Vendor/Product ID: %s\n", buffer);
997
998         memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
999         buffer[IPR_SERIAL_NUM_LEN] = '\0';
1000         ipr_err("    Serial Number: %s\n", buffer);
1001 }
1002
1003 /**
1004  * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
1005  * @prefix:             string to print at start of printk
1006  * @hostrcb:    hostrcb pointer
1007  * @vpd:                vendor/product id/sn/wwn struct
1008  *
1009  * Return value:
1010  *      none
1011  **/
1012 static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1013                                     struct ipr_ext_vpd *vpd)
1014 {
1015         ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
1016         ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
1017                      be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
1018 }
1019
1020 /**
1021  * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
1022  * @vpd:                vendor/product id/sn/wwn struct
1023  *
1024  * Return value:
1025  *      none
1026  **/
1027 static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
1028 {
1029         ipr_log_vpd(&vpd->vpd);
1030         ipr_err("    WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
1031                 be32_to_cpu(vpd->wwid[1]));
1032 }
1033
1034 /**
1035  * ipr_log_enhanced_cache_error - Log a cache error.
1036  * @ioa_cfg:    ioa config struct
1037  * @hostrcb:    hostrcb struct
1038  *
1039  * Return value:
1040  *      none
1041  **/
1042 static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1043                                          struct ipr_hostrcb *hostrcb)
1044 {
1045         struct ipr_hostrcb_type_12_error *error =
1046                 &hostrcb->hcam.u.error.u.type_12_error;
1047
1048         ipr_err("-----Current Configuration-----\n");
1049         ipr_err("Cache Directory Card Information:\n");
1050         ipr_log_ext_vpd(&error->ioa_vpd);
1051         ipr_err("Adapter Card Information:\n");
1052         ipr_log_ext_vpd(&error->cfc_vpd);
1053
1054         ipr_err("-----Expected Configuration-----\n");
1055         ipr_err("Cache Directory Card Information:\n");
1056         ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
1057         ipr_err("Adapter Card Information:\n");
1058         ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
1059
1060         ipr_err("Additional IOA Data: %08X %08X %08X\n",
1061                      be32_to_cpu(error->ioa_data[0]),
1062                      be32_to_cpu(error->ioa_data[1]),
1063                      be32_to_cpu(error->ioa_data[2]));
1064 }
1065
1066 /**
1067  * ipr_log_cache_error - Log a cache error.
1068  * @ioa_cfg:    ioa config struct
1069  * @hostrcb:    hostrcb struct
1070  *
1071  * Return value:
1072  *      none
1073  **/
1074 static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1075                                 struct ipr_hostrcb *hostrcb)
1076 {
1077         struct ipr_hostrcb_type_02_error *error =
1078                 &hostrcb->hcam.u.error.u.type_02_error;
1079
1080         ipr_err("-----Current Configuration-----\n");
1081         ipr_err("Cache Directory Card Information:\n");
1082         ipr_log_vpd(&error->ioa_vpd);
1083         ipr_err("Adapter Card Information:\n");
1084         ipr_log_vpd(&error->cfc_vpd);
1085
1086         ipr_err("-----Expected Configuration-----\n");
1087         ipr_err("Cache Directory Card Information:\n");
1088         ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
1089         ipr_err("Adapter Card Information:\n");
1090         ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
1091
1092         ipr_err("Additional IOA Data: %08X %08X %08X\n",
1093                      be32_to_cpu(error->ioa_data[0]),
1094                      be32_to_cpu(error->ioa_data[1]),
1095                      be32_to_cpu(error->ioa_data[2]));
1096 }
1097
1098 /**
1099  * ipr_log_enhanced_config_error - Log a configuration error.
1100  * @ioa_cfg:    ioa config struct
1101  * @hostrcb:    hostrcb struct
1102  *
1103  * Return value:
1104  *      none
1105  **/
1106 static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
1107                                           struct ipr_hostrcb *hostrcb)
1108 {
1109         int errors_logged, i;
1110         struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
1111         struct ipr_hostrcb_type_13_error *error;
1112
1113         error = &hostrcb->hcam.u.error.u.type_13_error;
1114         errors_logged = be32_to_cpu(error->errors_logged);
1115
1116         ipr_err("Device Errors Detected/Logged: %d/%d\n",
1117                 be32_to_cpu(error->errors_detected), errors_logged);
1118
1119         dev_entry = error->dev;
1120
1121         for (i = 0; i < errors_logged; i++, dev_entry++) {
1122                 ipr_err_separator;
1123
1124                 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1125                 ipr_log_ext_vpd(&dev_entry->vpd);
1126
1127                 ipr_err("-----New Device Information-----\n");
1128                 ipr_log_ext_vpd(&dev_entry->new_vpd);
1129
1130                 ipr_err("Cache Directory Card Information:\n");
1131                 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1132
1133                 ipr_err("Adapter Card Information:\n");
1134                 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1135         }
1136 }
1137
1138 /**
1139  * ipr_log_config_error - Log a configuration error.
1140  * @ioa_cfg:    ioa config struct
1141  * @hostrcb:    hostrcb struct
1142  *
1143  * Return value:
1144  *      none
1145  **/
1146 static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
1147                                  struct ipr_hostrcb *hostrcb)
1148 {
1149         int errors_logged, i;
1150         struct ipr_hostrcb_device_data_entry *dev_entry;
1151         struct ipr_hostrcb_type_03_error *error;
1152
1153         error = &hostrcb->hcam.u.error.u.type_03_error;
1154         errors_logged = be32_to_cpu(error->errors_logged);
1155
1156         ipr_err("Device Errors Detected/Logged: %d/%d\n",
1157                 be32_to_cpu(error->errors_detected), errors_logged);
1158
1159         dev_entry = error->dev;
1160
1161         for (i = 0; i < errors_logged; i++, dev_entry++) {
1162                 ipr_err_separator;
1163
1164                 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1165                 ipr_log_vpd(&dev_entry->vpd);
1166
1167                 ipr_err("-----New Device Information-----\n");
1168                 ipr_log_vpd(&dev_entry->new_vpd);
1169
1170                 ipr_err("Cache Directory Card Information:\n");
1171                 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
1172
1173                 ipr_err("Adapter Card Information:\n");
1174                 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
1175
1176                 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1177                         be32_to_cpu(dev_entry->ioa_data[0]),
1178                         be32_to_cpu(dev_entry->ioa_data[1]),
1179                         be32_to_cpu(dev_entry->ioa_data[2]),
1180                         be32_to_cpu(dev_entry->ioa_data[3]),
1181                         be32_to_cpu(dev_entry->ioa_data[4]));
1182         }
1183 }
1184
1185 /**
1186  * ipr_log_enhanced_array_error - Log an array configuration error.
1187  * @ioa_cfg:    ioa config struct
1188  * @hostrcb:    hostrcb struct
1189  *
1190  * Return value:
1191  *      none
1192  **/
1193 static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
1194                                          struct ipr_hostrcb *hostrcb)
1195 {
1196         int i, num_entries;
1197         struct ipr_hostrcb_type_14_error *error;
1198         struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
1199         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1200
1201         error = &hostrcb->hcam.u.error.u.type_14_error;
1202
1203         ipr_err_separator;
1204
1205         ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1206                 error->protection_level,
1207                 ioa_cfg->host->host_no,
1208                 error->last_func_vset_res_addr.bus,
1209                 error->last_func_vset_res_addr.target,
1210                 error->last_func_vset_res_addr.lun);
1211
1212         ipr_err_separator;
1213
1214         array_entry = error->array_member;
1215         num_entries = min_t(u32, be32_to_cpu(error->num_entries),
1216                             sizeof(error->array_member));
1217
1218         for (i = 0; i < num_entries; i++, array_entry++) {
1219                 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1220                         continue;
1221
1222                 if (be32_to_cpu(error->exposed_mode_adn) == i)
1223                         ipr_err("Exposed Array Member %d:\n", i);
1224                 else
1225                         ipr_err("Array Member %d:\n", i);
1226
1227                 ipr_log_ext_vpd(&array_entry->vpd);
1228                 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1229                 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1230                                  "Expected Location");
1231
1232                 ipr_err_separator;
1233         }
1234 }
1235
1236 /**
1237  * ipr_log_array_error - Log an array configuration error.
1238  * @ioa_cfg:    ioa config struct
1239  * @hostrcb:    hostrcb struct
1240  *
1241  * Return value:
1242  *      none
1243  **/
1244 static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1245                                 struct ipr_hostrcb *hostrcb)
1246 {
1247         int i;
1248         struct ipr_hostrcb_type_04_error *error;
1249         struct ipr_hostrcb_array_data_entry *array_entry;
1250         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1251
1252         error = &hostrcb->hcam.u.error.u.type_04_error;
1253
1254         ipr_err_separator;
1255
1256         ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1257                 error->protection_level,
1258                 ioa_cfg->host->host_no,
1259                 error->last_func_vset_res_addr.bus,
1260                 error->last_func_vset_res_addr.target,
1261                 error->last_func_vset_res_addr.lun);
1262
1263         ipr_err_separator;
1264
1265         array_entry = error->array_member;
1266
1267         for (i = 0; i < 18; i++) {
1268                 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1269                         continue;
1270
1271                 if (be32_to_cpu(error->exposed_mode_adn) == i)
1272                         ipr_err("Exposed Array Member %d:\n", i);
1273                 else
1274                         ipr_err("Array Member %d:\n", i);
1275
1276                 ipr_log_vpd(&array_entry->vpd);
1277
1278                 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1279                 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1280                                  "Expected Location");
1281
1282                 ipr_err_separator;
1283
1284                 if (i == 9)
1285                         array_entry = error->array_member2;
1286                 else
1287                         array_entry++;
1288         }
1289 }
1290
1291 /**
1292  * ipr_log_hex_data - Log additional hex IOA error data.
1293  * @ioa_cfg:    ioa config struct
1294  * @data:               IOA error data
1295  * @len:                data length
1296  *
1297  * Return value:
1298  *      none
1299  **/
1300 static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
1301 {
1302         int i;
1303
1304         if (len == 0)
1305                 return;
1306
1307         if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
1308                 len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
1309
1310         for (i = 0; i < len / 4; i += 4) {
1311                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1312                         be32_to_cpu(data[i]),
1313                         be32_to_cpu(data[i+1]),
1314                         be32_to_cpu(data[i+2]),
1315                         be32_to_cpu(data[i+3]));
1316         }
1317 }
1318
1319 /**
1320  * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
1321  * @ioa_cfg:    ioa config struct
1322  * @hostrcb:    hostrcb struct
1323  *
1324  * Return value:
1325  *      none
1326  **/
1327 static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1328                                             struct ipr_hostrcb *hostrcb)
1329 {
1330         struct ipr_hostrcb_type_17_error *error;
1331
1332         error = &hostrcb->hcam.u.error.u.type_17_error;
1333         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1334         strstrip(error->failure_reason);
1335
1336         ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1337                      be32_to_cpu(hostrcb->hcam.u.error.prc));
1338         ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1339         ipr_log_hex_data(ioa_cfg, error->data,
1340                          be32_to_cpu(hostrcb->hcam.length) -
1341                          (offsetof(struct ipr_hostrcb_error, u) +
1342                           offsetof(struct ipr_hostrcb_type_17_error, data)));
1343 }
1344
1345 /**
1346  * ipr_log_dual_ioa_error - Log a dual adapter error.
1347  * @ioa_cfg:    ioa config struct
1348  * @hostrcb:    hostrcb struct
1349  *
1350  * Return value:
1351  *      none
1352  **/
1353 static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1354                                    struct ipr_hostrcb *hostrcb)
1355 {
1356         struct ipr_hostrcb_type_07_error *error;
1357
1358         error = &hostrcb->hcam.u.error.u.type_07_error;
1359         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1360         strstrip(error->failure_reason);
1361
1362         ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1363                      be32_to_cpu(hostrcb->hcam.u.error.prc));
1364         ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1365         ipr_log_hex_data(ioa_cfg, error->data,
1366                          be32_to_cpu(hostrcb->hcam.length) -
1367                          (offsetof(struct ipr_hostrcb_error, u) +
1368                           offsetof(struct ipr_hostrcb_type_07_error, data)));
1369 }
1370
1371 static const struct {
1372         u8 active;
1373         char *desc;
1374 } path_active_desc[] = {
1375         { IPR_PATH_NO_INFO, "Path" },
1376         { IPR_PATH_ACTIVE, "Active path" },
1377         { IPR_PATH_NOT_ACTIVE, "Inactive path" }
1378 };
1379
1380 static const struct {
1381         u8 state;
1382         char *desc;
1383 } path_state_desc[] = {
1384         { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
1385         { IPR_PATH_HEALTHY, "is healthy" },
1386         { IPR_PATH_DEGRADED, "is degraded" },
1387         { IPR_PATH_FAILED, "is failed" }
1388 };
1389
1390 /**
1391  * ipr_log_fabric_path - Log a fabric path error
1392  * @hostrcb:    hostrcb struct
1393  * @fabric:             fabric descriptor
1394  *
1395  * Return value:
1396  *      none
1397  **/
1398 static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
1399                                 struct ipr_hostrcb_fabric_desc *fabric)
1400 {
1401         int i, j;
1402         u8 path_state = fabric->path_state;
1403         u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1404         u8 state = path_state & IPR_PATH_STATE_MASK;
1405
1406         for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1407                 if (path_active_desc[i].active != active)
1408                         continue;
1409
1410                 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
1411                         if (path_state_desc[j].state != state)
1412                                 continue;
1413
1414                         if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
1415                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
1416                                              path_active_desc[i].desc, path_state_desc[j].desc,
1417                                              fabric->ioa_port);
1418                         } else if (fabric->cascaded_expander == 0xff) {
1419                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
1420                                              path_active_desc[i].desc, path_state_desc[j].desc,
1421                                              fabric->ioa_port, fabric->phy);
1422                         } else if (fabric->phy == 0xff) {
1423                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
1424                                              path_active_desc[i].desc, path_state_desc[j].desc,
1425                                              fabric->ioa_port, fabric->cascaded_expander);
1426                         } else {
1427                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
1428                                              path_active_desc[i].desc, path_state_desc[j].desc,
1429                                              fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1430                         }
1431                         return;
1432                 }
1433         }
1434
1435         ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
1436                 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1437 }
1438
1439 static const struct {
1440         u8 type;
1441         char *desc;
1442 } path_type_desc[] = {
1443         { IPR_PATH_CFG_IOA_PORT, "IOA port" },
1444         { IPR_PATH_CFG_EXP_PORT, "Expander port" },
1445         { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
1446         { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
1447 };
1448
1449 static const struct {
1450         u8 status;
1451         char *desc;
1452 } path_status_desc[] = {
1453         { IPR_PATH_CFG_NO_PROB, "Functional" },
1454         { IPR_PATH_CFG_DEGRADED, "Degraded" },
1455         { IPR_PATH_CFG_FAILED, "Failed" },
1456         { IPR_PATH_CFG_SUSPECT, "Suspect" },
1457         { IPR_PATH_NOT_DETECTED, "Missing" },
1458         { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
1459 };
1460
1461 static const char *link_rate[] = {
1462         "unknown",
1463         "disabled",
1464         "phy reset problem",
1465         "spinup hold",
1466         "port selector",
1467         "unknown",
1468         "unknown",
1469         "unknown",
1470         "1.5Gbps",
1471         "3.0Gbps",
1472         "unknown",
1473         "unknown",
1474         "unknown",
1475         "unknown",
1476         "unknown",
1477         "unknown"
1478 };
1479
1480 /**
1481  * ipr_log_path_elem - Log a fabric path element.
1482  * @hostrcb:    hostrcb struct
1483  * @cfg:                fabric path element struct
1484  *
1485  * Return value:
1486  *      none
1487  **/
1488 static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
1489                               struct ipr_hostrcb_config_element *cfg)
1490 {
1491         int i, j;
1492         u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
1493         u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
1494
1495         if (type == IPR_PATH_CFG_NOT_EXIST)
1496                 return;
1497
1498         for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
1499                 if (path_type_desc[i].type != type)
1500                         continue;
1501
1502                 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
1503                         if (path_status_desc[j].status != status)
1504                                 continue;
1505
1506                         if (type == IPR_PATH_CFG_IOA_PORT) {
1507                                 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
1508                                              path_status_desc[j].desc, path_type_desc[i].desc,
1509                                              cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1510                                              be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1511                         } else {
1512                                 if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
1513                                         ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
1514                                                      path_status_desc[j].desc, path_type_desc[i].desc,
1515                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1516                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1517                                 } else if (cfg->cascaded_expander == 0xff) {
1518                                         ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
1519                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
1520                                                      path_type_desc[i].desc, cfg->phy,
1521                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1522                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1523                                 } else if (cfg->phy == 0xff) {
1524                                         ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
1525                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
1526                                                      path_type_desc[i].desc, cfg->cascaded_expander,
1527                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1528                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1529                                 } else {
1530                                         ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
1531                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
1532                                                      path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
1533                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1534                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1535                                 }
1536                         }
1537                         return;
1538                 }
1539         }
1540
1541         ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
1542                      "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
1543                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1544                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1545 }
1546
1547 /**
1548  * ipr_log_fabric_error - Log a fabric error.
1549  * @ioa_cfg:    ioa config struct
1550  * @hostrcb:    hostrcb struct
1551  *
1552  * Return value:
1553  *      none
1554  **/
1555 static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
1556                                  struct ipr_hostrcb *hostrcb)
1557 {
1558         struct ipr_hostrcb_type_20_error *error;
1559         struct ipr_hostrcb_fabric_desc *fabric;
1560         struct ipr_hostrcb_config_element *cfg;
1561         int i, add_len;
1562
1563         error = &hostrcb->hcam.u.error.u.type_20_error;
1564         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1565         ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
1566
1567         add_len = be32_to_cpu(hostrcb->hcam.length) -
1568                 (offsetof(struct ipr_hostrcb_error, u) +
1569                  offsetof(struct ipr_hostrcb_type_20_error, desc));
1570
1571         for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
1572                 ipr_log_fabric_path(hostrcb, fabric);
1573                 for_each_fabric_cfg(fabric, cfg)
1574                         ipr_log_path_elem(hostrcb, cfg);
1575
1576                 add_len -= be16_to_cpu(fabric->length);
1577                 fabric = (struct ipr_hostrcb_fabric_desc *)
1578                         ((unsigned long)fabric + be16_to_cpu(fabric->length));
1579         }
1580
1581         ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
1582 }
1583
1584 /**
1585  * ipr_log_generic_error - Log an adapter error.
1586  * @ioa_cfg:    ioa config struct
1587  * @hostrcb:    hostrcb struct
1588  *
1589  * Return value:
1590  *      none
1591  **/
1592 static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1593                                   struct ipr_hostrcb *hostrcb)
1594 {
1595         ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
1596                          be32_to_cpu(hostrcb->hcam.length));
1597 }
1598
1599 /**
1600  * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1601  * @ioasc:      IOASC
1602  *
1603  * This function will return the index of into the ipr_error_table
1604  * for the specified IOASC. If the IOASC is not in the table,
1605  * 0 will be returned, which points to the entry used for unknown errors.
1606  *
1607  * Return value:
1608  *      index into the ipr_error_table
1609  **/
1610 static u32 ipr_get_error(u32 ioasc)
1611 {
1612         int i;
1613
1614         for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
1615                 if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
1616                         return i;
1617
1618         return 0;
1619 }
1620
1621 /**
1622  * ipr_handle_log_data - Log an adapter error.
1623  * @ioa_cfg:    ioa config struct
1624  * @hostrcb:    hostrcb struct
1625  *
1626  * This function logs an adapter error to the system.
1627  *
1628  * Return value:
1629  *      none
1630  **/
1631 static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1632                                 struct ipr_hostrcb *hostrcb)
1633 {
1634         u32 ioasc;
1635         int error_index;
1636
1637         if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1638                 return;
1639
1640         if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1641                 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1642
1643         ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1644
1645         if (ioasc == IPR_IOASC_BUS_WAS_RESET ||
1646             ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER) {
1647                 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
1648                 scsi_report_bus_reset(ioa_cfg->host,
1649                                       hostrcb->hcam.u.error.failing_dev_res_addr.bus);
1650         }
1651
1652         error_index = ipr_get_error(ioasc);
1653
1654         if (!ipr_error_table[error_index].log_hcam)
1655                 return;
1656
1657         ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
1658
1659         /* Set indication we have logged an error */
1660         ioa_cfg->errors_logged++;
1661
1662         if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
1663                 return;
1664         if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
1665                 hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
1666
1667         switch (hostrcb->hcam.overlay_id) {
1668         case IPR_HOST_RCB_OVERLAY_ID_2:
1669                 ipr_log_cache_error(ioa_cfg, hostrcb);
1670                 break;
1671         case IPR_HOST_RCB_OVERLAY_ID_3:
1672                 ipr_log_config_error(ioa_cfg, hostrcb);
1673                 break;
1674         case IPR_HOST_RCB_OVERLAY_ID_4:
1675         case IPR_HOST_RCB_OVERLAY_ID_6:
1676                 ipr_log_array_error(ioa_cfg, hostrcb);
1677                 break;
1678         case IPR_HOST_RCB_OVERLAY_ID_7:
1679                 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
1680                 break;
1681         case IPR_HOST_RCB_OVERLAY_ID_12:
1682                 ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
1683                 break;
1684         case IPR_HOST_RCB_OVERLAY_ID_13:
1685                 ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
1686                 break;
1687         case IPR_HOST_RCB_OVERLAY_ID_14:
1688         case IPR_HOST_RCB_OVERLAY_ID_16:
1689                 ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
1690                 break;
1691         case IPR_HOST_RCB_OVERLAY_ID_17:
1692                 ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
1693                 break;
1694         case IPR_HOST_RCB_OVERLAY_ID_20:
1695                 ipr_log_fabric_error(ioa_cfg, hostrcb);
1696                 break;
1697         case IPR_HOST_RCB_OVERLAY_ID_1:
1698         case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
1699         default:
1700                 ipr_log_generic_error(ioa_cfg, hostrcb);
1701                 break;
1702         }
1703 }
1704
1705 /**
1706  * ipr_process_error - Op done function for an adapter error log.
1707  * @ipr_cmd:    ipr command struct
1708  *
1709  * This function is the op done function for an error log host
1710  * controlled async from the adapter. It will log the error and
1711  * send the HCAM back to the adapter.
1712  *
1713  * Return value:
1714  *      none
1715  **/
1716 static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
1717 {
1718         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1719         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1720         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
1721         u32 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1722
1723         list_del(&hostrcb->queue);
1724         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1725
1726         if (!ioasc) {
1727                 ipr_handle_log_data(ioa_cfg, hostrcb);
1728                 if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
1729                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
1730         } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
1731                 dev_err(&ioa_cfg->pdev->dev,
1732                         "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1733         }
1734
1735         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
1736 }
1737
1738 /**
1739  * ipr_timeout -  An internally generated op has timed out.
1740  * @ipr_cmd:    ipr command struct
1741  *
1742  * This function blocks host requests and initiates an
1743  * adapter reset.
1744  *
1745  * Return value:
1746  *      none
1747  **/
1748 static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
1749 {
1750         unsigned long lock_flags = 0;
1751         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1752
1753         ENTER;
1754         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1755
1756         ioa_cfg->errors_logged++;
1757         dev_err(&ioa_cfg->pdev->dev,
1758                 "Adapter being reset due to command timeout.\n");
1759
1760         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1761                 ioa_cfg->sdt_state = GET_DUMP;
1762
1763         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
1764                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1765
1766         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1767         LEAVE;
1768 }
1769
1770 /**
1771  * ipr_oper_timeout -  Adapter timed out transitioning to operational
1772  * @ipr_cmd:    ipr command struct
1773  *
1774  * This function blocks host requests and initiates an
1775  * adapter reset.
1776  *
1777  * Return value:
1778  *      none
1779  **/
1780 static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
1781 {
1782         unsigned long lock_flags = 0;
1783         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1784
1785         ENTER;
1786         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1787
1788         ioa_cfg->errors_logged++;
1789         dev_err(&ioa_cfg->pdev->dev,
1790                 "Adapter timed out transitioning to operational.\n");
1791
1792         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1793                 ioa_cfg->sdt_state = GET_DUMP;
1794
1795         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
1796                 if (ipr_fastfail)
1797                         ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
1798                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1799         }
1800
1801         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1802         LEAVE;
1803 }
1804
1805 /**
1806  * ipr_reset_reload - Reset/Reload the IOA
1807  * @ioa_cfg:            ioa config struct
1808  * @shutdown_type:      shutdown type
1809  *
1810  * This function resets the adapter and re-initializes it.
1811  * This function assumes that all new host commands have been stopped.
1812  * Return value:
1813  *      SUCCESS / FAILED
1814  **/
1815 static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
1816                             enum ipr_shutdown_type shutdown_type)
1817 {
1818         if (!ioa_cfg->in_reset_reload)
1819                 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
1820
1821         spin_unlock_irq(ioa_cfg->host->host_lock);
1822         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
1823         spin_lock_irq(ioa_cfg->host->host_lock);
1824
1825         /* If we got hit with a host reset while we were already resetting
1826          the adapter for some reason, and the reset failed. */
1827         if (ioa_cfg->ioa_is_dead) {
1828                 ipr_trace;
1829                 return FAILED;
1830         }
1831
1832         return SUCCESS;
1833 }
1834
1835 /**
1836  * ipr_find_ses_entry - Find matching SES in SES table
1837  * @res:        resource entry struct of SES
1838  *
1839  * Return value:
1840  *      pointer to SES table entry / NULL on failure
1841  **/
1842 static const struct ipr_ses_table_entry *
1843 ipr_find_ses_entry(struct ipr_resource_entry *res)
1844 {
1845         int i, j, matches;
1846         const struct ipr_ses_table_entry *ste = ipr_ses_table;
1847
1848         for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
1849                 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
1850                         if (ste->compare_product_id_byte[j] == 'X') {
1851                                 if (res->cfgte.std_inq_data.vpids.product_id[j] == ste->product_id[j])
1852                                         matches++;
1853                                 else
1854                                         break;
1855                         } else
1856                                 matches++;
1857                 }
1858
1859                 if (matches == IPR_PROD_ID_LEN)
1860                         return ste;
1861         }
1862
1863         return NULL;
1864 }
1865
1866 /**
1867  * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
1868  * @ioa_cfg:    ioa config struct
1869  * @bus:                SCSI bus
1870  * @bus_width:  bus width
1871  *
1872  * Return value:
1873  *      SCSI bus speed in units of 100KHz, 1600 is 160 MHz
1874  *      For a 2-byte wide SCSI bus, the maximum transfer speed is
1875  *      twice the maximum transfer rate (e.g. for a wide enabled bus,
1876  *      max 160MHz = max 320MB/sec).
1877  **/
1878 static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
1879 {
1880         struct ipr_resource_entry *res;
1881         const struct ipr_ses_table_entry *ste;
1882         u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
1883
1884         /* Loop through each config table entry in the config table buffer */
1885         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1886                 if (!(IPR_IS_SES_DEVICE(res->cfgte.std_inq_data)))
1887                         continue;
1888
1889                 if (bus != res->cfgte.res_addr.bus)
1890                         continue;
1891
1892                 if (!(ste = ipr_find_ses_entry(res)))
1893                         continue;
1894
1895                 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
1896         }
1897
1898         return max_xfer_rate;
1899 }
1900
1901 /**
1902  * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
1903  * @ioa_cfg:            ioa config struct
1904  * @max_delay:          max delay in micro-seconds to wait
1905  *
1906  * Waits for an IODEBUG ACK from the IOA, doing busy looping.
1907  *
1908  * Return value:
1909  *      0 on success / other on failure
1910  **/
1911 static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
1912 {
1913         volatile u32 pcii_reg;
1914         int delay = 1;
1915
1916         /* Read interrupt reg until IOA signals IO Debug Acknowledge */
1917         while (delay < max_delay) {
1918                 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
1919
1920                 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
1921                         return 0;
1922
1923                 /* udelay cannot be used if delay is more than a few milliseconds */
1924                 if ((delay / 1000) > MAX_UDELAY_MS)
1925                         mdelay(delay / 1000);
1926                 else
1927                         udelay(delay);
1928
1929                 delay += delay;
1930         }
1931         return -EIO;
1932 }
1933
1934 /**
1935  * ipr_get_ldump_data_section - Dump IOA memory
1936  * @ioa_cfg:                    ioa config struct
1937  * @start_addr:                 adapter address to dump
1938  * @dest:                               destination kernel buffer
1939  * @length_in_words:    length to dump in 4 byte words
1940  *
1941  * Return value:
1942  *      0 on success / -EIO on failure
1943  **/
1944 static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
1945                                       u32 start_addr,
1946                                       __be32 *dest, u32 length_in_words)
1947 {
1948         volatile u32 temp_pcii_reg;
1949         int i, delay = 0;
1950
1951         /* Write IOA interrupt reg starting LDUMP state  */
1952         writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
1953                ioa_cfg->regs.set_uproc_interrupt_reg);
1954
1955         /* Wait for IO debug acknowledge */
1956         if (ipr_wait_iodbg_ack(ioa_cfg,
1957                                IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
1958                 dev_err(&ioa_cfg->pdev->dev,
1959                         "IOA dump long data transfer timeout\n");
1960                 return -EIO;
1961         }
1962
1963         /* Signal LDUMP interlocked - clear IO debug ack */
1964         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1965                ioa_cfg->regs.clr_interrupt_reg);
1966
1967         /* Write Mailbox with starting address */
1968         writel(start_addr, ioa_cfg->ioa_mailbox);
1969
1970         /* Signal address valid - clear IOA Reset alert */
1971         writel(IPR_UPROCI_RESET_ALERT,
1972                ioa_cfg->regs.clr_uproc_interrupt_reg);
1973
1974         for (i = 0; i < length_in_words; i++) {
1975                 /* Wait for IO debug acknowledge */
1976                 if (ipr_wait_iodbg_ack(ioa_cfg,
1977                                        IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
1978                         dev_err(&ioa_cfg->pdev->dev,
1979                                 "IOA dump short data transfer timeout\n");
1980                         return -EIO;
1981                 }
1982
1983                 /* Read data from mailbox and increment destination pointer */
1984                 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
1985                 dest++;
1986
1987                 /* For all but the last word of data, signal data received */
1988                 if (i < (length_in_words - 1)) {
1989                         /* Signal dump data received - Clear IO debug Ack */
1990                         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1991                                ioa_cfg->regs.clr_interrupt_reg);
1992                 }
1993         }
1994
1995         /* Signal end of block transfer. Set reset alert then clear IO debug ack */
1996         writel(IPR_UPROCI_RESET_ALERT,
1997                ioa_cfg->regs.set_uproc_interrupt_reg);
1998
1999         writel(IPR_UPROCI_IO_DEBUG_ALERT,
2000                ioa_cfg->regs.clr_uproc_interrupt_reg);
2001
2002         /* Signal dump data received - Clear IO debug Ack */
2003         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2004                ioa_cfg->regs.clr_interrupt_reg);
2005
2006         /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
2007         while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
2008                 temp_pcii_reg =
2009                     readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
2010
2011                 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
2012                         return 0;
2013
2014                 udelay(10);
2015                 delay += 10;
2016         }
2017
2018         return 0;
2019 }
2020
2021 #ifdef CONFIG_SCSI_IPR_DUMP
2022 /**
2023  * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
2024  * @ioa_cfg:            ioa config struct
2025  * @pci_address:        adapter address
2026  * @length:                     length of data to copy
2027  *
2028  * Copy data from PCI adapter to kernel buffer.
2029  * Note: length MUST be a 4 byte multiple
2030  * Return value:
2031  *      0 on success / other on failure
2032  **/
2033 static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
2034                         unsigned long pci_address, u32 length)
2035 {
2036         int bytes_copied = 0;
2037         int cur_len, rc, rem_len, rem_page_len;
2038         __be32 *page;
2039         unsigned long lock_flags = 0;
2040         struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
2041
2042         while (bytes_copied < length &&
2043                (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
2044                 if (ioa_dump->page_offset >= PAGE_SIZE ||
2045                     ioa_dump->page_offset == 0) {
2046                         page = (__be32 *)__get_free_page(GFP_ATOMIC);
2047
2048                         if (!page) {
2049                                 ipr_trace;
2050                                 return bytes_copied;
2051                         }
2052
2053                         ioa_dump->page_offset = 0;
2054                         ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
2055                         ioa_dump->next_page_index++;
2056                 } else
2057                         page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
2058
2059                 rem_len = length - bytes_copied;
2060                 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
2061                 cur_len = min(rem_len, rem_page_len);
2062
2063                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2064                 if (ioa_cfg->sdt_state == ABORT_DUMP) {
2065                         rc = -EIO;
2066                 } else {
2067                         rc = ipr_get_ldump_data_section(ioa_cfg,
2068                                                         pci_address + bytes_copied,
2069                                                         &page[ioa_dump->page_offset / 4],
2070                                                         (cur_len / sizeof(u32)));
2071                 }
2072                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2073
2074                 if (!rc) {
2075                         ioa_dump->page_offset += cur_len;
2076                         bytes_copied += cur_len;
2077                 } else {
2078                         ipr_trace;
2079                         break;
2080                 }
2081                 schedule();
2082         }
2083
2084         return bytes_copied;
2085 }
2086
2087 /**
2088  * ipr_init_dump_entry_hdr - Initialize a dump entry header.
2089  * @hdr:        dump entry header struct
2090  *
2091  * Return value:
2092  *      nothing
2093  **/
2094 static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
2095 {
2096         hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
2097         hdr->num_elems = 1;
2098         hdr->offset = sizeof(*hdr);
2099         hdr->status = IPR_DUMP_STATUS_SUCCESS;
2100 }
2101
2102 /**
2103  * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
2104  * @ioa_cfg:    ioa config struct
2105  * @driver_dump:        driver dump struct
2106  *
2107  * Return value:
2108  *      nothing
2109  **/
2110 static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
2111                                    struct ipr_driver_dump *driver_dump)
2112 {
2113         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2114
2115         ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
2116         driver_dump->ioa_type_entry.hdr.len =
2117                 sizeof(struct ipr_dump_ioa_type_entry) -
2118                 sizeof(struct ipr_dump_entry_header);
2119         driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2120         driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
2121         driver_dump->ioa_type_entry.type = ioa_cfg->type;
2122         driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
2123                 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
2124                 ucode_vpd->minor_release[1];
2125         driver_dump->hdr.num_entries++;
2126 }
2127
2128 /**
2129  * ipr_dump_version_data - Fill in the driver version in the dump.
2130  * @ioa_cfg:    ioa config struct
2131  * @driver_dump:        driver dump struct
2132  *
2133  * Return value:
2134  *      nothing
2135  **/
2136 static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
2137                                   struct ipr_driver_dump *driver_dump)
2138 {
2139         ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
2140         driver_dump->version_entry.hdr.len =
2141                 sizeof(struct ipr_dump_version_entry) -
2142                 sizeof(struct ipr_dump_entry_header);
2143         driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2144         driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
2145         strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
2146         driver_dump->hdr.num_entries++;
2147 }
2148
2149 /**
2150  * ipr_dump_trace_data - Fill in the IOA trace in the dump.
2151  * @ioa_cfg:    ioa config struct
2152  * @driver_dump:        driver dump struct
2153  *
2154  * Return value:
2155  *      nothing
2156  **/
2157 static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
2158                                    struct ipr_driver_dump *driver_dump)
2159 {
2160         ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
2161         driver_dump->trace_entry.hdr.len =
2162                 sizeof(struct ipr_dump_trace_entry) -
2163                 sizeof(struct ipr_dump_entry_header);
2164         driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2165         driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
2166         memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
2167         driver_dump->hdr.num_entries++;
2168 }
2169
2170 /**
2171  * ipr_dump_location_data - Fill in the IOA location in the dump.
2172  * @ioa_cfg:    ioa config struct
2173  * @driver_dump:        driver dump struct
2174  *
2175  * Return value:
2176  *      nothing
2177  **/
2178 static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
2179                                    struct ipr_driver_dump *driver_dump)
2180 {
2181         ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
2182         driver_dump->location_entry.hdr.len =
2183                 sizeof(struct ipr_dump_location_entry) -
2184                 sizeof(struct ipr_dump_entry_header);
2185         driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2186         driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
2187         strcpy(driver_dump->location_entry.location, ioa_cfg->pdev->dev.bus_id);
2188         driver_dump->hdr.num_entries++;
2189 }
2190
2191 /**
2192  * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
2193  * @ioa_cfg:    ioa config struct
2194  * @dump:               dump struct
2195  *
2196  * Return value:
2197  *      nothing
2198  **/
2199 static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
2200 {
2201         unsigned long start_addr, sdt_word;
2202         unsigned long lock_flags = 0;
2203         struct ipr_driver_dump *driver_dump = &dump->driver_dump;
2204         struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
2205         u32 num_entries, start_off, end_off;
2206         u32 bytes_to_copy, bytes_copied, rc;
2207         struct ipr_sdt *sdt;
2208         int i;
2209
2210         ENTER;
2211
2212         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2213
2214         if (ioa_cfg->sdt_state != GET_DUMP) {
2215                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2216                 return;
2217         }
2218
2219         start_addr = readl(ioa_cfg->ioa_mailbox);
2220
2221         if (!ipr_sdt_is_fmt2(start_addr)) {
2222                 dev_err(&ioa_cfg->pdev->dev,
2223                         "Invalid dump table format: %lx\n", start_addr);
2224                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2225                 return;
2226         }
2227
2228         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
2229
2230         driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
2231
2232         /* Initialize the overall dump header */
2233         driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
2234         driver_dump->hdr.num_entries = 1;
2235         driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
2236         driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
2237         driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
2238         driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
2239
2240         ipr_dump_version_data(ioa_cfg, driver_dump);
2241         ipr_dump_location_data(ioa_cfg, driver_dump);
2242         ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
2243         ipr_dump_trace_data(ioa_cfg, driver_dump);
2244
2245         /* Update dump_header */
2246         driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
2247
2248         /* IOA Dump entry */
2249         ipr_init_dump_entry_hdr(&ioa_dump->hdr);
2250         ioa_dump->format = IPR_SDT_FMT2;
2251         ioa_dump->hdr.len = 0;
2252         ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2253         ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
2254
2255         /* First entries in sdt are actually a list of dump addresses and
2256          lengths to gather the real dump data.  sdt represents the pointer
2257          to the ioa generated dump table.  Dump data will be extracted based
2258          on entries in this table */
2259         sdt = &ioa_dump->sdt;
2260
2261         rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
2262                                         sizeof(struct ipr_sdt) / sizeof(__be32));
2263
2264         /* Smart Dump table is ready to use and the first entry is valid */
2265         if (rc || (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE)) {
2266                 dev_err(&ioa_cfg->pdev->dev,
2267                         "Dump of IOA failed. Dump table not valid: %d, %X.\n",
2268                         rc, be32_to_cpu(sdt->hdr.state));
2269                 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
2270                 ioa_cfg->sdt_state = DUMP_OBTAINED;
2271                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2272                 return;
2273         }
2274
2275         num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
2276
2277         if (num_entries > IPR_NUM_SDT_ENTRIES)
2278                 num_entries = IPR_NUM_SDT_ENTRIES;
2279
2280         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2281
2282         for (i = 0; i < num_entries; i++) {
2283                 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
2284                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2285                         break;
2286                 }
2287
2288                 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
2289                         sdt_word = be32_to_cpu(sdt->entry[i].bar_str_offset);
2290                         start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
2291                         end_off = be32_to_cpu(sdt->entry[i].end_offset);
2292
2293                         if (ipr_sdt_is_fmt2(sdt_word) && sdt_word) {
2294                                 bytes_to_copy = end_off - start_off;
2295                                 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
2296                                         sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
2297                                         continue;
2298                                 }
2299
2300                                 /* Copy data from adapter to driver buffers */
2301                                 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
2302                                                             bytes_to_copy);
2303
2304                                 ioa_dump->hdr.len += bytes_copied;
2305
2306                                 if (bytes_copied != bytes_to_copy) {
2307                                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2308                                         break;
2309                                 }
2310                         }
2311                 }
2312         }
2313
2314         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
2315
2316         /* Update dump_header */
2317         driver_dump->hdr.len += ioa_dump->hdr.len;
2318         wmb();
2319         ioa_cfg->sdt_state = DUMP_OBTAINED;
2320         LEAVE;
2321 }
2322
2323 #else
2324 #define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
2325 #endif
2326
2327 /**
2328  * ipr_release_dump - Free adapter dump memory
2329  * @kref:       kref struct
2330  *
2331  * Return value:
2332  *      nothing
2333  **/
2334 static void ipr_release_dump(struct kref *kref)
2335 {
2336         struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
2337         struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
2338         unsigned long lock_flags = 0;
2339         int i;
2340
2341         ENTER;
2342         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2343         ioa_cfg->dump = NULL;
2344         ioa_cfg->sdt_state = INACTIVE;
2345         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2346
2347         for (i = 0; i < dump->ioa_dump.next_page_index; i++)
2348                 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
2349
2350         kfree(dump);
2351         LEAVE;
2352 }
2353
2354 /**
2355  * ipr_worker_thread - Worker thread
2356  * @work:               ioa config struct
2357  *
2358  * Called at task level from a work thread. This function takes care
2359  * of adding and removing device from the mid-layer as configuration
2360  * changes are detected by the adapter.
2361  *
2362  * Return value:
2363  *      nothing
2364  **/
2365 static void ipr_worker_thread(struct work_struct *work)
2366 {
2367         unsigned long lock_flags;
2368         struct ipr_resource_entry *res;
2369         struct scsi_device *sdev;
2370         struct ipr_dump *dump;
2371         struct ipr_ioa_cfg *ioa_cfg =
2372                 container_of(work, struct ipr_ioa_cfg, work_q);
2373         u8 bus, target, lun;
2374         int did_work;
2375
2376         ENTER;
2377         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2378
2379         if (ioa_cfg->sdt_state == GET_DUMP) {
2380                 dump = ioa_cfg->dump;
2381                 if (!dump) {
2382                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2383                         return;
2384                 }
2385                 kref_get(&dump->kref);
2386                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2387                 ipr_get_ioa_dump(ioa_cfg, dump);
2388                 kref_put(&dump->kref, ipr_release_dump);
2389
2390                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2391                 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
2392                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2393                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2394                 return;
2395         }
2396
2397 restart:
2398         do {
2399                 did_work = 0;
2400                 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
2401                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2402                         return;
2403                 }
2404
2405                 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2406                         if (res->del_from_ml && res->sdev) {
2407                                 did_work = 1;
2408                                 sdev = res->sdev;
2409                                 if (!scsi_device_get(sdev)) {
2410                                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
2411                                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2412                                         scsi_remove_device(sdev);
2413                                         scsi_device_put(sdev);
2414                                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2415                                 }
2416                                 break;
2417                         }
2418                 }
2419         } while(did_work);
2420
2421         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2422                 if (res->add_to_ml) {
2423                         bus = res->cfgte.res_addr.bus;
2424                         target = res->cfgte.res_addr.target;
2425                         lun = res->cfgte.res_addr.lun;
2426                         res->add_to_ml = 0;
2427                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2428                         scsi_add_device(ioa_cfg->host, bus, target, lun);
2429                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2430                         goto restart;
2431                 }
2432         }
2433
2434         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2435         kobject_uevent(&ioa_cfg->host->shost_dev.kobj, KOBJ_CHANGE);
2436         LEAVE;
2437 }
2438
2439 #ifdef CONFIG_SCSI_IPR_TRACE
2440 /**
2441  * ipr_read_trace - Dump the adapter trace
2442  * @kobj:               kobject struct
2443  * @bin_attr:           bin_attribute struct
2444  * @buf:                buffer
2445  * @off:                offset
2446  * @count:              buffer size
2447  *
2448  * Return value:
2449  *      number of bytes printed to buffer
2450  **/
2451 static ssize_t ipr_read_trace(struct kobject *kobj,
2452                               struct bin_attribute *bin_attr,
2453                               char *buf, loff_t off, size_t count)
2454 {
2455         struct device *dev = container_of(kobj, struct device, kobj);
2456         struct Scsi_Host *shost = class_to_shost(dev);
2457         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2458         unsigned long lock_flags = 0;
2459         ssize_t ret;
2460
2461         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2462         ret = memory_read_from_buffer(buf, count, &off, ioa_cfg->trace,
2463                                 IPR_TRACE_SIZE);
2464         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2465
2466         return ret;
2467 }
2468
2469 static struct bin_attribute ipr_trace_attr = {
2470         .attr = {
2471                 .name = "trace",
2472                 .mode = S_IRUGO,
2473         },
2474         .size = 0,
2475         .read = ipr_read_trace,
2476 };
2477 #endif
2478
2479 static const struct {
2480         enum ipr_cache_state state;
2481         char *name;
2482 } cache_state [] = {
2483         { CACHE_NONE, "none" },
2484         { CACHE_DISABLED, "disabled" },
2485         { CACHE_ENABLED, "enabled" }
2486 };
2487
2488 /**
2489  * ipr_show_write_caching - Show the write caching attribute
2490  * @dev:        device struct
2491  * @buf:        buffer
2492  *
2493  * Return value:
2494  *      number of bytes printed to buffer
2495  **/
2496 static ssize_t ipr_show_write_caching(struct device *dev,
2497                                       struct device_attribute *attr, char *buf)
2498 {
2499         struct Scsi_Host *shost = class_to_shost(dev);
2500         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2501         unsigned long lock_flags = 0;
2502         int i, len = 0;
2503
2504         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2505         for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2506                 if (cache_state[i].state == ioa_cfg->cache_state) {
2507                         len = snprintf(buf, PAGE_SIZE, "%s\n", cache_state[i].name);
2508                         break;
2509                 }
2510         }
2511         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2512         return len;
2513 }
2514
2515
2516 /**
2517  * ipr_store_write_caching - Enable/disable adapter write cache
2518  * @dev:        device struct
2519  * @buf:        buffer
2520  * @count:      buffer size
2521  *
2522  * This function will enable/disable adapter write cache.
2523  *
2524  * Return value:
2525  *      count on success / other on failure
2526  **/
2527 static ssize_t ipr_store_write_caching(struct device *dev,
2528                                        struct device_attribute *attr,
2529                                        const char *buf, size_t count)
2530 {
2531         struct Scsi_Host *shost = class_to_shost(dev);
2532         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2533         unsigned long lock_flags = 0;
2534         enum ipr_cache_state new_state = CACHE_INVALID;
2535         int i;
2536
2537         if (!capable(CAP_SYS_ADMIN))
2538                 return -EACCES;
2539         if (ioa_cfg->cache_state == CACHE_NONE)
2540                 return -EINVAL;
2541
2542         for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2543                 if (!strncmp(cache_state[i].name, buf, strlen(cache_state[i].name))) {
2544                         new_state = cache_state[i].state;
2545                         break;
2546                 }
2547         }
2548
2549         if (new_state != CACHE_DISABLED && new_state != CACHE_ENABLED)
2550                 return -EINVAL;
2551
2552         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2553         if (ioa_cfg->cache_state == new_state) {
2554                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2555                 return count;
2556         }
2557
2558         ioa_cfg->cache_state = new_state;
2559         dev_info(&ioa_cfg->pdev->dev, "%s adapter write cache.\n",
2560                  new_state == CACHE_ENABLED ? "Enabling" : "Disabling");
2561         if (!ioa_cfg->in_reset_reload)
2562                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2563         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2564         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2565
2566         return count;
2567 }
2568
2569 static struct device_attribute ipr_ioa_cache_attr = {
2570         .attr = {
2571                 .name =         "write_cache",
2572                 .mode =         S_IRUGO | S_IWUSR,
2573         },
2574         .show = ipr_show_write_caching,
2575         .store = ipr_store_write_caching
2576 };
2577
2578 /**
2579  * ipr_show_fw_version - Show the firmware version
2580  * @dev:        class device struct
2581  * @buf:        buffer
2582  *
2583  * Return value:
2584  *      number of bytes printed to buffer
2585  **/
2586 static ssize_t ipr_show_fw_version(struct device *dev,
2587                                    struct device_attribute *attr, char *buf)
2588 {
2589         struct Scsi_Host *shost = class_to_shost(dev);
2590         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2591         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2592         unsigned long lock_flags = 0;
2593         int len;
2594
2595         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2596         len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
2597                        ucode_vpd->major_release, ucode_vpd->card_type,
2598                        ucode_vpd->minor_release[0],
2599                        ucode_vpd->minor_release[1]);
2600         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2601         return len;
2602 }
2603
2604 static struct device_attribute ipr_fw_version_attr = {
2605         .attr = {
2606                 .name =         "fw_version",
2607                 .mode =         S_IRUGO,
2608         },
2609         .show = ipr_show_fw_version,
2610 };
2611
2612 /**
2613  * ipr_show_log_level - Show the adapter's error logging level
2614  * @dev:        class device struct
2615  * @buf:        buffer
2616  *
2617  * Return value:
2618  *      number of bytes printed to buffer
2619  **/
2620 static ssize_t ipr_show_log_level(struct device *dev,
2621                                    struct device_attribute *attr, char *buf)
2622 {
2623         struct Scsi_Host *shost = class_to_shost(dev);
2624         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2625         unsigned long lock_flags = 0;
2626         int len;
2627
2628         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2629         len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
2630         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2631         return len;
2632 }
2633
2634 /**
2635  * ipr_store_log_level - Change the adapter's error logging level
2636  * @dev:        class device struct
2637  * @buf:        buffer
2638  *
2639  * Return value:
2640  *      number of bytes printed to buffer
2641  **/
2642 static ssize_t ipr_store_log_level(struct device *dev,
2643                                    struct device_attribute *attr,
2644                                    const char *buf, size_t count)
2645 {
2646         struct Scsi_Host *shost = class_to_shost(dev);
2647         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2648         unsigned long lock_flags = 0;
2649
2650         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2651         ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
2652         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2653         return strlen(buf);
2654 }
2655
2656 static struct device_attribute ipr_log_level_attr = {
2657         .attr = {
2658                 .name =         "log_level",
2659                 .mode =         S_IRUGO | S_IWUSR,
2660         },
2661         .show = ipr_show_log_level,
2662         .store = ipr_store_log_level
2663 };
2664
2665 /**
2666  * ipr_store_diagnostics - IOA Diagnostics interface
2667  * @dev:        device struct
2668  * @buf:        buffer
2669  * @count:      buffer size
2670  *
2671  * This function will reset the adapter and wait a reasonable
2672  * amount of time for any errors that the adapter might log.
2673  *
2674  * Return value:
2675  *      count on success / other on failure
2676  **/
2677 static ssize_t ipr_store_diagnostics(struct device *dev,
2678                                      struct device_attribute *attr,
2679                                      const char *buf, size_t count)
2680 {
2681         struct Scsi_Host *shost = class_to_shost(dev);
2682         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2683         unsigned long lock_flags = 0;
2684         int rc = count;
2685
2686         if (!capable(CAP_SYS_ADMIN))
2687                 return -EACCES;
2688
2689         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2690         while(ioa_cfg->in_reset_reload) {
2691                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2692                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2693                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2694         }
2695
2696         ioa_cfg->errors_logged = 0;
2697         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2698
2699         if (ioa_cfg->in_reset_reload) {
2700                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2701                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2702
2703                 /* Wait for a second for any errors to be logged */
2704                 msleep(1000);
2705         } else {
2706                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2707                 return -EIO;
2708         }
2709
2710         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2711         if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
2712                 rc = -EIO;
2713         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2714
2715         return rc;
2716 }
2717
2718 static struct device_attribute ipr_diagnostics_attr = {
2719         .attr = {
2720                 .name =         "run_diagnostics",
2721                 .mode =         S_IWUSR,
2722         },
2723         .store = ipr_store_diagnostics
2724 };
2725
2726 /**
2727  * ipr_show_adapter_state - Show the adapter's state
2728  * @class_dev:  device struct
2729  * @buf:        buffer
2730  *
2731  * Return value:
2732  *      number of bytes printed to buffer
2733  **/
2734 static ssize_t ipr_show_adapter_state(struct device *dev,
2735                                       struct device_attribute *attr, char *buf)
2736 {
2737         struct Scsi_Host *shost = class_to_shost(dev);
2738         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2739         unsigned long lock_flags = 0;
2740         int len;
2741
2742         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2743         if (ioa_cfg->ioa_is_dead)
2744                 len = snprintf(buf, PAGE_SIZE, "offline\n");
2745         else
2746                 len = snprintf(buf, PAGE_SIZE, "online\n");
2747         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2748         return len;
2749 }
2750
2751 /**
2752  * ipr_store_adapter_state - Change adapter state
2753  * @dev:        device struct
2754  * @buf:        buffer
2755  * @count:      buffer size
2756  *
2757  * This function will change the adapter's state.
2758  *
2759  * Return value:
2760  *      count on success / other on failure
2761  **/
2762 static ssize_t ipr_store_adapter_state(struct device *dev,
2763                                        struct device_attribute *attr,
2764                                        const char *buf, size_t count)
2765 {
2766         struct Scsi_Host *shost = class_to_shost(dev);
2767         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2768         unsigned long lock_flags;
2769         int result = count;
2770
2771         if (!capable(CAP_SYS_ADMIN))
2772                 return -EACCES;
2773
2774         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2775         if (ioa_cfg->ioa_is_dead && !strncmp(buf, "online", 6)) {
2776                 ioa_cfg->ioa_is_dead = 0;
2777                 ioa_cfg->reset_retries = 0;
2778                 ioa_cfg->in_ioa_bringdown = 0;
2779                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2780         }
2781         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2782         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2783
2784         return result;
2785 }
2786
2787 static struct device_attribute ipr_ioa_state_attr = {
2788         .attr = {
2789                 .name =         "online_state",
2790                 .mode =         S_IRUGO | S_IWUSR,
2791         },
2792         .show = ipr_show_adapter_state,
2793         .store = ipr_store_adapter_state
2794 };
2795
2796 /**
2797  * ipr_store_reset_adapter - Reset the adapter
2798  * @dev:        device struct
2799  * @buf:        buffer
2800  * @count:      buffer size
2801  *
2802  * This function will reset the adapter.
2803  *
2804  * Return value:
2805  *      count on success / other on failure
2806  **/
2807 static ssize_t ipr_store_reset_adapter(struct device *dev,
2808                                        struct device_attribute *attr,
2809                                        const char *buf, size_t count)
2810 {
2811         struct Scsi_Host *shost = class_to_shost(dev);
2812         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2813         unsigned long lock_flags;
2814         int result = count;
2815
2816         if (!capable(CAP_SYS_ADMIN))
2817                 return -EACCES;
2818
2819         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2820         if (!ioa_cfg->in_reset_reload)
2821                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2822         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2823         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2824
2825         return result;
2826 }
2827
2828 static struct device_attribute ipr_ioa_reset_attr = {
2829         .attr = {
2830                 .name =         "reset_host",
2831                 .mode =         S_IWUSR,
2832         },
2833         .store = ipr_store_reset_adapter
2834 };
2835
2836 /**
2837  * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
2838  * @buf_len:            buffer length
2839  *
2840  * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
2841  * list to use for microcode download
2842  *
2843  * Return value:
2844  *      pointer to sglist / NULL on failure
2845  **/
2846 static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
2847 {
2848         int sg_size, order, bsize_elem, num_elem, i, j;
2849         struct ipr_sglist *sglist;
2850         struct scatterlist *scatterlist;
2851         struct page *page;
2852
2853         /* Get the minimum size per scatter/gather element */
2854         sg_size = buf_len / (IPR_MAX_SGLIST - 1);
2855
2856         /* Get the actual size per element */
2857         order = get_order(sg_size);
2858
2859         /* Determine the actual number of bytes per element */
2860         bsize_elem = PAGE_SIZE * (1 << order);
2861
2862         /* Determine the actual number of sg entries needed */
2863         if (buf_len % bsize_elem)
2864                 num_elem = (buf_len / bsize_elem) + 1;
2865         else
2866                 num_elem = buf_len / bsize_elem;
2867
2868         /* Allocate a scatter/gather list for the DMA */
2869         sglist = kzalloc(sizeof(struct ipr_sglist) +
2870                          (sizeof(struct scatterlist) * (num_elem - 1)),
2871                          GFP_KERNEL);
2872
2873         if (sglist == NULL) {
2874                 ipr_trace;
2875                 return NULL;
2876         }
2877
2878         scatterlist = sglist->scatterlist;
2879         sg_init_table(scatterlist, num_elem);
2880
2881         sglist->order = order;
2882         sglist->num_sg = num_elem;
2883
2884         /* Allocate a bunch of sg elements */
2885         for (i = 0; i < num_elem; i++) {
2886                 page = alloc_pages(GFP_KERNEL, order);
2887                 if (!page) {
2888                         ipr_trace;
2889
2890                         /* Free up what we already allocated */
2891                         for (j = i - 1; j >= 0; j--)
2892                                 __free_pages(sg_page(&scatterlist[j]), order);
2893                         kfree(sglist);
2894                         return NULL;
2895                 }
2896
2897                 sg_set_page(&scatterlist[i], page, 0, 0);
2898         }
2899
2900         return sglist;
2901 }
2902
2903 /**
2904  * ipr_free_ucode_buffer - Frees a microcode download buffer
2905  * @p_dnld:             scatter/gather list pointer
2906  *
2907  * Free a DMA'able ucode download buffer previously allocated with
2908  * ipr_alloc_ucode_buffer
2909  *
2910  * Return value:
2911  *      nothing
2912  **/
2913 static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
2914 {
2915         int i;
2916
2917         for (i = 0; i < sglist->num_sg; i++)
2918                 __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
2919
2920         kfree(sglist);
2921 }
2922
2923 /**
2924  * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
2925  * @sglist:             scatter/gather list pointer
2926  * @buffer:             buffer pointer
2927  * @len:                buffer length
2928  *
2929  * Copy a microcode image from a user buffer into a buffer allocated by
2930  * ipr_alloc_ucode_buffer
2931  *
2932  * Return value:
2933  *      0 on success / other on failure
2934  **/
2935 static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
2936                                  u8 *buffer, u32 len)
2937 {
2938         int bsize_elem, i, result = 0;
2939         struct scatterlist *scatterlist;
2940         void *kaddr;
2941
2942         /* Determine the actual number of bytes per element */
2943         bsize_elem = PAGE_SIZE * (1 << sglist->order);
2944
2945         scatterlist = sglist->scatterlist;
2946
2947         for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
2948                 struct page *page = sg_page(&scatterlist[i]);
2949
2950                 kaddr = kmap(page);
2951                 memcpy(kaddr, buffer, bsize_elem);
2952                 kunmap(page);
2953
2954                 scatterlist[i].length = bsize_elem;
2955
2956                 if (result != 0) {
2957                         ipr_trace;
2958                         return result;
2959                 }
2960         }
2961
2962         if (len % bsize_elem) {
2963                 struct page *page = sg_page(&scatterlist[i]);
2964
2965                 kaddr = kmap(page);
2966                 memcpy(kaddr, buffer, len % bsize_elem);
2967                 kunmap(page);
2968
2969                 scatterlist[i].length = len % bsize_elem;
2970         }
2971
2972         sglist->buffer_len = len;
2973         return result;
2974 }
2975
2976 /**
2977  * ipr_build_ucode_ioadl - Build a microcode download IOADL
2978  * @ipr_cmd:    ipr command struct
2979  * @sglist:             scatter/gather list
2980  *
2981  * Builds a microcode download IOA data list (IOADL).
2982  *
2983  **/
2984 static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
2985                                   struct ipr_sglist *sglist)
2986 {
2987         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
2988         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
2989         struct scatterlist *scatterlist = sglist->scatterlist;
2990         int i;
2991
2992         ipr_cmd->dma_use_sg = sglist->num_dma_sg;
2993         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
2994         ioarcb->write_data_transfer_length = cpu_to_be32(sglist->buffer_len);
2995         ioarcb->write_ioadl_len =
2996                 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
2997
2998         for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
2999                 ioadl[i].flags_and_data_len =
3000                         cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
3001                 ioadl[i].address =
3002                         cpu_to_be32(sg_dma_address(&scatterlist[i]));
3003         }
3004
3005         ioadl[i-1].flags_and_data_len |=
3006                 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3007 }
3008
3009 /**
3010  * ipr_update_ioa_ucode - Update IOA's microcode
3011  * @ioa_cfg:    ioa config struct
3012  * @sglist:             scatter/gather list
3013  *
3014  * Initiate an adapter reset to update the IOA's microcode
3015  *
3016  * Return value:
3017  *      0 on success / -EIO on failure
3018  **/
3019 static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
3020                                 struct ipr_sglist *sglist)
3021 {
3022         unsigned long lock_flags;
3023
3024         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3025         while(ioa_cfg->in_reset_reload) {
3026                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3027                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3028                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3029         }
3030
3031         if (ioa_cfg->ucode_sglist) {
3032                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3033                 dev_err(&ioa_cfg->pdev->dev,
3034                         "Microcode download already in progress\n");
3035                 return -EIO;
3036         }
3037
3038         sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
3039                                         sglist->num_sg, DMA_TO_DEVICE);
3040
3041         if (!sglist->num_dma_sg) {
3042                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3043                 dev_err(&ioa_cfg->pdev->dev,
3044                         "Failed to map microcode download buffer!\n");
3045                 return -EIO;
3046         }
3047
3048         ioa_cfg->ucode_sglist = sglist;
3049         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3050         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3051         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3052
3053         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3054         ioa_cfg->ucode_sglist = NULL;
3055         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3056         return 0;
3057 }
3058
3059 /**
3060  * ipr_store_update_fw - Update the firmware on the adapter
3061  * @class_dev:  device struct
3062  * @buf:        buffer
3063  * @count:      buffer size
3064  *
3065  * This function will update the firmware on the adapter.
3066  *
3067  * Return value:
3068  *      count on success / other on failure
3069  **/
3070 static ssize_t ipr_store_update_fw(struct device *dev,
3071                                    struct device_attribute *attr,
3072                                    const char *buf, size_t count)
3073 {
3074         struct Scsi_Host *shost = class_to_shost(dev);
3075         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3076         struct ipr_ucode_image_header *image_hdr;
3077         const struct firmware *fw_entry;
3078         struct ipr_sglist *sglist;
3079         char fname[100];
3080         char *src;
3081         int len, result, dnld_size;
3082
3083         if (!capable(CAP_SYS_ADMIN))
3084                 return -EACCES;
3085
3086         len = snprintf(fname, 99, "%s", buf);
3087         fname[len-1] = '\0';
3088
3089         if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
3090                 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
3091                 return -EIO;
3092         }
3093
3094         image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
3095
3096         if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
3097             (ioa_cfg->vpd_cbs->page3_data.card_type &&
3098              ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
3099                 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
3100                 release_firmware(fw_entry);
3101                 return -EINVAL;
3102         }
3103
3104         src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
3105         dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
3106         sglist = ipr_alloc_ucode_buffer(dnld_size);
3107
3108         if (!sglist) {
3109                 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
3110                 release_firmware(fw_entry);
3111                 return -ENOMEM;
3112         }
3113
3114         result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
3115
3116         if (result) {
3117                 dev_err(&ioa_cfg->pdev->dev,
3118                         "Microcode buffer copy to DMA buffer failed\n");
3119                 goto out;
3120         }
3121
3122         result = ipr_update_ioa_ucode(ioa_cfg, sglist);
3123
3124         if (!result)
3125                 result = count;
3126 out:
3127         ipr_free_ucode_buffer(sglist);
3128         release_firmware(fw_entry);
3129         return result;
3130 }
3131
3132 static struct device_attribute ipr_update_fw_attr = {
3133         .attr = {
3134                 .name =         "update_fw",
3135                 .mode =         S_IWUSR,
3136         },
3137         .store = ipr_store_update_fw
3138 };
3139
3140 static struct device_attribute *ipr_ioa_attrs[] = {
3141         &ipr_fw_version_attr,
3142         &ipr_log_level_attr,
3143         &ipr_diagnostics_attr,
3144         &ipr_ioa_state_attr,
3145         &ipr_ioa_reset_attr,
3146         &ipr_update_fw_attr,
3147         &ipr_ioa_cache_attr,
3148         NULL,
3149 };
3150
3151 #ifdef CONFIG_SCSI_IPR_DUMP
3152 /**
3153  * ipr_read_dump - Dump the adapter
3154  * @kobj:               kobject struct
3155  * @bin_attr:           bin_attribute struct
3156  * @buf:                buffer
3157  * @off:                offset
3158  * @count:              buffer size
3159  *
3160  * Return value:
3161  *      number of bytes printed to buffer
3162  **/
3163 static ssize_t ipr_read_dump(struct kobject *kobj,
3164                              struct bin_attribute *bin_attr,
3165                              char *buf, loff_t off, size_t count)
3166 {
3167         struct device *cdev = container_of(kobj, struct device, kobj);
3168         struct Scsi_Host *shost = class_to_shost(cdev);
3169         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3170         struct ipr_dump *dump;
3171         unsigned long lock_flags = 0;
3172         char *src;
3173         int len;
3174         size_t rc = count;
3175
3176         if (!capable(CAP_SYS_ADMIN))
3177                 return -EACCES;
3178
3179         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3180         dump = ioa_cfg->dump;
3181
3182         if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
3183                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3184                 return 0;
3185         }
3186         kref_get(&dump->kref);
3187         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3188
3189         if (off > dump->driver_dump.hdr.len) {
3190                 kref_put(&dump->kref, ipr_release_dump);
3191                 return 0;
3192         }
3193
3194         if (off + count > dump->driver_dump.hdr.len) {
3195                 count = dump->driver_dump.hdr.len - off;
3196                 rc = count;
3197         }
3198
3199         if (count && off < sizeof(dump->driver_dump)) {
3200                 if (off + count > sizeof(dump->driver_dump))
3201                         len = sizeof(dump->driver_dump) - off;
3202                 else
3203                         len = count;
3204                 src = (u8 *)&dump->driver_dump + off;
3205                 memcpy(buf, src, len);
3206                 buf += len;
3207                 off += len;
3208                 count -= len;
3209         }
3210
3211         off -= sizeof(dump->driver_dump);
3212
3213         if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
3214                 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
3215                         len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
3216                 else
3217                         len = count;
3218                 src = (u8 *)&dump->ioa_dump + off;
3219                 memcpy(buf, src, len);
3220                 buf += len;
3221                 off += len;
3222                 count -= len;
3223         }
3224
3225         off -= offsetof(struct ipr_ioa_dump, ioa_data);
3226
3227         while (count) {
3228                 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
3229                         len = PAGE_ALIGN(off) - off;
3230                 else
3231                         len = count;
3232                 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
3233                 src += off & ~PAGE_MASK;
3234                 memcpy(buf, src, len);
3235                 buf += len;
3236                 off += len;
3237                 count -= len;
3238         }
3239
3240         kref_put(&dump->kref, ipr_release_dump);
3241         return rc;
3242 }
3243
3244 /**
3245  * ipr_alloc_dump - Prepare for adapter dump
3246  * @ioa_cfg:    ioa config struct
3247  *
3248  * Return value:
3249  *      0 on success / other on failure
3250  **/
3251 static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
3252 {
3253         struct ipr_dump *dump;
3254         unsigned long lock_flags = 0;
3255
3256         dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
3257
3258         if (!dump) {
3259                 ipr_err("Dump memory allocation failed\n");
3260                 return -ENOMEM;
3261         }
3262
3263         kref_init(&dump->kref);
3264         dump->ioa_cfg = ioa_cfg;
3265
3266         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3267
3268         if (INACTIVE != ioa_cfg->sdt_state) {
3269                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3270                 kfree(dump);
3271                 return 0;
3272         }
3273
3274         ioa_cfg->dump = dump;
3275         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
3276         if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
3277                 ioa_cfg->dump_taken = 1;
3278                 schedule_work(&ioa_cfg->work_q);
3279         }
3280         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3281
3282         return 0;
3283 }
3284
3285 /**
3286  * ipr_free_dump - Free adapter dump memory
3287  * @ioa_cfg:    ioa config struct
3288  *
3289  * Return value:
3290  *      0 on success / other on failure
3291  **/
3292 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
3293 {
3294         struct ipr_dump *dump;
3295         unsigned long lock_flags = 0;
3296
3297         ENTER;
3298
3299         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3300         dump = ioa_cfg->dump;
3301         if (!dump) {
3302                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3303                 return 0;
3304         }
3305
3306         ioa_cfg->dump = NULL;
3307         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3308
3309         kref_put(&dump->kref, ipr_release_dump);
3310
3311         LEAVE;
3312         return 0;
3313 }
3314
3315 /**
3316  * ipr_write_dump - Setup dump state of adapter
3317  * @kobj:               kobject struct
3318  * @bin_attr:           bin_attribute struct
3319  * @buf:                buffer
3320  * @off:                offset
3321  * @count:              buffer size
3322  *
3323  * Return value:
3324  *      number of bytes printed to buffer
3325  **/
3326 static ssize_t ipr_write_dump(struct kobject *kobj,
3327                               struct bin_attribute *bin_attr,
3328                               char *buf, loff_t off, size_t count)
3329 {
3330         struct device *cdev = container_of(kobj, struct device, kobj);
3331         struct Scsi_Host *shost = class_to_shost(cdev);
3332         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3333         int rc;
3334
3335         if (!capable(CAP_SYS_ADMIN))
3336                 return -EACCES;
3337
3338         if (buf[0] == '1')
3339                 rc = ipr_alloc_dump(ioa_cfg);
3340         else if (buf[0] == '0')
3341                 rc = ipr_free_dump(ioa_cfg);
3342         else
3343                 return -EINVAL;
3344
3345         if (rc)
3346                 return rc;
3347         else
3348                 return count;
3349 }
3350
3351 static struct bin_attribute ipr_dump_attr = {
3352         .attr = {
3353                 .name = "dump",
3354                 .mode = S_IRUSR | S_IWUSR,
3355         },
3356         .size = 0,
3357         .read = ipr_read_dump,
3358         .write = ipr_write_dump
3359 };
3360 #else
3361 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
3362 #endif
3363
3364 /**
3365  * ipr_change_queue_depth - Change the device's queue depth
3366  * @sdev:       scsi device struct
3367  * @qdepth:     depth to set
3368  *
3369  * Return value:
3370  *      actual depth set
3371  **/
3372 static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
3373 {
3374         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3375         struct ipr_resource_entry *res;
3376         unsigned long lock_flags = 0;
3377
3378         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3379         res = (struct ipr_resource_entry *)sdev->hostdata;
3380
3381         if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
3382                 qdepth = IPR_MAX_CMD_PER_ATA_LUN;
3383         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3384
3385         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3386         return sdev->queue_depth;
3387 }
3388
3389 /**
3390  * ipr_change_queue_type - Change the device's queue type
3391  * @dsev:               scsi device struct
3392  * @tag_type:   type of tags to use
3393  *
3394  * Return value:
3395  *      actual queue type set
3396  **/
3397 static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
3398 {
3399         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3400         struct ipr_resource_entry *res;
3401         unsigned long lock_flags = 0;
3402
3403         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3404         res = (struct ipr_resource_entry *)sdev->hostdata;
3405
3406         if (res) {
3407                 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
3408                         /*
3409                          * We don't bother quiescing the device here since the
3410                          * adapter firmware does it for us.
3411                          */
3412                         scsi_set_tag_type(sdev, tag_type);
3413
3414                         if (tag_type)
3415                                 scsi_activate_tcq(sdev, sdev->queue_depth);
3416                         else
3417                                 scsi_deactivate_tcq(sdev, sdev->queue_depth);
3418                 } else
3419                         tag_type = 0;
3420         } else
3421                 tag_type = 0;
3422
3423         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3424         return tag_type;
3425 }
3426
3427 /**
3428  * ipr_show_adapter_handle - Show the adapter's resource handle for this device
3429  * @dev:        device struct