[S390] fill out file list in s390 MAINTAINERS entry
[safe/jmp/linux-2.6] / drivers / scsi / ipr.c
1 /*
2  * ipr.c -- driver for IBM Power Linux RAID adapters
3  *
4  * Written By: Brian King <brking@us.ibm.com>, IBM Corporation
5  *
6  * Copyright (C) 2003, 2004 IBM Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 /*
25  * Notes:
26  *
27  * This driver is used to control the following SCSI adapters:
28  *
29  * IBM iSeries: 5702, 5703, 2780, 5709, 570A, 570B
30  *
31  * IBM pSeries: PCI-X Dual Channel Ultra 320 SCSI RAID Adapter
32  *              PCI-X Dual Channel Ultra 320 SCSI Adapter
33  *              PCI-X Dual Channel Ultra 320 SCSI RAID Enablement Card
34  *              Embedded SCSI adapter on p615 and p655 systems
35  *
36  * Supported Hardware Features:
37  *      - Ultra 320 SCSI controller
38  *      - PCI-X host interface
39  *      - Embedded PowerPC RISC Processor and Hardware XOR DMA Engine
40  *      - Non-Volatile Write Cache
41  *      - Supports attachment of non-RAID disks, tape, and optical devices
42  *      - RAID Levels 0, 5, 10
43  *      - Hot spare
44  *      - Background Parity Checking
45  *      - Background Data Scrubbing
46  *      - Ability to increase the capacity of an existing RAID 5 disk array
47  *              by adding disks
48  *
49  * Driver Features:
50  *      - Tagged command queuing
51  *      - Adapter microcode download
52  *      - PCI hot plug
53  *      - SCSI device hot plug
54  *
55  */
56
57 #include <linux/fs.h>
58 #include <linux/init.h>
59 #include <linux/types.h>
60 #include <linux/errno.h>
61 #include <linux/kernel.h>
62 #include <linux/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  * @filp:               open sysfs file
3124  * @kobj:               kobject struct
3125  * @bin_attr:           bin_attribute struct
3126  * @buf:                buffer
3127  * @off:                offset
3128  * @count:              buffer size
3129  *
3130  * Return value:
3131  *      number of bytes printed to buffer
3132  **/
3133 static ssize_t ipr_read_trace(struct file *filp, struct kobject *kobj,
3134                               struct bin_attribute *bin_attr,
3135                               char *buf, loff_t off, size_t count)
3136 {
3137         struct device *dev = container_of(kobj, struct device, kobj);
3138         struct Scsi_Host *shost = class_to_shost(dev);
3139         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3140         unsigned long lock_flags = 0;
3141         ssize_t ret;
3142
3143         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3144         ret = memory_read_from_buffer(buf, count, &off, ioa_cfg->trace,
3145                                 IPR_TRACE_SIZE);
3146         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3147
3148         return ret;
3149 }
3150
3151 static struct bin_attribute ipr_trace_attr = {
3152         .attr = {
3153                 .name = "trace",
3154                 .mode = S_IRUGO,
3155         },
3156         .size = 0,
3157         .read = ipr_read_trace,
3158 };
3159 #endif
3160
3161 /**
3162  * ipr_show_fw_version - Show the firmware version
3163  * @dev:        class device struct
3164  * @buf:        buffer
3165  *
3166  * Return value:
3167  *      number of bytes printed to buffer
3168  **/
3169 static ssize_t ipr_show_fw_version(struct device *dev,
3170                                    struct device_attribute *attr, char *buf)
3171 {
3172         struct Scsi_Host *shost = class_to_shost(dev);
3173         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3174         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
3175         unsigned long lock_flags = 0;
3176         int len;
3177
3178         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3179         len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
3180                        ucode_vpd->major_release, ucode_vpd->card_type,
3181                        ucode_vpd->minor_release[0],
3182                        ucode_vpd->minor_release[1]);
3183         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3184         return len;
3185 }
3186
3187 static struct device_attribute ipr_fw_version_attr = {
3188         .attr = {
3189                 .name =         "fw_version",
3190                 .mode =         S_IRUGO,
3191         },
3192         .show = ipr_show_fw_version,
3193 };
3194
3195 /**
3196  * ipr_show_log_level - Show the adapter's error logging level
3197  * @dev:        class device struct
3198  * @buf:        buffer
3199  *
3200  * Return value:
3201  *      number of bytes printed to buffer
3202  **/
3203 static ssize_t ipr_show_log_level(struct device *dev,
3204                                    struct device_attribute *attr, char *buf)
3205 {
3206         struct Scsi_Host *shost = class_to_shost(dev);
3207         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3208         unsigned long lock_flags = 0;
3209         int len;
3210
3211         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3212         len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
3213         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3214         return len;
3215 }
3216
3217 /**
3218  * ipr_store_log_level - Change the adapter's error logging level
3219  * @dev:        class device struct
3220  * @buf:        buffer
3221  *
3222  * Return value:
3223  *      number of bytes printed to buffer
3224  **/
3225 static ssize_t ipr_store_log_level(struct device *dev,
3226                                    struct device_attribute *attr,
3227                                    const char *buf, size_t count)
3228 {
3229         struct Scsi_Host *shost = class_to_shost(dev);
3230         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3231         unsigned long lock_flags = 0;
3232
3233         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3234         ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
3235         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3236         return strlen(buf);
3237 }
3238
3239 static struct device_attribute ipr_log_level_attr = {
3240         .attr = {
3241                 .name =         "log_level",
3242                 .mode =         S_IRUGO | S_IWUSR,
3243         },
3244         .show = ipr_show_log_level,
3245         .store = ipr_store_log_level
3246 };
3247
3248 /**
3249  * ipr_store_diagnostics - IOA Diagnostics interface
3250  * @dev:        device struct
3251  * @buf:        buffer
3252  * @count:      buffer size
3253  *
3254  * This function will reset the adapter and wait a reasonable
3255  * amount of time for any errors that the adapter might log.
3256  *
3257  * Return value:
3258  *      count on success / other on failure
3259  **/
3260 static ssize_t ipr_store_diagnostics(struct device *dev,
3261                                      struct device_attribute *attr,
3262                                      const char *buf, size_t count)
3263 {
3264         struct Scsi_Host *shost = class_to_shost(dev);
3265         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3266         unsigned long lock_flags = 0;
3267         int rc = count;
3268
3269         if (!capable(CAP_SYS_ADMIN))
3270                 return -EACCES;
3271
3272         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3273         while(ioa_cfg->in_reset_reload) {
3274                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3275                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3276                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3277         }
3278
3279         ioa_cfg->errors_logged = 0;
3280         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3281
3282         if (ioa_cfg->in_reset_reload) {
3283                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3284                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3285
3286                 /* Wait for a second for any errors to be logged */
3287                 msleep(1000);
3288         } else {
3289                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3290                 return -EIO;
3291         }
3292
3293         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3294         if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
3295                 rc = -EIO;
3296         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3297
3298         return rc;
3299 }
3300
3301 static struct device_attribute ipr_diagnostics_attr = {
3302         .attr = {
3303                 .name =         "run_diagnostics",
3304                 .mode =         S_IWUSR,
3305         },
3306         .store = ipr_store_diagnostics
3307 };
3308
3309 /**
3310  * ipr_show_adapter_state - Show the adapter's state
3311  * @class_dev:  device struct
3312  * @buf:        buffer
3313  *
3314  * Return value:
3315  *      number of bytes printed to buffer
3316  **/
3317 static ssize_t ipr_show_adapter_state(struct device *dev,
3318                                       struct device_attribute *attr, char *buf)
3319 {
3320         struct Scsi_Host *shost = class_to_shost(dev);
3321         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3322         unsigned long lock_flags = 0;
3323         int len;
3324
3325         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3326         if (ioa_cfg->ioa_is_dead)
3327                 len = snprintf(buf, PAGE_SIZE, "offline\n");
3328         else
3329                 len = snprintf(buf, PAGE_SIZE, "online\n");
3330         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3331         return len;
3332 }
3333
3334 /**
3335  * ipr_store_adapter_state - Change adapter state
3336  * @dev:        device struct
3337  * @buf:        buffer
3338  * @count:      buffer size
3339  *
3340  * This function will change the adapter's state.
3341  *
3342  * Return value:
3343  *      count on success / other on failure
3344  **/
3345 static ssize_t ipr_store_adapter_state(struct device *dev,
3346                                        struct device_attribute *attr,
3347                                        const char *buf, size_t count)
3348 {
3349         struct Scsi_Host *shost = class_to_shost(dev);
3350         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3351         unsigned long lock_flags;
3352         int result = count;
3353
3354         if (!capable(CAP_SYS_ADMIN))
3355                 return -EACCES;
3356
3357         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3358         if (ioa_cfg->ioa_is_dead && !strncmp(buf, "online", 6)) {
3359                 ioa_cfg->ioa_is_dead = 0;
3360                 ioa_cfg->reset_retries = 0;
3361                 ioa_cfg->in_ioa_bringdown = 0;
3362                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3363         }
3364         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3365         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3366
3367         return result;
3368 }
3369
3370 static struct device_attribute ipr_ioa_state_attr = {
3371         .attr = {
3372                 .name =         "online_state",
3373                 .mode =         S_IRUGO | S_IWUSR,
3374         },
3375         .show = ipr_show_adapter_state,
3376         .store = ipr_store_adapter_state
3377 };
3378
3379 /**
3380  * ipr_store_reset_adapter - Reset the adapter
3381  * @dev:        device struct
3382  * @buf:        buffer
3383  * @count:      buffer size
3384  *
3385  * This function will reset the adapter.
3386  *
3387  * Return value:
3388  *      count on success / other on failure
3389  **/
3390 static ssize_t ipr_store_reset_adapter(struct device *dev,
3391                                        struct device_attribute *attr,
3392                                        const char *buf, size_t count)
3393 {
3394         struct Scsi_Host *shost = class_to_shost(dev);
3395         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3396         unsigned long lock_flags;
3397         int result = count;
3398
3399         if (!capable(CAP_SYS_ADMIN))
3400                 return -EACCES;
3401
3402         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3403         if (!ioa_cfg->in_reset_reload)
3404                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3405         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3406         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3407
3408         return result;
3409 }
3410
3411 static struct device_attribute ipr_ioa_reset_attr = {
3412         .attr = {
3413                 .name =         "reset_host",
3414                 .mode =         S_IWUSR,
3415         },
3416         .store = ipr_store_reset_adapter
3417 };
3418
3419 /**
3420  * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
3421  * @buf_len:            buffer length
3422  *
3423  * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3424  * list to use for microcode download
3425  *
3426  * Return value:
3427  *      pointer to sglist / NULL on failure
3428  **/
3429 static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
3430 {
3431         int sg_size, order, bsize_elem, num_elem, i, j;
3432         struct ipr_sglist *sglist;
3433         struct scatterlist *scatterlist;
3434         struct page *page;
3435
3436         /* Get the minimum size per scatter/gather element */
3437         sg_size = buf_len / (IPR_MAX_SGLIST - 1);
3438
3439         /* Get the actual size per element */
3440         order = get_order(sg_size);
3441
3442         /* Determine the actual number of bytes per element */
3443         bsize_elem = PAGE_SIZE * (1 << order);
3444
3445         /* Determine the actual number of sg entries needed */
3446         if (buf_len % bsize_elem)
3447                 num_elem = (buf_len / bsize_elem) + 1;
3448         else
3449                 num_elem = buf_len / bsize_elem;
3450
3451         /* Allocate a scatter/gather list for the DMA */
3452         sglist = kzalloc(sizeof(struct ipr_sglist) +
3453                          (sizeof(struct scatterlist) * (num_elem - 1)),
3454                          GFP_KERNEL);
3455
3456         if (sglist == NULL) {
3457                 ipr_trace;
3458                 return NULL;
3459         }
3460
3461         scatterlist = sglist->scatterlist;
3462         sg_init_table(scatterlist, num_elem);
3463
3464         sglist->order = order;
3465         sglist->num_sg = num_elem;
3466
3467         /* Allocate a bunch of sg elements */
3468         for (i = 0; i < num_elem; i++) {
3469                 page = alloc_pages(GFP_KERNEL, order);
3470                 if (!page) {
3471                         ipr_trace;
3472
3473                         /* Free up what we already allocated */
3474                         for (j = i - 1; j >= 0; j--)
3475                                 __free_pages(sg_page(&scatterlist[j]), order);
3476                         kfree(sglist);
3477                         return NULL;
3478                 }
3479
3480                 sg_set_page(&scatterlist[i], page, 0, 0);
3481         }
3482
3483         return sglist;
3484 }
3485
3486 /**
3487  * ipr_free_ucode_buffer - Frees a microcode download buffer
3488  * @p_dnld:             scatter/gather list pointer
3489  *
3490  * Free a DMA'able ucode download buffer previously allocated with
3491  * ipr_alloc_ucode_buffer
3492  *
3493  * Return value:
3494  *      nothing
3495  **/
3496 static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
3497 {
3498         int i;
3499
3500         for (i = 0; i < sglist->num_sg; i++)
3501                 __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
3502
3503         kfree(sglist);
3504 }
3505
3506 /**
3507  * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
3508  * @sglist:             scatter/gather list pointer
3509  * @buffer:             buffer pointer
3510  * @len:                buffer length
3511  *
3512  * Copy a microcode image from a user buffer into a buffer allocated by
3513  * ipr_alloc_ucode_buffer
3514  *
3515  * Return value:
3516  *      0 on success / other on failure
3517  **/
3518 static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
3519                                  u8 *buffer, u32 len)
3520 {
3521         int bsize_elem, i, result = 0;
3522         struct scatterlist *scatterlist;
3523         void *kaddr;
3524
3525         /* Determine the actual number of bytes per element */
3526         bsize_elem = PAGE_SIZE * (1 << sglist->order);
3527
3528         scatterlist = sglist->scatterlist;
3529
3530         for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
3531                 struct page *page = sg_page(&scatterlist[i]);
3532
3533                 kaddr = kmap(page);
3534                 memcpy(kaddr, buffer, bsize_elem);
3535                 kunmap(page);
3536
3537                 scatterlist[i].length = bsize_elem;
3538
3539                 if (result != 0) {
3540                         ipr_trace;
3541                         return result;
3542                 }
3543         }
3544
3545         if (len % bsize_elem) {
3546                 struct page *page = sg_page(&scatterlist[i]);
3547
3548                 kaddr = kmap(page);
3549                 memcpy(kaddr, buffer, len % bsize_elem);
3550                 kunmap(page);
3551
3552                 scatterlist[i].length = len % bsize_elem;
3553         }
3554
3555         sglist->buffer_len = len;
3556         return result;
3557 }
3558
3559 /**
3560  * ipr_build_ucode_ioadl64 - Build a microcode download IOADL
3561  * @ipr_cmd:            ipr command struct
3562  * @sglist:             scatter/gather list
3563  *
3564  * Builds a microcode download IOA data list (IOADL).
3565  *
3566  **/
3567 static void ipr_build_ucode_ioadl64(struct ipr_cmnd *ipr_cmd,
3568                                     struct ipr_sglist *sglist)
3569 {
3570         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3571         struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
3572         struct scatterlist *scatterlist = sglist->scatterlist;
3573         int i;
3574
3575         ipr_cmd->dma_use_sg = sglist->num_dma_sg;
3576         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3577         ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3578
3579         ioarcb->ioadl_len =
3580                 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
3581         for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3582                 ioadl64[i].flags = cpu_to_be32(IPR_IOADL_FLAGS_WRITE);
3583                 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(&scatterlist[i]));
3584                 ioadl64[i].address = cpu_to_be64(sg_dma_address(&scatterlist[i]));
3585         }
3586
3587         ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3588 }
3589
3590 /**
3591  * ipr_build_ucode_ioadl - Build a microcode download IOADL
3592  * @ipr_cmd:    ipr command struct
3593  * @sglist:             scatter/gather list
3594  *
3595  * Builds a microcode download IOA data list (IOADL).
3596  *
3597  **/
3598 static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
3599                                   struct ipr_sglist *sglist)
3600 {
3601         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3602         struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
3603         struct scatterlist *scatterlist = sglist->scatterlist;
3604         int i;
3605
3606         ipr_cmd->dma_use_sg = sglist->num_dma_sg;
3607         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3608         ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3609
3610         ioarcb->ioadl_len =
3611                 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3612
3613         for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3614                 ioadl[i].flags_and_data_len =
3615                         cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
3616                 ioadl[i].address =
3617                         cpu_to_be32(sg_dma_address(&scatterlist[i]));
3618         }
3619
3620         ioadl[i-1].flags_and_data_len |=
3621                 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3622 }
3623
3624 /**
3625  * ipr_update_ioa_ucode - Update IOA's microcode
3626  * @ioa_cfg:    ioa config struct
3627  * @sglist:             scatter/gather list
3628  *
3629  * Initiate an adapter reset to update the IOA's microcode
3630  *
3631  * Return value:
3632  *      0 on success / -EIO on failure
3633  **/
3634 static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
3635                                 struct ipr_sglist *sglist)
3636 {
3637         unsigned long lock_flags;
3638
3639         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3640         while(ioa_cfg->in_reset_reload) {
3641                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3642                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3643                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3644         }
3645
3646         if (ioa_cfg->ucode_sglist) {
3647                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3648                 dev_err(&ioa_cfg->pdev->dev,
3649                         "Microcode download already in progress\n");
3650                 return -EIO;
3651         }
3652
3653         sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
3654                                         sglist->num_sg, DMA_TO_DEVICE);
3655
3656         if (!sglist->num_dma_sg) {
3657                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3658                 dev_err(&ioa_cfg->pdev->dev,
3659                         "Failed to map microcode download buffer!\n");
3660                 return -EIO;
3661         }
3662
3663         ioa_cfg->ucode_sglist = sglist;
3664         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3665         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3666         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3667
3668         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3669         ioa_cfg->ucode_sglist = NULL;
3670         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3671         return 0;
3672 }
3673
3674 /**
3675  * ipr_store_update_fw - Update the firmware on the adapter
3676  * @class_dev:  device struct
3677  * @buf:        buffer
3678  * @count:      buffer size
3679  *
3680  * This function will update the firmware on the adapter.
3681  *
3682  * Return value:
3683  *      count on success / other on failure
3684  **/
3685 static ssize_t ipr_store_update_fw(struct device *dev,
3686                                    struct device_attribute *attr,
3687                                    const char *buf, size_t count)
3688 {
3689         struct Scsi_Host *shost = class_to_shost(dev);
3690         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3691         struct ipr_ucode_image_header *image_hdr;
3692         const struct firmware *fw_entry;
3693         struct ipr_sglist *sglist;
3694         char fname[100];
3695         char *src;
3696         int len, result, dnld_size;
3697
3698         if (!capable(CAP_SYS_ADMIN))
3699                 return -EACCES;
3700
3701         len = snprintf(fname, 99, "%s", buf);
3702         fname[len-1] = '\0';
3703
3704         if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
3705                 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
3706                 return -EIO;
3707         }
3708
3709         image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
3710
3711         if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
3712             (ioa_cfg->vpd_cbs->page3_data.card_type &&
3713              ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
3714                 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
3715                 release_firmware(fw_entry);
3716                 return -EINVAL;
3717         }
3718
3719         src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
3720         dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
3721         sglist = ipr_alloc_ucode_buffer(dnld_size);
3722
3723         if (!sglist) {
3724                 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
3725                 release_firmware(fw_entry);
3726                 return -ENOMEM;
3727         }
3728
3729         result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
3730
3731         if (result) {
3732                 dev_err(&ioa_cfg->pdev->dev,
3733                         "Microcode buffer copy to DMA buffer failed\n");
3734                 goto out;
3735         }
3736
3737         result = ipr_update_ioa_ucode(ioa_cfg, sglist);
3738
3739         if (!result)
3740                 result = count;
3741 out:
3742         ipr_free_ucode_buffer(sglist);
3743         release_firmware(fw_entry);
3744         return result;
3745 }
3746
3747 static struct device_attribute ipr_update_fw_attr = {
3748         .attr = {
3749                 .name =         "update_fw",
3750                 .mode =         S_IWUSR,
3751         },
3752         .store = ipr_store_update_fw
3753 };
3754
3755 static struct device_attribute *ipr_ioa_attrs[] = {
3756         &ipr_fw_version_attr,
3757         &ipr_log_level_attr,
3758         &ipr_diagnostics_attr,
3759         &ipr_ioa_state_attr,
3760         &ipr_ioa_reset_attr,
3761         &ipr_update_fw_attr,
3762         NULL,
3763 };
3764
3765 #ifdef CONFIG_SCSI_IPR_DUMP
3766 /**
3767  * ipr_read_dump - Dump the adapter
3768  * @filp:               open sysfs file
3769  * @kobj:               kobject struct
3770  * @bin_attr:           bin_attribute struct
3771  * @buf:                buffer
3772  * @off:                offset
3773  * @count:              buffer size
3774  *
3775  * Return value:
3776  *      number of bytes printed to buffer
3777  **/
3778 static ssize_t ipr_read_dump(struct file *filp, struct kobject *kobj,
3779                              struct bin_attribute *bin_attr,
3780                              char *buf, loff_t off, size_t count)
3781 {
3782         struct device *cdev = container_of(kobj, struct device, kobj);
3783         struct Scsi_Host *shost = class_to_shost(cdev);
3784         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3785         struct ipr_dump *dump;
3786         unsigned long lock_flags = 0;
3787         char *src;
3788         int len;
3789         size_t rc = count;
3790
3791         if (!capable(CAP_SYS_ADMIN))
3792                 return -EACCES;
3793
3794         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3795         dump = ioa_cfg->dump;
3796
3797         if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
3798                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3799                 return 0;
3800         }
3801         kref_get(&dump->kref);
3802         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3803
3804         if (off > dump->driver_dump.hdr.len) {
3805                 kref_put(&dump->kref, ipr_release_dump);
3806                 return 0;
3807         }
3808
3809         if (off + count > dump->driver_dump.hdr.len) {
3810                 count = dump->driver_dump.hdr.len - off;
3811                 rc = count;
3812         }
3813
3814         if (count && off < sizeof(dump->driver_dump)) {
3815                 if (off + count > sizeof(dump->driver_dump))
3816                         len = sizeof(dump->driver_dump) - off;
3817                 else
3818                         len = count;
3819                 src = (u8 *)&dump->driver_dump + off;
3820                 memcpy(buf, src, len);
3821                 buf += len;
3822                 off += len;
3823                 count -= len;
3824         }
3825
3826         off -= sizeof(dump->driver_dump);
3827
3828         if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
3829                 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
3830                         len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
3831                 else
3832                         len = count;
3833                 src = (u8 *)&dump->ioa_dump + off;
3834                 memcpy(buf, src, len);
3835                 buf += len;
3836                 off += len;
3837                 count -= len;
3838         }
3839
3840         off -= offsetof(struct ipr_ioa_dump, ioa_data);
3841
3842         while (count) {
3843                 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
3844                         len = PAGE_ALIGN(off) - off;
3845                 else
3846                         len = count;
3847                 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
3848                 src += off & ~PAGE_MASK;
3849                 memcpy(buf, src, len);
3850                 buf += len;
3851                 off += len;
3852                 count -= len;
3853         }
3854
3855         kref_put(&dump->kref, ipr_release_dump);
3856         return rc;
3857 }
3858
3859 /**
3860  * ipr_alloc_dump - Prepare for adapter dump
3861  * @ioa_cfg:    ioa config struct
3862  *
3863  * Return value:
3864  *      0 on success / other on failure
3865  **/
3866 static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
3867 {
3868         struct ipr_dump *dump;
3869         unsigned long lock_flags = 0;
3870
3871         dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
3872
3873         if (!dump) {
3874                 ipr_err("Dump memory allocation failed\n");
3875                 return -ENOMEM;
3876         }
3877
3878         kref_init(&dump->kref);
3879         dump->ioa_cfg = ioa_cfg;
3880
3881         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3882
3883         if (INACTIVE != ioa_cfg->sdt_state) {
3884                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3885                 kfree(dump);
3886                 return 0;
3887         }
3888
3889         ioa_cfg->dump = dump;
3890         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
3891         if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
3892                 ioa_cfg->dump_taken = 1;
3893                 schedule_work(&ioa_cfg->work_q);
3894         }
3895         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3896
3897         return 0;
3898 }
3899
3900 /**
3901  * ipr_free_dump - Free adapter dump memory
3902  * @ioa_cfg:    ioa config struct
3903  *
3904  * Return value:
3905  *      0 on success / other on failure
3906  **/
3907 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
3908 {
3909         struct ipr_dump *dump;
3910         unsigned long lock_flags = 0;
3911
3912         ENTER;
3913
3914         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3915         dump = ioa_cfg->dump;
3916         if (!dump) {
3917                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3918                 return 0;
3919         }
3920
3921         ioa_cfg->dump = NULL;
3922         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3923
3924         kref_put(&dump->kref, ipr_release_dump);
3925
3926         LEAVE;
3927         return 0;
3928 }
3929
3930 /**
3931  * ipr_write_dump - Setup dump state of adapter
3932  * @filp:               open sysfs file
3933  * @kobj:               kobject struct
3934  * @bin_attr:           bin_attribute struct
3935  * @buf:                buffer
3936  * @off:                offset
3937  * @count:              buffer size
3938  *
3939  * Return value:
3940  *      number of bytes printed to buffer
3941  **/
3942 static ssize_t ipr_write_dump(struct file *filp, struct kobject *kobj,
3943                               struct bin_attribute *bin_attr,
3944                               char *buf, loff_t off, size_t count)
3945 {
3946         struct device *cdev = container_of(kobj, struct device, kobj);
3947         struct Scsi_Host *shost = class_to_shost(cdev);
3948         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3949         int rc;
3950
3951         if (!capable(CAP_SYS_ADMIN))
3952                 return -EACCES;
3953
3954         if (buf[0] == '1')
3955                 rc = ipr_alloc_dump(ioa_cfg);
3956         else if (buf[0] == '0')
3957                 rc = ipr_free_dump(ioa_cfg);
3958         else
3959                 return -EINVAL;
3960
3961         if (rc)
3962                 return rc;
3963         else
3964                 return count;
3965 }
3966
3967 static struct bin_attribute ipr_dump_attr = {
3968         .attr = {
3969                 .name = "dump",
3970                 .mode = S_IRUSR | S_IWUSR,
3971         },
3972         .size = 0,
3973         .read = ipr_read_dump,
3974         .write = ipr_write_dump
3975 };
3976 #else
3977 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
3978 #endif
3979
3980 /**
3981  * ipr_change_queue_depth - Change the device's queue depth
3982  * @sdev:       scsi device struct
3983  * @qdepth:     depth to set
3984  * @reason:     calling context
3985  *
3986  * Return value:
3987  *      actual depth set
3988  **/
3989 static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth,
3990                                   int reason)
3991 {
3992         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3993         struct ipr_resource_entry *res;
3994         unsigned long lock_flags = 0;
3995
3996         if (reason != SCSI_QDEPTH_DEFAULT)
3997                 return -EOPNOTSUPP;
3998
3999         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4000         res = (struct ipr_resource_entry *)sdev->hostdata;
4001
4002         if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
4003                 qdepth = IPR_MAX_CMD_PER_ATA_LUN;
4004         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4005
4006         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
4007         return sdev->queue_depth;
4008 }
4009
4010 /**
4011  * ipr_change_queue_type - Change the device's queue type
4012  * @dsev:               scsi device struct
4013  * @tag_type:   type of tags to use
4014  *
4015  * Return value:
4016  *      actual queue type set
4017  **/
4018 static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
4019 {
4020         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4021         struct ipr_resource_entry *res;
4022         unsigned long lock_flags = 0;
4023
4024         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4025         res = (struct ipr_resource_entry *)sdev->hostdata;
4026
4027         if (res) {
4028                 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
4029                         /*
4030                          * We don't bother quiescing the device here since the
4031                          * adapter firmware does it for us.
4032                          */
4033                         scsi_set_tag_type(sdev, tag_type);
4034
4035                         if (tag_type)
4036                                 scsi_activate_tcq(sdev, sdev->queue_depth);
4037                         else
4038                                 scsi_deactivate_tcq(sdev, sdev->queue_depth);
4039                 } else
4040                         tag_type = 0;
4041         } else
4042                 tag_type = 0;
4043
4044         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4045         return tag_type;
4046 }
4047
4048 /**
4049  * ipr_show_adapter_handle - Show the adapter's resource handle for this device
4050  * @dev:        device struct
4051  * @buf:        buffer
4052  *
4053  * Return value:
4054  *      number of bytes printed to buffer
4055  **/
4056 static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
4057 {
4058         struct scsi_device *sdev = to_scsi_device(dev);
4059         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4060         struct ipr_resource_entry *res;
4061         unsigned long lock_flags = 0;
4062         ssize_t len = -ENXIO;
4063
4064         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4065         res = (struct ipr_resource_entry *)sdev->hostdata;
4066         if (res)
4067                 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->res_handle);
4068         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4069         return len;
4070 }
4071
4072 static struct device_attribute ipr_adapter_handle_attr = {
4073         .attr = {
4074                 .name =         "adapter_handle",
4075                 .mode =         S_IRUSR,
4076         },
4077         .show = ipr_show_adapter_handle
4078 };
4079
4080 /**
4081  * ipr_show_resource_path - Show the resource path for this device.
4082  * @dev:        device struct
4083  * @buf:        buffer
4084  *
4085  * Return value:
4086  *      number of bytes printed to buffer
4087  **/
4088 static ssize_t ipr_show_resource_path(struct device *dev, struct device_attribute *attr, char *buf)
4089 {
4090         struct scsi_device *sdev = to_scsi_device(dev);
4091         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4092         struct ipr_resource_entry *res;
4093         unsigned long lock_flags = 0;
4094         ssize_t len = -ENXIO;
4095         char buffer[IPR_MAX_RES_PATH_LENGTH];
4096
4097         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4098         res = (struct ipr_resource_entry *)sdev->hostdata;
4099         if (res)
4100                 len = snprintf(buf, PAGE_SIZE, "%s\n",
4101                                ipr_format_resource_path(&res->res_path[0], &buffer[0]));
4102         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4103         return len;
4104 }
4105
4106 static struct device_attribute ipr_resource_path_attr = {
4107         .attr = {
4108                 .name =         "resource_path",
4109                 .mode =         S_IRUSR,
4110         },
4111         .show = ipr_show_resource_path
4112 };
4113
4114 static struct device_attribute *ipr_dev_attrs[] = {
4115         &ipr_adapter_handle_attr,
4116         &ipr_resource_path_attr,
4117         NULL,
4118 };
4119
4120 /**
4121  * ipr_biosparam - Return the HSC mapping
4122  * @sdev:                       scsi device struct
4123  * @block_device:       block device pointer
4124  * @capacity:           capacity of the device
4125  * @parm:                       Array containing returned HSC values.
4126  *
4127  * This function generates the HSC parms that fdisk uses.
4128  * We want to make sure we return something that places partitions
4129  * on 4k boundaries for best performance with the IOA.
4130  *
4131  * Return value:
4132  *      0 on success
4133  **/
4134 static int ipr_biosparam(struct scsi_device *sdev,
4135                          struct block_device *block_device,
4136                          sector_t capacity, int *parm)
4137 {
4138         int heads, sectors;
4139         sector_t cylinders;
4140
4141         heads = 128;
4142         sectors = 32;
4143
4144         cylinders = capacity;
4145         sector_div(cylinders, (128 * 32));
4146
4147         /* return result */
4148         parm[0] = heads;
4149         parm[1] = sectors;
4150         parm[2] = cylinders;
4151
4152         return 0;
4153 }
4154
4155 /**
4156  * ipr_find_starget - Find target based on bus/target.
4157  * @starget:    scsi target struct
4158  *
4159  * Return value:
4160  *      resource entry pointer if found / NULL if not found
4161  **/
4162 static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
4163 {
4164         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4165         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4166         struct ipr_resource_entry *res;
4167
4168         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4169                 if ((res->bus == starget->channel) &&
4170                     (res->target == starget->id) &&
4171                     (res->lun == 0)) {
4172                         return res;
4173                 }
4174         }
4175
4176         return NULL;
4177 }
4178
4179 static struct ata_port_info sata_port_info;
4180
4181 /**
4182  * ipr_target_alloc - Prepare for commands to a SCSI target
4183  * @starget:    scsi target struct
4184  *
4185  * If the device is a SATA device, this function allocates an
4186  * ATA port with libata, else it does nothing.
4187  *
4188  * Return value:
4189  *      0 on success / non-0 on failure
4190  **/
4191 static int ipr_target_alloc(struct scsi_target *starget)
4192 {
4193         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4194         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4195         struct ipr_sata_port *sata_port;
4196         struct ata_port *ap;
4197         struct ipr_resource_entry *res;
4198         unsigned long lock_flags;
4199
4200         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4201         res = ipr_find_starget(starget);
4202         starget->hostdata = NULL;
4203
4204         if (res && ipr_is_gata(res)) {
4205                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4206                 sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
4207                 if (!sata_port)
4208                         return -ENOMEM;
4209
4210                 ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
4211                 if (ap) {
4212                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4213                         sata_port->ioa_cfg = ioa_cfg;
4214                         sata_port->ap = ap;
4215                         sata_port->res = res;
4216
4217                         res->sata_port = sata_port;
4218                         ap->private_data = sata_port;
4219                         starget->hostdata = sata_port;
4220                 } else {
4221                         kfree(sata_port);
4222                         return -ENOMEM;
4223                 }
4224         }
4225         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4226
4227         return 0;
4228 }
4229
4230 /**
4231  * ipr_target_destroy - Destroy a SCSI target
4232  * @starget:    scsi target struct
4233  *
4234  * If the device was a SATA device, this function frees the libata
4235  * ATA port, else it does nothing.
4236  *
4237  **/
4238 static void ipr_target_destroy(struct scsi_target *starget)
4239 {
4240         struct ipr_sata_port *sata_port = starget->hostdata;
4241         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4242         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4243
4244         if (ioa_cfg->sis64) {
4245                 if (starget->channel == IPR_ARRAY_VIRTUAL_BUS)
4246                         clear_bit(starget->id, ioa_cfg->array_ids);
4247                 else if (starget->channel == IPR_VSET_VIRTUAL_BUS)
4248                         clear_bit(starget->id, ioa_cfg->vset_ids);
4249                 else if (starget->channel == 0)
4250                         clear_bit(starget->id, ioa_cfg->target_ids);
4251         }
4252
4253         if (sata_port) {
4254                 starget->hostdata = NULL;
4255                 ata_sas_port_destroy(sata_port->ap);
4256                 kfree(sata_port);
4257         }
4258 }
4259
4260 /**
4261  * ipr_find_sdev - Find device based on bus/target/lun.
4262  * @sdev:       scsi device struct
4263  *
4264  * Return value:
4265  *      resource entry pointer if found / NULL if not found
4266  **/
4267 static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
4268 {
4269         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4270         struct ipr_resource_entry *res;
4271
4272         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4273                 if ((res->bus == sdev->channel) &&
4274                     (res->target == sdev->id) &&
4275                     (res->lun == sdev->lun))
4276                         return res;
4277         }
4278
4279         return NULL;
4280 }
4281
4282 /**
4283  * ipr_slave_destroy - Unconfigure a SCSI device
4284  * @sdev:       scsi device struct
4285  *
4286  * Return value:
4287  *      nothing
4288  **/
4289 static void ipr_slave_destroy(struct scsi_device *sdev)
4290 {
4291         struct ipr_resource_entry *res;
4292         struct ipr_ioa_cfg *ioa_cfg;
4293         unsigned long lock_flags = 0;
4294
4295         ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4296
4297         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4298         res = (struct ipr_resource_entry *) sdev->hostdata;
4299         if (res) {
4300                 if (res->sata_port)
4301                         res->sata_port->ap->link.device[0].class = ATA_DEV_NONE;
4302                 sdev->hostdata = NULL;
4303                 res->sdev = NULL;
4304                 res->sata_port = NULL;
4305         }
4306         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4307 }
4308
4309 /**
4310  * ipr_slave_configure - Configure a SCSI device
4311  * @sdev:       scsi device struct
4312  *
4313  * This function configures the specified scsi device.
4314  *
4315  * Return value:
4316  *      0 on success
4317  **/
4318 static int ipr_slave_configure(struct scsi_device *sdev)
4319 {
4320         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4321         struct ipr_resource_entry *res;
4322         struct ata_port *ap = NULL;
4323         unsigned long lock_flags = 0;
4324         char buffer[IPR_MAX_RES_PATH_LENGTH];
4325
4326         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4327         res = sdev->hostdata;
4328         if (res) {
4329                 if (ipr_is_af_dasd_device(res))
4330                         sdev->type = TYPE_RAID;
4331                 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
4332                         sdev->scsi_level = 4;
4333                         sdev->no_uld_attach = 1;
4334                 }
4335                 if (ipr_is_vset_device(res)) {
4336                         blk_queue_rq_timeout(sdev->request_queue,
4337                                              IPR_VSET_RW_TIMEOUT);
4338                         blk_queue_max_hw_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
4339                 }
4340                 if (ipr_is_vset_device(res) || ipr_is_scsi_disk(res))
4341                         sdev->allow_restart = 1;
4342                 if (ipr_is_gata(res) && res->sata_port)
4343                         ap = res->sata_port->ap;
4344                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4345
4346                 if (ap) {
4347                         scsi_adjust_queue_depth(sdev, 0, IPR_MAX_CMD_PER_ATA_LUN);
4348                         ata_sas_slave_configure(sdev, ap);
4349                 } else
4350                         scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
4351                 if (ioa_cfg->sis64)
4352                         sdev_printk(KERN_INFO, sdev, "Resource path: %s\n",
4353                                     ipr_format_resource_path(&res->res_path[0], &buffer[0]));
4354                 return 0;
4355         }
4356         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4357         return 0;
4358 }
4359
4360 /**
4361  * ipr_ata_slave_alloc - Prepare for commands to a SATA device
4362  * @sdev:       scsi device struct
4363  *
4364  * This function initializes an ATA port so that future commands
4365  * sent through queuecommand will work.
4366  *
4367  * Return value:
4368  *      0 on success
4369  **/
4370 static int ipr_ata_slave_alloc(struct scsi_device *sdev)
4371 {
4372         struct ipr_sata_port *sata_port = NULL;
4373         int rc = -ENXIO;
4374
4375         ENTER;
4376         if (sdev->sdev_target)
4377                 sata_port = sdev->sdev_target->hostdata;
4378         if (sata_port)
4379                 rc = ata_sas_port_init(sata_port->ap);
4380         if (rc)
4381                 ipr_slave_destroy(sdev);
4382
4383         LEAVE;
4384         return rc;
4385 }
4386
4387 /**
4388  * ipr_slave_alloc - Prepare for commands to a device.
4389  * @sdev:       scsi device struct
4390  *
4391  * This function saves a pointer to the resource entry
4392  * in the scsi device struct if the device exists. We
4393  * can then use this pointer in ipr_queuecommand when
4394  * handling new commands.
4395  *
4396  * Return value:
4397  *      0 on success / -ENXIO if device does not exist
4398  **/
4399 static int ipr_slave_alloc(struct scsi_device *sdev)
4400 {
4401         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4402         struct ipr_resource_entry *res;
4403         unsigned long lock_flags;
4404         int rc = -ENXIO;
4405
4406         sdev->hostdata = NULL;
4407
4408         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4409
4410         res = ipr_find_sdev(sdev);
4411         if (res) {
4412                 res->sdev = sdev;
4413                 res->add_to_ml = 0;
4414                 res->in_erp = 0;
4415                 sdev->hostdata = res;
4416                 if (!ipr_is_naca_model(res))
4417                         res->needs_sync_complete = 1;
4418                 rc = 0;
4419                 if (ipr_is_gata(res)) {
4420                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4421                         return ipr_ata_slave_alloc(sdev);
4422                 }
4423         }
4424
4425         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4426
4427         return rc;
4428 }
4429
4430 /**
4431  * ipr_eh_host_reset - Reset the host adapter
4432  * @scsi_cmd:   scsi command struct
4433  *
4434  * Return value:
4435  *      SUCCESS / FAILED
4436  **/
4437 static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
4438 {
4439         struct ipr_ioa_cfg *ioa_cfg;
4440         int rc;
4441
4442         ENTER;
4443         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
4444
4445         dev_err(&ioa_cfg->pdev->dev,
4446                 "Adapter being reset as a result of error recovery.\n");
4447
4448         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4449                 ioa_cfg->sdt_state = GET_DUMP;
4450
4451         rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
4452
4453         LEAVE;
4454         return rc;
4455 }
4456
4457 static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
4458 {
4459         int rc;
4460
4461         spin_lock_irq(cmd->device->host->host_lock);
4462         rc = __ipr_eh_host_reset(cmd);
4463         spin_unlock_irq(cmd->device->host->host_lock);
4464
4465         return rc;
4466 }
4467
4468 /**
4469  * ipr_device_reset - Reset the device
4470  * @ioa_cfg:    ioa config struct
4471  * @res:                resource entry struct
4472  *
4473  * This function issues a device reset to the affected device.
4474  * If the device is a SCSI device, a LUN reset will be sent
4475  * to the device first. If that does not work, a target reset
4476  * will be sent. If the device is a SATA device, a PHY reset will
4477  * be sent.
4478  *
4479  * Return value:
4480  *      0 on success / non-zero on failure
4481  **/
4482 static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
4483                             struct ipr_resource_entry *res)
4484 {
4485         struct ipr_cmnd *ipr_cmd;
4486         struct ipr_ioarcb *ioarcb;
4487         struct ipr_cmd_pkt *cmd_pkt;
4488         struct ipr_ioarcb_ata_regs *regs;
4489         u32 ioasc;
4490
4491         ENTER;
4492         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4493         ioarcb = &ipr_cmd->ioarcb;
4494         cmd_pkt = &ioarcb->cmd_pkt;
4495
4496         if (ipr_cmd->ioa_cfg->sis64) {
4497                 regs = &ipr_cmd->i.ata_ioadl.regs;
4498                 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
4499         } else
4500                 regs = &ioarcb->u.add_data.u.regs;
4501
4502         ioarcb->res_handle = res->res_handle;
4503         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4504         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
4505         if (ipr_is_gata(res)) {
4506                 cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
4507                 ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(regs->flags));
4508                 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
4509         }
4510
4511         ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
4512         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4513         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4514         if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET)
4515                 memcpy(&res->sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
4516                        sizeof(struct ipr_ioasa_gata));
4517
4518         LEAVE;
4519         return (IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0);
4520 }
4521
4522 /**
4523  * ipr_sata_reset - Reset the SATA port
4524  * @link:       SATA link to reset
4525  * @classes:    class of the attached device
4526  *
4527  * This function issues a SATA phy reset to the affected ATA link.
4528  *
4529  * Return value:
4530  *      0 on success / non-zero on failure
4531  **/
4532 static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
4533                                 unsigned long deadline)
4534 {
4535         struct ipr_sata_port *sata_port = link->ap->private_data;
4536         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
4537         struct ipr_resource_entry *res;
4538         unsigned long lock_flags = 0;
4539         int rc = -ENXIO;
4540
4541         ENTER;
4542         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4543         while(ioa_cfg->in_reset_reload) {
4544                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4545                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4546                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4547         }
4548
4549         res = sata_port->res;
4550         if (res) {
4551                 rc = ipr_device_reset(ioa_cfg, res);
4552                 *classes = res->ata_class;
4553         }
4554
4555         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4556         LEAVE;
4557         return rc;
4558 }
4559
4560 /**
4561  * ipr_eh_dev_reset - Reset the device
4562  * @scsi_cmd:   scsi command struct
4563  *
4564  * This function issues a device reset to the affected device.
4565  * A LUN reset will be sent to the device first. If that does
4566  * not work, a target reset will be sent.
4567  *
4568  * Return value:
4569  *      SUCCESS / FAILED
4570  **/
4571 static int __ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
4572 {
4573         struct ipr_cmnd *ipr_cmd;
4574         struct ipr_ioa_cfg *ioa_cfg;
4575         struct ipr_resource_entry *res;
4576         struct ata_port *ap;
4577         int rc = 0;
4578
4579         ENTER;
4580         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
4581         res = scsi_cmd->device->hostdata;
4582
4583         if (!res)
4584                 return FAILED;
4585
4586         /*
4587          * If we are currently going through reset/reload, return failed. This will force the
4588          * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
4589          * reset to complete
4590          */
4591         if (ioa_cfg->in_reset_reload)
4592                 return FAILED;
4593         if (ioa_cfg->ioa_is_dead)
4594                 return FAILED;
4595
4596         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
4597                 if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
4598                         if (ipr_cmd->scsi_cmd)
4599                                 ipr_cmd->done = ipr_scsi_eh_done;
4600                         if (ipr_cmd->qc)
4601                                 ipr_cmd->done = ipr_sata_eh_done;
4602                         if (ipr_cmd->qc && !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
4603                                 ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
4604                                 ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
4605                         }
4606                 }
4607         }
4608
4609         res->resetting_device = 1;
4610         scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
4611
4612         if (ipr_is_gata(res) && res->sata_port) {
4613                 ap = res->sata_port->ap;
4614                 spin_unlock_irq(scsi_cmd->device->host->host_lock);
4615                 ata_std_error_handler(ap);
4616                 spin_lock_irq(scsi_cmd->device->host->host_lock);
4617
4618                 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
4619                         if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
4620                                 rc = -EIO;
4621                                 break;
4622                         }
4623                 }
4624         } else
4625                 rc = ipr_device_reset(ioa_cfg, res);
4626         res->resetting_device = 0;
4627
4628         LEAVE;
4629         return (rc ? FAILED : SUCCESS);
4630 }
4631
4632 static int ipr_eh_dev_reset(struct scsi_cmnd * cmd)
4633 {
4634         int rc;
4635
4636         spin_lock_irq(cmd->device->host->host_lock);
4637         rc = __ipr_eh_dev_reset(cmd);
4638         spin_unlock_irq(cmd->device->host->host_lock);
4639
4640         return rc;
4641 }
4642
4643 /**
4644  * ipr_bus_reset_done - Op done function for bus reset.
4645  * @ipr_cmd:    ipr command struct
4646  *
4647  * This function is the op done function for a bus reset
4648  *
4649  * Return value:
4650  *      none
4651  **/
4652 static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
4653 {
4654         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4655         struct ipr_resource_entry *res;
4656
4657         ENTER;
4658         if (!ioa_cfg->sis64)
4659                 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4660                         if (res->res_handle == ipr_cmd->ioarcb.res_handle) {
4661                                 scsi_report_bus_reset(ioa_cfg->host, res->bus);
4662                                 break;
4663                         }
4664                 }
4665
4666         /*
4667          * If abort has not completed, indicate the reset has, else call the
4668          * abort's done function to wake the sleeping eh thread
4669          */
4670         if (ipr_cmd->sibling->sibling)
4671                 ipr_cmd->sibling->sibling = NULL;
4672         else
4673                 ipr_cmd->sibling->done(ipr_cmd->sibling);
4674
4675         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4676         LEAVE;
4677 }
4678
4679 /**
4680  * ipr_abort_timeout - An abort task has timed out
4681  * @ipr_cmd:    ipr command struct
4682  *
4683  * This function handles when an abort task times out. If this
4684  * happens we issue a bus reset since we have resources tied
4685  * up that must be freed before returning to the midlayer.
4686  *
4687  * Return value:
4688  *      none
4689  **/
4690 static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
4691 {
4692         struct ipr_cmnd *reset_cmd;
4693         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4694         struct ipr_cmd_pkt *cmd_pkt;
4695         unsigned long lock_flags = 0;
4696
4697         ENTER;
4698         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4699         if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
4700                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4701                 return;
4702         }
4703
4704         sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
4705         reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4706         ipr_cmd->sibling = reset_cmd;
4707         reset_cmd->sibling = ipr_cmd;
4708         reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
4709         cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
4710         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4711         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
4712         cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
4713
4714         ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
4715         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4716         LEAVE;
4717 }
4718
4719 /**
4720  * ipr_cancel_op - Cancel specified op
4721  * @scsi_cmd:   scsi command struct
4722  *
4723  * This function cancels specified op.
4724  *
4725  * Return value:
4726  *      SUCCESS / FAILED
4727  **/
4728 static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
4729 {
4730         struct ipr_cmnd *ipr_cmd;
4731         struct ipr_ioa_cfg *ioa_cfg;
4732         struct ipr_resource_entry *res;
4733         struct ipr_cmd_pkt *cmd_pkt;
4734         u32 ioasc;
4735         int op_found = 0;
4736
4737         ENTER;
4738         ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
4739         res = scsi_cmd->device->hostdata;
4740
4741         /* If we are currently going through reset/reload, return failed.
4742          * This will force the mid-layer to call ipr_eh_host_reset,
4743          * which will then go to sleep and wait for the reset to complete
4744          */
4745         if (ioa_cfg->in_reset_reload || ioa_cfg->ioa_is_dead)
4746                 return FAILED;
4747         if (!res || !ipr_is_gscsi(res))
4748                 return FAILED;
4749
4750         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
4751                 if (ipr_cmd->scsi_cmd == scsi_cmd) {
4752                         ipr_cmd->done = ipr_scsi_eh_done;
4753                         op_found = 1;
4754                         break;
4755                 }
4756         }
4757
4758         if (!op_found)
4759                 return SUCCESS;
4760
4761         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4762         ipr_cmd->ioarcb.res_handle = res->res_handle;
4763         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4764         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4765         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4766         ipr_cmd->u.sdev = scsi_cmd->device;
4767
4768         scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
4769                     scsi_cmd->cmnd[0]);
4770         ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
4771         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4772
4773         /*
4774          * If the abort task timed out and we sent a bus reset, we will get
4775          * one the following responses to the abort
4776          */
4777         if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
4778                 ioasc = 0;
4779                 ipr_trace;
4780         }
4781
4782         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4783         if (!ipr_is_naca_model(res))
4784                 res->needs_sync_complete = 1;
4785
4786         LEAVE;
4787         return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
4788 }
4789
4790 /**
4791  * ipr_eh_abort - Abort a single op
4792  * @scsi_cmd:   scsi command struct
4793  *
4794  * Return value:
4795  *      SUCCESS / FAILED
4796  **/
4797 static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
4798 {
4799         unsigned long flags;
4800         int rc;
4801
4802         ENTER;
4803
4804         spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
4805         rc = ipr_cancel_op(scsi_cmd);
4806         spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
4807
4808         LEAVE;
4809         return rc;
4810 }
4811
4812 /**
4813  * ipr_handle_other_interrupt - Handle "other" interrupts
4814  * @ioa_cfg:    ioa config struct
4815  * @int_reg:    interrupt register
4816  *
4817  * Return value:
4818  *      IRQ_NONE / IRQ_HANDLED
4819  **/
4820 static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
4821                                               volatile u32 int_reg)
4822 {
4823         irqreturn_t rc = IRQ_HANDLED;
4824
4825         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
4826                 /* Mask the interrupt */
4827                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
4828
4829                 /* Clear the interrupt */
4830                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
4831                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4832
4833                 list_del(&ioa_cfg->reset_cmd->queue);
4834                 del_timer(&ioa_cfg->reset_cmd->timer);
4835                 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
4836         } else {
4837                 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
4838                         ioa_cfg->ioa_unit_checked = 1;
4839                 else
4840                         dev_err(&ioa_cfg->pdev->dev,
4841                                 "Permanent IOA failure. 0x%08X\n", int_reg);
4842
4843                 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4844                         ioa_cfg->sdt_state = GET_DUMP;
4845
4846                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
4847                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4848         }
4849
4850         return rc;
4851 }
4852
4853 /**
4854  * ipr_isr_eh - Interrupt service routine error handler
4855  * @ioa_cfg:    ioa config struct
4856  * @msg:        message to log
4857  *
4858  * Return value:
4859  *      none
4860  **/
4861 static void ipr_isr_eh(struct ipr_ioa_cfg *ioa_cfg, char *msg)
4862 {
4863         ioa_cfg->errors_logged++;
4864         dev_err(&ioa_cfg->pdev->dev, "%s\n", msg);
4865
4866         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4867                 ioa_cfg->sdt_state = GET_DUMP;
4868
4869         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4870 }
4871
4872 /**
4873  * ipr_isr - Interrupt service routine
4874  * @irq:        irq number
4875  * @devp:       pointer to ioa config struct
4876  *
4877  * Return value:
4878  *      IRQ_NONE / IRQ_HANDLED
4879  **/
4880 static irqreturn_t ipr_isr(int irq, void *devp)
4881 {
4882         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
4883         unsigned long lock_flags = 0;
4884         volatile u32 int_reg, int_mask_reg;
4885         u32 ioasc;
4886         u16 cmd_index;
4887         int num_hrrq = 0;
4888         struct ipr_cmnd *ipr_cmd;
4889         irqreturn_t rc = IRQ_NONE;
4890
4891         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4892
4893         /* If interrupts are disabled, ignore the interrupt */
4894         if (!ioa_cfg->allow_interrupts) {
4895                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4896                 return IRQ_NONE;
4897         }
4898
4899         int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
4900         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32) & ~int_mask_reg;
4901
4902         /* If an interrupt on the adapter did not occur, ignore it.
4903          * Or in the case of SIS 64, check for a stage change interrupt.
4904          */
4905         if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
4906                 if (ioa_cfg->sis64) {
4907                         int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4908                         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
4909                         if (int_reg & IPR_PCII_IPL_STAGE_CHANGE) {
4910
4911                                 /* clear stage change */
4912                                 writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.clr_interrupt_reg);
4913                                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
4914                                 list_del(&ioa_cfg->reset_cmd->queue);
4915                                 del_timer(&ioa_cfg->reset_cmd->timer);
4916                                 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
4917                                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4918                                 return IRQ_HANDLED;
4919                         }
4920                 }
4921
4922                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4923                 return IRQ_NONE;
4924         }
4925
4926         while (1) {
4927                 ipr_cmd = NULL;
4928
4929                 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
4930                        ioa_cfg->toggle_bit) {
4931
4932                         cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
4933                                      IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
4934
4935                         if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
4936                                 ipr_isr_eh(ioa_cfg, "Invalid response handle from IOA");
4937                                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4938                                 return IRQ_HANDLED;
4939                         }
4940
4941                         ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
4942
4943                         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4944
4945                         ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
4946
4947                         list_del(&ipr_cmd->queue);
4948                         del_timer(&ipr_cmd->timer);
4949                         ipr_cmd->done(ipr_cmd);
4950
4951                         rc = IRQ_HANDLED;
4952
4953                         if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
4954                                 ioa_cfg->hrrq_curr++;
4955                         } else {
4956                                 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
4957                                 ioa_cfg->toggle_bit ^= 1u;
4958                         }
4959                 }
4960
4961                 if (ipr_cmd != NULL) {
4962                         /* Clear the PCI interrupt */
4963                         do {
4964                                 writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg32);
4965                                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32) & ~int_mask_reg;
4966                         } while (int_reg & IPR_PCII_HRRQ_UPDATED &&
4967                                         num_hrrq++ < IPR_MAX_HRRQ_RETRIES);
4968
4969                         if (int_reg & IPR_PCII_HRRQ_UPDATED) {
4970                                 ipr_isr_eh(ioa_cfg, "Error clearing HRRQ");
4971                                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4972                                 return IRQ_HANDLED;
4973                         }
4974
4975                 } else
4976                         break;
4977         }
4978
4979         if (unlikely(rc == IRQ_NONE))
4980                 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
4981
4982         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4983         return rc;
4984 }
4985
4986 /**
4987  * ipr_build_ioadl64 - Build a scatter/gather list and map the buffer
4988  * @ioa_cfg:    ioa config struct
4989  * @ipr_cmd:    ipr command struct
4990  *
4991  * Return value:
4992  *      0 on success / -1 on failure
4993  **/
4994 static int ipr_build_ioadl64(struct ipr_ioa_cfg *ioa_cfg,
4995                              struct ipr_cmnd *ipr_cmd)
4996 {
4997         int i, nseg;
4998         struct scatterlist *sg;
4999         u32 length;
5000         u32 ioadl_flags = 0;
5001         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5002         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5003         struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
5004
5005         length = scsi_bufflen(scsi_cmd);
5006         if (!length)
5007                 return 0;
5008
5009         nseg = scsi_dma_map(scsi_cmd);
5010         if (nseg < 0) {
5011                 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
5012                 return -1;
5013         }
5014
5015         ipr_cmd->dma_use_sg = nseg;
5016
5017         if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
5018                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5019                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5020         } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE)
5021                 ioadl_flags = IPR_IOADL_FLAGS_READ;
5022
5023         scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
5024                 ioadl64[i].flags = cpu_to_be32(ioadl_flags);
5025                 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(sg));
5026                 ioadl64[i].address = cpu_to_be64(sg_dma_address(sg));
5027         }
5028
5029         ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5030         return 0;
5031 }
5032
5033 /**
5034  * ipr_build_ioadl - Build a scatter/gather list and map the buffer
5035  * @ioa_cfg:    ioa config struct
5036  * @ipr_cmd:    ipr command struct
5037  *
5038  * Return value:
5039  *      0 on success / -1 on failure
5040  **/
5041 static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
5042                            struct ipr_cmnd *ipr_cmd)
5043 {
5044         int i, nseg;
5045         struct scatterlist *sg;
5046         u32 length;
5047         u32 ioadl_flags = 0;
5048         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5049         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5050         struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
5051
5052         length = scsi_bufflen(scsi_cmd);
5053         if (!length)
5054                 return 0;
5055
5056         nseg = scsi_dma_map(scsi_cmd);
5057         if (nseg < 0) {
5058                 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
5059                 return -1;
5060         }
5061
5062         ipr_cmd->dma_use_sg = nseg;
5063
5064         if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
5065                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5066                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5067                 ioarcb->data_transfer_length = cpu_to_be32(length);
5068                 ioarcb->ioadl_len =
5069                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5070         } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
5071                 ioadl_flags = IPR_IOADL_FLAGS_READ;
5072                 ioarcb->read_data_transfer_length = cpu_to_be32(length);
5073                 ioarcb->read_ioadl_len =
5074                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5075         }
5076
5077         if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->u.add_data.u.ioadl)) {
5078                 ioadl = ioarcb->u.add_data.u.ioadl;
5079                 ioarcb->write_ioadl_addr = cpu_to_be32((ipr_cmd->dma_addr) +
5080                                     offsetof(struct ipr_ioarcb, u.add_data));
5081                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5082         }
5083
5084         scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
5085                 ioadl[i].flags_and_data_len =
5086                         cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5087                 ioadl[i].address = cpu_to_be32(sg_dma_address(sg));
5088         }
5089
5090         ioadl[i-1].flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5091         return 0;
5092 }
5093
5094 /**
5095  * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
5096  * @scsi_cmd:   scsi command struct
5097  *
5098  * Return value:
5099  *      task attributes
5100  **/
5101 static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
5102 {
5103         u8 tag[2];
5104         u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
5105
5106         if (scsi_populate_tag_msg(scsi_cmd, tag)) {
5107                 switch (tag[0]) {
5108                 case MSG_SIMPLE_TAG:
5109                         rc = IPR_FLAGS_LO_SIMPLE_TASK;
5110                         break;
5111                 case MSG_HEAD_TAG:
5112                         rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
5113                         break;
5114                 case MSG_ORDERED_TAG:
5115                         rc = IPR_FLAGS_LO_ORDERED_TASK;
5116                         break;
5117                 };
5118         }
5119
5120         return rc;
5121 }
5122
5123 /**
5124  * ipr_erp_done - Process completion of ERP for a device
5125  * @ipr_cmd:            ipr command struct
5126  *
5127  * This function copies the sense buffer into the scsi_cmd
5128  * struct and pushes the scsi_done function.
5129  *
5130  * Return value:
5131  *      nothing
5132  **/
5133 static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
5134 {
5135         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5136         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5137         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5138         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5139
5140         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
5141                 scsi_cmd->result |= (DID_ERROR << 16);
5142                 scmd_printk(KERN_ERR, scsi_cmd,
5143                             "Request Sense failed with IOASC: 0x%08X\n", ioasc);
5144         } else {
5145                 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
5146                        SCSI_SENSE_BUFFERSIZE);
5147         }
5148
5149         if (res) {
5150                 if (!ipr_is_naca_model(res))
5151                         res->needs_sync_complete = 1;
5152                 res->in_erp = 0;
5153         }
5154         scsi_dma_unmap(ipr_cmd->scsi_cmd);
5155         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5156         scsi_cmd->scsi_done(scsi_cmd);
5157 }
5158
5159 /**
5160  * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
5161  * @ipr_cmd:    ipr command struct
5162  *
5163  * Return value:
5164  *      none
5165  **/
5166 static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
5167 {
5168         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5169         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
5170         dma_addr_t dma_addr = ipr_cmd->dma_addr;
5171
5172         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
5173         ioarcb->data_transfer_length = 0;
5174         ioarcb->read_data_transfer_length = 0;
5175         ioarcb->ioadl_len = 0;
5176         ioarcb->read_ioadl_len = 0;
5177         ioasa->ioasc = 0;
5178         ioasa->residual_data_len = 0;
5179
5180         if (ipr_cmd->ioa_cfg->sis64)
5181                 ioarcb->u.sis64_addr_data.data_ioadl_addr =
5182                         cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
5183         else {
5184                 ioarcb->write_ioadl_addr =
5185                         cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
5186                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5187         }
5188 }
5189
5190 /**
5191  * ipr_erp_request_sense - Send request sense to a device
5192  * @ipr_cmd:    ipr command struct
5193  *
5194  * This function sends a request sense to a device as a result
5195  * of a check condition.
5196  *
5197  * Return value:
5198  *      nothing
5199  **/
5200 static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
5201 {
5202         struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5203         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5204
5205         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
5206                 ipr_erp_done(ipr_cmd);
5207                 return;
5208         }
5209
5210         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
5211
5212         cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
5213         cmd_pkt->cdb[0] = REQUEST_SENSE;
5214         cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
5215         cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
5216         cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5217         cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
5218
5219         ipr_init_ioadl(ipr_cmd, ipr_cmd->sense_buffer_dma,
5220                        SCSI_SENSE_BUFFERSIZE, IPR_IOADL_FLAGS_READ_LAST);
5221
5222         ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
5223                    IPR_REQUEST_SENSE_TIMEOUT * 2);
5224 }
5225
5226 /**
5227  * ipr_erp_cancel_all - Send cancel all to a device
5228  * @ipr_cmd:    ipr command struct
5229  *
5230  * This function sends a cancel all to a device to clear the
5231  * queue. If we are running TCQ on the device, QERR is set to 1,
5232  * which means all outstanding ops have been dropped on the floor.
5233  * Cancel all will return them to us.
5234  *
5235  * Return value:
5236  *      nothing
5237  **/
5238 static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
5239 {
5240         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5241         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5242         struct ipr_cmd_pkt *cmd_pkt;
5243
5244         res->in_erp = 1;
5245
5246         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
5247
5248         if (!scsi_get_tag_type(scsi_cmd->device)) {
5249                 ipr_erp_request_sense(ipr_cmd);
5250                 return;
5251         }
5252
5253         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5254         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5255         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
5256
5257         ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
5258                    IPR_CANCEL_ALL_TIMEOUT);
5259 }
5260
5261 /**
5262  * ipr_dump_ioasa - Dump contents of IOASA
5263  * @ioa_cfg:    ioa config struct
5264  * @ipr_cmd:    ipr command struct
5265  * @res:                resource entry struct
5266  *
5267  * This function is invoked by the interrupt handler when ops
5268  * fail. It will log the IOASA if appropriate. Only called
5269  * for GPDD ops.
5270  *
5271  * Return value:
5272  *      none
5273  **/
5274 static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
5275                            struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
5276 {
5277         int i;
5278         u16 data_len;
5279         u32 ioasc, fd_ioasc;
5280         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
5281         __be32 *ioasa_data = (__be32 *)ioasa;
5282         int error_index;
5283
5284         ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
5285         fd_ioasc = be32_to_cpu(ioasa->fd_ioasc) & IPR_IOASC_IOASC_MASK;
5286
5287         if (0 == ioasc)
5288                 return;
5289
5290         if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
5291                 return;
5292
5293         if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
5294                 error_index = ipr_get_error(fd_ioasc);
5295         else
5296                 error_index = ipr_get_error(ioasc);
5297
5298         if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
5299                 /* Don't log an error if the IOA already logged one */
5300                 if (ioasa->ilid != 0)
5301                         return;
5302
5303                 if (!ipr_is_gscsi(res))
5304                         return;
5305
5306                 if (ipr_error_table[error_index].log_ioasa == 0)
5307                         return;
5308         }
5309
5310         ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
5311
5312         if (sizeof(struct ipr_ioasa) < be16_to_cpu(ioasa->ret_stat_len))
5313                 data_len = sizeof(struct ipr_ioasa);
5314         else
5315                 data_len = be16_to_cpu(ioasa->ret_stat_len);
5316
5317         ipr_err("IOASA Dump:\n");
5318
5319         for (i = 0; i < data_len / 4; i += 4) {
5320                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
5321                         be32_to_cpu(ioasa_data[i]),
5322                         be32_to_cpu(ioasa_data[i+1]),
5323                         be32_to_cpu(ioasa_data[i+2]),
5324                         be32_to_cpu(ioasa_data[i+3]));
5325         }
5326 }
5327
5328 /**
5329  * ipr_gen_sense - Generate SCSI sense data from an IOASA
5330  * @ioasa:              IOASA
5331  * @sense_buf:  sense data buffer
5332  *
5333  * Return value:
5334  *      none
5335  **/
5336 static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
5337 {
5338         u32 failing_lba;
5339         u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
5340         struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
5341         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
5342         u32 ioasc = be32_to_cpu(ioasa->ioasc);
5343
5344         memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
5345
5346         if (ioasc >= IPR_FIRST_DRIVER_IOASC)
5347                 return;
5348
5349         ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
5350
5351         if (ipr_is_vset_device(res) &&
5352             ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
5353             ioasa->u.vset.failing_lba_hi != 0) {
5354                 sense_buf[0] = 0x72;
5355                 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
5356                 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
5357                 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
5358
5359                 sense_buf[7] = 12;
5360                 sense_buf[8] = 0;
5361                 sense_buf[9] = 0x0A;
5362                 sense_buf[10] = 0x80;
5363
5364                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
5365
5366                 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
5367                 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
5368                 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
5369                 sense_buf[15] = failing_lba & 0x000000ff;
5370
5371                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
5372
5373                 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
5374                 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
5375                 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
5376                 sense_buf[19] = failing_lba & 0x000000ff;
5377         } else {
5378                 sense_buf[0] = 0x70;
5379                 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
5380                 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
5381                 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
5382
5383                 /* Illegal request */
5384                 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
5385                     (be32_to_cpu(ioasa->ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
5386                         sense_buf[7] = 10;      /* additional length */
5387
5388                         /* IOARCB was in error */
5389                         if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
5390                                 sense_buf[15] = 0xC0;
5391                         else    /* Parameter data was invalid */
5392                                 sense_buf[15] = 0x80;
5393
5394                         sense_buf[16] =
5395                             ((IPR_FIELD_POINTER_MASK &
5396                               be32_to_cpu(ioasa->ioasc_specific)) >> 8) & 0xff;
5397                         sense_buf[17] =
5398                             (IPR_FIELD_POINTER_MASK &
5399                              be32_to_cpu(ioasa->ioasc_specific)) & 0xff;
5400                 } else {
5401                         if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
5402                                 if (ipr_is_vset_device(res))
5403                                         failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
5404                                 else
5405                                         failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
5406
5407                                 sense_buf[0] |= 0x80;   /* Or in the Valid bit */
5408                                 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
5409                                 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
5410                                 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
5411                                 sense_buf[6] = failing_lba & 0x000000ff;
5412                         }
5413
5414                         sense_buf[7] = 6;       /* additional length */
5415                 }
5416         }
5417 }
5418
5419 /**
5420  * ipr_get_autosense - Copy autosense data to sense buffer
5421  * @ipr_cmd:    ipr command struct
5422  *
5423  * This function copies the autosense buffer to the buffer
5424  * in the scsi_cmd, if there is autosense available.
5425  *
5426  * Return value:
5427  *      1 if autosense was available / 0 if not
5428  **/
5429 static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
5430 {
5431         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
5432
5433         if ((be32_to_cpu(ioasa->ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
5434                 return 0;
5435
5436         memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
5437                min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
5438                    SCSI_SENSE_BUFFERSIZE));
5439         return 1;
5440 }
5441
5442 /**
5443  * ipr_erp_start - Process an error response for a SCSI op
5444  * @ioa_cfg:    ioa config struct
5445  * @ipr_cmd:    ipr command struct
5446  *
5447  * This function determines whether or not to initiate ERP
5448  * on the affected device.
5449  *
5450  * Return value:
5451  *      nothing
5452  **/
5453 static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
5454                               struct ipr_cmnd *ipr_cmd)
5455 {
5456         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5457         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5458         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5459         u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
5460
5461         if (!res) {
5462                 ipr_scsi_eh_done(ipr_cmd);
5463                 return;
5464         }
5465
5466         if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
5467                 ipr_gen_sense(ipr_cmd);
5468
5469         ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
5470
5471         switch (masked_ioasc) {
5472         case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
5473                 if (ipr_is_naca_model(res))
5474                         scsi_cmd->result |= (DID_ABORT << 16);
5475                 else
5476                         scsi_cmd->result |= (DID_IMM_RETRY << 16);
5477                 break;
5478         case IPR_IOASC_IR_RESOURCE_HANDLE:
5479         case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
5480                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
5481                 break;
5482         case IPR_IOASC_HW_SEL_TIMEOUT:
5483                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
5484                 if (!ipr_is_naca_model(res))
5485                         res->needs_sync_complete = 1;
5486                 break;
5487         case IPR_IOASC_SYNC_REQUIRED:
5488                 if (!res->in_erp)
5489                         res->needs_sync_complete = 1;
5490                 scsi_cmd->result |= (DID_IMM_RETRY << 16);
5491                 break;
5492         case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
5493         case IPR_IOASA_IR_DUAL_IOA_DISABLED:
5494                 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
5495                 break;
5496         case IPR_IOASC_BUS_WAS_RESET:
5497         case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
5498                 /*
5499                  * Report the bus reset and ask for a retry. The device
5500                  * will give CC/UA the next command.
5501                  */
5502                 if (!res->resetting_device)
5503                         scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
5504                 scsi_cmd->result |= (DID_ERROR << 16);
5505                 if (!ipr_is_naca_model(res))
5506                         res->needs_sync_complete = 1;
5507                 break;
5508         case IPR_IOASC_HW_DEV_BUS_STATUS:
5509                 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
5510                 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
5511                         if (!ipr_get_autosense(ipr_cmd)) {
5512                                 if (!ipr_is_naca_model(res)) {
5513                                         ipr_erp_cancel_all(ipr_cmd);
5514                                         return;
5515                                 }
5516                         }
5517                 }
5518                 if (!ipr_is_naca_model(res))
5519                         res->needs_sync_complete = 1;
5520                 break;
5521         case IPR_IOASC_NR_INIT_CMD_REQUIRED:
5522                 break;
5523         default:
5524                 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
5525                         scsi_cmd->result |= (DID_ERROR << 16);
5526                 if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
5527                         res->needs_sync_complete = 1;
5528                 break;
5529         }
5530
5531         scsi_dma_unmap(ipr_cmd->scsi_cmd);
5532         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5533         scsi_cmd->scsi_done(scsi_cmd);
5534 }
5535
5536 /**
5537  * ipr_scsi_done - mid-layer done function
5538  * @ipr_cmd:    ipr command struct
5539  *
5540  * This function is invoked by the interrupt handler for
5541  * ops generated by the SCSI mid-layer
5542  *
5543  * Return value:
5544  *      none
5545  **/
5546 static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
5547 {
5548         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5549         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5550         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5551
5552         scsi_set_resid(scsi_cmd, be32_to_cpu(ipr_cmd->ioasa.residual_data_len));
5553
5554         if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
5555                 scsi_dma_unmap(ipr_cmd->scsi_cmd);
5556                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5557                 scsi_cmd->scsi_done(scsi_cmd);
5558         } else
5559                 ipr_erp_start(ioa_cfg, ipr_cmd);
5560 }
5561
5562 /**
5563  * ipr_queuecommand - Queue a mid-layer request
5564  * @scsi_cmd:   scsi command struct
5565  * @done:               done function
5566  *
5567  * This function queues a request generated by the mid-layer.
5568  *
5569  * Return value:
5570  *      0 on success
5571  *      SCSI_MLQUEUE_DEVICE_BUSY if device is busy
5572  *      SCSI_MLQUEUE_HOST_BUSY if host is busy
5573  **/
5574 static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
5575                             void (*done) (struct scsi_cmnd *))
5576 {
5577         struct ipr_ioa_cfg *ioa_cfg;
5578         struct ipr_resource_entry *res;
5579         struct ipr_ioarcb *ioarcb;
5580         struct ipr_cmnd *ipr_cmd;
5581         int rc = 0;
5582
5583         scsi_cmd->scsi_done = done;
5584         ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
5585         res = scsi_cmd->device->hostdata;
5586         scsi_cmd->result = (DID_OK << 16);
5587
5588         /*
5589          * We are currently blocking all devices due to a host reset
5590          * We have told the host to stop giving us new requests, but
5591          * ERP ops don't count. FIXME
5592          */
5593         if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
5594                 return SCSI_MLQUEUE_HOST_BUSY;
5595
5596         /*
5597          * FIXME - Create scsi_set_host_offline interface
5598          *  and the ioa_is_dead check can be removed
5599          */
5600         if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
5601                 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
5602                 scsi_cmd->result = (DID_NO_CONNECT << 16);
5603                 scsi_cmd->scsi_done(scsi_cmd);
5604                 return 0;
5605         }
5606
5607         if (ipr_is_gata(res) && res->sata_port)
5608                 return ata_sas_queuecmd(scsi_cmd, done, res->sata_port->ap);
5609
5610         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5611         ioarcb = &ipr_cmd->ioarcb;
5612         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
5613
5614         memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
5615         ipr_cmd->scsi_cmd = scsi_cmd;
5616         ioarcb->res_handle = res->res_handle;
5617         ipr_cmd->done = ipr_scsi_done;
5618         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
5619
5620         if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
5621                 if (scsi_cmd->underflow == 0)
5622                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5623
5624                 if (res->needs_sync_complete) {
5625                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
5626                         res->needs_sync_complete = 0;
5627                 }
5628
5629                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
5630                 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
5631                 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
5632                 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
5633         }
5634
5635         if (scsi_cmd->cmnd[0] >= 0xC0 &&
5636             (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
5637                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5638
5639         if (likely(rc == 0)) {
5640                 if (ioa_cfg->sis64)
5641                         rc = ipr_build_ioadl64(ioa_cfg, ipr_cmd);
5642                 else
5643                         rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
5644         }
5645
5646         if (likely(rc == 0)) {
5647                 mb();
5648                 ipr_send_command(ipr_cmd);
5649         } else {
5650                  list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5651                  return SCSI_MLQUEUE_HOST_BUSY;
5652         }
5653
5654         return 0;
5655 }
5656
5657 /**
5658  * ipr_ioctl - IOCTL handler
5659  * @sdev:       scsi device struct
5660  * @cmd:        IOCTL cmd
5661  * @arg:        IOCTL arg
5662  *
5663  * Return value:
5664  *      0 on success / other on failure
5665  **/
5666 static int ipr_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
5667 {
5668         struct ipr_resource_entry *res;
5669
5670         res = (struct ipr_resource_entry *)sdev->hostdata;
5671         if (res && ipr_is_gata(res)) {
5672                 if (cmd == HDIO_GET_IDENTITY)
5673                         return -ENOTTY;
5674                 return ata_sas_scsi_ioctl(res->sata_port->ap, sdev, cmd, arg);
5675         }
5676
5677         return -EINVAL;
5678 }
5679
5680 /**
5681  * ipr_info - Get information about the card/driver
5682  * @scsi_host:  scsi host struct
5683  *
5684  * Return value:
5685  *      pointer to buffer with description string
5686  **/
5687 static const char * ipr_ioa_info(struct Scsi_Host *host)
5688 {
5689         static char buffer[512];
5690         struct ipr_ioa_cfg *ioa_cfg;
5691         unsigned long lock_flags = 0;
5692
5693         ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
5694
5695         spin_lock_irqsave(host->host_lock, lock_flags);
5696         sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
5697         spin_unlock_irqrestore(host->host_lock, lock_flags);
5698
5699         return buffer;
5700 }
5701
5702 static struct scsi_host_template driver_template = {
5703         .module = THIS_MODULE,
5704         .name = "IPR",
5705         .info = ipr_ioa_info,
5706         .ioctl = ipr_ioctl,
5707         .queuecommand = ipr_queuecommand,
5708         .eh_abort_handler = ipr_eh_abort,
5709         .eh_device_reset_handler = ipr_eh_dev_reset,
5710         .eh_host_reset_handler = ipr_eh_host_reset,
5711         .slave_alloc = ipr_slave_alloc,
5712         .slave_configure = ipr_slave_configure,
5713         .slave_destroy = ipr_slave_destroy,
5714         .target_alloc = ipr_target_alloc,
5715         .target_destroy = ipr_target_destroy,
5716         .change_queue_depth = ipr_change_queue_depth,
5717         .change_queue_type = ipr_change_queue_type,
5718         .bios_param = ipr_biosparam,
5719         .can_queue = IPR_MAX_COMMANDS,
5720         .this_id = -1,
5721         .sg_tablesize = IPR_MAX_SGLIST,
5722         .max_sectors = IPR_IOA_MAX_SECTORS,
5723         .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
5724         .use_clustering = ENABLE_CLUSTERING,
5725         .shost_attrs = ipr_ioa_attrs,
5726         .sdev_attrs = ipr_dev_attrs,
5727         .proc_name = IPR_NAME
5728 };
5729
5730 /**
5731  * ipr_ata_phy_reset - libata phy_reset handler
5732  * @ap:         ata port to reset
5733  *
5734  **/
5735 static void ipr_ata_phy_reset(struct ata_port *ap)
5736 {
5737         unsigned long flags;
5738         struct ipr_sata_port *sata_port = ap->private_data;
5739         struct ipr_resource_entry *res = sata_port->res;
5740         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5741         int rc;
5742
5743         ENTER;
5744         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5745         while(ioa_cfg->in_reset_reload) {
5746                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5747                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5748                 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5749         }
5750
5751         if (!ioa_cfg->allow_cmds)
5752                 goto out_unlock;
5753
5754         rc = ipr_device_reset(ioa_cfg, res);
5755
5756         if (rc) {
5757                 ap->link.device[0].class = ATA_DEV_NONE;
5758                 goto out_unlock;
5759         }
5760
5761         ap->link.device[0].class = res->ata_class;
5762         if (ap->link.device[0].class == ATA_DEV_UNKNOWN)
5763                 ap->link.device[0].class = ATA_DEV_NONE;
5764
5765 out_unlock:
5766         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5767         LEAVE;
5768 }
5769
5770 /**
5771  * ipr_ata_post_internal - Cleanup after an internal command
5772  * @qc: ATA queued command
5773  *
5774  * Return value:
5775  *      none
5776  **/
5777 static void ipr_ata_post_internal(struct ata_queued_cmd *qc)
5778 {
5779         struct ipr_sata_port *sata_port = qc->ap->private_data;
5780         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5781         struct ipr_cmnd *ipr_cmd;
5782         unsigned long flags;
5783
5784         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5785         while(ioa_cfg->in_reset_reload) {
5786                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5787                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5788                 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5789         }
5790
5791         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
5792                 if (ipr_cmd->qc == qc) {
5793                         ipr_device_reset(ioa_cfg, sata_port->res);
5794                         break;
5795                 }
5796         }
5797         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5798 }
5799
5800 /**
5801  * ipr_copy_sata_tf - Copy a SATA taskfile to an IOA data structure
5802  * @regs:       destination
5803  * @tf: source ATA taskfile
5804  *
5805  * Return value:
5806  *      none
5807  **/
5808 static void ipr_copy_sata_tf(struct ipr_ioarcb_ata_regs *regs,
5809                              struct ata_taskfile *tf)
5810 {
5811         regs->feature = tf->feature;
5812         regs->nsect = tf->nsect;
5813         regs->lbal = tf->lbal;
5814         regs->lbam = tf->lbam;
5815         regs->lbah = tf->lbah;
5816         regs->device = tf->device;
5817         regs->command = tf->command;
5818         regs->hob_feature = tf->hob_feature;
5819         regs->hob_nsect = tf->hob_nsect;
5820         regs->hob_lbal = tf->hob_lbal;
5821         regs->hob_lbam = tf->hob_lbam;
5822         regs->hob_lbah = tf->hob_lbah;
5823         regs->ctl = tf->ctl;
5824 }
5825
5826 /**
5827  * ipr_sata_done - done function for SATA commands
5828  * @ipr_cmd:    ipr command struct
5829  *
5830  * This function is invoked by the interrupt handler for
5831  * ops generated by the SCSI mid-layer to SATA devices
5832  *
5833  * Return value:
5834  *      none
5835  **/
5836 static void ipr_sata_done(struct ipr_cmnd *ipr_cmd)
5837 {
5838         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5839         struct ata_queued_cmd *qc = ipr_cmd->qc;
5840         struct ipr_sata_port *sata_port = qc->ap->private_data;
5841         struct ipr_resource_entry *res = sata_port->res;
5842         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5843
5844         memcpy(&sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
5845                sizeof(struct ipr_ioasa_gata));
5846         ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
5847
5848         if (be32_to_cpu(ipr_cmd->ioasa.ioasc_specific) & IPR_ATA_DEVICE_WAS_RESET)
5849                 scsi_report_device_reset(ioa_cfg->host, res->bus, res->target);
5850
5851         if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
5852                 qc->err_mask |= __ac_err_mask(ipr_cmd->ioasa.u.gata.status);
5853         else
5854                 qc->err_mask |= ac_err_mask(ipr_cmd->ioasa.u.gata.status);
5855         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5856         ata_qc_complete(qc);
5857 }
5858
5859 /**
5860  * ipr_build_ata_ioadl64 - Build an ATA scatter/gather list
5861  * @ipr_cmd:    ipr command struct
5862  * @qc:         ATA queued command
5863  *
5864  **/
5865 static void ipr_build_ata_ioadl64(struct ipr_cmnd *ipr_cmd,
5866                                   struct ata_queued_cmd *qc)
5867 {
5868         u32 ioadl_flags = 0;
5869         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5870         struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
5871         struct ipr_ioadl64_desc *last_ioadl64 = NULL;
5872         int len = qc->nbytes;
5873         struct scatterlist *sg;
5874         unsigned int si;
5875         dma_addr_t dma_addr = ipr_cmd->dma_addr;
5876
5877         if (len == 0)
5878                 return;
5879
5880         if (qc->dma_dir == DMA_TO_DEVICE) {
5881                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5882                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5883         } else if (qc->dma_dir == DMA_FROM_DEVICE)
5884                 ioadl_flags = IPR_IOADL_FLAGS_READ;
5885
5886         ioarcb->data_transfer_length = cpu_to_be32(len);
5887         ioarcb->ioadl_len =
5888                 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
5889         ioarcb->u.sis64_addr_data.data_ioadl_addr =
5890                 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ata_ioadl));
5891
5892         for_each_sg(qc->sg, sg, qc->n_elem, si) {
5893                 ioadl64->flags = cpu_to_be32(ioadl_flags);
5894                 ioadl64->data_len = cpu_to_be32(sg_dma_len(sg));
5895                 ioadl64->address = cpu_to_be64(sg_dma_address(sg));
5896
5897                 last_ioadl64 = ioadl64;
5898                 ioadl64++;
5899         }
5900
5901         if (likely(last_ioadl64))
5902                 last_ioadl64->flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5903 }
5904
5905 /**
5906  * ipr_build_ata_ioadl - Build an ATA scatter/gather list
5907  * @ipr_cmd:    ipr command struct
5908  * @qc:         ATA queued command
5909  *
5910  **/
5911 static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
5912                                 struct ata_queued_cmd *qc)
5913 {
5914         u32 ioadl_flags = 0;
5915         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5916         struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
5917         struct ipr_ioadl_desc *last_ioadl = NULL;
5918         int len = qc->nbytes;
5919         struct scatterlist *sg;
5920         unsigned int si;
5921
5922         if (len == 0)
5923                 return;
5924
5925         if (qc->dma_dir == DMA_TO_DEVICE) {
5926                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5927                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5928                 ioarcb->data_transfer_length = cpu_to_be32(len);
5929                 ioarcb->ioadl_len =
5930                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5931         } else if (qc->dma_dir == DMA_FROM_DEVICE) {
5932                 ioadl_flags = IPR_IOADL_FLAGS_READ;
5933                 ioarcb->read_data_transfer_length = cpu_to_be32(len);
5934                 ioarcb->read_ioadl_len =
5935                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5936         }
5937
5938         for_each_sg(qc->sg, sg, qc->n_elem, si) {
5939                 ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5940                 ioadl->address = cpu_to_be32(sg_dma_address(sg));
5941
5942                 last_ioadl = ioadl;
5943                 ioadl++;
5944         }
5945
5946         if (likely(last_ioadl))
5947                 last_ioadl->flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5948 }
5949
5950 /**
5951  * ipr_qc_issue - Issue a SATA qc to a device
5952  * @qc: queued command
5953  *
5954  * Return value:
5955  *      0 if success
5956  **/
5957 static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
5958 {
5959         struct ata_port *ap = qc->ap;
5960         struct ipr_sata_port *sata_port = ap->private_data;
5961         struct ipr_resource_entry *res = sata_port->res;
5962         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5963         struct ipr_cmnd *ipr_cmd;
5964         struct ipr_ioarcb *ioarcb;
5965         struct ipr_ioarcb_ata_regs *regs;
5966
5967         if (unlikely(!ioa_cfg->allow_cmds || ioa_cfg->ioa_is_dead))
5968                 return AC_ERR_SYSTEM;
5969
5970         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5971         ioarcb = &ipr_cmd->ioarcb;
5972
5973         if (ioa_cfg->sis64) {
5974                 regs = &ipr_cmd->i.ata_ioadl.regs;
5975                 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
5976         } else
5977                 regs = &ioarcb->u.add_data.u.regs;
5978
5979         memset(regs, 0, sizeof(*regs));
5980         ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(*regs));
5981
5982         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
5983         ipr_cmd->qc = qc;
5984         ipr_cmd->done = ipr_sata_done;
5985         ipr_cmd->ioarcb.res_handle = res->res_handle;
5986         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_ATA_PASSTHRU;
5987         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
5988         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5989         ipr_cmd->dma_use_sg = qc->n_elem;
5990
5991         if (ioa_cfg->sis64)
5992                 ipr_build_ata_ioadl64(ipr_cmd, qc);
5993         else
5994                 ipr_build_ata_ioadl(ipr_cmd, qc);
5995
5996         regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
5997         ipr_copy_sata_tf(regs, &qc->tf);
5998         memcpy(ioarcb->cmd_pkt.cdb, qc->cdb, IPR_MAX_CDB_LEN);
5999         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
6000
6001         switch (qc->tf.protocol) {
6002         case ATA_PROT_NODATA:
6003         case ATA_PROT_PIO:
6004                 break;
6005
6006         case ATA_PROT_DMA:
6007                 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
6008                 break;
6009
6010         case ATAPI_PROT_PIO:
6011         case ATAPI_PROT_NODATA:
6012                 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
6013                 break;
6014
6015         case ATAPI_PROT_DMA:
6016                 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
6017                 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
6018                 break;
6019
6020         default:
6021                 WARN_ON(1);
6022                 return AC_ERR_INVALID;
6023         }
6024
6025         mb();
6026
6027         ipr_send_command(ipr_cmd);
6028
6029         return 0;
6030 }
6031
6032 /**
6033  * ipr_qc_fill_rtf - Read result TF
6034  * @qc: ATA queued command
6035  *
6036  * Return value:
6037  *      true
6038  **/
6039 static bool ipr_qc_fill_rtf(struct ata_queued_cmd *qc)
6040 {
6041         struct ipr_sata_port *sata_port = qc->ap->private_data;
6042         struct ipr_ioasa_gata *g = &sata_port->ioasa;
6043         struct ata_taskfile *tf = &qc->result_tf;
6044
6045         tf->feature = g->error;
6046         tf->nsect = g->nsect;
6047         tf->lbal = g->lbal;
6048         tf->lbam = g->lbam;
6049         tf->lbah = g->lbah;
6050         tf->device = g->device;
6051         tf->command = g->status;
6052         tf->hob_nsect = g->hob_nsect;
6053         tf->hob_lbal = g->hob_lbal;
6054         tf->hob_lbam = g->hob_lbam;
6055         tf->hob_lbah = g->hob_lbah;
6056         tf->ctl = g->alt_status;
6057
6058         return true;
6059 }
6060
6061 static struct ata_port_operations ipr_sata_ops = {
6062         .phy_reset = ipr_ata_phy_reset,
6063         .hardreset = ipr_sata_reset,
6064         .post_internal_cmd = ipr_ata_post_internal,
6065         .qc_prep = ata_noop_qc_prep,
6066         .qc_issue = ipr_qc_issue,
6067         .qc_fill_rtf = ipr_qc_fill_rtf,
6068         .port_start = ata_sas_port_start,
6069         .port_stop = ata_sas_port_stop
6070 };
6071
6072 static struct ata_port_info sata_port_info = {
6073         .flags  = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
6074         ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
6075         .pio_mask       = 0x10, /* pio4 */
6076         .mwdma_mask = 0x07,
6077         .udma_mask      = 0x7f, /* udma0-6 */
6078         .port_ops       = &ipr_sata_ops
6079 };
6080
6081 #ifdef CONFIG_PPC_PSERIES
6082 static const u16 ipr_blocked_processors[] = {
6083         PV_NORTHSTAR,
6084         PV_PULSAR,
6085         PV_POWER4,
6086         PV_ICESTAR,
6087         PV_SSTAR,
6088         PV_POWER4p,
6089         PV_630,
6090         PV_630p
6091 };
6092
6093 /**
6094  * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
6095  * @ioa_cfg:    ioa cfg struct
6096  *
6097  * Adapters that use Gemstone revision < 3.1 do not work reliably on
6098  * certain pSeries hardware. This function determines if the given
6099  * adapter is in one of these confgurations or not.
6100  *
6101  * Return value:
6102  *      1 if adapter is not supported / 0 if adapter is supported
6103  **/
6104 static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
6105 {
6106         int i;
6107
6108         if ((ioa_cfg->type == 0x5702) && (ioa_cfg->pdev->revision < 4)) {
6109                 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
6110                         if (__is_processor(ipr_blocked_processors[i]))
6111                                 return 1;
6112                 }
6113         }
6114         return 0;
6115 }
6116 #else
6117 #define ipr_invalid_adapter(ioa_cfg) 0
6118 #endif
6119
6120 /**
6121  * ipr_ioa_bringdown_done - IOA bring down completion.
6122  * @ipr_cmd:    ipr command struct
6123  *
6124  * This function processes the completion of an adapter bring down.
6125  * It wakes any reset sleepers.
6126  *
6127  * Return value:
6128  *      IPR_RC_JOB_RETURN
6129  **/
6130 static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
6131 {
6132         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6133
6134         ENTER;
6135         ioa_cfg->in_reset_reload = 0;
6136         ioa_cfg->reset_retries = 0;
6137         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6138         wake_up_all(&ioa_cfg->reset_wait_q);
6139
6140         spin_unlock_irq(ioa_cfg->host->host_lock);
6141         scsi_unblock_requests(ioa_cfg->host);
6142         spin_lock_irq(ioa_cfg->host->host_lock);
6143         LEAVE;
6144
6145         return IPR_RC_JOB_RETURN;
6146 }
6147
6148 /**
6149  * ipr_ioa_reset_done - IOA reset completion.
6150  * @ipr_cmd:    ipr command struct
6151  *
6152  * This function processes the completion of an adapter reset.
6153  * It schedules any necessary mid-layer add/removes and
6154  * wakes any reset sleepers.
6155  *
6156  * Return value:
6157  *      IPR_RC_JOB_RETURN
6158  **/
6159 static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
6160 {
6161         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6162         struct ipr_resource_entry *res;
6163         struct ipr_hostrcb *hostrcb, *temp;
6164         int i = 0;
6165
6166         ENTER;
6167         ioa_cfg->in_reset_reload = 0;
6168         ioa_cfg->allow_cmds = 1;
6169         ioa_cfg->reset_cmd = NULL;
6170         ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
6171
6172         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
6173                 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
6174                         ipr_trace;
6175                         break;
6176                 }
6177         }
6178         schedule_work(&ioa_cfg->work_q);
6179
6180         list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
6181                 list_del(&hostrcb->queue);
6182                 if (i++ < IPR_NUM_LOG_HCAMS)
6183                         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
6184                 else
6185                         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
6186         }
6187
6188         scsi_report_bus_reset(ioa_cfg->host, IPR_VSET_BUS);
6189         dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
6190
6191         ioa_cfg->reset_retries = 0;
6192         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6193         wake_up_all(&ioa_cfg->reset_wait_q);
6194
6195         spin_unlock(ioa_cfg->host->host_lock);
6196         scsi_unblock_requests(ioa_cfg->host);
6197         spin_lock(ioa_cfg->host->host_lock);
6198
6199         if (!ioa_cfg->allow_cmds)
6200                 scsi_block_requests(ioa_cfg->host);
6201
6202         LEAVE;
6203         return IPR_RC_JOB_RETURN;
6204 }
6205
6206 /**
6207  * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
6208  * @supported_dev:      supported device struct
6209  * @vpids:                      vendor product id struct
6210  *
6211  * Return value:
6212  *      none
6213  **/
6214 static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
6215                                  struct ipr_std_inq_vpids *vpids)
6216 {
6217         memset(supported_dev, 0, sizeof(struct ipr_supported_device));
6218         memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
6219         supported_dev->num_records = 1;
6220         supported_dev->data_length =
6221                 cpu_to_be16(sizeof(struct ipr_supported_device));
6222         supported_dev->reserved = 0;
6223 }
6224
6225 /**
6226  * ipr_set_supported_devs - Send Set Supported Devices for a device
6227  * @ipr_cmd:    ipr command struct
6228  *
6229  * This function sends a Set Supported Devices to the adapter
6230  *
6231  * Return value:
6232  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6233  **/
6234 static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
6235 {
6236         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6237         struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
6238         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6239         struct ipr_resource_entry *res = ipr_cmd->u.res;
6240
6241         ipr_cmd->job_step = ipr_ioa_reset_done;
6242
6243         list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
6244                 if (!ipr_is_scsi_disk(res))
6245                         continue;
6246
6247                 ipr_cmd->u.res = res;
6248                 ipr_set_sup_dev_dflt(supp_dev, &res->std_inq_data.vpids);
6249
6250                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6251                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6252                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6253
6254                 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
6255                 ioarcb->cmd_pkt.cdb[1] = IPR_SET_ALL_SUPPORTED_DEVICES;
6256                 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
6257                 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
6258
6259                 ipr_init_ioadl(ipr_cmd,
6260                                ioa_cfg->vpd_cbs_dma +
6261                                  offsetof(struct ipr_misc_cbs, supp_dev),
6262                                sizeof(struct ipr_supported_device),
6263                                IPR_IOADL_FLAGS_WRITE_LAST);
6264
6265                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
6266                            IPR_SET_SUP_DEVICE_TIMEOUT);
6267
6268                 if (!ioa_cfg->sis64)
6269                         ipr_cmd->job_step = ipr_set_supported_devs;
6270                 return IPR_RC_JOB_RETURN;
6271         }
6272
6273         return IPR_RC_JOB_CONTINUE;
6274 }
6275
6276 /**
6277  * ipr_get_mode_page - Locate specified mode page
6278  * @mode_pages: mode page buffer
6279  * @page_code:  page code to find
6280  * @len:                minimum required length for mode page
6281  *
6282  * Return value:
6283  *      pointer to mode page / NULL on failure
6284  **/
6285 static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
6286                                u32 page_code, u32 len)
6287 {
6288         struct ipr_mode_page_hdr *mode_hdr;
6289         u32 page_length;
6290         u32 length;
6291
6292         if (!mode_pages || (mode_pages->hdr.length == 0))
6293                 return NULL;
6294
6295         length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
6296         mode_hdr = (struct ipr_mode_page_hdr *)
6297                 (mode_pages->data + mode_pages->hdr.block_desc_len);
6298
6299         while (length) {
6300                 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
6301                         if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
6302                                 return mode_hdr;
6303                         break;
6304                 } else {
6305                         page_length = (sizeof(struct ipr_mode_page_hdr) +
6306                                        mode_hdr->page_length);
6307                         length -= page_length;
6308                         mode_hdr = (struct ipr_mode_page_hdr *)
6309                                 ((unsigned long)mode_hdr + page_length);
6310                 }
6311         }
6312         return NULL;
6313 }
6314
6315 /**
6316  * ipr_check_term_power - Check for term power errors
6317  * @ioa_cfg:    ioa config struct
6318  * @mode_pages: IOAFP mode pages buffer
6319  *
6320  * Check the IOAFP's mode page 28 for term power errors
6321  *
6322  * Return value:
6323  *      nothing
6324  **/
6325 static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
6326                                  struct ipr_mode_pages *mode_pages)
6327 {
6328         int i;
6329         int entry_length;
6330         struct ipr_dev_bus_entry *bus;
6331         struct ipr_mode_page28 *mode_page;
6332
6333         mode_page = ipr_get_mode_page(mode_pages, 0x28,
6334                                       sizeof(struct ipr_mode_page28));
6335
6336         entry_length = mode_page->entry_length;
6337
6338         bus = mode_page->bus;
6339
6340         for (i = 0; i < mode_page->num_entries; i++) {
6341                 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
6342                         dev_err(&ioa_cfg->pdev->dev,
6343                                 "Term power is absent on scsi bus %d\n",
6344                                 bus->res_addr.bus);
6345                 }
6346
6347                 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
6348         }
6349 }
6350
6351 /**
6352  * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
6353  * @ioa_cfg:    ioa config struct
6354  *
6355  * Looks through the config table checking for SES devices. If
6356  * the SES device is in the SES table indicating a maximum SCSI
6357  * bus speed, the speed is limited for the bus.
6358  *
6359  * Return value:
6360  *      none
6361  **/
6362 static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
6363 {
6364         u32 max_xfer_rate;
6365         int i;
6366
6367         for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
6368                 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
6369                                                        ioa_cfg->bus_attr[i].bus_width);
6370
6371                 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
6372                         ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
6373         }
6374 }
6375
6376 /**
6377  * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
6378  * @ioa_cfg:    ioa config struct
6379  * @mode_pages: mode page 28 buffer
6380  *
6381  * Updates mode page 28 based on driver configuration
6382  *
6383  * Return value:
6384  *      none
6385  **/
6386 static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
6387                                                 struct ipr_mode_pages *mode_pages)
6388 {
6389         int i, entry_length;
6390         struct ipr_dev_bus_entry *bus;
6391         struct ipr_bus_attributes *bus_attr;
6392         struct ipr_mode_page28 *mode_page;
6393
6394         mode_page = ipr_get_mode_page(mode_pages, 0x28,
6395                                       sizeof(struct ipr_mode_page28));
6396
6397         entry_length = mode_page->entry_length;
6398
6399         /* Loop for each device bus entry */
6400         for (i = 0, bus = mode_page->bus;
6401              i < mode_page->num_entries;
6402              i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
6403                 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
6404                         dev_err(&ioa_cfg->pdev->dev,
6405                                 "Invalid resource address reported: 0x%08X\n",
6406                                 IPR_GET_PHYS_LOC(bus->res_addr));
6407                         continue;
6408                 }
6409
6410                 bus_attr = &ioa_cfg->bus_attr[i];
6411                 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
6412                 bus->bus_width = bus_attr->bus_width;
6413                 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
6414                 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
6415                 if (bus_attr->qas_enabled)
6416                         bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
6417                 else
6418                         bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
6419         }
6420 }
6421
6422 /**
6423  * ipr_build_mode_select - Build a mode select command
6424  * @ipr_cmd:    ipr command struct
6425  * @res_handle: resource handle to send command to
6426  * @parm:               Byte 2 of Mode Sense command
6427  * @dma_addr:   DMA buffer address
6428  * @xfer_len:   data transfer length
6429  *
6430  * Return value:
6431  *      none
6432  **/
6433 static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
6434                                   __be32 res_handle, u8 parm,
6435                                   dma_addr_t dma_addr, u8 xfer_len)
6436 {
6437         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6438
6439         ioarcb->res_handle = res_handle;
6440         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6441         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6442         ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
6443         ioarcb->cmd_pkt.cdb[1] = parm;
6444         ioarcb->cmd_pkt.cdb[4] = xfer_len;
6445
6446         ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_WRITE_LAST);
6447 }
6448
6449 /**
6450  * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
6451  * @ipr_cmd:    ipr command struct
6452  *
6453  * This function sets up the SCSI bus attributes and sends
6454  * a Mode Select for Page 28 to activate them.
6455  *
6456  * Return value:
6457  *      IPR_RC_JOB_RETURN
6458  **/
6459 static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
6460 {
6461         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6462         struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
6463         int length;
6464
6465         ENTER;
6466         ipr_scsi_bus_speed_limit(ioa_cfg);
6467         ipr_check_term_power(ioa_cfg, mode_pages);
6468         ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
6469         length = mode_pages->hdr.length + 1;
6470         mode_pages->hdr.length = 0;
6471
6472         ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
6473                               ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
6474                               length);
6475
6476         ipr_cmd->job_step = ipr_set_supported_devs;
6477         ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
6478                                     struct ipr_resource_entry, queue);
6479         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6480
6481         LEAVE;
6482         return IPR_RC_JOB_RETURN;
6483 }
6484
6485 /**
6486  * ipr_build_mode_sense - Builds a mode sense command
6487  * @ipr_cmd:    ipr command struct
6488  * @res:                resource entry struct
6489  * @parm:               Byte 2 of mode sense command
6490  * @dma_addr:   DMA address of mode sense buffer
6491  * @xfer_len:   Size of DMA buffer
6492  *
6493  * Return value:
6494  *      none
6495  **/
6496 static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
6497                                  __be32 res_handle,
6498                                  u8 parm, dma_addr_t dma_addr, u8 xfer_len)
6499 {
6500         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6501
6502         ioarcb->res_handle = res_handle;
6503         ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
6504         ioarcb->cmd_pkt.cdb[2] = parm;
6505         ioarcb->cmd_pkt.cdb[4] = xfer_len;
6506         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6507
6508         ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
6509 }
6510
6511 /**
6512  * ipr_reset_cmd_failed - Handle failure of IOA reset command
6513  * @ipr_cmd:    ipr command struct
6514  *
6515  * This function handles the failure of an IOA bringup command.
6516  *
6517  * Return value:
6518  *      IPR_RC_JOB_RETURN
6519  **/
6520 static int ipr_reset_cmd_failed(struct ipr_cmnd *ipr_cmd)
6521 {
6522         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6523         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
6524
6525         dev_err(&ioa_cfg->pdev->dev,
6526                 "0x%02X failed with IOASC: 0x%08X\n",
6527                 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
6528
6529         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
6530         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6531         return IPR_RC_JOB_RETURN;
6532 }
6533
6534 /**
6535  * ipr_reset_mode_sense_failed - Handle failure of IOAFP mode sense
6536  * @ipr_cmd:    ipr command struct
6537  *
6538  * This function handles the failure of a Mode Sense to the IOAFP.
6539  * Some adapters do not handle all mode pages.
6540  *
6541  * Return value:
6542  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6543  **/
6544 static int ipr_reset_mode_sense_failed(struct ipr_cmnd *ipr_cmd)
6545 {
6546         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6547         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
6548
6549         if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
6550                 ipr_cmd->job_step = ipr_set_supported_devs;
6551                 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
6552                                             struct ipr_resource_entry, queue);
6553                 return IPR_RC_JOB_CONTINUE;
6554         }
6555
6556         return ipr_reset_cmd_failed(ipr_cmd);
6557 }
6558
6559 /**
6560  * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
6561  * @ipr_cmd:    ipr command struct
6562  *
6563  * This function send a Page 28 mode sense to the IOA to
6564  * retrieve SCSI bus attributes.
6565  *
6566  * Return value:
6567  *      IPR_RC_JOB_RETURN
6568  **/
6569 static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
6570 {
6571         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6572
6573         ENTER;
6574         ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
6575                              0x28, ioa_cfg->vpd_cbs_dma +
6576                              offsetof(struct ipr_misc_cbs, mode_pages),
6577                              sizeof(struct ipr_mode_pages));
6578
6579         ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
6580         ipr_cmd->job_step_failed = ipr_reset_mode_sense_failed;
6581
6582         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6583
6584         LEAVE;
6585         return IPR_RC_JOB_RETURN;
6586 }
6587
6588 /**
6589  * ipr_ioafp_mode_select_page24 - Issue Mode Select to IOA
6590  * @ipr_cmd:    ipr command struct
6591  *
6592  * This function enables dual IOA RAID support if possible.
6593  *
6594  * Return value:
6595  *      IPR_RC_JOB_RETURN
6596  **/
6597 static int ipr_ioafp_mode_select_page24(struct ipr_cmnd *ipr_cmd)
6598 {
6599         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6600         struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
6601         struct ipr_mode_page24 *mode_page;
6602         int length;
6603
6604         ENTER;
6605         mode_page = ipr_get_mode_page(mode_pages, 0x24,
6606                                       sizeof(struct ipr_mode_page24));
6607
6608         if (mode_page)
6609                 mode_page->flags |= IPR_ENABLE_DUAL_IOA_AF;
6610
6611         length = mode_pages->hdr.length + 1;
6612         mode_pages->hdr.length = 0;
6613
6614         ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
6615                               ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
6616                               length);
6617
6618         ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
6619         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6620
6621         LEAVE;
6622         return IPR_RC_JOB_RETURN;
6623 }
6624
6625 /**
6626  * ipr_reset_mode_sense_page24_failed - Handle failure of IOAFP mode sense
6627  * @ipr_cmd:    ipr command struct
6628  *
6629  * This function handles the failure of a Mode Sense to the IOAFP.
6630  * Some adapters do not handle all mode pages.
6631  *
6632  * Return value:
6633  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6634  **/
6635 static int ipr_reset_mode_sense_page24_failed(struct ipr_cmnd *ipr_cmd)
6636 {
6637         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
6638
6639         if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
6640                 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
6641                 return IPR_RC_JOB_CONTINUE;
6642         }
6643
6644         return ipr_reset_cmd_failed(ipr_cmd);
6645 }
6646
6647 /**
6648  * ipr_ioafp_mode_sense_page24 - Issue Page 24 Mode Sense to IOA
6649  * @ipr_cmd:    ipr command struct
6650  *
6651  * This function send a mode sense to the IOA to retrieve
6652  * the IOA Advanced Function Control mode page.
6653  *
6654  * Return value:
6655  *      IPR_RC_JOB_RETURN
6656  **/
6657 static int ipr_ioafp_mode_sense_page24(struct ipr_cmnd *ipr_cmd)
6658 {
6659         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6660
6661         ENTER;
6662         ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
6663                              0x24, ioa_cfg->vpd_cbs_dma +
6664                              offsetof(struct ipr_misc_cbs, mode_pages),
6665                              sizeof(struct ipr_mode_pages));
6666
6667         ipr_cmd->job_step = ipr_ioafp_mode_select_page24;
6668         ipr_cmd->job_step_failed = ipr_reset_mode_sense_page24_failed;
6669
6670         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6671
6672         LEAVE;
6673         return IPR_RC_JOB_RETURN;
6674 }
6675
6676 /**
6677  * ipr_init_res_table - Initialize the resource table
6678  * @ipr_cmd:    ipr command struct
6679  *
6680  * This function looks through the existing resource table, comparing
6681  * it with the config table. This function will take care of old/new
6682  * devices and schedule adding/removing them from the mid-layer
6683  * as appropriate.
6684  *
6685  * Return value:
6686  *      IPR_RC_JOB_CONTINUE
6687  **/
6688 static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
6689 {
6690         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6691         struct ipr_resource_entry *res, *temp;
6692         struct ipr_config_table_entry_wrapper cfgtew;
6693         int entries, found, flag, i;
6694         LIST_HEAD(old_res);
6695
6696         ENTER;
6697         if (ioa_cfg->sis64)
6698                 flag = ioa_cfg->u.cfg_table64->hdr64.flags;
6699         else
6700                 flag = ioa_cfg->u.cfg_table->hdr.flags;
6701
6702         if (flag & IPR_UCODE_DOWNLOAD_REQ)
6703                 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
6704
6705         list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
6706                 list_move_tail(&res->queue, &old_res);
6707
6708         if (ioa_cfg->sis64)
6709                 entries = ioa_cfg->u.cfg_table64->hdr64.num_entries;
6710         else
6711                 entries = ioa_cfg->u.cfg_table->hdr.num_entries;
6712
6713         for (i = 0; i < entries; i++) {
6714                 if (ioa_cfg->sis64)
6715                         cfgtew.u.cfgte64 = &ioa_cfg->u.cfg_table64->dev[i];
6716                 else
6717                         cfgtew.u.cfgte = &ioa_cfg->u.cfg_table->dev[i];
6718                 found = 0;
6719
6720                 list_for_each_entry_safe(res, temp, &old_res, queue) {
6721                         if (ipr_is_same_device(res, &cfgtew)) {
6722                                 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
6723                                 found = 1;
6724                                 break;
6725                         }
6726                 }
6727
6728                 if (!found) {
6729                         if (list_empty(&ioa_cfg->free_res_q)) {
6730                                 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
6731                                 break;
6732                         }
6733
6734                         found = 1;
6735                         res = list_entry(ioa_cfg->free_res_q.next,
6736                                          struct ipr_resource_entry, queue);
6737                         list_move_tail(&res->queue, &ioa_cfg->used_res_q);
6738                         ipr_init_res_entry(res, &cfgtew);
6739                         res->add_to_ml = 1;
6740                 }
6741
6742                 if (found)
6743                         ipr_update_res_entry(res, &cfgtew);
6744         }
6745
6746         list_for_each_entry_safe(res, temp, &old_res, queue) {
6747                 if (res->sdev) {
6748                         res->del_from_ml = 1;
6749                         res->res_handle = IPR_INVALID_RES_HANDLE;
6750                         list_move_tail(&res->queue, &ioa_cfg->used_res_q);
6751                 }
6752         }
6753
6754         list_for_each_entry_safe(res, temp, &old_res, queue) {
6755                 ipr_clear_res_target(res);
6756                 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
6757         }
6758
6759         if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
6760                 ipr_cmd->job_step = ipr_ioafp_mode_sense_page24;
6761         else
6762                 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
6763
6764         LEAVE;
6765         return IPR_RC_JOB_CONTINUE;
6766 }
6767
6768 /**
6769  * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
6770  * @ipr_cmd:    ipr command struct
6771  *
6772  * This function sends a Query IOA Configuration command
6773  * to the adapter to retrieve the IOA configuration table.
6774  *
6775  * Return value:
6776  *      IPR_RC_JOB_RETURN
6777  **/
6778 static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
6779 {
6780         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6781         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6782         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
6783         struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
6784
6785         ENTER;
6786         if (cap->cap & IPR_CAP_DUAL_IOA_RAID)
6787                 ioa_cfg->dual_raid = 1;
6788         dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
6789                  ucode_vpd->major_release, ucode_vpd->card_type,
6790                  ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
6791         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6792         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6793
6794         ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
6795         ioarcb->cmd_pkt.cdb[7] = (ioa_cfg->cfg_table_size >> 8) & 0xff;
6796         ioarcb->cmd_pkt.cdb[8] = ioa_cfg->cfg_table_size & 0xff;
6797
6798         ipr_init_ioadl(ipr_cmd, ioa_cfg->cfg_table_dma, ioa_cfg->cfg_table_size,
6799                        IPR_IOADL_FLAGS_READ_LAST);
6800
6801         ipr_cmd->job_step = ipr_init_res_table;
6802
6803         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6804
6805         LEAVE;
6806         return IPR_RC_JOB_RETURN;
6807 }
6808
6809 /**
6810  * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
6811  * @ipr_cmd:    ipr command struct
6812  *
6813  * This utility function sends an inquiry to the adapter.
6814  *
6815  * Return value:
6816  *      none
6817  **/
6818 static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
6819                               dma_addr_t dma_addr, u8 xfer_len)
6820 {
6821         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6822
6823         ENTER;
6824         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6825         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6826
6827         ioarcb->cmd_pkt.cdb[0] = INQUIRY;
6828         ioarcb->cmd_pkt.cdb[1] = flags;
6829         ioarcb->cmd_pkt.cdb[2] = page;
6830         ioarcb->cmd_pkt.cdb[4] = xfer_len;
6831
6832         ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
6833
6834         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6835         LEAVE;
6836 }
6837
6838 /**
6839  * ipr_inquiry_page_supported - Is the given inquiry page supported
6840  * @page0:              inquiry page 0 buffer
6841  * @page:               page code.
6842  *
6843  * This function determines if the specified inquiry page is supported.
6844  *
6845  * Return value:
6846  *      1 if page is supported / 0 if not
6847  **/
6848 static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
6849 {
6850         int i;
6851
6852         for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
6853                 if (page0->page[i] == page)
6854                         return 1;
6855
6856         return 0;
6857 }
6858
6859 /**
6860  * ipr_ioafp_cap_inquiry - Send a Page 0xD0 Inquiry to the adapter.
6861  * @ipr_cmd:    ipr command struct
6862  *
6863  * This function sends a Page 0xD0 inquiry to the adapter
6864  * to retrieve adapter capabilities.
6865  *
6866  * Return value:
6867  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6868  **/
6869 static int ipr_ioafp_cap_inquiry(struct ipr_cmnd *ipr_cmd)
6870 {
6871         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6872         struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
6873         struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
6874
6875         ENTER;
6876         ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
6877         memset(cap, 0, sizeof(*cap));
6878
6879         if (ipr_inquiry_page_supported(page0, 0xD0)) {
6880                 ipr_ioafp_inquiry(ipr_cmd, 1, 0xD0,
6881                                   ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, cap),
6882                                   sizeof(struct ipr_inquiry_cap));
6883                 return IPR_RC_JOB_RETURN;
6884         }
6885
6886         LEAVE;
6887         return IPR_RC_JOB_CONTINUE;
6888 }
6889
6890 /**
6891  * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
6892  * @ipr_cmd:    ipr command struct
6893  *
6894  * This function sends a Page 3 inquiry to the adapter
6895  * to retrieve software VPD information.
6896  *
6897  * Return value:
6898  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6899  **/
6900 static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
6901 {
6902         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6903
6904         ENTER;
6905
6906         ipr_cmd->job_step = ipr_ioafp_cap_inquiry;
6907
6908         ipr_ioafp_inquiry(ipr_cmd, 1, 3,
6909                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
6910                           sizeof(struct ipr_inquiry_page3));
6911
6912         LEAVE;
6913         return IPR_RC_JOB_RETURN;
6914 }
6915
6916 /**
6917  * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
6918  * @ipr_cmd:    ipr command struct
6919  *
6920  * This function sends a Page 0 inquiry to the adapter
6921  * to retrieve supported inquiry pages.
6922  *
6923  * Return value:
6924  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6925  **/
6926 static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
6927 {
6928         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6929         char type[5];
6930
6931         ENTER;
6932
6933         /* Grab the type out of the VPD and store it away */
6934         memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
6935         type[4] = '\0';
6936         ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
6937
6938         ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
6939
6940         ipr_ioafp_inquiry(ipr_cmd, 1, 0,
6941                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
6942                           sizeof(struct ipr_inquiry_page0));
6943
6944         LEAVE;
6945         return IPR_RC_JOB_RETURN;
6946 }
6947
6948 /**
6949  * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
6950  * @ipr_cmd:    ipr command struct
6951  *
6952  * This function sends a standard inquiry to the adapter.
6953  *
6954  * Return value:
6955  *      IPR_RC_JOB_RETURN
6956  **/
6957 static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
6958 {
6959         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6960
6961         ENTER;
6962         ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
6963
6964         ipr_ioafp_inquiry(ipr_cmd, 0, 0,
6965                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
6966                           sizeof(struct ipr_ioa_vpd));
6967
6968         LEAVE;
6969         return IPR_RC_JOB_RETURN;
6970 }
6971
6972 /**
6973  * ipr_ioafp_identify_hrrq - Send Identify Host RRQ.
6974  * @ipr_cmd:    ipr command struct
6975  *
6976  * This function send an Identify Host Request Response Queue
6977  * command to establish the HRRQ with the adapter.
6978  *
6979  * Return value:
6980  *      IPR_RC_JOB_RETURN
6981  **/
6982 static int ipr_ioafp_identify_hrrq(struct ipr_cmnd *ipr_cmd)
6983 {
6984         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6985         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6986
6987         ENTER;
6988         dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
6989
6990         ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
6991         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6992
6993         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6994         if (ioa_cfg->sis64)
6995                 ioarcb->cmd_pkt.cdb[1] = 0x1;
6996         ioarcb->cmd_pkt.cdb[2] =
6997                 ((u64) ioa_cfg->host_rrq_dma >> 24) & 0xff;
6998         ioarcb->cmd_pkt.cdb[3] =
6999                 ((u64) ioa_cfg->host_rrq_dma >> 16) & 0xff;
7000         ioarcb->cmd_pkt.cdb[4] =
7001                 ((u64) ioa_cfg->host_rrq_dma >> 8) & 0xff;
7002         ioarcb->cmd_pkt.cdb[5] =
7003                 ((u64) ioa_cfg->host_rrq_dma) & 0xff;
7004         ioarcb->cmd_pkt.cdb[7] =
7005                 ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
7006         ioarcb->cmd_pkt.cdb[8] =
7007                 (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
7008
7009         if (ioa_cfg->sis64) {
7010                 ioarcb->cmd_pkt.cdb[10] =
7011                         ((u64) ioa_cfg->host_rrq_dma >> 56) & 0xff;
7012                 ioarcb->cmd_pkt.cdb[11] =
7013                         ((u64) ioa_cfg->host_rrq_dma >> 48) & 0xff;
7014                 ioarcb->cmd_pkt.cdb[12] =
7015                         ((u64) ioa_cfg->host_rrq_dma >> 40) & 0xff;
7016                 ioarcb->cmd_pkt.cdb[13] =
7017                         ((u64) ioa_cfg->host_rrq_dma >> 32) & 0xff;
7018         }
7019
7020         ipr_cmd->job_step = ipr_ioafp_std_inquiry;
7021
7022         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7023
7024         LEAVE;
7025         return IPR_RC_JOB_RETURN;
7026 }
7027
7028 /**
7029  * ipr_reset_timer_done - Adapter reset timer function
7030  * @ipr_cmd:    ipr command struct
7031  *
7032  * Description: This function is used in adapter reset processing
7033  * for timing events. If the reset_cmd pointer in the IOA
7034  * config struct is not this adapter's we are doing nested
7035  * resets and fail_all_ops will take care of freeing the
7036  * command block.
7037  *
7038  * Return value:
7039  *      none
7040  **/
7041 static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
7042 {
7043         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7044         unsigned long lock_flags = 0;
7045
7046         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7047
7048         if (ioa_cfg->reset_cmd == ipr_cmd) {
7049                 list_del(&ipr_cmd->queue);
7050                 ipr_cmd->done(ipr_cmd);
7051         }
7052
7053         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7054 }
7055
7056 /**
7057  * ipr_reset_start_timer - Start a timer for adapter reset job
7058  * @ipr_cmd:    ipr command struct
7059  * @timeout:    timeout value
7060  *
7061  * Description: This function is used in adapter reset processing
7062  * for timing events. If the reset_cmd pointer in the IOA
7063  * config struct is not this adapter's we are doing nested
7064  * resets and fail_all_ops will take care of freeing the
7065  * command block.
7066  *
7067  * Return value:
7068  *      none
7069  **/
7070 static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
7071                                   unsigned long timeout)
7072 {
7073         list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
7074         ipr_cmd->done = ipr_reset_ioa_job;
7075
7076         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
7077         ipr_cmd->timer.expires = jiffies + timeout;
7078         ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
7079         add_timer(&ipr_cmd->timer);
7080 }
7081
7082 /**
7083  * ipr_init_ioa_mem - Initialize ioa_cfg control block
7084  * @ioa_cfg:    ioa cfg struct
7085  *
7086  * Return value:
7087  *      nothing
7088  **/
7089 static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
7090 {
7091         memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
7092
7093         /* Initialize Host RRQ pointers */
7094         ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
7095         ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
7096         ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
7097         ioa_cfg->toggle_bit = 1;
7098
7099         /* Zero out config table */
7100         memset(ioa_cfg->u.cfg_table, 0, ioa_cfg->cfg_table_size);
7101 }
7102
7103 /**
7104  * ipr_reset_next_stage - Process IPL stage change based on feedback register.
7105  * @ipr_cmd:    ipr command struct
7106  *
7107  * Return value:
7108  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7109  **/
7110 static int ipr_reset_next_stage(struct ipr_cmnd *ipr_cmd)
7111 {
7112         unsigned long stage, stage_time;
7113         u32 feedback;
7114         volatile u32 int_reg;
7115         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7116         u64 maskval = 0;
7117
7118         feedback = readl(ioa_cfg->regs.init_feedback_reg);
7119         stage = feedback & IPR_IPL_INIT_STAGE_MASK;
7120         stage_time = feedback & IPR_IPL_INIT_STAGE_TIME_MASK;
7121
7122         ipr_dbg("IPL stage = 0x%lx, IPL stage time = %ld\n", stage, stage_time);
7123
7124         /* sanity check the stage_time value */
7125         if (stage_time < IPR_IPL_INIT_MIN_STAGE_TIME)
7126                 stage_time = IPR_IPL_INIT_MIN_STAGE_TIME;
7127         else if (stage_time > IPR_LONG_OPERATIONAL_TIMEOUT)
7128                 stage_time = IPR_LONG_OPERATIONAL_TIMEOUT;
7129
7130         if (stage == IPR_IPL_INIT_STAGE_UNKNOWN) {
7131                 writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.set_interrupt_mask_reg);
7132                 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7133                 stage_time = ioa_cfg->transop_timeout;
7134                 ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7135         } else if (stage == IPR_IPL_INIT_STAGE_TRANSOP) {
7136                 ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7137                 maskval = IPR_PCII_IPL_STAGE_CHANGE;
7138                 maskval = (maskval << 32) | IPR_PCII_IOA_TRANS_TO_OPER;
7139                 writeq(maskval, ioa_cfg->regs.set_interrupt_mask_reg);
7140                 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7141                 return IPR_RC_JOB_CONTINUE;
7142         }
7143
7144         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
7145         ipr_cmd->timer.expires = jiffies + stage_time * HZ;
7146         ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
7147         ipr_cmd->done = ipr_reset_ioa_job;
7148         add_timer(&ipr_cmd->timer);
7149         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
7150
7151         return IPR_RC_JOB_RETURN;
7152 }
7153
7154 /**
7155  * ipr_reset_enable_ioa - Enable the IOA following a reset.
7156  * @ipr_cmd:    ipr command struct
7157  *
7158  * This function reinitializes some control blocks and
7159  * enables destructive diagnostics on the adapter.
7160  *
7161  * Return value:
7162  *      IPR_RC_JOB_RETURN
7163  **/
7164 static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
7165 {
7166         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7167         volatile u32 int_reg;
7168
7169         ENTER;
7170         ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7171         ipr_init_ioa_mem(ioa_cfg);
7172
7173         ioa_cfg->allow_interrupts = 1;
7174         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
7175
7176         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
7177                 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
7178                        ioa_cfg->regs.clr_interrupt_mask_reg32);
7179                 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7180                 return IPR_RC_JOB_CONTINUE;
7181         }
7182
7183         /* Enable destructive diagnostics on IOA */
7184         writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg32);
7185
7186         writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg32);
7187         if (ioa_cfg->sis64)
7188                 writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.clr_interrupt_mask_reg);
7189
7190         int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7191
7192         dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
7193
7194         if (ioa_cfg->sis64) {
7195                 ipr_cmd->job_step = ipr_reset_next_stage;
7196                 return IPR_RC_JOB_CONTINUE;
7197         }
7198
7199         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
7200         ipr_cmd->timer.expires = jiffies + (ioa_cfg->transop_timeout * HZ);
7201         ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
7202         ipr_cmd->done = ipr_reset_ioa_job;
7203         add_timer(&ipr_cmd->timer);
7204         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
7205
7206         LEAVE;
7207         return IPR_RC_JOB_RETURN;
7208 }
7209
7210 /**
7211  * ipr_reset_wait_for_dump - Wait for a dump to timeout.
7212  * @ipr_cmd:    ipr command struct
7213  *
7214  * This function is invoked when an adapter dump has run out
7215  * of processing time.
7216  *
7217  * Return value:
7218  *      IPR_RC_JOB_CONTINUE
7219  **/
7220 static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
7221 {
7222         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7223
7224         if (ioa_cfg->sdt_state == GET_DUMP)
7225                 ioa_cfg->sdt_state = ABORT_DUMP;
7226
7227         ipr_cmd->job_step = ipr_reset_alert;
7228
7229         return IPR_RC_JOB_CONTINUE;
7230 }
7231
7232 /**
7233  * ipr_unit_check_no_data - Log a unit check/no data error log
7234  * @ioa_cfg:            ioa config struct
7235  *
7236  * Logs an error indicating the adapter unit checked, but for some
7237  * reason, we were unable to fetch the unit check buffer.
7238  *
7239  * Return value:
7240  *      nothing
7241  **/
7242 static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
7243 {
7244         ioa_cfg->errors_logged++;
7245         dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
7246 }
7247
7248 /**
7249  * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
7250  * @ioa_cfg:            ioa config struct
7251  *
7252  * Fetches the unit check buffer from the adapter by clocking the data
7253  * through the mailbox register.
7254  *
7255  * Return value:
7256  *      nothing
7257  **/
7258 static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
7259 {
7260         unsigned long mailbox;
7261         struct ipr_hostrcb *hostrcb;
7262         struct ipr_uc_sdt sdt;
7263         int rc, length;
7264         u32 ioasc;
7265
7266         mailbox = readl(ioa_cfg->ioa_mailbox);
7267
7268         if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(mailbox)) {
7269                 ipr_unit_check_no_data(ioa_cfg);
7270                 return;
7271         }
7272
7273         memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
7274         rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
7275                                         (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
7276
7277         if (rc || !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY) ||
7278             ((be32_to_cpu(sdt.hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
7279             (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
7280                 ipr_unit_check_no_data(ioa_cfg);
7281                 return;
7282         }
7283
7284         /* Find length of the first sdt entry (UC buffer) */
7285         if (be32_to_cpu(sdt.hdr.state) == IPR_FMT3_SDT_READY_TO_USE)
7286                 length = be32_to_cpu(sdt.entry[0].end_token);
7287         else
7288                 length = (be32_to_cpu(sdt.entry[0].end_token) -
7289                           be32_to_cpu(sdt.entry[0].start_token)) &
7290                           IPR_FMT2_MBX_ADDR_MASK;
7291
7292         hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
7293                              struct ipr_hostrcb, queue);
7294         list_del(&hostrcb->queue);
7295         memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
7296
7297         rc = ipr_get_ldump_data_section(ioa_cfg,
7298                                         be32_to_cpu(sdt.entry[0].start_token),
7299                                         (__be32 *)&hostrcb->hcam,
7300                                         min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
7301
7302         if (!rc) {
7303                 ipr_handle_log_data(ioa_cfg, hostrcb);
7304                 ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
7305                 if (ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED &&
7306                     ioa_cfg->sdt_state == GET_DUMP)
7307                         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
7308         } else
7309                 ipr_unit_check_no_data(ioa_cfg);
7310
7311         list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
7312 }
7313
7314 /**
7315  * ipr_reset_restore_cfg_space - Restore PCI config space.
7316  * @ipr_cmd:    ipr command struct
7317  *
7318  * Description: This function restores the saved PCI config space of
7319  * the adapter, fails all outstanding ops back to the callers, and
7320  * fetches the dump/unit check if applicable to this reset.
7321  *
7322  * Return value:
7323  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7324  **/
7325 static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
7326 {
7327         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7328         int rc;
7329
7330         ENTER;
7331         ioa_cfg->pdev->state_saved = true;
7332         rc = pci_restore_state(ioa_cfg->pdev);
7333
7334         if (rc != PCIBIOS_SUCCESSFUL) {
7335                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
7336                 return IPR_RC_JOB_CONTINUE;
7337         }
7338
7339         if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
7340                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
7341                 return IPR_RC_JOB_CONTINUE;
7342         }
7343
7344         ipr_fail_all_ops(ioa_cfg);
7345
7346         if (ioa_cfg->ioa_unit_checked) {
7347                 ioa_cfg->ioa_unit_checked = 0;
7348                 ipr_get_unit_check_buffer(ioa_cfg);
7349                 ipr_cmd->job_step = ipr_reset_alert;
7350                 ipr_reset_start_timer(ipr_cmd, 0);
7351                 return IPR_RC_JOB_RETURN;
7352         }
7353
7354         if (ioa_cfg->in_ioa_bringdown) {
7355                 ipr_cmd->job_step = ipr_ioa_bringdown_done;
7356         } else {
7357                 ipr_cmd->job_step = ipr_reset_enable_ioa;
7358
7359                 if (GET_DUMP == ioa_cfg->sdt_state) {
7360                         ipr_reset_start_timer(ipr_cmd, IPR_DUMP_TIMEOUT);
7361                         ipr_cmd->job_step = ipr_reset_wait_for_dump;
7362                         schedule_work(&ioa_cfg->work_q);
7363                         return IPR_RC_JOB_RETURN;
7364                 }
7365         }
7366
7367         ENTER;
7368         return IPR_RC_JOB_CONTINUE;
7369 }
7370
7371 /**
7372  * ipr_reset_bist_done - BIST has completed on the adapter.
7373  * @ipr_cmd:    ipr command struct
7374  *
7375  * Description: Unblock config space and resume the reset process.
7376  *
7377  * Return value:
7378  *      IPR_RC_JOB_CONTINUE
7379  **/
7380 static int ipr_reset_bist_done(struct ipr_cmnd *ipr_cmd)
7381 {
7382         ENTER;
7383         pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
7384         ipr_cmd->job_step = ipr_reset_restore_cfg_space;
7385         LEAVE;
7386         return IPR_RC_JOB_CONTINUE;
7387 }
7388
7389 /**
7390  * ipr_reset_start_bist - Run BIST on the adapter.
7391  * @ipr_cmd:    ipr command struct
7392  *
7393  * Description: This function runs BIST on the adapter, then delays 2 seconds.
7394  *
7395  * Return value:
7396  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7397  **/
7398 static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
7399 {
7400         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7401         int rc;
7402
7403         ENTER;
7404         pci_block_user_cfg_access(ioa_cfg->pdev);
7405         rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
7406
7407         if (rc != PCIBIOS_SUCCESSFUL) {
7408                 pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
7409                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
7410                 rc = IPR_RC_JOB_CONTINUE;
7411         } else {
7412                 ipr_cmd->job_step = ipr_reset_bist_done;
7413                 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
7414                 rc = IPR_RC_JOB_RETURN;
7415         }
7416
7417         LEAVE;
7418         return rc;
7419 }
7420
7421 /**
7422  * ipr_reset_slot_reset_done - Clear PCI reset to the adapter
7423  * @ipr_cmd:    ipr command struct
7424  *
7425  * Description: This clears PCI reset to the adapter and delays two seconds.
7426  *
7427  * Return value:
7428  *      IPR_RC_JOB_RETURN
7429  **/
7430 static int ipr_reset_slot_reset_done(struct ipr_cmnd *ipr_cmd)
7431 {
7432         ENTER;
7433         pci_set_pcie_reset_state(ipr_cmd->ioa_cfg->pdev, pcie_deassert_reset);
7434         ipr_cmd->job_step = ipr_reset_bist_done;
7435         ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
7436         LEAVE;
7437         return IPR_RC_JOB_RETURN;
7438 }
7439
7440 /**
7441  * ipr_reset_slot_reset - Reset the PCI slot of the adapter.
7442  * @ipr_cmd:    ipr command struct
7443  *
7444  * Description: This asserts PCI reset to the adapter.
7445  *
7446  * Return value:
7447  *      IPR_RC_JOB_RETURN
7448  **/
7449 static int ipr_reset_slot_reset(struct ipr_cmnd *ipr_cmd)
7450 {
7451         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7452         struct pci_dev *pdev = ioa_cfg->pdev;
7453
7454         ENTER;
7455         pci_block_user_cfg_access(pdev);
7456         pci_set_pcie_reset_state(pdev, pcie_warm_reset);
7457         ipr_cmd->job_step = ipr_reset_slot_reset_done;
7458         ipr_reset_start_timer(ipr_cmd, IPR_PCI_RESET_TIMEOUT);
7459         LEAVE;
7460         return IPR_RC_JOB_RETURN;
7461 }
7462
7463 /**
7464  * ipr_reset_allowed - Query whether or not IOA can be reset
7465  * @ioa_cfg:    ioa config struct
7466  *
7467  * Return value:
7468  *      0 if reset not allowed / non-zero if reset is allowed
7469  **/
7470 static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
7471 {
7472         volatile u32 temp_reg;
7473
7474         temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
7475         return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
7476 }
7477
7478 /**
7479  * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
7480  * @ipr_cmd:    ipr command struct
7481  *
7482  * Description: This function waits for adapter permission to run BIST,
7483  * then runs BIST. If the adapter does not give permission after a
7484  * reasonable time, we will reset the adapter anyway. The impact of
7485  * resetting the adapter without warning the adapter is the risk of
7486  * losing the persistent error log on the adapter. If the adapter is
7487  * reset while it is writing to the flash on the adapter, the flash
7488  * segment will have bad ECC and be zeroed.
7489  *
7490  * Return value:
7491  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7492  **/
7493 static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
7494 {
7495         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7496         int rc = IPR_RC_JOB_RETURN;
7497
7498         if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
7499                 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
7500                 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
7501         } else {
7502                 ipr_cmd->job_step = ioa_cfg->reset;
7503                 rc = IPR_RC_JOB_CONTINUE;
7504         }
7505
7506         return rc;
7507 }
7508
7509 /**
7510  * ipr_reset_alert_part2 - Alert the adapter of a pending reset
7511  * @ipr_cmd:    ipr command struct
7512  *
7513  * Description: This function alerts the adapter that it will be reset.
7514  * If memory space is not currently enabled, proceed directly
7515  * to running BIST on the adapter. The timer must always be started
7516  * so we guarantee we do not run BIST from ipr_isr.
7517  *
7518  * Return value:
7519  *      IPR_RC_JOB_RETURN
7520  **/
7521 static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
7522 {
7523         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7524         u16 cmd_reg;
7525         int rc;
7526
7527         ENTER;
7528         rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
7529
7530         if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
7531                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
7532                 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg32);
7533                 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
7534         } else {
7535                 ipr_cmd->job_step = ioa_cfg->reset;
7536         }
7537
7538         ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
7539         ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
7540
7541         LEAVE;
7542         return IPR_RC_JOB_RETURN;
7543 }
7544
7545 /**
7546  * ipr_reset_ucode_download_done - Microcode download completion
7547  * @ipr_cmd:    ipr command struct
7548  *
7549  * Description: This function unmaps the microcode download buffer.
7550  *
7551  * Return value:
7552  *      IPR_RC_JOB_CONTINUE
7553  **/
7554 static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
7555 {
7556         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7557         struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
7558
7559         pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
7560                      sglist->num_sg, DMA_TO_DEVICE);
7561
7562         ipr_cmd->job_step = ipr_reset_alert;
7563         return IPR_RC_JOB_CONTINUE;
7564 }
7565
7566 /**
7567  * ipr_reset_ucode_download - Download microcode to the adapter
7568  * @ipr_cmd:    ipr command struct
7569  *
7570  * Description: This function checks to see if it there is microcode
7571  * to download to the adapter. If there is, a download is performed.
7572  *
7573  * Return value:
7574  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7575  **/
7576 static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
7577 {
7578         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7579         struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
7580
7581         ENTER;
7582         ipr_cmd->job_step = ipr_reset_alert;
7583
7584         if (!sglist)
7585                 return IPR_RC_JOB_CONTINUE;
7586
7587         ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7588         ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
7589         ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
7590         ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
7591         ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
7592         ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
7593         ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
7594
7595         if (ioa_cfg->sis64)
7596                 ipr_build_ucode_ioadl64(ipr_cmd, sglist);
7597         else
7598                 ipr_build_ucode_ioadl(ipr_cmd, sglist);
7599         ipr_cmd->job_step = ipr_reset_ucode_download_done;
7600
7601         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
7602                    IPR_WRITE_BUFFER_TIMEOUT);
7603
7604         LEAVE;
7605         return IPR_RC_JOB_RETURN;
7606 }
7607
7608 /**
7609  * ipr_reset_shutdown_ioa - Shutdown the adapter
7610  * @ipr_cmd:    ipr command struct
7611  *
7612  * Description: This function issues an adapter shutdown of the
7613  * specified type to the specified adapter as part of the
7614  * adapter reset job.
7615  *
7616  * Return value:
7617  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7618  **/
7619 static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
7620 {
7621         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7622         enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
7623         unsigned long timeout;
7624         int rc = IPR_RC_JOB_CONTINUE;
7625
7626         ENTER;
7627         if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
7628                 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7629                 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
7630                 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
7631                 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
7632
7633                 if (shutdown_type == IPR_SHUTDOWN_NORMAL)
7634                         timeout = IPR_SHUTDOWN_TIMEOUT;
7635                 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
7636                         timeout = IPR_INTERNAL_TIMEOUT;
7637                 else if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
7638                         timeout = IPR_DUAL_IOA_ABBR_SHUTDOWN_TO;
7639                 else
7640                         timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
7641
7642                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
7643
7644                 rc = IPR_RC_JOB_RETURN;
7645                 ipr_cmd->job_step = ipr_reset_ucode_download;
7646         } else
7647                 ipr_cmd->job_step = ipr_reset_alert;
7648
7649         LEAVE;
7650         return rc;
7651 }
7652
7653 /**
7654  * ipr_reset_ioa_job - Adapter reset job
7655  * @ipr_cmd:    ipr command struct
7656  *
7657  * Description: This function is the job router for the adapter reset job.
7658  *
7659  * Return value:
7660  *      none
7661  **/
7662 static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
7663 {
7664         u32 rc, ioasc;
7665         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7666
7667         do {
7668                 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
7669
7670                 if (ioa_cfg->reset_cmd != ipr_cmd) {
7671                         /*
7672                          * We are doing nested adapter resets and this is
7673                          * not the current reset job.
7674                          */
7675                         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
7676                         return;
7677                 }
7678
7679                 if (IPR_IOASC_SENSE_KEY(ioasc)) {
7680                         rc = ipr_cmd->job_step_failed(ipr_cmd);
7681                         if (rc == IPR_RC_JOB_RETURN)
7682                                 return;
7683                 }
7684
7685                 ipr_reinit_ipr_cmnd(ipr_cmd);
7686                 ipr_cmd->job_step_failed = ipr_reset_cmd_failed;
7687                 rc = ipr_cmd->job_step(ipr_cmd);
7688         } while(rc == IPR_RC_JOB_CONTINUE);
7689 }
7690
7691 /**
7692  * _ipr_initiate_ioa_reset - Initiate an adapter reset
7693  * @ioa_cfg:            ioa config struct
7694  * @job_step:           first job step of reset job
7695  * @shutdown_type:      shutdown type
7696  *
7697  * Description: This function will initiate the reset of the given adapter
7698  * starting at the selected job step.
7699  * If the caller needs to wait on the completion of the reset,
7700  * the caller must sleep on the reset_wait_q.
7701  *
7702  * Return value:
7703  *      none
7704  **/
7705 static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
7706                                     int (*job_step) (struct ipr_cmnd *),
7707                                     enum ipr_shutdown_type shutdown_type)
7708 {
7709         struct ipr_cmnd *ipr_cmd;
7710
7711         ioa_cfg->in_reset_reload = 1;
7712         ioa_cfg->allow_cmds = 0;
7713         scsi_block_requests(ioa_cfg->host);
7714
7715         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
7716         ioa_cfg->reset_cmd = ipr_cmd;
7717         ipr_cmd->job_step = job_step;
7718         ipr_cmd->u.shutdown_type = shutdown_type;
7719
7720         ipr_reset_ioa_job(ipr_cmd);
7721 }
7722
7723 /**
7724  * ipr_initiate_ioa_reset - Initiate an adapter reset
7725  * @ioa_cfg:            ioa config struct
7726  * @shutdown_type:      shutdown type
7727  *
7728  * Description: This function will initiate the reset of the given adapter.
7729  * If the caller needs to wait on the completion of the reset,
7730  * the caller must sleep on the reset_wait_q.
7731  *
7732  * Return value:
7733  *      none
7734  **/
7735 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
7736                                    enum ipr_shutdown_type shutdown_type)
7737 {
7738         if (ioa_cfg->ioa_is_dead)
7739                 return;
7740
7741         if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
7742                 ioa_cfg->sdt_state = ABORT_DUMP;
7743
7744         if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
7745                 dev_err(&ioa_cfg->pdev->dev,
7746                         "IOA taken offline - error recovery failed\n");
7747
7748                 ioa_cfg->reset_retries = 0;
7749                 ioa_cfg->ioa_is_dead = 1;
7750
7751                 if (ioa_cfg->in_ioa_bringdown) {
7752                         ioa_cfg->reset_cmd = NULL;
7753                         ioa_cfg->in_reset_reload = 0;
7754                         ipr_fail_all_ops(ioa_cfg);
7755                         wake_up_all(&ioa_cfg->reset_wait_q);
7756
7757                         spin_unlock_irq(ioa_cfg->host->host_lock);
7758                         scsi_unblock_requests(ioa_cfg->host);
7759                         spin_lock_irq(ioa_cfg->host->host_lock);
7760                         return;
7761                 } else {
7762                         ioa_cfg->in_ioa_bringdown = 1;
7763                         shutdown_type = IPR_SHUTDOWN_NONE;
7764                 }
7765         }
7766
7767         _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
7768                                 shutdown_type);
7769 }
7770
7771 /**
7772  * ipr_reset_freeze - Hold off all I/O activity
7773  * @ipr_cmd:    ipr command struct
7774  *
7775  * Description: If the PCI slot is frozen, hold off all I/O
7776  * activity; then, as soon as the slot is available again,
7777  * initiate an adapter reset.
7778  */
7779 static int ipr_reset_freeze(struct ipr_cmnd *ipr_cmd)
7780 {
7781         /* Disallow new interrupts, avoid loop */
7782         ipr_cmd->ioa_cfg->allow_interrupts = 0;
7783         list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
7784         ipr_cmd->done = ipr_reset_ioa_job;
7785         return IPR_RC_JOB_RETURN;
7786 }
7787
7788 /**
7789  * ipr_pci_frozen - Called when slot has experienced a PCI bus error.
7790  * @pdev:       PCI device struct
7791  *
7792  * Description: This routine is called to tell us that the PCI bus
7793  * is down. Can't do anything here, except put the device driver
7794  * into a holding pattern, waiting for the PCI bus to come back.
7795  */
7796 static void ipr_pci_frozen(struct pci_dev *pdev)
7797 {
7798         unsigned long flags = 0;
7799         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7800
7801         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
7802         _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_freeze, IPR_SHUTDOWN_NONE);
7803         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
7804 }
7805
7806 /**
7807  * ipr_pci_slot_reset - Called when PCI slot has been reset.
7808  * @pdev:       PCI device struct
7809  *
7810  * Description: This routine is called by the pci error recovery
7811  * code after the PCI slot has been reset, just before we
7812  * should resume normal operations.
7813  */
7814 static pci_ers_result_t ipr_pci_slot_reset(struct pci_dev *pdev)
7815 {
7816         unsigned long flags = 0;
7817         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7818
7819         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
7820         if (ioa_cfg->needs_warm_reset)
7821                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7822         else
7823                 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_restore_cfg_space,
7824                                         IPR_SHUTDOWN_NONE);
7825         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
7826         return PCI_ERS_RESULT_RECOVERED;
7827 }
7828
7829 /**
7830  * ipr_pci_perm_failure - Called when PCI slot is dead for good.
7831  * @pdev:       PCI device struct
7832  *
7833  * Description: This routine is called when the PCI bus has
7834  * permanently failed.
7835  */
7836 static void ipr_pci_perm_failure(struct pci_dev *pdev)
7837 {
7838         unsigned long flags = 0;
7839         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7840
7841         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
7842         if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
7843                 ioa_cfg->sdt_state = ABORT_DUMP;
7844         ioa_cfg->reset_retries = IPR_NUM_RESET_RELOAD_RETRIES;
7845         ioa_cfg->in_ioa_bringdown = 1;
7846         ioa_cfg->allow_cmds = 0;
7847         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7848         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
7849 }
7850
7851 /**
7852  * ipr_pci_error_detected - Called when a PCI error is detected.
7853  * @pdev:       PCI device struct
7854  * @state:      PCI channel state
7855  *
7856  * Description: Called when a PCI error is detected.
7857  *
7858  * Return value:
7859  *      PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7860  */
7861 static pci_ers_result_t ipr_pci_error_detected(struct pci_dev *pdev,
7862                                                pci_channel_state_t state)
7863 {
7864         switch (state) {
7865         case pci_channel_io_frozen:
7866                 ipr_pci_frozen(pdev);
7867                 return PCI_ERS_RESULT_NEED_RESET;
7868         case pci_channel_io_perm_failure:
7869                 ipr_pci_perm_failure(pdev);
7870                 return PCI_ERS_RESULT_DISCONNECT;
7871                 break;
7872         default:
7873                 break;
7874         }
7875         return PCI_ERS_RESULT_NEED_RESET;
7876 }
7877
7878 /**
7879  * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
7880  * @ioa_cfg:    ioa cfg struct
7881  *
7882  * Description: This is the second phase of adapter intialization
7883  * This function takes care of initilizing the adapter to the point
7884  * where it can accept new commands.
7885
7886  * Return value:
7887  *      0 on success / -EIO on failure
7888  **/
7889 static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
7890 {
7891         int rc = 0;
7892         unsigned long host_lock_flags = 0;
7893
7894         ENTER;
7895         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7896         dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
7897         if (ioa_cfg->needs_hard_reset) {
7898                 ioa_cfg->needs_hard_reset = 0;
7899                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7900         } else
7901                 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa,
7902                                         IPR_SHUTDOWN_NONE);
7903
7904         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7905         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7906         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7907
7908         if (ioa_cfg->ioa_is_dead) {
7909                 rc = -EIO;
7910         } else if (ipr_invalid_adapter(ioa_cfg)) {
7911                 if (!ipr_testmode)
7912                         rc = -EIO;
7913
7914                 dev_err(&ioa_cfg->pdev->dev,
7915                         "Adapter not supported in this hardware configuration.\n");
7916         }
7917
7918         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7919
7920         LEAVE;
7921         return rc;
7922 }
7923
7924 /**
7925  * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
7926  * @ioa_cfg:    ioa config struct
7927  *
7928  * Return value:
7929  *      none
7930  **/
7931 static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
7932 {
7933         int i;
7934
7935         for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
7936                 if (ioa_cfg->ipr_cmnd_list[i])
7937                         pci_pool_free(ioa_cfg->ipr_cmd_pool,
7938                                       ioa_cfg->ipr_cmnd_list[i],
7939                                       ioa_cfg->ipr_cmnd_list_dma[i]);
7940
7941                 ioa_cfg->ipr_cmnd_list[i] = NULL;
7942         }
7943
7944         if (ioa_cfg->ipr_cmd_pool)
7945                 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
7946
7947         ioa_cfg->ipr_cmd_pool = NULL;
7948 }
7949
7950 /**
7951  * ipr_free_mem - Frees memory allocated for an adapter
7952  * @ioa_cfg:    ioa cfg struct
7953  *
7954  * Return value:
7955  *      nothing
7956  **/
7957 static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
7958 {
7959         int i;
7960
7961         kfree(ioa_cfg->res_entries);
7962         pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
7963                             ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
7964         ipr_free_cmd_blks(ioa_cfg);
7965         pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
7966                             ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
7967         pci_free_consistent(ioa_cfg->pdev, ioa_cfg->cfg_table_size,
7968                             ioa_cfg->u.cfg_table,
7969                             ioa_cfg->cfg_table_dma);
7970
7971         for (i = 0; i < IPR_NUM_HCAMS; i++) {
7972                 pci_free_consistent(ioa_cfg->pdev,
7973                                     sizeof(struct ipr_hostrcb),
7974                                     ioa_cfg->hostrcb[i],
7975                                     ioa_cfg->hostrcb_dma[i]);
7976         }
7977
7978         ipr_free_dump(ioa_cfg);
7979         kfree(ioa_cfg->trace);
7980 }
7981
7982 /**
7983  * ipr_free_all_resources - Free all allocated resources for an adapter.
7984  * @ipr_cmd:    ipr command struct
7985  *
7986  * This function frees all allocated resources for the
7987  * specified adapter.
7988  *
7989  * Return value:
7990  *      none
7991  **/
7992 static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
7993 {
7994         struct pci_dev *pdev = ioa_cfg->pdev;
7995
7996         ENTER;
7997         free_irq(pdev->irq, ioa_cfg);
7998         pci_disable_msi(pdev);
7999         iounmap(ioa_cfg->hdw_dma_regs);
8000         pci_release_regions(pdev);
8001         ipr_free_mem(ioa_cfg);
8002         scsi_host_put(ioa_cfg->host);
8003         pci_disable_device(pdev);
8004         LEAVE;
8005 }
8006
8007 /**
8008  * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
8009  * @ioa_cfg:    ioa config struct
8010  *
8011  * Return value:
8012  *      0 on success / -ENOMEM on allocation failure
8013  **/
8014 static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
8015 {
8016         struct ipr_cmnd *ipr_cmd;
8017         struct ipr_ioarcb *ioarcb;
8018         dma_addr_t dma_addr;
8019         int i;
8020
8021         ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
8022                                                  sizeof(struct ipr_cmnd), 16, 0);
8023
8024         if (!ioa_cfg->ipr_cmd_pool)
8025                 return -ENOMEM;
8026
8027         for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
8028                 ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, GFP_KERNEL, &dma_addr);
8029
8030                 if (!ipr_cmd) {
8031                         ipr_free_cmd_blks(ioa_cfg);
8032                         return -ENOMEM;
8033                 }
8034
8035                 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
8036                 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
8037                 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
8038
8039                 ioarcb = &ipr_cmd->ioarcb;
8040                 ipr_cmd->dma_addr = dma_addr;
8041                 if (ioa_cfg->sis64)
8042                         ioarcb->a.ioarcb_host_pci_addr64 = cpu_to_be64(dma_addr);
8043                 else
8044                         ioarcb->a.ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
8045
8046                 ioarcb->host_response_handle = cpu_to_be32(i << 2);
8047                 if (ioa_cfg->sis64) {
8048                         ioarcb->u.sis64_addr_data.data_ioadl_addr =
8049                                 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
8050                         ioarcb->u.sis64_addr_data.ioasa_host_pci_addr =
8051                                 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, ioasa));
8052                 } else {
8053                         ioarcb->write_ioadl_addr =
8054                                 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
8055                         ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
8056                         ioarcb->ioasa_host_pci_addr =
8057                                 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioasa));
8058                 }
8059                 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
8060                 ipr_cmd->cmd_index = i;
8061                 ipr_cmd->ioa_cfg = ioa_cfg;
8062                 ipr_cmd->sense_buffer_dma = dma_addr +
8063                         offsetof(struct ipr_cmnd, sense_buffer);
8064
8065                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
8066         }
8067
8068         return 0;
8069 }
8070
8071 /**
8072  * ipr_alloc_mem - Allocate memory for an adapter
8073  * @ioa_cfg:    ioa config struct
8074  *
8075  * Return value:
8076  *      0 on success / non-zero for error
8077  **/
8078 static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
8079 {
8080         struct pci_dev *pdev = ioa_cfg->pdev;
8081         int i, rc = -ENOMEM;
8082
8083         ENTER;
8084         ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
8085                                        ioa_cfg->max_devs_supported, GFP_KERNEL);
8086
8087         if (!ioa_cfg->res_entries)
8088                 goto out;
8089
8090         if (ioa_cfg->sis64) {
8091                 ioa_cfg->target_ids = kzalloc(sizeof(unsigned long) *
8092                                               BITS_TO_LONGS(ioa_cfg->max_devs_supported), GFP_KERNEL);
8093                 ioa_cfg->array_ids = kzalloc(sizeof(unsigned long) *
8094                                              BITS_TO_LONGS(ioa_cfg->max_devs_supported), GFP_KERNEL);
8095                 ioa_cfg->vset_ids = kzalloc(sizeof(unsigned long) *
8096                                             BITS_TO_LONGS(ioa_cfg->max_devs_supported), GFP_KERNEL);
8097         }
8098
8099         for (i = 0; i < ioa_cfg->max_devs_supported; i++) {
8100                 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
8101                 ioa_cfg->res_entries[i].ioa_cfg = ioa_cfg;
8102         }
8103
8104         ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
8105                                                 sizeof(struct ipr_misc_cbs),
8106                                                 &ioa_cfg->vpd_cbs_dma);
8107
8108         if (!ioa_cfg->vpd_cbs)
8109                 goto out_free_res_entries;
8110
8111         if (ipr_alloc_cmd_blks(ioa_cfg))
8112                 goto out_free_vpd_cbs;
8113
8114         ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
8115                                                  sizeof(u32) * IPR_NUM_CMD_BLKS,
8116                                                  &ioa_cfg->host_rrq_dma);
8117
8118         if (!ioa_cfg->host_rrq)
8119                 goto out_ipr_free_cmd_blocks;
8120
8121         ioa_cfg->u.cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
8122                                                     ioa_cfg->cfg_table_size,
8123                                                     &ioa_cfg->cfg_table_dma);
8124
8125         if (!ioa_cfg->u.cfg_table)
8126                 goto out_free_host_rrq;
8127
8128         for (i = 0; i < IPR_NUM_HCAMS; i++) {
8129                 ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
8130                                                            sizeof(struct ipr_hostrcb),
8131                                                            &ioa_cfg->hostrcb_dma[i]);
8132
8133                 if (!ioa_cfg->hostrcb[i])
8134                         goto out_free_hostrcb_dma;
8135
8136                 ioa_cfg->hostrcb[i]->hostrcb_dma =
8137                         ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
8138                 ioa_cfg->hostrcb[i]->ioa_cfg = ioa_cfg;
8139                 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
8140         }
8141
8142         ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
8143                                  IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
8144
8145         if (!ioa_cfg->trace)
8146                 goto out_free_hostrcb_dma;
8147
8148         rc = 0;
8149 out:
8150         LEAVE;
8151         return rc;
8152
8153 out_free_hostrcb_dma:
8154         while (i-- > 0) {
8155                 pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
8156                                     ioa_cfg->hostrcb[i],
8157                                     ioa_cfg->hostrcb_dma[i]);
8158         }
8159         pci_free_consistent(pdev, ioa_cfg->cfg_table_size,
8160                             ioa_cfg->u.cfg_table,
8161                             ioa_cfg->cfg_table_dma);
8162 out_free_host_rrq:
8163         pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
8164                             ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
8165 out_ipr_free_cmd_blocks:
8166         ipr_free_cmd_blks(ioa_cfg);
8167 out_free_vpd_cbs:
8168         pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
8169                             ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
8170 out_free_res_entries:
8171         kfree(ioa_cfg->res_entries);
8172         goto out;
8173 }
8174
8175 /**
8176  * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
8177  * @ioa_cfg:    ioa config struct
8178  *
8179  * Return value:
8180  *      none
8181  **/
8182 static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
8183 {
8184         int i;
8185
8186         for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
8187                 ioa_cfg->bus_attr[i].bus = i;
8188                 ioa_cfg->bus_attr[i].qas_enabled = 0;
8189                 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
8190                 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
8191                         ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
8192                 else
8193                         ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
8194         }
8195 }
8196
8197 /**
8198  * ipr_init_ioa_cfg - Initialize IOA config struct
8199  * @ioa_cfg:    ioa config struct
8200  * @host:               scsi host struct
8201  * @pdev:               PCI dev struct
8202  *
8203  * Return value:
8204  *      none
8205  **/
8206 static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
8207                                        struct Scsi_Host *host, struct pci_dev *pdev)
8208 {
8209         const struct ipr_interrupt_offsets *p;
8210         struct ipr_interrupts *t;
8211         void __iomem *base;
8212
8213         ioa_cfg->host = host;
8214         ioa_cfg->pdev = pdev;
8215         ioa_cfg->log_level = ipr_log_level;
8216         ioa_cfg->doorbell = IPR_DOORBELL;
8217         sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
8218         sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
8219         sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
8220         sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
8221         sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
8222         sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
8223         sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
8224         sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
8225
8226         INIT_LIST_HEAD(&ioa_cfg->free_q);
8227         INIT_LIST_HEAD(&ioa_cfg->pending_q);
8228         INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
8229         INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
8230         INIT_LIST_HEAD(&ioa_cfg->free_res_q);
8231         INIT_LIST_HEAD(&ioa_cfg->used_res_q);
8232         INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread);
8233         init_waitqueue_head(&ioa_cfg->reset_wait_q);
8234         init_waitqueue_head(&ioa_cfg->msi_wait_q);
8235         ioa_cfg->sdt_state = INACTIVE;
8236
8237         ipr_initialize_bus_attr(ioa_cfg);
8238         ioa_cfg->max_devs_supported = ipr_max_devs;
8239
8240         if (ioa_cfg->sis64) {
8241                 host->max_id = IPR_MAX_SIS64_TARGETS_PER_BUS;
8242                 host->max_lun = IPR_MAX_SIS64_LUNS_PER_TARGET;
8243                 if (ipr_max_devs > IPR_MAX_SIS64_DEVS)
8244                         ioa_cfg->max_devs_supported = IPR_MAX_SIS64_DEVS;
8245         } else {
8246                 host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
8247                 host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
8248                 if (ipr_max_devs > IPR_MAX_PHYSICAL_DEVS)
8249                         ioa_cfg->max_devs_supported = IPR_MAX_PHYSICAL_DEVS;
8250         }
8251         host->max_channel = IPR_MAX_BUS_TO_SCAN;
8252         host->unique_id = host->host_no;
8253         host->max_cmd_len = IPR_MAX_CDB_LEN;
8254         pci_set_drvdata(pdev, ioa_cfg);
8255
8256         p = &ioa_cfg->chip_cfg->regs;
8257         t = &ioa_cfg->regs;
8258         base = ioa_cfg->hdw_dma_regs;
8259
8260         t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
8261         t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
8262         t->clr_interrupt_mask_reg32 = base + p->clr_interrupt_mask_reg32;
8263         t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
8264         t->sense_interrupt_mask_reg32 = base + p->sense_interrupt_mask_reg32;
8265         t->clr_interrupt_reg = base + p->clr_interrupt_reg;
8266         t->clr_interrupt_reg32 = base + p->clr_interrupt_reg32;
8267         t->sense_interrupt_reg = base + p->sense_interrupt_reg;
8268         t->sense_interrupt_reg32 = base + p->sense_interrupt_reg32;
8269         t->ioarrin_reg = base + p->ioarrin_reg;
8270         t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
8271         t->sense_uproc_interrupt_reg32 = base + p->sense_uproc_interrupt_reg32;
8272         t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
8273         t->set_uproc_interrupt_reg32 = base + p->set_uproc_interrupt_reg32;
8274         t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
8275         t->clr_uproc_interrupt_reg32 = base + p->clr_uproc_interrupt_reg32;
8276
8277         if (ioa_cfg->sis64) {
8278                 t->init_feedback_reg = base + p->init_feedback_reg;
8279                 t->dump_addr_reg = base + p->dump_addr_reg;
8280                 t->dump_data_reg = base + p->dump_data_reg;
8281         }
8282 }
8283
8284 /**
8285  * ipr_get_chip_info - Find adapter chip information
8286  * @dev_id:             PCI device id struct
8287  *
8288  * Return value:
8289  *      ptr to chip information on success / NULL on failure
8290  **/
8291 static const struct ipr_chip_t * __devinit
8292 ipr_get_chip_info(const struct pci_device_id *dev_id)
8293 {
8294         int i;
8295
8296         for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
8297                 if (ipr_chip[i].vendor == dev_id->vendor &&
8298                     ipr_chip[i].device == dev_id->device)
8299                         return &ipr_chip[i];
8300         return NULL;
8301 }
8302
8303 /**
8304  * ipr_test_intr - Handle the interrupt generated in ipr_test_msi().
8305  * @pdev:               PCI device struct
8306  *
8307  * Description: Simply set the msi_received flag to 1 indicating that
8308  * Message Signaled Interrupts are supported.
8309  *
8310  * Return value:
8311  *      0 on success / non-zero on failure
8312  **/
8313 static irqreturn_t __devinit ipr_test_intr(int irq, void *devp)
8314 {
8315         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
8316         unsigned long lock_flags = 0;
8317         irqreturn_t rc = IRQ_HANDLED;
8318
8319         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8320
8321         ioa_cfg->msi_received = 1;
8322         wake_up(&ioa_cfg->msi_wait_q);
8323
8324         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8325         return rc;
8326 }
8327
8328 /**
8329  * ipr_test_msi - Test for Message Signaled Interrupt (MSI) support.
8330  * @pdev:               PCI device struct
8331  *
8332  * Description: The return value from pci_enable_msi() can not always be
8333  * trusted.  This routine sets up and initiates a test interrupt to determine
8334  * if the interrupt is received via the ipr_test_intr() service routine.
8335  * If the tests fails, the driver will fall back to LSI.
8336  *
8337  * Return value:
8338  *      0 on success / non-zero on failure
8339  **/
8340 static int __devinit ipr_test_msi(struct ipr_ioa_cfg *ioa_cfg,
8341                                   struct pci_dev *pdev)
8342 {
8343         int rc;
8344         volatile u32 int_reg;
8345         unsigned long lock_flags = 0;
8346
8347         ENTER;
8348
8349         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8350         init_waitqueue_head(&ioa_cfg->msi_wait_q);
8351         ioa_cfg->msi_received = 0;
8352         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
8353         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.clr_interrupt_mask_reg32);
8354         int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
8355         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8356
8357         rc = request_irq(pdev->irq, ipr_test_intr, 0, IPR_NAME, ioa_cfg);
8358         if (rc) {
8359                 dev_err(&pdev->dev, "Can not assign irq %d\n", pdev->irq);
8360                 return rc;
8361         } else if (ipr_debug)
8362                 dev_info(&pdev->dev, "IRQ assigned: %d\n", pdev->irq);
8363
8364         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.sense_interrupt_reg32);
8365         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
8366         wait_event_timeout(ioa_cfg->msi_wait_q, ioa_cfg->msi_received, HZ);
8367         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
8368
8369         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8370         if (!ioa_cfg->msi_received) {
8371                 /* MSI test failed */
8372                 dev_info(&pdev->dev, "MSI test failed.  Falling back to LSI.\n");
8373                 rc = -EOPNOTSUPP;
8374         } else if (ipr_debug)
8375                 dev_info(&pdev->dev, "MSI test succeeded.\n");
8376
8377         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8378
8379         free_irq(pdev->irq, ioa_cfg);
8380
8381         LEAVE;
8382
8383         return rc;
8384 }
8385
8386 /**
8387  * ipr_probe_ioa - Allocates memory and does first stage of initialization
8388  * @pdev:               PCI device struct
8389  * @dev_id:             PCI device id struct
8390  *
8391  * Return value:
8392  *      0 on success / non-zero on failure
8393  **/
8394 static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
8395                                    const struct pci_device_id *dev_id)
8396 {
8397         struct ipr_ioa_cfg *ioa_cfg;
8398         struct Scsi_Host *host;
8399         unsigned long ipr_regs_pci;
8400         void __iomem *ipr_regs;
8401         int rc = PCIBIOS_SUCCESSFUL;
8402         volatile u32 mask, uproc, interrupts;
8403
8404         ENTER;
8405
8406         if ((rc = pci_enable_device(pdev))) {
8407                 dev_err(&pdev->dev, "Cannot enable adapter\n");
8408                 goto out;
8409         }
8410
8411         dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
8412
8413         host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
8414
8415         if (!host) {
8416                 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
8417                 rc = -ENOMEM;
8418                 goto out_disable;
8419         }
8420
8421         ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
8422         memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
8423         ata_host_init(&ioa_cfg->ata_host, &pdev->dev,
8424                       sata_port_info.flags, &ipr_sata_ops);
8425
8426         ioa_cfg->ipr_chip = ipr_get_chip_info(dev_id);
8427
8428         if (!ioa_cfg->ipr_chip) {
8429                 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
8430                         dev_id->vendor, dev_id->device);
8431                 goto out_scsi_host_put;
8432         }
8433
8434         /* set SIS 32 or SIS 64 */
8435         ioa_cfg->sis64 = ioa_cfg->ipr_chip->sis_type == IPR_SIS64 ? 1 : 0;
8436         ioa_cfg->chip_cfg = ioa_cfg->ipr_chip->cfg;
8437
8438         if (ipr_transop_timeout)
8439                 ioa_cfg->transop_timeout = ipr_transop_timeout;
8440         else if (dev_id->driver_data & IPR_USE_LONG_TRANSOP_TIMEOUT)
8441                 ioa_cfg->transop_timeout = IPR_LONG_OPERATIONAL_TIMEOUT;
8442         else
8443                 ioa_cfg->transop_timeout = IPR_OPERATIONAL_TIMEOUT;
8444
8445         ioa_cfg->revid = pdev->revision;
8446
8447         ipr_regs_pci = pci_resource_start(pdev, 0);
8448
8449         rc = pci_request_regions(pdev, IPR_NAME);
8450         if (rc < 0) {
8451                 dev_err(&pdev->dev,
8452                         "Couldn't register memory range of registers\n");
8453                 goto out_scsi_host_put;
8454         }
8455
8456         ipr_regs = pci_ioremap_bar(pdev, 0);
8457
8458         if (!ipr_regs) {
8459                 dev_err(&pdev->dev,
8460                         "Couldn't map memory range of registers\n");
8461                 rc = -ENOMEM;
8462                 goto out_release_regions;
8463         }
8464
8465         ioa_cfg->hdw_dma_regs = ipr_regs;
8466         ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
8467         ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
8468
8469         ipr_init_ioa_cfg(ioa_cfg, host, pdev);
8470
8471         pci_set_master(pdev);
8472
8473         if (ioa_cfg->sis64) {
8474                 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
8475                 if (rc < 0) {
8476                         dev_dbg(&pdev->dev, "Failed to set 64 bit PCI DMA mask\n");
8477                         rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8478                 }
8479
8480         } else
8481                 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8482
8483         if (rc < 0) {
8484                 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
8485                 goto cleanup_nomem;
8486         }
8487
8488         rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
8489                                    ioa_cfg->chip_cfg->cache_line_size);
8490
8491         if (rc != PCIBIOS_SUCCESSFUL) {
8492                 dev_err(&pdev->dev, "Write of cache line size failed\n");
8493                 rc = -EIO;
8494                 goto cleanup_nomem;
8495         }
8496
8497         /* Enable MSI style interrupts if they are supported. */
8498         if (ioa_cfg->ipr_chip->intr_type == IPR_USE_MSI && !pci_enable_msi(pdev)) {
8499                 rc = ipr_test_msi(ioa_cfg, pdev);
8500                 if (rc == -EOPNOTSUPP)
8501                         pci_disable_msi(pdev);
8502                 else if (rc)
8503                         goto out_msi_disable;
8504                 else
8505                         dev_info(&pdev->dev, "MSI enabled with IRQ: %d\n", pdev->irq);
8506         } else if (ipr_debug)
8507                 dev_info(&pdev->dev, "Cannot enable MSI.\n");
8508
8509         /* Save away PCI config space for use following IOA reset */
8510         rc = pci_save_state(pdev);
8511
8512         if (rc != PCIBIOS_SUCCESSFUL) {
8513                 dev_err(&pdev->dev, "Failed to save PCI config space\n");
8514                 rc = -EIO;
8515                 goto cleanup_nomem;
8516         }
8517
8518         if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
8519                 goto cleanup_nomem;
8520
8521         if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
8522                 goto cleanup_nomem;
8523
8524         if (ioa_cfg->sis64)
8525                 ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr64)
8526                                 + ((sizeof(struct ipr_config_table_entry64)
8527                                 * ioa_cfg->max_devs_supported)));
8528         else
8529                 ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr)
8530                                 + ((sizeof(struct ipr_config_table_entry)
8531                                 * ioa_cfg->max_devs_supported)));
8532
8533         rc = ipr_alloc_mem(ioa_cfg);
8534         if (rc < 0) {
8535                 dev_err(&pdev->dev,
8536                         "Couldn't allocate enough memory for device driver!\n");
8537                 goto cleanup_nomem;
8538         }
8539
8540         /*
8541          * If HRRQ updated interrupt is not masked, or reset alert is set,
8542          * the card is in an unknown state and needs a hard reset
8543          */
8544         mask = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
8545         interrupts = readl(ioa_cfg->regs.sense_interrupt_reg32);
8546         uproc = readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
8547         if ((mask & IPR_PCII_HRRQ_UPDATED) == 0 || (uproc & IPR_UPROCI_RESET_ALERT))
8548                 ioa_cfg->needs_hard_reset = 1;
8549         if (interrupts & IPR_PCII_ERROR_INTERRUPTS)
8550                 ioa_cfg->needs_hard_reset = 1;
8551         if (interrupts & IPR_PCII_IOA_UNIT_CHECKED)
8552                 ioa_cfg->ioa_unit_checked = 1;
8553
8554         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
8555         rc = request_irq(pdev->irq, ipr_isr,
8556                          ioa_cfg->msi_received ? 0 : IRQF_SHARED,
8557                          IPR_NAME, ioa_cfg);
8558
8559         if (rc) {
8560                 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
8561                         pdev->irq, rc);
8562                 goto cleanup_nolog;
8563         }
8564
8565         if ((dev_id->driver_data & IPR_USE_PCI_WARM_RESET) ||
8566             (dev_id->device == PCI_DEVICE_ID_IBM_OBSIDIAN_E && !ioa_cfg->revid)) {
8567                 ioa_cfg->needs_warm_reset = 1;
8568                 ioa_cfg->reset = ipr_reset_slot_reset;
8569         } else
8570                 ioa_cfg->reset = ipr_reset_start_bist;
8571
8572         spin_lock(&ipr_driver_lock);
8573         list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
8574         spin_unlock(&ipr_driver_lock);
8575
8576         LEAVE;
8577 out:
8578         return rc;
8579
8580 cleanup_nolog:
8581         ipr_free_mem(ioa_cfg);
8582 cleanup_nomem:
8583         iounmap(ipr_regs);
8584 out_msi_disable:
8585         pci_disable_msi(pdev);
8586 out_release_regions:
8587         pci_release_regions(pdev);
8588 out_scsi_host_put:
8589         scsi_host_put(host);
8590 out_disable:
8591         pci_disable_device(pdev);
8592         goto out;
8593 }
8594
8595 /**
8596  * ipr_scan_vsets - Scans for VSET devices
8597  * @ioa_cfg:    ioa config struct
8598  *
8599  * Description: Since the VSET resources do not follow SAM in that we can have
8600  * sparse LUNs with no LUN 0, we have to scan for these ourselves.
8601  *
8602  * Return value:
8603  *      none
8604  **/
8605 static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
8606 {
8607         int target, lun;
8608
8609         for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
8610                 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
8611                         scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
8612 }
8613
8614 /**
8615  * ipr_initiate_ioa_bringdown - Bring down an adapter
8616  * @ioa_cfg:            ioa config struct
8617  * @shutdown_type:      shutdown type
8618  *
8619  * Description: This function will initiate bringing down the adapter.
8620  * This consists of issuing an IOA shutdown to the adapter
8621  * to flush the cache, and running BIST.
8622  * If the caller needs to wait on the completion of the reset,
8623  * the caller must sleep on the reset_wait_q.
8624  *
8625  * Return value:
8626  *      none
8627  **/
8628 static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
8629                                        enum ipr_shutdown_type shutdown_type)
8630 {
8631         ENTER;
8632         if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
8633                 ioa_cfg->sdt_state = ABORT_DUMP;
8634         ioa_cfg->reset_retries = 0;
8635         ioa_cfg->in_ioa_bringdown = 1;
8636         ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
8637         LEAVE;
8638 }
8639
8640 /**
8641  * __ipr_remove - Remove a single adapter
8642  * @pdev:       pci device struct
8643  *
8644  * Adapter hot plug remove entry point.
8645  *
8646  * Return value:
8647  *      none
8648  **/
8649 static void __ipr_remove(struct pci_dev *pdev)
8650 {
8651         unsigned long host_lock_flags = 0;
8652         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8653         ENTER;
8654
8655         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
8656         while(ioa_cfg->in_reset_reload) {
8657                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
8658                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
8659                 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
8660         }
8661
8662         ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
8663
8664         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
8665         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
8666         flush_scheduled_work();
8667         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
8668
8669         spin_lock(&ipr_driver_lock);
8670         list_del(&ioa_cfg->queue);
8671         spin_unlock(&ipr_driver_lock);
8672
8673         if (ioa_cfg->sdt_state == ABORT_DUMP)
8674                 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
8675         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
8676
8677         ipr_free_all_resources(ioa_cfg);
8678
8679         LEAVE;
8680 }
8681
8682 /**
8683  * ipr_remove - IOA hot plug remove entry point
8684  * @pdev:       pci device struct
8685  *
8686  * Adapter hot plug remove entry point.
8687  *
8688  * Return value:
8689  *      none
8690  **/
8691 static void __devexit ipr_remove(struct pci_dev *pdev)
8692 {
8693         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8694
8695         ENTER;
8696
8697         ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
8698                               &ipr_trace_attr);
8699         ipr_remove_dump_file(&ioa_cfg->host->shost_dev.kobj,
8700                              &ipr_dump_attr);
8701         scsi_remove_host(ioa_cfg->host);
8702
8703         __ipr_remove(pdev);
8704
8705         LEAVE;
8706 }
8707
8708 /**
8709  * ipr_probe - Adapter hot plug add entry point
8710  *
8711  * Return value:
8712  *      0 on success / non-zero on failure
8713  **/
8714 static int __devinit ipr_probe(struct pci_dev *pdev,
8715                                const struct pci_device_id *dev_id)
8716 {
8717         struct ipr_ioa_cfg *ioa_cfg;
8718         int rc;
8719
8720         rc = ipr_probe_ioa(pdev, dev_id);
8721
8722         if (rc)
8723                 return rc;
8724
8725         ioa_cfg = pci_get_drvdata(pdev);
8726         rc = ipr_probe_ioa_part2(ioa_cfg);
8727
8728         if (rc) {
8729                 __ipr_remove(pdev);
8730                 return rc;
8731         }
8732
8733         rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
8734
8735         if (rc) {
8736                 __ipr_remove(pdev);
8737                 return rc;
8738         }
8739
8740         rc = ipr_create_trace_file(&ioa_cfg->host->shost_dev.kobj,
8741                                    &ipr_trace_attr);
8742
8743         if (rc) {
8744                 scsi_remove_host(ioa_cfg->host);
8745                 __ipr_remove(pdev);
8746                 return rc;
8747         }
8748
8749         rc = ipr_create_dump_file(&ioa_cfg->host->shost_dev.kobj,
8750                                    &ipr_dump_attr);
8751
8752         if (rc) {
8753                 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
8754                                       &ipr_trace_attr);
8755                 scsi_remove_host(ioa_cfg->host);
8756                 __ipr_remove(pdev);
8757                 return rc;
8758         }
8759
8760         scsi_scan_host(ioa_cfg->host);
8761         ipr_scan_vsets(ioa_cfg);
8762         scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
8763         ioa_cfg->allow_ml_add_del = 1;
8764         ioa_cfg->host->max_channel = IPR_VSET_BUS;
8765         schedule_work(&ioa_cfg->work_q);
8766         return 0;
8767 }
8768
8769 /**
8770  * ipr_shutdown - Shutdown handler.
8771  * @pdev:       pci device struct
8772  *
8773  * This function is invoked upon system shutdown/reboot. It will issue
8774  * an adapter shutdown to the adapter to flush the write cache.
8775  *
8776  * Return value:
8777  *      none
8778  **/
8779 static void ipr_shutdown(struct pci_dev *pdev)
8780 {
8781         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8782         unsigned long lock_flags = 0;
8783
8784         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8785         while(ioa_cfg->in_reset_reload) {
8786                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8787                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
8788                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8789         }
8790
8791         ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
8792         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8793         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
8794 }
8795
8796 static struct pci_device_id ipr_pci_table[] __devinitdata = {
8797         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
8798                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702, 0, 0, 0 },
8799         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
8800                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703, 0, 0, 0 },
8801         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
8802                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D, 0, 0, 0 },
8803         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
8804                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E, 0, 0, 0 },
8805         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
8806                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B, 0, 0, 0 },
8807         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
8808                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E, 0, 0, 0 },
8809         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
8810                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A, 0, 0, 0 },
8811         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
8812                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575B, 0, 0,
8813                 IPR_USE_LONG_TRANSOP_TIMEOUT },
8814         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
8815               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
8816         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
8817               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
8818               IPR_USE_LONG_TRANSOP_TIMEOUT },
8819         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
8820               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
8821               IPR_USE_LONG_TRANSOP_TIMEOUT },
8822         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
8823               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
8824         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
8825               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
8826               IPR_USE_LONG_TRANSOP_TIMEOUT},
8827         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
8828               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
8829               IPR_USE_LONG_TRANSOP_TIMEOUT },
8830         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
8831               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574E, 0, 0,
8832               IPR_USE_LONG_TRANSOP_TIMEOUT },
8833         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
8834               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B3, 0, 0, 0 },
8835         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
8836               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B7, 0, 0,
8837               IPR_USE_LONG_TRANSOP_TIMEOUT | IPR_USE_PCI_WARM_RESET },
8838         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
8839                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780, 0, 0, 0 },
8840         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
8841                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E, 0, 0, 0 },
8842         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
8843                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571F, 0, 0,
8844                 IPR_USE_LONG_TRANSOP_TIMEOUT },
8845         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
8846                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572F, 0, 0,
8847                 IPR_USE_LONG_TRANSOP_TIMEOUT },
8848         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
8849                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B5, 0, 0, 0 },
8850         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
8851                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574D, 0, 0, 0 },
8852         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
8853                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B2, 0, 0, 0 },
8854         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_ASIC_E2,
8855                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B4, 0, 0, 0 },
8856         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_ASIC_E2,
8857                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B1, 0, 0, 0 },
8858         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_ASIC_E2,
8859                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C6, 0, 0, 0 },
8860         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_ASIC_E2,
8861                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575D, 0, 0, 0 },
8862         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_ASIC_E2,
8863                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57CE, 0, 0, 0 },
8864         { }
8865 };
8866 MODULE_DEVICE_TABLE(pci, ipr_pci_table);
8867
8868 static struct pci_error_handlers ipr_err_handler = {
8869         .error_detected = ipr_pci_error_detected,
8870         .slot_reset = ipr_pci_slot_reset,
8871 };
8872
8873 static struct pci_driver ipr_driver = {
8874         .name = IPR_NAME,
8875         .id_table = ipr_pci_table,
8876         .probe = ipr_probe,
8877         .remove = __devexit_p(ipr_remove),
8878         .shutdown = ipr_shutdown,
8879         .err_handler = &ipr_err_handler,
8880 };
8881
8882 /**
8883  * ipr_halt_done - Shutdown prepare completion
8884  *
8885  * Return value:
8886  *      none
8887  **/
8888 static void ipr_halt_done(struct ipr_cmnd *ipr_cmd)
8889 {
8890         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8891
8892         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
8893 }
8894
8895 /**
8896  * ipr_halt - Issue shutdown prepare to all adapters
8897  *
8898  * Return value:
8899  *      NOTIFY_OK on success / NOTIFY_DONE on failure
8900  **/
8901 static int ipr_halt(struct notifier_block *nb, ulong event, void *buf)
8902 {
8903         struct ipr_cmnd *ipr_cmd;
8904         struct ipr_ioa_cfg *ioa_cfg;
8905         unsigned long flags = 0;
8906
8907         if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
8908                 return NOTIFY_DONE;
8909
8910         spin_lock(&ipr_driver_lock);
8911
8912         list_for_each_entry(ioa_cfg, &ipr_ioa_head, queue) {
8913                 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
8914                 if (!ioa_cfg->allow_cmds) {
8915                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8916                         continue;
8917                 }
8918
8919                 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
8920                 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
8921                 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
8922                 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
8923                 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;
8924
8925                 ipr_do_req(ipr_cmd, ipr_halt_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
8926                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8927         }
8928         spin_unlock(&ipr_driver_lock);
8929
8930         return NOTIFY_OK;
8931 }
8932
8933 static struct notifier_block ipr_notifier = {
8934         ipr_halt, NULL, 0
8935 };
8936
8937 /**
8938  * ipr_init - Module entry point
8939  *
8940  * Return value:
8941  *      0 on success / negative value on failure
8942  **/
8943 static int __init ipr_init(void)
8944 {
8945         ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
8946                  IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
8947
8948         register_reboot_notifier(&ipr_notifier);
8949         return pci_register_driver(&ipr_driver);
8950 }
8951
8952 /**
8953  * ipr_exit - Module unload
8954  *
8955  * Module unload entry point.
8956  *
8957  * Return value:
8958  *      none
8959  **/
8960 static void __exit ipr_exit(void)
8961 {
8962         unregister_reboot_notifier(&ipr_notifier);
8963         pci_unregister_driver(&ipr_driver);
8964 }
8965
8966 module_init(ipr_init);
8967 module_exit(ipr_exit);