Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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_USE_LSI, &ipr_chip_cfg[0] },
135         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, IPR_USE_LSI, &ipr_chip_cfg[0] },
136         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, IPR_USE_LSI, &ipr_chip_cfg[0] },
137         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, IPR_USE_LSI, &ipr_chip_cfg[0] },
138         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, IPR_USE_MSI, &ipr_chip_cfg[0] },
139         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, IPR_USE_LSI, &ipr_chip_cfg[1] },
140         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, IPR_USE_LSI, &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, S_IRUGO | S_IWUSR);
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, S_IRUGO | S_IWUSR);
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         {0x06690000, 0, IPR_DEFAULT_LOG_LEVEL,
358         "4070: Logically bad block written on device"},
359         {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
360         "9041: Array protection temporarily suspended"},
361         {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
362         "9042: Corrupt array parity detected on specified device"},
363         {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
364         "9030: Array no longer protected due to missing or failed disk unit"},
365         {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
366         "9071: Link operational transition"},
367         {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
368         "9072: Link not operational transition"},
369         {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
370         "9032: Array exposed but still protected"},
371         {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
372         "70DD: Device forced failed by disrupt device command"},
373         {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
374         "4061: Multipath redundancy level got better"},
375         {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
376         "4060: Multipath redundancy level got worse"},
377         {0x07270000, 0, 0,
378         "Failure due to other device"},
379         {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
380         "9008: IOA does not support functions expected by devices"},
381         {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
382         "9010: Cache data associated with attached devices cannot be found"},
383         {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
384         "9011: Cache data belongs to devices other than those attached"},
385         {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
386         "9020: Array missing 2 or more devices with only 1 device present"},
387         {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
388         "9021: Array missing 2 or more devices with 2 or more devices present"},
389         {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
390         "9022: Exposed array is missing a required device"},
391         {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
392         "9023: Array member(s) not at required physical locations"},
393         {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
394         "9024: Array not functional due to present hardware configuration"},
395         {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
396         "9026: Array not functional due to present hardware configuration"},
397         {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
398         "9027: Array is missing a device and parity is out of sync"},
399         {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
400         "9028: Maximum number of arrays already exist"},
401         {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
402         "9050: Required cache data cannot be located for a disk unit"},
403         {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
404         "9052: Cache data exists for a device that has been modified"},
405         {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
406         "9054: IOA resources not available due to previous problems"},
407         {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
408         "9092: Disk unit requires initialization before use"},
409         {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
410         "9029: Incorrect hardware configuration change has been detected"},
411         {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
412         "9060: One or more disk pairs are missing from an array"},
413         {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
414         "9061: One or more disks are missing from an array"},
415         {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
416         "9062: One or more disks are missing from an array"},
417         {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
418         "9063: Maximum number of functional arrays has been exceeded"},
419         {0x0B260000, 0, 0,
420         "Aborted command, invalid descriptor"},
421         {0x0B5A0000, 0, 0,
422         "Command terminated by host"}
423 };
424 
425 static const struct ipr_ses_table_entry ipr_ses_table[] = {
426         { "2104-DL1        ", "XXXXXXXXXXXXXXXX", 80 },
427         { "2104-TL1        ", "XXXXXXXXXXXXXXXX", 80 },
428         { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
429         { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
430         { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
431         { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
432         { "2104-DU3        ", "XXXXXXXXXXXXXXXX", 160 },
433         { "2104-TU3        ", "XXXXXXXXXXXXXXXX", 160 },
434         { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
435         { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
436         { "St  V1S2        ", "XXXXXXXXXXXXXXXX", 160 },
437         { "HSBPD4M  PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
438         { "VSBPD1H   U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
439 };
440 
441 /*
442  *  Function Prototypes
443  */
444 static int ipr_reset_alert(struct ipr_cmnd *);
445 static void ipr_process_ccn(struct ipr_cmnd *);
446 static void ipr_process_error(struct ipr_cmnd *);
447 static void ipr_reset_ioa_job(struct ipr_cmnd *);
448 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
449                                    enum ipr_shutdown_type);
450 
451 #ifdef CONFIG_SCSI_IPR_TRACE
452 /**
453  * ipr_trc_hook - Add a trace entry to the driver trace
454  * @ipr_cmd:    ipr command struct
455  * @type:               trace type
456  * @add_data:   additional data
457  *
458  * Return value:
459  *      none
460  **/
461 static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
462                          u8 type, u32 add_data)
463 {
464         struct ipr_trace_entry *trace_entry;
465         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
466 
467         trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
468         trace_entry->time = jiffies;
469         trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
470         trace_entry->type = type;
471         trace_entry->ata_op_code = ipr_cmd->ioarcb.add_data.u.regs.command;
472         trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
473         trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
474         trace_entry->u.add_data = add_data;
475 }
476 #else
477 #define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
478 #endif
479 
480 /**
481  * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
482  * @ipr_cmd:    ipr command struct
483  *
484  * Return value:
485  *      none
486  **/
487 static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
488 {
489         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
490         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
491         dma_addr_t dma_addr = be32_to_cpu(ioarcb->ioarcb_host_pci_addr);
492 
493         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
494         ioarcb->write_data_transfer_length = 0;
495         ioarcb->read_data_transfer_length = 0;
496         ioarcb->write_ioadl_len = 0;
497         ioarcb->read_ioadl_len = 0;
498         ioarcb->write_ioadl_addr =
499                 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
500         ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
501         ioasa->ioasc = 0;
502         ioasa->residual_data_len = 0;
503         ioasa->u.gata.status = 0;
504 
505         ipr_cmd->scsi_cmd = NULL;
506         ipr_cmd->qc = NULL;
507         ipr_cmd->sense_buffer[0] = 0;
508         ipr_cmd->dma_use_sg = 0;
509 }
510 
511 /**
512  * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
513  * @ipr_cmd:    ipr command struct
514  *
515  * Return value:
516  *      none
517  **/
518 static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
519 {
520         ipr_reinit_ipr_cmnd(ipr_cmd);
521         ipr_cmd->u.scratch = 0;
522         ipr_cmd->sibling = NULL;
523         init_timer(&ipr_cmd->timer);
524 }
525 
526 /**
527  * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
528  * @ioa_cfg:    ioa config struct
529  *
530  * Return value:
531  *      pointer to ipr command struct
532  **/
533 static
534 struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
535 {
536         struct ipr_cmnd *ipr_cmd;
537 
538         ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
539         list_del(&ipr_cmd->queue);
540         ipr_init_ipr_cmnd(ipr_cmd);
541 
542         return ipr_cmd;
543 }
544 
545 /**
546  * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
547  * @ioa_cfg:    ioa config struct
548  * @clr_ints:     interrupts to clear
549  *
550  * This function masks all interrupts on the adapter, then clears the
551  * interrupts specified in the mask
552  *
553  * Return value:
554  *      none
555  **/
556 static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
557                                           u32 clr_ints)
558 {
559         volatile u32 int_reg;
560 
561         /* Stop new interrupts */
562         ioa_cfg->allow_interrupts = 0;
563 
564         /* Set interrupt mask to stop all new interrupts */
565         writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
566 
567         /* Clear any pending interrupts */
568         writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg);
569         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
570 }
571 
572 /**
573  * ipr_save_pcix_cmd_reg - Save PCI-X command register
574  * @ioa_cfg:    ioa config struct
575  *
576  * Return value:
577  *      0 on success / -EIO on failure
578  **/
579 static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
580 {
581         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
582 
583         if (pcix_cmd_reg == 0)
584                 return 0;
585 
586         if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
587                                  &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
588                 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
589                 return -EIO;
590         }
591 
592         ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
593         return 0;
594 }
595 
596 /**
597  * ipr_set_pcix_cmd_reg - Setup PCI-X command register
598  * @ioa_cfg:    ioa config struct
599  *
600  * Return value:
601  *      0 on success / -EIO on failure
602  **/
603 static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
604 {
605         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
606 
607         if (pcix_cmd_reg) {
608                 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
609                                           ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
610                         dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
611                         return -EIO;
612                 }
613         }
614 
615         return 0;
616 }
617 
618 /**
619  * ipr_sata_eh_done - done function for aborted SATA commands
620  * @ipr_cmd:    ipr command struct
621  *
622  * This function is invoked for ops generated to SATA
623  * devices which are being aborted.
624  *
625  * Return value:
626  *      none
627  **/
628 static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
629 {
630         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
631         struct ata_queued_cmd *qc = ipr_cmd->qc;
632         struct ipr_sata_port *sata_port = qc->ap->private_data;
633 
634         qc->err_mask |= AC_ERR_OTHER;
635         sata_port->ioasa.status |= ATA_BUSY;
636         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
637         ata_qc_complete(qc);
638 }
639 
640 /**
641  * ipr_scsi_eh_done - mid-layer done function for aborted ops
642  * @ipr_cmd:    ipr command struct
643  *
644  * This function is invoked by the interrupt handler for
645  * ops generated by the SCSI mid-layer which are being aborted.
646  *
647  * Return value:
648  *      none
649  **/
650 static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
651 {
652         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
653         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
654 
655         scsi_cmd->result |= (DID_ERROR << 16);
656 
657         scsi_dma_unmap(ipr_cmd->scsi_cmd);
658         scsi_cmd->scsi_done(scsi_cmd);
659         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
660 }
661 
662 /**
663  * ipr_fail_all_ops - Fails all outstanding ops.
664  * @ioa_cfg:    ioa config struct
665  *
666  * This function fails all outstanding ops.
667  *
668  * Return value:
669  *      none
670  **/
671 static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
672 {
673         struct ipr_cmnd *ipr_cmd, *temp;
674 
675         ENTER;
676         list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
677                 list_del(&ipr_cmd->queue);
678 
679                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
680                 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
681 
682                 if (ipr_cmd->scsi_cmd)
683                         ipr_cmd->done = ipr_scsi_eh_done;
684                 else if (ipr_cmd->qc)
685                         ipr_cmd->done = ipr_sata_eh_done;
686 
687                 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
688                 del_timer(&ipr_cmd->timer);
689                 ipr_cmd->done(ipr_cmd);
690         }
691 
692         LEAVE;
693 }
694 
695 /**
696  * ipr_do_req -  Send driver initiated requests.
697  * @ipr_cmd:            ipr command struct
698  * @done:                       done function
699  * @timeout_func:       timeout function
700  * @timeout:            timeout value
701  *
702  * This function sends the specified command to the adapter with the
703  * timeout given. The done function is invoked on command completion.
704  *
705  * Return value:
706  *      none
707  **/
708 static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
709                        void (*done) (struct ipr_cmnd *),
710                        void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
711 {
712         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
713 
714         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
715 
716         ipr_cmd->done = done;
717 
718         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
719         ipr_cmd->timer.expires = jiffies + timeout;
720         ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
721 
722         add_timer(&ipr_cmd->timer);
723 
724         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
725 
726         mb();
727         writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
728                ioa_cfg->regs.ioarrin_reg);
729 }
730 
731 /**
732  * ipr_internal_cmd_done - Op done function for an internally generated op.
733  * @ipr_cmd:    ipr command struct
734  *
735  * This function is the op done function for an internally generated,
736  * blocking op. It simply wakes the sleeping thread.
737  *
738  * Return value:
739  *      none
740  **/
741 static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
742 {
743         if (ipr_cmd->sibling)
744                 ipr_cmd->sibling = NULL;
745         else
746                 complete(&ipr_cmd->completion);
747 }
748 
749 /**
750  * ipr_send_blocking_cmd - Send command and sleep on its completion.
751  * @ipr_cmd:    ipr command struct
752  * @timeout_func:       function to invoke if command times out
753  * @timeout:    timeout
754  *
755  * Return value:
756  *      none
757  **/
758 static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
759                                   void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
760                                   u32 timeout)
761 {
762         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
763 
764         init_completion(&ipr_cmd->completion);
765         ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
766 
767         spin_unlock_irq(ioa_cfg->host->host_lock);
768         wait_for_completion(&ipr_cmd->completion);
769         spin_lock_irq(ioa_cfg->host->host_lock);
770 }
771 
772 /**
773  * ipr_send_hcam - Send an HCAM to the adapter.
774  * @ioa_cfg:    ioa config struct
775  * @type:               HCAM type
776  * @hostrcb:    hostrcb struct
777  *
778  * This function will send a Host Controlled Async command to the adapter.
779  * If HCAMs are currently not allowed to be issued to the adapter, it will
780  * place the hostrcb on the free queue.
781  *
782  * Return value:
783  *      none
784  **/
785 static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
786                           struct ipr_hostrcb *hostrcb)
787 {
788         struct ipr_cmnd *ipr_cmd;
789         struct ipr_ioarcb *ioarcb;
790 
791         if (ioa_cfg->allow_cmds) {
792                 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
793                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
794                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
795 
796                 ipr_cmd->u.hostrcb = hostrcb;
797                 ioarcb = &ipr_cmd->ioarcb;
798 
799                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
800                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
801                 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
802                 ioarcb->cmd_pkt.cdb[1] = type;
803                 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
804                 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
805 
806                 ioarcb->read_data_transfer_length = cpu_to_be32(sizeof(hostrcb->hcam));
807                 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
808                 ipr_cmd->ioadl[0].flags_and_data_len =
809                         cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(hostrcb->hcam));
810                 ipr_cmd->ioadl[0].address = cpu_to_be32(hostrcb->hostrcb_dma);
811 
812                 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
813                         ipr_cmd->done = ipr_process_ccn;
814                 else
815                         ipr_cmd->done = ipr_process_error;
816 
817                 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
818 
819                 mb();
820                 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
821                        ioa_cfg->regs.ioarrin_reg);
822         } else {
823                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
824         }
825 }
826 
827 /**
828  * ipr_init_res_entry - Initialize a resource entry struct.
829  * @res:        resource entry struct
830  *
831  * Return value:
832  *      none
833  **/
834 static void ipr_init_res_entry(struct ipr_resource_entry *res)
835 {
836         res->needs_sync_complete = 0;
837         res->in_erp = 0;
838         res->add_to_ml = 0;
839         res->del_from_ml = 0;
840         res->resetting_device = 0;
841         res->sdev = NULL;
842         res->sata_port = NULL;
843 }
844 
845 /**
846  * ipr_handle_config_change - Handle a config change from the adapter
847  * @ioa_cfg:    ioa config struct
848  * @hostrcb:    hostrcb
849  *
850  * Return value:
851  *      none
852  **/
853 static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
854                               struct ipr_hostrcb *hostrcb)
855 {
856         struct ipr_resource_entry *res = NULL;
857         struct ipr_config_table_entry *cfgte;
858         u32 is_ndn = 1;
859 
860         cfgte = &hostrcb->hcam.u.ccn.cfgte;
861 
862         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
863                 if (!memcmp(&res->cfgte.res_addr, &cfgte->res_addr,
864                             sizeof(cfgte->res_addr))) {
865                         is_ndn = 0;
866                         break;
867                 }
868         }
869 
870         if (is_ndn) {
871                 if (list_empty(&ioa_cfg->free_res_q)) {
872                         ipr_send_hcam(ioa_cfg,
873                                       IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
874                                       hostrcb);
875                         return;
876                 }
877 
878                 res = list_entry(ioa_cfg->free_res_q.next,
879                                  struct ipr_resource_entry, queue);
880 
881                 list_del(&res->queue);
882                 ipr_init_res_entry(res);
883                 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
884         }
885 
886         memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
887 
888         if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
889                 if (res->sdev) {
890                         res->del_from_ml = 1;
891                         res->cfgte.res_handle = IPR_INVALID_RES_HANDLE;
892                         if (ioa_cfg->allow_ml_add_del)
893                                 schedule_work(&ioa_cfg->work_q);
894                 } else
895                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
896         } else if (!res->sdev) {
897                 res->add_to_ml = 1;
898                 if (ioa_cfg->allow_ml_add_del)
899                         schedule_work(&ioa_cfg->work_q);
900         }
901 
902         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
903 }
904 
905 /**
906  * ipr_process_ccn - Op done function for a CCN.
907  * @ipr_cmd:    ipr command struct
908  *
909  * This function is the op done function for a configuration
910  * change notification host controlled async from the adapter.
911  *
912  * Return value:
913  *      none
914  **/
915 static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
916 {
917         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
918         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
919         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
920 
921         list_del(&hostrcb->queue);
922         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
923 
924         if (ioasc) {
925                 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
926                         dev_err(&ioa_cfg->pdev->dev,
927                                 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
928 
929                 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
930         } else {
931                 ipr_handle_config_change(ioa_cfg, hostrcb);
932         }
933 }
934 
935 /**
936  * strip_and_pad_whitespace - Strip and pad trailing whitespace.
937  * @i:          index into buffer
938  * @buf:                string to modify
939  *
940  * This function will strip all trailing whitespace, pad the end
941  * of the string with a single space, and NULL terminate the string.
942  *
943  * Return value:
944  *      new length of string
945  **/
946 static int strip_and_pad_whitespace(int i, char *buf)
947 {
948         while (i && buf[i] == ' ')
949                 i--;
950         buf[i+1] = ' ';
951         buf[i+2] = '\0';
952         return i + 2;
953 }
954 
955 /**
956  * ipr_log_vpd_compact - Log the passed extended VPD compactly.
957  * @prefix:             string to print at start of printk
958  * @hostrcb:    hostrcb pointer
959  * @vpd:                vendor/product id/sn struct
960  *
961  * Return value:
962  *      none
963  **/
964 static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
965                                 struct ipr_vpd *vpd)
966 {
967         char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
968         int i = 0;
969 
970         memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
971         i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
972 
973         memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
974         i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
975 
976         memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
977         buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
978 
979         ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
980 }
981 
982 /**
983  * ipr_log_vpd - Log the passed VPD to the error log.
984  * @vpd:                vendor/product id/sn struct
985  *
986  * Return value:
987  *      none
988  **/
989 static void ipr_log_vpd(struct ipr_vpd *vpd)
990 {
991         char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
992                     + IPR_SERIAL_NUM_LEN];
993 
994         memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
995         memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
996                IPR_PROD_ID_LEN);
997         buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
998         ipr_err("Vendor/Product ID: %s\n", buffer);
999 
1000         memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
1001         buffer[IPR_SERIAL_NUM_LEN] = '\0';
1002         ipr_err("    Serial Number: %s\n", buffer);
1003 }
1004 
1005 /**
1006  * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
1007  * @prefix:             string to print at start of printk
1008  * @hostrcb:    hostrcb pointer
1009  * @vpd:                vendor/product id/sn/wwn struct
1010  *
1011  * Return value:
1012  *      none
1013  **/
1014 static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1015                                     struct ipr_ext_vpd *vpd)
1016 {
1017         ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
1018         ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
1019                      be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
1020 }
1021 
1022 /**
1023  * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
1024  * @vpd:                vendor/product id/sn/wwn struct
1025  *
1026  * Return value:
1027  *      none
1028  **/
1029 static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
1030 {
1031         ipr_log_vpd(&vpd->vpd);
1032         ipr_err("    WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
1033                 be32_to_cpu(vpd->wwid[1]));
1034 }
1035 
1036 /**
1037  * ipr_log_enhanced_cache_error - Log a cache error.
1038  * @ioa_cfg:    ioa config struct
1039  * @hostrcb:    hostrcb struct
1040  *
1041  * Return value:
1042  *      none
1043  **/
1044 static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1045                                          struct ipr_hostrcb *hostrcb)
1046 {
1047         struct ipr_hostrcb_type_12_error *error =
1048                 &hostrcb->hcam.u.error.u.type_12_error;
1049 
1050         ipr_err("-----Current Configuration-----\n");
1051         ipr_err("Cache Directory Card Information:\n");
1052         ipr_log_ext_vpd(&error->ioa_vpd);
1053         ipr_err("Adapter Card Information:\n");
1054         ipr_log_ext_vpd(&error->cfc_vpd);
1055 
1056         ipr_err("-----Expected Configuration-----\n");
1057         ipr_err("Cache Directory Card Information:\n");
1058         ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
1059         ipr_err("Adapter Card Information:\n");
1060         ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
1061 
1062         ipr_err("Additional IOA Data: %08X %08X %08X\n",
1063                      be32_to_cpu(error->ioa_data[0]),
1064                      be32_to_cpu(error->ioa_data[1]),
1065                      be32_to_cpu(error->ioa_data[2]));
1066 }
1067 
1068 /**
1069  * ipr_log_cache_error - Log a cache error.
1070  * @ioa_cfg:    ioa config struct
1071  * @hostrcb:    hostrcb struct
1072  *
1073  * Return value:
1074  *      none
1075  **/
1076 static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1077                                 struct ipr_hostrcb *hostrcb)
1078 {
1079         struct ipr_hostrcb_type_02_error *error =
1080                 &hostrcb->hcam.u.error.u.type_02_error;
1081 
1082         ipr_err("-----Current Configuration-----\n");
1083         ipr_err("Cache Directory Card Information:\n");
1084         ipr_log_vpd(&error->ioa_vpd);
1085         ipr_err("Adapter Card Information:\n");
1086         ipr_log_vpd(&error->cfc_vpd);
1087 
1088         ipr_err("-----Expected Configuration-----\n");
1089         ipr_err("Cache Directory Card Information:\n");
1090         ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
1091         ipr_err("Adapter Card Information:\n");
1092         ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
1093 
1094         ipr_err("Additional IOA Data: %08X %08X %08X\n",
1095                      be32_to_cpu(error->ioa_data[0]),
1096                      be32_to_cpu(error->ioa_data[1]),
1097                      be32_to_cpu(error->ioa_data[2]));
1098 }
1099 
1100 /**
1101  * ipr_log_enhanced_config_error - Log a configuration error.
1102  * @ioa_cfg:    ioa config struct
1103  * @hostrcb:    hostrcb struct
1104  *
1105  * Return value:
1106  *      none
1107  **/
1108 static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
1109                                           struct ipr_hostrcb *hostrcb)
1110 {
1111         int errors_logged, i;
1112         struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
1113         struct ipr_hostrcb_type_13_error *error;
1114 
1115         error = &hostrcb->hcam.u.error.u.type_13_error;
1116         errors_logged = be32_to_cpu(error->errors_logged);
1117 
1118         ipr_err("Device Errors Detected/Logged: %d/%d\n",
1119                 be32_to_cpu(error->errors_detected), errors_logged);
1120 
1121         dev_entry = error->dev;
1122 
1123         for (i = 0; i < errors_logged; i++, dev_entry++) {
1124                 ipr_err_separator;
1125 
1126                 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1127                 ipr_log_ext_vpd(&dev_entry->vpd);
1128 
1129                 ipr_err("-----New Device Information-----\n");
1130                 ipr_log_ext_vpd(&dev_entry->new_vpd);
1131 
1132                 ipr_err("Cache Directory Card Information:\n");
1133                 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1134 
1135                 ipr_err("Adapter Card Information:\n");
1136                 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1137         }
1138 }
1139 
1140 /**
1141  * ipr_log_config_error - Log a configuration error.
1142  * @ioa_cfg:    ioa config struct
1143  * @hostrcb:    hostrcb struct
1144  *
1145  * Return value:
1146  *      none
1147  **/
1148 static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
1149                                  struct ipr_hostrcb *hostrcb)
1150 {
1151         int errors_logged, i;
1152         struct ipr_hostrcb_device_data_entry *dev_entry;
1153         struct ipr_hostrcb_type_03_error *error;
1154 
1155         error = &hostrcb->hcam.u.error.u.type_03_error;
1156         errors_logged = be32_to_cpu(error->errors_logged);
1157 
1158         ipr_err("Device Errors Detected/Logged: %d/%d\n",
1159                 be32_to_cpu(error->errors_detected), errors_logged);
1160 
1161         dev_entry = error->dev;
1162 
1163         for (i = 0; i < errors_logged; i++, dev_entry++) {
1164                 ipr_err_separator;
1165 
1166                 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1167                 ipr_log_vpd(&dev_entry->vpd);
1168 
1169                 ipr_err("-----New Device Information-----\n");
1170                 ipr_log_vpd(&dev_entry->new_vpd);
1171 
1172                 ipr_err("Cache Directory Card Information:\n");
1173                 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
1174 
1175                 ipr_err("Adapter Card Information:\n");
1176                 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
1177 
1178                 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1179                         be32_to_cpu(dev_entry->ioa_data[0]),
1180                         be32_to_cpu(dev_entry->ioa_data[1]),
1181                         be32_to_cpu(dev_entry->ioa_data[2]),
1182                         be32_to_cpu(dev_entry->ioa_data[3]),
1183                         be32_to_cpu(dev_entry->ioa_data[4]));
1184         }
1185 }
1186 
1187 /**
1188  * ipr_log_enhanced_array_error - Log an array configuration error.
1189  * @ioa_cfg:    ioa config struct
1190  * @hostrcb:    hostrcb struct
1191  *
1192  * Return value:
1193  *      none
1194  **/
1195 static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
1196                                          struct ipr_hostrcb *hostrcb)
1197 {
1198         int i, num_entries;
1199         struct ipr_hostrcb_type_14_error *error;
1200         struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
1201         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '' };
1202 
1203         error = &hostrcb->hcam.u.error.u.type_14_error;
1204 
1205         ipr_err_separator;
1206 
1207         ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1208                 error->protection_level,
1209                 ioa_cfg->host->host_no,
1210                 error->last_func_vset_res_addr.bus,
1211                 error->last_func_vset_res_addr.target,
1212                 error->last_func_vset_res_addr.lun);
1213 
1214         ipr_err_separator;
1215 
1216         array_entry = error->array_member;
1217         num_entries = min_t(u32, be32_to_cpu(error->num_entries),
1218                             sizeof(error->array_member));
1219 
1220         for (i = 0; i < num_entries; i++, array_entry++) {
1221                 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1222                         continue;
1223 
1224                 if (be32_to_cpu(error->exposed_mode_adn) == i)
1225                         ipr_err("Exposed Array Member %d:\n", i);
1226                 else
1227                         ipr_err("Array Member %d:\n", i);
1228 
1229                 ipr_log_ext_vpd(&array_entry->vpd);
1230                 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1231                 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1232                                  "Expected Location");
1233 
1234                 ipr_err_separator;
1235         }
1236 }
1237 
1238 /**
1239  * ipr_log_array_error - Log an array configuration error.
1240  * @ioa_cfg:    ioa config struct
1241  * @hostrcb:    hostrcb struct
1242  *
1243  * Return value:
1244  *      none
1245  **/
1246 static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1247                                 struct ipr_hostrcb *hostrcb)
1248 {
1249         int i;
1250         struct ipr_hostrcb_type_04_error *error;
1251         struct ipr_hostrcb_array_data_entry *array_entry;
1252         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '' };
1253 
1254         error = &hostrcb->hcam.u.error.u.type_04_error;
1255 
1256         ipr_err_separator;
1257 
1258         ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1259                 error->protection_level,
1260                 ioa_cfg->host->host_no,
1261                 error->last_func_vset_res_addr.bus,
1262                 error->last_func_vset_res_addr.target,
1263                 error->last_func_vset_res_addr.lun);
1264 
1265         ipr_err_separator;
1266 
1267         array_entry = error->array_member;
1268 
1269         for (i = 0; i < 18; i++) {
1270                 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1271                         continue;
1272 
1273                 if (be32_to_cpu(error->exposed_mode_adn) == i)
1274                         ipr_err("Exposed Array Member %d:\n", i);
1275                 else
1276                         ipr_err("Array Member %d:\n", i);
1277 
1278                 ipr_log_vpd(&array_entry->vpd);
1279 
1280                 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1281                 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1282                                  "Expected Location");
1283 
1284                 ipr_err_separator;
1285 
1286                 if (i == 9)
1287                         array_entry = error->array_member2;
1288                 else
1289                         array_entry++;
1290         }
1291 }
1292 
1293 /**
1294  * ipr_log_hex_data - Log additional hex IOA error data.
1295  * @ioa_cfg:    ioa config struct
1296  * @data:               IOA error data
1297  * @len:                data length
1298  *
1299  * Return value:
1300  *      none
1301  **/
1302 static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
1303 {
1304         int i;
1305 
1306         if (len == 0)
1307                 return;
1308 
1309         if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
1310                 len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
1311 
1312         for (i = 0; i < len / 4; i += 4) {
1313                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1314                         be32_to_cpu(data[i]),
1315                         be32_to_cpu(data[i+1]),
1316                         be32_to_cpu(data[i+2]),
1317                         be32_to_cpu(data[i+3]));
1318         }
1319 }
1320 
1321 /**
1322  * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
1323  * @ioa_cfg:    ioa config struct
1324  * @hostrcb:    hostrcb struct
1325  *
1326  * Return value:
1327  *      none
1328  **/
1329 static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1330                                             struct ipr_hostrcb *hostrcb)
1331 {
1332         struct ipr_hostrcb_type_17_error *error;
1333 
1334         error = &hostrcb->hcam.u.error.u.type_17_error;
1335         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1336         strstrip(error->failure_reason);
1337 
1338         ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1339                      be32_to_cpu(hostrcb->hcam.u.error.prc));
1340         ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1341         ipr_log_hex_data(ioa_cfg, error->data,
1342                          be32_to_cpu(hostrcb->hcam.length) -
1343                          (offsetof(struct ipr_hostrcb_error, u) +
1344                           offsetof(struct ipr_hostrcb_type_17_error, data)));
1345 }
1346 
1347 /**
1348  * ipr_log_dual_ioa_error - Log a dual adapter error.
1349  * @ioa_cfg:    ioa config struct
1350  * @hostrcb:    hostrcb struct
1351  *
1352  * Return value:
1353  *      none
1354  **/
1355 static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1356                                    struct ipr_hostrcb *hostrcb)
1357 {
1358         struct ipr_hostrcb_type_07_error *error;
1359 
1360         error = &hostrcb->hcam.u.error.u.type_07_error;
1361         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1362         strstrip(error->failure_reason);
1363 
1364         ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1365                      be32_to_cpu(hostrcb->hcam.u.error.prc));
1366         ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1367         ipr_log_hex_data(ioa_cfg, error->data,
1368                          be32_to_cpu(hostrcb->hcam.length) -
1369                          (offsetof(struct ipr_hostrcb_error, u) +
1370                           offsetof(struct ipr_hostrcb_type_07_error, data)));
1371 }
1372 
1373 static const struct {
1374         u8 active;
1375         char *desc;
1376 } path_active_desc[] = {
1377         { IPR_PATH_NO_INFO, "Path" },
1378         { IPR_PATH_ACTIVE, "Active path" },
1379         { IPR_PATH_NOT_ACTIVE, "Inactive path" }
1380 };
1381 
1382 static const struct {
1383         u8 state;
1384         char *desc;
1385 } path_state_desc[] = {
1386         { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
1387         { IPR_PATH_HEALTHY, "is healthy" },
1388         { IPR_PATH_DEGRADED, "is degraded" },
1389         { IPR_PATH_FAILED, "is failed" }
1390 };
1391 
1392 /**
1393  * ipr_log_fabric_path - Log a fabric path error
1394  * @hostrcb:    hostrcb struct
1395  * @fabric:             fabric descriptor
1396  *
1397  * Return value:
1398  *      none
1399  **/
1400 static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
1401                                 struct ipr_hostrcb_fabric_desc *fabric)
1402 {
1403         int i, j;
1404         u8 path_state = fabric->path_state;
1405         u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1406         u8 state = path_state & IPR_PATH_STATE_MASK;
1407 
1408         for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1409                 if (path_active_desc[i].active != active)
1410                         continue;
1411 
1412                 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
1413                         if (path_state_desc[j].state != state)
1414                                 continue;
1415 
1416                         if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
1417                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
1418                                              path_active_desc[i].desc, path_state_desc[j].desc,
1419                                              fabric->ioa_port);
1420                         } else if (fabric->cascaded_expander == 0xff) {
1421                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
1422                                              path_active_desc[i].desc, path_state_desc[j].desc,
1423                                              fabric->ioa_port, fabric->phy);
1424                         } else if (fabric->phy == 0xff) {
1425                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
1426                                              path_active_desc[i].desc, path_state_desc[j].desc,
1427                                              fabric->ioa_port, fabric->cascaded_expander);
1428                         } else {
1429                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
1430                                              path_active_desc[i].desc, path_state_desc[j].desc,
1431                                              fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1432                         }
1433                         return;
1434                 }
1435         }
1436 
1437         ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
1438                 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1439 }
1440 
1441 static const struct {
1442         u8 type;
1443         char *desc;
1444 } path_type_desc[] = {
1445         { IPR_PATH_CFG_IOA_PORT, "IOA port" },
1446         { IPR_PATH_CFG_EXP_PORT, "Expander port" },
1447         { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
1448         { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
1449 };
1450 
1451 static const struct {
1452         u8 status;
1453         char *desc;
1454 } path_status_desc[] = {
1455         { IPR_PATH_CFG_NO_PROB, "Functional" },
1456         { IPR_PATH_CFG_DEGRADED, "Degraded" },
1457         { IPR_PATH_CFG_FAILED, "Failed" },
1458         { IPR_PATH_CFG_SUSPECT, "Suspect" },
1459         { IPR_PATH_NOT_DETECTED, "Missing" },
1460         { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
1461 };
1462 
1463 static const char *link_rate[] = {
1464         "unknown",
1465         "disabled",
1466         "phy reset problem",
1467         "spinup hold",
1468         "port selector",
1469         "unknown",
1470         "unknown",
1471         "unknown",
1472         "1.5Gbps",
1473         "3.0Gbps",
1474         "unknown",
1475         "unknown",
1476         "unknown",
1477         "unknown",
1478         "unknown",
1479         "unknown"
1480 };
1481 
1482 /**
1483  * ipr_log_path_elem - Log a fabric path element.
1484  * @hostrcb:    hostrcb struct
1485  * @cfg:                fabric path element struct
1486  *
1487  * Return value:
1488  *      none
1489  **/
1490 static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
1491                               struct ipr_hostrcb_config_element *cfg)
1492 {
1493         int i, j;
1494         u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
1495         u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
1496 
1497         if (type == IPR_PATH_CFG_NOT_EXIST)
1498                 return;
1499 
1500         for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
1501                 if (path_type_desc[i].type != type)
1502                         continue;
1503 
1504                 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
1505                         if (path_status_desc[j].status != status)
1506                                 continue;
1507 
1508                         if (type == IPR_PATH_CFG_IOA_PORT) {
1509                                 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
1510                                              path_status_desc[j].desc, path_type_desc[i].desc,
1511                                              cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1512                                              be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1513                         } else {
1514                                 if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
1515                                         ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
1516                                                      path_status_desc[j].desc, path_type_desc[i].desc,
1517                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1518                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1519                                 } else if (cfg->cascaded_expander == 0xff) {
1520                                         ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
1521                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
1522                                                      path_type_desc[i].desc, cfg->phy,
1523                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1524                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1525                                 } else if (cfg->phy == 0xff) {
1526                                         ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
1527                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
1528                                                      path_type_desc[i].desc, cfg->cascaded_expander,
1529                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1530                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1531                                 } else {
1532                                         ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
1533                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
1534                                                      path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
1535                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1536                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1537                                 }
1538                         }
1539                         return;
1540                 }
1541         }
1542 
1543         ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
1544                      "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
1545                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1546                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1547 }
1548 
1549 /**
1550  * ipr_log_fabric_error - Log a fabric error.
1551  * @ioa_cfg:    ioa config struct
1552  * @hostrcb:    hostrcb struct
1553  *
1554  * Return value:
1555  *      none
1556  **/
1557 static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
1558                                  struct ipr_hostrcb *hostrcb)
1559 {
1560         struct ipr_hostrcb_type_20_error *error;
1561         struct ipr_hostrcb_fabric_desc *fabric;
1562         struct ipr_hostrcb_config_element *cfg;
1563         int i, add_len;
1564 
1565         error = &hostrcb->hcam.u.error.u.type_20_error;
1566         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1567         ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
1568 
1569         add_len = be32_to_cpu(hostrcb->hcam.length) -
1570                 (offsetof(struct ipr_hostrcb_error, u) +
1571                  offsetof(struct ipr_hostrcb_type_20_error, desc));
1572 
1573         for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
1574                 ipr_log_fabric_path(hostrcb, fabric);
1575                 for_each_fabric_cfg(fabric, cfg)
1576                         ipr_log_path_elem(hostrcb, cfg);
1577 
1578                 add_len -= be16_to_cpu(fabric->length);
1579                 fabric = (struct ipr_hostrcb_fabric_desc *)
1580                         ((unsigned long)fabric + be16_to_cpu(fabric->length));
1581         }
1582 
1583         ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
1584 }
1585 
1586 /**
1587  * ipr_log_generic_error - Log an adapter error.
1588  * @ioa_cfg:    ioa config struct
1589  * @hostrcb:    hostrcb struct
1590  *
1591  * Return value:
1592  *      none
1593  **/
1594 static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1595                                   struct ipr_hostrcb *hostrcb)
1596 {
1597         ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
1598                          be32_to_cpu(hostrcb->hcam.length));
1599 }
1600 
1601 /**
1602  * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1603  * @ioasc:      IOASC
1604  *
1605  * This function will return the index of into the ipr_error_table
1606  * for the specified IOASC. If the IOASC is not in the table,
1607  * 0 will be returned, which points to the entry used for unknown errors.
1608  *
1609  * Return value:
1610  *      index into the ipr_error_table
1611  **/
1612 static u32 ipr_get_error(u32 ioasc)
1613 {
1614         int i;
1615 
1616         for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
1617                 if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
1618                         return i;
1619 
1620         return 0;
1621 }
1622 
1623 /**
1624  * ipr_handle_log_data - Log an adapter error.
1625  * @ioa_cfg:    ioa config struct
1626  * @hostrcb:    hostrcb struct
1627  *
1628  * This function logs an adapter error to the system.
1629  *
1630  * Return value:
1631  *      none
1632  **/
1633 static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1634                                 struct ipr_hostrcb *hostrcb)
1635 {
1636         u32 ioasc;
1637         int error_index;
1638 
1639         if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1640                 return;
1641 
1642         if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1643                 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1644 
1645         ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1646 
1647         if (ioasc == IPR_IOASC_BUS_WAS_RESET ||
1648             ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER) {
1649                 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
1650                 scsi_report_bus_reset(ioa_cfg->host,
1651                                       hostrcb->hcam.u.error.failing_dev_res_addr.bus);
1652         }
1653 
1654         error_index = ipr_get_error(ioasc);
1655 
1656         if (!ipr_error_table[error_index].log_hcam)
1657                 return;
1658 
1659         ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
1660 
1661         /* Set indication we have logged an error */
1662         ioa_cfg->errors_logged++;
1663 
1664         if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
1665                 return;
1666         if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
1667                 hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
1668 
1669         switch (hostrcb->hcam.overlay_id) {
1670         case IPR_HOST_RCB_OVERLAY_ID_2:
1671                 ipr_log_cache_error(ioa_cfg, hostrcb);
1672                 break;
1673         case IPR_HOST_RCB_OVERLAY_ID_3:
1674                 ipr_log_config_error(ioa_cfg, hostrcb);
1675                 break;
1676         case IPR_HOST_RCB_OVERLAY_ID_4:
1677         case IPR_HOST_RCB_OVERLAY_ID_6:
1678                 ipr_log_array_error(ioa_cfg, hostrcb);
1679                 break;
1680         case IPR_HOST_RCB_OVERLAY_ID_7:
1681                 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
1682                 break;
1683         case IPR_HOST_RCB_OVERLAY_ID_12:
1684                 ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
1685                 break;
1686         case IPR_HOST_RCB_OVERLAY_ID_13:
1687                 ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
1688                 break;
1689         case IPR_HOST_RCB_OVERLAY_ID_14:
1690         case IPR_HOST_RCB_OVERLAY_ID_16:
1691                 ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
1692                 break;
1693         case IPR_HOST_RCB_OVERLAY_ID_17:
1694                 ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
1695                 break;
1696         case IPR_HOST_RCB_OVERLAY_ID_20:
1697                 ipr_log_fabric_error(ioa_cfg, hostrcb);
1698                 break;
1699         case IPR_HOST_RCB_OVERLAY_ID_1:
1700         case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
1701         default:
1702                 ipr_log_generic_error(ioa_cfg, hostrcb);
1703                 break;
1704         }
1705 }
1706 
1707 /**
1708  * ipr_process_error - Op done function for an adapter error log.
1709  * @ipr_cmd:    ipr command struct
1710  *
1711  * This function is the op done function for an error log host
1712  * controlled async from the adapter. It will log the error and
1713  * send the HCAM back to the adapter.
1714  *
1715  * Return value:
1716  *      none
1717  **/
1718 static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
1719 {
1720         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1721         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1722         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
1723         u32 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1724 
1725         list_del(&hostrcb->queue);
1726         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1727 
1728         if (!ioasc) {
1729                 ipr_handle_log_data(ioa_cfg, hostrcb);
1730                 if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
1731                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
1732         } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
1733                 dev_err(&ioa_cfg->pdev->dev,
1734                         "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1735         }
1736 
1737         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
1738 }
1739 
1740 /**
1741  * ipr_timeout -  An internally generated op has timed out.
1742  * @ipr_cmd:    ipr command struct
1743  *
1744  * This function blocks host requests and initiates an
1745  * adapter reset.
1746  *
1747  * Return value:
1748  *      none
1749  **/
1750 static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
1751 {
1752         unsigned long lock_flags = 0;
1753         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1754 
1755         ENTER;
1756         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1757 
1758         ioa_cfg->errors_logged++;
1759         dev_err(&ioa_cfg->pdev->dev,
1760                 "Adapter being reset due to command timeout.\n");
1761 
1762         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1763                 ioa_cfg->sdt_state = GET_DUMP;
1764 
1765         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
1766                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1767 
1768         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1769         LEAVE;
1770 }
1771 
1772 /**
1773  * ipr_oper_timeout -  Adapter timed out transitioning to operational
1774  * @ipr_cmd:    ipr command struct
1775  *
1776  * This function blocks host requests and initiates an
1777  * adapter reset.
1778  *
1779  * Return value:
1780  *      none
1781  **/
1782 static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
1783 {
1784         unsigned long lock_flags = 0;
1785         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1786 
1787         ENTER;
1788         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1789 
1790         ioa_cfg->errors_logged++;
1791         dev_err(&ioa_cfg->pdev->dev,
1792                 "Adapter timed out transitioning to operational.\n");
1793 
1794         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1795                 ioa_cfg->sdt_state = GET_DUMP;
1796 
1797         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
1798                 if (ipr_fastfail)
1799                         ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
1800                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1801         }
1802 
1803         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1804         LEAVE;
1805 }
1806 
1807 /**
1808  * ipr_reset_reload - Reset/Reload the IOA
1809  * @ioa_cfg:            ioa config struct
1810  * @shutdown_type:      shutdown type
1811  *
1812  * This function resets the adapter and re-initializes it.
1813  * This function assumes that all new host commands have been stopped.
1814  * Return value:
1815  *      SUCCESS / FAILED
1816  **/
1817 static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
1818                             enum ipr_shutdown_type shutdown_type)
1819 {
1820         if (!ioa_cfg->in_reset_reload)
1821                 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
1822 
1823         spin_unlock_irq(ioa_cfg->host->host_lock);
1824         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
1825         spin_lock_irq(ioa_cfg->host->host_lock);
1826 
1827         /* If we got hit with a host reset while we were already resetting
1828          the adapter for some reason, and the reset failed. */
1829         if (ioa_cfg->ioa_is_dead) {
1830                 ipr_trace;
1831                 return FAILED;
1832         }
1833 
1834         return SUCCESS;
1835 }
1836 
1837 /**
1838  * ipr_find_ses_entry - Find matching SES in SES table
1839  * @res:        resource entry struct of SES
1840  *
1841  * Return value:
1842  *      pointer to SES table entry / NULL on failure
1843  **/
1844 static const struct ipr_ses_table_entry *
1845 ipr_find_ses_entry(struct ipr_resource_entry *res)
1846 {
1847         int i, j, matches;
1848         const struct ipr_ses_table_entry *ste = ipr_ses_table;
1849 
1850         for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
1851                 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
1852                         if (ste->compare_product_id_byte[j] == 'X') {
1853                                 if (res->cfgte.std_inq_data.vpids.product_id[j] == ste->product_id[j])
1854                                         matches++;
1855                                 else
1856                                         break;
1857                         } else
1858                                 matches++;
1859                 }
1860 
1861                 if (matches == IPR_PROD_ID_LEN)
1862                         return ste;
1863         }
1864 
1865         return NULL;
1866 }
1867 
1868 /**
1869  * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
1870  * @ioa_cfg:    ioa config struct
1871  * @bus:                SCSI bus
1872  * @bus_width:  bus width
1873  *
1874  * Return value:
1875  *      SCSI bus speed in units of 100KHz, 1600 is 160 MHz
1876  *      For a 2-byte wide SCSI bus, the maximum transfer speed is
1877  *      twice the maximum transfer rate (e.g. for a wide enabled bus,
1878  *      max 160MHz = max 320MB/sec).
1879  **/
1880 static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
1881 {
1882         struct ipr_resource_entry *res;
1883         const struct ipr_ses_table_entry *ste;
1884         u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
1885 
1886         /* Loop through each config table entry in the config table buffer */
1887         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1888                 if (!(IPR_IS_SES_DEVICE(res->cfgte.std_inq_data)))
1889                         continue;
1890 
1891                 if (bus != res->cfgte.res_addr.bus)
1892                         continue;
1893 
1894                 if (!(ste = ipr_find_ses_entry(res)))
1895                         continue;
1896 
1897                 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
1898         }
1899 
1900         return max_xfer_rate;
1901 }
1902 
1903 /**
1904  * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
1905  * @ioa_cfg:            ioa config struct
1906  * @max_delay:          max delay in micro-seconds to wait
1907  *
1908  * Waits for an IODEBUG ACK from the IOA, doing busy looping.
1909  *
1910  * Return value:
1911  *      0 on success / other on failure
1912  **/
1913 static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
1914 {
1915         volatile u32 pcii_reg;
1916         int delay = 1;
1917 
1918         /* Read interrupt reg until IOA signals IO Debug Acknowledge */
1919         while (delay < max_delay) {
1920                 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
1921 
1922                 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
1923                         return 0;
1924 
1925                 /* udelay cannot be used if delay is more than a few milliseconds */
1926                 if ((delay / 1000) > MAX_UDELAY_MS)
1927                         mdelay(delay / 1000);
1928                 else
1929                         udelay(delay);
1930 
1931                 delay += delay;
1932         }
1933         return -EIO;
1934 }
1935 
1936 /**
1937  * ipr_get_ldump_data_section - Dump IOA memory
1938  * @ioa_cfg:                    ioa config struct
1939  * @start_addr:                 adapter address to dump
1940  * @dest:                               destination kernel buffer
1941  * @length_in_words:    length to dump in 4 byte words
1942  *
1943  * Return value:
1944  *      0 on success / -EIO on failure
1945  **/
1946 static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
1947                                       u32 start_addr,
1948                                       __be32 *dest, u32 length_in_words)
1949 {
1950         volatile u32 temp_pcii_reg;
1951         int i, delay = 0;
1952 
1953         /* Write IOA interrupt reg starting LDUMP state  */
1954         writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
1955                ioa_cfg->regs.set_uproc_interrupt_reg);
1956 
1957         /* Wait for IO debug acknowledge */
1958         if (ipr_wait_iodbg_ack(ioa_cfg,
1959                                IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
1960                 dev_err(&ioa_cfg->pdev->dev,
1961                         "IOA dump long data transfer timeout\n");
1962                 return -EIO;
1963         }
1964 
1965         /* Signal LDUMP interlocked - clear IO debug ack */
1966         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1967                ioa_cfg->regs.clr_interrupt_reg);
1968 
1969         /* Write Mailbox with starting address */
1970         writel(start_addr, ioa_cfg->ioa_mailbox);
1971 
1972         /* Signal address valid - clear IOA Reset alert */
1973         writel(IPR_UPROCI_RESET_ALERT,
1974                ioa_cfg->regs.clr_uproc_interrupt_reg);
1975 
1976         for (i = 0; i < length_in_words; i++) {
1977                 /* Wait for IO debug acknowledge */
1978                 if (ipr_wait_iodbg_ack(ioa_cfg,
1979                                        IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
1980                         dev_err(&ioa_cfg->pdev->dev,
1981                                 "IOA dump short data transfer timeout\n");
1982                         return -EIO;
1983                 }
1984 
1985                 /* Read data from mailbox and increment destination pointer */
1986                 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
1987                 dest++;
1988 
1989                 /* For all but the last word of data, signal data received */
1990                 if (i < (length_in_words - 1)) {
1991                         /* Signal dump data received - Clear IO debug Ack */
1992                         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1993                                ioa_cfg->regs.clr_interrupt_reg);
1994                 }
1995         }
1996 
1997         /* Signal end of block transfer. Set reset alert then clear IO debug ack */
1998         writel(IPR_UPROCI_RESET_ALERT,
1999                ioa_cfg->regs.set_uproc_interrupt_reg);
2000 
2001         writel(IPR_UPROCI_IO_DEBUG_ALERT,
2002                ioa_cfg->regs.clr_uproc_interrupt_reg);
2003 
2004         /* Signal dump data received - Clear IO debug Ack */
2005         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2006                ioa_cfg->regs.clr_interrupt_reg);
2007 
2008         /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
2009         while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
2010                 temp_pcii_reg =
2011                     readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
2012 
2013                 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
2014                         return 0;
2015 
2016                 udelay(10);
2017                 delay += 10;
2018         }
2019 
2020         return 0;
2021 }
2022 
2023 #ifdef CONFIG_SCSI_IPR_DUMP
2024 /**
2025  * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
2026  * @ioa_cfg:            ioa config struct
2027  * @pci_address:        adapter address
2028  * @length:                     length of data to copy
2029  *
2030  * Copy data from PCI adapter to kernel buffer.
2031  * Note: length MUST be a 4 byte multiple
2032  * Return value:
2033  *      0 on success / other on failure
2034  **/
2035 static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
2036                         unsigned long pci_address, u32 length)
2037 {
2038         int bytes_copied = 0;
2039         int cur_len, rc, rem_len, rem_page_len;
2040         __be32 *page;
2041         unsigned long lock_flags = 0;
2042         struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
2043 
2044         while (bytes_copied < length &&
2045                (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
2046                 if (ioa_dump->page_offset >= PAGE_SIZE ||
2047                     ioa_dump->page_offset == 0) {
2048                         page = (__be32 *)__get_free_page(GFP_ATOMIC);
2049 
2050                         if (!page) {
2051                                 ipr_trace;
2052                                 return bytes_copied;
2053                         }
2054 
2055                         ioa_dump->page_offset = 0;
2056                         ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
2057                         ioa_dump->next_page_index++;
2058                 } else
2059                         page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
2060 
2061                 rem_len = length - bytes_copied;
2062                 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
2063                 cur_len = min(rem_len, rem_page_len);
2064 
2065                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2066                 if (ioa_cfg->sdt_state == ABORT_DUMP) {
2067                         rc = -EIO;
2068                 } else {
2069                         rc = ipr_get_ldump_data_section(ioa_cfg,
2070                                                         pci_address + bytes_copied,
2071                                                         &page[ioa_dump->page_offset / 4],
2072                                                         (cur_len / sizeof(u32)));
2073                 }
2074                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2075 
2076                 if (!rc) {
2077                         ioa_dump->page_offset += cur_len;
2078                         bytes_copied += cur_len;
2079                 } else {
2080                         ipr_trace;
2081                         break;
2082                 }
2083                 schedule();
2084         }
2085 
2086         return bytes_copied;
2087 }
2088 
2089 /**
2090  * ipr_init_dump_entry_hdr - Initialize a dump entry header.
2091  * @hdr:        dump entry header struct
2092  *
2093  * Return value:
2094  *      nothing
2095  **/
2096 static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
2097 {
2098         hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
2099         hdr->num_elems = 1;
2100         hdr->offset = sizeof(*hdr);
2101         hdr->status = IPR_DUMP_STATUS_SUCCESS;
2102 }
2103 
2104 /**
2105  * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
2106  * @ioa_cfg:    ioa config struct
2107  * @driver_dump:        driver dump struct
2108  *
2109  * Return value:
2110  *      nothing
2111  **/
2112 static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
2113                                    struct ipr_driver_dump *driver_dump)
2114 {
2115         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2116 
2117         ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
2118         driver_dump->ioa_type_entry.hdr.len =
2119                 sizeof(struct ipr_dump_ioa_type_entry) -
2120                 sizeof(struct ipr_dump_entry_header);
2121         driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2122         driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
2123         driver_dump->ioa_type_entry.type = ioa_cfg->type;
2124         driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
2125                 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
2126                 ucode_vpd->minor_release[1];
2127         driver_dump->hdr.num_entries++;
2128 }
2129 
2130 /**
2131  * ipr_dump_version_data - Fill in the driver version in the dump.
2132  * @ioa_cfg:    ioa config struct
2133  * @driver_dump:        driver dump struct
2134  *
2135  * Return value:
2136  *      nothing
2137  **/
2138 static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
2139                                   struct ipr_driver_dump *driver_dump)
2140 {
2141         ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
2142         driver_dump->version_entry.hdr.len =
2143                 sizeof(struct ipr_dump_version_entry) -
2144                 sizeof(struct ipr_dump_entry_header);
2145         driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2146         driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
2147         strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
2148         driver_dump->hdr.num_entries++;
2149 }
2150 
2151 /**
2152  * ipr_dump_trace_data - Fill in the IOA trace in the dump.
2153  * @ioa_cfg:    ioa config struct
2154  * @driver_dump:        driver dump struct
2155  *
2156  * Return value:
2157  *      nothing
2158  **/
2159 static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
2160                                    struct ipr_driver_dump *driver_dump)
2161 {
2162         ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
2163         driver_dump->trace_entry.hdr.len =
2164                 sizeof(struct ipr_dump_trace_entry) -
2165                 sizeof(struct ipr_dump_entry_header);
2166         driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2167         driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
2168         memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
2169         driver_dump->hdr.num_entries++;
2170 }
2171 
2172 /**
2173  * ipr_dump_location_data - Fill in the IOA location in the dump.
2174  * @ioa_cfg:    ioa config struct
2175  * @driver_dump:        driver dump struct
2176  *
2177  * Return value:
2178  *      nothing
2179  **/
2180 static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
2181                                    struct ipr_driver_dump *driver_dump)
2182 {
2183         ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
2184         driver_dump->location_entry.hdr.len =
2185                 sizeof(struct ipr_dump_location_entry) -
2186                 sizeof(struct ipr_dump_entry_header);
2187         driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2188         driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
2189         strcpy(driver_dump->location_entry.location, dev_name(&ioa_cfg->pdev->dev));
2190         driver_dump->hdr.num_entries++;
2191 }
2192 
2193 /**
2194  * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
2195  * @ioa_cfg:    ioa config struct
2196  * @dump:               dump struct
2197  *
2198  * Return value:
2199  *      nothing
2200  **/
2201 static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
2202 {
2203         unsigned long start_addr, sdt_word;
2204         unsigned long lock_flags = 0;
2205         struct ipr_driver_dump *driver_dump = &dump->driver_dump;
2206         struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
2207         u32 num_entries, start_off, end_off;
2208         u32 bytes_to_copy, bytes_copied, rc;
2209         struct ipr_sdt *sdt;
2210         int i;
2211 
2212         ENTER;
2213 
2214         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2215 
2216         if (ioa_cfg->sdt_state != GET_DUMP) {
2217                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2218                 return;
2219         }
2220 
2221         start_addr = readl(ioa_cfg->ioa_mailbox);
2222 
2223         if (!ipr_sdt_is_fmt2(start_addr)) {
2224                 dev_err(&ioa_cfg->pdev->dev,
2225                         "Invalid dump table format: %lx\n", start_addr);
2226                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2227                 return;
2228         }
2229 
2230         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
2231 
2232         driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
2233 
2234         /* Initialize the overall dump header */
2235         driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
2236         driver_dump->hdr.num_entries = 1;
2237         driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
2238         driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
2239         driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
2240         driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
2241 
2242         ipr_dump_version_data(ioa_cfg, driver_dump);
2243         ipr_dump_location_data(ioa_cfg, driver_dump);
2244         ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
2245         ipr_dump_trace_data(ioa_cfg, driver_dump);
2246 
2247         /* Update dump_header */
2248         driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
2249 
2250         /* IOA Dump entry */
2251         ipr_init_dump_entry_hdr(&ioa_dump->hdr);
2252         ioa_dump->format = IPR_SDT_FMT2;
2253         ioa_dump->hdr.len = 0;
2254         ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2255         ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
2256 
2257         /* First entries in sdt are actually a list of dump addresses and
2258          lengths to gather the real dump data.  sdt represents the pointer
2259          to the ioa generated dump table.  Dump data will be extracted based
2260          on entries in this table */
2261         sdt = &ioa_dump->sdt;
2262 
2263         rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
2264                                         sizeof(struct ipr_sdt) / sizeof(__be32));
2265 
2266         /* Smart Dump table is ready to use and the first entry is valid */
2267         if (rc || (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE)) {
2268                 dev_err(&ioa_cfg->pdev->dev,
2269                         "Dump of IOA failed. Dump table not valid: %d, %X.\n",
2270                         rc, be32_to_cpu(sdt->hdr.state));
2271                 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
2272                 ioa_cfg->sdt_state = DUMP_OBTAINED;
2273                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2274                 return;
2275         }
2276 
2277         num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
2278 
2279         if (num_entries > IPR_NUM_SDT_ENTRIES)
2280                 num_entries = IPR_NUM_SDT_ENTRIES;
2281 
2282         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2283 
2284         for (i = 0; i < num_entries; i++) {
2285                 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
2286                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2287                         break;
2288                 }
2289 
2290                 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
2291                         sdt_word = be32_to_cpu(sdt->entry[i].bar_str_offset);
2292                         start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
2293                         end_off = be32_to_cpu(sdt->entry[i].end_offset);
2294 
2295                         if (ipr_sdt_is_fmt2(sdt_word) && sdt_word) {
2296                                 bytes_to_copy = end_off - start_off;
2297                                 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
2298                                         sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
2299                                         continue;
2300                                 }
2301 
2302                                 /* Copy data from adapter to driver buffers */
2303                                 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
2304                                                             bytes_to_copy);
2305 
2306                                 ioa_dump->hdr.len += bytes_copied;
2307 
2308                                 if (bytes_copied != bytes_to_copy) {
2309                                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2310                                         break;
2311                                 }
2312                         }
2313                 }
2314         }
2315 
2316         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
2317 
2318         /* Update dump_header */
2319         driver_dump->hdr.len += ioa_dump->hdr.len;
2320         wmb();
2321         ioa_cfg->sdt_state = DUMP_OBTAINED;
2322         LEAVE;
2323 }
2324 
2325 #else
2326 #define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
2327 #endif
2328 
2329 /**
2330  * ipr_release_dump - Free adapter dump memory
2331  * @kref:       kref struct
2332  *
2333  * Return value:
2334  *      nothing
2335  **/
2336 static void ipr_release_dump(struct kref *kref)
2337 {
2338         struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
2339         struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
2340         unsigned long lock_flags = 0;
2341         int i;
2342 
2343         ENTER;
2344         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2345         ioa_cfg->dump = NULL;
2346         ioa_cfg->sdt_state = INACTIVE;
2347         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2348 
2349         for (i = 0; i < dump->ioa_dump.next_page_index; i++)
2350                 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
2351 
2352         kfree(dump);
2353         LEAVE;
2354 }
2355 
2356 /**
2357  * ipr_worker_thread - Worker thread
2358  * @work:               ioa config struct
2359  *
2360  * Called at task level from a work thread. This function takes care
2361  * of adding and removing device from the mid-layer as configuration
2362  * changes are detected by the adapter.
2363  *
2364  * Return value:
2365  *      nothing
2366  **/
2367 static void ipr_worker_thread(struct work_struct *work)
2368 {
2369         unsigned long lock_flags;
2370         struct ipr_resource_entry *res;
2371         struct scsi_device *sdev;
2372         struct ipr_dump *dump;
2373         struct ipr_ioa_cfg *ioa_cfg =
2374                 container_of(work, struct ipr_ioa_cfg, work_q);
2375         u8 bus, target, lun;
2376         int did_work;
2377 
2378         ENTER;
2379         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2380 
2381         if (ioa_cfg->sdt_state == GET_DUMP) {
2382                 dump = ioa_cfg->dump;
2383                 if (!dump) {
2384                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2385                         return;
2386                 }
2387                 kref_get(&dump->kref);
2388                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2389                 ipr_get_ioa_dump(ioa_cfg, dump);
2390                 kref_put(&dump->kref, ipr_release_dump);
2391 
2392                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2393                 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
2394                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2395                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2396                 return;
2397         }
2398 
2399 restart:
2400         do {
2401                 did_work = 0;
2402                 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
2403                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2404                         return;
2405                 }
2406 
2407                 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2408                         if (res->del_from_ml && res->sdev) {
2409                                 did_work = 1;
2410                                 sdev = res->sdev;
2411                                 if (!scsi_device_get(sdev)) {
2412                                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
2413                                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2414                                         scsi_remove_device(sdev);
2415                                         scsi_device_put(sdev);
2416                                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2417                                 }
2418                                 break;
2419                         }
2420                 }
2421         } while(did_work);
2422 
2423         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2424                 if (res->add_to_ml) {
2425                         bus = res->cfgte.res_addr.bus;
2426                         target = res->cfgte.res_addr.target;
2427                         lun = res->cfgte.res_addr.lun;
2428                         res->add_to_ml = 0;
2429                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2430                         scsi_add_device(ioa_cfg->host, bus, target, lun);
2431                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2432                         goto restart;
2433                 }
2434         }
2435 
2436         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2437         kobject_uevent(&ioa_cfg->host->shost_dev.kobj, KOBJ_CHANGE);
2438         LEAVE;
2439 }
2440 
2441 #ifdef CONFIG_SCSI_IPR_TRACE
2442 /**
2443  * ipr_read_trace - Dump the adapter trace
2444  * @kobj:               kobject struct
2445  * @bin_attr:           bin_attribute struct
2446  * @buf:                buffer
2447  * @off:                offset
2448  * @count:              buffer size
2449  *
2450  * Return value:
2451  *      number of bytes printed to buffer
2452  **/
2453 static ssize_t ipr_read_trace(struct kobject *kobj,
2454                               struct bin_attribute *bin_attr,
2455                               char *buf, loff_t off, size_t count)
2456 {
2457         struct device *dev = container_of(kobj, struct device, kobj);
2458         struct Scsi_Host *shost = class_to_shost(dev);
2459         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2460         unsigned long lock_flags = 0;
2461         ssize_t ret;
2462 
2463         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2464         ret = memory_read_from_buffer(buf, count, &off, ioa_cfg->trace,
2465                                 IPR_TRACE_SIZE);
2466         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2467 
2468         return ret;
2469 }
2470 
2471 static struct bin_attribute ipr_trace_attr = {
2472         .attr = {
2473                 .name = "trace",
2474                 .mode = S_IRUGO,
2475         },
2476         .size = 0,
2477         .read = ipr_read_trace,
2478 };
2479 #endif
2480 
2481 static const struct {
2482         enum ipr_cache_state state;
2483         char *name;
2484 } cache_state [] = {
2485         { CACHE_NONE, "none" },
2486         { CACHE_DISABLED, "disabled" },
2487         { CACHE_ENABLED, "enabled" }
2488 };
2489 
2490 /**
2491  * ipr_show_write_caching - Show the write caching attribute
2492  * @dev:        device struct
2493  * @buf:        buffer
2494  *
2495  * Return value:
2496  *      number of bytes printed to buffer
2497  **/
2498 static ssize_t ipr_show_write_caching(struct device *dev,
2499                                       struct device_attribute *attr, char *buf)
2500 {
2501         struct Scsi_Host *shost = class_to_shost(dev);
2502         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2503         unsigned long lock_flags = 0;
2504         int i, len = 0;
2505 
2506         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2507         for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2508                 if (cache_state[i].state == ioa_cfg->cache_state) {
2509                         len = snprintf(buf, PAGE_SIZE, "%s\n", cache_state[i].name);
2510                         break;
2511                 }
2512         }
2513         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2514         return len;
2515 }
2516 
2517 
2518 /**
2519  * ipr_store_write_caching - Enable/disable adapter write cache
2520  * @dev:        device struct
2521  * @buf:        buffer
2522  * @count:      buffer size
2523  *
2524  * This function will enable/disable adapter write cache.
2525  *
2526  * Return value:
2527  *      count on success / other on failure
2528  **/
2529 static ssize_t ipr_store_write_caching(struct device *dev,
2530                                        struct device_attribute *attr,
2531                                        const char *buf, size_t count)
2532 {
2533         struct Scsi_Host *shost = class_to_shost(dev);
2534         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2535         unsigned long lock_flags = 0;
2536         enum ipr_cache_state new_state = CACHE_INVALID;
2537         int i;
2538 
2539         if (!capable(CAP_SYS_ADMIN))
2540                 return -EACCES;
2541         if (ioa_cfg->cache_state == CACHE_NONE)
2542                 return -EINVAL;
2543 
2544         for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2545                 if (!strncmp(cache_state[i].name, buf, strlen(cache_state[i].name))) {
2546                         new_state = cache_state[i].state;
2547                         break;
2548                 }
2549         }
2550 
2551         if (new_state != CACHE_DISABLED && new_state != CACHE_ENABLED)
2552                 return -EINVAL;
2553 
2554         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2555         if (ioa_cfg->cache_state == new_state) {
2556                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2557                 return count;
2558         }
2559 
2560         ioa_cfg->cache_state = new_state;
2561         dev_info(&ioa_cfg->pdev->dev, "%s adapter write cache.\n",
2562                  new_state == CACHE_ENABLED ? "Enabling" : "Disabling");
2563         if (!ioa_cfg->in_reset_reload)
2564                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2565         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2566         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2567 
2568         return count;
2569 }
2570 
2571 static struct device_attribute ipr_ioa_cache_attr = {
2572         .attr = {
2573                 .name =         "write_cache",
2574                 .mode =         S_IRUGO | S_IWUSR,
2575         },
2576         .show = ipr_show_write_caching,
2577         .store = ipr_store_write_caching
2578 };
2579 
2580 /**
2581  * ipr_show_fw_version - Show the firmware version
2582  * @dev:        class device struct
2583  * @buf:        buffer
2584  *
2585  * Return value:
2586  *      number of bytes printed to buffer
2587  **/
2588 static ssize_t ipr_show_fw_version(struct device *dev,
2589                                    struct device_attribute *attr, char *buf)
2590 {
2591         struct Scsi_Host *shost = class_to_shost(dev);
2592         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2593         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2594         unsigned long lock_flags = 0;
2595         int len;
2596 
2597         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2598         len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
2599                        ucode_vpd->major_release, ucode_vpd->card_type,
2600                        ucode_vpd->minor_release[0],
2601                        ucode_vpd->minor_release[1]);
2602         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2603         return len;
2604 }
2605 
2606 static struct device_attribute ipr_fw_version_attr = {
2607         .attr = {
2608                 .name =         "fw_version",
2609                 .mode =         S_IRUGO,
2610         },
2611         .show = ipr_show_fw_version,
2612 };
2613 
2614 /**
2615  * ipr_show_log_level - Show the adapter's error logging level
2616  * @dev:        class device struct
2617  * @buf:        buffer
2618  *
2619  * Return value:
2620  *      number of bytes printed to buffer
2621  **/
2622 static ssize_t ipr_show_log_level(struct device *dev,
2623                                    struct device_attribute *attr, char *buf)
2624 {
2625         struct Scsi_Host *shost = class_to_shost(dev);
2626         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2627         unsigned long lock_flags = 0;
2628         int len;
2629 
2630         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2631         len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
2632         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2633         return len;
2634 }
2635 
2636 /**
2637  * ipr_store_log_level - Change the adapter's error logging level
2638  * @dev:        class device struct
2639  * @buf:        buffer
2640  *
2641  * Return value:
2642  *      number of bytes printed to buffer
2643  **/
2644 static ssize_t ipr_store_log_level(struct device *dev,
2645                                    struct device_attribute *attr,
2646                                    const char *buf, size_t count)
2647 {
2648         struct Scsi_Host *shost = class_to_shost(dev);
2649         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2650         unsigned long lock_flags = 0;
2651 
2652         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2653         ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
2654         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2655         return strlen(buf);
2656 }
2657 
2658 static struct device_attribute ipr_log_level_attr = {
2659         .attr = {
2660                 .name =         "log_level",
2661                 .mode =         S_IRUGO | S_IWUSR,
2662         },
2663         .show = ipr_show_log_level,
2664         .store = ipr_store_log_level
2665 };
2666 
2667 /**
2668  * ipr_store_diagnostics - IOA Diagnostics interface
2669  * @dev:        device struct
2670  * @buf:        buffer
2671  * @count:      buffer size
2672  *
2673  * This function will reset the adapter and wait a reasonable
2674  * amount of time for any errors that the adapter might log.
2675  *
2676  * Return value:
2677  *      count on success / other on failure
2678  **/
2679 static ssize_t ipr_store_diagnostics(struct device *dev,
2680                                      struct device_attribute *attr,
2681                                      const char *buf, size_t count)
2682 {
2683         struct Scsi_Host *shost = class_to_shost(dev);
2684         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2685         unsigned long lock_flags = 0;
2686         int rc = count;
2687 
2688         if (!capable(CAP_SYS_ADMIN))
2689                 return -EACCES;
2690 
2691         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2692         while(ioa_cfg->in_reset_reload) {
2693                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2694                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2695                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2696         }
2697 
2698         ioa_cfg->errors_logged = 0;
2699         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2700 
2701         if (ioa_cfg->in_reset_reload) {
2702                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2703                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2704 
2705                 /* Wait for a second for any errors to be logged */
2706                 msleep(1000);
2707         } else {
2708                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2709                 return -EIO;
2710         }
2711 
2712         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2713         if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
2714                 rc = -EIO;
2715         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2716 
2717         return rc;
2718 }
2719 
2720 static struct device_attribute ipr_diagnostics_attr = {
2721         .attr = {
2722                 .name =         "run_diagnostics",
2723                 .mode =         S_IWUSR,
2724         },
2725         .store = ipr_store_diagnostics
2726 };
2727 
2728 /**
2729  * ipr_show_adapter_state - Show the adapter's state
2730  * @class_dev:  device struct
2731  * @buf:        buffer
2732  *
2733  * Return value:
2734  *      number of bytes printed to buffer
2735  **/
2736 static ssize_t ipr_show_adapter_state(struct device *dev,
2737                                       struct device_attribute *attr, char *buf)
2738 {
2739         struct Scsi_Host *shost = class_to_shost(dev);
2740         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2741         unsigned long lock_flags = 0;
2742         int len;
2743 
2744         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2745         if (ioa_cfg->ioa_is_dead)
2746                 len = snprintf(buf, PAGE_SIZE, "offline\n");
2747         else
2748                 len = snprintf(buf, PAGE_SIZE, "online\n");
2749         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2750         return len;
2751 }
2752 
2753 /**
2754  * ipr_store_adapter_state - Change adapter state
2755  * @dev:        device struct
2756  * @buf:        buffer
2757  * @count:      buffer size
2758  *
2759  * This function will change the adapter's state.
2760  *
2761  * Return value:
2762  *      count on success / other on failure
2763  **/
2764 static ssize_t ipr_store_adapter_state(struct device *dev,
2765                                        struct device_attribute *attr,
2766                                        const char *buf, size_t count)
2767 {
2768         struct Scsi_Host *shost = class_to_shost(dev);
2769         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2770         unsigned long lock_flags;
2771         int result = count;
2772 
2773         if (!capable(CAP_SYS_ADMIN))
2774                 return -EACCES;
2775 
2776         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2777         if (ioa_cfg->ioa_is_dead && !strncmp(buf, "online", 6)) {
2778                 ioa_cfg->ioa_is_dead = 0;
2779                 ioa_cfg->reset_retries = 0;
2780                 ioa_cfg->in_ioa_bringdown = 0;
2781                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2782         }
2783         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2784         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2785 
2786         return result;
2787 }
2788 
2789 static struct device_attribute ipr_ioa_state_attr = {
2790         .attr = {
2791                 .name =         "online_state",
2792                 .mode =         S_IRUGO | S_IWUSR,
2793         },
2794         .show = ipr_show_adapter_state,
2795         .store = ipr_store_adapter_state
2796 };
2797 
2798 /**
2799  * ipr_store_reset_adapter - Reset the adapter
2800  * @dev:        device struct
2801  * @buf:        buffer
2802  * @count:      buffer size
2803  *
2804  * This function will reset the adapter.
2805  *
2806  * Return value:
2807  *      count on success / other on failure
2808  **/
2809 static ssize_t ipr_store_reset_adapter(struct device *dev,
2810                                        struct device_attribute *attr,
2811                                        const char *buf, size_t count)
2812 {
2813         struct Scsi_Host *shost = class_to_shost(dev);
2814         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2815         unsigned long lock_flags;
2816         int result = count;
2817 
2818         if (!capable(CAP_SYS_ADMIN))
2819                 return -EACCES;
2820 
2821         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2822         if (!ioa_cfg->in_reset_reload)
2823                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2824         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2825         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2826 
2827         return result;
2828 }
2829 
2830 static struct device_attribute ipr_ioa_reset_attr = {
2831         .attr = {
2832                 .name =         "reset_host",
2833                 .mode =         S_IWUSR,
2834         },
2835         .store = ipr_store_reset_adapter
2836 };
2837 
2838 /**
2839  * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
2840  * @buf_len:            buffer length
2841  *
2842  * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
2843  * list to use for microcode download
2844  *
2845  * Return value:
2846  *      pointer to sglist / NULL on failure
2847  **/
2848 static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
2849 {
2850         int sg_size, order, bsize_elem, num_elem, i, j;
2851         struct ipr_sglist *sglist;
2852         struct scatterlist *scatterlist;
2853         struct page *page;
2854 
2855         /* Get the minimum size per scatter/gather element */
2856         sg_size = buf_len / (IPR_MAX_SGLIST - 1);
2857 
2858         /* Get the actual size per element */
2859         order = get_order(sg_size);
2860 
2861         /* Determine the actual number of bytes per element */
2862         bsize_elem = PAGE_SIZE * (1 << order);
2863 
2864         /* Determine the actual number of sg entries needed */
2865         if (buf_len % bsize_elem)
2866                 num_elem = (buf_len / bsize_elem) + 1;
2867         else
2868                 num_elem = buf_len / bsize_elem;
2869 
2870         /* Allocate a scatter/gather list for the DMA */
2871         sglist = kzalloc(sizeof(struct ipr_sglist) +
2872                          (sizeof(struct scatterlist) * (num_elem - 1)),
2873                          GFP_KERNEL);
2874 
2875         if (sglist == NULL) {
2876                 ipr_trace;
2877                 return NULL;
2878         }
2879 
2880         scatterlist = sglist->scatterlist;
2881         sg_init_table(scatterlist, num_elem);
2882 
2883         sglist->order = order;
2884         sglist->num_sg = num_elem;
2885 
2886         /* Allocate a bunch of sg elements */
2887         for (i = 0; i < num_elem; i++) {
2888                 page = alloc_pages(GFP_KERNEL, order);
2889                 if (!page) {
2890                         ipr_trace;
2891 
2892                         /* Free up what we already allocated */
2893                         for (j = i - 1; j >= 0; j--)
2894                                 __free_pages(sg_page(&scatterlist[j]), order);
2895                         kfree(sglist);
2896                         return NULL;
2897                 }
2898 
2899                 sg_set_page(&scatterlist[i], page, 0, 0);
2900         }
2901 
2902         return sglist;
2903 }
2904 
2905 /**
2906  * ipr_free_ucode_buffer - Frees a microcode download buffer
2907  * @p_dnld:             scatter/gather list pointer
2908  *
2909  * Free a DMA'able ucode download buffer previously allocated with
2910  * ipr_alloc_ucode_buffer
2911  *
2912  * Return value:
2913  *      nothing
2914  **/
2915 static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
2916 {
2917         int i;
2918 
2919         for (i = 0; i < sglist->num_sg; i++)
2920                 __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
2921 
2922         kfree(sglist);
2923 }
2924 
2925 /**
2926  * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
2927  * @sglist:             scatter/gather list pointer
2928  * @buffer:             buffer pointer
2929  * @len:                buffer length
2930  *
2931  * Copy a microcode image from a user buffer into a buffer allocated by
2932  * ipr_alloc_ucode_buffer
2933  *
2934  * Return value:
2935  *      0 on success / other on failure
2936  **/
2937 static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
2938                                  u8 *buffer, u32 len)
2939 {
2940         int bsize_elem, i, result = 0;
2941         struct scatterlist *scatterlist;
2942         void *kaddr;
2943 
2944         /* Determine the actual number of bytes per element */
2945         bsize_elem = PAGE_SIZE * (1 << sglist->order);
2946 
2947         scatterlist = sglist->scatterlist;
2948 
2949         for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
2950                 struct page *page = sg_page(&scatterlist[i]);
2951 
2952                 kaddr = kmap(page);
2953                 memcpy(kaddr, buffer, bsize_elem);
2954                 kunmap(page);
2955 
2956                 scatterlist[i].length = bsize_elem;
2957 
2958                 if (result != 0) {
2959                         ipr_trace;
2960                         return result;
2961                 }
2962         }
2963 
2964         if (len % bsize_elem) {
2965                 struct page *page = sg_page(&scatterlist[i]);
2966 
2967                 kaddr = kmap(page);
2968                 memcpy(kaddr, buffer, len % bsize_elem);
2969                 kunmap(page);
2970 
2971                 scatterlist[i].length = len % bsize_elem;
2972         }
2973 
2974         sglist->buffer_len = len;
2975         return result;
2976 }
2977 
2978 /**
2979  * ipr_build_ucode_ioadl - Build a microcode download IOADL
2980  * @ipr_cmd:    ipr command struct
2981  * @sglist:             scatter/gather list
2982  *
2983  * Builds a microcode download IOA data list (IOADL).
2984  *
2985  **/
2986 static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
2987                                   struct ipr_sglist *sglist)
2988 {
2989         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
2990         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
2991         struct scatterlist *scatterlist = sglist->scatterlist;
2992         int i;
2993 
2994         ipr_cmd->dma_use_sg = sglist->num_dma_sg;
2995         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
2996         ioarcb->write_data_transfer_length = cpu_to_be32(sglist->buffer_len);
2997         ioarcb->write_ioadl_len =
2998                 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
2999 
3000         for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3001                 ioadl[i].flags_and_data_len =
3002                         cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
3003                 ioadl[i].address =
3004                         cpu_to_be32(sg_dma_address(&scatterlist[i]));
3005         }
3006 
3007         ioadl[i-1].flags_and_data_len |=
3008                 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3009 }
3010 
3011 /**
3012  * ipr_update_ioa_ucode - Update IOA's microcode
3013  * @ioa_cfg:    ioa config struct
3014  * @sglist:             scatter/gather list
3015  *
3016  * Initiate an adapter reset to update the IOA's microcode
3017  *
3018  * Return value:
3019  *      0 on success / -EIO on failure
3020  **/
3021 static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
3022                                 struct ipr_sglist *sglist)
3023 {
3024         unsigned long lock_flags;
3025 
3026         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3027         while(ioa_cfg->in_reset_reload) {
3028                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3029                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3030                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3031         }
3032 
3033         if (ioa_cfg->ucode_sglist) {
3034                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3035                 dev_err(&ioa_cfg->pdev->dev,
3036                         "Microcode download already in progress\n");
3037                 return -EIO;
3038         }
3039 
3040         sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
3041                                         sglist->num_sg, DMA_TO_DEVICE);
3042 
3043         if (!sglist->num_dma_sg) {
3044                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3045                 dev_err(&ioa_cfg->pdev->dev,
3046                         "Failed to map microcode download buffer!\n");
3047                 return -EIO;
3048         }
3049 
3050         ioa_cfg->ucode_sglist = sglist;
3051         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3052         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3053         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3054 
3055         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3056         ioa_cfg->ucode_sglist = NULL;
3057         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3058         return 0;
3059 }
3060 
3061 /**
3062  * ipr_store_update_fw - Update the firmware on the adapter
3063  * @class_dev:  device struct
3064  * @buf:        buffer
3065  * @count:      buffer size
3066  *
3067  * This function will update the firmware on the adapter.
3068  *
3069  * Return value:
3070  *      count on success / other on failure
3071  **/
3072 static ssize_t ipr_store_update_fw(struct device *dev,
3073                                    struct device_attribute *attr,
3074                                    const char *buf, size_t count)
3075 {
3076         struct Scsi_Host *shost = class_to_shost(dev);
3077         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3078         struct ipr_ucode_image_header *image_hdr;
3079         const struct firmware *fw_entry;
3080         struct ipr_sglist *sglist;
3081         char fname[100];
3082         char *src;
3083         int len, result, dnld_size;
3084 
3085         if (!capable(CAP_SYS_ADMIN))
3086                 return -EACCES;
3087 
3088         len = snprintf(fname, 99, "%s", buf);
3089         fname[len-1] = '\0';
3090 
3091         if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
3092                 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
3093                 return -EIO;
3094         }
3095 
3096         image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
3097 
3098         if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
3099             (ioa_cfg->vpd_cbs->page3_data.card_type &&
3100              ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
3101                 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
3102                 release_firmware(fw_entry);
3103                 return -EINVAL;
3104         }
3105 
3106         src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
3107         dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
3108         sglist = ipr_alloc_ucode_buffer(dnld_size);
3109 
3110         if (!sglist) {
3111                 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
3112                 release_firmware(fw_entry);
3113                 return -ENOMEM;
3114         }
3115 
3116         result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
3117 
3118         if (result) {
3119                 dev_err(&ioa_cfg->pdev->dev,
3120                         "Microcode buffer copy to DMA buffer failed\n");
3121                 goto out;
3122         }
3123 
3124         result = ipr_update_ioa_ucode(ioa_cfg, sglist);
3125 
3126         if (!result)
3127                 result = count;
3128 out:
3129         ipr_free_ucode_buffer(sglist);
3130         release_firmware(fw_entry);
3131         return result;
3132 }
3133 
3134 static struct device_attribute ipr_update_fw_attr = {
3135         .attr = {
3136                 .name =         "update_fw",
3137                 .mode =         S_IWUSR,
3138         },
3139         .store = ipr_store_update_fw
3140 };
3141 
3142 static struct device_attribute *ipr_ioa_attrs[] = {
3143         &ipr_fw_version_attr,
3144         &ipr_log_level_attr,
3145         &ipr_diagnostics_attr,
3146         &ipr_ioa_state_attr,
3147         &ipr_ioa_reset_attr,
3148         &ipr_update_fw_attr,
3149         &ipr_ioa_cache_attr,
3150         NULL,
3151 };
3152 
3153 #ifdef CONFIG_SCSI_IPR_DUMP
3154 /**
3155  * ipr_read_dump - Dump the adapter
3156  * @kobj:               kobject struct
3157  * @bin_attr:           bin_attribute struct
3158  * @buf:                buffer
3159  * @off:                offset
3160  * @count:              buffer size
3161  *
3162  * Return value:
3163  *      number of bytes printed to buffer
3164  **/
3165 static ssize_t ipr_read_dump(struct kobject *kobj,
3166                              struct bin_attribute *bin_attr,
3167                              char *buf, loff_t off, size_t count)
3168 {
3169         struct device *cdev = container_of(kobj, struct device, kobj);
3170         struct Scsi_Host *shost = class_to_shost(cdev);
3171         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3172         struct ipr_dump *dump;
3173         unsigned long lock_flags = 0;
3174         char *src;
3175         int len;
3176         size_t rc = count;
3177 
3178         if (!capable(CAP_SYS_ADMIN))
3179                 return -EACCES;
3180 
3181         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3182         dump = ioa_cfg->dump;
3183 
3184         if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
3185                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3186                 return 0;
3187         }
3188         kref_get(&dump->kref);
3189         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3190 
3191         if (off > dump->driver_dump.hdr.len) {
3192                 kref_put(&dump->kref, ipr_release_dump);
3193                 return 0;
3194         }
3195 
3196         if (off + count > dump->driver_dump.hdr.len) {
3197                 count = dump->driver_dump.hdr.len - off;
3198                 rc = count;
3199         }
3200 
3201         if (count && off < sizeof(dump->driver_dump)) {
3202                 if (off + count > sizeof(dump->driver_dump))
3203                         len = sizeof(dump->driver_dump) - off;
3204                 else
3205                         len = count;
3206                 src = (u8 *)&dump->driver_dump + off;
3207                 memcpy(buf, src, len);
3208                 buf += len;
3209                 off += len;
3210                 count -= len;
3211         }
3212 
3213         off -= sizeof(dump->driver_dump);
3214 
3215         if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
3216                 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
3217                         len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
3218                 else
3219                         len = count;
3220                 src = (u8 *)&dump->ioa_dump + off;
3221                 memcpy(buf, src, len);
3222                 buf += len;
3223                 off += len;
3224                 count -= len;
3225         }
3226 
3227         off -= offsetof(struct ipr_ioa_dump, ioa_data);
3228 
3229         while (count) {
3230                 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
3231                         len = PAGE_ALIGN(off) - off;
3232                 else
3233                         len = count;
3234                 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
3235                 src += off & ~PAGE_MASK;
3236                 memcpy(buf, src, len);
3237                 buf += len;
3238                 off += len;
3239                 count -= len;
3240         }
3241 
3242         kref_put(&dump->kref, ipr_release_dump);
3243         return rc;
3244 }
3245 
3246 /**
3247  * ipr_alloc_dump - Prepare for adapter dump
3248  * @ioa_cfg:    ioa config struct
3249  *
3250  * Return value:
3251  *      0 on success / other on failure
3252  **/
3253 static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
3254 {
3255         struct ipr_dump *dump;
3256         unsigned long lock_flags = 0;
3257 
3258         dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
3259 
3260         if (!dump) {
3261                 ipr_err("Dump memory allocation failed\n");
3262                 return -ENOMEM;
3263         }
3264 
3265         kref_init(&dump->kref);
3266         dump->ioa_cfg = ioa_cfg;
3267 
3268         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3269 
3270         if (INACTIVE != ioa_cfg->sdt_state) {
3271                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3272                 kfree(dump);
3273                 return 0;
3274         }
3275 
3276         ioa_cfg->dump = dump;
3277         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
3278         if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
3279                 ioa_cfg->dump_taken = 1;
3280                 schedule_work(&ioa_cfg->work_q);
3281         }
3282         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3283 
3284         return 0;
3285 }
3286 
3287 /**
3288  * ipr_free_dump - Free adapter dump memory
3289  * @ioa_cfg:    ioa config struct
3290  *
3291  * Return value:
3292  *      0 on success / other on failure
3293  **/
3294 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
3295 {
3296         struct ipr_dump *dump;
3297         unsigned long lock_flags = 0;
3298 
3299         ENTER;
3300 
3301         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3302         dump = ioa_cfg->dump;
3303         if (!dump) {
3304                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3305                 return 0;
3306         }
3307 
3308         ioa_cfg->dump = NULL;
3309         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3310 
3311         kref_put(&dump->kref, ipr_release_dump);
3312 
3313         LEAVE;
3314         return 0;
3315 }
3316 
3317 /**
3318  * ipr_write_dump - Setup dump state of adapter
3319  * @kobj:               kobject struct
3320  * @bin_attr:           bin_attribute struct
3321  * @buf:                buffer
3322  * @off:                offset
3323  * @count:              buffer size
3324  *
3325  * Return value:
3326  *      number of bytes printed to buffer
3327  **/
3328 static ssize_t ipr_write_dump(struct kobject *kobj,
3329                               struct bin_attribute *bin_attr,
3330                               char *buf, loff_t off, size_t count)
3331 {
3332         struct device *cdev = container_of(kobj, struct device, kobj);
3333         struct Scsi_Host *shost = class_to_shost(cdev);
3334         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3335         int rc;
3336 
3337         if (!capable(CAP_SYS_ADMIN))
3338                 return -EACCES;
3339 
3340         if (buf[0] == '1')
3341                 rc = ipr_alloc_dump(ioa_cfg);
3342         else if (buf[0] == '')
3343                 rc = ipr_free_dump(ioa_cfg);
3344         else
3345                 return -EINVAL;
3346 
3347         if (rc)
3348                 return rc;
3349         else
3350                 return count;
3351 }
3352 
3353 static struct bin_attribute ipr_dump_attr = {
3354         .attr = {
3355                 .name = "dump",
3356                 .mode = S_IRUSR | S_IWUSR,
3357         },
3358         .size = 0,
3359         .read = ipr_read_dump,
3360         .write = ipr_write_dump
3361 };
3362 #else
3363 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
3364 #endif
3365 
3366 /**
3367  * ipr_change_queue_depth - Change the device's queue depth
3368  * @sdev:       scsi device struct
3369  * @qdepth:     depth to set
3370  *
3371  * Return value:
3372  *      actual depth set
3373  **/
3374 static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
3375 {
3376         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3377         struct ipr_resource_entry *res;
3378         unsigned long lock_flags = 0;
3379 
3380         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3381         res = (struct ipr_resource_entry *)sdev->hostdata;
3382 
3383         if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
3384                 qdepth = IPR_MAX_CMD_PER_ATA_LUN;
3385         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3386 
3387         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3388         return sdev->queue_depth;
3389 }
3390 
3391 /**
3392  * ipr_change_queue_type - Change the device's queue type
3393  * @dsev:               scsi device struct
3394  * @tag_type:   type of tags to use
3395  *
3396  * Return value:
3397  *      actual queue type set
3398  **/
3399 static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
3400 {
3401         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3402         struct ipr_resource_entry *res;
3403         unsigned long lock_flags = 0;
3404 
3405         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3406         res = (struct ipr_resource_entry *)sdev->hostdata;
3407 
3408         if (res) {
3409                 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
3410                         /*
3411                          * We don't bother quiescing the device here since the
3412                          * adapter firmware does it for us.
3413                          */
3414                         scsi_set_tag_type(sdev, tag_type);
3415 
3416                         if (tag_type)
3417                                 scsi_activate_tcq(sdev, sdev->queue_depth);
3418                         else
3419                                 scsi_deactivate_tcq(sdev, sdev->queue_depth);
3420                 } else
3421                         tag_type = 0;
3422         } else
3423                 tag_type = 0;
3424 
3425         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3426         return tag_type;
3427 }
3428 
3429 /**
3430  * ipr_show_adapter_handle - Show the adapter's resource handle for this device
3431  * @dev:        device struct
3432  * @buf:        buffer
3433  *
3434  * Return value:
3435  *      number of bytes printed to buffer
3436  **/
3437 static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
3438 {
3439         struct scsi_device *sdev = to_scsi_device(dev);
3440         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3441         struct ipr_resource_entry *res;
3442         unsigned long lock_flags = 0;
3443         ssize_t len = -ENXIO;
3444 
3445         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3446         res = (struct ipr_resource_entry *)sdev->hostdata;
3447         if (res)
3448                 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->cfgte.res_handle);
3449         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3450         return len;
3451 }
3452 
3453 static struct device_attribute ipr_adapter_handle_attr = {
3454         .attr = {
3455                 .name =         "adapter_handle",
3456                 .mode =         S_IRUSR,
3457         },
3458         .show = ipr_show_adapter_handle
3459 };
3460 
3461 static struct device_attribute *ipr_dev_attrs[] = {
3462         &ipr_adapter_handle_attr,
3463         NULL,
3464 };
3465 
3466 /**
3467  * ipr_biosparam - Return the HSC mapping
3468  * @sdev:                       scsi device struct
3469  * @block_device:       block device pointer
3470  * @capacity:           capacity of the device
3471  * @parm:                       Array containing returned HSC values.
3472  *
3473  * This function generates the HSC parms that fdisk uses.
3474  * We want to make sure we return something that places partitions
3475  * on 4k boundaries for best performance with the IOA.
3476  *
3477  * Return value:
3478  *      0 on success
3479  **/
3480 static int ipr_biosparam(struct scsi_device *sdev,
3481                          struct block_device *block_device,
3482                          sector_t capacity, int *parm)
3483 {
3484         int heads, sectors;
3485         sector_t cylinders;
3486 
3487         heads = 128;
3488         sectors = 32;
3489 
3490         cylinders = capacity;
3491         sector_div(cylinders, (128 * 32));
3492 
3493         /* return result */
3494         parm[0] = heads;
3495         parm[1] = sectors;
3496         parm[2] = cylinders;
3497 
3498         return 0;
3499 }
3500 
3501 /**
3502  * ipr_find_starget - Find target based on bus/target.
3503  * @starget:    scsi target struct
3504  *
3505  * Return value:
3506  *      resource entry pointer if found / NULL if not found
3507  **/
3508 static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
3509 {
3510         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3511         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
3512         struct ipr_resource_entry *res;
3513 
3514         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3515                 if ((res->cfgte.res_addr.bus == starget->channel) &&
3516                     (res->cfgte.res_addr.target == starget->id) &&
3517                     (res->cfgte.res_addr.lun == 0)) {
3518                         return res;
3519                 }
3520         }
3521 
3522         return NULL;
3523 }
3524 
3525 static struct ata_port_info sata_port_info;
3526 
3527 /**
3528  * ipr_target_alloc - Prepare for commands to a SCSI target
3529  * @starget:    scsi target struct
3530  *
3531  * If the device is a SATA device, this function allocates an
3532  * ATA port with libata, else it does nothing.
3533  *
3534  * Return value:
3535  *      0 on success / non-0 on failure
3536  **/
3537 static int ipr_target_alloc(struct scsi_target *starget)
3538 {
3539         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3540         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
3541         struct ipr_sata_port *sata_port;
3542         struct ata_port *ap;
3543         struct ipr_resource_entry *res;
3544         unsigned long lock_flags;
3545 
3546         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3547         res = ipr_find_starget(starget);
3548         starget->hostdata = NULL;
3549 
3550         if (res && ipr_is_gata(res)) {
3551                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3552                 sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
3553                 if (!sata_port)
3554                         return -ENOMEM;
3555 
3556                 ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
3557                 if (ap) {
3558                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3559                         sata_port->ioa_cfg = ioa_cfg;
3560                         sata_port->ap = ap;
3561                         sata_port->res = res;
3562 
3563                         res->sata_port = sata_port;
3564                         ap->private_data = sata_port;
3565                         starget->hostdata = sata_port;
3566                 } else {
3567                         kfree(sata_port);
3568                         return -ENOMEM;
3569                 }
3570         }
3571         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3572 
3573         return 0;
3574 }
3575 
3576 /**
3577  * ipr_target_destroy - Destroy a SCSI target
3578  * @starget:    scsi target struct
3579  *
3580  * If the device was a SATA device, this function frees the libata
3581  * ATA port, else it does nothing.
3582  *
3583  **/
3584 static void ipr_target_destroy(struct scsi_target *starget)
3585 {
3586         struct ipr_sata_port *sata_port = starget->hostdata;
3587 
3588         if (sata_port) {
3589                 starget->hostdata = NULL;
3590                 ata_sas_port_destroy(sata_port->ap);
3591                 kfree(sata_port);
3592         }
3593 }
3594 
3595 /**
3596  * ipr_find_sdev - Find device based on bus/target/lun.
3597  * @sdev:       scsi device struct
3598  *
3599  * Return value:
3600  *      resource entry pointer if found / NULL if not found
3601  **/
3602 static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
3603 {
3604         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3605         struct ipr_resource_entry *res;
3606 
3607         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3608                 if ((res->cfgte.res_addr.bus == sdev->channel) &&
3609                     (res->cfgte.res_addr.target == sdev->id) &&
3610                     (res->cfgte.res_addr.lun == sdev->lun))
3611                         return res;
3612         }
3613 
3614         return NULL;
3615 }
3616 
3617 /**
3618  * ipr_slave_destroy - Unconfigure a SCSI device
3619  * @sdev:       scsi device struct
3620  *
3621  * Return value:
3622  *      nothing
3623  **/
3624 static void ipr_slave_destroy(struct scsi_device *sdev)
3625 {
3626         struct ipr_resource_entry *res;
3627         struct ipr_ioa_cfg *ioa_cfg;
3628         unsigned long lock_flags = 0;
3629 
3630         ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3631 
3632         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3633         res = (struct ipr_resource_entry *) sdev->hostdata;
3634         if (res) {
3635                 if (res->sata_port)
3636                         ata_port_disable(res->sata_port->ap);
3637                 sdev->hostdata = NULL;
3638                 res->sdev = NULL;
3639                 res->sata_port = NULL;
3640         }
3641         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3642 }
3643 
3644 /**
3645  * ipr_slave_configure - Configure a SCSI device
3646  * @sdev:       scsi device struct
3647  *
3648  * This function configures the specified scsi device.
3649  *
3650  * Return value:
3651  *      0 on success
3652  **/
3653 static int ipr_slave_configure(struct scsi_device *sdev)
3654 {
3655         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3656         struct ipr_resource_entry *res;
3657         struct ata_port *ap = NULL;
3658         unsigned long lock_flags = 0;
3659 
3660         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3661         res = sdev->hostdata;
3662         if (res) {
3663                 if (ipr_is_af_dasd_device(res))
3664                         sdev->type = TYPE_RAID;
3665                 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
3666                         sdev->scsi_level = 4;
3667                         sdev->no_uld_attach = 1;
3668                 }
3669                 if (ipr_is_vset_device(res)) {
3670                         blk_queue_rq_timeout(sdev->request_queue,
3671                                              IPR_VSET_RW_TIMEOUT);
3672                         blk_queue_max_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
3673                 }
3674                 if (ipr_is_vset_device(res) || ipr_is_scsi_disk(res))
3675                         sdev->allow_restart = 1;
3676                 if (ipr_is_gata(res) && res->sata_port)
3677                         ap = res->sata_port->ap;
3678                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3679 
3680                 if (ap) {
3681                         scsi_adjust_queue_depth(sdev, 0, IPR_MAX_CMD_PER_ATA_LUN);
3682                         ata_sas_slave_configure(sdev, ap);
3683                 } else
3684                         scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
3685                 return 0;
3686         }
3687         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3688         return 0;
3689 }
3690 
3691 /**
3692  * ipr_ata_slave_alloc - Prepare for commands to a SATA device
3693  * @sdev:       scsi device struct
3694  *
3695  * This function initializes an ATA port so that future commands
3696  * sent through queuecommand will work.
3697  *
3698  * Return value:
3699  *      0 on success
3700  **/
3701 static int ipr_ata_slave_alloc(struct scsi_device *sdev)
3702 {
3703         struct ipr_sata_port *sata_port = NULL;
3704         int rc = -ENXIO;
3705 
3706         ENTER;
3707         if (sdev->sdev_target)
3708                 sata_port = sdev->sdev_target->hostdata;
3709         if (sata_port)
3710                 rc = ata_sas_port_init(sata_port->ap);
3711         if (rc)
3712                 ipr_slave_destroy(sdev);
3713 
3714         LEAVE;
3715         return rc;
3716 }
3717 
3718 /**
3719  * ipr_slave_alloc - Prepare for commands to a device.
3720  * @sdev:       scsi device struct
3721  *
3722  * This function saves a pointer to the resource entry
3723  * in the scsi device struct if the device exists. We
3724  * can then use this pointer in ipr_queuecommand when
3725  * handling new commands.
3726  *
3727  * Return value:
3728  *      0 on success / -ENXIO if device does not exist
3729  **/
3730 static int ipr_slave_alloc(struct scsi_device *sdev)
3731 {
3732         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3733         struct ipr_resource_entry *res;
3734         unsigned long lock_flags;
3735         int rc = -ENXIO;
3736 
3737         sdev->hostdata = NULL;
3738 
3739         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3740 
3741         res = ipr_find_sdev(sdev);
3742         if (res) {
3743                 res->sdev = sdev;
3744                 res->add_to_ml = 0;
3745                 res->in_erp = 0;
3746                 sdev->hostdata = res;
3747                 if (!ipr_is_naca_model(res))
3748                         res->needs_sync_complete = 1;
3749                 rc = 0;
3750                 if (ipr_is_gata(res)) {
3751                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3752                         return ipr_ata_slave_alloc(sdev);
3753                 }
3754         }
3755 
3756         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3757 
3758         return rc;
3759 }
3760 
3761 /**
3762  * ipr_eh_host_reset - Reset the host adapter
3763  * @scsi_cmd:   scsi command struct
3764  *
3765  * Return value:
3766  *      SUCCESS / FAILED
3767  **/
3768 static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
3769 {
3770         struct ipr_ioa_cfg *ioa_cfg;
3771         int rc;
3772 
3773         ENTER;
3774         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3775 
3776         dev_err(&ioa_cfg->pdev->dev,
3777                 "Adapter being reset as a result of error recovery.\n");
3778 
3779         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3780                 ioa_cfg->sdt_state = GET_DUMP;
3781 
3782         rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
3783 
3784         LEAVE;
3785         return rc;
3786 }
3787 
3788 static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
3789 {
3790         int rc;
3791 
3792         spin_lock_irq(cmd->device->host->host_lock);
3793         rc = __ipr_eh_host_reset(cmd);
3794         spin_unlock_irq(cmd->device->host->host_lock);
3795 
3796         return rc;
3797 }
3798 
3799 /**
3800  * ipr_device_reset - Reset the device
3801  * @ioa_cfg:    ioa config struct
3802  * @res:                resource entry struct
3803  *
3804  * This function issues a device reset to the affected device.
3805  * If the device is a SCSI device, a LUN reset will be sent
3806  * to the device first. If that does not work, a target reset
3807  * will be sent. If the device is a SATA device, a PHY reset will
3808  * be sent.
3809  *
3810  * Return value:
3811  *      0 on success / non-zero on failure
3812  **/
3813 static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
3814                             struct ipr_resource_entry *res)
3815 {
3816         struct ipr_cmnd *ipr_cmd;
3817         struct ipr_ioarcb *ioarcb;
3818         struct ipr_cmd_pkt *cmd_pkt;
3819         struct ipr_ioarcb_ata_regs *regs;
3820         u32 ioasc;
3821 
3822         ENTER;
3823         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3824         ioarcb = &ipr_cmd->ioarcb;
3825         cmd_pkt = &ioarcb->cmd_pkt;
3826         regs = &ioarcb->add_data.u.regs;
3827 
3828         ioarcb->res_handle = res->cfgte.res_handle;
3829         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3830         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
3831         if (ipr_is_gata(res)) {
3832                 cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
3833                 ioarcb->add_cmd_parms_len = cpu_to_be32(sizeof(regs->flags));
3834                 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
3835         }
3836 
3837         ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3838         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3839         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3840         if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET)
3841                 memcpy(&res->sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
3842                        sizeof(struct ipr_ioasa_gata));
3843 
3844         LEAVE;
3845         return (IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0);
3846 }
3847 
3848 /**
3849  * ipr_sata_reset - Reset the SATA port
3850  * @link:       SATA link to reset
3851  * @classes:    class of the attached device
3852  *
3853  * This function issues a SATA phy reset to the affected ATA link.
3854  *
3855  * Return value:
3856  *      0 on success / non-zero on failure
3857  **/
3858 static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
3859                                 unsigned long deadline)
3860 {
3861         struct ipr_sata_port *sata_port = link->ap->private_data;
3862         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
3863         struct ipr_resource_entry *res;
3864         unsigned long lock_flags = 0;
3865         int rc = -ENXIO;
3866 
3867         ENTER;
3868         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3869         while(ioa_cfg->in_reset_reload) {
3870                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3871                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3872                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3873         }
3874 
3875         res = sata_port->res;
3876         if (res) {
3877                 rc = ipr_device_reset(ioa_cfg, res);
3878