PCI: make pci_restore_state return void
[linux-2.6.git] / drivers / scsi / ipr.c
1 /*
2  * ipr.c -- driver for IBM Power Linux RAID adapters
3  *
4  * Written By: Brian King <brking@us.ibm.com>, IBM Corporation
5  *
6  * Copyright (C) 2003, 2004 IBM Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 /*
25  * Notes:
26  *
27  * This driver is used to control the following SCSI adapters:
28  *
29  * IBM iSeries: 5702, 5703, 2780, 5709, 570A, 570B
30  *
31  * IBM pSeries: PCI-X Dual Channel Ultra 320 SCSI RAID Adapter
32  *              PCI-X Dual Channel Ultra 320 SCSI Adapter
33  *              PCI-X Dual Channel Ultra 320 SCSI RAID Enablement Card
34  *              Embedded SCSI adapter on p615 and p655 systems
35  *
36  * Supported Hardware Features:
37  *      - Ultra 320 SCSI controller
38  *      - PCI-X host interface
39  *      - Embedded PowerPC RISC Processor and Hardware XOR DMA Engine
40  *      - Non-Volatile Write Cache
41  *      - Supports attachment of non-RAID disks, tape, and optical devices
42  *      - RAID Levels 0, 5, 10
43  *      - Hot spare
44  *      - Background Parity Checking
45  *      - Background Data Scrubbing
46  *      - Ability to increase the capacity of an existing RAID 5 disk array
47  *              by adding disks
48  *
49  * Driver Features:
50  *      - Tagged command queuing
51  *      - Adapter microcode download
52  *      - PCI hot plug
53  *      - SCSI device hot plug
54  *
55  */
56
57 #include <linux/fs.h>
58 #include <linux/init.h>
59 #include <linux/types.h>
60 #include <linux/errno.h>
61 #include <linux/kernel.h>
62 #include <linux/slab.h>
63 #include <linux/ioport.h>
64 #include <linux/delay.h>
65 #include <linux/pci.h>
66 #include <linux/wait.h>
67 #include <linux/spinlock.h>
68 #include <linux/sched.h>
69 #include <linux/interrupt.h>
70 #include <linux/blkdev.h>
71 #include <linux/firmware.h>
72 #include <linux/module.h>
73 #include <linux/moduleparam.h>
74 #include <linux/libata.h>
75 #include <linux/hdreg.h>
76 #include <linux/reboot.h>
77 #include <linux/stringify.h>
78 #include <asm/io.h>
79 #include <asm/irq.h>
80 #include <asm/processor.h>
81 #include <scsi/scsi.h>
82 #include <scsi/scsi_host.h>
83 #include <scsi/scsi_tcq.h>
84 #include <scsi/scsi_eh.h>
85 #include <scsi/scsi_cmnd.h>
86 #include "ipr.h"
87
88 /*
89  *   Global Data
90  */
91 static LIST_HEAD(ipr_ioa_head);
92 static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
93 static unsigned int ipr_max_speed = 1;
94 static int ipr_testmode = 0;
95 static unsigned int ipr_fastfail = 0;
96 static unsigned int ipr_transop_timeout = 0;
97 static unsigned int ipr_debug = 0;
98 static unsigned int ipr_max_devs = IPR_DEFAULT_SIS64_DEVS;
99 static unsigned int ipr_dual_ioa_raid = 1;
100 static DEFINE_SPINLOCK(ipr_driver_lock);
101
102 /* This table describes the differences between DMA controller chips */
103 static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
104         { /* Gemstone, Citrine, Obsidian, and Obsidian-E */
105                 .mailbox = 0x0042C,
106                 .cache_line_size = 0x20,
107                 {
108                         .set_interrupt_mask_reg = 0x0022C,
109                         .clr_interrupt_mask_reg = 0x00230,
110                         .clr_interrupt_mask_reg32 = 0x00230,
111                         .sense_interrupt_mask_reg = 0x0022C,
112                         .sense_interrupt_mask_reg32 = 0x0022C,
113                         .clr_interrupt_reg = 0x00228,
114                         .clr_interrupt_reg32 = 0x00228,
115                         .sense_interrupt_reg = 0x00224,
116                         .sense_interrupt_reg32 = 0x00224,
117                         .ioarrin_reg = 0x00404,
118                         .sense_uproc_interrupt_reg = 0x00214,
119                         .sense_uproc_interrupt_reg32 = 0x00214,
120                         .set_uproc_interrupt_reg = 0x00214,
121                         .set_uproc_interrupt_reg32 = 0x00214,
122                         .clr_uproc_interrupt_reg = 0x00218,
123                         .clr_uproc_interrupt_reg32 = 0x00218
124                 }
125         },
126         { /* Snipe and Scamp */
127                 .mailbox = 0x0052C,
128                 .cache_line_size = 0x20,
129                 {
130                         .set_interrupt_mask_reg = 0x00288,
131                         .clr_interrupt_mask_reg = 0x0028C,
132                         .clr_interrupt_mask_reg32 = 0x0028C,
133                         .sense_interrupt_mask_reg = 0x00288,
134                         .sense_interrupt_mask_reg32 = 0x00288,
135                         .clr_interrupt_reg = 0x00284,
136                         .clr_interrupt_reg32 = 0x00284,
137                         .sense_interrupt_reg = 0x00280,
138                         .sense_interrupt_reg32 = 0x00280,
139                         .ioarrin_reg = 0x00504,
140                         .sense_uproc_interrupt_reg = 0x00290,
141                         .sense_uproc_interrupt_reg32 = 0x00290,
142                         .set_uproc_interrupt_reg = 0x00290,
143                         .set_uproc_interrupt_reg32 = 0x00290,
144                         .clr_uproc_interrupt_reg = 0x00294,
145                         .clr_uproc_interrupt_reg32 = 0x00294
146                 }
147         },
148         { /* CRoC */
149                 .mailbox = 0x00040,
150                 .cache_line_size = 0x20,
151                 {
152                         .set_interrupt_mask_reg = 0x00010,
153                         .clr_interrupt_mask_reg = 0x00018,
154                         .clr_interrupt_mask_reg32 = 0x0001C,
155                         .sense_interrupt_mask_reg = 0x00010,
156                         .sense_interrupt_mask_reg32 = 0x00014,
157                         .clr_interrupt_reg = 0x00008,
158                         .clr_interrupt_reg32 = 0x0000C,
159                         .sense_interrupt_reg = 0x00000,
160                         .sense_interrupt_reg32 = 0x00004,
161                         .ioarrin_reg = 0x00070,
162                         .sense_uproc_interrupt_reg = 0x00020,
163                         .sense_uproc_interrupt_reg32 = 0x00024,
164                         .set_uproc_interrupt_reg = 0x00020,
165                         .set_uproc_interrupt_reg32 = 0x00024,
166                         .clr_uproc_interrupt_reg = 0x00028,
167                         .clr_uproc_interrupt_reg32 = 0x0002C,
168                         .init_feedback_reg = 0x0005C,
169                         .dump_addr_reg = 0x00064,
170                         .dump_data_reg = 0x00068,
171                         .endian_swap_reg = 0x00084
172                 }
173         },
174 };
175
176 static const struct ipr_chip_t ipr_chip[] = {
177         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
178         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
179         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
180         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
181         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, IPR_USE_MSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
182         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[1] },
183         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[1] },
184         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2, IPR_USE_MSI, IPR_SIS64, IPR_MMIO, &ipr_chip_cfg[2] },
185         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_ASIC_E2, IPR_USE_MSI, IPR_SIS64, IPR_MMIO, &ipr_chip_cfg[2] }
186 };
187
188 static int ipr_max_bus_speeds [] = {
189         IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
190 };
191
192 MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
193 MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
194 module_param_named(max_speed, ipr_max_speed, uint, 0);
195 MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
196 module_param_named(log_level, ipr_log_level, uint, 0);
197 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
198 module_param_named(testmode, ipr_testmode, int, 0);
199 MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
200 module_param_named(fastfail, ipr_fastfail, int, S_IRUGO | S_IWUSR);
201 MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
202 module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
203 MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
204 module_param_named(debug, ipr_debug, int, S_IRUGO | S_IWUSR);
205 MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
206 module_param_named(dual_ioa_raid, ipr_dual_ioa_raid, int, 0);
207 MODULE_PARM_DESC(dual_ioa_raid, "Enable dual adapter RAID support. Set to 1 to enable. (default: 1)");
208 module_param_named(max_devs, ipr_max_devs, int, 0);
209 MODULE_PARM_DESC(max_devs, "Specify the maximum number of physical devices. "
210                  "[Default=" __stringify(IPR_DEFAULT_SIS64_DEVS) "]");
211 MODULE_LICENSE("GPL");
212 MODULE_VERSION(IPR_DRIVER_VERSION);
213
214 /*  A constant array of IOASCs/URCs/Error Messages */
215 static const
216 struct ipr_error_table_t ipr_error_table[] = {
217         {0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
218         "8155: An unknown error was received"},
219         {0x00330000, 0, 0,
220         "Soft underlength error"},
221         {0x005A0000, 0, 0,
222         "Command to be cancelled not found"},
223         {0x00808000, 0, 0,
224         "Qualified success"},
225         {0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
226         "FFFE: Soft device bus error recovered by the IOA"},
227         {0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
228         "4101: Soft device bus fabric error"},
229         {0x01100100, 0, IPR_DEFAULT_LOG_LEVEL,
230         "FFFC: Logical block guard error recovered by the device"},
231         {0x01100300, 0, IPR_DEFAULT_LOG_LEVEL,
232         "FFFC: Logical block reference tag error recovered by the device"},
233         {0x01108300, 0, IPR_DEFAULT_LOG_LEVEL,
234         "4171: Recovered scatter list tag / sequence number error"},
235         {0x01109000, 0, IPR_DEFAULT_LOG_LEVEL,
236         "FF3D: Recovered logical block CRC error on IOA to Host transfer"},
237         {0x01109200, 0, IPR_DEFAULT_LOG_LEVEL,
238         "4171: Recovered logical block sequence number error on IOA to Host transfer"},
239         {0x0110A000, 0, IPR_DEFAULT_LOG_LEVEL,
240         "FFFD: Recovered logical block reference tag error detected by the IOA"},
241         {0x0110A100, 0, IPR_DEFAULT_LOG_LEVEL,
242         "FFFD: Logical block guard error recovered by the IOA"},
243         {0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
244         "FFF9: Device sector reassign successful"},
245         {0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
246         "FFF7: Media error recovered by device rewrite procedures"},
247         {0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
248         "7001: IOA sector reassignment successful"},
249         {0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
250         "FFF9: Soft media error. Sector reassignment recommended"},
251         {0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
252         "FFF7: Media error recovered by IOA rewrite procedures"},
253         {0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
254         "FF3D: Soft PCI bus error recovered by the IOA"},
255         {0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
256         "FFF6: Device hardware error recovered by the IOA"},
257         {0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
258         "FFF6: Device hardware error recovered by the device"},
259         {0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
260         "FF3D: Soft IOA error recovered by the IOA"},
261         {0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
262         "FFFA: Undefined device response recovered by the IOA"},
263         {0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
264         "FFF6: Device bus error, message or command phase"},
265         {0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
266         "FFFE: Task Management Function failed"},
267         {0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
268         "FFF6: Failure prediction threshold exceeded"},
269         {0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
270         "8009: Impending cache battery pack failure"},
271         {0x02040400, 0, 0,
272         "34FF: Disk device format in progress"},
273         {0x02048000, 0, IPR_DEFAULT_LOG_LEVEL,
274         "9070: IOA requested reset"},
275         {0x023F0000, 0, 0,
276         "Synchronization required"},
277         {0x024E0000, 0, 0,
278         "No ready, IOA shutdown"},
279         {0x025A0000, 0, 0,
280         "Not ready, IOA has been shutdown"},
281         {0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
282         "3020: Storage subsystem configuration error"},
283         {0x03110B00, 0, 0,
284         "FFF5: Medium error, data unreadable, recommend reassign"},
285         {0x03110C00, 0, 0,
286         "7000: Medium error, data unreadable, do not reassign"},
287         {0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
288         "FFF3: Disk media format bad"},
289         {0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
290         "3002: Addressed device failed to respond to selection"},
291         {0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
292         "3100: Device bus error"},
293         {0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
294         "3109: IOA timed out a device command"},
295         {0x04088000, 0, 0,
296         "3120: SCSI bus is not operational"},
297         {0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
298         "4100: Hard device bus fabric error"},
299         {0x04100100, 0, IPR_DEFAULT_LOG_LEVEL,
300         "310C: Logical block guard error detected by the device"},
301         {0x04100300, 0, IPR_DEFAULT_LOG_LEVEL,
302         "310C: Logical block reference tag error detected by the device"},
303         {0x04108300, 1, IPR_DEFAULT_LOG_LEVEL,
304         "4170: Scatter list tag / sequence number error"},
305         {0x04109000, 1, IPR_DEFAULT_LOG_LEVEL,
306         "8150: Logical block CRC error on IOA to Host transfer"},
307         {0x04109200, 1, IPR_DEFAULT_LOG_LEVEL,
308         "4170: Logical block sequence number error on IOA to Host transfer"},
309         {0x0410A000, 0, IPR_DEFAULT_LOG_LEVEL,
310         "310D: Logical block reference tag error detected by the IOA"},
311         {0x0410A100, 0, IPR_DEFAULT_LOG_LEVEL,
312         "310D: Logical block guard error detected by the IOA"},
313         {0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
314         "9000: IOA reserved area data check"},
315         {0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
316         "9001: IOA reserved area invalid data pattern"},
317         {0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
318         "9002: IOA reserved area LRC error"},
319         {0x04118300, 1, IPR_DEFAULT_LOG_LEVEL,
320         "Hardware Error, IOA metadata access error"},
321         {0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
322         "102E: Out of alternate sectors for disk storage"},
323         {0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
324         "FFF4: Data transfer underlength error"},
325         {0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
326         "FFF4: Data transfer overlength error"},
327         {0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
328         "3400: Logical unit failure"},
329         {0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
330         "FFF4: Device microcode is corrupt"},
331         {0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
332         "8150: PCI bus error"},
333         {0x04430000, 1, 0,
334         "Unsupported device bus message received"},
335         {0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
336         "FFF4: Disk device problem"},
337         {0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
338         "8150: Permanent IOA failure"},
339         {0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
340         "3010: Disk device returned wrong response to IOA"},
341         {0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
342         "8151: IOA microcode error"},
343         {0x04448500, 0, 0,
344         "Device bus status error"},
345         {0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
346         "8157: IOA error requiring IOA reset to recover"},
347         {0x04448700, 0, 0,
348         "ATA device status error"},
349         {0x04490000, 0, 0,
350         "Message reject received from the device"},
351         {0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
352         "8008: A permanent cache battery pack failure occurred"},
353         {0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
354         "9090: Disk unit has been modified after the last known status"},
355         {0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
356         "9081: IOA detected device error"},
357         {0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
358         "9082: IOA detected device error"},
359         {0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
360         "3110: Device bus error, message or command phase"},
361         {0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
362         "3110: SAS Command / Task Management Function failed"},
363         {0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
364         "9091: Incorrect hardware configuration change has been detected"},
365         {0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
366         "9073: Invalid multi-adapter configuration"},
367         {0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
368         "4010: Incorrect connection between cascaded expanders"},
369         {0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
370         "4020: Connections exceed IOA design limits"},
371         {0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
372         "4030: Incorrect multipath connection"},
373         {0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
374         "4110: Unsupported enclosure function"},
375         {0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
376         "FFF4: Command to logical unit failed"},
377         {0x05240000, 1, 0,
378         "Illegal request, invalid request type or request packet"},
379         {0x05250000, 0, 0,
380         "Illegal request, invalid resource handle"},
381         {0x05258000, 0, 0,
382         "Illegal request, commands not allowed to this device"},
383         {0x05258100, 0, 0,
384         "Illegal request, command not allowed to a secondary adapter"},
385         {0x05258200, 0, 0,
386         "Illegal request, command not allowed to a non-optimized resource"},
387         {0x05260000, 0, 0,
388         "Illegal request, invalid field in parameter list"},
389         {0x05260100, 0, 0,
390         "Illegal request, parameter not supported"},
391         {0x05260200, 0, 0,
392         "Illegal request, parameter value invalid"},
393         {0x052C0000, 0, 0,
394         "Illegal request, command sequence error"},
395         {0x052C8000, 1, 0,
396         "Illegal request, dual adapter support not enabled"},
397         {0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
398         "9031: Array protection temporarily suspended, protection resuming"},
399         {0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
400         "9040: Array protection temporarily suspended, protection resuming"},
401         {0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
402         "3140: Device bus not ready to ready transition"},
403         {0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
404         "FFFB: SCSI bus was reset"},
405         {0x06290500, 0, 0,
406         "FFFE: SCSI bus transition to single ended"},
407         {0x06290600, 0, 0,
408         "FFFE: SCSI bus transition to LVD"},
409         {0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
410         "FFFB: SCSI bus was reset by another initiator"},
411         {0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
412         "3029: A device replacement has occurred"},
413         {0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
414         "9051: IOA cache data exists for a missing or failed device"},
415         {0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
416         "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
417         {0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
418         "9025: Disk unit is not supported at its physical location"},
419         {0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
420         "3020: IOA detected a SCSI bus configuration error"},
421         {0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
422         "3150: SCSI bus configuration error"},
423         {0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
424         "9074: Asymmetric advanced function disk configuration"},
425         {0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
426         "4040: Incomplete multipath connection between IOA and enclosure"},
427         {0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
428         "4041: Incomplete multipath connection between enclosure and device"},
429         {0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
430         "9075: Incomplete multipath connection between IOA and remote IOA"},
431         {0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
432         "9076: Configuration error, missing remote IOA"},
433         {0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
434         "4050: Enclosure does not support a required multipath function"},
435         {0x06690000, 0, IPR_DEFAULT_LOG_LEVEL,
436         "4070: Logically bad block written on device"},
437         {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
438         "9041: Array protection temporarily suspended"},
439         {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
440         "9042: Corrupt array parity detected on specified device"},
441         {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
442         "9030: Array no longer protected due to missing or failed disk unit"},
443         {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
444         "9071: Link operational transition"},
445         {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
446         "9072: Link not operational transition"},
447         {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
448         "9032: Array exposed but still protected"},
449         {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
450         "70DD: Device forced failed by disrupt device command"},
451         {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
452         "4061: Multipath redundancy level got better"},
453         {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
454         "4060: Multipath redundancy level got worse"},
455         {0x07270000, 0, 0,
456         "Failure due to other device"},
457         {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
458         "9008: IOA does not support functions expected by devices"},
459         {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
460         "9010: Cache data associated with attached devices cannot be found"},
461         {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
462         "9011: Cache data belongs to devices other than those attached"},
463         {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
464         "9020: Array missing 2 or more devices with only 1 device present"},
465         {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
466         "9021: Array missing 2 or more devices with 2 or more devices present"},
467         {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
468         "9022: Exposed array is missing a required device"},
469         {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
470         "9023: Array member(s) not at required physical locations"},
471         {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
472         "9024: Array not functional due to present hardware configuration"},
473         {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
474         "9026: Array not functional due to present hardware configuration"},
475         {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
476         "9027: Array is missing a device and parity is out of sync"},
477         {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
478         "9028: Maximum number of arrays already exist"},
479         {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
480         "9050: Required cache data cannot be located for a disk unit"},
481         {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
482         "9052: Cache data exists for a device that has been modified"},
483         {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
484         "9054: IOA resources not available due to previous problems"},
485         {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
486         "9092: Disk unit requires initialization before use"},
487         {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
488         "9029: Incorrect hardware configuration change has been detected"},
489         {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
490         "9060: One or more disk pairs are missing from an array"},
491         {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
492         "9061: One or more disks are missing from an array"},
493         {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
494         "9062: One or more disks are missing from an array"},
495         {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
496         "9063: Maximum number of functional arrays has been exceeded"},
497         {0x0B260000, 0, 0,
498         "Aborted command, invalid descriptor"},
499         {0x0B5A0000, 0, 0,
500         "Command terminated by host"}
501 };
502
503 static const struct ipr_ses_table_entry ipr_ses_table[] = {
504         { "2104-DL1        ", "XXXXXXXXXXXXXXXX", 80 },
505         { "2104-TL1        ", "XXXXXXXXXXXXXXXX", 80 },
506         { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
507         { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
508         { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
509         { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
510         { "2104-DU3        ", "XXXXXXXXXXXXXXXX", 160 },
511         { "2104-TU3        ", "XXXXXXXXXXXXXXXX", 160 },
512         { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
513         { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
514         { "St  V1S2        ", "XXXXXXXXXXXXXXXX", 160 },
515         { "HSBPD4M  PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
516         { "VSBPD1H   U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
517 };
518
519 /*
520  *  Function Prototypes
521  */
522 static int ipr_reset_alert(struct ipr_cmnd *);
523 static void ipr_process_ccn(struct ipr_cmnd *);
524 static void ipr_process_error(struct ipr_cmnd *);
525 static void ipr_reset_ioa_job(struct ipr_cmnd *);
526 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
527                                    enum ipr_shutdown_type);
528
529 #ifdef CONFIG_SCSI_IPR_TRACE
530 /**
531  * ipr_trc_hook - Add a trace entry to the driver trace
532  * @ipr_cmd:    ipr command struct
533  * @type:               trace type
534  * @add_data:   additional data
535  *
536  * Return value:
537  *      none
538  **/
539 static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
540                          u8 type, u32 add_data)
541 {
542         struct ipr_trace_entry *trace_entry;
543         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
544
545         trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
546         trace_entry->time = jiffies;
547         trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
548         trace_entry->type = type;
549         if (ipr_cmd->ioa_cfg->sis64)
550                 trace_entry->ata_op_code = ipr_cmd->i.ata_ioadl.regs.command;
551         else
552                 trace_entry->ata_op_code = ipr_cmd->ioarcb.u.add_data.u.regs.command;
553         trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
554         trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
555         trace_entry->u.add_data = add_data;
556 }
557 #else
558 #define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
559 #endif
560
561 /**
562  * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
563  * @ipr_cmd:    ipr command struct
564  *
565  * Return value:
566  *      none
567  **/
568 static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
569 {
570         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
571         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
572         struct ipr_ioasa64 *ioasa64 = &ipr_cmd->s.ioasa64;
573         dma_addr_t dma_addr = ipr_cmd->dma_addr;
574
575         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
576         ioarcb->data_transfer_length = 0;
577         ioarcb->read_data_transfer_length = 0;
578         ioarcb->ioadl_len = 0;
579         ioarcb->read_ioadl_len = 0;
580
581         if (ipr_cmd->ioa_cfg->sis64) {
582                 ioarcb->u.sis64_addr_data.data_ioadl_addr =
583                         cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
584                 ioasa64->u.gata.status = 0;
585         } else {
586                 ioarcb->write_ioadl_addr =
587                         cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
588                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
589                 ioasa->u.gata.status = 0;
590         }
591
592         ioasa->hdr.ioasc = 0;
593         ioasa->hdr.residual_data_len = 0;
594         ipr_cmd->scsi_cmd = NULL;
595         ipr_cmd->qc = NULL;
596         ipr_cmd->sense_buffer[0] = 0;
597         ipr_cmd->dma_use_sg = 0;
598 }
599
600 /**
601  * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
602  * @ipr_cmd:    ipr command struct
603  *
604  * Return value:
605  *      none
606  **/
607 static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
608 {
609         ipr_reinit_ipr_cmnd(ipr_cmd);
610         ipr_cmd->u.scratch = 0;
611         ipr_cmd->sibling = NULL;
612         init_timer(&ipr_cmd->timer);
613 }
614
615 /**
616  * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
617  * @ioa_cfg:    ioa config struct
618  *
619  * Return value:
620  *      pointer to ipr command struct
621  **/
622 static
623 struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
624 {
625         struct ipr_cmnd *ipr_cmd;
626
627         ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
628         list_del(&ipr_cmd->queue);
629         ipr_init_ipr_cmnd(ipr_cmd);
630
631         return ipr_cmd;
632 }
633
634 /**
635  * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
636  * @ioa_cfg:    ioa config struct
637  * @clr_ints:     interrupts to clear
638  *
639  * This function masks all interrupts on the adapter, then clears the
640  * interrupts specified in the mask
641  *
642  * Return value:
643  *      none
644  **/
645 static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
646                                           u32 clr_ints)
647 {
648         volatile u32 int_reg;
649
650         /* Stop new interrupts */
651         ioa_cfg->allow_interrupts = 0;
652
653         /* Set interrupt mask to stop all new interrupts */
654         if (ioa_cfg->sis64)
655                 writeq(~0, ioa_cfg->regs.set_interrupt_mask_reg);
656         else
657                 writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
658
659         /* Clear any pending interrupts */
660         if (ioa_cfg->sis64)
661                 writel(~0, ioa_cfg->regs.clr_interrupt_reg);
662         writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg32);
663         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
664 }
665
666 /**
667  * ipr_save_pcix_cmd_reg - Save PCI-X command register
668  * @ioa_cfg:    ioa config struct
669  *
670  * Return value:
671  *      0 on success / -EIO on failure
672  **/
673 static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
674 {
675         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
676
677         if (pcix_cmd_reg == 0)
678                 return 0;
679
680         if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
681                                  &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
682                 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
683                 return -EIO;
684         }
685
686         ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
687         return 0;
688 }
689
690 /**
691  * ipr_set_pcix_cmd_reg - Setup PCI-X command register
692  * @ioa_cfg:    ioa config struct
693  *
694  * Return value:
695  *      0 on success / -EIO on failure
696  **/
697 static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
698 {
699         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
700
701         if (pcix_cmd_reg) {
702                 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
703                                           ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
704                         dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
705                         return -EIO;
706                 }
707         }
708
709         return 0;
710 }
711
712 /**
713  * ipr_sata_eh_done - done function for aborted SATA commands
714  * @ipr_cmd:    ipr command struct
715  *
716  * This function is invoked for ops generated to SATA
717  * devices which are being aborted.
718  *
719  * Return value:
720  *      none
721  **/
722 static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
723 {
724         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
725         struct ata_queued_cmd *qc = ipr_cmd->qc;
726         struct ipr_sata_port *sata_port = qc->ap->private_data;
727
728         qc->err_mask |= AC_ERR_OTHER;
729         sata_port->ioasa.status |= ATA_BUSY;
730         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
731         ata_qc_complete(qc);
732 }
733
734 /**
735  * ipr_scsi_eh_done - mid-layer done function for aborted ops
736  * @ipr_cmd:    ipr command struct
737  *
738  * This function is invoked by the interrupt handler for
739  * ops generated by the SCSI mid-layer which are being aborted.
740  *
741  * Return value:
742  *      none
743  **/
744 static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
745 {
746         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
747         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
748
749         scsi_cmd->result |= (DID_ERROR << 16);
750
751         scsi_dma_unmap(ipr_cmd->scsi_cmd);
752         scsi_cmd->scsi_done(scsi_cmd);
753         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
754 }
755
756 /**
757  * ipr_fail_all_ops - Fails all outstanding ops.
758  * @ioa_cfg:    ioa config struct
759  *
760  * This function fails all outstanding ops.
761  *
762  * Return value:
763  *      none
764  **/
765 static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
766 {
767         struct ipr_cmnd *ipr_cmd, *temp;
768
769         ENTER;
770         list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
771                 list_del(&ipr_cmd->queue);
772
773                 ipr_cmd->s.ioasa.hdr.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
774                 ipr_cmd->s.ioasa.hdr.ilid = cpu_to_be32(IPR_DRIVER_ILID);
775
776                 if (ipr_cmd->scsi_cmd)
777                         ipr_cmd->done = ipr_scsi_eh_done;
778                 else if (ipr_cmd->qc)
779                         ipr_cmd->done = ipr_sata_eh_done;
780
781                 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
782                 del_timer(&ipr_cmd->timer);
783                 ipr_cmd->done(ipr_cmd);
784         }
785
786         LEAVE;
787 }
788
789 /**
790  * ipr_send_command -  Send driver initiated requests.
791  * @ipr_cmd:            ipr command struct
792  *
793  * This function sends a command to the adapter using the correct write call.
794  * In the case of sis64, calculate the ioarcb size required. Then or in the
795  * appropriate bits.
796  *
797  * Return value:
798  *      none
799  **/
800 static void ipr_send_command(struct ipr_cmnd *ipr_cmd)
801 {
802         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
803         dma_addr_t send_dma_addr = ipr_cmd->dma_addr;
804
805         if (ioa_cfg->sis64) {
806                 /* The default size is 256 bytes */
807                 send_dma_addr |= 0x1;
808
809                 /* If the number of ioadls * size of ioadl > 128 bytes,
810                    then use a 512 byte ioarcb */
811                 if (ipr_cmd->dma_use_sg * sizeof(struct ipr_ioadl64_desc) > 128 )
812                         send_dma_addr |= 0x4;
813                 writeq(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
814         } else
815                 writel(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
816 }
817
818 /**
819  * ipr_do_req -  Send driver initiated requests.
820  * @ipr_cmd:            ipr command struct
821  * @done:                       done function
822  * @timeout_func:       timeout function
823  * @timeout:            timeout value
824  *
825  * This function sends the specified command to the adapter with the
826  * timeout given. The done function is invoked on command completion.
827  *
828  * Return value:
829  *      none
830  **/
831 static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
832                        void (*done) (struct ipr_cmnd *),
833                        void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
834 {
835         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
836
837         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
838
839         ipr_cmd->done = done;
840
841         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
842         ipr_cmd->timer.expires = jiffies + timeout;
843         ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
844
845         add_timer(&ipr_cmd->timer);
846
847         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
848
849         mb();
850
851         ipr_send_command(ipr_cmd);
852 }
853
854 /**
855  * ipr_internal_cmd_done - Op done function for an internally generated op.
856  * @ipr_cmd:    ipr command struct
857  *
858  * This function is the op done function for an internally generated,
859  * blocking op. It simply wakes the sleeping thread.
860  *
861  * Return value:
862  *      none
863  **/
864 static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
865 {
866         if (ipr_cmd->sibling)
867                 ipr_cmd->sibling = NULL;
868         else
869                 complete(&ipr_cmd->completion);
870 }
871
872 /**
873  * ipr_init_ioadl - initialize the ioadl for the correct SIS type
874  * @ipr_cmd:    ipr command struct
875  * @dma_addr:   dma address
876  * @len:        transfer length
877  * @flags:      ioadl flag value
878  *
879  * This function initializes an ioadl in the case where there is only a single
880  * descriptor.
881  *
882  * Return value:
883  *      nothing
884  **/
885 static void ipr_init_ioadl(struct ipr_cmnd *ipr_cmd, dma_addr_t dma_addr,
886                            u32 len, int flags)
887 {
888         struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
889         struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
890
891         ipr_cmd->dma_use_sg = 1;
892
893         if (ipr_cmd->ioa_cfg->sis64) {
894                 ioadl64->flags = cpu_to_be32(flags);
895                 ioadl64->data_len = cpu_to_be32(len);
896                 ioadl64->address = cpu_to_be64(dma_addr);
897
898                 ipr_cmd->ioarcb.ioadl_len =
899                         cpu_to_be32(sizeof(struct ipr_ioadl64_desc));
900                 ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
901         } else {
902                 ioadl->flags_and_data_len = cpu_to_be32(flags | len);
903                 ioadl->address = cpu_to_be32(dma_addr);
904
905                 if (flags == IPR_IOADL_FLAGS_READ_LAST) {
906                         ipr_cmd->ioarcb.read_ioadl_len =
907                                 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
908                         ipr_cmd->ioarcb.read_data_transfer_length = cpu_to_be32(len);
909                 } else {
910                         ipr_cmd->ioarcb.ioadl_len =
911                                 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
912                         ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
913                 }
914         }
915 }
916
917 /**
918  * ipr_send_blocking_cmd - Send command and sleep on its completion.
919  * @ipr_cmd:    ipr command struct
920  * @timeout_func:       function to invoke if command times out
921  * @timeout:    timeout
922  *
923  * Return value:
924  *      none
925  **/
926 static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
927                                   void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
928                                   u32 timeout)
929 {
930         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
931
932         init_completion(&ipr_cmd->completion);
933         ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
934
935         spin_unlock_irq(ioa_cfg->host->host_lock);
936         wait_for_completion(&ipr_cmd->completion);
937         spin_lock_irq(ioa_cfg->host->host_lock);
938 }
939
940 /**
941  * ipr_send_hcam - Send an HCAM to the adapter.
942  * @ioa_cfg:    ioa config struct
943  * @type:               HCAM type
944  * @hostrcb:    hostrcb struct
945  *
946  * This function will send a Host Controlled Async command to the adapter.
947  * If HCAMs are currently not allowed to be issued to the adapter, it will
948  * place the hostrcb on the free queue.
949  *
950  * Return value:
951  *      none
952  **/
953 static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
954                           struct ipr_hostrcb *hostrcb)
955 {
956         struct ipr_cmnd *ipr_cmd;
957         struct ipr_ioarcb *ioarcb;
958
959         if (ioa_cfg->allow_cmds) {
960                 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
961                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
962                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
963
964                 ipr_cmd->u.hostrcb = hostrcb;
965                 ioarcb = &ipr_cmd->ioarcb;
966
967                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
968                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
969                 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
970                 ioarcb->cmd_pkt.cdb[1] = type;
971                 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
972                 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
973
974                 ipr_init_ioadl(ipr_cmd, hostrcb->hostrcb_dma,
975                                sizeof(hostrcb->hcam), IPR_IOADL_FLAGS_READ_LAST);
976
977                 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
978                         ipr_cmd->done = ipr_process_ccn;
979                 else
980                         ipr_cmd->done = ipr_process_error;
981
982                 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
983
984                 mb();
985
986                 ipr_send_command(ipr_cmd);
987         } else {
988                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
989         }
990 }
991
992 /**
993  * ipr_update_ata_class - Update the ata class in the resource entry
994  * @res:        resource entry struct
995  * @proto:      cfgte device bus protocol value
996  *
997  * Return value:
998  *      none
999  **/
1000 static void ipr_update_ata_class(struct ipr_resource_entry *res, unsigned int proto)
1001 {
1002         switch(proto) {
1003         case IPR_PROTO_SATA:
1004         case IPR_PROTO_SAS_STP:
1005                 res->ata_class = ATA_DEV_ATA;
1006                 break;
1007         case IPR_PROTO_SATA_ATAPI:
1008         case IPR_PROTO_SAS_STP_ATAPI:
1009                 res->ata_class = ATA_DEV_ATAPI;
1010                 break;
1011         default:
1012                 res->ata_class = ATA_DEV_UNKNOWN;
1013                 break;
1014         };
1015 }
1016
1017 /**
1018  * ipr_init_res_entry - Initialize a resource entry struct.
1019  * @res:        resource entry struct
1020  * @cfgtew:     config table entry wrapper struct
1021  *
1022  * Return value:
1023  *      none
1024  **/
1025 static void ipr_init_res_entry(struct ipr_resource_entry *res,
1026                                struct ipr_config_table_entry_wrapper *cfgtew)
1027 {
1028         int found = 0;
1029         unsigned int proto;
1030         struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
1031         struct ipr_resource_entry *gscsi_res = NULL;
1032
1033         res->needs_sync_complete = 0;
1034         res->in_erp = 0;
1035         res->add_to_ml = 0;
1036         res->del_from_ml = 0;
1037         res->resetting_device = 0;
1038         res->sdev = NULL;
1039         res->sata_port = NULL;
1040
1041         if (ioa_cfg->sis64) {
1042                 proto = cfgtew->u.cfgte64->proto;
1043                 res->res_flags = cfgtew->u.cfgte64->res_flags;
1044                 res->qmodel = IPR_QUEUEING_MODEL64(res);
1045                 res->type = cfgtew->u.cfgte64->res_type;
1046
1047                 memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
1048                         sizeof(res->res_path));
1049
1050                 res->bus = 0;
1051                 res->lun = scsilun_to_int(&res->dev_lun);
1052
1053                 if (res->type == IPR_RES_TYPE_GENERIC_SCSI) {
1054                         list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue) {
1055                                 if (gscsi_res->dev_id == cfgtew->u.cfgte64->dev_id) {
1056                                         found = 1;
1057                                         res->target = gscsi_res->target;
1058                                         break;
1059                                 }
1060                         }
1061                         if (!found) {
1062                                 res->target = find_first_zero_bit(ioa_cfg->target_ids,
1063                                                                   ioa_cfg->max_devs_supported);
1064                                 set_bit(res->target, ioa_cfg->target_ids);
1065                         }
1066
1067                         memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1068                                 sizeof(res->dev_lun.scsi_lun));
1069                 } else if (res->type == IPR_RES_TYPE_IOAFP) {
1070                         res->bus = IPR_IOAFP_VIRTUAL_BUS;
1071                         res->target = 0;
1072                 } else if (res->type == IPR_RES_TYPE_ARRAY) {
1073                         res->bus = IPR_ARRAY_VIRTUAL_BUS;
1074                         res->target = find_first_zero_bit(ioa_cfg->array_ids,
1075                                                           ioa_cfg->max_devs_supported);
1076                         set_bit(res->target, ioa_cfg->array_ids);
1077                 } else if (res->type == IPR_RES_TYPE_VOLUME_SET) {
1078                         res->bus = IPR_VSET_VIRTUAL_BUS;
1079                         res->target = find_first_zero_bit(ioa_cfg->vset_ids,
1080                                                           ioa_cfg->max_devs_supported);
1081                         set_bit(res->target, ioa_cfg->vset_ids);
1082                 } else {
1083                         res->target = find_first_zero_bit(ioa_cfg->target_ids,
1084                                                           ioa_cfg->max_devs_supported);
1085                         set_bit(res->target, ioa_cfg->target_ids);
1086                 }
1087         } else {
1088                 proto = cfgtew->u.cfgte->proto;
1089                 res->qmodel = IPR_QUEUEING_MODEL(res);
1090                 res->flags = cfgtew->u.cfgte->flags;
1091                 if (res->flags & IPR_IS_IOA_RESOURCE)
1092                         res->type = IPR_RES_TYPE_IOAFP;
1093                 else
1094                         res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
1095
1096                 res->bus = cfgtew->u.cfgte->res_addr.bus;
1097                 res->target = cfgtew->u.cfgte->res_addr.target;
1098                 res->lun = cfgtew->u.cfgte->res_addr.lun;
1099                 res->lun_wwn = get_unaligned_be64(cfgtew->u.cfgte->lun_wwn);
1100         }
1101
1102         ipr_update_ata_class(res, proto);
1103 }
1104
1105 /**
1106  * ipr_is_same_device - Determine if two devices are the same.
1107  * @res:        resource entry struct
1108  * @cfgtew:     config table entry wrapper struct
1109  *
1110  * Return value:
1111  *      1 if the devices are the same / 0 otherwise
1112  **/
1113 static int ipr_is_same_device(struct ipr_resource_entry *res,
1114                               struct ipr_config_table_entry_wrapper *cfgtew)
1115 {
1116         if (res->ioa_cfg->sis64) {
1117                 if (!memcmp(&res->dev_id, &cfgtew->u.cfgte64->dev_id,
1118                                         sizeof(cfgtew->u.cfgte64->dev_id)) &&
1119                         !memcmp(&res->lun, &cfgtew->u.cfgte64->lun,
1120                                         sizeof(cfgtew->u.cfgte64->lun))) {
1121                         return 1;
1122                 }
1123         } else {
1124                 if (res->bus == cfgtew->u.cfgte->res_addr.bus &&
1125                     res->target == cfgtew->u.cfgte->res_addr.target &&
1126                     res->lun == cfgtew->u.cfgte->res_addr.lun)
1127                         return 1;
1128         }
1129
1130         return 0;
1131 }
1132
1133 /**
1134  * ipr_format_res_path - Format the resource path for printing.
1135  * @res_path:   resource path
1136  * @buf:        buffer
1137  *
1138  * Return value:
1139  *      pointer to buffer
1140  **/
1141 static char *ipr_format_res_path(u8 *res_path, char *buffer, int len)
1142 {
1143         int i;
1144         char *p = buffer;
1145
1146         *p = '\0';
1147         p += snprintf(p, buffer + len - p, "%02X", res_path[0]);
1148         for (i = 1; res_path[i] != 0xff && ((i * 3) < len); i++)
1149                 p += snprintf(p, buffer + len - p, "-%02X", res_path[i]);
1150
1151         return buffer;
1152 }
1153
1154 /**
1155  * ipr_update_res_entry - Update the resource entry.
1156  * @res:        resource entry struct
1157  * @cfgtew:     config table entry wrapper struct
1158  *
1159  * Return value:
1160  *      none
1161  **/
1162 static void ipr_update_res_entry(struct ipr_resource_entry *res,
1163                                  struct ipr_config_table_entry_wrapper *cfgtew)
1164 {
1165         char buffer[IPR_MAX_RES_PATH_LENGTH];
1166         unsigned int proto;
1167         int new_path = 0;
1168
1169         if (res->ioa_cfg->sis64) {
1170                 res->flags = cfgtew->u.cfgte64->flags;
1171                 res->res_flags = cfgtew->u.cfgte64->res_flags;
1172                 res->type = cfgtew->u.cfgte64->res_type;
1173
1174                 memcpy(&res->std_inq_data, &cfgtew->u.cfgte64->std_inq_data,
1175                         sizeof(struct ipr_std_inq_data));
1176
1177                 res->qmodel = IPR_QUEUEING_MODEL64(res);
1178                 proto = cfgtew->u.cfgte64->proto;
1179                 res->res_handle = cfgtew->u.cfgte64->res_handle;
1180                 res->dev_id = cfgtew->u.cfgte64->dev_id;
1181
1182                 memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1183                         sizeof(res->dev_lun.scsi_lun));
1184
1185                 if (memcmp(res->res_path, &cfgtew->u.cfgte64->res_path,
1186                                         sizeof(res->res_path))) {
1187                         memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
1188                                 sizeof(res->res_path));
1189                         new_path = 1;
1190                 }
1191
1192                 if (res->sdev && new_path)
1193                         sdev_printk(KERN_INFO, res->sdev, "Resource path: %s\n",
1194                                     ipr_format_res_path(res->res_path, buffer,
1195                                                         sizeof(buffer)));
1196         } else {
1197                 res->flags = cfgtew->u.cfgte->flags;
1198                 if (res->flags & IPR_IS_IOA_RESOURCE)
1199                         res->type = IPR_RES_TYPE_IOAFP;
1200                 else
1201                         res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
1202
1203                 memcpy(&res->std_inq_data, &cfgtew->u.cfgte->std_inq_data,
1204                         sizeof(struct ipr_std_inq_data));
1205
1206                 res->qmodel = IPR_QUEUEING_MODEL(res);
1207                 proto = cfgtew->u.cfgte->proto;
1208                 res->res_handle = cfgtew->u.cfgte->res_handle;
1209         }
1210
1211         ipr_update_ata_class(res, proto);
1212 }
1213
1214 /**
1215  * ipr_clear_res_target - Clear the bit in the bit map representing the target
1216  *                        for the resource.
1217  * @res:        resource entry struct
1218  * @cfgtew:     config table entry wrapper struct
1219  *
1220  * Return value:
1221  *      none
1222  **/
1223 static void ipr_clear_res_target(struct ipr_resource_entry *res)
1224 {
1225         struct ipr_resource_entry *gscsi_res = NULL;
1226         struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
1227
1228         if (!ioa_cfg->sis64)
1229                 return;
1230
1231         if (res->bus == IPR_ARRAY_VIRTUAL_BUS)
1232                 clear_bit(res->target, ioa_cfg->array_ids);
1233         else if (res->bus == IPR_VSET_VIRTUAL_BUS)
1234                 clear_bit(res->target, ioa_cfg->vset_ids);
1235         else if (res->bus == 0 && res->type == IPR_RES_TYPE_GENERIC_SCSI) {
1236                 list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue)
1237                         if (gscsi_res->dev_id == res->dev_id && gscsi_res != res)
1238                                 return;
1239                 clear_bit(res->target, ioa_cfg->target_ids);
1240
1241         } else if (res->bus == 0)
1242                 clear_bit(res->target, ioa_cfg->target_ids);
1243 }
1244
1245 /**
1246  * ipr_handle_config_change - Handle a config change from the adapter
1247  * @ioa_cfg:    ioa config struct
1248  * @hostrcb:    hostrcb
1249  *
1250  * Return value:
1251  *      none
1252  **/
1253 static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
1254                                      struct ipr_hostrcb *hostrcb)
1255 {
1256         struct ipr_resource_entry *res = NULL;
1257         struct ipr_config_table_entry_wrapper cfgtew;
1258         __be32 cc_res_handle;
1259
1260         u32 is_ndn = 1;
1261
1262         if (ioa_cfg->sis64) {
1263                 cfgtew.u.cfgte64 = &hostrcb->hcam.u.ccn.u.cfgte64;
1264                 cc_res_handle = cfgtew.u.cfgte64->res_handle;
1265         } else {
1266                 cfgtew.u.cfgte = &hostrcb->hcam.u.ccn.u.cfgte;
1267                 cc_res_handle = cfgtew.u.cfgte->res_handle;
1268         }
1269
1270         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1271                 if (res->res_handle == cc_res_handle) {
1272                         is_ndn = 0;
1273                         break;
1274                 }
1275         }
1276
1277         if (is_ndn) {
1278                 if (list_empty(&ioa_cfg->free_res_q)) {
1279                         ipr_send_hcam(ioa_cfg,
1280                                       IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
1281                                       hostrcb);
1282                         return;
1283                 }
1284
1285                 res = list_entry(ioa_cfg->free_res_q.next,
1286                                  struct ipr_resource_entry, queue);
1287
1288                 list_del(&res->queue);
1289                 ipr_init_res_entry(res, &cfgtew);
1290                 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
1291         }
1292
1293         ipr_update_res_entry(res, &cfgtew);
1294
1295         if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
1296                 if (res->sdev) {
1297                         res->del_from_ml = 1;
1298                         res->res_handle = IPR_INVALID_RES_HANDLE;
1299                         if (ioa_cfg->allow_ml_add_del)
1300                                 schedule_work(&ioa_cfg->work_q);
1301                 } else {
1302                         ipr_clear_res_target(res);
1303                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
1304                 }
1305         } else if (!res->sdev) {
1306                 res->add_to_ml = 1;
1307                 if (ioa_cfg->allow_ml_add_del)
1308                         schedule_work(&ioa_cfg->work_q);
1309         }
1310
1311         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
1312 }
1313
1314 /**
1315  * ipr_process_ccn - Op done function for a CCN.
1316  * @ipr_cmd:    ipr command struct
1317  *
1318  * This function is the op done function for a configuration
1319  * change notification host controlled async from the adapter.
1320  *
1321  * Return value:
1322  *      none
1323  **/
1324 static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
1325 {
1326         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1327         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1328         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1329
1330         list_del(&hostrcb->queue);
1331         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1332
1333         if (ioasc) {
1334                 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
1335                         dev_err(&ioa_cfg->pdev->dev,
1336                                 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1337
1338                 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
1339         } else {
1340                 ipr_handle_config_change(ioa_cfg, hostrcb);
1341         }
1342 }
1343
1344 /**
1345  * strip_and_pad_whitespace - Strip and pad trailing whitespace.
1346  * @i:          index into buffer
1347  * @buf:                string to modify
1348  *
1349  * This function will strip all trailing whitespace, pad the end
1350  * of the string with a single space, and NULL terminate the string.
1351  *
1352  * Return value:
1353  *      new length of string
1354  **/
1355 static int strip_and_pad_whitespace(int i, char *buf)
1356 {
1357         while (i && buf[i] == ' ')
1358                 i--;
1359         buf[i+1] = ' ';
1360         buf[i+2] = '\0';
1361         return i + 2;
1362 }
1363
1364 /**
1365  * ipr_log_vpd_compact - Log the passed extended VPD compactly.
1366  * @prefix:             string to print at start of printk
1367  * @hostrcb:    hostrcb pointer
1368  * @vpd:                vendor/product id/sn struct
1369  *
1370  * Return value:
1371  *      none
1372  **/
1373 static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1374                                 struct ipr_vpd *vpd)
1375 {
1376         char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
1377         int i = 0;
1378
1379         memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
1380         i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
1381
1382         memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
1383         i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
1384
1385         memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
1386         buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
1387
1388         ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
1389 }
1390
1391 /**
1392  * ipr_log_vpd - Log the passed VPD to the error log.
1393  * @vpd:                vendor/product id/sn struct
1394  *
1395  * Return value:
1396  *      none
1397  **/
1398 static void ipr_log_vpd(struct ipr_vpd *vpd)
1399 {
1400         char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
1401                     + IPR_SERIAL_NUM_LEN];
1402
1403         memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
1404         memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
1405                IPR_PROD_ID_LEN);
1406         buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
1407         ipr_err("Vendor/Product ID: %s\n", buffer);
1408
1409         memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
1410         buffer[IPR_SERIAL_NUM_LEN] = '\0';
1411         ipr_err("    Serial Number: %s\n", buffer);
1412 }
1413
1414 /**
1415  * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
1416  * @prefix:             string to print at start of printk
1417  * @hostrcb:    hostrcb pointer
1418  * @vpd:                vendor/product id/sn/wwn struct
1419  *
1420  * Return value:
1421  *      none
1422  **/
1423 static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1424                                     struct ipr_ext_vpd *vpd)
1425 {
1426         ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
1427         ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
1428                      be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
1429 }
1430
1431 /**
1432  * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
1433  * @vpd:                vendor/product id/sn/wwn struct
1434  *
1435  * Return value:
1436  *      none
1437  **/
1438 static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
1439 {
1440         ipr_log_vpd(&vpd->vpd);
1441         ipr_err("    WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
1442                 be32_to_cpu(vpd->wwid[1]));
1443 }
1444
1445 /**
1446  * ipr_log_enhanced_cache_error - Log a cache error.
1447  * @ioa_cfg:    ioa config struct
1448  * @hostrcb:    hostrcb struct
1449  *
1450  * Return value:
1451  *      none
1452  **/
1453 static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1454                                          struct ipr_hostrcb *hostrcb)
1455 {
1456         struct ipr_hostrcb_type_12_error *error;
1457
1458         if (ioa_cfg->sis64)
1459                 error = &hostrcb->hcam.u.error64.u.type_12_error;
1460         else
1461                 error = &hostrcb->hcam.u.error.u.type_12_error;
1462
1463         ipr_err("-----Current Configuration-----\n");
1464         ipr_err("Cache Directory Card Information:\n");
1465         ipr_log_ext_vpd(&error->ioa_vpd);
1466         ipr_err("Adapter Card Information:\n");
1467         ipr_log_ext_vpd(&error->cfc_vpd);
1468
1469         ipr_err("-----Expected Configuration-----\n");
1470         ipr_err("Cache Directory Card Information:\n");
1471         ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
1472         ipr_err("Adapter Card Information:\n");
1473         ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
1474
1475         ipr_err("Additional IOA Data: %08X %08X %08X\n",
1476                      be32_to_cpu(error->ioa_data[0]),
1477                      be32_to_cpu(error->ioa_data[1]),
1478                      be32_to_cpu(error->ioa_data[2]));
1479 }
1480
1481 /**
1482  * ipr_log_cache_error - Log a cache error.
1483  * @ioa_cfg:    ioa config struct
1484  * @hostrcb:    hostrcb struct
1485  *
1486  * Return value:
1487  *      none
1488  **/
1489 static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1490                                 struct ipr_hostrcb *hostrcb)
1491 {
1492         struct ipr_hostrcb_type_02_error *error =
1493                 &hostrcb->hcam.u.error.u.type_02_error;
1494
1495         ipr_err("-----Current Configuration-----\n");
1496         ipr_err("Cache Directory Card Information:\n");
1497         ipr_log_vpd(&error->ioa_vpd);
1498         ipr_err("Adapter Card Information:\n");
1499         ipr_log_vpd(&error->cfc_vpd);
1500
1501         ipr_err("-----Expected Configuration-----\n");
1502         ipr_err("Cache Directory Card Information:\n");
1503         ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
1504         ipr_err("Adapter Card Information:\n");
1505         ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
1506
1507         ipr_err("Additional IOA Data: %08X %08X %08X\n",
1508                      be32_to_cpu(error->ioa_data[0]),
1509                      be32_to_cpu(error->ioa_data[1]),
1510                      be32_to_cpu(error->ioa_data[2]));
1511 }
1512
1513 /**
1514  * ipr_log_enhanced_config_error - Log a configuration error.
1515  * @ioa_cfg:    ioa config struct
1516  * @hostrcb:    hostrcb struct
1517  *
1518  * Return value:
1519  *      none
1520  **/
1521 static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
1522                                           struct ipr_hostrcb *hostrcb)
1523 {
1524         int errors_logged, i;
1525         struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
1526         struct ipr_hostrcb_type_13_error *error;
1527
1528         error = &hostrcb->hcam.u.error.u.type_13_error;
1529         errors_logged = be32_to_cpu(error->errors_logged);
1530
1531         ipr_err("Device Errors Detected/Logged: %d/%d\n",
1532                 be32_to_cpu(error->errors_detected), errors_logged);
1533
1534         dev_entry = error->dev;
1535
1536         for (i = 0; i < errors_logged; i++, dev_entry++) {
1537                 ipr_err_separator;
1538
1539                 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1540                 ipr_log_ext_vpd(&dev_entry->vpd);
1541
1542                 ipr_err("-----New Device Information-----\n");
1543                 ipr_log_ext_vpd(&dev_entry->new_vpd);
1544
1545                 ipr_err("Cache Directory Card Information:\n");
1546                 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1547
1548                 ipr_err("Adapter Card Information:\n");
1549                 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1550         }
1551 }
1552
1553 /**
1554  * ipr_log_sis64_config_error - Log a device error.
1555  * @ioa_cfg:    ioa config struct
1556  * @hostrcb:    hostrcb struct
1557  *
1558  * Return value:
1559  *      none
1560  **/
1561 static void ipr_log_sis64_config_error(struct ipr_ioa_cfg *ioa_cfg,
1562                                        struct ipr_hostrcb *hostrcb)
1563 {
1564         int errors_logged, i;
1565         struct ipr_hostrcb64_device_data_entry_enhanced *dev_entry;
1566         struct ipr_hostrcb_type_23_error *error;
1567         char buffer[IPR_MAX_RES_PATH_LENGTH];
1568
1569         error = &hostrcb->hcam.u.error64.u.type_23_error;
1570         errors_logged = be32_to_cpu(error->errors_logged);
1571
1572         ipr_err("Device Errors Detected/Logged: %d/%d\n",
1573                 be32_to_cpu(error->errors_detected), errors_logged);
1574
1575         dev_entry = error->dev;
1576
1577         for (i = 0; i < errors_logged; i++, dev_entry++) {
1578                 ipr_err_separator;
1579
1580                 ipr_err("Device %d : %s", i + 1,
1581                          ipr_format_res_path(dev_entry->res_path, buffer,
1582                                              sizeof(buffer)));
1583                 ipr_log_ext_vpd(&dev_entry->vpd);
1584
1585                 ipr_err("-----New Device Information-----\n");
1586                 ipr_log_ext_vpd(&dev_entry->new_vpd);
1587
1588                 ipr_err("Cache Directory Card Information:\n");
1589                 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1590
1591                 ipr_err("Adapter Card Information:\n");
1592                 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1593         }
1594 }
1595
1596 /**
1597  * ipr_log_config_error - Log a configuration error.
1598  * @ioa_cfg:    ioa config struct
1599  * @hostrcb:    hostrcb struct
1600  *
1601  * Return value:
1602  *      none
1603  **/
1604 static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
1605                                  struct ipr_hostrcb *hostrcb)
1606 {
1607         int errors_logged, i;
1608         struct ipr_hostrcb_device_data_entry *dev_entry;
1609         struct ipr_hostrcb_type_03_error *error;
1610
1611         error = &hostrcb->hcam.u.error.u.type_03_error;
1612         errors_logged = be32_to_cpu(error->errors_logged);
1613
1614         ipr_err("Device Errors Detected/Logged: %d/%d\n",
1615                 be32_to_cpu(error->errors_detected), errors_logged);
1616
1617         dev_entry = error->dev;
1618
1619         for (i = 0; i < errors_logged; i++, dev_entry++) {
1620                 ipr_err_separator;
1621
1622                 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1623                 ipr_log_vpd(&dev_entry->vpd);
1624
1625                 ipr_err("-----New Device Information-----\n");
1626                 ipr_log_vpd(&dev_entry->new_vpd);
1627
1628                 ipr_err("Cache Directory Card Information:\n");
1629                 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
1630
1631                 ipr_err("Adapter Card Information:\n");
1632                 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
1633
1634                 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1635                         be32_to_cpu(dev_entry->ioa_data[0]),
1636                         be32_to_cpu(dev_entry->ioa_data[1]),
1637                         be32_to_cpu(dev_entry->ioa_data[2]),
1638                         be32_to_cpu(dev_entry->ioa_data[3]),
1639                         be32_to_cpu(dev_entry->ioa_data[4]));
1640         }
1641 }
1642
1643 /**
1644  * ipr_log_enhanced_array_error - Log an array configuration error.
1645  * @ioa_cfg:    ioa config struct
1646  * @hostrcb:    hostrcb struct
1647  *
1648  * Return value:
1649  *      none
1650  **/
1651 static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
1652                                          struct ipr_hostrcb *hostrcb)
1653 {
1654         int i, num_entries;
1655         struct ipr_hostrcb_type_14_error *error;
1656         struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
1657         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1658
1659         error = &hostrcb->hcam.u.error.u.type_14_error;
1660
1661         ipr_err_separator;
1662
1663         ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1664                 error->protection_level,
1665                 ioa_cfg->host->host_no,
1666                 error->last_func_vset_res_addr.bus,
1667                 error->last_func_vset_res_addr.target,
1668                 error->last_func_vset_res_addr.lun);
1669
1670         ipr_err_separator;
1671
1672         array_entry = error->array_member;
1673         num_entries = min_t(u32, be32_to_cpu(error->num_entries),
1674                             ARRAY_SIZE(error->array_member));
1675
1676         for (i = 0; i < num_entries; i++, array_entry++) {
1677                 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1678                         continue;
1679
1680                 if (be32_to_cpu(error->exposed_mode_adn) == i)
1681                         ipr_err("Exposed Array Member %d:\n", i);
1682                 else
1683                         ipr_err("Array Member %d:\n", i);
1684
1685                 ipr_log_ext_vpd(&array_entry->vpd);
1686                 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1687                 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1688                                  "Expected Location");
1689
1690                 ipr_err_separator;
1691         }
1692 }
1693
1694 /**
1695  * ipr_log_array_error - Log an array configuration error.
1696  * @ioa_cfg:    ioa config struct
1697  * @hostrcb:    hostrcb struct
1698  *
1699  * Return value:
1700  *      none
1701  **/
1702 static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1703                                 struct ipr_hostrcb *hostrcb)
1704 {
1705         int i;
1706         struct ipr_hostrcb_type_04_error *error;
1707         struct ipr_hostrcb_array_data_entry *array_entry;
1708         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1709
1710         error = &hostrcb->hcam.u.error.u.type_04_error;
1711
1712         ipr_err_separator;
1713
1714         ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1715                 error->protection_level,
1716                 ioa_cfg->host->host_no,
1717                 error->last_func_vset_res_addr.bus,
1718                 error->last_func_vset_res_addr.target,
1719                 error->last_func_vset_res_addr.lun);
1720
1721         ipr_err_separator;
1722
1723         array_entry = error->array_member;
1724
1725         for (i = 0; i < 18; i++) {
1726                 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1727                         continue;
1728
1729                 if (be32_to_cpu(error->exposed_mode_adn) == i)
1730                         ipr_err("Exposed Array Member %d:\n", i);
1731                 else
1732                         ipr_err("Array Member %d:\n", i);
1733
1734                 ipr_log_vpd(&array_entry->vpd);
1735
1736                 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1737                 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1738                                  "Expected Location");
1739
1740                 ipr_err_separator;
1741
1742                 if (i == 9)
1743                         array_entry = error->array_member2;
1744                 else
1745                         array_entry++;
1746         }
1747 }
1748
1749 /**
1750  * ipr_log_hex_data - Log additional hex IOA error data.
1751  * @ioa_cfg:    ioa config struct
1752  * @data:               IOA error data
1753  * @len:                data length
1754  *
1755  * Return value:
1756  *      none
1757  **/
1758 static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
1759 {
1760         int i;
1761
1762         if (len == 0)
1763                 return;
1764
1765         if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
1766                 len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
1767
1768         for (i = 0; i < len / 4; i += 4) {
1769                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1770                         be32_to_cpu(data[i]),
1771                         be32_to_cpu(data[i+1]),
1772                         be32_to_cpu(data[i+2]),
1773                         be32_to_cpu(data[i+3]));
1774         }
1775 }
1776
1777 /**
1778  * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
1779  * @ioa_cfg:    ioa config struct
1780  * @hostrcb:    hostrcb struct
1781  *
1782  * Return value:
1783  *      none
1784  **/
1785 static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1786                                             struct ipr_hostrcb *hostrcb)
1787 {
1788         struct ipr_hostrcb_type_17_error *error;
1789
1790         if (ioa_cfg->sis64)
1791                 error = &hostrcb->hcam.u.error64.u.type_17_error;
1792         else
1793                 error = &hostrcb->hcam.u.error.u.type_17_error;
1794
1795         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1796         strim(error->failure_reason);
1797
1798         ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1799                      be32_to_cpu(hostrcb->hcam.u.error.prc));
1800         ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1801         ipr_log_hex_data(ioa_cfg, error->data,
1802                          be32_to_cpu(hostrcb->hcam.length) -
1803                          (offsetof(struct ipr_hostrcb_error, u) +
1804                           offsetof(struct ipr_hostrcb_type_17_error, data)));
1805 }
1806
1807 /**
1808  * ipr_log_dual_ioa_error - Log a dual adapter error.
1809  * @ioa_cfg:    ioa config struct
1810  * @hostrcb:    hostrcb struct
1811  *
1812  * Return value:
1813  *      none
1814  **/
1815 static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1816                                    struct ipr_hostrcb *hostrcb)
1817 {
1818         struct ipr_hostrcb_type_07_error *error;
1819
1820         error = &hostrcb->hcam.u.error.u.type_07_error;
1821         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1822         strim(error->failure_reason);
1823
1824         ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1825                      be32_to_cpu(hostrcb->hcam.u.error.prc));
1826         ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1827         ipr_log_hex_data(ioa_cfg, error->data,
1828                          be32_to_cpu(hostrcb->hcam.length) -
1829                          (offsetof(struct ipr_hostrcb_error, u) +
1830                           offsetof(struct ipr_hostrcb_type_07_error, data)));
1831 }
1832
1833 static const struct {
1834         u8 active;
1835         char *desc;
1836 } path_active_desc[] = {
1837         { IPR_PATH_NO_INFO, "Path" },
1838         { IPR_PATH_ACTIVE, "Active path" },
1839         { IPR_PATH_NOT_ACTIVE, "Inactive path" }
1840 };
1841
1842 static const struct {
1843         u8 state;
1844         char *desc;
1845 } path_state_desc[] = {
1846         { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
1847         { IPR_PATH_HEALTHY, "is healthy" },
1848         { IPR_PATH_DEGRADED, "is degraded" },
1849         { IPR_PATH_FAILED, "is failed" }
1850 };
1851
1852 /**
1853  * ipr_log_fabric_path - Log a fabric path error
1854  * @hostrcb:    hostrcb struct
1855  * @fabric:             fabric descriptor
1856  *
1857  * Return value:
1858  *      none
1859  **/
1860 static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
1861                                 struct ipr_hostrcb_fabric_desc *fabric)
1862 {
1863         int i, j;
1864         u8 path_state = fabric->path_state;
1865         u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1866         u8 state = path_state & IPR_PATH_STATE_MASK;
1867
1868         for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1869                 if (path_active_desc[i].active != active)
1870                         continue;
1871
1872                 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
1873                         if (path_state_desc[j].state != state)
1874                                 continue;
1875
1876                         if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
1877                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
1878                                              path_active_desc[i].desc, path_state_desc[j].desc,
1879                                              fabric->ioa_port);
1880                         } else if (fabric->cascaded_expander == 0xff) {
1881                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
1882                                              path_active_desc[i].desc, path_state_desc[j].desc,
1883                                              fabric->ioa_port, fabric->phy);
1884                         } else if (fabric->phy == 0xff) {
1885                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
1886                                              path_active_desc[i].desc, path_state_desc[j].desc,
1887                                              fabric->ioa_port, fabric->cascaded_expander);
1888                         } else {
1889                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
1890                                              path_active_desc[i].desc, path_state_desc[j].desc,
1891                                              fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1892                         }
1893                         return;
1894                 }
1895         }
1896
1897         ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
1898                 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1899 }
1900
1901 /**
1902  * ipr_log64_fabric_path - Log a fabric path error
1903  * @hostrcb:    hostrcb struct
1904  * @fabric:             fabric descriptor
1905  *
1906  * Return value:
1907  *      none
1908  **/
1909 static void ipr_log64_fabric_path(struct ipr_hostrcb *hostrcb,
1910                                   struct ipr_hostrcb64_fabric_desc *fabric)
1911 {
1912         int i, j;
1913         u8 path_state = fabric->path_state;
1914         u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1915         u8 state = path_state & IPR_PATH_STATE_MASK;
1916         char buffer[IPR_MAX_RES_PATH_LENGTH];
1917
1918         for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1919                 if (path_active_desc[i].active != active)
1920                         continue;
1921
1922                 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
1923                         if (path_state_desc[j].state != state)
1924                                 continue;
1925
1926                         ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s\n",
1927                                      path_active_desc[i].desc, path_state_desc[j].desc,
1928                                      ipr_format_res_path(fabric->res_path, buffer,
1929                                                          sizeof(buffer)));
1930                         return;
1931                 }
1932         }
1933
1934         ipr_err("Path state=%02X Resource Path=%s\n", path_state,
1935                 ipr_format_res_path(fabric->res_path, buffer, sizeof(buffer)));
1936 }
1937
1938 static const struct {
1939         u8 type;
1940         char *desc;
1941 } path_type_desc[] = {
1942         { IPR_PATH_CFG_IOA_PORT, "IOA port" },
1943         { IPR_PATH_CFG_EXP_PORT, "Expander port" },
1944         { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
1945         { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
1946 };
1947
1948 static const struct {
1949         u8 status;
1950         char *desc;
1951 } path_status_desc[] = {
1952         { IPR_PATH_CFG_NO_PROB, "Functional" },
1953         { IPR_PATH_CFG_DEGRADED, "Degraded" },
1954         { IPR_PATH_CFG_FAILED, "Failed" },
1955         { IPR_PATH_CFG_SUSPECT, "Suspect" },
1956         { IPR_PATH_NOT_DETECTED, "Missing" },
1957         { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
1958 };
1959
1960 static const char *link_rate[] = {
1961         "unknown",
1962         "disabled",
1963         "phy reset problem",
1964         "spinup hold",
1965         "port selector",
1966         "unknown",
1967         "unknown",
1968         "unknown",
1969         "1.5Gbps",
1970         "3.0Gbps",
1971         "unknown",
1972         "unknown",
1973         "unknown",
1974         "unknown",
1975         "unknown",
1976         "unknown"
1977 };
1978
1979 /**
1980  * ipr_log_path_elem - Log a fabric path element.
1981  * @hostrcb:    hostrcb struct
1982  * @cfg:                fabric path element struct
1983  *
1984  * Return value:
1985  *      none
1986  **/
1987 static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
1988                               struct ipr_hostrcb_config_element *cfg)
1989 {
1990         int i, j;
1991         u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
1992         u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
1993
1994         if (type == IPR_PATH_CFG_NOT_EXIST)
1995                 return;
1996
1997         for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
1998                 if (path_type_desc[i].type != type)
1999                         continue;
2000
2001                 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
2002                         if (path_status_desc[j].status != status)
2003                                 continue;
2004
2005                         if (type == IPR_PATH_CFG_IOA_PORT) {
2006                                 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
2007                                              path_status_desc[j].desc, path_type_desc[i].desc,
2008                                              cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2009                                              be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2010                         } else {
2011                                 if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
2012                                         ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
2013                                                      path_status_desc[j].desc, path_type_desc[i].desc,
2014                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2015                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2016                                 } else if (cfg->cascaded_expander == 0xff) {
2017                                         ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
2018                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
2019                                                      path_type_desc[i].desc, cfg->phy,
2020                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2021                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2022                                 } else if (cfg->phy == 0xff) {
2023                                         ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
2024                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
2025                                                      path_type_desc[i].desc, cfg->cascaded_expander,
2026                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2027                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2028                                 } else {
2029                                         ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
2030                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
2031                                                      path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
2032                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2033                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2034                                 }
2035                         }
2036                         return;
2037                 }
2038         }
2039
2040         ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
2041                      "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
2042                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2043                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2044 }
2045
2046 /**
2047  * ipr_log64_path_elem - Log a fabric path element.
2048  * @hostrcb:    hostrcb struct
2049  * @cfg:                fabric path element struct
2050  *
2051  * Return value:
2052  *      none
2053  **/
2054 static void ipr_log64_path_elem(struct ipr_hostrcb *hostrcb,
2055                                 struct ipr_hostrcb64_config_element *cfg)
2056 {
2057         int i, j;
2058         u8 desc_id = cfg->descriptor_id & IPR_DESCRIPTOR_MASK;
2059         u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
2060         u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
2061         char buffer[IPR_MAX_RES_PATH_LENGTH];
2062
2063         if (type == IPR_PATH_CFG_NOT_EXIST || desc_id != IPR_DESCRIPTOR_SIS64)
2064                 return;
2065
2066         for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
2067                 if (path_type_desc[i].type != type)
2068                         continue;
2069
2070                 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
2071                         if (path_status_desc[j].status != status)
2072                                 continue;
2073
2074                         ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s, Link rate=%s, WWN=%08X%08X\n",
2075                                      path_status_desc[j].desc, path_type_desc[i].desc,
2076                                      ipr_format_res_path(cfg->res_path, buffer,
2077                                                          sizeof(buffer)),
2078                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2079                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2080                         return;
2081                 }
2082         }
2083         ipr_hcam_err(hostrcb, "Path element=%02X: Resource Path=%s, Link rate=%s "
2084                      "WWN=%08X%08X\n", cfg->type_status,
2085                      ipr_format_res_path(cfg->res_path, buffer, sizeof(buffer)),
2086                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2087                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2088 }
2089
2090 /**
2091  * ipr_log_fabric_error - Log a fabric error.
2092  * @ioa_cfg:    ioa config struct
2093  * @hostrcb:    hostrcb struct
2094  *
2095  * Return value:
2096  *      none
2097  **/
2098 static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
2099                                  struct ipr_hostrcb *hostrcb)
2100 {
2101         struct ipr_hostrcb_type_20_error *error;
2102         struct ipr_hostrcb_fabric_desc *fabric;
2103         struct ipr_hostrcb_config_element *cfg;
2104         int i, add_len;
2105
2106         error = &hostrcb->hcam.u.error.u.type_20_error;
2107         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
2108         ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
2109
2110         add_len = be32_to_cpu(hostrcb->hcam.length) -
2111                 (offsetof(struct ipr_hostrcb_error, u) +
2112                  offsetof(struct ipr_hostrcb_type_20_error, desc));
2113
2114         for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
2115                 ipr_log_fabric_path(hostrcb, fabric);
2116                 for_each_fabric_cfg(fabric, cfg)
2117                         ipr_log_path_elem(hostrcb, cfg);
2118
2119                 add_len -= be16_to_cpu(fabric->length);
2120                 fabric = (struct ipr_hostrcb_fabric_desc *)
2121                         ((unsigned long)fabric + be16_to_cpu(fabric->length));
2122         }
2123
2124         ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
2125 }
2126
2127 /**
2128  * ipr_log_sis64_array_error - Log a sis64 array error.
2129  * @ioa_cfg:    ioa config struct
2130  * @hostrcb:    hostrcb struct
2131  *
2132  * Return value:
2133  *      none
2134  **/
2135 static void ipr_log_sis64_array_error(struct ipr_ioa_cfg *ioa_cfg,
2136                                       struct ipr_hostrcb *hostrcb)
2137 {
2138         int i, num_entries;
2139         struct ipr_hostrcb_type_24_error *error;
2140         struct ipr_hostrcb64_array_data_entry *array_entry;
2141         char buffer[IPR_MAX_RES_PATH_LENGTH];
2142         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
2143
2144         error = &hostrcb->hcam.u.error64.u.type_24_error;
2145
2146         ipr_err_separator;
2147
2148         ipr_err("RAID %s Array Configuration: %s\n",
2149                 error->protection_level,
2150                 ipr_format_res_path(error->last_res_path, buffer, sizeof(buffer)));
2151
2152         ipr_err_separator;
2153
2154         array_entry = error->array_member;
2155         num_entries = min_t(u32, error->num_entries,
2156                             ARRAY_SIZE(error->array_member));
2157
2158         for (i = 0; i < num_entries; i++, array_entry++) {
2159
2160                 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
2161                         continue;
2162
2163                 if (error->exposed_mode_adn == i)
2164                         ipr_err("Exposed Array Member %d:\n", i);
2165                 else
2166                         ipr_err("Array Member %d:\n", i);
2167
2168                 ipr_err("Array Member %d:\n", i);
2169                 ipr_log_ext_vpd(&array_entry->vpd);
2170                 ipr_err("Current Location: %s\n",
2171                          ipr_format_res_path(array_entry->res_path, buffer,
2172                                              sizeof(buffer)));
2173                 ipr_err("Expected Location: %s\n",
2174                          ipr_format_res_path(array_entry->expected_res_path,
2175                                              buffer, sizeof(buffer)));
2176
2177                 ipr_err_separator;
2178         }
2179 }
2180
2181 /**
2182  * ipr_log_sis64_fabric_error - Log a sis64 fabric error.
2183  * @ioa_cfg:    ioa config struct
2184  * @hostrcb:    hostrcb struct
2185  *
2186  * Return value:
2187  *      none
2188  **/
2189 static void ipr_log_sis64_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
2190                                        struct ipr_hostrcb *hostrcb)
2191 {
2192         struct ipr_hostrcb_type_30_error *error;
2193         struct ipr_hostrcb64_fabric_desc *fabric;
2194         struct ipr_hostrcb64_config_element *cfg;
2195         int i, add_len;
2196
2197         error = &hostrcb->hcam.u.error64.u.type_30_error;
2198
2199         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
2200         ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
2201
2202         add_len = be32_to_cpu(hostrcb->hcam.length) -
2203                 (offsetof(struct ipr_hostrcb64_error, u) +
2204                  offsetof(struct ipr_hostrcb_type_30_error, desc));
2205
2206         for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
2207                 ipr_log64_fabric_path(hostrcb, fabric);
2208                 for_each_fabric_cfg(fabric, cfg)
2209                         ipr_log64_path_elem(hostrcb, cfg);
2210
2211                 add_len -= be16_to_cpu(fabric->length);
2212                 fabric = (struct ipr_hostrcb64_fabric_desc *)
2213                         ((unsigned long)fabric + be16_to_cpu(fabric->length));
2214         }
2215
2216         ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
2217 }
2218
2219 /**
2220  * ipr_log_generic_error - Log an adapter error.
2221  * @ioa_cfg:    ioa config struct
2222  * @hostrcb:    hostrcb struct
2223  *
2224  * Return value:
2225  *      none
2226  **/
2227 static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
2228                                   struct ipr_hostrcb *hostrcb)
2229 {
2230         ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
2231                          be32_to_cpu(hostrcb->hcam.length));
2232 }
2233
2234 /**
2235  * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
2236  * @ioasc:      IOASC
2237  *
2238  * This function will return the index of into the ipr_error_table
2239  * for the specified IOASC. If the IOASC is not in the table,
2240  * 0 will be returned, which points to the entry used for unknown errors.
2241  *
2242  * Return value:
2243  *      index into the ipr_error_table
2244  **/
2245 static u32 ipr_get_error(u32 ioasc)
2246 {
2247         int i;
2248
2249         for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
2250                 if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
2251                         return i;
2252
2253         return 0;
2254 }
2255
2256 /**
2257  * ipr_handle_log_data - Log an adapter error.
2258  * @ioa_cfg:    ioa config struct
2259  * @hostrcb:    hostrcb struct
2260  *
2261  * This function logs an adapter error to the system.
2262  *
2263  * Return value:
2264  *      none
2265  **/
2266 static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
2267                                 struct ipr_hostrcb *hostrcb)
2268 {
2269         u32 ioasc;
2270         int error_index;
2271
2272         if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
2273                 return;
2274
2275         if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
2276                 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
2277
2278         if (ioa_cfg->sis64)
2279                 ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
2280         else
2281                 ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
2282
2283         if (!ioa_cfg->sis64 && (ioasc == IPR_IOASC_BUS_WAS_RESET ||
2284             ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER)) {
2285                 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
2286                 scsi_report_bus_reset(ioa_cfg->host,
2287                                       hostrcb->hcam.u.error.fd_res_addr.bus);
2288         }
2289
2290         error_index = ipr_get_error(ioasc);
2291
2292         if (!ipr_error_table[error_index].log_hcam)
2293                 return;
2294
2295         ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
2296
2297         /* Set indication we have logged an error */
2298         ioa_cfg->errors_logged++;
2299
2300         if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
2301                 return;
2302         if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
2303                 hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
2304
2305         switch (hostrcb->hcam.overlay_id) {
2306         case IPR_HOST_RCB_OVERLAY_ID_2:
2307                 ipr_log_cache_error(ioa_cfg, hostrcb);
2308                 break;
2309         case IPR_HOST_RCB_OVERLAY_ID_3:
2310                 ipr_log_config_error(ioa_cfg, hostrcb);
2311                 break;
2312         case IPR_HOST_RCB_OVERLAY_ID_4:
2313         case IPR_HOST_RCB_OVERLAY_ID_6:
2314                 ipr_log_array_error(ioa_cfg, hostrcb);
2315                 break;
2316         case IPR_HOST_RCB_OVERLAY_ID_7:
2317                 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
2318                 break;
2319         case IPR_HOST_RCB_OVERLAY_ID_12:
2320                 ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
2321                 break;
2322         case IPR_HOST_RCB_OVERLAY_ID_13:
2323                 ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
2324                 break;
2325         case IPR_HOST_RCB_OVERLAY_ID_14:
2326         case IPR_HOST_RCB_OVERLAY_ID_16:
2327                 ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
2328                 break;
2329         case IPR_HOST_RCB_OVERLAY_ID_17:
2330                 ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
2331                 break;
2332         case IPR_HOST_RCB_OVERLAY_ID_20:
2333                 ipr_log_fabric_error(ioa_cfg, hostrcb);
2334                 break;
2335         case IPR_HOST_RCB_OVERLAY_ID_23:
2336                 ipr_log_sis64_config_error(ioa_cfg, hostrcb);
2337                 break;
2338         case IPR_HOST_RCB_OVERLAY_ID_24:
2339         case IPR_HOST_RCB_OVERLAY_ID_26:
2340                 ipr_log_sis64_array_error(ioa_cfg, hostrcb);
2341                 break;
2342         case IPR_HOST_RCB_OVERLAY_ID_30:
2343                 ipr_log_sis64_fabric_error(ioa_cfg, hostrcb);
2344                 break;
2345         case IPR_HOST_RCB_OVERLAY_ID_1:
2346         case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
2347         default:
2348                 ipr_log_generic_error(ioa_cfg, hostrcb);
2349                 break;
2350         }
2351 }
2352
2353 /**
2354  * ipr_process_error - Op done function for an adapter error log.
2355  * @ipr_cmd:    ipr command struct
2356  *
2357  * This function is the op done function for an error log host
2358  * controlled async from the adapter. It will log the error and
2359  * send the HCAM back to the adapter.
2360  *
2361  * Return value:
2362  *      none
2363  **/
2364 static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
2365 {
2366         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2367         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
2368         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
2369         u32 fd_ioasc;
2370
2371         if (ioa_cfg->sis64)
2372                 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
2373         else
2374                 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
2375
2376         list_del(&hostrcb->queue);
2377         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
2378
2379         if (!ioasc) {
2380                 ipr_handle_log_data(ioa_cfg, hostrcb);
2381                 if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
2382                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
2383         } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
2384                 dev_err(&ioa_cfg->pdev->dev,
2385                         "Host RCB failed with IOASC: 0x%08X\n", ioasc);
2386         }
2387
2388         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
2389 }
2390
2391 /**
2392  * ipr_timeout -  An internally generated op has timed out.
2393  * @ipr_cmd:    ipr command struct
2394  *
2395  * This function blocks host requests and initiates an
2396  * adapter reset.
2397  *
2398  * Return value:
2399  *      none
2400  **/
2401 static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
2402 {
2403         unsigned long lock_flags = 0;
2404         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2405
2406         ENTER;
2407         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2408
2409         ioa_cfg->errors_logged++;
2410         dev_err(&ioa_cfg->pdev->dev,
2411                 "Adapter being reset due to command timeout.\n");
2412
2413         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2414                 ioa_cfg->sdt_state = GET_DUMP;
2415
2416         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
2417                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2418
2419         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2420         LEAVE;
2421 }
2422
2423 /**
2424  * ipr_oper_timeout -  Adapter timed out transitioning to operational
2425  * @ipr_cmd:    ipr command struct
2426  *
2427  * This function blocks host requests and initiates an
2428  * adapter reset.
2429  *
2430  * Return value:
2431  *      none
2432  **/
2433 static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
2434 {
2435         unsigned long lock_flags = 0;
2436         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2437
2438         ENTER;
2439         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2440
2441         ioa_cfg->errors_logged++;
2442         dev_err(&ioa_cfg->pdev->dev,
2443                 "Adapter timed out transitioning to operational.\n");
2444
2445         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2446                 ioa_cfg->sdt_state = GET_DUMP;
2447
2448         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
2449                 if (ipr_fastfail)
2450                         ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
2451                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2452         }
2453
2454         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2455         LEAVE;
2456 }
2457
2458 /**
2459  * ipr_reset_reload - Reset/Reload the IOA
2460  * @ioa_cfg:            ioa config struct
2461  * @shutdown_type:      shutdown type
2462  *
2463  * This function resets the adapter and re-initializes it.
2464  * This function assumes that all new host commands have been stopped.
2465  * Return value:
2466  *      SUCCESS / FAILED
2467  **/
2468 static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
2469                             enum ipr_shutdown_type shutdown_type)
2470 {
2471         if (!ioa_cfg->in_reset_reload)
2472                 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
2473
2474         spin_unlock_irq(ioa_cfg->host->host_lock);
2475         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2476         spin_lock_irq(ioa_cfg->host->host_lock);
2477
2478         /* If we got hit with a host reset while we were already resetting
2479          the adapter for some reason, and the reset failed. */
2480         if (ioa_cfg->ioa_is_dead) {
2481                 ipr_trace;
2482                 return FAILED;
2483         }
2484
2485         return SUCCESS;
2486 }
2487
2488 /**
2489  * ipr_find_ses_entry - Find matching SES in SES table
2490  * @res:        resource entry struct of SES
2491  *
2492  * Return value:
2493  *      pointer to SES table entry / NULL on failure
2494  **/
2495 static const struct ipr_ses_table_entry *
2496 ipr_find_ses_entry(struct ipr_resource_entry *res)
2497 {
2498         int i, j, matches;
2499         struct ipr_std_inq_vpids *vpids;
2500         const struct ipr_ses_table_entry *ste = ipr_ses_table;
2501
2502         for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
2503                 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
2504                         if (ste->compare_product_id_byte[j] == 'X') {
2505                                 vpids = &res->std_inq_data.vpids;
2506                                 if (vpids->product_id[j] == ste->product_id[j])
2507                                         matches++;
2508                                 else
2509                                         break;
2510                         } else
2511                                 matches++;
2512                 }
2513
2514                 if (matches == IPR_PROD_ID_LEN)
2515                         return ste;
2516         }
2517
2518         return NULL;
2519 }
2520
2521 /**
2522  * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
2523  * @ioa_cfg:    ioa config struct
2524  * @bus:                SCSI bus
2525  * @bus_width:  bus width
2526  *
2527  * Return value:
2528  *      SCSI bus speed in units of 100KHz, 1600 is 160 MHz
2529  *      For a 2-byte wide SCSI bus, the maximum transfer speed is
2530  *      twice the maximum transfer rate (e.g. for a wide enabled bus,
2531  *      max 160MHz = max 320MB/sec).
2532  **/
2533 static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
2534 {
2535         struct ipr_resource_entry *res;
2536         const struct ipr_ses_table_entry *ste;
2537         u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
2538
2539         /* Loop through each config table entry in the config table buffer */
2540         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2541                 if (!(IPR_IS_SES_DEVICE(res->std_inq_data)))
2542                         continue;
2543
2544                 if (bus != res->bus)
2545                         continue;
2546
2547                 if (!(ste = ipr_find_ses_entry(res)))
2548                         continue;
2549
2550                 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
2551         }
2552
2553         return max_xfer_rate;
2554 }
2555
2556 /**
2557  * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
2558  * @ioa_cfg:            ioa config struct
2559  * @max_delay:          max delay in micro-seconds to wait
2560  *
2561  * Waits for an IODEBUG ACK from the IOA, doing busy looping.
2562  *
2563  * Return value:
2564  *      0 on success / other on failure
2565  **/
2566 static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
2567 {
2568         volatile u32 pcii_reg;
2569         int delay = 1;
2570
2571         /* Read interrupt reg until IOA signals IO Debug Acknowledge */
2572         while (delay < max_delay) {
2573                 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
2574
2575                 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
2576                         return 0;
2577
2578                 /* udelay cannot be used if delay is more than a few milliseconds */
2579                 if ((delay / 1000) > MAX_UDELAY_MS)
2580                         mdelay(delay / 1000);
2581                 else
2582                         udelay(delay);
2583
2584                 delay += delay;
2585         }
2586         return -EIO;
2587 }
2588
2589 /**
2590  * ipr_get_sis64_dump_data_section - Dump IOA memory
2591  * @ioa_cfg:                    ioa config struct
2592  * @start_addr:                 adapter address to dump
2593  * @dest:                       destination kernel buffer
2594  * @length_in_words:            length to dump in 4 byte words
2595  *
2596  * Return value:
2597  *      0 on success
2598  **/
2599 static int ipr_get_sis64_dump_data_section(struct ipr_ioa_cfg *ioa_cfg,
2600                                            u32 start_addr,
2601                                            __be32 *dest, u32 length_in_words)
2602 {
2603         int i;
2604
2605         for (i = 0; i < length_in_words; i++) {
2606                 writel(start_addr+(i*4), ioa_cfg->regs.dump_addr_reg);
2607                 *dest = cpu_to_be32(readl(ioa_cfg->regs.dump_data_reg));
2608                 dest++;
2609         }
2610
2611         return 0;
2612 }
2613
2614 /**
2615  * ipr_get_ldump_data_section - Dump IOA memory
2616  * @ioa_cfg:                    ioa config struct
2617  * @start_addr:                 adapter address to dump
2618  * @dest:                               destination kernel buffer
2619  * @length_in_words:    length to dump in 4 byte words
2620  *
2621  * Return value:
2622  *      0 on success / -EIO on failure
2623  **/
2624 static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
2625                                       u32 start_addr,
2626                                       __be32 *dest, u32 length_in_words)
2627 {
2628         volatile u32 temp_pcii_reg;
2629         int i, delay = 0;
2630
2631         if (ioa_cfg->sis64)
2632                 return ipr_get_sis64_dump_data_section(ioa_cfg, start_addr,
2633                                                        dest, length_in_words);
2634
2635         /* Write IOA interrupt reg starting LDUMP state  */
2636         writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
2637                ioa_cfg->regs.set_uproc_interrupt_reg32);
2638
2639         /* Wait for IO debug acknowledge */
2640         if (ipr_wait_iodbg_ack(ioa_cfg,
2641                                IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
2642                 dev_err(&ioa_cfg->pdev->dev,
2643                         "IOA dump long data transfer timeout\n");
2644                 return -EIO;
2645         }
2646
2647         /* Signal LDUMP interlocked - clear IO debug ack */
2648         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2649                ioa_cfg->regs.clr_interrupt_reg);
2650
2651         /* Write Mailbox with starting address */
2652         writel(start_addr, ioa_cfg->ioa_mailbox);
2653
2654         /* Signal address valid - clear IOA Reset alert */
2655         writel(IPR_UPROCI_RESET_ALERT,
2656                ioa_cfg->regs.clr_uproc_interrupt_reg32);
2657
2658         for (i = 0; i < length_in_words; i++) {
2659                 /* Wait for IO debug acknowledge */
2660                 if (ipr_wait_iodbg_ack(ioa_cfg,
2661                                        IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
2662                         dev_err(&ioa_cfg->pdev->dev,
2663                                 "IOA dump short data transfer timeout\n");
2664                         return -EIO;
2665                 }
2666
2667                 /* Read data from mailbox and increment destination pointer */
2668                 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
2669                 dest++;
2670
2671                 /* For all but the last word of data, signal data received */
2672                 if (i < (length_in_words - 1)) {
2673                         /* Signal dump data received - Clear IO debug Ack */
2674                         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2675                                ioa_cfg->regs.clr_interrupt_reg);
2676                 }
2677         }
2678
2679         /* Signal end of block transfer. Set reset alert then clear IO debug ack */
2680         writel(IPR_UPROCI_RESET_ALERT,
2681                ioa_cfg->regs.set_uproc_interrupt_reg32);
2682
2683         writel(IPR_UPROCI_IO_DEBUG_ALERT,
2684                ioa_cfg->regs.clr_uproc_interrupt_reg32);
2685
2686         /* Signal dump data received - Clear IO debug Ack */
2687         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2688                ioa_cfg->regs.clr_interrupt_reg);
2689
2690         /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
2691         while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
2692                 temp_pcii_reg =
2693                     readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
2694
2695                 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
2696                         return 0;
2697
2698                 udelay(10);
2699                 delay += 10;
2700         }
2701
2702         return 0;
2703 }
2704
2705 #ifdef CONFIG_SCSI_IPR_DUMP
2706 /**
2707  * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
2708  * @ioa_cfg:            ioa config struct
2709  * @pci_address:        adapter address
2710  * @length:                     length of data to copy
2711  *
2712  * Copy data from PCI adapter to kernel buffer.
2713  * Note: length MUST be a 4 byte multiple
2714  * Return value:
2715  *      0 on success / other on failure
2716  **/
2717 static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
2718                         unsigned long pci_address, u32 length)
2719 {
2720         int bytes_copied = 0;
2721         int cur_len, rc, rem_len, rem_page_len;
2722         __be32 *page;
2723         unsigned long lock_flags = 0;
2724         struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
2725
2726         while (bytes_copied < length &&
2727                (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
2728                 if (ioa_dump->page_offset >= PAGE_SIZE ||
2729                     ioa_dump->page_offset == 0) {
2730                         page = (__be32 *)__get_free_page(GFP_ATOMIC);
2731
2732                         if (!page) {
2733                                 ipr_trace;
2734                                 return bytes_copied;
2735                         }
2736
2737                         ioa_dump->page_offset = 0;
2738                         ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
2739                         ioa_dump->next_page_index++;
2740                 } else
2741                         page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
2742
2743                 rem_len = length - bytes_copied;
2744                 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
2745                 cur_len = min(rem_len, rem_page_len);
2746
2747                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2748                 if (ioa_cfg->sdt_state == ABORT_DUMP) {
2749                         rc = -EIO;
2750                 } else {
2751                         rc = ipr_get_ldump_data_section(ioa_cfg,
2752                                                         pci_address + bytes_copied,
2753                                                         &page[ioa_dump->page_offset / 4],
2754                                                         (cur_len / sizeof(u32)));
2755                 }
2756                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2757
2758                 if (!rc) {
2759                         ioa_dump->page_offset += cur_len;
2760                         bytes_copied += cur_len;
2761                 } else {
2762                         ipr_trace;
2763                         break;
2764                 }
2765                 schedule();
2766         }
2767
2768         return bytes_copied;
2769 }
2770
2771 /**
2772  * ipr_init_dump_entry_hdr - Initialize a dump entry header.
2773  * @hdr:        dump entry header struct
2774  *
2775  * Return value:
2776  *      nothing
2777  **/
2778 static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
2779 {
2780         hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
2781         hdr->num_elems = 1;
2782         hdr->offset = sizeof(*hdr);
2783         hdr->status = IPR_DUMP_STATUS_SUCCESS;
2784 }
2785
2786 /**
2787  * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
2788  * @ioa_cfg:    ioa config struct
2789  * @driver_dump:        driver dump struct
2790  *
2791  * Return value:
2792  *      nothing
2793  **/
2794 static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
2795                                    struct ipr_driver_dump *driver_dump)
2796 {
2797         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2798
2799         ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
2800         driver_dump->ioa_type_entry.hdr.len =
2801                 sizeof(struct ipr_dump_ioa_type_entry) -
2802                 sizeof(struct ipr_dump_entry_header);
2803         driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2804         driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
2805         driver_dump->ioa_type_entry.type = ioa_cfg->type;
2806         driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
2807                 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
2808                 ucode_vpd->minor_release[1];
2809         driver_dump->hdr.num_entries++;
2810 }
2811
2812 /**
2813  * ipr_dump_version_data - Fill in the driver version in the dump.
2814  * @ioa_cfg:    ioa config struct
2815  * @driver_dump:        driver dump struct
2816  *
2817  * Return value:
2818  *      nothing
2819  **/
2820 static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
2821                                   struct ipr_driver_dump *driver_dump)
2822 {
2823         ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
2824         driver_dump->version_entry.hdr.len =
2825                 sizeof(struct ipr_dump_version_entry) -
2826                 sizeof(struct ipr_dump_entry_header);
2827         driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2828         driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
2829         strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
2830         driver_dump->hdr.num_entries++;
2831 }
2832
2833 /**
2834  * ipr_dump_trace_data - Fill in the IOA trace in the dump.
2835  * @ioa_cfg:    ioa config struct
2836  * @driver_dump:        driver dump struct
2837  *
2838  * Return value:
2839  *      nothing
2840  **/
2841 static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
2842                                    struct ipr_driver_dump *driver_dump)
2843 {
2844         ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
2845         driver_dump->trace_entry.hdr.len =
2846                 sizeof(struct ipr_dump_trace_entry) -
2847                 sizeof(struct ipr_dump_entry_header);
2848         driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2849         driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
2850         memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
2851         driver_dump->hdr.num_entries++;
2852 }
2853
2854 /**
2855  * ipr_dump_location_data - Fill in the IOA location in the dump.
2856  * @ioa_cfg:    ioa config struct
2857  * @driver_dump:        driver dump struct
2858  *
2859  * Return value:
2860  *      nothing
2861  **/
2862 static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
2863                                    struct ipr_driver_dump *driver_dump)
2864 {
2865         ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
2866         driver_dump->location_entry.hdr.len =
2867                 sizeof(struct ipr_dump_location_entry) -
2868                 sizeof(struct ipr_dump_entry_header);
2869         driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2870         driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
2871         strcpy(driver_dump->location_entry.location, dev_name(&ioa_cfg->pdev->dev));
2872         driver_dump->hdr.num_entries++;
2873 }
2874
2875 /**
2876  * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
2877  * @ioa_cfg:    ioa config struct
2878  * @dump:               dump struct
2879  *
2880  * Return value:
2881  *      nothing
2882  **/
2883 static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
2884 {
2885         unsigned long start_addr, sdt_word;
2886         unsigned long lock_flags = 0;
2887         struct ipr_driver_dump *driver_dump = &dump->driver_dump;
2888         struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
2889         u32 num_entries, start_off, end_off;
2890         u32 bytes_to_copy, bytes_copied, rc;
2891         struct ipr_sdt *sdt;
2892         int valid = 1;
2893         int i;
2894
2895         ENTER;
2896
2897         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2898
2899         if (ioa_cfg->sdt_state != GET_DUMP) {
2900                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2901                 return;
2902         }
2903
2904         start_addr = readl(ioa_cfg->ioa_mailbox);
2905
2906         if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(start_addr)) {
2907                 dev_err(&ioa_cfg->pdev->dev,
2908                         "Invalid dump table format: %lx\n", start_addr);
2909                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2910                 return;
2911         }
2912
2913         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
2914
2915         driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
2916
2917         /* Initialize the overall dump header */
2918         driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
2919         driver_dump->hdr.num_entries = 1;
2920         driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
2921         driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
2922         driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
2923         driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
2924
2925         ipr_dump_version_data(ioa_cfg, driver_dump);
2926         ipr_dump_location_data(ioa_cfg, driver_dump);
2927         ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
2928         ipr_dump_trace_data(ioa_cfg, driver_dump);
2929
2930         /* Update dump_header */
2931         driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
2932
2933         /* IOA Dump entry */
2934         ipr_init_dump_entry_hdr(&ioa_dump->hdr);
2935         ioa_dump->hdr.len = 0;
2936         ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2937         ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
2938
2939         /* First entries in sdt are actually a list of dump addresses and
2940          lengths to gather the real dump data.  sdt represents the pointer
2941          to the ioa generated dump table.  Dump data will be extracted based
2942          on entries in this table */
2943         sdt = &ioa_dump->sdt;
2944
2945         rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
2946                                         sizeof(struct ipr_sdt) / sizeof(__be32));
2947
2948         /* Smart Dump table is ready to use and the first entry is valid */
2949         if (rc || ((be32_to_cpu(sdt->hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
2950             (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
2951                 dev_err(&ioa_cfg->pdev->dev,
2952                         "Dump of IOA failed. Dump table not valid: %d, %X.\n",
2953                         rc, be32_to_cpu(sdt->hdr.state));
2954                 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
2955                 ioa_cfg->sdt_state = DUMP_OBTAINED;
2956                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2957                 return;
2958         }
2959
2960         num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
2961
2962         if (num_entries > IPR_NUM_SDT_ENTRIES)
2963                 num_entries = IPR_NUM_SDT_ENTRIES;
2964
2965         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2966
2967         for (i = 0; i < num_entries; i++) {
2968                 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
2969                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2970                         break;
2971                 }
2972
2973                 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
2974                         sdt_word = be32_to_cpu(sdt->entry[i].start_token);
2975                         if (ioa_cfg->sis64)
2976                                 bytes_to_copy = be32_to_cpu(sdt->entry[i].end_token);
2977                         else {
2978                                 start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
2979                                 end_off = be32_to_cpu(sdt->entry[i].end_token);
2980
2981                                 if (ipr_sdt_is_fmt2(sdt_word) && sdt_word)
2982                                         bytes_to_copy = end_off - start_off;
2983                                 else
2984                                         valid = 0;
2985                         }
2986                         if (valid) {
2987                                 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
2988                                         sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
2989                                         continue;
2990                                 }
2991
2992                                 /* Copy data from adapter to driver buffers */
2993                                 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
2994                                                             bytes_to_copy);
2995
2996                                 ioa_dump->hdr.len += bytes_copied;
2997
2998                                 if (bytes_copied != bytes_to_copy) {
2999                                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
3000                                         break;
3001                                 }
3002                         }
3003                 }
3004         }
3005
3006         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
3007
3008         /* Update dump_header */
3009         driver_dump->hdr.len += ioa_dump->hdr.len;
3010         wmb();
3011         ioa_cfg->sdt_state = DUMP_OBTAINED;
3012         LEAVE;
3013 }
3014
3015 #else
3016 #define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
3017 #endif
3018
3019 /**
3020  * ipr_release_dump - Free adapter dump memory
3021  * @kref:       kref struct
3022  *
3023  * Return value:
3024  *      nothing
3025  **/
3026 static void ipr_release_dump(struct kref *kref)
3027 {
3028         struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
3029         struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
3030         unsigned long lock_flags = 0;
3031         int i;
3032
3033         ENTER;
3034         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3035         ioa_cfg->dump = NULL;
3036         ioa_cfg->sdt_state = INACTIVE;
3037         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3038
3039         for (i = 0; i < dump->ioa_dump.next_page_index; i++)
3040                 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
3041
3042         kfree(dump);
3043         LEAVE;
3044 }
3045
3046 /**
3047  * ipr_worker_thread - Worker thread
3048  * @work:               ioa config struct
3049  *
3050  * Called at task level from a work thread. This function takes care
3051  * of adding and removing device from the mid-layer as configuration
3052  * changes are detected by the adapter.
3053  *
3054  * Return value:
3055  *      nothing
3056  **/
3057 static void ipr_worker_thread(struct work_struct *work)
3058 {
3059         unsigned long lock_flags;
3060         struct ipr_resource_entry *res;
3061         struct scsi_device *sdev;
3062         struct ipr_dump *dump;
3063         struct ipr_ioa_cfg *ioa_cfg =
3064                 container_of(work, struct ipr_ioa_cfg, work_q);
3065         u8 bus, target, lun;
3066         int did_work;
3067
3068         ENTER;
3069         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3070
3071         if (ioa_cfg->sdt_state == GET_DUMP) {
3072                 dump = ioa_cfg->dump;
3073                 if (!dump) {
3074                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3075                         return;
3076                 }
3077                 kref_get(&dump->kref);
3078                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3079                 ipr_get_ioa_dump(ioa_cfg, dump);
3080                 kref_put(&dump->kref, ipr_release_dump);
3081
3082                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3083                 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
3084                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3085                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3086                 return;
3087         }
3088
3089 restart:
3090         do {
3091                 did_work = 0;
3092                 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
3093                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3094                         return;
3095                 }
3096
3097                 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3098                         if (res->del_from_ml && res->sdev) {
3099                                 did_work = 1;
3100                                 sdev = res->sdev;
3101                                 if (!scsi_device_get(sdev)) {
3102                                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
3103                                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3104                                         scsi_remove_device(sdev);
3105                                         scsi_device_put(sdev);
3106                                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3107                                 }
3108                                 break;
3109                         }
3110                 }
3111         } while(did_work);
3112
3113         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3114                 if (res->add_to_ml) {
3115                         bus = res->bus;
3116                         target = res->target;
3117                         lun = res->lun;
3118                         res->add_to_ml = 0;
3119                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3120                         scsi_add_device(ioa_cfg->host, bus, target, lun);
3121                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3122                         goto restart;
3123                 }
3124         }
3125
3126         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3127         kobject_uevent(&ioa_cfg->host->shost_dev.kobj, KOBJ_CHANGE);
3128         LEAVE;
3129 }
3130
3131 #ifdef CONFIG_SCSI_IPR_TRACE
3132 /**
3133  * ipr_read_trace - Dump the adapter trace
3134  * @filp:               open sysfs file
3135  * @kobj:               kobject struct
3136  * @bin_attr:           bin_attribute struct
3137  * @buf:                buffer
3138  * @off:                offset
3139  * @count:              buffer size
3140  *
3141  * Return value:
3142  *      number of bytes printed to buffer
3143  **/
3144 static ssize_t ipr_read_trace(struct file *filp, struct kobject *kobj,
3145                               struct bin_attribute *bin_attr,
3146                               char *buf, loff_t off, size_t count)
3147 {
3148         struct device *dev = container_of(kobj, struct device, kobj);
3149         struct Scsi_Host *shost = class_to_shost(dev);
3150         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3151         unsigned long lock_flags = 0;
3152         ssize_t ret;
3153
3154         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3155         ret = memory_read_from_buffer(buf, count, &off, ioa_cfg->trace,
3156                                 IPR_TRACE_SIZE);
3157         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3158
3159         return ret;
3160 }
3161
3162 static struct bin_attribute ipr_trace_attr = {
3163         .attr = {
3164                 .name = "trace",
3165                 .mode = S_IRUGO,
3166         },
3167         .size = 0,
3168         .read = ipr_read_trace,
3169 };
3170 #endif
3171
3172 /**
3173  * ipr_show_fw_version - Show the firmware version
3174  * @dev:        class device struct
3175  * @buf:        buffer
3176  *
3177  * Return value:
3178  *      number of bytes printed to buffer
3179  **/
3180 static ssize_t ipr_show_fw_version(struct device *dev,
3181                                    struct device_attribute *attr, char *buf)
3182 {
3183         struct Scsi_Host *shost = class_to_shost(dev);
3184         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3185         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
3186         unsigned long lock_flags = 0;
3187         int len;
3188
3189         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3190         len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
3191                        ucode_vpd->major_release, ucode_vpd->card_type,
3192                        ucode_vpd->minor_release[0],
3193                        ucode_vpd->minor_release[1]);
3194         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3195         return len;
3196 }
3197
3198 static struct device_attribute ipr_fw_version_attr = {
3199         .attr = {
3200                 .name =         "fw_version",
3201                 .mode =         S_IRUGO,
3202         },
3203         .show = ipr_show_fw_version,
3204 };
3205
3206 /**
3207  * ipr_show_log_level - Show the adapter's error logging level
3208  * @dev:        class device struct
3209  * @buf:        buffer
3210  *
3211  * Return value:
3212  *      number of bytes printed to buffer
3213  **/
3214 static ssize_t ipr_show_log_level(struct device *dev,
3215                                    struct device_attribute *attr, char *buf)
3216 {
3217         struct Scsi_Host *shost = class_to_shost(dev);
3218         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3219         unsigned long lock_flags = 0;
3220         int len;
3221
3222         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3223         len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
3224         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3225         return len;
3226 }
3227
3228 /**
3229  * ipr_store_log_level - Change the adapter's error logging level
3230  * @dev:        class device struct
3231  * @buf:        buffer
3232  *
3233  * Return value:
3234  *      number of bytes printed to buffer
3235  **/
3236 static ssize_t ipr_store_log_level(struct device *dev,
3237                                    struct device_attribute *attr,
3238                                    const char *buf, size_t count)
3239 {
3240         struct Scsi_Host *shost = class_to_shost(dev);
3241         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3242         unsigned long lock_flags = 0;
3243
3244         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3245         ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
3246         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3247         return strlen(buf);
3248 }
3249
3250 static struct device_attribute ipr_log_level_attr = {
3251         .attr = {
3252                 .name =         "log_level",
3253                 .mode =         S_IRUGO | S_IWUSR,
3254         },
3255         .show = ipr_show_log_level,
3256         .store = ipr_store_log_level
3257 };
3258
3259 /**
3260  * ipr_store_diagnostics - IOA Diagnostics interface
3261  * @dev:        device struct
3262  * @buf:        buffer
3263  * @count:      buffer size
3264  *
3265  * This function will reset the adapter and wait a reasonable
3266  * amount of time for any errors that the adapter might log.
3267  *
3268  * Return value:
3269  *      count on success / other on failure
3270  **/
3271 static ssize_t ipr_store_diagnostics(struct device *dev,
3272                                      struct device_attribute *attr,
3273                                      const char *buf, size_t count)
3274 {
3275         struct Scsi_Host *shost = class_to_shost(dev);
3276         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3277         unsigned long lock_flags = 0;
3278         int rc = count;
3279
3280         if (!capable(CAP_SYS_ADMIN))
3281                 return -EACCES;
3282
3283         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3284         while(ioa_cfg->in_reset_reload) {
3285                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3286                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3287                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3288         }
3289
3290         ioa_cfg->errors_logged = 0;
3291         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3292
3293         if (ioa_cfg->in_reset_reload) {
3294                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3295                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3296
3297                 /* Wait for a second for any errors to be logged */
3298                 msleep(1000);
3299         } else {
3300                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3301                 return -EIO;
3302         }
3303
3304         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3305         if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
3306                 rc = -EIO;
3307         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3308
3309         return rc;
3310 }
3311
3312 static struct device_attribute ipr_diagnostics_attr = {
3313         .attr = {
3314                 .name =         "run_diagnostics",
3315                 .mode =         S_IWUSR,
3316         },
3317         .store = ipr_store_diagnostics
3318 };
3319
3320 /**
3321  * ipr_show_adapter_state - Show the adapter's state
3322  * @class_dev:  device struct
3323  * @buf:        buffer
3324  *
3325  * Return value:
3326  *      number of bytes printed to buffer
3327  **/
3328 static ssize_t ipr_show_adapter_state(struct device *dev,
3329                                       struct device_attribute *attr, char *buf)
3330 {
3331         struct Scsi_Host *shost = class_to_shost(dev);
3332         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3333         unsigned long lock_flags = 0;
3334         int len;
3335
3336         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3337         if (ioa_cfg->ioa_is_dead)
3338                 len = snprintf(buf, PAGE_SIZE, "offline\n");
3339         else
3340                 len = snprintf(buf, PAGE_SIZE, "online\n");
3341         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3342         return len;
3343 }
3344
3345 /**
3346  * ipr_store_adapter_state - Change adapter state
3347  * @dev:        device struct
3348  * @buf:        buffer
3349  * @count:      buffer size
3350  *
3351  * This function will change the adapter's state.
3352  *
3353  * Return value:
3354  *      count on success / other on failure
3355  **/
3356 static ssize_t ipr_store_adapter_state(struct device *dev,
3357                                        struct device_attribute *attr,
3358                                        const char *buf, size_t count)
3359 {
3360         struct Scsi_Host *shost = class_to_shost(dev);
3361         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3362         unsigned long lock_flags;
3363         int result = count;
3364
3365         if (!capable(CAP_SYS_ADMIN))
3366                 return -EACCES;
3367
3368         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3369         if (ioa_cfg->ioa_is_dead && !strncmp(buf, "online", 6)) {
3370                 ioa_cfg->ioa_is_dead = 0;
3371                 ioa_cfg->reset_retries = 0;
3372                 ioa_cfg->in_ioa_bringdown = 0;
3373                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3374         }
3375         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3376         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3377
3378         return result;
3379 }
3380
3381 static struct device_attribute ipr_ioa_state_attr = {
3382         .attr = {
3383                 .name =         "online_state",
3384                 .mode =         S_IRUGO | S_IWUSR,
3385         },
3386         .show = ipr_show_adapter_state,
3387         .store = ipr_store_adapter_state
3388 };
3389
3390 /**
3391  * ipr_store_reset_adapter - Reset the adapter
3392  * @dev:        device struct
3393  * @buf:        buffer
3394  * @count:      buffer size
3395  *
3396  * This function will reset the adapter.
3397  *
3398  * Return value:
3399  *      count on success / other on failure
3400  **/
3401 static ssize_t ipr_store_reset_adapter(struct device *dev,
3402                                        struct device_attribute *attr,
3403                                        const char *buf, size_t count)
3404 {
3405         struct Scsi_Host *shost = class_to_shost(dev);
3406         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3407         unsigned long lock_flags;
3408         int result = count;
3409
3410         if (!capable(CAP_SYS_ADMIN))
3411                 return -EACCES;
3412
3413         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3414         if (!ioa_cfg->in_reset_reload)
3415                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3416         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3417         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3418
3419         return result;
3420 }
3421
3422 static struct device_attribute ipr_ioa_reset_attr = {
3423         .attr = {
3424                 .name =         "reset_host",
3425                 .mode =         S_IWUSR,
3426         },
3427         .store = ipr_store_reset_adapter
3428 };
3429
3430 /**
3431  * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
3432  * @buf_len:            buffer length
3433  *
3434  * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3435  * list to use for microcode download
3436  *
3437  * Return value:
3438  *      pointer to sglist / NULL on failure
3439  **/
3440 static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
3441 {
3442         int sg_size, order, bsize_elem, num_elem, i, j;
3443         struct ipr_sglist *sglist;
3444         struct scatterlist *scatterlist;
3445         struct page *page;
3446
3447         /* Get the minimum size per scatter/gather element */
3448         sg_size = buf_len / (IPR_MAX_SGLIST - 1);
3449
3450         /* Get the actual size per element */
3451         order = get_order(sg_size);
3452
3453         /* Determine the actual number of bytes per element */
3454         bsize_elem = PAGE_SIZE * (1 << order);
3455
3456         /* Determine the actual number of sg entries needed */
3457         if (buf_len % bsize_elem)
3458                 num_elem = (buf_len / bsize_elem) + 1;
3459         else
3460                 num_elem = buf_len / bsize_elem;
3461
3462         /* Allocate a scatter/gather list for the DMA */
3463         sglist = kzalloc(sizeof(struct ipr_sglist) +
3464                          (sizeof(struct scatterlist) * (num_elem - 1)),
3465                          GFP_KERNEL);
3466
3467         if (sglist == NULL) {
3468                 ipr_trace;
3469                 return NULL;
3470         }
3471
3472         scatterlist = sglist->scatterlist;
3473         sg_init_table(scatterlist, num_elem);
3474
3475         sglist->order = order;
3476         sglist->num_sg = num_elem;
3477
3478         /* Allocate a bunch of sg elements */
3479         for (i = 0; i < num_elem; i++) {
3480                 page = alloc_pages(GFP_KERNEL, order);
3481                 if (!page) {
3482                         ipr_trace;
3483
3484                         /* Free up what we already allocated */
3485                         for (j = i - 1; j >= 0; j--)
3486                                 __free_pages(sg_page(&scatterlist[j]), order);
3487                         kfree(sglist);
3488                         return NULL;
3489                 }
3490
3491                 sg_set_page(&scatterlist[i], page, 0, 0);
3492         }
3493
3494         return sglist;
3495 }
3496
3497 /**
3498  * ipr_free_ucode_buffer - Frees a microcode download buffer
3499  * @p_dnld:             scatter/gather list pointer
3500  *
3501  * Free a DMA'able ucode download buffer previously allocated with
3502  * ipr_alloc_ucode_buffer
3503  *
3504  * Return value:
3505  *      nothing
3506  **/
3507 static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
3508 {
3509         int i;
3510
3511         for (i = 0; i < sglist->num_sg; i++)
3512                 __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
3513
3514         kfree(sglist);
3515 }
3516
3517 /**
3518  * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
3519  * @sglist:             scatter/gather list pointer
3520  * @buffer:             buffer pointer
3521  * @len:                buffer length
3522  *
3523  * Copy a microcode image from a user buffer into a buffer allocated by
3524  * ipr_alloc_ucode_buffer
3525  *
3526  * Return value:
3527  *      0 on success / other on failure
3528  **/
3529 static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
3530                                  u8 *buffer, u32 len)
3531 {
3532         int bsize_elem, i, result = 0;
3533         struct scatterlist *scatterlist;
3534         void *kaddr;
3535
3536         /* Determine the actual number of bytes per element */
3537         bsize_elem = PAGE_SIZE * (1 << sglist->order);
3538
3539         scatterlist = sglist->scatterlist;
3540
3541         for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
3542                 struct page *page = sg_page(&scatterlist[i]);
3543
3544                 kaddr = kmap(page);
3545                 memcpy(kaddr, buffer, bsize_elem);
3546                 kunmap(page);
3547
3548                 scatterlist[i].length = bsize_elem;
3549
3550                 if (result != 0) {
3551                         ipr_trace;
3552                         return result;
3553                 }
3554         }
3555
3556         if (len % bsize_elem) {
3557                 struct page *page = sg_page(&scatterlist[i]);
3558
3559                 kaddr = kmap(page);
3560                 memcpy(kaddr, buffer, len % bsize_elem);
3561                 kunmap(page);
3562
3563                 scatterlist[i].length = len % bsize_elem;
3564         }
3565
3566         sglist->buffer_len = len;
3567         return result;
3568 }
3569
3570 /**
3571  * ipr_build_ucode_ioadl64 - Build a microcode download IOADL
3572  * @ipr_cmd:            ipr command struct
3573  * @sglist:             scatter/gather list
3574  *
3575  * Builds a microcode download IOA data list (IOADL).
3576  *
3577  **/
3578 static void ipr_build_ucode_ioadl64(struct ipr_cmnd *ipr_cmd,
3579                                     struct ipr_sglist *sglist)
3580 {
3581         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3582         struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
3583         struct scatterlist *scatterlist = sglist->scatterlist;
3584         int i;
3585
3586         ipr_cmd->dma_use_sg = sglist->num_dma_sg;
3587         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3588         ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3589
3590         ioarcb->ioadl_len =
3591                 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
3592         for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3593                 ioadl64[i].flags = cpu_to_be32(IPR_IOADL_FLAGS_WRITE);
3594                 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(&scatterlist[i]));
3595                 ioadl64[i].address = cpu_to_be64(sg_dma_address(&scatterlist[i]));
3596         }
3597
3598         ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3599 }
3600
3601 /**
3602  * ipr_build_ucode_ioadl - Build a microcode download IOADL
3603  * @ipr_cmd:    ipr command struct
3604  * @sglist:             scatter/gather list
3605  *
3606  * Builds a microcode download IOA data list (IOADL).
3607  *
3608  **/
3609 static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
3610                                   struct ipr_sglist *sglist)
3611 {
3612         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3613         struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
3614         struct scatterlist *scatterlist = sglist->scatterlist;
3615         int i;
3616
3617         ipr_cmd->dma_use_sg = sglist->num_dma_sg;
3618         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3619         ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3620
3621         ioarcb->ioadl_len =
3622                 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3623
3624         for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3625                 ioadl[i].flags_and_data_len =
3626                         cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
3627                 ioadl[i].address =
3628                         cpu_to_be32(sg_dma_address(&scatterlist[i]));
3629         }
3630
3631         ioadl[i-1].flags_and_data_len |=
3632                 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3633 }
3634
3635 /**
3636  * ipr_update_ioa_ucode - Update IOA's microcode
3637  * @ioa_cfg:    ioa config struct
3638  * @sglist:             scatter/gather list
3639  *
3640  * Initiate an adapter reset to update the IOA's microcode
3641  *
3642  * Return value:
3643  *      0 on success / -EIO on failure
3644  **/
3645 static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
3646                                 struct ipr_sglist *sglist)
3647 {
3648         unsigned long lock_flags;
3649
3650         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3651         while(ioa_cfg->in_reset_reload) {
3652                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3653                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3654                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3655         }
3656
3657         if (ioa_cfg->ucode_sglist) {
3658                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3659                 dev_err(&ioa_cfg->pdev->dev,
3660                         "Microcode download already in progress\n");
3661                 return -EIO;
3662         }
3663
3664         sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
3665                                         sglist->num_sg, DMA_TO_DEVICE);
3666
3667         if (!sglist->num_dma_sg) {
3668                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3669                 dev_err(&ioa_cfg->pdev->dev,
3670                         "Failed to map microcode download buffer!\n");
3671                 return -EIO;
3672         }
3673
3674         ioa_cfg->ucode_sglist = sglist;
3675         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3676         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3677         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3678
3679         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3680         ioa_cfg->ucode_sglist = NULL;
3681         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3682         return 0;
3683 }
3684
3685 /**
3686  * ipr_store_update_fw - Update the firmware on the adapter
3687  * @class_dev:  device struct
3688  * @buf:        buffer
3689  * @count:      buffer size
3690  *
3691  * This function will update the firmware on the adapter.
3692  *
3693  * Return value:
3694  *      count on success / other on failure
3695  **/
3696 static ssize_t ipr_store_update_fw(struct device *dev,
3697                                    struct device_attribute *attr,
3698                                    const char *buf, size_t count)
3699 {
3700         struct Scsi_Host *shost = class_to_shost(dev);
3701         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3702         struct ipr_ucode_image_header *image_hdr;
3703         const struct firmware *fw_entry;
3704         struct ipr_sglist *sglist;
3705         char fname[100];
3706         char *src;
3707         int len, result, dnld_size;
3708
3709         if (!capable(CAP_SYS_ADMIN))
3710                 return -EACCES;
3711
3712         len = snprintf(fname, 99, "%s", buf);
3713         fname[len-1] = '\0';
3714
3715         if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
3716                 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
3717                 return -EIO;
3718         }
3719
3720         image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
3721
3722         if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
3723             (ioa_cfg->vpd_cbs->page3_data.card_type &&
3724              ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
3725                 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
3726                 release_firmware(fw_entry);
3727                 return -EINVAL;
3728         }
3729
3730         src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
3731         dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
3732         sglist = ipr_alloc_ucode_buffer(dnld_size);
3733
3734         if (!sglist) {
3735                 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
3736                 release_firmware(fw_entry);
3737                 return -ENOMEM;
3738         }
3739
3740         result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
3741
3742         if (result) {
3743                 dev_err(&ioa_cfg->pdev->dev,
3744                         "Microcode buffer copy to DMA buffer failed\n");
3745                 goto out;
3746         }
3747
3748         result = ipr_update_ioa_ucode(ioa_cfg, sglist);
3749
3750         if (!result)
3751                 result = count;
3752 out:
3753         ipr_free_ucode_buffer(sglist);
3754         release_firmware(fw_entry);
3755         return result;
3756 }
3757
3758 static struct device_attribute ipr_update_fw_attr = {
3759         .attr = {
3760                 .name =         "update_fw",
3761                 .mode =         S_IWUSR,
3762         },
3763         .store = ipr_store_update_fw
3764 };
3765
3766 /**
3767  * ipr_show_fw_type - Show the adapter's firmware type.
3768  * @dev:        class device struct
3769  * @buf:        buffer
3770  *
3771  * Return value:
3772  *      number of bytes printed to buffer
3773  **/
3774 static ssize_t ipr_show_fw_type(struct device *dev,
3775                                 struct device_attribute *attr, char *buf)
3776 {
3777         struct Scsi_Host *shost = class_to_shost(dev);
3778         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3779         unsigned long lock_flags = 0;
3780         int len;
3781
3782         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3783         len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->sis64);
3784         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3785         return len;
3786 }
3787
3788 static struct device_attribute ipr_ioa_fw_type_attr = {
3789         .attr = {
3790                 .name =         "fw_type",
3791                 .mode =         S_IRUGO,
3792         },
3793         .show = ipr_show_fw_type
3794 };
3795
3796 static struct device_attribute *ipr_ioa_attrs[] = {
3797         &ipr_fw_version_attr,
3798         &ipr_log_level_attr,
3799         &ipr_diagnostics_attr,
3800         &ipr_ioa_state_attr,
3801         &ipr_ioa_reset_attr,
3802         &ipr_update_fw_attr,
3803         &ipr_ioa_fw_type_attr,
3804         NULL,
3805 };
3806
3807 #ifdef CONFIG_SCSI_IPR_DUMP
3808 /**
3809  * ipr_read_dump - Dump the adapter
3810  * @filp:               open sysfs file
3811  * @kobj:               kobject struct
3812  * @bin_attr:           bin_attribute struct
3813  * @buf:                buffer
3814  * @off:                offset
3815  * @count:              buffer size
3816  *
3817  * Return value:
3818  *      number of bytes printed to buffer
3819  **/
3820 static ssize_t ipr_read_dump(struct file *filp, struct kobject *kobj,
3821                              struct bin_attribute *bin_attr,
3822                              char *buf, loff_t off, size_t count)
3823 {
3824         struct device *cdev = container_of(kobj, struct device, kobj);
3825         struct Scsi_Host *shost = class_to_shost(cdev);
3826         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3827         struct ipr_dump *dump;
3828         unsigned long lock_flags = 0;
3829         char *src;
3830         int len;
3831         size_t rc = count;
3832
3833         if (!capable(CAP_SYS_ADMIN))
3834                 return -EACCES;
3835
3836         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3837         dump = ioa_cfg->dump;
3838
3839         if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
3840                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3841                 return 0;
3842         }
3843         kref_get(&dump->kref);
3844         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3845
3846         if (off > dump->driver_dump.hdr.len) {
3847                 kref_put(&dump->kref, ipr_release_dump);
3848                 return 0;
3849         }
3850
3851         if (off + count > dump->driver_dump.hdr.len) {
3852                 count = dump->driver_dump.hdr.len - off;
3853                 rc = count;
3854         }
3855
3856         if (count && off < sizeof(dump->driver_dump)) {
3857                 if (off + count > sizeof(dump->driver_dump))
3858                         len = sizeof(dump->driver_dump) - off;
3859                 else
3860                         len = count;
3861                 src = (u8 *)&dump->driver_dump + off;
3862                 memcpy(buf, src, len);
3863                 buf += len;
3864                 off += len;
3865                 count -= len;
3866         }
3867
3868         off -= sizeof(dump->driver_dump);
3869
3870         if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
3871                 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
3872                         len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
3873                 else
3874                         len = count;
3875                 src = (u8 *)&dump->ioa_dump + off;
3876                 memcpy(buf, src, len);
3877                 buf += len;
3878                 off += len;
3879                 count -= len;
3880         }
3881
3882         off -= offsetof(struct ipr_ioa_dump, ioa_data);
3883
3884         while (count) {
3885                 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
3886                         len = PAGE_ALIGN(off) - off;
3887                 else
3888                         len = count;
3889                 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
3890                 src += off & ~PAGE_MASK;
3891                 memcpy(buf, src, len);
3892                 buf += len;
3893                 off += len;
3894                 count -= len;
3895         }
3896
3897         kref_put(&dump->kref, ipr_release_dump);
3898         return rc;
3899 }
3900
3901 /**
3902  * ipr_alloc_dump - Prepare for adapter dump
3903  * @ioa_cfg:    ioa config struct
3904  *
3905  * Return value:
3906  *      0 on success / other on failure
3907  **/
3908 static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
3909 {
3910         struct ipr_dump *dump;
3911         unsigned long lock_flags = 0;
3912
3913         dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
3914
3915         if (!dump) {
3916                 ipr_err("Dump memory allocation failed\n");
3917                 return -ENOMEM;
3918         }
3919
3920         kref_init(&dump->kref);
3921         dump->ioa_cfg = ioa_cfg;
3922
3923         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3924
3925         if (INACTIVE != ioa_cfg->sdt_state) {
3926                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3927                 kfree(dump);
3928                 return 0;
3929         }
3930
3931         ioa_cfg->dump = dump;
3932         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
3933         if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
3934                 ioa_cfg->dump_taken = 1;
3935                 schedule_work(&ioa_cfg->work_q);
3936         }
3937         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3938
3939         return 0;
3940 }
3941
3942 /**
3943  * ipr_free_dump - Free adapter dump memory
3944  * @ioa_cfg:    ioa config struct
3945  *
3946  * Return value:
3947  *      0 on success / other on failure
3948  **/
3949 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
3950 {
3951         struct ipr_dump *dump;
3952         unsigned long lock_flags = 0;
3953
3954         ENTER;
3955
3956         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3957         dump = ioa_cfg->dump;
3958         if (!dump) {
3959                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3960                 return 0;
3961         }
3962
3963         ioa_cfg->dump = NULL;
3964         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3965
3966         kref_put(&dump->kref, ipr_release_dump);
3967
3968         LEAVE;
3969         return 0;
3970 }
3971
3972 /**
3973  * ipr_write_dump - Setup dump state of adapter
3974  * @filp:               open sysfs file
3975  * @kobj:               kobject struct
3976  * @bin_attr:           bin_attribute struct
3977  * @buf:                buffer
3978  * @off:                offset
3979  * @count:              buffer size
3980  *
3981  * Return value:
3982  *      number of bytes printed to buffer
3983  **/
3984 static ssize_t ipr_write_dump(struct file *filp, struct kobject *kobj,
3985                               struct bin_attribute *bin_attr,
3986                               char *buf, loff_t off, size_t count)
3987 {
3988         struct device *cdev = container_of(kobj, struct device, kobj);
3989         struct Scsi_Host *shost = class_to_shost(cdev);
3990         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3991         int rc;
3992
3993         if (!capable(CAP_SYS_ADMIN))
3994                 return -EACCES;
3995
3996         if (buf[0] == '1')
3997                 rc = ipr_alloc_dump(ioa_cfg);
3998         else if (buf[0] == '0')
3999                 rc = ipr_free_dump(ioa_cfg);
4000         else
4001                 return -EINVAL;
4002
4003         if (rc)
4004                 return rc;
4005         else
4006                 return count;
4007 }
4008
4009 static struct bin_attribute ipr_dump_attr = {
4010         .attr = {
4011                 .name = "dump",
4012                 .mode = S_IRUSR | S_IWUSR,
4013         },
4014         .size = 0,
4015         .read = ipr_read_dump,
4016         .write = ipr_write_dump
4017 };
4018 #else
4019 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
4020 #endif
4021
4022 /**
4023  * ipr_change_queue_depth - Change the device's queue depth
4024  * @sdev:       scsi device struct
4025  * @qdepth:     depth to set
4026  * @reason:     calling context
4027  *
4028  * Return value:
4029  *      actual depth set
4030  **/
4031 static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth,
4032                                   int reason)
4033 {
4034         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4035         struct ipr_resource_entry *res;
4036         unsigned long lock_flags = 0;
4037
4038         if (reason != SCSI_QDEPTH_DEFAULT)
4039                 return -EOPNOTSUPP;
4040
4041         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4042         res = (struct ipr_resource_entry *)sdev->hostdata;
4043
4044         if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
4045                 qdepth = IPR_MAX_CMD_PER_ATA_LUN;
4046         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4047
4048         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
4049         return sdev->queue_depth;
4050 }
4051
4052 /**
4053  * ipr_change_queue_type - Change the device's queue type
4054  * @dsev:               scsi device struct
4055  * @tag_type:   type of tags to use
4056  *
4057  * Return value:
4058  *      actual queue type set
4059  **/
4060 static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
4061 {
4062         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4063         struct ipr_resource_entry *res;
4064         unsigned long lock_flags = 0;
4065
4066         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4067         res = (struct ipr_resource_entry *)sdev->hostdata;
4068
4069         if (res) {
4070                 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
4071                         /*
4072                          * We don't bother quiescing the device here since the
4073                          * adapter firmware does it for us.
4074                          */
4075                         scsi_set_tag_type(sdev, tag_type);
4076
4077                         if (tag_type)
4078                                 scsi_activate_tcq(sdev, sdev->queue_depth);
4079                         else
4080                                 scsi_deactivate_tcq(sdev, sdev->queue_depth);
4081                 } else
4082                         tag_type = 0;
4083         } else
4084                 tag_type = 0;
4085
4086         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4087         return tag_type;
4088 }
4089
4090 /**
4091  * ipr_show_adapter_handle - Show the adapter's resource handle for this device
4092  * @dev:        device struct
4093  * @attr:       device attribute structure
4094  * @buf:        buffer
4095  *
4096  * Return value:
4097  *      number of bytes printed to buffer
4098  **/
4099 static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
4100 {
4101         struct scsi_device *sdev = to_scsi_device(dev);
4102         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4103         struct ipr_resource_entry *res;
4104         unsigned long lock_flags = 0;
4105         ssize_t len = -ENXIO;
4106
4107         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4108         res = (struct ipr_resource_entry *)sdev->hostdata;
4109         if (res)
4110                 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->res_handle);
4111         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4112         return len;
4113 }
4114
4115 static struct device_attribute ipr_adapter_handle_attr = {
4116         .attr = {
4117                 .name =         "adapter_handle",
4118                 .mode =         S_IRUSR,
4119         },
4120         .show = ipr_show_adapter_handle
4121 };
4122
4123 /**
4124  * ipr_show_resource_path - Show the resource path or the resource address for
4125  *                          this device.
4126  * @dev:        device struct
4127  * @attr:       device attribute structure
4128  * @buf:        buffer
4129  *
4130  * Return value:
4131  *      number of bytes printed to buffer
4132  **/
4133 static ssize_t ipr_show_resource_path(struct device *dev, struct device_attribute *attr, char *buf)
4134 {
4135         struct scsi_device *sdev = to_scsi_device(dev);
4136         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4137         struct ipr_resource_entry *res;
4138         unsigned long lock_flags = 0;
4139         ssize_t len = -ENXIO;
4140         char buffer[IPR_MAX_RES_PATH_LENGTH];
4141
4142         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4143         res = (struct ipr_resource_entry *)sdev->hostdata;
4144         if (res && ioa_cfg->sis64)
4145                 len = snprintf(buf, PAGE_SIZE, "%s\n",
4146                                ipr_format_res_path(res->res_path, buffer,
4147                                                    sizeof(buffer)));
4148         else if (res)
4149                 len = snprintf(buf, PAGE_SIZE, "%d:%d:%d:%d\n", ioa_cfg->host->host_no,
4150                                res->bus, res->target, res->lun);
4151
4152         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4153         return len;
4154 }
4155
4156 static struct device_attribute ipr_resource_path_attr = {
4157         .attr = {
4158                 .name =         "resource_path",
4159                 .mode =         S_IRUGO,
4160         },
4161         .show = ipr_show_resource_path
4162 };
4163
4164 /**
4165  * ipr_show_device_id - Show the device_id for this device.
4166  * @dev:        device struct
4167  * @attr:       device attribute structure
4168  * @buf:        buffer
4169  *
4170  * Return value:
4171  *      number of bytes printed to buffer
4172  **/
4173 static ssize_t ipr_show_device_id(struct device *dev, struct device_attribute *attr, char *buf)
4174 {
4175         struct scsi_device *sdev = to_scsi_device(dev);
4176         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4177         struct ipr_resource_entry *res;
4178         unsigned long lock_flags = 0;
4179         ssize_t len = -ENXIO;
4180
4181         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4182         res = (struct ipr_resource_entry *)sdev->hostdata;
4183         if (res && ioa_cfg->sis64)
4184                 len = snprintf(buf, PAGE_SIZE, "0x%llx\n", res->dev_id);
4185         else if (res)
4186                 len = snprintf(buf, PAGE_SIZE, "0x%llx\n", res->lun_wwn);
4187
4188         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4189         return len;
4190 }
4191
4192 static struct device_attribute ipr_device_id_attr = {
4193         .attr = {
4194                 .name =         "device_id",
4195                 .mode =         S_IRUGO,
4196         },
4197         .show = ipr_show_device_id
4198 };
4199
4200 /**
4201  * ipr_show_resource_type - Show the resource type for this device.
4202  * @dev:        device struct
4203  * @attr:       device attribute structure
4204  * @buf:        buffer
4205  *
4206  * Return value:
4207  *      number of bytes printed to buffer
4208  **/
4209 static ssize_t ipr_show_resource_type(struct device *dev, struct device_attribute *attr, char *buf)
4210 {
4211         struct scsi_device *sdev = to_scsi_device(dev);
4212         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4213         struct ipr_resource_entry *res;
4214         unsigned long lock_flags = 0;
4215         ssize_t len = -ENXIO;
4216
4217         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4218         res = (struct ipr_resource_entry *)sdev->hostdata;
4219
4220         if (res)
4221                 len = snprintf(buf, PAGE_SIZE, "%x\n", res->type);
4222
4223         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4224         return len;
4225 }
4226
4227 static struct device_attribute ipr_resource_type_attr = {
4228         .attr = {
4229                 .name =         "resource_type",
4230                 .mode =         S_IRUGO,
4231         },
4232         .show = ipr_show_resource_type
4233 };
4234
4235 static struct device_attribute *ipr_dev_attrs[] = {
4236         &ipr_adapter_handle_attr,
4237         &ipr_resource_path_attr,
4238         &ipr_device_id_attr,
4239         &ipr_resource_type_attr,
4240         NULL,
4241 };
4242
4243 /**
4244  * ipr_biosparam - Return the HSC mapping
4245  * @sdev:                       scsi device struct
4246  * @block_device:       block device pointer
4247  * @capacity:           capacity of the device
4248  * @parm:                       Array containing returned HSC values.
4249  *
4250  * This function generates the HSC parms that fdisk uses.
4251  * We want to make sure we return something that places partitions
4252  * on 4k boundaries for best performance with the IOA.
4253  *
4254  * Return value:
4255  *      0 on success
4256  **/
4257 static int ipr_biosparam(struct scsi_device *sdev,
4258                          struct block_device *block_device,
4259                          sector_t capacity, int *parm)
4260 {
4261         int heads, sectors;
4262         sector_t cylinders;
4263
4264         heads = 128;
4265         sectors = 32;
4266
4267         cylinders = capacity;
4268         sector_div(cylinders, (128 * 32));
4269
4270         /* return result */
4271         parm[0] = heads;
4272         parm[1] = sectors;
4273         parm[2] = cylinders;
4274
4275         return 0;
4276 }
4277
4278 /**
4279  * ipr_find_starget - Find target based on bus/target.
4280  * @starget:    scsi target struct
4281  *
4282  * Return value:
4283  *      resource entry pointer if found / NULL if not found
4284  **/
4285 static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
4286 {
4287         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4288         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4289         struct ipr_resource_entry *res;
4290
4291         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4292                 if ((res->bus == starget->channel) &&
4293                     (res->target == starget->id) &&
4294                     (res->lun == 0)) {
4295                         return res;
4296                 }
4297         }
4298
4299         return NULL;
4300 }
4301
4302 static struct ata_port_info sata_port_info;
4303
4304 /**
4305  * ipr_target_alloc - Prepare for commands to a SCSI target
4306  * @starget:    scsi target struct
4307  *
4308  * If the device is a SATA device, this function allocates an
4309  * ATA port with libata, else it does nothing.
4310  *
4311  * Return value:
4312  *      0 on success / non-0 on failure
4313  **/
4314 static int ipr_target_alloc(struct scsi_target *starget)
4315 {
4316         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4317         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4318         struct ipr_sata_port *sata_port;
4319         struct ata_port *ap;
4320         struct ipr_resource_entry *res;
4321         unsigned long lock_flags;
4322
4323         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4324         res = ipr_find_starget(starget);
4325         starget->hostdata = NULL;
4326
4327         if (res && ipr_is_gata(res)) {
4328                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4329                 sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
4330                 if (!sata_port)
4331                         return -ENOMEM;
4332
4333                 ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
4334                 if (ap) {
4335                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4336                         sata_port->ioa_cfg = ioa_cfg;
4337                         sata_port->ap = ap;
4338                         sata_port->res = res;
4339
4340                         res->sata_port = sata_port;
4341                         ap->private_data = sata_port;
4342                         starget->hostdata = sata_port;
4343                 } else {
4344                         kfree(sata_port);
4345                         return -ENOMEM;
4346                 }
4347         }
4348         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4349
4350         return 0;
4351 }
4352
4353 /**
4354  * ipr_target_destroy - Destroy a SCSI target
4355  * @starget:    scsi target struct
4356  *
4357  * If the device was a SATA device, this function frees the libata
4358  * ATA port, else it does nothing.
4359  *
4360  **/
4361 static void ipr_target_destroy(struct scsi_target *starget)
4362 {
4363         struct ipr_sata_port *sata_port = starget->hostdata;
4364         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4365         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4366
4367         if (ioa_cfg->sis64) {
4368                 if (starget->channel == IPR_ARRAY_VIRTUAL_BUS)
4369                         clear_bit(starget->id, ioa_cfg->array_ids);
4370                 else if (starget->channel == IPR_VSET_VIRTUAL_BUS)
4371                         clear_bit(starget->id, ioa_cfg->vset_ids);
4372                 else if (starget->channel == 0)
4373                         clear_bit(starget->id, ioa_cfg->target_ids);
4374         }
4375
4376         if (sata_port) {
4377                 starget->hostdata = NULL;
4378                 ata_sas_port_destroy(sata_port->ap);
4379                 kfree(sata_port);
4380         }
4381 }
4382
4383 /**
4384  * ipr_find_sdev - Find device based on bus/target/lun.
4385  * @sdev:       scsi device struct
4386  *
4387  * Return value:
4388  *      resource entry pointer if found / NULL if not found
4389  **/
4390 static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
4391 {
4392         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4393         struct ipr_resource_entry *res;
4394
4395         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4396                 if ((res->bus == sdev->channel) &&
4397                     (res->target == sdev->id) &&
4398                     (res->lun == sdev->lun))
4399                         return res;
4400         }
4401
4402         return NULL;
4403 }
4404
4405 /**
4406  * ipr_slave_destroy - Unconfigure a SCSI device
4407  * @sdev:       scsi device struct
4408  *
4409  * Return value:
4410  *      nothing
4411  **/
4412 static void ipr_slave_destroy(struct scsi_device *sdev)
4413 {
4414         struct ipr_resource_entry *res;
4415         struct ipr_ioa_cfg *ioa_cfg;
4416         unsigned long lock_flags = 0;
4417
4418         ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4419
4420         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4421         res = (struct ipr_resource_entry *) sdev->hostdata;
4422         if (res) {
4423                 if (res->sata_port)
4424                         res->sata_port->ap->link.device[0].class = ATA_DEV_NONE;
4425                 sdev->hostdata = NULL;
4426                 res->sdev = NULL;
4427                 res->sata_port = NULL;
4428         }
4429         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4430 }
4431
4432 /**
4433  * ipr_slave_configure - Configure a SCSI device
4434  * @sdev:       scsi device struct
4435  *
4436  * This function configures the specified scsi device.
4437  *
4438  * Return value:
4439  *      0 on success
4440  **/
4441 static int ipr_slave_configure(struct scsi_device *sdev)
4442 {
4443         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4444         struct ipr_resource_entry *res;
4445         struct ata_port *ap = NULL;
4446         unsigned long lock_flags = 0;
4447         char buffer[IPR_MAX_RES_PATH_LENGTH];
4448
4449         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4450         res = sdev->hostdata;
4451         if (res) {
4452                 if (ipr_is_af_dasd_device(res))
4453                         sdev->type = TYPE_RAID;
4454                 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
4455                         sdev->scsi_level = 4;
4456                         sdev->no_uld_attach = 1;
4457                 }
4458                 if (ipr_is_vset_device(res)) {
4459                         blk_queue_rq_timeout(sdev->request_queue,
4460                                              IPR_VSET_RW_TIMEOUT);
4461                         blk_queue_max_hw_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
4462                 }
4463                 if (ipr_is_gata(res) && res->sata_port)
4464                         ap = res->sata_port->ap;
4465                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4466
4467                 if (ap) {
4468                         scsi_adjust_queue_depth(sdev, 0, IPR_MAX_CMD_PER_ATA_LUN);
4469                         ata_sas_slave_configure(sdev, ap);
4470                 } else
4471                         scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
4472                 if (ioa_cfg->sis64)
4473                         sdev_printk(KERN_INFO, sdev, "Resource path: %s\n",
4474                                     ipr_format_res_path(res->res_path, buffer,
4475                                                         sizeof(buffer)));
4476                 return 0;
4477         }
4478         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4479         return 0;
4480 }
4481
4482 /**
4483  * ipr_ata_slave_alloc - Prepare for commands to a SATA device
4484  * @sdev:       scsi device struct
4485  *
4486  * This function initializes an ATA port so that future commands
4487  * sent through queuecommand will work.
4488  *
4489  * Return value:
4490  *      0 on success
4491  **/
4492 static int ipr_ata_slave_alloc(struct scsi_device *sdev)
4493 {
4494         struct ipr_sata_port *sata_port = NULL;
4495         int rc = -ENXIO;
4496
4497         ENTER;
4498         if (sdev->sdev_target)
4499                 sata_port = sdev->sdev_target->hostdata;
4500         if (sata_port)
4501                 rc = ata_sas_port_init(sata_port->ap);
4502         if (rc)
4503                 ipr_slave_destroy(sdev);
4504
4505         LEAVE;
4506         return rc;
4507 }
4508
4509 /**
4510  * ipr_slave_alloc - Prepare for commands to a device.
4511  * @sdev:       scsi device struct
4512  *
4513  * This function saves a pointer to the resource entry
4514  * in the scsi device struct if the device exists. We
4515  * can then use this pointer in ipr_queuecommand when
4516  * handling new commands.
4517  *
4518  * Return value:
4519  *      0 on success / -ENXIO if device does not exist
4520  **/
4521 static int ipr_slave_alloc(struct scsi_device *sdev)
4522 {
4523         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4524         struct ipr_resource_entry *res;
4525         unsigned long lock_flags;
4526         int rc = -ENXIO;
4527
4528         sdev->hostdata = NULL;
4529
4530         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4531
4532         res = ipr_find_sdev(sdev);
4533         if (res) {
4534                 res->sdev = sdev;
4535                 res->add_to_ml = 0;
4536                 res->in_erp = 0;
4537                 sdev->hostdata = res;
4538                 if (!ipr_is_naca_model(res))
4539                         res->needs_sync_complete = 1;
4540                 rc = 0;
4541                 if (ipr_is_gata(res)) {
4542                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4543                         return ipr_ata_slave_alloc(sdev);
4544                 }
4545         }
4546
4547         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4548
4549         return rc;
4550 }
4551
4552 /**
4553  * ipr_eh_host_reset - Reset the host adapter
4554  * @scsi_cmd:   scsi command struct
4555  *
4556  * Return value:
4557  *      SUCCESS / FAILED
4558  **/
4559 static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
4560 {
4561         struct ipr_ioa_cfg *ioa_cfg;
4562         int rc;
4563
4564         ENTER;
4565         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
4566
4567         dev_err(&ioa_cfg->pdev->dev,
4568                 "Adapter being reset as a result of error recovery.\n");
4569
4570         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4571                 ioa_cfg->sdt_state = GET_DUMP;
4572
4573         rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
4574
4575         LEAVE;
4576         return rc;
4577 }
4578
4579 static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
4580 {
4581         int rc;
4582
4583         spin_lock_irq(cmd->device->host->host_lock);
4584         rc = __ipr_eh_host_reset(cmd);
4585         spin_unlock_irq(cmd->device->host->host_lock);
4586
4587         return rc;
4588 }
4589
4590 /**
4591  * ipr_device_reset - Reset the device
4592  * @ioa_cfg:    ioa config struct
4593  * @res:                resource entry struct
4594  *
4595  * This function issues a device reset to the affected device.
4596  * If the device is a SCSI device, a LUN reset will be sent
4597  * to the device first. If that does not work, a target reset
4598  * will be sent. If the device is a SATA device, a PHY reset will
4599  * be sent.
4600  *
4601  * Return value:
4602  *      0 on success / non-zero on failure
4603  **/
4604 static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
4605                             struct ipr_resource_entry *res)
4606 {
4607         struct ipr_cmnd *ipr_cmd;
4608         struct ipr_ioarcb *ioarcb;
4609         struct ipr_cmd_pkt *cmd_pkt;
4610         struct ipr_ioarcb_ata_regs *regs;
4611         u32 ioasc;
4612
4613         ENTER;
4614         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4615         ioarcb = &ipr_cmd->ioarcb;
4616         cmd_pkt = &ioarcb->cmd_pkt;
4617
4618         if (ipr_cmd->ioa_cfg->sis64) {
4619                 regs = &ipr_cmd->i.ata_ioadl.regs;
4620                 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
4621         } else
4622                 regs = &ioarcb->u.add_data.u.regs;
4623
4624         ioarcb->res_handle = res->res_handle;
4625         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4626         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
4627         if (ipr_is_gata(res)) {
4628                 cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
4629                 ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(regs->flags));
4630                 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
4631         }
4632
4633         ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
4634         ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
4635         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4636         if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET) {
4637                 if (ipr_cmd->ioa_cfg->sis64)
4638                         memcpy(&res->sata_port->ioasa, &ipr_cmd->s.ioasa64.u.gata,
4639                                sizeof(struct ipr_ioasa_gata));
4640                 else
4641                         memcpy(&res->sata_port->ioasa, &ipr_cmd->s.ioasa.u.gata,
4642                                sizeof(struct ipr_ioasa_gata));
4643         }
4644
4645         LEAVE;
4646         return (IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0);
4647 }
4648
4649 /**
4650  * ipr_sata_reset - Reset the SATA port
4651  * @link:       SATA link to reset
4652  * @classes:    class of the attached device
4653  *
4654  * This function issues a SATA phy reset to the affected ATA link.
4655  *
4656  * Return value:
4657  *      0 on success / non-zero on failure
4658  **/
4659 static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
4660                                 unsigned long deadline)
4661 {
4662         struct ipr_sata_port *sata_port = link->ap->private_data;
4663         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
4664         struct ipr_resource_entry *res;
4665         unsigned long lock_flags = 0;
4666         int rc = -ENXIO;
4667
4668         ENTER;
4669         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4670         while(ioa_cfg->in_reset_reload) {
4671                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4672                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4673                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4674         }
4675
4676         res = sata_port->res;
4677         if (res) {
4678                 rc = ipr_device_reset(ioa_cfg, res);
4679                 *classes = res->ata_class;
4680         }
4681
4682         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4683         LEAVE;
4684         return rc;
4685 }
4686
4687 /**
4688  * ipr_eh_dev_reset - Reset the device
4689  * @scsi_cmd:   scsi command struct
4690  *
4691  * This function issues a device reset to the affected device.
4692  * A LUN reset will be sent to the device first. If that does
4693  * not work, a target reset will be sent.
4694  *
4695  * Return value:
4696  *      SUCCESS / FAILED
4697  **/
4698 static int __ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
4699 {
4700         struct ipr_cmnd *ipr_cmd;
4701         struct ipr_ioa_cfg *ioa_cfg;
4702         struct ipr_resource_entry *res;
4703         struct ata_port *ap;
4704         int rc = 0;
4705
4706         ENTER;
4707         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
4708         res = scsi_cmd->device->hostdata;
4709
4710         if (!res)
4711                 return FAILED;
4712
4713         /*
4714          * If we are currently going through reset/reload, return failed. This will force the
4715          * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
4716          * reset to complete
4717          */
4718         if (ioa_cfg->in_reset_reload)
4719                 return FAILED;
4720         if (ioa_cfg->ioa_is_dead)
4721                 return FAILED;
4722
4723         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
4724                 if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
4725                         if (ipr_cmd->scsi_cmd)
4726                                 ipr_cmd->done = ipr_scsi_eh_done;
4727                         if (ipr_cmd->qc)
4728                                 ipr_cmd->done = ipr_sata_eh_done;
4729                         if (ipr_cmd->qc && !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
4730                                 ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
4731                                 ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
4732                         }
4733                 }
4734         }
4735
4736         res->resetting_device = 1;
4737         scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
4738
4739         if (ipr_is_gata(res) && res->sata_port) {
4740                 ap = res->sata_port->ap;
4741                 spin_unlock_irq(scsi_cmd->device->host->host_lock);
4742                 ata_std_error_handler(ap);
4743                 spin_lock_irq(scsi_cmd->device->host->host_lock);
4744
4745                 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
4746                         if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
4747                                 rc = -EIO;
4748                                 break;
4749                         }
4750                 }
4751         } else
4752                 rc = ipr_device_reset(ioa_cfg, res);
4753         res->resetting_device = 0;
4754
4755         LEAVE;
4756         return (rc ? FAILED : SUCCESS);
4757 }
4758
4759 static int ipr_eh_dev_reset(struct scsi_cmnd * cmd)
4760 {
4761         int rc;
4762
4763         spin_lock_irq(cmd->device->host->host_lock);
4764         rc = __ipr_eh_dev_reset(cmd);
4765         spin_unlock_irq(cmd->device->host->host_lock);
4766
4767         return rc;
4768 }
4769
4770 /**
4771  * ipr_bus_reset_done - Op done function for bus reset.
4772  * @ipr_cmd:    ipr command struct
4773  *
4774  * This function is the op done function for a bus reset
4775  *
4776  * Return value:
4777  *      none
4778  **/
4779 static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
4780 {
4781         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4782         struct ipr_resource_entry *res;
4783
4784         ENTER;
4785         if (!ioa_cfg->sis64)
4786                 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4787                         if (res->res_handle == ipr_cmd->ioarcb.res_handle) {
4788                                 scsi_report_bus_reset(ioa_cfg->host, res->bus);
4789                                 break;
4790                         }
4791                 }
4792
4793         /*
4794          * If abort has not completed, indicate the reset has, else call the
4795          * abort's done function to wake the sleeping eh thread
4796          */
4797         if (ipr_cmd->sibling->sibling)
4798                 ipr_cmd->sibling->sibling = NULL;
4799         else
4800                 ipr_cmd->sibling->done(ipr_cmd->sibling);
4801
4802         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4803         LEAVE;
4804 }
4805
4806 /**
4807  * ipr_abort_timeout - An abort task has timed out
4808  * @ipr_cmd:    ipr command struct
4809  *
4810  * This function handles when an abort task times out. If this
4811  * happens we issue a bus reset since we have resources tied
4812  * up that must be freed before returning to the midlayer.
4813  *
4814  * Return value:
4815  *      none
4816  **/
4817 static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
4818 {
4819         struct ipr_cmnd *reset_cmd;
4820         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4821         struct ipr_cmd_pkt *cmd_pkt;
4822         unsigned long lock_flags = 0;
4823
4824         ENTER;
4825         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4826         if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
4827                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4828                 return;
4829         }
4830
4831         sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
4832         reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4833         ipr_cmd->sibling = reset_cmd;
4834         reset_cmd->sibling = ipr_cmd;
4835         reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
4836         cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
4837         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4838         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
4839         cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
4840
4841         ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
4842         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4843         LEAVE;
4844 }
4845
4846 /**
4847  * ipr_cancel_op - Cancel specified op
4848  * @scsi_cmd:   scsi command struct
4849  *
4850  * This function cancels specified op.
4851  *
4852  * Return value:
4853  *      SUCCESS / FAILED
4854  **/
4855 static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
4856 {
4857         struct ipr_cmnd *ipr_cmd;
4858         struct ipr_ioa_cfg *ioa_cfg;
4859         struct ipr_resource_entry *res;
4860         struct ipr_cmd_pkt *cmd_pkt;
4861         u32 ioasc;
4862         int op_found = 0;
4863
4864         ENTER;
4865         ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
4866         res = scsi_cmd->device->hostdata;
4867
4868         /* If we are currently going through reset/reload, return failed.
4869          * This will force the mid-layer to call ipr_eh_host_reset,
4870          * which will then go to sleep and wait for the reset to complete
4871          */
4872         if (ioa_cfg->in_reset_reload || ioa_cfg->ioa_is_dead)
4873                 return FAILED;
4874         if (!res || !ipr_is_gscsi(res))
4875                 return FAILED;
4876
4877         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
4878                 if (ipr_cmd->scsi_cmd == scsi_cmd) {
4879                         ipr_cmd->done = ipr_scsi_eh_done;
4880                         op_found = 1;
4881                         break;
4882                 }
4883         }
4884
4885         if (!op_found)
4886                 return SUCCESS;
4887
4888         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4889         ipr_cmd->ioarcb.res_handle = res->res_handle;
4890         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4891         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4892         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4893         ipr_cmd->u.sdev = scsi_cmd->device;
4894
4895         scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
4896                     scsi_cmd->cmnd[0]);
4897         ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
4898         ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
4899
4900         /*
4901          * If the abort task timed out and we sent a bus reset, we will get
4902          * one the following responses to the abort
4903          */
4904         if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
4905                 ioasc = 0;
4906                 ipr_trace;
4907         }
4908
4909         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4910         if (!ipr_is_naca_model(res))
4911                 res->needs_sync_complete = 1;
4912
4913         LEAVE;
4914         return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
4915 }
4916
4917 /**
4918  * ipr_eh_abort - Abort a single op
4919  * @scsi_cmd:   scsi command struct
4920  *
4921  * Return value:
4922  *      SUCCESS / FAILED
4923  **/
4924 static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
4925 {
4926         unsigned long flags;
4927         int rc;
4928
4929         ENTER;
4930
4931         spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
4932         rc = ipr_cancel_op(scsi_cmd);
4933         spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
4934
4935         LEAVE;
4936         return rc;
4937 }
4938
4939 /**
4940  * ipr_handle_other_interrupt - Handle "other" interrupts
4941  * @ioa_cfg:    ioa config struct
4942  * @int_reg:    interrupt register
4943  *
4944  * Return value:
4945  *      IRQ_NONE / IRQ_HANDLED
4946  **/
4947 static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
4948                                               volatile u32 int_reg)
4949 {
4950         irqreturn_t rc = IRQ_HANDLED;
4951
4952         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
4953                 /* Mask the interrupt */
4954                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
4955
4956                 /* Clear the interrupt */
4957                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
4958                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4959
4960                 list_del(&ioa_cfg->reset_cmd->queue);
4961                 del_timer(&ioa_cfg->reset_cmd->timer);
4962                 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
4963         } else {
4964                 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
4965                         ioa_cfg->ioa_unit_checked = 1;
4966                 else
4967                         dev_err(&ioa_cfg->pdev->dev,
4968                                 "Permanent IOA failure. 0x%08X\n", int_reg);
4969
4970                 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4971                         ioa_cfg->sdt_state = GET_DUMP;
4972
4973                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
4974                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4975         }
4976
4977         return rc;
4978 }
4979
4980 /**
4981  * ipr_isr_eh - Interrupt service routine error handler
4982  * @ioa_cfg:    ioa config struct
4983  * @msg:        message to log
4984  *
4985  * Return value:
4986  *      none
4987  **/
4988 static void ipr_isr_eh(struct ipr_ioa_cfg *ioa_cfg, char *msg)
4989 {
4990         ioa_cfg->errors_logged++;
4991         dev_err(&ioa_cfg->pdev->dev, "%s\n", msg);
4992
4993         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4994                 ioa_cfg->sdt_state = GET_DUMP;
4995
4996         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4997 }
4998
4999 /**
5000  * ipr_isr - Interrupt service routine
5001  * @irq:        irq number
5002  * @devp:       pointer to ioa config struct
5003  *
5004  * Return value:
5005  *      IRQ_NONE / IRQ_HANDLED
5006  **/
5007 static irqreturn_t ipr_isr(int irq, void *devp)
5008 {
5009         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
5010         unsigned long lock_flags = 0;
5011         volatile u32 int_reg, int_mask_reg;
5012         u32 ioasc;
5013         u16 cmd_index;
5014         int num_hrrq = 0;
5015         struct ipr_cmnd *ipr_cmd;
5016         irqreturn_t rc = IRQ_NONE;
5017
5018         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5019
5020         /* If interrupts are disabled, ignore the interrupt */
5021         if (!ioa_cfg->allow_interrupts) {
5022                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5023                 return IRQ_NONE;
5024         }
5025
5026         int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
5027         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32) & ~int_mask_reg;
5028
5029         /* If an interrupt on the adapter did not occur, ignore it.
5030          * Or in the case of SIS 64, check for a stage change interrupt.
5031          */
5032         if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
5033                 if (ioa_cfg->sis64) {
5034                         int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
5035                         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
5036                         if (int_reg & IPR_PCII_IPL_STAGE_CHANGE) {
5037
5038                                 /* clear stage change */
5039                                 writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.clr_interrupt_reg);
5040                                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
5041                                 list_del(&ioa_cfg->reset_cmd->queue);
5042                                 del_timer(&ioa_cfg->reset_cmd->timer);
5043                                 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
5044                                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5045                                 return IRQ_HANDLED;
5046                         }
5047                 }
5048
5049                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5050                 return IRQ_NONE;
5051         }
5052
5053         while (1) {
5054                 ipr_cmd = NULL;
5055
5056                 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
5057                        ioa_cfg->toggle_bit) {
5058
5059                         cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
5060                                      IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
5061
5062                         if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
5063                                 ipr_isr_eh(ioa_cfg, "Invalid response handle from IOA");
5064                                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5065                                 return IRQ_HANDLED;
5066                         }
5067
5068                         ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
5069
5070                         ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5071
5072                         ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
5073
5074                         list_del(&ipr_cmd->queue);
5075                         del_timer(&ipr_cmd->timer);
5076                         ipr_cmd->done(ipr_cmd);
5077
5078                         rc = IRQ_HANDLED;
5079
5080                         if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
5081                                 ioa_cfg->hrrq_curr++;
5082                         } else {
5083                                 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
5084                                 ioa_cfg->toggle_bit ^= 1u;
5085                         }
5086                 }
5087
5088                 if (ipr_cmd != NULL) {
5089                         /* Clear the PCI interrupt */
5090                         do {
5091                                 writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg32);
5092                                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32) & ~int_mask_reg;
5093                         } while (int_reg & IPR_PCII_HRRQ_UPDATED &&
5094                                         num_hrrq++ < IPR_MAX_HRRQ_RETRIES);
5095
5096                         if (int_reg & IPR_PCII_HRRQ_UPDATED) {
5097                                 ipr_isr_eh(ioa_cfg, "Error clearing HRRQ");
5098                                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5099                                 return IRQ_HANDLED;
5100                         }
5101
5102                 } else
5103                         break;
5104         }
5105
5106         if (unlikely(rc == IRQ_NONE))
5107                 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
5108
5109         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5110         return rc;
5111 }
5112
5113 /**
5114  * ipr_build_ioadl64 - Build a scatter/gather list and map the buffer
5115  * @ioa_cfg:    ioa config struct
5116  * @ipr_cmd:    ipr command struct
5117  *
5118  * Return value:
5119  *      0 on success / -1 on failure
5120  **/
5121 static int ipr_build_ioadl64(struct ipr_ioa_cfg *ioa_cfg,
5122                              struct ipr_cmnd *ipr_cmd)
5123 {
5124         int i, nseg;
5125         struct scatterlist *sg;
5126         u32 length;
5127         u32 ioadl_flags = 0;
5128         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5129         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5130         struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
5131
5132         length = scsi_bufflen(scsi_cmd);
5133         if (!length)
5134                 return 0;
5135
5136         nseg = scsi_dma_map(scsi_cmd);
5137         if (nseg < 0) {
5138                 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
5139                 return -1;
5140         }
5141
5142         ipr_cmd->dma_use_sg = nseg;
5143
5144         ioarcb->data_transfer_length = cpu_to_be32(length);
5145         ioarcb->ioadl_len =
5146                 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
5147
5148         if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
5149                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5150                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5151         } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE)
5152                 ioadl_flags = IPR_IOADL_FLAGS_READ;
5153
5154         scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
5155                 ioadl64[i].flags = cpu_to_be32(ioadl_flags);
5156                 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(sg));
5157                 ioadl64[i].address = cpu_to_be64(sg_dma_address(sg));
5158         }
5159
5160         ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5161         return 0;
5162 }
5163
5164 /**
5165  * ipr_build_ioadl - Build a scatter/gather list and map the buffer
5166  * @ioa_cfg:    ioa config struct
5167  * @ipr_cmd:    ipr command struct
5168  *
5169  * Return value:
5170  *      0 on success / -1 on failure
5171  **/
5172 static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
5173                            struct ipr_cmnd *ipr_cmd)
5174 {
5175         int i, nseg;
5176         struct scatterlist *sg;
5177         u32 length;
5178         u32 ioadl_flags = 0;
5179         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5180         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5181         struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
5182
5183         length = scsi_bufflen(scsi_cmd);
5184         if (!length)
5185                 return 0;
5186
5187         nseg = scsi_dma_map(scsi_cmd);
5188         if (nseg < 0) {
5189                 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
5190                 return -1;
5191         }
5192
5193         ipr_cmd->dma_use_sg = nseg;
5194
5195         if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
5196                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5197                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5198                 ioarcb->data_transfer_length = cpu_to_be32(length);
5199                 ioarcb->ioadl_len =
5200                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5201         } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
5202                 ioadl_flags = IPR_IOADL_FLAGS_READ;
5203                 ioarcb->read_data_transfer_length = cpu_to_be32(length);
5204                 ioarcb->read_ioadl_len =
5205                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5206         }
5207
5208         if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->u.add_data.u.ioadl)) {
5209                 ioadl = ioarcb->u.add_data.u.ioadl;
5210                 ioarcb->write_ioadl_addr = cpu_to_be32((ipr_cmd->dma_addr) +
5211                                     offsetof(struct ipr_ioarcb, u.add_data));
5212                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5213         }
5214
5215         scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
5216                 ioadl[i].flags_and_data_len =
5217                         cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5218                 ioadl[i].address = cpu_to_be32(sg_dma_address(sg));
5219         }
5220
5221         ioadl[i-1].flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5222         return 0;
5223 }
5224
5225 /**
5226  * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
5227  * @scsi_cmd:   scsi command struct
5228  *
5229  * Return value:
5230  *      task attributes
5231  **/
5232 static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
5233 {
5234         u8 tag[2];
5235         u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
5236
5237         if (scsi_populate_tag_msg(scsi_cmd, tag)) {
5238                 switch (tag[0]) {
5239                 case MSG_SIMPLE_TAG:
5240                         rc = IPR_FLAGS_LO_SIMPLE_TASK;
5241                         break;
5242                 case MSG_HEAD_TAG:
5243                         rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
5244                         break;
5245                 case MSG_ORDERED_TAG:
5246                         rc = IPR_FLAGS_LO_ORDERED_TASK;
5247                         break;
5248                 };
5249         }
5250
5251         return rc;
5252 }
5253
5254 /**
5255  * ipr_erp_done - Process completion of ERP for a device
5256  * @ipr_cmd:            ipr command struct
5257  *
5258  * This function copies the sense buffer into the scsi_cmd
5259  * struct and pushes the scsi_done function.
5260  *
5261  * Return value:
5262  *      nothing
5263  **/
5264 static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
5265 {
5266         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5267         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5268         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5269         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5270
5271         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
5272                 scsi_cmd->result |= (DID_ERROR << 16);
5273                 scmd_printk(KERN_ERR, scsi_cmd,
5274                             "Request Sense failed with IOASC: 0x%08X\n", ioasc);
5275         } else {
5276                 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
5277                        SCSI_SENSE_BUFFERSIZE);
5278         }
5279
5280         if (res) {
5281                 if (!ipr_is_naca_model(res))
5282                         res->needs_sync_complete = 1;
5283                 res->in_erp = 0;
5284         }
5285         scsi_dma_unmap(ipr_cmd->scsi_cmd);
5286         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5287         scsi_cmd->scsi_done(scsi_cmd);
5288 }
5289
5290 /**
5291  * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
5292  * @ipr_cmd:    ipr command struct
5293  *
5294  * Return value:
5295  *      none
5296  **/
5297 static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
5298 {
5299         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5300         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5301         dma_addr_t dma_addr = ipr_cmd->dma_addr;
5302
5303         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
5304         ioarcb->data_transfer_length = 0;
5305         ioarcb->read_data_transfer_length = 0;
5306         ioarcb->ioadl_len = 0;
5307         ioarcb->read_ioadl_len = 0;
5308         ioasa->hdr.ioasc = 0;
5309         ioasa->hdr.residual_data_len = 0;
5310
5311         if (ipr_cmd->ioa_cfg->sis64)
5312                 ioarcb->u.sis64_addr_data.data_ioadl_addr =
5313                         cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
5314         else {
5315                 ioarcb->write_ioadl_addr =
5316                         cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
5317                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5318         }
5319 }
5320
5321 /**
5322  * ipr_erp_request_sense - Send request sense to a device
5323  * @ipr_cmd:    ipr command struct
5324  *
5325  * This function sends a request sense to a device as a result
5326  * of a check condition.
5327  *
5328  * Return value:
5329  *      nothing
5330  **/
5331 static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
5332 {
5333         struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5334         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5335
5336         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
5337                 ipr_erp_done(ipr_cmd);
5338                 return;
5339         }
5340
5341         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
5342
5343         cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
5344         cmd_pkt->cdb[0] = REQUEST_SENSE;
5345         cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
5346         cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
5347         cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5348         cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
5349
5350         ipr_init_ioadl(ipr_cmd, ipr_cmd->sense_buffer_dma,
5351                        SCSI_SENSE_BUFFERSIZE, IPR_IOADL_FLAGS_READ_LAST);
5352
5353         ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
5354                    IPR_REQUEST_SENSE_TIMEOUT * 2);
5355 }
5356
5357 /**
5358  * ipr_erp_cancel_all - Send cancel all to a device
5359  * @ipr_cmd:    ipr command struct
5360  *
5361  * This function sends a cancel all to a device to clear the
5362  * queue. If we are running TCQ on the device, QERR is set to 1,
5363  * which means all outstanding ops have been dropped on the floor.
5364  * Cancel all will return them to us.
5365  *
5366  * Return value:
5367  *      nothing
5368  **/
5369 static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
5370 {
5371         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5372         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5373         struct ipr_cmd_pkt *cmd_pkt;
5374
5375         res->in_erp = 1;
5376
5377         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
5378
5379         if (!scsi_get_tag_type(scsi_cmd->device)) {
5380                 ipr_erp_request_sense(ipr_cmd);
5381                 return;
5382         }
5383
5384         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5385         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5386         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
5387
5388         ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
5389                    IPR_CANCEL_ALL_TIMEOUT);
5390 }
5391
5392 /**
5393  * ipr_dump_ioasa - Dump contents of IOASA
5394  * @ioa_cfg:    ioa config struct
5395  * @ipr_cmd:    ipr command struct
5396  * @res:                resource entry struct
5397  *
5398  * This function is invoked by the interrupt handler when ops
5399  * fail. It will log the IOASA if appropriate. Only called
5400  * for GPDD ops.
5401  *
5402  * Return value:
5403  *      none
5404  **/
5405 static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
5406                            struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
5407 {
5408         int i;
5409         u16 data_len;
5410         u32 ioasc, fd_ioasc;
5411         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5412         __be32 *ioasa_data = (__be32 *)ioasa;
5413         int error_index;
5414
5415         ioasc = be32_to_cpu(ioasa->hdr.ioasc) & IPR_IOASC_IOASC_MASK;
5416         fd_ioasc = be32_to_cpu(ioasa->hdr.fd_ioasc) & IPR_IOASC_IOASC_MASK;
5417
5418         if (0 == ioasc)
5419                 return;
5420
5421         if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
5422                 return;
5423
5424         if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
5425                 error_index = ipr_get_error(fd_ioasc);
5426         else
5427                 error_index = ipr_get_error(ioasc);
5428
5429         if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
5430                 /* Don't log an error if the IOA already logged one */
5431                 if (ioasa->hdr.ilid != 0)
5432                         return;
5433
5434                 if (!ipr_is_gscsi(res))
5435                         return;
5436
5437                 if (ipr_error_table[error_index].log_ioasa == 0)
5438                         return;
5439         }
5440
5441         ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
5442
5443         data_len = be16_to_cpu(ioasa->hdr.ret_stat_len);
5444         if (ioa_cfg->sis64 && sizeof(struct ipr_ioasa64) < data_len)
5445                 data_len = sizeof(struct ipr_ioasa64);
5446         else if (!ioa_cfg->sis64 && sizeof(struct ipr_ioasa) < data_len)
5447                 data_len = sizeof(struct ipr_ioasa);
5448
5449         ipr_err("IOASA Dump:\n");
5450
5451         for (i = 0; i < data_len / 4; i += 4) {
5452                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
5453                         be32_to_cpu(ioasa_data[i]),
5454                         be32_to_cpu(ioasa_data[i+1]),
5455                         be32_to_cpu(ioasa_data[i+2]),
5456                         be32_to_cpu(ioasa_data[i+3]));
5457         }
5458 }
5459
5460 /**
5461  * ipr_gen_sense - Generate SCSI sense data from an IOASA
5462  * @ioasa:              IOASA
5463  * @sense_buf:  sense data buffer
5464  *
5465  * Return value:
5466  *      none
5467  **/
5468 static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
5469 {
5470         u32 failing_lba;
5471         u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
5472         struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
5473         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5474         u32 ioasc = be32_to_cpu(ioasa->hdr.ioasc);
5475
5476         memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
5477
5478         if (ioasc >= IPR_FIRST_DRIVER_IOASC)
5479                 return;
5480
5481         ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
5482
5483         if (ipr_is_vset_device(res) &&
5484             ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
5485             ioasa->u.vset.failing_lba_hi != 0) {
5486                 sense_buf[0] = 0x72;
5487                 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
5488                 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
5489                 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
5490
5491                 sense_buf[7] = 12;
5492                 sense_buf[8] = 0;
5493                 sense_buf[9] = 0x0A;
5494                 sense_buf[10] = 0x80;
5495
5496                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
5497
5498                 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
5499                 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
5500                 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
5501                 sense_buf[15] = failing_lba & 0x000000ff;
5502
5503                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
5504
5505                 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
5506                 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
5507                 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
5508                 sense_buf[19] = failing_lba & 0x000000ff;
5509         } else {
5510                 sense_buf[0] = 0x70;
5511                 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
5512                 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
5513                 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
5514
5515                 /* Illegal request */
5516                 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
5517                     (be32_to_cpu(ioasa->hdr.ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
5518                         sense_buf[7] = 10;      /* additional length */
5519
5520                         /* IOARCB was in error */
5521                         if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
5522                                 sense_buf[15] = 0xC0;
5523                         else    /* Parameter data was invalid */
5524                                 sense_buf[15] = 0x80;
5525
5526                         sense_buf[16] =
5527                             ((IPR_FIELD_POINTER_MASK &
5528                               be32_to_cpu(ioasa->hdr.ioasc_specific)) >> 8) & 0xff;
5529                         sense_buf[17] =
5530                             (IPR_FIELD_POINTER_MASK &
5531                              be32_to_cpu(ioasa->hdr.ioasc_specific)) & 0xff;
5532                 } else {
5533                         if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
5534                                 if (ipr_is_vset_device(res))
5535                                         failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
5536                                 else
5537                                         failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
5538
5539                                 sense_buf[0] |= 0x80;   /* Or in the Valid bit */
5540                                 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
5541                                 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
5542                                 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
5543                                 sense_buf[6] = failing_lba & 0x000000ff;
5544                         }
5545
5546                         sense_buf[7] = 6;       /* additional length */
5547                 }
5548         }
5549 }
5550
5551 /**
5552  * ipr_get_autosense - Copy autosense data to sense buffer
5553  * @ipr_cmd:    ipr command struct
5554  *
5555  * This function copies the autosense buffer to the buffer
5556  * in the scsi_cmd, if there is autosense available.
5557  *
5558  * Return value:
5559  *      1 if autosense was available / 0 if not
5560  **/
5561 static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
5562 {
5563         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5564         struct ipr_ioasa64 *ioasa64 = &ipr_cmd->s.ioasa64;
5565
5566         if ((be32_to_cpu(ioasa->hdr.ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
5567                 return 0;
5568
5569         if (ipr_cmd->ioa_cfg->sis64)
5570                 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa64->auto_sense.data,
5571                        min_t(u16, be16_to_cpu(ioasa64->auto_sense.auto_sense_len),
5572                            SCSI_SENSE_BUFFERSIZE));
5573         else
5574                 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
5575                        min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
5576                            SCSI_SENSE_BUFFERSIZE));
5577         return 1;
5578 }
5579
5580 /**
5581  * ipr_erp_start - Process an error response for a SCSI op
5582  * @ioa_cfg:    ioa config struct
5583  * @ipr_cmd:    ipr command struct
5584  *
5585  * This function determines whether or not to initiate ERP
5586  * on the affected device.
5587  *
5588  * Return value:
5589  *      nothing
5590  **/
5591 static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
5592                               struct ipr_cmnd *ipr_cmd)
5593 {
5594         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5595         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5596         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5597         u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
5598
5599         if (!res) {
5600                 ipr_scsi_eh_done(ipr_cmd);
5601                 return;
5602         }
5603
5604         if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
5605                 ipr_gen_sense(ipr_cmd);
5606
5607         ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
5608
5609         switch (masked_ioasc) {
5610         case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
5611                 if (ipr_is_naca_model(res))
5612                         scsi_cmd->result |= (DID_ABORT << 16);
5613                 else
5614                         scsi_cmd->result |= (DID_IMM_RETRY << 16);
5615                 break;
5616         case IPR_IOASC_IR_RESOURCE_HANDLE:
5617         case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
5618                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
5619                 break;
5620         case IPR_IOASC_HW_SEL_TIMEOUT:
5621                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
5622                 if (!ipr_is_naca_model(res))
5623                         res->needs_sync_complete = 1;
5624                 break;
5625         case IPR_IOASC_SYNC_REQUIRED:
5626                 if (!res->in_erp)
5627                         res->needs_sync_complete = 1;
5628                 scsi_cmd->result |= (DID_IMM_RETRY << 16);
5629                 break;
5630         case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
5631         case IPR_IOASA_IR_DUAL_IOA_DISABLED:
5632                 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
5633                 break;
5634         case IPR_IOASC_BUS_WAS_RESET:
5635         case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
5636                 /*
5637                  * Report the bus reset and ask for a retry. The device
5638                  * will give CC/UA the next command.
5639                  */
5640                 if (!res->resetting_device)
5641                         scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
5642                 scsi_cmd->result |= (DID_ERROR << 16);
5643                 if (!ipr_is_naca_model(res))
5644                         res->needs_sync_complete = 1;
5645                 break;
5646         case IPR_IOASC_HW_DEV_BUS_STATUS:
5647                 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
5648                 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
5649                         if (!ipr_get_autosense(ipr_cmd)) {
5650                                 if (!ipr_is_naca_model(res)) {
5651                                         ipr_erp_cancel_all(ipr_cmd);
5652                                         return;
5653                                 }
5654                         }
5655                 }
5656                 if (!ipr_is_naca_model(res))
5657                         res->needs_sync_complete = 1;
5658                 break;
5659         case IPR_IOASC_NR_INIT_CMD_REQUIRED:
5660                 break;
5661         default:
5662                 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
5663                         scsi_cmd->result |= (DID_ERROR << 16);
5664                 if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
5665                         res->needs_sync_complete = 1;
5666                 break;
5667         }
5668
5669         scsi_dma_unmap(ipr_cmd->scsi_cmd);
5670         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5671         scsi_cmd->scsi_done(scsi_cmd);
5672 }
5673
5674 /**
5675  * ipr_scsi_done - mid-layer done function
5676  * @ipr_cmd:    ipr command struct
5677  *
5678  * This function is invoked by the interrupt handler for
5679  * ops generated by the SCSI mid-layer
5680  *
5681  * Return value:
5682  *      none
5683  **/
5684 static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
5685 {
5686         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5687         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5688         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5689
5690         scsi_set_resid(scsi_cmd, be32_to_cpu(ipr_cmd->s.ioasa.hdr.residual_data_len));
5691
5692         if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
5693                 scsi_dma_unmap(ipr_cmd->scsi_cmd);
5694                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5695                 scsi_cmd->scsi_done(scsi_cmd);
5696         } else
5697                 ipr_erp_start(ioa_cfg, ipr_cmd);
5698 }
5699
5700 /**
5701  * ipr_queuecommand - Queue a mid-layer request
5702  * @scsi_cmd:   scsi command struct
5703  * @done:               done function
5704  *
5705  * This function queues a request generated by the mid-layer.
5706  *
5707  * Return value:
5708  *      0 on success
5709  *      SCSI_MLQUEUE_DEVICE_BUSY if device is busy
5710  *      SCSI_MLQUEUE_HOST_BUSY if host is busy
5711  **/
5712 static int ipr_queuecommand_lck(struct scsi_cmnd *scsi_cmd,
5713                             void (*done) (struct scsi_cmnd *))
5714 {
5715         struct ipr_ioa_cfg *ioa_cfg;
5716         struct ipr_resource_entry *res;
5717         struct ipr_ioarcb *ioarcb;
5718         struct ipr_cmnd *ipr_cmd;
5719         int rc = 0;
5720
5721         scsi_cmd->scsi_done = done;
5722         ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
5723         res = scsi_cmd->device->hostdata;
5724         scsi_cmd->result = (DID_OK << 16);
5725
5726         /*
5727          * We are currently blocking all devices due to a host reset
5728          * We have told the host to stop giving us new requests, but
5729          * ERP ops don't count. FIXME
5730          */
5731         if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
5732                 return SCSI_MLQUEUE_HOST_BUSY;
5733
5734         /*
5735          * FIXME - Create scsi_set_host_offline interface
5736          *  and the ioa_is_dead check can be removed
5737          */
5738         if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
5739                 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
5740                 scsi_cmd->result = (DID_NO_CONNECT << 16);
5741                 scsi_cmd->scsi_done(scsi_cmd);
5742                 return 0;
5743         }
5744
5745         if (ipr_is_gata(res) && res->sata_port)
5746                 return ata_sas_queuecmd(scsi_cmd, done, res->sata_port->ap);
5747
5748         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5749         ioarcb = &ipr_cmd->ioarcb;
5750         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
5751
5752         memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
5753         ipr_cmd->scsi_cmd = scsi_cmd;
5754         ioarcb->res_handle = res->res_handle;
5755         ipr_cmd->done = ipr_scsi_done;
5756         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
5757
5758         if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
5759                 if (scsi_cmd->underflow == 0)
5760                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5761
5762                 if (res->needs_sync_complete) {
5763                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
5764                         res->needs_sync_complete = 0;
5765                 }
5766
5767                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
5768                 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
5769                 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
5770                 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
5771         }
5772
5773         if (scsi_cmd->cmnd[0] >= 0xC0 &&
5774             (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
5775                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5776
5777         if (likely(rc == 0)) {
5778                 if (ioa_cfg->sis64)
5779                         rc = ipr_build_ioadl64(ioa_cfg, ipr_cmd);
5780                 else
5781                         rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
5782         }
5783
5784         if (likely(rc == 0)) {
5785                 mb();
5786                 ipr_send_command(ipr_cmd);
5787         } else {
5788                  list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5789                  return SCSI_MLQUEUE_HOST_BUSY;
5790         }
5791
5792         return 0;
5793 }
5794
5795 static DEF_SCSI_QCMD(ipr_queuecommand)
5796
5797 /**
5798  * ipr_ioctl - IOCTL handler
5799  * @sdev:       scsi device struct
5800  * @cmd:        IOCTL cmd
5801  * @arg:        IOCTL arg
5802  *
5803  * Return value:
5804  *      0 on success / other on failure
5805  **/
5806 static int ipr_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
5807 {
5808         struct ipr_resource_entry *res;
5809
5810         res = (struct ipr_resource_entry *)sdev->hostdata;
5811         if (res && ipr_is_gata(res)) {
5812                 if (cmd == HDIO_GET_IDENTITY)
5813                         return -ENOTTY;
5814                 return ata_sas_scsi_ioctl(res->sata_port->ap, sdev, cmd, arg);
5815         }
5816
5817         return -EINVAL;
5818 }
5819
5820 /**
5821  * ipr_info - Get information about the card/driver
5822  * @scsi_host:  scsi host struct
5823  *
5824  * Return value:
5825  *      pointer to buffer with description string
5826  **/
5827 static const char * ipr_ioa_info(struct Scsi_Host *host)
5828 {
5829         static char buffer[512];
5830         struct ipr_ioa_cfg *ioa_cfg;
5831         unsigned long lock_flags = 0;
5832
5833         ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
5834
5835         spin_lock_irqsave(host->host_lock, lock_flags);
5836         sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
5837         spin_unlock_irqrestore(host->host_lock, lock_flags);
5838
5839         return buffer;
5840 }
5841
5842 static struct scsi_host_template driver_template = {
5843         .module = THIS_MODULE,
5844         .name = "IPR",
5845         .info = ipr_ioa_info,
5846         .ioctl = ipr_ioctl,
5847         .queuecommand = ipr_queuecommand,
5848         .eh_abort_handler = ipr_eh_abort,
5849         .eh_device_reset_handler = ipr_eh_dev_reset,
5850         .eh_host_reset_handler = ipr_eh_host_reset,
5851         .slave_alloc = ipr_slave_alloc,
5852         .slave_configure = ipr_slave_configure,
5853         .slave_destroy = ipr_slave_destroy,
5854         .target_alloc = ipr_target_alloc,
5855         .target_destroy = ipr_target_destroy,
5856         .change_queue_depth = ipr_change_queue_depth,
5857         .change_queue_type = ipr_change_queue_type,
5858         .bios_param = ipr_biosparam,
5859         .can_queue = IPR_MAX_COMMANDS,
5860         .this_id = -1,
5861         .sg_tablesize = IPR_MAX_SGLIST,
5862         .max_sectors = IPR_IOA_MAX_SECTORS,
5863         .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
5864         .use_clustering = ENABLE_CLUSTERING,
5865         .shost_attrs = ipr_ioa_attrs,
5866         .sdev_attrs = ipr_dev_attrs,
5867         .proc_name = IPR_NAME
5868 };
5869
5870 /**
5871  * ipr_ata_phy_reset - libata phy_reset handler
5872  * @ap:         ata port to reset
5873  *
5874  **/
5875 static void ipr_ata_phy_reset(struct ata_port *ap)
5876 {
5877         unsigned long flags;
5878         struct ipr_sata_port *sata_port = ap->private_data;
5879         struct ipr_resource_entry *res = sata_port->res;
5880         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5881         int rc;
5882
5883         ENTER;
5884         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5885         while(ioa_cfg->in_reset_reload) {
5886                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5887                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5888                 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5889         }
5890
5891         if (!ioa_cfg->allow_cmds)
5892                 goto out_unlock;
5893
5894         rc = ipr_device_reset(ioa_cfg, res);
5895
5896         if (rc) {
5897                 ap->link.device[0].class = ATA_DEV_NONE;
5898                 goto out_unlock;
5899         }
5900
5901         ap->link.device[0].class = res->ata_class;
5902         if (ap->link.device[0].class == ATA_DEV_UNKNOWN)
5903                 ap->link.device[0].class = ATA_DEV_NONE;
5904
5905 out_unlock:
5906         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5907         LEAVE;
5908 }
5909
5910 /**
5911  * ipr_ata_post_internal - Cleanup after an internal command
5912  * @qc: ATA queued command
5913  *
5914  * Return value:
5915  *      none
5916  **/
5917 static void ipr_ata_post_internal(struct ata_queued_cmd *qc)
5918 {
5919         struct ipr_sata_port *sata_port = qc->ap->private_data;
5920         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5921         struct ipr_cmnd *ipr_cmd;
5922         unsigned long flags;
5923
5924         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5925         while(ioa_cfg->in_reset_reload) {
5926                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5927                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5928                 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5929         }
5930
5931         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
5932                 if (ipr_cmd->qc == qc) {
5933                         ipr_device_reset(ioa_cfg, sata_port->res);
5934                         break;
5935                 }
5936         }
5937         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5938 }
5939
5940 /**
5941  * ipr_copy_sata_tf - Copy a SATA taskfile to an IOA data structure
5942  * @regs:       destination
5943  * @tf: source ATA taskfile
5944  *
5945  * Return value:
5946  *      none
5947  **/
5948 static void ipr_copy_sata_tf(struct ipr_ioarcb_ata_regs *regs,
5949                              struct ata_taskfile *tf)
5950 {
5951         regs->feature = tf->feature;
5952         regs->nsect = tf->nsect;
5953         regs->lbal = tf->lbal;
5954         regs->lbam = tf->lbam;
5955         regs->lbah = tf->lbah;
5956         regs->device = tf->device;
5957         regs->command = tf->command;
5958         regs->hob_feature = tf->hob_feature;
5959         regs->hob_nsect = tf->hob_nsect;
5960         regs->hob_lbal = tf->hob_lbal;
5961         regs->hob_lbam = tf->hob_lbam;
5962         regs->hob_lbah = tf->hob_lbah;
5963         regs->ctl = tf->ctl;
5964 }
5965
5966 /**
5967  * ipr_sata_done - done function for SATA commands
5968  * @ipr_cmd:    ipr command struct
5969  *
5970  * This function is invoked by the interrupt handler for
5971  * ops generated by the SCSI mid-layer to SATA devices
5972  *
5973  * Return value:
5974  *      none
5975  **/
5976 static void ipr_sata_done(struct ipr_cmnd *ipr_cmd)
5977 {
5978         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5979         struct ata_queued_cmd *qc = ipr_cmd->qc;
5980         struct ipr_sata_port *sata_port = qc->ap->private_data;
5981         struct ipr_resource_entry *res = sata_port->res;
5982         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5983
5984         if (ipr_cmd->ioa_cfg->sis64)
5985                 memcpy(&sata_port->ioasa, &ipr_cmd->s.ioasa64.u.gata,
5986                        sizeof(struct ipr_ioasa_gata));
5987         else
5988                 memcpy(&sata_port->ioasa, &ipr_cmd->s.ioasa.u.gata,
5989                        sizeof(struct ipr_ioasa_gata));
5990         ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
5991
5992         if (be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc_specific) & IPR_ATA_DEVICE_WAS_RESET)
5993                 scsi_report_device_reset(ioa_cfg->host, res->bus, res->target);
5994
5995         if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
5996                 qc->err_mask |= __ac_err_mask(sata_port->ioasa.status);
5997         else
5998                 qc->err_mask |= ac_err_mask(sata_port->ioasa.status);
5999         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6000         ata_qc_complete(qc);
6001 }
6002
6003 /**
6004  * ipr_build_ata_ioadl64 - Build an ATA scatter/gather list
6005  * @ipr_cmd:    ipr command struct
6006  * @qc:         ATA queued command
6007  *
6008  **/
6009 static void ipr_build_ata_ioadl64(struct ipr_cmnd *ipr_cmd,
6010                                   struct ata_queued_cmd *qc)
6011 {
6012         u32 ioadl_flags = 0;
6013         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6014         struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
6015         struct ipr_ioadl64_desc *last_ioadl64 = NULL;
6016         int len = qc->nbytes;
6017         struct scatterlist *sg;
6018         unsigned int si;
6019         dma_addr_t dma_addr = ipr_cmd->dma_addr;
6020
6021         if (len == 0)
6022                 return;
6023
6024         if (qc->dma_dir == DMA_TO_DEVICE) {
6025                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
6026                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6027         } else if (qc->dma_dir == DMA_FROM_DEVICE)
6028                 ioadl_flags = IPR_IOADL_FLAGS_READ;
6029
6030         ioarcb->data_transfer_length = cpu_to_be32(len);
6031         ioarcb->ioadl_len =
6032                 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
6033         ioarcb->u.sis64_addr_data.data_ioadl_addr =
6034                 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ata_ioadl));
6035
6036         for_each_sg(qc->sg, sg, qc->n_elem, si) {
6037                 ioadl64->flags = cpu_to_be32(ioadl_flags);
6038                 ioadl64->data_len = cpu_to_be32(sg_dma_len(sg));
6039                 ioadl64->address = cpu_to_be64(sg_dma_address(sg));
6040
6041                 last_ioadl64 = ioadl64;
6042                 ioadl64++;
6043         }
6044
6045         if (likely(last_ioadl64))
6046                 last_ioadl64->flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
6047 }
6048
6049 /**
6050  * ipr_build_ata_ioadl - Build an ATA scatter/gather list
6051  * @ipr_cmd:    ipr command struct
6052  * @qc:         ATA queued command
6053  *
6054  **/
6055 static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
6056                                 struct ata_queued_cmd *qc)
6057 {
6058         u32 ioadl_flags = 0;
6059         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6060         struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
6061         struct ipr_ioadl_desc *last_ioadl = NULL;
6062         int len = qc->nbytes;
6063         struct scatterlist *sg;
6064         unsigned int si;
6065
6066         if (len == 0)
6067                 return;
6068
6069         if (qc->dma_dir == DMA_TO_DEVICE) {
6070                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
6071                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6072                 ioarcb->data_transfer_length = cpu_to_be32(len);
6073                 ioarcb->ioadl_len =
6074                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
6075         } else if (qc->dma_dir == DMA_FROM_DEVICE) {
6076                 ioadl_flags = IPR_IOADL_FLAGS_READ;
6077                 ioarcb->read_data_transfer_length = cpu_to_be32(len);
6078                 ioarcb->read_ioadl_len =
6079                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
6080         }
6081
6082         for_each_sg(qc->sg, sg, qc->n_elem, si) {
6083                 ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
6084                 ioadl->address = cpu_to_be32(sg_dma_address(sg));
6085
6086                 last_ioadl = ioadl;
6087                 ioadl++;
6088         }
6089
6090         if (likely(last_ioadl))
6091                 last_ioadl->flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
6092 }
6093
6094 /**
6095  * ipr_qc_issue - Issue a SATA qc to a device
6096  * @qc: queued command
6097  *
6098  * Return value:
6099  *      0 if success
6100  **/
6101 static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
6102 {
6103         struct ata_port *ap = qc->ap;
6104         struct ipr_sata_port *sata_port = ap->private_data;
6105         struct ipr_resource_entry *res = sata_port->res;
6106         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
6107         struct ipr_cmnd *ipr_cmd;
6108         struct ipr_ioarcb *ioarcb;
6109         struct ipr_ioarcb_ata_regs *regs;
6110
6111         if (unlikely(!ioa_cfg->allow_cmds || ioa_cfg->ioa_is_dead))
6112                 return AC_ERR_SYSTEM;
6113
6114         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
6115         ioarcb = &ipr_cmd->ioarcb;
6116
6117         if (ioa_cfg->sis64) {
6118                 regs = &ipr_cmd->i.ata_ioadl.regs;
6119                 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
6120         } else
6121                 regs = &ioarcb->u.add_data.u.regs;
6122
6123         memset(regs, 0, sizeof(*regs));
6124         ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(*regs));
6125
6126         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
6127         ipr_cmd->qc = qc;
6128         ipr_cmd->done = ipr_sata_done;
6129         ipr_cmd->ioarcb.res_handle = res->res_handle;
6130         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_ATA_PASSTHRU;
6131         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
6132         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
6133         ipr_cmd->dma_use_sg = qc->n_elem;
6134
6135         if (ioa_cfg->sis64)
6136                 ipr_build_ata_ioadl64(ipr_cmd, qc);
6137         else
6138                 ipr_build_ata_ioadl(ipr_cmd, qc);
6139
6140         regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
6141         ipr_copy_sata_tf(regs, &qc->tf);
6142         memcpy(ioarcb->cmd_pkt.cdb, qc->cdb, IPR_MAX_CDB_LEN);
6143         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
6144
6145         switch (qc->tf.protocol) {
6146         case ATA_PROT_NODATA:
6147         case ATA_PROT_PIO:
6148                 break;
6149
6150         case ATA_PROT_DMA:
6151                 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
6152                 break;
6153
6154         case ATAPI_PROT_PIO:
6155         case ATAPI_PROT_NODATA:
6156                 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
6157                 break;
6158
6159         case ATAPI_PROT_DMA:
6160                 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
6161                 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
6162                 break;
6163
6164         default:
6165                 WARN_ON(1);
6166                 return AC_ERR_INVALID;
6167         }
6168
6169         mb();
6170
6171         ipr_send_command(ipr_cmd);
6172
6173         return 0;
6174 }
6175
6176 /**
6177  * ipr_qc_fill_rtf - Read result TF
6178  * @qc: ATA queued command
6179  *
6180  * Return value:
6181  *      true
6182  **/
6183 static bool ipr_qc_fill_rtf(struct ata_queued_cmd *qc)
6184 {
6185         struct ipr_sata_port *sata_port = qc->ap->private_data;
6186         struct ipr_ioasa_gata *g = &sata_port->ioasa;
6187         struct ata_taskfile *tf = &qc->result_tf;
6188
6189         tf->feature = g->error;
6190         tf->nsect = g->nsect;
6191         tf->lbal = g->lbal;
6192         tf->lbam = g->lbam;
6193         tf->lbah = g->lbah;
6194         tf->device = g->device;
6195         tf->command = g->status;
6196         tf->hob_nsect = g->hob_nsect;
6197         tf->hob_lbal = g->hob_lbal;
6198         tf->hob_lbam = g->hob_lbam;
6199         tf->hob_lbah = g->hob_lbah;
6200         tf->ctl = g->alt_status;
6201
6202         return true;
6203 }
6204
6205 static struct ata_port_operations ipr_sata_ops = {
6206         .phy_reset = ipr_ata_phy_reset,
6207         .hardreset = ipr_sata_reset,
6208         .post_internal_cmd = ipr_ata_post_internal,
6209         .qc_prep = ata_noop_qc_prep,
6210         .qc_issue = ipr_qc_issue,
6211         .qc_fill_rtf = ipr_qc_fill_rtf,
6212         .port_start = ata_sas_port_start,
6213         .port_stop = ata_sas_port_stop
6214 };
6215
6216 static struct ata_port_info sata_port_info = {
6217         .flags  = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
6218         ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
6219         .pio_mask       = 0x10, /* pio4 */
6220         .mwdma_mask = 0x07,
6221         .udma_mask      = 0x7f, /* udma0-6 */
6222         .port_ops       = &ipr_sata_ops
6223 };
6224
6225 #ifdef CONFIG_PPC_PSERIES
6226 static const u16 ipr_blocked_processors[] = {
6227         PV_NORTHSTAR,
6228         PV_PULSAR,
6229         PV_POWER4,
6230         PV_ICESTAR,
6231         PV_SSTAR,
6232         PV_POWER4p,
6233         PV_630,
6234         PV_630p
6235 };
6236
6237 /**
6238  * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
6239  * @ioa_cfg:    ioa cfg struct
6240  *
6241  * Adapters that use Gemstone revision < 3.1 do not work reliably on
6242  * certain pSeries hardware. This function determines if the given
6243  * adapter is in one of these confgurations or not.
6244  *
6245  * Return value:
6246  *      1 if adapter is not supported / 0 if adapter is supported
6247  **/
6248 static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
6249 {
6250         int i;
6251
6252         if ((ioa_cfg->type == 0x5702) && (ioa_cfg->pdev->revision < 4)) {
6253                 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
6254                         if (__is_processor(ipr_blocked_processors[i]))
6255                                 return 1;
6256                 }
6257         }
6258         return 0;
6259 }
6260 #else
6261 #define ipr_invalid_adapter(ioa_cfg) 0
6262 #endif
6263
6264 /**
6265  * ipr_ioa_bringdown_done - IOA bring down completion.
6266  * @ipr_cmd:    ipr command struct
6267  *
6268  * This function processes the completion of an adapter bring down.
6269  * It wakes any reset sleepers.
6270  *
6271  * Return value:
6272  *      IPR_RC_JOB_RETURN
6273  **/
6274 static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
6275 {
6276         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6277
6278         ENTER;
6279         ioa_cfg->in_reset_reload = 0;
6280         ioa_cfg->reset_retries = 0;
6281         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6282         wake_up_all(&ioa_cfg->reset_wait_q);
6283
6284         spin_unlock_irq(ioa_cfg->host->host_lock);
6285         scsi_unblock_requests(ioa_cfg->host);
6286         spin_lock_irq(ioa_cfg->host->host_lock);
6287         LEAVE;
6288
6289         return IPR_RC_JOB_RETURN;
6290 }
6291
6292 /**
6293  * ipr_ioa_reset_done - IOA reset completion.
6294  * @ipr_cmd:    ipr&nbs