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